@tldraw/sync-core
Version:
tldraw infinite canvas SDK (multiplayer sync).
8 lines (7 loc) • 6.38 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/lib/ServerSocketAdapter.ts"],
"sourcesContent": ["import { UnknownRecord } from '@tldraw/store'\nimport { TLRoomSocket } from './TLSyncRoom'\nimport { TLSocketServerSentEvent } from './protocol'\n\n/**\n * Minimal server-side WebSocket interface that is compatible with various WebSocket implementations.\n * This interface abstracts over different WebSocket libraries and platforms to provide a consistent\n * API for the ServerSocketAdapter.\n *\n * Supports:\n * - The standard WebSocket interface (Cloudflare, Deno, some Node.js setups)\n * - The 'ws' WebSocket interface (Node.js ws library)\n * - The Bun.serve socket implementation\n *\n * @public\n * @example\n * ```ts\n * // Standard WebSocket\n * const standardWs: WebSocketMinimal = new WebSocket('ws://localhost:8080')\n *\n * // Node.js 'ws' library WebSocket\n * import WebSocket from 'ws'\n * const nodeWs: WebSocketMinimal = new WebSocket('ws://localhost:8080')\n *\n * // Bun WebSocket (in server context)\n * // const bunWs: WebSocketMinimal = server.upgrade(request)\n * ```\n */\nexport interface WebSocketMinimal {\n\t/**\n\t * Optional method to add event listeners for WebSocket events.\n\t * Not all WebSocket implementations provide this method.\n\t *\n\t * @param type - The event type to listen for\n\t * @param listener - The event handler function\n\t */\n\t// eslint-disable-next-line @typescript-eslint/method-signature-style\n\taddEventListener?: (type: 'message' | 'close' | 'error', listener: (event: any) => void) => void\n\n\t/**\n\t * Optional method to remove event listeners for WebSocket events.\n\t * Not all WebSocket implementations provide this method.\n\t *\n\t * @param type - The event type to stop listening for\n\t * @param listener - The event handler function to remove\n\t */\n\t// eslint-disable-next-line @typescript-eslint/method-signature-style\n\tremoveEventListener?: (\n\t\ttype: 'message' | 'close' | 'error',\n\t\tlistener: (event: any) => void\n\t) => void\n\n\t/**\n\t * Sends a string message through the WebSocket connection.\n\t *\n\t * @param data - The string data to send\n\t */\n\t// eslint-disable-next-line @typescript-eslint/method-signature-style\n\tsend: (data: string) => void\n\n\t/**\n\t * Closes the WebSocket connection.\n\t *\n\t * @param code - Optional close code (default: 1000 for normal closure)\n\t * @param reason - Optional human-readable close reason\n\t */\n\t// eslint-disable-next-line @typescript-eslint/method-signature-style\n\tclose: (code?: number, reason?: string) => void\n\n\t/**\n\t * The current state of the WebSocket connection.\n\t * - 0: CONNECTING\n\t * - 1: OPEN\n\t * - 2: CLOSING\n\t * - 3: CLOSED\n\t */\n\treadyState: number\n}\n\n/**\n * Configuration options for creating a ServerSocketAdapter instance.\n *\n * @internal\n */\nexport interface ServerSocketAdapterOptions<R extends UnknownRecord> {\n\t/** The underlying WebSocket connection to wrap */\n\treadonly ws: WebSocketMinimal\n\n\t/**\n\t * Optional callback invoked before each message is sent to the client.\n\t * Useful for logging, metrics, or message transformation.\n\t *\n\t * @param msg - The message object being sent\n\t * @param stringified - The JSON stringified version of the message\n\t */\n\t// eslint-disable-next-line @typescript-eslint/method-signature-style\n\treadonly onBeforeSendMessage?: (msg: TLSocketServerSentEvent<R>, stringified: string) => void\n}\n\n/**\n * Server-side adapter that wraps various WebSocket implementations to provide a consistent\n * TLRoomSocket interface for the TLSyncRoom. This adapter handles the differences between\n * WebSocket libraries and platforms, allowing sync-core to work across different server\n * environments.\n *\n * The adapter implements the TLRoomSocket interface, providing methods for sending messages,\n * checking connection status, and closing connections.\n *\n * @internal\n * @example\n * ```ts\n * import { ServerSocketAdapter } from '@tldraw/sync-core'\n *\n * // Wrap a standard WebSocket\n * const adapter = new ServerSocketAdapter({\n * ws: webSocketConnection,\n * onBeforeSendMessage: (msg, json) => {\n * console.log('Sending:', msg.type)\n * }\n * })\n *\n * // Use with TLSyncRoom\n * room.handleNewSession({\n * sessionId: 'session-123',\n * socket: adapter,\n * isReadonly: false\n * })\n * ```\n */\nexport class ServerSocketAdapter<R extends UnknownRecord> implements TLRoomSocket<R> {\n\t/**\n\t * Creates a new ServerSocketAdapter instance.\n\t *\n\t * opts - Configuration options for the adapter\n\t */\n\tconstructor(public readonly opts: ServerSocketAdapterOptions<R>) {}\n\n\t/**\n\t * Checks if the underlying WebSocket connection is currently open and ready to send messages.\n\t *\n\t * @returns True if the connection is open (readyState === 1), false otherwise\n\t */\n\t// eslint-disable-next-line no-restricted-syntax\n\tget isOpen(): boolean {\n\t\treturn this.opts.ws.readyState === 1 // ready state open\n\t}\n\n\t/**\n\t * Sends a sync protocol message to the connected client. The message is JSON stringified\n\t * before being sent through the WebSocket. If configured, the onBeforeSendMessage callback\n\t * is invoked before sending.\n\t *\n\t * @param msg - The sync protocol message to send\n\t */\n\t// see TLRoomSocket for details on why this accepts a union and not just arrays\n\tsendMessage(msg: TLSocketServerSentEvent<R>) {\n\t\tconst message = JSON.stringify(msg)\n\t\tthis.opts.onBeforeSendMessage?.(msg, message)\n\t\tthis.opts.ws.send(message)\n\t}\n\n\t/**\n\t * Closes the WebSocket connection with an optional close code and reason.\n\t *\n\t * @param code - Optional close code (default: 1000 for normal closure)\n\t * @param reason - Optional human-readable reason for closing\n\t */\n\tclose(code?: number, reason?: string) {\n\t\tthis.opts.ws.close(code, reason)\n\t}\n}\n"],
"mappings": "AAiIO,MAAM,oBAAwE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMpF,YAA4B,MAAqC;AAArC;AAAA,EAAsC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQlE,IAAI,SAAkB;AACrB,WAAO,KAAK,KAAK,GAAG,eAAe;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,YAAY,KAAiC;AAC5C,UAAM,UAAU,KAAK,UAAU,GAAG;AAClC,SAAK,KAAK,sBAAsB,KAAK,OAAO;AAC5C,SAAK,KAAK,GAAG,KAAK,OAAO;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,MAAe,QAAiB;AACrC,SAAK,KAAK,GAAG,MAAM,MAAM,MAAM;AAAA,EAChC;AACD;",
"names": []
}