@open-pencil/cloud 0.17.0-beta.1 → 0.17.0-beta.4
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 +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +236 -46
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -400
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +680 -443
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +9 -1
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +99 -58
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +468 -64
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +575 -738
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +6564 -2934
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +8931 -4198
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5260 -3859
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5686 -3736
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +9 -6
- package/src/admin/api/routes.ts +103 -33
- package/src/admin/audit/service.ts +25 -6
- package/src/admin/documents/service.ts +361 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +147 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +36 -21
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/index.ts +7 -7
- package/src/client/portal/account.ts +25 -0
- package/src/client/portal/administration.ts +56 -0
- package/src/client/{ai.ts → services.ts} +44 -27
- package/src/contract/account.ts +58 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/collaboration.ts +6 -1
- package/src/contract/discovery.ts +22 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +2 -2
- package/src/contract/index.ts +63 -37
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/services.ts +233 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/render.ts +26 -0
- package/src/email/schema.ts +11 -1
- package/src/email/templates/brand.ts +1 -1
- package/src/runtime/cloudflare/environment.ts +31 -6
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +176 -0
- package/src/runtime/cloudflare/relay/sockets.ts +112 -0
- package/src/runtime/cloudflare/worker.ts +89 -26
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +20 -4
- package/src/runtime/node/cleanup.ts +16 -3
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +71 -15
- package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +32 -0
- package/src/runtime/s3/objects.ts +50 -1
- package/src/runtime/site/server.ts +33 -4
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +302 -0
- package/src/server/account/routes.ts +47 -3
- package/src/server/account/terms.ts +42 -0
- package/src/server/api/router.ts +12 -7
- package/src/server/api/types.ts +9 -0
- package/src/server/app.ts +93 -15
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/adapter.ts +11 -0
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/factory.ts +30 -3
- package/src/server/auth/index.ts +10 -1
- package/src/server/cleanup/configured.ts +12 -5
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/objects.ts +10 -2
- package/src/server/cleanup/revisions.ts +24 -7
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +5 -2
- package/src/server/collaboration/index.ts +3 -0
- package/src/server/collaboration/relay/connection.ts +196 -0
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +279 -195
- package/src/server/collaboration/relay/index.ts +3 -1
- package/src/server/collaboration/relay/log.ts +79 -0
- package/src/server/collaboration/relay/persistence.ts +147 -22
- package/src/server/collaboration/relay/resume.ts +28 -0
- package/src/server/collaboration/routes.ts +27 -16
- package/src/server/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +35 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +104 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -114
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/schema.ts +71 -0
- package/src/server/db/tables.ts +70 -7
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/limits.ts +9 -9
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +132 -80
- package/src/server/documents/service.ts +18 -4
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/routes.ts +36 -25
- package/src/server/documents/versions/service.ts +22 -4
- package/src/server/errors/index.ts +99 -0
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +56 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -15
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +12 -0
- package/src/server/pictures/routes.ts +4 -2
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/database.ts +39 -25
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +1 -0
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +12 -9
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +69 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/index.ts +4 -1
- package/src/server/rate-limit/middleware.ts +100 -12
- package/src/server/reports/index.ts +8 -0
- package/src/server/reports/routes.ts +37 -0
- package/src/server/reports/service.ts +123 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -22
- package/src/server/sharing/document-routes.ts +30 -22
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +21 -11
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +108 -85
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +59 -2
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +89 -38
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +67 -45
- package/src/{ai-relay → services}/app.ts +48 -44
- package/src/{ai-relay → services}/config.ts +25 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +43 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/contract/ai.ts +0 -150
- package/src/runtime/cloudflare/relay.ts +0 -136
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -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,79 @@
|
|
|
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; savedBytes: number }> {
|
|
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
|
+
const savedBytes =
|
|
76
|
+
(saved.snapshot?.byteLength ?? 0) +
|
|
77
|
+
saved.updates.reduce((total, update) => total + update.byteLength, 0)
|
|
78
|
+
return { document, log, savedBytes }
|
|
79
|
+
}
|
|
@@ -1,48 +1,173 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
-
import type {
|
|
2
|
+
import type { ObjectStore } from '#cloud/server/objects'
|
|
3
|
+
import { sha256 } from '@noble/hashes/sha2.js'
|
|
4
|
+
import { fromUint8Array } from 'js-base64'
|
|
5
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
3
6
|
|
|
4
7
|
export type CollaborationRoomIdentity = {
|
|
5
8
|
documentId: string
|
|
6
9
|
roomEpoch: number
|
|
7
10
|
}
|
|
8
11
|
|
|
9
|
-
/**
|
|
12
|
+
/** A room as the store keeps it: its last snapshot and the updates logged after it. */
|
|
13
|
+
export type SavedRoom = {
|
|
14
|
+
snapshot: Uint8Array | null
|
|
15
|
+
/** The updates after the snapshot, in the order they were logged. */
|
|
16
|
+
updates: Uint8Array[]
|
|
17
|
+
/** The sequence of the last update the snapshot or the log holds; 0 for a new room. */
|
|
18
|
+
sequence: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The relay's saved copy of each room's document, by document and collaboration epoch: a snapshot
|
|
23
|
+
* and an append-only log of the updates after it. Each room has one writer, the relay that holds
|
|
24
|
+
* it, so sequences only grow.
|
|
25
|
+
*/
|
|
10
26
|
export type CollaborationStateStore = {
|
|
11
|
-
load(room: CollaborationRoomIdentity): Promise<
|
|
12
|
-
|
|
27
|
+
load(room: CollaborationRoomIdentity): Promise<SavedRoom>
|
|
28
|
+
/**
|
|
29
|
+
* Logs one update; fails when the sequence is already taken. Writes nothing once a restore moved
|
|
30
|
+
* the document past the room's epoch, since that room never opens again.
|
|
31
|
+
*/
|
|
32
|
+
append(room: CollaborationRoomIdentity, sequence: number, update: Uint8Array): Promise<void>
|
|
33
|
+
/**
|
|
34
|
+
* Replaces the snapshot with `state`, which holds every update through `sequence`, and drops
|
|
35
|
+
* those updates; running it again leaves the same room. Like `append`, it changes no room of an
|
|
36
|
+
* epoch a restore replaced.
|
|
37
|
+
*/
|
|
38
|
+
compact(room: CollaborationRoomIdentity, sequence: number, state: Uint8Array): Promise<void>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Whether the document is still at the room's epoch, holding its row so a restore, which advances
|
|
43
|
+
* the epoch and deletes the replaced rooms' rows, waits for this write or this write sees it.
|
|
44
|
+
*/
|
|
45
|
+
async function holdsEpoch(
|
|
46
|
+
transaction: Transaction<CloudDatabase>,
|
|
47
|
+
room: CollaborationRoomIdentity
|
|
48
|
+
): Promise<boolean> {
|
|
49
|
+
const document = await transaction
|
|
50
|
+
.selectFrom('document')
|
|
51
|
+
.select('collaborationEpoch')
|
|
52
|
+
.where('id', '=', room.documentId)
|
|
53
|
+
.forShare()
|
|
54
|
+
.executeTakeFirst()
|
|
55
|
+
return document?.collaborationEpoch === room.roomEpoch
|
|
13
56
|
}
|
|
14
57
|
|
|
58
|
+
/** A snapshot is a Yjs update, as the store hands it back. */
|
|
59
|
+
const SNAPSHOT_CONTENT_TYPE = 'application/octet-stream'
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Where a room's snapshots go. Each compaction writes a new file and points the room at it; the one
|
|
63
|
+
* it replaced is no longer referenced, and the object sweep removes it as it does a deleted
|
|
64
|
+
* revision's file.
|
|
65
|
+
*/
|
|
66
|
+
function snapshotKey(room: CollaborationRoomIdentity): string {
|
|
67
|
+
return `collaboration/${room.documentId}/${room.roomEpoch}/${crypto.randomUUID()}.yjs`
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* A store keeping each room's snapshot as a file in the object store and the updates logged after
|
|
72
|
+
* it in the database, so the database holds only what changed since the last snapshot.
|
|
73
|
+
*/
|
|
15
74
|
export function createCollaborationStateStore(
|
|
16
|
-
database: Kysely<CloudDatabase
|
|
75
|
+
database: Kysely<CloudDatabase>,
|
|
76
|
+
objects: Pick<ObjectStore, 'write' | 'read'>
|
|
17
77
|
): CollaborationStateStore {
|
|
18
78
|
return {
|
|
19
79
|
async load(room) {
|
|
20
|
-
const
|
|
21
|
-
.selectFrom('
|
|
22
|
-
.
|
|
80
|
+
const snapshot = await database
|
|
81
|
+
.selectFrom('documentRoomSnapshot')
|
|
82
|
+
.innerJoin('storageObject', 'storageObject.id', 'documentRoomSnapshot.snapshotObjectId')
|
|
83
|
+
.select(['storageObject.objectKey', 'documentRoomSnapshot.throughSequence'])
|
|
84
|
+
.where('documentRoomSnapshot.documentId', '=', room.documentId)
|
|
85
|
+
.where('documentRoomSnapshot.roomEpoch', '=', room.roomEpoch)
|
|
86
|
+
.executeTakeFirst()
|
|
87
|
+
const state = snapshot ? await objects.read(snapshot.objectKey) : null
|
|
88
|
+
if (snapshot && !state) {
|
|
89
|
+
throw new Error(`The snapshot of room ${room.documentId}:${room.roomEpoch} is missing`)
|
|
90
|
+
}
|
|
91
|
+
const through = snapshot?.throughSequence ?? 0
|
|
92
|
+
const updates = await database
|
|
93
|
+
.selectFrom('documentRoomUpdate')
|
|
94
|
+
.select(['sequence', 'data'])
|
|
23
95
|
.where('documentId', '=', room.documentId)
|
|
24
96
|
.where('roomEpoch', '=', room.roomEpoch)
|
|
25
|
-
.
|
|
26
|
-
|
|
97
|
+
.where('sequence', '>', through)
|
|
98
|
+
.orderBy('sequence')
|
|
99
|
+
.execute()
|
|
100
|
+
return {
|
|
101
|
+
snapshot: state,
|
|
102
|
+
updates: updates.map((row) => new Uint8Array(row.data)),
|
|
103
|
+
sequence: updates.at(-1)?.sequence ?? through
|
|
104
|
+
}
|
|
27
105
|
},
|
|
28
106
|
|
|
29
|
-
async
|
|
107
|
+
async append(room, sequence, update) {
|
|
108
|
+
await database.transaction().execute(async (transaction) => {
|
|
109
|
+
if (!(await holdsEpoch(transaction, room))) return
|
|
110
|
+
await transaction
|
|
111
|
+
.insertInto('documentRoomUpdate')
|
|
112
|
+
.values({
|
|
113
|
+
documentId: room.documentId,
|
|
114
|
+
roomEpoch: room.roomEpoch,
|
|
115
|
+
sequence,
|
|
116
|
+
data: update
|
|
117
|
+
})
|
|
118
|
+
.execute()
|
|
119
|
+
})
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
async compact(room, sequence, state) {
|
|
123
|
+
// The file is recorded before it is written and referenced only once the room points at it,
|
|
124
|
+
// so a compaction that fails anywhere leaves a file nothing refers to, which the object sweep
|
|
125
|
+
// removes; none is ever deleted while a room might still point at it.
|
|
126
|
+
const snapshotObjectId = crypto.randomUUID()
|
|
127
|
+
const objectKey = snapshotKey(room)
|
|
128
|
+
const checksum = fromUint8Array(sha256(state))
|
|
30
129
|
await database
|
|
31
|
-
.insertInto('
|
|
130
|
+
.insertInto('storageObject')
|
|
32
131
|
.values({
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
132
|
+
id: snapshotObjectId,
|
|
133
|
+
objectKey,
|
|
134
|
+
checksum,
|
|
135
|
+
byteSize: state.byteLength,
|
|
136
|
+
contentType: SNAPSHOT_CONTENT_TYPE
|
|
37
137
|
})
|
|
38
|
-
.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
138
|
+
.execute()
|
|
139
|
+
await objects.write({
|
|
140
|
+
key: objectKey,
|
|
141
|
+
bytes: state,
|
|
142
|
+
contentType: SNAPSHOT_CONTENT_TYPE,
|
|
143
|
+
checksum
|
|
144
|
+
})
|
|
145
|
+
await database.transaction().execute(async (transaction) => {
|
|
146
|
+
if (!(await holdsEpoch(transaction, room))) return
|
|
147
|
+
await transaction
|
|
148
|
+
.insertInto('documentRoomSnapshot')
|
|
149
|
+
.values({
|
|
150
|
+
documentId: room.documentId,
|
|
151
|
+
roomEpoch: room.roomEpoch,
|
|
152
|
+
snapshotObjectId,
|
|
153
|
+
throughSequence: sequence,
|
|
42
154
|
updatedAt: new Date()
|
|
43
155
|
})
|
|
44
|
-
|
|
45
|
-
|
|
156
|
+
.onConflict((conflict) =>
|
|
157
|
+
conflict.columns(['documentId', 'roomEpoch']).doUpdateSet({
|
|
158
|
+
snapshotObjectId,
|
|
159
|
+
throughSequence: sequence,
|
|
160
|
+
updatedAt: new Date()
|
|
161
|
+
})
|
|
162
|
+
)
|
|
163
|
+
.execute()
|
|
164
|
+
await transaction
|
|
165
|
+
.deleteFrom('documentRoomUpdate')
|
|
166
|
+
.where('documentId', '=', room.documentId)
|
|
167
|
+
.where('roomEpoch', '=', room.roomEpoch)
|
|
168
|
+
.where('sequence', '<=', sequence)
|
|
169
|
+
.execute()
|
|
170
|
+
})
|
|
46
171
|
}
|
|
47
172
|
}
|
|
48
173
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { collaborationPrincipalSchema } from '#cloud/contract'
|
|
2
|
+
import * as v from 'valibot'
|
|
3
|
+
|
|
4
|
+
const peerStateSchema = v.object({
|
|
5
|
+
peerId: v.pipe(v.string(), v.uuid()),
|
|
6
|
+
authorization: v.object({
|
|
7
|
+
roomId: v.string(),
|
|
8
|
+
documentId: v.pipe(v.string(), v.uuid()),
|
|
9
|
+
roomEpoch: v.pipe(v.number(), v.integer(), v.minValue(0)),
|
|
10
|
+
permission: v.picklist(['view', 'edit']),
|
|
11
|
+
principal: collaborationPrincipalSchema,
|
|
12
|
+
expiresAt: v.number()
|
|
13
|
+
}),
|
|
14
|
+
presenceClients: v.array(v.pipe(v.number(), v.integer()))
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* What a runtime keeps with each admitted socket so the relay can resume its peer after the
|
|
19
|
+
* runtime set the relay aside, as a hibernating Durable Object does: who it is, what its ticket
|
|
20
|
+
* allows, and the presence clients it speaks for.
|
|
21
|
+
*/
|
|
22
|
+
export type RelayPeerState = v.InferOutput<typeof peerStateSchema>
|
|
23
|
+
|
|
24
|
+
/** A peer's kept state, or null when what the runtime kept is not one. */
|
|
25
|
+
export function readRelayPeerState(value: unknown): RelayPeerState | null {
|
|
26
|
+
const parsed = v.safeParse(peerStateSchema, value)
|
|
27
|
+
return parsed.success ? parsed.output : null
|
|
28
|
+
}
|
|
@@ -3,9 +3,13 @@ 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'
|
|
5
5
|
import { DocumentNotFoundError } from '#cloud/server/documents'
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
CLOUD_RATE_LIMITS,
|
|
8
|
+
routeParamResource,
|
|
9
|
+
createResourceRateLimiter
|
|
10
|
+
} from '#cloud/server/rate-limit'
|
|
7
11
|
import { DocumentShareInvalidError, DocumentShareSignInRequiredError } from '#cloud/server/sharing'
|
|
8
|
-
import { validatedJSON } from '#cloud/server/validation'
|
|
12
|
+
import { documentParams, shareParams, validatedJSON } from '#cloud/server/validation'
|
|
9
13
|
import { Hono, type Context } from 'hono'
|
|
10
14
|
import type { Kysely } from 'kysely'
|
|
11
15
|
|
|
@@ -29,26 +33,32 @@ export function createCollaborationRoutes(
|
|
|
29
33
|
if (rateLimit) {
|
|
30
34
|
router.use(
|
|
31
35
|
'/documents/:documentId/collaboration-ticket',
|
|
32
|
-
|
|
36
|
+
createResourceRateLimiter(
|
|
33
37
|
rateLimit.database,
|
|
34
38
|
rateLimit.secret,
|
|
35
39
|
CLOUD_RATE_LIMITS.collaborationTicket,
|
|
36
|
-
|
|
37
|
-
(context) => context.req.param('documentId') ?? 'unknown-document'
|
|
40
|
+
routeParamResource('documentId')
|
|
38
41
|
)
|
|
39
42
|
)
|
|
40
43
|
}
|
|
41
|
-
return router.post(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
44
|
+
return router.post(
|
|
45
|
+
'/documents/:documentId/collaboration-ticket',
|
|
46
|
+
documentParams,
|
|
47
|
+
async (context) => {
|
|
48
|
+
try {
|
|
49
|
+
return context.json({
|
|
50
|
+
ticket: await service.issueUserTicket(
|
|
51
|
+
context.get('actor'),
|
|
52
|
+
context.req.valid('param').documentId
|
|
53
|
+
)
|
|
54
|
+
})
|
|
55
|
+
} catch (error) {
|
|
56
|
+
const response = notFound(context, error)
|
|
57
|
+
if (response) return response
|
|
58
|
+
throw error
|
|
59
|
+
}
|
|
50
60
|
}
|
|
51
|
-
|
|
61
|
+
)
|
|
52
62
|
}
|
|
53
63
|
|
|
54
64
|
export function createPublicCollaborationRoutes(
|
|
@@ -57,12 +67,13 @@ export function createPublicCollaborationRoutes(
|
|
|
57
67
|
) {
|
|
58
68
|
return new Hono().post(
|
|
59
69
|
'/shares/:shareId/collaboration-ticket',
|
|
70
|
+
shareParams,
|
|
60
71
|
validatedJSON(parseResolveDocumentShare),
|
|
61
72
|
async (context) => {
|
|
62
73
|
try {
|
|
63
74
|
return context.json({
|
|
64
75
|
ticket: await service.issueShareTicket(
|
|
65
|
-
context.req.
|
|
76
|
+
context.req.valid('param').shareId,
|
|
66
77
|
context.req.valid('json'),
|
|
67
78
|
resolveSession ? ((await resolveSession(context.req.raw)) ?? undefined) : undefined
|
|
68
79
|
)
|
|
@@ -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 { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
14
15
|
import type { DocumentSharingService } from '#cloud/server/sharing'
|
|
15
16
|
import { base64url, SignJWT } from 'jose'
|
|
16
17
|
import type { Kysely } from 'kysely'
|
|
@@ -74,13 +75,21 @@ export async function signCollaborationTicket({
|
|
|
74
75
|
.setExpirationTime(expiresAtSeconds)
|
|
75
76
|
.setSubject(principal.kind === 'user' ? principal.userId : principal.guestId)
|
|
76
77
|
.sign(new TextEncoder().encode(authSecret))
|
|
77
|
-
|
|
78
|
+
const ticket = {
|
|
78
79
|
token,
|
|
79
|
-
provider:
|
|
80
|
-
...(relayURL ? { serverURL: withQuery(relayURL, { [RELAY_DOCUMENT_PARAM]: documentId }) } : {}),
|
|
80
|
+
provider: 'trystero' as const,
|
|
81
81
|
...claims,
|
|
82
82
|
roomKey,
|
|
83
|
-
expiresAt: new Date(expiresAtSeconds * 1000).toISOString()
|
|
83
|
+
expiresAt: new Date(expiresAtSeconds * 1000).toISOString(),
|
|
84
|
+
relayHeartbeat: false
|
|
85
|
+
}
|
|
86
|
+
if (!relayURL) return ticket
|
|
87
|
+
// This deployment's relay answers the heartbeat, which editors then use to notice a lost socket.
|
|
88
|
+
return {
|
|
89
|
+
...ticket,
|
|
90
|
+
provider: 'relay' as const,
|
|
91
|
+
serverURL: withQuery(relayURL, { [RELAY_DOCUMENT_PARAM]: documentId }),
|
|
92
|
+
relayHeartbeat: true
|
|
84
93
|
}
|
|
85
94
|
}
|
|
86
95
|
|
|
@@ -106,20 +115,12 @@ export function createCollaborationTicketService({
|
|
|
106
115
|
const access = await resolveDocumentAccess(database, actor.userId, documentId)
|
|
107
116
|
if (!access) throw new DocumentNotFoundError()
|
|
108
117
|
if (policy) {
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
.
|
|
113
|
-
|
|
114
|
-
if (
|
|
115
|
-
!(await policy.boolean(CLOUD_FEATURE_KEYS.collaboration, false, {
|
|
116
|
-
targetingKey: workspace.workspaceId,
|
|
117
|
-
actorId: actor.userId,
|
|
118
|
-
workspaceId: workspace.workspaceId,
|
|
119
|
-
documentId,
|
|
120
|
-
deploymentMode
|
|
121
|
-
}))
|
|
122
|
-
) {
|
|
118
|
+
const context = await workspacePolicyContext(
|
|
119
|
+
database,
|
|
120
|
+
{ documentId },
|
|
121
|
+
{ actorId: actor.userId, deploymentMode }
|
|
122
|
+
)
|
|
123
|
+
if (!context || !(await policy.boolean(CLOUD_FEATURE_KEYS.collaboration, false, context))) {
|
|
123
124
|
throw new DocumentNotFoundError()
|
|
124
125
|
}
|
|
125
126
|
}
|
|
@@ -128,6 +129,7 @@ export function createCollaborationTicketService({
|
|
|
128
129
|
.select('collaborationEpoch')
|
|
129
130
|
.where('id', '=', documentId)
|
|
130
131
|
.where('deletedAt', 'is', null)
|
|
132
|
+
.where('disabledAt', 'is', null)
|
|
131
133
|
.executeTakeFirst()
|
|
132
134
|
if (!document) throw new DocumentNotFoundError()
|
|
133
135
|
return signCollaborationTicket({
|
|
@@ -147,19 +149,12 @@ export function createCollaborationTicketService({
|
|
|
147
149
|
): Promise<CollaborationTicket> {
|
|
148
150
|
const resolved = await sharing.resolveShare(shareId, input, actor)
|
|
149
151
|
if (policy) {
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
.
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
targetingKey: workspace.workspaceId,
|
|
157
|
-
...(actor ? { actorId: actor.userId } : {}),
|
|
158
|
-
workspaceId: workspace.workspaceId,
|
|
159
|
-
documentId: resolved.documentId,
|
|
160
|
-
deploymentMode
|
|
161
|
-
}
|
|
162
|
-
if (!(await policy.boolean(CLOUD_FEATURE_KEYS.collaboration, false, context))) {
|
|
152
|
+
const context = await workspacePolicyContext(
|
|
153
|
+
database,
|
|
154
|
+
{ documentId: resolved.documentId },
|
|
155
|
+
{ actorId: actor?.userId, deploymentMode }
|
|
156
|
+
)
|
|
157
|
+
if (!context || !(await policy.boolean(CLOUD_FEATURE_KEYS.collaboration, false, context))) {
|
|
163
158
|
throw new DocumentNotFoundError()
|
|
164
159
|
}
|
|
165
160
|
if (!actor && !(await policy.boolean(CLOUD_FEATURE_KEYS.guestPresence, false, context))) {
|
|
@@ -171,6 +166,7 @@ export function createCollaborationTicketService({
|
|
|
171
166
|
.select('collaborationEpoch')
|
|
172
167
|
.where('id', '=', resolved.documentId)
|
|
173
168
|
.where('deletedAt', 'is', null)
|
|
169
|
+
.where('disabledAt', 'is', null)
|
|
174
170
|
.executeTakeFirst()
|
|
175
171
|
if (!document) throw new DocumentNotFoundError()
|
|
176
172
|
return signCollaborationTicket({
|
|
@@ -83,15 +83,20 @@ function workerConfig(config: CloudDeploymentConfig) {
|
|
|
83
83
|
cleanupBatchSize: cleanup?.batch_size,
|
|
84
84
|
cleanupIntervalMs: cleanup?.interval_ms,
|
|
85
85
|
cleanupLeaseDurationMs: cleanup?.lease_ms,
|
|
86
|
-
documentRetentionMs: config.documents.retention_days * 86_400_000
|
|
86
|
+
documentRetentionMs: config.documents.retention_days * 86_400_000,
|
|
87
|
+
documentPurgeGraceMs: config.documents.purge_grace_days * 86_400_000
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
90
|
|
|
90
91
|
function serviceConfig(config: CloudDeploymentConfig, environment: CloudEnvironment) {
|
|
91
92
|
return {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
servicesEndpoint: config.services?.endpoint,
|
|
94
|
+
servicesSigningKey: config.services
|
|
95
|
+
? requiredSecret(environment, config.services.signing_key)
|
|
96
|
+
: undefined,
|
|
97
|
+
servicesTokenLifetimeSeconds: config.services
|
|
98
|
+
? config.services.token_lifetime_minutes * 60
|
|
99
|
+
: undefined,
|
|
95
100
|
billingPortalURL: config.billing?.portal_url,
|
|
96
101
|
internalServiceToken: config.internal_api
|
|
97
102
|
? requiredSecret(environment, config.internal_api.service_token)
|
|
@@ -99,6 +104,29 @@ function serviceConfig(config: CloudDeploymentConfig, environment: CloudEnvironm
|
|
|
99
104
|
}
|
|
100
105
|
}
|
|
101
106
|
|
|
107
|
+
function legalConfig(config: CloudDeploymentConfig) {
|
|
108
|
+
const legal = config.legal
|
|
109
|
+
if (!legal) return undefined
|
|
110
|
+
return {
|
|
111
|
+
termsURL: legal.terms_url,
|
|
112
|
+
privacyURL: legal.privacy_url,
|
|
113
|
+
termsVersion: legal.terms_version,
|
|
114
|
+
requireAcceptance: legal.require_acceptance,
|
|
115
|
+
abuseEmail: legal.abuse_email,
|
|
116
|
+
minimumAge: legal.minimum_age
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function errorTrackingConfig(config: CloudDeploymentConfig, environment: CloudEnvironment) {
|
|
121
|
+
const tracking = config.error_tracking
|
|
122
|
+
if (!tracking) return undefined
|
|
123
|
+
return {
|
|
124
|
+
provider: tracking.provider,
|
|
125
|
+
host: tracking.host,
|
|
126
|
+
projectAPIKey: configuredText(environment, tracking.project_api_key)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
102
130
|
/** Hard limits, whatever a plan allows; each lives in the section it limits. */
|
|
103
131
|
function technicalLimits(config: CloudDeploymentConfig) {
|
|
104
132
|
return {
|
|
@@ -161,11 +189,14 @@ export function parseCloudDeploymentConfig(
|
|
|
161
189
|
s3ChecksumVerification: config.object_storage.checksum_verification,
|
|
162
190
|
s3ServerSideEncryption: config.object_storage.server_side_encryption,
|
|
163
191
|
s3KmsKeyId: config.object_storage.kms_key_id,
|
|
192
|
+
s3DeletedPrefix: config.object_storage.deleted_prefix,
|
|
164
193
|
relayURL: config.collaboration.relay_url,
|
|
165
194
|
staticEntitlements: staticEntitlements(config),
|
|
166
195
|
...emailConfig(config, environment),
|
|
167
196
|
...workerConfig(config),
|
|
168
197
|
...serviceConfig(config, environment),
|
|
198
|
+
legal: legalConfig(config),
|
|
199
|
+
errorTracking: errorTrackingConfig(config, environment),
|
|
169
200
|
technicalLimits: technicalLimits(config)
|
|
170
201
|
})
|
|
171
202
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
CLOUD_SERVICES_CONTRACT_VERSION,
|
|
3
3
|
CLOUD_PROTOCOL_VERSION,
|
|
4
4
|
parseCloudDiscovery,
|
|
5
5
|
type CloudDiscovery
|
|
@@ -58,9 +58,10 @@ export function cloudDiscoveryFromConfig(config: CloudServerConfig): CloudDiscov
|
|
|
58
58
|
listing: config.listingMinimumEditorVersion
|
|
59
59
|
? { minimumEditorVersion: config.listingMinimumEditorVersion }
|
|
60
60
|
: undefined,
|
|
61
|
-
|
|
62
|
-
? { endpoint: config.
|
|
61
|
+
services: config.servicesEndpoint
|
|
62
|
+
? { endpoint: config.servicesEndpoint, contractVersion: CLOUD_SERVICES_CONTRACT_VERSION }
|
|
63
63
|
: undefined,
|
|
64
|
-
billing: config.billingPortalURL ? { portalURL: config.billingPortalURL } : undefined
|
|
64
|
+
billing: config.billingPortalURL ? { portalURL: config.billingPortalURL } : undefined,
|
|
65
|
+
legal: config.legal
|
|
65
66
|
})
|
|
66
67
|
}
|