@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,85 @@
|
|
|
1
|
+
import { capString, redactRecord } from "./redact.js";
|
|
2
|
+
/**
|
|
3
|
+
* Entry sanitization at the sink boundary (D-119).
|
|
4
|
+
*
|
|
5
|
+
* Core builds the entry shapes; this pass makes them safe to serialize: every free-text field
|
|
6
|
+
* is capped, and the only arbitrary payloads (`data`/`details`) are redacted. Strings are
|
|
7
|
+
* capped rather than truncated with a marker so a hostile value cannot grow a line.
|
|
8
|
+
*/
|
|
9
|
+
export const MAX_LOG_MESSAGE = 1000;
|
|
10
|
+
export const MAX_LOG_STACK = 4000;
|
|
11
|
+
export const MAX_LOG_FIELD = 1000;
|
|
12
|
+
export const MAX_LOG_SHORT = 200;
|
|
13
|
+
export const MAX_LOG_ARRAY = 50;
|
|
14
|
+
export function sanitizeEntry(entry) {
|
|
15
|
+
switch (entry.kind) {
|
|
16
|
+
case "access":
|
|
17
|
+
return sanitizeAccess(entry);
|
|
18
|
+
case "error":
|
|
19
|
+
return sanitizeError(entry);
|
|
20
|
+
case "audit":
|
|
21
|
+
return sanitizeAudit(entry);
|
|
22
|
+
case "app":
|
|
23
|
+
return sanitizeApp(entry);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function sanitizeAccess(entry) {
|
|
27
|
+
return {
|
|
28
|
+
...entry,
|
|
29
|
+
path: capString(entry.path, MAX_LOG_FIELD),
|
|
30
|
+
...(entry.userAgent === undefined
|
|
31
|
+
? {}
|
|
32
|
+
: { userAgent: capString(entry.userAgent, MAX_LOG_FIELD) }),
|
|
33
|
+
// The client IP comes from an attacker-controlled header when trustProxy is on: cap it
|
|
34
|
+
// or a multi-KB x-forwarded-for would inflate every access line and the file.
|
|
35
|
+
...(entry.ip === undefined ? {} : { ip: capString(entry.ip, MAX_LOG_SHORT) }),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function sanitizeError(entry) {
|
|
39
|
+
return {
|
|
40
|
+
...entry,
|
|
41
|
+
code: capString(entry.code, MAX_LOG_SHORT),
|
|
42
|
+
name: capString(entry.name, MAX_LOG_SHORT),
|
|
43
|
+
message: capString(entry.message, MAX_LOG_MESSAGE),
|
|
44
|
+
...(entry.stack === undefined ? {} : { stack: capString(entry.stack, MAX_LOG_STACK) }),
|
|
45
|
+
...(entry.method === undefined ? {} : { method: capString(entry.method, MAX_LOG_SHORT) }),
|
|
46
|
+
...(entry.path === undefined ? {} : { path: capString(entry.path, MAX_LOG_FIELD) }),
|
|
47
|
+
requestId: capString(entry.requestId, MAX_LOG_SHORT),
|
|
48
|
+
...(entry.ip === undefined ? {} : { ip: capString(entry.ip, MAX_LOG_SHORT) }),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function sanitizeAudit(entry) {
|
|
52
|
+
return {
|
|
53
|
+
...entry,
|
|
54
|
+
event: capString(entry.event, MAX_LOG_SHORT),
|
|
55
|
+
actor: capString(entry.actor, MAX_LOG_SHORT),
|
|
56
|
+
...(entry.operation === undefined
|
|
57
|
+
? {}
|
|
58
|
+
: { operation: capString(entry.operation, MAX_LOG_FIELD) }),
|
|
59
|
+
...(entry.errorCode === undefined
|
|
60
|
+
? {}
|
|
61
|
+
: { errorCode: capString(entry.errorCode, MAX_LOG_SHORT) }),
|
|
62
|
+
...(entry.reason === undefined ? {} : { reason: capString(entry.reason, MAX_LOG_MESSAGE) }),
|
|
63
|
+
...(entry.method === undefined ? {} : { method: capString(entry.method, MAX_LOG_SHORT) }),
|
|
64
|
+
...(entry.path === undefined ? {} : { path: capString(entry.path, MAX_LOG_FIELD) }),
|
|
65
|
+
...(entry.tables === undefined ? {} : { tables: capList(entry.tables) }),
|
|
66
|
+
...(entry.roles === undefined ? {} : { roles: capList(entry.roles) }),
|
|
67
|
+
...(entry.details === undefined ? {} : { details: redactRecord(entry.details) }),
|
|
68
|
+
...(entry.ip === undefined ? {} : { ip: capString(entry.ip, MAX_LOG_SHORT) }),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function sanitizeApp(entry) {
|
|
72
|
+
return {
|
|
73
|
+
...entry,
|
|
74
|
+
message: capString(entry.message, MAX_LOG_MESSAGE),
|
|
75
|
+
...(entry.data === undefined ? {} : { data: redactRecord(entry.data) }),
|
|
76
|
+
...(entry.method === undefined ? {} : { method: capString(entry.method, MAX_LOG_SHORT) }),
|
|
77
|
+
...(entry.path === undefined ? {} : { path: capString(entry.path, MAX_LOG_FIELD) }),
|
|
78
|
+
...(entry.ip === undefined ? {} : { ip: capString(entry.ip, MAX_LOG_SHORT) }),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function capList(values) {
|
|
82
|
+
if (values === undefined)
|
|
83
|
+
return undefined;
|
|
84
|
+
return values.slice(0, MAX_LOG_ARRAY).map((value) => capString(value, MAX_LOG_FIELD));
|
|
85
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { LogSettingsWarning } from "./log-settings.js";
|
|
2
|
+
import type { LogSink, LogStream } from "./log-sink.js";
|
|
3
|
+
/**
|
|
4
|
+
* JSONL file sink with size-based rotation (D-119).
|
|
5
|
+
*
|
|
6
|
+
* Writes are synchronous and serialized by construction (one file writer per process), which
|
|
7
|
+
* is what makes the audit chain's `prevHash` exact. A file is rotated before an append that
|
|
8
|
+
* would pass the cap: the active file becomes `.1`, older files shift by one and the oldest is
|
|
9
|
+
* deleted, keeping exactly `keep` rotated files. Retention beyond that is the deploy's job.
|
|
10
|
+
*
|
|
11
|
+
* `append` throws on any filesystem failure. The runtime turns that into a dropped
|
|
12
|
+
* access/error/app entry (best effort) or a failed request for audit (fail closed, D-099).
|
|
13
|
+
*/
|
|
14
|
+
export interface FileLogSinkOptions {
|
|
15
|
+
readonly dir: string;
|
|
16
|
+
readonly maxBytes: number;
|
|
17
|
+
readonly keep: number;
|
|
18
|
+
}
|
|
19
|
+
export interface FileLogSinkSetup {
|
|
20
|
+
readonly sink: LogSink;
|
|
21
|
+
readonly warnings: readonly LogSettingsWarning[];
|
|
22
|
+
}
|
|
23
|
+
export declare function logFilePath(dir: string, stream: LogStream, index?: number): string;
|
|
24
|
+
export declare function createFileLogSink(options: FileLogSinkOptions): FileLogSinkSetup;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync, readFileSync, renameSync, rmSync, statSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { chainAuditEntry, isChainedAuditEntry } from "./audit-chain.js";
|
|
4
|
+
const LOG_FILE_EXTENSION = ".jsonl";
|
|
5
|
+
export function logFilePath(dir, stream, index) {
|
|
6
|
+
const suffix = index === undefined ? LOG_FILE_EXTENSION : `.${index}${LOG_FILE_EXTENSION}`;
|
|
7
|
+
return join(dir, `${stream}${suffix}`);
|
|
8
|
+
}
|
|
9
|
+
export function createFileLogSink(options) {
|
|
10
|
+
const warnings = [];
|
|
11
|
+
let directoryReady = tryCreateDirectory(options.dir, warnings);
|
|
12
|
+
let auditHead = directoryReady
|
|
13
|
+
? readAuditHead(options.dir, warnings)
|
|
14
|
+
: null;
|
|
15
|
+
const append = (stream, entry) => {
|
|
16
|
+
if (!directoryReady)
|
|
17
|
+
directoryReady = createDirectory(options.dir);
|
|
18
|
+
const path = logFilePath(options.dir, stream);
|
|
19
|
+
const chained = stream === "audit" ? chainAuditEntry(entry, auditHead) : null;
|
|
20
|
+
const line = `${JSON.stringify(chained ?? entry)}\n`;
|
|
21
|
+
rotateIfNeeded(path, Buffer.byteLength(line, "utf8"), options);
|
|
22
|
+
appendFileSync(path, line, "utf8");
|
|
23
|
+
// The chain only advances after the bytes are on disk, so a failed append cannot leave a
|
|
24
|
+
// gap the next entry would silently hash over.
|
|
25
|
+
if (chained !== null)
|
|
26
|
+
auditHead = { seq: chained.seq, hash: chained.hash };
|
|
27
|
+
};
|
|
28
|
+
return { sink: { append }, warnings };
|
|
29
|
+
}
|
|
30
|
+
function readAuditHead(dir, warnings) {
|
|
31
|
+
const path = logFilePath(dir, "audit");
|
|
32
|
+
let content;
|
|
33
|
+
try {
|
|
34
|
+
content = readFileSync(path, "utf8");
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (isMissingFile(error))
|
|
38
|
+
return null;
|
|
39
|
+
warnings.push({
|
|
40
|
+
code: "logging_audit_chain_unreadable",
|
|
41
|
+
message: `Could not read the audit chain ${path}: ${describe(error)}. Starting a new chain.`,
|
|
42
|
+
});
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const lines = content.split("\n").filter((line) => line.trim().length > 0);
|
|
46
|
+
const last = lines[lines.length - 1];
|
|
47
|
+
if (last === undefined)
|
|
48
|
+
return null;
|
|
49
|
+
try {
|
|
50
|
+
const parsed = JSON.parse(last);
|
|
51
|
+
if (!isChainedAuditEntry(parsed))
|
|
52
|
+
throw new Error("the last line is not a chained audit entry");
|
|
53
|
+
return { seq: parsed.seq, hash: parsed.hash };
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
// A corrupt/truncated tail must not make the audit stream unwritable (the app would keep
|
|
57
|
+
// booting) nor pretend the chain is intact: warn and restart, the tamper is visible.
|
|
58
|
+
warnings.push({
|
|
59
|
+
code: "logging_audit_chain_reset",
|
|
60
|
+
message: `The audit chain in ${path} could not be continued (${describe(error)}); starting a new chain.`,
|
|
61
|
+
});
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function rotateIfNeeded(path, incomingBytes, options) {
|
|
66
|
+
const size = fileSize(path);
|
|
67
|
+
// `size > 0` keeps a single over-cap entry writable instead of rotating an empty file
|
|
68
|
+
// forever: a line larger than the cap is written once and rotated by the next append.
|
|
69
|
+
if (size === 0 || size + incomingBytes <= options.maxBytes)
|
|
70
|
+
return;
|
|
71
|
+
rotate(path, options.keep);
|
|
72
|
+
}
|
|
73
|
+
function rotate(path, keep) {
|
|
74
|
+
if (keep <= 0) {
|
|
75
|
+
rmSync(path, { force: true });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
rmSync(rotatedPath(path, keep), { force: true });
|
|
79
|
+
for (let index = keep - 1; index >= 1; index -= 1) {
|
|
80
|
+
renameIfExists(rotatedPath(path, index), rotatedPath(path, index + 1));
|
|
81
|
+
}
|
|
82
|
+
renameSync(path, rotatedPath(path, 1));
|
|
83
|
+
}
|
|
84
|
+
function rotatedPath(path, index) {
|
|
85
|
+
return `${path.slice(0, -LOG_FILE_EXTENSION.length)}.${index}${LOG_FILE_EXTENSION}`;
|
|
86
|
+
}
|
|
87
|
+
function fileSize(path) {
|
|
88
|
+
try {
|
|
89
|
+
return statSync(path).size;
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (isMissingFile(error))
|
|
93
|
+
return 0;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function renameIfExists(from, to) {
|
|
98
|
+
try {
|
|
99
|
+
renameSync(from, to);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (isMissingFile(error))
|
|
103
|
+
return;
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function createDirectory(dir) {
|
|
108
|
+
mkdirSync(dir, { recursive: true });
|
|
109
|
+
return true;
|
|
110
|
+
}
|
|
111
|
+
function tryCreateDirectory(dir, warnings) {
|
|
112
|
+
try {
|
|
113
|
+
return createDirectory(dir);
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
warnings.push({
|
|
117
|
+
code: "logging_dir_unwritable",
|
|
118
|
+
message: `Could not create the log directory ${dir}: ${describe(error)}. Access/error/app entries will be dropped; audit writes will fail closed.`,
|
|
119
|
+
});
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function isMissingFile(error) {
|
|
124
|
+
return error instanceof Error && error.code === "ENOENT";
|
|
125
|
+
}
|
|
126
|
+
function describe(error) {
|
|
127
|
+
return error instanceof Error ? error.message : String(error);
|
|
128
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type LogLevel } from "@facilis.dev/core";
|
|
2
|
+
/**
|
|
3
|
+
* Log destination resolution (D-119, SPEC §8.7).
|
|
4
|
+
*
|
|
5
|
+
* Where entries go is a deployment concern, so it comes from the environment and never from
|
|
6
|
+
* `facilis.config.ts`: a generated project must not be able to redirect an operator's audit
|
|
7
|
+
* trail. Every invalid value falls back to its documented default and yields a warning, so a
|
|
8
|
+
* typo degrades the sink instead of breaking the boot.
|
|
9
|
+
*/
|
|
10
|
+
export declare const LOG_SINK_MODES: readonly ["auto", "stdout", "file", "both"];
|
|
11
|
+
export type LogSinkMode = (typeof LOG_SINK_MODES)[number];
|
|
12
|
+
export declare const LOG_SINK_ENV = "FACILIS_LOG_SINK";
|
|
13
|
+
export declare const LOG_DIR_ENV = "FACILIS_LOG_DIR";
|
|
14
|
+
export declare const LOG_MAX_BYTES_ENV = "FACILIS_LOG_MAX_BYTES";
|
|
15
|
+
export declare const LOG_KEEP_ENV = "FACILIS_LOG_KEEP";
|
|
16
|
+
export declare const LOG_LEVEL_ENV = "FACILIS_LOG_LEVEL";
|
|
17
|
+
/** 10 MiB keeps a dev log bounded while the IDE editor can still read it comfortably. */
|
|
18
|
+
export declare const DEFAULT_LOG_MAX_BYTES: number;
|
|
19
|
+
export declare const DEFAULT_LOG_KEEP = 5;
|
|
20
|
+
export declare const DEFAULT_LOG_DIR_SEGMENTS: readonly [".facilis", "logs"];
|
|
21
|
+
export interface LogSinkSelection {
|
|
22
|
+
readonly stdout: boolean;
|
|
23
|
+
readonly file: boolean;
|
|
24
|
+
}
|
|
25
|
+
export interface ResolvedLogSettings {
|
|
26
|
+
/** The configured mode, kept for diagnostics (`auto` stays `auto`). */
|
|
27
|
+
readonly mode: LogSinkMode;
|
|
28
|
+
readonly sinks: LogSinkSelection;
|
|
29
|
+
/** Absolute directory of the JSONL files (only used when `sinks.file` is true). */
|
|
30
|
+
readonly dir: string;
|
|
31
|
+
readonly maxBytes: number;
|
|
32
|
+
readonly keep: number;
|
|
33
|
+
/** Environment override for `logging.level`; null keeps the project config. */
|
|
34
|
+
readonly level: LogLevel | null;
|
|
35
|
+
}
|
|
36
|
+
/** Shaped like `ManifestWarning` so the bootstrap can forward it unchanged. */
|
|
37
|
+
export interface LogSettingsWarning {
|
|
38
|
+
readonly code: string;
|
|
39
|
+
readonly message: string;
|
|
40
|
+
}
|
|
41
|
+
export interface ResolvedLogSettingsResult {
|
|
42
|
+
readonly settings: ResolvedLogSettings;
|
|
43
|
+
readonly warnings: readonly LogSettingsWarning[];
|
|
44
|
+
}
|
|
45
|
+
export interface ResolveLogSettingsInput {
|
|
46
|
+
readonly env: NodeJS.ProcessEnv;
|
|
47
|
+
readonly projectRoot: string;
|
|
48
|
+
/** Test seam: override the NODE_ENV-derived production detection. */
|
|
49
|
+
readonly production?: boolean;
|
|
50
|
+
}
|
|
51
|
+
export declare function resolveLogSettings(input: ResolveLogSettingsInput): ResolvedLogSettingsResult;
|
|
52
|
+
export declare function isLogSinkMode(value: string): value is LogSinkMode;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { resolve as resolvePath } from "node:path";
|
|
2
|
+
import { isLogLevel } from "@facilis.dev/core";
|
|
3
|
+
import { isProductionEnvironment } from "../env.js";
|
|
4
|
+
/**
|
|
5
|
+
* Log destination resolution (D-119, SPEC §8.7).
|
|
6
|
+
*
|
|
7
|
+
* Where entries go is a deployment concern, so it comes from the environment and never from
|
|
8
|
+
* `facilis.config.ts`: a generated project must not be able to redirect an operator's audit
|
|
9
|
+
* trail. Every invalid value falls back to its documented default and yields a warning, so a
|
|
10
|
+
* typo degrades the sink instead of breaking the boot.
|
|
11
|
+
*/
|
|
12
|
+
export const LOG_SINK_MODES = ["auto", "stdout", "file", "both"];
|
|
13
|
+
export const LOG_SINK_ENV = "FACILIS_LOG_SINK";
|
|
14
|
+
export const LOG_DIR_ENV = "FACILIS_LOG_DIR";
|
|
15
|
+
export const LOG_MAX_BYTES_ENV = "FACILIS_LOG_MAX_BYTES";
|
|
16
|
+
export const LOG_KEEP_ENV = "FACILIS_LOG_KEEP";
|
|
17
|
+
export const LOG_LEVEL_ENV = "FACILIS_LOG_LEVEL";
|
|
18
|
+
/** 10 MiB keeps a dev log bounded while the IDE editor can still read it comfortably. */
|
|
19
|
+
export const DEFAULT_LOG_MAX_BYTES = 10 * 1024 * 1024;
|
|
20
|
+
export const DEFAULT_LOG_KEEP = 5;
|
|
21
|
+
export const DEFAULT_LOG_DIR_SEGMENTS = [".facilis", "logs"];
|
|
22
|
+
const MAX_LOG_MAX_BYTES = 1024 * 1024 * 1024;
|
|
23
|
+
const MAX_LOG_KEEP = 1000;
|
|
24
|
+
export function resolveLogSettings(input) {
|
|
25
|
+
const warnings = [];
|
|
26
|
+
const production = input.production ?? isProductionEnvironment(input.env);
|
|
27
|
+
const mode = parseSinkMode(readEnv(input.env, LOG_SINK_ENV), warnings);
|
|
28
|
+
const dirValue = readEnv(input.env, LOG_DIR_ENV);
|
|
29
|
+
const dir = dirValue === undefined
|
|
30
|
+
? resolvePath(input.projectRoot, ...DEFAULT_LOG_DIR_SEGMENTS)
|
|
31
|
+
: resolvePath(input.projectRoot, dirValue);
|
|
32
|
+
const maxBytes = parseBoundedInteger(input.env, LOG_MAX_BYTES_ENV, DEFAULT_LOG_MAX_BYTES, 1, MAX_LOG_MAX_BYTES, warnings);
|
|
33
|
+
const keep = parseBoundedInteger(input.env, LOG_KEEP_ENV, DEFAULT_LOG_KEEP, 0, MAX_LOG_KEEP, warnings);
|
|
34
|
+
const level = parseLevel(input.env, warnings);
|
|
35
|
+
return {
|
|
36
|
+
settings: { mode, sinks: sinkSelection(mode, production), dir, maxBytes, keep, level },
|
|
37
|
+
warnings,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export function isLogSinkMode(value) {
|
|
41
|
+
return LOG_SINK_MODES.includes(value);
|
|
42
|
+
}
|
|
43
|
+
function sinkSelection(mode, production) {
|
|
44
|
+
switch (mode) {
|
|
45
|
+
case "stdout":
|
|
46
|
+
return { stdout: true, file: false };
|
|
47
|
+
case "file":
|
|
48
|
+
return { stdout: false, file: true };
|
|
49
|
+
case "both":
|
|
50
|
+
return { stdout: true, file: true };
|
|
51
|
+
case "auto":
|
|
52
|
+
// Dev is where the IDE workflow lives: files need no setup. Production defaults to
|
|
53
|
+
// stdout because a deploy without writable disk still has a log platform.
|
|
54
|
+
return production ? { stdout: true, file: false } : { stdout: false, file: true };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function parseSinkMode(raw, warnings) {
|
|
58
|
+
if (raw === undefined)
|
|
59
|
+
return "auto";
|
|
60
|
+
if (!isLogSinkMode(raw)) {
|
|
61
|
+
warnings.push(warning(`${LOG_SINK_ENV}="${raw}" is not one of ${LOG_SINK_MODES.join("|")}; using "auto".`));
|
|
62
|
+
return "auto";
|
|
63
|
+
}
|
|
64
|
+
return raw;
|
|
65
|
+
}
|
|
66
|
+
function parseBoundedInteger(env, name, fallback, min, max, warnings) {
|
|
67
|
+
const raw = readEnv(env, name);
|
|
68
|
+
if (raw === undefined)
|
|
69
|
+
return fallback;
|
|
70
|
+
const value = Number(raw);
|
|
71
|
+
if (!Number.isSafeInteger(value) || value < min || value > max) {
|
|
72
|
+
warnings.push(warning(`${name}="${raw}" is not an integer in [${min}, ${max}]; using ${fallback}.`));
|
|
73
|
+
return fallback;
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
function parseLevel(env, warnings) {
|
|
78
|
+
const raw = readEnv(env, LOG_LEVEL_ENV);
|
|
79
|
+
if (raw === undefined)
|
|
80
|
+
return null;
|
|
81
|
+
if (!isLogLevel(raw)) {
|
|
82
|
+
warnings.push(warning(`${LOG_LEVEL_ENV}="${raw}" is not a log level; keeping logging.level.`));
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
return raw;
|
|
86
|
+
}
|
|
87
|
+
function readEnv(env, name) {
|
|
88
|
+
const value = env[name]?.trim();
|
|
89
|
+
return value === undefined || value.length === 0 ? undefined : value;
|
|
90
|
+
}
|
|
91
|
+
function warning(message) {
|
|
92
|
+
return { code: "logging_env_invalid", message };
|
|
93
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { LogEntry } from "@facilis.dev/core";
|
|
2
|
+
/**
|
|
3
|
+
* A destination for one serialized log entry (D-119).
|
|
4
|
+
*
|
|
5
|
+
* `append` throws when the destination is not writable; the runtime decides whether that is
|
|
6
|
+
* fatal (audit) or best-effort (access/error/app).
|
|
7
|
+
*/
|
|
8
|
+
export declare const LOG_STREAMS: readonly ["access", "audit", "error", "app"];
|
|
9
|
+
export type LogStream = (typeof LOG_STREAMS)[number];
|
|
10
|
+
export interface LogSink {
|
|
11
|
+
append(stream: LogStream, entry: LogEntry): void;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* One JSON object per line on stdout, for the deploy's log platform. Stdout audit lines stay
|
|
15
|
+
* plain entries (no chain fields): the hash chain belongs to the file record, WORM/retention
|
|
16
|
+
* is the platform's job (O-014).
|
|
17
|
+
*/
|
|
18
|
+
export declare function createStdoutLogSink(write?: (line: string) => void): LogSink;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A destination for one serialized log entry (D-119).
|
|
3
|
+
*
|
|
4
|
+
* `append` throws when the destination is not writable; the runtime decides whether that is
|
|
5
|
+
* fatal (audit) or best-effort (access/error/app).
|
|
6
|
+
*/
|
|
7
|
+
export const LOG_STREAMS = ["access", "audit", "error", "app"];
|
|
8
|
+
/**
|
|
9
|
+
* One JSON object per line on stdout, for the deploy's log platform. Stdout audit lines stay
|
|
10
|
+
* plain entries (no chain fields): the hash chain belongs to the file record, WORM/retention
|
|
11
|
+
* is the platform's job (O-014).
|
|
12
|
+
*/
|
|
13
|
+
export function createStdoutLogSink(write = writeToStdout) {
|
|
14
|
+
return {
|
|
15
|
+
append(_stream, entry) {
|
|
16
|
+
write(`${JSON.stringify(entry)}\n`);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function writeToStdout(line) {
|
|
21
|
+
process.stdout.write(line);
|
|
22
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redaction and caps for logger-provided data (D-119).
|
|
3
|
+
*
|
|
4
|
+
* `data`/`details` is the only arbitrary payload a route can hand the pipeline, so it is the
|
|
5
|
+
* one place a secret could leak. Keys are matched case-insensitively against the names a
|
|
6
|
+
* credential commonly travels under; values are additionally capped so one huge or deeply
|
|
7
|
+
* nested object cannot blow up a JSONL line. Headers and bodies are never passed in here in
|
|
8
|
+
* the first place: the pipeline logs shapes, not requests.
|
|
9
|
+
*/
|
|
10
|
+
export declare const REDACTED = "[redacted]";
|
|
11
|
+
export declare const CIRCULAR = "[circular]";
|
|
12
|
+
export declare const MAX_DEPTH_PLACEHOLDER = "[max-depth]";
|
|
13
|
+
export declare const UNSERIALIZABLE = "[unserializable]";
|
|
14
|
+
export declare const MAX_REDACT_DEPTH = 6;
|
|
15
|
+
export declare const MAX_REDACT_ARRAY = 50;
|
|
16
|
+
export declare const MAX_REDACT_STRING = 1000;
|
|
17
|
+
/** True when a key names something that must never reach the sink in clear text. */
|
|
18
|
+
export declare function isSensitiveKey(key: string): boolean;
|
|
19
|
+
/** Truncates a string to `max` characters; never appends a marker (it would exceed the cap). */
|
|
20
|
+
export declare function capString(value: string, max?: number): string;
|
|
21
|
+
/**
|
|
22
|
+
* Recursively redacts and caps an arbitrary value.
|
|
23
|
+
*
|
|
24
|
+
* Accepted output types are the JSON ones (plus stringified bigints and ISO dates); anything
|
|
25
|
+
* else is replaced with a placeholder rather than serialized by an arbitrary method. Cycles
|
|
26
|
+
* become `[circular]`, cycles are detected along the current path only, so a shared value is
|
|
27
|
+
* emitted twice instead of being mistaken for a cycle.
|
|
28
|
+
*/
|
|
29
|
+
export declare function redactUnknown(value: unknown): unknown;
|
|
30
|
+
/** Redacts a `data`/`details` record; returns undefined when the value is not a plain object. */
|
|
31
|
+
export declare function redactRecord(value: Record<string, unknown> | undefined): Record<string, unknown> | undefined;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redaction and caps for logger-provided data (D-119).
|
|
3
|
+
*
|
|
4
|
+
* `data`/`details` is the only arbitrary payload a route can hand the pipeline, so it is the
|
|
5
|
+
* one place a secret could leak. Keys are matched case-insensitively against the names a
|
|
6
|
+
* credential commonly travels under; values are additionally capped so one huge or deeply
|
|
7
|
+
* nested object cannot blow up a JSONL line. Headers and bodies are never passed in here in
|
|
8
|
+
* the first place: the pipeline logs shapes, not requests.
|
|
9
|
+
*/
|
|
10
|
+
export const REDACTED = "[redacted]";
|
|
11
|
+
export const CIRCULAR = "[circular]";
|
|
12
|
+
export const MAX_DEPTH_PLACEHOLDER = "[max-depth]";
|
|
13
|
+
export const UNSERIALIZABLE = "[unserializable]";
|
|
14
|
+
export const MAX_REDACT_DEPTH = 6;
|
|
15
|
+
export const MAX_REDACT_ARRAY = 50;
|
|
16
|
+
export const MAX_REDACT_STRING = 1000;
|
|
17
|
+
const SENSITIVE_KEY_PATTERN = /token|secret|password|cookie|authorization|set-cookie|api[-_]?key|credential|samlresponse|samlrequest|assertion|email|sessionid/i;
|
|
18
|
+
/** True when a key names something that must never reach the sink in clear text. */
|
|
19
|
+
export function isSensitiveKey(key) {
|
|
20
|
+
return SENSITIVE_KEY_PATTERN.test(key);
|
|
21
|
+
}
|
|
22
|
+
/** Truncates a string to `max` characters; never appends a marker (it would exceed the cap). */
|
|
23
|
+
export function capString(value, max = MAX_REDACT_STRING) {
|
|
24
|
+
return value.length <= max ? value : value.slice(0, max);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Recursively redacts and caps an arbitrary value.
|
|
28
|
+
*
|
|
29
|
+
* Accepted output types are the JSON ones (plus stringified bigints and ISO dates); anything
|
|
30
|
+
* else is replaced with a placeholder rather than serialized by an arbitrary method. Cycles
|
|
31
|
+
* become `[circular]`, cycles are detected along the current path only, so a shared value is
|
|
32
|
+
* emitted twice instead of being mistaken for a cycle.
|
|
33
|
+
*/
|
|
34
|
+
export function redactUnknown(value) {
|
|
35
|
+
return redact(value, 0, new WeakSet());
|
|
36
|
+
}
|
|
37
|
+
/** Redacts a `data`/`details` record; returns undefined when the value is not a plain object. */
|
|
38
|
+
export function redactRecord(value) {
|
|
39
|
+
if (value === undefined)
|
|
40
|
+
return undefined;
|
|
41
|
+
const redacted = redactUnknown(value);
|
|
42
|
+
return isPlainObject(redacted) ? redacted : undefined;
|
|
43
|
+
}
|
|
44
|
+
function redact(value, depth, seen) {
|
|
45
|
+
if (value === null || typeof value === "boolean" || typeof value === "number")
|
|
46
|
+
return value;
|
|
47
|
+
if (typeof value === "string")
|
|
48
|
+
return capString(value);
|
|
49
|
+
if (typeof value === "bigint")
|
|
50
|
+
return value.toString();
|
|
51
|
+
if (value === undefined || typeof value === "symbol" || typeof value === "function") {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
if (typeof value !== "object")
|
|
55
|
+
return UNSERIALIZABLE;
|
|
56
|
+
if (value instanceof Date)
|
|
57
|
+
return value.toISOString();
|
|
58
|
+
if (seen.has(value))
|
|
59
|
+
return CIRCULAR;
|
|
60
|
+
if (depth >= MAX_REDACT_DEPTH)
|
|
61
|
+
return MAX_DEPTH_PLACEHOLDER;
|
|
62
|
+
seen.add(value);
|
|
63
|
+
try {
|
|
64
|
+
if (Array.isArray(value)) {
|
|
65
|
+
return value.slice(0, MAX_REDACT_ARRAY).map((item) => redact(item, depth + 1, seen));
|
|
66
|
+
}
|
|
67
|
+
if (value instanceof Error) {
|
|
68
|
+
return {
|
|
69
|
+
name: capString(value.name),
|
|
70
|
+
message: capString(value.message),
|
|
71
|
+
...(value.stack === undefined ? {} : { stack: capString(value.stack) }),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (value instanceof Map) {
|
|
75
|
+
const entries = Array.from(value, ([key, item]) => [String(key), item]);
|
|
76
|
+
return redact(Object.fromEntries(entries), depth + 1, seen);
|
|
77
|
+
}
|
|
78
|
+
if (value instanceof Set) {
|
|
79
|
+
return redact([...value], depth + 1, seen);
|
|
80
|
+
}
|
|
81
|
+
if (!isPlainObject(value))
|
|
82
|
+
return UNSERIALIZABLE;
|
|
83
|
+
const redacted = {};
|
|
84
|
+
for (const [key, item] of Object.entries(value)) {
|
|
85
|
+
if (isSensitiveKey(key)) {
|
|
86
|
+
redacted[key] = REDACTED;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const next = redact(item, depth + 1, seen);
|
|
90
|
+
// `undefined` is dropped: JSON.stringify would drop it too, and keeping it would make
|
|
91
|
+
// the output shape depend on the serializer.
|
|
92
|
+
if (next !== undefined)
|
|
93
|
+
redacted[key] = next;
|
|
94
|
+
}
|
|
95
|
+
return redacted;
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
seen.delete(value);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function isPlainObject(value) {
|
|
102
|
+
if (value === null || typeof value !== "object")
|
|
103
|
+
return false;
|
|
104
|
+
const prototype = Object.getPrototypeOf(value);
|
|
105
|
+
return prototype === Object.prototype || prototype === null;
|
|
106
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type FacilisLoggingApi, type FacilisLoggingConfig } from "@facilis.dev/core";
|
|
2
|
+
import type { LogSettingsWarning, ResolvedLogSettings } from "./log-settings.js";
|
|
3
|
+
/**
|
|
4
|
+
* The logging runtime behind `event.context.facilis.logging` (D-119).
|
|
5
|
+
*
|
|
6
|
+
* `record` is the one pipeline: it filters by the project's logging config, redacts/caps and
|
|
7
|
+
* writes to the env-selected sinks. Audit is never filtered and never best-effort: a failed
|
|
8
|
+
* audit write throws so sudo refuses to escalate (D-099), while a broken access/error/app sink
|
|
9
|
+
* only drops entries and reports once.
|
|
10
|
+
*/
|
|
11
|
+
export interface LoggingRuntimeOptions {
|
|
12
|
+
readonly config: FacilisLoggingConfig;
|
|
13
|
+
readonly settings: ResolvedLogSettings;
|
|
14
|
+
/** Test/deployment seam: collect stdout lines instead of writing to the process stream. */
|
|
15
|
+
readonly writeStdout?: (line: string) => void;
|
|
16
|
+
/** Test seam: fixed clock. */
|
|
17
|
+
readonly now?: () => Date;
|
|
18
|
+
}
|
|
19
|
+
export interface LoggingRuntime extends FacilisLoggingApi {
|
|
20
|
+
readonly config: FacilisLoggingConfig;
|
|
21
|
+
readonly settings: ResolvedLogSettings;
|
|
22
|
+
/** Non-fatal setup problems the bootstrap forwards to the manifest. */
|
|
23
|
+
readonly warnings: readonly LogSettingsWarning[];
|
|
24
|
+
}
|
|
25
|
+
export declare function createLoggingRuntime(options: LoggingRuntimeOptions): LoggingRuntime;
|