11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst {
41cb0ef41Sopenharmony_ci  StringPrototypeEndsWith,
51cb0ef41Sopenharmony_ci} = primordials;
61cb0ef41Sopenharmony_ciconst { URL, fileURLToPath } = require('internal/url');
71cb0ef41Sopenharmony_ciconst packageJsonReader = require('internal/modules/package_json_reader');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci/**
101cb0ef41Sopenharmony_ci * @typedef {object} PackageConfig
111cb0ef41Sopenharmony_ci * @property {string} pjsonPath - The path to the package.json file.
121cb0ef41Sopenharmony_ci * @property {boolean} exists - Whether the package.json file exists.
131cb0ef41Sopenharmony_ci * @property {'none' | 'commonjs' | 'module'} type - The type of the package.
141cb0ef41Sopenharmony_ci * @property {string} [name] - The name of the package.
151cb0ef41Sopenharmony_ci * @property {string} [main] - The main entry point of the package.
161cb0ef41Sopenharmony_ci * @property {PackageTarget} [exports] - The exports configuration of the package.
171cb0ef41Sopenharmony_ci * @property {Record<string, string | Record<string, string>>} [imports] - The imports configuration of the package.
181cb0ef41Sopenharmony_ci */
191cb0ef41Sopenharmony_ci/**
201cb0ef41Sopenharmony_ci * @typedef {string | string[] | Record<string, string | Record<string, string>>} PackageTarget
211cb0ef41Sopenharmony_ci */
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci/**
241cb0ef41Sopenharmony_ci * Returns the package configuration for the given resolved URL.
251cb0ef41Sopenharmony_ci * @param {URL | string} resolved - The resolved URL.
261cb0ef41Sopenharmony_ci * @returns {PackageConfig} - The package configuration.
271cb0ef41Sopenharmony_ci */
281cb0ef41Sopenharmony_cifunction getPackageScopeConfig(resolved) {
291cb0ef41Sopenharmony_ci  let packageJSONUrl = new URL('./package.json', resolved);
301cb0ef41Sopenharmony_ci  while (true) {
311cb0ef41Sopenharmony_ci    const packageJSONPath = packageJSONUrl.pathname;
321cb0ef41Sopenharmony_ci    if (StringPrototypeEndsWith(packageJSONPath, 'node_modules/package.json')) {
331cb0ef41Sopenharmony_ci      break;
341cb0ef41Sopenharmony_ci    }
351cb0ef41Sopenharmony_ci    const packageConfig = packageJsonReader.read(fileURLToPath(packageJSONUrl), {
361cb0ef41Sopenharmony_ci      __proto__: null,
371cb0ef41Sopenharmony_ci      specifier: resolved,
381cb0ef41Sopenharmony_ci      isESM: true,
391cb0ef41Sopenharmony_ci    });
401cb0ef41Sopenharmony_ci    if (packageConfig.exists) {
411cb0ef41Sopenharmony_ci      return packageConfig;
421cb0ef41Sopenharmony_ci    }
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci    const lastPackageJSONUrl = packageJSONUrl;
451cb0ef41Sopenharmony_ci    packageJSONUrl = new URL('../package.json', packageJSONUrl);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci    // Terminates at root where ../package.json equals ../../package.json
481cb0ef41Sopenharmony_ci    // (can't just check "/package.json" for Windows support).
491cb0ef41Sopenharmony_ci    if (packageJSONUrl.pathname === lastPackageJSONUrl.pathname) {
501cb0ef41Sopenharmony_ci      break;
511cb0ef41Sopenharmony_ci    }
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci  const packageJSONPath = fileURLToPath(packageJSONUrl);
541cb0ef41Sopenharmony_ci  return {
551cb0ef41Sopenharmony_ci    __proto__: null,
561cb0ef41Sopenharmony_ci    pjsonPath: packageJSONPath,
571cb0ef41Sopenharmony_ci    exists: false,
581cb0ef41Sopenharmony_ci    main: undefined,
591cb0ef41Sopenharmony_ci    name: undefined,
601cb0ef41Sopenharmony_ci    type: 'none',
611cb0ef41Sopenharmony_ci    exports: undefined,
621cb0ef41Sopenharmony_ci    imports: undefined,
631cb0ef41Sopenharmony_ci  };
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_cimodule.exports = {
681cb0ef41Sopenharmony_ci  getPackageScopeConfig,
691cb0ef41Sopenharmony_ci};
70