@@ -844,13 +844,13 @@ void AfterOpenFileHandle(uv_fs_t* req) {
844
844
845
845
// Reverse the logic applied by path.toNamespacedPath() to create a
846
846
// namespace-prefixed path.
847
- void FromNamespacedPath (std::string* path) {
847
+ void FromNamespacedPath (std::string& path) { // NOLINT(runtime/references)
848
848
#ifdef _WIN32
849
- if (path-> compare (0 , 8 , " \\\\ ?\\ UNC\\ " , 8 ) == 0 ) {
850
- * path = path-> substr (8 );
851
- path-> insert (0 , " \\\\ " );
852
- } else if (path-> compare (0 , 4 , " \\\\ ?\\ " , 4 ) == 0 ) {
853
- * path = path-> substr (4 );
849
+ if (path. compare (0 , 8 , " \\\\ ?\\ UNC\\ " , 8 ) == 0 ) {
850
+ path = path. substr (8 );
851
+ path. insert (0 , " \\\\ " );
852
+ } else if (path. compare (0 , 4 , " \\\\ ?\\ " , 4 ) == 0 ) {
853
+ path = path. substr (4 );
854
854
}
855
855
#endif
856
856
}
@@ -864,7 +864,7 @@ void AfterMkdirp(uv_fs_t* req) {
864
864
std::string first_path (req_wrap->continuation_data ()->first_path ());
865
865
if (first_path.empty ())
866
866
return req_wrap->Resolve (Undefined (req_wrap->env ()->isolate ()));
867
- FromNamespacedPath (& first_path);
867
+ FromNamespacedPath (first_path);
868
868
Local<Value> path;
869
869
Local<Value> error;
870
870
if (!StringBytes::Encode (req_wrap->env ()->isolate (), first_path.c_str (),
@@ -1804,7 +1804,7 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
1804
1804
!req_wrap_sync.continuation_data ()->first_path ().empty ()) {
1805
1805
Local<Value> error;
1806
1806
std::string first_path (req_wrap_sync.continuation_data ()->first_path ());
1807
- FromNamespacedPath (& first_path);
1807
+ FromNamespacedPath (first_path);
1808
1808
MaybeLocal<Value> path = StringBytes::Encode (env->isolate (),
1809
1809
first_path.c_str (),
1810
1810
UTF8, &error);
@@ -2732,67 +2732,52 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
2732
2732
static bool FileURLToPath (
2733
2733
Environment* env,
2734
2734
const ada::url_aggregator& file_url,
2735
- /* The linter can't detect the assign for result_file_path
2736
- So we need to ignore since it suggest to put const */
2737
- // NOLINTNEXTLINE(runtime/references)
2738
- std::string& result_file_path) {
2735
+ std::string& result_file_path) { // NOLINT(runtime/references)
2739
2736
if (file_url.type != ada::scheme::FILE) {
2740
- env->isolate ()->ThrowException (ERR_INVALID_URL_SCHEME (env->isolate ()));
2741
-
2737
+ THROW_ERR_INVALID_URL_SCHEME (env);
2742
2738
return false ;
2743
2739
}
2744
2740
2745
2741
std::string_view pathname = file_url.get_pathname ();
2746
2742
#ifdef _WIN32
2747
- size_t first_percent = std::string::npos;
2748
2743
size_t pathname_size = pathname.size ();
2749
- std::string pathname_escaped_slash;
2744
+ std::string pathname_escaped_slash{};
2745
+ pathname_escaped_slash.reserve (pathname_size);
2750
2746
2751
2747
for (size_t i = 0 ; i < pathname_size; i++) {
2752
2748
if (pathname[i] == ' /' ) {
2753
2749
pathname_escaped_slash += ' \\ ' ;
2754
- } else {
2755
- pathname_escaped_slash += pathname[i];
2750
+ continue ;
2756
2751
}
2757
2752
2758
- if (pathname[i] != ' %' ) continue ;
2759
-
2760
- if (first_percent == std::string::npos) {
2761
- first_percent = i;
2762
- }
2763
-
2764
- // just safe-guard against access the pathname
2765
- // outside the bounds
2766
- if ((i + 2 ) >= pathname_size) continue ;
2767
-
2768
- char third = pathname[i + 2 ] | 0x20 ;
2769
-
2770
- bool is_slash = pathname[i + 1 ] == ' 2' && third == 102 ;
2771
- bool is_forward_slash = pathname[i + 1 ] == ' 5' && third == 99 ;
2753
+ pathname_escaped_slash += pathname[i];
2772
2754
2773
- if (!is_slash && !is_forward_slash) continue ;
2755
+ if (pathname[i] == ' %' && (i + 2 ) <= pathname_size) {
2756
+ char third = pathname[i + 2 ] | 0x20 ;
2757
+ bool is_slash = pathname[i + 1 ] == ' 2' && third == 102 ;
2758
+ bool is_forward_slash = pathname[i + 1 ] == ' 5' && third == 99 ;
2774
2759
2775
- env->isolate ()->ThrowException (ERR_INVALID_FILE_URL_PATH (
2776
- env->isolate (),
2777
- " File URL path must not include encoded \\ or / characters" ));
2778
-
2779
- return false ;
2760
+ if (is_slash || is_forward_slash) {
2761
+ THROW_ERR_INVALID_FILE_URL_PATH (
2762
+ env, " File URL path must not include encoded \\ or / characters" );
2763
+ return false ;
2764
+ }
2765
+ }
2780
2766
}
2781
2767
2782
- std::string_view hostname = file_url.get_hostname ();
2783
2768
std::string decoded_pathname = ada::unicode::percent_decode (
2784
- std::string_view ( pathname_escaped_slash), first_percent );
2769
+ pathname_escaped_slash, pathname_escaped_slash. find ( ' % ' ) );
2785
2770
2786
- if (hostname. size () > 0 ) {
2771
+ if (!file_url. has_empty_hostname () ) {
2787
2772
// If hostname is set, then we have a UNC path
2788
2773
// Pass the hostname through domainToUnicode just in case
2789
2774
// it is an IDN using punycode encoding. We do not need to worry
2790
2775
// about percent encoding because the URL parser will have
2791
2776
// already taken care of that for us. Note that this only
2792
2777
// causes IDNs with an appropriate `xn--` prefix to be decoded.
2793
- result_file_path =
2794
- " \\\\ " + ada::unicode::to_unicode (hostname) + decoded_pathname;
2795
-
2778
+ result_file_path = " \\\\ " +
2779
+ ada::unicode::to_unicode (file_url. get_hostname ()) +
2780
+ decoded_pathname;
2796
2781
return true ;
2797
2782
}
2798
2783
@@ -2801,42 +2786,31 @@ static bool FileURLToPath(
2801
2786
2802
2787
// a..z A..Z
2803
2788
if (letter < ' a' || letter > ' z' || sep != ' :' ) {
2804
- env->isolate ()->ThrowException (ERR_INVALID_FILE_URL_PATH (
2805
- env->isolate (), " File URL path must be absolute" ));
2806
-
2789
+ THROW_ERR_INVALID_FILE_URL_PATH (env, " File URL path must be absolute" );
2807
2790
return false ;
2808
2791
}
2809
2792
2810
2793
result_file_path = decoded_pathname.substr (1 );
2811
2794
2812
2795
return true ;
2813
2796
#else // _WIN32
2814
- std::string_view hostname = file_url.get_hostname ();
2815
-
2816
- if (hostname.size () > 0 ) {
2797
+ if (!file_url.has_empty_hostname ()) {
2817
2798
std::string error_message =
2818
- std::string ( " File URL host must be \" localhost\" or empty on " ) +
2799
+ " File URL host must be \" localhost\" or empty on " +
2819
2800
std::string (per_process::metadata.platform );
2820
- env->isolate ()->ThrowException (
2821
- ERR_INVALID_FILE_URL_HOST (env->isolate (), error_message.c_str ()));
2822
-
2801
+ THROW_ERR_INVALID_FILE_URL_HOST (env, error_message.c_str ());
2823
2802
return false ;
2824
2803
}
2825
2804
2826
- size_t first_percent = std::string::npos;
2827
- for (size_t i = 0 ; (i + 2 ) < pathname.size (); i++) {
2828
- if (pathname[i] != ' %' ) continue ;
2829
-
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" ));
2805
+ auto first_percent = pathname.find (' %' );
2838
2806
2839
- return false ;
2807
+ if (first_percent != std::string_view::npos) {
2808
+ for (size_t i = first_percent; (i + 2 ) < pathname.size (); i++) {
2809
+ if (pathname[i + 1 ] == ' 2' && (pathname[i + 2 ] | 0x20 ) == 102 ) {
2810
+ THROW_ERR_INVALID_FILE_URL_PATH (
2811
+ env, " File URL path must not include encoded / characters" );
2812
+ return false ;
2813
+ }
2840
2814
}
2841
2815
}
2842
2816
@@ -2847,16 +2821,16 @@ static bool FileURLToPath(
2847
2821
}
2848
2822
2849
2823
BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile (
2850
- Environment* env, const std::string& file_path) {
2824
+ Environment* env, const std::string_view file_path) {
2851
2825
THROW_IF_INSUFFICIENT_PERMISSIONS (
2852
2826
env,
2853
2827
permission::PermissionScope::kFileSystemRead ,
2854
2828
file_path,
2855
- BindingData:: FilePathIsFileReturnType::kThrowInsufficientPermissions );
2829
+ FilePathIsFileReturnType::kThrowInsufficientPermissions );
2856
2830
2857
2831
uv_fs_t req;
2858
2832
2859
- int rc = uv_fs_stat (env->event_loop (), &req, file_path.c_str (), nullptr );
2833
+ int rc = uv_fs_stat (env->event_loop (), &req, file_path.data (), nullptr );
2860
2834
2861
2835
if (rc == 0 ) {
2862
2836
const uv_stat_t * const s = static_cast <const uv_stat_t *>(req.ptr );
@@ -2871,22 +2845,6 @@ BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile(
2871
2845
return BindingData::FilePathIsFileReturnType::kIsNotFile ;
2872
2846
}
2873
2847
2874
- // the possible file extensions that should be tested
2875
- // 0-6: when packageConfig.main is defined
2876
- // 7-9: when packageConfig.main is NOT defined,
2877
- // or when the previous case didn't found the file
2878
- const std::array<std::string, 10 > BindingData::legacy_main_extensions = {
2879
- " " ,
2880
- " .js" ,
2881
- " .json" ,
2882
- " .node" ,
2883
- " /index.js" ,
2884
- " /index.json" ,
2885
- " /index.node" ,
2886
- " .js" ,
2887
- " .json" ,
2888
- " .node" };
2889
-
2890
2848
void BindingData::LegacyMainResolve (const FunctionCallbackInfo<Value>& args) {
2891
2849
CHECK_GE (args.Length (), 1 );
2892
2850
CHECK (args[0 ]->IsString ());
@@ -2898,38 +2856,32 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2898
2856
ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView ());
2899
2857
2900
2858
if (!package_json_url) {
2901
- env->isolate ()->ThrowException (
2902
- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2903
-
2859
+ THROW_ERR_INVALID_URL (env, " Invalid URL" );
2904
2860
return ;
2905
2861
}
2906
2862
2907
2863
ada::result<ada::url_aggregator> file_path_url;
2908
2864
std::string initial_file_path;
2909
- std::string file_path;
2910
2865
2911
- if (args.Length () >= 2 && !args[1 ]->IsNullOrUndefined () &&
2912
- args[1 ]->IsString ()) {
2866
+ if (args.Length () >= 2 && args[1 ]->IsString ()) {
2913
2867
std::string package_config_main =
2914
2868
Utf8Value (env->isolate (), args[1 ].As <String>()).ToString ();
2915
2869
2916
- file_path_url = ada::parse<ada::url_aggregator>(
2917
- std::string ( " ./ " ) + package_config_main, &package_json_url.value ());
2870
+ file_path_url = ada::parse<ada::url_aggregator>(" ./ " + package_config_main,
2871
+ &package_json_url.value ());
2918
2872
2919
2873
if (!file_path_url) {
2920
- env->isolate ()->ThrowException (
2921
- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2922
-
2874
+ THROW_ERR_INVALID_URL (env, " Invalid URL" );
2923
2875
return ;
2924
2876
}
2925
2877
2926
- if (!FileURLToPath (env, file_path_url. value () , initial_file_path)) return ;
2878
+ if (!FileURLToPath (env, * file_path_url, initial_file_path)) return ;
2927
2879
2928
- FromNamespacedPath (& initial_file_path);
2880
+ FromNamespacedPath (initial_file_path);
2929
2881
2930
- for (int i = 0 ; i < BindingData:: legacy_main_extensions_with_main_end;
2931
- i++) {
2932
- file_path = initial_file_path + BindingData::legacy_main_extensions[i];
2882
+ for (int i = 0 ; i < legacy_main_extensions_with_main_end; i++) {
2883
+ auto file_path = initial_file_path +
2884
+ std::string ( BindingData::legacy_main_extensions[i]) ;
2933
2885
2934
2886
switch (FilePathIsFile (env, file_path)) {
2935
2887
case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2952,20 +2904,19 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2952
2904
ada::parse<ada::url_aggregator>(" ./index" , &package_json_url.value ());
2953
2905
2954
2906
if (!file_path_url) {
2955
- env->isolate ()->ThrowException (
2956
- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2957
-
2907
+ THROW_ERR_INVALID_URL (env, " Invalid URL" );
2958
2908
return ;
2959
2909
}
2960
2910
2961
- if (!FileURLToPath (env, file_path_url. value () , initial_file_path)) return ;
2911
+ if (!FileURLToPath (env, * file_path_url, initial_file_path)) return ;
2962
2912
2963
- FromNamespacedPath (& initial_file_path);
2913
+ FromNamespacedPath (initial_file_path);
2964
2914
2965
- for (int i = BindingData:: legacy_main_extensions_with_main_end;
2966
- i < BindingData:: legacy_main_extensions_package_fallback_end;
2915
+ for (int i = legacy_main_extensions_with_main_end;
2916
+ i < legacy_main_extensions_package_fallback_end;
2967
2917
i++) {
2968
- file_path = initial_file_path + BindingData::legacy_main_extensions[i];
2918
+ auto file_path =
2919
+ initial_file_path + std::string (BindingData::legacy_main_extensions[i]);
2969
2920
2970
2921
switch (FilePathIsFile (env, file_path)) {
2971
2922
case BindingData::FilePathIsFileReturnType::kIsFile :
@@ -2987,32 +2938,27 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
2987
2938
2988
2939
if (!FileURLToPath (env, package_json_url.value (), module_path)) return ;
2989
2940
2990
- if (args.Length () >= 3 && !args[2 ]->IsNullOrUndefined () &&
2991
- args[2 ]->IsString ()) {
2941
+ if (args.Length () >= 3 && args[2 ]->IsString ()) {
2992
2942
Utf8Value utf8_base_path (env->isolate (), args[2 ].As <String>());
2993
2943
auto base_url =
2994
2944
ada::parse<ada::url_aggregator>(utf8_base_path.ToStringView ());
2995
2945
2996
2946
if (!base_url) {
2997
- env->isolate ()->ThrowException (
2998
- ERR_INVALID_URL (env->isolate (), " Invalid URL" ));
2999
-
2947
+ THROW_ERR_INVALID_URL (env->isolate (), " Invalid URL" );
3000
2948
return ;
3001
2949
}
3002
2950
3003
2951
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 ()));
2952
+
2953
+ std::string err_module_message = " Cannot find package '" + module_path +
2954
+ " ' imported from " + module_base;
2955
+ THROW_ERR_MODULE_NOT_FOUND (env, err_module_message.c_str ());
3009
2956
return ;
3010
2957
}
3011
2958
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 ()));
2959
+ THROW_ERR_INVALID_ARG_TYPE (
2960
+ env,
2961
+ " The \" base\" argument must be of type string or an instance of URL." );
3016
2962
}
3017
2963
3018
2964
void BindingData::MemoryInfo (MemoryTracker* tracker) const {
0 commit comments