| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b92416f commit 3c8aa21
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -228,6 +228,8 @@ import fs from 'node:fs/promises'; | |||
| 228 | 228 | added: v17.1.0 | |
| 229 | 229 | --> | |
| 230 | 230 | ||
| 231 | + > Stability: 1 - Experimental | ||
| 232 | + | ||
| 231 | 233 | The [Import Assertions proposal][] adds an inline syntax for module import | |
| 232 | 234 | statements to pass on more information alongside the module specifier. | |
| 233 | 235 | ||
@@ -238,11 +240,12 @@ const { default: barData } = | |||
| 238 | 240 | await import('./bar.json', { assert: { type: 'json' } }); | |
| 239 | 241 | ``` | |
| 240 | 242 | ||
| 241 | - Node.js supports the following `type` values: | ||
| 243 | + Node.js supports the following `type` values, for which the assertion is | ||
| 244 | + mandatory: | ||
| 242 | 245 | ||
| 243 | - | `type` | Resolves to | | ||
| 244 | - | -------- | ---------------- | | ||
| 245 | - | `'json'` | [JSON modules][] | | ||
| 246 | + | Assertion `type` | Needed for | | ||
| 247 | + | ---------------- | ---------------- | | ||
| 248 | + | `'json'` | [JSON modules][] | | ||
| 246 | 249 | ||
| 247 | 250 | ## Builtin modules | |
| 248 | 251 | ||
@@ -553,6 +556,8 @@ node index.mjs # fails | |||
| 553 | 556 | node --experimental-json-modules index.mjs # works | |
| 554 | 557 | ``` | |
| 555 | 558 | ||
| 559 | + The `assert { type: 'json' }` syntax is mandatory; see [Import Assertions][]. | ||
| 560 | + | ||
| 556 | 561 | <i id="esm_experimental_wasm_modules"></i> | |
| 557 | 562 | ||
| 558 | 563 | ## Wasm modules | |
@@ -1390,6 +1395,7 @@ success! | |||
| 1390 | 1395 | [Dynamic `import()`]: https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Dynamic_Imports | |
| 1391 | 1396 | [ECMAScript Top-Level `await` proposal]: https://github.com/tc39/proposal-top-level-await/ | |
| 1392 | 1397 | [ES Module Integration Proposal for WebAssembly]: https://github.com/webassembly/esm-integration | |
| 1398 | + [Import Assertions]: #import-assertions | ||
| 1393 | 1399 | [Import Assertions proposal]: https://github.com/tc39/proposal-import-assertions | |
| 1394 | 1400 | [JSON modules]: #json-modules | |
| 1395 | 1401 | [Node.js Module Resolution Algorithm]: #resolver-algorithm-specification | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,11 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | + ArrayPrototypeFilter, | ||
| 4 | 5 | ArrayPrototypeIncludes, | |
| 5 | 6 | ObjectCreate, | |
| 6 | 7 | ObjectValues, | |
| 7 | 8 | ObjectPrototypeHasOwnProperty, | |
| 8 | - Symbol, | ||
| 9 | 9 | } = primordials; | |
| 10 | 10 | const { validateString } = require('internal/validators'); | |
| 11 | 11 | ||
@@ -15,24 +15,32 @@ const { | |||
| 15 | 15 | ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED, | |
| 16 | 16 | } = require('internal/errors').codes; | |
| 17 | 17 | ||
| 18 | - const kImplicitAssertType = Symbol('implicit assert type'); | ||
| 18 | + // The HTML spec has an implied default type of `'javascript'`. | ||
| 19 | + const kImplicitAssertType = 'javascript'; | ||
| 19 | 20 | ||
| 20 | 21 | /** | |
| 21 | - * Define a map of module formats to import assertion types (the value of `type` | ||
| 22 | - * in `assert { type: 'json' }`). | ||
| 23 | - * @type {Map<string, string | typeof kImplicitAssertType} | ||
| 22 | + * Define a map of module formats to import assertion types (the value of | ||
| 23 | + * `type` in `assert { type: 'json' }`). | ||
| 24 | + * @type {Map<string, string>} | ||
| 24 | 25 | */ | |
| 25 | 26 | const formatTypeMap = { | |
| 26 | 27 | '__proto__': null, | |
| 27 | 28 | 'builtin': kImplicitAssertType, | |
| 28 | 29 | 'commonjs': kImplicitAssertType, | |
| 29 | 30 | 'json': 'json', | |
| 30 | 31 | 'module': kImplicitAssertType, | |
| 31 | - 'wasm': kImplicitAssertType, // Should probably be 'webassembly' per https://github.com/tc39/proposal-import-assertions | ||
| 32 | + 'wasm': kImplicitAssertType, // It's unclear whether the HTML spec will require an assertion type or not for Wasm; see https://github.com/WebAssembly/esm-integration/issues/42 | ||
| 32 | 33 | }; | |
| 33 | 34 | ||
| 34 | - /** @type {Array<string, string | typeof kImplicitAssertType} */ | ||
| 35 | - const supportedAssertionTypes = ObjectValues(formatTypeMap); | ||
| 35 | + /** | ||
| 36 | + * The HTML spec disallows the default type to be explicitly specified | ||
| 37 | + * (for now); so `import './file.js'` is okay but | ||
| 38 | + * `import './file.js' assert { type: 'javascript' }` throws. | ||
| 39 | + * @type {Array<string, string>} | ||
| 40 | + */ | ||
| 41 | + const supportedAssertionTypes = ArrayPrototypeFilter( | ||
| 42 | + ObjectValues(formatTypeMap), | ||
| 43 | + (type) => type !== kImplicitAssertType); | ||
| 36 | 44 | ||
| 37 | 45 | ||
| 38 | 46 | /** | |
@@ -50,14 +58,10 @@ function validateAssertions(url, format, | |||
| 50 | 58 | ||
| 51 | 59 | switch (validType) { | |
| 52 | 60 | case undefined: | |
| 53 | - // Ignore assertions for module types we don't recognize, to allow new | ||
| 61 | + // Ignore assertions for module formats we don't recognize, to allow new | ||
| 54 | 62 | // formats in the future. | |
| 55 | 63 | return true; | |
| 56 | 64 | ||
| 57 | - case importAssertions.type: | ||
| 58 | - // The asserted type is the valid type for this format. | ||
| 59 | - return true; | ||
| 60 | - | ||
| 61 | 65 | case kImplicitAssertType: | |
| 62 | 66 | // This format doesn't allow an import assertion type, so the property | |
| 63 | 67 | // must not be set on the import assertions object. | |
@@ -66,9 +70,13 @@ function validateAssertions(url, format, | |||
| 66 | 70 | } | |
| 67 | 71 | return handleInvalidType(url, importAssertions.type); | |
| 68 | 72 | ||
| 73 | + case importAssertions.type: | ||
| 74 | + // The asserted type is the valid type for this format. | ||
| 75 | + return true; | ||
| 76 | + | ||
| 69 | 77 | default: | |
| 70 | 78 | // There is an expected type for this format, but the value of | |
| 71 | - // `importAssertions.type` was not it. | ||
| 79 | + // `importAssertions.type` might not have been it. | ||
| 72 | 80 | if (!ObjectPrototypeHasOwnProperty(importAssertions, 'type')) { | |
| 73 | 81 | // `type` wasn't specified at all. | |
| 74 | 82 | throw new ERR_IMPORT_ASSERTION_TYPE_MISSING(url, validType); | |
@@ -86,7 +94,7 @@ function handleInvalidType(url, type) { | |||
| 86 | 94 | // `type` might have not been a string. | |
| 87 | 95 | validateString(type, 'type'); | |
| 88 | 96 | ||
| 89 | - // `type` was not one of the types we understand. | ||
| 97 | + // `type` might not have been one of the types we understand. | ||
| 90 | 98 | if (!ArrayPrototypeIncludes(supportedAssertionTypes, type)) { | |
| 91 | 99 | throw new ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED(type); | |
| 92 | 100 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,20 +12,17 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => { | |||
| 12 | 12 | const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; | |
| 13 | 13 | const { validateString } = require('internal/validators'); | |
| 14 | 14 | ||
| 15 | - const validateAssertType = (type) => | ||
| 16 | - type === kImplicitAssertType || validateString(type, 'type'); | ||
| 17 | - | ||
| 18 | 15 | // Tracks the state of the loader-level module cache | |
| 19 | 16 | class ModuleMap extends SafeMap { | |
| 20 | 17 | constructor(i) { super(i); } // eslint-disable-line no-useless-constructor | |
| 21 | 18 | get(url, type = kImplicitAssertType) { | |
| 22 | 19 | validateString(url, 'url'); | |
| 23 | - validateAssertType(type); | ||
| 20 | + validateString(type, 'type'); | ||
| 24 | 21 | return super.get(url)?.[type]; | |
| 25 | 22 | } | |
| 26 | 23 | set(url, type = kImplicitAssertType, job) { | |
| 27 | 24 | validateString(url, 'url'); | |
| 28 | - validateAssertType(type); | ||
| 25 | + validateString(type, 'type'); | ||
| 29 | 26 | if (job instanceof ModuleJob !== true && | |
| 30 | 27 | typeof job !== 'function') { | |
| 31 | 28 | throw new ERR_INVALID_ARG_TYPE('job', 'ModuleJob', job); | |
@@ -39,7 +36,7 @@ class ModuleMap extends SafeMap { | |||
| 39 | 36 | } | |
| 40 | 37 | has(url, type = kImplicitAssertType) { | |
| 41 | 38 | validateString(url, 'url'); | |
| 42 | - validateAssertType(type); | ||
| 39 | + validateString(type, 'type'); | ||
| 43 | 40 | return super.get(url)?.[type] !== undefined; | |
| 44 | 41 | } | |
| 45 | 42 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,7 +25,7 @@ async function test() { | |||
| 25 | 25 | ); | |
| 26 | 26 | ||
| 27 | 27 | await rejects( | |
| 28 | - import('data:text/javascript,', { assert: { type: 'unsupported' } }), | ||
| 28 | + import(jsModuleDataUrl, { assert: { type: 'unsupported' } }), | ||
| 29 | 29 | { code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED' } | |
| 30 | 30 | ); | |
| 31 | 31 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,8 +22,8 @@ assert.throws(() => validateAssertions(url, 'module', { type: 'json' }), { | |||
| 22 | 22 | code: 'ERR_IMPORT_ASSERTION_TYPE_FAILED', | |
| 23 | 23 | }); | |
| 24 | 24 | ||
| 25 | - // This should be allowed according to HTML spec. Let's keep it disabled | ||
| 26 | - // until WASM module import is sorted out. | ||
| 25 | + // The HTML spec specifically disallows this for now, while Wasm module import | ||
| 26 | + // and whether it will require a type assertion is still an open question. | ||
| 27 | 27 | assert.throws(() => validateAssertions(url, 'module', { type: 'javascript' }), { | |
| 28 | 28 | code: 'ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED', | |
| 29 | 29 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,6 @@ const { strictEqual, throws } = require('assert'); | |||
| 7 | 7 | const { ESMLoader } = require('internal/modules/esm/loader'); | |
| 8 | 8 | const ModuleMap = require('internal/modules/esm/module_map'); | |
| 9 | 9 | const ModuleJob = require('internal/modules/esm/module_job'); | |
| 10 | - const { kImplicitAssertType } = require('internal/modules/esm/assert'); | ||
| 11 | 10 | const createDynamicModule = require( | |
| 12 | 11 | 'internal/modules/esm/create_dynamic_module'); | |
| 13 | 12 | ||
@@ -38,14 +37,14 @@ const jsonModuleJob = new ModuleJob(loader, stubJsonModule.module, | |||
| 38 | 37 | strictEqual(moduleMap.get(jsonModuleDataUrl, 'json'), jsonModuleJob); | |
| 39 | 38 | ||
| 40 | 39 | strictEqual(moduleMap.has(jsModuleDataUrl), true); | |
| 41 | - strictEqual(moduleMap.has(jsModuleDataUrl, kImplicitAssertType), true); | ||
| 40 | + strictEqual(moduleMap.has(jsModuleDataUrl, 'javascript'), true); | ||
| 42 | 41 | strictEqual(moduleMap.has(jsonModuleDataUrl, 'json'), true); | |
| 43 | 42 | ||
| 44 | 43 | strictEqual(moduleMap.has('unknown'), false); | |
| 45 | 44 | ||
| 46 | 45 | // The types must match | |
| 47 | 46 | strictEqual(moduleMap.has(jsModuleDataUrl, 'json'), false); | |
| 48 | - strictEqual(moduleMap.has(jsonModuleDataUrl, kImplicitAssertType), false); | ||
| 47 | + strictEqual(moduleMap.has(jsonModuleDataUrl, 'javascript'), false); | ||
| 49 | 48 | strictEqual(moduleMap.has(jsonModuleDataUrl), false); | |
| 50 | 49 | strictEqual(moduleMap.has(jsModuleDataUrl, 'unknown'), false); | |
| 51 | 50 | strictEqual(moduleMap.has(jsonModuleDataUrl, 'unknown'), false); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments