@tldraw/sync-core 5.5.0-canary.4fbbdcf5e86c → 5.5.0-canary.51fe070ff155

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,6 +12,7 @@ vi.mock('@tldraw/utils', async (importOriginal) => {
12
12
  // NOTE: setupVitest.js replaces the global WebSocket with the 'ws' package's WebSocket,
13
13
  // matching the WebSocketServer the tests connect to.
14
14
  import { WebSocketServer, WebSocket as WsWebSocket } from 'ws'
15
+ import { MAX_ASSEMBLED_MESSAGE_CHARS } from './chunk'
15
16
  import {
16
17
  ACTIVE_MAX_DELAY,
17
18
  ACTIVE_MIN_DELAY,
@@ -400,6 +401,32 @@ describe('ClientWebSocketAdapter', () => {
400
401
  )
401
402
  })
402
403
 
404
+ it('[CW6] refuses to send a message the server would reject as too large', async () => {
405
+ const onMessage = vi.fn()
406
+ connectMock.mockImplementationOnce((ws: any) => {
407
+ ws.on('message', onMessage)
408
+ })
409
+ await waitFor(() => adapter._ws?.readyState === WebSocket.OPEN)
410
+
411
+ const onStatusChange = vi.fn()
412
+ adapter.onStatusChange(onStatusChange)
413
+
414
+ const message = {
415
+ ...connectMessage(),
416
+ largeData: 'x'.repeat(MAX_ASSEMBLED_MESSAGE_CHARS),
417
+ } as any
418
+ adapter.sendMessage(message)
419
+
420
+ // nothing goes on the wire: sending would only earn a close and a reconnect that
421
+ // re-sends the same message
422
+ expect(onMessage).not.toHaveBeenCalled()
423
+ expect(adapter.connectionStatus).toBe('error')
424
+ expect(onStatusChange).toHaveBeenCalledWith({
425
+ status: 'error',
426
+ reason: TLSyncErrorCloseEventReason.MESSAGE_TOO_LARGE,
427
+ })
428
+ })
429
+
403
430
  it('[CW6] silently drops the message when there is no socket', async () => {
404
431
  await waitFor(() => adapter._ws?.readyState === WebSocket.OPEN)
405
432
 
@@ -882,6 +909,37 @@ describe('ReconnectManager', () => {
882
909
  // closing again is safe
883
910
  expect(() => manager.close()).not.toThrow()
884
911
  })
912
+
913
+ it('[RM4] reconnect hints and the disconnect handler survive an undefined WebSocket global', async () => {
914
+ // Regression test for the undefined-WebSocket crash guarded in ClientWebSocketAdapter.ts (#10106).
915
+ await waitFor(() => adapter._ws?.readyState === WebSocket.OPEN)
916
+ const manager = adapter._reconnectManager
917
+ // jsdom reports a throwing event listener on window instead of from dispatchEvent
918
+ const listenerErrors: unknown[] = []
919
+ const onError = (event: ErrorEvent) => {
920
+ event.preventDefault()
921
+ listenerErrors.push(event.error)
922
+ }
923
+ window.addEventListener('error', onError)
924
+ const originalWebSocket = globalThis.WebSocket
925
+ try {
926
+ ;(globalThis as any).WebSocket = undefined
927
+
928
+ window.dispatchEvent(new Event('online'))
929
+ expect(() => manager.maybeReconnected()).not.toThrow()
930
+ expect(() => manager.connected()).not.toThrow()
931
+ expect(adapter.connectionStatus).toBe('online')
932
+
933
+ window.dispatchEvent(new Event('offline'))
934
+ expect(() => manager.disconnected()).not.toThrow()
935
+ expect(listenerErrors).toEqual([])
936
+ expect(adapter.connectionStatus).toBe('offline')
937
+ expect(adapter._ws).toBeNull()
938
+ } finally {
939
+ globalThis.WebSocket = originalWebSocket
940
+ window.removeEventListener('error', onError)
941
+ }
942
+ })
885
943
  })
886
944
 
887
945
  describe('URI failure boundaries', () => {
@@ -1,7 +1,7 @@
1
1
  import { atom, Atom } from '@tldraw/state'
2
2
  import { TLRecord } from '@tldraw/tlschema'
3
3
  import { assert, warnOnce } from '@tldraw/utils'
4
- import { chunk } from './chunk'
4
+ import { chunk, MAX_ASSEMBLED_MESSAGE_CHARS } from './chunk'
5
5
  import { TLSocketClientSentEvent, TLSocketServerSentEvent } from './protocol'
6
6
  import {
7
7
  TLPersistentClientSocket,
@@ -11,6 +11,16 @@ import {
11
11
  TLSyncErrorCloseEventReason,
12
12
  } from './TLSyncClient'
13
13
 
14
+ // The ready states from the WebSocket spec, kept local instead of read off the global
15
+ // constructor: an extension or page teardown can leave `WebSocket` undefined by the time a
16
+ // window `online` or close handler fires, and `WebSocket.OPEN` then throws (#10106).
17
+ const ReadyState = {
18
+ CONNECTING: 0,
19
+ OPEN: 1,
20
+ CLOSING: 2,
21
+ CLOSED: 3,
22
+ } as const
23
+
14
24
  function listenTo<T extends EventTarget>(target: T, event: string, handler: () => void) {
15
25
  target.addEventListener(event, handler)
16
26
  return () => {
@@ -176,8 +186,8 @@ export class ClientWebSocketAdapter implements TLPersistentClientSocket<
176
186
  assert(!this.isDisposed, 'Tried to set a new websocket on a disposed socket')
177
187
  assert(
178
188
  this._ws === null ||
179
- this._ws.readyState === WebSocket.CLOSED ||
180
- this._ws.readyState === WebSocket.CLOSING,
189
+ this._ws.readyState === ReadyState.CLOSED ||
190
+ this._ws.readyState === ReadyState.CLOSING,
181
191
  `Tried to set a new websocket in when the existing one was ${this._ws?.readyState}`
182
192
  )
183
193
 
@@ -280,7 +290,20 @@ export class ClientWebSocketAdapter implements TLPersistentClientSocket<
280
290
 
281
291
  if (!this._ws) return
282
292
  if (this.connectionStatus === 'online') {
283
- const chunks = chunk(JSON.stringify(msg))
293
+ const stringified = JSON.stringify(msg)
294
+ // The server rejects a session whose assembled message exceeds this, so sending would
295
+ // only earn a close and a reconnect that re-sends the same message. Fail here the way
296
+ // the server would, so onSyncError runs once instead of the client looping forever.
297
+ if (stringified.length > MAX_ASSEMBLED_MESSAGE_CHARS) {
298
+ this._handleDisconnect(
299
+ 'closed',
300
+ TLSyncErrorCloseEventCode,
301
+ true,
302
+ TLSyncErrorCloseEventReason.MESSAGE_TOO_LARGE
303
+ )
304
+ return
305
+ }
306
+ const chunks = chunk(stringified)
284
307
  for (const part of chunks) {
285
308
  this._ws.send(part)
286
309
  }
@@ -536,7 +559,7 @@ export class ReconnectManager {
536
559
  // this can happen if the promise gets resolved too late
537
560
  if (this.state !== 'pendingAttempt' || this.isDisposed) return
538
561
  assert(
539
- this.socketAdapter._ws?.readyState !== WebSocket.OPEN,
562
+ this.socketAdapter._ws?.readyState !== ReadyState.OPEN,
540
563
  'There should be no connection attempts while already connected'
541
564
  )
542
565
 
@@ -590,13 +613,13 @@ export class ReconnectManager {
590
613
  this.clearRecheckConnectingTimeout()
591
614
 
592
615
  // readyState can be CONNECTING, OPEN, CLOSING, CLOSED, or null (if getUri() is still pending)
593
- if (this.socketAdapter._ws?.readyState === WebSocket.OPEN) {
616
+ if (this.socketAdapter._ws?.readyState === ReadyState.OPEN) {
594
617
  debug('ReconnectManager.maybeReconnected: already connected')
595
618
  // nothing to do, we're already OK
596
619
  return
597
620
  }
598
621
 
599
- if (this.socketAdapter._ws?.readyState === WebSocket.CONNECTING) {
622
+ if (this.socketAdapter._ws?.readyState === ReadyState.CONNECTING) {
600
623
  debug('ReconnectManager.maybeReconnected: connecting')
601
624
  // We might be waiting for a TCP connection that sent SYN out and will never get it back,
602
625
  // while a new connection appeared. On the other hand, we might have just started connecting
@@ -660,8 +683,8 @@ export class ReconnectManager {
660
683
 
661
684
  // Guard against delayed notifications and recheck synchronously
662
685
  if (
663
- this.socketAdapter._ws?.readyState !== WebSocket.OPEN &&
664
- this.socketAdapter._ws?.readyState !== WebSocket.CONNECTING
686
+ this.socketAdapter._ws?.readyState !== ReadyState.OPEN &&
687
+ this.socketAdapter._ws?.readyState !== ReadyState.CONNECTING
665
688
  ) {
666
689
  debug('ReconnectManager.disconnected: websocket is not OPEN or CONNECTING')
667
690
  this.clearReconnectTimeout()
@@ -718,7 +741,7 @@ export class ReconnectManager {
718
741
  connected() {
719
742
  debug('ReconnectManager.connected')
720
743
  // this notification could've been delayed, recheck synchronously
721
- if (this.socketAdapter._ws?.readyState === WebSocket.OPEN) {
744
+ if (this.socketAdapter._ws?.readyState === ReadyState.OPEN) {
722
745
  debug('ReconnectManager.connected: websocket is OPEN')
723
746
  this.state = 'connected'
724
747
  this.clearReconnectTimeout()
@@ -1,7 +1,7 @@
1
1
  import type { SerializedSchema, StoreSchema, UnknownRecord } from '@tldraw/store'
2
2
  import { createTLSchema, TLInstancePresence, TLStoreSnapshot } from '@tldraw/tlschema'
3
3
  import { getOwnProperty, hasOwnProperty, isEqual, structuredClone } from '@tldraw/utils'
4
- import { JsonChunkAssembler } from './chunk'
4
+ import { JsonChunkAssembler, MessageTooLargeError } from './chunk'
5
5
  import { DEFAULT_INITIAL_SNAPSHOT, InMemorySyncStorage } from './InMemorySyncStorage'
6
6
  import { TLObjectStoreAccess, TLSocketServerSentEvent } from './protocol'
7
7
  import { RoomSessionState } from './RoomSession'
@@ -477,6 +477,11 @@ export class TLSocketRoom<R extends UnknownRecord = UnknownRecord, SessionMeta =
477
477
  })
478
478
  this.room.pruneSessions()
479
479
  this.scheduleDebouncedSnapshot(sessionId)
480
+ } else if (res.error instanceof MessageTooLargeError) {
481
+ this.log?.error?.('Error assembling message', res.error)
482
+ // Resetting the connection would have the client reconnect and re-send the same
483
+ // oversized message, so reject the session instead and let the client surface it.
484
+ this.room.rejectSession(sessionId, TLSyncErrorCloseEventReason.MESSAGE_TOO_LARGE)
480
485
  } else {
481
486
  this.log?.error?.('Error assembling message', res.error)
482
487
  // close the socket to reset the connection
@@ -122,6 +122,8 @@ export const TLSyncErrorCloseEventReason = {
122
122
  RATE_LIMITED: 'RATE_LIMITED',
123
123
  /** Room has reached maximum capacity */
124
124
  ROOM_FULL: 'ROOM_FULL',
125
+ /** A single message exceeded the size the server will assemble */
126
+ MESSAGE_TOO_LARGE: 'MESSAGE_TOO_LARGE',
125
127
  } as const
126
128
 
127
129
  /**
@@ -1023,6 +1023,10 @@ export class TLSyncRoom<R extends UnknownRecord, SessionMeta> {
1023
1023
 
1024
1024
  private forceAllReconnect() {
1025
1025
  for (const session of this.sessions.values()) {
1026
+ // Only connected clients hold state at a clock we can no longer diff from. A session
1027
+ // mid-handshake gets hydrated from its own lastServerClock in the same transaction, so
1028
+ // removing it would close its socket and then re-add it as Connected (resurrected).
1029
+ if (session.state !== RoomSessionState.Connected) continue
1026
1030
  this.removeSession(session.sessionId)
1027
1031
  }
1028
1032
  }
@@ -1116,7 +1120,7 @@ export class TLSyncRoom<R extends UnknownRecord, SessionMeta> {
1116
1120
 
1117
1121
  const requiresDownMigrations = migrations.value.length > 0
1118
1122
 
1119
- const connect = async (msg: Extract<TLSocketServerSentEvent<R>, { type: 'connect' }>) => {
1123
+ const connect = (msg: Extract<TLSocketServerSentEvent<R>, { type: 'connect' }>) => {
1120
1124
  this.sessions.set(session.sessionId, {
1121
1125
  state: RoomSessionState.Connected,
1122
1126
  sessionId: session.sessionId,
@@ -1,5 +1,11 @@
1
1
  import { assert } from 'tldraw'
2
- import { JsonChunkAssembler, chunk } from './chunk'
2
+ import {
3
+ JsonChunkAssembler,
4
+ MAX_ASSEMBLED_MESSAGE_CHARS,
5
+ MAX_CHUNK_COUNT,
6
+ MessageTooLargeError,
7
+ chunk,
8
+ } from './chunk'
3
9
 
4
10
  describe('chunk (CH1–CH3)', () => {
5
11
  describe('size boundary (CH1)', () => {
@@ -403,4 +409,51 @@ describe('JsonChunkAssembler (CH4–CH8)', () => {
403
409
  })
404
410
  })
405
411
  })
412
+
413
+ describe('assembly bounds (CH10)', () => {
414
+ it('[CH10] rejects a first chunk declaring more than MAX_CHUNK_COUNT chunks', () => {
415
+ const unchunker = new JsonChunkAssembler()
416
+
417
+ const result = unchunker.handleMessage(`${MAX_CHUNK_COUNT}_{"a":`)
418
+ expect(result).toMatchObject({
419
+ error: expect.objectContaining({ message: `Too many chunks: ${MAX_CHUNK_COUNT + 1}` }),
420
+ })
421
+ expect(unchunker.state).toBe('idle')
422
+ })
423
+
424
+ it('[CH10] accepts a declared count at the limit', () => {
425
+ const unchunker = new JsonChunkAssembler()
426
+
427
+ expect(unchunker.handleMessage(`${MAX_CHUNK_COUNT - 1}_{"a":`)).toBeNull()
428
+ expect(unchunker.state).not.toBe('idle')
429
+ })
430
+
431
+ it('[CH10] rejects an assembly once its accumulated bodies pass MAX_ASSEMBLED_MESSAGE_CHARS', () => {
432
+ const unchunker = new JsonChunkAssembler()
433
+ // A chunk stream that never sends its final chunk. Before CH10 this accumulated
434
+ // without limit for as long as the sender kept sending.
435
+ const body = 'x'.repeat(1024 * 1024)
436
+ const chunksNeeded = Math.ceil(MAX_ASSEMBLED_MESSAGE_CHARS / body.length) + 1
437
+
438
+ let result = null
439
+ for (let i = 0; i < chunksNeeded; i++) {
440
+ result = unchunker.handleMessage(`${chunksNeeded - i}_${body}`)
441
+ if (result) break
442
+ }
443
+
444
+ expect(result).toMatchObject({ error: expect.any(MessageTooLargeError) })
445
+ expect(unchunker.state).toBe('idle')
446
+ })
447
+
448
+ it('[CH10] leaves the assembler usable after rejecting an oversized assembly', () => {
449
+ const unchunker = new JsonChunkAssembler()
450
+ const body = 'x'.repeat(1024 * 1024)
451
+ const chunksNeeded = Math.ceil(MAX_ASSEMBLED_MESSAGE_CHARS / body.length) + 1
452
+ for (let i = 0; i < chunksNeeded; i++) {
453
+ if (unchunker.handleMessage(`${chunksNeeded - i}_${body}`)) break
454
+ }
455
+
456
+ expect(unchunker.handleMessage('{"test": true}')).toMatchObject({ data: { test: true } })
457
+ })
458
+ })
406
459
  })
package/src/lib/chunk.ts CHANGED
@@ -8,6 +8,35 @@ const MAX_BYTES_PER_CHAR = 4
8
8
  // in the (admittedly impossible) worst case, the max size is 1/4 of a megabyte
9
9
  const MAX_SAFE_MESSAGE_SIZE = MAX_CLIENT_SENT_MESSAGE_SIZE_BYTES / MAX_BYTES_PER_CHAR
10
10
 
11
+ // JsonChunkAssembler holds every chunk of a message until the last one arrives, and the sender
12
+ // decides how many that is — the count is just a prefix on the first chunk. Nothing in the
13
+ // protocol obliges a sender to ever send the last chunk, so without a ceiling a single connection
14
+ // can make the receiver hold an arbitrarily large buffer for as long as the socket stays open.
15
+ //
16
+ // The cap counts UTF-16 code units, which is what a JS string costs in memory: 16M of them is the
17
+ // 32MB of retained string data a room can afford to hold per session and stay inside a Durable
18
+ // Object's 128MB instance limit with room for the document itself. It is far above anything
19
+ // chunk() produces for a real document push. The character cap is the real bound; the chunk-count
20
+ // cap only rejects an absurd declared count on arrival instead of waiting for the characters to
21
+ // accumulate, and is loose enough not to constrain a sender that chunks more finely than the
22
+ // default.
23
+ export const MAX_ASSEMBLED_MESSAGE_CHARS = 16 * 1024 * 1024
24
+ export const MAX_CHUNK_COUNT = 100_000
25
+
26
+ /**
27
+ * Thrown when a message would exceed MAX_ASSEMBLED_MESSAGE_CHARS, on either side of the wire.
28
+ * Distinct from the other assembly errors because it is fatal: retrying sends the same
29
+ * oversized message, so the session is rejected rather than reset.
30
+ *
31
+ * @internal
32
+ */
33
+ export class MessageTooLargeError extends Error {
34
+ constructor(chars: number) {
35
+ super(`Message too large: ${chars} characters, max ${MAX_ASSEMBLED_MESSAGE_CHARS}`)
36
+ this.name = 'MessageTooLargeError'
37
+ }
38
+ }
39
+
11
40
  /**
12
41
  * Splits a string into smaller chunks suitable for transmission over WebSockets.
13
42
  * This function ensures messages don't exceed size limits imposed by platforms like Cloudflare Workers (1MB max).
@@ -100,6 +129,7 @@ export class JsonChunkAssembler {
100
129
  | {
101
130
  chunksReceived: string[]
102
131
  totalChunks: number
132
+ charsReceived: number
103
133
  } = 'idle'
104
134
 
105
135
  /**
@@ -110,7 +140,8 @@ export class JsonChunkAssembler {
110
140
  * @param msg - The message to process, either JSON or chunk format
111
141
  * @returns Result object with data/stringified on success, error object on failure, or null for incomplete chunks
112
142
  * - `\{ data: object, stringified: string \}` - Successfully parsed complete message
113
- * - `\{ error: Error \}` - Parse error or invalid chunk sequence
143
+ * - `\{ error: Error \}` - Parse error, invalid chunk sequence, or a message exceeding
144
+ * MAX_ASSEMBLED_MESSAGE_CHARS / MAX_CHUNK_COUNT
114
145
  * - `null` - Chunk received but more chunks expected
115
146
  *
116
147
  * @example
@@ -144,17 +175,30 @@ export class JsonChunkAssembler {
144
175
  const data = match[2]
145
176
 
146
177
  if (this.state === 'idle') {
178
+ const totalChunks = numChunksRemaining + 1
179
+ if (totalChunks > MAX_CHUNK_COUNT) {
180
+ // Nothing has been buffered yet, so the state is already idle.
181
+ return { error: new Error(`Too many chunks: ${totalChunks}`) }
182
+ }
147
183
  this.state = {
148
184
  chunksReceived: [data],
149
- totalChunks: numChunksRemaining + 1,
185
+ totalChunks,
186
+ charsReceived: data.length,
150
187
  }
151
188
  } else {
152
189
  this.state.chunksReceived.push(data)
190
+ this.state.charsReceived += data.length
153
191
  if (numChunksRemaining !== this.state.totalChunks - this.state.chunksReceived.length) {
154
192
  this.state = 'idle'
155
193
  return { error: new Error(`Chunks received in wrong order`) }
156
194
  }
157
195
  }
196
+ if (this.state.charsReceived > MAX_ASSEMBLED_MESSAGE_CHARS) {
197
+ // Drop what was buffered rather than keeping it until the socket closes.
198
+ const charsReceived = this.state.charsReceived
199
+ this.state = 'idle'
200
+ return { error: new MessageTooLargeError(charsReceived) }
201
+ }
158
202
  if (this.state.chunksReceived.length === this.state.totalChunks) {
159
203
  try {
160
204
  const stringified = this.state.chunksReceived.join('')
@@ -21,6 +21,7 @@ import {
21
21
  ZERO_INDEX_KEY,
22
22
  } from 'tldraw'
23
23
  import { beforeEach, describe, expect, it, vi } from 'vitest'
24
+ import { MAX_ASSEMBLED_MESSAGE_CHARS } from '../lib/chunk'
24
25
  import { RecordOpType } from '../lib/diff'
25
26
  import { DEFAULT_INITIAL_SNAPSHOT, InMemorySyncStorage } from '../lib/InMemorySyncStorage'
26
27
  import { getTlsyncProtocolVersion } from '../lib/protocol'
@@ -392,6 +393,29 @@ describe('28. TLSocketRoom (SR)', () => {
392
393
  expect(room.getSessions()[0].isConnected).toBe(false)
393
394
  })
394
395
 
396
+ it('[SR5] rejects the session when an assembly exceeds the size cap', () => {
397
+ const log: TLSyncLog = { warn: vi.fn(), error: vi.fn() }
398
+ const room = new TLSocketRoom({ log })
399
+ const socket = createMockSocket()
400
+ connectSession(room, 'test-session', socket)
401
+
402
+ // A chunk stream that never sends its last chunk. Unlike the other assembly errors
403
+ // this one is fatal: a reconnecting client would re-send the same oversized message.
404
+ const body = 'x'.repeat(1024 * 1024)
405
+ const chunksNeeded = Math.ceil(MAX_ASSEMBLED_MESSAGE_CHARS / body.length) + 1
406
+ for (let i = 0; i < chunksNeeded; i++) {
407
+ room.handleSocketMessage('test-session', `${chunksNeeded - i}_${body}`)
408
+ if ((log.error as any).mock.calls.length) break
409
+ }
410
+
411
+ expect(log.error).toHaveBeenCalledWith('Error assembling message', expect.anything())
412
+ expect(socket.close).toHaveBeenCalledWith(
413
+ TLSyncErrorCloseEventCode,
414
+ TLSyncErrorCloseEventReason.MESSAGE_TOO_LARGE
415
+ )
416
+ expect(room.getSessions()).toHaveLength(0)
417
+ })
418
+
395
419
  it('[SR5] rejects the session with UNKNOWN_ERROR when message handling throws', () => {
396
420
  const log: TLSyncLog = { warn: vi.fn(), error: vi.fn() }
397
421
  const room = new TLSocketRoom({ log })
@@ -1019,6 +1019,52 @@ describe('23. Connect handshake (HS)', () => {
1019
1019
  expect(room.sessions.get('current-client-session')?.state).toBe(RoomSessionState.Connected)
1020
1020
  expect(socket.__lastMessage?.type).toBe('connect')
1021
1021
  })
1022
+
1023
+ it('[RC5][HS6] a wipeAll during a handshake closes connected sessions but hydrates the connecting one', async () => {
1024
+ const { room, storage } = makeRoom()
1025
+ const socketA = connectSession(room, 'a')
1026
+ const removed = vi.fn()
1027
+ room.events.on('session_removed', removed)
1028
+
1029
+ // 'b' has opened its socket but not yet sent its connect message
1030
+ const socketB = makeSocket()
1031
+ room.handleNewSession({ sessionId: 'b', socket: socketB, meta: undefined, isReadonly: false })
1032
+
1033
+ // the room hasn't seen this change yet because the storage notifies on a microtask
1034
+ const newPage = makePage('wipe_page', 'Wipe Page')
1035
+ storage.transaction((txn) => {
1036
+ txn.set(newPage.id, newPage)
1037
+ })
1038
+ storage.tombstoneHistoryStartsAtClock.set(storage.getClock())
1039
+
1040
+ // the handshake's own transaction runs broadcastChanges first and hits the wipeAll
1041
+ room.handleMessage('b', {
1042
+ type: 'connect',
1043
+ connectRequestId: 'connect-b',
1044
+ lastServerClock: 0,
1045
+ protocolVersion: getTlsyncProtocolVersion(),
1046
+ schema: room.serializedSchema,
1047
+ } satisfies TLConnectRequest)
1048
+
1049
+ expect(socketA.close).toHaveBeenCalled()
1050
+ expect(room.sessions.has('a')).toBe(false)
1051
+
1052
+ expect(socketB.close).not.toHaveBeenCalled()
1053
+ expect(room.sessions.get('b')?.state).toBe(RoomSessionState.Connected)
1054
+ expect(socketB.__messages).toHaveLength(1)
1055
+ const connectMessage = socketB.__lastMessage as Extract<
1056
+ TLSocketServerSentEvent<any>,
1057
+ { type: 'connect' }
1058
+ >
1059
+ expect(connectMessage.type).toBe('connect')
1060
+ expect(connectMessage.hydrationType).toBe('wipe_all')
1061
+ expect(connectMessage.diff[newPage.id]).toEqual(['put', newPage])
1062
+
1063
+ await Promise.resolve()
1064
+ await Promise.resolve()
1065
+ expect(removed.mock.calls.map(([e]) => e.sessionId)).toEqual(['a'])
1066
+ expect(room.sessions.has('b')).toBe(true)
1067
+ })
1022
1068
  })
1023
1069
 
1024
1070
  describe('24. Push handling (RP)', () => {