Skip to content

Commit

Permalink
lib: merge cjs and esm package json reader caches
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jun 16, 2023
1 parent 0d725d6 commit d6a4186
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 203 deletions.
48 changes: 11 additions & 37 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ const {
pendingDeprecate,
emitExperimentalWarning,
kEmptyObject,
filterOwnProperties,
setOwnProperty,
getLazy,
} = require('internal/util');
Expand Down Expand Up @@ -353,36 +352,10 @@ function initializeCJS() {
// -> a.<ext>
// -> a/index.<ext>

const packageJsonCache = new SafeMap();

function readPackage(requestPath) {
const jsonPath = path.resolve(requestPath, 'package.json');

const existing = packageJsonCache.get(jsonPath);
if (existing !== undefined) return existing;

const result = packageJsonReader.read(jsonPath);
const json = result.containsKeys === false ? '{}' : result.string;
if (json === undefined) {
packageJsonCache.set(jsonPath, false);
return false;
}

try {
const filtered = filterOwnProperties(JSONParse(json), [
'name',
'main',
'exports',
'imports',
'type',
]);
packageJsonCache.set(jsonPath, filtered);
return filtered;
} catch (e) {
e.path = jsonPath;
e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
throw e;
}
// Return undefined or the filtered package.json as a JS object
return packageJsonReader.read(jsonPath);
}

let _readPackage = readPackage;
Expand Down Expand Up @@ -412,7 +385,7 @@ function readPackageScope(checkPath) {
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
return false;
const pjson = _readPackage(checkPath + sep);
if (pjson) return {
if (pjson.exists) return {
data: pjson,
path: checkPath,
};
Expand All @@ -421,13 +394,13 @@ function readPackageScope(checkPath) {
}

function tryPackage(requestPath, exts, isMain, originalPath) {
const pkg = _readPackage(requestPath)?.main;
const pkg = _readPackage(requestPath);

if (!pkg) {
if (!pkg.main) {
return tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
}

const filename = path.resolve(requestPath, pkg);
const filename = path.resolve(requestPath, pkg.main);
let actual = tryFile(filename, isMain) ||
tryExtensions(filename, exts, isMain) ||
tryExtensions(path.resolve(filename, 'index'), exts, isMain);
Expand All @@ -447,7 +420,7 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
} else {
const jsonPath = path.resolve(requestPath, 'package.json');
process.emitWarning(
`Invalid 'main' field in '${jsonPath}' of '${pkg}'. ` +
`Invalid 'main' field in '${jsonPath}' of '${pkg.main}'. ` +
'Please either fix that or report it to the module author',
'DeprecationWarning',
'DEP0128',
Expand Down Expand Up @@ -527,8 +500,9 @@ function trySelf(parentPath, request) {
if (!parentPath) return false;

const { data: pkg, path: pkgPath } = readPackageScope(parentPath) || {};
if (!pkg || pkg.exports === undefined) return false;
if (typeof pkg.name !== 'string') return false;
if (!pkg || pkg.exports === undefined || pkg.name === undefined) {
return false;
}

let expansion;
if (request === pkg.name) {
Expand Down Expand Up @@ -563,7 +537,7 @@ function resolveExports(nmPath, request) {
return;
const pkgPath = path.resolve(nmPath, name);
const pkg = _readPackage(pkgPath);
if (pkg?.exports != null) {
if (pkg.exists && pkg.exports != null) {
try {
const { packageExportsResolve } = require('internal/modules/esm/resolve');
return finalizeEsmResolution(packageExportsResolve(
Expand Down
89 changes: 3 additions & 86 deletions lib/internal/modules/esm/package_config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
'use strict';

const {
JSONParse,
ObjectPrototypeHasOwnProperty,
SafeMap,
StringPrototypeEndsWith,
} = primordials;
const { URL, fileURLToPath } = require('internal/url');
const {
ERR_INVALID_PACKAGE_CONFIG,
} = require('internal/errors').codes;

const { filterOwnProperties } = require('internal/util');

const packageJsonReader = require('internal/modules/package_json_reader');

/**
* @typedef {string | string[] | Record<string, unknown>} Exports
Expand All @@ -26,78 +18,6 @@ const { filterOwnProperties } = require('internal/util');
* }} PackageConfig
*/

/** @type {Map<string, PackageConfig>} */
const packageJSONCache = new SafeMap();


/**
* @param {string} path
* @param {string} specifier
* @param {string | URL | undefined} base
* @returns {PackageConfig}
*/
function getPackageConfig(path, specifier, base) {
const existing = packageJSONCache.get(path);
if (existing !== undefined) {
return existing;
}
const packageJsonReader = require('internal/modules/package_json_reader');
const source = packageJsonReader.read(path).string;
if (source === undefined) {
const packageConfig = {
pjsonPath: path,
exists: false,
main: undefined,
name: undefined,
type: 'none',
exports: undefined,
imports: undefined,
};
packageJSONCache.set(path, packageConfig);
return packageConfig;
}

let packageJSON;
try {
packageJSON = JSONParse(source);
} catch (error) {
throw new ERR_INVALID_PACKAGE_CONFIG(
path,
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier),
error.message,
);
}

let { imports, main, name, type } = filterOwnProperties(packageJSON, ['imports', 'main', 'name', 'type']);
const exports = ObjectPrototypeHasOwnProperty(packageJSON, 'exports') ? packageJSON.exports : undefined;
if (typeof imports !== 'object' || imports === null) {
imports = undefined;
}
if (typeof main !== 'string') {
main = undefined;
}
if (typeof name !== 'string') {
name = undefined;
}
// Ignore unknown types for forwards compatibility
if (type !== 'module' && type !== 'commonjs') {
type = 'none';
}

const packageConfig = {
pjsonPath: path,
exists: true,
main,
name,
type,
exports,
imports,
};
packageJSONCache.set(path, packageConfig);
return packageConfig;
}


/**
* @param {URL | string} resolved
* @returns {PackageConfig}
Expand All @@ -109,7 +29,7 @@ function getPackageScopeConfig(resolved) {
if (StringPrototypeEndsWith(packageJSONPath, 'node_modules/package.json')) {
break;
}
const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl), resolved);
const packageConfig = packageJsonReader.read(fileURLToPath(packageJSONUrl), { specifier: resolved, isESM: true });
if (packageConfig.exists) {
return packageConfig;
}
Expand All @@ -124,7 +44,7 @@ function getPackageScopeConfig(resolved) {
}
}
const packageJSONPath = fileURLToPath(packageJSONUrl);
const packageConfig = {
return {
pjsonPath: packageJSONPath,
exists: false,
main: undefined,
Expand All @@ -133,12 +53,9 @@ function getPackageScopeConfig(resolved) {
exports: undefined,
imports: undefined,
};
packageJSONCache.set(packageJSONPath, packageConfig);
return packageConfig;
}


module.exports = {
getPackageConfig,
getPackageScopeConfig,
};
10 changes: 5 additions & 5 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ const {
} = require('internal/errors').codes;

const { Module: CJSModule } = require('internal/modules/cjs/loader');
const { getPackageConfig, getPackageScopeConfig } = require('internal/modules/esm/package_config');
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 @@ -734,8 +735,7 @@ function packageResolve(specifier, base, conditions) {
const packageConfig = getPackageScopeConfig(base);
if (packageConfig.exists) {
const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
if (packageConfig.name === packageName &&
packageConfig.exports !== undefined && packageConfig.exports !== null) {
if (packageConfig.name === packageName && packageConfig.exports != null) {
return packageExportsResolve(
packageJSONUrl, packageSubpath, packageConfig, base, conditions);
}
Expand All @@ -759,8 +759,8 @@ function packageResolve(specifier, base, conditions) {
}

// Package match.
const packageConfig = getPackageConfig(packageJSONPath, specifier, base);
if (packageConfig.exports !== undefined && packageConfig.exports !== null) {
const packageConfig = packageJsonReader.read(packageJSONPath, { specifier, base, isESM: true });
if (packageConfig.exports != null) {
return packageExportsResolve(
packageJSONUrl, packageSubpath, packageConfig, base, conditions);
}
Expand Down
93 changes: 86 additions & 7 deletions lib/internal/modules/package_json_reader.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,109 @@
'use strict';

const { SafeMap } = primordials;
const {
JSONParse,
SafeMap,
} = primordials;
const {
ERR_INVALID_PACKAGE_CONFIG,
} = require('internal/errors').codes;
const { internalModuleReadJSON } = internalBinding('fs');
const { pathToFileURL } = require('url');
const { toNamespacedPath } = require('path');
const {
filterOwnProperties,
kEmptyObject,
} = require('internal/util');
const { fileURLToPath, pathToFileURL } = require('internal/url');

const cache = new SafeMap();

let manifest;

/**
*
* @param {string} jsonPath
* @param {{
* base?: string,
* specifier: string,
* isESM: boolean,
* }} options
* @returns {{
* exists: boolean,
* pjsonPath: string,
* name?: string,
* main?: string,
* exports?: string | Record<string, unknown>,
* imports?: string | Record<string, unknown>,
* type: 'commonjs' | 'module' | 'none',
* }}
*/
function read(jsonPath) {
function read(jsonPath, { base, specifier, isESM } = kEmptyObject) {
if (cache.has(jsonPath)) {
return cache.get(jsonPath);
}

const { 0: string, 1: containsKeys } = internalModuleReadJSON(
const string = internalModuleReadJSON(
toNamespacedPath(jsonPath),
);
const result = { string, containsKeys };
const { getOptionValue } = require('internal/options');
let result = {
__proto__: null,
exists: false,
pjsonPath: jsonPath,
main: undefined,
name: undefined,
type: 'none',
exports: undefined,
imports: undefined,
};

if (string !== undefined) {
let parsed;
try {
parsed = filterOwnProperties(JSONParse(string), [
'name',
'main',
'exports',
'imports',
'type',
]);
} catch (error) {
if (isESM) {
throw new ERR_INVALID_PACKAGE_CONFIG(
jsonPath,
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier),
error.message,
);
} else {
error.path = jsonPath;
error.message = 'Error parsing ' + jsonPath + ': ' + error.message;
throw error;
}
}

result = {
__proto__: null,
exists: true,
pjsonPath: jsonPath,
name: parsed.name,
main: parsed.main,
exports: parsed.exports,
imports: parsed.imports,
type: parsed.type,
};

if (typeof result.name !== 'string') {
result.name = undefined;
}

if (typeof result.main !== 'string') {
result.main = undefined;
}

if (result.type !== 'commonjs' && result.type !== 'module') {
result.type = 'none';
}

if (manifest === undefined) {
const { getOptionValue } = require('internal/options');
manifest = getOptionValue('--experimental-policy') ?
require('internal/process/policy').manifest :
null;
Expand Down
Loading

0 comments on commit d6a4186

Please sign in to comment.