handleSyncRequest
handleSyncRequest<
TEnv,TDurableObjectRpc,CFHostMetada,TSyncPayload>(__namedParameters):Promise<Response>
Defined in: packages/@livestore/sync-cf/src/cf-worker/worker.ts:156
Handles LiveStore sync requests (e.g. with search params ?storeId=...&transport=...).
Type Parameters
Section titled “Type Parameters”TDurableObjectRpc
Section titled “TDurableObjectRpc”TDurableObjectRpc extends undefined | DurableObjectBranded = undefined
CFHostMetada
Section titled “CFHostMetada”CFHostMetada = unknown
TSyncPayload
Section titled “TSyncPayload”TSyncPayload = JsonValue
Parameters
Section titled “Parameters”__namedParameters
Section titled “__namedParameters”Only there for type-level reasons
TEnv
headers?
Section titled “headers?”request
Section titled “request”Request<CFHostMetada>
searchParams
Section titled “searchParams”{ payload: undefined | JsonValue; storeId: string; transport: "http" | "ws"; }
searchParams.payload
Section titled “searchParams.payload”undefined | JsonValue = ...
searchParams.storeId
Section titled “searchParams.storeId”string = Schema.String
searchParams.transport
Section titled “searchParams.transport”"http" | "ws" = ...
syncBackendBinding
Section titled “syncBackendBinding”ExtractDurableObjectKeys<TEnv>
Binding name of the sync backend Durable Object
syncPayloadSchema?
Section titled “syncPayloadSchema?”Schema<TSyncPayload, TSyncPayload, never>
validatePayload?
Section titled “validatePayload?”(payload, context) => void | Promise<void>
Returns
Section titled “Returns”Promise<Response>
Example
Section titled “Example”const validatePayload = (payload: Schema.JsonValue | undefined, context: { storeId: string }) => { console.log(`Validating connection for store: ${context.storeId}`) if (payload?.authToken !== 'insecure-token-change-me') { throw new Error('Invalid auth token') }}
export default { fetch: async (request, env, ctx) => { const searchParams = matchSyncRequest(request)
// Is LiveStore sync request if (searchParams !== undefined) { return handleSyncRequest({ request, searchParams, env, ctx, syncBackendBinding: 'SYNC_BACKEND_DO', headers: {}, validatePayload, }) }
return new Response('Invalid path', { status: 400 }) }}Throws
Section titled “Throws”If the payload is invalid