@tldraw/sync-core 5.5.0-canary.fdb2d37fcb2a → 5.5.0-canary.fdd9b4dee0d9
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.
- package/dist-cjs/index.d.ts +8 -4
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/lib/ClientWebSocketAdapter.js +23 -7
- package/dist-cjs/lib/ClientWebSocketAdapter.js.map +2 -2
- package/dist-cjs/lib/InMemorySyncStorage.js +18 -18
- package/dist-cjs/lib/InMemorySyncStorage.js.map +2 -2
- package/dist-cjs/lib/SQLiteSyncStorage.js.map +2 -2
- package/dist-cjs/lib/TLSocketRoom.js +5 -1
- package/dist-cjs/lib/TLSocketRoom.js.map +2 -2
- package/dist-cjs/lib/TLSyncClient.js +3 -1
- package/dist-cjs/lib/TLSyncClient.js.map +2 -2
- package/dist-cjs/lib/TLSyncRoom.js +52 -47
- package/dist-cjs/lib/TLSyncRoom.js.map +2 -2
- package/dist-cjs/lib/TLSyncStorage.js +2 -2
- package/dist-cjs/lib/TLSyncStorage.js.map +2 -2
- package/dist-cjs/lib/chunk.js +25 -2
- package/dist-cjs/lib/chunk.js.map +2 -2
- package/dist-cjs/lib/diff.js +15 -7
- package/dist-cjs/lib/diff.js.map +2 -2
- package/dist-esm/index.d.mts +8 -4
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/lib/ClientWebSocketAdapter.mjs +24 -8
- package/dist-esm/lib/ClientWebSocketAdapter.mjs.map +2 -2
- package/dist-esm/lib/InMemorySyncStorage.mjs +18 -18
- package/dist-esm/lib/InMemorySyncStorage.mjs.map +2 -2
- package/dist-esm/lib/SQLiteSyncStorage.mjs.map +2 -2
- package/dist-esm/lib/TLSocketRoom.mjs +6 -2
- package/dist-esm/lib/TLSocketRoom.mjs.map +2 -2
- package/dist-esm/lib/TLSyncClient.mjs +3 -1
- package/dist-esm/lib/TLSyncClient.mjs.map +2 -2
- package/dist-esm/lib/TLSyncRoom.mjs +52 -47
- package/dist-esm/lib/TLSyncRoom.mjs.map +2 -2
- package/dist-esm/lib/TLSyncStorage.mjs +2 -2
- package/dist-esm/lib/TLSyncStorage.mjs.map +2 -2
- package/dist-esm/lib/chunk.mjs +25 -2
- package/dist-esm/lib/chunk.mjs.map +2 -2
- package/dist-esm/lib/diff.mjs +16 -8
- package/dist-esm/lib/diff.mjs.map +2 -2
- package/package.json +20 -17
- package/src/lib/ClientWebSocketAdapter.test.ts +58 -0
- package/src/lib/ClientWebSocketAdapter.ts +33 -10
- package/src/lib/InMemorySyncStorage.test.ts +21 -0
- package/src/lib/InMemorySyncStorage.ts +22 -24
- package/src/lib/SQLiteSyncStorage.ts +2 -2
- package/src/lib/TLSocketRoom.ts +8 -2
- package/src/lib/TLSyncClient.ts +2 -0
- package/src/lib/TLSyncRoom.ts +83 -51
- package/src/lib/TLSyncStorage.ts +7 -2
- package/src/lib/chunk.test.ts +54 -1
- package/src/lib/chunk.ts +46 -2
- package/src/lib/diff.test.ts +91 -1
- package/src/lib/diff.ts +30 -9
- package/src/test/TLSocketRoom.test.ts +24 -0
- package/src/test/TLSyncRoom.test.ts +195 -0
- package/src/test/upgradeDowngrade.test.ts +21 -0
|
@@ -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)', () => {
|
|
@@ -2340,6 +2386,31 @@ describe('26. Session lifecycle (SES)', () => {
|
|
|
2340
2386
|
expect(room.sessions.size).toBe(0)
|
|
2341
2387
|
})
|
|
2342
2388
|
|
|
2389
|
+
it('[SES4] a reason longer than 123 UTF-8 bytes is truncated on a code-point boundary with a dropped-bytes suffix', () => {
|
|
2390
|
+
const { room } = makeRoom()
|
|
2391
|
+
|
|
2392
|
+
const exact = connectSession(room, 'exact')
|
|
2393
|
+
room.rejectSession('exact', 'x'.repeat(123))
|
|
2394
|
+
expect(exact.close).toHaveBeenCalledWith(TLSyncErrorCloseEventCode, 'x'.repeat(123))
|
|
2395
|
+
|
|
2396
|
+
// '... (+200 bytes)' reserves 16 bytes, leaving 107 for the reason
|
|
2397
|
+
const ascii = connectSession(room, 'ascii')
|
|
2398
|
+
room.rejectSession('ascii', 'x'.repeat(200))
|
|
2399
|
+
expect(ascii.close).toHaveBeenCalledWith(
|
|
2400
|
+
TLSyncErrorCloseEventCode,
|
|
2401
|
+
'x'.repeat(107) + '... (+93 bytes)'
|
|
2402
|
+
)
|
|
2403
|
+
|
|
2404
|
+
// 161 bytes in, 107 to fill: 1 + 26 × 4 = 105, and a 27th 4-byte character is dropped whole
|
|
2405
|
+
const emoji = connectSession(room, 'emoji')
|
|
2406
|
+
room.rejectSession('emoji', 'x' + '\u{1F600}'.repeat(40))
|
|
2407
|
+
expect(emoji.close).toHaveBeenCalledWith(
|
|
2408
|
+
TLSyncErrorCloseEventCode,
|
|
2409
|
+
'x' + '\u{1F600}'.repeat(26) + '... (+56 bytes)'
|
|
2410
|
+
)
|
|
2411
|
+
expect(room.sessions.size).toBe(0)
|
|
2412
|
+
})
|
|
2413
|
+
|
|
2343
2414
|
it('[HS2][SES5] sets supportsStringAppend to false for protocol version 7', () => {
|
|
2344
2415
|
const { room } = makeRoom()
|
|
2345
2416
|
connectSession(room, 'v7-session', { protocolVersion: 7 })
|
|
@@ -2411,6 +2482,130 @@ describe('26. Session lifecycle (SES)', () => {
|
|
|
2411
2482
|
expect(socket.__lastMessage).toEqual({ type: 'pong' })
|
|
2412
2483
|
})
|
|
2413
2484
|
|
|
2485
|
+
it('[SES5] no session receives string-append ops while a client without append support is connected', () => {
|
|
2486
|
+
vi.useFakeTimers()
|
|
2487
|
+
const { room } = makeRoom()
|
|
2488
|
+
const v8 = connectSession(room, 'v8', { protocolVersion: getTlsyncProtocolVersion() })
|
|
2489
|
+
const v7 = connectSession(room, 'v7', { protocolVersion: 7 })
|
|
2490
|
+
expect(room.getCanEmitStringAppend()).toBe(false)
|
|
2491
|
+
|
|
2492
|
+
const hasAppend = (msg: any) => JSON.stringify(msg).includes('"append"')
|
|
2493
|
+
|
|
2494
|
+
// a full put over an existing record whose only change is a string append
|
|
2495
|
+
room.handleMessage('v8', {
|
|
2496
|
+
type: 'push',
|
|
2497
|
+
clientClock: 1,
|
|
2498
|
+
diff: { [pageRecord.id]: ['put', { ...pageRecord, name: pageRecord.name + ' more' }] },
|
|
2499
|
+
} as TLPushRequest<TLRecord>)
|
|
2500
|
+
vi.advanceTimersByTime(DATA_MESSAGE_DEBOUNCE_INTERVAL + 1)
|
|
2501
|
+
expect(sentDataMessages(v7)).toEqual([
|
|
2502
|
+
{
|
|
2503
|
+
type: 'patch',
|
|
2504
|
+
diff: { [pageRecord.id]: ['patch', { name: ['put', pageRecord.name + ' more'] }] },
|
|
2505
|
+
serverClock: 1,
|
|
2506
|
+
},
|
|
2507
|
+
])
|
|
2508
|
+
expect(sentDataMessages(v8).some(hasAppend)).toBe(false)
|
|
2509
|
+
clearSocket(v7)
|
|
2510
|
+
clearSocket(v8)
|
|
2511
|
+
|
|
2512
|
+
// a patch whose recomputed broadcast would otherwise be an append
|
|
2513
|
+
room.handleMessage('v8', {
|
|
2514
|
+
type: 'push',
|
|
2515
|
+
clientClock: 2,
|
|
2516
|
+
diff: { [pageRecord.id]: ['patch', { name: ['put', pageRecord.name + ' more and more'] }] },
|
|
2517
|
+
} as TLPushRequest<TLRecord>)
|
|
2518
|
+
vi.advanceTimersByTime(DATA_MESSAGE_DEBOUNCE_INTERVAL + 1)
|
|
2519
|
+
expect(sentDataMessages(v7)).toEqual([
|
|
2520
|
+
{
|
|
2521
|
+
type: 'patch',
|
|
2522
|
+
diff: {
|
|
2523
|
+
[pageRecord.id]: ['patch', { name: ['put', pageRecord.name + ' more and more'] }],
|
|
2524
|
+
},
|
|
2525
|
+
serverClock: 2,
|
|
2526
|
+
},
|
|
2527
|
+
])
|
|
2528
|
+
expect(sentDataMessages(v8)).toEqual([
|
|
2529
|
+
{ type: 'push_result', clientClock: 2, serverClock: 2, action: 'commit' },
|
|
2530
|
+
])
|
|
2531
|
+
})
|
|
2532
|
+
|
|
2533
|
+
it('[SES6] handleResumedSession rejects a session whose schema the server can no longer reconcile', () => {
|
|
2534
|
+
const { room } = makeRoom()
|
|
2535
|
+
const socket = makeSocket()
|
|
2536
|
+
const newerSchema: SerializedSchemaV2 = {
|
|
2537
|
+
schemaVersion: 2,
|
|
2538
|
+
sequences: {
|
|
2539
|
+
...(room.serializedSchema as SerializedSchemaV2).sequences,
|
|
2540
|
+
'com.tldraw.store': 999,
|
|
2541
|
+
},
|
|
2542
|
+
}
|
|
2543
|
+
|
|
2544
|
+
room.handleResumedSession({
|
|
2545
|
+
sessionId: 'resumed',
|
|
2546
|
+
socket,
|
|
2547
|
+
meta: undefined,
|
|
2548
|
+
isReadonly: false,
|
|
2549
|
+
serializedSchema: newerSchema,
|
|
2550
|
+
presenceId: null,
|
|
2551
|
+
presenceRecord: null,
|
|
2552
|
+
requiresLegacyRejection: false,
|
|
2553
|
+
supportsStringAppend: true,
|
|
2554
|
+
})
|
|
2555
|
+
|
|
2556
|
+
expect(room.sessions.has('resumed')).toBe(false)
|
|
2557
|
+
expect(socket.close).toHaveBeenCalledWith(
|
|
2558
|
+
TLSyncErrorCloseEventCode,
|
|
2559
|
+
TLSyncErrorCloseEventReason.SERVER_TOO_OLD
|
|
2560
|
+
)
|
|
2561
|
+
})
|
|
2562
|
+
|
|
2563
|
+
it('[SES6] rejecting a resumed session removes its restored presence for other sessions', () => {
|
|
2564
|
+
vi.useFakeTimers()
|
|
2565
|
+
const { room } = makeRoom()
|
|
2566
|
+
const makePresence = (name: string) =>
|
|
2567
|
+
InstancePresenceRecordType.create({
|
|
2568
|
+
id: InstancePresenceRecordType.createId(name),
|
|
2569
|
+
currentPageId: pageRecord.id,
|
|
2570
|
+
userId: createUserId(name),
|
|
2571
|
+
userName: name,
|
|
2572
|
+
})
|
|
2573
|
+
const resume = (sessionId: string, serializedSchema: SerializedSchema) => {
|
|
2574
|
+
const socket = makeSocket()
|
|
2575
|
+
const presenceRecord = makePresence(sessionId)
|
|
2576
|
+
room.handleResumedSession({
|
|
2577
|
+
sessionId,
|
|
2578
|
+
socket,
|
|
2579
|
+
meta: undefined,
|
|
2580
|
+
isReadonly: false,
|
|
2581
|
+
serializedSchema,
|
|
2582
|
+
presenceId: presenceRecord.id,
|
|
2583
|
+
presenceRecord,
|
|
2584
|
+
requiresLegacyRejection: false,
|
|
2585
|
+
supportsStringAppend: true,
|
|
2586
|
+
})
|
|
2587
|
+
return { socket, presenceId: presenceRecord.id }
|
|
2588
|
+
}
|
|
2589
|
+
|
|
2590
|
+
const other = resume('other', room.serializedSchema)
|
|
2591
|
+
const rejected = resume('rejected', {
|
|
2592
|
+
schemaVersion: 2,
|
|
2593
|
+
sequences: {
|
|
2594
|
+
...(room.serializedSchema as SerializedSchemaV2).sequences,
|
|
2595
|
+
'com.tldraw.store': 999,
|
|
2596
|
+
},
|
|
2597
|
+
})
|
|
2598
|
+
vi.advanceTimersByTime(DATA_MESSAGE_DEBOUNCE_INTERVAL * 2)
|
|
2599
|
+
|
|
2600
|
+
expect(room.sessions.has('rejected')).toBe(false)
|
|
2601
|
+
expect(room.presenceStore.get(rejected.presenceId)).toBeUndefined()
|
|
2602
|
+
// other clients still hold this presence from before the socket slept
|
|
2603
|
+
expect(sentDataMessages(other.socket).at(-1)).toMatchObject({
|
|
2604
|
+
type: 'patch',
|
|
2605
|
+
diff: { [rejected.presenceId]: [RecordOpType.Remove] },
|
|
2606
|
+
})
|
|
2607
|
+
})
|
|
2608
|
+
|
|
2414
2609
|
it('[SES7] a message from an unknown session id logs a warning and is ignored', () => {
|
|
2415
2610
|
const warn = vi.fn()
|
|
2416
2611
|
const { room } = makeRoom({ log: { warn } })
|
|
@@ -413,6 +413,27 @@ it('[HS3] out-of-date clients will receive incompatibility errors', () => {
|
|
|
413
413
|
expect(socket.close).toHaveBeenCalledWith(4099, TLSyncErrorCloseEventReason.CLIENT_TOO_OLD)
|
|
414
414
|
})
|
|
415
415
|
|
|
416
|
+
it.each([
|
|
417
|
+
['a v2 schema without sequences', { schemaVersion: 2 }],
|
|
418
|
+
['a v2 schema with null sequences', { schemaVersion: 2, sequences: null }],
|
|
419
|
+
['a null schema', null],
|
|
420
|
+
])('[HS3] clients sending %s are rejected as too old', (_, schema) => {
|
|
421
|
+
const server = new TestServer(schemaV3)
|
|
422
|
+
const id = 'test_malformed_schema'
|
|
423
|
+
const socket = mockSocket()
|
|
424
|
+
|
|
425
|
+
server.room.handleNewSession({ sessionId: id, socket, meta: undefined, isReadonly: false })
|
|
426
|
+
server.room.handleMessage(id, {
|
|
427
|
+
type: 'connect',
|
|
428
|
+
connectRequestId: 'test',
|
|
429
|
+
lastServerClock: 0,
|
|
430
|
+
protocolVersion: getTlsyncProtocolVersion(),
|
|
431
|
+
schema: schema as any,
|
|
432
|
+
})
|
|
433
|
+
|
|
434
|
+
expect(socket.close).toHaveBeenCalledWith(4099, TLSyncErrorCloseEventReason.CLIENT_TOO_OLD)
|
|
435
|
+
})
|
|
436
|
+
|
|
416
437
|
it('[HS2] clients using an out-of-date protocol will receive compatibility errors', () => {
|
|
417
438
|
const actualVersion = getTlsyncProtocolVersion()
|
|
418
439
|
mockGetTlsyncProtocolVersion.mockReturnValue(actualVersion + 1)
|