Files
wpep/tools/gp/pdf_builder/worker.ts
T

49 lines
1.3 KiB
TypeScript

import { type GPConfig } from "./pipeline.ts";
import { PublicationInput } from "../document/inputs.ts";
import { PdfOutput } from "../document/outputs.ts";
import { convertDocument } from "../document/types.ts";
import type { TemplateVariables } from "../template/engine.ts";
type WorkerRequest = {
id: number;
config: GPConfig;
options: {
addIndex?: boolean | number;
useQpdf?: boolean;
signPdf?: boolean;
signPassphrase?: string;
keepIndexPdf?: boolean;
variables?: TemplateVariables;
};
};
type WorkerResponse =
| { id: number; ok: true }
| { id: number; ok: false; error: string };
const workerGlobal = self as unknown as {
onmessage: ((event: MessageEvent<WorkerRequest>) => void) | null;
postMessage: (message: WorkerResponse) => void;
};
workerGlobal.onmessage = async (event: MessageEvent<WorkerRequest>) => {
const { id, config, options } = event.data;
try {
await convertDocument(
new PublicationInput(config, options.variables),
new PdfOutput(config, options),
);
workerGlobal.postMessage({ id, ok: true } satisfies WorkerResponse);
} catch (error) {
workerGlobal.postMessage(
{
id,
ok: false,
error: error instanceof Error ? error.stack ?? error.message : String(
error,
),
} satisfies WorkerResponse,
);
}
};