@open-pencil/cloud 0.17.0-beta.1 → 0.17.0-beta.2

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.
Files changed (55) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/listen.mjs +236 -117
  3. package/dist/listen.mjs.map +1 -1
  4. package/dist/runtime-node.d.mts +2937 -703
  5. package/dist/runtime-node.d.mts.map +1 -1
  6. package/dist/server.d.ts +4011 -967
  7. package/dist/server.d.ts.map +1 -1
  8. package/dist/server.js +833 -376
  9. package/dist/server.js.map +1 -1
  10. package/dist/server.mjs +665 -271
  11. package/dist/server.mjs.map +1 -1
  12. package/package.json +4 -2
  13. package/src/admin/api/routes.ts +44 -33
  14. package/src/runtime/cloudflare/environment.ts +16 -2
  15. package/src/runtime/cloudflare/relay/index.ts +128 -0
  16. package/src/runtime/cloudflare/relay/sockets.ts +112 -0
  17. package/src/runtime/cloudflare/worker.ts +56 -19
  18. package/src/runtime/node/bootstrap.ts +9 -4
  19. package/src/runtime/node/server.ts +40 -8
  20. package/src/runtime/posthog/errors.ts +48 -0
  21. package/src/runtime/s3/objects.ts +26 -0
  22. package/src/runtime/site/server.ts +5 -3
  23. package/src/server/api/router.ts +3 -2
  24. package/src/server/api/types.ts +7 -0
  25. package/src/server/app.ts +4 -0
  26. package/src/server/cleanup/objects.ts +10 -2
  27. package/src/server/cleanup/revisions.ts +8 -2
  28. package/src/server/collaboration/index.ts +3 -0
  29. package/src/server/collaboration/relay/connection.ts +195 -0
  30. package/src/server/collaboration/relay/hub.ts +199 -187
  31. package/src/server/collaboration/relay/index.ts +3 -1
  32. package/src/server/collaboration/relay/log.ts +76 -0
  33. package/src/server/collaboration/relay/persistence.ts +147 -22
  34. package/src/server/collaboration/relay/resume.ts +28 -0
  35. package/src/server/collaboration/routes.ts +27 -16
  36. package/src/server/config/deployment.ts +11 -0
  37. package/src/server/config/schema.ts +24 -2
  38. package/src/server/db/migrate.ts +3 -1
  39. package/src/server/db/migrations/028_collaboration_updates.ts +84 -0
  40. package/src/server/db/tables.ts +18 -4
  41. package/src/server/documents/routes.ts +106 -80
  42. package/src/server/documents/versions/routes.ts +36 -25
  43. package/src/server/documents/versions/service.ts +9 -0
  44. package/src/server/errors/index.ts +56 -0
  45. package/src/server/index.ts +11 -0
  46. package/src/server/internal/routes.ts +3 -8
  47. package/src/server/objects/types.ts +12 -0
  48. package/src/server/pictures/routes.ts +4 -2
  49. package/src/server/rate-limit/index.ts +4 -1
  50. package/src/server/rate-limit/middleware.ts +99 -11
  51. package/src/server/sharing/document-routes.ts +30 -22
  52. package/src/server/sharing/routes.ts +122 -83
  53. package/src/server/validation.ts +58 -2
  54. package/src/server/workspaces/routes.ts +63 -45
  55. package/src/runtime/cloudflare/relay.ts +0 -136
@@ -1,8 +1,6 @@
1
1
  import {
2
2
  COLLAB_ACTIONS,
3
- createRelayClientAssembler,
4
3
  encodeSyncReply,
5
- decodeRelayClientFrame,
6
4
  decodeRoomUpdate,
7
5
  MAX_COLLAB_MESSAGE_BYTES,
8
6
  RELAY_CLOSE,
@@ -12,21 +10,21 @@ import {
12
10
  } from '#cloud/contract'
13
11
  import * as Y from 'yjs'
14
12
 
15
- import { authorizeRelayTicket, principalKey, type RelayAuthorization } from './authorize'
13
+ import type { RelayAuthorization } from './authorize'
16
14
  import { stampAwareness } from './awareness'
15
+ import {
16
+ openRelayConnection,
17
+ type ConnectionRelay,
18
+ type RelayConnection,
19
+ type RelayPeer,
20
+ type RelayRoomFrame,
21
+ type RelaySocket
22
+ } from './connection'
23
+ import { openRoomLog, type RoomLog } from './log'
17
24
  import type { CollaborationStateStore } from './persistence'
25
+ import type { RelayPeerState } from './resume'
18
26
 
19
- /** One client's socket, as a runtime adapter hands it to the relay. */
20
- export type RelaySocket = {
21
- send(data: Uint8Array): void
22
- close(code: number, reason: string): void
23
- }
24
-
25
- /** What the adapter reports back about that socket. */
26
- export type RelayConnection = {
27
- receive(data: Uint8Array): void
28
- closed(): void
29
- }
27
+ export type { RelayConnection, RelaySocket } from './connection'
30
28
 
31
29
  export type CollaborationRelayOptions = {
32
30
  authSecret: string
@@ -51,6 +49,12 @@ export type CollaborationRelayOptions = {
51
49
  currentRoomEpoch?: (documentId: string) => Promise<number | null>
52
50
  /** How long after a change the room's document is saved. */
53
51
  persistDelayMs?: number
52
+ /**
53
+ * How many logged updates, or how many of their bytes, a room saves before it writes a fresh
54
+ * snapshot in their place; a closing room always does.
55
+ */
56
+ compactAfterUpdates?: number
57
+ compactAfterBytes?: number
54
58
  /** How long a room whose last save failed waits before saving again. */
55
59
  persistRetryMs?: number
56
60
  /** How long a socket may stay open without presenting a ticket. */
@@ -61,20 +65,22 @@ export type CollaborationRelayOptions = {
61
65
  /** Messages a viewer may send; their document writes never leave the relay. */
62
66
  const VIEWER_ACTIONS = new Set<string>([COLLAB_ACTIONS.syncStep1, COLLAB_ACTIONS.awareness])
63
67
 
64
- type Peer = {
65
- id: string
66
- socket: RelaySocket
67
- authorization: RelayAuthorization
68
- presenceClients: Set<number>
69
- expiry?: ReturnType<typeof setTimeout>
70
- }
68
+ type Peer = RelayPeer
69
+
70
+ /** A room's document and the log its saves go to, loaded when a message first needs them. */
71
+ type RoomContent = { document: Y.Doc; log: RoomLog }
71
72
 
72
73
  type Room = {
73
74
  id: string
74
75
  identity: { documentId: string; roomEpoch: number }
75
- document: Y.Doc
76
76
  peers: Map<string, Peer>
77
77
  persistTimer: ReturnType<typeof setTimeout> | null
78
+ /**
79
+ * Null until a message needs the document: peers resumed after the runtime woke the relay
80
+ * exchange presence without it, so a wake for presence alone loads nothing from the store.
81
+ */
82
+ content: RoomContent | null
83
+ loading: Promise<RoomContent> | null
78
84
  }
79
85
 
80
86
  export type CollaborationRelay = ReturnType<typeof createCollaborationRelay>
@@ -93,6 +99,10 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
93
99
  let shuttingDown = false
94
100
  const persistDelayMs = options.persistDelayMs ?? 2_000
95
101
  const persistRetryMs = options.persistRetryMs ?? 30_000
102
+ const logLimits = {
103
+ updates: options.compactAfterUpdates ?? 500,
104
+ bytes: options.compactAfterBytes ?? 4 * 1024 * 1024
105
+ }
96
106
  const authTimeoutMs = options.authTimeoutMs ?? 10_000
97
107
  const report = options.onError ?? ((message, error) => console.error(`[Cloud] ${message}`, error))
98
108
 
@@ -115,20 +125,11 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
115
125
  // A room reopened while its last save is in flight must load what that save writes, or,
116
126
  // when the save failed, take the room it kept.
117
127
  await closing.get(authorization.roomId)
118
- const kept = rooms.get(authorization.roomId)
119
- if (kept) return kept
120
- const document = new Y.Doc()
121
- const state = await options.store.load(identity)
122
- if (state) Y.applyUpdate(document, state)
123
- const room: Room = {
124
- id: authorization.roomId,
125
- identity,
126
- document,
127
- peers: new Map(),
128
- persistTimer: null
129
- }
130
- document.on('update', () => schedulePersist(room))
131
- rooms.set(room.id, room)
128
+ // A room resumed without its document loads it in place; a new one joins the rooms once
129
+ // loaded, so a restore while it loads finds nothing to end and the epoch check turns it away.
130
+ const room = rooms.get(authorization.roomId) ?? newRoom(authorization.roomId, identity)
131
+ await contentOf(room)
132
+ if (!rooms.has(room.id)) rooms.set(room.id, room)
132
133
  return room
133
134
  })()
134
135
  opening.set(authorization.roomId, created)
@@ -139,15 +140,45 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
139
140
  }
140
141
  }
141
142
 
142
- /** Saves the room's document; false when the store refused it. */
143
- async function persist(room: Room): Promise<boolean> {
143
+ function newRoom(id: string, identity: Room['identity']): Room {
144
+ return { id, identity, peers: new Map(), persistTimer: null, content: null, loading: null }
145
+ }
146
+
147
+ /** The room's document and log, loaded from the store the first time something needs them. */
148
+ function contentOf(room: Room): Promise<RoomContent> {
149
+ if (room.content) return Promise.resolve(room.content)
150
+ room.loading ??= openRoomLog(options.store, room.identity, logLimits).then(
151
+ (content) => {
152
+ content.document.on('update', () => schedulePersist(room))
153
+ room.content = content
154
+ return content
155
+ },
156
+ (error: unknown) => {
157
+ room.loading = null
158
+ throw error
159
+ }
160
+ )
161
+ return room.loading
162
+ }
163
+
164
+ /** Saves what changed in the room since its last save; false when the store refused it. */
165
+ async function persist(room: Room, closing = false): Promise<boolean> {
144
166
  if (room.persistTimer) clearTimeout(room.persistTimer)
145
167
  room.persistTimer = null
168
+ // A room nobody needed the document of since it was resumed has nothing new to save, but
169
+ // a closing one loads it anyway, to fold what its earlier wakes logged into a snapshot.
170
+ if (!room.content && !closing) return true
146
171
  try {
147
- await options.store.store(room.identity, Y.encodeStateAsUpdate(room.document))
172
+ const { log } = await contentOf(room)
173
+ await log.save({ closing })
148
174
  return true
149
175
  } catch (error) {
150
176
  report('Saving a collaboration room failed:', error)
177
+ // The room holds edits the store doesn't have; save again without waiting for another edit.
178
+ // A closing room retries through `saveAndClose`.
179
+ if (!closing && rooms.get(room.id) === room) {
180
+ room.persistTimer ??= setTimeout(() => void persist(room), persistRetryMs)
181
+ }
151
182
  return false
152
183
  }
153
184
  }
@@ -160,7 +191,12 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
160
191
  * Applies a peer's V2 update to the room, inflated first for a sync reply. Rooms are saved as
161
192
  * V1 updates; only what peers exchange is V2.
162
193
  */
163
- function applyToRoom(room: Room, peer: Peer, namespace: string, payload: Uint8Array): boolean {
194
+ function applyToRoom(
195
+ document: Y.Doc,
196
+ peer: Peer,
197
+ namespace: string,
198
+ payload: Uint8Array
199
+ ): boolean {
164
200
  try {
165
201
  const update = decodeRoomUpdate(payload, {
166
202
  limit: MAX_COLLAB_MESSAGE_BYTES,
@@ -170,7 +206,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
170
206
  peer.socket.close(RELAY_CLOSE.messageTooLarge, 'Collaboration update is too large')
171
207
  return false
172
208
  }
173
- Y.applyUpdateV2(room.document, update, peer.id)
209
+ Y.applyUpdateV2(document, update, peer.id)
174
210
  return true
175
211
  } catch {
176
212
  peer.socket.close(RELAY_CLOSE.protocol, 'Malformed document update')
@@ -189,17 +225,19 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
189
225
  const recipients = target
190
226
  ? [room.peers.get(target)]
191
227
  : [...room.peers.values()].filter((peer) => peer !== from)
192
- sendAll(
193
- recipients.flatMap((peer) => (peer ? [peer.socket] : [])),
194
- frame
195
- )
228
+ sendAll(deliverable(recipients.flatMap((peer) => (peer ? [peer] : []))), frame)
196
229
  }
197
230
 
198
- function handleServerMessage(room: Room, peer: Peer, namespace: string, payload: Uint8Array) {
231
+ function handleServerMessage(
232
+ document: Y.Doc,
233
+ peer: Peer,
234
+ namespace: string,
235
+ payload: Uint8Array
236
+ ) {
199
237
  if (namespace === COLLAB_ACTIONS.syncStep1) {
200
238
  let reply: Uint8Array
201
239
  try {
202
- reply = encodeSyncReply(room.document, payload)
240
+ reply = encodeSyncReply(document, payload)
203
241
  } catch {
204
242
  peer.socket.close(RELAY_CLOSE.protocol, 'Malformed state vector')
205
243
  return
@@ -211,12 +249,20 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
211
249
  payload: reply
212
250
  })
213
251
  } else if (namespace === COLLAB_ACTIONS.syncReply) {
214
- applyToRoom(room, peer, namespace, payload)
252
+ applyToRoom(document, peer, namespace, payload)
215
253
  }
216
254
  }
217
255
 
256
+ /** Whether handling a message reads or writes the room's document. */
257
+ function needsDocument(namespace: string, target: string | null): boolean {
258
+ if (namespace === COLLAB_ACTIONS.yjsUpdate || namespace === COLLAB_ACTIONS.syncReply)
259
+ return true
260
+ return target === RELAY_SERVER_PEER && namespace === COLLAB_ACTIONS.syncStep1
261
+ }
262
+
218
263
  function handleMessage(
219
264
  room: Room,
265
+ document: Y.Doc | null,
220
266
  peer: Peer,
221
267
  namespace: string,
222
268
  target: string | null,
@@ -234,29 +280,74 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
234
280
  (other) => other !== peer && other.presenceClients.has(clientId)
235
281
  )
236
282
  )
283
+ const known = peer.presenceClients.size
237
284
  for (const clientId of stamped.claimed) peer.presenceClients.add(clientId)
285
+ if (peer.presenceClients.size !== known) remember(peer)
238
286
  if (!stamped.update) return
239
287
  forwarded = stamped.update
240
288
  }
241
289
 
242
290
  if (target === RELAY_SERVER_PEER) {
243
- handleServerMessage(room, peer, namespace, payload)
291
+ if (document) handleServerMessage(document, peer, namespace, payload)
244
292
  return
245
293
  }
246
294
  if (
247
295
  (namespace === COLLAB_ACTIONS.yjsUpdate || namespace === COLLAB_ACTIONS.syncReply) &&
248
- !applyToRoom(room, peer, namespace, payload)
296
+ !(document && applyToRoom(document, peer, namespace, payload))
249
297
  ) {
250
298
  return
251
299
  }
252
300
  forward(room, peer, namespace, target, forwarded)
253
301
  }
254
302
 
255
- function scheduleExpiry(peer: Peer) {
256
- clearTimeout(peer.expiry)
257
- peer.expiry = setTimeout(
258
- () => peer.socket.close(RELAY_CLOSE.expired, 'Collaboration ticket expired'),
259
- Math.max(0, peer.authorization.expiresAt - Date.now())
303
+ /**
304
+ * Whether the peer's ticket still holds, closing its socket when it lapsed. It is checked when
305
+ * the relay hears from the peer and before it sends the peer anything, never by a timer, so a
306
+ * runtime can set an idle relay aside while sockets stay open.
307
+ */
308
+ function current(peer: Peer): boolean {
309
+ if (Date.now() < peer.authorization.expiresAt) return true
310
+ peer.socket.close(RELAY_CLOSE.expired, 'Collaboration ticket expired')
311
+ return false
312
+ }
313
+
314
+ /** The sockets of peers whose tickets still hold. */
315
+ function deliverable(peers: Iterable<Peer>): RelaySocket[] {
316
+ return [...peers].filter(current).map((peer) => peer.socket)
317
+ }
318
+
319
+ function remember(peer: Peer) {
320
+ peer.socket.remember?.({
321
+ peerId: peer.id,
322
+ authorization: peer.authorization,
323
+ presenceClients: [...peer.presenceClients]
324
+ })
325
+ }
326
+
327
+ /** Handles a room message from an admitted peer; a promise while the room's document loads. */
328
+ function deliver(peer: Peer, frame: RelayRoomFrame): Promise<void> | undefined {
329
+ const room = rooms.get(peer.authorization.roomId)
330
+ if (!room) {
331
+ peer.socket.close(RELAY_CLOSE.unauthorized, 'Expected a collaboration ticket')
332
+ return undefined
333
+ }
334
+ if (!current(peer)) return undefined
335
+ const { namespace, target, payload } = frame
336
+ if (room.content || !needsDocument(namespace, target)) {
337
+ handleMessage(room, room.content?.document ?? null, peer, namespace, target, payload)
338
+ return undefined
339
+ }
340
+ return contentOf(room).then(
341
+ ({ document }) => {
342
+ // The peer may have left while the document loaded.
343
+ if (room.peers.get(peer.id) !== peer) return undefined
344
+ handleMessage(room, document, peer, namespace, target, payload)
345
+ return undefined
346
+ },
347
+ (error: unknown) => {
348
+ report('Loading a collaboration room failed:', error)
349
+ peer.socket.close(RELAY_CLOSE.protocol, 'Could not open the collaboration room')
350
+ }
260
351
  )
261
352
  }
262
353
 
@@ -290,29 +381,30 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
290
381
  authorization,
291
382
  presenceClients: new Set()
292
383
  }
293
- scheduleExpiry(peer)
384
+ remember(peer)
294
385
  send(socket, {
295
386
  type: 'welcome',
296
387
  peerId: peer.id,
297
388
  peers: [RELAY_SERVER_PEER, ...room.peers.keys()]
298
389
  })
299
- for (const other of room.peers.values()) send(other.socket, { type: 'join', peerId: peer.id })
390
+ sendAll(deliverable(room.peers.values()), { type: 'join', peerId: peer.id })
300
391
  room.peers.set(peer.id, peer)
301
392
  // Ask for what the newcomer has that the room lacks, such as edits made offline.
302
393
  if (authorization.permission === 'edit') {
394
+ const { document } = await contentOf(room)
303
395
  send(socket, {
304
396
  type: 'message',
305
397
  namespace: COLLAB_ACTIONS.syncStep1,
306
398
  from: RELAY_SERVER_PEER,
307
- payload: Y.encodeStateVector(room.document)
399
+ payload: Y.encodeStateVector(document)
308
400
  })
309
401
  }
310
402
  return peer
311
403
  }
312
404
 
313
405
  async function saveAndClose(room: Room) {
314
- if ((await persist(room)) || shuttingDown) {
315
- room.document.destroy()
406
+ if ((await persist(room, true)) || shuttingDown) {
407
+ room.content?.document.destroy()
316
408
  return
317
409
  }
318
410
  // The room holds edits the store doesn't have: keep it for whoever joins next, and save
@@ -336,137 +428,57 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
336
428
  }
337
429
 
338
430
  function depart(peer: Peer) {
339
- clearTimeout(peer.expiry)
340
431
  const room = rooms.get(peer.authorization.roomId)
341
432
  if (!room?.peers.delete(peer.id)) return
342
- for (const other of room.peers.values()) send(other.socket, { type: 'leave', peerId: peer.id })
433
+ sendAll(deliverable(room.peers.values()), { type: 'leave', peerId: peer.id })
343
434
  void leaveRoom(room)
344
435
  }
345
436
 
437
+ /**
438
+ * A socket's connection: a new one, which presents a ticket before it joins, or one resumed with
439
+ * the peer it already is.
440
+ */
441
+ const connectionRelay: ConnectionRelay = {
442
+ authSecret: options.authSecret,
443
+ documentId: options.documentId,
444
+ maximumMessageBytes: options.maximumMessageBytes,
445
+ authTimeoutMs,
446
+ admit,
447
+ refreshed: remember,
448
+ deliver,
449
+ depart,
450
+ report
451
+ }
452
+
346
453
  return {
347
454
  connect(socket: RelaySocket): RelayConnection {
348
- let peer: Peer | null = null
349
- /** Read through an object, since it changes across awaits and between messages. */
350
- const state = { closed: false, authenticating: false }
351
- const isClosed = () => state.closed
352
- const queued: Uint8Array[] = []
353
- let queuedBytes = 0
354
- // Only an editor's writes can be large: a viewer's frames, and a peer's before it is
355
- // admitted, fit in one message, so nobody else can make the relay hold more.
356
- const assembler = createRelayClientAssembler(() =>
357
- peer?.authorization.permission === 'edit'
358
- ? MAX_COLLAB_MESSAGE_BYTES
359
- : options.maximumMessageBytes
360
- )
361
- const authTimer = setTimeout(
362
- () => socket.close(RELAY_CLOSE.unauthorized, 'Expected a collaboration ticket'),
363
- authTimeoutMs
364
- )
365
-
366
- async function authenticate(token: string) {
367
- let authorization: RelayAuthorization
368
- try {
369
- authorization = await authorizeRelayTicket(token, options.authSecret)
370
- } catch {
371
- socket.close(RELAY_CLOSE.unauthorized, 'Invalid collaboration ticket')
372
- return
373
- }
374
- if (options.documentId !== undefined && authorization.documentId !== options.documentId) {
375
- socket.close(RELAY_CLOSE.unauthorized, 'Ticket is for a different document')
376
- return
377
- }
378
- if (state.closed) return
379
- if (peer) {
380
- // A refreshed ticket keeps the peer in its room as the same person; a lowered
381
- // permission applies from now on.
382
- const current = peer.authorization
383
- if (
384
- authorization.roomId !== current.roomId ||
385
- principalKey(authorization.principal) !== principalKey(current.principal)
386
- ) {
387
- socket.close(RELAY_CLOSE.unauthorized, 'Ticket is for a different room or person')
388
- return
389
- }
390
- peer.authorization = authorization
391
- scheduleExpiry(peer)
392
- return
393
- }
394
- clearTimeout(authTimer)
395
- const admitted = await admit(socket, authorization)
396
- // The socket may have closed while the room loaded.
397
- if (admitted && isClosed()) depart(admitted)
398
- else peer = admitted
399
- }
400
-
401
- function process(data: Uint8Array) {
402
- if (data.byteLength > options.maximumMessageBytes) {
403
- socket.close(RELAY_CLOSE.messageTooLarge, 'Collaboration message is too large')
404
- return
405
- }
406
- const assembled = assembler.push(data)
407
- if (assembled.type === 'partial') return
408
- if (assembled.type === 'too-large') {
409
- socket.close(RELAY_CLOSE.messageTooLarge, 'Collaboration message is too large')
410
- return
411
- }
412
- const frame = assembled.type === 'frame' ? decodeRelayClientFrame(assembled.data) : null
413
- if (!frame) {
414
- socket.close(RELAY_CLOSE.protocol, 'Malformed collaboration frame')
415
- return
416
- }
417
- if (frame.type === 'auth') {
418
- state.authenticating = true
419
- void authenticate(frame.token)
420
- .catch((error: unknown) => {
421
- report('Admitting a collaboration peer failed:', error)
422
- socket.close(RELAY_CLOSE.protocol, 'Could not open the collaboration room')
423
- })
424
- .finally(() => {
425
- state.authenticating = false
426
- flush()
427
- })
428
- return
429
- }
430
- const room = peer && rooms.get(peer.authorization.roomId)
431
- if (!peer || !room) {
432
- socket.close(RELAY_CLOSE.unauthorized, 'Expected a collaboration ticket')
433
- return
434
- }
435
- handleMessage(room, peer, frame.namespace, frame.target, frame.payload)
436
- }
455
+ return openRelayConnection(socket, null, connectionRelay)
456
+ },
437
457
 
438
- function flush() {
439
- while (!state.authenticating && !state.closed && queued.length > 0) {
440
- const next = queued.shift()
441
- if (!next) continue
442
- queuedBytes -= next.byteLength
443
- process(next)
444
- }
458
+ /**
459
+ * Takes back a socket whose peer was admitted before the runtime set the relay aside, as the
460
+ * peer it was, without loading the room's document until a message needs it. A peer whose
461
+ * ticket lapsed meanwhile is closed.
462
+ */
463
+ resume(socket: RelaySocket, saved: RelayPeerState): RelayConnection {
464
+ const { authorization } = saved
465
+ const identity = { documentId: authorization.documentId, roomEpoch: authorization.roomEpoch }
466
+ let room = rooms.get(authorization.roomId)
467
+ if (!room) {
468
+ room = newRoom(authorization.roomId, identity)
469
+ rooms.set(room.id, room)
445
470
  }
446
-
447
- return {
448
- receive(data) {
449
- if (state.closed) return
450
- queuedBytes += data.byteLength
451
- // Before admission a peer has sent its ticket and has nothing else to say, so what
452
- // waits for the room to open is held to one message's worth.
453
- if (!peer && queuedBytes > options.maximumMessageBytes) {
454
- queued.length = 0
455
- queuedBytes = 0
456
- socket.close(RELAY_CLOSE.messageTooLarge, 'Too much sent before joining the room')
457
- return
458
- }
459
- queued.push(data)
460
- flush()
461
- },
462
- closed() {
463
- state.closed = true
464
- queued.length = 0
465
- queuedBytes = 0
466
- clearTimeout(authTimer)
467
- if (peer) depart(peer)
468
- }
471
+ const peer: Peer = {
472
+ id: saved.peerId,
473
+ socket,
474
+ authorization,
475
+ presenceClients: new Set(saved.presenceClients)
469
476
  }
477
+ room.peers.set(peer.id, peer)
478
+ const connection = openRelayConnection(socket, peer, connectionRelay)
479
+ // After the runtime holds the connection, which its socket's close reports to.
480
+ queueMicrotask(() => current(peer))
481
+ return connection
470
482
  },
471
483
 
472
484
  /**
@@ -480,11 +492,10 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
480
492
  rooms.delete(room.id)
481
493
  if (room.persistTimer) clearTimeout(room.persistTimer)
482
494
  for (const peer of room.peers.values()) {
483
- clearTimeout(peer.expiry)
484
495
  peer.socket.close(RELAY_CLOSE.restored, 'The document was restored')
485
496
  }
486
497
  room.peers.clear()
487
- room.document.destroy()
498
+ room.content?.document.destroy()
488
499
  }
489
500
  },
490
501
 
@@ -497,9 +508,11 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
497
508
  payload: new Uint8Array()
498
509
  }
499
510
  sendAll(
500
- [...rooms.values()]
501
- .filter((room) => room.identity.documentId === documentId)
502
- .flatMap((room) => [...room.peers.values()].map((peer) => peer.socket)),
511
+ deliverable(
512
+ [...rooms.values()]
513
+ .filter((room) => room.identity.documentId === documentId)
514
+ .flatMap((room) => [...room.peers.values()])
515
+ ),
503
516
  frame
504
517
  )
505
518
  },
@@ -510,7 +523,6 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
510
523
  await Promise.all(
511
524
  [...rooms.values()].map(async (room) => {
512
525
  for (const peer of room.peers.values()) {
513
- clearTimeout(peer.expiry)
514
526
  peer.socket.close(1001, 'Server shutting down')
515
527
  }
516
528
  room.peers.clear()
@@ -11,5 +11,7 @@ export {
11
11
  export {
12
12
  createCollaborationStateStore,
13
13
  type CollaborationRoomIdentity,
14
- type CollaborationStateStore
14
+ type CollaborationStateStore,
15
+ type SavedRoom
15
16
  } from './persistence'
17
+ export { readRelayPeerState, type RelayPeerState } from './resume'
@@ -0,0 +1,76 @@
1
+ import * as Y from 'yjs'
2
+
3
+ import type { CollaborationRoomIdentity, CollaborationStateStore } from './persistence'
4
+
5
+ /** When a room folds its log into a fresh snapshot: after this many updates or bytes logged. */
6
+ export type RoomLogLimits = { updates: number; bytes: number }
7
+
8
+ export type RoomLog = {
9
+ /**
10
+ * Saves what the document took since the last save: one merged update in the log, or a snapshot
11
+ * in the log's place once it reaches its limits, as the room closes, or after a failed save.
12
+ * Saves run one at a time; a refused one rejects, and the next save writes a snapshot.
13
+ */
14
+ save(options?: { closing?: boolean }): Promise<void>
15
+ }
16
+
17
+ /**
18
+ * A room's document as its store saved it, its snapshot with the updates logged after it, and the
19
+ * log its saves go to. Each room has one writer, the relay holding it, so sequences only grow.
20
+ */
21
+ export async function openRoomLog(
22
+ store: CollaborationStateStore,
23
+ room: CollaborationRoomIdentity,
24
+ limits: RoomLogLimits
25
+ ): Promise<{ document: Y.Doc; log: RoomLog }> {
26
+ const saved = await store.load(room)
27
+ const document = new Y.Doc()
28
+ if (saved.snapshot) Y.applyUpdate(document, saved.snapshot)
29
+ for (const update of saved.updates) Y.applyUpdate(document, update)
30
+
31
+ /** Updates since the last save, which the next one logs. */
32
+ let pending: Uint8Array[] = []
33
+ document.on('update', (update: Uint8Array) => pending.push(update))
34
+ /** The last sequence a save may have written: an append that failed may still have landed. */
35
+ let sequence = saved.sequence
36
+ let loggedCount = saved.updates.length
37
+ let loggedBytes = saved.updates.reduce((total, update) => total + update.byteLength, 0)
38
+ /** Set by a failed save, after which only a snapshot is sure to hold the room. */
39
+ let compactNext = false
40
+ /** The save in flight; the next one waits for it, so two never claim one sequence. */
41
+ let saving: Promise<void> = Promise.resolve()
42
+
43
+ async function write(closing: boolean) {
44
+ const compact =
45
+ compactNext ||
46
+ loggedCount >= limits.updates ||
47
+ loggedBytes >= limits.bytes ||
48
+ (closing && (loggedCount > 0 || pending.length > 0))
49
+ // Updates that arrive while this save is in flight go to the next one.
50
+ const batch = pending
51
+ pending = []
52
+ if (compact) {
53
+ await store.compact(room, sequence, Y.encodeStateAsUpdate(document))
54
+ loggedCount = 0
55
+ loggedBytes = 0
56
+ compactNext = false
57
+ } else if (batch.length > 0) {
58
+ const update = Y.mergeUpdates(batch)
59
+ sequence += 1
60
+ await store.append(room, sequence, update)
61
+ loggedCount += 1
62
+ loggedBytes += update.byteLength
63
+ }
64
+ }
65
+
66
+ const log: RoomLog = {
67
+ save({ closing = false } = {}) {
68
+ const run = saving.then(() => write(closing))
69
+ saving = run.catch(() => {
70
+ compactNext = true
71
+ })
72
+ return run
73
+ }
74
+ }
75
+ return { document, log }
76
+ }