-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
module: Set dynamic import callback #15713
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Flags: --experimental-modules --harmony-dynamic-import | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { URL } = require('url'); | ||
const vm = require('vm'); | ||
|
||
common.crashOnUnhandledRejection(); | ||
|
||
const relativePath = './test-esm-ok.mjs'; | ||
const absolutePath = require.resolve('./test-esm-ok.mjs'); | ||
const targetURL = new URL('file:///'); | ||
targetURL.pathname = absolutePath; | ||
|
||
function expectErrorProperty(result, propertyKey, value) { | ||
Promise.resolve(result) | ||
.catch(common.mustCall(error => { | ||
assert.equal(error[propertyKey], value); | ||
})); | ||
} | ||
|
||
function expectMissingModuleError(result) { | ||
expectErrorProperty(result, 'code', 'MODULE_NOT_FOUND'); | ||
} | ||
|
||
function expectInvalidUrlError(result) { | ||
expectErrorProperty(result, 'code', 'ERR_INVALID_URL'); | ||
} | ||
|
||
function expectInvalidReferrerError(result) { | ||
expectErrorProperty(result, 'code', 'ERR_INVALID_URL'); | ||
} | ||
|
||
function expectInvalidProtocolError(result) { | ||
expectErrorProperty(result, 'code', 'ERR_INVALID_PROTOCOL'); | ||
} | ||
|
||
function expectInvalidContextError(result) { | ||
expectErrorProperty(result, | ||
'message', 'import() called outside of main context'); | ||
} | ||
|
||
function expectOkNamespace(result) { | ||
Promise.resolve(result) | ||
.then(common.mustCall(ns => { | ||
// Can't deepStrictEqual because ns isn't a normal object | ||
assert.deepEqual(ns, { default: true }); | ||
})); | ||
} | ||
|
||
function expectFsNamespace(result) { | ||
Promise.resolve(result) | ||
.then(common.mustCall(ns => { | ||
assert.equal(typeof ns.default.writeFile, 'function'); | ||
})); | ||
} | ||
|
||
// For direct use of import expressions inside of CJS or ES modules, including | ||
// via eval, all kinds of specifiers should work without issue. | ||
(function testScriptOrModuleImport() { | ||
// Importing another file, both direct & via eval | ||
// expectOkNamespace(import(relativePath)); | ||
expectOkNamespace(eval.call(null, `import("${relativePath}")`)); | ||
expectOkNamespace(eval(`import("${relativePath}")`)); | ||
expectOkNamespace(eval.call(null, `import("${targetURL}")`)); | ||
|
||
// Importing a built-in, both direct & via eval | ||
expectFsNamespace(import("fs")); | ||
expectFsNamespace(eval('import("fs")')); | ||
expectFsNamespace(eval.call(null, 'import("fs")')); | ||
|
||
expectMissingModuleError(import("./not-an-existing-module.mjs")); | ||
// TODO(jkrems): Right now this doesn't hit a protocol error because the | ||
// module resolution step already rejects it. These arguably should be | ||
// protocol errors. | ||
expectMissingModuleError(import("node:fs")); | ||
expectMissingModuleError(import('http://example.com/foo.js')); | ||
})(); | ||
|
||
// vm.runInThisContext: | ||
// * Supports built-ins, always | ||
// * Supports imports if the script has a known defined origin | ||
(function testRunInThisContext() { | ||
// Succeeds because it's got an valid base url | ||
expectFsNamespace(vm.runInThisContext(`import("fs")`, { | ||
filename: __filename, | ||
})); | ||
expectOkNamespace(vm.runInThisContext(`import("${relativePath}")`, { | ||
filename: __filename, | ||
})); | ||
// Rejects because it's got an invalid referrer URL. | ||
// TODO(jkrems): Arguably the first two (built-in + absolute URL) could work | ||
// with some additional effort. | ||
expectInvalidReferrerError(vm.runInThisContext('import("fs")')); | ||
expectInvalidReferrerError(vm.runInThisContext(`import("${targetURL}")`)); | ||
expectInvalidReferrerError(vm.runInThisContext(`import("${relativePath}")`)); | ||
})(); | ||
|
||
// vm.runInNewContext is currently completely unsupported, pending well-defined | ||
// semantics for per-context/realm module maps in node. | ||
(function testRunInNewContext() { | ||
// Rejects because it's running in the wrong context | ||
expectInvalidContextError( | ||
vm.runInNewContext(`import("${targetURL}")`, undefined, { | ||
filename: __filename, | ||
}) | ||
); | ||
|
||
// Rejects because it's running in the wrong context | ||
expectInvalidContextError(vm.runInNewContext(`import("fs")`, undefined, { | ||
filename: __filename, | ||
})); | ||
})(); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit when landing: alphabetize