import { UnknownRecord } from '@tldraw/store' import ws from 'ws' import { TLRoomSocket } from './TLSyncRoom' import { TLSocketServerSentEvent } from './protocol' type ServerSocketAdapterOptions = { readonly ws: WebSocket | ws.WebSocket readonly logSendMessage: (type: string, size: number) => void } /** @public */ export class ServerSocketAdapter implements TLRoomSocket { constructor(public readonly opts: ServerSocketAdapterOptions) {} // eslint-disable-next-line no-restricted-syntax get isOpen(): boolean { return this.opts.ws.readyState === 1 // ready state open } // see TLRoomSocket for details on why this accepts a union and not just arrays sendMessage(msg: TLSocketServerSentEvent) { const message = JSON.stringify(msg) this.opts.logSendMessage(msg.type, message.length) this.opts.ws.send(message) } close() { this.opts.ws.close() } }