Skip to content
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