AI, fully local on your computer.
TypeScript aiai-toolskiwixllama-cpplocal-ailocal-llmofflineoffline-aioffline-firstprivacy-firstragwikipediazim
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { app, BrowserWindow, ipcMain } from 'electron'
import { OCR_RASTER } from '../../../shared/ipc'
import { log } from '../logging'
import { installNavigationGuard } from '../navigation-guard'
import { SECURE_WINDOW_WEB_PREFERENCES } from '../../window-security'
import { appPageUrl, devRendererUrl } from '../../app-protocol'
import { assertPageWithinByteCap } from './page-cap'
import { pagesToWalk, pipelinePages } from './pipeline'
import { resolveIngestionLimits } from '../ingestion/limits'
import { watchRunWindowGone } from './window-gone'
// ESM main bundle — `__dirname` doesn't exist; reconstruct from import.meta.url.
const __dirname = dirname(fileURLToPath(import.meta.url))
// PDF → page-PNG rasterizer: rendering a PDF page
// to pixels needs a canvas, the main process has none, node-canvas
// is a native dep (avoided by policy), and Electron's utilityProcess has NO
// OffscreenCanvas (probed). So a HIDDEN BrowserWindow does exactly this one job:
// open the PDF with the pinned pdfjs, render one page at a time on request, return
// PNG bytes. Recognition stays main-side; the window is created per task and
// destroyed afterwards (or on cancel).
//
// Bounded 1-deep look-ahead (ING-5): render (pdfjs in the hidden window) and recognize
// (the caller's `onPage`, e.g. WASM tesseract) are different engines, so page N+1 is
// rendered WHILE page N is recognized — the two pipeline instead of running strictly
// serially. Memory stays bounded: at most one extra page PNG is resident (the just-rendered
// N+1 while N recognizes), so a 500-page scan never queues 500 images. Recognitions stay
// serial and in order (one engine, one in flight), so progress %, ordering, and cancellation
// are unchanged from the old strictly-serial loop. See `pipelinePages`.
export interface RasterizePdfOptions {
/**
* Called once, as soon as the page count is known, with the number of pages this run will
* render (all of them, or the `pages` that exist).
*/
onPageCount?: (pageCount: number) => void
/** #575: render only these page numbers (a text PDF's scanned pages); absent ⇒ every page. */
pages?: readonly number[]
/**
* Called per page, in order, recognitions serialized. The rasterizer renders ONE page
* ahead while this promise is pending (1-deep look-ahead), but never starts the next
* `onPage` until this one resolves — so memory holds at most one extra PNG. A throw
* aborts the run.
*/
onPage: (pageNumber: number, png: Buffer) => void | Promise<void>
/** Abort: destroys the hidden window; the promise rejects with AbortError. */
signal?: AbortSignal
}
/** The rasterizer seam the OCR task uses — fakeable in tests (no Electron). */
export type RasterizePdf = (
pdf: Buffer,
opts: RasterizePdfOptions
) => Promise<{ pageCount: number }>
/**
* Per-page render+open timeout — a wedged renderer must fail the task, not hang it.
* Exported for the wiring harness (tests/unit/ocr-rasterizer-harness.test.ts), the
* PRINT_STEP_TIMEOUT_MS idiom.
*/
export const RASTER_STEP_TIMEOUT_MS = 60_000
function abortError(): DOMException {
return new DOMException('PDF rasterization aborted', 'AbortError')
}
/**
* The hidden window's single-slot reply waiter. The rasterize protocol keeps exactly ONE request in
* flight at a time (open, then page-by-page), so there is normally never a second `expect()` before
* the prior settles. R6 (full-audit-2026-06-30, Phase C) hardens the slot regardless: a fresh
* `expect()` while a prior waiter is still unsettled REJECTS the prior ('superseded') instead of
* silently overwriting it — an overwritten waiter would otherwise orphan and hang to its 60 s
* `withTimeout` (the symptom a duplicate reply frame or a future refactor could trigger). Extracted
* from the per-run closure so the supersede guard is unit-testable without Electron.
*/
export class RasterReplySlot {
private waiter: { resolve: (m: Record<string, unknown>) => void; reject: (e: Error) => void } | null = null
private channel: string | null = null
/** Arm for `channel` and return the reply promise, superseding any still-pending prior waiter (R6). */
expect(channel: string): Promise<Record<string, unknown>> {
return new Promise<Record<string, unknown>>((resolve, reject) => {
if (this.waiter) this.waiter.reject(new Error('superseded'))
this.waiter = { resolve, reject }
this.channel = channel
})
}
/** True iff a reply on `channel` is the one currently awaited (the `expectChannel` gate). */
awaits(channel: string): boolean {
return this.channel === channel
}
/** Resolve the current waiter with `payload` and clear the slot (no-op if none pending). */
deliver(payload: Record<string, unknown>): void {
const w = this.waiter
this.waiter = null
w?.resolve(payload)
}
/** Reject the current waiter (error frame / abort) and clear it; returns whether one was pending. */
fail(err: Error): boolean {
const w = this.waiter
this.waiter = null
if (!w) return false
w.reject(err)
return true
}
}
/**
* Rasterize `pdf` page by page through a hidden window. One run at a time per
* process (the DocTaskManager serializes tasks anyway; a second concurrent call
* fails fast rather than crosstalking on the shared channels).
*/
let inUse = false
export async function rasterizePdfWithHiddenWindow(
pdf: Buffer,
opts: RasterizePdfOptions
): Promise<{ pageCount: number }> {
if (inUse) throw new Error('PDF rasterizer is busy')
if (opts.signal?.aborted) throw abortError()
inUse = true
// Window construction can throw (resource exhaustion, mid-quit). Reset the busy
// flag if it does, or the rasterizer wedges to "busy" for the rest of the session
// (the `finally` below only runs once we are past this point).
let win: BrowserWindow
try {
win = new BrowserWindow({
show: false,
// Never steal focus or appear in any window list UX; it is a worker, not a UI.
skipTaskbar: true,
webPreferences: {
preload: join(__dirname, '../preload/ocr.js'),
...SECURE_WINDOW_WEB_PREFERENCES
}
})
} catch (err) {
inUse = false
throw err
}
// Untrusted PDF bytes render here — deny any navigation/window-open the page attempts
// (defence in depth on top of the shared-session CSP), matching the main window. SEC-3
// (backend-audit-2026-06-27): the guard covers `will-redirect` alongside `will-navigate`
// (a server/<meta> redirect reaches a remote origin via `will-redirect` without firing
// `will-navigate`). This worker page only ever loads ocr.html, so ALL navigation is denied.
win.webContents.setWindowOpenHandler(() => ({ action: 'deny' }))
installNavigationGuard(win.webContents, () => false)
// Collect replies addressed from OUR window only (defence in depth — the channels
// are not exposed on the main window's bridge at all). One request in flight at a time (R6).
const slot = new RasterReplySlot()
const onMessage =
(channel: string) =>
(event: Electron.IpcMainEvent, payload: Record<string, unknown>): void => {
if (win.isDestroyed() || event.sender !== win.webContents) return
if (channel === OCR_RASTER.error) {
const message = typeof payload?.message === 'string' ? payload.message : 'render failed'
// No request in flight (e.g. the error raced a timeout/abort that already cleared the
// waiter) — don't drop it silently; the next expect() would otherwise hang to its timeout
// with no trace of the real cause.
if (!slot.fail(new Error(message))) {
log.warn('OCR rasterizer error frame arrived with no request in flight', { message })
}
return
}
if (!slot.awaits(channel)) return
slot.deliver(payload ?? {})
}
const listeners: Array<[string, ReturnType<typeof onMessage>]> = [
[OCR_RASTER.opened, onMessage(OCR_RASTER.opened)],
[OCR_RASTER.page, onMessage(OCR_RASTER.page)],
[OCR_RASTER.error, onMessage(OCR_RASTER.error)]
]
for (const [ch, fn] of listeners) ipcMain.on(ch, fn)
const onAbort = (): void => {
slot.fail(abortError())
}
opts.signal?.addEventListener('abort', onAbort, { once: true })
// BE-7 (ocr-audit 2026-07-18): if the hidden renderer dies mid-scan (an OOM on a huge
// image-heavy PDF, a GPU-process reset), fail the run immediately instead of letting the
// in-flight page render hang to its 60 s step timeout — and then burn another 60 s per
// remaining step. Failing the pending waiter rejects the current render; the OCR task maps it
// to the friendly `ocrFailed`. The listener is torn down with the rest in the `finally`.
const onRenderGone = (_event: Electron.Event, details: Electron.RenderProcessGoneDetails): void => {
log.warn('OCR rasterizer render process gone', { reason: details?.reason })
slot.fail(new Error('The renderer process ended while rendering the PDF'))
}
win.webContents.on('render-process-gone', onRenderGone)
// #910: the window going away mid-run (`window-gone.ts`). After a quit was asked the run ends as
// an abort, so the OCR task ends `cancelled` on every quit path, the ones the close guard holds
// included; with no quit asked it fails at once. Never after the 60 s step timeout. The outcome
// holds for whatever step the run was on (the catch below). Disposed in the `finally`, before the
// window is destroyed there.
const windowGone = watchRunWindowGone({
app,
webContents: win.webContents,
isWindowDestroyed: () => win.isDestroyed(),
failPage: (err) => slot.fail(err),
abortError,
log
})
const withTimeout = async (p: Promise<Record<string, unknown>>): Promise<Record<string, unknown>> => {
let timer: ReturnType<typeof setTimeout> | null = null
try {
return await Promise.race([
p,
new Promise<never>((_, reject) => {
timer = setTimeout(
() => reject(new Error('The page took too long to render')),
RASTER_STEP_TIMEOUT_MS
)
})
])
} finally {
if (timer) clearTimeout(timer)
}
}
/**
* One request on the shared channel: arm the reply, send, wait (bounded). A window already gone is not sent to
* (Electron throws "Object has been destroyed"); the run's catch turns either failure into the window-gone outcome.
*/
const request = (reply: string, channel: string, payload: unknown): Promise<Record<string, unknown>> => {
if (win.isDestroyed()) return Promise.reject(new Error('The rasterizer window is gone'))
const replyP = slot.expect(reply)
try {
win.webContents.send(channel, payload)
} catch (err) {
slot.fail(err instanceof Error ? err : new Error(String(err)))
}
return withTimeout(replyP)
}
try {
// Load the window's page: dev server in dev, the app's own scheme otherwise (the main
// window's pattern; #560 moved it off `file://` — app-protocol.ts). A packaged build
// ignores the dev-server variable (#562).
const devUrl = devRendererUrl(app.isPackaged)
await win.loadURL(devUrl ? `${devUrl}/ocr.html` : appPageUrl('ocr'))
if (opts.signal?.aborted) throw abortError()
const opened = await request(OCR_RASTER.opened, OCR_RASTER.open, { pdf: new Uint8Array(pdf) })
const declaredPageCount = Number(opened.pageCount)
if (!Number.isInteger(declaredPageCount) || declaredPageCount <= 0) {
throw new Error('The PDF could not be opened for rendering')
}
// BE-6 (ocr-audit 2026-07-18): clamp the walk to the ingestion M-2 page cap — a tiny PDF can
// declare an enormous page count. Report the CLAMPED count via onPageCount so `stepsTotal`,
// progress and the persisted `ocr_json.pageCount` stay truthful; pipelinePages enforces the
// same cap on the walk itself.
const maxPages = resolveIngestionLimits().pdfMaxPages
const pageCount = Math.min(declaredPageCount, maxPages)
if (pageCount < declaredPageCount) {
log.warn('OCR page walk clamped to the ingestion page cap (M-2 parity)', {
declaredPageCount,
maxPages
})
}
opts.onPageCount?.(pagesToWalk(declaredPageCount, { maxPages, pages: opts.pages }).length)
// ING-5: render page N+1 while page N recognizes (1-deep look-ahead). The render itself
// stays sequential on the shared channel (one `expect`/`send` in flight); only render and
// recognize — different engines — overlap. See `pipelinePages`.
await pipelinePages(
declaredPageCount,
async (pageNumber) => {
const msg = await request(OCR_RASTER.page, OCR_RASTER.render, { pageNumber })
const png = msg.png
if (!(png instanceof Uint8Array)) throw new Error('The rendered page was empty')
// REL-4: reject an over-cap page before it is handed to recognition or held resident
// behind the 1-deep look-ahead.
assertPageWithinByteCap(png)
return Buffer.from(png)
},
opts.onPage,
// BE-6: pipelinePages enforces the same cap on the walk (min(declared, maxPages)).
{ signal: opts.signal, abortError, maxPages, pages: opts.pages }
)
return { pageCount }
} catch (err) {
// #910 (code review): the window went away while the run was on any step (a page waiting, a recognition still
// running before the next request, the page load): the run ends as the window-gone outcome, one task after the
// window's `destroyed`. The task's own cancel keeps its abort.
if (!opts.signal?.aborted) {
const gone = await windowGone.outcomeIfGone()
if (gone) throw gone
}
throw err
} finally {
inUse = false
opts.signal?.removeEventListener('abort', onAbort)
for (const [ch, fn] of listeners) ipcMain.removeListener(ch, fn)
windowGone.dispose()
if (!win.isDestroyed()) {
win.webContents.removeListener('render-process-gone', onRenderGone)
win.destroy()
}
}
}