Files
openworker/harness/examples/quickstart/web/pdf.mjs
Rohit PrasadandDevika Verma d53e058eda Introduce OpenWorker Harness SDK (in beta), agent service cluster and relevant developer documentation (#844)
Allow developers to easily build AI apps without worrying about the harness part.
Bundles the Rust agent runtime as a SDK (Rust and TypeScript) for building desktop and web applications. Applications can manage a local harness or connect to an authenticated harness service on k8s with separately scaled workers and sandbox execution.

The implementation is primarily contained in harness/, with deployment tooling in deploy/, Markdown guides in docs/harness/guides/, and the static documentation site in docs-site/. It does not replace the existing Python harness or migrate OpenWorker Desktop.

Co-authored-by: Devika Verma <devikaverma11@gmail.com>
2026-11-06 09:45:20 -08:00

32 lines
1.1 KiB
JavaScript

import { Worker } from "node:worker_threads";
// Parse untrusted PDFs in a bounded worker. No document JavaScript or rendering.
export function extractPdf(bytes) {
if (bytes.length > 8 * 1024 * 1024)
return Promise.reject(Error("PDF must be 8 MB or smaller."));
if (!bytes.subarray(0, 1024).includes(Buffer.from("%PDF-")))
return Promise.reject(Error("Choose a PDF file."));
return new Promise((resolve, reject) => {
const worker = new Worker(new URL("./pdf-worker.mjs", import.meta.url), {
workerData: new Uint8Array(bytes),
resourceLimits: { maxOldGenerationSizeMb: 192 },
});
const timer = setTimeout(() => {
worker.terminate();
reject(Error("PDF parsing timed out."));
}, 15000);
worker.once("message", (value) => {
clearTimeout(timer);
worker.terminate();
value.error ? reject(Error(value.error)) : resolve(value);
});
worker.once("error", (error) => {
clearTimeout(timer);
reject(error);
});
worker.once("exit", (code) => {
clearTimeout(timer);
if (code !== 0) reject(Error("PDF parser stopped."));
});
});
}