MCPcopy Index your code
hub / github.com/nodejs/node / validateAttributes

Function validateAttributes

lib/internal/modules/esm/assert.js:55–92  ·  view source on GitHub ↗

* Test a module's import attributes. * @param {string} url The URL of the imported module, for error reporting. * @param {string} format One of Node's supported translators * @param {Record } importAttributes Validations for the * module import. * @returns {true} * @throws {Ty

(url, format,
                            importAttributes = { __proto__: null })

Source from the content-addressed store, hash-verified

53 * @throws {TypeError} If the format and type attribute are incompatible.
54 */
55function validateAttributes(url, format,
56 importAttributes = { __proto__: null }) {
57 const keys = ObjectKeys(importAttributes);
58 for (let i = 0; i < keys.length; i++) {
59 if (keys[i] !== 'type') {
60 throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED(keys[i], importAttributes[keys[i]], url);
61 }
62 }
63 const validType = formatTypeMap[format];
64
65 switch (validType) {
66 case undefined:
67 // Ignore attributes for module formats we don't recognize, to allow new
68 // formats in the future.
69 return true;
70
71 case kImplicitTypeAttribute:
72 // This format doesn't allow an import type attribute, so the property
73 // must not be set on the import attributes object.
74 if (!ObjectPrototypeHasOwnProperty(importAttributes, 'type')) {
75 return true;
76 }
77 return handleInvalidType(url, importAttributes.type);
78
79 case importAttributes.type:
80 // The type attribute is the valid type for this format.
81 return true;
82
83 default:
84 // There is an expected type for this format, but the value of
85 // `importAttributes.type` might not have been it.
86 if (!ObjectPrototypeHasOwnProperty(importAttributes, 'type')) {
87 // `type` wasn't specified at all.
88 throw new ERR_IMPORT_ATTRIBUTE_MISSING(url, 'type', validType);
89 }
90 return handleInvalidType(url, importAttributes.type);
91 }
92}
93
94/**
95 * Throw the correct error depending on what's wrong with the type attribute.

Callers 3

defaultLoadFunction · 0.85
defaultLoadSyncFunction · 0.85

Calls 1

handleInvalidTypeFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…