@open-pencil/cloud 0.17.0-beta.2 → 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 +469 -65
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +505 -787
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +3920 -2524
- 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 +3586 -1897
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5216 -4272
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5179 -3623
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +7 -6
- package/src/admin/api/routes.ts +59 -0
- 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 +22 -11
- 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 +62 -14
- package/src/runtime/cloudflare/worker.ts +39 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -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 +39 -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 +15 -31
- package/src/runtime/s3/objects.ts +24 -1
- package/src/runtime/site/server.ts +32 -5
- 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 +9 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app.ts +90 -16
- 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/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +5 -2
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +97 -25
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +24 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +82 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -116
- 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 +52 -3
- 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 +26 -0
- 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/service.ts +13 -4
- package/src/server/errors/index.ts +59 -16
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +46 -17
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- 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/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/middleware.ts +2 -2
- 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/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 +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +1 -0
- 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 +4 -0
- 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/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/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
DocumentConflictError,
|
|
13
13
|
DocumentForbiddenError,
|
|
14
14
|
DocumentNotFoundError,
|
|
15
|
+
DocumentPurgeFailedError,
|
|
15
16
|
UploadInvalidError
|
|
16
17
|
} from '#cloud/server/documents/errors'
|
|
17
18
|
import type { DocumentService } from '#cloud/server/documents/service'
|
|
@@ -50,6 +51,10 @@ export function documentDomainError(context: Context, error: unknown): Response
|
|
|
50
51
|
if (error instanceof StorageQuotaExceededError) {
|
|
51
52
|
return context.json({ error: { code: 'storage_quota_exceeded' as const } }, 409)
|
|
52
53
|
}
|
|
54
|
+
// Reported where cleanup failed, so the request handler doesn't report it again.
|
|
55
|
+
if (error instanceof DocumentPurgeFailedError) {
|
|
56
|
+
return context.json({ error: { code: 'purge_failed' as const } }, 503)
|
|
57
|
+
}
|
|
53
58
|
if (error instanceof UploadInvalidError) {
|
|
54
59
|
return context.json({ error: { code: 'invalid_upload' as const } }, 422)
|
|
55
60
|
}
|
|
@@ -111,6 +116,27 @@ export function createDocumentRoutes(
|
|
|
111
116
|
.get('/shared-documents', async (context) =>
|
|
112
117
|
context.json({ documents: await service.listShared(context.get('actor').userId) })
|
|
113
118
|
)
|
|
119
|
+
.get('/trash', async (context) =>
|
|
120
|
+
context.json({ documents: await service.trash.list(context.get('actor').userId) })
|
|
121
|
+
)
|
|
122
|
+
.post('/trash/:documentId/restore', documentParams, (context) =>
|
|
123
|
+
documentResponse(context, async () => {
|
|
124
|
+
await service.trash.restore(
|
|
125
|
+
context.get('actor').userId,
|
|
126
|
+
context.req.valid('param').documentId
|
|
127
|
+
)
|
|
128
|
+
return context.body(null, 204)
|
|
129
|
+
})
|
|
130
|
+
)
|
|
131
|
+
.delete('/trash/:documentId', documentParams, (context) =>
|
|
132
|
+
documentResponse(context, async () => {
|
|
133
|
+
await service.trash.purge(
|
|
134
|
+
context.get('actor').userId,
|
|
135
|
+
context.req.valid('param').documentId
|
|
136
|
+
)
|
|
137
|
+
return context.body(null, 204)
|
|
138
|
+
})
|
|
139
|
+
)
|
|
114
140
|
.get('/workspaces/:workspaceId/documents', workspaceParams, async (context) => {
|
|
115
141
|
const documents = await service.list(
|
|
116
142
|
context.get('actor').userId,
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
RequestImageUploadsInput,
|
|
10
10
|
SharedDocument
|
|
11
11
|
} from '#cloud/contract'
|
|
12
|
+
import { workspaceRoleCan } from '#cloud/server/auth/access-control'
|
|
12
13
|
import { createUploadCleanupService } from '#cloud/server/cleanup'
|
|
13
14
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
14
15
|
import {
|
|
@@ -37,8 +38,14 @@ import {
|
|
|
37
38
|
workspaceRole
|
|
38
39
|
} from '#cloud/server/documents/repository'
|
|
39
40
|
import { documentSummary, getDocumentSummaryRow } from '#cloud/server/documents/summary'
|
|
41
|
+
import {
|
|
42
|
+
createDocumentTrash,
|
|
43
|
+
DEFAULT_TRASH_OPTIONS,
|
|
44
|
+
type DocumentTrashOptions
|
|
45
|
+
} from '#cloud/server/documents/trash'
|
|
40
46
|
import type { CreateDocumentUploadResult } from '#cloud/server/documents/types'
|
|
41
47
|
import { announceVersions, recordCheckpoint } from '#cloud/server/documents/versions/checkpoint'
|
|
48
|
+
import type { CloudErrorReporter } from '#cloud/server/errors'
|
|
42
49
|
import type { ObjectStore, StoredObject } from '#cloud/server/objects'
|
|
43
50
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
44
51
|
import { createStorageQuotaService } from '#cloud/server/quota'
|
|
@@ -49,13 +56,16 @@ const DOWNLOAD_LIFETIME_MS = 5 * 60 * 1000
|
|
|
49
56
|
const UPLOAD_LIFETIME_MS = 15 * 60 * 1000
|
|
50
57
|
/** How many of a save's images are checked and landed at once. */
|
|
51
58
|
const IMAGE_COMMIT_CONCURRENCY = 8
|
|
52
|
-
const WRITABLE_ROLES = new Set(['admin', 'editor'])
|
|
53
59
|
|
|
54
60
|
export type DocumentServiceOptions = {
|
|
55
61
|
policy?: CloudPolicy
|
|
56
62
|
technicalMaximumUploadBytes?: number
|
|
57
63
|
/** Called once a save adds a checkpoint to the document's history. */
|
|
58
64
|
onVersionsChanged?: (documentId: string) => void
|
|
65
|
+
/** How long Trash keeps documents, the grace of one deleted forever, and the cleanup lease. */
|
|
66
|
+
trash?: DocumentTrashOptions
|
|
67
|
+
/** Where cleanup the service runs itself, such as of expired uploads, reports a failure. */
|
|
68
|
+
reportError?: CloudErrorReporter
|
|
59
69
|
}
|
|
60
70
|
|
|
61
71
|
/** Whether the stored object is the file the upload declared, which the commit names again. */
|
|
@@ -75,9 +85,10 @@ export function createDocumentService(
|
|
|
75
85
|
objects: ObjectStore,
|
|
76
86
|
options: DocumentServiceOptions = {}
|
|
77
87
|
) {
|
|
78
|
-
const cleanup = createUploadCleanupService(database, objects)
|
|
88
|
+
const cleanup = createUploadCleanupService(database, objects, options.reportError)
|
|
79
89
|
const quota = createStorageQuotaService(database)
|
|
80
90
|
const images = createDocumentImages(database, objects, quota)
|
|
91
|
+
const trash = createDocumentTrash(database, objects, options.trash ?? DEFAULT_TRASH_OPTIONS)
|
|
81
92
|
|
|
82
93
|
/**
|
|
83
94
|
* Makes a committed upload the document's current revision, unless it already was, and records
|
|
@@ -362,6 +373,9 @@ export function createDocumentService(
|
|
|
362
373
|
return listSharedDocuments(database, userId)
|
|
363
374
|
},
|
|
364
375
|
|
|
376
|
+
/** Trash: list what the person may restore, restore it, or delete it for good. */
|
|
377
|
+
trash,
|
|
378
|
+
|
|
365
379
|
async usage(userId: string, workspaceId: string) {
|
|
366
380
|
const role = await workspaceRole(database, userId, workspaceId)
|
|
367
381
|
if (!role) throw new DocumentNotFoundError()
|
|
@@ -423,7 +437,7 @@ export function createDocumentService(
|
|
|
423
437
|
}
|
|
424
438
|
await database
|
|
425
439
|
.updateTable('document')
|
|
426
|
-
.set({ deletedAt: new Date() })
|
|
440
|
+
.set({ deletedAt: new Date(), deletedBy: userId })
|
|
427
441
|
.where('id', '=', documentId)
|
|
428
442
|
.where('deletedAt', 'is', null)
|
|
429
443
|
.execute()
|
|
@@ -436,7 +450,7 @@ export function createDocumentService(
|
|
|
436
450
|
): Promise<DocumentSummary> {
|
|
437
451
|
const role = await workspaceRole(database, userId, workspaceId)
|
|
438
452
|
if (!role) throw new DocumentNotFoundError()
|
|
439
|
-
if (!
|
|
453
|
+
if (!workspaceRoleCan(role, { document: ['create'] })) throw new DocumentForbiddenError()
|
|
440
454
|
const documentId = input.id ?? crypto.randomUUID()
|
|
441
455
|
await insertDocument(database, {
|
|
442
456
|
id: documentId,
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import type { TrashedDocument, WorkspaceRole } from '#cloud/contract'
|
|
2
|
+
import { documentRoleCan, workspaceRoleCan } from '#cloud/server/auth/access-control'
|
|
3
|
+
import { createDocumentCleanupService } from '#cloud/server/cleanup'
|
|
4
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
5
|
+
import type { CloudErrorReporter } from '#cloud/server/errors'
|
|
6
|
+
import type { ObjectStore } from '#cloud/server/objects'
|
|
7
|
+
import { compact, uniq } from 'es-toolkit'
|
|
8
|
+
import type { Kysely, Transaction } from 'kysely'
|
|
9
|
+
import * as v from 'valibot'
|
|
10
|
+
|
|
11
|
+
import { trashedDocumentAllows } from './access'
|
|
12
|
+
import { DocumentForbiddenError, DocumentNotFoundError, DocumentPurgeFailedError } from './errors'
|
|
13
|
+
import { markPurged } from './purge'
|
|
14
|
+
|
|
15
|
+
const uuidSchema = v.pipe(v.string(), v.uuid())
|
|
16
|
+
|
|
17
|
+
const WORKSPACE_ROLES: WorkspaceRole[] = ['admin', 'editor', 'viewer']
|
|
18
|
+
|
|
19
|
+
/** The workspace roles whose members may delete any document there, and so restore it. */
|
|
20
|
+
const RESTORING_ROLES = WORKSPACE_ROLES.filter((role) =>
|
|
21
|
+
workspaceRoleCan(role, { document: ['delete'] })
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
export type DocumentTrashOptions = {
|
|
25
|
+
/** How long a deleted document stays in Trash before the cleanup worker deletes it for good. */
|
|
26
|
+
retentionMs: number
|
|
27
|
+
/**
|
|
28
|
+
* How long a document deleted forever keeps its files, out of sight, before cleanup destroys
|
|
29
|
+
* it; 0 destroys it at once.
|
|
30
|
+
*/
|
|
31
|
+
purgeGraceMs: number
|
|
32
|
+
/** How long a cleanup claim holds before another pass may take the document over. */
|
|
33
|
+
leaseDurationMs: number
|
|
34
|
+
/** Where destroying a document deleted forever with no grace reports a failure. */
|
|
35
|
+
reportError?: CloudErrorReporter
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** A deployment's defaults: 30 days in Trash, 7 days' grace, and a five-minute cleanup lease. */
|
|
39
|
+
export const DEFAULT_TRASH_OPTIONS: DocumentTrashOptions = {
|
|
40
|
+
retentionMs: 30 * 24 * 60 * 60_000,
|
|
41
|
+
purgeGraceMs: 7 * 24 * 60 * 60_000,
|
|
42
|
+
leaseDurationMs: 5 * 60_000
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The names of the accounts these IDs belong to; IDs that are not accounts are left out. */
|
|
46
|
+
async function accountNames(
|
|
47
|
+
database: Kysely<CloudDatabase>,
|
|
48
|
+
ids: (string | null)[]
|
|
49
|
+
): Promise<Map<string, string>> {
|
|
50
|
+
const accounts = uniq(compact(ids)).filter((id) => v.is(uuidSchema, id))
|
|
51
|
+
if (accounts.length === 0) return new Map()
|
|
52
|
+
const users = await database
|
|
53
|
+
.selectFrom('user')
|
|
54
|
+
.select(['id', 'name'])
|
|
55
|
+
.where('id', 'in', accounts)
|
|
56
|
+
.execute()
|
|
57
|
+
return new Map(users.map((user) => [user.id, user.name]))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function toISOString(value: Date | string): string {
|
|
61
|
+
return value instanceof Date ? value.toISOString() : value
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Trash: documents deleted in the person's workspaces that cleanup has not removed yet. Whoever
|
|
66
|
+
* may delete a document may restore it; its owner and workspace administrators may also delete
|
|
67
|
+
* it forever, which takes it out of Trash and off their storage at once (see `markPurged`).
|
|
68
|
+
*/
|
|
69
|
+
export function createDocumentTrash(
|
|
70
|
+
database: Kysely<CloudDatabase>,
|
|
71
|
+
objects: ObjectStore,
|
|
72
|
+
options: DocumentTrashOptions
|
|
73
|
+
) {
|
|
74
|
+
const cleanup = createDocumentCleanupService(database, objects, {
|
|
75
|
+
reportError: options.reportError
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
/** Deleted after this moment, a document is still in Trash. */
|
|
79
|
+
function retainedSince(now: Date): Date {
|
|
80
|
+
return new Date(now.getTime() - options.retentionMs)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
async list(userId: string, now = new Date()): Promise<TrashedDocument[]> {
|
|
85
|
+
const rows = await database
|
|
86
|
+
.selectFrom('document')
|
|
87
|
+
.innerJoin('workspaceMember', (join) =>
|
|
88
|
+
join
|
|
89
|
+
.onRef('workspaceMember.workspaceId', '=', 'document.workspaceId')
|
|
90
|
+
.on('workspaceMember.userId', '=', userId)
|
|
91
|
+
)
|
|
92
|
+
.innerJoin('workspace', 'workspace.id', 'document.workspaceId')
|
|
93
|
+
.select([
|
|
94
|
+
'document.id',
|
|
95
|
+
'document.workspaceId',
|
|
96
|
+
'workspace.name as workspaceName',
|
|
97
|
+
'document.name',
|
|
98
|
+
'document.kind',
|
|
99
|
+
'document.createdBy',
|
|
100
|
+
'document.deletedAt',
|
|
101
|
+
'document.deletedBy',
|
|
102
|
+
'workspaceMember.role'
|
|
103
|
+
])
|
|
104
|
+
.where('document.deletedAt', '>', retainedSince(now))
|
|
105
|
+
.where('document.purgedAt', 'is', null)
|
|
106
|
+
.where('document.cleanupClaimId', 'is', null)
|
|
107
|
+
.where('document.disabledAt', 'is', null)
|
|
108
|
+
.where((expression) =>
|
|
109
|
+
expression.or([
|
|
110
|
+
expression('workspaceMember.role', 'in', RESTORING_ROLES),
|
|
111
|
+
expression('document.createdBy', '=', userId)
|
|
112
|
+
])
|
|
113
|
+
)
|
|
114
|
+
.orderBy('document.deletedAt', 'desc')
|
|
115
|
+
.execute()
|
|
116
|
+
const names = await accountNames(
|
|
117
|
+
database,
|
|
118
|
+
rows.map((row) => row.deletedBy)
|
|
119
|
+
)
|
|
120
|
+
return rows.flatMap(({ createdBy, deletedAt, deletedBy, role, ...row }) => {
|
|
121
|
+
if (deletedAt === null) return []
|
|
122
|
+
const deleted = new Date(deletedAt)
|
|
123
|
+
const deleterName = deletedBy === null ? undefined : names.get(deletedBy)
|
|
124
|
+
return {
|
|
125
|
+
...row,
|
|
126
|
+
deletedAt: toISOString(deletedAt),
|
|
127
|
+
deletedBy: deleterName === undefined ? null : { name: deleterName },
|
|
128
|
+
purgeAt: new Date(deleted.getTime() + options.retentionMs).toISOString(),
|
|
129
|
+
canPurge:
|
|
130
|
+
workspaceRoleCan(role, { document: ['purge'] }) ||
|
|
131
|
+
(createdBy === userId && documentRoleCan('owner', { document: ['purge'] }))
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
/** Puts a document back where it was; it counted toward storage all along, so no quota check. */
|
|
137
|
+
async restore(userId: string, documentId: string, now = new Date()): Promise<void> {
|
|
138
|
+
await database.transaction().execute(async (transaction) => {
|
|
139
|
+
await lockInTrash(transaction, userId, documentId, now)
|
|
140
|
+
await transaction
|
|
141
|
+
.updateTable('document')
|
|
142
|
+
// Changed now, so devices that deleted it stop hiding it (see `reconcileStorageDocuments`).
|
|
143
|
+
.set({ deletedAt: null, deletedBy: null, updatedAt: now })
|
|
144
|
+
.where('id', '=', documentId)
|
|
145
|
+
.execute()
|
|
146
|
+
})
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Deletes a document in Trash forever, for the person: it leaves Trash and their storage now,
|
|
151
|
+
* and cleanup destroys it once the deployment's grace passes, or at once without one.
|
|
152
|
+
*/
|
|
153
|
+
async purge(userId: string, documentId: string, now = new Date()): Promise<void> {
|
|
154
|
+
await database.transaction().execute(async (transaction) => {
|
|
155
|
+
await lockInTrash(transaction, userId, documentId, now)
|
|
156
|
+
if (!(await trashedDocumentAllows(transaction, userId, documentId, 'purge'))) {
|
|
157
|
+
throw new DocumentForbiddenError()
|
|
158
|
+
}
|
|
159
|
+
await markPurged(transaction, [documentId], userId, now)
|
|
160
|
+
})
|
|
161
|
+
if (options.purgeGraceMs > 0) return
|
|
162
|
+
try {
|
|
163
|
+
// A save still in flight holds it for the next cleanup pass; it is out of Trash already.
|
|
164
|
+
await cleanup.destroyPurgedDocument(documentId, {
|
|
165
|
+
leaseDurationMs: options.leaseDurationMs,
|
|
166
|
+
now
|
|
167
|
+
})
|
|
168
|
+
} catch (error) {
|
|
169
|
+
throw new DocumentPurgeFailedError(`Document ${documentId} could not be destroyed`, {
|
|
170
|
+
cause: error
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Holds a document in Trash until the transaction ends: deleted, not forever, inside retention,
|
|
178
|
+
* and not being cleaned, by someone who may restore it; anything else is not found.
|
|
179
|
+
*/
|
|
180
|
+
async function lockInTrash(
|
|
181
|
+
transaction: Transaction<CloudDatabase>,
|
|
182
|
+
userId: string,
|
|
183
|
+
documentId: string,
|
|
184
|
+
now: Date
|
|
185
|
+
): Promise<void> {
|
|
186
|
+
const document = await transaction
|
|
187
|
+
.selectFrom('document')
|
|
188
|
+
.select(['deletedAt', 'purgedAt', 'cleanupClaimId'])
|
|
189
|
+
.where('id', '=', documentId)
|
|
190
|
+
.forUpdate()
|
|
191
|
+
.executeTakeFirst()
|
|
192
|
+
const inTrash =
|
|
193
|
+
document?.deletedAt != null &&
|
|
194
|
+
document.purgedAt === null &&
|
|
195
|
+
document.cleanupClaimId === null &&
|
|
196
|
+
new Date(document.deletedAt) > retainedSince(now)
|
|
197
|
+
if (!inTrash || !(await trashedDocumentAllows(transaction, userId, documentId, 'delete'))) {
|
|
198
|
+
throw new DocumentNotFoundError()
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export type DocumentTrash = ReturnType<typeof createDocumentTrash>
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
2
|
+
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
2
3
|
import type { Transaction } from 'kysely'
|
|
3
4
|
|
|
4
5
|
/** How long the newest version may be old before a save keeps the state it replaces. */
|
|
@@ -93,6 +94,6 @@ export function announceVersions(
|
|
|
93
94
|
try {
|
|
94
95
|
onVersionsChanged?.(documentId)
|
|
95
96
|
} catch (error) {
|
|
96
|
-
|
|
97
|
+
logCloud('error', 'Announcing a version history change failed', { error: loggedError(error) })
|
|
97
98
|
}
|
|
98
99
|
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
1
2
|
import { CLOUD_FEATURE_KEYS, type CloudPolicyContext } from '#cloud/server/policy/keys'
|
|
2
3
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
4
|
+
import { workspacePolicyContext } from '#cloud/server/policy/workspace'
|
|
5
|
+
import type { Kysely } from 'kysely'
|
|
3
6
|
|
|
4
7
|
/** How far back a workspace without full revision history keeps its versions. */
|
|
5
8
|
export const LIMITED_HISTORY_DAYS = 30
|
|
@@ -9,17 +12,18 @@ export const LIMITED_HISTORY_DAYS = 30
|
|
|
9
12
|
* `revisionHistory` entitlement off, only the last 30 days, as on a free plan.
|
|
10
13
|
*/
|
|
11
14
|
export async function historyStart(
|
|
15
|
+
database: Kysely<CloudDatabase>,
|
|
12
16
|
policy: CloudPolicy | undefined,
|
|
13
17
|
deploymentMode: CloudPolicyContext['deploymentMode'],
|
|
14
18
|
workspaceId: string,
|
|
15
19
|
now: Date
|
|
16
20
|
): Promise<Date | null> {
|
|
17
|
-
const
|
|
18
|
-
? await
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const context = policy
|
|
22
|
+
? await workspacePolicyContext(database, { workspaceId }, { deploymentMode })
|
|
23
|
+
: null
|
|
24
|
+
const full =
|
|
25
|
+
policy && context
|
|
26
|
+
? await policy.boolean(CLOUD_FEATURE_KEYS.revisionHistory, true, context)
|
|
27
|
+
: true
|
|
24
28
|
return full ? null : new Date(now.getTime() - LIMITED_HISTORY_DAYS * 24 * 60 * 60_000)
|
|
25
29
|
}
|
|
@@ -7,12 +7,14 @@ import type {
|
|
|
7
7
|
SaveDocumentVersionInput,
|
|
8
8
|
UpdateDocumentVersionInput
|
|
9
9
|
} from '#cloud/contract'
|
|
10
|
+
import { workspaceRoleCan } from '#cloud/server/auth/access-control'
|
|
10
11
|
import type { CloudDatabase } from '#cloud/server/db'
|
|
11
12
|
import { canEditDocument, resolveDocumentAccess } from '#cloud/server/documents/access'
|
|
12
13
|
import { DocumentForbiddenError, DocumentNotFoundError } from '#cloud/server/documents/errors'
|
|
13
14
|
import { copyRevisionImages, revisionImageDownloads } from '#cloud/server/documents/images'
|
|
14
15
|
import { findDocument, insertDocument, workspaceRole } from '#cloud/server/documents/repository'
|
|
15
16
|
import { documentSummary, getDocumentSummaryRow } from '#cloud/server/documents/summary'
|
|
17
|
+
import { logCloud, loggedError } from '#cloud/server/logging'
|
|
16
18
|
import type { ObjectStore } from '#cloud/server/objects'
|
|
17
19
|
import type { CloudPolicyContext } from '#cloud/server/policy/keys'
|
|
18
20
|
import type { CloudPolicy } from '#cloud/server/policy/policy'
|
|
@@ -24,7 +26,6 @@ import { historyStart } from './history'
|
|
|
24
26
|
const DOWNLOAD_LIFETIME_MS = 5 * 60 * 1000
|
|
25
27
|
const DEFAULT_PAGE_SIZE = 50
|
|
26
28
|
const MAXIMUM_PAGE_SIZE = 100
|
|
27
|
-
const WRITABLE_ROLES = new Set(['admin', 'editor'])
|
|
28
29
|
|
|
29
30
|
type VersionRow = {
|
|
30
31
|
id: string
|
|
@@ -103,7 +104,13 @@ export function createDocumentVersionService(
|
|
|
103
104
|
if (!document) throw new DocumentNotFoundError()
|
|
104
105
|
return {
|
|
105
106
|
document,
|
|
106
|
-
start: await historyStart(
|
|
107
|
+
start: await historyStart(
|
|
108
|
+
database,
|
|
109
|
+
options.policy,
|
|
110
|
+
deploymentMode,
|
|
111
|
+
document.workspaceId,
|
|
112
|
+
new Date()
|
|
113
|
+
)
|
|
107
114
|
}
|
|
108
115
|
}
|
|
109
116
|
|
|
@@ -331,7 +338,9 @@ export function createDocumentVersionService(
|
|
|
331
338
|
try {
|
|
332
339
|
options.onRestored?.(documentId)
|
|
333
340
|
} catch (error) {
|
|
334
|
-
|
|
341
|
+
logCloud('error', 'Ending the rooms of a restored document failed', {
|
|
342
|
+
error: loggedError(error)
|
|
343
|
+
})
|
|
335
344
|
}
|
|
336
345
|
const document = await findDocument(database, userId, documentId)
|
|
337
346
|
if (!document) throw new DocumentNotFoundError()
|
|
@@ -351,7 +360,7 @@ export function createDocumentVersionService(
|
|
|
351
360
|
const { document, start } = await openHistory(userId, documentId, false)
|
|
352
361
|
const role = await workspaceRole(database, userId, document.workspaceId)
|
|
353
362
|
if (!role) throw new DocumentNotFoundError()
|
|
354
|
-
if (!
|
|
363
|
+
if (!workspaceRoleCan(role, { document: ['create'] })) throw new DocumentForbiddenError()
|
|
355
364
|
const version = await findVersion(documentId, versionId, start)
|
|
356
365
|
const copyId = crypto.randomUUID()
|
|
357
366
|
const revisionId = crypto.randomUUID()
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { loggedError, writeJSONLine, type CloudLogWriter } from '#cloud/server/logging'
|
|
1
2
|
import type { Context, ErrorHandler } from 'hono'
|
|
2
3
|
import { HTTPException } from 'hono/http-exception'
|
|
3
4
|
import { routePath } from 'hono/route'
|
|
4
5
|
|
|
6
|
+
import type { ErrorReportContext, ErrorReporter } from './posthog'
|
|
7
|
+
|
|
5
8
|
/** Where the server caught an error. */
|
|
6
9
|
export const CLOUD_ERROR_SOURCES = [
|
|
7
10
|
'request',
|
|
@@ -14,43 +17,83 @@ export const CLOUD_ERROR_SOURCES = [
|
|
|
14
17
|
] as const
|
|
15
18
|
export type CloudErrorSource = (typeof CLOUD_ERROR_SOURCES)[number]
|
|
16
19
|
|
|
17
|
-
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
/**
|
|
21
|
+
* What the server knows about a caught error; never who made the request or what it carried. For a
|
|
22
|
+
* request: its method, the route pattern it matched (not the path with its IDs), and its ID.
|
|
23
|
+
*/
|
|
24
|
+
export type CloudErrorContext = ErrorReportContext<CloudErrorSource>
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Logs a caught error and, when the deployment configures error tracking, sends it there. Runtimes
|
|
28
|
+
* create one with `createLoggingErrorReporter`; its promise never rejects.
|
|
29
|
+
*/
|
|
30
|
+
export type CloudErrorReporter = ErrorReporter<CloudErrorSource>
|
|
24
31
|
|
|
25
32
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
33
|
+
* A reporter that writes the error as a structured line with the runtime's writer, then hands it
|
|
34
|
+
* to `send`, such as PostHog's, if the deployment has one.
|
|
28
35
|
*/
|
|
29
|
-
export
|
|
36
|
+
export function createLoggingErrorReporter(
|
|
37
|
+
log: CloudLogWriter,
|
|
38
|
+
send?: CloudErrorReporter
|
|
39
|
+
): CloudErrorReporter {
|
|
40
|
+
return async (error, context) => {
|
|
41
|
+
try {
|
|
42
|
+
log({
|
|
43
|
+
level: 'error',
|
|
44
|
+
message: context.message ?? 'Failed',
|
|
45
|
+
source: context.source,
|
|
46
|
+
method: context.method,
|
|
47
|
+
route: context.route,
|
|
48
|
+
requestId: context.requestId,
|
|
49
|
+
error: loggedError(error)
|
|
50
|
+
})
|
|
51
|
+
} catch (failure) {
|
|
52
|
+
// A writer that fails must not stop the report or reject; the console still gets both.
|
|
53
|
+
console.error('[Cloud] Writing a log line failed:', failure, error)
|
|
54
|
+
}
|
|
55
|
+
await send?.(error, context)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
30
58
|
|
|
31
|
-
/**
|
|
59
|
+
/** Without a reporter, errors are still logged, as lines of JSON. */
|
|
60
|
+
const logOnly = createLoggingErrorReporter(writeJSONLine)
|
|
61
|
+
|
|
62
|
+
/** Logs a caught error and reports it, through the runtime's reporter. */
|
|
32
63
|
export function reportCloudError(
|
|
33
64
|
reporter: CloudErrorReporter | undefined,
|
|
34
65
|
message: string,
|
|
35
66
|
error: unknown,
|
|
36
67
|
context: CloudErrorContext
|
|
37
68
|
): Promise<void> {
|
|
38
|
-
|
|
39
|
-
return reporter ? reporter(error, context) : Promise.resolve()
|
|
69
|
+
return (reporter ?? logOnly)(error, { ...context, message })
|
|
40
70
|
}
|
|
41
71
|
|
|
42
72
|
function requestContext(context: Context): CloudErrorContext {
|
|
43
|
-
|
|
73
|
+
const requestId: unknown = context.get('requestId')
|
|
74
|
+
return {
|
|
75
|
+
source: 'request',
|
|
76
|
+
method: context.req.method,
|
|
77
|
+
route: routePath(context),
|
|
78
|
+
...(typeof requestId === 'string' && { requestId })
|
|
79
|
+
}
|
|
44
80
|
}
|
|
45
81
|
|
|
46
82
|
/**
|
|
47
83
|
* The app's last error handler, as Hono's default: an `HTTPException` answers with its response,
|
|
48
|
-
* anything else is logged, reported, and answered with a 500
|
|
84
|
+
* anything else is logged, reported, and answered with a 500 naming the request's `X-Request-Id`.
|
|
49
85
|
*/
|
|
50
86
|
export function cloudErrorHandler(reporter: CloudErrorReporter | undefined): ErrorHandler {
|
|
51
87
|
return (error, context) => {
|
|
52
88
|
if (error instanceof HTTPException) return error.getResponse()
|
|
53
|
-
void reportCloudError(reporter, 'Request failed
|
|
89
|
+
void reportCloudError(reporter, 'Request failed', error, requestContext(context))
|
|
54
90
|
return context.text('Internal Server Error', 500)
|
|
55
91
|
}
|
|
56
92
|
}
|
|
93
|
+
|
|
94
|
+
export {
|
|
95
|
+
createPostHogErrorReporter,
|
|
96
|
+
type ErrorReportContext,
|
|
97
|
+
type ErrorReporter,
|
|
98
|
+
type PostHogErrorReporterOptions
|
|
99
|
+
} from './posthog'
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { PostHog, type PostHogOptions } from 'posthog-node'
|
|
2
|
+
|
|
3
|
+
/** Where a server caught an error, and for a request, its method, matched route, and ID. */
|
|
4
|
+
export type ErrorReportContext<Source extends string = string> = {
|
|
5
|
+
source: Source
|
|
6
|
+
method?: string
|
|
7
|
+
route?: string
|
|
8
|
+
/** The request's ID, as `cloudRequestId` named it; logs carry it too. */
|
|
9
|
+
requestId?: string
|
|
10
|
+
/** What failed, as the server's log says it; not sent to PostHog, whose report has the error. */
|
|
11
|
+
message?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Sends a caught error where it is seen; it never rejects. */
|
|
15
|
+
export type ErrorReporter<Source extends string = string> = (
|
|
16
|
+
error: unknown,
|
|
17
|
+
context: ErrorReportContext<Source>
|
|
18
|
+
) => Promise<void>
|
|
19
|
+
|
|
20
|
+
export type PostHogErrorReporterOptions = {
|
|
21
|
+
/** The project API key, or how to read it; a key that cannot be read yet is read again later. */
|
|
22
|
+
key: string | (() => Promise<string>)
|
|
23
|
+
host: string
|
|
24
|
+
/** Sent with every report: which service and deployment report, never who made the request. */
|
|
25
|
+
properties: Record<string, string>
|
|
26
|
+
/** The property that names the context's `source`, such as `cloud_source`. */
|
|
27
|
+
sourceProperty: string
|
|
28
|
+
/** Names the service in its own log, such as `Cloud`. */
|
|
29
|
+
logName: string
|
|
30
|
+
fetch?: PostHogOptions['fetch']
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Reports errors to PostHog error tracking, each as it is caught, as a `$exception` with no person
|
|
35
|
+
* profile and no GeoIP. A report carries `properties`, the source, and a request's method and
|
|
36
|
+
* matched route and ID; never who made the request or what it carried.
|
|
37
|
+
*/
|
|
38
|
+
export function createPostHogErrorReporter<Source extends string>(
|
|
39
|
+
options: PostHogErrorReporterOptions
|
|
40
|
+
): ErrorReporter<Source> {
|
|
41
|
+
const { key, logName } = options
|
|
42
|
+
const connect = async () => {
|
|
43
|
+
const client = new PostHog(typeof key === 'string' ? key : await key(), {
|
|
44
|
+
host: options.host,
|
|
45
|
+
disableGeoip: true,
|
|
46
|
+
// The SDK's own fetch unless a caller passes one.
|
|
47
|
+
...(options.fetch && { fetch: options.fetch })
|
|
48
|
+
})
|
|
49
|
+
// The SDK resolves a capture that PostHog refused and emits the refusal instead.
|
|
50
|
+
client.on('error', (failure: unknown) => {
|
|
51
|
+
console.error(`[${logName}] PostHog refused an error report:`, failure)
|
|
52
|
+
})
|
|
53
|
+
return client
|
|
54
|
+
}
|
|
55
|
+
let connecting: Promise<PostHog> | undefined
|
|
56
|
+
const posthog = async () => {
|
|
57
|
+
connecting ??= connect()
|
|
58
|
+
try {
|
|
59
|
+
return await connecting
|
|
60
|
+
} catch (failure) {
|
|
61
|
+
connecting = undefined
|
|
62
|
+
throw failure
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return async (error, context) => {
|
|
66
|
+
try {
|
|
67
|
+
const client = await posthog()
|
|
68
|
+
// Undefined request fields are left out of the event's JSON.
|
|
69
|
+
await client.captureExceptionImmediate(error, undefined, {
|
|
70
|
+
...options.properties,
|
|
71
|
+
[options.sourceProperty]: context.source,
|
|
72
|
+
http_method: context.method,
|
|
73
|
+
http_route: context.route,
|
|
74
|
+
request_id: context.requestId
|
|
75
|
+
})
|
|
76
|
+
} catch (failure) {
|
|
77
|
+
console.error(`[${logName}] Reporting an error failed:`, failure)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|