@facilis.dev/nitro 0.0.0-stage → 0.1.2
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 +21 -0
- package/README.md +18 -2
- package/dist/config.d.ts +30 -0
- package/dist/config.js +112 -0
- package/dist/constants.d.ts +24 -0
- package/dist/constants.js +24 -0
- package/dist/facade.d.ts +37 -0
- package/dist/facade.js +121 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +14 -0
- package/dist/middleware/context.d.ts +5 -0
- package/dist/middleware/context.js +86 -0
- package/dist/middleware/cors.d.ts +10 -0
- package/dist/middleware/cors.js +34 -0
- package/dist/middleware/session.d.ts +9 -0
- package/dist/middleware/session.js +18 -0
- package/dist/module.d.ts +7 -0
- package/dist/module.js +234 -0
- package/dist/rolldown-parser.d.ts +3 -0
- package/dist/rolldown-parser.js +66 -0
- package/dist/route-metadata.d.ts +22 -0
- package/dist/route-metadata.js +161 -0
- package/dist/routes/dev-query.post.d.ts +10 -0
- package/dist/routes/dev-query.post.js +49 -0
- package/dist/routes/dev-schema.get.d.ts +9 -0
- package/dist/routes/dev-schema.get.js +34 -0
- package/dist/routes/dev-view.post.d.ts +11 -0
- package/dist/routes/dev-view.post.js +61 -0
- package/dist/routes/health.get.d.ts +8 -0
- package/dist/routes/health.get.js +18 -0
- package/dist/routes/manifest-revision.get.d.ts +8 -0
- package/dist/routes/manifest-revision.get.js +26 -0
- package/dist/routes/manifest.get.d.ts +8 -0
- package/dist/routes/manifest.get.js +68 -0
- package/dist/routes/openapi.get.d.ts +8 -0
- package/dist/routes/openapi.get.js +29 -0
- package/dist/routes/sso.d.ts +2 -0
- package/dist/routes/sso.js +216 -0
- package/dist/routes/token.post.d.ts +10 -0
- package/dist/routes/token.post.js +75 -0
- package/dist/runtime/boot-runtimes.d.ts +21 -0
- package/dist/runtime/boot-runtimes.js +38 -0
- package/dist/runtime/bootstrap.d.ts +47 -0
- package/dist/runtime/bootstrap.js +214 -0
- package/dist/runtime/config.d.ts +27 -0
- package/dist/runtime/config.js +44 -0
- package/dist/runtime/cors-policy.d.ts +16 -0
- package/dist/runtime/cors-policy.js +88 -0
- package/dist/runtime/db-bootstrap.d.ts +9 -0
- package/dist/runtime/db-bootstrap.js +87 -0
- package/dist/runtime/db-target.d.ts +21 -0
- package/dist/runtime/db-target.js +52 -0
- package/dist/runtime/dev-db-persistence.d.ts +39 -0
- package/dist/runtime/dev-db-persistence.js +124 -0
- package/dist/runtime/dev-query.d.ts +38 -0
- package/dist/runtime/dev-query.js +87 -0
- package/dist/runtime/env.d.ts +9 -0
- package/dist/runtime/env.js +14 -0
- package/dist/runtime/http-guard.d.ts +16 -0
- package/dist/runtime/http-guard.js +42 -0
- package/dist/runtime/http.d.ts +11 -0
- package/dist/runtime/http.js +18 -0
- package/dist/runtime/jobs/boot.d.ts +20 -0
- package/dist/runtime/jobs/boot.js +77 -0
- package/dist/runtime/jobs/contracts.d.ts +161 -0
- package/dist/runtime/jobs/contracts.js +1 -0
- package/dist/runtime/jobs/db-store.d.ts +21 -0
- package/dist/runtime/jobs/db-store.js +210 -0
- package/dist/runtime/jobs/definitions.d.ts +14 -0
- package/dist/runtime/jobs/definitions.js +31 -0
- package/dist/runtime/jobs/engine-support.d.ts +22 -0
- package/dist/runtime/jobs/engine-support.js +32 -0
- package/dist/runtime/jobs/engine.d.ts +6 -0
- package/dist/runtime/jobs/engine.js +261 -0
- package/dist/runtime/jobs/executor.d.ts +11 -0
- package/dist/runtime/jobs/executor.js +27 -0
- package/dist/runtime/jobs/memory-store.d.ts +7 -0
- package/dist/runtime/jobs/memory-store.js +183 -0
- package/dist/runtime/jobs/row-mapping.d.ts +28 -0
- package/dist/runtime/jobs/row-mapping.js +61 -0
- package/dist/runtime/jobs/runtime.d.ts +24 -0
- package/dist/runtime/jobs/runtime.js +86 -0
- package/dist/runtime/logging/audit-chain.d.ts +54 -0
- package/dist/runtime/logging/audit-chain.js +102 -0
- package/dist/runtime/logging/bootstrap-logging.d.ts +28 -0
- package/dist/runtime/logging/bootstrap-logging.js +26 -0
- package/dist/runtime/logging/entry.d.ts +14 -0
- package/dist/runtime/logging/entry.js +85 -0
- package/dist/runtime/logging/file-sink.d.ts +24 -0
- package/dist/runtime/logging/file-sink.js +128 -0
- package/dist/runtime/logging/log-settings.d.ts +52 -0
- package/dist/runtime/logging/log-settings.js +93 -0
- package/dist/runtime/logging/log-sink.d.ts +18 -0
- package/dist/runtime/logging/log-sink.js +22 -0
- package/dist/runtime/logging/redact.d.ts +31 -0
- package/dist/runtime/logging/redact.js +106 -0
- package/dist/runtime/logging/runtime.d.ts +25 -0
- package/dist/runtime/logging/runtime.js +114 -0
- package/dist/runtime/logging/sudo-bridge.d.ts +13 -0
- package/dist/runtime/logging/sudo-bridge.js +38 -0
- package/dist/runtime/manifest-data.d.ts +28 -0
- package/dist/runtime/manifest-data.js +70 -0
- package/dist/runtime/manifest-response.d.ts +45 -0
- package/dist/runtime/manifest-response.js +50 -0
- package/dist/runtime/manifest-sections.d.ts +20 -0
- package/dist/runtime/manifest-sections.js +59 -0
- package/dist/runtime/manifest-service.d.ts +16 -0
- package/dist/runtime/manifest-service.js +66 -0
- package/dist/runtime/project-config.d.ts +8 -0
- package/dist/runtime/project-config.js +22 -0
- package/dist/runtime/project-files.d.ts +12 -0
- package/dist/runtime/project-files.js +99 -0
- package/dist/runtime/rate-limit.d.ts +6 -0
- package/dist/runtime/rate-limit.js +6 -0
- package/dist/runtime/revision-service.d.ts +15 -0
- package/dist/runtime/revision-service.js +26 -0
- package/dist/runtime/sql.d.ts +8 -0
- package/dist/runtime/sql.js +23 -0
- package/dist/runtime/sso.d.ts +40 -0
- package/dist/runtime/sso.js +98 -0
- package/dist/runtime/state.d.ts +7 -0
- package/dist/runtime/state.js +7 -0
- package/dist/runtime/storage-keys.d.ts +9 -0
- package/dist/runtime/storage-keys.js +82 -0
- package/dist/runtime/storage-target.d.ts +25 -0
- package/dist/runtime/storage-target.js +155 -0
- package/dist/runtime/system-role.d.ts +9 -0
- package/dist/runtime/system-role.js +21 -0
- package/dist/runtime/tasks/boot.d.ts +20 -0
- package/dist/runtime/tasks/boot.js +32 -0
- package/dist/runtime/tasks/contracts.d.ts +70 -0
- package/dist/runtime/tasks/contracts.js +1 -0
- package/dist/runtime/tasks/db-store.d.ts +18 -0
- package/dist/runtime/tasks/db-store.js +139 -0
- package/dist/runtime/tasks/row-mapping.d.ts +11 -0
- package/dist/runtime/tasks/row-mapping.js +69 -0
- package/dist/runtime/tasks/runtime.d.ts +7 -0
- package/dist/runtime/tasks/runtime.js +162 -0
- package/dist/runtime/tasks/tasks-test-harness.d.ts +16 -0
- package/dist/runtime/tasks/tasks-test-harness.js +70 -0
- package/dist/runtime/tenant.d.ts +10 -0
- package/dist/runtime/tenant.js +13 -0
- package/dist/runtime/workflows/boot.d.ts +25 -0
- package/dist/runtime/workflows/boot.js +75 -0
- package/dist/runtime/workflows/contracts.d.ts +96 -0
- package/dist/runtime/workflows/contracts.js +1 -0
- package/dist/runtime/workflows/db-store.d.ts +18 -0
- package/dist/runtime/workflows/db-store.js +128 -0
- package/dist/runtime/workflows/definitions.d.ts +14 -0
- package/dist/runtime/workflows/definitions.js +50 -0
- package/dist/runtime/workflows/dispatch.d.ts +4 -0
- package/dist/runtime/workflows/dispatch.js +119 -0
- package/dist/runtime/workflows/row-mapping.d.ts +5 -0
- package/dist/runtime/workflows/row-mapping.js +38 -0
- package/dist/runtime/workflows/runner.d.ts +15 -0
- package/dist/runtime/workflows/runner.js +89 -0
- package/dist/runtime/workflows/runtime.d.ts +14 -0
- package/dist/runtime/workflows/runtime.js +129 -0
- package/dist/runtime/workflows/steps.d.ts +14 -0
- package/dist/runtime/workflows/steps.js +49 -0
- package/dist/runtime/workflows/support.d.ts +20 -0
- package/dist/runtime/workflows/support.js +36 -0
- package/dist/runtime/workflows/transitions.d.ts +8 -0
- package/dist/runtime/workflows/transitions.js +184 -0
- package/dist/runtime/workflows/workflows-test-harness.d.ts +62 -0
- package/dist/runtime/workflows/workflows-test-harness.js +178 -0
- package/dist/session-cookie.d.ts +13 -0
- package/dist/session-cookie.js +48 -0
- package/dist/storage.d.ts +50 -0
- package/dist/storage.js +118 -0
- package/dist/types.d.ts +44 -0
- package/dist/types.js +1 -0
- package/dist/virtual-ids.d.ts +17 -0
- package/dist/virtual-ids.js +17 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.js +135 -0
- package/package.json +61 -4
- package/src/config.ts +120 -0
- package/src/constants.ts +24 -0
- package/src/facade.ts +153 -0
- package/src/index.ts +44 -0
- package/src/middleware/context.ts +106 -0
- package/src/middleware/cors.ts +42 -0
- package/src/middleware/session.ts +23 -0
- package/src/module.ts +278 -0
- package/src/rolldown-parser.ts +79 -0
- package/src/route-metadata-test-harness.ts +62 -0
- package/src/route-metadata.ts +194 -0
- package/src/routes/dev-query.post.ts +53 -0
- package/src/routes/dev-schema.get.ts +43 -0
- package/src/routes/dev-view.post.ts +66 -0
- package/src/routes/health.get.ts +26 -0
- package/src/routes/manifest-revision.get.ts +38 -0
- package/src/routes/manifest.get.ts +111 -0
- package/src/routes/openapi.get.ts +34 -0
- package/src/routes/sso.ts +247 -0
- package/src/routes/token.post.ts +89 -0
- package/src/runtime/boot-runtimes.ts +69 -0
- package/src/runtime/bootstrap.ts +300 -0
- package/src/runtime/config.ts +60 -0
- package/src/runtime/cors-policy.ts +107 -0
- package/src/runtime/db-bootstrap.ts +99 -0
- package/src/runtime/db-target.ts +87 -0
- package/src/runtime/dev-db-persistence.ts +146 -0
- package/src/runtime/dev-query.ts +124 -0
- package/src/runtime/env.ts +18 -0
- package/src/runtime/http-guard.ts +50 -0
- package/src/runtime/http.ts +30 -0
- package/src/runtime/jobs/boot.ts +97 -0
- package/src/runtime/jobs/contracts.ts +165 -0
- package/src/runtime/jobs/db-store-test-harness.ts +98 -0
- package/src/runtime/jobs/db-store.ts +273 -0
- package/src/runtime/jobs/definitions.ts +42 -0
- package/src/runtime/jobs/engine-support.ts +44 -0
- package/src/runtime/jobs/engine.ts +296 -0
- package/src/runtime/jobs/executor.ts +37 -0
- package/src/runtime/jobs/memory-store.ts +222 -0
- package/src/runtime/jobs/row-mapping.ts +67 -0
- package/src/runtime/jobs/runtime.ts +110 -0
- package/src/runtime/logging/audit-chain.ts +135 -0
- package/src/runtime/logging/bootstrap-logging.ts +62 -0
- package/src/runtime/logging/entry.ts +99 -0
- package/src/runtime/logging/file-sink.ts +157 -0
- package/src/runtime/logging/log-settings.ts +166 -0
- package/src/runtime/logging/log-sink.ts +31 -0
- package/src/runtime/logging/redact.ts +108 -0
- package/src/runtime/logging/runtime.ts +163 -0
- package/src/runtime/logging/sudo-bridge.ts +41 -0
- package/src/runtime/manifest-data.ts +103 -0
- package/src/runtime/manifest-response.ts +95 -0
- package/src/runtime/manifest-sections.ts +79 -0
- package/src/runtime/manifest-service.ts +83 -0
- package/src/runtime/project-config.ts +25 -0
- package/src/runtime/project-files.ts +103 -0
- package/src/runtime/rate-limit.ts +12 -0
- package/src/runtime/revision-service.ts +38 -0
- package/src/runtime/sql.ts +24 -0
- package/src/runtime/sso.ts +153 -0
- package/src/runtime/state.ts +7 -0
- package/src/runtime/storage-keys.ts +95 -0
- package/src/runtime/storage-target.ts +238 -0
- package/src/runtime/system-role.ts +26 -0
- package/src/runtime/tasks/boot.ts +49 -0
- package/src/runtime/tasks/contracts.ts +77 -0
- package/src/runtime/tasks/db-store.ts +191 -0
- package/src/runtime/tasks/row-mapping.ts +75 -0
- package/src/runtime/tasks/runtime.ts +192 -0
- package/src/runtime/tasks/tasks-test-harness.ts +92 -0
- package/src/runtime/tenant.ts +15 -0
- package/src/runtime/workflows/boot.ts +97 -0
- package/src/runtime/workflows/contracts.ts +114 -0
- package/src/runtime/workflows/db-store.ts +156 -0
- package/src/runtime/workflows/definitions.ts +75 -0
- package/src/runtime/workflows/dispatch.ts +148 -0
- package/src/runtime/workflows/row-mapping.ts +54 -0
- package/src/runtime/workflows/runner.ts +105 -0
- package/src/runtime/workflows/runtime.ts +161 -0
- package/src/runtime/workflows/steps.ts +74 -0
- package/src/runtime/workflows/support.ts +60 -0
- package/src/runtime/workflows/transitions.ts +259 -0
- package/src/runtime/workflows/workflows-test-harness.ts +238 -0
- package/src/session-cookie.ts +69 -0
- package/src/storage.ts +173 -0
- package/src/types.ts +51 -0
- package/src/virtual-ids.ts +18 -0
- package/src/virtual-modules.d.ts +53 -0
- package/src/vite.ts +175 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { createDb } from "@facilis.dev/db";
|
|
2
|
+
import { sessionMiddleware } from "@facilis.dev/auth";
|
|
3
|
+
import {} from "@facilis.dev/core";
|
|
4
|
+
import { projectRoot } from "#facilis/config";
|
|
5
|
+
import dbSchema from "#facilis/db-schema";
|
|
6
|
+
import dbViewModules from "#facilis/db-views";
|
|
7
|
+
import jobModules from "#facilis/jobs";
|
|
8
|
+
import workflowModules from "#facilis/workflows";
|
|
9
|
+
import { toAuthConfig } from "./config.js";
|
|
10
|
+
import { bootstrapDevelopmentDatabase, describe } from "./db-bootstrap.js";
|
|
11
|
+
import { resolveDbTarget } from "./db-target.js";
|
|
12
|
+
import { devDbSnapshotPath, installDevDbSnapshotter, readDevDbSnapshot, } from "./dev-db-persistence.js";
|
|
13
|
+
import { bootRuntimes } from "./boot-runtimes.js";
|
|
14
|
+
import { isProductionEnvironment } from "./env.js";
|
|
15
|
+
import { readProjectConfig } from "./project-config.js";
|
|
16
|
+
import { createBootstrapLogging } from "./logging/bootstrap-logging.js";
|
|
17
|
+
import { installSudoAuditBridge } from "./logging/sudo-bridge.js";
|
|
18
|
+
import { readDefaultTenantId } from "./tenant.js";
|
|
19
|
+
import { resolveStorage } from "./storage-target.js";
|
|
20
|
+
let bootstrapPromise;
|
|
21
|
+
/**
|
|
22
|
+
* Boot the framework runtime once per process (lazily, on the first request).
|
|
23
|
+
*
|
|
24
|
+
* Contract (docs/archive/PHASE1-CONTRACTS.md §8): never crash the request. Each failure is
|
|
25
|
+
* recorded as a manifest warning and the remaining steps still run, so a broken
|
|
26
|
+
* migration yields a 200 partial manifest instead of a 500.
|
|
27
|
+
*/
|
|
28
|
+
export function getBootstrap() {
|
|
29
|
+
bootstrapPromise ??= bootstrap();
|
|
30
|
+
return bootstrapPromise;
|
|
31
|
+
}
|
|
32
|
+
export function getSessionHandler(state) {
|
|
33
|
+
if (state.db === null || state.config === null)
|
|
34
|
+
return null;
|
|
35
|
+
state.sessionHandler ??= sessionMiddleware({
|
|
36
|
+
db: state.db,
|
|
37
|
+
config: toAuthConfig(state.config.auth),
|
|
38
|
+
tenantId: readDefaultTenantId(),
|
|
39
|
+
});
|
|
40
|
+
return state.sessionHandler;
|
|
41
|
+
}
|
|
42
|
+
async function bootstrap() {
|
|
43
|
+
const warnings = [];
|
|
44
|
+
const startedAt = new Date().toISOString();
|
|
45
|
+
const projectConfig = readProjectConfig(warnings);
|
|
46
|
+
// Operator logging is built BEFORE the database (D-119): a database boot failure must not
|
|
47
|
+
// take the access/error streams down with it, and logging never prevents a boot.
|
|
48
|
+
const loggingState = createBootstrapLogging({
|
|
49
|
+
loggingConfig: projectConfig?.logging ?? null,
|
|
50
|
+
env: process.env,
|
|
51
|
+
projectRoot,
|
|
52
|
+
});
|
|
53
|
+
warnings.push(...loggingState.warnings);
|
|
54
|
+
const loggingFields = {
|
|
55
|
+
logging: loggingState.logging,
|
|
56
|
+
loggingConfig: loggingState.config,
|
|
57
|
+
};
|
|
58
|
+
// The sudo trail is one of the audit stream's events, not a parallel protocol (D-099):
|
|
59
|
+
// install the bridge once per process, before the db can run an elevation.
|
|
60
|
+
installSudoAuditBridge(loggingState.logging);
|
|
61
|
+
if (projectConfig === null) {
|
|
62
|
+
return {
|
|
63
|
+
db: null,
|
|
64
|
+
config: null,
|
|
65
|
+
warnings,
|
|
66
|
+
startedAt,
|
|
67
|
+
jobs: null,
|
|
68
|
+
tasks: null,
|
|
69
|
+
workflows: null,
|
|
70
|
+
storage: null,
|
|
71
|
+
...loggingFields,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
// Binary storage is independent of the database: a refused storage configuration is a
|
|
75
|
+
// warning and a null API, so the rest of the app keeps working (D-053).
|
|
76
|
+
const resolvedStorage = resolveStorage(projectConfig, process.env, projectRoot);
|
|
77
|
+
warnings.push(...resolvedStorage.warnings);
|
|
78
|
+
const storage = resolvedStorage.storage;
|
|
79
|
+
// The environment, not the config alone, decides where data lives (D-068): a deployment
|
|
80
|
+
// sets a Postgres URL and the same build runs there. Every refusal is a warning, so a
|
|
81
|
+
// misconfigured production boot degrades to "no database" instead of crashing.
|
|
82
|
+
const resolved = resolveDbTarget(projectConfig, process.env);
|
|
83
|
+
warnings.push(...resolved.warnings);
|
|
84
|
+
if (resolved.target === null) {
|
|
85
|
+
return {
|
|
86
|
+
db: null,
|
|
87
|
+
config: projectConfig,
|
|
88
|
+
warnings,
|
|
89
|
+
startedAt,
|
|
90
|
+
jobs: null,
|
|
91
|
+
tasks: null,
|
|
92
|
+
workflows: null,
|
|
93
|
+
storage,
|
|
94
|
+
...loggingFields,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
const target = resolved.target;
|
|
98
|
+
const usesPglite = target.driver === "pglite";
|
|
99
|
+
// Dev-only persistence (D-052): an in-memory PGlite cluster is restored from one snapshot
|
|
100
|
+
// file in the project. A postgres target has no snapshot to read, and must not restore one.
|
|
101
|
+
const devPersistenceEnabled = usesPglite && !isProductionEnvironment();
|
|
102
|
+
const snapshotPath = devDbSnapshotPath(projectRoot);
|
|
103
|
+
let snapshot = null;
|
|
104
|
+
if (devPersistenceEnabled) {
|
|
105
|
+
try {
|
|
106
|
+
snapshot = await readDevDbSnapshot(snapshotPath);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
warnings.push(warning("db_snapshot_unreadable", `Could not read the dev database snapshot: ${describe(error)}`));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
let db;
|
|
113
|
+
try {
|
|
114
|
+
db = await createDb({
|
|
115
|
+
driver: target.driver,
|
|
116
|
+
url: target.url,
|
|
117
|
+
loadSnapshot: snapshot ?? undefined,
|
|
118
|
+
schema: dbSchema,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (snapshot === null) {
|
|
123
|
+
warnings.push(warning("db_unavailable", `Could not open the database: ${describe(error)}`));
|
|
124
|
+
return {
|
|
125
|
+
db: null,
|
|
126
|
+
config: projectConfig,
|
|
127
|
+
warnings,
|
|
128
|
+
startedAt,
|
|
129
|
+
jobs: null,
|
|
130
|
+
tasks: null,
|
|
131
|
+
workflows: null,
|
|
132
|
+
storage,
|
|
133
|
+
...loggingFields,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
// A corrupt or incompatible snapshot must not make the project unbootable: warn and
|
|
137
|
+
// start from an empty cluster instead (the old file is replaced by the next dump).
|
|
138
|
+
// Only pglite ever reads a snapshot (see devPersistenceEnabled), so the retry keeps the
|
|
139
|
+
// resolved driver instead of hard-coding one.
|
|
140
|
+
warnings.push(warning("db_snapshot_failed", `Could not restore the dev database snapshot, starting empty: ${describe(error)}`));
|
|
141
|
+
try {
|
|
142
|
+
db = await createDb({
|
|
143
|
+
driver: target.driver,
|
|
144
|
+
url: target.url,
|
|
145
|
+
schema: dbSchema,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
catch (retryError) {
|
|
149
|
+
warnings.push(warning("db_unavailable", `Could not open the database: ${describe(retryError)}`));
|
|
150
|
+
return {
|
|
151
|
+
db: null,
|
|
152
|
+
config: projectConfig,
|
|
153
|
+
warnings,
|
|
154
|
+
startedAt,
|
|
155
|
+
jobs: null,
|
|
156
|
+
tasks: null,
|
|
157
|
+
workflows: null,
|
|
158
|
+
storage,
|
|
159
|
+
...loggingFields,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// The dev bootstrap keeps a PGlite project self-contained: migrations -> seed -> app role
|
|
164
|
+
// (D-052). A real Postgres is provisioned out of band by the deploy step; the runtime may
|
|
165
|
+
// even connect as facilis_app, which can neither create roles nor run schema changes.
|
|
166
|
+
if (usesPglite) {
|
|
167
|
+
await bootstrapDevelopmentDatabase(db, projectConfig, warnings);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
warnings.push(warning("db_migrations_skipped", "Postgres target: migrations, roles and views are provisioned by the deploy step, not at boot."));
|
|
171
|
+
// Views are DDL like migrations: a restricted runtime role cannot create them, so the
|
|
172
|
+
// deploy step owns them and the boot only reports that it skipped the step.
|
|
173
|
+
if (dbViewModules.length > 0) {
|
|
174
|
+
warnings.push(warning("view_ddl_skipped", "Postgres target: SQL views are provisioned by the deploy step, not at boot."));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (devPersistenceEnabled) {
|
|
178
|
+
installDevDbSnapshotter({
|
|
179
|
+
db,
|
|
180
|
+
projectRoot,
|
|
181
|
+
snapshotPath,
|
|
182
|
+
onError: (error) => {
|
|
183
|
+
// One warning per process is enough: a full disk would otherwise flood the manifest.
|
|
184
|
+
if (!warnings.some((entry) => entry.code === "db_snapshot_write_failed")) {
|
|
185
|
+
warnings.push(warning("db_snapshot_write_failed", `Could not write the dev database snapshot: ${describe(error)}`));
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
// The queue, manual tasks and workflows all run as facilis_app after the roles/views exist
|
|
191
|
+
// (D-107, D-135); a failure in any of them is a warning and a null service, not a 500.
|
|
192
|
+
const { jobs, tasks, workflows } = await bootRuntimes({
|
|
193
|
+
db,
|
|
194
|
+
tenantId: readDefaultTenantId(),
|
|
195
|
+
projectConfig,
|
|
196
|
+
jobModules,
|
|
197
|
+
workflowModules,
|
|
198
|
+
warnings,
|
|
199
|
+
});
|
|
200
|
+
return {
|
|
201
|
+
db,
|
|
202
|
+
config: projectConfig,
|
|
203
|
+
warnings,
|
|
204
|
+
startedAt,
|
|
205
|
+
jobs,
|
|
206
|
+
tasks,
|
|
207
|
+
workflows,
|
|
208
|
+
storage,
|
|
209
|
+
...loggingFields,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
function warning(code, message) {
|
|
213
|
+
return { code, message };
|
|
214
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { AuthConfig } from "@facilis.dev/auth";
|
|
2
|
+
import type { FacilisConfig } from "@facilis.dev/core";
|
|
3
|
+
export declare const DEFAULT_SESSION_DURATION_MS: number;
|
|
4
|
+
export declare const DEFAULT_MIGRATIONS_DIR = "db/migrations";
|
|
5
|
+
export declare const DEFAULT_LOCALE = "en";
|
|
6
|
+
/** Subset of FacilisConfig.auth that may be missing when the virtual module fell back. */
|
|
7
|
+
export interface AuthConfigLike {
|
|
8
|
+
permissions?: readonly string[];
|
|
9
|
+
roles?: Readonly<Record<string, readonly string[]>>;
|
|
10
|
+
defaultRole?: string;
|
|
11
|
+
sessionDurationMs?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Normalise the project auth config into @facilis.dev/auth's AuthConfig. The config is
|
|
15
|
+
* validated by @facilis.dev/core at definition time; defaults here only cover the
|
|
16
|
+
* "config module missing/corrupt" bootstrap path so it degrades to a warning.
|
|
17
|
+
*/
|
|
18
|
+
export declare function toAuthConfig(auth: AuthConfigLike | undefined): AuthConfig;
|
|
19
|
+
export declare function readMigrationsDir(config: FacilisConfig | null | undefined): string;
|
|
20
|
+
export declare function readDefaultLocale(config: FacilisConfig | null | undefined): string;
|
|
21
|
+
/**
|
|
22
|
+
* Runtime guard for the virtual `#facilis/config` default export.
|
|
23
|
+
*
|
|
24
|
+
* The virtual module falls back to `{}` when the project has no config file, so the
|
|
25
|
+
* bootstrap must not trust the static type at the module boundary.
|
|
26
|
+
*/
|
|
27
|
+
export declare function isFacilisConfig(value: unknown): value is FacilisConfig;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export const DEFAULT_SESSION_DURATION_MS = 7 * 24 * 60 * 60 * 1000;
|
|
2
|
+
export const DEFAULT_MIGRATIONS_DIR = "db/migrations";
|
|
3
|
+
export const DEFAULT_LOCALE = "en";
|
|
4
|
+
/**
|
|
5
|
+
* Normalise the project auth config into @facilis.dev/auth's AuthConfig. The config is
|
|
6
|
+
* validated by @facilis.dev/core at definition time; defaults here only cover the
|
|
7
|
+
* "config module missing/corrupt" bootstrap path so it degrades to a warning.
|
|
8
|
+
*/
|
|
9
|
+
export function toAuthConfig(auth) {
|
|
10
|
+
return {
|
|
11
|
+
permissions: auth?.permissions ?? [],
|
|
12
|
+
roles: auth?.roles ?? {},
|
|
13
|
+
defaultRole: auth?.defaultRole ?? "user",
|
|
14
|
+
sessionDurationMs: auth?.sessionDurationMs ?? DEFAULT_SESSION_DURATION_MS,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export function readMigrationsDir(config) {
|
|
18
|
+
return config?.db?.migrationsDir ?? DEFAULT_MIGRATIONS_DIR;
|
|
19
|
+
}
|
|
20
|
+
export function readDefaultLocale(config) {
|
|
21
|
+
return config?.i18n?.defaultLocale ?? DEFAULT_LOCALE;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Runtime guard for the virtual `#facilis/config` default export.
|
|
25
|
+
*
|
|
26
|
+
* The virtual module falls back to `{}` when the project has no config file, so the
|
|
27
|
+
* bootstrap must not trust the static type at the module boundary.
|
|
28
|
+
*/
|
|
29
|
+
export function isFacilisConfig(value) {
|
|
30
|
+
if (typeof value !== "object" || value === null)
|
|
31
|
+
return false;
|
|
32
|
+
const candidate = value;
|
|
33
|
+
const { auth, db, i18n } = candidate;
|
|
34
|
+
return (typeof auth === "object" &&
|
|
35
|
+
auth !== null &&
|
|
36
|
+
Array.isArray(auth.permissions) &&
|
|
37
|
+
typeof auth.defaultRole === "string" &&
|
|
38
|
+
typeof auth.roles === "object" &&
|
|
39
|
+
auth.roles !== null &&
|
|
40
|
+
typeof db === "object" &&
|
|
41
|
+
db !== null &&
|
|
42
|
+
typeof i18n === "object" &&
|
|
43
|
+
i18n !== null);
|
|
44
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RouteMetadata, RouteMetadataMap } from "../types.js";
|
|
2
|
+
/** The route whose OpenAPI path template matches the request path and method, if any. */
|
|
3
|
+
export declare function matchRoute(metadata: RouteMetadataMap, pathname: string, method: string): RouteMetadata | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* Headers for a non-preflight response. A crossSite route answers every origin with "*"
|
|
6
|
+
* and no credentials; a trusted origin gets an exact echo plus credentials. Nothing else
|
|
7
|
+
* may be exposed, so an untrusted origin receives no CORS header at all.
|
|
8
|
+
*/
|
|
9
|
+
export declare function corsHeadersFor(origin: string | null, trusted: boolean, route: RouteMetadata | undefined): Headers;
|
|
10
|
+
/**
|
|
11
|
+
* Preflight answer, or null when the request is not a CORS preflight the policy allows.
|
|
12
|
+
*
|
|
13
|
+
* The caller gates on the OPTIONS method; this function only needs the headers, because a
|
|
14
|
+
* missing access-control-request-method is what distinguishes a plain OPTIONS request.
|
|
15
|
+
*/
|
|
16
|
+
export declare function preflightResponse(metadata: RouteMetadataMap, pathname: string, requestHeaders: Headers, trustedOrigins: readonly string[]): Response | null;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CORS policy for the runtime middleware (SPEC §8).
|
|
3
|
+
*
|
|
4
|
+
* Pure on purpose: it owns the OpenAPI-path matcher and the header/preflight decisions, so
|
|
5
|
+
* the middleware is only wiring and the rules stay unit-testable without virtual modules.
|
|
6
|
+
*/
|
|
7
|
+
/** Compiled path templates, cached: every request would otherwise recompile the route table. */
|
|
8
|
+
const PATH_PATTERNS = new Map();
|
|
9
|
+
const PARAM_SEGMENT = /^\{[^}]+\}$/;
|
|
10
|
+
const CATCH_ALL_SEGMENT = /^\{\*[^}]+\}$/;
|
|
11
|
+
const ALLOW_METHODS = "GET,POST,PUT,PATCH,DELETE,OPTIONS";
|
|
12
|
+
// A browser preflight for a Bearer token asks for authorization: without it in the default
|
|
13
|
+
// allow-list a cross-site token request (v3a) would be blocked before reaching the route.
|
|
14
|
+
const DEFAULT_ALLOW_HEADERS = "content-type,authorization";
|
|
15
|
+
const PREFLIGHT_MAX_AGE = "600";
|
|
16
|
+
function escapeSegment(segment) {
|
|
17
|
+
return segment.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
18
|
+
}
|
|
19
|
+
/** A {name} segment matches one segment, a {*rest} segment matches the remainder. */
|
|
20
|
+
function compilePattern(template) {
|
|
21
|
+
const cached = PATH_PATTERNS.get(template);
|
|
22
|
+
if (cached !== undefined)
|
|
23
|
+
return cached;
|
|
24
|
+
const pattern = template
|
|
25
|
+
.split("/")
|
|
26
|
+
.map((segment) => {
|
|
27
|
+
if (CATCH_ALL_SEGMENT.test(segment))
|
|
28
|
+
return ".*";
|
|
29
|
+
if (PARAM_SEGMENT.test(segment))
|
|
30
|
+
return "[^/]+";
|
|
31
|
+
return escapeSegment(segment);
|
|
32
|
+
})
|
|
33
|
+
.join("/");
|
|
34
|
+
const compiled = new RegExp("^" + pattern + "$");
|
|
35
|
+
PATH_PATTERNS.set(template, compiled);
|
|
36
|
+
return compiled;
|
|
37
|
+
}
|
|
38
|
+
/** The route whose OpenAPI path template matches the request path and method, if any. */
|
|
39
|
+
export function matchRoute(metadata, pathname, method) {
|
|
40
|
+
const normalizedMethod = method.toLowerCase();
|
|
41
|
+
for (const [template, operations] of Object.entries(metadata)) {
|
|
42
|
+
if (!compilePattern(template).test(pathname))
|
|
43
|
+
continue;
|
|
44
|
+
const route = operations[normalizedMethod];
|
|
45
|
+
if (route !== undefined)
|
|
46
|
+
return route;
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Headers for a non-preflight response. A crossSite route answers every origin with "*"
|
|
52
|
+
* and no credentials; a trusted origin gets an exact echo plus credentials. Nothing else
|
|
53
|
+
* may be exposed, so an untrusted origin receives no CORS header at all.
|
|
54
|
+
*/
|
|
55
|
+
export function corsHeadersFor(origin, trusted, route) {
|
|
56
|
+
const headers = new Headers();
|
|
57
|
+
if (route?.crossSite === true) {
|
|
58
|
+
headers.set("access-control-allow-origin", "*");
|
|
59
|
+
return headers;
|
|
60
|
+
}
|
|
61
|
+
if (!trusted || origin === null)
|
|
62
|
+
return headers;
|
|
63
|
+
headers.set("access-control-allow-origin", origin);
|
|
64
|
+
headers.set("access-control-allow-credentials", "true");
|
|
65
|
+
headers.set("vary", "origin");
|
|
66
|
+
return headers;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Preflight answer, or null when the request is not a CORS preflight the policy allows.
|
|
70
|
+
*
|
|
71
|
+
* The caller gates on the OPTIONS method; this function only needs the headers, because a
|
|
72
|
+
* missing access-control-request-method is what distinguishes a plain OPTIONS request.
|
|
73
|
+
*/
|
|
74
|
+
export function preflightResponse(metadata, pathname, requestHeaders, trustedOrigins) {
|
|
75
|
+
const requestedMethod = requestHeaders.get("access-control-request-method");
|
|
76
|
+
if (requestedMethod === null)
|
|
77
|
+
return null;
|
|
78
|
+
const origin = requestHeaders.get("origin");
|
|
79
|
+
const trusted = origin !== null && trustedOrigins.includes(origin);
|
|
80
|
+
const route = matchRoute(metadata, pathname, requestedMethod);
|
|
81
|
+
const headers = corsHeadersFor(origin, trusted, route);
|
|
82
|
+
if (!headers.has("access-control-allow-origin"))
|
|
83
|
+
return null;
|
|
84
|
+
headers.set("access-control-allow-methods", ALLOW_METHODS);
|
|
85
|
+
headers.set("access-control-allow-headers", requestHeaders.get("access-control-request-headers") ?? DEFAULT_ALLOW_HEADERS);
|
|
86
|
+
headers.set("access-control-max-age", PREFLIGHT_MAX_AGE);
|
|
87
|
+
return new Response(null, { status: 204, headers });
|
|
88
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type FacilisDb } from "@facilis.dev/db";
|
|
2
|
+
import type { FacilisConfig, ManifestWarning } from "@facilis.dev/core";
|
|
3
|
+
/**
|
|
4
|
+
* Dev-only database bootstrap: migrations -> seed -> app role -> system role -> views
|
|
5
|
+
* (D-052/D-097/D-099). Split from `bootstrap.ts` to keep both modules under the file-length
|
|
6
|
+
* limit; a Postgres target skips this module entirely, because the deploy step owns DDL.
|
|
7
|
+
*/
|
|
8
|
+
export declare function bootstrapDevelopmentDatabase(db: FacilisDb, projectConfig: FacilisConfig, warnings: ManifestWarning[]): Promise<void>;
|
|
9
|
+
export declare function describe(error: unknown): string;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { readdir } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { applyStatements, applyViews, ensureAppRole } from "@facilis.dev/db";
|
|
4
|
+
import { seedStatements } from "@facilis.dev/auth";
|
|
5
|
+
import { projectRoot } from "#facilis/config";
|
|
6
|
+
import dbViewModules from "#facilis/db-views";
|
|
7
|
+
import { readMigrationsDir, toAuthConfig } from "./config.js";
|
|
8
|
+
import { readTextFile } from "./project-files.js";
|
|
9
|
+
import { splitSqlStatements } from "./sql.js";
|
|
10
|
+
import { provisionSystemRole } from "./system-role.js";
|
|
11
|
+
/**
|
|
12
|
+
* Dev-only database bootstrap: migrations -> seed -> app role -> system role -> views
|
|
13
|
+
* (D-052/D-097/D-099). Split from `bootstrap.ts` to keep both modules under the file-length
|
|
14
|
+
* limit; a Postgres target skips this module entirely, because the deploy step owns DDL.
|
|
15
|
+
*/
|
|
16
|
+
export async function bootstrapDevelopmentDatabase(db, projectConfig, warnings) {
|
|
17
|
+
// Order mandated by the Lead: migrations -> seed (roles/role_permissions) -> app role.
|
|
18
|
+
try {
|
|
19
|
+
const statements = await readMigrationStatements(projectConfig);
|
|
20
|
+
if (statements.length > 0)
|
|
21
|
+
await applyStatements(db, statements);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
warnings.push(warning("migration_failed", `Migrations failed: ${describe(error)}`));
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
await applyStatements(db, seedStatements(toAuthConfig(projectConfig.auth)));
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
warnings.push(warning("seed_failed", `Seeding roles/permissions failed: ${describe(error)}`));
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
await ensureAppRole(db);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
warnings.push(warning("app_role_failed", `Could not provision the app role: ${describe(error)}`));
|
|
37
|
+
}
|
|
38
|
+
// D-099: the system role is the sudo identity; provision it after the app role so the
|
|
39
|
+
// membership grant can reference it.
|
|
40
|
+
await provisionSystemRole(db, warnings);
|
|
41
|
+
// Views are code (D-097): create/replace them after the app role exists, because the
|
|
42
|
+
// security_invoker view must be granted to that role. A broken view is a warning, never a
|
|
43
|
+
// failed boot, and the endpoint returns the same failures on demand.
|
|
44
|
+
if (dbViewModules.length === 0)
|
|
45
|
+
return;
|
|
46
|
+
try {
|
|
47
|
+
const applied = await applyViews(db, dbViewModules, {
|
|
48
|
+
permissions: projectConfig.auth.permissions,
|
|
49
|
+
});
|
|
50
|
+
for (const failure of applied.failed) {
|
|
51
|
+
warnings.push(warning("view_failed", `View "${failure.name}" could not be created: ${failure.error}`));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
warnings.push(warning("view_failed", `Views could not be applied: ${describe(error)}`));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async function readMigrationStatements(projectConfig) {
|
|
59
|
+
const dir = join(projectRoot, readMigrationsDir(projectConfig));
|
|
60
|
+
let entries;
|
|
61
|
+
try {
|
|
62
|
+
entries = await readdir(dir);
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
// No migrations directory is a valid early-project state.
|
|
66
|
+
if (isMissingFileError(error))
|
|
67
|
+
return [];
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
const names = entries.filter((name) => name.endsWith(".sql")).toSorted();
|
|
71
|
+
const contents = await Promise.all(names.map((name) => readTextFile(join(dir, name))));
|
|
72
|
+
const statements = [];
|
|
73
|
+
for (const source of contents) {
|
|
74
|
+
if (source !== null)
|
|
75
|
+
statements.push(...splitSqlStatements(source));
|
|
76
|
+
}
|
|
77
|
+
return statements;
|
|
78
|
+
}
|
|
79
|
+
function warning(code, message) {
|
|
80
|
+
return { code, message };
|
|
81
|
+
}
|
|
82
|
+
function isMissingFileError(error) {
|
|
83
|
+
return error?.code === "ENOENT";
|
|
84
|
+
}
|
|
85
|
+
export function describe(error) {
|
|
86
|
+
return error instanceof Error ? error.message : String(error);
|
|
87
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { FacilisConfig, ManifestWarning } from "@facilis.dev/core";
|
|
2
|
+
/** Driver a booting project can open; mirrors @facilis.dev/db's `DbDriver`. */
|
|
3
|
+
export type DbTargetDriver = "pglite" | "postgres";
|
|
4
|
+
/** The database the runtime must open. `url` is set for a postgres target only. */
|
|
5
|
+
export interface DbTarget {
|
|
6
|
+
readonly driver: DbTargetDriver;
|
|
7
|
+
readonly url?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ResolvedDbTarget {
|
|
10
|
+
readonly target: DbTarget | null;
|
|
11
|
+
readonly warnings: readonly ManifestWarning[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Decide which database the runtime opens (SPEC §5.2/§7.1, D-068).
|
|
15
|
+
*
|
|
16
|
+
* A connection string wins over the project config so a deployment can point any project at
|
|
17
|
+
* its Postgres without editing source. Every refusal returns a null target plus a warning:
|
|
18
|
+
* the boot then degrades to "no database" (manifest warning, no session) instead of crashing
|
|
19
|
+
* or, worse, silently writing a production app to the in-memory PGlite cluster.
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveDbTarget(config: FacilisConfig | null, env: NodeJS.ProcessEnv): ResolvedDbTarget;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { isProductionEnvironment } from "./env.js";
|
|
2
|
+
/**
|
|
3
|
+
* Decide which database the runtime opens (SPEC §5.2/§7.1, D-068).
|
|
4
|
+
*
|
|
5
|
+
* A connection string wins over the project config so a deployment can point any project at
|
|
6
|
+
* its Postgres without editing source. Every refusal returns a null target plus a warning:
|
|
7
|
+
* the boot then degrades to "no database" (manifest warning, no session) instead of crashing
|
|
8
|
+
* or, worse, silently writing a production app to the in-memory PGlite cluster.
|
|
9
|
+
*/
|
|
10
|
+
export function resolveDbTarget(config, env) {
|
|
11
|
+
// A hosting panel that defines the variable but leaves it blank is "not configured": an
|
|
12
|
+
// empty URL would otherwise reach the driver and surface as a confusing connection error.
|
|
13
|
+
const url = readUrl(env["FACILIS_DATABASE_URL"]) ?? readUrl(env["DATABASE_URL"]);
|
|
14
|
+
if (url !== undefined) {
|
|
15
|
+
return { target: { driver: "postgres", url }, warnings: [] };
|
|
16
|
+
}
|
|
17
|
+
const mode = config?.db.mode ?? "pglite";
|
|
18
|
+
if (mode === "pglite") {
|
|
19
|
+
if (isProductionEnvironment(env)) {
|
|
20
|
+
return {
|
|
21
|
+
target: null,
|
|
22
|
+
warnings: [
|
|
23
|
+
warning("db_pglite_in_production", "Production refuses the in-memory PGlite driver: set FACILIS_DATABASE_URL (or DATABASE_URL) to a Postgres connection string."),
|
|
24
|
+
],
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
return { target: { driver: "pglite" }, warnings: [] };
|
|
28
|
+
}
|
|
29
|
+
if (mode === "postgres") {
|
|
30
|
+
return {
|
|
31
|
+
target: null,
|
|
32
|
+
warnings: [
|
|
33
|
+
warning("db_url_missing", 'db.mode is "postgres" but neither FACILIS_DATABASE_URL nor DATABASE_URL is set.'),
|
|
34
|
+
],
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
// "json" is a declared config mode (SPEC §7.1) with no runtime driver yet.
|
|
38
|
+
return {
|
|
39
|
+
target: null,
|
|
40
|
+
warnings: [
|
|
41
|
+
warning("db_mode_unsupported", `db.mode "${mode}" has no runtime driver; use "pglite" or "postgres".`),
|
|
42
|
+
],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** Trimmed connection string, or undefined when unset or blank. */
|
|
46
|
+
function readUrl(value) {
|
|
47
|
+
const trimmed = value?.trim();
|
|
48
|
+
return trimmed === undefined || trimmed.length === 0 ? undefined : trimmed;
|
|
49
|
+
}
|
|
50
|
+
function warning(code, message) {
|
|
51
|
+
return { code, message };
|
|
52
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { FacilisDb } from "@facilis.dev/db";
|
|
2
|
+
/** Per-project, non-source state directory (dev database, binary blobs). */
|
|
3
|
+
export declare const FACILIS_STATE_DIR = ".facilis";
|
|
4
|
+
/** The dev database snapshot, relative to the project root. */
|
|
5
|
+
export declare const DEV_DB_SNAPSHOT_FILE: string;
|
|
6
|
+
/** What the snapshotter needs from the database handle. */
|
|
7
|
+
export type SnapshotSource = Pick<FacilisDb, "activity" | "snapshot">;
|
|
8
|
+
export interface DevDbSnapshotter {
|
|
9
|
+
/** Write a snapshot now if the cluster changed since the last one. */
|
|
10
|
+
flush(): Promise<void>;
|
|
11
|
+
stop(): void;
|
|
12
|
+
/** Epoch ms of the last successful snapshot, or null before the first one. */
|
|
13
|
+
lastSnapshotAt(): number | null;
|
|
14
|
+
}
|
|
15
|
+
export declare function devDbSnapshotPath(projectRoot: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Create `.facilis` and its self-ignoring `.gitignore`.
|
|
18
|
+
*
|
|
19
|
+
* The folder must persist in the VFS (that is how it survives a reload), but it is not
|
|
20
|
+
* source: a `.gitignore` containing `*` ignores the whole tree even when the project's
|
|
21
|
+
* own .gitignore was written before this directory existed. Written idempotently, and
|
|
22
|
+
* never overwriting a file the user may have edited.
|
|
23
|
+
*/
|
|
24
|
+
export declare function ensureFacilisStateDir(projectRoot: string): Promise<void>;
|
|
25
|
+
/** Read the dev snapshot; null when the project has none yet. */
|
|
26
|
+
export declare function readDevDbSnapshot(path: string): Promise<Uint8Array | null>;
|
|
27
|
+
export interface DevDbSnapshotterOptions {
|
|
28
|
+
readonly db: SnapshotSource;
|
|
29
|
+
readonly projectRoot: string;
|
|
30
|
+
readonly snapshotPath?: string;
|
|
31
|
+
readonly intervalMs?: number;
|
|
32
|
+
/** Called with a failed dump/write; the previous snapshot stays valid. */
|
|
33
|
+
readonly onError?: (error: unknown) => void;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Start snapshotting a dev database. Only a changed cluster is dumped (`activity()`),
|
|
37
|
+
* so an idle project never pays for a dump, and a failed write keeps the last good file.
|
|
38
|
+
*/
|
|
39
|
+
export declare function installDevDbSnapshotter(options: DevDbSnapshotterOptions): DevDbSnapshotter;
|