refactor(runtime): centralize safe JSON parsing and preserve error causes (#629)
* refactor(domain): consolidate canonical ordering and digest helpers (#624) * refactor(domain): consolidate canonical ordering and digest helpers Single owner for code-point ordering (domain/canonicalOrdering.ts) and canonical JSON digests (domain/comparisonSemantics.ts). Delete all local compareCodePoints/compareText/digestCanonical duplicates and migrate every importer. No re-export shims. * fix(ci): allow slower real browser startup * chore(config): remove dead eslint, dedupe scripts, cacheable turbo tasks Delete eslint.config.js (oxlint is the linter), drop lint:dead alias, pin smol-toml, fold jscpd/scan:todos into check:pr, make test:run cacheable, align oxlint ignores. * fix(config): hash all test inputs for cached runs * docs: fix architecture diagram and pretty-print tool catalog Drop dangling REPLAY edges, add dotnet/reference/error/evaluation nodes. Emit 2-space JSON from the catalog generator and regenerate. * test: extract shared fixtures, drop knip carve-outs Dedupe android/apple session tests, bundler adapter headers, and reconstruction helpers into tests/support fixtures. Delete unused type aliases and the knip ignoreIssues block. Lower jscpd threshold 2.7 to 2.5. * refactor(app): split Setup and ProcessOwnership modules Extract SetupTypes/SetupHost from Setup.ts and move observation helpers into ProcessOwnershipObservation/ProcessTree. Migrate all importers including CLI commands. No re-export shims. * refactor(runtime): centralize safe JSON parsing and preserve error causes Add domain/safeJson.safeParseJson and migrate all raw JSON.parse call sites. Name catch causes and preserve failure reasons instead of swallowing; annotate genuine best-effort cleanup. * fix(runtime): retain JSON parse error causes
This commit is contained in:
@@ -415,6 +415,7 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
);
|
||||
if (!capture.ok) return capture;
|
||||
const parsed = parsePlistJson(capture.value.stdout);
|
||||
if (!parsed.ok) return parsed;
|
||||
const provenance = [classified.value, capture.value].map((item) =>
|
||||
invocation(item, plist.value, "$PLIST"),
|
||||
);
|
||||
@@ -424,7 +425,7 @@ class NativeMacOSClient implements AnalysisClient {
|
||||
: /XML|text/iu.test(classified.value.stdout)
|
||||
? "xml"
|
||||
: "unknown",
|
||||
...parsed,
|
||||
...parsed.value,
|
||||
source_path: plist.value,
|
||||
provenance,
|
||||
limitations: [],
|
||||
|
||||
@@ -3,6 +3,7 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { execFileOutput } from "../process/ExecFileOutput.js";
|
||||
import { safeParseJson } from "../domain/safeJson.js";
|
||||
import type { NativeUiHelper } from "./NativeUiObservation.js";
|
||||
|
||||
/** Lazily compile one owned helper per observation/scenario and remove its compiler cache. */
|
||||
@@ -42,7 +43,12 @@ export const createNativeUiHelperRuntime = () => {
|
||||
...(signal === undefined ? {} : { signal }),
|
||||
},
|
||||
);
|
||||
return JSON.parse(output.stdout) as unknown;
|
||||
const parsed = safeParseJson(output.stdout);
|
||||
if (!parsed.ok)
|
||||
throw new Error(`Native helper returned invalid JSON: ${parsed.error}`, {
|
||||
cause: parsed.cause,
|
||||
});
|
||||
return parsed.value;
|
||||
};
|
||||
return {
|
||||
invoke,
|
||||
|
||||
@@ -1,18 +1,43 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { AnalysisOutputError } from "../../domain/errors.js";
|
||||
import { err, ok, type Result } from "../../domain/result.js";
|
||||
import { safeParseJson } from "../../domain/safeJson.js";
|
||||
|
||||
const plistObject = z.record(z.string(), z.unknown());
|
||||
|
||||
/** Parse plutil JSON output and project stable bundle metadata. */
|
||||
export const parsePlistJson = (output: string) => {
|
||||
export const parsePlistJson = (
|
||||
output: string,
|
||||
): Result<
|
||||
{
|
||||
readonly value: unknown;
|
||||
readonly bundle: {
|
||||
readonly identifier: string | null;
|
||||
readonly executable: string | null;
|
||||
readonly name: string | null;
|
||||
readonly version: string | null;
|
||||
readonly short_version: string | null;
|
||||
};
|
||||
},
|
||||
AnalysisOutputError
|
||||
> => {
|
||||
// plutil output can carry a UTF-8 BOM, which JSON.parse rejects outright.
|
||||
const value: unknown = JSON.parse(output.replace(/^\uFEFF/u, ""));
|
||||
const parsed = safeParseJson(output.replace(/^\uFEFF/u, ""));
|
||||
if (!parsed.ok)
|
||||
return err(
|
||||
new AnalysisOutputError("inspect_plist", parsed.error, {
|
||||
cause: parsed.cause,
|
||||
}),
|
||||
);
|
||||
const value: unknown = parsed.value;
|
||||
const object = plistObject.safeParse(value);
|
||||
const field = (name: string): string | null => {
|
||||
if (!object.success) return null;
|
||||
const candidate = object.data[name];
|
||||
return typeof candidate === "string" ? candidate : null;
|
||||
};
|
||||
return {
|
||||
return ok({
|
||||
value,
|
||||
bundle: {
|
||||
identifier: field("CFBundleIdentifier"),
|
||||
@@ -21,5 +46,5 @@ export const parsePlistJson = (output: string) => {
|
||||
version: field("CFBundleVersion"),
|
||||
short_version: field("CFBundleShortVersionString"),
|
||||
},
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user