-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: loader hook URL validation and error messages
PR-URL: #21352 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
- Loading branch information
1 parent
81f06ba
commit 1bf42f4
Showing
8 changed files
with
219 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,123 @@ | ||
// Flags: --experimental-modules | ||
/* eslint-disable node-core/required-modules */ | ||
import common from './index.js'; | ||
|
||
import assert from 'assert'; | ||
const { | ||
PORT, | ||
isMainThread, | ||
isWindows, | ||
isWOW64, | ||
isAIX, | ||
isLinuxPPCBE, | ||
isSunOS, | ||
isFreeBSD, | ||
isOpenBSD, | ||
isLinux, | ||
isOSX, | ||
isGlibc, | ||
enoughTestMem, | ||
enoughTestCpu, | ||
rootDir, | ||
buildType, | ||
localIPv6Hosts, | ||
opensslCli, | ||
PIPE, | ||
hasIPv6, | ||
childShouldThrowAndAbort, | ||
ddCommand, | ||
spawnPwd, | ||
spawnSyncPwd, | ||
platformTimeout, | ||
allowGlobals, | ||
leakedGlobals, | ||
mustCall, | ||
mustCallAtLeast, | ||
mustCallAsync, | ||
hasMultiLocalhost, | ||
fileExists, | ||
skipIfEslintMissing, | ||
canCreateSymLink, | ||
getCallSite, | ||
mustNotCall, | ||
printSkipMessage, | ||
skip, | ||
ArrayStream, | ||
nodeProcessAborted, | ||
busyLoop, | ||
isAlive, | ||
noWarnCode, | ||
expectWarning, | ||
expectsError, | ||
skipIfInspectorDisabled, | ||
skipIf32Bits, | ||
getArrayBufferViews, | ||
getBufferSources, | ||
crashOnUnhandledRejection, | ||
getTTYfd, | ||
runWithInvalidFD, | ||
hijackStdout, | ||
hijackStderr, | ||
restoreStdout, | ||
restoreStderr, | ||
isCPPSymbolsNotMapped | ||
} = common; | ||
|
||
let knownGlobals = [ | ||
Buffer, | ||
clearImmediate, | ||
clearInterval, | ||
clearTimeout, | ||
global, | ||
process, | ||
setImmediate, | ||
setInterval, | ||
setTimeout | ||
]; | ||
|
||
if (process.env.NODE_TEST_KNOWN_GLOBALS) { | ||
const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(','); | ||
allowGlobals(...knownFromEnv); | ||
} | ||
|
||
export function allowGlobals(...whitelist) { | ||
knownGlobals = knownGlobals.concat(whitelist); | ||
} | ||
|
||
export function leakedGlobals() { | ||
// Add possible expected globals | ||
if (global.gc) { | ||
knownGlobals.push(global.gc); | ||
} | ||
|
||
if (global.DTRACE_HTTP_SERVER_RESPONSE) { | ||
knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); | ||
knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); | ||
knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); | ||
knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST); | ||
knownGlobals.push(DTRACE_NET_STREAM_END); | ||
knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); | ||
} | ||
|
||
if (global.COUNTER_NET_SERVER_CONNECTION) { | ||
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION); | ||
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE); | ||
knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST); | ||
knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE); | ||
knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST); | ||
knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE); | ||
} | ||
|
||
const leaked = []; | ||
|
||
for (const val in global) { | ||
if (!knownGlobals.includes(global[val])) { | ||
leaked.push(val); | ||
} | ||
} | ||
|
||
if (global.__coverage__) { | ||
return leaked.filter((varname) => !/^(?:cov_|__cov)/.test(varname)); | ||
} else { | ||
return leaked; | ||
} | ||
} | ||
|
||
process.on('exit', function() { | ||
const leaked = leakedGlobals(); | ||
if (leaked.length > 0) { | ||
assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`); | ||
} | ||
}); | ||
export { | ||
PORT, | ||
isMainThread, | ||
isWindows, | ||
isWOW64, | ||
isAIX, | ||
isLinuxPPCBE, | ||
isSunOS, | ||
isFreeBSD, | ||
isOpenBSD, | ||
isLinux, | ||
isOSX, | ||
isGlibc, | ||
enoughTestMem, | ||
enoughTestCpu, | ||
rootDir, | ||
buildType, | ||
localIPv6Hosts, | ||
opensslCli, | ||
PIPE, | ||
hasIPv6, | ||
childShouldThrowAndAbort, | ||
ddCommand, | ||
spawnPwd, | ||
spawnSyncPwd, | ||
platformTimeout, | ||
allowGlobals, | ||
leakedGlobals, | ||
mustCall, | ||
mustCallAtLeast, | ||
mustCallAsync, | ||
hasMultiLocalhost, | ||
fileExists, | ||
skipIfEslintMissing, | ||
canCreateSymLink, | ||
getCallSite, | ||
mustNotCall, | ||
printSkipMessage, | ||
skip, | ||
ArrayStream, | ||
nodeProcessAborted, | ||
busyLoop, | ||
isAlive, | ||
noWarnCode, | ||
expectWarning, | ||
expectsError, | ||
skipIfInspectorDisabled, | ||
skipIf32Bits, | ||
getArrayBufferViews, | ||
getBufferSources, | ||
crashOnUnhandledRejection, | ||
getTTYfd, | ||
runWithInvalidFD, | ||
hijackStdout, | ||
hijackStderr, | ||
restoreStdout, | ||
restoreStderr, | ||
isCPPSymbolsNotMapped | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-invalid-format.mjs | ||
import { expectsError, mustCall } from '../common'; | ||
import assert from 'assert'; | ||
|
||
import('../fixtures/es-modules/test-esm-ok.mjs') | ||
.then(assert.fail, expectsError({ | ||
code: 'ERR_INVALID_RETURN_PROPERTY', | ||
message: 'Expected string to be returned for the "url" from the ' + | ||
'"loader resolve" function but got "undefined"' | ||
})) | ||
.then(mustCall()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-invalid-url.mjs | ||
import { expectsError, mustCall } from '../common'; | ||
import assert from 'assert'; | ||
|
||
import('../fixtures/es-modules/test-esm-ok.mjs') | ||
.then(assert.fail, expectsError({ | ||
code: 'ERR_INVALID_RETURN_PROPERTY', | ||
message: 'Expected a valid url to be returned for the "url" from the ' + | ||
'"loader resolve" function but got ' + | ||
'../fixtures/es-modules/test-esm-ok.mjs.' | ||
})) | ||
.then(mustCall()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export async function resolve(specifier, parentModuleURL, defaultResolve) { | ||
if (parentModuleURL && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { | ||
return { | ||
url: 'file:///asdf' | ||
}; | ||
} | ||
return defaultResolve(specifier, parentModuleURL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export async function resolve(specifier, parentModuleURL, defaultResolve) { | ||
if (parentModuleURL && specifier === '../fixtures/es-modules/test-esm-ok.mjs') { | ||
return { | ||
url: specifier, | ||
format: 'esm' | ||
}; | ||
} | ||
return defaultResolve(specifier, parentModuleURL); | ||
} |