Skip to content

src: add cache to nearest parent package json #59086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/internal/modules/package_json_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
JSONParse,
ObjectDefineProperty,
RegExpPrototypeExec,
SafeMap,
StringPrototypeIndexOf,
StringPrototypeSlice,
} = primordials;
Expand All @@ -28,6 +29,8 @@ const path = require('path');
const { validateString } = require('internal/validators');
const internalFsBinding = internalBinding('fs');

const nearestParentPackageJSONCache = new SafeMap();

/**
* @typedef {import('typings/internalBinding/modules').DeserializedPackageConfig} DeserializedPackageConfig
* @typedef {import('typings/internalBinding/modules').PackageConfig} PackageConfig
Expand Down Expand Up @@ -131,13 +134,21 @@ function read(jsonPath, { base, specifier, isESM } = kEmptyObject) {
* @returns {undefined | DeserializedPackageConfig}
*/
function getNearestParentPackageJSON(checkPath) {
if (nearestParentPackageJSONCache.has(checkPath)) {
return nearestParentPackageJSONCache.get(checkPath);
}

const result = modulesBinding.getNearestParentPackageJSON(checkPath);

if (result === undefined) {
nearestParentPackageJSONCache.set(checkPath, undefined);
return undefined;
}

return deserializePackageJSON(checkPath, result);
const packageConfig = deserializePackageJSON(checkPath, result);
nearestParentPackageJSONCache.set(checkPath, packageConfig);

return packageConfig;
}

/**
Expand Down
Loading