Skip to content

Commit

Permalink
module: merge config with package_json_reader
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
anonrig authored and RafaelGSS committed Dec 15, 2023
1 parent 45e4f82 commit 9f54987
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 51 deletions.
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 +=
Expand Down
44 changes: 0 additions & 44 deletions lib/internal/modules/esm/package_config.js

This file was deleted.

5 changes: 2 additions & 3 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
36 changes: 34 additions & 2 deletions lib/internal/modules/package_json_reader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayIsArray,
JSONParse,
StringPrototypeSlice,
StringPrototypeLastIndexOf,
Expand Down Expand Up @@ -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,
};

0 comments on commit 9f54987

Please sign in to comment.