@pairdraw/cloud 0.0.0-stage → 0.16.0
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 +1578 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +2008 -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 +1673 -0
- package/dist/contract2.js.map +1 -0
- package/dist/email.d.ts +155 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +700 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +1945 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/listen.mjs +1905 -0
- package/dist/listen.mjs.map +1 -0
- package/dist/runtime-node.d.mts +10715 -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 +13772 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +8856 -0
- package/dist/server.js.map +1 -0
- package/dist/server.mjs +8836 -0
- package/dist/server.mjs.map +1 -0
- package/dist/server2.mjs +505 -0
- package/dist/server2.mjs.map +1 -0
- package/package.json +120 -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 +26 -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 +530 -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/profiles.ts +79 -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/comments.ts +149 -0
- package/src/contract/discovery.ts +81 -0
- package/src/contract/documents.ts +263 -0
- package/src/contract/entitlements.ts +28 -0
- package/src/contract/index.ts +210 -0
- package/src/contract/links.ts +108 -0
- package/src/contract/portal.ts +11 -0
- package/src/contract/profiles.ts +88 -0
- package/src/contract/relay.ts +370 -0
- package/src/contract/sharing.ts +215 -0
- package/src/contract/versions.ts +87 -0
- package/src/contract/workspaces.ts +100 -0
- package/src/email/index.ts +13 -0
- package/src/email/render.ts +304 -0
- package/src/email/schema.ts +144 -0
- package/src/email/templates/brand.ts +125 -0
- package/src/email/templates/document-invitation.ts +39 -0
- package/src/email/templates/enrollment.ts +30 -0
- package/src/email/templates/layout.ts +20 -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 +161 -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 +44 -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 +119 -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 +129 -0
- package/src/server/api/types.ts +7 -0
- package/src/server/app.ts +385 -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 +246 -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 +54 -0
- package/src/server/collaboration/relay/awareness.ts +104 -0
- package/src/server/collaboration/relay/comments.ts +86 -0
- package/src/server/collaboration/relay/documents.ts +39 -0
- package/src/server/collaboration/relay/hub.ts +567 -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 +190 -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_sharing_comments_snapshots_profiles.ts +74 -0
- package/src/server/db/migrations/helpers.ts +78 -0
- package/src/server/db/tables.ts +399 -0
- package/src/server/documents/access.ts +103 -0
- package/src/server/documents/errors.ts +16 -0
- package/src/server/documents/images.ts +405 -0
- package/src/server/documents/index.ts +14 -0
- package/src/server/documents/limits.ts +57 -0
- package/src/server/documents/repository.ts +131 -0
- package/src/server/documents/routes.ts +239 -0
- package/src/server/documents/service.ts +607 -0
- package/src/server/documents/summary.ts +36 -0
- package/src/server/documents/types.ts +13 -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 +396 -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 +247 -0
- package/src/server/email/types.ts +56 -0
- package/src/server/email/worker.ts +33 -0
- package/src/server/index.ts +288 -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/profiles/index.ts +12 -0
- package/src/server/profiles/routes.ts +103 -0
- package/src/server/profiles/service.ts +266 -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 +318 -0
- package/src/server/sharing/continuation.ts +83 -0
- package/src/server/sharing/contracts.ts +82 -0
- package/src/server/sharing/crypto.ts +48 -0
- package/src/server/sharing/document-routes.ts +41 -0
- package/src/server/sharing/errors.ts +20 -0
- package/src/server/sharing/grants/service.ts +79 -0
- package/src/server/sharing/http.ts +45 -0
- package/src/server/sharing/index.ts +40 -0
- package/src/server/sharing/invitations/service.ts +315 -0
- package/src/server/sharing/policy.ts +42 -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,63 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
AIRelayConfigError,
|
|
5
|
+
createAIRelayApp,
|
|
6
|
+
createAIRelayDatabase,
|
|
7
|
+
migrateAIRelayDatabase,
|
|
8
|
+
parseAIRelayTOML,
|
|
9
|
+
type AIRelayEnvironment
|
|
10
|
+
} from '#cloud/ai-relay'
|
|
11
|
+
import { CLOUD_AI_JWKS_PATH } from '#cloud/contract'
|
|
12
|
+
import { createRemoteJWKSet } from 'jose'
|
|
13
|
+
import { PostgresDialect } from 'kysely'
|
|
14
|
+
import { Pool } from 'pg'
|
|
15
|
+
|
|
16
|
+
import { listen } from '../listen'
|
|
17
|
+
|
|
18
|
+
export type NodeAIRelayOptions = {
|
|
19
|
+
environment?: AIRelayEnvironment
|
|
20
|
+
port?: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export async function startNodeAIRelay(options: NodeAIRelayOptions = {}) {
|
|
24
|
+
const environment = options.environment ?? process.env
|
|
25
|
+
const path = environment.PAIRDRAW_AI_RELAY_CONFIG
|
|
26
|
+
if (!path) {
|
|
27
|
+
throw new AIRelayConfigError('PAIRDRAW_AI_RELAY_CONFIG must name a relay TOML file')
|
|
28
|
+
}
|
|
29
|
+
const config = parseAIRelayTOML(await readFile(path, 'utf8'), environment)
|
|
30
|
+
const database = createAIRelayDatabase(
|
|
31
|
+
new PostgresDialect({ pool: new Pool({ connectionString: config.databaseURL }) })
|
|
32
|
+
)
|
|
33
|
+
try {
|
|
34
|
+
await migrateAIRelayDatabase(database)
|
|
35
|
+
} catch (error) {
|
|
36
|
+
await database.destroy()
|
|
37
|
+
throw error
|
|
38
|
+
}
|
|
39
|
+
// Metering outlives its answer; shutdown waits for it so no request goes uncharged.
|
|
40
|
+
const metering = new Set<Promise<unknown>>()
|
|
41
|
+
const app = createAIRelayApp({
|
|
42
|
+
config,
|
|
43
|
+
database,
|
|
44
|
+
keys: createRemoteJWKSet(new URL(CLOUD_AI_JWKS_PATH, config.cloudURL)),
|
|
45
|
+
waitUntil: (promise) => {
|
|
46
|
+
metering.add(promise)
|
|
47
|
+
void promise.finally(() => metering.delete(promise))
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
const server = await listen(
|
|
51
|
+
async (request) => app.fetch(request),
|
|
52
|
+
options.port ?? Number(environment.PORT ?? 8788),
|
|
53
|
+
environment.HOST ?? '0.0.0.0'
|
|
54
|
+
)
|
|
55
|
+
return {
|
|
56
|
+
url: server.url,
|
|
57
|
+
async stop() {
|
|
58
|
+
await server.close()
|
|
59
|
+
await Promise.allSettled(metering)
|
|
60
|
+
await database.destroy()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createPostgresCloudDatabase } from '#cloud/runtime/postgres/database'
|
|
2
|
+
import { createS3ObjectStore } from '#cloud/runtime/s3/objects'
|
|
3
|
+
import {
|
|
4
|
+
createCloudAuthenticationRuntime,
|
|
5
|
+
createCloudApp,
|
|
6
|
+
loadAISigningKey,
|
|
7
|
+
type CloudApp,
|
|
8
|
+
type CloudEnvironment,
|
|
9
|
+
type CloudServerConfig,
|
|
10
|
+
type ObjectStore
|
|
11
|
+
} from '#cloud/server'
|
|
12
|
+
|
|
13
|
+
import { loadNodeCloudServerConfig } from './config'
|
|
14
|
+
import { createNodeTransactionalEmailRuntime } from './email/runtime'
|
|
15
|
+
|
|
16
|
+
export type NodeCloudApplicationOptions = {
|
|
17
|
+
environment?: CloudEnvironment
|
|
18
|
+
databaseURL?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function createNodeCloudApplication(
|
|
22
|
+
options: NodeCloudApplicationOptions = {}
|
|
23
|
+
): Promise<{
|
|
24
|
+
app: CloudApp
|
|
25
|
+
config: CloudServerConfig
|
|
26
|
+
database: ReturnType<typeof createPostgresCloudDatabase>
|
|
27
|
+
objects: ObjectStore
|
|
28
|
+
email: ReturnType<typeof createNodeTransactionalEmailRuntime>['email']
|
|
29
|
+
}> {
|
|
30
|
+
const environment = options.environment ?? process.env
|
|
31
|
+
const config = await loadNodeCloudServerConfig(environment)
|
|
32
|
+
const aiSigningKey = await loadAISigningKey(config)
|
|
33
|
+
const database = createPostgresCloudDatabase({
|
|
34
|
+
connectionString: options.databaseURL ?? config.databaseURL
|
|
35
|
+
})
|
|
36
|
+
const objects = createS3ObjectStore(config)
|
|
37
|
+
const { email, invitationOutbox } = createNodeTransactionalEmailRuntime(config, database)
|
|
38
|
+
const { auth, enrollment } = createCloudAuthenticationRuntime(config, database, email)
|
|
39
|
+
const app = createCloudApp({
|
|
40
|
+
config,
|
|
41
|
+
database,
|
|
42
|
+
auth,
|
|
43
|
+
objects,
|
|
44
|
+
invitationOutbox,
|
|
45
|
+
transactionalEmail: email,
|
|
46
|
+
enrollment,
|
|
47
|
+
aiSigningKey
|
|
48
|
+
})
|
|
49
|
+
return { app, config, database, objects, email }
|
|
50
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createPostgresCloudDatabase } from '#cloud/runtime/postgres/database'
|
|
2
|
+
import {
|
|
3
|
+
createBetterAuthAdapter,
|
|
4
|
+
migrateCloudDatabase,
|
|
5
|
+
type CloudServerConfig
|
|
6
|
+
} from '#cloud/server'
|
|
7
|
+
|
|
8
|
+
import { loadNodeCloudServerConfig } from './config'
|
|
9
|
+
|
|
10
|
+
export async function createMigratedNodeCloudDatabase(
|
|
11
|
+
environment: Readonly<Record<string, string | undefined>>
|
|
12
|
+
): Promise<{
|
|
13
|
+
config: CloudServerConfig
|
|
14
|
+
database: ReturnType<typeof createPostgresCloudDatabase>
|
|
15
|
+
auth: ReturnType<typeof createBetterAuthAdapter>
|
|
16
|
+
}> {
|
|
17
|
+
const config = await loadNodeCloudServerConfig(environment)
|
|
18
|
+
const database = createPostgresCloudDatabase({ connectionString: config.databaseURL })
|
|
19
|
+
const auth = createBetterAuthAdapter(config, database)
|
|
20
|
+
await migrateCloudDatabase(database, { run: auth.migrate, schemaVersion: auth.schemaVersion })
|
|
21
|
+
return { config, database, auth }
|
|
22
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { createPostgresCloudDatabase } from '#cloud/runtime/postgres/database'
|
|
2
|
+
import { createS3ObjectStore } from '#cloud/runtime/s3/objects'
|
|
3
|
+
import {
|
|
4
|
+
cleanupPassOptions,
|
|
5
|
+
createCleanupServices,
|
|
6
|
+
createConfiguredCloudPolicy,
|
|
7
|
+
runCleanup
|
|
8
|
+
} from '#cloud/server'
|
|
9
|
+
|
|
10
|
+
import { loadNodeCloudServerConfig } from './config'
|
|
11
|
+
|
|
12
|
+
const config = await loadNodeCloudServerConfig(process.env)
|
|
13
|
+
const database = createPostgresCloudDatabase({ connectionString: config.databaseURL })
|
|
14
|
+
const objects = createS3ObjectStore(config)
|
|
15
|
+
try {
|
|
16
|
+
const policy = createConfiguredCloudPolicy(config, database)
|
|
17
|
+
const result = await runCleanup(
|
|
18
|
+
createCleanupServices(config, database, objects, policy),
|
|
19
|
+
cleanupPassOptions(config)
|
|
20
|
+
)
|
|
21
|
+
console.log(JSON.stringify(result))
|
|
22
|
+
} finally {
|
|
23
|
+
await database.destroy()
|
|
24
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createMigratedNodeCloudDatabase } from './bootstrap'
|
|
2
|
+
|
|
3
|
+
type MigratedNodeCloudDatabase = Awaited<ReturnType<typeof createMigratedNodeCloudDatabase>>
|
|
4
|
+
|
|
5
|
+
export async function withNodeCloudDatabase<T>(
|
|
6
|
+
operation: (
|
|
7
|
+
database: MigratedNodeCloudDatabase['database'],
|
|
8
|
+
config: MigratedNodeCloudDatabase['config']
|
|
9
|
+
) => Promise<T>
|
|
10
|
+
): Promise<T> {
|
|
11
|
+
const { config, database } = await createMigratedNodeCloudDatabase(process.env)
|
|
12
|
+
try {
|
|
13
|
+
return await operation(database, config)
|
|
14
|
+
} finally {
|
|
15
|
+
await database.destroy()
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
CloudConfigError,
|
|
5
|
+
parseCloudDeploymentTOML,
|
|
6
|
+
type CloudEnvironment,
|
|
7
|
+
type CloudServerConfig
|
|
8
|
+
} from '#cloud/server'
|
|
9
|
+
|
|
10
|
+
export async function loadNodeCloudServerConfig(
|
|
11
|
+
environment: CloudEnvironment
|
|
12
|
+
): Promise<CloudServerConfig> {
|
|
13
|
+
const path = environment.PAIRDRAW_CLOUD_CONFIG
|
|
14
|
+
if (!path) throw new CloudConfigError('PAIRDRAW_CLOUD_CONFIG must name a deployment TOML file')
|
|
15
|
+
return parseCloudDeploymentTOML(await readFile(path, 'utf8'), environment)
|
|
16
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createInvitationOutbox,
|
|
3
|
+
createTransactionalEmailService,
|
|
4
|
+
type CloudServerConfig,
|
|
5
|
+
type TransactionalEmailService,
|
|
6
|
+
type TransactionalEmailTransport
|
|
7
|
+
} from '#cloud/server'
|
|
8
|
+
import type { CloudDatabase } from '#cloud/server/db'
|
|
9
|
+
import type { Kysely } from 'kysely'
|
|
10
|
+
|
|
11
|
+
import { createSMTPTransactionalEmailTransport } from './transport'
|
|
12
|
+
|
|
13
|
+
export type NodeTransactionalEmailRuntime = {
|
|
14
|
+
email: TransactionalEmailService
|
|
15
|
+
transport?: TransactionalEmailTransport
|
|
16
|
+
invitationOutbox?: ReturnType<typeof createInvitationOutbox>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function createNodeTransactionalEmailRuntime(
|
|
20
|
+
config: CloudServerConfig,
|
|
21
|
+
database: Kysely<CloudDatabase>
|
|
22
|
+
): NodeTransactionalEmailRuntime {
|
|
23
|
+
const transport =
|
|
24
|
+
config.emailTransport === 'smtp' && config.smtpHost && config.smtpPort
|
|
25
|
+
? createSMTPTransactionalEmailTransport({
|
|
26
|
+
host: config.smtpHost,
|
|
27
|
+
port: config.smtpPort,
|
|
28
|
+
secure: config.smtpSecure ?? config.smtpPort === 465,
|
|
29
|
+
user: config.smtpUser,
|
|
30
|
+
password: config.smtpPassword
|
|
31
|
+
})
|
|
32
|
+
: undefined
|
|
33
|
+
const email = createTransactionalEmailService(database, {
|
|
34
|
+
encryptionSecret: config.authSecret,
|
|
35
|
+
from: config.emailFrom ?? '',
|
|
36
|
+
site: config.publicURL,
|
|
37
|
+
transport
|
|
38
|
+
})
|
|
39
|
+
return {
|
|
40
|
+
email,
|
|
41
|
+
transport,
|
|
42
|
+
invitationOutbox: transport ? createInvitationOutbox(email) : undefined
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { renderTransactionalEmail } from '#cloud/email'
|
|
2
|
+
import type { TransactionalEmailTransport } from '#cloud/server/email'
|
|
3
|
+
import type { DocumentInvitationMessage, InvitationDelivery } from '#cloud/server/invitations'
|
|
4
|
+
import nodemailer, { type Transporter } from 'nodemailer'
|
|
5
|
+
import type Mail from 'nodemailer/lib/mailer/index.js'
|
|
6
|
+
|
|
7
|
+
type NodemailerTransporter = {
|
|
8
|
+
sendMail(mail: Mail.Options): Promise<unknown>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type NodemailerTransactionalEmailTransportOptions = {
|
|
12
|
+
transporter: NodemailerTransporter
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createNodemailerTransactionalEmailTransport(
|
|
16
|
+
options: NodemailerTransactionalEmailTransportOptions
|
|
17
|
+
): TransactionalEmailTransport {
|
|
18
|
+
return {
|
|
19
|
+
id: 'smtp',
|
|
20
|
+
async send(message) {
|
|
21
|
+
const result = (await options.transporter.sendMail({
|
|
22
|
+
from: message.from,
|
|
23
|
+
to: message.to,
|
|
24
|
+
subject: message.subject,
|
|
25
|
+
html: message.html,
|
|
26
|
+
text: message.text,
|
|
27
|
+
headers: message.headers,
|
|
28
|
+
messageId: `<${message.deliveryId}@pairdraw.cloud>`
|
|
29
|
+
})) as { messageId?: unknown }
|
|
30
|
+
if (typeof result.messageId !== 'string') {
|
|
31
|
+
throw new TypeError('SMTP transport returned no message ID')
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
transportMessageId: result.messageId,
|
|
35
|
+
acceptedAt: new Date().toISOString()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type SMTPTransactionalEmailTransportOptions = {
|
|
42
|
+
host: string
|
|
43
|
+
port: number
|
|
44
|
+
secure: boolean
|
|
45
|
+
user?: string
|
|
46
|
+
password?: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function createSMTPTransporter(options: SMTPTransactionalEmailTransportOptions): Transporter {
|
|
50
|
+
return nodemailer.createTransport({
|
|
51
|
+
host: options.host,
|
|
52
|
+
port: options.port,
|
|
53
|
+
secure: options.secure,
|
|
54
|
+
connectionTimeout: 10_000,
|
|
55
|
+
greetingTimeout: 10_000,
|
|
56
|
+
socketTimeout: 30_000,
|
|
57
|
+
auth:
|
|
58
|
+
options.user && options.password ? { user: options.user, pass: options.password } : undefined
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function createSMTPTransactionalEmailTransport(
|
|
63
|
+
options: SMTPTransactionalEmailTransportOptions
|
|
64
|
+
): TransactionalEmailTransport {
|
|
65
|
+
return createNodemailerTransactionalEmailTransport({
|
|
66
|
+
transporter: createSMTPTransporter(options)
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export type NodemailerInvitationDeliveryOptions = NodemailerTransactionalEmailTransportOptions & {
|
|
71
|
+
from: string
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function createNodemailerInvitationDelivery(
|
|
75
|
+
options: NodemailerInvitationDeliveryOptions
|
|
76
|
+
): InvitationDelivery {
|
|
77
|
+
const transport = createNodemailerTransactionalEmailTransport(options)
|
|
78
|
+
return {
|
|
79
|
+
async sendDocumentInvitation(message: DocumentInvitationMessage): Promise<void> {
|
|
80
|
+
const rendered = await renderTransactionalEmail({
|
|
81
|
+
kind: 'document-invitation',
|
|
82
|
+
payload: {
|
|
83
|
+
inviterName: message.inviterName,
|
|
84
|
+
documentName: message.documentName,
|
|
85
|
+
permission: message.permission,
|
|
86
|
+
expiresAt: message.expiresAt,
|
|
87
|
+
acceptanceURL: message.acceptanceURL
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
await transport.send({
|
|
91
|
+
deliveryId: message.deliveryId,
|
|
92
|
+
from: options.from,
|
|
93
|
+
to: message.recipientEmail,
|
|
94
|
+
...rendered,
|
|
95
|
+
headers: { 'X-Pairdraw-Delivery-ID': message.deliveryId }
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type SMTPInvitationDeliveryOptions = SMTPTransactionalEmailTransportOptions & {
|
|
102
|
+
from: string
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function createSMTPInvitationDelivery(
|
|
106
|
+
options: SMTPInvitationDeliveryOptions
|
|
107
|
+
): InvitationDelivery {
|
|
108
|
+
return createNodemailerInvitationDelivery({
|
|
109
|
+
transporter: createSMTPTransporter(options),
|
|
110
|
+
from: options.from
|
|
111
|
+
})
|
|
112
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
|
|
3
|
+
import { createWorkspaceEntitlementRepository, parseStaticEntitlementsTOML } from '#cloud/server'
|
|
4
|
+
import { parse as parseTOML } from 'smol-toml'
|
|
5
|
+
|
|
6
|
+
import { withNodeCloudDatabase } from '../command'
|
|
7
|
+
import { entitlementWriteRefusal } from './refusal'
|
|
8
|
+
|
|
9
|
+
const operation = process.argv[2]
|
|
10
|
+
const workspaceIndex = process.argv.indexOf('--workspace')
|
|
11
|
+
const fileIndex = process.argv.indexOf('--file')
|
|
12
|
+
const dryRun = process.argv.includes('--dry-run')
|
|
13
|
+
const workspaceId = workspaceIndex !== -1 ? process.argv[workspaceIndex + 1] : undefined
|
|
14
|
+
if (!workspaceId) throw new Error('Expected --workspace <uuid>')
|
|
15
|
+
|
|
16
|
+
await withNodeCloudDatabase(async (database, config) => {
|
|
17
|
+
const repository = createWorkspaceEntitlementRepository(database)
|
|
18
|
+
const refusal = entitlementWriteRefusal(config, operation, dryRun)
|
|
19
|
+
if (refusal) throw new Error(refusal)
|
|
20
|
+
if (operation === 'get') {
|
|
21
|
+
console.log(JSON.stringify({ entitlement: await repository.get(workspaceId) }))
|
|
22
|
+
} else if (operation === 'clear') {
|
|
23
|
+
if (dryRun) {
|
|
24
|
+
console.log(JSON.stringify({ dryRun, cleared: false, workspaceId }))
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
await repository.clear(workspaceId)
|
|
28
|
+
console.log(JSON.stringify({ cleared: true, workspaceId }))
|
|
29
|
+
} else if (operation === 'set') {
|
|
30
|
+
const file = fileIndex !== -1 ? process.argv[fileIndex + 1] : undefined
|
|
31
|
+
if (!file) throw new Error('Expected --file <path>')
|
|
32
|
+
const values = parseStaticEntitlementsTOML(parseTOML(await readFile(file, 'utf8')))
|
|
33
|
+
if (dryRun) {
|
|
34
|
+
console.log(JSON.stringify({ dryRun, workspaceId, values }))
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
console.log(JSON.stringify({ entitlement: await repository.set(workspaceId, values, 'cli') }))
|
|
38
|
+
} else {
|
|
39
|
+
throw new Error('Expected operation: get, set, or clear')
|
|
40
|
+
}
|
|
41
|
+
})
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CloudServerConfig } from '#cloud/server'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Why the `entitlements` command must not write, if it must not: with
|
|
5
|
+
* `[entitlements] source = "static"` the server never reads the database rows it would change.
|
|
6
|
+
*/
|
|
7
|
+
export function entitlementWriteRefusal(
|
|
8
|
+
config: Pick<CloudServerConfig, 'staticEntitlements'>,
|
|
9
|
+
operation: string | undefined,
|
|
10
|
+
dryRun: boolean
|
|
11
|
+
): string | null {
|
|
12
|
+
if (!config.staticEntitlements || dryRun) return null
|
|
13
|
+
if (operation !== 'set' && operation !== 'clear') return null
|
|
14
|
+
return 'Entitlements come from the static [entitlements] table in the deployment TOML; change them there'
|
|
15
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export { startNodeAIRelay, type NodeAIRelayOptions } from './ai-relay/server'
|
|
2
|
+
export { createMigratedNodeCloudDatabase } from './bootstrap'
|
|
3
|
+
export { createNodeCloudApplication, type NodeCloudApplicationOptions } from './application'
|
|
4
|
+
export { loadNodeCloudServerConfig } from './config'
|
|
5
|
+
export {
|
|
6
|
+
createNodeTransactionalEmailRuntime,
|
|
7
|
+
type NodeTransactionalEmailRuntime
|
|
8
|
+
} from './email/runtime'
|
|
9
|
+
export {
|
|
10
|
+
createPostgresCloudDatabase,
|
|
11
|
+
type PostgresCloudDatabaseOptions
|
|
12
|
+
} from '#cloud/runtime/postgres/database'
|
|
13
|
+
export {
|
|
14
|
+
createNodemailerInvitationDelivery,
|
|
15
|
+
createSMTPInvitationDelivery,
|
|
16
|
+
createNodemailerTransactionalEmailTransport,
|
|
17
|
+
createSMTPTransactionalEmailTransport,
|
|
18
|
+
type NodemailerInvitationDeliveryOptions,
|
|
19
|
+
type SMTPInvitationDeliveryOptions,
|
|
20
|
+
type NodemailerTransactionalEmailTransportOptions,
|
|
21
|
+
type SMTPTransactionalEmailTransportOptions
|
|
22
|
+
} from './email/transport'
|
|
23
|
+
export { createS3ObjectStore } from '#cloud/runtime/s3/objects'
|
|
24
|
+
export { attachCollaborationRelay } from './relay'
|
|
25
|
+
export { startNodeCloudServer, type NodeCloudServerOptions } from './server'
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createServer } from 'node:http'
|
|
2
|
+
|
|
3
|
+
import { getRequestListener } from '@hono/node-server'
|
|
4
|
+
|
|
5
|
+
/** Serves a fetch handler over Node HTTP and resolves once the port is bound. */
|
|
6
|
+
export async function listen(
|
|
7
|
+
fetch: (request: Request) => Promise<Response>,
|
|
8
|
+
port: number,
|
|
9
|
+
hostname: string
|
|
10
|
+
) {
|
|
11
|
+
const listener = getRequestListener(fetch)
|
|
12
|
+
const server = createServer((request, response) => {
|
|
13
|
+
void listener(request, response)
|
|
14
|
+
})
|
|
15
|
+
await new Promise<void>((resolve, reject) => {
|
|
16
|
+
server.once('error', reject)
|
|
17
|
+
server.listen(port, hostname, () => {
|
|
18
|
+
server.off('error', reject)
|
|
19
|
+
resolve()
|
|
20
|
+
})
|
|
21
|
+
})
|
|
22
|
+
const address = server.address()
|
|
23
|
+
const boundPort = typeof address === 'object' && address ? address.port : port
|
|
24
|
+
return {
|
|
25
|
+
url: new URL(`http://localhost:${boundPort}`),
|
|
26
|
+
http: server,
|
|
27
|
+
close: () =>
|
|
28
|
+
new Promise<void>((done, fail) => {
|
|
29
|
+
server.close((error) => {
|
|
30
|
+
if (error) fail(error)
|
|
31
|
+
else done()
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import dedent from 'dedent'
|
|
3
|
+
|
|
4
|
+
import { startNodeCloudServer } from './server'
|
|
5
|
+
|
|
6
|
+
if (process.argv.includes('--help') || process.argv.includes('-h')) {
|
|
7
|
+
console.log(dedent`
|
|
8
|
+
Usage: pairdraw-cloud
|
|
9
|
+
|
|
10
|
+
Runs Pairdraw Cloud. Settings come from the environment:
|
|
11
|
+
PAIRDRAW_CLOUD_CONFIG the deployment TOML file (required)
|
|
12
|
+
PAIRDRAW_CLOUD_SITE_DIR the editor and portal build to serve
|
|
13
|
+
PAIRDRAW_CLOUD_DEV_SITE_URL
|
|
14
|
+
a Vite dev server to serve them from instead (cloud:dev)
|
|
15
|
+
PORT, HOST where to listen (default 8787 on 0.0.0.0)
|
|
16
|
+
plus the secrets the TOML names. See https://github.com/BadryansahBangsawan/pairdraw/blob/master/packages/docs/reference/self-hosting-cloud.md
|
|
17
|
+
`)
|
|
18
|
+
process.exit(0)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const cloud = await startNodeCloudServer()
|
|
22
|
+
console.log(`Pairdraw Cloud listening on ${cloud.url}`)
|
|
23
|
+
|
|
24
|
+
function shutdown() {
|
|
25
|
+
void cloud.stop().then(() => process.exit(0))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
process.on('SIGINT', shutdown)
|
|
29
|
+
process.on('SIGTERM', shutdown)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createPostgresCloudDatabase } from '#cloud/runtime/postgres/database'
|
|
2
|
+
import { createBetterAuthAdapter, migrateCloudDatabase } from '#cloud/server'
|
|
3
|
+
|
|
4
|
+
import { loadNodeCloudServerConfig } from './config'
|
|
5
|
+
|
|
6
|
+
const config = await loadNodeCloudServerConfig(process.env)
|
|
7
|
+
const database = createPostgresCloudDatabase({ connectionString: config.databaseURL })
|
|
8
|
+
try {
|
|
9
|
+
const auth = createBetterAuthAdapter(config, database)
|
|
10
|
+
await migrateCloudDatabase(database, { run: auth.migrate, schemaVersion: auth.schemaVersion })
|
|
11
|
+
} finally {
|
|
12
|
+
await database.destroy()
|
|
13
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { createStorageReconciliationService } from '#cloud/server'
|
|
2
|
+
|
|
3
|
+
import { withNodeCloudDatabase } from './command'
|
|
4
|
+
|
|
5
|
+
const dryRun = process.argv.includes('--dry-run')
|
|
6
|
+
const results = await withNodeCloudDatabase((database) =>
|
|
7
|
+
createStorageReconciliationService(database).reconcile({ dryRun })
|
|
8
|
+
)
|
|
9
|
+
console.log(JSON.stringify({ dryRun, results }))
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { IncomingMessage, Server } from 'node:http'
|
|
2
|
+
import type { Duplex } from 'node:stream'
|
|
3
|
+
|
|
4
|
+
import { RELAY_PATH } from '#cloud/contract'
|
|
5
|
+
import type { CollaborationRelay } from '#cloud/server'
|
|
6
|
+
import { WebSocketServer, type RawData } from 'ws'
|
|
7
|
+
|
|
8
|
+
function bytes(data: RawData): Uint8Array {
|
|
9
|
+
if (Array.isArray(data)) return new Uint8Array(Buffer.concat(data))
|
|
10
|
+
return data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Accepts WebSocket upgrades on the relay path of an HTTP server and hands each socket to the
|
|
15
|
+
* relay. Other upgrade requests are refused.
|
|
16
|
+
*/
|
|
17
|
+
export function attachCollaborationRelay(
|
|
18
|
+
server: Pick<Server, 'on' | 'off'>,
|
|
19
|
+
relay: CollaborationRelay,
|
|
20
|
+
maximumMessageBytes: number
|
|
21
|
+
) {
|
|
22
|
+
// ws enforces the limit before buffering; the relay checks again for other runtimes.
|
|
23
|
+
const sockets = new WebSocketServer({ noServer: true, maxPayload: maximumMessageBytes })
|
|
24
|
+
|
|
25
|
+
function upgrade(request: IncomingMessage, socket: Duplex, head: Buffer) {
|
|
26
|
+
const path = new URL(request.url ?? '/', 'http://relay').pathname
|
|
27
|
+
if (path !== RELAY_PATH) {
|
|
28
|
+
socket.destroy()
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
sockets.handleUpgrade(request, socket, head, (webSocket) => {
|
|
32
|
+
webSocket.binaryType = 'nodebuffer'
|
|
33
|
+
const connection = relay.connect({
|
|
34
|
+
send: (data) => webSocket.send(data),
|
|
35
|
+
close: (code, reason) => webSocket.close(code, reason)
|
|
36
|
+
})
|
|
37
|
+
webSocket.on('message', (data) => connection.receive(bytes(data)))
|
|
38
|
+
webSocket.on('close', () => connection.closed())
|
|
39
|
+
webSocket.on('error', () => webSocket.terminate())
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
server.on('upgrade', upgrade)
|
|
44
|
+
return {
|
|
45
|
+
async close() {
|
|
46
|
+
server.off('upgrade', upgrade)
|
|
47
|
+
await relay.close()
|
|
48
|
+
for (const client of sockets.clients) client.terminate()
|
|
49
|
+
await new Promise<void>((resolve) => {
|
|
50
|
+
sockets.close(() => resolve())
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|