@facilis.dev/nitro 0.0.0-stage → 0.1.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 +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,114 @@
|
|
|
1
|
+
import { logLevelEnabled, } from "@facilis.dev/core";
|
|
2
|
+
import { sanitizeEntry } from "./entry.js";
|
|
3
|
+
import { createFileLogSink } from "./file-sink.js";
|
|
4
|
+
import { createStdoutLogSink } from "./log-sink.js";
|
|
5
|
+
export function createLoggingRuntime(options) {
|
|
6
|
+
const warnings = [];
|
|
7
|
+
let file = null;
|
|
8
|
+
if (options.settings.sinks.file) {
|
|
9
|
+
const setup = createFileLogSink({
|
|
10
|
+
dir: options.settings.dir,
|
|
11
|
+
maxBytes: options.settings.maxBytes,
|
|
12
|
+
keep: options.settings.keep,
|
|
13
|
+
});
|
|
14
|
+
warnings.push(...setup.warnings);
|
|
15
|
+
file = setup.sink;
|
|
16
|
+
}
|
|
17
|
+
const stdout = options.settings.sinks.stdout
|
|
18
|
+
? createStdoutLogSink(options.writeStdout)
|
|
19
|
+
: null;
|
|
20
|
+
const now = options.now ?? (() => new Date());
|
|
21
|
+
let reportedWriteFailure = false;
|
|
22
|
+
const reportWriteFailure = (error) => {
|
|
23
|
+
if (reportedWriteFailure)
|
|
24
|
+
return;
|
|
25
|
+
reportedWriteFailure = true;
|
|
26
|
+
// One line per process: a broken sink would otherwise flood the terminal with the very
|
|
27
|
+
// entries it cannot store. Audit does not come through here: it fails the request instead.
|
|
28
|
+
// oxlint-disable-next-line no-console -- operator fallback for the best-effort streams.
|
|
29
|
+
console.error("facilis: log sink is not writable; dropping access/error/app entries", error);
|
|
30
|
+
};
|
|
31
|
+
const write = (stream, entry) => {
|
|
32
|
+
try {
|
|
33
|
+
file?.append(stream, entry);
|
|
34
|
+
stdout?.append(stream, entry);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (stream === "audit")
|
|
38
|
+
throw error;
|
|
39
|
+
reportWriteFailure(error);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
const accepts = (entry) => {
|
|
43
|
+
switch (entry.kind) {
|
|
44
|
+
case "audit":
|
|
45
|
+
// Never filtered: an audit trail with holes is not an audit trail.
|
|
46
|
+
return true;
|
|
47
|
+
case "access":
|
|
48
|
+
return options.config.access && logLevelEnabled(options.config.level, entry.level);
|
|
49
|
+
case "error":
|
|
50
|
+
return options.config.errors;
|
|
51
|
+
case "app":
|
|
52
|
+
return logLevelEnabled(options.config.level, entry.level);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const record = (entry) => {
|
|
56
|
+
if (!accepts(entry))
|
|
57
|
+
return;
|
|
58
|
+
write(entry.kind, sanitizeEntry(entry));
|
|
59
|
+
};
|
|
60
|
+
const logger = (meta) => {
|
|
61
|
+
const identity = buildIdentity(meta);
|
|
62
|
+
const app = (level) => (message, data) => {
|
|
63
|
+
const entry = {
|
|
64
|
+
kind: "app",
|
|
65
|
+
ts: now().toISOString(),
|
|
66
|
+
level,
|
|
67
|
+
requestId: meta.requestId,
|
|
68
|
+
message,
|
|
69
|
+
method: meta.method,
|
|
70
|
+
path: meta.path,
|
|
71
|
+
...(data === undefined ? {} : { data }),
|
|
72
|
+
...identity,
|
|
73
|
+
};
|
|
74
|
+
record(entry);
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
debug: app("debug"),
|
|
78
|
+
info: app("info"),
|
|
79
|
+
warn: app("warn"),
|
|
80
|
+
error: app("error"),
|
|
81
|
+
audit: (input) => {
|
|
82
|
+
// The actor is derived, never caller-supplied: "anonymous" is the session-less case
|
|
83
|
+
// (a public route or a boot-time event), not a claim about identity.
|
|
84
|
+
const actor = meta.userId === undefined ? "anonymous" : `user:${meta.userId}`;
|
|
85
|
+
const entry = {
|
|
86
|
+
kind: "audit",
|
|
87
|
+
ts: now().toISOString(),
|
|
88
|
+
level: "info",
|
|
89
|
+
event: input.operation,
|
|
90
|
+
actor,
|
|
91
|
+
requestId: meta.requestId,
|
|
92
|
+
method: meta.method,
|
|
93
|
+
path: meta.path,
|
|
94
|
+
...(input.outcome === undefined ? {} : { outcome: input.outcome }),
|
|
95
|
+
...(input.errorCode === undefined ? {} : { errorCode: input.errorCode }),
|
|
96
|
+
...(input.reason === undefined ? {} : { reason: input.reason }),
|
|
97
|
+
...(input.tables === undefined ? {} : { tables: input.tables }),
|
|
98
|
+
...(input.details === undefined ? {} : { details: input.details }),
|
|
99
|
+
...identity,
|
|
100
|
+
};
|
|
101
|
+
record(entry);
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
return { record, logger, config: options.config, settings: options.settings, warnings };
|
|
106
|
+
}
|
|
107
|
+
function buildIdentity(meta) {
|
|
108
|
+
return {
|
|
109
|
+
...(meta.userId === undefined ? {} : { userId: meta.userId }),
|
|
110
|
+
...(meta.sessionHash === undefined ? {} : { sessionHash: meta.sessionHash }),
|
|
111
|
+
...(meta.tenantId === undefined ? {} : { tenantId: meta.tenantId }),
|
|
112
|
+
...(meta.ip === undefined ? {} : { ip: meta.ip }),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type SudoAuditEvent } from "@facilis.dev/db";
|
|
2
|
+
import type { AuditLogEntry, FacilisLoggingApi } from "@facilis.dev/core";
|
|
3
|
+
/**
|
|
4
|
+
* Bridge the db package's sudo trail into the audit stream (D-099, D-119).
|
|
5
|
+
*
|
|
6
|
+
* The sudo event is the framework's own security record; it becomes one more `audit` entry
|
|
7
|
+
* instead of a parallel `facilis:sudo` stdout protocol. Fail-closed is preserved: the bridge
|
|
8
|
+
* calls `record` synchronously, and a throwing audit sink propagates out of `writeSudoAudit`
|
|
9
|
+
* so sudo refuses to escalate untraced.
|
|
10
|
+
*/
|
|
11
|
+
export declare function toSudoAuditEntry(event: SudoAuditEvent): AuditLogEntry;
|
|
12
|
+
/** Replaces the process-wide sudo sink once, at boot, with the audit stream. */
|
|
13
|
+
export declare function installSudoAuditBridge(logging: FacilisLoggingApi): void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { setSudoAuditSink } from "@facilis.dev/db";
|
|
2
|
+
/**
|
|
3
|
+
* Bridge the db package's sudo trail into the audit stream (D-099, D-119).
|
|
4
|
+
*
|
|
5
|
+
* The sudo event is the framework's own security record; it becomes one more `audit` entry
|
|
6
|
+
* instead of a parallel `facilis:sudo` stdout protocol. Fail-closed is preserved: the bridge
|
|
7
|
+
* calls `record` synchronously, and a throwing audit sink propagates out of `writeSudoAudit`
|
|
8
|
+
* so sudo refuses to escalate untraced.
|
|
9
|
+
*/
|
|
10
|
+
export function toSudoAuditEntry(event) {
|
|
11
|
+
return {
|
|
12
|
+
kind: "audit",
|
|
13
|
+
ts: event.at,
|
|
14
|
+
level: "info",
|
|
15
|
+
event: `sudo:${event.event}`,
|
|
16
|
+
actor: event.actor,
|
|
17
|
+
operation: event.operation,
|
|
18
|
+
reason: event.reason,
|
|
19
|
+
tables: event.tables,
|
|
20
|
+
...(event.observedTables === undefined ? {} : { observedTables: event.observedTables }),
|
|
21
|
+
roles: event.roles,
|
|
22
|
+
...(event.actorUserId === undefined ? {} : { userId: event.actorUserId }),
|
|
23
|
+
...(event.sessionHash === undefined ? {} : { sessionHash: event.sessionHash }),
|
|
24
|
+
...(event.tenantId === undefined ? {} : { tenantId: event.tenantId }),
|
|
25
|
+
...(event.requestId === undefined ? {} : { requestId: event.requestId }),
|
|
26
|
+
...(event.method === undefined ? {} : { method: event.method }),
|
|
27
|
+
...(event.route === undefined ? {} : { path: event.route }),
|
|
28
|
+
...(event.outcome === undefined ? {} : { outcome: event.outcome }),
|
|
29
|
+
...(event.errorCode === undefined ? {} : { errorCode: event.errorCode }),
|
|
30
|
+
...(event.durationMs === undefined ? {} : { durationMs: event.durationMs }),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Replaces the process-wide sudo sink once, at boot, with the audit stream. */
|
|
34
|
+
export function installSudoAuditBridge(logging) {
|
|
35
|
+
setSudoAuditSink((event) => {
|
|
36
|
+
logging.record(toSudoAuditEntry(event));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { BlockManifest, FacilisConfig, FacilisManifest, LocaleCatalogs, ManifestWarning, RouteMeta, SchemaSnapshot } from "@facilis.dev/core";
|
|
2
|
+
import type { RouteMetadata, RouteMetadataMap } from "../types.js";
|
|
3
|
+
export interface ManifestDataSources {
|
|
4
|
+
readonly config: FacilisConfig;
|
|
5
|
+
readonly routeMetadata: RouteMetadataMap;
|
|
6
|
+
/** Omitted when the schema could not be introspected; `schemaFailure` explains why. */
|
|
7
|
+
readonly schema?: SchemaSnapshot;
|
|
8
|
+
readonly schemaFailure?: ManifestWarning;
|
|
9
|
+
readonly catalogs?: LocaleCatalogs;
|
|
10
|
+
readonly blocks?: readonly BlockManifest[];
|
|
11
|
+
readonly coreVersion?: string;
|
|
12
|
+
readonly nitroVersion?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ManifestDataResult {
|
|
15
|
+
readonly manifest: FacilisManifest;
|
|
16
|
+
readonly warnings: readonly ManifestWarning[];
|
|
17
|
+
/** True when a section failed and the response is a 200 partial (SPEC §6.4). */
|
|
18
|
+
readonly partial: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Pure assembler joining the toolchain outputs (AST pass, schema snapshot, locale
|
|
22
|
+
* files) into the FacilisManifest contract. Kept free of fs/db access so the route
|
|
23
|
+
* handlers stay thin and this logic is unit-testable.
|
|
24
|
+
*/
|
|
25
|
+
export declare function buildManifestData(sources: ManifestDataSources): ManifestDataResult;
|
|
26
|
+
/** Deterministic order: path first, then HTTP method (visualizers and diffs depend on it). */
|
|
27
|
+
export declare function flattenRouteMetadata(metadata: RouteMetadataMap): readonly RouteMetadata[];
|
|
28
|
+
export declare function toRouteMeta(route: RouteMetadata): RouteMeta;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { buildManifest } from "@facilis.dev/core";
|
|
2
|
+
/**
|
|
3
|
+
* Pure assembler joining the toolchain outputs (AST pass, schema snapshot, locale
|
|
4
|
+
* files) into the FacilisManifest contract. Kept free of fs/db access so the route
|
|
5
|
+
* handlers stay thin and this logic is unit-testable.
|
|
6
|
+
*/
|
|
7
|
+
export function buildManifestData(sources) {
|
|
8
|
+
const routes = flattenRouteMetadata(sources.routeMetadata);
|
|
9
|
+
const manifest = buildManifest({
|
|
10
|
+
config: sources.config,
|
|
11
|
+
routes: routes.map(toRouteMeta),
|
|
12
|
+
schema: sources.schema,
|
|
13
|
+
catalogs: sources.catalogs,
|
|
14
|
+
blocks: sources.blocks === undefined ? undefined : sortBlocks(sources.blocks),
|
|
15
|
+
coreVersion: sources.coreVersion,
|
|
16
|
+
nitroVersion: sources.nitroVersion,
|
|
17
|
+
});
|
|
18
|
+
const warnings = routes.flatMap((route) => route.warnings);
|
|
19
|
+
if (sources.schemaFailure !== undefined)
|
|
20
|
+
warnings.push(sources.schemaFailure);
|
|
21
|
+
return { manifest, warnings, partial: sources.schemaFailure !== undefined };
|
|
22
|
+
}
|
|
23
|
+
/** Deterministic order: path first, then HTTP method (visualizers and diffs depend on it). */
|
|
24
|
+
export function flattenRouteMetadata(metadata) {
|
|
25
|
+
const routes = [];
|
|
26
|
+
for (const path of Object.keys(metadata).toSorted((left, right) => left.localeCompare(right))) {
|
|
27
|
+
const operations = metadata[path];
|
|
28
|
+
if (operations === undefined)
|
|
29
|
+
continue;
|
|
30
|
+
for (const method of Object.keys(operations).toSorted((left, right) => left.localeCompare(right))) {
|
|
31
|
+
const route = operations[method];
|
|
32
|
+
if (route !== undefined)
|
|
33
|
+
routes.push(route);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return routes;
|
|
37
|
+
}
|
|
38
|
+
export function toRouteMeta(route) {
|
|
39
|
+
return {
|
|
40
|
+
method: route.method,
|
|
41
|
+
path: route.path,
|
|
42
|
+
summary: route.description,
|
|
43
|
+
description: route.description,
|
|
44
|
+
permission: route.permission,
|
|
45
|
+
...(route.crossSite === true ? { crossSite: true } : {}),
|
|
46
|
+
// A resolved false is published (the derogation is off); an unreadable flag stays unknown.
|
|
47
|
+
...(route.unresolved.includes("rawSql") ? {} : { rawSql: route.rawSql === true }),
|
|
48
|
+
dbTables: route.dbTables,
|
|
49
|
+
dbColumns: route.dbColumns,
|
|
50
|
+
security: deriveSecurity(route),
|
|
51
|
+
sourceFile: route.sourceFile,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* SPEC §7.3 security classification. A value the AST pass could not read is
|
|
56
|
+
* "unknown", never "public": guessing would weaken the Studio's security view.
|
|
57
|
+
*/
|
|
58
|
+
function deriveSecurity(route) {
|
|
59
|
+
if (route.unresolved.includes("permission"))
|
|
60
|
+
return "unknown";
|
|
61
|
+
const permission = route.permission;
|
|
62
|
+
if (permission === undefined)
|
|
63
|
+
return "public";
|
|
64
|
+
if (typeof permission === "string")
|
|
65
|
+
return permission.length === 0 ? "public" : "required";
|
|
66
|
+
return permission.length === 0 ? "public" : "required";
|
|
67
|
+
}
|
|
68
|
+
function sortBlocks(blocks) {
|
|
69
|
+
return [...blocks].toSorted((left, right) => left.name.localeCompare(right.name));
|
|
70
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { FacilisManifest, ManifestSection, ManifestSummary, ManifestWarning } from "@facilis.dev/core";
|
|
2
|
+
import { type HttpResult } from "./http.js";
|
|
3
|
+
/** Weak ETag: the revision is stable content, not a strong representation validator. */
|
|
4
|
+
export declare function weakEtag(revision: string): string;
|
|
5
|
+
/** Split an If-None-Match list into bare revisions (weakness and quotes removed). */
|
|
6
|
+
export declare function parseEntityTags(header: string | null): string[];
|
|
7
|
+
/**
|
|
8
|
+
* RFC 9110 conditional request handling for an ETag-based endpoint. `since` is the
|
|
9
|
+
* spec's query-parameter shortcut (SPEC §6.1) and behaves like If-None-Match.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isNotModified(input: {
|
|
12
|
+
readonly ifNoneMatch: string | null;
|
|
13
|
+
readonly since: string | null;
|
|
14
|
+
readonly revision: string;
|
|
15
|
+
}): boolean;
|
|
16
|
+
export declare function manifestHeaders(revision: string, partial: boolean): Record<string, string>;
|
|
17
|
+
export declare function notModifiedResult(revision: string): HttpResult;
|
|
18
|
+
export declare function manifestResponse(input: {
|
|
19
|
+
readonly revision: string;
|
|
20
|
+
readonly generatedAt: string;
|
|
21
|
+
readonly sections: readonly ManifestSection[];
|
|
22
|
+
readonly manifest: Partial<FacilisManifest>;
|
|
23
|
+
readonly warnings: readonly ManifestWarning[];
|
|
24
|
+
readonly partial: boolean;
|
|
25
|
+
}): HttpResult;
|
|
26
|
+
/**
|
|
27
|
+
* `?format=summary` payload (SPEC §6.1, §14.1-11).
|
|
28
|
+
*
|
|
29
|
+
* Deviation from FacilisMetaResponse: ManifestSummary is not a Partial<FacilisManifest>,
|
|
30
|
+
* so the compact view rides under `summary` instead of pretending to be a manifest.
|
|
31
|
+
*/
|
|
32
|
+
export interface FacilisSummaryResponse {
|
|
33
|
+
readonly revision: string;
|
|
34
|
+
readonly generatedAt: string;
|
|
35
|
+
readonly sections: readonly ManifestSection[];
|
|
36
|
+
readonly summary: ManifestSummary;
|
|
37
|
+
readonly warnings: readonly ManifestWarning[];
|
|
38
|
+
}
|
|
39
|
+
export declare function summaryResponse(input: {
|
|
40
|
+
readonly revision: string;
|
|
41
|
+
readonly generatedAt: string;
|
|
42
|
+
readonly sections: readonly ManifestSection[];
|
|
43
|
+
readonly summary: ManifestSummary;
|
|
44
|
+
readonly warnings: readonly ManifestWarning[];
|
|
45
|
+
}): HttpResult;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { jsonResult } from "./http.js";
|
|
2
|
+
const NO_CACHE = "no-cache";
|
|
3
|
+
/** Weak ETag: the revision is stable content, not a strong representation validator. */
|
|
4
|
+
export function weakEtag(revision) {
|
|
5
|
+
return `W/"${revision}"`;
|
|
6
|
+
}
|
|
7
|
+
/** Split an If-None-Match list into bare revisions (weakness and quotes removed). */
|
|
8
|
+
export function parseEntityTags(header) {
|
|
9
|
+
if (header === null)
|
|
10
|
+
return [];
|
|
11
|
+
return header
|
|
12
|
+
.split(",")
|
|
13
|
+
.map((tag) => tag.trim().replace(/^W\//, "").replace(/^"|"$/g, ""))
|
|
14
|
+
.filter((tag) => tag.length > 0);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* RFC 9110 conditional request handling for an ETag-based endpoint. `since` is the
|
|
18
|
+
* spec's query-parameter shortcut (SPEC §6.1) and behaves like If-None-Match.
|
|
19
|
+
*/
|
|
20
|
+
export function isNotModified(input) {
|
|
21
|
+
const tags = parseEntityTags(input.ifNoneMatch);
|
|
22
|
+
if (tags.includes("*") || tags.includes(input.revision))
|
|
23
|
+
return true;
|
|
24
|
+
return input.since !== null && input.since !== "" && input.since === input.revision;
|
|
25
|
+
}
|
|
26
|
+
export function manifestHeaders(revision, partial) {
|
|
27
|
+
return {
|
|
28
|
+
"cache-control": NO_CACHE,
|
|
29
|
+
etag: weakEtag(revision),
|
|
30
|
+
"x-facilis-revision": revision,
|
|
31
|
+
...(partial ? { "x-facilis-partial": "true" } : {}),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function notModifiedResult(revision) {
|
|
35
|
+
return { status: 304, headers: manifestHeaders(revision, false) };
|
|
36
|
+
}
|
|
37
|
+
export function manifestResponse(input) {
|
|
38
|
+
const body = {
|
|
39
|
+
revision: input.revision,
|
|
40
|
+
generatedAt: input.generatedAt,
|
|
41
|
+
sections: input.sections,
|
|
42
|
+
manifest: input.manifest,
|
|
43
|
+
warnings: input.warnings,
|
|
44
|
+
};
|
|
45
|
+
return jsonResult(200, body, manifestHeaders(input.revision, input.partial));
|
|
46
|
+
}
|
|
47
|
+
export function summaryResponse(input) {
|
|
48
|
+
const body = { ...input };
|
|
49
|
+
return jsonResult(200, body, manifestHeaders(input.revision, false));
|
|
50
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type FacilisManifest, type ManifestSection } from "@facilis.dev/core";
|
|
2
|
+
export interface SectionsParameter {
|
|
3
|
+
/** True when every requested section name is valid (or none were requested). */
|
|
4
|
+
readonly ok: boolean;
|
|
5
|
+
/** Undefined means "all sections" (the documented default). */
|
|
6
|
+
readonly sections?: readonly ManifestSection[];
|
|
7
|
+
readonly invalid: readonly string[];
|
|
8
|
+
}
|
|
9
|
+
/** Parse `?sections=config,routes` (SPEC §6.1); invalid names make the request a 400. */
|
|
10
|
+
export declare function parseSectionsParameter(value: string | null): SectionsParameter;
|
|
11
|
+
/**
|
|
12
|
+
* Select the manifest sections in the response envelope.
|
|
13
|
+
*
|
|
14
|
+
* `config` maps to `project` and `meta` to `framework`: those are the names the
|
|
15
|
+
* spec uses on the wire (SPEC §6.1) while the manifest type keeps its own layout
|
|
16
|
+
* (SPEC §7.3). Without a filter every section is returned.
|
|
17
|
+
*/
|
|
18
|
+
export declare function selectManifestSections(manifest: FacilisManifest, sections: readonly ManifestSection[] | undefined): Partial<FacilisManifest>;
|
|
19
|
+
/** All section names, in the canonical order (used when no filter is supplied). */
|
|
20
|
+
export declare const ALL_MANIFEST_SECTIONS: readonly ManifestSection[];
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { MANIFEST_SECTIONS, isManifestSection, } from "@facilis.dev/core";
|
|
2
|
+
/** Parse `?sections=config,routes` (SPEC §6.1); invalid names make the request a 400. */
|
|
3
|
+
export function parseSectionsParameter(value) {
|
|
4
|
+
if (value === null || value.trim() === "")
|
|
5
|
+
return { ok: true, invalid: [] };
|
|
6
|
+
const requested = value
|
|
7
|
+
.split(",")
|
|
8
|
+
.map((section) => section.trim())
|
|
9
|
+
.filter((section) => section.length > 0);
|
|
10
|
+
const invalid = requested.filter((section) => !isManifestSection(section));
|
|
11
|
+
if (invalid.length > 0)
|
|
12
|
+
return { ok: false, invalid };
|
|
13
|
+
return { ok: true, sections: [...new Set(requested)], invalid: [] };
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Select the manifest sections in the response envelope.
|
|
17
|
+
*
|
|
18
|
+
* `config` maps to `project` and `meta` to `framework`: those are the names the
|
|
19
|
+
* spec uses on the wire (SPEC §6.1) while the manifest type keeps its own layout
|
|
20
|
+
* (SPEC §7.3). Without a filter every section is returned.
|
|
21
|
+
*/
|
|
22
|
+
export function selectManifestSections(manifest, sections) {
|
|
23
|
+
if (sections === undefined) {
|
|
24
|
+
return {
|
|
25
|
+
framework: manifest.framework,
|
|
26
|
+
project: manifest.project,
|
|
27
|
+
blocks: manifest.blocks,
|
|
28
|
+
routes: manifest.routes,
|
|
29
|
+
schema: manifest.schema,
|
|
30
|
+
i18n: manifest.i18n,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const selected = {};
|
|
34
|
+
for (const section of sections) {
|
|
35
|
+
switch (section) {
|
|
36
|
+
case "config":
|
|
37
|
+
selected.project = manifest.project;
|
|
38
|
+
break;
|
|
39
|
+
case "meta":
|
|
40
|
+
selected.framework = manifest.framework;
|
|
41
|
+
break;
|
|
42
|
+
case "blocks":
|
|
43
|
+
selected.blocks = manifest.blocks;
|
|
44
|
+
break;
|
|
45
|
+
case "routes":
|
|
46
|
+
selected.routes = manifest.routes;
|
|
47
|
+
break;
|
|
48
|
+
case "schema":
|
|
49
|
+
selected.schema = manifest.schema;
|
|
50
|
+
break;
|
|
51
|
+
case "i18n":
|
|
52
|
+
selected.i18n = manifest.i18n;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return selected;
|
|
57
|
+
}
|
|
58
|
+
/** All section names, in the canonical order (used when no filter is supplied). */
|
|
59
|
+
export const ALL_MANIFEST_SECTIONS = MANIFEST_SECTIONS;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ManifestWarning } from "@facilis.dev/core";
|
|
2
|
+
import { type ManifestDataResult } from "./manifest-data.js";
|
|
3
|
+
export interface ManifestSnapshot {
|
|
4
|
+
readonly data: ManifestDataResult;
|
|
5
|
+
readonly revision: string;
|
|
6
|
+
readonly generatedAt: string;
|
|
7
|
+
readonly warnings: readonly ManifestWarning[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Assemble everything `/__facilis/manifest` needs.
|
|
11
|
+
*
|
|
12
|
+
* Returns null only when the project config itself is unusable (the module cannot
|
|
13
|
+
* describe the project at all); a broken schema is a 200-partial via
|
|
14
|
+
* ManifestDataResult.partial (SPEC §6.4/§6.7).
|
|
15
|
+
*/
|
|
16
|
+
export declare function getManifestSnapshot(): Promise<ManifestSnapshot | null>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { schemaSnapshot } from "@facilis.dev/db";
|
|
2
|
+
import config, { coreVersion, nitroVersion } from "#facilis/config";
|
|
3
|
+
import routeMetadata from "#facilis/openapi-meta";
|
|
4
|
+
import { getBootstrap } from "./bootstrap.js";
|
|
5
|
+
import { isFacilisConfig } from "./config.js";
|
|
6
|
+
import { buildManifestData, } from "./manifest-data.js";
|
|
7
|
+
import { getRevisionSnapshot } from "./revision-service.js";
|
|
8
|
+
/**
|
|
9
|
+
* Assemble everything `/__facilis/manifest` needs.
|
|
10
|
+
*
|
|
11
|
+
* Returns null only when the project config itself is unusable (the module cannot
|
|
12
|
+
* describe the project at all); a broken schema is a 200-partial via
|
|
13
|
+
* ManifestDataResult.partial (SPEC §6.4/§6.7).
|
|
14
|
+
*/
|
|
15
|
+
export async function getManifestSnapshot() {
|
|
16
|
+
const [revision, bootstrap] = await Promise.all([getRevisionSnapshot(), getBootstrap()]);
|
|
17
|
+
const projectConfig = resolveConfig(bootstrap.config);
|
|
18
|
+
if (projectConfig === null)
|
|
19
|
+
return null;
|
|
20
|
+
const schemaOutcome = await readSchema(bootstrap.db);
|
|
21
|
+
const sources = {
|
|
22
|
+
config: projectConfig,
|
|
23
|
+
routeMetadata,
|
|
24
|
+
catalogs: revision.catalogs,
|
|
25
|
+
coreVersion,
|
|
26
|
+
nitroVersion,
|
|
27
|
+
...schemaOutcome,
|
|
28
|
+
};
|
|
29
|
+
const data = buildManifestData(sources);
|
|
30
|
+
return {
|
|
31
|
+
data,
|
|
32
|
+
revision: revision.revision,
|
|
33
|
+
generatedAt: revision.generatedAt,
|
|
34
|
+
warnings: [...bootstrap.warnings, ...data.warnings],
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function resolveConfig(bootstrapped) {
|
|
38
|
+
if (bootstrapped !== null)
|
|
39
|
+
return bootstrapped;
|
|
40
|
+
// The middleware may not have run yet (e.g. a direct handler import in tests);
|
|
41
|
+
// the virtual module is still a valid config source when the bootstrap failed early.
|
|
42
|
+
return isFacilisConfig(config) ? config : null;
|
|
43
|
+
}
|
|
44
|
+
async function readSchema(db) {
|
|
45
|
+
if (db === null) {
|
|
46
|
+
return {
|
|
47
|
+
failure: {
|
|
48
|
+
code: "db_unavailable",
|
|
49
|
+
message: "The database could not be booted; the schema section is unavailable.",
|
|
50
|
+
section: "schema",
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
return { schema: await schemaSnapshot(db) };
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
return {
|
|
59
|
+
failure: {
|
|
60
|
+
code: "schema_unavailable",
|
|
61
|
+
message: `Schema introspection failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
62
|
+
section: "schema",
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FacilisConfig, ManifestWarning } from "@facilis.dev/core";
|
|
2
|
+
/**
|
|
3
|
+
* Reads and validates the project's virtual config module.
|
|
4
|
+
*
|
|
5
|
+
* A broken config is a warning, never a boot failure: the rest of the bootstrap still runs
|
|
6
|
+
* (contracts §8), and the caller degrades to "no config" with a partial manifest.
|
|
7
|
+
*/
|
|
8
|
+
export declare function readProjectConfig(warnings: ManifestWarning[]): FacilisConfig | null;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import config, { configError } from "#facilis/config";
|
|
2
|
+
import { isFacilisConfig } from "./config.js";
|
|
3
|
+
/**
|
|
4
|
+
* Reads and validates the project's virtual config module.
|
|
5
|
+
*
|
|
6
|
+
* A broken config is a warning, never a boot failure: the rest of the bootstrap still runs
|
|
7
|
+
* (contracts §8), and the caller degrades to "no config" with a partial manifest.
|
|
8
|
+
*/
|
|
9
|
+
export function readProjectConfig(warnings) {
|
|
10
|
+
if (configError !== undefined) {
|
|
11
|
+
warnings.push({ code: "config_load_failed", message: configError });
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
if (!isFacilisConfig(config)) {
|
|
15
|
+
warnings.push({
|
|
16
|
+
code: "config_invalid",
|
|
17
|
+
message: "facilis.config.ts must export defineConfig({ auth, db, i18n, ... }).",
|
|
18
|
+
});
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return config;
|
|
22
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { LocaleCatalogs, SchemaFileRevisionSource } from "@facilis.dev/core";
|
|
2
|
+
export declare function readTextFile(filePath: string): Promise<string | null>;
|
|
3
|
+
/**
|
|
4
|
+
* Follow db/schema.ts and its relative imports, returning path + content pairs.
|
|
5
|
+
*
|
|
6
|
+
* A static regex walk is enough here: the revision only needs the file set and its
|
|
7
|
+
* bytes (SPEC §6.5), not a resolved module graph. Missing files are skipped so a
|
|
8
|
+
* half-written schema never breaks the endpoint.
|
|
9
|
+
*/
|
|
10
|
+
export declare function collectSchemaFiles(rootDir: string): Promise<readonly SchemaFileRevisionSource[]>;
|
|
11
|
+
/** Read `locales/*.json` into the manifest i18n.catalogs shape; invalid files are skipped. */
|
|
12
|
+
export declare function collectLocaleCatalogs(rootDir: string): Promise<LocaleCatalogs>;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join, relative, sep } from "node:path";
|
|
3
|
+
/** SPEC §6.5 hashes db/schema.ts and the modules it imports (transitively). */
|
|
4
|
+
const SCHEMA_ENTRY = "db/schema.ts";
|
|
5
|
+
const LOCALES_DIR = "locales";
|
|
6
|
+
const MAX_SCHEMA_FILES = 32;
|
|
7
|
+
const MAX_SCHEMA_DEPTH = 8;
|
|
8
|
+
const RELATIVE_IMPORT = /(?:import|export)\s[^;]*?from\s*["'](\.[^"']+)["']/g;
|
|
9
|
+
export async function readTextFile(filePath) {
|
|
10
|
+
try {
|
|
11
|
+
return await readFile(filePath, "utf8");
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Follow db/schema.ts and its relative imports, returning path + content pairs.
|
|
19
|
+
*
|
|
20
|
+
* A static regex walk is enough here: the revision only needs the file set and its
|
|
21
|
+
* bytes (SPEC §6.5), not a resolved module graph. Missing files are skipped so a
|
|
22
|
+
* half-written schema never breaks the endpoint.
|
|
23
|
+
*/
|
|
24
|
+
export async function collectSchemaFiles(rootDir) {
|
|
25
|
+
const files = new Map();
|
|
26
|
+
await visitSchemaFile(join(rootDir, SCHEMA_ENTRY), files, 0);
|
|
27
|
+
return [...files.entries()]
|
|
28
|
+
.map(([absolute, content]) => ({
|
|
29
|
+
path: relative(rootDir, absolute).split(sep).join("/"),
|
|
30
|
+
content,
|
|
31
|
+
}))
|
|
32
|
+
.toSorted((left, right) => left.path.localeCompare(right.path));
|
|
33
|
+
}
|
|
34
|
+
async function visitSchemaFile(filePath, files, depth) {
|
|
35
|
+
if (depth > MAX_SCHEMA_DEPTH || files.has(filePath) || files.size >= MAX_SCHEMA_FILES)
|
|
36
|
+
return;
|
|
37
|
+
const content = await readTextFile(filePath);
|
|
38
|
+
if (content === null)
|
|
39
|
+
return;
|
|
40
|
+
files.set(filePath, content);
|
|
41
|
+
for (const match of content.matchAll(RELATIVE_IMPORT)) {
|
|
42
|
+
const specifier = match[1];
|
|
43
|
+
if (specifier === undefined)
|
|
44
|
+
continue;
|
|
45
|
+
// oxlint-disable-next-line no-await-in-loop -- recursion shares the dedupe Map; parallel visits would race on it.
|
|
46
|
+
const resolved = await resolveRelativeModule(dirname(filePath), specifier);
|
|
47
|
+
// oxlint-disable-next-line no-await-in-loop -- depth-first keeps the file-count cap deterministic.
|
|
48
|
+
if (resolved !== null)
|
|
49
|
+
await visitSchemaFile(resolved, files, depth + 1);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function resolveRelativeModule(fromDir, specifier) {
|
|
53
|
+
const base = join(fromDir, specifier);
|
|
54
|
+
const candidates = [base, `${base}.ts`, `${base}.tsx`, `${base}.mts`, join(base, "index.ts")];
|
|
55
|
+
for (const candidate of candidates) {
|
|
56
|
+
// oxlint-disable-next-line no-await-in-loop -- stop at the first existing candidate.
|
|
57
|
+
if ((await readTextFile(candidate)) !== null)
|
|
58
|
+
return candidate;
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
/** Read `locales/*.json` into the manifest i18n.catalogs shape; invalid files are skipped. */
|
|
63
|
+
export async function collectLocaleCatalogs(rootDir) {
|
|
64
|
+
const dir = join(rootDir, LOCALES_DIR);
|
|
65
|
+
let entries;
|
|
66
|
+
try {
|
|
67
|
+
entries = await readdir(dir);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
const names = entries.filter((name) => name.endsWith(".json")).toSorted();
|
|
73
|
+
const contents = await Promise.all(names.map((name) => readTextFile(join(dir, name))));
|
|
74
|
+
const catalogs = {};
|
|
75
|
+
for (const [index, name] of names.entries()) {
|
|
76
|
+
const content = contents[index];
|
|
77
|
+
if (content === null || content === undefined)
|
|
78
|
+
continue;
|
|
79
|
+
try {
|
|
80
|
+
const messages = asStringRecord(JSON.parse(content));
|
|
81
|
+
if (messages !== undefined)
|
|
82
|
+
catalogs[name.slice(0, -".json".length)] = messages;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
// A malformed locale file must not break the manifest; it is simply absent.
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return catalogs;
|
|
89
|
+
}
|
|
90
|
+
function asStringRecord(value) {
|
|
91
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
92
|
+
return undefined;
|
|
93
|
+
const record = {};
|
|
94
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
95
|
+
if (typeof entry === "string")
|
|
96
|
+
record[key] = entry;
|
|
97
|
+
}
|
|
98
|
+
return record;
|
|
99
|
+
}
|