Files
reverse-engineering-anything/src/domain/webContentArtifact.ts
T
郭立 Guo Li 7590b50801 perf(schema): reduce retained integer validator allocations (#4360)
Construct integer formats directly while preserving bounds, refinements, metadata and current shared schema owners. Keep Incur-dependent CLI numeric coercion declarations unchanged.

Verify canonical advertisements, 126 numeric boundary cases, 974 actual CLI parser cases, broad source tests and real public-artifact SDK workflows. Record 72 normal tasks and 12 separate owned-inspector heap diagnostics with whole-family resource bounds; distinguish SDK spans and asymmetric audit overhead. Generate numerical report sections from receipts and reject stale tables through documentation conformance checks.

Closes #6236. Current-head CI and Greptile pass; actionable review findings are addressed. Codex review was unavailable due repository usage limits.
2026-10-22 05:11:08 +00:00

48 lines
1.4 KiB
TypeScript

import { createHash } from "node:crypto";
import { z } from "zod";
/** Metadata shared by complete and summarized retained web text artifacts. */
export const webTextArtifactMetadataSchema = z.object({
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
bytes: z.int().min(0),
media_type: z.string().min(1),
charset: z.literal("utf-8"),
});
/** Self-verifying text artifact used by web-source analysis boundaries. */
export const webTextArtifactSchema = webTextArtifactMetadataSchema
.extend({ text: z.string() })
.superRefine((artifact, context) => {
const bytes = Buffer.byteLength(artifact.text);
const digest = createHash("sha256").update(artifact.text).digest("hex");
if (artifact.bytes !== bytes)
context.addIssue({
code: "custom",
path: ["bytes"],
message: "Byte count mismatch",
});
if (artifact.sha256 !== digest)
context.addIssue({
code: "custom",
path: ["sha256"],
message: "Digest mismatch",
});
});
export type WebTextArtifact = z.infer<typeof webTextArtifactSchema>;
/** Build an immutable content-addressed UTF-8 text artifact. */
export const createWebTextArtifact = (
text: string,
mediaType: string,
): WebTextArtifact => {
const sha256 = createHash("sha256").update(text).digest("hex");
return {
sha256,
bytes: Buffer.byteLength(text),
media_type: mediaType,
charset: "utf-8",
text,
};
};