Skip to content

Commit 9f54987

Browse files
anonrigRafaelGSS
authored andcommitted
module: merge config with package_json_reader
PR-URL: #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 45e4f82 commit 9f54987

File tree

5 files changed

+38
-51
lines changed

5 files changed

+38
-51
lines changed

lib/internal/modules/esm/get_format.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const {
1919
const experimentalNetworkImports =
2020
getOptionValue('--experimental-network-imports');
2121
const { containsModuleSyntax } = internalBinding('contextify');
22-
const { getPackageType } = require('internal/modules/esm/package_config');
22+
const { getPackageType } = require('internal/modules/package_json_reader');
2323
const { fileURLToPath } = require('internal/url');
2424
const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;
2525

lib/internal/modules/esm/module_job.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ class ModuleJob {
228228
const packageConfig =
229229
StringPrototypeStartsWith(this.module.url, 'file://') &&
230230
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, this.module.url) !== null &&
231-
require('internal/modules/esm/package_config')
231+
require('internal/modules/package_json_reader')
232232
.getPackageScopeConfig(this.module.url);
233233
if (packageConfig.type === 'module') {
234234
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
@@ -55,7 +55,6 @@ const {
5555
} = require('internal/errors').codes;
5656

5757
const { Module: CJSModule } = require('internal/modules/cjs/loader');
58-
const { getPackageScopeConfig } = require('internal/modules/esm/package_config');
5958
const { getConditionsSet } = require('internal/modules/esm/utils');
6059
const packageJsonReader = require('internal/modules/package_json_reader');
6160
const { internalModuleStat } = internalBinding('fs');
@@ -687,7 +686,7 @@ function packageImportsResolve(name, base, conditions) {
687686
throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base));
688687
}
689688
let packageJSONUrl;
690-
const packageConfig = getPackageScopeConfig(base);
689+
const packageConfig = packageJsonReader.getPackageScopeConfig(base);
691690
if (packageConfig.exists) {
692691
packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
693692
const imports = packageConfig.imports;
@@ -796,7 +795,7 @@ function packageResolve(specifier, base, conditions) {
796795
parsePackageName(specifier, base);
797796

798797
// ResolveSelf
799-
const packageConfig = getPackageScopeConfig(base);
798+
const packageConfig = packageJsonReader.getPackageScopeConfig(base);
800799
if (packageConfig.exists) {
801800
const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
802801
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,
@@ -149,11 +150,42 @@ function getNearestParentPackageJSON(checkPath) {
149150
return { data, path };
150151
}
151152

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

0 commit comments

Comments
 (0)