Skip to content

Cloudflare and edge

View Markdown

Instrument a fetch-based worker and flush within its invocation lifetime.

Initialize the edge entry

npm install @everock/sdk@beta

The edge entry avoids Node-only modules. It supplies explicit error capture, inbound request wrappers, endpoint declarations, bounded delivery, and an optional persistence adapter.

worker.ts
import { init, withRequest, registerEndpoints } from '@everock/sdk/edge';

let initialized = false;

const handler = withRequest(async (request: Request, _env: { EVEROCK_KEY: string; EVEROCK_URL: string }, _ctx: ExecutionContext) => {
  return new Response('OK');
}, { route: '/health' });

export default {
  async fetch(request: Request, env: { EVEROCK_KEY: string; EVEROCK_URL: string }, ctx: ExecutionContext) {
    if (!initialized) {
      init({ publicKey: env.EVEROCK_KEY, endpoint: env.EVEROCK_URL, component: 'worker' });
      registerEndpoints([{ method: 'GET', path: '/health', host: 'api.example.com' }]);
      initialized = true;
    }
    return handler(request, env, ctx);
  },
};

Initialize once per worker isolate with a stable configuration. Reinitializing the singleton on every request resets its pending queue.

withRequest records inbound status and duration. It uses a supplied execution context's waitUntil for delivery after the response. Without an invocation-lifetime mechanism, a runtime can stop background delivery early.

Declare endpoints

Endpoint catalog
// Call after init(), as in the worker example above.
registerEndpoints([{ method: 'GET', path: '/health', host: 'api.example.com' }]);

A declared endpoint can appear without requests. Declaration does not imply that the endpoint is reachable or healthy.

Limits

Edge capture does not patch Node database drivers, record browser DOM replay, or inspect unrelated Cloudflare infrastructure. Durable storage is not assumed: supply an EventStore adapter if your runtime requires it.

See endpoint health and reliability.