import type { HelperTypes } from '@livestore/common-cf'; import { Schema } from '@livestore/utils/effect'; import type { CfTypes, SearchParams } from '../common/mod.ts'; import { type Env, type ForwardedHeaders } from './shared.ts'; export type CFWorker = { fetch: (request: CfTypes.Request, env: TEnv, ctx: CfTypes.ExecutionContext) => Promise; }; /** Context passed to validatePayload callback */ export type ValidatePayloadContext = { storeId: string; /** Request headers (raw, not filtered by forwardHeaders) */ headers: ForwardedHeaders; }; /** * Options accepted by {@link makeWorker}. The Durable Object binding has to be * supplied explicitly so we never fall back to deprecated defaults when Cloudflare config changes. */ export type MakeWorkerOptions = { /** * Binding name of the sync Durable Object declared in wrangler config. */ syncBackendBinding: HelperTypes.ExtractDurableObjectKeys; /** * Optionally pass a schema to decode the client-provided payload into a typed object * before calling {@link validatePayload}. If omitted, the raw JSON value is forwarded. */ syncPayloadSchema?: Schema.Schema; /** * Validates the (optionally decoded) payload during WebSocket connection establishment. * If {@link syncPayloadSchema} is provided, `payload` will be of the schema's inferred type. * * The context includes request headers for cookie-based or header-based authentication. * * @example Cookie-based authentication * ```ts * validatePayload: async (payload, { storeId, headers }) => { * const cookie = headers.get('cookie') * const session = await validateSessionFromCookie(cookie) * if (!session) throw new Error('Unauthorized') * } * ``` * * Note: This runs only at connection time, not for individual push events. * For push event validation, use the `onPush` callback in the Durable Object. */ validatePayload?: (payload: TSyncPayload, context: ValidatePayloadContext) => void | Promise; /** @default false */ enableCORS?: boolean; }; /** * Produces a Cloudflare Worker `fetch` handler that delegates sync traffic to the * Durable Object identified by `syncBackendBinding`. * * For more complex setups prefer implementing a custom `fetch` and call {@link handleSyncRequest} * from the branch that handles LiveStore sync requests. */ export declare const makeWorker: (options: MakeWorkerOptions) => CFWorker; /** * Handles LiveStore sync requests (e.g. with search params `?storeId=...&transport=...`). * * @example Token-based authentication * ```ts * const validatePayload = (payload: Schema.JsonValue | undefined, context: { storeId: string }) => { * if (payload?.authToken !== 'insecure-token-change-me') { * throw new Error('Invalid auth token') * } * } * ``` * * @example Cookie-based authentication * ```ts * const validatePayload = async (payload: Schema.JsonValue | undefined, { storeId, headers }) => { * const cookie = headers.get('cookie') * const session = await validateSessionFromCookie(cookie) * if (!session) throw new Error('Unauthorized') * } * ``` * * @throws {UnknownError} If the payload is invalid */ export declare const handleSyncRequest: ({ request, searchParams: { storeId, payload, transport }, env: explicitlyProvidedEnv, syncBackendBinding, headers, validatePayload, syncPayloadSchema, }: { request: CfTypes.Request; searchParams: SearchParams; env?: TEnv | undefined; /** Only there for type-level reasons */ ctx: CfTypes.ExecutionContext; /** Binding name of the sync backend Durable Object */ syncBackendBinding: MakeWorkerOptions["syncBackendBinding"]; headers?: CfTypes.HeadersInit | undefined; validatePayload?: MakeWorkerOptions["validatePayload"]; syncPayloadSchema?: MakeWorkerOptions["syncPayloadSchema"]; }) => Promise; //# sourceMappingURL=worker.d.ts.map