fix: deterministic pdf render and restore pnpm pin
- Revert packageManager from pnpm@11.16.0 back to pnpm@10.34.5 (restore the plan's pinned version; the PATH pnpm is unaffected). - Inject a deterministic PDFKit document /ID derived from SHA-256(snapshot.documentId) so the same snapshot always produces byte-identical output, making the stored SHA-256 a verifiable integrity fingerprint. - Add covering test: render same fixture twice, assert bytes.equals() and sha256 equality. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
89d76e2711
commit
a384c4c35e
3 files changed
+32
-2
No files matched your search
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"packageManager": "pnpm@11.16.0",
|
"packageManager": "pnpm@10.34.5",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=24 <25"
|
"node": ">=24 <25"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -52,3 +52,10 @@ test("pdf buffer is non-empty and starts with the PDF magic header", async () =>
|
|||||||
expect(result.bytes.length).toBeGreaterThan(1000);
|
expect(result.bytes.length).toBeGreaterThan(1000);
|
||||||
expect(result.bytes.subarray(0, 5).toString("ascii")).toBe("%PDF-");
|
expect(result.bytes.subarray(0, 5).toString("ascii")).toBe("%PDF-");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("same snapshot produces byte-identical deterministic output", async () => {
|
||||||
|
const a = await renderBillingPdf(reportFixture);
|
||||||
|
const b = await renderBillingPdf(reportFixture);
|
||||||
|
expect(a.bytes.equals(b.bytes)).toBe(true);
|
||||||
|
expect(a.sha256).toBe(b.sha256);
|
||||||
|
});
|
||||||
@@ -20,7 +20,7 @@ import { buildDocumentDefinition, FONT_NAME } from "./document";
|
|||||||
const FONTS_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "fonts");
|
const FONTS_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "fonts");
|
||||||
|
|
||||||
interface PdfMakeInstance {
|
interface PdfMakeInstance {
|
||||||
createPdf(docDefinition: unknown): { getBuffer(): Promise<Buffer> };
|
createPdf(docDefinition: unknown): PdfMakeResult;
|
||||||
setFonts(fonts: Record<string, unknown>): void;
|
setFonts(fonts: Record<string, unknown>): void;
|
||||||
setLocalAccessPolicy(callback: (p: string) => boolean): void;
|
setLocalAccessPolicy(callback: (p: string) => boolean): void;
|
||||||
virtualfs: {
|
virtualfs: {
|
||||||
@@ -29,6 +29,16 @@ interface PdfMakeInstance {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface PdfMakeResult {
|
||||||
|
/** Returns the underlying PDFKit document (a Node Readable stream). */
|
||||||
|
getStream(): Promise<PdfKitDocument>;
|
||||||
|
getBuffer(): Promise<Buffer>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PdfKitDocument {
|
||||||
|
_id: Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
// pdfmake exports a CommonJS singleton; use createRequire to preserve the
|
// pdfmake exports a CommonJS singleton; use createRequire to preserve the
|
||||||
// `this` context that methods like setFonts rely on.
|
// `this` context that methods like setFonts rely on.
|
||||||
const require = createRequire(import.meta.url);
|
const require = createRequire(import.meta.url);
|
||||||
@@ -70,6 +80,19 @@ export type RenderedPdf = {
|
|||||||
export async function renderBillingPdf(snapshot: BillingReportSnapshot): Promise<RenderedPdf> {
|
export async function renderBillingPdf(snapshot: BillingReportSnapshot): Promise<RenderedPdf> {
|
||||||
const docDefinition = buildDocumentDefinition(snapshot);
|
const docDefinition = buildDocumentDefinition(snapshot);
|
||||||
const pdf = pdfmake.createPdf(docDefinition);
|
const pdf = pdfmake.createPdf(docDefinition);
|
||||||
|
|
||||||
|
// Inject a deterministic PDFKit document /ID derived from the snapshot's
|
||||||
|
// documentId. PDFKit normally generates a random /ID via MD5 of the info
|
||||||
|
// dictionary (including a `new Date()` fallback). We override it after
|
||||||
|
// document creation but before the buffer is flushed so that the same
|
||||||
|
// snapshot always produces byte-identical output — making the stored
|
||||||
|
// SHA-256 a verifiable integrity fingerprint rather than a random index.
|
||||||
|
// We use the first 16 bytes of SHA-256(documentId) to match the 16-byte
|
||||||
|
// width that PDFKit's MD5-based generateFileID produces.
|
||||||
|
const docId = createHash("sha256").update(snapshot.documentId).digest().subarray(0, 16);
|
||||||
|
const stream = await pdf.getStream();
|
||||||
|
stream._id = docId;
|
||||||
|
|
||||||
const bytes = await pdf.getBuffer();
|
const bytes = await pdf.getBuffer();
|
||||||
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
||||||
return { bytes, sha256 };
|
return { bytes, sha256 };
|
||||||
|
|||||||
Reference in new issue
Block a user