@@ -174,6 +174,64 @@ MaybeLocal<Function> NativeModuleLoader::CompileAsModule(
174174 return LookupAndCompile (context, id, ¶meters, result);
175175}
176176
177+ #ifdef NODE_BUILTIN_MODULES_PATH
178+ static std::string OnDiskFileName (const char * id) {
179+ std::string filename = NODE_BUILTIN_MODULES_PATH;
180+ filename += " /" ;
181+
182+ if (strncmp (id, " internal/deps" , strlen (" internal/deps" )) == 0 ) {
183+ id += strlen (" internal/" );
184+ } else {
185+ filename += " lib/" ;
186+ }
187+ filename += id;
188+ filename += " .js" ;
189+
190+ return filename;
191+ }
192+ #endif // NODE_BUILTIN_MODULES_PATH
193+
194+ MaybeLocal<String> NativeModuleLoader::LoadBuiltinModuleSource (Isolate* isolate,
195+ const char * id) {
196+ #ifdef NODE_BUILTIN_MODULES_PATH
197+ std::string filename = OnDiskFileName (id);
198+
199+ uv_fs_t req;
200+ uv_file file =
201+ uv_fs_open (nullptr , &req, filename.c_str (), O_RDONLY, 0 , nullptr );
202+ CHECK_GE (req.result , 0 );
203+ uv_fs_req_cleanup (&req);
204+
205+ std::shared_ptr<void > defer_close (nullptr , [file](...) {
206+ uv_fs_t close_req;
207+ CHECK_EQ (0 , uv_fs_close (nullptr , &close_req, file, nullptr ));
208+ uv_fs_req_cleanup (&close_req);
209+ });
210+
211+ std::string contents;
212+ char buffer[4096 ];
213+ uv_buf_t buf = uv_buf_init (buffer, sizeof (buffer));
214+
215+ while (true ) {
216+ const int r =
217+ uv_fs_read (nullptr , &req, file, &buf, 1 , contents.length (), nullptr );
218+ CHECK_GE (req.result , 0 );
219+ uv_fs_req_cleanup (&req);
220+ if (r <= 0 ) {
221+ break ;
222+ }
223+ contents.append (buf.base , r);
224+ }
225+
226+ return String::NewFromUtf8 (
227+ isolate, contents.c_str (), v8::NewStringType::kNormal , contents.length ());
228+ #else
229+ const auto source_it = source_.find (id);
230+ CHECK_NE (source_it, source_.end ());
231+ return source_it->second .ToStringChecked (isolate);
232+ #endif // NODE_BUILTIN_MODULES_PATH
233+ }
234+
177235// Returns Local<Function> of the compiled module if return_code_cache
178236// is false (we are only compiling the function).
179237// Otherwise return a Local<Object> containing the cache.
@@ -185,9 +243,10 @@ MaybeLocal<Function> NativeModuleLoader::LookupAndCompile(
185243 Isolate* isolate = context->GetIsolate ();
186244 EscapableHandleScope scope (isolate);
187245
188- const auto source_it = source_.find (id);
189- CHECK_NE (source_it, source_.end ());
190- Local<String> source = source_it->second .ToStringChecked (isolate);
246+ Local<String> source;
247+ if (!LoadBuiltinModuleSource (isolate, id).ToLocal (&source)) {
248+ return {};
249+ }
191250
192251 std::string filename_s = id + std::string (" .js" );
193252 Local<String> filename =
0 commit comments