All files / lib/internal/modules/esm assert.js

100% Statements 102/102
100% Branches 14/14
100% Functions 2/2
100% Lines 102/102

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 828x 828x 828x 828x 828x 828x 7x 7x 7x 828x 828x 21x 21x 828x 828x 791x 791x 791x 779x 779x 12x 828x 828x 9x 9x 9x 7x 7x 7x 2x 828x 828x 134x 134x 134x 134x 134x 134x 14x 14x 14x 14x 14x 14x 7x 7x 6x 6x 6x 14x 134x 134x 134x 134x 134x 134x  
'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,
};