| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2993,6 +2993,26 @@ import 'package-name'; // supported | |||
| 2993 | 2993 | ||
| 2994 | 2994 | `import` with URL schemes other than `file` and `data` is unsupported. | |
| 2995 | 2995 | ||
| 2996 | + <a id="ERR_UNSUPPORTED_RESOLVE_REQUEST"></a> | ||
| 2997 | + | ||
| 2998 | + ### `ERR_UNSUPPORTED_RESOLVE_REQUEST` | ||
| 2999 | + | ||
| 3000 | + An attempt was made to resolve an invalid module referrer. This can happen when | ||
| 3001 | + importing or calling `import.meta.resolve()` with either: | ||
| 3002 | + | ||
| 3003 | + * a bare specifier that is not a builtin module from a module whose URL scheme | ||
| 3004 | + is not `file`. | ||
| 3005 | + * a [relative URL][] from a module whose URL scheme is not a [special scheme][]. | ||
| 3006 | + | ||
| 3007 | + ```mjs | ||
| 3008 | + try { | ||
| 3009 | + // Trying to import the package 'bare-specifier' from a `data:` URL module: | ||
| 3010 | + await import('data:text/javascript,import "bare-specifier"'); | ||
| 3011 | + } catch (e) { | ||
| 3012 | + console.log(e.code); // ERR_UNSUPPORTED_RESOLVE_REQUEST | ||
| 3013 | + } | ||
| 3014 | + ``` | ||
| 3015 | + | ||
| 2996 | 3016 | <a id="ERR_USE_AFTER_CLOSE"></a> | |
| 2997 | 3017 | ||
| 2998 | 3018 | ### `ERR_USE_AFTER_CLOSE` | |
@@ -3681,7 +3701,9 @@ The native call from `process.cpuUsage` could not be processed. | |||
| 3681 | 3701 | [event emitter-based]: events.md#class-eventemitter | |
| 3682 | 3702 | [file descriptors]: https://en.wikipedia.org/wiki/File_descriptor | |
| 3683 | 3703 | [policy]: permissions.md#policies | |
| 3704 | + [relative URL]: https://url.spec.whatwg.org/#relative-url-string | ||
| 3684 | 3705 | [self-reference a package using its name]: packages.md#self-referencing-a-package-using-its-name | |
| 3706 | + [special scheme]: https://url.spec.whatwg.org/#special-scheme | ||
| 3685 | 3707 | [stream-based]: stream.md | |
| 3686 | 3708 | [syscall]: https://man7.org/linux/man-pages/man2/syscalls.2.html | |
| 3687 | 3709 | [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 | |
|---|---|---|---|
@@ -1868,6 +1868,9 @@ E('ERR_UNSUPPORTED_ESM_URL_SCHEME', (url, supported) => { | |||
| 1868 | 1868 | msg += `. Received protocol '${url.protocol}'`; | |
| 1869 | 1869 | return msg; | |
| 1870 | 1870 | }, Error); | |
| 1871 | + E('ERR_UNSUPPORTED_RESOLVE_REQUEST', | ||
| 1872 | + 'Failed to resolve module specifier "%s" from "%s": Invalid relative URL or base scheme is not hierarchical.', | ||
| 1873 | + TypeError); | ||
| 1871 | 1874 | E('ERR_USE_AFTER_CLOSE', '%s was closed', Error); | |
| 1872 | 1875 | ||
| 1873 | 1876 | // 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 | ||
@@ -893,22 +894,37 @@ function shouldBeTreatedAsRelativeOrAbsolutePath(specifier) { | |||
| 893 | 894 | * @param {boolean} preserveSymlinks - Whether to preserve symlinks in the resolved URL. | |
| 894 | 895 | */ | |
| 895 | 896 | function moduleResolve(specifier, base, conditions, preserveSymlinks) { | |
| 896 | - const isRemote = base.protocol === 'http:' || | ||
| 897 | - base.protocol === 'https:'; | ||
| 897 | + const protocol = typeof base === 'string' ? | ||
| 898 | + StringPrototypeSlice(base, 0, StringPrototypeIndexOf(base, ':') + 1) : | ||
| 899 | + base.protocol; | ||
| 900 | + const isData = protocol === 'data:'; | ||
| 901 | + const isRemote = | ||
| 902 | + isData || | ||
| 903 | + protocol === 'http:' || | ||
| 904 | + protocol === 'https:'; | ||
| 898 | 905 | // Order swapped from spec for minor perf gain. | |
| 899 | 906 | // Ok since relative URLs cannot parse as URLs. | |
| 900 | 907 | let resolved; | |
| 901 | 908 | if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { | |
| 902 | - resolved = new URL(specifier, base); | ||
| 903 | - } else if (!isRemote && specifier[0] === '#') { | ||
| 909 | + try { | ||
| 910 | + resolved = new URL(specifier, base); | ||
| 911 | + } catch (cause) { | ||
| 912 | + const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base); | ||
| 913 | + setOwnProperty(error, 'cause', cause); | ||
| 914 | + throw error; | ||
| 915 | + } | ||
| 916 | + } else if (protocol === 'file:' && specifier[0] === '#') { | ||
| 904 | 917 | resolved = packageImportsResolve(specifier, base, conditions); | |
| 905 | 918 | } else { | |
| 906 | 919 | try { | |
| 907 | 920 | resolved = new URL(specifier); | |
| 908 | - } catch { | ||
| 909 | - if (!isRemote) { | ||
| 910 | - resolved = packageResolve(specifier, base, conditions); | ||
| 921 | + } catch (cause) { | ||
| 922 | + if (isRemote && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) { | ||
| 923 | + const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base); | ||
| 924 | + setOwnProperty(error, 'cause', cause); | ||
| 925 | + throw error; | ||
| 911 | 926 | } | |
| 927 | + resolved = packageResolve(specifier, base, conditions); | ||
| 912 | 928 | } | |
| 913 | 929 | } | |
| 914 | 930 | if (resolved.protocol !== 'file:') { | |
@@ -1082,7 +1098,7 @@ function defaultResolve(specifier, context = {}) { | |||
| 1082 | 1098 | } | |
| 1083 | 1099 | } | |
| 1084 | 1100 | ||
| 1085 | - let parsed; | ||
| 1101 | + let parsed, protocol; | ||
| 1086 | 1102 | try { | |
| 1087 | 1103 | if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) { | |
| 1088 | 1104 | parsed = new URL(specifier, parsedParentURL); | |
@@ -1091,7 +1107,7 @@ function defaultResolve(specifier, context = {}) { | |||
| 1091 | 1107 | } | |
| 1092 | 1108 | ||
| 1093 | 1109 | // Avoid accessing the `protocol` property due to the lazy getters. | |
| 1094 | - const protocol = parsed.protocol; | ||
| 1110 | + protocol = parsed.protocol; | ||
| 1095 | 1111 | if (protocol === 'data:' || | |
| 1096 | 1112 | (experimentalNetworkImports && | |
| 1097 | 1113 | ( | |
@@ -1118,7 +1134,8 @@ function defaultResolve(specifier, context = {}) { | |||
| 1118 | 1134 | if (maybeReturn) { return maybeReturn; } | |
| 1119 | 1135 | ||
| 1120 | 1136 | // This must come after checkIfDisallowedImport | |
| 1121 | - if (parsed && parsed.protocol === 'node:') { return { __proto__: null, url: specifier }; } | ||
| 1137 | + protocol ??= parsed?.protocol; | ||
| 1138 | + if (protocol === 'node:') { return { __proto__: null, url: specifier }; } | ||
| 1122 | 1139 | ||
| 1123 | 1140 | ||
| 1124 | 1141 | 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