|
| 1 | +'use strict'; |
| 2 | +const { |
| 3 | + ArrayIsArray, |
| 4 | + SafeSet, |
| 5 | + SafeWeakMap, |
| 6 | + ObjectFreeze, |
| 7 | +} = primordials; |
| 8 | + |
| 9 | +const { |
| 10 | + ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING, |
| 11 | + ERR_INVALID_ARG_VALUE, |
| 12 | +} = require('internal/errors').codes; |
| 13 | + |
| 14 | +const { getOptionValue } = require('internal/options'); |
| 15 | + |
| 16 | +const { |
| 17 | + setImportModuleDynamicallyCallback, |
| 18 | + setInitializeImportMetaObjectCallback |
| 19 | +} = internalBinding('module_wrap'); |
| 20 | +const { |
| 21 | + getModuleFromWrap, |
| 22 | +} = require('internal/vm/module'); |
| 23 | +const assert = require('internal/assert'); |
| 24 | + |
| 25 | +const callbackMap = new SafeWeakMap(); |
| 26 | +function setCallbackForWrap(wrap, data) { |
| 27 | + callbackMap.set(wrap, data); |
| 28 | +} |
| 29 | + |
| 30 | +let defaultConditions; |
| 31 | +function getDefaultConditions() { |
| 32 | + assert(defaultConditions !== undefined); |
| 33 | + return defaultConditions; |
| 34 | +} |
| 35 | + |
| 36 | +let defaultConditionsSet; |
| 37 | +function getDefaultConditionsSet() { |
| 38 | + assert(defaultConditionsSet !== undefined); |
| 39 | + return defaultConditionsSet; |
| 40 | +} |
| 41 | + |
| 42 | +// This function is called during pre-execution, before any user code is run. |
| 43 | +function initializeDefaultConditions() { |
| 44 | + const userConditions = getOptionValue('--conditions'); |
| 45 | + const noAddons = getOptionValue('--no-addons'); |
| 46 | + const addonConditions = noAddons ? [] : ['node-addons']; |
| 47 | + |
| 48 | + defaultConditions = ObjectFreeze([ |
| 49 | + 'node', |
| 50 | + 'import', |
| 51 | + ...addonConditions, |
| 52 | + ...userConditions, |
| 53 | + ]); |
| 54 | + defaultConditionsSet = new SafeSet(defaultConditions); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * @param {string[]} [conditions] |
| 59 | + * @returns {Set<string>} |
| 60 | + */ |
| 61 | +function getConditionsSet(conditions) { |
| 62 | + if (conditions !== undefined && conditions !== getDefaultConditions()) { |
| 63 | + if (!ArrayIsArray(conditions)) { |
| 64 | + throw new ERR_INVALID_ARG_VALUE('conditions', conditions, |
| 65 | + 'expected an array'); |
| 66 | + } |
| 67 | + return new SafeSet(conditions); |
| 68 | + } |
| 69 | + return getDefaultConditionsSet(); |
| 70 | +} |
| 71 | + |
| 72 | +function initializeImportMetaObject(wrap, meta) { |
| 73 | + if (callbackMap.has(wrap)) { |
| 74 | + const { initializeImportMeta } = callbackMap.get(wrap); |
| 75 | + if (initializeImportMeta !== undefined) { |
| 76 | + initializeImportMeta(meta, getModuleFromWrap(wrap) || wrap); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +async function importModuleDynamicallyCallback(wrap, specifier, assertions) { |
| 82 | + if (callbackMap.has(wrap)) { |
| 83 | + const { importModuleDynamically } = callbackMap.get(wrap); |
| 84 | + if (importModuleDynamically !== undefined) { |
| 85 | + return importModuleDynamically( |
| 86 | + specifier, getModuleFromWrap(wrap) || wrap, assertions); |
| 87 | + } |
| 88 | + } |
| 89 | + throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING(); |
| 90 | +} |
| 91 | + |
| 92 | +function initializeESM() { |
| 93 | + initializeDefaultConditions(); |
| 94 | + // Setup per-isolate callbacks that locate data or callbacks that we keep |
| 95 | + // track of for different ESM modules. |
| 96 | + setInitializeImportMetaObjectCallback(initializeImportMetaObject); |
| 97 | + setImportModuleDynamicallyCallback(importModuleDynamicallyCallback); |
| 98 | +} |
| 99 | + |
| 100 | +module.exports = { |
| 101 | + setCallbackForWrap, |
| 102 | + initializeESM, |
| 103 | + getDefaultConditions, |
| 104 | + getConditionsSet, |
| 105 | +}; |
0 commit comments