UNPKG

y-durableobjects

Version:

[![Yjs on Cloudflare Workers with Durable Objects Demo Movie](https://i.gyazo.com/e94637740dbb11fc5107b0cd0850326d.gif)](https://gyazo.com/e94637740dbb11fc5107b0cd0850326d)

147 lines (137 loc) 4.76 kB
/// <reference types="@cloudflare/workers-types" /> import * as hono_hono_base from 'hono/hono-base'; import * as hono_utils_http_status from 'hono/utils/http-status'; import * as hono_types from 'hono/types'; import { Env } from 'hono'; import { DurableObject } from 'cloudflare:workers'; import { Awareness } from 'y-protocols/awareness'; import { Doc } from 'yjs'; type Listener<T> = (message: T) => void; type Unsubscribe = () => void; interface Notification<T> extends RemoteDoc { notify(cb: Listener<T>): Unsubscribe; } declare class WSSharedDoc extends Doc implements Notification<Uint8Array> { private listeners; readonly awareness: Awareness; constructor(gc?: boolean); update(message: Uint8Array): void; notify(listener: Listener<Uint8Array>): () => void; private syncMessageHandler; private awarenessChangeHandler; private _notify; } interface RemoteDoc extends Doc { readonly awareness: Awareness; } type Service = { createRoom(roomId: string): Promise<WebSocket> | WebSocket; }; declare const createApp: (service: Service) => hono_hono_base.HonoBase<hono_types.BlankEnv, { "/rooms/:roomId": { $get: { input: { param: { roomId: string; }; }; output: {}; outputFormat: string; status: hono_utils_http_status.StatusCode; }; }; }, "/">; interface ListOptions { start?: string; startAfter?: string; end?: string; prefix?: string; reverse?: boolean; limit?: number; } interface TransactionStorage { get<T = unknown>(key: string): Promise<T | undefined>; list<T = unknown>(options?: ListOptions): Promise<Map<string, T>>; put<T>(key: string, value: T): Promise<unknown>; delete(key: string | string[]): Promise<unknown>; transaction<T>(closure: (txn: Omit<TransactionStorage, "transaction">) => Promise<T>): Promise<T>; } interface YTransactionStorage { getYDoc(): Promise<Doc>; storeUpdate(update: Uint8Array): Promise<void>; commit(): Promise<void>; } type Options = { /** * @description default is 10KB * @default 10 * 1024 * 1 */ maxBytes?: number; /** * @description default is 500 snapshot * @default 500 */ maxUpdates?: number; }; declare class YTransactionStorageImpl implements YTransactionStorage { private readonly storage; private readonly MAX_BYTES; private readonly MAX_UPDATES; constructor(storage: TransactionStorage, options?: Options); getYDoc(): Promise<Doc>; storeUpdate(update: Uint8Array): Promise<void>; private _commit; commit(): Promise<void>; } type YDurableObjectsAppType = ReturnType<typeof createApp>; declare class YDurableObjects<T extends Env> extends DurableObject<T["Bindings"]> { state: DurableObjectState; env: T["Bindings"]; protected app: hono_hono_base.HonoBase<hono_types.BlankEnv, { "/rooms/:roomId": { $get: { input: { param: { roomId: string; }; }; output: {}; outputFormat: string; status: hono_utils_http_status.StatusCode; }; }; }, "/">; protected doc: WSSharedDoc; protected storage: YTransactionStorageImpl; protected sessions: Map<WebSocket, () => void>; private awarenessClients; constructor(state: DurableObjectState, env: T["Bindings"]); protected onStart(): Promise<void>; protected createRoom(roomId: string): WebSocket; fetch(request: Request): Response | Promise<Response>; updateYDoc(update: Uint8Array): Promise<void>; getYDoc(): Promise<Uint8Array>; webSocketMessage(ws: WebSocket, message: string | ArrayBuffer): Promise<void>; webSocketError(ws: WebSocket): Promise<void>; webSocketClose(ws: WebSocket): Promise<void>; protected registerWebSocket(ws: WebSocket): void; protected unregisterWebSocket(ws: WebSocket): Promise<void>; protected cleanup(): Promise<void>; } type Selector<E extends Env> = (c: E["Bindings"]) => DurableObjectNamespace; declare const yRoute: <E extends Env>(selector: Selector<E>) => hono_hono_base.HonoBase<hono_types.BlankEnv, { "/:id": { $get: { input: { param: { id: string; }; }; output: {}; outputFormat: "ws"; status: hono_utils_http_status.StatusCode; }; }; }, "/">; type YRoute = ReturnType<typeof yRoute>; export { type RemoteDoc, WSSharedDoc, YDurableObjects, type YDurableObjectsAppType, type YRoute, type YTransactionStorage, yRoute };