-
-
Notifications
You must be signed in to change notification settings - Fork 35k
[v16.x Backport] Import assertions related PRs #41776
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cdee445
esm: add support for JSON import assertion
aduh95 0d728a3
test: disable warnings to fix flaky test
aduh95 7ad3028
doc: fix spelling of 'WebAssembly'
GeoffreyBooth 55fdfb2
module: import assertions improvements
GeoffreyBooth e79707e
esm: refactor esm tests out of test/message
GeoffreyBooth 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
Next
Next commit
esm: add support for JSON import assertion
Remove V8 flag for import assertions, enabling support for the syntax; require the import assertion syntax for imports of JSON. Support import assertions in user loaders. Use both resolved module URL and import assertion type as the key for caching modules. Co-authored-by: Geoffrey Booth <[email protected]> PR-URL: #40250 Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
- Loading branch information
commit cdee44557e38ca35bdb2eff082cf08acebec8766
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,102 @@ | ||
| 'use strict'; | ||
|
|
||
| const { | ||
| ArrayPrototypeIncludes, | ||
| ObjectCreate, | ||
| ObjectValues, | ||
| ObjectPrototypeHasOwnProperty, | ||
| Symbol, | ||
| } = primordials; | ||
| const { validateString } = require('internal/validators'); | ||
|
|
||
| const { | ||
| ERR_IMPORT_ASSERTION_TYPE_FAILED, | ||
| ERR_IMPORT_ASSERTION_TYPE_MISSING, | ||
| ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED, | ||
| } = require('internal/errors').codes; | ||
|
|
||
| const kImplicitAssertType = Symbol('implicit assert type'); | ||
|
|
||
| /** | ||
| * Define a map of module formats to import assertion types (the value of `type` | ||
| * in `assert { type: 'json' }`). | ||
| * @type {Map<string, string | typeof kImplicitAssertType} | ||
| */ | ||
| const formatTypeMap = { | ||
| '__proto__': null, | ||
| 'builtin': kImplicitAssertType, | ||
| 'commonjs': kImplicitAssertType, | ||
| 'json': 'json', | ||
| 'module': kImplicitAssertType, | ||
| 'wasm': kImplicitAssertType, // Should probably be 'webassembly' per https://github.com/tc39/proposal-import-assertions | ||
| }; | ||
|
|
||
| /** @type {Array<string, string | typeof kImplicitAssertType} */ | ||
| const supportedAssertionTypes = ObjectValues(formatTypeMap); | ||
|
|
||
|
|
||
| /** | ||
| * Test a module's import assertions. | ||
| * @param {string} url The URL of the imported module, for error reporting. | ||
| * @param {string} format One of Node's supported translators | ||
| * @param {Record<string, string>} importAssertions Validations for the | ||
| * module import. | ||
| * @returns {true} | ||
| * @throws {TypeError} If the format and assertion type are incompatible. | ||
| */ | ||
| function validateAssertions(url, format, | ||
| importAssertions = ObjectCreate(null)) { | ||
| const validType = formatTypeMap[format]; | ||
|
|
||
| switch (validType) { | ||
| case undefined: | ||
| // Ignore assertions for module types we don't recognize, to allow new | ||
| // formats in the future. | ||
| return true; | ||
|
|
||
| case importAssertions.type: | ||
| // The asserted type is the valid type for this format. | ||
| return true; | ||
|
|
||
| case kImplicitAssertType: | ||
| // This format doesn't allow an import assertion type, so the property | ||
| // must not be set on the import assertions object. | ||
| if (!ObjectPrototypeHasOwnProperty(importAssertions, 'type')) { | ||
| return true; | ||
| } | ||
| return handleInvalidType(url, importAssertions.type); | ||
|
|
||
| default: | ||
| // There is an expected type for this format, but the value of | ||
| // `importAssertions.type` was not it. | ||
| if (!ObjectPrototypeHasOwnProperty(importAssertions, 'type')) { | ||
| // `type` wasn't specified at all. | ||
| throw new ERR_IMPORT_ASSERTION_TYPE_MISSING(url, validType); | ||
| } | ||
| handleInvalidType(url, importAssertions.type); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Throw the correct error depending on what's wrong with the type assertion. | ||
| * @param {string} url The resolved URL for the module to be imported | ||
| * @param {string} type The value of the import assertion `type` property | ||
| */ | ||
| function handleInvalidType(url, type) { | ||
| // `type` might have not been a string. | ||
| validateString(type, 'type'); | ||
|
|
||
| // `type` was not one of the types we understand. | ||
| if (!ArrayPrototypeIncludes(supportedAssertionTypes, type)) { | ||
| throw new ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED(type); | ||
| } | ||
|
|
||
| // `type` was the wrong value for this format. | ||
| throw new ERR_IMPORT_ASSERTION_TYPE_FAILED(url, type); | ||
| } | ||
|
|
||
|
|
||
| module.exports = { | ||
| kImplicitAssertType, | ||
| validateAssertions, | ||
| }; | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.