Skip to content

Commit 5bf61c5

Browse files
anonrigmarco-ippolito
authored andcommitted
module: merge config with package_json_reader
PR-URL: #50322 Backport-PR-URL: #56590 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]>
1 parent eb5f757 commit 5bf61c5

File tree

5 files changed

+38
-52
lines changed

5 files changed

+38
-52
lines changed

lib/internal/modules/esm/get_format.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ const {
2020
const experimentalNetworkImports =
2121
getOptionValue('--experimental-network-imports');
2222
const { containsModuleSyntax } = internalBinding('contextify');
23-
const { getPackageScopeConfig } = require('internal/modules/esm/resolve');
24-
const { getPackageType } = require('internal/modules/esm/package_config');
23+
const { getPackageScopeConfig, getPackageType } = require('internal/modules/package_json_reader');
2524
const { fileURLToPath } = require('internal/url');
2625
const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;
2726

lib/internal/modules/esm/module_job.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class ModuleJob extends ModuleJobBase {
244244
const packageConfig =
245245
StringPrototypeStartsWith(this.module.url, 'file://') &&
246246
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null &&
247-
require('internal/modules/esm/package_config')
247+
require('internal/modules/package_json_reader')
248248
.getPackageScopeConfig(this.module.url);
249249
if (packageConfig.type === 'module') {
250250
e.message +=

lib/internal/modules/esm/package_config.js

Lines changed: 0 additions & 44 deletions
This file was deleted.

lib/internal/modules/esm/resolve.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ const {
5656
} = require('internal/errors').codes;
5757

5858
const { Module: CJSModule } = require('internal/modules/cjs/loader');
59-
const { getPackageScopeConfig } = require('internal/modules/esm/package_config');
6059
const { getConditionsSet } = require('internal/modules/esm/utils');
6160
const packageJsonReader = require('internal/modules/package_json_reader');
6261
const { internalModuleStat } = internalBinding('fs');
@@ -707,7 +706,7 @@ function packageImportsResolve(name, base, conditions) {
707706
throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base));
708707
}
709708
let packageJSONUrl;
710-
const packageConfig = getPackageScopeConfig(base);
709+
const packageConfig = packageJsonReader.getPackageScopeConfig(base);
711710
if (packageConfig.exists) {
712711
packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
713712
const imports = packageConfig.imports;
@@ -816,7 +815,7 @@ function packageResolve(specifier, base, conditions) {
816815
parsePackageName(specifier, base);
817816

818817
// ResolveSelf
819-
const packageConfig = getPackageScopeConfig(base);
818+
const packageConfig = packageJsonReader.getPackageScopeConfig(base);
820819
if (packageConfig.exists) {
821820
const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
822821
if (packageConfig.exports != null && packageConfig.name === packageName) {

lib/internal/modules/package_json_reader.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const {
4+
ArrayIsArray,
45
JSONParse,
56
StringPrototypeSlice,
67
StringPrototypeLastIndexOf,
@@ -150,11 +151,42 @@ function getNearestParentPackageJSON(checkPath) {
150151
return { data, path };
151152
}
152153

154+
/**
155+
* Returns the package configuration for the given resolved URL.
156+
* @param {URL | string} resolved - The resolved URL.
157+
* @returns {import('typings/internalBinding/modules').PackageConfig} - The package configuration.
158+
*/
159+
function getPackageScopeConfig(resolved) {
160+
const result = modulesBinding.getPackageScopeConfig(`${resolved}`);
161+
162+
if (ArrayIsArray(result)) {
163+
return deserializePackageJSON(`${resolved}`, result, false /* checkIntegrity */);
164+
}
165+
166+
// This means that the response is a string
167+
// and it is the path to the package.json file
168+
return {
169+
__proto__: null,
170+
pjsonPath: result,
171+
exists: false,
172+
type: 'none',
173+
};
174+
}
175+
176+
/**
177+
* Returns the package type for a given URL.
178+
* @param {URL} url - The URL to get the package type for.
179+
*/
180+
function getPackageType(url) {
181+
// TODO(@anonrig): Write a C++ function that returns only "type".
182+
return getPackageScopeConfig(url).type;
183+
}
184+
153185
module.exports = {
154186
checkPackageJSONIntegrity,
155187
read,
156188
readPackage,
157189
getNearestParentPackageJSON,
158-
159-
deserializePackageJSON,
190+
getPackageScopeConfig,
191+
getPackageType,
160192
};

0 commit comments

Comments
 (0)