Files
worldmonitor/server
Elie Habib 122204f691 feat(brief): Phase 8 — Telegram carousel images via Satori + resvg-wasm (#3174)
* feat(brief): Phase 8 — Telegram carousel images via Satori + resvg-wasm

Implements the Phase 8 carousel renderer (Option B): server-side PNG
generation in a Vercel edge function using Satori (JSX to SVG) +
@resvg/resvg-wasm (SVG to PNG). Zero new Railway infra, zero
Chromium, same edge runtime that already serves the magazine HTML.

Files:

  server/_shared/brief-carousel-render.ts (new)
    Pure renderer: (BriefEnvelope, CarouselPage) -> Uint8Array PNG.
    Three layouts (cover/threads/story), 1200x630 OG size.
    Satori + resvg + WASM are lazy-imported so Node tests don't trip
    over '?url' asset imports and the 800KB wasm doesn't ship in
    every bundle. Font: Noto Serif regular, fetched once from Google
    Fonts and memoised on the edge isolate.

  api/brief/carousel/[userId]/[issueDate]/[page].ts (new)
    Public edge function reusing the magazine route's HMAC token —
    same signer, same (userId, issueDate) binding, so one token
    unlocks magazine HTML AND all three carousel images. Returns
    image/png with 7d immutable cache headers. 404 on invalid page
    index, 403 on bad token, 404 on Redis miss, 503 on missing
    signing secret. Render failure falls back to a 1x1 transparent
    PNG so Telegram's sendMediaGroup doesn't 500 the brief.

  scripts/seed-digest-notifications.mjs
    carouselUrlsFrom(magazineUrl) derives the 3 signed carousel
    URLs from the already-signed magazine URL. sendTelegramBriefCarousel
    calls Telegram's sendMediaGroup with those URLs + short caption.
    Runs before the existing sendTelegram(text) so the carousel is
    the header and the text the body — long-form stories remain
    forwardable as text. Best-effort: carousel failure doesn't
    block text delivery.

  package.json + package-lock.json
    satori ^0.10.14 + @resvg/resvg-wasm ^2.6.2.

Tests (tests/brief-carousel.test.mjs, 9 cases):
  - pageFromIndex mapping + out-of-range
  - carouselUrlsFrom: valid URL, localhost origin preserved, missing
    token, wrong path, invalid issueDate, garbage input
  - Drift guard: cron must still declare the same helper + template
    string. If it drifts, test fails with a pointer to move the impl
    into a shared module.

PNG render itself isn't unit-tested — Satori + WASM need a
browser/edge runtime. Covered by smoke validation step in the
deploy monitoring plan.

Both tsconfigs typecheck clean. 152/152 brief tests pass.

Scope boundaries (deferred):
  - Slack + Discord image attachments (different payload shapes)
  - notification-relay.cjs brief_ready dispatch (real-time route)
  - Redis caching of rendered PNG (edge Cache-Control is enough for
    MVP)

* fix(brief): address two P1 review findings on Phase 8 carousel

P1-A: 200 placeholder PNG cached 7d on render failure.
Route config said runtime: 'edge' but a comment contradicted it
claiming Node semantics. More importantly, any render/init failure
(WASM load, Satori, Google Fonts) was converted to a 1x1 transparent
PNG returned with Cache-Control: public, max-age=7d, immutable.
Telegram's media fetcher and Vercel's CDN would cache that blank
for the full brief TTL per chat message — one cold-start mismatch
= every reader of that brief sees blank carousel previews for a
week.

Fix: deleted errorPng(). Render failure now returns 503 with
Cache-Control: no-store. sendMediaGroup fails cleanly for that
carousel (the digest cron already treats it as best-effort and
still sends the long-form text message), next cron tick re-renders
from a fresh isolate. Self-healing across ticks. Contradictory
comment about Node runtime removed.

P1-B: Google Fonts as silent hard dependency.
The renderer claimed 'safe embedded/fallback path' in comments but
no fallback existed. loadFont() fetches Noto Serif from gstatic.com
and rethrows on any failure. Combined with P1-A's old 200-cache-7d
path, a transient CDN blip would lock in a blank carousel for a
week.

Fix: updated comments to honestly declare the CDN dependency plus
document the self-healing semantics now that P1-A's fix no longer
caches the failure. If Google Fonts reliability becomes a problem,
swap the fetch for a bundled base64 TTF — noted as the escape hatch.

Tests (tests/brief-carousel.test.mjs): 2 new regression cases.
11/11 carousel tests pass. Both tsconfigs typecheck clean locally.

Note on currently-red CI: failures are NOT typecheck errors — npm
ci dies fetching libvips for sharp (504 Gateway Time-out from
GitHub releases). sharp is a transitive dep via @xenova/transformers,
pre-existing, not touched by this PR. Transient infra flake.

* fix(brief): switch carousel to Node + @resvg/resvg-js (fixes deploy block)

Vercel edge bundler fails the carousel deploy with:
  'Edge Function is referencing unsupported modules:
   @resvg/resvg-wasm/index_bg.wasm?url'

The ?url asset-import syntax is a Vite-ism that Vercel's edge
bundler doesn't resolve. Two ways out: find a Vercel-blessed edge
WASM import incantation, or switch to Node runtime with the native
@resvg/resvg-js binding. The second is simpler, faster per request,
and avoids the whole WASM-in-edge-bundler rabbit hole.

Changes:
  - package.json: @resvg/resvg-wasm -> @resvg/resvg-js ^2.6.2
  - api/brief/carousel/.../[page].ts: runtime 'edge' -> 'nodejs20.x'
  - server/_shared/brief-carousel-render.ts: drop initWasm path,
    dynamic-import resvg-js in ensureLibs(). Satori and resvg load
    in parallel via Promise.all, shaving ~30ms off cold start.

Also addresses the P2 finding from review: the old ensureLibsAndWasm
had a concurrent-cold-start race where two callers could reach
'await initWasm()' simultaneously. Replaced the boolean flag with a
shared _libsLoadPromise so concurrent callers await the same load.
On failure the promise resets so the NEXT request retries rather
than poisoning the isolate for its lifetime.

Cold start ~700ms (Satori + resvg-js native init), warm ~40ms.
Carousel images are not latency-critical — fetched by Telegram's
media service, CDN-cached 7d.

Both tsconfigs typecheck clean. 11/11 carousel tests pass.

* fix(brief): carousel runtime = 'nodejs' (was 'nodejs20.x', rejected by Vercel)

Vercel's functions config validator rejects 'nodejs20.x' at deploy
time:

  unsupported "runtime" value in config: "nodejs20.x"
  (must be one of: ["edge","experimental-edge","nodejs"])

The Node version comes from the project's default (currently Node 20
via package.json engines + Vercel project settings), not from the
runtime string. Use 'nodejs' — unversioned — and let the platform
resolve it.

11/11 carousel tests pass.

* fix(brief): swap carousel font from woff2 to woff (Satori can't parse woff2)

Review on PR #3174: the FONT_URL was pointing at a gstatic.com woff2
file. Satori parses ttf / otf / woff v1 — NOT woff2. Every render
was about to throw on font decode, the route would return 503, and
the carousel would never deliver a single image.

Fix: point FONT_URL at @fontsource's Noto Serif Latin 400 WOFF v1
via jsdelivr. WOFF v1 is a TrueType wrapper that Satori parses
natively (verified: file says 'Web Open Font Format, TrueType,
version 1.1'). Same cold-start semantics as before — one fetch per
warm isolate, memoised.

Regression test: asserts FONT_URL ends in ttf/otf/woff and explicitly
rejects any .woff2 suffix. A future swap that silently reintroduces
woff2 now fails CI loudly instead of shipping a permanently-broken
renderer.

12/12 carousel tests pass. Both tsconfigs typecheck clean.
2026-04-18 20:27:41 +04:00
..