FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

src: add cache to nearest parent package json by IlyasShabi · Pull Request #59086 · nodejs/node · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
src: add cache to nearest parent package json
  • Loading branch information
IlyasShabi committed Jul 21, 2025
commit 0a098d202fbb11341b2b1e95e3664d943f8698a0
13 changes: 12 additions & 1 deletion lib/internal/modules/package_json_reader.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL