29
29
#include " node_metadata.h"
30
30
#include " node_process-inl.h"
31
31
#include " node_stat_watcher.h"
32
+ #include " node_url.h"
32
33
#include " permission/permission.h"
33
34
#include " util-inl.h"
34
35
@@ -2812,123 +2813,6 @@ static void GetFormatOfExtensionlessFile(
2812
2813
return args.GetReturnValue ().Set (EXTENSIONLESS_FORMAT_JAVASCRIPT);
2813
2814
}
2814
2815
2815
- static bool FileURLToPath (
2816
- Environment* env,
2817
- const ada::url_aggregator& file_url,
2818
- /* The linter can't detect the assign for result_file_path
2819
- So we need to ignore since it suggest to put const */
2820
- // NOLINTNEXTLINE(runtime/references)
2821
- std::string& result_file_path) {
2822
- if (file_url.type != ada::scheme::FILE) {
2823
- env->isolate ()->ThrowException (ERR_INVALID_URL_SCHEME (env->isolate ()));
2824
-
2825
- return false ;
2826
- }
2827
-
2828
- std::string_view pathname = file_url.get_pathname ();
2829
- #ifdef _WIN32
2830
- size_t first_percent = std::string::npos;
2831
- size_t pathname_size = pathname.size ();
2832
- std::string pathname_escaped_slash;
2833
-
2834
- for (size_t i = 0 ; i < pathname_size; i++) {
2835
- if (pathname[i] == ' /' ) {
2836
- pathname_escaped_slash += ' \\ ' ;
2837
- } else {
2838
- pathname_escaped_slash += pathname[i];
2839
- }
2840
-
2841
- if (pathname[i] != ' %' ) continue ;
2842
-
2843
- if (first_percent == std::string::npos) {
2844
- first_percent = i;
2845
- }
2846
-
2847
- // just safe-guard against access the pathname
2848
- // outside the bounds
2849
- if ((i + 2 ) >= pathname_size) continue ;
2850
-
2851
- char third = pathname[i + 2 ] | 0x20 ;
2852
-
2853
- bool is_slash = pathname[i + 1 ] == ' 2' && third == 102 ;
2854
- bool is_forward_slash = pathname[i + 1 ] == ' 5' && third == 99 ;
2855
-
2856
- if (!is_slash && !is_forward_slash) continue ;
2857
-
2858
- env->isolate ()->ThrowException (ERR_INVALID_FILE_URL_PATH (
2859
- env->isolate (),
2860
- " File URL path must not include encoded \\ or / characters" ));
2861
-
2862
- return false ;
2863
- }
2864
-
2865
- std::string_view hostname = file_url.get_hostname ();
2866
- std::string decoded_pathname = ada::unicode::percent_decode (
2867
- std::string_view (pathname_escaped_slash), first_percent);
2868
-
2869
- if (hostname.size () > 0 ) {
2870
- // If hostname is set, then we have a UNC path
2871
- // Pass the hostname through domainToUnicode just in case
2872
- // it is an IDN using punycode encoding. We do not need to worry
2873
- // about percent encoding because the URL parser will have
2874
- // already taken care of that for us. Note that this only
2875
- // causes IDNs with an appropriate `xn--` prefix to be decoded.
2876
- result_file_path =
2877
- " \\\\ " + ada::unicode::to_unicode (hostname) + decoded_pathname;
2878
-
2879
- return true ;
2880
- }
2881
-
2882
- char letter = decoded_pathname[1 ] | 0x20 ;
2883
- char sep = decoded_pathname[2 ];
2884
-
2885
- // a..z A..Z
2886
- if (letter < ' a' || letter > ' z' || sep != ' :' ) {
2887
- env->isolate ()->ThrowException (ERR_INVALID_FILE_URL_PATH (
2888
- env->isolate (), " File URL path must be absolute" ));
2889
-
2890
- return false ;
2891
- }
2892
-
2893
- result_file_path = decoded_pathname.substr (1 );
2894
-
2895
- return true ;
2896
- #else // _WIN32
2897
- std::string_view hostname = file_url.get_hostname ();
2898
-
2899
- if (hostname.size () > 0 ) {
2900
- std::string error_message =
2901
- std::string (" File URL host must be \" localhost\" or empty on " ) +
2902
- std::string (per_process::metadata.platform );
2903
- env->isolate ()->ThrowException (
2904
- ERR_INVALID_FILE_URL_HOST (env->isolate (), error_message.c_str ()));
2905
-
2906
- return false ;
2907
- }
2908
-
2909
- size_t first_percent = std::string::npos;
2910
- for (size_t i = 0 ; (i + 2 ) < pathname.size (); i++) {
2911
- if (pathname[i] != ' %' ) continue ;
2912
-
2913
- if (first_percent == std::string::npos) {
2914
- first_percent = i;
2915
- }
2916
-
2917
- if (pathname[i + 1 ] == ' 2' && (pathname[i + 2 ] | 0x20 ) == 102 ) {
2918
- env->isolate ()->ThrowException (ERR_INVALID_FILE_URL_PATH (
2919
- env->isolate (),
2920
- " File URL path must not include encoded / characters" ));
2921
-
2922
- return false ;
2923
- }
2924
- }
2925
-
2926
- result_file_path = ada::unicode::percent_decode (pathname, first_percent);
2927
-
2928
- return true ;
2929
- #endif // _WIN32
2930
- }
2931
-
2932
2816
BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile (
2933
2817
Environment* env, const std::string& file_path) {
2934
2818
THROW_IF_INSUFFICIENT_PERMISSIONS (
@@ -3016,7 +2900,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3016
2900
return ;
3017
2901
}
3018
2902
3019
- if (!FileURLToPath (env, file_path_url.value (), initial_file_path)) return ;
2903
+ if (!node::url::FileURLToPath (
2904
+ env, file_path_url.value (), initial_file_path))
2905
+ return ;
3020
2906
3021
2907
FromNamespacedPath (&initial_file_path);
3022
2908
@@ -3050,7 +2936,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3050
2936
return ;
3051
2937
}
3052
2938
3053
- if (!FileURLToPath (env, file_path_url.value (), initial_file_path)) return ;
2939
+ if (!node::url::FileURLToPath (env, file_path_url.value (), initial_file_path))
2940
+ return ;
3054
2941
3055
2942
FromNamespacedPath (&initial_file_path);
3056
2943
@@ -3077,7 +2964,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3077
2964
std::string module_path;
3078
2965
std::string module_base;
3079
2966
3080
- if (!FileURLToPath (env, package_json_url.value (), module_path)) return ;
2967
+ if (!node::url::FileURLToPath (env, package_json_url.value (), module_path))
2968
+ return ;
3081
2969
3082
2970
if (args.Length () >= 3 && !args[2 ]->IsNullOrUndefined () &&
3083
2971
args[2 ]->IsString ()) {
@@ -3092,7 +2980,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3092
2980
return ;
3093
2981
}
3094
2982
3095
- if (!FileURLToPath (env, base_url.value (), module_base)) return ;
2983
+ if (!node::url:: FileURLToPath (env, base_url.value (), module_base)) return ;
3096
2984
} else {
3097
2985
std::string err_arg_message =
3098
2986
" The \" base\" argument must be of type string or an instance of URL." ;
0 commit comments