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

100% Statements 49/49
100% Branches 11/11
100% Functions 3/3
100% Lines 49/49

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 50139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 139x 3x 3x 18x 18x 18x 18x 15x 15x 18x 3x 3x 139x 135x 135x 135x 135x 135x 3x 3x 135x 135x 135x 129x 135x 12x 12x 12x 12x 135x 135x 135x 139x 139x 139x 139x  
'use strict';
 
const { getOptionValue } = require('internal/options');
const experimentalImportMetaResolve =
  getOptionValue('--experimental-import-meta-resolve');
const { fetchModule } = require('internal/modules/esm/fetch_module');
const { URL } = require('internal/url');
const {
  PromisePrototypeThen,
  PromiseReject,
  StringPrototypeStartsWith,
} = primordials;
const asyncESM = require('internal/process/esm_loader');
 
function createImportMetaResolve(defaultParentUrl) {
  return async function resolve(specifier, parentUrl = defaultParentUrl) {
    return PromisePrototypeThen(
      asyncESM.esmLoader.resolve(specifier, parentUrl),
      ({ url }) => url,
      (error) => (
        error.code === 'ERR_UNSUPPORTED_DIR_IMPORT' ?
          error.url : PromiseReject(error))
    );
  };
}
 
function initializeImportMeta(meta, context) {
  let url = context.url;
 
  // Alphabetical
  if (experimentalImportMetaResolve) {
    meta.resolve = createImportMetaResolve(url);
  }
 
  if (
    StringPrototypeStartsWith(url, 'http:') ||
    StringPrototypeStartsWith(url, 'https:')
  ) {
    // The request & response have already settled, so they are in fetchModule's
    // cache, in which case, fetchModule returns immediately and synchronously
    url = fetchModule(new URL(url), context).resolvedHREF;
  }
 
  meta.url = url;
}
 
module.exports = {
  initializeImportMeta
};