GeezerVison is showing github.com — esm: improve `fetch_module` test coverage and remove hack · nodejs/node@72cb44b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/fetch_module.js +1-8Lines changed: 1 addition & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,7 +20,6 @@ const { | 20 | 20 | } = require('internal/errors').codes; | 21 | 21 | const { URL } = require('internal/url'); | 22 | 22 | const net = require('net'); | 23 | | -const path = require('path'); | 24 | 23 |
| 25 | 24 | /** | 26 | 25 | * @typedef CacheEntry | @@ -263,15 +262,9 @@ function fetchModule(parsed, { parentURL }) { | 263 | 262 | if (parsed.protocol === 'http:') { | 264 | 263 | return PromisePrototypeThen(isLocalAddress(parsed.hostname), (is) => { | 265 | 264 | if (is !== true) { | 266 | | - let parent = parentURL; | 267 | | - const parentName = path.basename(parent.pathname); | 268 | | - if ( | 269 | | - parentName === '[eval]' || | 270 | | - parentName === '[stdin]' | 271 | | - ) parent = 'command-line'; | 272 | 265 | throw new ERR_NETWORK_IMPORT_DISALLOWED( | 273 | 266 | href, | 274 | | - parent, | | 267 | + parentURL, | 275 | 268 | 'http can only be used to load local resources (use https instead).' | 276 | 269 | ); | 277 | 270 | } | |
Collapse file Copy file name to clipboard +48Lines changed: 48 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { mustCall } from '../common/index.mjs'; | | 2 | +import { match, notStrictEqual } from 'assert'; | | 3 | +import { spawn } from 'child_process'; | | 4 | +import { execPath } from 'process'; | | 5 | + | | 6 | +{ | | 7 | + const child = spawn(execPath, [ | | 8 | + '--experimental-network-imports', | | 9 | + '--input-type=module', | | 10 | + '-e', | | 11 | + 'import "http://example.com"', | | 12 | + ]); | | 13 | + | | 14 | + let stderr = ''; | | 15 | + child.stderr.setEncoding('utf8'); | | 16 | + child.stderr.on('data', (data) => { | | 17 | + stderr += data; | | 18 | + }); | | 19 | + child.on('close', mustCall((code, _signal) => { | | 20 | + notStrictEqual(code, 0); | | 21 | + | | 22 | + // [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'http://example.com/' by | | 23 | + // …/[eval1] is not supported: http can only be used to load local | | 24 | + // resources (use https instead). | | 25 | + match(stderr, /[ERR_NETWORK_IMPORT_DISALLOWED]/); | | 26 | + })); | | 27 | +} | | 28 | +{ | | 29 | + const child = spawn(execPath, [ | | 30 | + '--experimental-network-imports', | | 31 | + '--input-type=module', | | 32 | + ]); | | 33 | + child.stdin.end('import "http://example.com"'); | | 34 | + | | 35 | + let stderr = ''; | | 36 | + child.stderr.setEncoding('utf8'); | | 37 | + child.stderr.on('data', (data) => { | | 38 | + stderr += data; | | 39 | + }); | | 40 | + child.on('close', mustCall((code, _signal) => { | | 41 | + notStrictEqual(code, 0); | | 42 | + | | 43 | + // [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'http://example.com/' by | | 44 | + // …/[stdin] is not supported: http can only be used to load local | | 45 | + // resources (use https instead). | | 46 | + match(stderr, /[ERR_NETWORK_IMPORT_DISALLOWED]/); | | 47 | + })); | | 48 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-http-imports.mjs +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -114,7 +114,7 @@ for (const { protocol, createServer } of [ | 114 | 114 | })); | 115 | 115 | await assert.rejects( | 116 | 116 | import(crossProtocolRedirect.href), | 117 | | - 'should not be able to redirect across protocols' | | 117 | + { code: 'ERR_NETWORK_IMPORT_DISALLOWED' } | 118 | 118 | ); | 119 | 119 |
| 120 | 120 | const deps = new URL(url.href); | @@ -135,7 +135,8 @@ for (const { protocol, createServer } of [ | 135 | 135 | export default 1;`); | 136 | 136 | await assert.rejects( | 137 | 137 | import(fileDep.href), | 138 | | - 'should not be able to load file: from http:'); | | 138 | + { code: 'ERR_INVALID_URL_SCHEME' } | | 139 | + ); | 139 | 140 |
| 140 | 141 | const builtinDep = new URL(url.href); | 141 | 142 | builtinDep.searchParams.set('body', ` | @@ -144,7 +145,7 @@ for (const { protocol, createServer } of [ | 144 | 145 | `); | 145 | 146 | await assert.rejects( | 146 | 147 | import(builtinDep.href), | 147 | | - 'should not be able to load node: from http:' | | 148 | + { code: 'ERR_INVALID_URL_SCHEME' } | 148 | 149 | ); | 149 | 150 |
| 150 | 151 | const unprefixedBuiltinDep = new URL(url.href); | @@ -154,15 +155,15 @@ for (const { protocol, createServer } of [ | 154 | 155 | `); | 155 | 156 | await assert.rejects( | 156 | 157 | import(unprefixedBuiltinDep.href), | 157 | | - 'should not be able to load unprefixed builtins from http:' | | 158 | + { code: 'ERR_INVALID_URL_SCHEME' } | 158 | 159 | ); | 159 | 160 |
| 160 | 161 | const unsupportedMIME = new URL(url.href); | 161 | 162 | unsupportedMIME.searchParams.set('mime', 'application/node'); | 162 | 163 | unsupportedMIME.searchParams.set('body', ''); | 163 | 164 | await assert.rejects( | 164 | 165 | import(unsupportedMIME.href), | 165 | | - 'should not be able to load unsupported MIMEs from http:' | | 166 | + { code: 'ERR_UNKNOWN_MODULE_FORMAT' } | 166 | 167 | ); | 167 | 168 |
| 168 | 169 | server.close(); | |
|