| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2956,6 +2956,26 @@ import 'package-name'; // supported | |||
| 2956 | 2956 | ||
| 2957 | 2957 | `import` with URL schemes other than `file` and `data` is unsupported. | |
| 2958 | 2958 | ||
| 2959 | + <a id="ERR_UNSUPPORTED_RESOLVE_REQUEST"></a> | ||
| 2960 | + | ||
| 2961 | + ### `ERR_UNSUPPORTED_RESOLVE_REQUEST` | ||
| 2962 | + | ||
| 2963 | + An attempt was made to resolve an invalid module referrer. This can happen when | ||
| 2964 | + importing or calling `import.meta.resolve()` with either: | ||
| 2965 | + | ||
| 2966 | + * a bare specifier that is not a builtin module from a module whose URL scheme | ||
| 2967 | + is not `file`. | ||
| 2968 | + * a [relative URL][] from a module whose URL scheme is not a [special scheme][]. | ||
| 2969 | + | ||
| 2970 | + ```mjs | ||
| 2971 | + try { | ||
| 2972 | + // Trying to import the package 'bare-specifier' from a `data:` URL module: | ||
| 2973 | + await import('data:text/javascript,import "bare-specifier"'); | ||
| 2974 | + } catch (e) { | ||
| 2975 | + console.log(e.code); // ERR_UNSUPPORTED_RESOLVE_REQUEST | ||
| 2976 | + } | ||
| 2977 | + ``` | ||
| 2978 | + | ||
| 2959 | 2979 | <a id="ERR_USE_AFTER_CLOSE"></a> | |
| 2960 | 2980 | ||
| 2961 | 2981 | ### `ERR_USE_AFTER_CLOSE` | |
@@ -3719,7 +3739,9 @@ The native call from `process.cpuUsage` could not be processed. | |||
| 3719 | 3739 | [event emitter-based]: events.md#class-eventemitter | |
| 3720 | 3740 | [file descriptors]: https://en.wikipedia.org/wiki/File_descriptor | |
| 3721 | 3741 | [policy]: permissions.md#policies | |
| 3742 | + [relative URL]: https://url.spec.whatwg.org/#relative-url-string | ||
| 3722 | 3743 | [self-reference a package using its name]: packages.md#self-referencing-a-package-using-its-name | |
| 3744 | + [special scheme]: https://url.spec.whatwg.org/#special-scheme | ||
| 3723 | 3745 | [stream-based]: stream.md | |
| 3724 | 3746 | [syscall]: https://man7.org/linux/man-pages/man2/syscalls.2.html | |
| 3725 | 3747 | [try-catch]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1863,6 +1863,9 @@ E('ERR_UNSUPPORTED_ESM_URL_SCHEME', (url, supported) => { | |||
| 1863 | 1863 | msg += `. Received protocol '${url.protocol}'`; | |
| 1864 | 1864 | return msg; | |
| 1865 | 1865 | }, Error); | |
| 1866 | + E('ERR_UNSUPPORTED_RESOLVE_REQUEST', | ||
| 1867 | + 'Failed to resolve module specifier "%s" from "%s": Invalid relative URL or base scheme is not hierarchical.', | ||
| 1868 | + TypeError); | ||
| 1866 | 1869 | E('ERR_USE_AFTER_CLOSE', '%s was closed', Error); | |
| 1867 | 1870 | ||
| 1868 | 1871 | // This should probably be a `TypeError`. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,7 +37,7 @@ const experimentalNetworkImports = | |||
| 37 | 37 | getOptionValue('--experimental-network-imports'); | |
| 38 | 38 | const inputTypeFlag = getOptionValue('--input-type'); | |
| 39 | 39 | const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url'); | |
| 40 | - const { getCWDURL } = require('internal/util'); | ||
| 40 | + const { getCWDURL, setOwnProperty } = require('internal/util'); | ||
| 41 | 41 | const { canParse: URLCanParse } = internalBinding('url'); | |
| 42 | 42 | const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs'); | |
| 43 | 43 | const { | |
@@ -51,6 +51,7 @@ const { | |||
| 51 | 51 | ERR_PACKAGE_IMPORT_NOT_DEFINED, | |
| 52 | 52 | ERR_PACKAGE_PATH_NOT_EXPORTED, | |
| 53 | 53 | ERR_UNSUPPORTED_DIR_IMPORT, | |
| 54 | + ERR_UNSUPPORTED_RESOLVE_REQUEST, | ||
| 54 | 55 | ERR_NETWORK_IMPORT_DISALLOWED, | |
| 55 | 56 | } = require('internal/errors').codes; | |
| 56 | 57 | ||
@@ -884,22 +885,37 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) { | |||
| 884 | 885 | * @param {boolean} preserveSymlinks - Whether to preserve symlinks in the resolved URL. | |
| 885 | 886 | */ | |
| 886 | 887 | function moduleResolve(specifier, base, conditions, preserveSymlinks) { | |
| 887 | - const isRemote = base.protocol === 'http:' || | ||
| 888 | - base.protocol === 'https:'; | ||
| 888 | + const protocol = typeof base === 'string' ? | ||
| 889 | + StringPrototypeSlice(base, 0, StringPrototypeIndexOf(base, ':') + 1) : | ||
| 890 | + base.protocol; | ||
| 891 | + const isData = protocol === 'data:'; | ||
| 892 | + const isRemote = | ||
| 893 | + isData || | ||
| 894 | + protocol === 'http:' || | ||
| 895 | + protocol === 'https:'; | ||
| 889 | 896 | // Order swapped from spec for minor perf gain. | |
| 890 | 897 | // Ok since relative URLs cannot parse as URLs. | |
| 891 | 898 | let resolved; | |
| 892 | 899 | if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { | |
| 893 | - resolved = new URL(specifier, base); | ||
| 894 | - } else if (!isRemote && specifier[0] === '#') { | ||
| 900 | + try { | ||
| 901 | + resolved = new URL(specifier, base); | ||
| 902 | + } catch (cause) { | ||
| 903 | + const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base); | ||
| 904 | + setOwnProperty(error, 'cause', cause); | ||
| 905 | + throw error; | ||
| 906 | + } | ||
| 907 | + } else if (protocol === 'file:' && specifier[0] === '#') { | ||
| 895 | 908 | resolved = packageImportsResolve(specifier, base, conditions); | |
| 896 | 909 | } else { | |
| 897 | 910 | try { | |
| 898 | 911 | resolved = new URL(specifier); | |
| 899 | - } catch { | ||
| 900 | - if (!isRemote) { | ||
| 901 | - resolved = packageResolve(specifier, base, conditions); | ||
| 912 | + } catch (cause) { | ||
| 913 | + if (isRemote && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) { | ||
| 914 | + const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base); | ||
| 915 | + setOwnProperty(error, 'cause', cause); | ||
| 916 | + throw error; | ||
| 902 | 917 | } | |
| 918 | + resolved = packageResolve(specifier, base, conditions); | ||
| 903 | 919 | } | |
| 904 | 920 | } | |
| 905 | 921 | if (resolved.protocol !== 'file:') { | |
@@ -1073,7 +1089,7 @@ function defaultResolve(specifier, context = {}) { | |||
| 1073 | 1089 | } | |
| 1074 | 1090 | } | |
| 1075 | 1091 | ||
| 1076 | - let parsed; | ||
| 1092 | + let parsed, protocol; | ||
| 1077 | 1093 | try { | |
| 1078 | 1094 | if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { | |
| 1079 | 1095 | parsed = new URL(specifier, parsedParentURL); | |
@@ -1082,7 +1098,7 @@ function defaultResolve(specifier, context = {}) { | |||
| 1082 | 1098 | } | |
| 1083 | 1099 | ||
| 1084 | 1100 | // Avoid accessing the `protocol` property due to the lazy getters. | |
| 1085 | - const protocol = parsed.protocol; | ||
| 1101 | + protocol = parsed.protocol; | ||
| 1086 | 1102 | if (protocol === 'data:' || | |
| 1087 | 1103 | (experimentalNetworkImports && | |
| 1088 | 1104 | ( | |
@@ -1109,7 +1125,8 @@ function defaultResolve(specifier, context = {}) { | |||
| 1109 | 1125 | if (maybeReturn) { return maybeReturn; } | |
| 1110 | 1126 | ||
| 1111 | 1127 | // This must come after checkIfDisallowedImport | |
| 1112 | - if (parsed && parsed.protocol === 'node:') { return { __proto__: null, url: specifier }; } | ||
| 1128 | + protocol ??= parsed?.protocol; | ||
| 1129 | + if (protocol === 'node:') { return { __proto__: null, url: specifier }; } | ||
| 1113 | 1130 | ||
| 1114 | 1131 | ||
| 1115 | 1132 | const isMain = parentURL === undefined; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,6 +36,28 @@ assert.strictEqual(import.meta.resolve('http://some-absolute/url'), 'http://some | |||
| 36 | 36 | assert.strictEqual(import.meta.resolve('some://weird/protocol'), 'some://weird/protocol'); | |
| 37 | 37 | assert.strictEqual(import.meta.resolve('baz/', fixtures), | |
| 38 | 38 | fixtures + 'node_modules/baz/'); | |
| 39 | + assert.deepStrictEqual( | ||
| 40 | + { ...await import('data:text/javascript,export default import.meta.resolve("http://some-absolute/url")') }, | ||
| 41 | + { default: 'http://some-absolute/url' }, | ||
| 42 | + ); | ||
| 43 | + assert.deepStrictEqual( | ||
| 44 | + { ...await import('data:text/javascript,export default import.meta.resolve("some://weird/protocol")') }, | ||
| 45 | + { default: 'some://weird/protocol' }, | ||
| 46 | + ); | ||
| 47 | + assert.deepStrictEqual( | ||
| 48 | + { ...await import(`data:text/javascript,export default import.meta.resolve("baz/", ${JSON.stringify(fixtures)})`) }, | ||
| 49 | + { default: fixtures + 'node_modules/baz/' }, | ||
| 50 | + ); | ||
| 51 | + assert.deepStrictEqual( | ||
| 52 | + { ...await import('data:text/javascript,export default import.meta.resolve("fs")') }, | ||
| 53 | + { default: 'node:fs' }, | ||
| 54 | + ); | ||
| 55 | + await assert.rejects(import('data:text/javascript,export default import.meta.resolve("does-not-exist")'), { | ||
| 56 | + code: 'ERR_UNSUPPORTED_RESOLVE_REQUEST', | ||
| 57 | + }); | ||
| 58 | + await assert.rejects(import('data:text/javascript,export default import.meta.resolve("./relative")'), { | ||
| 59 | + code: 'ERR_UNSUPPORTED_RESOLVE_REQUEST', | ||
| 60 | + }); | ||
| 39 | 61 | ||
| 40 | 62 | { | |
| 41 | 63 | const cp = spawn(execPath, [ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments