@open-pencil/cloud 0.17.0-beta.4 → 0.17.0-beta.6
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 +783 -47
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1651 -382
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +701 -45
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +2138 -34
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +4343 -306
- package/dist/email.js.map +1 -1
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +61 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +1338 -44
- package/dist/index.d.ts.map +1 -1
- package/dist/index2.d.ts +45 -0
- package/dist/index2.d.ts.map +1 -0
- package/dist/listen.mjs +958 -340
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-cloudflare.d.ts +93 -0
- package/dist/runtime-cloudflare.d.ts.map +1 -0
- package/dist/runtime-cloudflare.js +18032 -0
- package/dist/runtime-cloudflare.js.map +1 -0
- package/dist/runtime-node.d.mts +12615 -5249
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +17367 -8567
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +13587 -7333
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +11601 -5401
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +84 -62
- package/dist/server2.mjs.map +1 -1
- package/package.json +31 -5
- package/src/admin/api/routes.ts +263 -190
- package/src/admin/audit/service.ts +11 -3
- package/src/admin/bootstrap.ts +86 -0
- package/src/admin/documents/service.ts +195 -10
- package/src/admin/email/service.ts +2 -1
- package/src/admin/enrollment/email.ts +17 -3
- package/src/admin/enrollment/service.ts +71 -10
- package/src/admin/errors.ts +5 -0
- package/src/admin/index.ts +1 -0
- package/src/admin/reports/service.ts +15 -3
- package/src/admin/schema.ts +6 -5
- package/src/admin/users/service.ts +173 -16
- package/src/client/api.ts +52 -473
- package/src/client/auth/client.ts +22 -3
- package/src/client/comments.ts +205 -0
- package/src/client/community.ts +40 -0
- package/src/client/device-authorization.ts +4 -20
- package/src/client/discovery.ts +39 -11
- package/src/client/documents.ts +160 -0
- package/src/client/files/archive.ts +60 -0
- package/src/client/files/checksum.ts +7 -0
- package/src/client/files/download.ts +122 -0
- package/src/client/files/images.ts +89 -0
- package/src/client/files/index.ts +15 -0
- package/src/client/files/objects.ts +173 -0
- package/src/client/files/save.ts +79 -0
- package/src/client/index.ts +4 -0
- package/src/client/portal/account.ts +73 -3
- package/src/client/portal/administration.ts +52 -5
- package/src/client/portal/request.ts +6 -2
- 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/tour.ts +44 -0
- package/src/client/versions.ts +110 -0
- package/src/contract/account.ts +111 -11
- package/src/contract/admin/index.ts +48 -3
- package/src/contract/billing.ts +235 -0
- package/src/contract/collaboration.ts +5 -0
- package/src/contract/comments.ts +223 -0
- package/src/contract/community.ts +60 -0
- package/src/contract/discovery.ts +94 -5
- package/src/contract/entitlements.ts +10 -0
- package/src/contract/index.ts +64 -5
- package/src/contract/links.ts +62 -4
- package/src/contract/locale.ts +27 -0
- package/src/contract/portal.ts +12 -1
- package/src/contract/relay.ts +3 -1
- package/src/contract/routes.ts +10 -0
- package/src/contract/services.ts +62 -20
- package/src/contract/workspaces.ts +5 -0
- package/src/email/i18n.ts +174 -0
- package/src/email/index.ts +13 -2
- package/src/email/locales/ar.json +169 -0
- package/src/email/locales/de.json +169 -0
- package/src/email/locales/es.json +169 -0
- package/src/email/locales/fr.json +169 -0
- package/src/email/locales/it.json +169 -0
- package/src/email/locales/ja.json +169 -0
- package/src/email/locales/pl.json +169 -0
- package/src/email/locales/pt-br.json +169 -0
- package/src/email/locales/ru.json +169 -0
- package/src/email/locales/zh-cn.json +169 -0
- package/src/email/messages.ts +235 -0
- package/src/email/render.ts +160 -276
- package/src/email/samples.ts +142 -0
- package/src/email/schema.ts +98 -8
- package/src/email/templates/BrandEmail.vue +56 -0
- package/src/email/templates/CommentNotification.vue +39 -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/EnrollmentInvitation.vue +53 -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 +47 -0
- package/src/email/templates/WaitlistConfirmation.vue +29 -0
- package/src/email/templates/WorkspaceInvitation.vue +28 -0
- package/src/email/templates/brand.ts +15 -61
- package/src/email/templates/localization.ts +17 -0
- package/src/errors/index.ts +6 -0
- package/src/{server/errors → errors}/posthog.ts +28 -4
- package/src/global.d.ts +7 -0
- package/src/runtime/cloudflare/email.ts +2 -0
- package/src/runtime/cloudflare/relay/index.ts +28 -7
- package/src/runtime/cloudflare/tsconfig.json +2 -1
- package/src/runtime/cloudflare/worker.ts +14 -1
- package/src/runtime/cloudflare/xml.ts +12 -0
- package/src/runtime/node/admin.ts +51 -47
- package/src/runtime/node/cleanup.ts +5 -1
- package/src/runtime/node/email/runtime.ts +1 -0
- package/src/runtime/node/email/transport.ts +14 -10
- package/src/runtime/node/main.ts +1 -1
- package/src/runtime/node/server.ts +20 -4
- package/src/runtime/node/services/main.ts +1 -1
- package/src/runtime/posthog/errors.ts +5 -2
- 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/actions.ts +3 -1
- package/src/server/account/deletion.ts +40 -4
- package/src/server/account/routes.ts +305 -253
- package/src/server/account/tokens.ts +57 -0
- package/src/server/api/router.ts +44 -6
- package/src/server/app/checks.ts +89 -0
- package/src/server/app/cors.ts +44 -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 +123 -0
- package/src/server/app/services.ts +219 -0
- package/src/server/auth/access-control.ts +13 -8
- package/src/server/auth/access-tokens.ts +62 -0
- package/src/server/auth/account.ts +13 -12
- package/src/server/auth/activity.ts +7 -2
- package/src/server/auth/adapter.ts +36 -3
- package/src/server/auth/email.ts +33 -18
- package/src/server/auth/endpoints.ts +4 -0
- package/src/server/auth/enrollment.ts +116 -0
- package/src/server/auth/factory.ts +165 -189
- package/src/server/auth/index.ts +1 -1
- package/src/server/auth/logger.ts +23 -0
- package/src/server/auth/methods.ts +162 -0
- package/src/server/auth/mfa-assurance.ts +190 -2
- package/src/server/auth/passwords.ts +43 -0
- package/src/server/auth/rate-limit.ts +31 -0
- package/src/server/auth/runtime.ts +19 -5
- package/src/server/auth/session.ts +2 -0
- package/src/server/auth/social-providers.ts +70 -24
- package/src/server/billing/index.ts +8 -0
- package/src/server/billing/notify.ts +55 -0
- package/src/server/billing/routes.ts +27 -0
- package/src/server/billing/seats.ts +17 -0
- package/src/server/billing/tokens.ts +37 -0
- package/src/server/cleanup/configured.ts +16 -3
- package/src/server/cleanup/documents.ts +5 -2
- package/src/server/cleanup/revisions.ts +6 -3
- package/src/server/cleanup/uploads.ts +2 -1
- package/src/server/cleanup/worker.ts +10 -1
- package/src/server/collaboration/relay/connection.ts +7 -2
- package/src/server/collaboration/relay/hub.ts +80 -166
- package/src/server/collaboration/relay/room.ts +154 -0
- package/src/server/collaboration/routes.ts +8 -5
- package/src/server/collaboration/service.ts +21 -2
- package/src/server/comments/index.ts +9 -0
- package/src/server/comments/notify.ts +277 -0
- package/src/server/comments/routes.ts +352 -0
- package/src/server/comments/rows.ts +249 -0
- package/src/server/comments/service.ts +570 -0
- 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 +35 -10
- package/src/server/config/index.ts +2 -2
- package/src/server/config/public.ts +25 -6
- package/src/server/config/schema.ts +53 -293
- package/src/server/config/toml/schema.ts +330 -0
- package/src/server/db/index.ts +3 -1
- package/src/server/db/migrate.ts +33 -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/015_comments.ts +57 -0
- package/src/server/db/migrations/016_comment_notifications.ts +76 -0
- package/src/server/db/migrations/017_invitation_emails.ts +19 -0
- package/src/server/db/migrations/018_enrollment_source.ts +17 -0
- package/src/server/db/migrations/schema.ts +26 -1
- package/src/server/db/tables.ts +122 -3
- package/src/server/db/users.ts +22 -0
- package/src/server/documents/access.ts +17 -0
- package/src/server/documents/images.ts +6 -1
- package/src/server/documents/index.ts +1 -1
- package/src/server/documents/limits.ts +7 -1
- package/src/server/documents/routes.ts +12 -12
- package/src/server/documents/service.ts +31 -407
- package/src/server/documents/summary.ts +15 -0
- package/src/server/documents/uploads.ts +450 -0
- package/src/server/documents/versions/routes.ts +7 -8
- package/src/server/documents/versions/service.ts +86 -26
- package/src/server/email/invitations.ts +1 -0
- package/src/server/email/service.ts +39 -6
- package/src/server/email/types.ts +13 -0
- package/src/server/errors/index.ts +49 -12
- package/src/server/index.ts +27 -6
- package/src/server/internal/routes.ts +8 -2
- package/src/server/internal/service.ts +10 -0
- package/src/server/invitations/budget.ts +3 -1
- package/src/server/invitations/index.ts +1 -0
- package/src/server/invitations/links.ts +6 -0
- package/src/server/invitations/types.ts +5 -1
- package/src/server/objects/types.ts +5 -0
- package/src/server/pictures/service.ts +3 -1
- 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/policy/workspace.ts +5 -2
- 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 +5 -3
- package/src/server/reports/service.ts +27 -3
- package/src/server/services/routes.ts +2 -2
- package/src/server/sharing/capabilities/service.ts +3 -4
- package/src/server/sharing/document-routes.ts +3 -3
- package/src/server/sharing/invitations/service.ts +17 -8
- package/src/server/sharing/routes.ts +15 -15
- package/src/server/validation.ts +35 -24
- package/src/server/workspaces/members.ts +57 -31
- package/src/server/workspaces/repository.ts +23 -12
- package/src/server/workspaces/routes.ts +8 -8
- package/src/services/app.ts +31 -22
- package/src/services/config.ts +48 -8
- package/src/services/metering.ts +15 -27
- 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
|
@@ -1,16 +1,15 @@
|
|
|
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'
|
|
12
9
|
import * as Y from 'yjs'
|
|
13
10
|
|
|
11
|
+
import { DEADLINE_MS, SECOND_MS } from '@open-pencil/std/timing'
|
|
12
|
+
|
|
14
13
|
import type { RelayAuthorization } from './authorize'
|
|
15
14
|
import { stampAwareness } from './awareness'
|
|
16
15
|
import {
|
|
@@ -21,12 +20,41 @@ import {
|
|
|
21
20
|
type RelayRoomFrame,
|
|
22
21
|
type RelaySocket
|
|
23
22
|
} from './connection'
|
|
24
|
-
import { openRoomLog
|
|
23
|
+
import { openRoomLog } from './log'
|
|
25
24
|
import type { CollaborationStateStore } from './persistence'
|
|
26
25
|
import type { RelayPeerState } from './resume'
|
|
26
|
+
import {
|
|
27
|
+
answerAsServer,
|
|
28
|
+
applyToRoom,
|
|
29
|
+
needsDocument,
|
|
30
|
+
newRoom,
|
|
31
|
+
send,
|
|
32
|
+
sendAll,
|
|
33
|
+
updatesDocument,
|
|
34
|
+
type Room,
|
|
35
|
+
type RoomContent
|
|
36
|
+
} from './room'
|
|
37
|
+
|
|
38
|
+
/** How long a room waits after an edit before saving, so a burst of edits saves once. */
|
|
39
|
+
const PERSIST_DELAY_MS = 2 * SECOND_MS
|
|
40
|
+
/** How long a room waits to save again after a save failed. */
|
|
41
|
+
const PERSIST_RETRY_MS = 30 * SECOND_MS
|
|
42
|
+
/** How long a new socket has to present its ticket before the room closes it. */
|
|
43
|
+
const AUTH_TIMEOUT_MS = DEADLINE_MS.request
|
|
27
44
|
|
|
28
45
|
export type { RelayConnection, RelaySocket } from './connection'
|
|
29
46
|
|
|
47
|
+
/**
|
|
48
|
+
* What a relay failure names: the room's document, and the user whose message met it. A peer
|
|
49
|
+
* that failed to be admitted names only the document a single-document relay serves.
|
|
50
|
+
*/
|
|
51
|
+
export type RelayErrorIds = { documentId?: string; userId?: string }
|
|
52
|
+
|
|
53
|
+
/** A verified ticket's document, and its person when they signed in rather than came as a guest. */
|
|
54
|
+
function ticketIds({ documentId, principal }: RelayAuthorization): RelayErrorIds {
|
|
55
|
+
return principal.kind === 'user' ? { documentId, userId: principal.userId } : { documentId }
|
|
56
|
+
}
|
|
57
|
+
|
|
30
58
|
export type CollaborationRelayOptions = {
|
|
31
59
|
authSecret: string
|
|
32
60
|
store: CollaborationStateStore
|
|
@@ -65,49 +93,15 @@ export type CollaborationRelayOptions = {
|
|
|
65
93
|
persistRetryMs?: number
|
|
66
94
|
/** How long a socket may stay open without presenting a ticket. */
|
|
67
95
|
authTimeoutMs?: number
|
|
68
|
-
|
|
96
|
+
/** Told of each failure, with the document and, when one asked, the signed-in user. */
|
|
97
|
+
onError?: (message: string, error: unknown, ids?: RelayErrorIds) => void
|
|
69
98
|
}
|
|
70
99
|
|
|
71
100
|
/** Messages a viewer may send; their document writes never leave the relay. */
|
|
72
101
|
const VIEWER_ACTIONS = new Set<string>([COLLAB_ACTIONS.syncStep1, COLLAB_ACTIONS.awareness])
|
|
73
102
|
|
|
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
103
|
export type CollaborationRelay = ReturnType<typeof createCollaborationRelay>
|
|
98
104
|
|
|
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
105
|
/**
|
|
112
106
|
* A runtime-neutral relay for collaboration rooms. It forwards room messages between peers and
|
|
113
107
|
* joins every room as a peer itself: it holds the room's document, answers sync requests from
|
|
@@ -120,8 +114,8 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
120
114
|
const closing = new Map<string, Promise<void>>()
|
|
121
115
|
/** Set by `close()`: a failed save then drops the room instead of retrying. */
|
|
122
116
|
let shuttingDown = false
|
|
123
|
-
const persistDelayMs = options.persistDelayMs ??
|
|
124
|
-
const persistRetryMs = options.persistRetryMs ??
|
|
117
|
+
const persistDelayMs = options.persistDelayMs ?? PERSIST_DELAY_MS
|
|
118
|
+
const persistRetryMs = options.persistRetryMs ?? PERSIST_RETRY_MS
|
|
125
119
|
const logLimits = {
|
|
126
120
|
updates: options.compactAfterUpdates ?? 500,
|
|
127
121
|
bytes: options.compactAfterBytes ?? 4 * 1024 * 1024
|
|
@@ -130,19 +124,11 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
130
124
|
options.maximumRoomBytes ?? MAX_COLLAB_MESSAGE_BYTES,
|
|
131
125
|
MAX_COLLAB_MESSAGE_BYTES
|
|
132
126
|
)
|
|
133
|
-
const authTimeoutMs = options.authTimeoutMs ??
|
|
127
|
+
const authTimeoutMs = options.authTimeoutMs ?? AUTH_TIMEOUT_MS
|
|
134
128
|
const report =
|
|
135
129
|
options.onError ??
|
|
136
|
-
((message, error
|
|
137
|
-
|
|
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
|
-
}
|
|
130
|
+
((message, error, ids) =>
|
|
131
|
+
logCloud('error', message, { source: 'relay', ...ids, error: loggedError(error) }))
|
|
146
132
|
|
|
147
133
|
async function openRoom(authorization: RelayAuthorization): Promise<Room> {
|
|
148
134
|
const existing = rooms.get(authorization.roomId)
|
|
@@ -169,10 +155,6 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
169
155
|
}
|
|
170
156
|
}
|
|
171
157
|
|
|
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
158
|
/** The room's document and log, loaded from the store the first time something needs them. */
|
|
177
159
|
function contentOf(room: Room): Promise<RoomContent> {
|
|
178
160
|
if (room.content) return Promise.resolve(room.content)
|
|
@@ -208,7 +190,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
208
190
|
await log.save({ closing })
|
|
209
191
|
return true
|
|
210
192
|
} catch (error) {
|
|
211
|
-
report('Saving a collaboration room failed', error)
|
|
193
|
+
report('Saving a collaboration room failed', error, { documentId: room.identity.documentId })
|
|
212
194
|
// The room holds edits the store doesn't have; save again without waiting for another edit.
|
|
213
195
|
// A closing room retries through `saveAndClose`.
|
|
214
196
|
if (!closing && rooms.get(room.id) === room) {
|
|
@@ -222,38 +204,9 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
222
204
|
room.persistTimer ??= setTimeout(() => void persist(room), persistDelayMs)
|
|
223
205
|
}
|
|
224
206
|
|
|
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
207
|
function forward(
|
|
255
208
|
room: Room,
|
|
256
|
-
from:
|
|
209
|
+
from: RelayPeer,
|
|
257
210
|
namespace: string,
|
|
258
211
|
target: string | null,
|
|
259
212
|
payload: Uint8Array
|
|
@@ -265,66 +218,18 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
265
218
|
sendAll(deliverable(recipients.flatMap((peer) => (peer ? [peer] : []))), frame)
|
|
266
219
|
}
|
|
267
220
|
|
|
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
221
|
function handleMessage(
|
|
317
222
|
room: Room,
|
|
318
223
|
content: RoomContent | null,
|
|
319
|
-
peer:
|
|
224
|
+
peer: RelayPeer,
|
|
320
225
|
namespace: string,
|
|
321
226
|
target: string | null,
|
|
322
227
|
payload: Uint8Array
|
|
323
228
|
) {
|
|
324
229
|
const readOnly = peer.authorization.permission === 'view'
|
|
325
230
|
if (readOnly && !VIEWER_ACTIONS.has(namespace)) return
|
|
326
|
-
// Only the server says the history changed.
|
|
327
|
-
if (namespace === COLLAB_ACTIONS.versions) return
|
|
231
|
+
// Only the server says the history or the comments changed.
|
|
232
|
+
if (namespace === COLLAB_ACTIONS.versions || namespace === COLLAB_ACTIONS.comments) return
|
|
328
233
|
|
|
329
234
|
let forwarded = payload
|
|
330
235
|
if (namespace === COLLAB_ACTIONS.awareness) {
|
|
@@ -341,12 +246,12 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
341
246
|
}
|
|
342
247
|
|
|
343
248
|
if (target === RELAY_SERVER_PEER) {
|
|
344
|
-
if (content)
|
|
249
|
+
if (content) answerAsServer(content, peer, namespace, payload, maximumRoomBytes)
|
|
345
250
|
return
|
|
346
251
|
}
|
|
347
252
|
if (
|
|
348
|
-
(namespace
|
|
349
|
-
!(content && applyToRoom(content, peer, namespace, payload))
|
|
253
|
+
updatesDocument(namespace) &&
|
|
254
|
+
!(content && applyToRoom(content, peer, namespace, payload, maximumRoomBytes))
|
|
350
255
|
) {
|
|
351
256
|
return
|
|
352
257
|
}
|
|
@@ -358,18 +263,31 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
358
263
|
* the relay hears from the peer and before it sends the peer anything, never by a timer, so a
|
|
359
264
|
* runtime can set an idle relay aside while sockets stay open.
|
|
360
265
|
*/
|
|
361
|
-
function current(peer:
|
|
266
|
+
function current(peer: RelayPeer): boolean {
|
|
362
267
|
if (Date.now() < peer.authorization.expiresAt) return true
|
|
363
268
|
peer.socket.close(RELAY_CLOSE.expired, 'Collaboration ticket expired')
|
|
364
269
|
return false
|
|
365
270
|
}
|
|
366
271
|
|
|
367
272
|
/** The sockets of peers whose tickets still hold. */
|
|
368
|
-
function deliverable(peers: Iterable<
|
|
273
|
+
function deliverable(peers: Iterable<RelayPeer>): RelaySocket[] {
|
|
369
274
|
return [...peers].filter(current).map((peer) => peer.socket)
|
|
370
275
|
}
|
|
371
276
|
|
|
372
|
-
|
|
277
|
+
/** Sends a server message to every peer in the document's rooms. */
|
|
278
|
+
function announce(documentId: string, namespace: string, payload: Uint8Array) {
|
|
279
|
+
const frame: RelayServerFrame = { type: 'message', namespace, from: RELAY_SERVER_PEER, payload }
|
|
280
|
+
sendAll(
|
|
281
|
+
deliverable(
|
|
282
|
+
[...rooms.values()]
|
|
283
|
+
.filter((room) => room.identity.documentId === documentId)
|
|
284
|
+
.flatMap((room) => [...room.peers.values()])
|
|
285
|
+
),
|
|
286
|
+
frame
|
|
287
|
+
)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function remember(peer: RelayPeer) {
|
|
373
291
|
peer.socket.remember?.({
|
|
374
292
|
peerId: peer.id,
|
|
375
293
|
authorization: peer.authorization,
|
|
@@ -378,7 +296,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
378
296
|
}
|
|
379
297
|
|
|
380
298
|
/** Handles a room message from an admitted peer; a promise while the room's document loads. */
|
|
381
|
-
function deliver(peer:
|
|
299
|
+
function deliver(peer: RelayPeer, frame: RelayRoomFrame): Promise<void> | undefined {
|
|
382
300
|
const room = rooms.get(peer.authorization.roomId)
|
|
383
301
|
if (!room) {
|
|
384
302
|
peer.socket.close(RELAY_CLOSE.unauthorized, 'Expected a collaboration ticket')
|
|
@@ -398,7 +316,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
398
316
|
return undefined
|
|
399
317
|
},
|
|
400
318
|
(error: unknown) => {
|
|
401
|
-
report('Loading a collaboration room failed', error)
|
|
319
|
+
report('Loading a collaboration room failed', error, ticketIds(peer.authorization))
|
|
402
320
|
peer.socket.close(RELAY_CLOSE.protocol, 'Could not open the collaboration room')
|
|
403
321
|
}
|
|
404
322
|
)
|
|
@@ -407,7 +325,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
407
325
|
async function admit(
|
|
408
326
|
socket: RelaySocket,
|
|
409
327
|
authorization: RelayAuthorization
|
|
410
|
-
): Promise<
|
|
328
|
+
): Promise<RelayPeer | null> {
|
|
411
329
|
let room: Room
|
|
412
330
|
let limit: number | null | undefined
|
|
413
331
|
let epoch: number | null | undefined
|
|
@@ -430,7 +348,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
430
348
|
if (room.peers.size === 0) await leaveRoom(room)
|
|
431
349
|
return null
|
|
432
350
|
}
|
|
433
|
-
const peer:
|
|
351
|
+
const peer: RelayPeer = {
|
|
434
352
|
id: crypto.randomUUID(),
|
|
435
353
|
socket,
|
|
436
354
|
authorization,
|
|
@@ -482,7 +400,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
482
400
|
}
|
|
483
401
|
}
|
|
484
402
|
|
|
485
|
-
function depart(peer:
|
|
403
|
+
function depart(peer: RelayPeer) {
|
|
486
404
|
const room = rooms.get(peer.authorization.roomId)
|
|
487
405
|
if (!room?.peers.delete(peer.id)) return
|
|
488
406
|
sendAll(deliverable(room.peers.values()), { type: 'leave', peerId: peer.id })
|
|
@@ -503,7 +421,11 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
503
421
|
refreshed: remember,
|
|
504
422
|
deliver,
|
|
505
423
|
depart,
|
|
506
|
-
report
|
|
424
|
+
report: (message, error, authorization) => {
|
|
425
|
+
// Before a ticket verified, only the document a per-document relay serves is known.
|
|
426
|
+
const configured = options.documentId === undefined ? {} : { documentId: options.documentId }
|
|
427
|
+
report(message, error, authorization ? ticketIds(authorization) : configured)
|
|
428
|
+
}
|
|
507
429
|
}
|
|
508
430
|
|
|
509
431
|
return {
|
|
@@ -524,7 +446,7 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
524
446
|
room = newRoom(authorization.roomId, identity)
|
|
525
447
|
rooms.set(room.id, room)
|
|
526
448
|
}
|
|
527
|
-
const peer:
|
|
449
|
+
const peer: RelayPeer = {
|
|
528
450
|
id: saved.peerId,
|
|
529
451
|
socket,
|
|
530
452
|
authorization,
|
|
@@ -573,20 +495,12 @@ export function createCollaborationRelay(options: CollaborationRelayOptions) {
|
|
|
573
495
|
|
|
574
496
|
/** Tells everyone in a document's rooms that its version history changed. */
|
|
575
497
|
announceVersions(documentId: string) {
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
sendAll(
|
|
583
|
-
deliverable(
|
|
584
|
-
[...rooms.values()]
|
|
585
|
-
.filter((room) => room.identity.documentId === documentId)
|
|
586
|
-
.flatMap((room) => [...room.peers.values()])
|
|
587
|
-
),
|
|
588
|
-
frame
|
|
589
|
-
)
|
|
498
|
+
announce(documentId, COLLAB_ACTIONS.versions, new Uint8Array())
|
|
499
|
+
},
|
|
500
|
+
|
|
501
|
+
/** Tells everyone in a document's rooms that one of its comment threads changed. */
|
|
502
|
+
announceComments(documentId: string, threadId: string) {
|
|
503
|
+
announce(documentId, COLLAB_ACTIONS.comments, new TextEncoder().encode(threadId))
|
|
590
504
|
},
|
|
591
505
|
|
|
592
506
|
/** Saves every open room and closes its sockets, as the runtime shuts down. */
|
|
@@ -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
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { resolveDocumentShareSchema } from '#cloud/contract'
|
|
2
2
|
import type { CloudActor, CloudSessionResolver } from '#cloud/server/auth'
|
|
3
3
|
import type { CollaborationTicketService } from '#cloud/server/collaboration/service'
|
|
4
4
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
@@ -62,20 +62,23 @@ export function createCollaborationRoutes(
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
export function createPublicCollaborationRoutes(
|
|
65
|
-
service: CollaborationTicketService,
|
|
65
|
+
service: Pick<CollaborationTicketService, 'issueShareTicket'>,
|
|
66
66
|
resolveSession?: CloudSessionResolver
|
|
67
67
|
) {
|
|
68
|
-
return new Hono().post(
|
|
68
|
+
return new Hono<CollaborationRouteEnvironment>().post(
|
|
69
69
|
'/shares/:shareId/collaboration-ticket',
|
|
70
70
|
shareParams,
|
|
71
|
-
validatedJSON(
|
|
71
|
+
validatedJSON(resolveDocumentShareSchema),
|
|
72
72
|
async (context) => {
|
|
73
73
|
try {
|
|
74
|
+
const actor = resolveSession ? await resolveSession(context.req.raw) : null
|
|
75
|
+
// A signed-in caller is named in error reports, as on the routes that require one.
|
|
76
|
+
if (actor) context.set('actor', actor)
|
|
74
77
|
return context.json({
|
|
75
78
|
ticket: await service.issueShareTicket(
|
|
76
79
|
context.req.valid('param').shareId,
|
|
77
80
|
context.req.valid('json'),
|
|
78
|
-
|
|
81
|
+
actor ?? undefined
|
|
79
82
|
)
|
|
80
83
|
})
|
|
81
84
|
} catch (error) {
|
|
@@ -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,9 @@
|
|
|
1
|
+
export { createCommentRoutes, createSharedCommentRoutes } from './routes'
|
|
2
|
+
export {
|
|
3
|
+
CommentConflictError,
|
|
4
|
+
CommentLimitError,
|
|
5
|
+
createCommentService,
|
|
6
|
+
type CommentActor,
|
|
7
|
+
type CommentService
|
|
8
|
+
} from './service'
|
|
9
|
+
export { COMMENT_EMAIL_BUDGET, DEFAULT_COMMENT_EMAILS } from './notify'
|