@@ -476,17 +476,6 @@ std::string ReadFile(uv_file file) {
476476 return contents;
477477}
478478
479- Maybe<std::string> RealPath (const std::string& path) {
480- uv_fs_t fs_req;
481- const int r = uv_fs_realpath (nullptr , &fs_req, path.c_str (), nullptr );
482- if (r < 0 ) {
483- return Nothing<std::string>();
484- }
485- std::string link_path = std::string (static_cast <const char *>(fs_req.ptr ));
486- uv_fs_req_cleanup (&fs_req);
487- return Just (link_path);
488- }
489-
490479enum DescriptorType {
491480 FILE,
492481 DIRECTORY,
@@ -895,21 +884,11 @@ Maybe<URL> Resolve(Environment* env,
895884 return FinalizeResolution (env, resolved, base);
896885}
897886
898- Maybe<PackageType::Type> GetPackageType (Environment* env,
899- const URL& resolved) {
900- Maybe<const PackageConfig*> pcfg =
901- GetPackageScopeConfig (env, resolved, resolved);
902- if (pcfg.IsNothing ()) {
903- return Nothing<PackageType::Type>();
904- }
905- return Just (pcfg.FromJust ()->type );
906- }
907-
908887void ModuleWrap::Resolve (const FunctionCallbackInfo<Value>& args) {
909888 Environment* env = Environment::GetCurrent (args);
910889
911- // module.resolve(specifier, url, realpath )
912- CHECK_EQ (args.Length (), 3 );
890+ // module.resolve(specifier, url)
891+ CHECK_EQ (args.Length (), 2 );
913892
914893 CHECK (args[0 ]->IsString ());
915894 Utf8Value specifier_utf8 (env->isolate (), args[0 ]);
@@ -919,9 +898,6 @@ void ModuleWrap::Resolve(const FunctionCallbackInfo<Value>& args) {
919898 Utf8Value url_utf8 (env->isolate (), args[1 ]);
920899 URL url (*url_utf8, url_utf8.length ());
921900
922- CHECK (args[2 ]->IsBoolean ());
923- bool realpath = args[2 ]->IsTrue ();
924-
925901 if (url.flags () & URL_FLAGS_FAILED) {
926902 return node::THROW_ERR_INVALID_ARG_TYPE (
927903 env, " second argument is not a URL string" );
@@ -942,47 +918,27 @@ void ModuleWrap::Resolve(const FunctionCallbackInfo<Value>& args) {
942918 URL resolution = result.FromJust ();
943919 CHECK (!(resolution.flags () & URL_FLAGS_FAILED));
944920
945- if (realpath) {
946- Maybe<std::string> real_resolution =
947- node::loader::RealPath (resolution.ToFilePath ());
948- // Note: Ensure module resolve FS error handling consistency
949- if (real_resolution.IsNothing ()) {
950- std::string msg = " realpath error resolving " + resolution.ToFilePath ();
951- env->ThrowError (msg.c_str ());
952- try_catch.ReThrow ();
953- return ;
954- }
955- std::string fragment = resolution.fragment ();
956- std::string query = resolution.query ();
957- resolution = URL::FromFilePath (real_resolution.FromJust ());
958- resolution.set_fragment (fragment);
959- resolution.set_query (query);
960- }
921+ args.GetReturnValue ().Set (resolution.ToObject (env).ToLocalChecked ());
922+ }
961923
962- Maybe<PackageType::Type> pkg_type =
963- node::loader::GetPackageType (env, resolution);
964- if (result.IsNothing ()) {
965- CHECK (try_catch.HasCaught ());
966- try_catch.ReThrow ();
967- return ;
968- }
969- CHECK (!try_catch.HasCaught ());
924+ void ModuleWrap::GetPackageType (const FunctionCallbackInfo<Value>& args) {
925+ Environment* env = Environment::GetCurrent (args);
970926
971- Local<Object> resolved = Object::New (env->isolate ());
927+ // module.getPackageType(url)
928+ CHECK_EQ (args.Length (), 1 );
972929
973- resolved->DefineOwnProperty (
974- env->context (),
975- env->type_string (),
976- Integer::New (env->isolate (), pkg_type.FromJust ()),
977- v8::ReadOnly).FromJust ();
930+ CHECK (args[0 ]->IsString ());
931+ Utf8Value url_utf8 (env->isolate (), args[0 ]);
932+ URL url (*url_utf8, url_utf8.length ());
978933
979- resolved->DefineOwnProperty (
980- env->context (),
981- env->url_string (),
982- resolution.ToObject (env).ToLocalChecked (),
983- v8::ReadOnly).FromJust ();
934+ PackageType::Type pkg_type = PackageType::None;
935+ Maybe<const PackageConfig*> pcfg =
936+ GetPackageScopeConfig (env, url, url);
937+ if (!pcfg.IsNothing ()) {
938+ pkg_type = pcfg.FromJust ()->type ;
939+ }
984940
985- args.GetReturnValue ().Set (resolved );
941+ args.GetReturnValue ().Set (Integer::New (env-> isolate (), pkg_type) );
986942}
987943
988944static MaybeLocal<Promise> ImportModuleDynamically (
@@ -1118,6 +1074,7 @@ void ModuleWrap::Initialize(Local<Object> target,
11181074 target->Set (env->context (), FIXED_ONE_BYTE_STRING (isolate, " ModuleWrap" ),
11191075 tpl->GetFunction (context).ToLocalChecked ()).FromJust ();
11201076 env->SetMethod (target, " resolve" , Resolve);
1077+ env->SetMethod (target, " getPackageType" , GetPackageType);
11211078 env->SetMethod (target,
11221079 " setImportModuleDynamicallyCallback" ,
11231080 SetImportModuleDynamicallyCallback);
0 commit comments