Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9f54987

Browse filesBrowse files
anonrigRafaelGSS
authored andcommitted
module: merge config with package_json_reader
PR-URL: #50322 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 45e4f82 commit 9f54987
Copy full SHA for 9f54987

File tree

Expand file treeCollapse file tree

5 files changed

+38
-51
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+38
-51
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/esm/get_format.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_format.js
+1-1Lines changed: 1 addition & 1 deletion
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

Collapse file

‎lib/internal/modules/esm/module_job.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/module_job.js
+1-1Lines changed: 1 addition & 1 deletion
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 +=
Collapse file

‎lib/internal/modules/esm/package_config.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/package_config.js
-44Lines changed: 0 additions & 44 deletions
This file was deleted.
Collapse file

‎lib/internal/modules/esm/resolve.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+2-3Lines changed: 2 additions & 3 deletions
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) {
Collapse file

‎lib/internal/modules/package_json_reader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/package_json_reader.js
+34-2Lines 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,
@@ -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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.