UNPKG

rwsdk

Version:

Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime

63 lines (62 loc) 3.6 kB
import { DurableObject } from "cloudflare:workers"; import type { RequestInfo } from "../../runtime/requestInfo/types"; import { type SyncedStateIdentity } from "./identity.mjs"; import { type SyncedStateValue } from "./protocol.mjs"; export type SyncedStateServerAttachment = { clientId: string; identity: SyncedStateIdentity; subscriptions: Array<{ userKey: string; storageKey: string; }>; }; type OnSetHandler = (key: string, value: SyncedStateValue, identity: SyncedStateIdentity, stub: DurableObjectStub<SyncedStateServer>) => void; type OnGetHandler = (key: string, value: SyncedStateValue | undefined, identity: SyncedStateIdentity, stub: DurableObjectStub<SyncedStateServer>) => void; type OnKeyHandler = (key: string, identity: SyncedStateIdentity, stub: DurableObjectStub<SyncedStateServer>) => Promise<string>; type OnRoomHandler = (roomId: string | undefined, requestInfo: RequestInfo | null) => Promise<string>; type OnSubscribeHandler = (key: string, identity: SyncedStateIdentity, stub: DurableObjectStub<SyncedStateServer>) => void; type OnUnsubscribeHandler = (key: string, identity: SyncedStateIdentity, stub: DurableObjectStub<SyncedStateServer>) => void; type IdentityExtractor = (requestInfo: RequestInfo) => SyncedStateIdentity | Promise<SyncedStateIdentity>; /** * Durable Object that keeps shared state for multiple clients and notifies * subscribers, using the Cloudflare Hibernation WebSocket API so idle * connections do not keep the object active. * * The implementation copies the lifecycle pattern from the older * RealtimeDurableObject but replaces its RSC/action protocol with a small * JSON state-sync protocol. * * Keys arrive as user-facing values. When a key handler is registered, the DO * transforms them internally using the identity captured at upgrade time by * the worker. This lets the worker hand off the WebSocket and exit instead of * staying alive as a proxy. */ export declare class SyncedStateServer extends DurableObject { #private; static registerKeyHandler(handler: OnKeyHandler | null): void; static getKeyHandler(): OnKeyHandler | null; static registerRoomHandler(handler: OnRoomHandler | null): void; static getRoomHandler(): OnRoomHandler | null; static registerIdentityExtractor(extractor: IdentityExtractor | null): void; static getIdentityExtractor(): IdentityExtractor | null; static registerNamespace(namespace: DurableObjectNamespace<SyncedStateServer>, durableObjectName?: string): void; static getNamespace(): DurableObjectNamespace<SyncedStateServer> | null; static getDurableObjectName(): string; static registerSetStateHandler(handler: OnSetHandler | null): void; static registerGetStateHandler(handler: OnGetHandler | null): void; static registerSubscribeHandler(handler: OnSubscribeHandler | null): void; static registerUnsubscribeHandler(handler: OnUnsubscribeHandler | null): void; static getSubscribeHandler(): OnSubscribeHandler | null; static getUnsubscribeHandler(): OnUnsubscribeHandler | null; state: DurableObjectState; env: Env; storage: DurableObjectStorage; constructor(state: DurableObjectState, env: Env); setStub(stub: DurableObjectStub<SyncedStateServer>): void; fetch(request: Request): Promise<Response>; webSocketMessage(ws: WebSocket, data: string | ArrayBuffer): Promise<void>; webSocketClose(_ws: WebSocket): Promise<void>; getState(key: string): Promise<SyncedStateValue | undefined>; setState(value: SyncedStateValue, key: string): Promise<void>; } export {};