@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.5
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/README.md +2 -1
- package/dist/bin/server.mjs +1 -1
- package/dist/bin/server.mjs.map +1 -1
- package/dist/bin/services.mjs +1 -1
- package/dist/bin/services.mjs.map +1 -1
- package/dist/client.d.ts +296 -27
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -295
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +331 -21
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +1773 -32
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +3478 -288
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +48 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +466 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +35 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +447 -290
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +89 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +15667 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +2963 -294
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +3473 -842
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +12187 -8244
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +7739 -3548
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +5 -5
- package/dist/server2.mjs.map +1 -1
- package/package.json +22 -5
- package/src/admin/audit/service.ts +2 -2
- package/src/admin/documents/service.ts +76 -6
- package/src/admin/reports/service.ts +7 -1
- package/src/client/api.ts +23 -471
- package/src/client/auth/client.ts +22 -3
- package/src/client/billing.ts +80 -0
- package/src/client/community.ts +40 -0
- package/src/client/discovery.ts +35 -9
- package/src/client/documents.ts +160 -0
- package/src/client/index.ts +7 -0
- package/src/client/portal/account.ts +54 -2
- package/src/client/portal/request.ts +2 -0
- package/src/client/response.ts +21 -0
- package/src/client/schema.ts +8 -0
- package/src/client/services.ts +23 -18
- package/src/client/sharing.ts +262 -0
- package/src/client/versions.ts +104 -0
- package/src/contract/account.ts +50 -0
- package/src/contract/billing.ts +166 -0
- package/src/contract/collaboration.ts +5 -0
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +63 -2
- package/src/contract/entitlements.ts +10 -0
- package/src/contract/index.ts +54 -5
- package/src/contract/links.ts +57 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +18 -16
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +141 -0
- package/src/email/locales/de.json +141 -0
- package/src/email/locales/es.json +141 -0
- package/src/email/locales/fr.json +141 -0
- package/src/email/locales/it.json +141 -0
- package/src/email/locales/ja.json +141 -0
- package/src/email/locales/pl.json +141 -0
- package/src/email/locales/pt-br.json +141 -0
- package/src/email/locales/ru.json +141 -0
- package/src/email/locales/zh-cn.json +141 -0
- package/src/email/messages.ts +200 -0
- package/src/email/render.ts +142 -276
- package/src/email/samples.ts +115 -0
- package/src/email/schema.ts +70 -6
- package/src/email/templates/BrandEmail.vue +54 -0
- package/src/email/templates/DocumentBlocked.vue +45 -0
- package/src/email/templates/DocumentInvitation.vue +24 -0
- package/src/email/templates/DocumentReport.vue +32 -0
- package/src/email/templates/DocumentReportDecided.vue +36 -0
- package/src/email/templates/DocumentReportReceived.vue +18 -0
- package/src/email/templates/DocumentRestored.vue +24 -0
- package/src/email/templates/EmailChangeRequested.vue +24 -0
- package/src/email/templates/EmailRoot.vue +19 -0
- package/src/email/templates/EmailText.vue +9 -0
- package/src/email/templates/EmailVerification.vue +38 -0
- package/src/email/templates/EnrollmentNotification.vue +24 -0
- package/src/email/templates/EnrollmentUpdate.vue +52 -0
- package/src/email/templates/NewSignIn.vue +37 -0
- package/src/email/templates/PasswordChanged.vue +22 -0
- package/src/email/templates/PasswordReset.vue +23 -0
- package/src/email/templates/StorageLimitReached.vue +28 -0
- package/src/email/templates/TwoStepChanged.vue +32 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +9 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/relay/index.ts +7 -1
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +8 -1
- package/src/runtime/node/cleanup.ts +5 -1
- package/src/runtime/node/email/transport.ts +13 -10
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/server.ts +12 -2
- package/src/runtime/node/services/main.ts +1 -1
- package/src/runtime/posthog/errors.ts +2 -1
- package/src/runtime/s3/objects.ts +17 -0
- package/src/runtime/site/community.ts +101 -0
- package/src/runtime/site/server.ts +17 -1
- package/src/server/account/deletion.ts +2 -0
- package/src/server/account/routes.ts +271 -240
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +23 -0
- package/src/server/app/checks.ts +84 -0
- package/src/server/app/editors.ts +20 -0
- package/src/server/app/index.ts +128 -0
- package/src/server/app/indexing.ts +40 -0
- package/src/server/app/limiters.ts +53 -0
- package/src/server/app/routes.ts +114 -0
- package/src/server/app/services.ts +185 -0
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/adapter.ts +21 -1
- package/src/server/auth/email.ts +4 -1
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +88 -131
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/mfa-assurance.ts +51 -0
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/session.ts +2 -0
- package/src/server/billing/index.ts +6 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +16 -3
- package/src/server/cleanup/worker.ts +10 -1
- package/src/server/collaboration/relay/hub.ts +25 -142
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/service.ts +21 -2
- package/src/server/community/index.ts +2 -0
- package/src/server/community/routes.ts +93 -0
- package/src/server/community/service.ts +134 -0
- package/src/server/config/deployment.ts +17 -5
- package/src/server/config/index.ts +1 -1
- package/src/server/config/public.ts +7 -1
- package/src/server/config/schema.ts +40 -292
- package/src/server/config/toml/schema.ts +307 -0
- package/src/server/db/index.ts +1 -0
- package/src/server/db/migrate.ts +13 -1
- package/src/server/db/migrations/009_access_tokens.ts +47 -0
- package/src/server/db/migrations/010_storage_limit_notices.ts +50 -0
- package/src/server/db/migrations/011_account_ids.ts +30 -0
- package/src/server/db/migrations/012_email_locales.ts +34 -0
- package/src/server/db/migrations/013_document_block_emails.ts +40 -0
- package/src/server/db/migrations/014_report_replies.ts +28 -0
- package/src/server/db/migrations/schema.ts +26 -1
- package/src/server/db/tables.ts +30 -1
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +7 -1
- package/src/server/documents/service.ts +12 -402
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/service.ts +81 -24
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +31 -4
- package/src/server/email/types.ts +12 -0
- package/src/server/errors/index.ts +1 -9
- package/src/server/index.ts +12 -5
- package/src/server/invitations/types.ts +5 -1
- package/src/server/objects/types.ts +5 -0
- package/src/server/policy/configured.ts +9 -3
- package/src/server/policy/database.ts +13 -2
- package/src/server/policy/policy.ts +39 -12
- package/src/server/policy/service.ts +71 -52
- package/src/server/policy/storage.ts +33 -0
- package/src/server/quota/index.ts +6 -0
- package/src/server/quota/notices.ts +121 -0
- package/src/server/quota/storage.ts +5 -0
- package/src/server/rate-limit/middleware.ts +4 -0
- package/src/server/reports/index.ts +1 -0
- package/src/server/reports/replies.ts +55 -0
- package/src/server/reports/routes.ts +3 -1
- package/src/server/reports/service.ts +24 -2
- package/src/server/sharing/capabilities/service.ts +2 -3
- package/src/server/sharing/invitations/service.ts +14 -4
- package/src/server/validation.ts +3 -0
- package/src/server/workspaces/members.ts +7 -19
- package/src/services/app.ts +1 -3
- package/src/services/config.ts +1 -0
- package/src/services/usage.ts +6 -14
- package/src/email/templates/document-invitation.ts +0 -35
- package/src/email/templates/enrollment.ts +0 -27
- package/src/email/templates/layout.ts +0 -18
- package/src/server/app.ts +0 -453
- /package/src/{server/errors → errors}/posthog.ts +0 -0
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
COLLAB_ACTIONS,
|
|
3
|
-
encodeSyncReply,
|
|
4
|
-
decodeRoomUpdate,
|
|
5
3
|
MAX_COLLAB_MESSAGE_BYTES,
|
|
6
4
|
RELAY_CLOSE,
|
|
7
5
|
RELAY_SERVER_PEER,
|
|
8
|
-
relayServerMessages,
|
|
9
6
|
type RelayServerFrame
|
|
10
7
|
} from '#cloud/contract'
|
|
11
8
|
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
@@ -21,9 +18,20 @@ import {
|
|
|
21
18
|
type RelayRoomFrame,
|
|
22
19
|
type RelaySocket
|
|
23
20
|
} from './connection'
|
|
24
|
-
import { openRoomLog
|
|
21
|
+
import { openRoomLog } from './log'
|
|
25
22
|
import type { CollaborationStateStore } from './persistence'
|
|
26
23
|
import type { RelayPeerState } from './resume'
|
|
24
|
+
import {
|
|
25
|
+
answerAsServer,
|
|
26
|
+
applyToRoom,
|
|
27
|
+
needsDocument,
|
|
28
|
+
newRoom,
|
|
29
|
+
send,
|
|
30
|
+
sendAll,
|
|
31
|
+
updatesDocument,
|
|
32
|
+
type Room,
|
|
33
|
+
type RoomContent
|
|
34
|
+
} from './room'
|
|
27
35
|
|
|
28
36
|
export type { RelayConnection, RelaySocket } from './connection'
|
|
29
37
|
|
|
@@ -71,43 +79,8 @@ export type CollaborationRelayOptions = {
|
|
|
71
79
|
/** Messages a viewer may send; their document writes never leave the relay. */
|
|
72
80
|
const VIEWER_ACTIONS = new Set<string>([COLLAB_ACTIONS.syncStep1, COLLAB_ACTIONS.awareness])
|
|
73
81
|
|
|
74
|
-
type Peer = RelayPeer
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* A room's document and the log its saves go to, loaded when a message first needs them, with an
|
|
78
|
-
* upper bound on the document's size: what was saved plus every update since. The bound is
|
|
79
|
-
* measured again only once it reaches `maximumRoomBytes` after something was deleted, since only
|
|
80
|
-
* a deletion can leave the document smaller than the bound.
|
|
81
|
-
*/
|
|
82
|
-
type RoomContent = { document: Y.Doc; log: RoomLog; bytes: number; deletedSinceMeasure: boolean }
|
|
83
|
-
|
|
84
|
-
type Room = {
|
|
85
|
-
id: string
|
|
86
|
-
identity: { documentId: string; roomEpoch: number }
|
|
87
|
-
peers: Map<string, Peer>
|
|
88
|
-
persistTimer: ReturnType<typeof setTimeout> | null
|
|
89
|
-
/**
|
|
90
|
-
* Null until a message needs the document: peers resumed after the runtime woke the relay
|
|
91
|
-
* exchange presence without it, so a wake for presence alone loads nothing from the store.
|
|
92
|
-
*/
|
|
93
|
-
content: RoomContent | null
|
|
94
|
-
loading: Promise<RoomContent> | null
|
|
95
|
-
}
|
|
96
|
-
|
|
97
82
|
export type CollaborationRelay = ReturnType<typeof createCollaborationRelay>
|
|
98
83
|
|
|
99
|
-
/** Whether `update` adds nothing and deletes only content `document` already holds. */
|
|
100
|
-
function deletesOnlyKnown(document: Y.Doc, update: Uint8Array): boolean {
|
|
101
|
-
const { from, to } = Y.parseUpdateMetaV2(update)
|
|
102
|
-
if (![...to].every(([client, clock]) => from.get(client) === clock)) return false
|
|
103
|
-
const { ds } = Y.decodeUpdateV2(update)
|
|
104
|
-
for (const [client, deletions] of ds.clients) {
|
|
105
|
-
const held = Y.getState(document.store, client)
|
|
106
|
-
if (deletions.some((deletion) => deletion.clock + deletion.len > held)) return false
|
|
107
|
-
}
|
|
108
|
-
return true
|
|
109
|
-
}
|
|
110
|
-
|
|
111
84
|
/**
|
|
112
85
|
* A runtime-neutral relay for collaboration rooms. It forwards room messages between peers and
|
|
113
86
|
* joins every room as a peer itself: it holds the room's document, answers sync requests from
|
|
@@ -135,15 +108,6 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
135
108
|
options.onError ??
|
|
136
109
|
((message, error) => logCloud('error', message, { source: 'relay', error: loggedError(error) }))
|
|
137
110
|
|
|
138
|
-
function send(socket: RelaySocket, frame: RelayServerFrame) {
|
|
139
|
-
for (const message of relayServerMessages(frame)) socket.send(message)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
function sendAll(sockets: Iterable<RelaySocket>, frame: RelayServerFrame) {
|
|
143
|
-
const messages = relayServerMessages(frame)
|
|
144
|
-
for (const socket of sockets) for (const message of messages) socket.send(message)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
111
|
async function openRoom(authorization: RelayAuthorization): Promise<Room> {
|
|
148
112
|
const existing = rooms.get(authorization.roomId)
|
|
149
113
|
if (existing) return existing
|
|
@@ -169,10 +133,6 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
169
133
|
}
|
|
170
134
|
}
|
|
171
135
|
|
|
172
|
-
function newRoom(id: string, identity: Room['identity']): Room {
|
|
173
|
-
return { id, identity, peers: new Map(), persistTimer: null, content: null, loading: null }
|
|
174
|
-
}
|
|
175
|
-
|
|
176
136
|
/** The room's document and log, loaded from the store the first time something needs them. */
|
|
177
137
|
function contentOf(room: Room): Promise<RoomContent> {
|
|
178
138
|
if (room.content) return Promise.resolve(room.content)
|
|
@@ -222,38 +182,9 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
222
182
|
room.persistTimer ??= setTimeout(() => void persist(room), persistDelayMs)
|
|
223
183
|
}
|
|
224
184
|
|
|
225
|
-
/**
|
|
226
|
-
* Applies a peer's V2 update to the room, inflated first for a sync reply. Rooms are saved as
|
|
227
|
-
* V1 updates; only what peers exchange is V2.
|
|
228
|
-
*/
|
|
229
|
-
function applyToRoom(
|
|
230
|
-
content: RoomContent,
|
|
231
|
-
peer: Peer,
|
|
232
|
-
namespace: string,
|
|
233
|
-
payload: Uint8Array
|
|
234
|
-
): boolean {
|
|
235
|
-
const { document } = content
|
|
236
|
-
try {
|
|
237
|
-
const update = decodeRoomUpdate(payload, {
|
|
238
|
-
limit: maximumRoomBytes,
|
|
239
|
-
compressed: namespace === COLLAB_ACTIONS.syncReply
|
|
240
|
-
})
|
|
241
|
-
if (!update || !fits(content, update)) {
|
|
242
|
-
peer.socket.close(RELAY_CLOSE.messageTooLarge, 'Collaboration update is too large')
|
|
243
|
-
return false
|
|
244
|
-
}
|
|
245
|
-
Y.applyUpdateV2(document, update, peer.id)
|
|
246
|
-
content.bytes += update.byteLength
|
|
247
|
-
return true
|
|
248
|
-
} catch {
|
|
249
|
-
peer.socket.close(RELAY_CLOSE.protocol, 'Malformed document update')
|
|
250
|
-
return false
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
|
|
254
185
|
function forward(
|
|
255
186
|
room: Room,
|
|
256
|
-
from:
|
|
187
|
+
from: RelayPeer,
|
|
257
188
|
namespace: string,
|
|
258
189
|
target: string | null,
|
|
259
190
|
payload: Uint8Array
|
|
@@ -265,58 +196,10 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
265
196
|
sendAll(deliverable(recipients.flatMap((peer) => (peer ? [peer] : []))), frame)
|
|
266
197
|
}
|
|
267
198
|
|
|
268
|
-
/**
|
|
269
|
-
* Whether the room's document stays within its limit after `update`. An update that only
|
|
270
|
-
* deletes what the room holds shrinks it, so it always fits; one that deletes what the room has
|
|
271
|
-
* not received waits in the document, so it counts like any other. Otherwise the bound grows by
|
|
272
|
-
* the update; past the limit, the document is measured only if a deletion may have shrunk it
|
|
273
|
-
* since the last measure, so edits near the limit never encode the whole document each time.
|
|
274
|
-
*/
|
|
275
|
-
function fits(content: RoomContent, update: Uint8Array): boolean {
|
|
276
|
-
if (deletesOnlyKnown(content.document, update)) return true
|
|
277
|
-
if (content.bytes + update.byteLength <= maximumRoomBytes) return true
|
|
278
|
-
if (!content.deletedSinceMeasure) return false
|
|
279
|
-
content.bytes = Y.encodeStateAsUpdateV2(content.document).byteLength
|
|
280
|
-
content.deletedSinceMeasure = false
|
|
281
|
-
return content.bytes + update.byteLength <= maximumRoomBytes
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
function handleServerMessage(
|
|
285
|
-
content: RoomContent,
|
|
286
|
-
peer: Peer,
|
|
287
|
-
namespace: string,
|
|
288
|
-
payload: Uint8Array
|
|
289
|
-
) {
|
|
290
|
-
if (namespace === COLLAB_ACTIONS.syncStep1) {
|
|
291
|
-
let reply: Uint8Array
|
|
292
|
-
try {
|
|
293
|
-
reply = encodeSyncReply(content.document, payload)
|
|
294
|
-
} catch {
|
|
295
|
-
peer.socket.close(RELAY_CLOSE.protocol, 'Malformed state vector')
|
|
296
|
-
return
|
|
297
|
-
}
|
|
298
|
-
send(peer.socket, {
|
|
299
|
-
type: 'message',
|
|
300
|
-
namespace: COLLAB_ACTIONS.syncReply,
|
|
301
|
-
from: RELAY_SERVER_PEER,
|
|
302
|
-
payload: reply
|
|
303
|
-
})
|
|
304
|
-
} else if (namespace === COLLAB_ACTIONS.syncReply) {
|
|
305
|
-
applyToRoom(content, peer, namespace, payload)
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/** Whether handling a message reads or writes the room's document. */
|
|
310
|
-
function needsDocument(namespace: string, target: string | null): boolean {
|
|
311
|
-
if (namespace === COLLAB_ACTIONS.yjsUpdate || namespace === COLLAB_ACTIONS.syncReply)
|
|
312
|
-
return true
|
|
313
|
-
return target === RELAY_SERVER_PEER && namespace === COLLAB_ACTIONS.syncStep1
|
|
314
|
-
}
|
|
315
|
-
|
|
316
199
|
function handleMessage(
|
|
317
200
|
room: Room,
|
|
318
201
|
content: RoomContent | null,
|
|
319
|
-
peer:
|
|
202
|
+
peer: RelayPeer,
|
|
320
203
|
namespace: string,
|
|
321
204
|
target: string | null,
|
|
322
205
|
payload: Uint8Array
|
|
@@ -341,12 +224,12 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
341
224
|
}
|
|
342
225
|
|
|
343
226
|
if (target === RELAY_SERVER_PEER) {
|
|
344
|
-
if (content)
|
|
227
|
+
if (content) answerAsServer(content, peer, namespace, payload, maximumRoomBytes)
|
|
345
228
|
return
|
|
346
229
|
}
|
|
347
230
|
if (
|
|
348
|
-
(namespace
|
|
349
|
-
!(content && applyToRoom(content, peer, namespace, payload))
|
|
231
|
+
updatesDocument(namespace) &&
|
|
232
|
+
!(content && applyToRoom(content, peer, namespace, payload, maximumRoomBytes))
|
|
350
233
|
) {
|
|
351
234
|
return
|
|
352
235
|
}
|
|
@@ -358,18 +241,18 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
358
241
|
* the relay hears from the peer and before it sends the peer anything, never by a timer, so a
|
|
359
242
|
* runtime can set an idle relay aside while sockets stay open.
|
|
360
243
|
*/
|
|
361
|
-
function current(peer:
|
|
244
|
+
function current(peer: RelayPeer): boolean {
|
|
362
245
|
if (Date.now() < peer.authorization.expiresAt) return true
|
|
363
246
|
peer.socket.close(RELAY_CLOSE.expired, 'Collaboration ticket expired')
|
|
364
247
|
return false
|
|
365
248
|
}
|
|
366
249
|
|
|
367
250
|
/** The sockets of peers whose tickets still hold. */
|
|
368
|
-
function deliverable(peers: Iterable<
|
|
251
|
+
function deliverable(peers: Iterable<RelayPeer>): RelaySocket[] {
|
|
369
252
|
return [...peers].filter(current).map((peer) => peer.socket)
|
|
370
253
|
}
|
|
371
254
|
|
|
372
|
-
function remember(peer:
|
|
255
|
+
function remember(peer: RelayPeer) {
|
|
373
256
|
peer.socket.remember?.({
|
|
374
257
|
peerId: peer.id,
|
|
375
258
|
authorization: peer.authorization,
|
|
@@ -378,7 +261,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
378
261
|
}
|
|
379
262
|
|
|
380
263
|
/** Handles a room message from an admitted peer; a promise while the room's document loads. */
|
|
381
|
-
function deliver(peer:
|
|
264
|
+
function deliver(peer: RelayPeer, frame: RelayRoomFrame): Promise<void> | undefined {
|
|
382
265
|
const room = rooms.get(peer.authorization.roomId)
|
|
383
266
|
if (!room) {
|
|
384
267
|
peer.socket.close(RELAY_CLOSE.unauthorized, 'Expected a collaboration ticket')
|
|
@@ -407,7 +290,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
407
290
|
async function admit(
|
|
408
291
|
socket: RelaySocket,
|
|
409
292
|
authorization: RelayAuthorization
|
|
410
|
-
): Promise<
|
|
293
|
+
): Promise<RelayPeer | null> {
|
|
411
294
|
let room: Room
|
|
412
295
|
let limit: number | null | undefined
|
|
413
296
|
let epoch: number | null | undefined
|
|
@@ -430,7 +313,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
430
313
|
if (room.peers.size === 0) await leaveRoom(room)
|
|
431
314
|
return null
|
|
432
315
|
}
|
|
433
|
-
const peer:
|
|
316
|
+
const peer: RelayPeer = {
|
|
434
317
|
id: crypto.randomUUID(),
|
|
435
318
|
socket,
|
|
436
319
|
authorization,
|
|
@@ -482,7 +365,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
482
365
|
}
|
|
483
366
|
}
|
|
484
367
|
|
|
485
|
-
function depart(peer:
|
|
368
|
+
function depart(peer: RelayPeer) {
|
|
486
369
|
const room = rooms.get(peer.authorization.roomId)
|
|
487
370
|
if (!room?.peers.delete(peer.id)) return
|
|
488
371
|
sendAll(deliverable(room.peers.values()), { type: 'leave', peerId: peer.id })
|
|
@@ -524,7 +407,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
524
407
|
room = newRoom(authorization.roomId, identity)
|
|
525
408
|
rooms.set(room.id, room)
|
|
526
409
|
}
|
|
527
|
-
const peer:
|
|
410
|
+
const peer: RelayPeer = {
|
|
528
411
|
id: saved.peerId,
|
|
529
412
|
socket,
|
|
530
413
|
authorization,
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COLLAB_ACTIONS,
|
|
3
|
+
decodeRoomUpdate,
|
|
4
|
+
encodeSyncReply,
|
|
5
|
+
RELAY_CLOSE,
|
|
6
|
+
RELAY_SERVER_PEER,
|
|
7
|
+
relayServerMessages,
|
|
8
|
+
type RelayServerFrame
|
|
9
|
+
} from '#cloud/contract'
|
|
10
|
+
import * as Y from 'yjs'
|
|
11
|
+
|
|
12
|
+
import type { RelayPeer, RelaySocket } from './connection'
|
|
13
|
+
import type { RoomLog } from './log'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A room's document and the log its saves go to, loaded when a message first needs them, with an
|
|
17
|
+
* upper bound on the document's size: what was saved plus every update since. The bound is
|
|
18
|
+
* measured again only once it reaches `maximumRoomBytes` after something was deleted, since only
|
|
19
|
+
* a deletion can leave the document smaller than the bound.
|
|
20
|
+
*/
|
|
21
|
+
export type RoomContent = {
|
|
22
|
+
document: Y.Doc
|
|
23
|
+
log: RoomLog
|
|
24
|
+
bytes: number
|
|
25
|
+
deletedSinceMeasure: boolean
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type Room = {
|
|
29
|
+
id: string
|
|
30
|
+
identity: { documentId: string; roomEpoch: number }
|
|
31
|
+
peers: Map<string, RelayPeer>
|
|
32
|
+
persistTimer: ReturnType<typeof setTimeout> | null
|
|
33
|
+
/**
|
|
34
|
+
* Null until a message needs the document: peers resumed after the runtime woke the relay
|
|
35
|
+
* exchange presence without it, so a wake for presence alone loads nothing from the store.
|
|
36
|
+
*/
|
|
37
|
+
content: RoomContent | null
|
|
38
|
+
loading: Promise<RoomContent> | null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A room with no peers whose document has not been loaded. */
|
|
42
|
+
export function newRoom(id: string, identity: Room['identity']): Room {
|
|
43
|
+
return { id, identity, peers: new Map(), persistTimer: null, content: null, loading: null }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Sends a frame to one socket, split into fragments as the relay sends every frame. */
|
|
47
|
+
export function send(socket: RelaySocket, frame: RelayServerFrame) {
|
|
48
|
+
for (const message of relayServerMessages(frame)) socket.send(message)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Sends one frame to every socket, encoding it once. */
|
|
52
|
+
export function sendAll(sockets: Iterable<RelaySocket>, frame: RelayServerFrame) {
|
|
53
|
+
const messages = relayServerMessages(frame)
|
|
54
|
+
for (const socket of sockets) for (const message of messages) socket.send(message)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Whether a room message carries a document update the room applies. */
|
|
58
|
+
export const updatesDocument = (namespace: string) =>
|
|
59
|
+
namespace === COLLAB_ACTIONS.yjsUpdate || namespace === COLLAB_ACTIONS.syncReply
|
|
60
|
+
|
|
61
|
+
/** Whether handling a message reads or writes the room's document. */
|
|
62
|
+
export function needsDocument(namespace: string, target: string | null): boolean {
|
|
63
|
+
if (updatesDocument(namespace)) return true
|
|
64
|
+
return target === RELAY_SERVER_PEER && namespace === COLLAB_ACTIONS.syncStep1
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Whether `update` adds nothing and deletes only content `document` already holds. */
|
|
68
|
+
function deletesOnlyKnown(document: Y.Doc, update: Uint8Array): boolean {
|
|
69
|
+
const { from, to } = Y.parseUpdateMetaV2(update)
|
|
70
|
+
if (![...to].every(([client, clock]) => from.get(client) === clock)) return false
|
|
71
|
+
const { ds } = Y.decodeUpdateV2(update)
|
|
72
|
+
for (const [client, deletions] of ds.clients) {
|
|
73
|
+
const held = Y.getState(document.store, client)
|
|
74
|
+
if (deletions.some((deletion) => deletion.clock + deletion.len > held)) return false
|
|
75
|
+
}
|
|
76
|
+
return true
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Whether the room's document stays within `limit` after `update`. An update that only deletes
|
|
81
|
+
* what the room holds shrinks it, so it always fits; one that deletes what the room has not
|
|
82
|
+
* received waits in the document, so it counts like any other. Otherwise the bound grows by the
|
|
83
|
+
* update; past the limit, the document is measured only if a deletion may have shrunk it since
|
|
84
|
+
* the last measure, so edits near the limit never encode the whole document each time.
|
|
85
|
+
*/
|
|
86
|
+
function fits(content: RoomContent, update: Uint8Array, limit: number): boolean {
|
|
87
|
+
if (deletesOnlyKnown(content.document, update)) return true
|
|
88
|
+
if (content.bytes + update.byteLength <= limit) return true
|
|
89
|
+
if (!content.deletedSinceMeasure) return false
|
|
90
|
+
content.bytes = Y.encodeStateAsUpdateV2(content.document).byteLength
|
|
91
|
+
content.deletedSinceMeasure = false
|
|
92
|
+
return content.bytes + update.byteLength <= limit
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Applies a peer's V2 update to the room, inflated first for a sync reply, closing the peer's
|
|
97
|
+
* socket when it is malformed or would grow the document past `limit`. Rooms are saved as V1
|
|
98
|
+
* updates; only what peers exchange is V2.
|
|
99
|
+
*/
|
|
100
|
+
export function applyToRoom(
|
|
101
|
+
content: RoomContent,
|
|
102
|
+
peer: RelayPeer,
|
|
103
|
+
namespace: string,
|
|
104
|
+
payload: Uint8Array,
|
|
105
|
+
limit: number
|
|
106
|
+
): boolean {
|
|
107
|
+
const { document } = content
|
|
108
|
+
try {
|
|
109
|
+
const update = decodeRoomUpdate(payload, {
|
|
110
|
+
limit,
|
|
111
|
+
compressed: namespace === COLLAB_ACTIONS.syncReply
|
|
112
|
+
})
|
|
113
|
+
if (!update || !fits(content, update, limit)) {
|
|
114
|
+
peer.socket.close(RELAY_CLOSE.messageTooLarge, 'Collaboration update is too large')
|
|
115
|
+
return false
|
|
116
|
+
}
|
|
117
|
+
Y.applyUpdateV2(document, update, peer.id)
|
|
118
|
+
content.bytes += update.byteLength
|
|
119
|
+
return true
|
|
120
|
+
} catch {
|
|
121
|
+
peer.socket.close(RELAY_CLOSE.protocol, 'Malformed document update')
|
|
122
|
+
return false
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Handles a message addressed to the relay's own peer: a sync request is answered from the
|
|
128
|
+
* room's document, and a sync reply is applied to it.
|
|
129
|
+
*/
|
|
130
|
+
export function answerAsServer(
|
|
131
|
+
content: RoomContent,
|
|
132
|
+
peer: RelayPeer,
|
|
133
|
+
namespace: string,
|
|
134
|
+
payload: Uint8Array,
|
|
135
|
+
limit: number
|
|
136
|
+
) {
|
|
137
|
+
if (namespace === COLLAB_ACTIONS.syncStep1) {
|
|
138
|
+
let reply: Uint8Array
|
|
139
|
+
try {
|
|
140
|
+
reply = encodeSyncReply(content.document, payload)
|
|
141
|
+
} catch {
|
|
142
|
+
peer.socket.close(RELAY_CLOSE.protocol, 'Malformed state vector')
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
send(peer.socket, {
|
|
146
|
+
type: 'message',
|
|
147
|
+
namespace: COLLAB_ACTIONS.syncReply,
|
|
148
|
+
from: RELAY_SERVER_PEER,
|
|
149
|
+
payload: reply
|
|
150
|
+
})
|
|
151
|
+
} else if (namespace === COLLAB_ACTIONS.syncReply) {
|
|
152
|
+
applyToRoom(content, peer, namespace, payload, limit)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -11,6 +11,7 @@ import { DocumentNotFoundError } from '#cloud/server/documents'
|
|
|
11
11
|
import { resolveDocumentAccess } from '#cloud/server/documents/access'
|
|
12
12
|
import { CLOUD_FEATURE_KEYS } from '#cloud/server/policy/keys'
|
|
13
13
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
14
|
+
import { storageLimitReached } from '#cloud/server/policy/storage'
|
|
14
15
|
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
15
16
|
import type { DocumentSharingService } from '#cloud/server/sharing'
|
|
16
17
|
import { base64url, SignJWT } from 'jose'
|
|
@@ -40,6 +41,8 @@ export type CollaborationTicketClaims = {
|
|
|
40
41
|
documentId: string
|
|
41
42
|
principal: CollaborationPrincipal
|
|
42
43
|
permission: DocumentPermission
|
|
44
|
+
/** Why a peer who could edit only views, which the editor explains; never part of the token. */
|
|
45
|
+
readOnly?: 'storage-limit'
|
|
43
46
|
roomEpoch: number
|
|
44
47
|
/**
|
|
45
48
|
* The relay that enforces writes; without one the ticket is for peer-to-peer rooms. The ticket's
|
|
@@ -54,6 +57,7 @@ export async function signCollaborationTicket({
|
|
|
54
57
|
documentId,
|
|
55
58
|
principal,
|
|
56
59
|
permission,
|
|
60
|
+
readOnly,
|
|
57
61
|
roomEpoch,
|
|
58
62
|
relayURL
|
|
59
63
|
}: CollaborationTicketClaims): Promise<CollaborationTicket> {
|
|
@@ -78,6 +82,7 @@ export async function signCollaborationTicket({
|
|
|
78
82
|
const ticket = {
|
|
79
83
|
token,
|
|
80
84
|
provider: 'trystero' as const,
|
|
85
|
+
...(readOnly && { readOnly }),
|
|
81
86
|
...claims,
|
|
82
87
|
roomKey,
|
|
83
88
|
expiresAt: new Date(expiresAtSeconds * 1000).toISOString(),
|
|
@@ -110,6 +115,20 @@ export function createCollaborationTicketService({
|
|
|
110
115
|
policy,
|
|
111
116
|
deploymentMode = 'self-hosted'
|
|
112
117
|
}: CollaborationTicketServiceOptions) {
|
|
118
|
+
/**
|
|
119
|
+
* The permission a ticket carries: an owner at their storage limit leaves their documents
|
|
120
|
+
* read-only, so whoever could edit them views them instead, and the ticket says why.
|
|
121
|
+
*/
|
|
122
|
+
async function ticketPermission(documentId: string, permission: DocumentPermission) {
|
|
123
|
+
if (
|
|
124
|
+
permission === 'edit' &&
|
|
125
|
+
(await storageLimitReached(database, policy, { documentId }, deploymentMode))
|
|
126
|
+
) {
|
|
127
|
+
return { permission: 'view' as const, readOnly: 'storage-limit' as const }
|
|
128
|
+
}
|
|
129
|
+
return { permission }
|
|
130
|
+
}
|
|
131
|
+
|
|
113
132
|
return {
|
|
114
133
|
async issueUserTicket(actor: CloudActor, documentId: string): Promise<CollaborationTicket> {
|
|
115
134
|
const access = await resolveDocumentAccess(database, actor.userId, documentId)
|
|
@@ -136,7 +155,7 @@ export function createCollaborationTicketService({
|
|
|
136
155
|
authSecret,
|
|
137
156
|
documentId,
|
|
138
157
|
principal: { kind: 'user', userId: actor.userId, name: actor.name, email: actor.email },
|
|
139
|
-
|
|
158
|
+
...(await ticketPermission(documentId, access.permission)),
|
|
140
159
|
roomEpoch: document.collaborationEpoch,
|
|
141
160
|
relayURL
|
|
142
161
|
})
|
|
@@ -173,7 +192,7 @@ export function createCollaborationTicketService({
|
|
|
173
192
|
authSecret,
|
|
174
193
|
documentId: resolved.documentId,
|
|
175
194
|
principal: resolved.principal,
|
|
176
|
-
|
|
195
|
+
...(await ticketPermission(resolved.documentId, resolved.permission)),
|
|
177
196
|
roomEpoch: document.collaborationEpoch,
|
|
178
197
|
relayURL
|
|
179
198
|
})
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COMMUNITY_THUMBNAIL_PATH,
|
|
3
|
+
parseCommunityDownloadQuery,
|
|
4
|
+
parseRequestImageLinks
|
|
5
|
+
} from '#cloud/contract'
|
|
6
|
+
import { documentDomainError } from '#cloud/server/documents'
|
|
7
|
+
import {
|
|
8
|
+
communityThumbnailParams,
|
|
9
|
+
documentParams,
|
|
10
|
+
validatedJSON,
|
|
11
|
+
validatedQuery
|
|
12
|
+
} from '#cloud/server/validation'
|
|
13
|
+
import { Hono, type Context } from 'hono'
|
|
14
|
+
|
|
15
|
+
import type { CommunityService } from './service'
|
|
16
|
+
|
|
17
|
+
const notFound = (context: Context) => context.json({ error: { code: 'not_found' as const } }, 404)
|
|
18
|
+
|
|
19
|
+
/** Answers a missing or hidden gallery document as not found, like any document outside it. */
|
|
20
|
+
async function communityRoute(context: Context, action: () => Promise<Response>) {
|
|
21
|
+
try {
|
|
22
|
+
return await action()
|
|
23
|
+
} catch (error) {
|
|
24
|
+
const response = documentDomainError(context, error)
|
|
25
|
+
if (response) return response
|
|
26
|
+
throw error
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The community gallery's data for anyone, signed in or not: the list, a document or one of its
|
|
32
|
+
* named versions to open read-only, and the links of its images. A deployment without a gallery
|
|
33
|
+
* answers not found.
|
|
34
|
+
*/
|
|
35
|
+
export function createCommunityRoutes(community: CommunityService) {
|
|
36
|
+
return new Hono()
|
|
37
|
+
.get('/community', async (context) => {
|
|
38
|
+
if (!community.enabled) return notFound(context)
|
|
39
|
+
// The gallery changes on a publish; a few minutes' copy spares the database repeat visits.
|
|
40
|
+
context.header('Cache-Control', 'public, max-age=300')
|
|
41
|
+
return context.json({ documents: await community.list() })
|
|
42
|
+
})
|
|
43
|
+
.get(
|
|
44
|
+
'/community/:documentId/document',
|
|
45
|
+
documentParams,
|
|
46
|
+
validatedQuery(parseCommunityDownloadQuery),
|
|
47
|
+
(context) =>
|
|
48
|
+
communityRoute(context, async () => {
|
|
49
|
+
const { documentId } = context.req.valid('param')
|
|
50
|
+
return context.json(
|
|
51
|
+
await community.download(documentId, context.req.valid('query').version)
|
|
52
|
+
)
|
|
53
|
+
})
|
|
54
|
+
)
|
|
55
|
+
.post(
|
|
56
|
+
'/community/:documentId/images',
|
|
57
|
+
documentParams,
|
|
58
|
+
validatedJSON(parseRequestImageLinks),
|
|
59
|
+
(context) =>
|
|
60
|
+
communityRoute(context, async () =>
|
|
61
|
+
context.json(
|
|
62
|
+
await community.imageLinks(
|
|
63
|
+
context.req.valid('param').documentId,
|
|
64
|
+
context.req.valid('json').checksums
|
|
65
|
+
)
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Gallery thumbnails, for pages and apps on any origin. Each address names the revision it shows,
|
|
73
|
+
* so a copy never goes stale, but it is cached only as long as the listing is: a document taken
|
|
74
|
+
* down or out of the gallery stops showing everywhere within minutes.
|
|
75
|
+
*/
|
|
76
|
+
export function createCommunityThumbnailRoutes(community: CommunityService) {
|
|
77
|
+
return new Hono().get(
|
|
78
|
+
`${COMMUNITY_THUMBNAIL_PATH}/:documentId/:revisionId`,
|
|
79
|
+
communityThumbnailParams,
|
|
80
|
+
(context) =>
|
|
81
|
+
communityRoute(context, async () => {
|
|
82
|
+
const { documentId, revisionId } = context.req.valid('param')
|
|
83
|
+
const thumbnail = await community.thumbnail(documentId, revisionId)
|
|
84
|
+
if (!thumbnail) return notFound(context)
|
|
85
|
+
context.header('Cache-Control', 'public, max-age=300')
|
|
86
|
+
context.header('Content-Type', 'image/png')
|
|
87
|
+
context.header('X-Content-Type-Options', 'nosniff')
|
|
88
|
+
context.header('Content-Security-Policy', "default-src 'none'; sandbox")
|
|
89
|
+
context.header('Cross-Origin-Resource-Policy', 'cross-origin')
|
|
90
|
+
return context.body(new Uint8Array(thumbnail))
|
|
91
|
+
})
|
|
92
|
+
)
|
|
93
|
+
}
|