Skip to content

Commit 0f2e413

Browse files
anonrigjoyeecheung
authored andcommitted
module: merge config with package_json_reader
PR-URL: nodejs#50322 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 7b7500a commit 0f2e413

File tree

5 files changed

+38
-52
lines changed

5 files changed

+38
-52
lines changed

lib/internal/modules/esm/get_format.js

+1-2
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

+1-1
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

-44
This file was deleted.

lib/internal/modules/esm/resolve.js

+2-3
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');
@@ -688,7 +687,7 @@ function packageImportsResolve(name, base, conditions) {
688687
throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base));
689688
}
690689
let packageJSONUrl;
691-
const packageConfig = getPackageScopeConfig(base);
690+
const packageConfig = packageJsonReader.getPackageScopeConfig(base);
692691
if (packageConfig.exists) {
693692
packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
694693
const imports = packageConfig.imports;
@@ -797,7 +796,7 @@ function packageResolve(specifier, base, conditions) {
797796
parsePackageName(specifier, base);
798797

799798
// ResolveSelf
800-
const packageConfig = getPackageScopeConfig(base);
799+
const packageConfig = packageJsonReader.getPackageScopeConfig(base);
801800
if (packageConfig.exists) {
802801
const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
803802
if (packageConfig.exports != null && packageConfig.name === packageName) {

lib/internal/modules/package_json_reader.js

+34-2
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)