@pairdraw/cloud 0.18.0 → 0.19.0-beta.1
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/client.d.ts +5 -10
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +0 -7
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +8 -5
- package/dist/contract2.js.map +1 -1
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +8 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +202 -110
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +2299 -661
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/server.d.ts +3298 -964
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +415 -190
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +243 -79
- package/dist/server.mjs.map +1 -1
- package/package.json +2 -2
- package/src/client/api.ts +0 -15
- package/src/contract/documents.ts +8 -0
- package/src/contract/index.ts +1 -3
- package/src/contract/sharing.ts +0 -9
- package/src/runtime/cloudflare/relay.ts +5 -1
- package/src/runtime/cloudflare/worker.ts +4 -1
- package/src/runtime/node/bootstrap.ts +13 -3
- package/src/runtime/node/cleanup.ts +5 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/server.ts +12 -2
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/site/server.ts +19 -2
- package/src/server/api/router.ts +16 -8
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +9 -0
- package/src/server/auth/factory.ts +3 -0
- package/src/server/collaboration/relay/hub.ts +42 -3
- package/src/server/collaboration/routes.ts +21 -14
- package/src/server/config/schema.ts +3 -0
- package/src/server/db/expressions.ts +13 -1
- package/src/server/db/migrate.ts +3 -1
- package/src/server/db/migrations/028_invitation_sends.ts +31 -0
- package/src/server/db/tables.ts +9 -0
- package/src/server/documents/access.ts +4 -2
- package/src/server/documents/routes.ts +81 -57
- package/src/server/documents/versions/routes.ts +27 -16
- package/src/server/index.ts +2 -0
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/profiles/routes.ts +16 -12
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/index.ts +4 -1
- package/src/server/rate-limit/middleware.ts +98 -11
- package/src/server/sharing/capabilities/service.ts +27 -2
- package/src/server/sharing/document-routes.ts +30 -22
- package/src/server/sharing/grants/service.ts +13 -7
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +2 -0
- package/src/server/sharing/routes.ts +79 -64
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +76 -0
- package/src/server/workspaces/members.ts +2 -0
- package/src/server/workspaces/routes.ts +56 -35
package/src/server/validation.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Context } from 'hono'
|
|
2
2
|
import { validator } from 'hono/validator'
|
|
3
|
+
import * as v from 'valibot'
|
|
3
4
|
import { ValiError } from 'valibot'
|
|
4
5
|
|
|
5
6
|
/** A request part that fails its schema is a 400, never a server error. */
|
|
@@ -22,3 +23,78 @@ export function validatedJSON<T>(parse: (input: unknown) => T) {
|
|
|
22
23
|
export function validatedQuery<T>(parse: (input: unknown) => T) {
|
|
23
24
|
return validator('query', (value, context: Context) => parseOrReject(parse, value, context))
|
|
24
25
|
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Route parameters, so a malformed id names nothing and never reaches the database: it answers
|
|
29
|
+
* 404 like a row that has gone, as upstream does.
|
|
30
|
+
*/
|
|
31
|
+
export function validatedParams<T>(parse: (input: unknown) => T) {
|
|
32
|
+
return validator('param', (value, context: Context) => {
|
|
33
|
+
try {
|
|
34
|
+
return parse(value)
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (error instanceof ValiError) {
|
|
37
|
+
return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
38
|
+
}
|
|
39
|
+
throw error
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const uuidParamSchema = v.pipe(v.string(), v.uuid())
|
|
45
|
+
const workspaceParamsSchema = v.object({ workspaceId: uuidParamSchema })
|
|
46
|
+
const textParamSchema = v.pipe(v.string(), v.minLength(1))
|
|
47
|
+
const workspaceMemberParamsSchema = v.object({
|
|
48
|
+
workspaceId: uuidParamSchema,
|
|
49
|
+
// The server takes a member id as text: routes accept a username as well as a UUID.
|
|
50
|
+
userId: textParamSchema
|
|
51
|
+
})
|
|
52
|
+
const workspaceInvitationParamsSchema = v.object({
|
|
53
|
+
workspaceId: uuidParamSchema,
|
|
54
|
+
invitationId: uuidParamSchema
|
|
55
|
+
})
|
|
56
|
+
const documentParamsSchema = v.object({ documentId: uuidParamSchema })
|
|
57
|
+
const documentVersionParamsSchema = v.object({
|
|
58
|
+
documentId: uuidParamSchema,
|
|
59
|
+
versionId: uuidParamSchema
|
|
60
|
+
})
|
|
61
|
+
const documentShareParamsSchema = v.object({
|
|
62
|
+
documentId: uuidParamSchema,
|
|
63
|
+
shareId: uuidParamSchema
|
|
64
|
+
})
|
|
65
|
+
const documentUserParamsSchema = v.object({
|
|
66
|
+
documentId: uuidParamSchema,
|
|
67
|
+
userId: textParamSchema
|
|
68
|
+
})
|
|
69
|
+
const documentInvitationParamsSchema = v.object({
|
|
70
|
+
documentId: uuidParamSchema,
|
|
71
|
+
invitationId: uuidParamSchema
|
|
72
|
+
})
|
|
73
|
+
/** A share or invitation named on its own, as a public link or an emailed link does. */
|
|
74
|
+
const shareParamsSchema = v.object({ shareId: uuidParamSchema })
|
|
75
|
+
const invitationParamsSchema = v.object({ invitationId: uuidParamSchema })
|
|
76
|
+
const uploadParamsSchema = v.object({ uploadId: uuidParamSchema })
|
|
77
|
+
|
|
78
|
+
export const workspaceParams = validatedParams((input) => v.parse(workspaceParamsSchema, input))
|
|
79
|
+
export const workspaceMemberParams = validatedParams((input) =>
|
|
80
|
+
v.parse(workspaceMemberParamsSchema, input)
|
|
81
|
+
)
|
|
82
|
+
export const workspaceInvitationParams = validatedParams((input) =>
|
|
83
|
+
v.parse(workspaceInvitationParamsSchema, input)
|
|
84
|
+
)
|
|
85
|
+
export const documentParams = validatedParams((input) => v.parse(documentParamsSchema, input))
|
|
86
|
+
export const documentVersionParams = validatedParams((input) =>
|
|
87
|
+
v.parse(documentVersionParamsSchema, input)
|
|
88
|
+
)
|
|
89
|
+
export const documentShareParams = validatedParams((input) =>
|
|
90
|
+
v.parse(documentShareParamsSchema, input)
|
|
91
|
+
)
|
|
92
|
+
export const documentUserParams = validatedParams((input) =>
|
|
93
|
+
v.parse(documentUserParamsSchema, input)
|
|
94
|
+
)
|
|
95
|
+
export const documentInvitationParams = validatedParams((input) =>
|
|
96
|
+
v.parse(documentInvitationParamsSchema, input)
|
|
97
|
+
)
|
|
98
|
+
export const shareParams = validatedParams((input) => v.parse(shareParamsSchema, input))
|
|
99
|
+
export const invitationParams = validatedParams((input) => v.parse(invitationParamsSchema, input))
|
|
100
|
+
export const uploadParams = validatedParams((input) => v.parse(uploadParamsSchema, input))
|
|
@@ -11,6 +11,7 @@ import { cloudInvitationURL } from '#cloud/contract'
|
|
|
11
11
|
import type { CloudActor } from '#cloud/server/auth'
|
|
12
12
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
13
13
|
import type { TransactionalEmailService } from '#cloud/server/email/types'
|
|
14
|
+
import { spendInvitationBudget } from '#cloud/server/invitations'
|
|
14
15
|
import {
|
|
15
16
|
invitationMatchesActor,
|
|
16
17
|
invitationTokenIsActive,
|
|
@@ -193,6 +194,7 @@ export function createWorkspaceMemberService(
|
|
|
193
194
|
const expiresAt = new Date(Date.now() + INVITATION_LIFETIME_MS)
|
|
194
195
|
const url = acceptanceURL(id, token)
|
|
195
196
|
const row = await database.transaction().execute(async (transaction) => {
|
|
197
|
+
await spendInvitationBudget(transaction, { senderId: userId, email: input.email })
|
|
196
198
|
await transaction
|
|
197
199
|
.updateTable('workspaceInvitation')
|
|
198
200
|
.set({ revokedAt: new Date() })
|
|
@@ -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 {
|
|
10
|
-
import {
|
|
9
|
+
import { InvitationLimitError } from '#cloud/server/invitations'
|
|
10
|
+
import {
|
|
11
|
+
CLOUD_RATE_LIMITS,
|
|
12
|
+
createResourceRateLimiter,
|
|
13
|
+
createRouteRateLimiter
|
|
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,
|
|
@@ -48,6 +58,9 @@ async function memberRoute<T>(context: Context, action: () => Promise<T>): Promi
|
|
|
48
58
|
if (error instanceof WorkspaceAlreadyMemberError) {
|
|
49
59
|
return context.json({ error: { code: 'already_member' as const } }, 409)
|
|
50
60
|
}
|
|
61
|
+
if (error instanceof InvitationLimitError) {
|
|
62
|
+
return context.json({ error: { code: 'rate_limited' as const } }, 429)
|
|
63
|
+
}
|
|
51
64
|
throw error
|
|
52
65
|
}
|
|
53
66
|
}
|
|
@@ -71,22 +84,22 @@ export function createWorkspaceRoutes(
|
|
|
71
84
|
// Resending mails the invitation again, so it counts against the same budget as sending.
|
|
72
85
|
router.use(
|
|
73
86
|
'/:workspaceId/invitations/:invitationId/resend',
|
|
74
|
-
|
|
87
|
+
createResourceRateLimiter(
|
|
75
88
|
rateLimit.database,
|
|
76
89
|
rateLimit.secret,
|
|
77
90
|
CLOUD_RATE_LIMITS.invitationMutation,
|
|
78
|
-
() =>
|
|
79
|
-
(
|
|
91
|
+
(context) => context.req.param('workspaceId') ?? 'unknown',
|
|
92
|
+
() => false
|
|
80
93
|
)
|
|
81
94
|
)
|
|
82
95
|
router.use(
|
|
83
96
|
'/:workspaceId/invitations',
|
|
84
|
-
|
|
97
|
+
createResourceRateLimiter(
|
|
85
98
|
rateLimit.database,
|
|
86
99
|
rateLimit.secret,
|
|
87
100
|
CLOUD_RATE_LIMITS.invitationMutation,
|
|
88
|
-
(context) => context.req.
|
|
89
|
-
(context) => context.req.
|
|
101
|
+
(context) => context.req.param('workspaceId') ?? 'unknown',
|
|
102
|
+
(context) => context.req.method !== 'POST'
|
|
90
103
|
)
|
|
91
104
|
)
|
|
92
105
|
}
|
|
@@ -109,7 +122,7 @@ export function createWorkspaceRoutes(
|
|
|
109
122
|
throw error
|
|
110
123
|
}
|
|
111
124
|
})
|
|
112
|
-
.get('/:workspaceId', async (context) => {
|
|
125
|
+
.get('/:workspaceId', workspaceParams, async (context) => {
|
|
113
126
|
const workspace = await service.get(
|
|
114
127
|
context.get('actor').userId,
|
|
115
128
|
context.req.param('workspaceId')
|
|
@@ -117,7 +130,7 @@ export function createWorkspaceRoutes(
|
|
|
117
130
|
if (!workspace) return context.json({ error: { code: 'not_found' as const } }, 404)
|
|
118
131
|
return context.json({ workspace })
|
|
119
132
|
})
|
|
120
|
-
.patch('/:workspaceId', validatedJSON(parseUpdateWorkspace), (context) =>
|
|
133
|
+
.patch('/:workspaceId', workspaceParams, validatedJSON(parseUpdateWorkspace), (context) =>
|
|
121
134
|
memberRoute(context, async () =>
|
|
122
135
|
context.json({
|
|
123
136
|
workspace: await members.rename(
|
|
@@ -128,32 +141,36 @@ export function createWorkspaceRoutes(
|
|
|
128
141
|
})
|
|
129
142
|
)
|
|
130
143
|
)
|
|
131
|
-
.delete('/:workspaceId', (context) =>
|
|
144
|
+
.delete('/:workspaceId', workspaceParams, (context) =>
|
|
132
145
|
memberRoute(context, async () => {
|
|
133
146
|
await members.delete(context.get('actor').userId, context.req.param('workspaceId'))
|
|
134
147
|
return context.body(null, 204)
|
|
135
148
|
})
|
|
136
149
|
)
|
|
137
|
-
.get('/:workspaceId/members', (context) =>
|
|
150
|
+
.get('/:workspaceId/members', workspaceParams, (context) =>
|
|
138
151
|
memberRoute(context, async () =>
|
|
139
152
|
context.json(
|
|
140
153
|
await members.list(context.get('actor').userId, context.req.param('workspaceId'))
|
|
141
154
|
)
|
|
142
155
|
)
|
|
143
156
|
)
|
|
144
|
-
.post(
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
157
|
+
.post(
|
|
158
|
+
'/:workspaceId/invitations',
|
|
159
|
+
workspaceParams,
|
|
160
|
+
validatedJSON(parseCreateWorkspaceInvitation),
|
|
161
|
+
(context) =>
|
|
162
|
+
memberRoute(context, async () =>
|
|
163
|
+
context.json(
|
|
164
|
+
await members.invite(
|
|
165
|
+
context.get('actor').userId,
|
|
166
|
+
context.req.param('workspaceId'),
|
|
167
|
+
context.req.valid('json')
|
|
168
|
+
),
|
|
169
|
+
201
|
|
170
|
+
)
|
|
153
171
|
)
|
|
154
|
-
)
|
|
155
172
|
)
|
|
156
|
-
.post('/:workspaceId/invitations/:invitationId/resend', (context) =>
|
|
173
|
+
.post('/:workspaceId/invitations/:invitationId/resend', workspaceInvitationParams, (context) =>
|
|
157
174
|
memberRoute(context, async () =>
|
|
158
175
|
context.json(
|
|
159
176
|
await members.resendInvitation(
|
|
@@ -164,7 +181,7 @@ export function createWorkspaceRoutes(
|
|
|
164
181
|
)
|
|
165
182
|
)
|
|
166
183
|
)
|
|
167
|
-
.delete('/:workspaceId/invitations/:invitationId', (context) =>
|
|
184
|
+
.delete('/:workspaceId/invitations/:invitationId', workspaceInvitationParams, (context) =>
|
|
168
185
|
memberRoute(context, async () => {
|
|
169
186
|
await members.revokeInvitation(
|
|
170
187
|
context.get('actor').userId,
|
|
@@ -174,18 +191,22 @@ export function createWorkspaceRoutes(
|
|
|
174
191
|
return context.body(null, 204)
|
|
175
192
|
})
|
|
176
193
|
)
|
|
177
|
-
.patch(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
194
|
+
.patch(
|
|
195
|
+
'/:workspaceId/members/:userId',
|
|
196
|
+
workspaceMemberParams,
|
|
197
|
+
validatedJSON(parseUpdateWorkspaceMember),
|
|
198
|
+
(context) =>
|
|
199
|
+
memberRoute(context, async () => {
|
|
200
|
+
await members.updateRole(
|
|
201
|
+
context.get('actor').userId,
|
|
202
|
+
context.req.param('workspaceId'),
|
|
203
|
+
context.req.param('userId'),
|
|
204
|
+
context.req.valid('json').role
|
|
205
|
+
)
|
|
206
|
+
return context.body(null, 204)
|
|
207
|
+
})
|
|
187
208
|
)
|
|
188
|
-
.delete('/:workspaceId/members/:userId', (context) =>
|
|
209
|
+
.delete('/:workspaceId/members/:userId', workspaceMemberParams, (context) =>
|
|
189
210
|
memberRoute(context, async () => {
|
|
190
211
|
await members.remove(
|
|
191
212
|
context.get('actor').userId,
|