A Cloudflare Worker that gates file downloads by membership tier. No origin server involved — access is checked and enforced at the edge in sub-milliseconds, with three distinct tiers and a clean fallback for non-members.
The membership site had three tiers — Free, Pro, and Elite — each with access to a different set of downloadable resources. Access control was handled by the WordPress backend: every download request went to the origin, authenticated against the database, and either served the file or returned a 403.
This added latency on every download, put unnecessary load on the origin server, and made the system fragile. Spikes in download activity directly affected the performance of the rest of the site. The access logic needed to move closer to the user.
A Cloudflare Worker intercepts every file request before it reaches the origin. The worker verifies the JWT from the request header, reads the membership tier from Workers KV, checks the requested path against the tier's allowed paths, and either streams the file from R2 or returns the appropriate error — all at the edge.
Files are stored in an R2 bucket under tier-prefixed paths: /files/free/, /files/pro/, and /files/elite/. The worker maps each membership tier to its allowed path prefixes. Free members can only access /free/, Pro members access /free/ and /pro/, and Elite members access everything.
The entire worker is written in TypeScript with strict types on request context, KV bindings, R2 object responses, and error codes. This makes the tier logic explicit and auditable. Adding a new tier or changing path rules is a one-line config change, not a logic rewrite.
Every download request is now handled entirely by the Cloudflare Worker. The origin server receives no download traffic at all. Spikes in download activity no longer affect site performance, and there is no database query on each file request.
Allowed requests receive a direct file stream. Requests from members whose tier is too low receive a 402 with an upgrade prompt. Requests with an invalid or expired token receive a 401. No silent failures, no incorrect file exposure.
Because the worker runs on Cloudflare's global network, file access is enforced and served from the edge location closest to the user. Response times are consistent regardless of where members are connecting from.
We build edge computing solutions, Cloudflare Workers, and backend automation for teams who need fast, reliable, infrastructure-light systems.