@open-pencil/cloud 0.17.0-beta.1 → 0.17.0-beta.4
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/README.md +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +236 -46
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -400
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +680 -443
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +9 -1
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +99 -58
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +468 -64
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +575 -738
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +6564 -2934
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +8931 -4198
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5260 -3859
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5686 -3736
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +9 -6
- package/src/admin/api/routes.ts +103 -33
- package/src/admin/audit/service.ts +25 -6
- package/src/admin/documents/service.ts +361 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +147 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +36 -21
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/index.ts +7 -7
- package/src/client/portal/account.ts +25 -0
- package/src/client/portal/administration.ts +56 -0
- package/src/client/{ai.ts → services.ts} +44 -27
- package/src/contract/account.ts +58 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/collaboration.ts +6 -1
- package/src/contract/discovery.ts +22 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +2 -2
- package/src/contract/index.ts +63 -37
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/services.ts +233 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/render.ts +26 -0
- package/src/email/schema.ts +11 -1
- package/src/email/templates/brand.ts +1 -1
- package/src/runtime/cloudflare/environment.ts +31 -6
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +176 -0
- package/src/runtime/cloudflare/relay/sockets.ts +112 -0
- package/src/runtime/cloudflare/worker.ts +89 -26
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +20 -4
- package/src/runtime/node/cleanup.ts +16 -3
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +71 -15
- package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +32 -0
- package/src/runtime/s3/objects.ts +50 -1
- package/src/runtime/site/server.ts +33 -4
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +302 -0
- package/src/server/account/routes.ts +47 -3
- package/src/server/account/terms.ts +42 -0
- package/src/server/api/router.ts +12 -7
- package/src/server/api/types.ts +9 -0
- package/src/server/app.ts +93 -15
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/adapter.ts +11 -0
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/factory.ts +30 -3
- package/src/server/auth/index.ts +10 -1
- package/src/server/cleanup/configured.ts +12 -5
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/objects.ts +10 -2
- package/src/server/cleanup/revisions.ts +24 -7
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +5 -2
- package/src/server/collaboration/index.ts +3 -0
- package/src/server/collaboration/relay/connection.ts +196 -0
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +279 -195
- package/src/server/collaboration/relay/index.ts +3 -1
- package/src/server/collaboration/relay/log.ts +79 -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/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +35 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +104 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -114
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/schema.ts +71 -0
- package/src/server/db/tables.ts +70 -7
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/limits.ts +9 -9
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +132 -80
- package/src/server/documents/service.ts +18 -4
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/routes.ts +36 -25
- package/src/server/documents/versions/service.ts +22 -4
- package/src/server/errors/index.ts +99 -0
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +56 -16
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -15
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/logging.ts +76 -0
- package/src/server/objects/types.ts +12 -0
- package/src/server/pictures/routes.ts +4 -2
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/database.ts +39 -25
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +1 -0
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +12 -9
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +69 -31
- 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 +100 -12
- package/src/server/reports/index.ts +8 -0
- package/src/server/reports/routes.ts +37 -0
- package/src/server/reports/service.ts +123 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -22
- package/src/server/sharing/document-routes.ts +30 -22
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +21 -11
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +108 -85
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +59 -2
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +89 -38
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +67 -45
- package/src/{ai-relay → services}/app.ts +48 -44
- package/src/{ai-relay → services}/config.ts +25 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +43 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/contract/ai.ts +0 -150
- package/src/runtime/cloudflare/relay.ts +0 -136
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -9,12 +9,15 @@ export const CLOUD_FEATURE_KEYS = {
|
|
|
9
9
|
maximumFileBytes: 'cloud.documents.maximum-file-bytes',
|
|
10
10
|
maximumStorageBytes: 'cloud.storage.maximum-bytes',
|
|
11
11
|
maximumParticipants: 'cloud.collaboration.maximum-participants',
|
|
12
|
-
|
|
13
|
-
monthlyCreditAllowance: 'cloud.
|
|
12
|
+
cloudServices: 'cloud.services.enabled',
|
|
13
|
+
monthlyCreditAllowance: 'cloud.services.monthly-credit-allowance'
|
|
14
14
|
} as const
|
|
15
15
|
|
|
16
16
|
export type CloudPolicyContext = {
|
|
17
|
+
/** Whose entitlements apply: an account by default for a workspace, its owner's. */
|
|
17
18
|
targetingKey: string
|
|
19
|
+
/** Required, so an account ID never reads as a workspace's and falls back to defaults. */
|
|
20
|
+
subjectType: 'workspace' | 'user' | 'organization' | 'deployment'
|
|
18
21
|
actorId?: string
|
|
19
22
|
workspaceId?: string
|
|
20
23
|
organizationId?: string
|
|
@@ -57,6 +57,7 @@ function evaluationContext(context: CloudPolicyContext): EvaluationContext {
|
|
|
57
57
|
targetingKey: context.targetingKey,
|
|
58
58
|
deploymentMode: context.deploymentMode
|
|
59
59
|
}
|
|
60
|
+
evaluation.subjectType = context.subjectType
|
|
60
61
|
if (context.actorId) evaluation.actorId = context.actorId
|
|
61
62
|
if (context.workspaceId) evaluation.workspaceId = context.workspaceId
|
|
62
63
|
if (context.organizationId) evaluation.organizationId = context.organizationId
|
|
@@ -28,7 +28,7 @@ export const staticEntitlementsSchema = v.object({
|
|
|
28
28
|
}),
|
|
29
29
|
{}
|
|
30
30
|
),
|
|
31
|
-
|
|
31
|
+
services: v.optional(
|
|
32
32
|
v.object({
|
|
33
33
|
enabled: v.optional(v.boolean()),
|
|
34
34
|
monthlyCreditAllowance: v.optional(nonNegativeIntegerSchema)
|
|
@@ -60,7 +60,7 @@ export const staticEntitlementsTOMLSchema = v.strictObject({
|
|
|
60
60
|
maximum_participants: v.optional(positiveIntegerSchema)
|
|
61
61
|
})
|
|
62
62
|
),
|
|
63
|
-
|
|
63
|
+
services: v.optional(
|
|
64
64
|
v.strictObject({
|
|
65
65
|
enabled: v.optional(v.boolean()),
|
|
66
66
|
monthly_credit_allowance: v.optional(nonNegativeIntegerSchema)
|
|
@@ -7,6 +7,7 @@ import type { Kysely } from 'kysely'
|
|
|
7
7
|
|
|
8
8
|
import { CLOUD_FEATURE_KEYS } from './keys'
|
|
9
9
|
import type { CloudPolicy } from './policy'
|
|
10
|
+
import { workspacePolicyContext } from './workspace'
|
|
10
11
|
|
|
11
12
|
export function createEntitlementService(
|
|
12
13
|
database: Kysely<CloudDatabase>,
|
|
@@ -18,12 +19,12 @@ export function createEntitlementService(
|
|
|
18
19
|
return {
|
|
19
20
|
async workspace(userId: string, workspaceId: string): Promise<WorkspaceEntitlements> {
|
|
20
21
|
if (!(await workspaceRole(database, userId, workspaceId))) throw new DocumentNotFoundError()
|
|
21
|
-
const context =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
const context = await workspacePolicyContext(
|
|
23
|
+
database,
|
|
24
|
+
{ workspaceId },
|
|
25
|
+
{ actorId: userId, deploymentMode: 'self-hosted' }
|
|
26
|
+
)
|
|
27
|
+
if (!context) throw new DocumentNotFoundError()
|
|
27
28
|
const [
|
|
28
29
|
links,
|
|
29
30
|
guestView,
|
|
@@ -31,7 +32,7 @@ export function createEntitlementService(
|
|
|
31
32
|
guestPresence,
|
|
32
33
|
collaboration,
|
|
33
34
|
revisionHistory,
|
|
34
|
-
|
|
35
|
+
cloudServices,
|
|
35
36
|
maximumFileBytes,
|
|
36
37
|
maximumStorage,
|
|
37
38
|
maximumParticipants,
|
|
@@ -44,7 +45,7 @@ export function createEntitlementService(
|
|
|
44
45
|
policy.boolean(CLOUD_FEATURE_KEYS.guestPresence, false, context),
|
|
45
46
|
policy.boolean(CLOUD_FEATURE_KEYS.collaboration, false, context),
|
|
46
47
|
policy.boolean(CLOUD_FEATURE_KEYS.revisionHistory, false, context),
|
|
47
|
-
policy.boolean(CLOUD_FEATURE_KEYS.
|
|
48
|
+
policy.boolean(CLOUD_FEATURE_KEYS.cloudServices, false, context),
|
|
48
49
|
policy.number(CLOUD_FEATURE_KEYS.maximumFileBytes, technicalMaximumFileBytes, context),
|
|
49
50
|
policy.number(CLOUD_FEATURE_KEYS.maximumStorageBytes, Number.MAX_SAFE_INTEGER, context),
|
|
50
51
|
policy.number(
|
|
@@ -55,6 +56,8 @@ export function createEntitlementService(
|
|
|
55
56
|
policy.number(CLOUD_FEATURE_KEYS.monthlyCreditAllowance, -1, context),
|
|
56
57
|
quota.snapshot(workspaceId)
|
|
57
58
|
])
|
|
59
|
+
// The workspace may have gone since its role was read; it has no usage to report then.
|
|
60
|
+
if (!usage) throw new DocumentNotFoundError()
|
|
58
61
|
// Technical limits are ceilings: an entitlement can lower them but never raise them.
|
|
59
62
|
const participants = Math.min(technicalMaximumParticipants, maximumParticipants)
|
|
60
63
|
return {
|
|
@@ -65,7 +68,7 @@ export function createEntitlementService(
|
|
|
65
68
|
guestPresence,
|
|
66
69
|
collaboration,
|
|
67
70
|
revisionHistory,
|
|
68
|
-
|
|
71
|
+
cloudServices
|
|
69
72
|
},
|
|
70
73
|
limits: {
|
|
71
74
|
maximumFileBytes: Math.min(technicalMaximumFileBytes, maximumFileBytes),
|
|
@@ -32,9 +32,9 @@ export function staticEntitlementsFromTOML(
|
|
|
32
32
|
enabled: value.collaboration?.enabled,
|
|
33
33
|
maximumParticipants: value.collaboration?.maximum_participants
|
|
34
34
|
},
|
|
35
|
-
|
|
36
|
-
enabled: value.
|
|
37
|
-
monthlyCreditAllowance: value.
|
|
35
|
+
services: {
|
|
36
|
+
enabled: value.services?.enabled,
|
|
37
|
+
monthlyCreditAllowance: value.services?.monthly_credit_allowance
|
|
38
38
|
}
|
|
39
39
|
})
|
|
40
40
|
}
|
|
@@ -55,8 +55,8 @@ export function staticEntitlementValues(input: unknown): Record<string, boolean
|
|
|
55
55
|
[CLOUD_FEATURE_KEYS.guestPresence]: entitlements.sharing.guestPresence,
|
|
56
56
|
[CLOUD_FEATURE_KEYS.collaboration]: entitlements.collaboration.enabled,
|
|
57
57
|
[CLOUD_FEATURE_KEYS.maximumParticipants]: entitlements.collaboration.maximumParticipants,
|
|
58
|
-
[CLOUD_FEATURE_KEYS.
|
|
59
|
-
[CLOUD_FEATURE_KEYS.monthlyCreditAllowance]: entitlements.
|
|
58
|
+
[CLOUD_FEATURE_KEYS.cloudServices]: entitlements.services.enabled,
|
|
59
|
+
[CLOUD_FEATURE_KEYS.monthlyCreditAllowance]: entitlements.services.monthlyCreditAllowance
|
|
60
60
|
}
|
|
61
61
|
return pickBy(values, isNotNil) as Record<string, boolean | number>
|
|
62
62
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
3
|
+
|
|
4
|
+
import type { CloudPolicyContext } from './keys'
|
|
5
|
+
|
|
6
|
+
type WorkspacePolicyTarget = { workspaceId: string } | { documentId: string }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The policy context for a workspace, or for a document by its workspace: its owner's plan, which
|
|
10
|
+
* every workspace they own takes, so creating more workspaces adds no allowance. Null when the
|
|
11
|
+
* workspace or document is gone.
|
|
12
|
+
*/
|
|
13
|
+
export async function workspacePolicyContext(
|
|
14
|
+
database: Kysely<CloudDatabase> | Transaction<CloudDatabase>,
|
|
15
|
+
target: WorkspacePolicyTarget,
|
|
16
|
+
context: Pick<CloudPolicyContext, 'actorId' | 'deploymentMode'>
|
|
17
|
+
): Promise<(CloudPolicyContext & { workspaceId: string }) | null> {
|
|
18
|
+
const row =
|
|
19
|
+
'documentId' in target
|
|
20
|
+
? await database
|
|
21
|
+
.selectFrom('document')
|
|
22
|
+
.innerJoin('workspace', 'workspace.id', 'document.workspaceId')
|
|
23
|
+
.select(['workspace.id as workspaceId', 'workspace.ownerId'])
|
|
24
|
+
.where('document.id', '=', target.documentId)
|
|
25
|
+
.executeTakeFirst()
|
|
26
|
+
: await database
|
|
27
|
+
.selectFrom('workspace')
|
|
28
|
+
.select(['id as workspaceId', 'ownerId'])
|
|
29
|
+
.where('id', '=', target.workspaceId)
|
|
30
|
+
.executeTakeFirst()
|
|
31
|
+
if (!row) return null
|
|
32
|
+
const policyContext: CloudPolicyContext & { workspaceId: string } = {
|
|
33
|
+
targetingKey: row.ownerId,
|
|
34
|
+
subjectType: 'user',
|
|
35
|
+
workspaceId: row.workspaceId,
|
|
36
|
+
deploymentMode: context.deploymentMode
|
|
37
|
+
}
|
|
38
|
+
if (context.actorId) policyContext.actorId = context.actorId
|
|
39
|
+
if ('documentId' in target) policyContext.documentId = target.documentId
|
|
40
|
+
return policyContext
|
|
41
|
+
}
|
|
@@ -28,6 +28,8 @@ export function createStorageReconciliationService(database: Kysely<CloudDatabas
|
|
|
28
28
|
.select(['storageObject.id', 'storageObject.byteSize'])
|
|
29
29
|
.distinct()
|
|
30
30
|
.where('document.workspaceId', '=', workspace.id)
|
|
31
|
+
// A document deleted forever came off the usage then, though its files wait out a grace.
|
|
32
|
+
.where('document.purgedAt', 'is', null)
|
|
31
33
|
// Images are the workspace's once, whichever revisions use them.
|
|
32
34
|
.union((union) =>
|
|
33
35
|
union
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { lockForTransaction } from '#cloud/server/db/expressions'
|
|
2
3
|
import type { Kysely, Transaction } from 'kysely'
|
|
3
4
|
|
|
4
5
|
export class StorageQuotaExceededError extends Error {
|
|
@@ -12,37 +13,66 @@ export type StorageQuotaSnapshot = {
|
|
|
12
13
|
|
|
13
14
|
type StorageDatabase = Kysely<CloudDatabase> | Transaction<CloudDatabase>
|
|
14
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Takes the lock an owner's storage allowance is checked under, until the transaction ends.
|
|
18
|
+
* Reservations hold it while they count and reserve; an ownership transfer holds it for both
|
|
19
|
+
* owners, so no reservation counts a workspace under an owner it is leaving or joining.
|
|
20
|
+
*/
|
|
21
|
+
export function lockStorageAllowance(
|
|
22
|
+
transaction: Transaction<CloudDatabase>,
|
|
23
|
+
ownerId: string
|
|
24
|
+
): Promise<void> {
|
|
25
|
+
return lockForTransaction(transaction, `storage-quota:${ownerId}`)
|
|
26
|
+
}
|
|
27
|
+
|
|
15
28
|
export function createStorageQuotaService(database: Kysely<CloudDatabase>) {
|
|
16
|
-
async function
|
|
29
|
+
async function ownerOf(executor: StorageDatabase, workspaceId: string): Promise<string | null> {
|
|
30
|
+
const workspace = await executor
|
|
31
|
+
.selectFrom('workspace')
|
|
32
|
+
.select('ownerId')
|
|
33
|
+
.where('id', '=', workspaceId)
|
|
34
|
+
.executeTakeFirst()
|
|
35
|
+
return workspace?.ownerId ?? null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* What `ownerId` holds and has reserved across every workspace they own, which all draw from
|
|
40
|
+
* the owner's one allowance. Both totals come from one statement, so an upload committing
|
|
41
|
+
* meanwhile counts once, as committed or as reserved.
|
|
42
|
+
*/
|
|
43
|
+
async function usage(
|
|
17
44
|
executor: StorageDatabase,
|
|
18
|
-
|
|
45
|
+
ownerId: string,
|
|
19
46
|
now = new Date()
|
|
20
47
|
): Promise<StorageQuotaSnapshot> {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
48
|
+
const owned = executor.selectFrom('workspace').select('id').where('ownerId', '=', ownerId)
|
|
49
|
+
const row = await executor
|
|
50
|
+
.selectNoFrom((expression) => [
|
|
51
|
+
expression
|
|
52
|
+
.selectFrom('workspaceStorageUsage')
|
|
53
|
+
.select((sum) =>
|
|
54
|
+
sum.fn.coalesce(sum.fn.sum('committedBytes'), sum.val(0)).as('committedBytes')
|
|
55
|
+
)
|
|
56
|
+
.where('workspaceId', 'in', owned)
|
|
57
|
+
.as('committedBytes'),
|
|
58
|
+
expression
|
|
59
|
+
.selectFrom('uploadStorageReservation')
|
|
60
|
+
.select((sum) => sum.fn.coalesce(sum.fn.sum('bytes'), sum.val(0)).as('reservedBytes'))
|
|
61
|
+
.where('workspaceId', 'in', owned)
|
|
62
|
+
.where('committedAt', 'is', null)
|
|
63
|
+
.where('releasedAt', 'is', null)
|
|
64
|
+
.where('expiresAt', '>', now)
|
|
65
|
+
.as('reservedBytes')
|
|
66
|
+
])
|
|
67
|
+
.executeTakeFirstOrThrow()
|
|
38
68
|
return {
|
|
39
|
-
committedBytes: Number(
|
|
40
|
-
reservedBytes: Number(
|
|
69
|
+
committedBytes: Number(row.committedBytes ?? 0),
|
|
70
|
+
reservedBytes: Number(row.reservedBytes ?? 0)
|
|
41
71
|
}
|
|
42
72
|
}
|
|
43
73
|
|
|
44
74
|
async function reserve(
|
|
45
|
-
executor:
|
|
75
|
+
executor: Transaction<CloudDatabase>,
|
|
46
76
|
input: {
|
|
47
77
|
workspaceId: string
|
|
48
78
|
uploadId: string
|
|
@@ -51,21 +81,25 @@ export function createStorageQuotaService(database: Kysely<CloudDatabase>) {
|
|
|
51
81
|
maximumBytes: number | null
|
|
52
82
|
}
|
|
53
83
|
): Promise<void> {
|
|
84
|
+
// The allowance spans the owner's workspaces, so the owner is what reservations queue on.
|
|
85
|
+
// An ownership transfer holds the same lock, so the owner read once it is held stays put.
|
|
86
|
+
let ownerId = await ownerOf(executor, input.workspaceId)
|
|
87
|
+
for (;;) {
|
|
88
|
+
if (ownerId === null) throw new Error('Workspace not found')
|
|
89
|
+
await lockStorageAllowance(executor, ownerId)
|
|
90
|
+
const current = await ownerOf(executor, input.workspaceId)
|
|
91
|
+
if (current === ownerId) break
|
|
92
|
+
ownerId = current
|
|
93
|
+
}
|
|
54
94
|
await executor
|
|
55
95
|
.insertInto('workspaceStorageUsage')
|
|
56
96
|
.values({ workspaceId: input.workspaceId, committedBytes: 0 })
|
|
57
97
|
.onConflict((conflict) => conflict.column('workspaceId').doNothing())
|
|
58
98
|
.execute()
|
|
59
|
-
await executor
|
|
60
|
-
.selectFrom('workspaceStorageUsage')
|
|
61
|
-
.select('workspaceId')
|
|
62
|
-
.where('workspaceId', '=', input.workspaceId)
|
|
63
|
-
.forUpdate()
|
|
64
|
-
.executeTakeFirstOrThrow()
|
|
65
|
-
const usage = await snapshot(executor, input.workspaceId)
|
|
99
|
+
const held = await usage(executor, ownerId)
|
|
66
100
|
if (
|
|
67
101
|
input.maximumBytes !== null &&
|
|
68
|
-
|
|
102
|
+
held.committedBytes + held.reservedBytes + input.bytes > input.maximumBytes
|
|
69
103
|
) {
|
|
70
104
|
throw new StorageQuotaExceededError('Workspace storage quota exceeded')
|
|
71
105
|
}
|
|
@@ -119,7 +153,11 @@ export function createStorageQuotaService(database: Kysely<CloudDatabase>) {
|
|
|
119
153
|
}
|
|
120
154
|
|
|
121
155
|
return {
|
|
122
|
-
|
|
156
|
+
/** The pooled usage of the workspace's owner, or null when the workspace is gone. */
|
|
157
|
+
async snapshot(workspaceId: string): Promise<StorageQuotaSnapshot | null> {
|
|
158
|
+
const ownerId = await ownerOf(database, workspaceId)
|
|
159
|
+
return ownerId === null ? null : usage(database, ownerId)
|
|
160
|
+
},
|
|
123
161
|
|
|
124
162
|
async reserve(input: {
|
|
125
163
|
workspaceId: string
|
|
@@ -1,14 +1,26 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { INVITATION_BUDGET } from '#cloud/server/invitations'
|
|
2
3
|
import type { Kysely } from 'kysely'
|
|
3
4
|
|
|
4
5
|
export function createRateLimitCleanupService(database: Kysely<CloudDatabase>) {
|
|
5
6
|
return {
|
|
6
7
|
async cleanupExpired(before: Date): Promise<number> {
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const [limits, invitations] = await Promise.all([
|
|
9
|
+
database
|
|
10
|
+
.deleteFrom('apiRateLimit')
|
|
11
|
+
.where('windowStartedAt', '<', before)
|
|
12
|
+
.executeTakeFirst(),
|
|
13
|
+
// Never a send the invitation budget still counts, whatever cutoff the sweep passes.
|
|
14
|
+
database
|
|
15
|
+
.deleteFrom('invitationSend')
|
|
16
|
+
.where(
|
|
17
|
+
'sentAt',
|
|
18
|
+
'<',
|
|
19
|
+
new Date(Math.min(before.getTime(), Date.now() - INVITATION_BUDGET.windowMs))
|
|
20
|
+
)
|
|
21
|
+
.executeTakeFirst()
|
|
22
|
+
])
|
|
23
|
+
return Number(limits.numDeletedRows) + Number(invitations.numDeletedRows)
|
|
12
24
|
}
|
|
13
25
|
}
|
|
14
26
|
}
|
|
@@ -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'
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { CloudAPIEnvironment } from '#cloud/server/api/types'
|
|
1
|
+
import type { CloudAPIEnvironment, RateLimitUse } from '#cloud/server/api/types'
|
|
2
2
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
-
import type { Context, Env, Input } from 'hono'
|
|
3
|
+
import type { Context, Env, Input, MiddlewareHandler } from 'hono'
|
|
4
4
|
import { rateLimiter, type RateLimitExceededEventHandler } from 'hono-rate-limiter'
|
|
5
|
+
import { every } from 'hono/combine'
|
|
5
6
|
import { matchedRoutes } from 'hono/route'
|
|
6
7
|
import type { Kysely } from 'kysely'
|
|
7
8
|
|
|
@@ -14,18 +15,39 @@ export type RateLimitPolicy = {
|
|
|
14
15
|
limit: number
|
|
15
16
|
}
|
|
16
17
|
|
|
18
|
+
/**
|
|
19
|
+
* A budget counted per resource, such as a document, with `actorLimit` capping one actor across
|
|
20
|
+
* every resource, so new resources do not add budget without end.
|
|
21
|
+
*/
|
|
22
|
+
export type ResourceRateLimitPolicy = RateLimitPolicy & { actorLimit: number }
|
|
23
|
+
|
|
17
24
|
export const CLOUD_RATE_LIMITS = {
|
|
18
25
|
publicCapability: { namespace: 'public-capability', windowMs: 60_000, limit: 30 },
|
|
19
|
-
collaborationTicket: {
|
|
20
|
-
|
|
26
|
+
collaborationTicket: {
|
|
27
|
+
namespace: 'collaboration-ticket',
|
|
28
|
+
windowMs: 60_000,
|
|
29
|
+
limit: 60,
|
|
30
|
+
actorLimit: 300
|
|
31
|
+
},
|
|
32
|
+
invitationMutation: {
|
|
33
|
+
namespace: 'invitation-mutation',
|
|
34
|
+
windowMs: 60_000,
|
|
35
|
+
limit: 30,
|
|
36
|
+
actorLimit: 60
|
|
37
|
+
},
|
|
21
38
|
workspaceCreation: { namespace: 'workspace-creation', windowMs: 60_000, limit: 30 },
|
|
22
|
-
documentCreation: {
|
|
23
|
-
|
|
39
|
+
documentCreation: {
|
|
40
|
+
namespace: 'document-creation',
|
|
41
|
+
windowMs: 60_000,
|
|
42
|
+
limit: 60,
|
|
43
|
+
actorLimit: 120
|
|
44
|
+
},
|
|
45
|
+
uploadCreation: { namespace: 'upload-creation', windowMs: 60_000, limit: 120, actorLimit: 600 },
|
|
24
46
|
authenticatedMutation: { namespace: 'authenticated-mutation', windowMs: 60_000, limit: 120 },
|
|
25
|
-
|
|
47
|
+
servicesToken: { namespace: 'services-token', windowMs: 60_000, limit: 30 },
|
|
26
48
|
adminRead: { namespace: 'admin-read', windowMs: 60_000, limit: 300 },
|
|
27
49
|
adminMutation: { namespace: 'admin-mutation', windowMs: 60_000, limit: 30 }
|
|
28
|
-
} as const satisfies Record<string, RateLimitPolicy>
|
|
50
|
+
} as const satisfies Record<string, RateLimitPolicy | ResourceRateLimitPolicy>
|
|
29
51
|
|
|
30
52
|
/**
|
|
31
53
|
* The limiters routes mount for their own budget, each with which requests it counts. A general
|
|
@@ -123,15 +145,81 @@ export function createActorRateLimiter(
|
|
|
123
145
|
}
|
|
124
146
|
|
|
125
147
|
/**
|
|
126
|
-
* A limiter a route mounts for its own budget, such as
|
|
148
|
+
* A limiter a route mounts for its own budget, such as workspace creation; general limiters leave
|
|
127
149
|
* the requests it counts to it.
|
|
128
150
|
*/
|
|
129
151
|
export function createRouteRateLimiter(
|
|
130
152
|
database: Kysely<CloudDatabase>,
|
|
131
153
|
secret: string,
|
|
132
154
|
policy: RateLimitPolicy,
|
|
133
|
-
skip?: (context: Context) => boolean
|
|
134
|
-
|
|
155
|
+
skip?: (context: Context) => boolean
|
|
156
|
+
) {
|
|
157
|
+
return ownBudget(createActorRateLimiter(database, secret, policy, skip), skip)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* A route's own budget counted per resource, such as uploads per document, and capped per actor
|
|
162
|
+
* across resources by `actorLimit`; general limiters leave the requests it counts to it.
|
|
163
|
+
*/
|
|
164
|
+
export function createResourceRateLimiter(
|
|
165
|
+
database: Kysely<CloudDatabase>,
|
|
166
|
+
secret: string,
|
|
167
|
+
policy: ResourceRateLimitPolicy,
|
|
168
|
+
resource: (context: Context<CloudAPIEnvironment, '*'>) => string,
|
|
169
|
+
skip?: (context: Context) => boolean
|
|
135
170
|
) {
|
|
136
|
-
|
|
171
|
+
const perResource = createActorRateLimiter(database, secret, policy, skip, resource)
|
|
172
|
+
const perActor = createActorRateLimiter(
|
|
173
|
+
database,
|
|
174
|
+
secret,
|
|
175
|
+
{ namespace: `${policy.namespace}-actor`, windowMs: policy.windowMs, limit: policy.actorLimit },
|
|
176
|
+
skip
|
|
177
|
+
)
|
|
178
|
+
return ownBudget(
|
|
179
|
+
every(perResource, keepResourceUse, perActor, reportTighterBudget(policy.windowMs)),
|
|
180
|
+
skip
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Keeps the resource budget's use, which the actor-wide limiter would record over. */
|
|
185
|
+
const keepResourceUse: MiddlewareHandler<CloudAPIEnvironment> = async (context, next) => {
|
|
186
|
+
context.set('resourceRateLimit', context.get('rateLimit'))
|
|
187
|
+
await next()
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Reports whichever budget has fewer requests left in the `RateLimit` headers, as both limiters
|
|
192
|
+
* write them and the last would otherwise win. A refused request keeps the headers of the budget
|
|
193
|
+
* that refused it.
|
|
194
|
+
*/
|
|
195
|
+
function reportTighterBudget(windowMs: number): MiddlewareHandler<CloudAPIEnvironment> {
|
|
196
|
+
return async (context, next) => {
|
|
197
|
+
const resource = context.get('resourceRateLimit')
|
|
198
|
+
const actor = context.get('rateLimit')
|
|
199
|
+
const tighter = resource && actor && resource.remaining < actor.remaining ? resource : actor
|
|
200
|
+
if (tighter) setBudgetHeaders(context, tighter, windowMs)
|
|
201
|
+
await next()
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The draft-7 `RateLimit` headers `hono-rate-limiter` writes, for one budget's use. */
|
|
206
|
+
function setBudgetHeaders(context: Context, use: RateLimitUse, windowMs: number): void {
|
|
207
|
+
const windowSeconds = Math.ceil(windowMs / 1000)
|
|
208
|
+
const reset = use.resetTime
|
|
209
|
+
? Math.max(0, Math.ceil((use.resetTime.getTime() - Date.now()) / 1000))
|
|
210
|
+
: windowSeconds
|
|
211
|
+
context.header('RateLimit-Policy', `${use.limit};w=${windowSeconds}`)
|
|
212
|
+
context.header('RateLimit', `limit=${use.limit}, remaining=${use.remaining}, reset=${reset}`)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* A limiter's resource named by a route parameter, such as the document a request uploads to. It
|
|
217
|
+
* runs before the route's param validator, and the value only names the limiter's counter, which
|
|
218
|
+
* `rateLimitKey` hashes; it never reaches a query.
|
|
219
|
+
*/
|
|
220
|
+
export function routeParamResource(name: string) {
|
|
221
|
+
return (context: Context): string =>
|
|
222
|
+
// Lowercased as validation lowercases UUIDs, so one row's spellings share a budget.
|
|
223
|
+
// eslint-disable-next-line @open-pencil/security/no-unvalidated-route-params -- a counter name, hashed, never queried.
|
|
224
|
+
context.req.param(name)?.toLowerCase() ?? 'unknown'
|
|
137
225
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { parseFileDocumentReport } from '#cloud/contract'
|
|
2
|
+
import type { CloudSessionResolver } from '#cloud/server/auth'
|
|
3
|
+
import { shareActor } from '#cloud/server/sharing/actor'
|
|
4
|
+
import { sharingRoute } from '#cloud/server/sharing/http'
|
|
5
|
+
import { shareParams, validatedJSON } from '#cloud/server/validation'
|
|
6
|
+
import { Hono } from 'hono'
|
|
7
|
+
|
|
8
|
+
import { DocumentReportLimitError, type DocumentReportService } from './service'
|
|
9
|
+
|
|
10
|
+
/** Reports about documents shared by link, which anyone the link lets in may file. */
|
|
11
|
+
export function createPublicReportRoutes(
|
|
12
|
+
service: DocumentReportService,
|
|
13
|
+
resolveSession?: CloudSessionResolver
|
|
14
|
+
) {
|
|
15
|
+
return new Hono().post(
|
|
16
|
+
'/shares/:shareId/reports',
|
|
17
|
+
shareParams,
|
|
18
|
+
validatedJSON(parseFileDocumentReport),
|
|
19
|
+
async (context) => {
|
|
20
|
+
try {
|
|
21
|
+
return await sharingRoute(context, async () => {
|
|
22
|
+
await service.file(
|
|
23
|
+
context.req.valid('param').shareId,
|
|
24
|
+
context.req.valid('json'),
|
|
25
|
+
await shareActor(context.req.raw, resolveSession)
|
|
26
|
+
)
|
|
27
|
+
return context.json({ received: true as const }, 201)
|
|
28
|
+
})
|
|
29
|
+
} catch (error) {
|
|
30
|
+
if (error instanceof DocumentReportLimitError) {
|
|
31
|
+
return context.json({ error: { code: 'rate_limited' as const } }, 429)
|
|
32
|
+
}
|
|
33
|
+
throw error
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
)
|
|
37
|
+
}
|