|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { |
| 4 | + ArrayPrototypeIncludes, |
| 5 | + ObjectCreate, |
| 6 | + ObjectValues, |
| 7 | + ObjectPrototypeHasOwnProperty, |
| 8 | + Symbol, |
| 9 | +} = primordials; |
| 10 | +const { validateString } = require('internal/validators'); |
| 11 | + |
| 12 | +const { |
| 13 | + ERR_IMPORT_ASSERTION_TYPE_FAILED, |
| 14 | + ERR_IMPORT_ASSERTION_TYPE_MISSING, |
| 15 | + ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED, |
| 16 | +} = require('internal/errors').codes; |
| 17 | + |
| 18 | +const kImplicitAssertType = Symbol('implicit assert type'); |
| 19 | + |
| 20 | +/** |
| 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} |
| 24 | + */ |
| 25 | +const formatTypeMap = { |
| 26 | + '__proto__': null, |
| 27 | + 'builtin': kImplicitAssertType, |
| 28 | + 'commonjs': kImplicitAssertType, |
| 29 | + 'json': 'json', |
| 30 | + 'module': kImplicitAssertType, |
| 31 | + 'wasm': kImplicitAssertType, // Should probably be 'webassembly' per https://github.com/tc39/proposal-import-assertions |
| 32 | +}; |
| 33 | + |
| 34 | +/** @type {Array<string, string | typeof kImplicitAssertType} */ |
| 35 | +const supportedAssertionTypes = ObjectValues(formatTypeMap); |
| 36 | + |
| 37 | + |
| 38 | +/** |
| 39 | + * Test a module's import assertions. |
| 40 | + * @param {string} url The URL of the imported module, for error reporting. |
| 41 | + * @param {string} format One of Node's supported translators |
| 42 | + * @param {Record<string, string>} importAssertions Validations for the |
| 43 | + * module import. |
| 44 | + * @returns {true} |
| 45 | + * @throws {TypeError} If the format and assertion type are incompatible. |
| 46 | + */ |
| 47 | +function validateAssertions(url, format, |
| 48 | + importAssertions = ObjectCreate(null)) { |
| 49 | + const validType = formatTypeMap[format]; |
| 50 | + |
| 51 | + switch (validType) { |
| 52 | + case undefined: |
| 53 | + // Ignore assertions for module types we don't recognize, to allow new |
| 54 | + // formats in the future. |
| 55 | + return true; |
| 56 | + |
| 57 | + case importAssertions.type: |
| 58 | + // The asserted type is the valid type for this format. |
| 59 | + return true; |
| 60 | + |
| 61 | + case kImplicitAssertType: |
| 62 | + // This format doesn't allow an import assertion type, so the property |
| 63 | + // must not be set on the import assertions object. |
| 64 | + if (!ObjectPrototypeHasOwnProperty(importAssertions, 'type')) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + return handleInvalidType(url, importAssertions.type); |
| 68 | + |
| 69 | + default: |
| 70 | + // There is an expected type for this format, but the value of |
| 71 | + // `importAssertions.type` was not it. |
| 72 | + if (!ObjectPrototypeHasOwnProperty(importAssertions, 'type')) { |
| 73 | + // `type` wasn't specified at all. |
| 74 | + throw new ERR_IMPORT_ASSERTION_TYPE_MISSING(url, validType); |
| 75 | + } |
| 76 | + handleInvalidType(url, importAssertions.type); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Throw the correct error depending on what's wrong with the type assertion. |
| 82 | + * @param {string} url The resolved URL for the module to be imported |
| 83 | + * @param {string} type The value of the import assertion `type` property |
| 84 | + */ |
| 85 | +function handleInvalidType(url, type) { |
| 86 | + // `type` might have not been a string. |
| 87 | + validateString(type, 'type'); |
| 88 | + |
| 89 | + // `type` was not one of the types we understand. |
| 90 | + if (!ArrayPrototypeIncludes(supportedAssertionTypes, type)) { |
| 91 | + throw new ERR_IMPORT_ASSERTION_TYPE_UNSUPPORTED(type); |
| 92 | + } |
| 93 | + |
| 94 | + // `type` was the wrong value for this format. |
| 95 | + throw new ERR_IMPORT_ASSERTION_TYPE_FAILED(url, type); |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +module.exports = { |
| 100 | + kImplicitAssertType, |
| 101 | + validateAssertions, |
| 102 | +}; |
0 commit comments