fix(javascript): isolate package facts from host prototypes

Build frozen package field snapshots with a null prototype and restrict reference-source main lookup to own properties. Preserve declared null and empty values, index fallback, source identity and unavailable manifest evidence.

Compare inherited main and exports lookup with the native Node resolver, retain package observation coverage and verify CLI/MCP package workflows.
This commit is contained in:
morluto
2026-10-23 21:57:14 +08:00
parent 2bcd7e6d13
commit 610966b836
5 changed files with 141 additions and 33 deletions
@@ -135,18 +135,16 @@ const parseDocument = (
const topLevelKeys = Object.freeze(
Object.keys(record).sort(compareUnicodeCodePoints),
);
const packageFields =
snapshot.kind === "package-json"
? Object.freeze(
Object.fromEntries(
PACKAGE_PROPERTIES.flatMap((key) =>
Object.hasOwn(record, key)
? [[key, Reflect.get(record, key)]]
: [],
),
),
)
: null;
// Absent source fields must stay absent when consumers read named properties.
const packageFields: Partial<
Record<(typeof PACKAGE_PROPERTIES)[number], unknown>
> | null = snapshot.kind === "package-json" ? Object.create(null) : null;
if (packageFields !== null) {
for (const key of PACKAGE_PROPERTIES)
if (Object.hasOwn(record, key))
packageFields[key] = Reflect.get(record, key);
Object.freeze(packageFields);
}
return Object.freeze({
source: snapshot.source,
status: "included",