@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.
- package/dist/listen.mjs +236 -117
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +2937 -703
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +3985 -941
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +833 -376
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +665 -271
- package/dist/server.mjs.map +1 -1
- package/package.json +4 -2
- package/src/admin/api/routes.ts +44 -33
- package/src/runtime/cloudflare/environment.ts +16 -2
- package/src/runtime/cloudflare/relay/index.ts +128 -0
- package/src/runtime/cloudflare/relay/sockets.ts +112 -0
- package/src/runtime/cloudflare/worker.ts +56 -19
- package/src/runtime/node/bootstrap.ts +9 -4
- package/src/runtime/node/server.ts +40 -8
- package/src/runtime/posthog/errors.ts +48 -0
- package/src/runtime/s3/objects.ts +26 -0
- package/src/runtime/site/server.ts +5 -3
- package/src/server/api/router.ts +3 -2
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +4 -0
- package/src/server/cleanup/objects.ts +10 -2
- package/src/server/cleanup/revisions.ts +8 -2
- package/src/server/collaboration/index.ts +3 -0
- package/src/server/collaboration/relay/connection.ts +195 -0
- package/src/server/collaboration/relay/hub.ts +199 -187
- package/src/server/collaboration/relay/index.ts +3 -1
- package/src/server/collaboration/relay/log.ts +76 -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/config/deployment.ts +11 -0
- package/src/server/config/schema.ts +24 -2
- package/src/server/db/migrate.ts +3 -1
- package/src/server/db/migrations/028_collaboration_updates.ts +84 -0
- package/src/server/db/tables.ts +18 -4
- package/src/server/documents/routes.ts +106 -80
- package/src/server/documents/versions/routes.ts +36 -25
- package/src/server/documents/versions/service.ts +9 -0
- package/src/server/errors/index.ts +56 -0
- package/src/server/index.ts +11 -0
- package/src/server/internal/routes.ts +3 -8
- package/src/server/objects/types.ts +12 -0
- package/src/server/pictures/routes.ts +4 -2
- package/src/server/rate-limit/index.ts +4 -1
- package/src/server/rate-limit/middleware.ts +99 -11
- package/src/server/sharing/document-routes.ts +30 -22
- package/src/server/sharing/routes.ts +122 -83
- package/src/server/validation.ts +58 -2
- package/src/server/workspaces/routes.ts +63 -45
- package/src/runtime/cloudflare/relay.ts +0 -136
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-pencil/cloud",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0-beta.2",
|
|
4
4
|
"license": "Apache-2.0 AND MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"@hono/node-server": "^1.19.17",
|
|
23
23
|
"@internationalized/date": "^3.12.3",
|
|
24
24
|
"@noble/hashes": "^2.4.0",
|
|
25
|
-
"@open-pencil/scene-graph": "^0.
|
|
25
|
+
"@open-pencil/scene-graph": "^0.17.0-beta.2",
|
|
26
26
|
"@openfeature/server-sdk": "^1.23.0",
|
|
27
27
|
"@vue-email/components": "^0.0.21",
|
|
28
28
|
"@vue-email/render": "^0.0.9",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"kysely": "^0.29.6",
|
|
40
40
|
"lib0": "^0.2.117",
|
|
41
41
|
"nodemailer": "^9.0.5",
|
|
42
|
+
"posthog-node": "5.56.0",
|
|
42
43
|
"smol-toml": "^1.8.0",
|
|
43
44
|
"ufo": "^1.6.4",
|
|
44
45
|
"valibot": "^1.5.0",
|
|
@@ -100,6 +101,7 @@
|
|
|
100
101
|
"#cloud/server/documents": "./src/server/documents/index.ts",
|
|
101
102
|
"#cloud/server/documents/versions": "./src/server/documents/versions/index.ts",
|
|
102
103
|
"#cloud/server/email": "./src/server/email/index.ts",
|
|
104
|
+
"#cloud/server/errors": "./src/server/errors/index.ts",
|
|
103
105
|
"#cloud/server/internal": "./src/server/internal/index.ts",
|
|
104
106
|
"#cloud/server/invitations": "./src/server/invitations/index.ts",
|
|
105
107
|
"#cloud/server/objects": "./src/server/objects/index.ts",
|
package/src/admin/api/routes.ts
CHANGED
|
@@ -20,7 +20,7 @@ import type { CloudAPIEnvironment } from '#cloud/server/api'
|
|
|
20
20
|
import type { CloudAuthAdapter } from '#cloud/server/auth'
|
|
21
21
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
22
22
|
import { CLOUD_RATE_LIMITS, createActorRateLimiter } from '#cloud/server/rate-limit'
|
|
23
|
-
import { validatedJSON, validatedQuery } from '#cloud/server/validation'
|
|
23
|
+
import { idParams, validatedJSON, validatedQuery } from '#cloud/server/validation'
|
|
24
24
|
import { Hono, type Context } from 'hono'
|
|
25
25
|
import { NoResultError, type Kysely } from 'kysely'
|
|
26
26
|
import * as v from 'valibot'
|
|
@@ -123,42 +123,53 @@ export function createCloudAdminRoutes(
|
|
|
123
123
|
.get('/enrollments', validatedQuery(parseEnrollmentQuery), async (context) =>
|
|
124
124
|
context.json(await services.enrollment.list(context.req.valid('query')))
|
|
125
125
|
)
|
|
126
|
-
.post(
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
126
|
+
.post(
|
|
127
|
+
'/enrollments/:id/approve',
|
|
128
|
+
idParams,
|
|
129
|
+
validatedJSON(parseEnrollmentReview),
|
|
130
|
+
async (context) =>
|
|
131
|
+
context.json({
|
|
132
|
+
enrollment: await services.enrollment.review(
|
|
133
|
+
context.get('actor').userId,
|
|
134
|
+
context.req.valid('param').id,
|
|
135
|
+
'approved',
|
|
136
|
+
context.req.valid('json')
|
|
137
|
+
)
|
|
138
|
+
})
|
|
135
139
|
)
|
|
136
|
-
.post(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
140
|
+
.post(
|
|
141
|
+
'/enrollments/:id/reject',
|
|
142
|
+
idParams,
|
|
143
|
+
validatedJSON(parseEnrollmentReview),
|
|
144
|
+
async (context) =>
|
|
145
|
+
context.json({
|
|
146
|
+
enrollment: await services.enrollment.review(
|
|
147
|
+
context.get('actor').userId,
|
|
148
|
+
context.req.valid('param').id,
|
|
149
|
+
'rejected',
|
|
150
|
+
context.req.valid('json')
|
|
151
|
+
)
|
|
152
|
+
})
|
|
145
153
|
)
|
|
146
|
-
.post(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
154
|
+
.post(
|
|
155
|
+
'/enrollments/:id/revoke',
|
|
156
|
+
idParams,
|
|
157
|
+
validatedJSON(parseEnrollmentReview),
|
|
158
|
+
async (context) =>
|
|
159
|
+
context.json({
|
|
160
|
+
enrollment: await services.enrollment.review(
|
|
161
|
+
context.get('actor').userId,
|
|
162
|
+
context.req.valid('param').id,
|
|
163
|
+
'revoked',
|
|
164
|
+
context.req.valid('json')
|
|
165
|
+
)
|
|
166
|
+
})
|
|
155
167
|
)
|
|
156
168
|
.get('/users', validatedQuery(parseUserQuery), async (context) =>
|
|
157
169
|
context.json(await services.users.list(context.req.valid('query')))
|
|
158
170
|
)
|
|
159
|
-
.get('/users/:id', async (context) => {
|
|
160
|
-
const
|
|
161
|
-
const detail = v.is(accountIdSchema, id) ? await services.users.detail(id) : null
|
|
171
|
+
.get('/users/:id', idParams, async (context) => {
|
|
172
|
+
const detail = await services.users.detail(context.req.valid('param').id)
|
|
162
173
|
if (!detail) return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
163
174
|
return context.json(detail)
|
|
164
175
|
})
|
|
@@ -209,8 +220,8 @@ export function createCloudAdminRoutes(
|
|
|
209
220
|
.get('/email', validatedQuery(parseEmailQuery), async (context) =>
|
|
210
221
|
context.json(await services.email.list(context.req.valid('query')))
|
|
211
222
|
)
|
|
212
|
-
.post('/email/:id/regenerate', async (context) => {
|
|
213
|
-
await services.email.regenerate(context.get('actor').userId, context.req.
|
|
223
|
+
.post('/email/:id/regenerate', idParams, async (context) => {
|
|
224
|
+
await services.email.regenerate(context.get('actor').userId, context.req.valid('param').id)
|
|
214
225
|
return context.json({ ok: true as const })
|
|
215
226
|
})
|
|
216
227
|
.get('/audit', validatedQuery(parseAuditQuery), async (context) =>
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { createPostHogErrorReporter } from '#cloud/runtime/posthog/errors'
|
|
1
2
|
import {
|
|
2
3
|
loadAISigningKey,
|
|
3
4
|
parseCloudDeploymentTOML,
|
|
4
5
|
type AISigningKey,
|
|
5
6
|
type CloudEnvironment,
|
|
7
|
+
type CloudErrorReporter,
|
|
6
8
|
type CloudServerConfig
|
|
7
9
|
} from '#cloud/server'
|
|
8
10
|
|
|
@@ -26,6 +28,8 @@ export type CloudflareCloudEnvironment = CloudflareCloudBindings & Record<string
|
|
|
26
28
|
export type PreparedCloudflareCloud = {
|
|
27
29
|
config: CloudServerConfig
|
|
28
30
|
aiSigningKey: AISigningKey | undefined
|
|
31
|
+
/** Set when the deployment configures error tracking. */
|
|
32
|
+
reportError: CloudErrorReporter | undefined
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
/** The Worker's string variables and secrets, with `DATABASE_URL` from Hyperdrive. */
|
|
@@ -44,14 +48,24 @@ function configEnvironment(env: CloudflareCloudEnvironment): CloudEnvironment {
|
|
|
44
48
|
*/
|
|
45
49
|
export function cloudflareCloudSetup(source: string) {
|
|
46
50
|
let prepared: Promise<PreparedCloudflareCloud> | null = null
|
|
47
|
-
|
|
51
|
+
/** Set once an attempt's config parses, so its later setup steps that fail can be reported. */
|
|
52
|
+
let reportError: CloudErrorReporter | undefined
|
|
53
|
+
const prepare = (env: CloudflareCloudEnvironment): Promise<PreparedCloudflareCloud> => {
|
|
48
54
|
prepared ??= (async () => {
|
|
55
|
+
// An attempt whose config fails to parse has no project to report to, not an earlier one's.
|
|
56
|
+
reportError = undefined
|
|
49
57
|
const config = parseCloudDeploymentTOML(source, configEnvironment(env))
|
|
50
|
-
|
|
58
|
+
reportError = createPostHogErrorReporter(config, 'cloudflare')
|
|
59
|
+
return { config, aiSigningKey: await loadAISigningKey(config), reportError }
|
|
51
60
|
})().catch((error: unknown) => {
|
|
52
61
|
prepared = null
|
|
53
62
|
throw error
|
|
54
63
|
})
|
|
55
64
|
return prepared
|
|
56
65
|
}
|
|
66
|
+
return {
|
|
67
|
+
prepare,
|
|
68
|
+
/** The reporter of the latest attempt, once its config parsed, if it configures error tracking. */
|
|
69
|
+
reporter: () => reportError
|
|
70
|
+
}
|
|
57
71
|
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { RELAY_DOCUMENT_PARAM } from '#cloud/contract'
|
|
2
|
+
import { createPostgresCloudDatabase } from '#cloud/runtime/postgres/database'
|
|
3
|
+
import { createS3ObjectStore } from '#cloud/runtime/s3/objects'
|
|
4
|
+
import {
|
|
5
|
+
createCollaborationRelay,
|
|
6
|
+
createCollaborationStateStore,
|
|
7
|
+
createConfiguredCloudPolicy,
|
|
8
|
+
relayDocumentLookups,
|
|
9
|
+
reportCloudError,
|
|
10
|
+
type CollaborationRelay
|
|
11
|
+
} from '#cloud/server'
|
|
12
|
+
import { DurableObject } from 'cloudflare:workers'
|
|
13
|
+
import type { Context } from 'hono'
|
|
14
|
+
import * as v from 'valibot'
|
|
15
|
+
|
|
16
|
+
import type { CloudflareCloudEnvironment, PreparedCloudflareCloud } from '../environment'
|
|
17
|
+
import { createRelaySockets } from './sockets'
|
|
18
|
+
|
|
19
|
+
/** What the Worker calls on a document's relay object. */
|
|
20
|
+
export interface CollaborationRelayRPC extends DurableObject<CloudflareCloudEnvironment> {
|
|
21
|
+
/** Ends the document's live rooms after a restore, without saving them. */
|
|
22
|
+
startOver(documentId: string): Promise<void>
|
|
23
|
+
/** Tells the document's live rooms its version history changed. */
|
|
24
|
+
announceVersions(documentId: string): Promise<void>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const documentIdSchema = v.pipe(v.string(), v.uuid())
|
|
28
|
+
|
|
29
|
+
/** The document's relay object. */
|
|
30
|
+
export function relayFor(env: CloudflareCloudEnvironment, documentId: string) {
|
|
31
|
+
return env.RELAY.get(env.RELAY.idFromName(documentId))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Sends a relay socket to its document's relay object. The ticket's URL names the document; the
|
|
36
|
+
* object refuses tickets for any other.
|
|
37
|
+
*/
|
|
38
|
+
export async function routeToRelay(
|
|
39
|
+
c: Context<{ Bindings: CloudflareCloudEnvironment }>
|
|
40
|
+
): Promise<Response> {
|
|
41
|
+
if (c.req.header('Upgrade')?.toLowerCase() !== 'websocket') {
|
|
42
|
+
return c.text('Expected a WebSocket upgrade', 426)
|
|
43
|
+
}
|
|
44
|
+
const documentId = c.req.query(RELAY_DOCUMENT_PARAM)
|
|
45
|
+
if (!v.is(documentIdSchema, documentId)) return c.text('Missing document', 400)
|
|
46
|
+
return relayFor(c.env, documentId).fetch(c.req.raw)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The relay over the deployment's PostgreSQL and object store. */
|
|
50
|
+
function configuredRelay(
|
|
51
|
+
{ config }: PreparedCloudflareCloud,
|
|
52
|
+
documentId: string,
|
|
53
|
+
onError: (message: string, error: unknown) => void
|
|
54
|
+
): CollaborationRelay {
|
|
55
|
+
const database = createPostgresCloudDatabase({ connectionString: config.databaseURL })
|
|
56
|
+
return createCollaborationRelay({
|
|
57
|
+
authSecret: config.authSecret,
|
|
58
|
+
documentId,
|
|
59
|
+
store: createCollaborationStateStore(database, createS3ObjectStore(config)),
|
|
60
|
+
maximumMessageBytes: config.technicalLimits.maximumCollaborationMessageBytes,
|
|
61
|
+
maximumConnectionsPerRoom: config.technicalLimits.maximumConnectionsPerRoom,
|
|
62
|
+
onError,
|
|
63
|
+
...relayDocumentLookups(
|
|
64
|
+
database,
|
|
65
|
+
createConfiguredCloudPolicy(config, database),
|
|
66
|
+
config.deployment
|
|
67
|
+
)
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The Durable Object class holding one document's live rooms: every epoch of the document meets
|
|
73
|
+
* on one relay, so a restore or a version change reaches all of them in one call. Its sockets
|
|
74
|
+
* hibernate with it (see `createRelaySockets`).
|
|
75
|
+
*/
|
|
76
|
+
export function createCloudflareRelayObject(
|
|
77
|
+
prepare: (env: CloudflareCloudEnvironment) => Promise<PreparedCloudflareCloud>
|
|
78
|
+
) {
|
|
79
|
+
return class CollaborationRelayObject
|
|
80
|
+
extends DurableObject<CloudflareCloudEnvironment>
|
|
81
|
+
implements CollaborationRelayRPC
|
|
82
|
+
{
|
|
83
|
+
readonly #sockets = createRelaySockets({
|
|
84
|
+
sockets: () => this.ctx.getWebSockets(),
|
|
85
|
+
createRelay: async (documentId) => {
|
|
86
|
+
const prepared = await prepare(this.env)
|
|
87
|
+
return configuredRelay(prepared, documentId, (message, error) =>
|
|
88
|
+
this.ctx.waitUntil(
|
|
89
|
+
reportCloudError(prepared.reportError, message, error, { source: 'relay' })
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
/** Accepts a socket that `routeToRelay` sent here. */
|
|
96
|
+
override async fetch(request: Request): Promise<Response> {
|
|
97
|
+
const documentId = new URL(request.url).searchParams.get(RELAY_DOCUMENT_PARAM)
|
|
98
|
+
if (!v.is(documentIdSchema, documentId)) return new Response(null, { status: 400 })
|
|
99
|
+
const relay = await this.#sockets.open(documentId)
|
|
100
|
+
const { 0: client, 1: server } = new WebSocketPair()
|
|
101
|
+
this.ctx.acceptWebSocket(server)
|
|
102
|
+
this.#sockets.accept(server, relay)
|
|
103
|
+
return new Response(null, { status: 101, webSocket: client })
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
override async webSocketMessage(socket: WebSocket, message: string | ArrayBuffer) {
|
|
107
|
+
await this.#sockets.message(socket, message)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
override async webSocketClose(socket: WebSocket) {
|
|
111
|
+
await this.#sockets.closed(socket)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
override async webSocketError(socket: WebSocket) {
|
|
115
|
+
await this.#sockets.closed(socket)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async startOver(documentId: string) {
|
|
119
|
+
if (!this.#sockets.active) return
|
|
120
|
+
;(await this.#sockets.open(documentId)).startOver(documentId)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async announceVersions(documentId: string) {
|
|
124
|
+
if (!this.#sockets.active) return
|
|
125
|
+
;(await this.#sockets.open(documentId)).announceVersions(documentId)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { RELAY_CLOSE } from '#cloud/contract'
|
|
2
|
+
import {
|
|
3
|
+
readRelayPeerState,
|
|
4
|
+
type CollaborationRelay,
|
|
5
|
+
type RelayConnection,
|
|
6
|
+
type RelaySocket
|
|
7
|
+
} from '#cloud/server'
|
|
8
|
+
|
|
9
|
+
/** What the relay's sockets need from the Durable Object that holds them. */
|
|
10
|
+
export type RelaySocketHost = {
|
|
11
|
+
/** The sockets the object still holds, as `DurableObjectState.getWebSockets` lists them. */
|
|
12
|
+
sockets(): WebSocket[]
|
|
13
|
+
/** Creates the relay for the document the object serves, once per wake. */
|
|
14
|
+
createRelay(documentId: string): Promise<CollaborationRelay>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A socket message's bytes; text is passed on as bytes for the relay to refuse. */
|
|
18
|
+
function bytes(data: unknown): Uint8Array {
|
|
19
|
+
if (data instanceof ArrayBuffer) return new Uint8Array(data)
|
|
20
|
+
return new TextEncoder().encode(typeof data === 'string' ? data : '')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A hibernatable socket as the relay sees it. The peer's state rides on the socket as its
|
|
25
|
+
* attachment, so a relay woken from hibernation can resume the peer.
|
|
26
|
+
*/
|
|
27
|
+
function relaySocket(socket: WebSocket): RelaySocket {
|
|
28
|
+
// A closing socket throws on send and close, which would stop a broadcast to the others.
|
|
29
|
+
const open = () => socket.readyState === WebSocket.OPEN
|
|
30
|
+
return {
|
|
31
|
+
send(data) {
|
|
32
|
+
if (open()) socket.send(data)
|
|
33
|
+
},
|
|
34
|
+
close(code, reason) {
|
|
35
|
+
if (open()) socket.close(code, reason)
|
|
36
|
+
},
|
|
37
|
+
remember(state) {
|
|
38
|
+
socket.serializeAttachment(state)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The relay a Durable Object wakes to and its sockets' connections to it. Sockets are accepted
|
|
45
|
+
* for hibernation, so an idle object is evicted while they stay open, each keeping its peer; the
|
|
46
|
+
* relay it wakes to resumes them, loading a room's document only once a message needs it.
|
|
47
|
+
*/
|
|
48
|
+
export function createRelaySockets(host: RelaySocketHost) {
|
|
49
|
+
let relay: Promise<CollaborationRelay> | null = null
|
|
50
|
+
/** Each socket's connection to the relay, since the object last woke. */
|
|
51
|
+
const connections = new Map<WebSocket, RelayConnection>()
|
|
52
|
+
|
|
53
|
+
/** The relay, created once per wake; it takes back every socket the object still holds. */
|
|
54
|
+
function open(documentId: string): Promise<CollaborationRelay> {
|
|
55
|
+
const opened = (relay ??= host.createRelay(documentId).then((created) => {
|
|
56
|
+
for (const socket of host.sockets()) {
|
|
57
|
+
if (connections.has(socket)) continue
|
|
58
|
+
const state = readRelayPeerState(socket.deserializeAttachment())
|
|
59
|
+
// A socket the relay never admitted has no peer to resume, and its ticket is long due.
|
|
60
|
+
if (state) connections.set(socket, created.resume(relaySocket(socket), state))
|
|
61
|
+
else socket.close(RELAY_CLOSE.unauthorized, 'Expected a collaboration ticket')
|
|
62
|
+
}
|
|
63
|
+
return created
|
|
64
|
+
}))
|
|
65
|
+
opened.catch(() => {
|
|
66
|
+
if (relay === opened) relay = null
|
|
67
|
+
})
|
|
68
|
+
return opened
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The socket's connection, waking the relay for the document its peer is in, or null for a
|
|
73
|
+
* stray socket. A socket that closed while the object hibernated may already be missing from
|
|
74
|
+
* the ones the woken relay takes back, so its peer is resumed here, if only to leave.
|
|
75
|
+
*/
|
|
76
|
+
async function connectionOf(socket: WebSocket): Promise<RelayConnection | null> {
|
|
77
|
+
const known = connections.get(socket)
|
|
78
|
+
if (known) return known
|
|
79
|
+
const state = readRelayPeerState(socket.deserializeAttachment())
|
|
80
|
+
if (!state) return null
|
|
81
|
+
const woken = await open(state.authorization.documentId)
|
|
82
|
+
const connection = connections.get(socket) ?? woken.resume(relaySocket(socket), state)
|
|
83
|
+
connections.set(socket, connection)
|
|
84
|
+
return connection
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
open,
|
|
89
|
+
/** Whether a relay is awake, or anyone is still connected to wake one for. */
|
|
90
|
+
get active() {
|
|
91
|
+
return relay !== null || host.sockets().length > 0
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
/** Connects a socket the object just accepted, which presents its ticket next. */
|
|
95
|
+
accept(socket: WebSocket, woken: CollaborationRelay) {
|
|
96
|
+
connections.set(socket, woken.connect(relaySocket(socket)))
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
async message(socket: WebSocket, message: string | ArrayBuffer) {
|
|
100
|
+
const connection = await connectionOf(socket)
|
|
101
|
+
if (connection) connection.receive(bytes(message))
|
|
102
|
+
else socket.close(RELAY_CLOSE.unauthorized, 'Expected a collaboration ticket')
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
/** Lets the socket's peer leave its room, so the room closes once it empties. */
|
|
106
|
+
async closed(socket: WebSocket) {
|
|
107
|
+
const connection = await connectionOf(socket)
|
|
108
|
+
connections.delete(socket)
|
|
109
|
+
connection?.closed()
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -4,13 +4,17 @@ import { createS3ObjectStore } from '#cloud/runtime/s3/objects'
|
|
|
4
4
|
import { createCloudServer } from '#cloud/runtime/site/server'
|
|
5
5
|
import {
|
|
6
6
|
cleanupPassOptions,
|
|
7
|
+
cloudErrorHandler,
|
|
7
8
|
createCleanupServices,
|
|
8
9
|
createCloudApp,
|
|
9
10
|
createCloudAuthenticationRuntime,
|
|
10
11
|
createConfiguredCloudPolicy,
|
|
11
12
|
createInvitationOutbox,
|
|
12
13
|
createTransactionalEmailService,
|
|
14
|
+
reportCloudError,
|
|
13
15
|
runCleanup,
|
|
16
|
+
type CloudErrorReporter,
|
|
17
|
+
type CloudErrorSource,
|
|
14
18
|
type TransactionalEmailService
|
|
15
19
|
} from '#cloud/server'
|
|
16
20
|
import { Hono } from 'hono'
|
|
@@ -30,6 +34,19 @@ function emailTransport({ config }: PreparedCloudflareCloud, env: CloudflareClou
|
|
|
30
34
|
return createCloudflareEmailTransport(env.EMAIL)
|
|
31
35
|
}
|
|
32
36
|
|
|
37
|
+
/** A reporter whose reports keep the Worker running until they are sent. */
|
|
38
|
+
function keptAlive(
|
|
39
|
+
reporter: CloudErrorReporter | undefined,
|
|
40
|
+
context: Pick<ExecutionContext, 'waitUntil'>
|
|
41
|
+
): CloudErrorReporter | undefined {
|
|
42
|
+
if (!reporter) return undefined
|
|
43
|
+
return (error, errorContext) => {
|
|
44
|
+
const reported = reporter(error, errorContext)
|
|
45
|
+
context.waitUntil(reported)
|
|
46
|
+
return reported
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
33
50
|
/**
|
|
34
51
|
* What one request or cron run needs. Workers can't share connections between requests, so each
|
|
35
52
|
* gets its own database client, closed once its background work settles.
|
|
@@ -40,6 +57,9 @@ function cloudServices(
|
|
|
40
57
|
context: Pick<ExecutionContext, 'waitUntil'>
|
|
41
58
|
) {
|
|
42
59
|
const { config } = prepared
|
|
60
|
+
const reporter = keptAlive(prepared.reportError, context)
|
|
61
|
+
const reportError = (message: string, error: unknown, source: CloudErrorSource) =>
|
|
62
|
+
reportCloudError(reporter, message, error, { source })
|
|
43
63
|
const database = createPostgresCloudDatabase({ connectionString: config.databaseURL })
|
|
44
64
|
const objects = createS3ObjectStore(config)
|
|
45
65
|
const transport = emailTransport(prepared, env)
|
|
@@ -61,7 +81,7 @@ function cloudServices(
|
|
|
61
81
|
const background: Promise<unknown>[] = []
|
|
62
82
|
const runInBackground = (task: Promise<unknown>) => {
|
|
63
83
|
background.push(
|
|
64
|
-
task.catch((error: unknown) =>
|
|
84
|
+
task.catch((error: unknown) => reportError('Background work failed:', error, 'background'))
|
|
65
85
|
)
|
|
66
86
|
}
|
|
67
87
|
const deliver = () =>
|
|
@@ -78,27 +98,52 @@ function cloudServices(
|
|
|
78
98
|
transport,
|
|
79
99
|
runInBackground,
|
|
80
100
|
deliver,
|
|
101
|
+
reporter,
|
|
81
102
|
/** Sends what the request queued and closes the database after everything it started. */
|
|
82
103
|
finish() {
|
|
83
104
|
context.waitUntil(
|
|
84
105
|
Promise.allSettled(background)
|
|
85
106
|
.then(() => (enqueued && transport ? deliver() : undefined))
|
|
86
|
-
.catch((error: unknown) =>
|
|
107
|
+
.catch((error: unknown) => reportError('Sending email failed:', error, 'email'))
|
|
87
108
|
.then(() => database.destroy())
|
|
88
109
|
)
|
|
89
110
|
}
|
|
90
111
|
}
|
|
91
112
|
}
|
|
92
113
|
|
|
114
|
+
/** Cleanup, and email that failed to send after its request or that a full batch left behind. */
|
|
115
|
+
async function runScheduled(
|
|
116
|
+
prepared: PreparedCloudflareCloud,
|
|
117
|
+
env: CloudflareCloudEnvironment,
|
|
118
|
+
context: Pick<ExecutionContext, 'waitUntil'>
|
|
119
|
+
) {
|
|
120
|
+
const services = cloudServices(prepared, env, context)
|
|
121
|
+
const { config, database, objects } = services
|
|
122
|
+
try {
|
|
123
|
+
if (config.cleanupEnabled) {
|
|
124
|
+
const policy = createConfiguredCloudPolicy(config, database)
|
|
125
|
+
await runCleanup(
|
|
126
|
+
createCleanupServices(config, database, objects, policy),
|
|
127
|
+
cleanupPassOptions(config)
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
if (services.transport) await services.deliver()
|
|
131
|
+
} finally {
|
|
132
|
+
services.finish()
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
93
136
|
/**
|
|
94
137
|
* OpenPencil Cloud on Cloudflare Workers, configured by a deployment TOML. The Worker serves the
|
|
95
138
|
* API, the editor, and the portal per request, a Durable Object per document runs its collaboration rooms, and a
|
|
96
139
|
* Cron Trigger runs cleanup and retries email.
|
|
97
140
|
*/
|
|
98
141
|
export function createCloudflareCloud(configSource: string) {
|
|
99
|
-
const prepare = cloudflareCloudSetup(configSource)
|
|
142
|
+
const { prepare, reporter } = cloudflareCloudSetup(configSource)
|
|
100
143
|
|
|
101
144
|
const router = new Hono<{ Bindings: CloudflareCloudEnvironment }>()
|
|
145
|
+
// Setup, relay routing, and the runtime around the app; the app reports its own errors.
|
|
146
|
+
.onError((error, c) => cloudErrorHandler(keptAlive(reporter(), c.executionCtx))(error, c))
|
|
102
147
|
.get(RELAY_PATH, routeToRelay)
|
|
103
148
|
.all('*', async (c) => {
|
|
104
149
|
const prepared = await prepare(c.env)
|
|
@@ -122,11 +167,13 @@ export function createCloudflareCloud(configSource: string) {
|
|
|
122
167
|
onDocumentRestored: (documentId) =>
|
|
123
168
|
runInBackground(relayFor(c.env, documentId).startOver(documentId)),
|
|
124
169
|
onDocumentVersionsChanged: (documentId) =>
|
|
125
|
-
runInBackground(relayFor(c.env, documentId).announceVersions(documentId))
|
|
170
|
+
runInBackground(relayFor(c.env, documentId).announceVersions(documentId)),
|
|
171
|
+
reportError: services.reporter
|
|
126
172
|
})
|
|
127
173
|
return await createCloudServer(app, {
|
|
128
174
|
site: assetSiteFiles(c.env.ASSETS),
|
|
129
|
-
indexingPolicy: config.indexingPolicy
|
|
175
|
+
indexingPolicy: config.indexingPolicy,
|
|
176
|
+
reportError: services.reporter
|
|
130
177
|
}).fetch(c.req.raw)
|
|
131
178
|
} finally {
|
|
132
179
|
services.finish()
|
|
@@ -137,21 +184,11 @@ export function createCloudflareCloud(configSource: string) {
|
|
|
137
184
|
fetch: (request, env, context) => router.fetch(request, env, context),
|
|
138
185
|
|
|
139
186
|
async scheduled(_controller, env, context) {
|
|
140
|
-
const prepared = await prepare(env)
|
|
141
|
-
const services = cloudServices(prepared, env, context)
|
|
142
|
-
const { config, database, objects } = services
|
|
143
187
|
try {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
cleanupPassOptions(config)
|
|
149
|
-
)
|
|
150
|
-
}
|
|
151
|
-
// Retries what failed to send after its request, and what a full batch left behind.
|
|
152
|
-
if (services.transport) await services.deliver()
|
|
153
|
-
} finally {
|
|
154
|
-
services.finish()
|
|
188
|
+
await runScheduled(await prepare(env), env, context)
|
|
189
|
+
} catch (error) {
|
|
190
|
+
await reportCloudError(reporter(), 'Scheduled run failed:', error, { source: 'scheduled' })
|
|
191
|
+
throw error
|
|
155
192
|
}
|
|
156
193
|
}
|
|
157
194
|
}
|
|
@@ -7,6 +7,14 @@ import {
|
|
|
7
7
|
|
|
8
8
|
import { loadNodeCloudServerConfig } from './config'
|
|
9
9
|
|
|
10
|
+
/** The deployment's database, migrated to this version before anything uses it. */
|
|
11
|
+
export async function migratedNodeCloudDatabase(config: CloudServerConfig) {
|
|
12
|
+
const database = createPostgresCloudDatabase({ connectionString: config.databaseURL })
|
|
13
|
+
const auth = createBetterAuthAdapter(config, database)
|
|
14
|
+
await migrateCloudDatabase(database, { run: auth.migrate, schemaVersion: auth.schemaVersion })
|
|
15
|
+
return { database, auth }
|
|
16
|
+
}
|
|
17
|
+
|
|
10
18
|
export async function createMigratedNodeCloudDatabase(
|
|
11
19
|
environment: Readonly<Record<string, string | undefined>>
|
|
12
20
|
): Promise<{
|
|
@@ -15,8 +23,5 @@ export async function createMigratedNodeCloudDatabase(
|
|
|
15
23
|
auth: ReturnType<typeof createBetterAuthAdapter>
|
|
16
24
|
}> {
|
|
17
25
|
const config = await loadNodeCloudServerConfig(environment)
|
|
18
|
-
|
|
19
|
-
const auth = createBetterAuthAdapter(config, database)
|
|
20
|
-
await migrateCloudDatabase(database, { run: auth.migrate, schemaVersion: auth.schemaVersion })
|
|
21
|
-
return { config, database, auth }
|
|
26
|
+
return { config, ...(await migratedNodeCloudDatabase(config)) }
|
|
22
27
|
}
|