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
@@ -2842,123 +2843,6 @@ static void GetFormatOfExtensionlessFile(
2842
2843
return args.GetReturnValue ().Set (EXTENSIONLESS_FORMAT_JAVASCRIPT);
2843
2844
}
2844
2845
2845
- static bool FileURLToPath (
2846
- Environment* env,
2847
- const ada::url_aggregator& file_url,
2848
- /* The linter can't detect the assign for result_file_path
2849
- So we need to ignore since it suggest to put const */
2850
- // NOLINTNEXTLINE(runtime/references)
2851
- std::string& result_file_path) {
2852
- if (file_url.type != ada::scheme::FILE) {
2853
- env->isolate ()->ThrowException (ERR_INVALID_URL_SCHEME (env->isolate ()));
2854
-
2855
- return false ;
2856
- }
2857
-
2858
- std::string_view pathname = file_url.get_pathname ();
2859
- #ifdef _WIN32
2860
- size_t first_percent = std::string::npos;
2861
- size_t pathname_size = pathname.size ();
2862
- std::string pathname_escaped_slash;
2863
-
2864
- for (size_t i = 0 ; i < pathname_size; i++) {
2865
- if (pathname[i] == ' /' ) {
2866
- pathname_escaped_slash += ' \\ ' ;
2867
- } else {
2868
- pathname_escaped_slash += pathname[i];
2869
- }
2870
-
2871
- if (pathname[i] != ' %' ) continue ;
2872
-
2873
- if (first_percent == std::string::npos) {
2874
- first_percent = i;
2875
- }
2876
-
2877
- // just safe-guard against access the pathname
2878
- // outside the bounds
2879
- if ((i + 2 ) >= pathname_size) continue ;
2880
-
2881
- char third = pathname[i + 2 ] | 0x20 ;
2882
-
2883
- bool is_slash = pathname[i + 1 ] == ' 2' && third == 102 ;
2884
- bool is_forward_slash = pathname[i + 1 ] == ' 5' && third == 99 ;
2885
-
2886
- if (!is_slash && !is_forward_slash) continue ;
2887
-
2888
- env->isolate ()->ThrowException (ERR_INVALID_FILE_URL_PATH (
2889
- env->isolate (),
2890
- " File URL path must not include encoded \\ or / characters" ));
2891
-
2892
- return false ;
2893
- }
2894
-
2895
- std::string_view hostname = file_url.get_hostname ();
2896
- std::string decoded_pathname = ada::unicode::percent_decode (
2897
- std::string_view (pathname_escaped_slash), first_percent);
2898
-
2899
- if (hostname.size () > 0 ) {
2900
- // If hostname is set, then we have a UNC path
2901
- // Pass the hostname through domainToUnicode just in case
2902
- // it is an IDN using punycode encoding. We do not need to worry
2903
- // about percent encoding because the URL parser will have
2904
- // already taken care of that for us. Note that this only
2905
- // causes IDNs with an appropriate `xn--` prefix to be decoded.
2906
- result_file_path =
2907
- " \\\\ " + ada::unicode::to_unicode (hostname) + decoded_pathname;
2908
-
2909
- return true ;
2910
- }
2911
-
2912
- char letter = decoded_pathname[1 ] | 0x20 ;
2913
- char sep = decoded_pathname[2 ];
2914
-
2915
- // a..z A..Z
2916
- if (letter < ' a' || letter > ' z' || sep != ' :' ) {
2917
- env->isolate ()->ThrowException (ERR_INVALID_FILE_URL_PATH (
2918
- env->isolate (), " File URL path must be absolute" ));
2919
-
2920
- return false ;
2921
- }
2922
-
2923
- result_file_path = decoded_pathname.substr (1 );
2924
-
2925
- return true ;
2926
- #else // _WIN32
2927
- std::string_view hostname = file_url.get_hostname ();
2928
-
2929
- if (hostname.size () > 0 ) {
2930
- std::string error_message =
2931
- std::string (" File URL host must be \" localhost\" or empty on " ) +
2932
- std::string (per_process::metadata.platform );
2933
- env->isolate ()->ThrowException (
2934
- ERR_INVALID_FILE_URL_HOST (env->isolate (), error_message.c_str ()));
2935
-
2936
- return false ;
2937
- }
2938
-
2939
- size_t first_percent = std::string::npos;
2940
- for (size_t i = 0 ; (i + 2 ) < pathname.size (); i++) {
2941
- if (pathname[i] != ' %' ) continue ;
2942
-
2943
- if (first_percent == std::string::npos) {
2944
- first_percent = i;
2945
- }
2946
-
2947
- if (pathname[i + 1 ] == ' 2' && (pathname[i + 2 ] | 0x20 ) == 102 ) {
2948
- env->isolate ()->ThrowException (ERR_INVALID_FILE_URL_PATH (
2949
- env->isolate (),
2950
- " File URL path must not include encoded / characters" ));
2951
-
2952
- return false ;
2953
- }
2954
- }
2955
-
2956
- result_file_path = ada::unicode::percent_decode (pathname, first_percent);
2957
-
2958
- return true ;
2959
- #endif // _WIN32
2960
- }
2961
-
2962
2846
BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile (
2963
2847
Environment* env, const std::string& file_path) {
2964
2848
THROW_IF_INSUFFICIENT_PERMISSIONS (
@@ -3046,7 +2930,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3046
2930
return ;
3047
2931
}
3048
2932
3049
- if (!FileURLToPath (env, file_path_url.value (), initial_file_path)) return ;
2933
+ if (!node::url::FileURLToPath (env, *file_path_url, &initial_file_path)) {
2934
+ return ;
2935
+ }
3050
2936
3051
2937
FromNamespacedPath (&initial_file_path);
3052
2938
@@ -3080,7 +2966,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3080
2966
return ;
3081
2967
}
3082
2968
3083
- if (!FileURLToPath (env, file_path_url.value (), initial_file_path)) return ;
2969
+ if (!node::url::FileURLToPath (env, *file_path_url, &initial_file_path)) {
2970
+ return ;
2971
+ }
3084
2972
3085
2973
FromNamespacedPath (&initial_file_path);
3086
2974
@@ -3107,7 +2995,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3107
2995
std::string module_path;
3108
2996
std::string module_base;
3109
2997
3110
- if (!FileURLToPath (env, package_json_url.value (), module_path)) return ;
2998
+ if (!node::url::FileURLToPath (env, *package_json_url, &module_path)) {
2999
+ return ;
3000
+ }
3111
3001
3112
3002
if (args.Length () >= 3 && !args[2 ]->IsNullOrUndefined () &&
3113
3003
args[2 ]->IsString ()) {
@@ -3122,7 +3012,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
3122
3012
return ;
3123
3013
}
3124
3014
3125
- if (!FileURLToPath (env, base_url.value (), module_base)) return ;
3015
+ if (!node::url::FileURLToPath (env, *base_url, &module_base)) {
3016
+ return ;
3017
+ }
3126
3018
} else {
3127
3019
std::string err_arg_message =
3128
3020
" The \" base\" argument must be of type string or an instance of URL." ;
0 commit comments