跳到正文
DEV Community· ops-sd·· 6 小時前AI 評分33

在 Cloudflare Workers 上以純 TypeScript 建構 13x13 填字遊戲 CSP 求解器及 100 頁向量 PDF 引擎

Building a Pure-TypeScript 13x13 Crossword CSP Solver and 100-Page Vector PDF Engine on Cloudflare Workers

AI 導讀

ClueBook 在 OpenNext Cloudflare Workers 上以純 TypeScript 建構 13x13 填字遊戲求解器、向量 PDF 排版及封面生成流程,無需 Python 執行環境或無頭瀏覽器。

正文

ops-sd

Turning a group of friends' inside jokes into a real, print-ready crossword keepsake book sounds simple until you try to run both puzzle generation and 100-page book typesetting inside a single edge Worker.

When we built ClueBook and its interactive puzzle & sample PDF preview, our main constraint was keeping the entire pipeline—13x13 crossword solving, vector PDF interior layout, and computed-spine cover generation—in pure TypeScript on OpenNext Cloudflare Workers with zero Python runtimes or headless browsers.

1. Deterministic 13x13 Bitmask CSP Solver

Contributor clues are short, irregular, and unpredictable. A naive backtracking solver easily stalls or blows past Worker CPU budgets on dense intersections.

We split grid construction into two bounded stages:

  • Skeleton placement: Personal answers are placed first with a strict cap of 3,500 skeleton steps to maximize crossings between contributor entries.
  • Bitmask CSP fill: Remaining open slots are filled against an open-licensed SCOWL wordlist using bitmask domain filtering, capped at 8,000 CSP nodes per grid with a deterministic fallback so every generation completes predictably.

Because the solver is deterministic for a given approved clue set, regenerating a book after moderation or order fulfillment always yields the exact same puzzles and answer keys.

2. 100-Page 6x9 Vector PDF in Under 200ms

Instead of rendering HTML through Chromium (which adds cold starts and memory spikes), we build the entire 100-page 6x9 (450 x 666 pt with print bleed) interior and dynamic-spine cover wrap directly with pdf-lib:

  • Vector grid lines, crisp clue numbering, dedication pages, and answer keys stream out without rasterization bloat.
  • The 100-page interior renders in under 200ms and the cover wrap in under 30ms, avoiding long-term blob storage of customer content.

3. Idempotent Fulfillment on D1

Paid print orders move through an atomic compare-and-swap state transition in Cloudflare D1 (paid -> submitting_to_lulu -> submitted_to_lulu) backed by an idempotent Stripe webhook log, ensuring retried webhooks never create duplicate print jobs.

You can test the live 13x13 solver and 3-page sample PDF layout directly in the ClueBook Preview or create a group puzzle at getcluebook.com.

來源:DEV Community · dev.to