-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathy-socket-io.js
More file actions
459 lines (429 loc) · 13.4 KB
/
y-socket-io.js
File metadata and controls
459 lines (429 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import * as Y from 'yjs'
import * as AwarenessProtocol from 'y-protocols/awareness'
import * as promise from 'lib0/promise'
import * as encoding from 'lib0/encoding'
import * as decoding from 'lib0/decoding'
import { assert } from 'lib0/testing'
import { User } from './user.js'
import * as api from '../api.js'
import * as protocol from '../protocol.js'
import { createSubscriber } from '../subscriber.js'
/**
* @typedef {import('socket.io').Namespace} Namespace
* @typedef {import('socket.io').Socket} Socket
* @typedef {import('socket.io').Server} Server
*
* @typedef {import('../subscriber.js').Subscriber} Subscriber
*/
/**
* @typedef UserLike
* @prop {string} userid
*
* @typedef {'sync-step-1' | 'sync-step-2' | 'sync-update' | 'awareness-update'} EventType
*
* @typedef {{
* ydoc: Y.Doc;
* awareness: AwarenessProtocol.Awareness;
* redisLastId: string;
* storeReferences: any[] | null;
* }} RedisDoc
*/
/**
* @typedef YSocketIOConfiguration
* YSocketIO instance configuration. Here you can configure:
* - authenticate: The callback to authenticate the client connection
*
* @prop {(socket: Socket)=> Promise<UserLike | null> | UserLike | null} authenticate
* Callback to authenticate the client connection.
*/
/**
* YSocketIO class. This handles document synchronization.
*/
export class YSocketIO {
/**
* @type {Server}
* @private
* @readonly
*/
io
/**
* @type {YSocketIOConfiguration}
* @private
* @readonly
*/
configuration
/**
* @type {Namespace | null}
* @public
*/
nsp = null
/**
* @type {api.Api | null}
* @private
*/
client = null
/**
* @type {Subscriber | null}
* @private
*/
subscriber = null
/**
* @type {Map<string, string>}
* @private
* @readonly
*/
namespaceStreamMap = new Map()
/**
* @type {Map<string, string>}
* @private
* @readonly
*/
streamNamespaceMap = new Map()
/**
* @type {Map<string, Namespace>}
* @private
* @readonly
*/
namespaceMap = new Map()
/**
* @type {Promise<void>[]}
* @private
*/
syncQueue = []
/**
* YSocketIO constructor.
* @constructor
* @param {Server} io Server instance from Socket IO
* @param {YSocketIOConfiguration} configuration The YSocketIO configuration
*/
constructor (io, configuration) {
this.io = io
this.configuration = configuration
}
/**
* YSocketIO initialization.
*
* This method set ups a dynamic namespace manager for namespaces that match with the regular expression `/^\/yjs\|.*$/`
* and adds the connection authentication middleware to the dynamics namespaces.
*
* It also starts socket connection listeners.
* @param {import('../storage.js').AbstractStorage} store
* @param {{ redisPrefix?: string, redisUrl?: string }=} opts
* @public
*/
async initialize (store, { redisUrl, redisPrefix = 'y' } = {}) {
const [client, subscriber] = await promise.all([
api.createApiClient(store, { redisUrl, redisPrefix }),
createSubscriber(store, { redisUrl, redisPrefix })
])
this.client = client
this.subscriber = subscriber
this.nsp = this.io.of(/^\/yjs\|.*$/)
this.nsp.use(async (socket, next) => {
if (this.configuration.authenticate == null) return next()
const user = await this.configuration.authenticate(socket)
if (user) {
socket.user = new User(this.getNamespaceString(socket.nsp), user.userid)
return next()
} else return next(new Error('Unauthorized'))
})
this.nsp.on('connection', async (socket) => {
assert(this.client)
assert(this.subscriber)
if (!socket.user) throw new Error('user does not exist in socket')
const namespace = this.getNamespaceString(socket.nsp)
const stream = api.computeRedisRoomStreamName(
namespace,
'index',
redisPrefix
)
if (!this.namespaceMap.has(namespace)) {
this.namespaceMap.set(namespace, socket.nsp)
}
socket.user.subs.add(stream)
socket.user.initialRedisSubId = subscriber.subscribe(
stream,
this.redisMessageSubscriber
).redisId
this.namespaceStreamMap.set(namespace, stream)
this.streamNamespaceMap.set(stream, namespace)
this.initSyncListeners(socket)
this.initAwarenessListeners(socket)
this.initSocketListeners(socket)
/**
* @type {Promise<void>}
*/
const task = new Promise((resolve) => {
assert(this.client)
this.client.getDoc(namespace, 'index').then((doc) => {
assert(socket.user)
assert(this.subscriber)
socket.emit('ready-for-sync')
if (
api.isSmallerRedisId(doc.redisLastId, socket.user.initialRedisSubId)
) {
// our subscription is newer than the content that we received from the api
// need to renew subscription id and make sure that we catch the latest content.
this.subscriber.ensureSubId(stream, doc.redisLastId)
}
this.startSynchronization(socket, doc)
resolve()
})
})
this.queueUpSyncTask(task)
})
return { client, subscriber }
}
/**
* This function initializes the socket event listeners to synchronize document changes.
*
* The synchronization protocol is as follows:
* - A client emits the sync step one event (`sync-step-1`) which sends the document as a state vector
* and the sync step two callback as an acknowledgment according to the socket io acknowledgments.
* - When the server receives the `sync-step-1` event, it executes the `syncStep2` acknowledgment callback and sends
* the difference between the received state vector and the local document (this difference is called an update).
* - The second step of the sync is to apply the update sent in the `syncStep2` callback parameters from the server
* to the document on the client side.
* - There is another event (`sync-update`) that is emitted from the client, which sends an update for the document,
* and when the server receives this event, it applies the received update to the local document.
* - When an update is applied to a document, it will fire the document's "update" event, which
* sends the update to clients connected to the document's namespace.
* @private
* @param {Socket} socket The socket connection
* @readonly
*/
initSyncListeners = (socket) => {
socket.on(
'sync-step-1',
async (
/** @type {Uint8Array} */
stateVector,
/** @type {(update: Uint8Array) => void} */
syncStep2
) => {
assert(this.client)
const doc = await this.client.getDoc(
this.getNamespaceString(socket.nsp),
'index'
)
syncStep2(Y.encodeStateAsUpdate(doc.ydoc, stateVector))
}
)
socket.on('sync-update', (/** @type {ArrayBuffer} */ update) => {
assert(this.client)
const message = Buffer.from(update.slice(0, update.byteLength))
this.client.addMessage(
this.getNamespaceString(socket.nsp),
'index',
Buffer.from(this.toRedis('sync-update', message))
)
})
}
/**
* This function initializes socket event listeners to synchronize awareness changes.
*
* The awareness protocol is as follows:
* - A client emits the `awareness-update` event by sending the awareness update.
* - The server receives that event and applies the received update to the local awareness.
* - When an update is applied to awareness, the awareness "update" event will fire, which
* sends the update to clients connected to the document namespace.
* @private
* @param {Socket} socket The socket connection
* @readonly
*/
initAwarenessListeners = (socket) => {
socket.on('awareness-update', (/** @type {ArrayBuffer} */ update) => {
assert(this.client)
const message = Buffer.from(update.slice(0, update.byteLength))
this.client.addMessage(
this.getNamespaceString(socket.nsp),
'index',
Buffer.from(this.toRedis('awareness-update', new Uint8Array(message)))
)
})
}
/**
* This function initializes socket event listeners for general purposes.
* @private
* @param {Socket} socket The socket connection
* @readonly
*/
initSocketListeners = (socket) => {
socket.on('disconnect', async () => {
assert(this.subscriber)
if (!socket.user) return
for (const ns of socket.user.subs) {
const stream = this.namespaceStreamMap.get(ns)
const nsp = this.namespaceMap.get(ns)
if (nsp?.sockets.size === 0 && stream) {
this.subscriber.unsubscribe(stream, this.redisMessageSubscriber)
this.namespaceStreamMap.delete(ns)
this.streamNamespaceMap.delete(stream)
this.namespaceMap.delete(ns)
}
}
})
}
/**
* This function is called when a client connects and it emit the `sync-step-1` and `awareness-update`
* events to the client to start the sync.
* @private
* @param {Socket} socket The socket connection
* @param {RedisDoc} doc The document
*/
startSynchronization = (socket, doc) => {
socket.emit(
'sync-step-1',
Y.encodeStateVector(doc.ydoc),
(/** @type {Uint8Array} */ update) => {
assert(this.client)
const message = Buffer.from(update.slice(0, update.byteLength))
this.client.addMessage(
this.getNamespaceString(socket.nsp),
'index',
Buffer.from(this.toRedis('sync-step-2', message))
)
}
)
if (doc.awareness.states.size > 0) {
socket.emit(
'awareness-update',
AwarenessProtocol.encodeAwarenessUpdate(
doc.awareness,
Array.from(doc.awareness.getStates().keys())
)
)
}
}
/**
* @private
* @param {string} stream
* @param {Array<Uint8Array>} messages
*/
redisMessageSubscriber = (stream, messages) => {
const namespace = this.streamNamespaceMap.get(stream)
if (!namespace) return
const nsp = this.namespaceMap.get(namespace)
if (!nsp) return
if (nsp.sockets.size === 0 && this.subscriber) {
this.subscriber.unsubscribe(stream, this.redisMessageSubscriber)
this.namespaceStreamMap.delete(namespace)
this.streamNamespaceMap.delete(stream)
this.namespaceMap.delete(namespace)
}
/** @type {Uint8Array[]} */
const updates = []
/** @type {Uint8Array[]} */
const awareness = []
for (const m of messages) {
const decoded = this.fromRedis(m)
if (decoded.type === 'awareness-update') awareness.push(decoded.message)
else updates.push(decoded.message)
}
for (const msg of updates) {
if (msg.length === 0) continue
nsp.emit('sync-update', msg)
}
for (const msg of awareness) {
if (msg.length === 0) continue
nsp.emit('awareness-update', msg)
}
}
/**
* @private
* @param {Promise<void>} task
*/
queueUpSyncTask (task) {
const len = this.syncQueue.push(task)
if (len === 1) this.consumeSyncQueue()
}
/**
* @private
*/
async consumeSyncQueue () {
if (this.syncQueue.length === 0) return
const task = this.syncQueue.shift()
await task
this.consumeSyncQueue()
}
/**
* @param {Namespace} namespace
*/
getNamespaceString (namespace) {
return namespace.name.replace(/\/yjs\|/, '')
}
/**
* @param {Uint8Array | ArrayBuffer | Buffer} message
* @returns {{ type: EventType, message: Uint8Array }}
*/
fromRedis (message) {
const decoder = decoding.createDecoder(new Uint8Array(message))
const opt = decoding.readUint8(decoder)
switch (opt) {
case protocol.messageSync: {
const syncStep = decoding.readUint8(decoder)
const message = decoding.readVarUint8Array(decoder)
switch (syncStep) {
case protocol.messageSyncStep1:
return {
type: 'sync-step-1',
message
}
case protocol.messageSyncStep2:
return {
type: 'sync-step-2',
message
}
case protocol.messageSyncUpdate:
return {
type: 'sync-update',
message
}
}
break
}
case protocol.messageAwareness: {
const message = decoding.readVarUint8Array(decoder)
return {
type: 'awareness-update',
message
}
}
}
throw new Error('unknown encoding')
}
/**
* @param {EventType} type
* @param {Uint8Array | ArrayBuffer | Buffer} message
*/
toRedis (type, message) {
return encoding.encode((encoder) => {
switch (type) {
case 'sync-step-1':
encoding.writeVarUint(encoder, protocol.messageSync)
encoding.writeVarUint(encoder, protocol.messageSyncStep1)
break
case 'sync-step-2':
encoding.writeVarUint(encoder, protocol.messageSync)
encoding.writeVarUint(encoder, protocol.messageSyncStep2)
break
case 'sync-update':
encoding.writeVarUint(encoder, protocol.messageSync)
encoding.writeVarUint(encoder, protocol.messageSyncUpdate)
break
case 'awareness-update':
encoding.writeVarUint(encoder, protocol.messageAwareness)
break
}
encoding.writeVarUint8Array(encoder, new Uint8Array(message))
})
}
destroy () {
try {
this.subscriber?.destroy()
return this.client?.destroy()
} catch (e) {
console.error(e)
}
}
}