@open-pencil/cloud 0.0.0-stage → 0.17.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/LICENSE +202 -0
- package/LICENSE-MIT +21 -0
- package/NOTICE +7 -0
- package/README.md +37 -2
- package/dist/bin/ai-relay.d.mts +1 -0
- package/dist/bin/ai-relay.mjs +31 -0
- package/dist/bin/ai-relay.mjs.map +1 -0
- package/dist/bin/server.d.mts +1 -0
- package/dist/bin/server.mjs +29 -0
- package/dist/bin/server.mjs.map +1 -0
- package/dist/client.d.ts +1520 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +1917 -0
- package/dist/client.js.map +1 -0
- package/dist/contract.d.ts +2 -0
- package/dist/contract.js +2 -0
- package/dist/contract2.js +1453 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +169 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +670 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1755 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1881 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10040 -0
- package/dist/runtime-node.d.mts.map +1 -0
- package/dist/runtime-node.mjs +32 -0
- package/dist/runtime-node.mjs.map +1 -0
- package/dist/server.d.ts +12792 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8475 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8324 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +124 -4
- package/src/admin/api/routes.ts +223 -0
- package/src/admin/api/security.ts +12 -0
- package/src/admin/audit/service.ts +138 -0
- package/src/admin/email/service.ts +140 -0
- package/src/admin/enrollment/email.ts +77 -0
- package/src/admin/enrollment/service.ts +308 -0
- package/src/admin/errors.ts +20 -0
- package/src/admin/index.ts +19 -0
- package/src/admin/list.ts +61 -0
- package/src/admin/operations/service.ts +70 -0
- package/src/admin/schema.ts +28 -0
- package/src/admin/users/service.ts +369 -0
- package/src/ai-relay/app.ts +251 -0
- package/src/ai-relay/config.ts +120 -0
- package/src/ai-relay/db/index.ts +10 -0
- package/src/ai-relay/db/migrate.ts +32 -0
- package/src/ai-relay/db/migrations/001_usage.ts +26 -0
- package/src/ai-relay/db/schema.ts +20 -0
- package/src/ai-relay/index.ts +18 -0
- package/src/ai-relay/metering.ts +68 -0
- package/src/ai-relay/usage.ts +115 -0
- package/src/client/ai.ts +96 -0
- package/src/client/api.ts +528 -0
- package/src/client/auth/client.ts +188 -0
- package/src/client/auth/failure.ts +54 -0
- package/src/client/auth/navigation.ts +45 -0
- package/src/client/device-authorization.ts +134 -0
- package/src/client/discovery.ts +105 -0
- package/src/client/images.ts +81 -0
- package/src/client/index.ts +53 -0
- package/src/client/portal/account.ts +183 -0
- package/src/client/portal/administration.ts +130 -0
- package/src/client/portal/client.ts +18 -0
- package/src/client/portal/request.ts +115 -0
- package/src/client/response.ts +51 -0
- package/src/client/workspaces.ts +142 -0
- package/src/contract/account.ts +204 -0
- package/src/contract/admin/index.ts +187 -0
- package/src/contract/ai.ts +150 -0
- package/src/contract/collaboration.ts +45 -0
- package/src/contract/discovery.ts +81 -0
- package/src/contract/documents.ts +265 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +204 -0
- package/src/contract/links.ts +87 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/relay.ts +368 -0
- package/src/contract/sharing.ts +191 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +14 -0
- package/src/email/render.ts +276 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +105 -0
- package/src/email/templates/document-invitation.ts +35 -0
- package/src/email/templates/enrollment.ts +27 -0
- package/src/email/templates/layout.ts +18 -0
- package/src/runtime/cloudflare/email.ts +70 -0
- package/src/runtime/cloudflare/environment.ts +57 -0
- package/src/runtime/cloudflare/index.ts +3 -0
- package/src/runtime/cloudflare/relay.ts +136 -0
- package/src/runtime/cloudflare/site.ts +13 -0
- package/src/runtime/cloudflare/tsconfig.json +21 -0
- package/src/runtime/cloudflare/worker.ts +160 -0
- package/src/runtime/node/admin.ts +59 -0
- package/src/runtime/node/ai-relay/main.ts +35 -0
- package/src/runtime/node/ai-relay/server.ts +63 -0
- package/src/runtime/node/application.ts +50 -0
- package/src/runtime/node/bootstrap.ts +22 -0
- package/src/runtime/node/cleanup.ts +24 -0
- package/src/runtime/node/command.ts +17 -0
- package/src/runtime/node/config.ts +16 -0
- package/src/runtime/node/email/runtime.ts +43 -0
- package/src/runtime/node/email/transport.ts +112 -0
- package/src/runtime/node/entitlements/main.ts +41 -0
- package/src/runtime/node/entitlements/refusal.ts +15 -0
- package/src/runtime/node/index.ts +25 -0
- package/src/runtime/node/listen.ts +35 -0
- package/src/runtime/node/main.ts +29 -0
- package/src/runtime/node/migrate.ts +13 -0
- package/src/runtime/node/quota-reconcile.ts +9 -0
- package/src/runtime/node/relay.ts +54 -0
- package/src/runtime/node/server.ts +121 -0
- package/src/runtime/node/site.ts +64 -0
- package/src/runtime/postgres/database.ts +19 -0
- package/src/runtime/s3/objects.ts +244 -0
- package/src/runtime/site/server.ts +82 -0
- package/src/server/account/actions.ts +66 -0
- package/src/server/account/routes.ts +300 -0
- package/src/server/ai/errors.ts +7 -0
- package/src/server/ai/index.ts +4 -0
- package/src/server/ai/keys.ts +43 -0
- package/src/server/ai/routes.ts +43 -0
- package/src/server/ai/service.ts +70 -0
- package/src/server/api/index.ts +8 -0
- package/src/server/api/router.ts +117 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +375 -0
- package/src/server/auth/account.ts +73 -0
- package/src/server/auth/activity.ts +213 -0
- package/src/server/auth/adapter.ts +79 -0
- package/src/server/auth/email.ts +125 -0
- package/src/server/auth/endpoints.ts +57 -0
- package/src/server/auth/factory.ts +583 -0
- package/src/server/auth/index.ts +17 -0
- package/src/server/auth/mfa-assurance.ts +55 -0
- package/src/server/auth/runtime.ts +26 -0
- package/src/server/auth/session.ts +19 -0
- package/src/server/auth/social-providers.ts +55 -0
- package/src/server/auth/user-agent.ts +16 -0
- package/src/server/auth/workspace-access.ts +23 -0
- package/src/server/cleanup/configured.ts +38 -0
- package/src/server/cleanup/documents.ts +231 -0
- package/src/server/cleanup/index.ts +29 -0
- package/src/server/cleanup/objects.ts +27 -0
- package/src/server/cleanup/revisions.ts +232 -0
- package/src/server/cleanup/uploads.ts +129 -0
- package/src/server/cleanup/worker.ts +79 -0
- package/src/server/collaboration/index.ts +28 -0
- package/src/server/collaboration/relay/authorize.ts +53 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +524 -0
- package/src/server/collaboration/relay/index.ts +15 -0
- package/src/server/collaboration/relay/persistence.ts +48 -0
- package/src/server/collaboration/routes.ts +77 -0
- package/src/server/collaboration/service.ts +188 -0
- package/src/server/config/deployment.ts +178 -0
- package/src/server/config/index.ts +20 -0
- package/src/server/config/limits.ts +8 -0
- package/src/server/config/public.ts +66 -0
- package/src/server/config/schema.ts +512 -0
- package/src/server/db/database.ts +15 -0
- package/src/server/db/expressions.ts +13 -0
- package/src/server/db/index.ts +21 -0
- package/src/server/db/migrate.ts +177 -0
- package/src/server/db/migrations/001_foundation.ts +164 -0
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +34 -0
- package/src/server/db/migrations/003_document_cleanup_claims.ts +23 -0
- package/src/server/db/migrations/004_document_sharing.ts +82 -0
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +13 -0
- package/src/server/db/migrations/006_invitation_continuation.ts +27 -0
- package/src/server/db/migrations/007_collaboration_state.ts +25 -0
- package/src/server/db/migrations/008_storage_reservations.ts +37 -0
- package/src/server/db/migrations/009_workspace_entitlements.ts +22 -0
- package/src/server/db/migrations/010_upload_finalization.ts +22 -0
- package/src/server/db/migrations/011_upload_finalization_lease.ts +12 -0
- package/src/server/db/migrations/012_transactional_email.ts +48 -0
- package/src/server/db/migrations/013_admin_enrollment.ts +54 -0
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +30 -0
- package/src/server/db/migrations/015_cloud_rate_limit.ts +22 -0
- package/src/server/db/migrations/016_auth_schema.ts +16 -0
- package/src/server/db/migrations/017_authentication_email_kinds.ts +32 -0
- package/src/server/db/migrations/018_authentication_mfa.ts +78 -0
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +42 -0
- package/src/server/db/migrations/020_workspace_invitations.ts +57 -0
- package/src/server/db/migrations/021_document_versions.ts +66 -0
- package/src/server/db/migrations/022_entitlement_external_version.ts +17 -0
- package/src/server/db/migrations/023_document_images.ts +61 -0
- package/src/server/db/migrations/024_account_activity.ts +66 -0
- package/src/server/db/migrations/025_table_names.ts +58 -0
- package/src/server/db/migrations/026_user_picture.ts +30 -0
- package/src/server/db/migrations/027_document_kind.ts +19 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +394 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +407 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +141 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +597 -0
- package/src/server/documents/summary.ts +37 -0
- package/src/server/documents/types.ts +14 -0
- package/src/server/documents/versions/checkpoint.ts +98 -0
- package/src/server/documents/versions/history.ts +25 -0
- package/src/server/documents/versions/index.ts +4 -0
- package/src/server/documents/versions/routes.ts +112 -0
- package/src/server/documents/versions/service.ts +397 -0
- package/src/server/email/crypto.ts +57 -0
- package/src/server/email/index.ts +18 -0
- package/src/server/email/invitations.ts +30 -0
- package/src/server/email/service.ts +245 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +279 -0
- package/src/server/internal/index.ts +8 -0
- package/src/server/internal/routes.ts +41 -0
- package/src/server/internal/schema.ts +10 -0
- package/src/server/internal/service.ts +44 -0
- package/src/server/invitations/index.ts +6 -0
- package/src/server/invitations/types.ts +30 -0
- package/src/server/objects/index.ts +16 -0
- package/src/server/objects/types.ts +81 -0
- package/src/server/pictures/index.ts +9 -0
- package/src/server/pictures/provider.ts +61 -0
- package/src/server/pictures/routes.ts +21 -0
- package/src/server/pictures/service.ts +235 -0
- package/src/server/policy/configured.ts +23 -0
- package/src/server/policy/database.ts +108 -0
- package/src/server/policy/entitlements.ts +29 -0
- package/src/server/policy/index.ts +17 -0
- package/src/server/policy/keys.ts +23 -0
- package/src/server/policy/policy.ts +117 -0
- package/src/server/policy/provider.ts +67 -0
- package/src/server/policy/schema.ts +69 -0
- package/src/server/policy/service.ts +85 -0
- package/src/server/policy/static.ts +62 -0
- package/src/server/quota/index.ts +7 -0
- package/src/server/quota/reconcile.ts +61 -0
- package/src/server/quota/storage.ts +165 -0
- package/src/server/rate-limit/cleanup.ts +16 -0
- package/src/server/rate-limit/index.ts +12 -0
- package/src/server/rate-limit/keys.ts +38 -0
- package/src/server/rate-limit/middleware.ts +137 -0
- package/src/server/rate-limit/postgres.ts +79 -0
- package/src/server/sharing/access.ts +29 -0
- package/src/server/sharing/actor.ts +12 -0
- package/src/server/sharing/capabilities/service.ts +262 -0
- package/src/server/sharing/continuation.ts +85 -0
- package/src/server/sharing/contracts.ts +80 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +12 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +41 -0
- package/src/server/sharing/index.ts +39 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +44 -0
- package/src/server/sharing/routes.ts +251 -0
- package/src/server/sharing/service.ts +117 -0
- package/src/server/validation.ts +24 -0
- package/src/server/worker/interval.ts +23 -0
- package/src/server/worker/periodic.ts +42 -0
- package/src/server/workspaces/index.ts +16 -0
- package/src/server/workspaces/members.ts +502 -0
- package/src/server/workspaces/repository.ts +91 -0
- package/src/server/workspaces/routes.ts +198 -0
- package/src/server/workspaces/service.ts +84 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import type { Kysely } from 'kysely'
|
|
3
|
+
import * as v from 'valibot'
|
|
4
|
+
|
|
5
|
+
import type { EntitlementSource, EntitlementSubject } from './entitlements'
|
|
6
|
+
import { staticEntitlementsSchema } from './schema'
|
|
7
|
+
import { staticEntitlementValues, type StaticEntitlementsInput } from './static'
|
|
8
|
+
|
|
9
|
+
export class DatabaseEntitlementSource implements EntitlementSource {
|
|
10
|
+
constructor(private readonly database: Kysely<CloudDatabase>) {}
|
|
11
|
+
|
|
12
|
+
private async values(subject: EntitlementSubject): Promise<Record<string, boolean | number>> {
|
|
13
|
+
if (subject.type !== 'workspace') return {}
|
|
14
|
+
const row = await this.database
|
|
15
|
+
.selectFrom('workspaceEntitlement')
|
|
16
|
+
.select('values')
|
|
17
|
+
.where('workspaceId', '=', subject.id)
|
|
18
|
+
.executeTakeFirst()
|
|
19
|
+
return row ? staticEntitlementValues(v.parse(staticEntitlementsSchema, row.values)) : {}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async boolean(subject: EntitlementSubject, key: string): Promise<boolean | null> {
|
|
23
|
+
const value = (await this.values(subject))[key]
|
|
24
|
+
return typeof value === 'boolean' ? value : null
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async number(subject: EntitlementSubject, key: string): Promise<number | null> {
|
|
28
|
+
const value = (await this.values(subject))[key]
|
|
29
|
+
return typeof value === 'number' ? value : null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async string(_subject: EntitlementSubject, _key: string): Promise<string | null> {
|
|
33
|
+
return null
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function createWorkspaceEntitlementRepository(database: Kysely<CloudDatabase>) {
|
|
38
|
+
return {
|
|
39
|
+
async get(workspaceId: string) {
|
|
40
|
+
return database
|
|
41
|
+
.selectFrom('workspaceEntitlement')
|
|
42
|
+
.selectAll()
|
|
43
|
+
.where('workspaceId', '=', workspaceId)
|
|
44
|
+
.executeTakeFirst()
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
async set(workspaceId: string, values: StaticEntitlementsInput, source: string) {
|
|
48
|
+
const validated = v.parse(staticEntitlementsSchema, values)
|
|
49
|
+
return database
|
|
50
|
+
.insertInto('workspaceEntitlement')
|
|
51
|
+
.values({ workspaceId, values: validated, source })
|
|
52
|
+
.onConflict((conflict) =>
|
|
53
|
+
conflict.column('workspaceId').doUpdateSet({
|
|
54
|
+
values: validated,
|
|
55
|
+
source,
|
|
56
|
+
revision: (expression) => expression('workspaceEntitlement.revision', '+', 1),
|
|
57
|
+
updatedAt: new Date()
|
|
58
|
+
})
|
|
59
|
+
)
|
|
60
|
+
.returningAll()
|
|
61
|
+
.executeTakeFirstOrThrow()
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Stores entitlements an external writer versioned, unless the stored row carries a newer
|
|
66
|
+
* version: billing events arrive out of order, and a late one must not undo a newer plan.
|
|
67
|
+
* An unversioned row, such as one an operator set, is replaced.
|
|
68
|
+
*/
|
|
69
|
+
async setVersioned(
|
|
70
|
+
workspaceId: string,
|
|
71
|
+
values: StaticEntitlementsInput,
|
|
72
|
+
source: string,
|
|
73
|
+
externalVersion: number
|
|
74
|
+
): Promise<{ applied: boolean }> {
|
|
75
|
+
const validated = v.parse(staticEntitlementsSchema, values)
|
|
76
|
+
const row = await database
|
|
77
|
+
.insertInto('workspaceEntitlement')
|
|
78
|
+
.values({ workspaceId, values: validated, source, externalVersion })
|
|
79
|
+
.onConflict((conflict) =>
|
|
80
|
+
conflict
|
|
81
|
+
.column('workspaceId')
|
|
82
|
+
.doUpdateSet({
|
|
83
|
+
values: validated,
|
|
84
|
+
source,
|
|
85
|
+
externalVersion,
|
|
86
|
+
revision: (expression) => expression('workspaceEntitlement.revision', '+', 1),
|
|
87
|
+
updatedAt: new Date()
|
|
88
|
+
})
|
|
89
|
+
.where((expression) =>
|
|
90
|
+
expression.or([
|
|
91
|
+
expression('workspaceEntitlement.externalVersion', 'is', null),
|
|
92
|
+
expression('workspaceEntitlement.externalVersion', '<', externalVersion)
|
|
93
|
+
])
|
|
94
|
+
)
|
|
95
|
+
)
|
|
96
|
+
.returning('workspaceId')
|
|
97
|
+
.executeTakeFirst()
|
|
98
|
+
return { applied: row !== undefined }
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
async clear(workspaceId: string): Promise<void> {
|
|
102
|
+
await database
|
|
103
|
+
.deleteFrom('workspaceEntitlement')
|
|
104
|
+
.where('workspaceId', '=', workspaceId)
|
|
105
|
+
.execute()
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type EntitlementSubject = {
|
|
2
|
+
type: 'workspace' | 'user' | 'organization' | 'deployment'
|
|
3
|
+
id: string
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type EntitlementSource = {
|
|
7
|
+
boolean(subject: EntitlementSubject, key: string): Promise<boolean | null>
|
|
8
|
+
number(subject: EntitlementSubject, key: string): Promise<number | null>
|
|
9
|
+
string(subject: EntitlementSubject, key: string): Promise<string | null>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class StaticEntitlementSource implements EntitlementSource {
|
|
13
|
+
constructor(private readonly values: Readonly<Record<string, boolean | number | string>>) {}
|
|
14
|
+
|
|
15
|
+
async boolean(_subject: EntitlementSubject, key: string): Promise<boolean | null> {
|
|
16
|
+
const value = this.values[key]
|
|
17
|
+
return typeof value === 'boolean' ? value : null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async number(_subject: EntitlementSubject, key: string): Promise<number | null> {
|
|
21
|
+
const value = this.values[key]
|
|
22
|
+
return typeof value === 'number' ? value : null
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async string(_subject: EntitlementSubject, key: string): Promise<string | null> {
|
|
26
|
+
const value = this.values[key]
|
|
27
|
+
return typeof value === 'string' ? value : null
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { createWorkspaceEntitlementRepository, DatabaseEntitlementSource } from './database'
|
|
2
|
+
export {
|
|
3
|
+
StaticEntitlementSource,
|
|
4
|
+
type EntitlementSource,
|
|
5
|
+
type EntitlementSubject
|
|
6
|
+
} from './entitlements'
|
|
7
|
+
export { CLOUD_FEATURE_KEYS, type CloudPolicyContext } from './keys'
|
|
8
|
+
export { EntitlementOpenFeatureProvider } from './provider'
|
|
9
|
+
export {
|
|
10
|
+
parseStaticEntitlementsTOML,
|
|
11
|
+
staticEntitlementValues,
|
|
12
|
+
type StaticEntitlementsInput
|
|
13
|
+
} from './static'
|
|
14
|
+
export { createConfiguredCloudPolicy } from './configured'
|
|
15
|
+
export { staticEntitlementsSchema, type StaticEntitlements } from './schema'
|
|
16
|
+
export { CloudPolicy, createDefaultCloudPolicy } from './policy'
|
|
17
|
+
export { createEntitlementService, type EntitlementService } from './service'
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const CLOUD_FEATURE_KEYS = {
|
|
2
|
+
links: 'cloud.sharing.links',
|
|
3
|
+
guestView: 'cloud.sharing.guest-view',
|
|
4
|
+
guestEdit: 'cloud.sharing.guest-edit',
|
|
5
|
+
guestPresence: 'cloud.sharing.guest-presence',
|
|
6
|
+
collaboration: 'cloud.collaboration.enabled',
|
|
7
|
+
serverEnforcedWrites: 'cloud.collaboration.server-enforced-writes',
|
|
8
|
+
revisionHistory: 'cloud.documents.revision-history',
|
|
9
|
+
maximumFileBytes: 'cloud.documents.maximum-file-bytes',
|
|
10
|
+
maximumStorageBytes: 'cloud.storage.maximum-bytes',
|
|
11
|
+
maximumParticipants: 'cloud.collaboration.maximum-participants',
|
|
12
|
+
cloudAI: 'cloud.ai.enabled',
|
|
13
|
+
monthlyCreditAllowance: 'cloud.ai.monthly-credit-allowance'
|
|
14
|
+
} as const
|
|
15
|
+
|
|
16
|
+
export type CloudPolicyContext = {
|
|
17
|
+
targetingKey: string
|
|
18
|
+
actorId?: string
|
|
19
|
+
workspaceId?: string
|
|
20
|
+
organizationId?: string
|
|
21
|
+
documentId?: string
|
|
22
|
+
deploymentMode: 'official' | 'self-hosted'
|
|
23
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { CLOUD_PROTOCOL_MAX_UPLOAD_BYTES } from '#cloud/server/config'
|
|
2
|
+
import {
|
|
3
|
+
DefaultLogger,
|
|
4
|
+
TypedInMemoryProvider,
|
|
5
|
+
type EvaluationContext,
|
|
6
|
+
type Provider,
|
|
7
|
+
type ResolutionDetails
|
|
8
|
+
} from '@openfeature/server-sdk'
|
|
9
|
+
|
|
10
|
+
import { CLOUD_FEATURE_KEYS, type CloudPolicyContext } from './keys'
|
|
11
|
+
|
|
12
|
+
const DEFAULT_FLAGS = {
|
|
13
|
+
[CLOUD_FEATURE_KEYS.links]: {
|
|
14
|
+
variants: { enabled: true, disabled: false },
|
|
15
|
+
defaultVariant: 'enabled',
|
|
16
|
+
disabled: false
|
|
17
|
+
},
|
|
18
|
+
[CLOUD_FEATURE_KEYS.guestView]: {
|
|
19
|
+
variants: { enabled: true, disabled: false },
|
|
20
|
+
defaultVariant: 'enabled',
|
|
21
|
+
disabled: false
|
|
22
|
+
},
|
|
23
|
+
[CLOUD_FEATURE_KEYS.guestEdit]: {
|
|
24
|
+
variants: { enabled: true, disabled: false },
|
|
25
|
+
defaultVariant: 'enabled',
|
|
26
|
+
disabled: false
|
|
27
|
+
},
|
|
28
|
+
[CLOUD_FEATURE_KEYS.guestPresence]: {
|
|
29
|
+
variants: { enabled: true, disabled: false },
|
|
30
|
+
defaultVariant: 'enabled',
|
|
31
|
+
disabled: false
|
|
32
|
+
},
|
|
33
|
+
[CLOUD_FEATURE_KEYS.collaboration]: {
|
|
34
|
+
variants: { enabled: true, disabled: false },
|
|
35
|
+
defaultVariant: 'enabled',
|
|
36
|
+
disabled: false
|
|
37
|
+
},
|
|
38
|
+
[CLOUD_FEATURE_KEYS.serverEnforcedWrites]: {
|
|
39
|
+
variants: { enabled: false, disabled: false },
|
|
40
|
+
defaultVariant: 'disabled',
|
|
41
|
+
disabled: false
|
|
42
|
+
},
|
|
43
|
+
[CLOUD_FEATURE_KEYS.revisionHistory]: {
|
|
44
|
+
variants: { enabled: true, disabled: false },
|
|
45
|
+
defaultVariant: 'enabled',
|
|
46
|
+
disabled: false
|
|
47
|
+
},
|
|
48
|
+
[CLOUD_FEATURE_KEYS.maximumFileBytes]: {
|
|
49
|
+
variants: { default: CLOUD_PROTOCOL_MAX_UPLOAD_BYTES },
|
|
50
|
+
defaultVariant: 'default',
|
|
51
|
+
disabled: false
|
|
52
|
+
}
|
|
53
|
+
} as const
|
|
54
|
+
|
|
55
|
+
function evaluationContext(context: CloudPolicyContext): EvaluationContext {
|
|
56
|
+
const evaluation: EvaluationContext = {
|
|
57
|
+
targetingKey: context.targetingKey,
|
|
58
|
+
deploymentMode: context.deploymentMode
|
|
59
|
+
}
|
|
60
|
+
if (context.actorId) evaluation.actorId = context.actorId
|
|
61
|
+
if (context.workspaceId) evaluation.workspaceId = context.workspaceId
|
|
62
|
+
if (context.organizationId) evaluation.organizationId = context.organizationId
|
|
63
|
+
if (context.documentId) evaluation.documentId = context.documentId
|
|
64
|
+
return evaluation
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const logger = new DefaultLogger()
|
|
68
|
+
|
|
69
|
+
/** A flag's value, or the default when the provider failed or reported an error, as a client does. */
|
|
70
|
+
async function resolved<T>(evaluation: () => Promise<ResolutionDetails<T>>, defaultValue: T) {
|
|
71
|
+
try {
|
|
72
|
+
const details = await evaluation()
|
|
73
|
+
return details.errorCode ? defaultValue : details.value
|
|
74
|
+
} catch {
|
|
75
|
+
return defaultValue
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Answers entitlement questions from one OpenFeature provider. It asks the provider directly
|
|
81
|
+
* rather than through the global `OpenFeature` API: each app has its own provider, and the API
|
|
82
|
+
* keeps every provider registered under a domain reachable for the life of the process, with the
|
|
83
|
+
* database its entitlement source holds.
|
|
84
|
+
*/
|
|
85
|
+
export class CloudPolicy {
|
|
86
|
+
constructor(private readonly provider: Provider) {}
|
|
87
|
+
|
|
88
|
+
boolean(key: string, defaultValue: boolean, context: CloudPolicyContext): Promise<boolean> {
|
|
89
|
+
return resolved(
|
|
90
|
+
() =>
|
|
91
|
+
this.provider.resolveBooleanEvaluation(
|
|
92
|
+
key,
|
|
93
|
+
defaultValue,
|
|
94
|
+
evaluationContext(context),
|
|
95
|
+
logger
|
|
96
|
+
),
|
|
97
|
+
defaultValue
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
number(key: string, defaultValue: number, context: CloudPolicyContext): Promise<number> {
|
|
102
|
+
return resolved(
|
|
103
|
+
() =>
|
|
104
|
+
this.provider.resolveNumberEvaluation(
|
|
105
|
+
key,
|
|
106
|
+
defaultValue,
|
|
107
|
+
evaluationContext(context),
|
|
108
|
+
logger
|
|
109
|
+
),
|
|
110
|
+
defaultValue
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function createDefaultCloudPolicy(provider?: Provider): CloudPolicy {
|
|
116
|
+
return new CloudPolicy(provider ?? new TypedInMemoryProvider(DEFAULT_FLAGS))
|
|
117
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type EvaluationContext,
|
|
3
|
+
type Logger,
|
|
4
|
+
type Provider,
|
|
5
|
+
type ProviderMetadata,
|
|
6
|
+
type ResolutionDetails
|
|
7
|
+
} from '@openfeature/server-sdk'
|
|
8
|
+
|
|
9
|
+
import type { EntitlementSource, EntitlementSubject } from './entitlements'
|
|
10
|
+
|
|
11
|
+
function subject(context: EvaluationContext): EntitlementSubject {
|
|
12
|
+
const type = context.subjectType
|
|
13
|
+
return {
|
|
14
|
+
type: type === 'user' || type === 'organization' || type === 'deployment' ? type : 'workspace',
|
|
15
|
+
id: context.targetingKey ?? 'deployment'
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class EntitlementOpenFeatureProvider implements Provider {
|
|
20
|
+
readonly metadata: ProviderMetadata = { name: 'openpencil-entitlements' }
|
|
21
|
+
readonly runsOn = 'server' as const
|
|
22
|
+
|
|
23
|
+
constructor(private readonly source: EntitlementSource) {}
|
|
24
|
+
|
|
25
|
+
async resolveBooleanEvaluation(
|
|
26
|
+
flagKey: string,
|
|
27
|
+
defaultValue: boolean,
|
|
28
|
+
context: EvaluationContext,
|
|
29
|
+
_logger: Logger
|
|
30
|
+
): Promise<ResolutionDetails<boolean>> {
|
|
31
|
+
const value = await this.source.boolean(subject(context), flagKey)
|
|
32
|
+
return value === null
|
|
33
|
+
? { value: defaultValue, reason: 'DEFAULT' }
|
|
34
|
+
: { value, reason: 'TARGETING_MATCH' }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async resolveNumberEvaluation(
|
|
38
|
+
flagKey: string,
|
|
39
|
+
defaultValue: number,
|
|
40
|
+
context: EvaluationContext,
|
|
41
|
+
_logger: Logger
|
|
42
|
+
): Promise<ResolutionDetails<number>> {
|
|
43
|
+
const value = await this.source.number(subject(context), flagKey)
|
|
44
|
+
return value === null
|
|
45
|
+
? { value: defaultValue, reason: 'DEFAULT' }
|
|
46
|
+
: { value, reason: 'TARGETING_MATCH' }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async resolveStringEvaluation(
|
|
50
|
+
flagKey: string,
|
|
51
|
+
defaultValue: string,
|
|
52
|
+
context: EvaluationContext,
|
|
53
|
+
_logger: Logger
|
|
54
|
+
): Promise<ResolutionDetails<string>> {
|
|
55
|
+
const value = await this.source.string(subject(context), flagKey)
|
|
56
|
+
return value === null
|
|
57
|
+
? { value: defaultValue, reason: 'DEFAULT' }
|
|
58
|
+
: { value, reason: 'TARGETING_MATCH' }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async resolveObjectEvaluation<T>(
|
|
62
|
+
_flagKey: string,
|
|
63
|
+
defaultValue: T
|
|
64
|
+
): Promise<ResolutionDetails<T>> {
|
|
65
|
+
return { value: defaultValue, reason: 'DEFAULT' }
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as v from 'valibot'
|
|
2
|
+
|
|
3
|
+
const positiveIntegerSchema = v.pipe(v.number(), v.integer(), v.minValue(1))
|
|
4
|
+
const nonNegativeIntegerSchema = v.pipe(v.number(), v.integer(), v.minValue(0))
|
|
5
|
+
|
|
6
|
+
export const staticEntitlementsSchema = v.object({
|
|
7
|
+
documents: v.optional(
|
|
8
|
+
v.object({
|
|
9
|
+
maximumFileBytes: v.optional(positiveIntegerSchema),
|
|
10
|
+
revisionHistory: v.optional(v.boolean())
|
|
11
|
+
}),
|
|
12
|
+
{}
|
|
13
|
+
),
|
|
14
|
+
storage: v.optional(v.object({ maximumBytes: v.optional(positiveIntegerSchema) }), {}),
|
|
15
|
+
sharing: v.optional(
|
|
16
|
+
v.object({
|
|
17
|
+
links: v.optional(v.boolean()),
|
|
18
|
+
guestView: v.optional(v.boolean()),
|
|
19
|
+
guestEdit: v.optional(v.boolean()),
|
|
20
|
+
guestPresence: v.optional(v.boolean())
|
|
21
|
+
}),
|
|
22
|
+
{}
|
|
23
|
+
),
|
|
24
|
+
collaboration: v.optional(
|
|
25
|
+
v.object({
|
|
26
|
+
enabled: v.optional(v.boolean()),
|
|
27
|
+
maximumParticipants: v.optional(positiveIntegerSchema)
|
|
28
|
+
}),
|
|
29
|
+
{}
|
|
30
|
+
),
|
|
31
|
+
ai: v.optional(
|
|
32
|
+
v.object({
|
|
33
|
+
enabled: v.optional(v.boolean()),
|
|
34
|
+
monthlyCreditAllowance: v.optional(nonNegativeIntegerSchema)
|
|
35
|
+
}),
|
|
36
|
+
{}
|
|
37
|
+
)
|
|
38
|
+
})
|
|
39
|
+
export type StaticEntitlements = v.InferOutput<typeof staticEntitlementsSchema>
|
|
40
|
+
|
|
41
|
+
export const staticEntitlementsTOMLSchema = v.strictObject({
|
|
42
|
+
documents: v.optional(
|
|
43
|
+
v.strictObject({
|
|
44
|
+
maximum_file_bytes: v.optional(positiveIntegerSchema),
|
|
45
|
+
revision_history: v.optional(v.boolean())
|
|
46
|
+
})
|
|
47
|
+
),
|
|
48
|
+
storage: v.optional(v.strictObject({ maximum_bytes: v.optional(positiveIntegerSchema) })),
|
|
49
|
+
sharing: v.optional(
|
|
50
|
+
v.strictObject({
|
|
51
|
+
links: v.optional(v.boolean()),
|
|
52
|
+
guest_view: v.optional(v.boolean()),
|
|
53
|
+
guest_edit: v.optional(v.boolean()),
|
|
54
|
+
guest_presence: v.optional(v.boolean())
|
|
55
|
+
})
|
|
56
|
+
),
|
|
57
|
+
collaboration: v.optional(
|
|
58
|
+
v.strictObject({
|
|
59
|
+
enabled: v.optional(v.boolean()),
|
|
60
|
+
maximum_participants: v.optional(positiveIntegerSchema)
|
|
61
|
+
})
|
|
62
|
+
),
|
|
63
|
+
ai: v.optional(
|
|
64
|
+
v.strictObject({
|
|
65
|
+
enabled: v.optional(v.boolean()),
|
|
66
|
+
monthly_credit_allowance: v.optional(nonNegativeIntegerSchema)
|
|
67
|
+
})
|
|
68
|
+
)
|
|
69
|
+
})
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { WorkspaceEntitlements } from '#cloud/contract'
|
|
2
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
3
|
+
import { DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
4
|
+
import { workspaceRole } from '#cloud/server/documents/repository'
|
|
5
|
+
import { createStorageQuotaService } from '#cloud/server/quota'
|
|
6
|
+
import type { Kysely } from 'kysely'
|
|
7
|
+
|
|
8
|
+
import { CLOUD_FEATURE_KEYS } from './keys'
|
|
9
|
+
import type { CloudPolicy } from './policy'
|
|
10
|
+
|
|
11
|
+
export function createEntitlementService(
|
|
12
|
+
database: Kysely<CloudDatabase>,
|
|
13
|
+
policy: CloudPolicy,
|
|
14
|
+
technicalMaximumFileBytes: number,
|
|
15
|
+
technicalMaximumParticipants = Number.MAX_SAFE_INTEGER
|
|
16
|
+
) {
|
|
17
|
+
const quota = createStorageQuotaService(database)
|
|
18
|
+
return {
|
|
19
|
+
async workspace(userId: string, workspaceId: string): Promise<WorkspaceEntitlements> {
|
|
20
|
+
if (!(await workspaceRole(database, userId, workspaceId))) throw new DocumentNotFoundError()
|
|
21
|
+
const context = {
|
|
22
|
+
targetingKey: workspaceId,
|
|
23
|
+
actorId: userId,
|
|
24
|
+
workspaceId,
|
|
25
|
+
deploymentMode: 'self-hosted' as const
|
|
26
|
+
}
|
|
27
|
+
const [
|
|
28
|
+
links,
|
|
29
|
+
guestView,
|
|
30
|
+
guestEdit,
|
|
31
|
+
guestPresence,
|
|
32
|
+
collaboration,
|
|
33
|
+
revisionHistory,
|
|
34
|
+
cloudAI,
|
|
35
|
+
maximumFileBytes,
|
|
36
|
+
maximumStorage,
|
|
37
|
+
maximumParticipants,
|
|
38
|
+
monthlyCreditAllowance,
|
|
39
|
+
usage
|
|
40
|
+
] = await Promise.all([
|
|
41
|
+
policy.boolean(CLOUD_FEATURE_KEYS.links, false, context),
|
|
42
|
+
policy.boolean(CLOUD_FEATURE_KEYS.guestView, false, context),
|
|
43
|
+
policy.boolean(CLOUD_FEATURE_KEYS.guestEdit, false, context),
|
|
44
|
+
policy.boolean(CLOUD_FEATURE_KEYS.guestPresence, false, context),
|
|
45
|
+
policy.boolean(CLOUD_FEATURE_KEYS.collaboration, false, context),
|
|
46
|
+
policy.boolean(CLOUD_FEATURE_KEYS.revisionHistory, false, context),
|
|
47
|
+
policy.boolean(CLOUD_FEATURE_KEYS.cloudAI, false, context),
|
|
48
|
+
policy.number(CLOUD_FEATURE_KEYS.maximumFileBytes, technicalMaximumFileBytes, context),
|
|
49
|
+
policy.number(CLOUD_FEATURE_KEYS.maximumStorageBytes, Number.MAX_SAFE_INTEGER, context),
|
|
50
|
+
policy.number(
|
|
51
|
+
CLOUD_FEATURE_KEYS.maximumParticipants,
|
|
52
|
+
technicalMaximumParticipants,
|
|
53
|
+
context
|
|
54
|
+
),
|
|
55
|
+
policy.number(CLOUD_FEATURE_KEYS.monthlyCreditAllowance, -1, context),
|
|
56
|
+
quota.snapshot(workspaceId)
|
|
57
|
+
])
|
|
58
|
+
// Technical limits are ceilings: an entitlement can lower them but never raise them.
|
|
59
|
+
const participants = Math.min(technicalMaximumParticipants, maximumParticipants)
|
|
60
|
+
return {
|
|
61
|
+
features: {
|
|
62
|
+
links,
|
|
63
|
+
guestView,
|
|
64
|
+
guestEdit,
|
|
65
|
+
guestPresence,
|
|
66
|
+
collaboration,
|
|
67
|
+
revisionHistory,
|
|
68
|
+
cloudAI
|
|
69
|
+
},
|
|
70
|
+
limits: {
|
|
71
|
+
maximumFileBytes: Math.min(technicalMaximumFileBytes, maximumFileBytes),
|
|
72
|
+
maximumStorageBytes: maximumStorage === Number.MAX_SAFE_INTEGER ? null : maximumStorage,
|
|
73
|
+
maximumParticipants: participants === Number.MAX_SAFE_INTEGER ? null : participants,
|
|
74
|
+
monthlyCreditAllowance: monthlyCreditAllowance < 0 ? null : monthlyCreditAllowance
|
|
75
|
+
},
|
|
76
|
+
usage: {
|
|
77
|
+
committedStorageBytes: usage.committedBytes,
|
|
78
|
+
reservedStorageBytes: usage.reservedBytes
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type EntitlementService = ReturnType<typeof createEntitlementService>
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { pickBy } from 'es-toolkit/object'
|
|
2
|
+
import { isNotNil } from 'es-toolkit/predicate'
|
|
3
|
+
import * as v from 'valibot'
|
|
4
|
+
|
|
5
|
+
import { CLOUD_FEATURE_KEYS } from './keys'
|
|
6
|
+
import {
|
|
7
|
+
staticEntitlementsSchema,
|
|
8
|
+
staticEntitlementsTOMLSchema,
|
|
9
|
+
type StaticEntitlements
|
|
10
|
+
} from './schema'
|
|
11
|
+
|
|
12
|
+
/** Entitlements as a writer sends them, with any group left out. */
|
|
13
|
+
export type StaticEntitlementsInput = v.InferInput<typeof staticEntitlementsSchema>
|
|
14
|
+
|
|
15
|
+
/** Entitlements as the deployment TOML spells them, renamed to their structured form. */
|
|
16
|
+
export function staticEntitlementsFromTOML(
|
|
17
|
+
value: v.InferOutput<typeof staticEntitlementsTOMLSchema>
|
|
18
|
+
): StaticEntitlements {
|
|
19
|
+
return v.parse(staticEntitlementsSchema, {
|
|
20
|
+
documents: {
|
|
21
|
+
maximumFileBytes: value.documents?.maximum_file_bytes,
|
|
22
|
+
revisionHistory: value.documents?.revision_history
|
|
23
|
+
},
|
|
24
|
+
storage: { maximumBytes: value.storage?.maximum_bytes },
|
|
25
|
+
sharing: {
|
|
26
|
+
links: value.sharing?.links,
|
|
27
|
+
guestView: value.sharing?.guest_view,
|
|
28
|
+
guestEdit: value.sharing?.guest_edit,
|
|
29
|
+
guestPresence: value.sharing?.guest_presence
|
|
30
|
+
},
|
|
31
|
+
collaboration: {
|
|
32
|
+
enabled: value.collaboration?.enabled,
|
|
33
|
+
maximumParticipants: value.collaboration?.maximum_participants
|
|
34
|
+
},
|
|
35
|
+
ai: {
|
|
36
|
+
enabled: value.ai?.enabled,
|
|
37
|
+
monthlyCreditAllowance: value.ai?.monthly_credit_allowance
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function parseStaticEntitlementsTOML(input: unknown): StaticEntitlements {
|
|
43
|
+
return staticEntitlementsFromTOML(v.parse(staticEntitlementsTOMLSchema, input))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function staticEntitlementValues(input: unknown): Record<string, boolean | number> {
|
|
47
|
+
const entitlements = v.parse(staticEntitlementsSchema, input)
|
|
48
|
+
const values: Record<string, boolean | number | undefined> = {
|
|
49
|
+
[CLOUD_FEATURE_KEYS.maximumFileBytes]: entitlements.documents.maximumFileBytes,
|
|
50
|
+
[CLOUD_FEATURE_KEYS.revisionHistory]: entitlements.documents.revisionHistory,
|
|
51
|
+
[CLOUD_FEATURE_KEYS.maximumStorageBytes]: entitlements.storage.maximumBytes,
|
|
52
|
+
[CLOUD_FEATURE_KEYS.links]: entitlements.sharing.links,
|
|
53
|
+
[CLOUD_FEATURE_KEYS.guestView]: entitlements.sharing.guestView,
|
|
54
|
+
[CLOUD_FEATURE_KEYS.guestEdit]: entitlements.sharing.guestEdit,
|
|
55
|
+
[CLOUD_FEATURE_KEYS.guestPresence]: entitlements.sharing.guestPresence,
|
|
56
|
+
[CLOUD_FEATURE_KEYS.collaboration]: entitlements.collaboration.enabled,
|
|
57
|
+
[CLOUD_FEATURE_KEYS.maximumParticipants]: entitlements.collaboration.maximumParticipants,
|
|
58
|
+
[CLOUD_FEATURE_KEYS.cloudAI]: entitlements.ai.enabled,
|
|
59
|
+
[CLOUD_FEATURE_KEYS.monthlyCreditAllowance]: entitlements.ai.monthlyCreditAllowance
|
|
60
|
+
}
|
|
61
|
+
return pickBy(values, isNotNil) as Record<string, boolean | number>
|
|
62
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { sumBy } from 'es-toolkit'
|
|
3
|
+
import type { Kysely } from 'kysely'
|
|
4
|
+
|
|
5
|
+
export type StorageReconciliation = {
|
|
6
|
+
workspaceId: string
|
|
7
|
+
recordedBytes: number
|
|
8
|
+
calculatedBytes: number
|
|
9
|
+
corrected: boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function createStorageReconciliationService(database: Kysely<CloudDatabase>) {
|
|
13
|
+
return {
|
|
14
|
+
async reconcile(options: { dryRun: boolean }): Promise<StorageReconciliation[]> {
|
|
15
|
+
const workspaces = await database.selectFrom('workspace').select('id').execute()
|
|
16
|
+
const results: StorageReconciliation[] = []
|
|
17
|
+
for (const workspace of workspaces) {
|
|
18
|
+
const [recorded, objects] = await Promise.all([
|
|
19
|
+
database
|
|
20
|
+
.selectFrom('workspaceStorageUsage')
|
|
21
|
+
.select('committedBytes')
|
|
22
|
+
.where('workspaceId', '=', workspace.id)
|
|
23
|
+
.executeTakeFirst(),
|
|
24
|
+
database
|
|
25
|
+
.selectFrom('storageObject')
|
|
26
|
+
.innerJoin('documentRevision', 'documentRevision.storageObjectId', 'storageObject.id')
|
|
27
|
+
.innerJoin('document', 'document.id', 'documentRevision.documentId')
|
|
28
|
+
.select(['storageObject.id', 'storageObject.byteSize'])
|
|
29
|
+
.distinct()
|
|
30
|
+
.where('document.workspaceId', '=', workspace.id)
|
|
31
|
+
// Images are the workspace's once, whichever revisions use them.
|
|
32
|
+
.union((union) =>
|
|
33
|
+
union
|
|
34
|
+
.selectFrom('storageObject')
|
|
35
|
+
.innerJoin('workspaceImage', 'workspaceImage.storageObjectId', 'storageObject.id')
|
|
36
|
+
.select(['storageObject.id', 'storageObject.byteSize'])
|
|
37
|
+
.where('workspaceImage.workspaceId', '=', workspace.id)
|
|
38
|
+
)
|
|
39
|
+
.execute()
|
|
40
|
+
])
|
|
41
|
+
const recordedBytes = Number(recorded?.committedBytes ?? 0)
|
|
42
|
+
const calculatedBytes = sumBy(objects, (object) => Number(object.byteSize))
|
|
43
|
+
const corrected = recordedBytes !== calculatedBytes
|
|
44
|
+
if (corrected && !options.dryRun) {
|
|
45
|
+
await database
|
|
46
|
+
.insertInto('workspaceStorageUsage')
|
|
47
|
+
.values({ workspaceId: workspace.id, committedBytes: calculatedBytes })
|
|
48
|
+
.onConflict((conflict) =>
|
|
49
|
+
conflict.column('workspaceId').doUpdateSet({
|
|
50
|
+
committedBytes: calculatedBytes,
|
|
51
|
+
updatedAt: new Date()
|
|
52
|
+
})
|
|
53
|
+
)
|
|
54
|
+
.execute()
|
|
55
|
+
}
|
|
56
|
+
results.push({ workspaceId: workspace.id, recordedBytes, calculatedBytes, corrected })
|
|
57
|
+
}
|
|
58
|
+
return results
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|