|
| 1 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 2 | +// @ts-expect-error |
| 3 | +import * as decoding from 'lib0/decoding'; |
| 4 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 5 | +// @ts-expect-error |
| 6 | +import * as encoding from 'lib0/encoding'; |
| 7 | +import { WebSocket } from 'ws'; |
| 8 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 9 | +// @ts-expect-error |
| 10 | +import * as awarenessProtocol from 'y-protocols/awareness'; |
| 11 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 12 | +// @ts-expect-error |
| 13 | +import * as syncProtocol from 'y-protocols/sync'; |
| 14 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 15 | +// @ts-expect-error |
| 16 | +import * as Y from 'yjs'; |
| 17 | + |
| 18 | +import { MESSAGE_AWARENESS_CODE, MESSAGE_SYNC_CODE } from './constants'; |
| 19 | + |
| 20 | +const wsReadyStateConnecting = 0; |
| 21 | +const wsReadyStateOpen = 1; |
| 22 | + |
| 23 | +/** |
| 24 | + * General wrapper of a yjs document |
| 25 | + * Broadcast updates to attached connections |
| 26 | + */ |
| 27 | +export class WSDoc extends Y.Doc { |
| 28 | + name: string; |
| 29 | + conns: Map<WebSocket, Set<number>>; |
| 30 | + enableAwareness: boolean; |
| 31 | + awareness: awarenessProtocol.Awareness; |
| 32 | + |
| 33 | + constructor(name: string, enableAwareness: boolean) { |
| 34 | + super(); |
| 35 | + this.name = name; |
| 36 | + this.conns = new Map(); |
| 37 | + this.enableAwareness = enableAwareness; |
| 38 | + this.awareness = new awarenessProtocol.Awareness(this); |
| 39 | + this.awareness.setLocalState(null); |
| 40 | + } |
| 41 | + |
| 42 | + protected broadcastUpdate(update: Uint8Array) { |
| 43 | + const encoder = encoding.createEncoder(); |
| 44 | + encoding.writeVarUint(encoder, MESSAGE_SYNC_CODE); |
| 45 | + syncProtocol.writeUpdate(encoder, update); |
| 46 | + const message = encoding.toUint8Array(encoder); |
| 47 | + |
| 48 | + this.conns.forEach((_, conn) => this.send(conn, message)); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Add a connection to the pool |
| 53 | + * @param conn |
| 54 | + */ |
| 55 | + addConnection(conn: WebSocket) { |
| 56 | + this.conns.set(conn, new Set()); |
| 57 | + // listen and reply to events |
| 58 | + conn.on('message', (message: ArrayBuffer) => { |
| 59 | + this.messageListener(conn, new Uint8Array(message)); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + /* |
| 64 | + * Get message from other connections at WS connection level |
| 65 | + */ |
| 66 | + messageListener(conn: WebSocket, message: Uint8Array) { |
| 67 | + try { |
| 68 | + const encoder = encoding.createEncoder(); |
| 69 | + const decoder = decoding.createDecoder(message); |
| 70 | + const messageType = decoding.readVarUint(decoder); |
| 71 | + switch (messageType) { |
| 72 | + case MESSAGE_SYNC_CODE: |
| 73 | + encoding.writeVarUint(encoder, MESSAGE_SYNC_CODE); |
| 74 | + syncProtocol.readSyncMessage(decoder, encoder, this, conn); |
| 75 | + |
| 76 | + // If the `encoder` only contains the type of reply message and no |
| 77 | + // message, there is no need to send the message. When `encoder` only |
| 78 | + // contains the type of reply, its length is 1. |
| 79 | + if (encoding.length(encoder) > 1) { |
| 80 | + this.send(conn, encoding.toUint8Array(encoder)); |
| 81 | + } |
| 82 | + break; |
| 83 | + case MESSAGE_AWARENESS_CODE: { |
| 84 | + if (this.enableAwareness) { |
| 85 | + awarenessProtocol.applyAwarenessUpdate( |
| 86 | + this.awareness, |
| 87 | + decoding.readVarUint8Array(decoder), |
| 88 | + conn, |
| 89 | + ); |
| 90 | + } |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + } catch (err) { |
| 95 | + console.error(err); |
| 96 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 97 | + // @ts-expect-error |
| 98 | + doc.emit('error', [err]); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Send message to given connection |
| 104 | + * @param conn connection to receive message |
| 105 | + * @param m message |
| 106 | + */ |
| 107 | + send(conn: WebSocket, m: Uint8Array) { |
| 108 | + if (conn.readyState !== wsReadyStateConnecting && conn.readyState !== wsReadyStateOpen) { |
| 109 | + this.closeConn(conn); |
| 110 | + } |
| 111 | + try { |
| 112 | + conn.send(m, {}, (err) => { |
| 113 | + // eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 114 | + err != null && this.closeConn(conn); |
| 115 | + }); |
| 116 | + } catch (e) { |
| 117 | + console.error(e); |
| 118 | + this.closeConn(conn); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Close given connection |
| 124 | + * @param conn connection to close |
| 125 | + */ |
| 126 | + closeConn(conn: WebSocket) { |
| 127 | + console.debug('Page: close connection'); |
| 128 | + if (this.conns.has(conn)) { |
| 129 | + const controlledIds: Set<number> = this.conns.get(conn)!; |
| 130 | + this.conns.delete(conn); |
| 131 | + awarenessProtocol.removeAwarenessStates(this.awareness, Array.from(controlledIds), null); |
| 132 | + this.destroy(); |
| 133 | + } |
| 134 | + conn.close(); |
| 135 | + } |
| 136 | +} |
0 commit comments