@open-pencil/cloud 0.0.0-stage → 0.16.0
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/LICENSE +202 -0
- package/LICENSE-MIT +21 -0
- package/NOTICE +7 -0
- package/README.md +37 -2
- package/dist/bin/ai-relay.d.mts +1 -0
- package/dist/bin/ai-relay.mjs +31 -0
- package/dist/bin/ai-relay.mjs.map +1 -0
- package/dist/bin/server.d.mts +1 -0
- package/dist/bin/server.mjs +29 -0
- package/dist/bin/server.mjs.map +1 -0
- package/dist/client.d.ts +1520 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1917 -0
- package/dist/client.js.map +1 -0
- package/dist/contract.d.ts +2 -0
- package/dist/contract.js +2 -0
- package/dist/contract2.js +1453 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +169 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +670 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1755 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1881 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10040 -0
- package/dist/runtime-node.d.mts.map +1 -0
- package/dist/runtime-node.mjs +32 -0
- package/dist/runtime-node.mjs.map +1 -0
- package/dist/server.d.ts +12792 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8475 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8324 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +124 -4
- package/src/admin/api/routes.ts +223 -0
- package/src/admin/api/security.ts +12 -0
- package/src/admin/audit/service.ts +138 -0
- package/src/admin/email/service.ts +140 -0
- package/src/admin/enrollment/email.ts +77 -0
- package/src/admin/enrollment/service.ts +308 -0
- package/src/admin/errors.ts +20 -0
- package/src/admin/index.ts +19 -0
- package/src/admin/list.ts +61 -0
- package/src/admin/operations/service.ts +70 -0
- package/src/admin/schema.ts +28 -0
- package/src/admin/users/service.ts +369 -0
- package/src/ai-relay/app.ts +251 -0
- package/src/ai-relay/config.ts +120 -0
- package/src/ai-relay/db/index.ts +10 -0
- package/src/ai-relay/db/migrate.ts +32 -0
- package/src/ai-relay/db/migrations/001_usage.ts +26 -0
- package/src/ai-relay/db/schema.ts +20 -0
- package/src/ai-relay/index.ts +18 -0
- package/src/ai-relay/metering.ts +68 -0
- package/src/ai-relay/usage.ts +115 -0
- package/src/client/ai.ts +96 -0
- package/src/client/api.ts +528 -0
- package/src/client/auth/client.ts +188 -0
- package/src/client/auth/failure.ts +54 -0
- package/src/client/auth/navigation.ts +45 -0
- package/src/client/device-authorization.ts +134 -0
- package/src/client/discovery.ts +105 -0
- package/src/client/images.ts +81 -0
- package/src/client/index.ts +53 -0
- package/src/client/portal/account.ts +183 -0
- package/src/client/portal/administration.ts +130 -0
- package/src/client/portal/client.ts +18 -0
- package/src/client/portal/request.ts +115 -0
- package/src/client/response.ts +51 -0
- package/src/client/workspaces.ts +142 -0
- package/src/contract/account.ts +204 -0
- package/src/contract/admin/index.ts +187 -0
- package/src/contract/ai.ts +150 -0
- package/src/contract/collaboration.ts +45 -0
- package/src/contract/discovery.ts +81 -0
- package/src/contract/documents.ts +265 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +204 -0
- package/src/contract/links.ts +87 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/relay.ts +368 -0
- package/src/contract/sharing.ts +191 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +14 -0
- package/src/email/render.ts +276 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +105 -0
- package/src/email/templates/document-invitation.ts +35 -0
- package/src/email/templates/enrollment.ts +27 -0
- package/src/email/templates/layout.ts +18 -0
- package/src/runtime/cloudflare/email.ts +70 -0
- package/src/runtime/cloudflare/environment.ts +57 -0
- package/src/runtime/cloudflare/index.ts +3 -0
- package/src/runtime/cloudflare/relay.ts +136 -0
- package/src/runtime/cloudflare/site.ts +13 -0
- package/src/runtime/cloudflare/tsconfig.json +21 -0
- package/src/runtime/cloudflare/worker.ts +160 -0
- package/src/runtime/node/admin.ts +59 -0
- package/src/runtime/node/ai-relay/main.ts +35 -0
- package/src/runtime/node/ai-relay/server.ts +63 -0
- package/src/runtime/node/application.ts +50 -0
- package/src/runtime/node/bootstrap.ts +22 -0
- package/src/runtime/node/cleanup.ts +24 -0
- package/src/runtime/node/command.ts +17 -0
- package/src/runtime/node/config.ts +16 -0
- package/src/runtime/node/email/runtime.ts +43 -0
- package/src/runtime/node/email/transport.ts +112 -0
- package/src/runtime/node/entitlements/main.ts +41 -0
- package/src/runtime/node/entitlements/refusal.ts +15 -0
- package/src/runtime/node/index.ts +25 -0
- package/src/runtime/node/listen.ts +35 -0
- package/src/runtime/node/main.ts +29 -0
- package/src/runtime/node/migrate.ts +13 -0
- package/src/runtime/node/quota-reconcile.ts +9 -0
- package/src/runtime/node/relay.ts +54 -0
- package/src/runtime/node/server.ts +121 -0
- package/src/runtime/node/site.ts +64 -0
- package/src/runtime/postgres/database.ts +19 -0
- package/src/runtime/s3/objects.ts +244 -0
- package/src/runtime/site/server.ts +82 -0
- package/src/server/account/actions.ts +66 -0
- package/src/server/account/routes.ts +300 -0
- package/src/server/ai/errors.ts +7 -0
- package/src/server/ai/index.ts +4 -0
- package/src/server/ai/keys.ts +43 -0
- package/src/server/ai/routes.ts +43 -0
- package/src/server/ai/service.ts +70 -0
- package/src/server/api/index.ts +8 -0
- package/src/server/api/router.ts +117 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +375 -0
- package/src/server/auth/account.ts +73 -0
- package/src/server/auth/activity.ts +213 -0
- package/src/server/auth/adapter.ts +79 -0
- package/src/server/auth/email.ts +125 -0
- package/src/server/auth/endpoints.ts +57 -0
- package/src/server/auth/factory.ts +583 -0
- package/src/server/auth/index.ts +17 -0
- package/src/server/auth/mfa-assurance.ts +55 -0
- package/src/server/auth/runtime.ts +26 -0
- package/src/server/auth/session.ts +19 -0
- package/src/server/auth/social-providers.ts +55 -0
- package/src/server/auth/user-agent.ts +16 -0
- package/src/server/auth/workspace-access.ts +23 -0
- package/src/server/cleanup/configured.ts +38 -0
- package/src/server/cleanup/documents.ts +231 -0
- package/src/server/cleanup/index.ts +29 -0
- package/src/server/cleanup/objects.ts +27 -0
- package/src/server/cleanup/revisions.ts +232 -0
- package/src/server/cleanup/uploads.ts +129 -0
- package/src/server/cleanup/worker.ts +79 -0
- package/src/server/collaboration/index.ts +28 -0
- package/src/server/collaboration/relay/authorize.ts +53 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +524 -0
- package/src/server/collaboration/relay/index.ts +15 -0
- package/src/server/collaboration/relay/persistence.ts +48 -0
- package/src/server/collaboration/routes.ts +77 -0
- package/src/server/collaboration/service.ts +188 -0
- package/src/server/config/deployment.ts +178 -0
- package/src/server/config/index.ts +20 -0
- package/src/server/config/limits.ts +8 -0
- package/src/server/config/public.ts +66 -0
- package/src/server/config/schema.ts +512 -0
- package/src/server/db/database.ts +15 -0
- package/src/server/db/expressions.ts +13 -0
- package/src/server/db/index.ts +21 -0
- package/src/server/db/migrate.ts +177 -0
- package/src/server/db/migrations/001_foundation.ts +164 -0
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +34 -0
- package/src/server/db/migrations/003_document_cleanup_claims.ts +23 -0
- package/src/server/db/migrations/004_document_sharing.ts +82 -0
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +13 -0
- package/src/server/db/migrations/006_invitation_continuation.ts +27 -0
- package/src/server/db/migrations/007_collaboration_state.ts +25 -0
- package/src/server/db/migrations/008_storage_reservations.ts +37 -0
- package/src/server/db/migrations/009_workspace_entitlements.ts +22 -0
- package/src/server/db/migrations/010_upload_finalization.ts +22 -0
- package/src/server/db/migrations/011_upload_finalization_lease.ts +12 -0
- package/src/server/db/migrations/012_transactional_email.ts +48 -0
- package/src/server/db/migrations/013_admin_enrollment.ts +54 -0
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +30 -0
- package/src/server/db/migrations/015_cloud_rate_limit.ts +22 -0
- package/src/server/db/migrations/016_auth_schema.ts +16 -0
- package/src/server/db/migrations/017_authentication_email_kinds.ts +32 -0
- package/src/server/db/migrations/018_authentication_mfa.ts +78 -0
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +42 -0
- package/src/server/db/migrations/020_workspace_invitations.ts +57 -0
- package/src/server/db/migrations/021_document_versions.ts +66 -0
- package/src/server/db/migrations/022_entitlement_external_version.ts +17 -0
- package/src/server/db/migrations/023_document_images.ts +61 -0
- package/src/server/db/migrations/024_account_activity.ts +66 -0
- package/src/server/db/migrations/025_table_names.ts +58 -0
- package/src/server/db/migrations/026_user_picture.ts +30 -0
- package/src/server/db/migrations/027_document_kind.ts +19 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +394 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +407 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +141 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +597 -0
- package/src/server/documents/summary.ts +37 -0
- package/src/server/documents/types.ts +14 -0
- package/src/server/documents/versions/checkpoint.ts +98 -0
- package/src/server/documents/versions/history.ts +25 -0
- package/src/server/documents/versions/index.ts +4 -0
- package/src/server/documents/versions/routes.ts +112 -0
- package/src/server/documents/versions/service.ts +397 -0
- package/src/server/email/crypto.ts +57 -0
- package/src/server/email/index.ts +18 -0
- package/src/server/email/invitations.ts +30 -0
- package/src/server/email/service.ts +245 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +279 -0
- package/src/server/internal/index.ts +8 -0
- package/src/server/internal/routes.ts +41 -0
- package/src/server/internal/schema.ts +10 -0
- package/src/server/internal/service.ts +44 -0
- package/src/server/invitations/index.ts +6 -0
- package/src/server/invitations/types.ts +30 -0
- package/src/server/objects/index.ts +16 -0
- package/src/server/objects/types.ts +81 -0
- package/src/server/pictures/index.ts +9 -0
- package/src/server/pictures/provider.ts +61 -0
- package/src/server/pictures/routes.ts +21 -0
- package/src/server/pictures/service.ts +235 -0
- package/src/server/policy/configured.ts +23 -0
- package/src/server/policy/database.ts +108 -0
- package/src/server/policy/entitlements.ts +29 -0
- package/src/server/policy/index.ts +17 -0
- package/src/server/policy/keys.ts +23 -0
- package/src/server/policy/policy.ts +117 -0
- package/src/server/policy/provider.ts +67 -0
- package/src/server/policy/schema.ts +69 -0
- package/src/server/policy/service.ts +85 -0
- package/src/server/policy/static.ts +62 -0
- package/src/server/quota/index.ts +7 -0
- package/src/server/quota/reconcile.ts +61 -0
- package/src/server/quota/storage.ts +165 -0
- package/src/server/rate-limit/cleanup.ts +16 -0
- package/src/server/rate-limit/index.ts +12 -0
- package/src/server/rate-limit/keys.ts +38 -0
- package/src/server/rate-limit/middleware.ts +137 -0
- package/src/server/rate-limit/postgres.ts +79 -0
- package/src/server/sharing/access.ts +29 -0
- package/src/server/sharing/actor.ts +12 -0
- package/src/server/sharing/capabilities/service.ts +262 -0
- package/src/server/sharing/continuation.ts +85 -0
- package/src/server/sharing/contracts.ts +80 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +12 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +41 -0
- package/src/server/sharing/index.ts +39 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +44 -0
- package/src/server/sharing/routes.ts +251 -0
- package/src/server/sharing/service.ts +117 -0
- package/src/server/validation.ts +24 -0
- package/src/server/worker/interval.ts +23 -0
- package/src/server/worker/periodic.ts +42 -0
- package/src/server/workspaces/index.ts +16 -0
- package/src/server/workspaces/members.ts +502 -0
- package/src/server/workspaces/repository.ts +91 -0
- package/src/server/workspaces/routes.ts +198 -0
- package/src/server/workspaces/service.ts +84 -0
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import { deflateSync, Inflate } from 'fflate'
|
|
2
|
+
import * as decoding from 'lib0/decoding'
|
|
3
|
+
import * as encoding from 'lib0/encoding'
|
|
4
|
+
import * as Y from 'yjs'
|
|
5
|
+
|
|
6
|
+
/** Where the relay accepts WebSocket upgrades, under the Cloud API origin. */
|
|
7
|
+
export const RELAY_PATH = '/api/collaboration/relay'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The relay URL's query parameter naming the ticket's document, so a runtime that keeps each
|
|
11
|
+
* document's room in its own place can route the socket there before the ticket arrives.
|
|
12
|
+
*/
|
|
13
|
+
export const RELAY_DOCUMENT_PARAM = 'document'
|
|
14
|
+
|
|
15
|
+
/** The relay's public URL on the same origin as the API, unless the deployment names another. */
|
|
16
|
+
export function defaultRelayURL(publicURL: string): string {
|
|
17
|
+
const url = new URL(RELAY_PATH, publicURL)
|
|
18
|
+
url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:'
|
|
19
|
+
return url.href
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The room messages collaboration sends. The relay reads the document ones to keep its copy,
|
|
24
|
+
* persist it, and drop writes from viewers; it forwards the rest unread.
|
|
25
|
+
*/
|
|
26
|
+
export const COLLAB_ACTIONS = {
|
|
27
|
+
yjsUpdate: 'yjs-update',
|
|
28
|
+
syncStep1: 'sync-step1',
|
|
29
|
+
syncReply: 'sync-reply',
|
|
30
|
+
awareness: 'awareness',
|
|
31
|
+
agentPreview: 'agent-preview',
|
|
32
|
+
upload: 'upload',
|
|
33
|
+
/** From the server only: the document's version history changed, so open lists load it again. */
|
|
34
|
+
versions: 'versions'
|
|
35
|
+
} as const
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The most a room message may decode to, compressed or not. A peer's update past it is dropped
|
|
39
|
+
* before it is decoded, so a small compressed reply cannot expand without bound.
|
|
40
|
+
*/
|
|
41
|
+
export const MAX_COLLAB_MESSAGE_BYTES = 256 * 1024 * 1024
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* What a peer joining needs is the whole document, which repeats the same field names across
|
|
45
|
+
* every layer, so the reply to its state vector travels compressed; Yjs's V2 encoding already
|
|
46
|
+
* groups that repetition, and together they make a room's first sync about a twelfth the size.
|
|
47
|
+
* Edits travel as V2 updates uncompressed: they are small, and arrive in a stream.
|
|
48
|
+
*/
|
|
49
|
+
export function encodeSyncReply(document: Y.Doc, stateVector: Uint8Array): Uint8Array {
|
|
50
|
+
return deflateSync(Y.encodeStateAsUpdateV2(document, stateVector))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Compressed input is inflated this much at a time, so a reply stops within a step of passing
|
|
55
|
+
* the limit: deflate expands at most about 1,032 times, so a step can overshoot by about 16 MB.
|
|
56
|
+
*/
|
|
57
|
+
const INFLATE_STEP = 16 * 1024
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The V2 update a peer sent, inflated first when `compressed`, as a sync reply is; null once it
|
|
61
|
+
* would decode past `limit` bytes. Throws on data that does not inflate.
|
|
62
|
+
*/
|
|
63
|
+
export function decodeRoomUpdate(
|
|
64
|
+
data: Uint8Array,
|
|
65
|
+
{ limit, compressed = false }: { limit: number; compressed?: boolean }
|
|
66
|
+
): Uint8Array | null {
|
|
67
|
+
if (!compressed) return data.length <= limit ? data : null
|
|
68
|
+
const chunks: Uint8Array[] = []
|
|
69
|
+
let size = 0
|
|
70
|
+
const inflate = new Inflate((chunk) => {
|
|
71
|
+
size += chunk.length
|
|
72
|
+
if (size <= limit) chunks.push(chunk)
|
|
73
|
+
})
|
|
74
|
+
for (let offset = 0; offset < data.length; offset += INFLATE_STEP) {
|
|
75
|
+
inflate.push(data.subarray(offset, offset + INFLATE_STEP), offset + INFLATE_STEP >= data.length)
|
|
76
|
+
if (size > limit) return null
|
|
77
|
+
}
|
|
78
|
+
const inflated = new Uint8Array(size)
|
|
79
|
+
let at = 0
|
|
80
|
+
for (const chunk of chunks) {
|
|
81
|
+
inflated.set(chunk, at)
|
|
82
|
+
at += chunk.length
|
|
83
|
+
}
|
|
84
|
+
return inflated
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The peer ID the relay itself uses: it holds the room's document and answers sync requests. */
|
|
88
|
+
export const RELAY_SERVER_PEER = 'server'
|
|
89
|
+
|
|
90
|
+
/** The longest name a peer's presence may carry; peers drop presence with a longer one. */
|
|
91
|
+
export const MAX_PRESENCE_NAME_LENGTH = 40
|
|
92
|
+
|
|
93
|
+
/** A name as presence carries it: trimmed and cut to what every peer accepts. */
|
|
94
|
+
export function presenceName(name: string): string {
|
|
95
|
+
return name.trim().slice(0, MAX_PRESENCE_NAME_LENGTH)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Close codes in the WebSocket application range. A client reconnects with a fresh ticket after
|
|
100
|
+
* `expired`, and gives up on the others.
|
|
101
|
+
*/
|
|
102
|
+
export const RELAY_CLOSE = {
|
|
103
|
+
unauthorized: 4401,
|
|
104
|
+
expired: 4403,
|
|
105
|
+
roomFull: 4409,
|
|
106
|
+
messageTooLarge: 4413,
|
|
107
|
+
protocol: 4400,
|
|
108
|
+
/** The document was restored to a version, so its room started over under a new ID. */
|
|
109
|
+
restored: 4410
|
|
110
|
+
} as const
|
|
111
|
+
|
|
112
|
+
const CLIENT_AUTH = 0
|
|
113
|
+
const CLIENT_MESSAGE = 1
|
|
114
|
+
const CLIENT_FRAGMENT = 2
|
|
115
|
+
const SERVER_WELCOME = 0
|
|
116
|
+
const SERVER_JOIN = 1
|
|
117
|
+
const SERVER_LEAVE = 2
|
|
118
|
+
const SERVER_MESSAGE = 3
|
|
119
|
+
const SERVER_FRAGMENT = 4
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The most one WebSocket message carries. A frame past it travels as fragments of this size that
|
|
123
|
+
* the receiver joins again, so a large paste or a room's first sync fits under any host's message
|
|
124
|
+
* cap, such as the 32 MiB of Cloudflare Workers, and the relay can turn away a viewer's or an
|
|
125
|
+
* unadmitted peer's large frame before holding it whole.
|
|
126
|
+
*/
|
|
127
|
+
export const RELAY_FRAGMENT_BYTES = 256 * 1024
|
|
128
|
+
|
|
129
|
+
/** The smallest per-message limit a relay may set: one fragment and its few bytes of framing. */
|
|
130
|
+
export const RELAY_MINIMUM_MESSAGE_BYTES = RELAY_FRAGMENT_BYTES + 16
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* What the relay adds to a frame it forwards: the frame type, the sender's peer ID, and lengths.
|
|
134
|
+
* A client reads frames this much past the relay's own limit, so a forwarded edit at that limit
|
|
135
|
+
* still arrives.
|
|
136
|
+
*/
|
|
137
|
+
export const RELAY_FORWARDING_BYTES = 64 * 1024
|
|
138
|
+
|
|
139
|
+
/** Frames a client sends: its ticket first, again to refresh it, then room messages. */
|
|
140
|
+
export type RelayClientFrame =
|
|
141
|
+
| { type: 'auth'; token: string }
|
|
142
|
+
| { type: 'message'; namespace: string; target: string | null; payload: Uint8Array }
|
|
143
|
+
|
|
144
|
+
/** Frames the relay sends: who is in the room, arrivals and departures, and forwarded messages. */
|
|
145
|
+
export type RelayServerFrame =
|
|
146
|
+
| { type: 'welcome'; peerId: string; peers: string[] }
|
|
147
|
+
| { type: 'join'; peerId: string }
|
|
148
|
+
| { type: 'leave'; peerId: string }
|
|
149
|
+
| { type: 'message'; namespace: string; from: string; payload: Uint8Array }
|
|
150
|
+
|
|
151
|
+
/** Encodes into a buffer of its own, which `WebSocket.send` accepts. */
|
|
152
|
+
function toSendable(write: (encoder: encoding.Encoder) => void): Uint8Array<ArrayBuffer> {
|
|
153
|
+
return new Uint8Array(encoding.encode(write))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function encodeRelayClientFrame(frame: RelayClientFrame): Uint8Array<ArrayBuffer> {
|
|
157
|
+
return toSendable((encoder) => {
|
|
158
|
+
if (frame.type === 'auth') {
|
|
159
|
+
encoding.writeVarUint(encoder, CLIENT_AUTH)
|
|
160
|
+
encoding.writeVarString(encoder, frame.token)
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
encoding.writeVarUint(encoder, CLIENT_MESSAGE)
|
|
164
|
+
encoding.writeVarString(encoder, frame.namespace)
|
|
165
|
+
encoding.writeVarString(encoder, frame.target ?? '')
|
|
166
|
+
encoding.writeVarUint8Array(encoder, frame.payload)
|
|
167
|
+
})
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function encodeRelayServerFrame(frame: RelayServerFrame): Uint8Array<ArrayBuffer> {
|
|
171
|
+
return toSendable((encoder) => {
|
|
172
|
+
switch (frame.type) {
|
|
173
|
+
case 'welcome':
|
|
174
|
+
encoding.writeVarUint(encoder, SERVER_WELCOME)
|
|
175
|
+
encoding.writeVarString(encoder, frame.peerId)
|
|
176
|
+
encoding.writeVarUint(encoder, frame.peers.length)
|
|
177
|
+
for (const peer of frame.peers) encoding.writeVarString(encoder, peer)
|
|
178
|
+
return
|
|
179
|
+
case 'join':
|
|
180
|
+
case 'leave':
|
|
181
|
+
encoding.writeVarUint(encoder, frame.type === 'join' ? SERVER_JOIN : SERVER_LEAVE)
|
|
182
|
+
encoding.writeVarString(encoder, frame.peerId)
|
|
183
|
+
return
|
|
184
|
+
case 'message':
|
|
185
|
+
encoding.writeVarUint(encoder, SERVER_MESSAGE)
|
|
186
|
+
encoding.writeVarString(encoder, frame.namespace)
|
|
187
|
+
encoding.writeVarString(encoder, frame.from)
|
|
188
|
+
encoding.writeVarUint8Array(encoder, frame.payload)
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The frame as it goes over the socket: itself, or fragments of it when it is large. */
|
|
194
|
+
function fragments(tag: number, frame: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>[] {
|
|
195
|
+
if (frame.byteLength <= RELAY_FRAGMENT_BYTES) return [frame]
|
|
196
|
+
const parts: Uint8Array<ArrayBuffer>[] = []
|
|
197
|
+
for (let offset = 0; offset < frame.byteLength; offset += RELAY_FRAGMENT_BYTES) {
|
|
198
|
+
const end = Math.min(offset + RELAY_FRAGMENT_BYTES, frame.byteLength)
|
|
199
|
+
parts.push(
|
|
200
|
+
toSendable((encoder) => {
|
|
201
|
+
encoding.writeVarUint(encoder, tag)
|
|
202
|
+
encoding.writeVarUint(encoder, end < frame.byteLength ? 1 : 0)
|
|
203
|
+
encoding.writeVarUint8Array(encoder, frame.subarray(offset, end))
|
|
204
|
+
})
|
|
205
|
+
)
|
|
206
|
+
}
|
|
207
|
+
return parts
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** A client frame as the socket messages that carry it, in order. */
|
|
211
|
+
export function relayClientMessages(frame: RelayClientFrame): Uint8Array<ArrayBuffer>[] {
|
|
212
|
+
return fragments(CLIENT_FRAGMENT, encodeRelayClientFrame(frame))
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** A server frame as the socket messages that carry it, in order. */
|
|
216
|
+
export function relayServerMessages(frame: RelayServerFrame): Uint8Array<ArrayBuffer>[] {
|
|
217
|
+
return fragments(SERVER_FRAGMENT, encodeRelayServerFrame(frame))
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** What one socket message gave: a whole frame, part of one, or a reason to close. */
|
|
221
|
+
export type RelayAssembly =
|
|
222
|
+
| { type: 'frame'; data: Uint8Array }
|
|
223
|
+
| { type: 'partial' }
|
|
224
|
+
| { type: 'too-large' }
|
|
225
|
+
| { type: 'malformed' }
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Joins one socket's fragments into the frames they carry. A socket sends a frame's fragments in
|
|
229
|
+
* a row, so a whole frame arriving between them is malformed. `limit` caps the joined frame and is
|
|
230
|
+
* read for every message, so it can follow what the sender may do.
|
|
231
|
+
*/
|
|
232
|
+
function createAssembler(tag: number, limit: () => number) {
|
|
233
|
+
let parts: Uint8Array[] = []
|
|
234
|
+
let size = 0
|
|
235
|
+
/** Set while the rest of a refused frame arrives, so its tail never reads as a frame of its own. */
|
|
236
|
+
let discarding = false
|
|
237
|
+
const reset = () => {
|
|
238
|
+
parts = []
|
|
239
|
+
size = 0
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
push(data: Uint8Array): RelayAssembly {
|
|
243
|
+
let decoder: decoding.Decoder
|
|
244
|
+
let type: number
|
|
245
|
+
try {
|
|
246
|
+
decoder = decoding.createDecoder(data)
|
|
247
|
+
type = decoding.readVarUint(decoder)
|
|
248
|
+
} catch {
|
|
249
|
+
reset()
|
|
250
|
+
return { type: 'malformed' }
|
|
251
|
+
}
|
|
252
|
+
if (type !== tag) {
|
|
253
|
+
if (parts.length > 0) {
|
|
254
|
+
reset()
|
|
255
|
+
return { type: 'malformed' }
|
|
256
|
+
}
|
|
257
|
+
return { type: 'frame', data }
|
|
258
|
+
}
|
|
259
|
+
let more: number
|
|
260
|
+
let chunk: Uint8Array
|
|
261
|
+
try {
|
|
262
|
+
more = decoding.readVarUint(decoder)
|
|
263
|
+
chunk = decoding.readVarUint8Array(decoder)
|
|
264
|
+
} catch {
|
|
265
|
+
// A fragment that cannot be read is taken as unfinished: only a sound last one ends it.
|
|
266
|
+
reset()
|
|
267
|
+
discarding = true
|
|
268
|
+
return { type: 'malformed' }
|
|
269
|
+
}
|
|
270
|
+
if (decoder.pos !== data.byteLength || more > 1) {
|
|
271
|
+
reset()
|
|
272
|
+
discarding = more !== 0
|
|
273
|
+
return { type: 'malformed' }
|
|
274
|
+
}
|
|
275
|
+
if (discarding) {
|
|
276
|
+
discarding = more === 1
|
|
277
|
+
return { type: 'partial' }
|
|
278
|
+
}
|
|
279
|
+
size += chunk.byteLength
|
|
280
|
+
if (size > limit()) {
|
|
281
|
+
reset()
|
|
282
|
+
discarding = more === 1
|
|
283
|
+
return { type: 'too-large' }
|
|
284
|
+
}
|
|
285
|
+
parts.push(chunk.slice())
|
|
286
|
+
if (more === 1) return { type: 'partial' }
|
|
287
|
+
const joined = new Uint8Array(size)
|
|
288
|
+
let at = 0
|
|
289
|
+
for (const part of parts) {
|
|
290
|
+
joined.set(part, at)
|
|
291
|
+
at += part.byteLength
|
|
292
|
+
}
|
|
293
|
+
reset()
|
|
294
|
+
return { type: 'frame', data: joined }
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Joins the fragments a client sends; the relay holds one per socket. */
|
|
300
|
+
export function createRelayClientAssembler(limit: () => number) {
|
|
301
|
+
return createAssembler(CLIENT_FRAGMENT, limit)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Joins the fragments the relay sends; a client holds one per socket. */
|
|
305
|
+
export function createRelayServerAssembler(limit: () => number) {
|
|
306
|
+
return createAssembler(SERVER_FRAGMENT, limit)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* lib0 reads byte arrays as views of the underlying buffer, which can run past the end of the
|
|
311
|
+
* frame's own view, as with pooled socket buffers. A frame is valid only when decoding consumed
|
|
312
|
+
* exactly its bytes, and payloads are copied out of the shared buffer.
|
|
313
|
+
*/
|
|
314
|
+
function complete<T>(decoder: decoding.Decoder, data: Uint8Array, frame: T): T | null {
|
|
315
|
+
return decoder.pos === data.byteLength ? frame : null
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function readPayload(decoder: decoding.Decoder): Uint8Array {
|
|
319
|
+
return decoding.readVarUint8Array(decoder).slice()
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** Returns null for a frame that is malformed or of an unknown type. */
|
|
323
|
+
export function decodeRelayClientFrame(data: Uint8Array): RelayClientFrame | null {
|
|
324
|
+
try {
|
|
325
|
+
const decoder = decoding.createDecoder(data)
|
|
326
|
+
const type = decoding.readVarUint(decoder)
|
|
327
|
+
if (type === CLIENT_AUTH) {
|
|
328
|
+
return complete(decoder, data, { type: 'auth', token: decoding.readVarString(decoder) })
|
|
329
|
+
}
|
|
330
|
+
if (type !== CLIENT_MESSAGE) return null
|
|
331
|
+
const namespace = decoding.readVarString(decoder)
|
|
332
|
+
const target = decoding.readVarString(decoder)
|
|
333
|
+
const payload = readPayload(decoder)
|
|
334
|
+
return complete(decoder, data, { type: 'message', namespace, target: target || null, payload })
|
|
335
|
+
} catch {
|
|
336
|
+
return null
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** Returns null for a frame that is malformed or of an unknown type. */
|
|
341
|
+
export function decodeRelayServerFrame(data: Uint8Array): RelayServerFrame | null {
|
|
342
|
+
try {
|
|
343
|
+
const decoder = decoding.createDecoder(data)
|
|
344
|
+
switch (decoding.readVarUint(decoder)) {
|
|
345
|
+
case SERVER_WELCOME: {
|
|
346
|
+
const peerId = decoding.readVarString(decoder)
|
|
347
|
+
const count = decoding.readVarUint(decoder)
|
|
348
|
+
const peers: string[] = []
|
|
349
|
+
for (let index = 0; index < count; index++) peers.push(decoding.readVarString(decoder))
|
|
350
|
+
return complete(decoder, data, { type: 'welcome', peerId, peers })
|
|
351
|
+
}
|
|
352
|
+
case SERVER_JOIN:
|
|
353
|
+
return complete(decoder, data, { type: 'join', peerId: decoding.readVarString(decoder) })
|
|
354
|
+
case SERVER_LEAVE:
|
|
355
|
+
return complete(decoder, data, { type: 'leave', peerId: decoding.readVarString(decoder) })
|
|
356
|
+
case SERVER_MESSAGE: {
|
|
357
|
+
const namespace = decoding.readVarString(decoder)
|
|
358
|
+
const from = decoding.readVarString(decoder)
|
|
359
|
+
const payload = readPayload(decoder)
|
|
360
|
+
return complete(decoder, data, { type: 'message', namespace, from, payload })
|
|
361
|
+
}
|
|
362
|
+
default:
|
|
363
|
+
return null
|
|
364
|
+
}
|
|
365
|
+
} catch {
|
|
366
|
+
return null
|
|
367
|
+
}
|
|
368
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
import { collaborationPrincipalSchema } from './collaboration'
|
|
4
|
+
import { documentPermissionSchema, requestImageLinksSchema } from './documents'
|
|
5
|
+
import { workspaceRoleSchema } from './workspaces'
|
|
6
|
+
|
|
7
|
+
export const documentShareSchema = v.object({
|
|
8
|
+
id: v.pipe(v.string(), v.uuid()),
|
|
9
|
+
documentId: v.pipe(v.string(), v.uuid()),
|
|
10
|
+
permission: documentPermissionSchema,
|
|
11
|
+
roomEpoch: v.pipe(v.number(), v.integer(), v.minValue(0)),
|
|
12
|
+
createdBy: v.string(),
|
|
13
|
+
createdAt: v.string(),
|
|
14
|
+
updatedAt: v.string(),
|
|
15
|
+
expiresAt: v.nullable(v.string()),
|
|
16
|
+
revokedAt: v.nullable(v.string()),
|
|
17
|
+
lastUsedAt: v.nullable(v.string())
|
|
18
|
+
})
|
|
19
|
+
export type DocumentShare = v.InferOutput<typeof documentShareSchema>
|
|
20
|
+
|
|
21
|
+
/** When a link stops working, as an ISO timestamp; `null` keeps it working until revoked. */
|
|
22
|
+
const shareExpirySchema = v.optional(v.nullable(v.pipe(v.string(), v.isoTimestamp())))
|
|
23
|
+
|
|
24
|
+
export const createDocumentShareSchema = v.object({
|
|
25
|
+
permission: documentPermissionSchema,
|
|
26
|
+
expiresAt: shareExpirySchema
|
|
27
|
+
})
|
|
28
|
+
export type CreateDocumentShareInput = v.InferOutput<typeof createDocumentShareSchema>
|
|
29
|
+
|
|
30
|
+
export const updateDocumentShareSchema = v.object({
|
|
31
|
+
permission: v.optional(documentPermissionSchema),
|
|
32
|
+
expiresAt: shareExpirySchema
|
|
33
|
+
})
|
|
34
|
+
export type UpdateDocumentShareInput = v.InferOutput<typeof updateDocumentShareSchema>
|
|
35
|
+
|
|
36
|
+
export const resolveDocumentShareSchema = v.object({
|
|
37
|
+
secret: v.pipe(v.string(), v.minLength(32), v.maxLength(128)),
|
|
38
|
+
guestName: v.optional(v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(80))),
|
|
39
|
+
guestId: v.optional(v.pipe(v.string(), v.minLength(16), v.maxLength(128)))
|
|
40
|
+
})
|
|
41
|
+
export type ResolveDocumentShareInput = v.InferOutput<typeof resolveDocumentShareSchema>
|
|
42
|
+
|
|
43
|
+
/** A guest's request for the links of images a shared document's live room names. */
|
|
44
|
+
export const requestSharedImageLinksSchema = v.object({
|
|
45
|
+
...resolveDocumentShareSchema.entries,
|
|
46
|
+
...requestImageLinksSchema.entries
|
|
47
|
+
})
|
|
48
|
+
export type RequestSharedImageLinksInput = v.InferOutput<typeof requestSharedImageLinksSchema>
|
|
49
|
+
|
|
50
|
+
export function parseRequestSharedImageLinks(input: unknown): RequestSharedImageLinksInput {
|
|
51
|
+
return v.parse(requestSharedImageLinksSchema, input)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const resolvedDocumentShareSchema = v.object({
|
|
55
|
+
documentId: v.pipe(v.string(), v.uuid()),
|
|
56
|
+
permission: documentPermissionSchema,
|
|
57
|
+
principal: collaborationPrincipalSchema,
|
|
58
|
+
roomEpoch: v.pipe(v.number(), v.integer(), v.minValue(0))
|
|
59
|
+
})
|
|
60
|
+
export type ResolvedDocumentShare = v.InferOutput<typeof resolvedDocumentShareSchema>
|
|
61
|
+
|
|
62
|
+
export const cloudUserProfileSchema = v.object({
|
|
63
|
+
id: v.string(),
|
|
64
|
+
name: v.string(),
|
|
65
|
+
email: v.pipe(v.string(), v.email()),
|
|
66
|
+
image: v.nullable(v.pipe(v.string(), v.url()))
|
|
67
|
+
})
|
|
68
|
+
export type CloudUserProfile = v.InferOutput<typeof cloudUserProfileSchema>
|
|
69
|
+
|
|
70
|
+
export const documentGrantSchema = v.object({
|
|
71
|
+
id: v.pipe(v.string(), v.uuid()),
|
|
72
|
+
documentId: v.pipe(v.string(), v.uuid()),
|
|
73
|
+
userId: v.string(),
|
|
74
|
+
permission: documentPermissionSchema,
|
|
75
|
+
createdBy: v.string(),
|
|
76
|
+
createdAt: v.string(),
|
|
77
|
+
updatedAt: v.string()
|
|
78
|
+
})
|
|
79
|
+
export type DocumentGrant = v.InferOutput<typeof documentGrantSchema>
|
|
80
|
+
|
|
81
|
+
/** What accepting an invitation gave: access to a document, or a place in a workspace. */
|
|
82
|
+
export const acceptedInvitationSchema = v.variant('kind', [
|
|
83
|
+
v.object({ kind: v.literal('document'), grant: documentGrantSchema }),
|
|
84
|
+
v.object({
|
|
85
|
+
kind: v.literal('workspace'),
|
|
86
|
+
workspaceId: v.pipe(v.string(), v.uuid()),
|
|
87
|
+
role: workspaceRoleSchema
|
|
88
|
+
})
|
|
89
|
+
])
|
|
90
|
+
export type AcceptedInvitation = v.InferOutput<typeof acceptedInvitationSchema>
|
|
91
|
+
|
|
92
|
+
export const lookupCloudUserSchema = v.object({
|
|
93
|
+
email: v.pipe(v.string(), v.trim(), v.toLowerCase(), v.email())
|
|
94
|
+
})
|
|
95
|
+
export type LookupCloudUserInput = v.InferOutput<typeof lookupCloudUserSchema>
|
|
96
|
+
|
|
97
|
+
export const putDocumentGrantSchema = v.object({ permission: documentPermissionSchema })
|
|
98
|
+
export type PutDocumentGrantInput = v.InferOutput<typeof putDocumentGrantSchema>
|
|
99
|
+
|
|
100
|
+
export const createDocumentInvitationSchema = v.object({
|
|
101
|
+
email: v.pipe(v.string(), v.trim(), v.toLowerCase(), v.email()),
|
|
102
|
+
permission: documentPermissionSchema
|
|
103
|
+
})
|
|
104
|
+
export type CreateDocumentInvitationInput = v.InferOutput<typeof createDocumentInvitationSchema>
|
|
105
|
+
|
|
106
|
+
export const acceptDocumentInvitationSchema = v.object({
|
|
107
|
+
token: v.pipe(v.string(), v.minLength(32), v.maxLength(128))
|
|
108
|
+
})
|
|
109
|
+
export type AcceptDocumentInvitationInput = v.InferOutput<typeof acceptDocumentInvitationSchema>
|
|
110
|
+
|
|
111
|
+
export const createInvitationContinuationSchema = v.object({
|
|
112
|
+
invitationId: v.pipe(v.string(), v.uuid()),
|
|
113
|
+
token: v.pipe(v.string(), v.minLength(32), v.maxLength(128))
|
|
114
|
+
})
|
|
115
|
+
export type CreateInvitationContinuationInput = v.InferOutput<
|
|
116
|
+
typeof createInvitationContinuationSchema
|
|
117
|
+
>
|
|
118
|
+
|
|
119
|
+
export const invitationContinuationSchema = v.object({
|
|
120
|
+
id: v.pipe(v.string(), v.minLength(16), v.maxLength(128))
|
|
121
|
+
})
|
|
122
|
+
export type InvitationContinuation = v.InferOutput<typeof invitationContinuationSchema>
|
|
123
|
+
|
|
124
|
+
const invitationPreviewFields = {
|
|
125
|
+
inviterName: v.string(),
|
|
126
|
+
expiresAt: v.string(),
|
|
127
|
+
recipientHint: v.string()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** What an invitation link opens: one document, or a whole workspace. */
|
|
131
|
+
export const invitationPreviewSchema = v.variant('kind', [
|
|
132
|
+
v.object({
|
|
133
|
+
kind: v.literal('document'),
|
|
134
|
+
documentName: v.string(),
|
|
135
|
+
permission: documentPermissionSchema,
|
|
136
|
+
...invitationPreviewFields
|
|
137
|
+
}),
|
|
138
|
+
v.object({
|
|
139
|
+
kind: v.literal('workspace'),
|
|
140
|
+
workspaceName: v.string(),
|
|
141
|
+
role: workspaceRoleSchema,
|
|
142
|
+
...invitationPreviewFields
|
|
143
|
+
})
|
|
144
|
+
])
|
|
145
|
+
export type InvitationPreview = v.InferOutput<typeof invitationPreviewSchema>
|
|
146
|
+
|
|
147
|
+
export const documentInvitationSchema = v.object({
|
|
148
|
+
id: v.pipe(v.string(), v.uuid()),
|
|
149
|
+
documentId: v.pipe(v.string(), v.uuid()),
|
|
150
|
+
email: v.pipe(v.string(), v.email()),
|
|
151
|
+
permission: documentPermissionSchema,
|
|
152
|
+
invitedBy: v.string(),
|
|
153
|
+
invitedAt: v.string(),
|
|
154
|
+
expiresAt: v.string(),
|
|
155
|
+
acceptedAt: v.nullable(v.string())
|
|
156
|
+
})
|
|
157
|
+
export type DocumentInvitation = v.InferOutput<typeof documentInvitationSchema>
|
|
158
|
+
|
|
159
|
+
export function parseCreateInvitationContinuation(
|
|
160
|
+
input: unknown
|
|
161
|
+
): CreateInvitationContinuationInput {
|
|
162
|
+
return v.parse(createInvitationContinuationSchema, input)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function parseLookupCloudUser(input: unknown): LookupCloudUserInput {
|
|
166
|
+
return v.parse(lookupCloudUserSchema, input)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function parseCreateDocumentShare(input: unknown): CreateDocumentShareInput {
|
|
170
|
+
return v.parse(createDocumentShareSchema, input)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function parseUpdateDocumentShare(input: unknown): UpdateDocumentShareInput {
|
|
174
|
+
return v.parse(updateDocumentShareSchema, input)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export function parseResolveDocumentShare(input: unknown): ResolveDocumentShareInput {
|
|
178
|
+
return v.parse(resolveDocumentShareSchema, input)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function parsePutDocumentGrant(input: unknown): PutDocumentGrantInput {
|
|
182
|
+
return v.parse(putDocumentGrantSchema, input)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function parseCreateDocumentInvitation(input: unknown): CreateDocumentInvitationInput {
|
|
186
|
+
return v.parse(createDocumentInvitationSchema, input)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function parseAcceptDocumentInvitation(input: unknown): AcceptDocumentInvitationInput {
|
|
190
|
+
return v.parse(acceptDocumentInvitationSchema, input)
|
|
191
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
import { documentDownloadSchema, documentSummarySchema } from './documents'
|
|
4
|
+
|
|
5
|
+
/** How long a version's name may be; the history list shows the start of longer ones. */
|
|
6
|
+
export const DOCUMENT_VERSION_NAME_MAX_LENGTH = 100
|
|
7
|
+
export const DOCUMENT_VERSION_DESCRIPTION_MAX_LENGTH = 1000
|
|
8
|
+
|
|
9
|
+
/** A checkpoint the server kept on its own, or a version someone named. */
|
|
10
|
+
export const documentVersionKindSchema = v.picklist(['checkpoint', 'named'])
|
|
11
|
+
export type DocumentVersionKind = v.InferOutput<typeof documentVersionKindSchema>
|
|
12
|
+
|
|
13
|
+
export const documentVersionSchema = v.object({
|
|
14
|
+
id: v.pipe(v.string(), v.uuid()),
|
|
15
|
+
revisionId: v.pipe(v.string(), v.uuid()),
|
|
16
|
+
kind: documentVersionKindSchema,
|
|
17
|
+
name: v.nullable(v.string()),
|
|
18
|
+
description: v.nullable(v.string()),
|
|
19
|
+
createdAt: v.string(),
|
|
20
|
+
/** Who made the version: the author of a checkpoint's save, or who named or restored it. */
|
|
21
|
+
createdBy: v.object({ id: v.string(), name: v.nullable(v.string()) }),
|
|
22
|
+
/** The version this one restored, while that version is still in the history. */
|
|
23
|
+
restoredFromVersionId: v.nullable(v.pipe(v.string(), v.uuid())),
|
|
24
|
+
/** Whether the document holds this version now. */
|
|
25
|
+
current: v.boolean()
|
|
26
|
+
})
|
|
27
|
+
export type DocumentVersion = v.InferOutput<typeof documentVersionSchema>
|
|
28
|
+
|
|
29
|
+
export const documentVersionsSchema = v.object({
|
|
30
|
+
versions: v.array(documentVersionSchema),
|
|
31
|
+
/** Pass as `before` for the next, older page; null on the last one. */
|
|
32
|
+
nextCursor: v.nullable(v.pipe(v.string(), v.uuid()))
|
|
33
|
+
})
|
|
34
|
+
export type DocumentVersions = v.InferOutput<typeof documentVersionsSchema>
|
|
35
|
+
|
|
36
|
+
const versionNameSchema = v.pipe(
|
|
37
|
+
v.string(),
|
|
38
|
+
v.trim(),
|
|
39
|
+
v.minLength(1),
|
|
40
|
+
v.maxLength(DOCUMENT_VERSION_NAME_MAX_LENGTH)
|
|
41
|
+
)
|
|
42
|
+
const versionDescriptionSchema = v.pipe(
|
|
43
|
+
v.string(),
|
|
44
|
+
v.trim(),
|
|
45
|
+
v.maxLength(DOCUMENT_VERSION_DESCRIPTION_MAX_LENGTH)
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
/** Names the document as it is now, like Save to version history. */
|
|
49
|
+
export const saveDocumentVersionSchema = v.object({
|
|
50
|
+
name: versionNameSchema,
|
|
51
|
+
description: v.optional(versionDescriptionSchema, '')
|
|
52
|
+
})
|
|
53
|
+
export type SaveDocumentVersionInput = v.InferOutput<typeof saveDocumentVersionSchema>
|
|
54
|
+
|
|
55
|
+
/** Names a version or changes its name; a null name deletes its info and leaves a checkpoint. */
|
|
56
|
+
export const updateDocumentVersionSchema = v.object({
|
|
57
|
+
name: v.nullable(versionNameSchema),
|
|
58
|
+
description: v.optional(versionDescriptionSchema, '')
|
|
59
|
+
})
|
|
60
|
+
export type UpdateDocumentVersionInput = v.InferOutput<typeof updateDocumentVersionSchema>
|
|
61
|
+
|
|
62
|
+
export const duplicateDocumentVersionSchema = v.object({
|
|
63
|
+
name: v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(255))
|
|
64
|
+
})
|
|
65
|
+
export type DuplicateDocumentVersionInput = v.InferOutput<typeof duplicateDocumentVersionSchema>
|
|
66
|
+
|
|
67
|
+
export const documentVersionResponseSchema = v.object({ version: documentVersionSchema })
|
|
68
|
+
/** A version with where to download it, so a link to it can show it without the history. */
|
|
69
|
+
export const documentVersionDownloadSchema = v.object({
|
|
70
|
+
document: documentDownloadSchema,
|
|
71
|
+
version: documentVersionSchema
|
|
72
|
+
})
|
|
73
|
+
export type DocumentVersionDownload = v.InferOutput<typeof documentVersionDownloadSchema>
|
|
74
|
+
export const restoredDocumentSchema = v.object({
|
|
75
|
+
document: documentSummarySchema,
|
|
76
|
+
version: documentVersionSchema
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
export function parseSaveDocumentVersion(input: unknown): SaveDocumentVersionInput {
|
|
80
|
+
return v.parse(saveDocumentVersionSchema, input)
|
|
81
|
+
}
|
|
82
|
+
export function parseUpdateDocumentVersion(input: unknown): UpdateDocumentVersionInput {
|
|
83
|
+
return v.parse(updateDocumentVersionSchema, input)
|
|
84
|
+
}
|
|
85
|
+
export function parseDuplicateDocumentVersion(input: unknown): DuplicateDocumentVersionInput {
|
|
86
|
+
return v.parse(duplicateDocumentVersionSchema, input)
|
|
87
|
+
}
|