@@ -219,6 +219,24 @@ void ModuleInternal::CheckFileExists(napi_env env, const std::string& path, cons
219
219
jEnv.CallStaticObjectMethod (MODULE_CLASS, RESOLVE_PATH_METHOD_ID, (jstring) jsModulename, (jstring) jsBaseDir);
220
220
}
221
221
222
+ napi_value ModuleInternal::LoadInternalModule (napi_env env, const std::string& moduleName) {
223
+ if (moduleName == " url" ) {
224
+ napi_value moduleObj;
225
+ napi_create_object (env, &moduleObj);
226
+ napi_value url;
227
+ napi_value exports;
228
+ napi_create_object (env, &exports);
229
+ napi_get_named_property (env, napi_util::global (env), " URL" , &url);
230
+ napi_set_named_property (env, exports, " URL" , url);
231
+ napi_set_named_property (env, moduleObj, " exports" , exports);
232
+ napi_util::napi_set_function (env, exports, " pathToFileURL" , [](napi_env env, napi_callback_info info) -> napi_value {
233
+ return ArgConverter::convertToJsString (env, " file://" );
234
+ });
235
+ return moduleObj;
236
+ }
237
+ return nullptr ;
238
+ }
239
+
222
240
napi_value ModuleInternal::LoadImpl (napi_env env, const std::string& moduleName, const std::string& baseDir, bool & isData) {
223
241
auto pathKind = GetModulePathKind (moduleName);
224
242
auto cachePathKey = (pathKind == ModulePathKind::Global) ? moduleName : (baseDir + " *" + moduleName);
@@ -229,6 +247,13 @@ napi_value ModuleInternal::LoadImpl(napi_env env, const std::string& moduleName,
229
247
230
248
auto it = m_loadedModules.find (cachePathKey);
231
249
250
+ /* *
251
+ * Load internal modules like url,fs etc directly if someone does
252
+ * require('url');
253
+ */
254
+ napi_value moduleObj = ModuleInternal::LoadInternalModule (env, moduleName);
255
+ if (moduleObj) return moduleObj;
256
+
232
257
if (it == m_loadedModules.end ()) {
233
258
std::string path;
234
259
@@ -240,6 +265,10 @@ napi_value ModuleInternal::LoadImpl(napi_env env, const std::string& moduleName,
240
265
path.replace (pos, sys_lib.length (), " " );
241
266
} else if (Util::EndsWith (moduleName, " .so" )) {
242
267
path = " lib" + moduleName;
268
+ } else if (Util::EndsWith (moduleName, " .node" )) {
269
+ std::string libName = moduleName;
270
+ Util::ReplaceAll (libName, " .node" , " " );
271
+ path = " lib" + libName + " .so" ;
243
272
} else {
244
273
JEnv jenv;
245
274
JniLocalRef jsModulename (jenv.NewStringUTF (moduleName.c_str ()));
0 commit comments