11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  ArrayIsArray,
51cb0ef41Sopenharmony_ci  ArrayPrototypeJoin,
61cb0ef41Sopenharmony_ci  ArrayPrototypeShift,
71cb0ef41Sopenharmony_ci  JSONStringify,
81cb0ef41Sopenharmony_ci  ObjectGetOwnPropertyNames,
91cb0ef41Sopenharmony_ci  ObjectPrototypeHasOwnProperty,
101cb0ef41Sopenharmony_ci  RegExp,
111cb0ef41Sopenharmony_ci  RegExpPrototypeExec,
121cb0ef41Sopenharmony_ci  RegExpPrototypeSymbolReplace,
131cb0ef41Sopenharmony_ci  SafeMap,
141cb0ef41Sopenharmony_ci  SafeSet,
151cb0ef41Sopenharmony_ci  String,
161cb0ef41Sopenharmony_ci  StringPrototypeEndsWith,
171cb0ef41Sopenharmony_ci  StringPrototypeIncludes,
181cb0ef41Sopenharmony_ci  StringPrototypeIndexOf,
191cb0ef41Sopenharmony_ci  StringPrototypeLastIndexOf,
201cb0ef41Sopenharmony_ci  StringPrototypeReplace,
211cb0ef41Sopenharmony_ci  StringPrototypeSlice,
221cb0ef41Sopenharmony_ci  StringPrototypeSplit,
231cb0ef41Sopenharmony_ci  StringPrototypeStartsWith,
241cb0ef41Sopenharmony_ci} = primordials;
251cb0ef41Sopenharmony_ciconst internalFS = require('internal/fs/utils');
261cb0ef41Sopenharmony_ciconst { BuiltinModule } = require('internal/bootstrap/realm');
271cb0ef41Sopenharmony_ciconst { realpathSync } = require('fs');
281cb0ef41Sopenharmony_ciconst { getOptionValue } = require('internal/options');
291cb0ef41Sopenharmony_ciconst pendingDeprecation = getOptionValue('--pending-deprecation');
301cb0ef41Sopenharmony_ci// Do not eagerly grab .manifest, it may be in TDZ
311cb0ef41Sopenharmony_ciconst policy = getOptionValue('--experimental-policy') ?
321cb0ef41Sopenharmony_ci  require('internal/process/policy') :
331cb0ef41Sopenharmony_ci  null;
341cb0ef41Sopenharmony_ciconst { sep, relative, resolve, toNamespacedPath } = require('path');
351cb0ef41Sopenharmony_ciconst preserveSymlinks = getOptionValue('--preserve-symlinks');
361cb0ef41Sopenharmony_ciconst preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
371cb0ef41Sopenharmony_ciconst experimentalNetworkImports =
381cb0ef41Sopenharmony_ci  getOptionValue('--experimental-network-imports');
391cb0ef41Sopenharmony_ciconst inputTypeFlag = getOptionValue('--input-type');
401cb0ef41Sopenharmony_ciconst { URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require('internal/url');
411cb0ef41Sopenharmony_ciconst { getCWDURL } = require('internal/util');
421cb0ef41Sopenharmony_ciconst { canParse: URLCanParse } = internalBinding('url');
431cb0ef41Sopenharmony_ciconst {
441cb0ef41Sopenharmony_ci  ERR_INPUT_TYPE_NOT_ALLOWED,
451cb0ef41Sopenharmony_ci  ERR_INVALID_ARG_TYPE,
461cb0ef41Sopenharmony_ci  ERR_INVALID_MODULE_SPECIFIER,
471cb0ef41Sopenharmony_ci  ERR_INVALID_PACKAGE_CONFIG,
481cb0ef41Sopenharmony_ci  ERR_INVALID_PACKAGE_TARGET,
491cb0ef41Sopenharmony_ci  ERR_MANIFEST_DEPENDENCY_MISSING,
501cb0ef41Sopenharmony_ci  ERR_MODULE_NOT_FOUND,
511cb0ef41Sopenharmony_ci  ERR_PACKAGE_IMPORT_NOT_DEFINED,
521cb0ef41Sopenharmony_ci  ERR_PACKAGE_PATH_NOT_EXPORTED,
531cb0ef41Sopenharmony_ci  ERR_UNSUPPORTED_DIR_IMPORT,
541cb0ef41Sopenharmony_ci  ERR_NETWORK_IMPORT_DISALLOWED,
551cb0ef41Sopenharmony_ci} = require('internal/errors').codes;
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ciconst { Module: CJSModule } = require('internal/modules/cjs/loader');
581cb0ef41Sopenharmony_ciconst { getPackageScopeConfig } = require('internal/modules/esm/package_config');
591cb0ef41Sopenharmony_ciconst { getConditionsSet } = require('internal/modules/esm/utils');
601cb0ef41Sopenharmony_ciconst packageJsonReader = require('internal/modules/package_json_reader');
611cb0ef41Sopenharmony_ciconst { internalModuleStat } = internalBinding('fs');
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci/**
641cb0ef41Sopenharmony_ci * @typedef {import('internal/modules/esm/package_config.js').PackageConfig} PackageConfig
651cb0ef41Sopenharmony_ci */
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciconst emittedPackageWarnings = new SafeSet();
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci/**
711cb0ef41Sopenharmony_ci * Emits a deprecation warning for the use of a deprecated trailing slash pattern mapping in the "exports" field
721cb0ef41Sopenharmony_ci * module resolution of a package.
731cb0ef41Sopenharmony_ci * @param {string} match - The deprecated trailing slash pattern mapping.
741cb0ef41Sopenharmony_ci * @param {string} pjsonUrl - The URL of the package.json file.
751cb0ef41Sopenharmony_ci * @param {string} base - The URL of the module that imported the package.
761cb0ef41Sopenharmony_ci */
771cb0ef41Sopenharmony_cifunction emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) {
781cb0ef41Sopenharmony_ci  const pjsonPath = fileURLToPath(pjsonUrl);
791cb0ef41Sopenharmony_ci  if (emittedPackageWarnings.has(pjsonPath + '|' + match)) { return; }
801cb0ef41Sopenharmony_ci  emittedPackageWarnings.add(pjsonPath + '|' + match);
811cb0ef41Sopenharmony_ci  process.emitWarning(
821cb0ef41Sopenharmony_ci    `Use of deprecated trailing slash pattern mapping "${match}" in the ` +
831cb0ef41Sopenharmony_ci    `"exports" field module resolution of the package at ${pjsonPath}${
841cb0ef41Sopenharmony_ci      base ? ` imported from ${fileURLToPath(base)}` :
851cb0ef41Sopenharmony_ci        ''}. Mapping specifiers ending in "/" is no longer supported.`,
861cb0ef41Sopenharmony_ci    'DeprecationWarning',
871cb0ef41Sopenharmony_ci    'DEP0155',
881cb0ef41Sopenharmony_ci  );
891cb0ef41Sopenharmony_ci}
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ciconst doubleSlashRegEx = /[/\\][/\\]/;
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci/**
941cb0ef41Sopenharmony_ci * Emits a deprecation warning for invalid segment in module resolution.
951cb0ef41Sopenharmony_ci * @param {string} target - The target module.
961cb0ef41Sopenharmony_ci * @param {string} request - The requested module.
971cb0ef41Sopenharmony_ci * @param {string} match - The matched module.
981cb0ef41Sopenharmony_ci * @param {string} pjsonUrl - The package.json URL.
991cb0ef41Sopenharmony_ci * @param {boolean} internal - Whether the module is in the "imports" or "exports" field.
1001cb0ef41Sopenharmony_ci * @param {string} base - The base URL.
1011cb0ef41Sopenharmony_ci * @param {boolean} isTarget - Whether the target is a module.
1021cb0ef41Sopenharmony_ci */
1031cb0ef41Sopenharmony_cifunction emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, internal, base, isTarget) {
1041cb0ef41Sopenharmony_ci  if (!pendingDeprecation) { return; }
1051cb0ef41Sopenharmony_ci  const pjsonPath = fileURLToPath(pjsonUrl);
1061cb0ef41Sopenharmony_ci  const double = RegExpPrototypeExec(doubleSlashRegEx, isTarget ? target : request) !== null;
1071cb0ef41Sopenharmony_ci  process.emitWarning(
1081cb0ef41Sopenharmony_ci    `Use of deprecated ${double ? 'double slash' :
1091cb0ef41Sopenharmony_ci      'leading or trailing slash matching'} resolving "${target}" for module ` +
1101cb0ef41Sopenharmony_ci      `request "${request}" ${request !== match ? `matched to "${match}" ` : ''
1111cb0ef41Sopenharmony_ci      }in the "${internal ? 'imports' : 'exports'}" field module resolution of the package at ${
1121cb0ef41Sopenharmony_ci        pjsonPath}${base ? ` imported from ${fileURLToPath(base)}` : ''}.`,
1131cb0ef41Sopenharmony_ci    'DeprecationWarning',
1141cb0ef41Sopenharmony_ci    'DEP0166',
1151cb0ef41Sopenharmony_ci  );
1161cb0ef41Sopenharmony_ci}
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_ci/**
1191cb0ef41Sopenharmony_ci * Emits a deprecation warning if the given URL is a module and
1201cb0ef41Sopenharmony_ci * the package.json file does not define a "main" or "exports" field.
1211cb0ef41Sopenharmony_ci * @param {URL} url - The URL of the module being resolved.
1221cb0ef41Sopenharmony_ci * @param {URL} packageJSONUrl - The URL of the package.json file for the module.
1231cb0ef41Sopenharmony_ci * @param {string | URL} [base] - The base URL for the module being resolved.
1241cb0ef41Sopenharmony_ci * @param {string} [main] - The "main" field from the package.json file.
1251cb0ef41Sopenharmony_ci */
1261cb0ef41Sopenharmony_cifunction emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
1271cb0ef41Sopenharmony_ci  const format = defaultGetFormatWithoutErrors(url);
1281cb0ef41Sopenharmony_ci  if (format !== 'module') { return; }
1291cb0ef41Sopenharmony_ci  const path = fileURLToPath(url);
1301cb0ef41Sopenharmony_ci  const pkgPath = fileURLToPath(new URL('.', packageJSONUrl));
1311cb0ef41Sopenharmony_ci  const basePath = fileURLToPath(base);
1321cb0ef41Sopenharmony_ci  if (!main) {
1331cb0ef41Sopenharmony_ci    process.emitWarning(
1341cb0ef41Sopenharmony_ci      `No "main" or "exports" field defined in the package.json for ${pkgPath
1351cb0ef41Sopenharmony_ci      } resolving the main entry point "${
1361cb0ef41Sopenharmony_ci        StringPrototypeSlice(path, pkgPath.length)}", imported from ${basePath
1371cb0ef41Sopenharmony_ci      }.\nDefault "index" lookups for the main are deprecated for ES modules.`,
1381cb0ef41Sopenharmony_ci      'DeprecationWarning',
1391cb0ef41Sopenharmony_ci      'DEP0151',
1401cb0ef41Sopenharmony_ci    );
1411cb0ef41Sopenharmony_ci  } else if (resolve(pkgPath, main) !== path) {
1421cb0ef41Sopenharmony_ci    process.emitWarning(
1431cb0ef41Sopenharmony_ci      `Package ${pkgPath} has a "main" field set to "${main}", ` +
1441cb0ef41Sopenharmony_ci      `excluding the full filename and extension to the resolved file at "${
1451cb0ef41Sopenharmony_ci        StringPrototypeSlice(path, pkgPath.length)}", imported from ${
1461cb0ef41Sopenharmony_ci        basePath}.\n Automatic extension resolution of the "main" field is ` +
1471cb0ef41Sopenharmony_ci      'deprecated for ES modules.',
1481cb0ef41Sopenharmony_ci      'DeprecationWarning',
1491cb0ef41Sopenharmony_ci      'DEP0151',
1501cb0ef41Sopenharmony_ci    );
1511cb0ef41Sopenharmony_ci  }
1521cb0ef41Sopenharmony_ci}
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ciconst realpathCache = new SafeMap();
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci/**
1571cb0ef41Sopenharmony_ci * @param {string | URL} url
1581cb0ef41Sopenharmony_ci * @returns {boolean}
1591cb0ef41Sopenharmony_ci */
1601cb0ef41Sopenharmony_cifunction fileExists(url) {
1611cb0ef41Sopenharmony_ci  return internalModuleStat(toNamespacedPath(toPathIfFileURL(url))) === 0;
1621cb0ef41Sopenharmony_ci}
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci/**
1651cb0ef41Sopenharmony_ci * Legacy CommonJS main resolution:
1661cb0ef41Sopenharmony_ci * 1. let M = pkg_url + (json main field)
1671cb0ef41Sopenharmony_ci * 2. TRY(M, M.js, M.json, M.node)
1681cb0ef41Sopenharmony_ci * 3. TRY(M/index.js, M/index.json, M/index.node)
1691cb0ef41Sopenharmony_ci * 4. TRY(pkg_url/index.js, pkg_url/index.json, pkg_url/index.node)
1701cb0ef41Sopenharmony_ci * 5. NOT_FOUND
1711cb0ef41Sopenharmony_ci * @param {URL} packageJSONUrl
1721cb0ef41Sopenharmony_ci * @param {PackageConfig} packageConfig
1731cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base
1741cb0ef41Sopenharmony_ci * @returns {URL}
1751cb0ef41Sopenharmony_ci */
1761cb0ef41Sopenharmony_cifunction legacyMainResolve(packageJSONUrl, packageConfig, base) {
1771cb0ef41Sopenharmony_ci  let guess;
1781cb0ef41Sopenharmony_ci  if (packageConfig.main !== undefined) {
1791cb0ef41Sopenharmony_ci    // Note: fs check redundances will be handled by Descriptor cache here.
1801cb0ef41Sopenharmony_ci    if (fileExists(guess = new URL(`./${packageConfig.main}`, packageJSONUrl))) {
1811cb0ef41Sopenharmony_ci      return guess;
1821cb0ef41Sopenharmony_ci    } else if (fileExists(guess = new URL(`./${packageConfig.main}.js`, packageJSONUrl))) {
1831cb0ef41Sopenharmony_ci      // Handled below.
1841cb0ef41Sopenharmony_ci    } else if (fileExists(guess = new URL(`./${packageConfig.main}.json`, packageJSONUrl))) {
1851cb0ef41Sopenharmony_ci      // Handled below.
1861cb0ef41Sopenharmony_ci    } else if (fileExists(guess = new URL(`./${packageConfig.main}.node`, packageJSONUrl))) {
1871cb0ef41Sopenharmony_ci      // Handled below.
1881cb0ef41Sopenharmony_ci    } else if (fileExists(guess = new URL(`./${packageConfig.main}/index.js`, packageJSONUrl))) {
1891cb0ef41Sopenharmony_ci      // Handled below.
1901cb0ef41Sopenharmony_ci    } else if (fileExists(guess = new URL(`./${packageConfig.main}/index.json`, packageJSONUrl))) {
1911cb0ef41Sopenharmony_ci      // Handled below.
1921cb0ef41Sopenharmony_ci    } else if (fileExists(guess = new URL(`./${packageConfig.main}/index.node`, packageJSONUrl))) {
1931cb0ef41Sopenharmony_ci      // Handled below.
1941cb0ef41Sopenharmony_ci    } else {
1951cb0ef41Sopenharmony_ci      guess = undefined;
1961cb0ef41Sopenharmony_ci    }
1971cb0ef41Sopenharmony_ci    if (guess) {
1981cb0ef41Sopenharmony_ci      emitLegacyIndexDeprecation(guess, packageJSONUrl, base,
1991cb0ef41Sopenharmony_ci                                 packageConfig.main);
2001cb0ef41Sopenharmony_ci      return guess;
2011cb0ef41Sopenharmony_ci    }
2021cb0ef41Sopenharmony_ci    // Fallthrough.
2031cb0ef41Sopenharmony_ci  }
2041cb0ef41Sopenharmony_ci  if (fileExists(guess = new URL('./index.js', packageJSONUrl))) {
2051cb0ef41Sopenharmony_ci    // Handled below.
2061cb0ef41Sopenharmony_ci  } else if (fileExists(guess = new URL('./index.json', packageJSONUrl))) {
2071cb0ef41Sopenharmony_ci    // Handled below.
2081cb0ef41Sopenharmony_ci  } else if (fileExists(guess = new URL('./index.node', packageJSONUrl))) {
2091cb0ef41Sopenharmony_ci    // Handled below.
2101cb0ef41Sopenharmony_ci  } else {
2111cb0ef41Sopenharmony_ci    guess = undefined;
2121cb0ef41Sopenharmony_ci  }
2131cb0ef41Sopenharmony_ci  if (guess) {
2141cb0ef41Sopenharmony_ci    emitLegacyIndexDeprecation(guess, packageJSONUrl, base, packageConfig.main);
2151cb0ef41Sopenharmony_ci    return guess;
2161cb0ef41Sopenharmony_ci  }
2171cb0ef41Sopenharmony_ci  // Not found.
2181cb0ef41Sopenharmony_ci  throw new ERR_MODULE_NOT_FOUND(
2191cb0ef41Sopenharmony_ci    fileURLToPath(new URL('.', packageJSONUrl)), fileURLToPath(base));
2201cb0ef41Sopenharmony_ci}
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci/**
2231cb0ef41Sopenharmony_ci * @param {URL} search
2241cb0ef41Sopenharmony_ci * @returns {URL | undefined}
2251cb0ef41Sopenharmony_ci */
2261cb0ef41Sopenharmony_cifunction resolveExtensionsWithTryExactName(search) {
2271cb0ef41Sopenharmony_ci  if (fileExists(search)) { return search; }
2281cb0ef41Sopenharmony_ci  return resolveExtensions(search);
2291cb0ef41Sopenharmony_ci}
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ciconst extensions = ['.js', '.json', '.node', '.mjs'];
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci/**
2341cb0ef41Sopenharmony_ci * @param {URL} search
2351cb0ef41Sopenharmony_ci * @returns {URL | undefined}
2361cb0ef41Sopenharmony_ci */
2371cb0ef41Sopenharmony_cifunction resolveExtensions(search) {
2381cb0ef41Sopenharmony_ci  for (let i = 0; i < extensions.length; i++) {
2391cb0ef41Sopenharmony_ci    const extension = extensions[i];
2401cb0ef41Sopenharmony_ci    const guess = new URL(`${search.pathname}${extension}`, search);
2411cb0ef41Sopenharmony_ci    if (fileExists(guess)) { return guess; }
2421cb0ef41Sopenharmony_ci  }
2431cb0ef41Sopenharmony_ci  return undefined;
2441cb0ef41Sopenharmony_ci}
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci/**
2471cb0ef41Sopenharmony_ci * @param {URL} search
2481cb0ef41Sopenharmony_ci * @returns {URL | undefined}
2491cb0ef41Sopenharmony_ci */
2501cb0ef41Sopenharmony_cifunction resolveDirectoryEntry(search) {
2511cb0ef41Sopenharmony_ci  const dirPath = fileURLToPath(search);
2521cb0ef41Sopenharmony_ci  const pkgJsonPath = resolve(dirPath, 'package.json');
2531cb0ef41Sopenharmony_ci  if (fileExists(pkgJsonPath)) {
2541cb0ef41Sopenharmony_ci    const pkgJson = packageJsonReader.read(pkgJsonPath);
2551cb0ef41Sopenharmony_ci    if (pkgJson.exists) {
2561cb0ef41Sopenharmony_ci      const { main } = pkgJson;
2571cb0ef41Sopenharmony_ci      if (main != null) {
2581cb0ef41Sopenharmony_ci        const mainUrl = pathToFileURL(resolve(dirPath, main));
2591cb0ef41Sopenharmony_ci        return resolveExtensionsWithTryExactName(mainUrl);
2601cb0ef41Sopenharmony_ci      }
2611cb0ef41Sopenharmony_ci    }
2621cb0ef41Sopenharmony_ci  }
2631cb0ef41Sopenharmony_ci  return resolveExtensions(new URL('index', search));
2641cb0ef41Sopenharmony_ci}
2651cb0ef41Sopenharmony_ci
2661cb0ef41Sopenharmony_ciconst encodedSepRegEx = /%2F|%5C/i;
2671cb0ef41Sopenharmony_ci/**
2681cb0ef41Sopenharmony_ci * Finalizes the resolution of a module specifier by checking if the resolved pathname contains encoded "/" or "\\"
2691cb0ef41Sopenharmony_ci * characters, checking if the resolved pathname is a directory or file, and resolving any symlinks if necessary.
2701cb0ef41Sopenharmony_ci * @param {URL} resolved - The resolved URL object.
2711cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base - The base URL object.
2721cb0ef41Sopenharmony_ci * @param {boolean} preserveSymlinks - Whether to preserve symlinks or not.
2731cb0ef41Sopenharmony_ci * @returns {URL} - The finalized URL object.
2741cb0ef41Sopenharmony_ci * @throws {ERR_INVALID_MODULE_SPECIFIER} - If the resolved pathname contains encoded "/" or "\\" characters.
2751cb0ef41Sopenharmony_ci * @throws {ERR_UNSUPPORTED_DIR_IMPORT} - If the resolved pathname is a directory.
2761cb0ef41Sopenharmony_ci * @throws {ERR_MODULE_NOT_FOUND} - If the resolved pathname is not a file.
2771cb0ef41Sopenharmony_ci */
2781cb0ef41Sopenharmony_cifunction finalizeResolution(resolved, base, preserveSymlinks) {
2791cb0ef41Sopenharmony_ci  if (RegExpPrototypeExec(encodedSepRegEx, resolved.pathname) !== null) {
2801cb0ef41Sopenharmony_ci    throw new ERR_INVALID_MODULE_SPECIFIER(
2811cb0ef41Sopenharmony_ci      resolved.pathname, 'must not include encoded "/" or "\\" characters',
2821cb0ef41Sopenharmony_ci      fileURLToPath(base));
2831cb0ef41Sopenharmony_ci  }
2841cb0ef41Sopenharmony_ci
2851cb0ef41Sopenharmony_ci  let path;
2861cb0ef41Sopenharmony_ci  try {
2871cb0ef41Sopenharmony_ci    path = fileURLToPath(resolved);
2881cb0ef41Sopenharmony_ci  } catch (err) {
2891cb0ef41Sopenharmony_ci    const { setOwnProperty } = require('internal/util');
2901cb0ef41Sopenharmony_ci    setOwnProperty(err, 'input', `${resolved}`);
2911cb0ef41Sopenharmony_ci    setOwnProperty(err, 'module', `${base}`);
2921cb0ef41Sopenharmony_ci    throw err;
2931cb0ef41Sopenharmony_ci  }
2941cb0ef41Sopenharmony_ci
2951cb0ef41Sopenharmony_ci  if (getOptionValue('--experimental-specifier-resolution') === 'node') {
2961cb0ef41Sopenharmony_ci    let file = resolveExtensionsWithTryExactName(resolved);
2971cb0ef41Sopenharmony_ci
2981cb0ef41Sopenharmony_ci    // Directory
2991cb0ef41Sopenharmony_ci    if (file === undefined) {
3001cb0ef41Sopenharmony_ci      file = StringPrototypeEndsWith(path, '/') ?
3011cb0ef41Sopenharmony_ci        (resolveDirectoryEntry(resolved) || resolved) : resolveDirectoryEntry(new URL(`${resolved}/`));
3021cb0ef41Sopenharmony_ci
3031cb0ef41Sopenharmony_ci      if (file === resolved) { return file; }
3041cb0ef41Sopenharmony_ci
3051cb0ef41Sopenharmony_ci      if (file === undefined) {
3061cb0ef41Sopenharmony_ci        throw new ERR_MODULE_NOT_FOUND(
3071cb0ef41Sopenharmony_ci          resolved.pathname, fileURLToPath(base), 'module');
3081cb0ef41Sopenharmony_ci      }
3091cb0ef41Sopenharmony_ci    }
3101cb0ef41Sopenharmony_ci    // If `preserveSymlinks` is false, `resolved` is returned and `path`
3111cb0ef41Sopenharmony_ci    // is used only to check that the resolved path exists.
3121cb0ef41Sopenharmony_ci    resolved = file;
3131cb0ef41Sopenharmony_ci    path = fileURLToPath(resolved);
3141cb0ef41Sopenharmony_ci  }
3151cb0ef41Sopenharmony_ci
3161cb0ef41Sopenharmony_ci  const stats = internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
3171cb0ef41Sopenharmony_ci    StringPrototypeSlice(path, -1) : path));
3181cb0ef41Sopenharmony_ci
3191cb0ef41Sopenharmony_ci  // Check for stats.isDirectory()
3201cb0ef41Sopenharmony_ci  if (stats === 1) {
3211cb0ef41Sopenharmony_ci    throw new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base), String(resolved));
3221cb0ef41Sopenharmony_ci  } else if (stats !== 0) {
3231cb0ef41Sopenharmony_ci    // Check for !stats.isFile()
3241cb0ef41Sopenharmony_ci    if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
3251cb0ef41Sopenharmony_ci      process.send({ 'watch:require': [path || resolved.pathname] });
3261cb0ef41Sopenharmony_ci    }
3271cb0ef41Sopenharmony_ci    throw new ERR_MODULE_NOT_FOUND(
3281cb0ef41Sopenharmony_ci      path || resolved.pathname, base && fileURLToPath(base), resolved);
3291cb0ef41Sopenharmony_ci  }
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci  if (!preserveSymlinks) {
3321cb0ef41Sopenharmony_ci    const real = realpathSync(path, {
3331cb0ef41Sopenharmony_ci      [internalFS.realpathCacheKey]: realpathCache,
3341cb0ef41Sopenharmony_ci    });
3351cb0ef41Sopenharmony_ci    const { search, hash } = resolved;
3361cb0ef41Sopenharmony_ci    resolved =
3371cb0ef41Sopenharmony_ci        pathToFileURL(real + (StringPrototypeEndsWith(path, sep) ? '/' : ''));
3381cb0ef41Sopenharmony_ci    resolved.search = search;
3391cb0ef41Sopenharmony_ci    resolved.hash = hash;
3401cb0ef41Sopenharmony_ci  }
3411cb0ef41Sopenharmony_ci
3421cb0ef41Sopenharmony_ci  return resolved;
3431cb0ef41Sopenharmony_ci}
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_ci/**
3461cb0ef41Sopenharmony_ci * Returns an error object indicating that the specified import is not defined.
3471cb0ef41Sopenharmony_ci * @param {string} specifier - The import specifier that is not defined.
3481cb0ef41Sopenharmony_ci * @param {URL} packageJSONUrl - The URL of the package.json file, or null if not available.
3491cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base - The base URL to use for resolving relative URLs.
3501cb0ef41Sopenharmony_ci * @returns {ERR_PACKAGE_IMPORT_NOT_DEFINED} - The error object.
3511cb0ef41Sopenharmony_ci */
3521cb0ef41Sopenharmony_cifunction importNotDefined(specifier, packageJSONUrl, base) {
3531cb0ef41Sopenharmony_ci  return new ERR_PACKAGE_IMPORT_NOT_DEFINED(
3541cb0ef41Sopenharmony_ci    specifier, packageJSONUrl && fileURLToPath(new URL('.', packageJSONUrl)),
3551cb0ef41Sopenharmony_ci    fileURLToPath(base));
3561cb0ef41Sopenharmony_ci}
3571cb0ef41Sopenharmony_ci
3581cb0ef41Sopenharmony_ci/**
3591cb0ef41Sopenharmony_ci * Returns an error object indicating that the specified subpath was not exported by the package.
3601cb0ef41Sopenharmony_ci * @param {string} subpath - The subpath that was not exported.
3611cb0ef41Sopenharmony_ci * @param {URL} packageJSONUrl - The URL of the package.json file.
3621cb0ef41Sopenharmony_ci * @param {string | URL | undefined} [base] - The base URL to use for resolving the subpath.
3631cb0ef41Sopenharmony_ci * @returns {ERR_PACKAGE_PATH_NOT_EXPORTED} - The error object.
3641cb0ef41Sopenharmony_ci */
3651cb0ef41Sopenharmony_cifunction exportsNotFound(subpath, packageJSONUrl, base) {
3661cb0ef41Sopenharmony_ci  return new ERR_PACKAGE_PATH_NOT_EXPORTED(
3671cb0ef41Sopenharmony_ci    fileURLToPath(new URL('.', packageJSONUrl)), subpath,
3681cb0ef41Sopenharmony_ci    base && fileURLToPath(base));
3691cb0ef41Sopenharmony_ci}
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ci/**
3721cb0ef41Sopenharmony_ci * Throws an error indicating that the given request is not a valid subpath match for the specified pattern.
3731cb0ef41Sopenharmony_ci * @param {string} request - The request that failed to match the pattern.
3741cb0ef41Sopenharmony_ci * @param {string} match - The pattern that the request was compared against.
3751cb0ef41Sopenharmony_ci * @param {URL} packageJSONUrl - The URL of the package.json file being resolved.
3761cb0ef41Sopenharmony_ci * @param {boolean} internal - Whether the resolution is for an "imports" or "exports" field in package.json.
3771cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base - The base URL for the resolution.
3781cb0ef41Sopenharmony_ci * @throws {ERR_INVALID_MODULE_SPECIFIER} When the request is not a valid match for the pattern.
3791cb0ef41Sopenharmony_ci */
3801cb0ef41Sopenharmony_cifunction throwInvalidSubpath(request, match, packageJSONUrl, internal, base) {
3811cb0ef41Sopenharmony_ci  const reason = `request is not a valid match in pattern "${match}" for the "${
3821cb0ef41Sopenharmony_ci    internal ? 'imports' : 'exports'}" resolution of ${
3831cb0ef41Sopenharmony_ci    fileURLToPath(packageJSONUrl)}`;
3841cb0ef41Sopenharmony_ci  throw new ERR_INVALID_MODULE_SPECIFIER(request, reason,
3851cb0ef41Sopenharmony_ci                                         base && fileURLToPath(base));
3861cb0ef41Sopenharmony_ci}
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_ci/**
3891cb0ef41Sopenharmony_ci * Creates an error object for an invalid package target.
3901cb0ef41Sopenharmony_ci * @param {string} subpath - The subpath.
3911cb0ef41Sopenharmony_ci * @param {import('internal/modules/esm/package_config.js').PackageTarget} target - The target.
3921cb0ef41Sopenharmony_ci * @param {URL} packageJSONUrl - The URL of the package.json file.
3931cb0ef41Sopenharmony_ci * @param {boolean} internal - Whether the package is internal.
3941cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base - The base URL.
3951cb0ef41Sopenharmony_ci * @returns {ERR_INVALID_PACKAGE_TARGET} - The error object.
3961cb0ef41Sopenharmony_ci */
3971cb0ef41Sopenharmony_cifunction invalidPackageTarget(
3981cb0ef41Sopenharmony_ci  subpath, target, packageJSONUrl, internal, base) {
3991cb0ef41Sopenharmony_ci  if (typeof target === 'object' && target !== null) {
4001cb0ef41Sopenharmony_ci    target = JSONStringify(target, null, '');
4011cb0ef41Sopenharmony_ci  } else {
4021cb0ef41Sopenharmony_ci    target = `${target}`;
4031cb0ef41Sopenharmony_ci  }
4041cb0ef41Sopenharmony_ci  return new ERR_INVALID_PACKAGE_TARGET(
4051cb0ef41Sopenharmony_ci    fileURLToPath(new URL('.', packageJSONUrl)), subpath, target,
4061cb0ef41Sopenharmony_ci    internal, base && fileURLToPath(base));
4071cb0ef41Sopenharmony_ci}
4081cb0ef41Sopenharmony_ci
4091cb0ef41Sopenharmony_ciconst invalidSegmentRegEx = /(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))?(\\|\/|$)/i;
4101cb0ef41Sopenharmony_ciconst deprecatedInvalidSegmentRegEx = /(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))(\\|\/|$)/i;
4111cb0ef41Sopenharmony_ciconst invalidPackageNameRegEx = /^\.|%|\\/;
4121cb0ef41Sopenharmony_ciconst patternRegEx = /\*/g;
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_ci/**
4151cb0ef41Sopenharmony_ci * Resolves the package target string to a URL object.
4161cb0ef41Sopenharmony_ci * @param {string} target - The target string to resolve.
4171cb0ef41Sopenharmony_ci * @param {string} subpath - The subpath to append to the resolved URL.
4181cb0ef41Sopenharmony_ci * @param {RegExpMatchArray} match - The matched string array from the import statement.
4191cb0ef41Sopenharmony_ci * @param {string} packageJSONUrl - The URL of the package.json file.
4201cb0ef41Sopenharmony_ci * @param {string} base - The base URL to resolve the target against.
4211cb0ef41Sopenharmony_ci * @param {RegExp} pattern - The pattern to replace in the target string.
4221cb0ef41Sopenharmony_ci * @param {boolean} internal - Whether the target is internal to the package.
4231cb0ef41Sopenharmony_ci * @param {boolean} isPathMap - Whether the target is a path map.
4241cb0ef41Sopenharmony_ci * @param {string[]} conditions - The import conditions.
4251cb0ef41Sopenharmony_ci * @returns {URL} - The resolved URL object.
4261cb0ef41Sopenharmony_ci * @throws {ERR_INVALID_PACKAGE_TARGET} - If the target is invalid.
4271cb0ef41Sopenharmony_ci * @throws {ERR_INVALID_SUBPATH} - If the subpath is invalid.
4281cb0ef41Sopenharmony_ci */
4291cb0ef41Sopenharmony_cifunction resolvePackageTargetString(
4301cb0ef41Sopenharmony_ci  target,
4311cb0ef41Sopenharmony_ci  subpath,
4321cb0ef41Sopenharmony_ci  match,
4331cb0ef41Sopenharmony_ci  packageJSONUrl,
4341cb0ef41Sopenharmony_ci  base,
4351cb0ef41Sopenharmony_ci  pattern,
4361cb0ef41Sopenharmony_ci  internal,
4371cb0ef41Sopenharmony_ci  isPathMap,
4381cb0ef41Sopenharmony_ci  conditions,
4391cb0ef41Sopenharmony_ci) {
4401cb0ef41Sopenharmony_ci
4411cb0ef41Sopenharmony_ci  if (subpath !== '' && !pattern && target[target.length - 1] !== '/') {
4421cb0ef41Sopenharmony_ci    throw invalidPackageTarget(match, target, packageJSONUrl, internal, base);
4431cb0ef41Sopenharmony_ci  }
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ci  if (!StringPrototypeStartsWith(target, './')) {
4461cb0ef41Sopenharmony_ci    if (internal && !StringPrototypeStartsWith(target, '../') &&
4471cb0ef41Sopenharmony_ci        !StringPrototypeStartsWith(target, '/')) {
4481cb0ef41Sopenharmony_ci      // No need to convert target to string, since it's already presumed to be
4491cb0ef41Sopenharmony_ci      if (!URLCanParse(target)) {
4501cb0ef41Sopenharmony_ci        const exportTarget = pattern ?
4511cb0ef41Sopenharmony_ci          RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
4521cb0ef41Sopenharmony_ci          target + subpath;
4531cb0ef41Sopenharmony_ci        return packageResolve(
4541cb0ef41Sopenharmony_ci          exportTarget, packageJSONUrl, conditions);
4551cb0ef41Sopenharmony_ci      }
4561cb0ef41Sopenharmony_ci    }
4571cb0ef41Sopenharmony_ci    throw invalidPackageTarget(match, target, packageJSONUrl, internal, base);
4581cb0ef41Sopenharmony_ci  }
4591cb0ef41Sopenharmony_ci
4601cb0ef41Sopenharmony_ci  if (RegExpPrototypeExec(invalidSegmentRegEx, StringPrototypeSlice(target, 2)) !== null) {
4611cb0ef41Sopenharmony_ci    if (RegExpPrototypeExec(deprecatedInvalidSegmentRegEx, StringPrototypeSlice(target, 2)) === null) {
4621cb0ef41Sopenharmony_ci      if (!isPathMap) {
4631cb0ef41Sopenharmony_ci        const request = pattern ?
4641cb0ef41Sopenharmony_ci          StringPrototypeReplace(match, '*', () => subpath) :
4651cb0ef41Sopenharmony_ci          match + subpath;
4661cb0ef41Sopenharmony_ci        const resolvedTarget = pattern ?
4671cb0ef41Sopenharmony_ci          RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
4681cb0ef41Sopenharmony_ci          target;
4691cb0ef41Sopenharmony_ci        emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, true);
4701cb0ef41Sopenharmony_ci      }
4711cb0ef41Sopenharmony_ci    } else {
4721cb0ef41Sopenharmony_ci      throw invalidPackageTarget(match, target, packageJSONUrl, internal, base);
4731cb0ef41Sopenharmony_ci    }
4741cb0ef41Sopenharmony_ci  }
4751cb0ef41Sopenharmony_ci
4761cb0ef41Sopenharmony_ci  const resolved = new URL(target, packageJSONUrl);
4771cb0ef41Sopenharmony_ci  const resolvedPath = resolved.pathname;
4781cb0ef41Sopenharmony_ci  const packagePath = new URL('.', packageJSONUrl).pathname;
4791cb0ef41Sopenharmony_ci
4801cb0ef41Sopenharmony_ci  if (!StringPrototypeStartsWith(resolvedPath, packagePath)) {
4811cb0ef41Sopenharmony_ci    throw invalidPackageTarget(match, target, packageJSONUrl, internal, base);
4821cb0ef41Sopenharmony_ci  }
4831cb0ef41Sopenharmony_ci
4841cb0ef41Sopenharmony_ci  if (subpath === '') { return resolved; }
4851cb0ef41Sopenharmony_ci
4861cb0ef41Sopenharmony_ci  if (RegExpPrototypeExec(invalidSegmentRegEx, subpath) !== null) {
4871cb0ef41Sopenharmony_ci    const request = pattern ? StringPrototypeReplace(match, '*', () => subpath) : match + subpath;
4881cb0ef41Sopenharmony_ci    if (RegExpPrototypeExec(deprecatedInvalidSegmentRegEx, subpath) === null) {
4891cb0ef41Sopenharmony_ci      if (!isPathMap) {
4901cb0ef41Sopenharmony_ci        const resolvedTarget = pattern ?
4911cb0ef41Sopenharmony_ci          RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
4921cb0ef41Sopenharmony_ci          target;
4931cb0ef41Sopenharmony_ci        emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, false);
4941cb0ef41Sopenharmony_ci      }
4951cb0ef41Sopenharmony_ci    } else {
4961cb0ef41Sopenharmony_ci      throwInvalidSubpath(request, match, packageJSONUrl, internal, base);
4971cb0ef41Sopenharmony_ci    }
4981cb0ef41Sopenharmony_ci  }
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ci  if (pattern) {
5011cb0ef41Sopenharmony_ci    return new URL(
5021cb0ef41Sopenharmony_ci      RegExpPrototypeSymbolReplace(patternRegEx, resolved.href, () => subpath),
5031cb0ef41Sopenharmony_ci    );
5041cb0ef41Sopenharmony_ci  }
5051cb0ef41Sopenharmony_ci
5061cb0ef41Sopenharmony_ci  return new URL(subpath, resolved);
5071cb0ef41Sopenharmony_ci}
5081cb0ef41Sopenharmony_ci
5091cb0ef41Sopenharmony_ci/**
5101cb0ef41Sopenharmony_ci * Checks if the given key is a valid array index.
5111cb0ef41Sopenharmony_ci * @param {string} key - The key to check.
5121cb0ef41Sopenharmony_ci * @returns {boolean} - Returns `true` if the key is a valid array index, else `false`.
5131cb0ef41Sopenharmony_ci */
5141cb0ef41Sopenharmony_cifunction isArrayIndex(key) {
5151cb0ef41Sopenharmony_ci  const keyNum = +key;
5161cb0ef41Sopenharmony_ci  if (`${keyNum}` !== key) { return false; }
5171cb0ef41Sopenharmony_ci  return keyNum >= 0 && keyNum < 0xFFFF_FFFF;
5181cb0ef41Sopenharmony_ci}
5191cb0ef41Sopenharmony_ci
5201cb0ef41Sopenharmony_ci/**
5211cb0ef41Sopenharmony_ci * Resolves the target of a package based on the provided parameters.
5221cb0ef41Sopenharmony_ci * @param {string} packageJSONUrl - The URL of the package.json file.
5231cb0ef41Sopenharmony_ci * @param {import('internal/modules/esm/package_config.js').PackageTarget} target - The target to resolve.
5241cb0ef41Sopenharmony_ci * @param {string} subpath - The subpath to resolve.
5251cb0ef41Sopenharmony_ci * @param {string} packageSubpath - The subpath of the package to resolve.
5261cb0ef41Sopenharmony_ci * @param {string} base - The base path to resolve.
5271cb0ef41Sopenharmony_ci * @param {RegExp} pattern - The pattern to match.
5281cb0ef41Sopenharmony_ci * @param {boolean} internal - Whether the package is internal.
5291cb0ef41Sopenharmony_ci * @param {boolean} isPathMap - Whether the package is a path map.
5301cb0ef41Sopenharmony_ci * @param {Set<string>} conditions - The conditions to match.
5311cb0ef41Sopenharmony_ci * @returns {URL | null | undefined} - The resolved target, or null if not found, or undefined if not resolvable.
5321cb0ef41Sopenharmony_ci */
5331cb0ef41Sopenharmony_cifunction resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath,
5341cb0ef41Sopenharmony_ci                              base, pattern, internal, isPathMap, conditions) {
5351cb0ef41Sopenharmony_ci  if (typeof target === 'string') {
5361cb0ef41Sopenharmony_ci    return resolvePackageTargetString(
5371cb0ef41Sopenharmony_ci      target, subpath, packageSubpath, packageJSONUrl, base, pattern, internal,
5381cb0ef41Sopenharmony_ci      isPathMap, conditions);
5391cb0ef41Sopenharmony_ci  } else if (ArrayIsArray(target)) {
5401cb0ef41Sopenharmony_ci    if (target.length === 0) {
5411cb0ef41Sopenharmony_ci      return null;
5421cb0ef41Sopenharmony_ci    }
5431cb0ef41Sopenharmony_ci
5441cb0ef41Sopenharmony_ci    let lastException;
5451cb0ef41Sopenharmony_ci    for (let i = 0; i < target.length; i++) {
5461cb0ef41Sopenharmony_ci      const targetItem = target[i];
5471cb0ef41Sopenharmony_ci      let resolveResult;
5481cb0ef41Sopenharmony_ci      try {
5491cb0ef41Sopenharmony_ci        resolveResult = resolvePackageTarget(
5501cb0ef41Sopenharmony_ci          packageJSONUrl, targetItem, subpath, packageSubpath, base, pattern,
5511cb0ef41Sopenharmony_ci          internal, isPathMap, conditions);
5521cb0ef41Sopenharmony_ci      } catch (e) {
5531cb0ef41Sopenharmony_ci        lastException = e;
5541cb0ef41Sopenharmony_ci        if (e.code === 'ERR_INVALID_PACKAGE_TARGET') {
5551cb0ef41Sopenharmony_ci          continue;
5561cb0ef41Sopenharmony_ci        }
5571cb0ef41Sopenharmony_ci        throw e;
5581cb0ef41Sopenharmony_ci      }
5591cb0ef41Sopenharmony_ci      if (resolveResult === undefined) {
5601cb0ef41Sopenharmony_ci        continue;
5611cb0ef41Sopenharmony_ci      }
5621cb0ef41Sopenharmony_ci      if (resolveResult === null) {
5631cb0ef41Sopenharmony_ci        lastException = null;
5641cb0ef41Sopenharmony_ci        continue;
5651cb0ef41Sopenharmony_ci      }
5661cb0ef41Sopenharmony_ci      return resolveResult;
5671cb0ef41Sopenharmony_ci    }
5681cb0ef41Sopenharmony_ci    if (lastException === undefined || lastException === null) {
5691cb0ef41Sopenharmony_ci      return lastException;
5701cb0ef41Sopenharmony_ci    }
5711cb0ef41Sopenharmony_ci    throw lastException;
5721cb0ef41Sopenharmony_ci  } else if (typeof target === 'object' && target !== null) {
5731cb0ef41Sopenharmony_ci    const keys = ObjectGetOwnPropertyNames(target);
5741cb0ef41Sopenharmony_ci    for (let i = 0; i < keys.length; i++) {
5751cb0ef41Sopenharmony_ci      const key = keys[i];
5761cb0ef41Sopenharmony_ci      if (isArrayIndex(key)) {
5771cb0ef41Sopenharmony_ci        throw new ERR_INVALID_PACKAGE_CONFIG(
5781cb0ef41Sopenharmony_ci          fileURLToPath(packageJSONUrl), base,
5791cb0ef41Sopenharmony_ci          '"exports" cannot contain numeric property keys.');
5801cb0ef41Sopenharmony_ci      }
5811cb0ef41Sopenharmony_ci    }
5821cb0ef41Sopenharmony_ci    for (let i = 0; i < keys.length; i++) {
5831cb0ef41Sopenharmony_ci      const key = keys[i];
5841cb0ef41Sopenharmony_ci      if (key === 'default' || conditions.has(key)) {
5851cb0ef41Sopenharmony_ci        const conditionalTarget = target[key];
5861cb0ef41Sopenharmony_ci        const resolveResult = resolvePackageTarget(
5871cb0ef41Sopenharmony_ci          packageJSONUrl, conditionalTarget, subpath, packageSubpath, base,
5881cb0ef41Sopenharmony_ci          pattern, internal, isPathMap, conditions);
5891cb0ef41Sopenharmony_ci        if (resolveResult === undefined) { continue; }
5901cb0ef41Sopenharmony_ci        return resolveResult;
5911cb0ef41Sopenharmony_ci      }
5921cb0ef41Sopenharmony_ci    }
5931cb0ef41Sopenharmony_ci    return undefined;
5941cb0ef41Sopenharmony_ci  } else if (target === null) {
5951cb0ef41Sopenharmony_ci    return null;
5961cb0ef41Sopenharmony_ci  }
5971cb0ef41Sopenharmony_ci  throw invalidPackageTarget(packageSubpath, target, packageJSONUrl, internal,
5981cb0ef41Sopenharmony_ci                             base);
5991cb0ef41Sopenharmony_ci}
6001cb0ef41Sopenharmony_ci
6011cb0ef41Sopenharmony_ci/**
6021cb0ef41Sopenharmony_ci * Is the given exports object using the shorthand syntax?
6031cb0ef41Sopenharmony_ci * @param {import('internal/modules/esm/package_config.js').PackageConfig['exports']} exports
6041cb0ef41Sopenharmony_ci * @param {URL} packageJSONUrl The URL of the package.json file.
6051cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base The base URL.
6061cb0ef41Sopenharmony_ci */
6071cb0ef41Sopenharmony_cifunction isConditionalExportsMainSugar(exports, packageJSONUrl, base) {
6081cb0ef41Sopenharmony_ci  if (typeof exports === 'string' || ArrayIsArray(exports)) { return true; }
6091cb0ef41Sopenharmony_ci  if (typeof exports !== 'object' || exports === null) { return false; }
6101cb0ef41Sopenharmony_ci
6111cb0ef41Sopenharmony_ci  const keys = ObjectGetOwnPropertyNames(exports);
6121cb0ef41Sopenharmony_ci  let isConditionalSugar = false;
6131cb0ef41Sopenharmony_ci  let i = 0;
6141cb0ef41Sopenharmony_ci  for (let j = 0; j < keys.length; j++) {
6151cb0ef41Sopenharmony_ci    const key = keys[j];
6161cb0ef41Sopenharmony_ci    const curIsConditionalSugar = key === '' || key[0] !== '.';
6171cb0ef41Sopenharmony_ci    if (i++ === 0) {
6181cb0ef41Sopenharmony_ci      isConditionalSugar = curIsConditionalSugar;
6191cb0ef41Sopenharmony_ci    } else if (isConditionalSugar !== curIsConditionalSugar) {
6201cb0ef41Sopenharmony_ci      throw new ERR_INVALID_PACKAGE_CONFIG(
6211cb0ef41Sopenharmony_ci        fileURLToPath(packageJSONUrl), base,
6221cb0ef41Sopenharmony_ci        '"exports" cannot contain some keys starting with \'.\' and some not.' +
6231cb0ef41Sopenharmony_ci        ' The exports object must either be an object of package subpath keys' +
6241cb0ef41Sopenharmony_ci        ' or an object of main entry condition name keys only.');
6251cb0ef41Sopenharmony_ci    }
6261cb0ef41Sopenharmony_ci  }
6271cb0ef41Sopenharmony_ci  return isConditionalSugar;
6281cb0ef41Sopenharmony_ci}
6291cb0ef41Sopenharmony_ci
6301cb0ef41Sopenharmony_ci/**
6311cb0ef41Sopenharmony_ci * Resolves the exports of a package.
6321cb0ef41Sopenharmony_ci * @param {URL} packageJSONUrl - The URL of the package.json file.
6331cb0ef41Sopenharmony_ci * @param {string} packageSubpath - The subpath of the package to resolve.
6341cb0ef41Sopenharmony_ci * @param {import('internal/modules/esm/package_config.js').PackageConfig} packageConfig - The package metadata.
6351cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base - The base path to resolve from.
6361cb0ef41Sopenharmony_ci * @param {Set<string>} conditions - An array of conditions to match.
6371cb0ef41Sopenharmony_ci * @returns {URL} - The resolved package target.
6381cb0ef41Sopenharmony_ci */
6391cb0ef41Sopenharmony_cifunction packageExportsResolve(
6401cb0ef41Sopenharmony_ci  packageJSONUrl, packageSubpath, packageConfig, base, conditions) {
6411cb0ef41Sopenharmony_ci  let exports = packageConfig.exports;
6421cb0ef41Sopenharmony_ci  if (isConditionalExportsMainSugar(exports, packageJSONUrl, base)) {
6431cb0ef41Sopenharmony_ci    exports = { '.': exports };
6441cb0ef41Sopenharmony_ci  }
6451cb0ef41Sopenharmony_ci
6461cb0ef41Sopenharmony_ci  if (ObjectPrototypeHasOwnProperty(exports, packageSubpath) &&
6471cb0ef41Sopenharmony_ci      !StringPrototypeIncludes(packageSubpath, '*') &&
6481cb0ef41Sopenharmony_ci      !StringPrototypeEndsWith(packageSubpath, '/')) {
6491cb0ef41Sopenharmony_ci    const target = exports[packageSubpath];
6501cb0ef41Sopenharmony_ci    const resolveResult = resolvePackageTarget(
6511cb0ef41Sopenharmony_ci      packageJSONUrl, target, '', packageSubpath, base, false, false, false,
6521cb0ef41Sopenharmony_ci      conditions,
6531cb0ef41Sopenharmony_ci    );
6541cb0ef41Sopenharmony_ci
6551cb0ef41Sopenharmony_ci    if (resolveResult == null) {
6561cb0ef41Sopenharmony_ci      throw exportsNotFound(packageSubpath, packageJSONUrl, base);
6571cb0ef41Sopenharmony_ci    }
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ci    return resolveResult;
6601cb0ef41Sopenharmony_ci  }
6611cb0ef41Sopenharmony_ci
6621cb0ef41Sopenharmony_ci  let bestMatch = '';
6631cb0ef41Sopenharmony_ci  let bestMatchSubpath;
6641cb0ef41Sopenharmony_ci  const keys = ObjectGetOwnPropertyNames(exports);
6651cb0ef41Sopenharmony_ci  for (let i = 0; i < keys.length; i++) {
6661cb0ef41Sopenharmony_ci    const key = keys[i];
6671cb0ef41Sopenharmony_ci    const patternIndex = StringPrototypeIndexOf(key, '*');
6681cb0ef41Sopenharmony_ci    if (patternIndex !== -1 &&
6691cb0ef41Sopenharmony_ci        StringPrototypeStartsWith(packageSubpath,
6701cb0ef41Sopenharmony_ci                                  StringPrototypeSlice(key, 0, patternIndex))) {
6711cb0ef41Sopenharmony_ci      // When this reaches EOL, this can throw at the top of the whole function:
6721cb0ef41Sopenharmony_ci      //
6731cb0ef41Sopenharmony_ci      // if (StringPrototypeEndsWith(packageSubpath, '/'))
6741cb0ef41Sopenharmony_ci      //   throwInvalidSubpath(packageSubpath)
6751cb0ef41Sopenharmony_ci      //
6761cb0ef41Sopenharmony_ci      // To match "imports" and the spec.
6771cb0ef41Sopenharmony_ci      if (StringPrototypeEndsWith(packageSubpath, '/')) {
6781cb0ef41Sopenharmony_ci        emitTrailingSlashPatternDeprecation(packageSubpath, packageJSONUrl,
6791cb0ef41Sopenharmony_ci                                            base);
6801cb0ef41Sopenharmony_ci      }
6811cb0ef41Sopenharmony_ci      const patternTrailer = StringPrototypeSlice(key, patternIndex + 1);
6821cb0ef41Sopenharmony_ci      if (packageSubpath.length >= key.length &&
6831cb0ef41Sopenharmony_ci          StringPrototypeEndsWith(packageSubpath, patternTrailer) &&
6841cb0ef41Sopenharmony_ci          patternKeyCompare(bestMatch, key) === 1 &&
6851cb0ef41Sopenharmony_ci          StringPrototypeLastIndexOf(key, '*') === patternIndex) {
6861cb0ef41Sopenharmony_ci        bestMatch = key;
6871cb0ef41Sopenharmony_ci        bestMatchSubpath = StringPrototypeSlice(
6881cb0ef41Sopenharmony_ci          packageSubpath, patternIndex,
6891cb0ef41Sopenharmony_ci          packageSubpath.length - patternTrailer.length);
6901cb0ef41Sopenharmony_ci      }
6911cb0ef41Sopenharmony_ci    }
6921cb0ef41Sopenharmony_ci  }
6931cb0ef41Sopenharmony_ci
6941cb0ef41Sopenharmony_ci  if (bestMatch) {
6951cb0ef41Sopenharmony_ci    const target = exports[bestMatch];
6961cb0ef41Sopenharmony_ci    const resolveResult = resolvePackageTarget(
6971cb0ef41Sopenharmony_ci      packageJSONUrl,
6981cb0ef41Sopenharmony_ci      target,
6991cb0ef41Sopenharmony_ci      bestMatchSubpath,
7001cb0ef41Sopenharmony_ci      bestMatch,
7011cb0ef41Sopenharmony_ci      base,
7021cb0ef41Sopenharmony_ci      true,
7031cb0ef41Sopenharmony_ci      false,
7041cb0ef41Sopenharmony_ci      StringPrototypeEndsWith(packageSubpath, '/'),
7051cb0ef41Sopenharmony_ci      conditions);
7061cb0ef41Sopenharmony_ci
7071cb0ef41Sopenharmony_ci    if (resolveResult == null) {
7081cb0ef41Sopenharmony_ci      throw exportsNotFound(packageSubpath, packageJSONUrl, base);
7091cb0ef41Sopenharmony_ci    }
7101cb0ef41Sopenharmony_ci    return resolveResult;
7111cb0ef41Sopenharmony_ci  }
7121cb0ef41Sopenharmony_ci
7131cb0ef41Sopenharmony_ci  throw exportsNotFound(packageSubpath, packageJSONUrl, base);
7141cb0ef41Sopenharmony_ci}
7151cb0ef41Sopenharmony_ci
7161cb0ef41Sopenharmony_ci/**
7171cb0ef41Sopenharmony_ci * Compares two strings that may contain a wildcard character ('*') and returns a value indicating their order.
7181cb0ef41Sopenharmony_ci * @param {string} a - The first string to compare.
7191cb0ef41Sopenharmony_ci * @param {string} b - The second string to compare.
7201cb0ef41Sopenharmony_ci * @returns {number} - A negative number if `a` should come before `b`, a positive number if `a` should come after `b`,
7211cb0ef41Sopenharmony_ci * or 0 if they are equal.
7221cb0ef41Sopenharmony_ci */
7231cb0ef41Sopenharmony_cifunction patternKeyCompare(a, b) {
7241cb0ef41Sopenharmony_ci  const aPatternIndex = StringPrototypeIndexOf(a, '*');
7251cb0ef41Sopenharmony_ci  const bPatternIndex = StringPrototypeIndexOf(b, '*');
7261cb0ef41Sopenharmony_ci  const baseLenA = aPatternIndex === -1 ? a.length : aPatternIndex + 1;
7271cb0ef41Sopenharmony_ci  const baseLenB = bPatternIndex === -1 ? b.length : bPatternIndex + 1;
7281cb0ef41Sopenharmony_ci  if (baseLenA > baseLenB) { return -1; }
7291cb0ef41Sopenharmony_ci  if (baseLenB > baseLenA) { return 1; }
7301cb0ef41Sopenharmony_ci  if (aPatternIndex === -1) { return 1; }
7311cb0ef41Sopenharmony_ci  if (bPatternIndex === -1) { return -1; }
7321cb0ef41Sopenharmony_ci  if (a.length > b.length) { return -1; }
7331cb0ef41Sopenharmony_ci  if (b.length > a.length) { return 1; }
7341cb0ef41Sopenharmony_ci  return 0;
7351cb0ef41Sopenharmony_ci}
7361cb0ef41Sopenharmony_ci
7371cb0ef41Sopenharmony_ci/**
7381cb0ef41Sopenharmony_ci * Resolves the given import name for a package.
7391cb0ef41Sopenharmony_ci * @param {string} name - The name of the import to resolve.
7401cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base - The base URL to resolve the import from.
7411cb0ef41Sopenharmony_ci * @param {Set<string>} conditions - An object containing the import conditions.
7421cb0ef41Sopenharmony_ci * @throws {ERR_INVALID_MODULE_SPECIFIER} If the import name is not valid.
7431cb0ef41Sopenharmony_ci * @throws {ERR_PACKAGE_IMPORT_NOT_DEFINED} If the import name cannot be resolved.
7441cb0ef41Sopenharmony_ci * @returns {URL} The resolved import URL.
7451cb0ef41Sopenharmony_ci */
7461cb0ef41Sopenharmony_cifunction packageImportsResolve(name, base, conditions) {
7471cb0ef41Sopenharmony_ci  if (name === '#' || StringPrototypeStartsWith(name, '#/') ||
7481cb0ef41Sopenharmony_ci      StringPrototypeEndsWith(name, '/')) {
7491cb0ef41Sopenharmony_ci    const reason = 'is not a valid internal imports specifier name';
7501cb0ef41Sopenharmony_ci    throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base));
7511cb0ef41Sopenharmony_ci  }
7521cb0ef41Sopenharmony_ci  let packageJSONUrl;
7531cb0ef41Sopenharmony_ci  const packageConfig = getPackageScopeConfig(base);
7541cb0ef41Sopenharmony_ci  if (packageConfig.exists) {
7551cb0ef41Sopenharmony_ci    packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
7561cb0ef41Sopenharmony_ci    const imports = packageConfig.imports;
7571cb0ef41Sopenharmony_ci    if (imports) {
7581cb0ef41Sopenharmony_ci      if (ObjectPrototypeHasOwnProperty(imports, name) &&
7591cb0ef41Sopenharmony_ci          !StringPrototypeIncludes(name, '*')) {
7601cb0ef41Sopenharmony_ci        const resolveResult = resolvePackageTarget(
7611cb0ef41Sopenharmony_ci          packageJSONUrl, imports[name], '', name, base, false, true, false,
7621cb0ef41Sopenharmony_ci          conditions,
7631cb0ef41Sopenharmony_ci        );
7641cb0ef41Sopenharmony_ci        if (resolveResult != null) {
7651cb0ef41Sopenharmony_ci          return resolveResult;
7661cb0ef41Sopenharmony_ci        }
7671cb0ef41Sopenharmony_ci      } else {
7681cb0ef41Sopenharmony_ci        let bestMatch = '';
7691cb0ef41Sopenharmony_ci        let bestMatchSubpath;
7701cb0ef41Sopenharmony_ci        const keys = ObjectGetOwnPropertyNames(imports);
7711cb0ef41Sopenharmony_ci        for (let i = 0; i < keys.length; i++) {
7721cb0ef41Sopenharmony_ci          const key = keys[i];
7731cb0ef41Sopenharmony_ci          const patternIndex = StringPrototypeIndexOf(key, '*');
7741cb0ef41Sopenharmony_ci          if (patternIndex !== -1 &&
7751cb0ef41Sopenharmony_ci              StringPrototypeStartsWith(name,
7761cb0ef41Sopenharmony_ci                                        StringPrototypeSlice(key, 0,
7771cb0ef41Sopenharmony_ci                                                             patternIndex))) {
7781cb0ef41Sopenharmony_ci            const patternTrailer = StringPrototypeSlice(key, patternIndex + 1);
7791cb0ef41Sopenharmony_ci            if (name.length >= key.length &&
7801cb0ef41Sopenharmony_ci                StringPrototypeEndsWith(name, patternTrailer) &&
7811cb0ef41Sopenharmony_ci                patternKeyCompare(bestMatch, key) === 1 &&
7821cb0ef41Sopenharmony_ci                StringPrototypeLastIndexOf(key, '*') === patternIndex) {
7831cb0ef41Sopenharmony_ci              bestMatch = key;
7841cb0ef41Sopenharmony_ci              bestMatchSubpath = StringPrototypeSlice(
7851cb0ef41Sopenharmony_ci                name, patternIndex, name.length - patternTrailer.length);
7861cb0ef41Sopenharmony_ci            }
7871cb0ef41Sopenharmony_ci          }
7881cb0ef41Sopenharmony_ci        }
7891cb0ef41Sopenharmony_ci
7901cb0ef41Sopenharmony_ci        if (bestMatch) {
7911cb0ef41Sopenharmony_ci          const target = imports[bestMatch];
7921cb0ef41Sopenharmony_ci          const resolveResult = resolvePackageTarget(packageJSONUrl, target,
7931cb0ef41Sopenharmony_ci                                                     bestMatchSubpath,
7941cb0ef41Sopenharmony_ci                                                     bestMatch, base, true,
7951cb0ef41Sopenharmony_ci                                                     true, false, conditions);
7961cb0ef41Sopenharmony_ci          if (resolveResult != null) {
7971cb0ef41Sopenharmony_ci            return resolveResult;
7981cb0ef41Sopenharmony_ci          }
7991cb0ef41Sopenharmony_ci        }
8001cb0ef41Sopenharmony_ci      }
8011cb0ef41Sopenharmony_ci    }
8021cb0ef41Sopenharmony_ci  }
8031cb0ef41Sopenharmony_ci  throw importNotDefined(name, packageJSONUrl, base);
8041cb0ef41Sopenharmony_ci}
8051cb0ef41Sopenharmony_ci
8061cb0ef41Sopenharmony_ci/**
8071cb0ef41Sopenharmony_ci * Returns the package type for a given URL.
8081cb0ef41Sopenharmony_ci * @param {URL} url - The URL to get the package type for.
8091cb0ef41Sopenharmony_ci */
8101cb0ef41Sopenharmony_cifunction getPackageType(url) {
8111cb0ef41Sopenharmony_ci  const packageConfig = getPackageScopeConfig(url);
8121cb0ef41Sopenharmony_ci  return packageConfig.type;
8131cb0ef41Sopenharmony_ci}
8141cb0ef41Sopenharmony_ci
8151cb0ef41Sopenharmony_ci/**
8161cb0ef41Sopenharmony_ci * Parse a package name from a specifier.
8171cb0ef41Sopenharmony_ci * @param {string} specifier - The import specifier.
8181cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base - The parent URL.
8191cb0ef41Sopenharmony_ci */
8201cb0ef41Sopenharmony_cifunction parsePackageName(specifier, base) {
8211cb0ef41Sopenharmony_ci  let separatorIndex = StringPrototypeIndexOf(specifier, '/');
8221cb0ef41Sopenharmony_ci  let validPackageName = true;
8231cb0ef41Sopenharmony_ci  let isScoped = false;
8241cb0ef41Sopenharmony_ci  if (specifier[0] === '@') {
8251cb0ef41Sopenharmony_ci    isScoped = true;
8261cb0ef41Sopenharmony_ci    if (separatorIndex === -1 || specifier.length === 0) {
8271cb0ef41Sopenharmony_ci      validPackageName = false;
8281cb0ef41Sopenharmony_ci    } else {
8291cb0ef41Sopenharmony_ci      separatorIndex = StringPrototypeIndexOf(
8301cb0ef41Sopenharmony_ci        specifier, '/', separatorIndex + 1);
8311cb0ef41Sopenharmony_ci    }
8321cb0ef41Sopenharmony_ci  }
8331cb0ef41Sopenharmony_ci
8341cb0ef41Sopenharmony_ci  const packageName = separatorIndex === -1 ?
8351cb0ef41Sopenharmony_ci    specifier : StringPrototypeSlice(specifier, 0, separatorIndex);
8361cb0ef41Sopenharmony_ci
8371cb0ef41Sopenharmony_ci  // Package name cannot have leading . and cannot have percent-encoding or
8381cb0ef41Sopenharmony_ci  // \\ separators.
8391cb0ef41Sopenharmony_ci  if (RegExpPrototypeExec(invalidPackageNameRegEx, packageName) !== null) {
8401cb0ef41Sopenharmony_ci    validPackageName = false;
8411cb0ef41Sopenharmony_ci  }
8421cb0ef41Sopenharmony_ci
8431cb0ef41Sopenharmony_ci  if (!validPackageName) {
8441cb0ef41Sopenharmony_ci    throw new ERR_INVALID_MODULE_SPECIFIER(
8451cb0ef41Sopenharmony_ci      specifier, 'is not a valid package name', fileURLToPath(base));
8461cb0ef41Sopenharmony_ci  }
8471cb0ef41Sopenharmony_ci
8481cb0ef41Sopenharmony_ci  const packageSubpath = '.' + (separatorIndex === -1 ? '' :
8491cb0ef41Sopenharmony_ci    StringPrototypeSlice(specifier, separatorIndex));
8501cb0ef41Sopenharmony_ci
8511cb0ef41Sopenharmony_ci  return { packageName, packageSubpath, isScoped };
8521cb0ef41Sopenharmony_ci}
8531cb0ef41Sopenharmony_ci
8541cb0ef41Sopenharmony_ci/**
8551cb0ef41Sopenharmony_ci * Resolves a package specifier to a URL.
8561cb0ef41Sopenharmony_ci * @param {string} specifier - The package specifier to resolve.
8571cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base - The base URL to use for resolution.
8581cb0ef41Sopenharmony_ci * @param {Set<string>} conditions - An object containing the conditions for resolution.
8591cb0ef41Sopenharmony_ci * @returns {URL} - The resolved URL.
8601cb0ef41Sopenharmony_ci */
8611cb0ef41Sopenharmony_cifunction packageResolve(specifier, base, conditions) {
8621cb0ef41Sopenharmony_ci  if (BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
8631cb0ef41Sopenharmony_ci    return new URL('node:' + specifier);
8641cb0ef41Sopenharmony_ci  }
8651cb0ef41Sopenharmony_ci
8661cb0ef41Sopenharmony_ci  const { packageName, packageSubpath, isScoped } =
8671cb0ef41Sopenharmony_ci    parsePackageName(specifier, base);
8681cb0ef41Sopenharmony_ci
8691cb0ef41Sopenharmony_ci  // ResolveSelf
8701cb0ef41Sopenharmony_ci  const packageConfig = getPackageScopeConfig(base);
8711cb0ef41Sopenharmony_ci  if (packageConfig.exists) {
8721cb0ef41Sopenharmony_ci    const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
8731cb0ef41Sopenharmony_ci    if (packageConfig.exports != null && packageConfig.name === packageName) {
8741cb0ef41Sopenharmony_ci      return packageExportsResolve(
8751cb0ef41Sopenharmony_ci        packageJSONUrl, packageSubpath, packageConfig, base, conditions);
8761cb0ef41Sopenharmony_ci    }
8771cb0ef41Sopenharmony_ci  }
8781cb0ef41Sopenharmony_ci
8791cb0ef41Sopenharmony_ci  let packageJSONUrl =
8801cb0ef41Sopenharmony_ci    new URL('./node_modules/' + packageName + '/package.json', base);
8811cb0ef41Sopenharmony_ci  let packageJSONPath = fileURLToPath(packageJSONUrl);
8821cb0ef41Sopenharmony_ci  let lastPath;
8831cb0ef41Sopenharmony_ci  do {
8841cb0ef41Sopenharmony_ci    const stat = internalModuleStat(toNamespacedPath(StringPrototypeSlice(packageJSONPath, 0,
8851cb0ef41Sopenharmony_ci                                                                          packageJSONPath.length - 13)));
8861cb0ef41Sopenharmony_ci    // Check for !stat.isDirectory()
8871cb0ef41Sopenharmony_ci    if (stat !== 1) {
8881cb0ef41Sopenharmony_ci      lastPath = packageJSONPath;
8891cb0ef41Sopenharmony_ci      packageJSONUrl = new URL((isScoped ?
8901cb0ef41Sopenharmony_ci        '../../../../node_modules/' : '../../../node_modules/') +
8911cb0ef41Sopenharmony_ci        packageName + '/package.json', packageJSONUrl);
8921cb0ef41Sopenharmony_ci      packageJSONPath = fileURLToPath(packageJSONUrl);
8931cb0ef41Sopenharmony_ci      continue;
8941cb0ef41Sopenharmony_ci    }
8951cb0ef41Sopenharmony_ci
8961cb0ef41Sopenharmony_ci    // Package match.
8971cb0ef41Sopenharmony_ci    const packageConfig = packageJsonReader.read(packageJSONPath, { __proto__: null, specifier, base, isESM: true });
8981cb0ef41Sopenharmony_ci    if (packageConfig.exports != null) {
8991cb0ef41Sopenharmony_ci      return packageExportsResolve(
9001cb0ef41Sopenharmony_ci        packageJSONUrl, packageSubpath, packageConfig, base, conditions);
9011cb0ef41Sopenharmony_ci    }
9021cb0ef41Sopenharmony_ci    if (packageSubpath === '.') {
9031cb0ef41Sopenharmony_ci      return legacyMainResolve(
9041cb0ef41Sopenharmony_ci        packageJSONUrl,
9051cb0ef41Sopenharmony_ci        packageConfig,
9061cb0ef41Sopenharmony_ci        base,
9071cb0ef41Sopenharmony_ci      );
9081cb0ef41Sopenharmony_ci    }
9091cb0ef41Sopenharmony_ci
9101cb0ef41Sopenharmony_ci    return new URL(packageSubpath, packageJSONUrl);
9111cb0ef41Sopenharmony_ci    // Cross-platform root check.
9121cb0ef41Sopenharmony_ci  } while (packageJSONPath.length !== lastPath.length);
9131cb0ef41Sopenharmony_ci
9141cb0ef41Sopenharmony_ci  // eslint can't handle the above code.
9151cb0ef41Sopenharmony_ci  // eslint-disable-next-line no-unreachable
9161cb0ef41Sopenharmony_ci  throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
9171cb0ef41Sopenharmony_ci}
9181cb0ef41Sopenharmony_ci
9191cb0ef41Sopenharmony_ci/**
9201cb0ef41Sopenharmony_ci * Checks if a specifier is a bare specifier.
9211cb0ef41Sopenharmony_ci * @param {string} specifier - The specifier to check.
9221cb0ef41Sopenharmony_ci */
9231cb0ef41Sopenharmony_cifunction isBareSpecifier(specifier) {
9241cb0ef41Sopenharmony_ci  return specifier[0] && specifier[0] !== '/' && specifier[0] !== '.';
9251cb0ef41Sopenharmony_ci}
9261cb0ef41Sopenharmony_ci
9271cb0ef41Sopenharmony_ci/**
9281cb0ef41Sopenharmony_ci * Determines whether a specifier is a relative path.
9291cb0ef41Sopenharmony_ci * @param {string} specifier - The specifier to check.
9301cb0ef41Sopenharmony_ci */
9311cb0ef41Sopenharmony_cifunction isRelativeSpecifier(specifier) {
9321cb0ef41Sopenharmony_ci  if (specifier[0] === '.') {
9331cb0ef41Sopenharmony_ci    if (specifier.length === 1 || specifier[1] === '/') { return true; }
9341cb0ef41Sopenharmony_ci    if (specifier[1] === '.') {
9351cb0ef41Sopenharmony_ci      if (specifier.length === 2 || specifier[2] === '/') { return true; }
9361cb0ef41Sopenharmony_ci    }
9371cb0ef41Sopenharmony_ci  }
9381cb0ef41Sopenharmony_ci  return false;
9391cb0ef41Sopenharmony_ci}
9401cb0ef41Sopenharmony_ci
9411cb0ef41Sopenharmony_ci/**
9421cb0ef41Sopenharmony_ci * Determines whether a specifier should be treated as a relative or absolute path.
9431cb0ef41Sopenharmony_ci * @param {string} specifier - The specifier to check.
9441cb0ef41Sopenharmony_ci */
9451cb0ef41Sopenharmony_cifunction shouldBeTreatedAsRelativeOrAbsolutePath(specifier) {
9461cb0ef41Sopenharmony_ci  if (specifier === '') { return false; }
9471cb0ef41Sopenharmony_ci  if (specifier[0] === '/') { return true; }
9481cb0ef41Sopenharmony_ci  return isRelativeSpecifier(specifier);
9491cb0ef41Sopenharmony_ci}
9501cb0ef41Sopenharmony_ci
9511cb0ef41Sopenharmony_ci/**
9521cb0ef41Sopenharmony_ci * Resolves a module specifier to a URL.
9531cb0ef41Sopenharmony_ci * @param {string} specifier - The module specifier to resolve.
9541cb0ef41Sopenharmony_ci * @param {string | URL | undefined} base - The base URL to resolve against.
9551cb0ef41Sopenharmony_ci * @param {Set<string>} conditions - An object containing environment conditions.
9561cb0ef41Sopenharmony_ci * @param {boolean} preserveSymlinks - Whether to preserve symlinks in the resolved URL.
9571cb0ef41Sopenharmony_ci */
9581cb0ef41Sopenharmony_cifunction moduleResolve(specifier, base, conditions, preserveSymlinks) {
9591cb0ef41Sopenharmony_ci  const isRemote = base.protocol === 'http:' ||
9601cb0ef41Sopenharmony_ci    base.protocol === 'https:';
9611cb0ef41Sopenharmony_ci  // Order swapped from spec for minor perf gain.
9621cb0ef41Sopenharmony_ci  // Ok since relative URLs cannot parse as URLs.
9631cb0ef41Sopenharmony_ci  let resolved;
9641cb0ef41Sopenharmony_ci  if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
9651cb0ef41Sopenharmony_ci    resolved = new URL(specifier, base);
9661cb0ef41Sopenharmony_ci  } else if (!isRemote && specifier[0] === '#') {
9671cb0ef41Sopenharmony_ci    resolved = packageImportsResolve(specifier, base, conditions);
9681cb0ef41Sopenharmony_ci  } else {
9691cb0ef41Sopenharmony_ci    try {
9701cb0ef41Sopenharmony_ci      resolved = new URL(specifier);
9711cb0ef41Sopenharmony_ci    } catch {
9721cb0ef41Sopenharmony_ci      if (!isRemote) {
9731cb0ef41Sopenharmony_ci        resolved = packageResolve(specifier, base, conditions);
9741cb0ef41Sopenharmony_ci      }
9751cb0ef41Sopenharmony_ci    }
9761cb0ef41Sopenharmony_ci  }
9771cb0ef41Sopenharmony_ci  if (resolved.protocol !== 'file:') {
9781cb0ef41Sopenharmony_ci    return resolved;
9791cb0ef41Sopenharmony_ci  }
9801cb0ef41Sopenharmony_ci  return finalizeResolution(resolved, base, preserveSymlinks);
9811cb0ef41Sopenharmony_ci}
9821cb0ef41Sopenharmony_ci
9831cb0ef41Sopenharmony_ci/**
9841cb0ef41Sopenharmony_ci * Try to resolve an import as a CommonJS module.
9851cb0ef41Sopenharmony_ci * @param {string} specifier - The specifier to resolve.
9861cb0ef41Sopenharmony_ci * @param {string} parentURL - The base URL.
9871cb0ef41Sopenharmony_ci */
9881cb0ef41Sopenharmony_cifunction resolveAsCommonJS(specifier, parentURL) {
9891cb0ef41Sopenharmony_ci  try {
9901cb0ef41Sopenharmony_ci    const parent = fileURLToPath(parentURL);
9911cb0ef41Sopenharmony_ci    const tmpModule = new CJSModule(parent, null);
9921cb0ef41Sopenharmony_ci    tmpModule.paths = CJSModule._nodeModulePaths(parent);
9931cb0ef41Sopenharmony_ci
9941cb0ef41Sopenharmony_ci    let found = CJSModule._resolveFilename(specifier, tmpModule, false);
9951cb0ef41Sopenharmony_ci
9961cb0ef41Sopenharmony_ci    // If it is a relative specifier return the relative path
9971cb0ef41Sopenharmony_ci    // to the parent
9981cb0ef41Sopenharmony_ci    if (isRelativeSpecifier(specifier)) {
9991cb0ef41Sopenharmony_ci      found = relative(parent, found);
10001cb0ef41Sopenharmony_ci      // Add '.separator if the path does not start with '..separator'
10011cb0ef41Sopenharmony_ci      // This should be a safe assumption because when loading
10021cb0ef41Sopenharmony_ci      // esm modules there should be always a file specified so
10031cb0ef41Sopenharmony_ci      // there should not be a specifier like '..' or '.'
10041cb0ef41Sopenharmony_ci      if (!StringPrototypeStartsWith(found, `..${sep}`)) {
10051cb0ef41Sopenharmony_ci        found = `.${sep}${found}`;
10061cb0ef41Sopenharmony_ci      }
10071cb0ef41Sopenharmony_ci    } else if (isBareSpecifier(specifier)) {
10081cb0ef41Sopenharmony_ci      // If it is a bare specifier return the relative path within the
10091cb0ef41Sopenharmony_ci      // module
10101cb0ef41Sopenharmony_ci      const pkg = StringPrototypeSplit(specifier, '/')[0];
10111cb0ef41Sopenharmony_ci      const index = StringPrototypeIndexOf(found, pkg);
10121cb0ef41Sopenharmony_ci      if (index !== -1) {
10131cb0ef41Sopenharmony_ci        found = StringPrototypeSlice(found, index);
10141cb0ef41Sopenharmony_ci      }
10151cb0ef41Sopenharmony_ci    }
10161cb0ef41Sopenharmony_ci    // Normalize the path separator to give a valid suggestion
10171cb0ef41Sopenharmony_ci    // on Windows
10181cb0ef41Sopenharmony_ci    if (process.platform === 'win32') {
10191cb0ef41Sopenharmony_ci      found = RegExpPrototypeSymbolReplace(new RegExp(`\\${sep}`, 'g'),
10201cb0ef41Sopenharmony_ci                                           found, '/');
10211cb0ef41Sopenharmony_ci    }
10221cb0ef41Sopenharmony_ci    return found;
10231cb0ef41Sopenharmony_ci  } catch {
10241cb0ef41Sopenharmony_ci    return false;
10251cb0ef41Sopenharmony_ci  }
10261cb0ef41Sopenharmony_ci}
10271cb0ef41Sopenharmony_ci
10281cb0ef41Sopenharmony_ci/**
10291cb0ef41Sopenharmony_ci * Throw an error if an import is not allowed.
10301cb0ef41Sopenharmony_ci * TODO(@JakobJingleheimer): de-dupe `specifier` & `parsed`
10311cb0ef41Sopenharmony_ci * @param {string} specifier - The import specifier.
10321cb0ef41Sopenharmony_ci * @param {URL} parsed - The parsed URL of the import specifier.
10331cb0ef41Sopenharmony_ci * @param {URL} parsedParentURL - The parsed URL of the parent module.
10341cb0ef41Sopenharmony_ci * @throws {ERR_NETWORK_IMPORT_DISALLOWED} - If the import is disallowed.
10351cb0ef41Sopenharmony_ci */
10361cb0ef41Sopenharmony_cifunction checkIfDisallowedImport(specifier, parsed, parsedParentURL) {
10371cb0ef41Sopenharmony_ci  if (parsedParentURL) {
10381cb0ef41Sopenharmony_ci    // Avoid accessing the `protocol` property due to the lazy getters.
10391cb0ef41Sopenharmony_ci    const parentProtocol = parsedParentURL.protocol;
10401cb0ef41Sopenharmony_ci    if (
10411cb0ef41Sopenharmony_ci      parentProtocol === 'http:' ||
10421cb0ef41Sopenharmony_ci      parentProtocol === 'https:'
10431cb0ef41Sopenharmony_ci    ) {
10441cb0ef41Sopenharmony_ci      if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
10451cb0ef41Sopenharmony_ci        // Avoid accessing the `protocol` property due to the lazy getters.
10461cb0ef41Sopenharmony_ci        const parsedProtocol = parsed?.protocol;
10471cb0ef41Sopenharmony_ci        // data: and blob: disallowed due to allowing file: access via
10481cb0ef41Sopenharmony_ci        // indirection
10491cb0ef41Sopenharmony_ci        if (parsedProtocol &&
10501cb0ef41Sopenharmony_ci          parsedProtocol !== 'https:' &&
10511cb0ef41Sopenharmony_ci          parsedProtocol !== 'http:'
10521cb0ef41Sopenharmony_ci        ) {
10531cb0ef41Sopenharmony_ci          throw new ERR_NETWORK_IMPORT_DISALLOWED(
10541cb0ef41Sopenharmony_ci            specifier,
10551cb0ef41Sopenharmony_ci            parsedParentURL,
10561cb0ef41Sopenharmony_ci            'remote imports cannot import from a local location.',
10571cb0ef41Sopenharmony_ci          );
10581cb0ef41Sopenharmony_ci        }
10591cb0ef41Sopenharmony_ci
10601cb0ef41Sopenharmony_ci        return { url: parsed.href };
10611cb0ef41Sopenharmony_ci      }
10621cb0ef41Sopenharmony_ci      if (BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
10631cb0ef41Sopenharmony_ci        throw new ERR_NETWORK_IMPORT_DISALLOWED(
10641cb0ef41Sopenharmony_ci          specifier,
10651cb0ef41Sopenharmony_ci          parsedParentURL,
10661cb0ef41Sopenharmony_ci          'remote imports cannot import from a local location.',
10671cb0ef41Sopenharmony_ci        );
10681cb0ef41Sopenharmony_ci      }
10691cb0ef41Sopenharmony_ci
10701cb0ef41Sopenharmony_ci      throw new ERR_NETWORK_IMPORT_DISALLOWED(
10711cb0ef41Sopenharmony_ci        specifier,
10721cb0ef41Sopenharmony_ci        parsedParentURL,
10731cb0ef41Sopenharmony_ci        'only relative and absolute specifiers are supported.',
10741cb0ef41Sopenharmony_ci      );
10751cb0ef41Sopenharmony_ci    }
10761cb0ef41Sopenharmony_ci  }
10771cb0ef41Sopenharmony_ci}
10781cb0ef41Sopenharmony_ci
10791cb0ef41Sopenharmony_ci/**
10801cb0ef41Sopenharmony_ci * Validate user-input in `context` supplied by a custom loader.
10811cb0ef41Sopenharmony_ci * @param {string | URL | undefined} parentURL - The parent URL.
10821cb0ef41Sopenharmony_ci */
10831cb0ef41Sopenharmony_cifunction throwIfInvalidParentURL(parentURL) {
10841cb0ef41Sopenharmony_ci  if (parentURL === undefined) {
10851cb0ef41Sopenharmony_ci    return; // Main entry point, so no parent
10861cb0ef41Sopenharmony_ci  }
10871cb0ef41Sopenharmony_ci  if (typeof parentURL !== 'string' && !isURL(parentURL)) {
10881cb0ef41Sopenharmony_ci    throw new ERR_INVALID_ARG_TYPE('parentURL', ['string', 'URL'], parentURL);
10891cb0ef41Sopenharmony_ci  }
10901cb0ef41Sopenharmony_ci}
10911cb0ef41Sopenharmony_ci
10921cb0ef41Sopenharmony_ci/**
10931cb0ef41Sopenharmony_ci * Resolves the given specifier using the provided context, which includes the parent URL and conditions.
10941cb0ef41Sopenharmony_ci * Throws an error if the parent URL is invalid or if the resolution is disallowed by the policy manifest.
10951cb0ef41Sopenharmony_ci * Otherwise, attempts to resolve the specifier and returns the resulting URL and format.
10961cb0ef41Sopenharmony_ci * @param {string} specifier - The specifier to resolve.
10971cb0ef41Sopenharmony_ci * @param {object} [context={}] - The context object containing the parent URL and conditions.
10981cb0ef41Sopenharmony_ci * @param {string} [context.parentURL] - The URL of the parent module.
10991cb0ef41Sopenharmony_ci * @param {string[]} [context.conditions] - The conditions for resolving the specifier.
11001cb0ef41Sopenharmony_ci */
11011cb0ef41Sopenharmony_cifunction defaultResolve(specifier, context = {}) {
11021cb0ef41Sopenharmony_ci  let { parentURL, conditions } = context;
11031cb0ef41Sopenharmony_ci  throwIfInvalidParentURL(parentURL);
11041cb0ef41Sopenharmony_ci  if (parentURL && policy?.manifest) {
11051cb0ef41Sopenharmony_ci    const redirects = policy.manifest.getDependencyMapper(parentURL);
11061cb0ef41Sopenharmony_ci    if (redirects) {
11071cb0ef41Sopenharmony_ci      const { resolve, reaction } = redirects;
11081cb0ef41Sopenharmony_ci      const destination = resolve(specifier, new SafeSet(conditions));
11091cb0ef41Sopenharmony_ci      let missing = true;
11101cb0ef41Sopenharmony_ci      if (destination === true) {
11111cb0ef41Sopenharmony_ci        missing = false;
11121cb0ef41Sopenharmony_ci      } else if (destination) {
11131cb0ef41Sopenharmony_ci        const href = destination.href;
11141cb0ef41Sopenharmony_ci        return { url: href };
11151cb0ef41Sopenharmony_ci      }
11161cb0ef41Sopenharmony_ci      if (missing) {
11171cb0ef41Sopenharmony_ci        // Prevent network requests from firing if resolution would be banned.
11181cb0ef41Sopenharmony_ci        // Network requests can extract data by doing things like putting
11191cb0ef41Sopenharmony_ci        // secrets in query params
11201cb0ef41Sopenharmony_ci        reaction(new ERR_MANIFEST_DEPENDENCY_MISSING(
11211cb0ef41Sopenharmony_ci          parentURL,
11221cb0ef41Sopenharmony_ci          specifier,
11231cb0ef41Sopenharmony_ci          ArrayPrototypeJoin([...conditions], ', ')),
11241cb0ef41Sopenharmony_ci        );
11251cb0ef41Sopenharmony_ci      }
11261cb0ef41Sopenharmony_ci    }
11271cb0ef41Sopenharmony_ci  }
11281cb0ef41Sopenharmony_ci
11291cb0ef41Sopenharmony_ci  let parsedParentURL;
11301cb0ef41Sopenharmony_ci  if (parentURL) {
11311cb0ef41Sopenharmony_ci    try {
11321cb0ef41Sopenharmony_ci      parsedParentURL = new URL(parentURL);
11331cb0ef41Sopenharmony_ci    } catch {
11341cb0ef41Sopenharmony_ci      // Ignore exception
11351cb0ef41Sopenharmony_ci    }
11361cb0ef41Sopenharmony_ci  }
11371cb0ef41Sopenharmony_ci
11381cb0ef41Sopenharmony_ci  let parsed;
11391cb0ef41Sopenharmony_ci  try {
11401cb0ef41Sopenharmony_ci    if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
11411cb0ef41Sopenharmony_ci      parsed = new URL(specifier, parsedParentURL);
11421cb0ef41Sopenharmony_ci    } else {
11431cb0ef41Sopenharmony_ci      parsed = new URL(specifier);
11441cb0ef41Sopenharmony_ci    }
11451cb0ef41Sopenharmony_ci
11461cb0ef41Sopenharmony_ci    // Avoid accessing the `protocol` property due to the lazy getters.
11471cb0ef41Sopenharmony_ci    const protocol = parsed.protocol;
11481cb0ef41Sopenharmony_ci    if (protocol === 'data:' ||
11491cb0ef41Sopenharmony_ci      (experimentalNetworkImports &&
11501cb0ef41Sopenharmony_ci        (
11511cb0ef41Sopenharmony_ci          protocol === 'https:' ||
11521cb0ef41Sopenharmony_ci          protocol === 'http:'
11531cb0ef41Sopenharmony_ci        )
11541cb0ef41Sopenharmony_ci      )
11551cb0ef41Sopenharmony_ci    ) {
11561cb0ef41Sopenharmony_ci      return { __proto__: null, url: parsed.href };
11571cb0ef41Sopenharmony_ci    }
11581cb0ef41Sopenharmony_ci  } catch {
11591cb0ef41Sopenharmony_ci    // Ignore exception
11601cb0ef41Sopenharmony_ci  }
11611cb0ef41Sopenharmony_ci
11621cb0ef41Sopenharmony_ci  // There are multiple deep branches that can either throw or return; instead
11631cb0ef41Sopenharmony_ci  // of duplicating that deeply nested logic for the possible returns, DRY and
11641cb0ef41Sopenharmony_ci  // check for a return. This seems the least gnarly.
11651cb0ef41Sopenharmony_ci  const maybeReturn = checkIfDisallowedImport(
11661cb0ef41Sopenharmony_ci    specifier,
11671cb0ef41Sopenharmony_ci    parsed,
11681cb0ef41Sopenharmony_ci    parsedParentURL,
11691cb0ef41Sopenharmony_ci  );
11701cb0ef41Sopenharmony_ci
11711cb0ef41Sopenharmony_ci  if (maybeReturn) { return maybeReturn; }
11721cb0ef41Sopenharmony_ci
11731cb0ef41Sopenharmony_ci  // This must come after checkIfDisallowedImport
11741cb0ef41Sopenharmony_ci  if (parsed && parsed.protocol === 'node:') { return { __proto__: null, url: specifier }; }
11751cb0ef41Sopenharmony_ci
11761cb0ef41Sopenharmony_ci
11771cb0ef41Sopenharmony_ci  const isMain = parentURL === undefined;
11781cb0ef41Sopenharmony_ci  if (isMain) {
11791cb0ef41Sopenharmony_ci    parentURL = getCWDURL().href;
11801cb0ef41Sopenharmony_ci
11811cb0ef41Sopenharmony_ci    // This is the initial entry point to the program, and --input-type has
11821cb0ef41Sopenharmony_ci    // been passed as an option; but --input-type can only be used with
11831cb0ef41Sopenharmony_ci    // --eval, --print or STDIN string input. It is not allowed with file
11841cb0ef41Sopenharmony_ci    // input, to avoid user confusion over how expansive the effect of the
11851cb0ef41Sopenharmony_ci    // flag should be (i.e. entry point only, package scope surrounding the
11861cb0ef41Sopenharmony_ci    // entry point, etc.).
11871cb0ef41Sopenharmony_ci    if (inputTypeFlag) { throw new ERR_INPUT_TYPE_NOT_ALLOWED(); }
11881cb0ef41Sopenharmony_ci  }
11891cb0ef41Sopenharmony_ci
11901cb0ef41Sopenharmony_ci  conditions = getConditionsSet(conditions);
11911cb0ef41Sopenharmony_ci  let url;
11921cb0ef41Sopenharmony_ci  try {
11931cb0ef41Sopenharmony_ci    url = moduleResolve(
11941cb0ef41Sopenharmony_ci      specifier,
11951cb0ef41Sopenharmony_ci      parentURL,
11961cb0ef41Sopenharmony_ci      conditions,
11971cb0ef41Sopenharmony_ci      isMain ? preserveSymlinksMain : preserveSymlinks,
11981cb0ef41Sopenharmony_ci    );
11991cb0ef41Sopenharmony_ci  } catch (error) {
12001cb0ef41Sopenharmony_ci    // Try to give the user a hint of what would have been the
12011cb0ef41Sopenharmony_ci    // resolved CommonJS module
12021cb0ef41Sopenharmony_ci    if (error.code === 'ERR_MODULE_NOT_FOUND' ||
12031cb0ef41Sopenharmony_ci        error.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
12041cb0ef41Sopenharmony_ci      if (StringPrototypeStartsWith(specifier, 'file://')) {
12051cb0ef41Sopenharmony_ci        specifier = fileURLToPath(specifier);
12061cb0ef41Sopenharmony_ci      }
12071cb0ef41Sopenharmony_ci      decorateErrorWithCommonJSHints(error, specifier, parentURL);
12081cb0ef41Sopenharmony_ci    }
12091cb0ef41Sopenharmony_ci    throw error;
12101cb0ef41Sopenharmony_ci  }
12111cb0ef41Sopenharmony_ci
12121cb0ef41Sopenharmony_ci  return {
12131cb0ef41Sopenharmony_ci    // Do NOT cast `url` to a string: that will work even when there are real
12141cb0ef41Sopenharmony_ci    // problems, silencing them
12151cb0ef41Sopenharmony_ci    url: url.href,
12161cb0ef41Sopenharmony_ci    format: defaultGetFormatWithoutErrors(url, context),
12171cb0ef41Sopenharmony_ci  };
12181cb0ef41Sopenharmony_ci}
12191cb0ef41Sopenharmony_ci
12201cb0ef41Sopenharmony_ci/**
12211cb0ef41Sopenharmony_ci * Decorates the given error with a hint for CommonJS modules.
12221cb0ef41Sopenharmony_ci * @param {Error} error - The error to decorate.
12231cb0ef41Sopenharmony_ci * @param {string} specifier - The specifier that was attempted to be imported.
12241cb0ef41Sopenharmony_ci * @param {string} parentURL - The URL of the parent module.
12251cb0ef41Sopenharmony_ci */
12261cb0ef41Sopenharmony_cifunction decorateErrorWithCommonJSHints(error, specifier, parentURL) {
12271cb0ef41Sopenharmony_ci  const found = resolveAsCommonJS(specifier, parentURL);
12281cb0ef41Sopenharmony_ci  if (found) {
12291cb0ef41Sopenharmony_ci    // Modify the stack and message string to include the hint
12301cb0ef41Sopenharmony_ci    const lines = StringPrototypeSplit(error.stack, '\n');
12311cb0ef41Sopenharmony_ci    const hint = `Did you mean to import ${found}?`;
12321cb0ef41Sopenharmony_ci    error.stack =
12331cb0ef41Sopenharmony_ci      ArrayPrototypeShift(lines) + '\n' +
12341cb0ef41Sopenharmony_ci      hint + '\n' +
12351cb0ef41Sopenharmony_ci      ArrayPrototypeJoin(lines, '\n');
12361cb0ef41Sopenharmony_ci    error.message += `\n${hint}`;
12371cb0ef41Sopenharmony_ci  }
12381cb0ef41Sopenharmony_ci}
12391cb0ef41Sopenharmony_ci
12401cb0ef41Sopenharmony_cimodule.exports = {
12411cb0ef41Sopenharmony_ci  decorateErrorWithCommonJSHints,
12421cb0ef41Sopenharmony_ci  defaultResolve,
12431cb0ef41Sopenharmony_ci  encodedSepRegEx,
12441cb0ef41Sopenharmony_ci  getPackageScopeConfig,
12451cb0ef41Sopenharmony_ci  getPackageType,
12461cb0ef41Sopenharmony_ci  packageExportsResolve,
12471cb0ef41Sopenharmony_ci  packageImportsResolve,
12481cb0ef41Sopenharmony_ci  throwIfInvalidParentURL,
12491cb0ef41Sopenharmony_ci};
12501cb0ef41Sopenharmony_ci
12511cb0ef41Sopenharmony_ci// cycle
12521cb0ef41Sopenharmony_ciconst {
12531cb0ef41Sopenharmony_ci  defaultGetFormatWithoutErrors,
12541cb0ef41Sopenharmony_ci} = require('internal/modules/esm/get_format');
12551cb0ef41Sopenharmony_ci
12561cb0ef41Sopenharmony_ciif (policy) {
12571cb0ef41Sopenharmony_ci  const $defaultResolve = defaultResolve;
12581cb0ef41Sopenharmony_ci  module.exports.defaultResolve = function defaultResolve(
12591cb0ef41Sopenharmony_ci    specifier,
12601cb0ef41Sopenharmony_ci    context,
12611cb0ef41Sopenharmony_ci  ) {
12621cb0ef41Sopenharmony_ci    const ret = $defaultResolve(specifier, context);
12631cb0ef41Sopenharmony_ci    // This is a preflight check to avoid data exfiltration by query params etc.
12641cb0ef41Sopenharmony_ci    policy.manifest.mightAllow(ret.url, () =>
12651cb0ef41Sopenharmony_ci      new ERR_MANIFEST_DEPENDENCY_MISSING(
12661cb0ef41Sopenharmony_ci        context.parentURL,
12671cb0ef41Sopenharmony_ci        specifier,
12681cb0ef41Sopenharmony_ci        context.conditions,
12691cb0ef41Sopenharmony_ci      ),
12701cb0ef41Sopenharmony_ci    );
12711cb0ef41Sopenharmony_ci    return ret;
12721cb0ef41Sopenharmony_ci  };
12731cb0ef41Sopenharmony_ci}
1274