Skip to content

kit/speed: the Speed client, starting with social cards, with a shared failover - #33

Merged
jgb-solutions merged 5 commits into
mainfrom
feat/speed-client
Dec 6, 2026
Merged

jgb-solutions merged 5 commits into
mainfrom
feat/speed-client

Conversation

@jgb-solutions

@jgb-solutions jgb-solutions commented Dec 6, 2026 •

Copy link
Copy Markdown
Owner

A new module, @jgb-solutions/kit/speed: typed clients for the Speed service, one per feature, called from an app's server. The first feature is social cards. Every feature shares one failover, so Speed never breaks or slows a page.

What

  • Transport: gRPC-Web over fetch, through Connect-ES 2.3 with messages from protobuf-es 2.17. The same code runs on Cloudflare Workers, Bun and Node.
  • createSocialCardsClient({ url, token, timeoutMs?, onFallback?, breaker? }):
    • The token goes in an authorization: Bearer header.
    • Each call gets a 1500 ms deadline and is never retried.
    • Redirects are never followed (see Workers below).
  • getCard(input) returns { url, width, height, contentType, created, key }. On failure it throws a SpeedError with a typed code: timeout, canceled, unauthenticated, forbidden, invalid_input, rate_limited, unavailable, bad_response, not_configured or failed.
  • getCardOr(input, fallback) never throws and never waits past the deadline. It returns { url, width, height, fromSpeed }.
    • The fallback is the app's own image, sized to the format (the logo on the brand colour is the recommended share image), or a hex colour drawn as a tiny inline SVG.
  • Shared by every feature:
    • withFallback and colorImage.
    • CircuitBreaker, one per client: after 3 failures in 30 s it skips Speed for 30 s, then lets one trial call through.
    • onFallback(reason), where the reason is timeout, error, breaker-open or not-configured.
  • No SPEED_URL or SPEED_TOKEN: the fallback comes back at once, so an app can ship before Speed is live.
  • Workers: Connect asks fetch for redirect: 'error'. Workers refuse that before compatibility date 2026-12-22 unless the fetch_redirect_error flag is set, so without a fix every call would fail and silently fall back. The client sends redirect: 'manual' and refuses any 3xx itself. The token never goes to another address.
  • The proto: proto/speed/socialcards/v1/social_cards.proto is copied unchanged from Speed's repository at 10648f9 (see proto/README.md). bun run generate (buf, then protoc-gen-es) rewrites src/speed/gen/. A new CI step runs buf lint and fails if the generated code no longer matches the proto.
  • Packaging: @connectrpc/connect, @connectrpc/connect-web and @bufbuild/protobuf are optional peers, needed only by this module. check-package allows exactly those three for ./speed.
  • Docs: a README section and docs/speed.md, covering:
    • the failover and the breaker;
    • Workers and Bun usage;
    • caching: key → URL in KV, the Cache API or SQLite, and never a fallback as the answer (60 s at most, marked);
    • prerendered pages asking while they are built;
    • mobile apps going through their site's oRPC, never calling Speed;
    • SPEED_URL and SPEED_TOKEN;
    • kit/ui's Avatar for images that fail inside pages.

Checks

  • Tests: bun run test, 281 pass. 25 of them are new for Speed, all with a fake fetch and no network: building the request, mapping the answer, the Bearer header, the deadline, a fetch that ignores its abort signal, error mapping, redirects, settings checks, the fallback in every form, and the breaker with a fake clock.
  • Static checks: bun check, bun fmt --check, bunx buf lint, bun run check:package.
  • Size: bun run check:size puts the client at 35.84 kB gzipped against a new 40 kB budget, with Connect-ES and protobuf-es bundled in.
  • bun audit: no vulnerabilities.
  • On workerd (the binary, compatibility date 2026-12-01, a scratch Worker against a fake Speed on localhost):
    • real cards came back through getCard and getCardOr;
    • a 302 was refused and the redirect target was never requested;
    • with no settings, the fallback came back at once.

Docs/skills

  • Buf's official protobuf skill (bufbuild/claude-plugins protobuf), including its TypeScript buf.gen template.
  • There is no official Connect-ES or protobuf-es skill. I used the official docs instead:
    • connectrpc.com/docs/web: using clients, choosing a protocol, cancellation and timeouts, errors, interceptors, generating code;
    • the protoc-gen-es README (target, import_extension);
    • the installed Connect-ES 2.3 sources, for defaultTimeoutMs and the redirect: 'error' fetch option.
  • Cloudflare's fetch_redirect_error behaviour, from a write-up on workerd PR #8973.

Security review

An independent review of the diff found nothing high-severity. Fixed here:

  • tokens must be printable ASCII (no spaces, control characters or other header injection);
  • the client keeps its own deadline, so a custom fetch that ignores its signal can't hang a page;
  • an answer with a user:password address or a malformed key is refused;
  • a caller's own abort reports canceled;
  • the body of a refused redirect is cancelled.

The base url must be HTTPS (plain HTTP only on localhost), with no userinfo, query or fragment. The token is never in a message, and the docs say to log code, not cause. Only example.com hosts appear. bun audit is clean.

buf is at 1.76.0, now that kit#32 has removed kit's 3-day minimumReleaseAge; regenerating gave identical code.

🤖 Generated with Claude Code

jgb-solutions and others added 2 commits December 6, 2026 01:02
createSocialCardsClient({ url, token, timeoutMs? }).getCard(input) over gRPC-Web on fetch
(Connect-ES 2.3, protobuf-es 2.17): Bearer token, a 1.5 s deadline, no retries, typed SpeedError
codes, redirects refused (sent as 'manual': Workers refuse Connect's 'error' before 2026-12-22).
socialCardOr(defaultUrl, promise) never throws and falls back to the app's own card.
The proto is copied from Speed's repository at 10648f9 (proto/README.md); bun run generate
rewrites src/speed/gen, and CI checks the two match. Optional peers; 34.5 kB gzipped, budget 40 kB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
withFallback never throws and never waits past its deadline; it returns { url, width, height,
fromSpeed }, with the app's own image or a hex colour (an inline SVG of the format's size).
CircuitBreaker per client: 3 failures in 30 s skip Speed for 30 s, then one trial call.
onFallback(reason): timeout, error, breaker-open, not-configured. No SPEED_URL or SPEED_TOKEN:
the fallback at once, so apps ship before Speed is live. getCardOr replaces socialCardOr.

From the security review: printable-ASCII tokens only, the client's own deadline (a fetch that
ignores its signal can't hang it), answers with a user:password address or a malformed key are
refused, a caller's abort is 'canceled' (not 'timeout'), redirect bodies are cancelled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
kit-showcase 0362784 Commit Preview URL

Branch Preview URL
Dec 06 2026, 08:08 AM

jgb-solutions and others added 3 commits December 6, 2026 02:10
@jgb-solutions
jgb-solutions merged commit ed827dc into main Dec 6, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant