All files / lib/internal/process esm_loader.js

93.67% Statements 74/79
89.47% Branches 17/19
100% Functions 4/4
93.67% Lines 74/79

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 80139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 85x 85x 85x 85x 84x 84x 85x 139x 139x 139x 179x 179x 179x 179x 178x 178x 178x 179x 1x 139x 139x 139x 139x 139x 155x 155x 155x 155x 155x 20x 20x 20x 20x     20x 20x 20x 20x 20x 20x 20x 17x 17x 17x 17x 20x 155x 139x 139x 155x 155x 152x 155x 31x       31x 31x 31x 31x 31x 139x  
'use strict';
 
const {
  ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING,
} = require('internal/errors').codes;
const { Loader } = require('internal/modules/esm/loader');
const {
  hasUncaughtExceptionCaptureCallback,
} = require('internal/process/execution');
const { pathToFileURL } = require('internal/url');
const {
  getModuleFromWrap,
} = require('internal/vm/module');
 
exports.initializeImportMetaObject = function(wrap, meta) {
  const { callbackMap } = internalBinding('module_wrap');
  if (callbackMap.has(wrap)) {
    const { initializeImportMeta } = callbackMap.get(wrap);
    if (initializeImportMeta !== undefined) {
      initializeImportMeta(meta, getModuleFromWrap(wrap) || wrap);
    }
  }
};
 
exports.importModuleDynamicallyCallback = async function(wrap, specifier) {
  const { callbackMap } = internalBinding('module_wrap');
  if (callbackMap.has(wrap)) {
    const { importModuleDynamically } = callbackMap.get(wrap);
    if (importModuleDynamically !== undefined) {
      return importModuleDynamically(
        specifier, getModuleFromWrap(wrap) || wrap);
    }
  }
  throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING();
};
 
let ESMLoader = new Loader();
exports.ESMLoader = ESMLoader;
 
async function initializeLoader() {
  const { getOptionValue } = require('internal/options');
  const userLoader = getOptionValue('--experimental-loader');
  if (!userLoader)
    return;
  let cwd;
  try {
    cwd = process.cwd() + '/';
  } catch {
    cwd = 'file:///';
  }
  // If --experimental-loader is specified, create a loader with user hooks.
  // Otherwise create the default loader.
  const { emitExperimentalWarning } = require('internal/util');
  emitExperimentalWarning('--experimental-loader');
  return (async () => {
    const hooks =
        await ESMLoader.import(userLoader, pathToFileURL(cwd).href);
    ESMLoader = new Loader();
    ESMLoader.hook(hooks);
    ESMLoader.runGlobalPreloadCode();
    return exports.ESMLoader = ESMLoader;
  })();
}
 
exports.loadESM = async function loadESM(callback) {
  try {
    await initializeLoader();
    await callback(ESMLoader);
  } catch (err) {
    if (hasUncaughtExceptionCaptureCallback()) {
      process._fatalException(err);
      return;
    }
    internalBinding('errors').triggerUncaughtException(
      err,
      true /* fromPromise */
    );
  }
};