Skip to content

Commit 72cb44b

Browse files
aduh95bengl
authored andcommitted
esm: improve fetch_module test coverage and remove hack
PR-URL: #41947 Reviewed-By: Geoffrey Booth <[email protected]>
1 parent e412b23 commit 72cb44b

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

lib/internal/modules/esm/fetch_module.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const {
2020
} = require('internal/errors').codes;
2121
const { URL } = require('internal/url');
2222
const net = require('net');
23-
const path = require('path');
2423

2524
/**
2625
* @typedef CacheEntry
@@ -263,15 +262,9 @@ function fetchModule(parsed, { parentURL }) {
263262
if (parsed.protocol === 'http:') {
264263
return PromisePrototypeThen(isLocalAddress(parsed.hostname), (is) => {
265264
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';
272265
throw new ERR_NETWORK_IMPORT_DISALLOWED(
273266
href,
274-
parent,
267+
parentURL,
275268
'http can only be used to load local resources (use https instead).'
276269
);
277270
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

test/es-module/test-http-imports.mjs

+6-5
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ for (const { protocol, createServer } of [
114114
}));
115115
await assert.rejects(
116116
import(crossProtocolRedirect.href),
117-
'should not be able to redirect across protocols'
117+
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
118118
);
119119

120120
const deps = new URL(url.href);
@@ -135,7 +135,8 @@ for (const { protocol, createServer } of [
135135
export default 1;`);
136136
await assert.rejects(
137137
import(fileDep.href),
138-
'should not be able to load file: from http:');
138+
{ code: 'ERR_INVALID_URL_SCHEME' }
139+
);
139140

140141
const builtinDep = new URL(url.href);
141142
builtinDep.searchParams.set('body', `
@@ -144,7 +145,7 @@ for (const { protocol, createServer } of [
144145
`);
145146
await assert.rejects(
146147
import(builtinDep.href),
147-
'should not be able to load node: from http:'
148+
{ code: 'ERR_INVALID_URL_SCHEME' }
148149
);
149150

150151
const unprefixedBuiltinDep = new URL(url.href);
@@ -154,15 +155,15 @@ for (const { protocol, createServer } of [
154155
`);
155156
await assert.rejects(
156157
import(unprefixedBuiltinDep.href),
157-
'should not be able to load unprefixed builtins from http:'
158+
{ code: 'ERR_INVALID_URL_SCHEME' }
158159
);
159160

160161
const unsupportedMIME = new URL(url.href);
161162
unsupportedMIME.searchParams.set('mime', 'application/node');
162163
unsupportedMIME.searchParams.set('body', '');
163164
await assert.rejects(
164165
import(unsupportedMIME.href),
165-
'should not be able to load unsupported MIMEs from http:'
166+
{ code: 'ERR_UNKNOWN_MODULE_FORMAT' }
166167
);
167168

168169
server.close();

0 commit comments

Comments
 (0)