JAlcocerTech E-books

Files, Payments and Email at the Edge

The Worker serves pages and APIs. Three parts of Ślubne Chwile cross into other systems: guest uploads go to R2, payments come from Stripe, and support notifications go out by email. Each one broke in a way no local test predicted.


The 4.9 MB Problem: Uploads That Don’t Fit

The product promise is up to 100 MiB per file. The Node app received each upload whole, spooled it to a temp file, checked its size and signature, and wrote it to private R2.

Moving that endpoint to a Worker runs into the domain’s incoming request limit. At the time of the decision Cloudflare allowed 100 MB (decimal) on Free and Pro, and 200 MB on Business. 100 MiB is about 104.9 MB. And Workers Paid doesn’t raise the domain limit.

The options, as the ADR weighed them:

OptionWhy not (or why)
Keep uploads through the Worker, lower the limit below 100 MBSimple, but changes the offer and blocks larger videos later
Upgrade the zone to Business200/monthbilledannuallyor200/month billed annually or 250 monthly, to bridge a 4.9 MB gap, and still capped at 200 MB
Proxy multipart chunks through the WorkerWorks, but every byte still crosses the Worker. Kept as fallback
EnterpriseContract pricing; out of proportion for a PoC
Browser uploads directly to private R2Chosen

The decision is explicit that it changes the transport, not the offer. The 100 MiB ceiling stays until a separate product decision.

How the direct upload works

sequenceDiagram
  participant B as Guest browser
  participant W as Worker
  participant F as Firestore
  participant R as Private R2
  B->>W: begin upload
  W->>F: transaction: check window + quota, reserve bytes
  W->>R: start multipart (server-chosen key)
  W-->>B: short-lived URLs, one per part, signed for length
  B->>R: PUT parts directly
  B->>W: complete
  W->>R: list parts, complete, check size + type + file signature
  W->>F: mark ready, move reserved → used quota

The rule behind it: the server never trusts the browser’s “done”. Media only appears in the gallery after the Worker has checked the real object. The bucket stays private, with no r2.dev and no public domain. CORS lets the browser make the transfer; the signed URL and server state are what authorise it.

What only showed up live

  • Quoted ETags. R2 returned HTTP ETags with quotes, and passing them unchanged to the multipart complete call broke it. The Worker strips them.
  • CORS for real origins, including exposing the ETag header, or the browser can’t read what it needs to complete.
  • Abandoned uploads. Abort, retry and stale-upload cleanup have to reconcile R2 and Firestore, or a guest who closes the tab holds quota forever. And never release a reservation while an upload might still complete.

Tests covered signed-length rejection, bad parts, idempotent completion, and the awkward case of R2 completing before Firestore does. Then a real Chrome guest upload went to R2, was read back through a bounded Worker range request, and was cleaned up.

Exports: the same idea in reverse

Owners download originals as ZIP files. Building big archives in a Worker fights its CPU limits, so the browser assembles the ZIP. The Worker returns an authenticated, bounded catalogue (at most 20 files and 128 MiB per part); the browser fetches those private objects through authorised Worker reads and zips them locally. A 100 MiB archive test and a live non-empty gallery both passed unzip integrity and byte checks.

Moving ZIP-building to the browser did not make the bucket a public download service. The Worker still decides what belongs to whom.


Payments: Test Mode All the Way, On Purpose

Stripe stayed in test mode with checkout disabled the whole time. That was a policy decision, written in the MVP ADR: live keys and events are rejected in test configuration, and switching to live is a separate release.

What was verified anyway:

  • A real test-card Checkout session reached the preview Worker’s webhook.
  • The signed event granted the wedding entitlement.
  • A replay was ignored: the Worker stores an event record, so a duplicate delivery can’t grant the entitlement twice.
  • At cutover, pending events could arrive signed with the older endpoint’s secret. Both test signing secrets were checked on the new route, and an invalid signature was rejected.

What that does not mean: live payments are ready. They need live credentials, a live webhook endpoint, a rollout and another payment check. The production config keeps CHECKOUT_ENABLED=false visible to anyone reading it.

Stripe’s test payment had to produce exactly one entitlement.


Email: The Bug Only the Edge Had

Support works like this: a signed-in user submits a request, the Worker saves the ticket first, then sends a reference-only notification (no message text, no account address).

On the Worker, the ticket saved but the notification was marked failed. The same message sent from Node worked, which put the bug in the Worker path. A diagnostic log showed a TypeError: Workers reject fetch with redirect: 'error'. Switching to redirect: 'manual' keeps the same behaviour (a redirect still counts as a failed send) using an option the runtime supports. A new live request then saved, the provider accepted the notification, and it arrived in the inbox.

The design choice that made this harmless: ticket persistence is authoritative, notification is best-effort. A failed email leaves a ticket marked failed, not a lost customer. And because there’s no retry worker yet, the support inbox gets checked independently of notifications.

Two smaller email lessons from the ADRs:

  • Provider acceptance is not inbox delivery. Each was its own check.
  • Turn off engagement tracking on transactional mail. It’s one API call, and service email doesn’t need open pixels.

Takeaways

  • When a platform limit hits the product, change the transport before you change the offer or the plan.
  • Never trust a client’s “done”: verify the stored object.
  • Push heavy work (uploads, ZIPs) to the edges: browser and bucket, with the Worker as the authority in the middle.
  • Idempotent webhooks: one event, one entitlement, replays ignored.
  • Persist first, notify second. Then edge-only bugs cost a log line, not a customer.

Next: backups, restore drills, and serverless vs homelab