@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
|
@@ -16,8 +16,17 @@ import {
|
|
|
16
16
|
} from '#cloud/server/documents/errors'
|
|
17
17
|
import type { DocumentService } from '#cloud/server/documents/service'
|
|
18
18
|
import { StorageQuotaExceededError } from '#cloud/server/quota'
|
|
19
|
-
import {
|
|
20
|
-
|
|
19
|
+
import {
|
|
20
|
+
CLOUD_RATE_LIMITS,
|
|
21
|
+
routeParamResource,
|
|
22
|
+
createResourceRateLimiter
|
|
23
|
+
} from '#cloud/server/rate-limit'
|
|
24
|
+
import {
|
|
25
|
+
documentParams,
|
|
26
|
+
uploadParams,
|
|
27
|
+
validatedJSON,
|
|
28
|
+
workspaceParams
|
|
29
|
+
} from '#cloud/server/validation'
|
|
21
30
|
import { Hono, type Context } from 'hono'
|
|
22
31
|
import type { Kysely } from 'kysely'
|
|
23
32
|
|
|
@@ -69,32 +78,32 @@ export function createDocumentRoutes(
|
|
|
69
78
|
if (rateLimit) {
|
|
70
79
|
router.use(
|
|
71
80
|
'/workspaces/:workspaceId/documents',
|
|
72
|
-
|
|
81
|
+
createResourceRateLimiter(
|
|
73
82
|
rateLimit.database,
|
|
74
83
|
rateLimit.secret,
|
|
75
84
|
CLOUD_RATE_LIMITS.documentCreation,
|
|
76
|
-
(
|
|
77
|
-
(context) => context.req.
|
|
85
|
+
routeParamResource('workspaceId'),
|
|
86
|
+
(context) => context.req.method !== 'POST'
|
|
78
87
|
)
|
|
79
88
|
)
|
|
80
89
|
router.use(
|
|
81
90
|
'/documents/:documentId/images',
|
|
82
|
-
|
|
91
|
+
createResourceRateLimiter(
|
|
83
92
|
rateLimit.database,
|
|
84
93
|
rateLimit.secret,
|
|
85
94
|
CLOUD_RATE_LIMITS.uploadCreation,
|
|
86
|
-
(
|
|
87
|
-
(context) => context.req.
|
|
95
|
+
routeParamResource('documentId'),
|
|
96
|
+
(context) => context.req.method !== 'POST'
|
|
88
97
|
)
|
|
89
98
|
)
|
|
90
99
|
router.use(
|
|
91
100
|
'/documents/:documentId/uploads',
|
|
92
|
-
|
|
101
|
+
createResourceRateLimiter(
|
|
93
102
|
rateLimit.database,
|
|
94
103
|
rateLimit.secret,
|
|
95
104
|
CLOUD_RATE_LIMITS.uploadCreation,
|
|
96
|
-
(
|
|
97
|
-
(context) => context.req.
|
|
105
|
+
routeParamResource('documentId'),
|
|
106
|
+
(context) => context.req.method !== 'POST'
|
|
98
107
|
)
|
|
99
108
|
)
|
|
100
109
|
}
|
|
@@ -102,18 +111,21 @@ export function createDocumentRoutes(
|
|
|
102
111
|
.get('/shared-documents', async (context) =>
|
|
103
112
|
context.json({ documents: await service.listShared(context.get('actor').userId) })
|
|
104
113
|
)
|
|
105
|
-
.get('/workspaces/:workspaceId/documents', async (context) => {
|
|
114
|
+
.get('/workspaces/:workspaceId/documents', workspaceParams, async (context) => {
|
|
106
115
|
const documents = await service.list(
|
|
107
116
|
context.get('actor').userId,
|
|
108
|
-
context.req.
|
|
117
|
+
context.req.valid('param').workspaceId
|
|
109
118
|
)
|
|
110
119
|
if (!documents) return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
111
120
|
return context.json({ documents })
|
|
112
121
|
})
|
|
113
|
-
.get('/workspaces/:workspaceId/usage', async (context) => {
|
|
122
|
+
.get('/workspaces/:workspaceId/usage', workspaceParams, async (context) => {
|
|
114
123
|
try {
|
|
115
124
|
return context.json({
|
|
116
|
-
usage: await service.usage(
|
|
125
|
+
usage: await service.usage(
|
|
126
|
+
context.get('actor').userId,
|
|
127
|
+
context.req.valid('param').workspaceId
|
|
128
|
+
)
|
|
117
129
|
})
|
|
118
130
|
} catch (error) {
|
|
119
131
|
const response = documentDomainError(context, error)
|
|
@@ -123,12 +135,13 @@ export function createDocumentRoutes(
|
|
|
123
135
|
})
|
|
124
136
|
.post(
|
|
125
137
|
'/workspaces/:workspaceId/documents',
|
|
138
|
+
workspaceParams,
|
|
126
139
|
validatedJSON(parseCreateDocument),
|
|
127
140
|
async (context) => {
|
|
128
141
|
try {
|
|
129
142
|
const document = await service.create(
|
|
130
143
|
context.get('actor').userId,
|
|
131
|
-
context.req.
|
|
144
|
+
context.req.valid('param').workspaceId,
|
|
132
145
|
context.req.valid('json')
|
|
133
146
|
)
|
|
134
147
|
return context.json({ document }, 201)
|
|
@@ -139,9 +152,9 @@ export function createDocumentRoutes(
|
|
|
139
152
|
}
|
|
140
153
|
}
|
|
141
154
|
)
|
|
142
|
-
.delete('/documents/:documentId', async (context) => {
|
|
155
|
+
.delete('/documents/:documentId', documentParams, async (context) => {
|
|
143
156
|
try {
|
|
144
|
-
await service.remove(context.get('actor').userId, context.req.
|
|
157
|
+
await service.remove(context.get('actor').userId, context.req.valid('param').documentId)
|
|
145
158
|
return context.body(null, 204)
|
|
146
159
|
} catch (error) {
|
|
147
160
|
const response = documentDomainError(context, error)
|
|
@@ -149,91 +162,104 @@ export function createDocumentRoutes(
|
|
|
149
162
|
throw error
|
|
150
163
|
}
|
|
151
164
|
})
|
|
152
|
-
.get('/documents/:documentId/access',
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
access: await service.access(
|
|
156
|
-
})
|
|
157
|
-
} catch (error) {
|
|
158
|
-
const response = documentDomainError(context, error)
|
|
159
|
-
if (response) return response
|
|
160
|
-
throw error
|
|
161
|
-
}
|
|
162
|
-
})
|
|
163
|
-
.get('/documents/:documentId', async (context) => {
|
|
164
|
-
try {
|
|
165
|
-
return context.json({
|
|
166
|
-
document: await service.download(
|
|
165
|
+
.get('/documents/:documentId/access', documentParams, (context) =>
|
|
166
|
+
documentResponse(context, async () =>
|
|
167
|
+
context.json({
|
|
168
|
+
access: await service.access(
|
|
167
169
|
context.get('actor').userId,
|
|
168
|
-
context.req.
|
|
170
|
+
context.req.valid('param').documentId
|
|
169
171
|
)
|
|
170
172
|
})
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
throw error
|
|
175
|
-
}
|
|
176
|
-
})
|
|
177
|
-
.post('/documents/:documentId/uploads', validatedJSON(parseCreateUpload), (context) =>
|
|
173
|
+
)
|
|
174
|
+
)
|
|
175
|
+
.get('/documents/:documentId', documentParams, (context) =>
|
|
178
176
|
documentResponse(context, async () =>
|
|
179
|
-
context.json(
|
|
180
|
-
await service.
|
|
177
|
+
context.json({
|
|
178
|
+
document: await service.download(
|
|
181
179
|
context.get('actor').userId,
|
|
182
|
-
context.req.
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
201
|
|
186
|
-
)
|
|
180
|
+
context.req.valid('param').documentId
|
|
181
|
+
)
|
|
182
|
+
})
|
|
187
183
|
)
|
|
188
184
|
)
|
|
185
|
+
.post(
|
|
186
|
+
'/documents/:documentId/uploads',
|
|
187
|
+
documentParams,
|
|
188
|
+
validatedJSON(parseCreateUpload),
|
|
189
|
+
(context) =>
|
|
190
|
+
documentResponse(context, async () =>
|
|
191
|
+
context.json(
|
|
192
|
+
await service.createUpload(
|
|
193
|
+
context.get('actor').userId,
|
|
194
|
+
context.req.valid('param').documentId,
|
|
195
|
+
context.req.valid('json')
|
|
196
|
+
),
|
|
197
|
+
201
|
|
198
|
+
)
|
|
199
|
+
)
|
|
200
|
+
)
|
|
189
201
|
.post(
|
|
190
202
|
'/documents/:documentId/images/commit',
|
|
203
|
+
documentParams,
|
|
191
204
|
validatedJSON(parseCommitImageUploads),
|
|
192
205
|
(context) =>
|
|
193
206
|
documentResponse(context, async () => {
|
|
194
207
|
await service.commitImageUploads(
|
|
195
208
|
context.get('actor').userId,
|
|
196
|
-
context.req.
|
|
209
|
+
context.req.valid('param').documentId,
|
|
197
210
|
context.req.valid('json').uploads
|
|
198
211
|
)
|
|
199
212
|
return context.body(null, 204)
|
|
200
213
|
})
|
|
201
214
|
)
|
|
202
|
-
.post(
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
215
|
+
.post(
|
|
216
|
+
'/documents/:documentId/images/links',
|
|
217
|
+
documentParams,
|
|
218
|
+
validatedJSON(parseRequestImageLinks),
|
|
219
|
+
(context) =>
|
|
220
|
+
documentResponse(context, async () =>
|
|
221
|
+
context.json(
|
|
222
|
+
await service.imageLinks(
|
|
223
|
+
context.get('actor').userId,
|
|
224
|
+
context.req.valid('param').documentId,
|
|
225
|
+
context.req.valid('json').checksums
|
|
226
|
+
)
|
|
209
227
|
)
|
|
210
228
|
)
|
|
211
|
-
)
|
|
212
229
|
)
|
|
213
|
-
.post(
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
230
|
+
.post(
|
|
231
|
+
'/documents/:documentId/images',
|
|
232
|
+
documentParams,
|
|
233
|
+
validatedJSON(parseRequestImageUploads),
|
|
234
|
+
(context) =>
|
|
235
|
+
documentResponse(context, async () =>
|
|
236
|
+
context.json(
|
|
237
|
+
await service.requestImageUploads(
|
|
238
|
+
context.get('actor').userId,
|
|
239
|
+
context.req.valid('param').documentId,
|
|
240
|
+
context.req.valid('json')
|
|
241
|
+
),
|
|
242
|
+
201
|
|
243
|
+
)
|
|
222
244
|
)
|
|
223
|
-
)
|
|
224
245
|
)
|
|
225
|
-
.post(
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
246
|
+
.post(
|
|
247
|
+
'/uploads/:uploadId/commit',
|
|
248
|
+
uploadParams,
|
|
249
|
+
validatedJSON(parseCommitUpload),
|
|
250
|
+
async (context) => {
|
|
251
|
+
try {
|
|
252
|
+
const document = await service.commitUpload(
|
|
253
|
+
context.get('actor').userId,
|
|
254
|
+
context.req.valid('param').uploadId,
|
|
255
|
+
context.req.valid('json')
|
|
256
|
+
)
|
|
257
|
+
return context.json({ document })
|
|
258
|
+
} catch (error) {
|
|
259
|
+
const response = documentDomainError(context, error)
|
|
260
|
+
if (response) return response
|
|
261
|
+
throw error
|
|
262
|
+
}
|
|
237
263
|
}
|
|
238
|
-
|
|
264
|
+
)
|
|
239
265
|
}
|
|
@@ -4,7 +4,12 @@ import {
|
|
|
4
4
|
parseUpdateDocumentVersion
|
|
5
5
|
} from '#cloud/contract'
|
|
6
6
|
import { documentDomainError, type DocumentRouteEnvironment } from '#cloud/server/documents/routes'
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
documentParams,
|
|
9
|
+
documentVersionParams,
|
|
10
|
+
validatedJSON,
|
|
11
|
+
validatedQuery
|
|
12
|
+
} from '#cloud/server/validation'
|
|
8
13
|
import { Hono, type Context } from 'hono'
|
|
9
14
|
import * as v from 'valibot'
|
|
10
15
|
|
|
@@ -29,70 +34,76 @@ async function answer(context: Context, run: () => Promise<Response>): Promise<R
|
|
|
29
34
|
/** A document's version history: listing, naming, opening, restoring, and duplicating versions. */
|
|
30
35
|
export function createDocumentVersionRoutes(service: DocumentVersionService) {
|
|
31
36
|
return new Hono<DocumentRouteEnvironment>()
|
|
32
|
-
.get('/documents/:documentId/versions', validatedQuery(parsePage), (context) =>
|
|
37
|
+
.get('/documents/:documentId/versions', documentParams, validatedQuery(parsePage), (context) =>
|
|
33
38
|
answer(context, async () =>
|
|
34
39
|
context.json(
|
|
35
40
|
await service.list(
|
|
36
41
|
context.get('actor').userId,
|
|
37
|
-
context.req.
|
|
42
|
+
context.req.valid('param').documentId,
|
|
38
43
|
context.req.valid('query')
|
|
39
44
|
)
|
|
40
45
|
)
|
|
41
46
|
)
|
|
42
47
|
)
|
|
43
|
-
.post(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
.post(
|
|
49
|
+
'/documents/:documentId/versions',
|
|
50
|
+
documentParams,
|
|
51
|
+
validatedJSON(parseSaveDocumentVersion),
|
|
52
|
+
(context) =>
|
|
53
|
+
answer(context, async () =>
|
|
54
|
+
context.json(
|
|
55
|
+
{
|
|
56
|
+
version: await service.save(
|
|
57
|
+
context.get('actor').userId,
|
|
58
|
+
context.req.valid('param').documentId,
|
|
59
|
+
context.req.valid('json')
|
|
60
|
+
)
|
|
61
|
+
},
|
|
62
|
+
201
|
|
63
|
+
)
|
|
54
64
|
)
|
|
55
|
-
)
|
|
56
65
|
)
|
|
57
66
|
.patch(
|
|
58
67
|
'/documents/:documentId/versions/:versionId',
|
|
68
|
+
documentVersionParams,
|
|
59
69
|
validatedJSON(parseUpdateDocumentVersion),
|
|
60
70
|
(context) =>
|
|
61
71
|
answer(context, async () =>
|
|
62
72
|
context.json({
|
|
63
73
|
version: await service.update(
|
|
64
74
|
context.get('actor').userId,
|
|
65
|
-
context.req.
|
|
66
|
-
context.req.
|
|
75
|
+
context.req.valid('param').documentId,
|
|
76
|
+
context.req.valid('param').versionId,
|
|
67
77
|
context.req.valid('json')
|
|
68
78
|
)
|
|
69
79
|
})
|
|
70
80
|
)
|
|
71
81
|
)
|
|
72
|
-
.get('/documents/:documentId/versions/:versionId', (context) =>
|
|
82
|
+
.get('/documents/:documentId/versions/:versionId', documentVersionParams, (context) =>
|
|
73
83
|
answer(context, async () =>
|
|
74
84
|
context.json(
|
|
75
85
|
await service.download(
|
|
76
86
|
context.get('actor').userId,
|
|
77
|
-
context.req.
|
|
78
|
-
context.req.
|
|
87
|
+
context.req.valid('param').documentId,
|
|
88
|
+
context.req.valid('param').versionId
|
|
79
89
|
)
|
|
80
90
|
)
|
|
81
91
|
)
|
|
82
92
|
)
|
|
83
|
-
.post('/documents/:documentId/versions/:versionId/restore', (context) =>
|
|
93
|
+
.post('/documents/:documentId/versions/:versionId/restore', documentVersionParams, (context) =>
|
|
84
94
|
answer(context, async () =>
|
|
85
95
|
context.json(
|
|
86
96
|
await service.restore(
|
|
87
97
|
context.get('actor').userId,
|
|
88
|
-
context.req.
|
|
89
|
-
context.req.
|
|
98
|
+
context.req.valid('param').documentId,
|
|
99
|
+
context.req.valid('param').versionId
|
|
90
100
|
)
|
|
91
101
|
)
|
|
92
102
|
)
|
|
93
103
|
)
|
|
94
104
|
.post(
|
|
95
105
|
'/documents/:documentId/versions/:versionId/duplicate',
|
|
106
|
+
documentVersionParams,
|
|
96
107
|
validatedJSON(parseDuplicateDocumentVersion),
|
|
97
108
|
(context) =>
|
|
98
109
|
answer(context, async () =>
|
|
@@ -100,8 +111,8 @@ export function createDocumentVersionRoutes(service: DocumentVersionService) {
|
|
|
100
111
|
{
|
|
101
112
|
document: await service.duplicate(
|
|
102
113
|
context.get('actor').userId,
|
|
103
|
-
context.req.
|
|
104
|
-
context.req.
|
|
114
|
+
context.req.valid('param').documentId,
|
|
115
|
+
context.req.valid('param').versionId,
|
|
105
116
|
context.req.valid('json')
|
|
106
117
|
)
|
|
107
118
|
},
|
|
@@ -309,6 +309,15 @@ export function createDocumentVersionService(
|
|
|
309
309
|
})
|
|
310
310
|
.where('id', '=', documentId)
|
|
311
311
|
.execute()
|
|
312
|
+
// The rooms of the replaced state never open again, so what the relay saved of them goes.
|
|
313
|
+
await transaction
|
|
314
|
+
.deleteFrom('documentRoomUpdate')
|
|
315
|
+
.where('documentId', '=', documentId)
|
|
316
|
+
.execute()
|
|
317
|
+
await transaction
|
|
318
|
+
.deleteFrom('documentRoomSnapshot')
|
|
319
|
+
.where('documentId', '=', documentId)
|
|
320
|
+
.execute()
|
|
312
321
|
// A moment after the state it replaces, so the history lists it first.
|
|
313
322
|
await keepRevision(transaction, {
|
|
314
323
|
documentId,
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Context, ErrorHandler } from 'hono'
|
|
2
|
+
import { HTTPException } from 'hono/http-exception'
|
|
3
|
+
import { routePath } from 'hono/route'
|
|
4
|
+
|
|
5
|
+
/** Where the server caught an error. */
|
|
6
|
+
export const CLOUD_ERROR_SOURCES = [
|
|
7
|
+
'request',
|
|
8
|
+
'background',
|
|
9
|
+
'cleanup',
|
|
10
|
+
'email',
|
|
11
|
+
'relay',
|
|
12
|
+
'scheduled',
|
|
13
|
+
'setup'
|
|
14
|
+
] as const
|
|
15
|
+
export type CloudErrorSource = (typeof CLOUD_ERROR_SOURCES)[number]
|
|
16
|
+
|
|
17
|
+
/** What the server knows about a caught error; never who made the request or what it carried. */
|
|
18
|
+
export type CloudErrorContext = {
|
|
19
|
+
source: CloudErrorSource
|
|
20
|
+
/** For a request: its method and the route pattern it matched, not the path with its IDs. */
|
|
21
|
+
method?: string
|
|
22
|
+
route?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Sends a caught error to the deployment's error tracking. Runtimes create one only when the
|
|
27
|
+
* deployment configures error tracking; its promise never rejects.
|
|
28
|
+
*/
|
|
29
|
+
export type CloudErrorReporter = (error: unknown, context: CloudErrorContext) => Promise<void>
|
|
30
|
+
|
|
31
|
+
/** Logs a caught error and reports it, when the deployment reports errors. */
|
|
32
|
+
export function reportCloudError(
|
|
33
|
+
reporter: CloudErrorReporter | undefined,
|
|
34
|
+
message: string,
|
|
35
|
+
error: unknown,
|
|
36
|
+
context: CloudErrorContext
|
|
37
|
+
): Promise<void> {
|
|
38
|
+
console.error(`[Cloud] ${message}`, error)
|
|
39
|
+
return reporter ? reporter(error, context) : Promise.resolve()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function requestContext(context: Context): CloudErrorContext {
|
|
43
|
+
return { source: 'request', method: context.req.method, route: routePath(context) }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The app's last error handler, as Hono's default: an `HTTPException` answers with its response,
|
|
48
|
+
* anything else is logged, reported, and answered with a 500.
|
|
49
|
+
*/
|
|
50
|
+
export function cloudErrorHandler(reporter: CloudErrorReporter | undefined): ErrorHandler {
|
|
51
|
+
return (error, context) => {
|
|
52
|
+
if (error instanceof HTTPException) return error.getResponse()
|
|
53
|
+
void reportCloudError(reporter, 'Request failed:', error, requestContext(context))
|
|
54
|
+
return context.text('Internal Server Error', 500)
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/server/index.ts
CHANGED
|
@@ -95,6 +95,9 @@ export {
|
|
|
95
95
|
type CollaborationRelayOptions,
|
|
96
96
|
type CollaborationRoomIdentity,
|
|
97
97
|
type CollaborationStateStore,
|
|
98
|
+
type SavedRoom,
|
|
99
|
+
readRelayPeerState,
|
|
100
|
+
type RelayPeerState,
|
|
98
101
|
type RelayAuthorization,
|
|
99
102
|
type RelayConnection,
|
|
100
103
|
type RelaySocket,
|
|
@@ -182,6 +185,14 @@ export {
|
|
|
182
185
|
type TransactionalEmailWorker,
|
|
183
186
|
type TransactionalEmailWorkerOptions
|
|
184
187
|
} from './email'
|
|
188
|
+
export {
|
|
189
|
+
CLOUD_ERROR_SOURCES,
|
|
190
|
+
cloudErrorHandler,
|
|
191
|
+
reportCloudError,
|
|
192
|
+
type CloudErrorContext,
|
|
193
|
+
type CloudErrorReporter,
|
|
194
|
+
type CloudErrorSource
|
|
195
|
+
} from './errors'
|
|
185
196
|
export {
|
|
186
197
|
createInternalRoutes,
|
|
187
198
|
createInternalService,
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { capabilityHashMatches, hashCapability } from '#cloud/server/sharing/crypto'
|
|
2
|
-
import { validatedJSON } from '#cloud/server/validation'
|
|
2
|
+
import { validatedJSON, workspaceParams } from '#cloud/server/validation'
|
|
3
3
|
import { Hono } from 'hono'
|
|
4
4
|
import { bearerAuth } from 'hono/bearer-auth'
|
|
5
|
-
import * as v from 'valibot'
|
|
6
5
|
|
|
7
6
|
import { parseSetWorkspaceEntitlements, type InternalService } from './service'
|
|
8
7
|
|
|
9
|
-
const uuidSchema = v.pipe(v.string(), v.uuid())
|
|
10
|
-
|
|
11
8
|
/** The internal API on HTTP, for runtimes without a private transport to their services. */
|
|
12
9
|
export function createInternalRoutes(service: InternalService, serviceToken: string) {
|
|
13
10
|
const digest = hashCapability(serviceToken)
|
|
@@ -24,12 +21,10 @@ export function createInternalRoutes(service: InternalService, serviceToken: str
|
|
|
24
21
|
)
|
|
25
22
|
.put(
|
|
26
23
|
'/workspaces/:workspaceId/entitlements',
|
|
24
|
+
workspaceParams,
|
|
27
25
|
validatedJSON(parseSetWorkspaceEntitlements),
|
|
28
26
|
async (context) => {
|
|
29
|
-
const workspaceId = context.req.
|
|
30
|
-
if (!v.is(uuidSchema, workspaceId)) {
|
|
31
|
-
return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
32
|
-
}
|
|
27
|
+
const { workspaceId } = context.req.valid('param')
|
|
33
28
|
const result = await service.setWorkspaceEntitlements(
|
|
34
29
|
workspaceId,
|
|
35
30
|
context.req.valid('json')
|
|
@@ -54,6 +54,14 @@ export type CreateObjectUploadInput = CreateObjectDownloadInput & {
|
|
|
54
54
|
contentType: string
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
/** Bytes the server stores itself, under the checksum the caller computed and recorded. */
|
|
58
|
+
export type WriteObjectInput = {
|
|
59
|
+
key: string
|
|
60
|
+
bytes: Uint8Array
|
|
61
|
+
contentType: string
|
|
62
|
+
checksum: string
|
|
63
|
+
}
|
|
64
|
+
|
|
57
65
|
export type CompleteObjectUploadInput = {
|
|
58
66
|
key: string
|
|
59
67
|
uploadId: string
|
|
@@ -78,4 +86,8 @@ export interface ObjectStore {
|
|
|
78
86
|
abortUpload(input: AbortObjectUploadInput): Promise<void>
|
|
79
87
|
head(key: string): Promise<StoredObject | null>
|
|
80
88
|
delete(key: string): Promise<void>
|
|
89
|
+
/** Stores bytes the server holds itself, such as a collaboration room's snapshot. */
|
|
90
|
+
write(input: WriteObjectInput): Promise<void>
|
|
91
|
+
/** The bytes stored under `key`, or null when there are none. */
|
|
92
|
+
read(key: string): Promise<Uint8Array | null>
|
|
81
93
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { pictureParams } from '#cloud/server/validation'
|
|
1
2
|
import { Hono } from 'hono'
|
|
2
3
|
|
|
3
4
|
import { PICTURE_PATH, type UserPictureService } from './service'
|
|
@@ -7,8 +8,9 @@ import { PICTURE_PATH, type UserPictureService } from './service'
|
|
|
7
8
|
* documents and workspaces with, and the desktop app shows them from another origin.
|
|
8
9
|
*/
|
|
9
10
|
export function createPictureRoutes(pictures: UserPictureService) {
|
|
10
|
-
return new Hono().get(`${PICTURE_PATH}/:userId/:version`, async (context) => {
|
|
11
|
-
const
|
|
11
|
+
return new Hono().get(`${PICTURE_PATH}/:userId/:version`, pictureParams, async (context) => {
|
|
12
|
+
const { userId, version } = context.req.valid('param')
|
|
13
|
+
const picture = await pictures.read(userId, version)
|
|
12
14
|
if (!picture) return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
13
15
|
// The address changes with the picture, but a removed one must stop showing within a day.
|
|
14
16
|
context.header('Cache-Control', 'public, max-age=86400')
|
|
@@ -4,9 +4,12 @@ export {
|
|
|
4
4
|
CLOUD_RATE_LIMITS,
|
|
5
5
|
createActorRateLimiter,
|
|
6
6
|
createCloudRateLimiter,
|
|
7
|
+
createResourceRateLimiter,
|
|
7
8
|
createRouteRateLimiter,
|
|
8
9
|
createTrustedIPRateLimiter,
|
|
9
10
|
routeHasOwnBudget,
|
|
10
|
-
|
|
11
|
+
routeParamResource,
|
|
12
|
+
type RateLimitPolicy,
|
|
13
|
+
type ResourceRateLimitPolicy
|
|
11
14
|
} from './middleware'
|
|
12
15
|
export { PostgresRateLimitStore } from './postgres'
|