Files
reverse-engineering-anything/src/artifacts/formatRecordHeaderPage.ts
T
ShuaixinHuangandVincent Koc 76c4e7fa18 perf(artifacts): verify and decode journal header pages in one pass (#15835)
* perf(artifacts): verify and decode journal header pages in one pass

* test(artifacts): verify journal cancellation during header streaming

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-12-02 09:02:09 +00:00

85 lines
3.1 KiB
TypeScript

import type { AnalysisPartialObservation } from "../domain/analysisErrorBase.js";
import {
ArtifactReaderFailure,
ArtifactResourceScope,
type ArtifactResourceOwner,
} from "../resources/index.js";
import { SystemdJournalArtifactReader } from "./SystemdJournalArtifactReader.js";
import type { FormatRecordHeader } from "../domain/analysisView/analysisViewParent.js";
/** One page of a root artifact's per-record headers. */
export interface FormatRecordHeaderPageRequest {
readonly path: string;
readonly format: string;
/** The parent inventory's root digest; the file must still match it. */
readonly sha256: string;
readonly start: number;
readonly limit: number;
readonly signal?: AbortSignal | undefined;
/** Retains the reader when its cleanup fails. */
readonly resources?: ArtifactResourceScope | undefined;
}
/** Read a bounded header window while verifying the digest of the same bytes. */
export const readFormatRecordHeaderPage = async (
request: FormatRecordHeaderPageRequest,
/**
* The caller's completed observation of a read page, kept on a cleanup
* failure so finished work survives a reader that fails to close.
*/
observe?: (
items: readonly FormatRecordHeader[],
) => AnalysisPartialObservation | undefined,
): Promise<readonly FormatRecordHeader[]> => {
if (request.format !== "systemd-journal")
throw new ArtifactReaderFailure(
"unavailable",
`${request.format} declares no per-record header window to page`,
);
const reader = new SystemdJournalArtifactReader(request.path, undefined, {
objectHeaderStart: request.start,
pageSha256: request.sha256,
});
const resources = request.resources ?? new ArtifactResourceScope();
// Entering the scope retries any reader an earlier page failed to close, and
// refuses this page while that cleanup stays unresolved, so repeated close
// failures cannot accumulate open descriptors.
return resources.run(async () => {
const owner: ArtifactResourceOwner = {
kind: "reader",
resource: request.path,
reader,
};
let outcome:
| {
readonly kind: "completed";
readonly value: readonly FormatRecordHeader[];
}
| { readonly kind: "failed"; readonly cause: unknown };
try {
for await (const _entry of reader.entries(request.signal)) {
// Windowed formats declare no members; traversal builds the window.
}
const metadata = reader.formatMetadata?.();
outcome = {
kind: "completed",
value:
metadata?.kind === "systemd-journal"
? metadata.object_headers.slice(0, Math.max(0, request.limit))
: [],
};
} catch (cause: unknown) {
outcome = { kind: "failed", cause };
}
const closed = await resources.release(owner);
if (closed.kind === "failed")
throw resources.cleanupFailure(
outcome.kind === "completed" ? { kind: "completed" } : outcome,
[closed],
outcome.kind === "completed" ? observe?.(outcome.value) : undefined,
);
if (outcome.kind === "failed") throw outcome.cause;
return outcome.value;
});
};