From 9f54987fbc50503a4644c3f5b22eba93ff97f405 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Fri, 10 Nov 2023 15:49:40 -0500 Subject: [PATCH] module: merge config with `package_json_reader` PR-URL: https://github.com/nodejs/node/pull/50322 Reviewed-By: Jacob Smith Reviewed-By: Matteo Collina Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Geoffrey Booth --- lib/internal/modules/esm/get_format.js | 2 +- lib/internal/modules/esm/module_job.js | 2 +- lib/internal/modules/esm/package_config.js | 44 --------------------- lib/internal/modules/esm/resolve.js | 5 +-- lib/internal/modules/package_json_reader.js | 36 ++++++++++++++++- 5 files changed, 38 insertions(+), 51 deletions(-) delete mode 100644 lib/internal/modules/esm/package_config.js diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index c029b6c614384f..5959d476e13bd9 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -19,7 +19,7 @@ const { const experimentalNetworkImports = getOptionValue('--experimental-network-imports'); const { containsModuleSyntax } = internalBinding('contextify'); -const { getPackageType } = require('internal/modules/esm/package_config'); +const { getPackageType } = require('internal/modules/package_json_reader'); const { fileURLToPath } = require('internal/url'); const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes; diff --git a/lib/internal/modules/esm/module_job.js b/lib/internal/modules/esm/module_job.js index 7116f3724bb6e1..33576c5893600b 100644 --- a/lib/internal/modules/esm/module_job.js +++ b/lib/internal/modules/esm/module_job.js @@ -228,7 +228,7 @@ class ModuleJob { const packageConfig = StringPrototypeStartsWith(this.module.url, 'file://') && RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null && - require('internal/modules/esm/package_config') + require('internal/modules/package_json_reader') .getPackageScopeConfig(this.module.url); if (packageConfig.type === 'module') { e.message += diff --git a/lib/internal/modules/esm/package_config.js b/lib/internal/modules/esm/package_config.js deleted file mode 100644 index 6b3847966cb1d3..00000000000000 --- a/lib/internal/modules/esm/package_config.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict'; - -const { ArrayIsArray } = primordials; -const modulesBinding = internalBinding('modules'); -const { deserializePackageJSON } = require('internal/modules/package_json_reader'); - -// TODO(@anonrig): Merge this file with internal/esm/package_json_reader.js - -/** - * Returns the package configuration for the given resolved URL. - * @param {URL | string} resolved - The resolved URL. - * @returns {import('typings/internalBinding/modules').PackageConfig} - The package configuration. - */ -function getPackageScopeConfig(resolved) { - const result = modulesBinding.getPackageScopeConfig(`${resolved}`); - - if (ArrayIsArray(result)) { - return deserializePackageJSON(`${resolved}`, result, false /* checkIntegrity */); - } - - // This means that the response is a string - // and it is the path to the package.json file - return { - __proto__: null, - pjsonPath: result, - exists: false, - type: 'none', - }; -} - -/** - * Returns the package type for a given URL. - * @param {URL} url - The URL to get the package type for. - */ -function getPackageType(url) { - // TODO(@anonrig): Write a C++ function that returns only "type". - return getPackageScopeConfig(url).type; -} - - -module.exports = { - getPackageScopeConfig, - getPackageType, -}; diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index a6469cfad77e47..c40f81200de5fc 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -55,7 +55,6 @@ const { } = require('internal/errors').codes; const { Module: CJSModule } = require('internal/modules/cjs/loader'); -const { getPackageScopeConfig } = require('internal/modules/esm/package_config'); const { getConditionsSet } = require('internal/modules/esm/utils'); const packageJsonReader = require('internal/modules/package_json_reader'); const { internalModuleStat } = internalBinding('fs'); @@ -687,7 +686,7 @@ function packageImportsResolve(name, base, conditions) { throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base)); } let packageJSONUrl; - const packageConfig = getPackageScopeConfig(base); + const packageConfig = packageJsonReader.getPackageScopeConfig(base); if (packageConfig.exists) { packageJSONUrl = pathToFileURL(packageConfig.pjsonPath); const imports = packageConfig.imports; @@ -796,7 +795,7 @@ function packageResolve(specifier, base, conditions) { parsePackageName(specifier, base); // ResolveSelf - const packageConfig = getPackageScopeConfig(base); + const packageConfig = packageJsonReader.getPackageScopeConfig(base); if (packageConfig.exists) { const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath); if (packageConfig.exports != null && packageConfig.name === packageName) { diff --git a/lib/internal/modules/package_json_reader.js b/lib/internal/modules/package_json_reader.js index 232ed35528c5c5..0842d543862c33 100644 --- a/lib/internal/modules/package_json_reader.js +++ b/lib/internal/modules/package_json_reader.js @@ -1,6 +1,7 @@ 'use strict'; const { + ArrayIsArray, JSONParse, StringPrototypeSlice, StringPrototypeLastIndexOf, @@ -149,11 +150,42 @@ function getNearestParentPackageJSON(checkPath) { return { data, path }; } +/** + * Returns the package configuration for the given resolved URL. + * @param {URL | string} resolved - The resolved URL. + * @returns {import('typings/internalBinding/modules').PackageConfig} - The package configuration. + */ +function getPackageScopeConfig(resolved) { + const result = modulesBinding.getPackageScopeConfig(`${resolved}`); + + if (ArrayIsArray(result)) { + return deserializePackageJSON(`${resolved}`, result, false /* checkIntegrity */); + } + + // This means that the response is a string + // and it is the path to the package.json file + return { + __proto__: null, + pjsonPath: result, + exists: false, + type: 'none', + }; +} + +/** + * Returns the package type for a given URL. + * @param {URL} url - The URL to get the package type for. + */ +function getPackageType(url) { + // TODO(@anonrig): Write a C++ function that returns only "type". + return getPackageScopeConfig(url).type; +} + module.exports = { checkPackageJSONIntegrity, read, readPackage, getNearestParentPackageJSON, - - deserializePackageJSON, + getPackageScopeConfig, + getPackageType, };