Add resolver, server, PDF rendering and web app

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-18 09:46:46 -07:00
1 parent 6a7fcdc4b8
commit bdfdf581f3
59 files changed
+4088

No files matched your search

+85
View File
@@ -0,0 +1,85 @@
import { BarcodeDetector, prepareZXingModule } from 'barcode-detector/ponyfill';
import { acceptRead, endRound, newReadState } from './readstate.ts';
export { acceptRead } from './readstate.ts';
export type { ReadState } from './readstate.ts';
/** The reader wasm is served by our own server (copied there by scripts/build-web.ts), never a CDN. */
export const WASM_PATH = '/zxing_reader.wasm';
let prepared = false;
function prepare(): void {
if (prepared) return;
prepared = true;
prepareZXingModule({
overrides: {
locateFile: (path: string, prefix: string) =>
path.endsWith('.wasm') ? WASM_PATH : prefix + path,
},
fireImmediately: true,
});
}
const INTERVAL_MS = 150;
export async function startScanner(
video: HTMLVideoElement,
deviceId: string | undefined,
onIsbn: (isbn13: string) => void,
onReject: (code: string) => void,
): Promise<() => void> {
prepare();
const stream = await navigator.mediaDevices.getUserMedia({
video: {
...(deviceId ? { deviceId: { exact: deviceId } } : { facingMode: 'environment' }),
width: { ideal: 1920 }, height: { ideal: 1080 },
},
audio: false,
});
let stopped = false;
let timer: ReturnType<typeof setTimeout> | undefined;
const stop = () => {
stopped = true;
if (timer !== undefined) clearTimeout(timer);
stream.getTracks().forEach(t => t.stop());
if (video.srcObject === stream) video.srcObject = null;
};
try {
video.srcObject = stream;
video.muted = true;
await video.play();
} catch (e) {
stop();
throw e;
}
const detector = new BarcodeDetector({ formats: ['ean_13'] });
let state = newReadState();
// Sequential loop: the next round is scheduled only after the previous detection finished,
// so two detections never overlap.
const tick = async () => {
if (stopped) return;
const began = performance.now();
if (video.readyState >= 2 && video.videoWidth > 0) {
try {
const found = await detector.detect(video);
if (stopped) return;
const codes = found.filter(f => f.format === 'ean_13').map(f => f.rawValue);
const now = Date.now();
for (const code of codes) {
const r = acceptRead(state, code, now);
state = r.state;
if (r.emit) onIsbn(r.emit);
else if (r.reject) onReject(code);
}
state = endRound(state, codes);
} catch { /* a failed frame is skipped */ }
}
if (stopped) return;
timer = setTimeout(tick, Math.max(0, INTERVAL_MS - (performance.now() - began)));
};
timer = setTimeout(tick, INTERVAL_MS);
return stop;
}