@@ -2737,16 +2737,15 @@ static bool FileURLToPath(
27372737 // NOLINTNEXTLINE(runtime/references)
27382738 std::string& result_file_path) {
27392739 if (file_url.type != ada::scheme::FILE) {
2740- env->isolate ()->ThrowException (ERR_INVALID_URL_SCHEME (env->isolate ()));
2741-
2740+ THROW_ERR_INVALID_URL_SCHEME (env->isolate ());
27422741 return false ;
27432742 }
27442743
27452744 std::string_view pathname = file_url.get_pathname ();
27462745#ifdef _WIN32
2747- size_t first_percent = std::string::npos;
27482746 size_t pathname_size = pathname.size ();
2749- std::string pathname_escaped_slash;
2747+ std::string pathname_escaped_slash{};
2748+ pathname_escaped_slash.reserve (pathname_size);
27502749
27512750 for (size_t i = 0 ; i < pathname_size; i++) {
27522751 if (pathname[i] == ' /' ) {
@@ -2755,44 +2754,38 @@ static bool FileURLToPath(
27552754 pathname_escaped_slash += pathname[i];
27562755 }
27572756
2758- if (pathname[i] != ' % ' ) continue ;
2759-
2760- if (first_percent == std::string::npos ) {
2761- first_percent = i ;
2757+ // skip all non-percentage characters and skip
2758+ // last 2 characters
2759+ if (pathname[i] != ' % ' || (i + 2 ) >= pathname_size ) {
2760+ continue ;
27622761 }
27632762
2764- // just safe-guard against access the pathname
2765- // outside the bounds
2766- if ((i + 2 ) >= pathname_size) continue ;
2767-
27682763 char third = pathname[i + 2 ] | 0x20 ;
2769-
27702764 bool is_slash = pathname[i + 1 ] == ' 2' && third == 102 ;
27712765 bool is_forward_slash = pathname[i + 1 ] == ' 5' && third == 99 ;
27722766
2773- if (!is_slash && !is_forward_slash) continue ;
2774-
2775- env->isolate ()->ThrowException (ERR_INVALID_FILE_URL_PATH (
2767+ if (is_slash || is_forward_slash) {
2768+ THROW_ERR_INVALID_FILE_URL_PATH (
27762769 env->isolate (),
27772770 " File URL path must not include encoded \\ or / characters" ));
27782771
2779- return false ;
2772+ return false ;
2773+ }
27802774 }
27812775
2782- std::string_view hostname = file_url.get_hostname ();
2783- std::string decoded_pathname = ada::unicode::percent_decode (
2784- std::string_view (pathname_escaped_slash), first_percent);
2776+ std::string decoded_pathname =
2777+ ada::unicode::percent_decode (pathname_escaped_slash, pathname_escaped_slash.find (' %' ));
27852778
2786- if (hostname. size () > 0 ) {
2779+ if (!file_url. has_empty_hostname () ) {
27872780 // If hostname is set, then we have a UNC path
27882781 // Pass the hostname through domainToUnicode just in case
27892782 // it is an IDN using punycode encoding. We do not need to worry
27902783 // about percent encoding because the URL parser will have
27912784 // already taken care of that for us. Note that this only
27922785 // causes IDNs with an appropriate `xn--` prefix to be decoded.
2786+ auto hostname = file_url.get_hostname ();
27932787 result_file_path =
27942788 " \\\\ " + ada::unicode::to_unicode (hostname) + decoded_pathname;
2795-
27962789 return true ;
27972790 }
27982791
@@ -2801,42 +2794,33 @@ static bool FileURLToPath(
28012794
28022795 // a..z A..Z
28032796 if (letter < ' a' || letter > ' z' || sep != ' :' ) {
2804- env-> isolate ()-> ThrowException ( ERR_INVALID_FILE_URL_PATH (
2797+ THROW_ERR_INVALID_FILE_URL_PATH (
28052798 env->isolate (), " File URL path must be absolute" ));
2806-
28072799 return false ;
28082800 }
28092801
28102802 result_file_path = decoded_pathname.substr (1 );
28112803
28122804 return true ;
28132805#else // _WIN32
2814- std::string_view hostname = file_url.get_hostname ();
2815-
2816- if (hostname.size () > 0 ) {
2806+ if (!file_url.has_empty_hostname ()) {
28172807 std::string error_message =
2818- std::string ( " File URL host must be \" localhost\" or empty on " ) +
2808+ " File URL host must be \" localhost\" or empty on " +
28192809 std::string (per_process::metadata.platform );
2820- env->isolate ()->ThrowException (
2821- ERR_INVALID_FILE_URL_HOST (env->isolate (), error_message.c_str ()));
2822-
2810+ THROW_ERR_INVALID_FILE_URL_HOST (env->isolate (), error_message.c_str ());
28232811 return false ;
28242812 }
28252813
2826- size_t first_percent = std::string::npos;
2827- for (size_t i = 0 ; (i + 2 ) < pathname.size (); i++) {
2828- if (pathname[i] != ' %' ) continue ;
2814+ auto first_percent = pathname.find (' %' );
28292815
2830- if (first_percent == std::string::npos) {
2831- first_percent = i;
2832- }
2833-
2834- if (pathname[i + 1 ] == ' 2' && (pathname[i + 2 ] | 0x20 ) == 102 ) {
2835- env->isolate ()->ThrowException (ERR_INVALID_FILE_URL_PATH (
2836- env->isolate (),
2837- " File URL path must not include encoded / characters" ));
2838-
2839- return false ;
2816+ if (first_percent != std::string_view::npos) {
2817+ for (size_t i = first_percent; (i + 2 ) < pathname.size (); i++) {
2818+ if (pathname[i + 1 ] == ' 2' && (pathname[i + 2 ] | 0x20 ) == 102 ) {
2819+ THROW_ERR_INVALID_FILE_URL_PATH (
2820+ env->isolate (),
2821+ " File URL path must not include encoded / characters" );
2822+ return false ;
2823+ }
28402824 }
28412825 }
28422826
@@ -2898,28 +2882,23 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
28982882 ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView ());
28992883
29002884 if (!package_json_url) {
2901- env->isolate ()->ThrowException (
2902- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2903-
2885+ THROW_ERR_INVALID_URL (env->isolate (), " Invalid URL" );
29042886 return ;
29052887 }
29062888
29072889 ada::result<ada::url_aggregator> file_path_url;
29082890 std::string initial_file_path;
29092891 std::string file_path;
29102892
2911- if (args.Length () >= 2 && !args[1 ]->IsNullOrUndefined () &&
2912- args[1 ]->IsString ()) {
2893+ if (args.Length () >= 2 && args[1 ]->IsString ()) {
29132894 std::string package_config_main =
29142895 Utf8Value (env->isolate (), args[1 ].As <String>()).ToString ();
29152896
2916- file_path_url = ada::parse<ada::url_aggregator>(
2917- std::string ( " ./ " ) + package_config_main, &package_json_url.value ());
2897+ file_path_url = ada::parse<ada::url_aggregator>(" ./ " + package_config_main,
2898+ &package_json_url.value ());
29182899
29192900 if (!file_path_url) {
2920- env->isolate ()->ThrowException (
2921- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2922-
2901+ THROW_ERR_INVALID_URL (env->isolate (), " Invalid URL" );
29232902 return ;
29242903 }
29252904
@@ -2952,9 +2931,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29522931 ada::parse<ada::url_aggregator>(" ./index" , &package_json_url.value ());
29532932
29542933 if (!file_path_url) {
2955- env->isolate ()->ThrowException (
2956- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2957-
2934+ THROW_ERR_INVALID_URL (env->isolate (), " Invalid URL" );
29582935 return ;
29592936 }
29602937
@@ -2987,32 +2964,27 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29872964
29882965 if (!FileURLToPath (env, package_json_url.value (), module_path)) return ;
29892966
2990- if (args.Length () >= 3 && !args[2 ]->IsNullOrUndefined () &&
2991- args[2 ]->IsString ()) {
2967+ if (args.Length () >= 3 && args[2 ]->IsString ()) {
29922968 Utf8Value utf8_base_path (env->isolate (), args[2 ].As <String>());
29932969 auto base_url =
29942970 ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView ());
29952971
29962972 if (!base_url) {
2997- env->isolate ()->ThrowException (
2998- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2999-
2973+ THROW_ERR_INVALID_URL (env->isolate (), " Invalid URL" );
30002974 return ;
30012975 }
30022976
30032977 if (!FileURLToPath (env, base_url.value (), module_base)) return ;
3004- } else {
3005- std::string err_arg_message =
3006- " The \" base\" argument must be of type string or an instance of URL." ;
3007- env->isolate ()->ThrowException (
3008- ERR_INVALID_ARG_TYPE (env->isolate (), err_arg_message.c_str ()));
2978+
2979+ std::string err_module_message = " Cannot find package '" + module_path +
2980+ " ' imported from " + module_base;
2981+ THROW_ERR_MODULE_NOT_FOUND (env->isolate (), err_module_message.c_str ());
30092982 return ;
30102983 }
30112984
3012- std::string err_module_message =
3013- " Cannot find package '" + module_path + " ' imported from " + module_base;
3014- env->isolate ()->ThrowException (
3015- ERR_MODULE_NOT_FOUND (env->isolate (), err_module_message.c_str ()));
2985+ THROW_ERR_INVALID_ARG_TYPE (
2986+ env->isolate (),
2987+ " The \" base\" argument must be of type string or an instance of URL." );
30162988}
30172989
30182990void BindingData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments