mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
esm: improve fetch_module test coverage and remove hack
PR-URL: https://github.com/nodejs/node/pull/41947 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
This commit is contained in:
1 parent
dde2f78887
commit
3a1a440802
3 files changed
+55
-13
No files matched your search
@@ -20,7 +20,6 @@ const {
|
||||
} = require('internal/errors').codes;
|
||||
const { URL } = require('internal/url');
|
||||
const net = require('net');
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* @typedef CacheEntry
|
||||
@@ -263,15 +262,9 @@ function fetchModule(parsed, { parentURL }) {
|
||||
if (parsed.protocol === 'http:') {
|
||||
return PromisePrototypeThen(isLocalAddress(parsed.hostname), (is) => {
|
||||
if (is !== true) {
|
||||
let parent = parentURL;
|
||||
const parentName = path.basename(parent.pathname);
|
||||
if (
|
||||
parentName === '[eval]' ||
|
||||
parentName === '[stdin]'
|
||||
) parent = 'command-line';
|
||||
throw new ERR_NETWORK_IMPORT_DISALLOWED(
|
||||
href,
|
||||
parent,
|
||||
parentURL,
|
||||
'http can only be used to load local resources (use https instead).'
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { mustCall } from '../common/index.mjs';
|
||||
import { match, notStrictEqual } from 'assert';
|
||||
import { spawn } from 'child_process';
|
||||
import { execPath } from 'process';
|
||||
|
||||
{
|
||||
const child = spawn(execPath, [
|
||||
'--experimental-network-imports',
|
||||
'--input-type=module',
|
||||
'-e',
|
||||
'import "http://example.com"',
|
||||
]);
|
||||
|
||||
let stderr = '';
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stderr.on('data', (data) => {
|
||||
stderr += data;
|
||||
});
|
||||
child.on('close', mustCall((code, _signal) => {
|
||||
notStrictEqual(code, 0);
|
||||
|
||||
// [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'http://example.com/' by
|
||||
// …/[eval1] is not supported: http can only be used to load local
|
||||
// resources (use https instead).
|
||||
match(stderr, /[ERR_NETWORK_IMPORT_DISALLOWED]/);
|
||||
}));
|
||||
}
|
||||
{
|
||||
const child = spawn(execPath, [
|
||||
'--experimental-network-imports',
|
||||
'--input-type=module',
|
||||
]);
|
||||
child.stdin.end('import "http://example.com"');
|
||||
|
||||
let stderr = '';
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stderr.on('data', (data) => {
|
||||
stderr += data;
|
||||
});
|
||||
child.on('close', mustCall((code, _signal) => {
|
||||
notStrictEqual(code, 0);
|
||||
|
||||
// [ERR_NETWORK_IMPORT_DISALLOWED]: import of 'http://example.com/' by
|
||||
// …/[stdin] is not supported: http can only be used to load local
|
||||
// resources (use https instead).
|
||||
match(stderr, /[ERR_NETWORK_IMPORT_DISALLOWED]/);
|
||||
}));
|
||||
}
|
||||
@@ -114,7 +114,7 @@ for (const { protocol, createServer } of [
|
||||
}));
|
||||
await assert.rejects(
|
||||
import(crossProtocolRedirect.href),
|
||||
'should not be able to redirect across protocols'
|
||||
{ code: 'ERR_NETWORK_IMPORT_DISALLOWED' }
|
||||
);
|
||||
|
||||
const deps = new URL(url.href);
|
||||
@@ -135,7 +135,8 @@ for (const { protocol, createServer } of [
|
||||
export default 1;`);
|
||||
await assert.rejects(
|
||||
import(fileDep.href),
|
||||
'should not be able to load file: from http:');
|
||||
{ code: 'ERR_INVALID_URL_SCHEME' }
|
||||
);
|
||||
|
||||
const builtinDep = new URL(url.href);
|
||||
builtinDep.searchParams.set('body', `
|
||||
@@ -144,7 +145,7 @@ for (const { protocol, createServer } of [
|
||||
`);
|
||||
await assert.rejects(
|
||||
import(builtinDep.href),
|
||||
'should not be able to load node: from http:'
|
||||
{ code: 'ERR_INVALID_URL_SCHEME' }
|
||||
);
|
||||
|
||||
const unprefixedBuiltinDep = new URL(url.href);
|
||||
@@ -154,7 +155,7 @@ for (const { protocol, createServer } of [
|
||||
`);
|
||||
await assert.rejects(
|
||||
import(unprefixedBuiltinDep.href),
|
||||
'should not be able to load unprefixed builtins from http:'
|
||||
{ code: 'ERR_INVALID_URL_SCHEME' }
|
||||
);
|
||||
|
||||
const unsupportedMIME = new URL(url.href);
|
||||
@@ -162,7 +163,7 @@ for (const { protocol, createServer } of [
|
||||
unsupportedMIME.searchParams.set('body', '');
|
||||
await assert.rejects(
|
||||
import(unsupportedMIME.href),
|
||||
'should not be able to load unsupported MIMEs from http:'
|
||||
{ code: 'ERR_UNKNOWN_MODULE_FORMAT' }
|
||||
);
|
||||
|
||||
server.close();
|
||||
|
||||
Reference in new issue
Block a user