Edge example
This example demonstrates reactive state in a Cloudflare Durable Object, with one object identity per IP. It allows the first 100 requests in a window and logs the first request that exceeds that limit.
This is an in-memory demonstration. An object can handle several requests while resident, but Cloudflare can discard its memory on hibernation, eviction, or restart. This example then starts a fresh counter and window. A production rate limiter that must enforce a window across restarts needs persistent storage; that is a different lifecycle contract. See Durable Object lifecycle.
The Worker entry and Durable Object request method use ordinary JavaScript. Lexical Para signals live inside the constructor, where the request callback closes over them; signal is not used as a class-field modifier.
src/limiter.pjs
Section titled “src/limiter.pjs”import { effect as observe, untrack } from "@lyku/para-signals";
export class RateLimiter { constructor(state) { signal count = 0; signal windowStart = Date.now(); derived overLimit = count > 100;
let wasOverLimit = false; observe(() => { const isOverLimit = overLimit; if (isOverLimit && !wasOverLimit) { untrack(() => { console.log(`rate limit crossed: ${count} requests in window`); // First crossing: rate limit crossed: 101 requests in window }); } wasOverLimit = isOverLimit; });
this.check = () => { if (Date.now() - windowStart > 60_000) { count = 0; windowStart = Date.now(); } count++;
return Response.json({ allowed: !overLimit, remaining: Math.max(0, 100 - count), }); }; }
async fetch(req) { return this.check(); }}What’s reactive and what isn’t
Section titled “What’s reactive and what isn’t”countandwindowStartbelong to each resident object instance. The window resets on the first request more than 60 seconds after its start, rather than on a background timer.overLimitis a lazy derived signal. Requests 1–100 are allowed; request 101 is rejected and logs the crossing. Further requests remain rejected without repeating the log until a new window begins.observealiases the published coreeffectAPI.wasOverLimitdetects the false-to-true transition.untrackreads the count for logging without adding another dependency. This avoids thewhenhelper that is bundled with Parabun but absent from npm@lyku/para-signals@0.0.1-pre.4.- The effect has the same lifetime as the instance and owns no timer or external resource. Storage is deliberately not added here; recreating the instance resets its state.
The Worker entry that calls it
Section titled “The Worker entry that calls it”The fetch handler that proxies requests through the Durable Object is plain Workers code; nothing reactive happens here, so it’s plain JavaScript:
// src/index.pjs — Wrangler needs this named class export.export { RateLimiter } from "./limiter.pjs";
export default { async fetch(req, env) { const ip = req.headers.get("cf-connecting-ip") ?? "unknown"; const stub = env.RATE_LIMIT.get(env.RATE_LIMIT.idFromName(ip)); const { allowed, remaining } = await (await stub.fetch("https://internal/check")).json();
if (!allowed) { return new Response("rate limited", { status: 429, headers: { "Cache-Control": "no-store", "Retry-After": "60" }, }); } return Response.json({ ok: true, remaining }, { headers: { "X-RateLimit-Remaining": String(remaining), "Cache-Control": "no-store" }, }); },};wrangler.jsonc
Section titled “wrangler.jsonc”{ "name": "my-worker", "main": "dist/index.js", "compatibility_date": "2026-04-30", "durable_objects": { "bindings": [{ "name": "RATE_LIMIT", "class_name": "RateLimiter" }] }, "migrations": [{ "tag": "v1", "new_sqlite_classes": ["RateLimiter"] }]}This configuration creates a new SQLite-backed namespace. The example makes no storage writes, so its counters still reset when the instance is recreated. For an existing deployment, preserve its migration history.
package.json
Section titled “package.json”{ "name": "my-worker", "type": "module", "scripts": { "build": "parabun build src/index.pjs --target browser --outfile dist/index.js", "dev": "parabun run build && wrangler dev", "deploy": "parabun run build && wrangler deploy" }, "dependencies": { "@lyku/para-signals": "0.0.1-pre.4" }, "devDependencies": { "wrangler": "^4" }}--target browser is correct for Workers — the V8 isolate runtime has web-platform globals but no Node APIs.
Build and run locally
Section titled “Build and run locally”parabun installparabun run devUse curl http://localhost:8787 to exercise the local Worker. On a fresh object, the first response has remaining: 99; request 100 has remaining: 0; request 101 returns HTTP 429. For production publication, parabun run deploy is a separate step requiring your Cloudflare account.
The Parabun build bundles the published signals package into standard JavaScript. The entry exports both the Worker handler and RateLimiter, matching the Wrangler binding. The HTTP responses use no-store because the allowance depends on the caller and current window.
Verification scope
Section titled “Verification scope”The .pjs source was compiled with Parabun 1.4.0-canary.1+117854cd6 (release parabun-117854cd62) and the published signals runtime above. Local tests with Miniflare 4.20260609.0 / workerd 1.20260609.1, using the configured compatibility date, verified the named class binding, HTTP threshold behavior, independent IP state, one crossing log, and reset after runtime restart. Controlled-clock tests also cover the exact 60-second window boundary. These are local checks; production deployment and Cloudflare-managed eviction timing have not been tested.