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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/dist/listen.mjs +236 -117
  2. package/dist/listen.mjs.map +1 -1
  3. package/dist/runtime-node.d.mts +2937 -703
  4. package/dist/runtime-node.d.mts.map +1 -1
  5. package/dist/server.d.ts +3985 -941
  6. package/dist/server.d.ts.map +1 -1
  7. package/dist/server.js +833 -376
  8. package/dist/server.js.map +1 -1
  9. package/dist/server.mjs +665 -271
  10. package/dist/server.mjs.map +1 -1
  11. package/package.json +4 -2
  12. package/src/admin/api/routes.ts +44 -33
  13. package/src/runtime/cloudflare/environment.ts +16 -2
  14. package/src/runtime/cloudflare/relay/index.ts +128 -0
  15. package/src/runtime/cloudflare/relay/sockets.ts +112 -0
  16. package/src/runtime/cloudflare/worker.ts +56 -19
  17. package/src/runtime/node/bootstrap.ts +9 -4
  18. package/src/runtime/node/server.ts +40 -8
  19. package/src/runtime/posthog/errors.ts +48 -0
  20. package/src/runtime/s3/objects.ts +26 -0
  21. package/src/runtime/site/server.ts +5 -3
  22. package/src/server/api/router.ts +3 -2
  23. package/src/server/api/types.ts +7 -0
  24. package/src/server/app.ts +4 -0
  25. package/src/server/cleanup/objects.ts +10 -2
  26. package/src/server/cleanup/revisions.ts +8 -2
  27. package/src/server/collaboration/index.ts +3 -0
  28. package/src/server/collaboration/relay/connection.ts +195 -0
  29. package/src/server/collaboration/relay/hub.ts +199 -187
  30. package/src/server/collaboration/relay/index.ts +3 -1
  31. package/src/server/collaboration/relay/log.ts +76 -0
  32. package/src/server/collaboration/relay/persistence.ts +147 -22
  33. package/src/server/collaboration/relay/resume.ts +28 -0
  34. package/src/server/collaboration/routes.ts +27 -16
  35. package/src/server/config/deployment.ts +11 -0
  36. package/src/server/config/schema.ts +24 -2
  37. package/src/server/db/migrate.ts +3 -1
  38. package/src/server/db/migrations/028_collaboration_updates.ts +84 -0
  39. package/src/server/db/tables.ts +18 -4
  40. package/src/server/documents/routes.ts +106 -80
  41. package/src/server/documents/versions/routes.ts +36 -25
  42. package/src/server/documents/versions/service.ts +9 -0
  43. package/src/server/errors/index.ts +56 -0
  44. package/src/server/index.ts +11 -0
  45. package/src/server/internal/routes.ts +3 -8
  46. package/src/server/objects/types.ts +12 -0
  47. package/src/server/pictures/routes.ts +4 -2
  48. package/src/server/rate-limit/index.ts +4 -1
  49. package/src/server/rate-limit/middleware.ts +99 -11
  50. package/src/server/sharing/document-routes.ts +30 -22
  51. package/src/server/sharing/routes.ts +122 -83
  52. package/src/server/validation.ts +58 -2
  53. package/src/server/workspaces/routes.ts +63 -45
  54. package/src/runtime/cloudflare/relay.ts +0 -136
@@ -6,8 +6,18 @@ import {
6
6
  } from '#cloud/contract'
7
7
  import type { CloudActor } from '#cloud/server/auth'
8
8
  import type { CloudDatabase } from '#cloud/server/db'
9
- import { CLOUD_RATE_LIMITS, createRouteRateLimiter } from '#cloud/server/rate-limit'
10
- import { validatedJSON } from '#cloud/server/validation'
9
+ import {
10
+ CLOUD_RATE_LIMITS,
11
+ createRouteRateLimiter,
12
+ routeParamResource,
13
+ createResourceRateLimiter
14
+ } from '#cloud/server/rate-limit'
15
+ import {
16
+ validatedJSON,
17
+ workspaceInvitationParams,
18
+ workspaceMemberParams,
19
+ workspaceParams
20
+ } from '#cloud/server/validation'
11
21
  import {
12
22
  WorkspaceAlreadyMemberError,
13
23
  WorkspaceForbiddenError,
@@ -71,22 +81,22 @@ export function createWorkspaceRoutes(
71
81
  // Resending mails the invitation again, so it counts against the same budget as sending.
72
82
  router.use(
73
83
  '/:workspaceId/invitations/:invitationId/resend',
74
- createRouteRateLimiter(
84
+ createResourceRateLimiter(
75
85
  rateLimit.database,
76
86
  rateLimit.secret,
77
87
  CLOUD_RATE_LIMITS.invitationMutation,
78
- () => false,
79
- (context) => context.req.param('workspaceId') ?? 'unknown'
88
+ routeParamResource('workspaceId'),
89
+ () => false
80
90
  )
81
91
  )
82
92
  router.use(
83
93
  '/:workspaceId/invitations',
84
- createRouteRateLimiter(
94
+ createResourceRateLimiter(
85
95
  rateLimit.database,
86
96
  rateLimit.secret,
87
97
  CLOUD_RATE_LIMITS.invitationMutation,
88
- (context) => context.req.method !== 'POST',
89
- (context) => context.req.param('workspaceId') ?? 'unknown'
98
+ routeParamResource('workspaceId'),
99
+ (context) => context.req.method !== 'POST'
90
100
  )
91
101
  )
92
102
  }
@@ -109,88 +119,96 @@ export function createWorkspaceRoutes(
109
119
  throw error
110
120
  }
111
121
  })
112
- .get('/:workspaceId', async (context) => {
122
+ .get('/:workspaceId', workspaceParams, async (context) => {
113
123
  const workspace = await service.get(
114
124
  context.get('actor').userId,
115
- context.req.param('workspaceId')
125
+ context.req.valid('param').workspaceId
116
126
  )
117
127
  if (!workspace) return context.json({ error: { code: 'not_found' as const } }, 404)
118
128
  return context.json({ workspace })
119
129
  })
120
- .patch('/:workspaceId', validatedJSON(parseUpdateWorkspace), (context) =>
130
+ .patch('/:workspaceId', workspaceParams, validatedJSON(parseUpdateWorkspace), (context) =>
121
131
  memberRoute(context, async () =>
122
132
  context.json({
123
133
  workspace: await members.rename(
124
134
  context.get('actor').userId,
125
- context.req.param('workspaceId'),
135
+ context.req.valid('param').workspaceId,
126
136
  context.req.valid('json').name
127
137
  )
128
138
  })
129
139
  )
130
140
  )
131
- .delete('/:workspaceId', (context) =>
141
+ .delete('/:workspaceId', workspaceParams, (context) =>
132
142
  memberRoute(context, async () => {
133
- await members.delete(context.get('actor').userId, context.req.param('workspaceId'))
143
+ await members.delete(context.get('actor').userId, context.req.valid('param').workspaceId)
134
144
  return context.body(null, 204)
135
145
  })
136
146
  )
137
- .get('/:workspaceId/members', (context) =>
147
+ .get('/:workspaceId/members', workspaceParams, (context) =>
138
148
  memberRoute(context, async () =>
139
149
  context.json(
140
- await members.list(context.get('actor').userId, context.req.param('workspaceId'))
150
+ await members.list(context.get('actor').userId, context.req.valid('param').workspaceId)
141
151
  )
142
152
  )
143
153
  )
144
- .post('/:workspaceId/invitations', validatedJSON(parseCreateWorkspaceInvitation), (context) =>
145
- memberRoute(context, async () =>
146
- context.json(
147
- await members.invite(
148
- context.get('actor').userId,
149
- context.req.param('workspaceId'),
150
- context.req.valid('json')
151
- ),
152
- 201
154
+ .post(
155
+ '/:workspaceId/invitations',
156
+ workspaceParams,
157
+ validatedJSON(parseCreateWorkspaceInvitation),
158
+ (context) =>
159
+ memberRoute(context, async () =>
160
+ context.json(
161
+ await members.invite(
162
+ context.get('actor').userId,
163
+ context.req.valid('param').workspaceId,
164
+ context.req.valid('json')
165
+ ),
166
+ 201
167
+ )
153
168
  )
154
- )
155
169
  )
156
- .post('/:workspaceId/invitations/:invitationId/resend', (context) =>
170
+ .post('/:workspaceId/invitations/:invitationId/resend', workspaceInvitationParams, (context) =>
157
171
  memberRoute(context, async () =>
158
172
  context.json(
159
173
  await members.resendInvitation(
160
174
  context.get('actor').userId,
161
- context.req.param('workspaceId'),
162
- context.req.param('invitationId')
175
+ context.req.valid('param').workspaceId,
176
+ context.req.valid('param').invitationId
163
177
  )
164
178
  )
165
179
  )
166
180
  )
167
- .delete('/:workspaceId/invitations/:invitationId', (context) =>
181
+ .delete('/:workspaceId/invitations/:invitationId', workspaceInvitationParams, (context) =>
168
182
  memberRoute(context, async () => {
169
183
  await members.revokeInvitation(
170
184
  context.get('actor').userId,
171
- context.req.param('workspaceId'),
172
- context.req.param('invitationId')
185
+ context.req.valid('param').workspaceId,
186
+ context.req.valid('param').invitationId
173
187
  )
174
188
  return context.body(null, 204)
175
189
  })
176
190
  )
177
- .patch('/:workspaceId/members/:userId', validatedJSON(parseUpdateWorkspaceMember), (context) =>
178
- memberRoute(context, async () => {
179
- await members.updateRole(
180
- context.get('actor').userId,
181
- context.req.param('workspaceId'),
182
- context.req.param('userId'),
183
- context.req.valid('json').role
184
- )
185
- return context.body(null, 204)
186
- })
191
+ .patch(
192
+ '/:workspaceId/members/:userId',
193
+ workspaceMemberParams,
194
+ validatedJSON(parseUpdateWorkspaceMember),
195
+ (context) =>
196
+ memberRoute(context, async () => {
197
+ await members.updateRole(
198
+ context.get('actor').userId,
199
+ context.req.valid('param').workspaceId,
200
+ context.req.valid('param').userId,
201
+ context.req.valid('json').role
202
+ )
203
+ return context.body(null, 204)
204
+ })
187
205
  )
188
- .delete('/:workspaceId/members/:userId', (context) =>
206
+ .delete('/:workspaceId/members/:userId', workspaceMemberParams, (context) =>
189
207
  memberRoute(context, async () => {
190
208
  await members.remove(
191
209
  context.get('actor').userId,
192
- context.req.param('workspaceId'),
193
- context.req.param('userId')
210
+ context.req.valid('param').workspaceId,
211
+ context.req.valid('param').userId
194
212
  )
195
213
  return context.body(null, 204)
196
214
  })
@@ -1,136 +0,0 @@
1
- import { RELAY_DOCUMENT_PARAM } from '#cloud/contract'
2
- import { createPostgresCloudDatabase } from '#cloud/runtime/postgres/database'
3
- import {
4
- createCollaborationRelay,
5
- createCollaborationStateStore,
6
- createConfiguredCloudPolicy,
7
- relayDocumentLookups,
8
- type CollaborationRelay,
9
- type RelayConnection
10
- } from '#cloud/server'
11
- import { DurableObject } from 'cloudflare:workers'
12
- import type { Context } from 'hono'
13
- import * as v from 'valibot'
14
-
15
- import type { CloudflareCloudEnvironment, PreparedCloudflareCloud } from './environment'
16
-
17
- /** What the Worker calls on a document's relay object. */
18
- export interface CollaborationRelayRPC extends DurableObject<CloudflareCloudEnvironment> {
19
- /** Ends the document's live rooms after a restore, without saving them. */
20
- startOver(documentId: string): void
21
- /** Tells the document's live rooms its version history changed. */
22
- announceVersions(documentId: string): void
23
- }
24
-
25
- const documentIdSchema = v.pipe(v.string(), v.uuid())
26
-
27
- /** The document's relay object. */
28
- export function relayFor(env: CloudflareCloudEnvironment, documentId: string) {
29
- return env.RELAY.get(env.RELAY.idFromName(documentId))
30
- }
31
-
32
- /**
33
- * Sends a relay socket to its document's relay object. The ticket's URL names the document; the
34
- * object refuses tickets for any other.
35
- */
36
- export async function routeToRelay(
37
- c: Context<{ Bindings: CloudflareCloudEnvironment }>
38
- ): Promise<Response> {
39
- if (c.req.header('Upgrade')?.toLowerCase() !== 'websocket') {
40
- return c.text('Expected a WebSocket upgrade', 426)
41
- }
42
- const documentId = c.req.query(RELAY_DOCUMENT_PARAM)
43
- if (!v.is(documentIdSchema, documentId)) return c.text('Missing document', 400)
44
- return relayFor(c.env, documentId).fetch(c.req.raw)
45
- }
46
-
47
- /** A socket message's bytes; text is passed on as bytes for the relay to refuse. */
48
- function bytes(data: unknown): Uint8Array {
49
- if (data instanceof ArrayBuffer) return new Uint8Array(data)
50
- return new TextEncoder().encode(typeof data === 'string' ? data : '')
51
- }
52
-
53
- /** Hands a WebSocket the object accepted to the relay, as the Node runtime does with `ws`. */
54
- function accept(relay: CollaborationRelay): Response {
55
- const { 0: client, 1: server } = new WebSocketPair()
56
- // Standard sockets deliver binary messages as Blobs unless asked otherwise.
57
- server.binaryType = 'arraybuffer'
58
- server.accept()
59
- let connection: RelayConnection | null = null
60
- const closed = () => {
61
- connection?.closed()
62
- connection = null
63
- }
64
- // A closing socket throws on send and close, which would stop a broadcast to the others.
65
- const open = () => server.readyState === WebSocket.OPEN
66
- connection = relay.connect({
67
- send(data) {
68
- if (open()) server.send(data)
69
- },
70
- close(code, reason) {
71
- if (open()) server.close(code, reason)
72
- }
73
- })
74
- server.addEventListener('message', ({ data }: { data: unknown }) =>
75
- connection?.receive(bytes(data))
76
- )
77
- server.addEventListener('close', closed)
78
- server.addEventListener('error', closed)
79
- return new Response(null, { status: 101, webSocket: client })
80
- }
81
-
82
- /**
83
- * The Durable Object class holding one document's live rooms: every epoch of the document meets
84
- * on one relay, so a restore or a version change reaches all of them in one call. Sockets stay
85
- * open without hibernation, so the rooms' documents and timers live in memory as on Node.
86
- */
87
- export function createCloudflareRelayObject(
88
- prepare: (env: CloudflareCloudEnvironment) => Promise<PreparedCloudflareCloud>
89
- ) {
90
- return class CollaborationRelayObject
91
- extends DurableObject<CloudflareCloudEnvironment>
92
- implements CollaborationRelayRPC
93
- {
94
- #relay: Promise<CollaborationRelay> | null = null
95
- /** Set by the first socket; restores and version changes before it have no rooms to reach. */
96
- #opened: CollaborationRelay | null = null
97
-
98
- #open(documentId: string) {
99
- this.#relay ??= prepare(this.env).then(({ config }) => {
100
- const database = createPostgresCloudDatabase({ connectionString: config.databaseURL })
101
- this.#opened = createCollaborationRelay({
102
- authSecret: config.authSecret,
103
- documentId,
104
- store: createCollaborationStateStore(database),
105
- maximumMessageBytes: config.technicalLimits.maximumCollaborationMessageBytes,
106
- maximumConnectionsPerRoom: config.technicalLimits.maximumConnectionsPerRoom,
107
- ...relayDocumentLookups(
108
- database,
109
- createConfiguredCloudPolicy(config, database),
110
- config.deployment
111
- )
112
- })
113
- return this.#opened
114
- })
115
- this.#relay.catch(() => {
116
- this.#relay = null
117
- })
118
- return this.#relay
119
- }
120
-
121
- /** Accepts a socket that `routeToRelay` sent here. */
122
- override async fetch(request: Request): Promise<Response> {
123
- const documentId = new URL(request.url).searchParams.get(RELAY_DOCUMENT_PARAM)
124
- if (!v.is(documentIdSchema, documentId)) return new Response(null, { status: 400 })
125
- return accept(await this.#open(documentId))
126
- }
127
-
128
- startOver(documentId: string) {
129
- this.#opened?.startOver(documentId)
130
- }
131
-
132
- announceVersions(documentId: string) {
133
- this.#opened?.announceVersions(documentId)
134
- }
135
- }
136
- }