@smthrs/journal 0.0.0-stage → 1.0.0-rc.3
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/CHANGELOG.md +149 -0
- package/LICENSE +21 -0
- package/README.md +206 -2
- package/dist/cjs/CallFact.d.ts +129 -0
- package/dist/cjs/CallFact.d.ts.map +1 -0
- package/dist/cjs/CallFact.js +106 -0
- package/dist/cjs/CallFact.js.map +7 -0
- package/dist/cjs/Consensus.d.ts +440 -0
- package/dist/cjs/Consensus.d.ts.map +1 -0
- package/dist/cjs/Consensus.js +251 -0
- package/dist/cjs/Consensus.js.map +7 -0
- package/dist/cjs/EngineEvent.d.ts +890 -0
- package/dist/cjs/EngineEvent.d.ts.map +1 -0
- package/dist/cjs/EngineEvent.js +383 -0
- package/dist/cjs/EngineEvent.js.map +7 -0
- package/dist/cjs/ExecutionFact.d.ts +216 -0
- package/dist/cjs/ExecutionFact.d.ts.map +1 -0
- package/dist/cjs/ExecutionFact.js +303 -0
- package/dist/cjs/ExecutionFact.js.map +7 -0
- package/dist/cjs/Journal.d.ts +617 -0
- package/dist/cjs/Journal.d.ts.map +1 -0
- package/dist/cjs/Journal.js +190 -0
- package/dist/cjs/Journal.js.map +7 -0
- package/dist/cjs/JournalEvent.d.ts +421 -0
- package/dist/cjs/JournalEvent.d.ts.map +1 -0
- package/dist/cjs/JournalEvent.js +281 -0
- package/dist/cjs/JournalEvent.js.map +7 -0
- package/dist/cjs/JournalGeneration.d.ts +34 -0
- package/dist/cjs/JournalGeneration.d.ts.map +1 -0
- package/dist/cjs/JournalGeneration.js +72 -0
- package/dist/cjs/JournalGeneration.js.map +7 -0
- package/dist/cjs/JournalMetrics.d.ts +72 -0
- package/dist/cjs/JournalMetrics.d.ts.map +1 -0
- package/dist/cjs/JournalMetrics.js +55 -0
- package/dist/cjs/JournalMetrics.js.map +7 -0
- package/dist/cjs/Migrations.d.ts +42 -0
- package/dist/cjs/Migrations.d.ts.map +1 -0
- package/dist/cjs/Migrations.js +58 -0
- package/dist/cjs/Migrations.js.map +7 -0
- package/dist/cjs/OwnerId.d.ts +40 -0
- package/dist/cjs/OwnerId.d.ts.map +1 -0
- package/dist/cjs/OwnerId.js +40 -0
- package/dist/cjs/OwnerId.js.map +7 -0
- package/dist/cjs/Projection.d.ts +29 -0
- package/dist/cjs/Projection.d.ts.map +1 -0
- package/dist/cjs/Projection.js +25 -0
- package/dist/cjs/Projection.js.map +7 -0
- package/dist/cjs/RedactedLogger.d.ts +121 -0
- package/dist/cjs/RedactedLogger.d.ts.map +1 -0
- package/dist/cjs/RedactedLogger.js +235 -0
- package/dist/cjs/RedactedLogger.js.map +7 -0
- package/dist/cjs/Redaction.d.ts +350 -0
- package/dist/cjs/Redaction.d.ts.map +1 -0
- package/dist/cjs/Redaction.js +928 -0
- package/dist/cjs/Redaction.js.map +7 -0
- package/dist/cjs/SqlConsensus.d.ts +44 -0
- package/dist/cjs/SqlConsensus.d.ts.map +1 -0
- package/dist/cjs/SqlConsensus.js +243 -0
- package/dist/cjs/SqlConsensus.js.map +7 -0
- package/dist/cjs/SqlJournal.d.ts +173 -0
- package/dist/cjs/SqlJournal.d.ts.map +1 -0
- package/dist/cjs/SqlJournal.js +1568 -0
- package/dist/cjs/SqlJournal.js.map +7 -0
- package/dist/cjs/StepFact.d.ts +66 -0
- package/dist/cjs/StepFact.d.ts.map +1 -0
- package/dist/cjs/StepFact.js +68 -0
- package/dist/cjs/StepFact.js.map +7 -0
- package/dist/cjs/index.d.ts +95 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +63 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/internal/JournalInternalFault.d.ts +37 -0
- package/dist/cjs/internal/JournalInternalFault.d.ts.map +1 -0
- package/dist/cjs/internal/JournalInternalFault.js +43 -0
- package/dist/cjs/internal/JournalInternalFault.js.map +7 -0
- package/dist/cjs/migrations/0001_initial.d.ts +15 -0
- package/dist/cjs/migrations/0001_initial.d.ts.map +1 -0
- package/dist/cjs/migrations/0001_initial.js +54 -0
- package/dist/cjs/migrations/0001_initial.js.map +7 -0
- package/dist/cjs/migrations/0002_checkpoints.d.ts +27 -0
- package/dist/cjs/migrations/0002_checkpoints.d.ts.map +1 -0
- package/dist/cjs/migrations/0002_checkpoints.js +48 -0
- package/dist/cjs/migrations/0002_checkpoints.js.map +7 -0
- package/dist/cjs/migrations/0003_startup_index.d.ts +15 -0
- package/dist/cjs/migrations/0003_startup_index.d.ts.map +1 -0
- package/dist/cjs/migrations/0003_startup_index.js +40 -0
- package/dist/cjs/migrations/0003_startup_index.js.map +7 -0
- package/dist/cjs/migrations/0004_dedup.d.ts +15 -0
- package/dist/cjs/migrations/0004_dedup.d.ts.map +1 -0
- package/dist/cjs/migrations/0004_dedup.js +57 -0
- package/dist/cjs/migrations/0004_dedup.js.map +7 -0
- package/dist/cjs/migrations/0005_run_event_type.d.ts +15 -0
- package/dist/cjs/migrations/0005_run_event_type.d.ts.map +1 -0
- package/dist/cjs/migrations/0005_run_event_type.js +40 -0
- package/dist/cjs/migrations/0005_run_event_type.js.map +7 -0
- package/dist/cjs/migrations/0006_consensus.d.ts +22 -0
- package/dist/cjs/migrations/0006_consensus.d.ts.map +1 -0
- package/dist/cjs/migrations/0006_consensus.js +84 -0
- package/dist/cjs/migrations/0006_consensus.js.map +7 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/test/Notifying.d.ts +65 -0
- package/dist/cjs/test/Notifying.d.ts.map +1 -0
- package/dist/cjs/test/Notifying.js +62 -0
- package/dist/cjs/test/Notifying.js.map +7 -0
- package/dist/cjs/test/TestJournal.d.ts +43 -0
- package/dist/cjs/test/TestJournal.d.ts.map +1 -0
- package/dist/cjs/test/TestJournal.js +43 -0
- package/dist/cjs/test/TestJournal.js.map +7 -0
- package/dist/esm/CallFact.d.ts +129 -0
- package/dist/esm/CallFact.d.ts.map +1 -0
- package/dist/esm/CallFact.js +96 -0
- package/dist/esm/CallFact.js.map +1 -0
- package/dist/esm/Consensus.d.ts +440 -0
- package/dist/esm/Consensus.d.ts.map +1 -0
- package/dist/esm/Consensus.js +454 -0
- package/dist/esm/Consensus.js.map +1 -0
- package/dist/esm/EngineEvent.d.ts +890 -0
- package/dist/esm/EngineEvent.d.ts.map +1 -0
- package/dist/esm/EngineEvent.js +614 -0
- package/dist/esm/EngineEvent.js.map +1 -0
- package/dist/esm/ExecutionFact.d.ts +216 -0
- package/dist/esm/ExecutionFact.d.ts.map +1 -0
- package/dist/esm/ExecutionFact.js +374 -0
- package/dist/esm/ExecutionFact.js.map +1 -0
- package/dist/esm/Journal.d.ts +617 -0
- package/dist/esm/Journal.d.ts.map +1 -0
- package/dist/esm/Journal.js +323 -0
- package/dist/esm/Journal.js.map +1 -0
- package/dist/esm/JournalEvent.d.ts +421 -0
- package/dist/esm/JournalEvent.d.ts.map +1 -0
- package/dist/esm/JournalEvent.js +517 -0
- package/dist/esm/JournalEvent.js.map +1 -0
- package/dist/esm/JournalGeneration.d.ts +34 -0
- package/dist/esm/JournalGeneration.d.ts.map +1 -0
- package/dist/esm/JournalGeneration.js +65 -0
- package/dist/esm/JournalGeneration.js.map +1 -0
- package/dist/esm/JournalMetrics.d.ts +72 -0
- package/dist/esm/JournalMetrics.d.ts.map +1 -0
- package/dist/esm/JournalMetrics.js +79 -0
- package/dist/esm/JournalMetrics.js.map +1 -0
- package/dist/esm/Migrations.d.ts +42 -0
- package/dist/esm/Migrations.d.ts.map +1 -0
- package/dist/esm/Migrations.js +59 -0
- package/dist/esm/Migrations.js.map +1 -0
- package/dist/esm/OwnerId.d.ts +40 -0
- package/dist/esm/OwnerId.d.ts.map +1 -0
- package/dist/esm/OwnerId.js +33 -0
- package/dist/esm/OwnerId.js.map +1 -0
- package/dist/esm/Projection.d.ts +29 -0
- package/dist/esm/Projection.d.ts.map +1 -0
- package/dist/esm/Projection.js +13 -0
- package/dist/esm/Projection.js.map +1 -0
- package/dist/esm/RedactedLogger.d.ts +121 -0
- package/dist/esm/RedactedLogger.d.ts.map +1 -0
- package/dist/esm/RedactedLogger.js +445 -0
- package/dist/esm/RedactedLogger.js.map +1 -0
- package/dist/esm/Redaction.d.ts +350 -0
- package/dist/esm/Redaction.d.ts.map +1 -0
- package/dist/esm/Redaction.js +1584 -0
- package/dist/esm/Redaction.js.map +1 -0
- package/dist/esm/SqlConsensus.d.ts +44 -0
- package/dist/esm/SqlConsensus.d.ts.map +1 -0
- package/dist/esm/SqlConsensus.js +252 -0
- package/dist/esm/SqlConsensus.js.map +1 -0
- package/dist/esm/SqlJournal.d.ts +173 -0
- package/dist/esm/SqlJournal.d.ts.map +1 -0
- package/dist/esm/SqlJournal.js +1858 -0
- package/dist/esm/SqlJournal.js.map +1 -0
- package/dist/esm/StepFact.d.ts +66 -0
- package/dist/esm/StepFact.d.ts.map +1 -0
- package/dist/esm/StepFact.js +50 -0
- package/dist/esm/StepFact.js.map +1 -0
- package/dist/esm/index.d.ts +95 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +95 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/internal/JournalInternalFault.d.ts +37 -0
- package/dist/esm/internal/JournalInternalFault.d.ts.map +1 -0
- package/dist/esm/internal/JournalInternalFault.js +28 -0
- package/dist/esm/internal/JournalInternalFault.js.map +1 -0
- package/dist/esm/migrations/0001_initial.d.ts +15 -0
- package/dist/esm/migrations/0001_initial.d.ts.map +1 -0
- package/dist/esm/migrations/0001_initial.js +32 -0
- package/dist/esm/migrations/0001_initial.js.map +1 -0
- package/dist/esm/migrations/0002_checkpoints.d.ts +27 -0
- package/dist/esm/migrations/0002_checkpoints.d.ts.map +1 -0
- package/dist/esm/migrations/0002_checkpoints.js +38 -0
- package/dist/esm/migrations/0002_checkpoints.js.map +1 -0
- package/dist/esm/migrations/0003_startup_index.d.ts +15 -0
- package/dist/esm/migrations/0003_startup_index.d.ts.map +1 -0
- package/dist/esm/migrations/0003_startup_index.js +18 -0
- package/dist/esm/migrations/0003_startup_index.js.map +1 -0
- package/dist/esm/migrations/0004_dedup.d.ts +15 -0
- package/dist/esm/migrations/0004_dedup.d.ts.map +1 -0
- package/dist/esm/migrations/0004_dedup.js +38 -0
- package/dist/esm/migrations/0004_dedup.js.map +1 -0
- package/dist/esm/migrations/0005_run_event_type.d.ts +15 -0
- package/dist/esm/migrations/0005_run_event_type.d.ts.map +1 -0
- package/dist/esm/migrations/0005_run_event_type.js +18 -0
- package/dist/esm/migrations/0005_run_event_type.js.map +1 -0
- package/dist/esm/migrations/0006_consensus.d.ts +22 -0
- package/dist/esm/migrations/0006_consensus.d.ts.map +1 -0
- package/dist/esm/migrations/0006_consensus.js +69 -0
- package/dist/esm/migrations/0006_consensus.js.map +1 -0
- package/dist/esm/test/Notifying.d.ts +65 -0
- package/dist/esm/test/Notifying.d.ts.map +1 -0
- package/dist/esm/test/Notifying.js +62 -0
- package/dist/esm/test/Notifying.js.map +1 -0
- package/dist/esm/test/TestJournal.d.ts +43 -0
- package/dist/esm/test/TestJournal.d.ts.map +1 -0
- package/dist/esm/test/TestJournal.js +27 -0
- package/dist/esm/test/TestJournal.js.map +1 -0
- package/docs/README.md +164 -0
- package/docs/api.md +639 -0
- package/docs/concepts/compaction.md +157 -0
- package/docs/concepts/idempotency.md +127 -0
- package/docs/concepts/owner-fence.md +124 -0
- package/docs/concepts/redaction.md +218 -0
- package/docs/concepts/state-event-authority.md +159 -0
- package/docs/concepts/two-channels.md +167 -0
- package/docs/guides/commit-state-and-entry.md +114 -0
- package/docs/guides/compact-a-run.md +134 -0
- package/docs/guides/fold-a-projection.md +106 -0
- package/docs/guides/read-a-run.md +139 -0
- package/docs/guides/redact-log-output.md +94 -0
- package/docs/guides/testing.md +124 -0
- package/docs/guides/write-lifecycle-events.md +110 -0
- package/docs/installation.md +133 -0
- package/docs/quickstart.md +143 -0
- package/docs/troubleshooting.md +214 -0
- package/package.json +263 -3
- package/src/CallFact.ts +117 -0
- package/src/Consensus.ts +683 -0
- package/src/EngineEvent.ts +823 -0
- package/src/ExecutionFact.ts +433 -0
- package/src/Journal.ts +688 -0
- package/src/JournalEvent.ts +654 -0
- package/src/JournalGeneration.ts +77 -0
- package/src/JournalMetrics.ts +89 -0
- package/src/Migrations.ts +62 -0
- package/src/OwnerId.ts +42 -0
- package/src/Projection.ts +31 -0
- package/src/RedactedLogger.ts +483 -0
- package/src/Redaction.ts +1784 -0
- package/src/SqlConsensus.ts +344 -0
- package/src/SqlJournal.ts +2732 -0
- package/src/StepFact.ts +69 -0
- package/src/index.ts +109 -0
- package/src/internal/JournalInternalFault.ts +40 -0
- package/src/migrations/0001_initial.ts +41 -0
- package/src/migrations/0002_checkpoints.ts +46 -0
- package/src/migrations/0003_startup_index.ts +19 -0
- package/src/migrations/0004_dedup.ts +43 -0
- package/src/migrations/0005_run_event_type.ts +19 -0
- package/src/migrations/0006_consensus.ts +83 -0
- package/src/test/Notifying.ts +101 -0
- package/src/test/TestJournal.ts +52 -0
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The redacting logger: the journal's redaction rules applied to log output.
|
|
3
|
+
*
|
|
4
|
+
* The journal redacts on the write path, so a credential a cell logs or
|
|
5
|
+
* returns never reaches a committed row (`./Redaction.ts`). The operator's
|
|
6
|
+
* terminal was the other place the same credential surfaces: every built-in
|
|
7
|
+
* Effect logger renders through the fiber's `Console`, so an action that hands
|
|
8
|
+
* a token to `Effect.logInfo` wrote it to stderr in full and it reached
|
|
9
|
+
* whatever collected that stream.
|
|
10
|
+
*
|
|
11
|
+
* This module closes that half with the SAME rules rather than a second rule
|
|
12
|
+
* set. {@link wrap} does not re-implement redaction and does not re-format a
|
|
13
|
+
* log line. It redacts the log event itself — the message, the cause, and the
|
|
14
|
+
* log annotations — before the wrapped logger is handed it, and it hands that
|
|
15
|
+
* logger a view of the fiber whose `Console` is a redacting console, so
|
|
16
|
+
* whatever the logger renders for itself — a pretty line, a JSON document —
|
|
17
|
+
* passes through `Redaction.redact` on its way to the stream as well.
|
|
18
|
+
*
|
|
19
|
+
* Both halves carry load. Most built-in loggers write through the fiber's
|
|
20
|
+
* `Console`, which is what covers the pretty line, the `--json` document, and
|
|
21
|
+
* the `.flows/logs/<runId>.log` stream. But a logger is free to read the event
|
|
22
|
+
* instead of rendering it, and one in Effect's own default logger set does:
|
|
23
|
+
* `Logger.tracerLogger` publishes the message as a span event name and
|
|
24
|
+
* `Cause.pretty` of the cause as the span's `effect.cause` attribute, and
|
|
25
|
+
* never reads `Console` at all. Redacting only the console would hide a
|
|
26
|
+
* credential on stderr and export it in clear to whatever OTLP collector is
|
|
27
|
+
* configured.
|
|
28
|
+
*
|
|
29
|
+
* Cost is bounded by the rules themselves: every alternation inside a
|
|
30
|
+
* repetition in `Redaction.diagnosticRules` starts its branches with disjoint
|
|
31
|
+
* characters, so each rule is linear in the length of the line and no input
|
|
32
|
+
* backtracks catastrophically.
|
|
33
|
+
*
|
|
34
|
+
* One deliberate difference from the journal write path. `Redaction.redact`
|
|
35
|
+
* rebuilds an object from its own enumerable entries, which is right for a row
|
|
36
|
+
* that is about to be JSON-encoded and wrong for a terminal: an `Error` keeps
|
|
37
|
+
* `message` and `stack` on non-enumerable properties, so rebuilding one prints
|
|
38
|
+
* `{}` where the operator expected a failure. An `Error` is rebuilt as a plain
|
|
39
|
+
* `Error` instead, carrying its name, message, stack and own members with the
|
|
40
|
+
* rules applied. Its class is NOT preserved. A copy built on the original's
|
|
41
|
+
* prototype inherits everything the prototype defines, an own-key walk sees
|
|
42
|
+
* none of it, and each of `name`, `cause`, `toJSON`, `Symbol.toStringTag` and
|
|
43
|
+
* `nodejs.util.inspect.custom` carried a credential out that way.
|
|
44
|
+
*
|
|
45
|
+
* @since 0.1.0
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
import * as Cause from "effect/Cause"
|
|
49
|
+
import * as Console from "effect/Console"
|
|
50
|
+
import * as Context from "effect/Context"
|
|
51
|
+
import * as Effect from "effect/Effect"
|
|
52
|
+
import type * as Fiber from "effect/Fiber"
|
|
53
|
+
import * as Layer from "effect/Layer"
|
|
54
|
+
import * as Logger from "effect/Logger"
|
|
55
|
+
import * as References from "effect/References"
|
|
56
|
+
import * as Redaction from "./Redaction.ts"
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Marks a logger this module already wrapped.
|
|
60
|
+
*
|
|
61
|
+
* Both the CLI and the durable runtime install {@link layer}, and a detached
|
|
62
|
+
* `smithers run` is both at once. Wrapping twice would be correct but pay the
|
|
63
|
+
* rules twice per line, so a wrapped logger is returned unchanged.
|
|
64
|
+
*
|
|
65
|
+
* @since 0.1.0
|
|
66
|
+
* @category type ids
|
|
67
|
+
*/
|
|
68
|
+
export const TypeId: unique symbol = Symbol.for("@smthrs/journal/RedactedLogger")
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Whether {@link wrap} already wrapped this logger.
|
|
72
|
+
*
|
|
73
|
+
* @since 0.1.0
|
|
74
|
+
* @category predicates
|
|
75
|
+
*/
|
|
76
|
+
export const isRedacted = (logger: Logger.Logger<any, any>): boolean => TypeId in logger
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Console methods that write caller-supplied values, mapped to whether their
|
|
80
|
+
* arguments carry log output.
|
|
81
|
+
*
|
|
82
|
+
* Only `clear` and `groupEnd` are marked `false`, because those two take no
|
|
83
|
+
* arguments at all: there is nothing for the rules to read and binding them
|
|
84
|
+
* straight through saves a copy per call. Every other method here, `count`,
|
|
85
|
+
* `countReset`, `time`, and `timeEnd` included, accepts a caller-supplied
|
|
86
|
+
* LABEL and prints it, so a label of `api_key=sk-…` reached the terminal
|
|
87
|
+
* through all four of them. `timeLog` already redacted its label, so the pair
|
|
88
|
+
* `time` / `timeLog` also disagreed about what a timer is called; redacting all
|
|
89
|
+
* of them is what makes a label match itself again.
|
|
90
|
+
*/
|
|
91
|
+
const consoleMethods = {
|
|
92
|
+
assert: true,
|
|
93
|
+
clear: false,
|
|
94
|
+
count: true,
|
|
95
|
+
countReset: true,
|
|
96
|
+
debug: true,
|
|
97
|
+
dir: true,
|
|
98
|
+
dirxml: true,
|
|
99
|
+
error: true,
|
|
100
|
+
group: true,
|
|
101
|
+
groupCollapsed: true,
|
|
102
|
+
groupEnd: false,
|
|
103
|
+
info: true,
|
|
104
|
+
log: true,
|
|
105
|
+
table: true,
|
|
106
|
+
time: true,
|
|
107
|
+
timeEnd: true,
|
|
108
|
+
timeLog: true,
|
|
109
|
+
trace: true,
|
|
110
|
+
warn: true
|
|
111
|
+
} as const
|
|
112
|
+
|
|
113
|
+
/** How deep the logger walks a value before it names it instead. */
|
|
114
|
+
const renderDepthLimit = 200
|
|
115
|
+
|
|
116
|
+
/** What the logger renders in place of a value nested past the depth limit. */
|
|
117
|
+
const renderDepthMarker = "[Deep]"
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The parts of a log message.
|
|
121
|
+
*
|
|
122
|
+
* Effect's logging API passes an array, one entry per argument, including for
|
|
123
|
+
* a single argument. A caller that builds `LogOptions` by hand may pass the
|
|
124
|
+
* message itself, so it is wrapped rather than walked.
|
|
125
|
+
*
|
|
126
|
+
* @since 0.1.0
|
|
127
|
+
* @category redaction
|
|
128
|
+
*/
|
|
129
|
+
const toArray = (message: unknown): ReadonlyArray<unknown> => Array.isArray(message) ? message : [message]
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Reads one piece of text off `error` and runs it through the rules.
|
|
133
|
+
*
|
|
134
|
+
* Everything a renderer prints about an error is read here, once, and only the
|
|
135
|
+
* redacted result crosses to the copy. A caller's error can define any of these
|
|
136
|
+
* as an accessor that throws, and reading one happens while a log line is
|
|
137
|
+
* rendering, so a refusal costs that piece of text and nothing else.
|
|
138
|
+
*/
|
|
139
|
+
const redactedText = (read: () => unknown, redactor: Redaction.Redactor, fallback: string): string => {
|
|
140
|
+
try {
|
|
141
|
+
return String(redactor(String(read())))
|
|
142
|
+
} catch {
|
|
143
|
+
return fallback
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** `error.stack` through the rules, or `undefined` when it has none to give. */
|
|
148
|
+
const redactedStack = (error: Error, redactor: Redaction.Redactor): string | undefined => {
|
|
149
|
+
try {
|
|
150
|
+
const stack = error.stack
|
|
151
|
+
// An `Error.stack` is optional. Under `exactOptionalPropertyTypes` an
|
|
152
|
+
// assignment cannot carry the `undefined` a stackless error has, so the
|
|
153
|
+
// copy defines the property instead of assigning it.
|
|
154
|
+
return typeof stack === "string" ? String(redactor(stack)) : undefined
|
|
155
|
+
} catch {
|
|
156
|
+
return undefined
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* A member NAME with the rules applied, replaced outright when one fires.
|
|
162
|
+
*
|
|
163
|
+
* The treatment `Redaction.redact` gives an object key, for the same reason: a
|
|
164
|
+
* name is text a renderer prints. Rewriting one in place would change what it
|
|
165
|
+
* reads as and redaction would stop being a fixed point, so a name that matches
|
|
166
|
+
* becomes the placeholder whole.
|
|
167
|
+
*/
|
|
168
|
+
const redactedName = (key: string, redactor: Redaction.Redactor): string => {
|
|
169
|
+
const redacted = String(redactor(key))
|
|
170
|
+
return redacted === key ? key : Redaction.placeholder
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Own names the copy never carries, because a renderer CALLS them.
|
|
175
|
+
*
|
|
176
|
+
* `name`, `message` and `stack` are defined from the rules above instead. The
|
|
177
|
+
* rest are hooks: a redacted `toString` or `valueOf` would leave a string under
|
|
178
|
+
* a name the language calls, and `String(error)` would then throw from inside
|
|
179
|
+
* the renderer, which is the failure mode this module exists to avoid.
|
|
180
|
+
*/
|
|
181
|
+
const renderHooks: ReadonlySet<string> = new Set([
|
|
182
|
+
"constructor",
|
|
183
|
+
"message",
|
|
184
|
+
"name",
|
|
185
|
+
"stack",
|
|
186
|
+
"toJSON",
|
|
187
|
+
"toString",
|
|
188
|
+
"valueOf"
|
|
189
|
+
])
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* A copy of `error` carrying the same information with the rules applied.
|
|
193
|
+
*
|
|
194
|
+
* A PLAIN `Error`, never a copy built on the original's prototype. A prototype
|
|
195
|
+
* is an unbounded leak surface and an own-key walk never looks at one: an
|
|
196
|
+
* inherited `name`, `cause`, `toJSON`, `Symbol.toStringTag` or
|
|
197
|
+
* `nodejs.util.inspect.custom` each carried a credential straight to an OTLP
|
|
198
|
+
* span attribute or to the operator's terminal, one per review round, and a
|
|
199
|
+
* getter anywhere on the chain is the same hole with a different name. So
|
|
200
|
+
* nothing inherited crosses. Only redacted text does: the name, the message,
|
|
201
|
+
* the stack, and the error's own members, each read once here and stored as
|
|
202
|
+
* data, since a getter carried across unchanged would hand the unredacted
|
|
203
|
+
* value to whatever reads it next.
|
|
204
|
+
*
|
|
205
|
+
* The copy is not an instance of the original's class, so a renderer that
|
|
206
|
+
* special-cases `instanceof MyError` sees a plain `Error`. That is the fidelity
|
|
207
|
+
* trade the release policy already records for every other value, and it is
|
|
208
|
+
* the trade the same problem's earlier, crashing form was settled with: a copy
|
|
209
|
+
* on a host prototype, such as the `DOMException` an `AbortSignal` carries as
|
|
210
|
+
* its reason, is an impostor whose brand check throws from inside `Cause.pretty`
|
|
211
|
+
* and kills the run the line was describing.
|
|
212
|
+
*/
|
|
213
|
+
const redactError = (
|
|
214
|
+
error: Error,
|
|
215
|
+
redactor: Redaction.Redactor,
|
|
216
|
+
seen: WeakMap<Error, Error>,
|
|
217
|
+
depth = 0
|
|
218
|
+
): Error => {
|
|
219
|
+
// The memo holds the COPY, not a visited mark: returning the original on a
|
|
220
|
+
// repeat reference put the unredacted error back into the output, so a
|
|
221
|
+
// diamond (two fields naming one error) leaked on its second field. The copy
|
|
222
|
+
// is registered before its own members are walked, so a cycle terminates on
|
|
223
|
+
// it and every reference to one error is the same redacted object.
|
|
224
|
+
const memoized = seen.get(error)
|
|
225
|
+
if (memoized !== undefined) return memoized
|
|
226
|
+
const clone = new Error()
|
|
227
|
+
seen.set(error, clone)
|
|
228
|
+
const define = (key: string, value: unknown, enumerable: boolean): void => {
|
|
229
|
+
Object.defineProperty(clone, key, { value, writable: true, enumerable, configurable: true })
|
|
230
|
+
}
|
|
231
|
+
define("name", redactedText(() => error.name, redactor, "Error"), false)
|
|
232
|
+
define("message", redactedText(() => error.message, redactor, ""), false)
|
|
233
|
+
define("stack", redactedStack(error, redactor), false)
|
|
234
|
+
// Own NAMES, not `Reflect.ownKeys`. A symbol key never crosses: its
|
|
235
|
+
// description is text no walk can rewrite in place, and the three hooks with
|
|
236
|
+
// no name to skip are symbols too, `Symbol.toPrimitive`,
|
|
237
|
+
// `Symbol.toStringTag`, and `nodejs.util.inspect.custom`.
|
|
238
|
+
for (const key of Object.getOwnPropertyNames(error)) {
|
|
239
|
+
if (renderHooks.has(key)) continue
|
|
240
|
+
const descriptor = Object.getOwnPropertyDescriptor(error, key)!
|
|
241
|
+
const value = "value" in descriptor
|
|
242
|
+
? descriptor.value
|
|
243
|
+
: (error as unknown as Record<PropertyKey, unknown>)[key]
|
|
244
|
+
define(
|
|
245
|
+
redactedName(key, redactor),
|
|
246
|
+
// A credential-named member is replaced wholesale, the way the journal's
|
|
247
|
+
// own walk replaces one, so both halves of the one rule set answer alike.
|
|
248
|
+
Redaction.isSensitiveKey(key)
|
|
249
|
+
? Redaction.placeholder
|
|
250
|
+
// The depth cap binds under an Error exactly as it does beside one: a
|
|
251
|
+
// deep value carried on a cause can exhaust the stack just as easily.
|
|
252
|
+
: depth + 1 >= renderDepthLimit
|
|
253
|
+
? renderDepthMarker
|
|
254
|
+
: value instanceof Error
|
|
255
|
+
? redactError(value, redactor, seen, depth + 1)
|
|
256
|
+
: redactor(value),
|
|
257
|
+
descriptor.enumerable === true
|
|
258
|
+
)
|
|
259
|
+
}
|
|
260
|
+
return clone
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Redacts one value on its way out of the logger.
|
|
265
|
+
*
|
|
266
|
+
* An `Error` is rebuilt as a plain `Error` whose name, message, stack and own
|
|
267
|
+
* members are redacted, so a renderer that special-cases an `Error` still gets
|
|
268
|
+
* one. Everything else goes through the journal's own rules, which rebuild a
|
|
269
|
+
* value from its plain data: a `Date`, a `Map`, a class instance or a host
|
|
270
|
+
* object therefore reaches the operator in that plain form rather than as
|
|
271
|
+
* itself.
|
|
272
|
+
*
|
|
273
|
+
* That collapse is deliberate, and it now covers the `Error` too. An earlier
|
|
274
|
+
* version rebuilt each value on its own prototype to keep the rendering
|
|
275
|
+
* faithful. Three rounds of review found three ways that could kill the run it
|
|
276
|
+
* was logging, because a host class keeps state a property walk cannot see and
|
|
277
|
+
* a copy of one throws the moment a brand check reads it, and four more found
|
|
278
|
+
* that the prototype an `Error` copy kept was itself the leak: everything it
|
|
279
|
+
* defines is read by a renderer and seen by no own-key walk. A log line is not
|
|
280
|
+
* worth a run, and the rendering it buys is cosmetic.
|
|
281
|
+
*
|
|
282
|
+
* @since 0.1.0
|
|
283
|
+
* @category redaction
|
|
284
|
+
*/
|
|
285
|
+
export const redactArgument = (value: unknown, redactor: Redaction.Redactor): unknown => {
|
|
286
|
+
try {
|
|
287
|
+
return value instanceof Error ? redactError(value, redactor, new WeakMap()) : redactor(value)
|
|
288
|
+
} catch {
|
|
289
|
+
// A logged value is arbitrary and a throw here happens while the line is
|
|
290
|
+
// rendering, which would kill the run the line describes. The marker is
|
|
291
|
+
// deliberately NOT derived from the value: an earlier fallback rendered
|
|
292
|
+
// the value to text and printed it, and that text had never been through
|
|
293
|
+
// the rules, so a credential reached the terminal in clear.
|
|
294
|
+
return unrenderableMarker
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** What the logger renders in place of a value that cannot be rendered. */
|
|
299
|
+
const unrenderableMarker = "[Unrenderable]"
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* A console that runs every value it is handed through `redactor` and then
|
|
303
|
+
* delegates to `target`.
|
|
304
|
+
*
|
|
305
|
+
* @since 0.1.0
|
|
306
|
+
* @category constructors
|
|
307
|
+
*/
|
|
308
|
+
export const redactingConsole = (target: Console.Console, redactor: Redaction.Redactor): Console.Console => {
|
|
309
|
+
const methods = target as unknown as Record<string, (...args: ReadonlyArray<any>) => unknown>
|
|
310
|
+
const view: Record<string, unknown> = {}
|
|
311
|
+
for (const [name, redacts] of Object.entries(consoleMethods)) {
|
|
312
|
+
const method = methods[name]!.bind(target)
|
|
313
|
+
view[name] = redacts
|
|
314
|
+
? (...args: ReadonlyArray<unknown>) => {
|
|
315
|
+
const redacted = args.map((value) => redactArgument(value, redactor))
|
|
316
|
+
try {
|
|
317
|
+
return method(...redacted)
|
|
318
|
+
} catch {
|
|
319
|
+
// The delegate refused what we handed it. Retry with markers, never
|
|
320
|
+
// with a rendering of the values: a rendering has not been through
|
|
321
|
+
// the rules, and printing one is how a credential reached the
|
|
322
|
+
// terminal before. A delegate that refuses the markers too is out of
|
|
323
|
+
// options, and a log line is not worth the run.
|
|
324
|
+
try {
|
|
325
|
+
return method(...redacted.map(() => unrenderableMarker))
|
|
326
|
+
} catch {
|
|
327
|
+
return undefined
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
: method
|
|
332
|
+
}
|
|
333
|
+
return view as unknown as Console.Console
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* A view of `fiber` whose console redacts and whose log annotations are
|
|
338
|
+
* already redacted.
|
|
339
|
+
*
|
|
340
|
+
* The view inherits from the fiber itself, so `id`, `currentSpan`, and every
|
|
341
|
+
* other member a logger reads answer exactly as before; only the two
|
|
342
|
+
* references a logger renders from are substituted.
|
|
343
|
+
*/
|
|
344
|
+
const redactedFiber = (
|
|
345
|
+
fiber: Fiber.Fiber<unknown, unknown>,
|
|
346
|
+
redactor: Redaction.Redactor
|
|
347
|
+
): Fiber.Fiber<unknown, unknown> => {
|
|
348
|
+
const view = Object.create(fiber) as Fiber.Fiber<unknown, unknown>
|
|
349
|
+
Object.defineProperty(view, "getRef", {
|
|
350
|
+
value: <X>(reference: Context.Reference<X>): X => {
|
|
351
|
+
const current = fiber.getRef(reference)
|
|
352
|
+
if (reference === (Console.Console as unknown as Context.Reference<X>)) {
|
|
353
|
+
return redactingConsole(current as Console.Console, redactor) as X
|
|
354
|
+
}
|
|
355
|
+
if (reference === (References.CurrentLogAnnotations as unknown as Context.Reference<X>)) {
|
|
356
|
+
// The same treatment a message part gets, so one log event obeys one
|
|
357
|
+
// rule and an annotated Error is redacted like a logged one.
|
|
358
|
+
return redactArgument(current, redactor) as X
|
|
359
|
+
}
|
|
360
|
+
return current
|
|
361
|
+
}
|
|
362
|
+
})
|
|
363
|
+
return view
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Rebuilds the frame chain as redacted data. Evaluate the lazy stack here:
|
|
368
|
+
* carrying its original function would defer the credential past the boundary.
|
|
369
|
+
* The same depth cap as logged values also bounds cyclic parent chains.
|
|
370
|
+
*/
|
|
371
|
+
const redactedFrame = (
|
|
372
|
+
frame: References.StackFrame | undefined,
|
|
373
|
+
redactor: Redaction.Redactor,
|
|
374
|
+
depth = 0
|
|
375
|
+
): References.StackFrame | undefined => {
|
|
376
|
+
if (frame === undefined) return undefined
|
|
377
|
+
if (depth >= renderDepthLimit) {
|
|
378
|
+
return { name: renderDepthMarker, stack: () => undefined, parent: undefined }
|
|
379
|
+
}
|
|
380
|
+
const stack = redactedText(() => frame.stack() ?? "", redactor, unrenderableMarker)
|
|
381
|
+
return {
|
|
382
|
+
name: redactedText(() => frame.name, redactor, unrenderableMarker),
|
|
383
|
+
stack: () => stack,
|
|
384
|
+
parent: redactedFrame(frame.parent, redactor, depth + 1)
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* The log event's cause with the rules applied to failures, defects and stack
|
|
390
|
+
* annotations. `Cause.pretty` renders these frames after rendering the error;
|
|
391
|
+
* the tracer exports that text without passing through the redacting console.
|
|
392
|
+
* Interruptions also carry both their own and the interruptor's frame chains.
|
|
393
|
+
* Rebuild reasons and annotation maps so the original cause stays untouched.
|
|
394
|
+
*/
|
|
395
|
+
const redactedCause = (
|
|
396
|
+
cause: Cause.Cause<unknown>,
|
|
397
|
+
redactor: Redaction.Redactor
|
|
398
|
+
): Cause.Cause<unknown> => {
|
|
399
|
+
if (cause.reasons.length === 0) return cause
|
|
400
|
+
return Cause.fromReasons(cause.reasons.map((reason) => {
|
|
401
|
+
const annotations = new Map(reason.annotations)
|
|
402
|
+
for (const key of [Cause.StackTrace.key, Cause.InterruptorStackTrace.key]) {
|
|
403
|
+
if (annotations.has(key)) {
|
|
404
|
+
annotations.set(key, redactedFrame(annotations.get(key) as References.StackFrame | undefined, redactor))
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
const copy = reason._tag === "Fail"
|
|
408
|
+
? Cause.makeFailReason(redactArgument(reason.error, redactor))
|
|
409
|
+
: reason._tag === "Die"
|
|
410
|
+
? Cause.makeDieReason(redactArgument(reason.defect, redactor))
|
|
411
|
+
: Cause.makeInterruptReason(reason.fiberId)
|
|
412
|
+
return copy.annotate(Context.makeUnsafe(annotations))
|
|
413
|
+
}))
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Returns `logger` with the journal's redaction rules over everything it
|
|
418
|
+
* renders. A logger this module already wrapped is returned unchanged.
|
|
419
|
+
*
|
|
420
|
+
* @since 0.1.0
|
|
421
|
+
* @category combinators
|
|
422
|
+
*/
|
|
423
|
+
export const wrap = <Message, Output>(
|
|
424
|
+
logger: Logger.Logger<Message, Output>,
|
|
425
|
+
options?: Redaction.Options
|
|
426
|
+
): Logger.Logger<Message, Output> => {
|
|
427
|
+
if (isRedacted(logger)) return logger
|
|
428
|
+
// A logger NAMES a too-deep value rather than throwing: a throw is caught one
|
|
429
|
+
// frame up and costs the operator every argument on the line, not just the
|
|
430
|
+
// deep one. A log line is a diagnostic, so it takes the diagnostic rules. A
|
|
431
|
+
// caller passing its own `onTooDeep` or `rules` still wins.
|
|
432
|
+
const redactor = Redaction.make({ onTooDeep: "name", rules: Redaction.diagnosticRules, ...options })
|
|
433
|
+
const wrapped = Logger.make<Message, Output>((logOptions) =>
|
|
434
|
+
logger.log({
|
|
435
|
+
...logOptions,
|
|
436
|
+
// The message is redacted here, not only behind the fiber's Console,
|
|
437
|
+
// because a logger is free to read it directly: `Logger.tracerLogger`
|
|
438
|
+
// ships in Effect's default set and publishes the message as a span
|
|
439
|
+
// event name, so a Console-only substitution would export a credential
|
|
440
|
+
// in clear to whatever collector is configured.
|
|
441
|
+
//
|
|
442
|
+
// Effect delivers the message as an array of parts, one per argument,
|
|
443
|
+
// including the single-argument case; a non-array reaches here only from
|
|
444
|
+
// a caller that builds `LogOptions` by hand, and `toArray` covers it
|
|
445
|
+
// without a branch no test could reach through the logging API.
|
|
446
|
+
message: (toArray(logOptions.message).map((part) => redactArgument(part, redactor))) as Message,
|
|
447
|
+
// The cause travels the same way and for the same reason. A failure is
|
|
448
|
+
// logged as a `Cause`, not as message parts (`Effect.logError(cause)`
|
|
449
|
+
// moves it out of the arguments), and `Logger.tracerLogger` renders it
|
|
450
|
+
// itself into the span's `effect.cause` attribute without going near the
|
|
451
|
+
// console, so an error message carrying a token would leave the process
|
|
452
|
+
// in clear.
|
|
453
|
+
cause: redactedCause(logOptions.cause, redactor),
|
|
454
|
+
fiber: redactedFiber(logOptions.fiber, redactor)
|
|
455
|
+
})
|
|
456
|
+
)
|
|
457
|
+
Object.defineProperty(wrapped, TypeId, { value: true })
|
|
458
|
+
return wrapped
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Replaces the active logger set with redacting wrappers of the same loggers.
|
|
463
|
+
*
|
|
464
|
+
* It composes over whatever loggers are already installed rather than
|
|
465
|
+
* choosing one, so an operator keeps the FORMAT they had, meaning their own
|
|
466
|
+
* logger and its layout, though a value inside a line is rendered from its
|
|
467
|
+
* redacted plain data rather than as its own class, and a host that
|
|
468
|
+
* installed its own logger keeps it too.
|
|
469
|
+
*
|
|
470
|
+
* @since 0.1.0
|
|
471
|
+
* @category layers
|
|
472
|
+
*/
|
|
473
|
+
export const layer = (options?: Redaction.Options): Layer.Layer<never> =>
|
|
474
|
+
Layer.effect(
|
|
475
|
+
Logger.CurrentLoggers,
|
|
476
|
+
Effect.withFiber((fiber) =>
|
|
477
|
+
Effect.sync(() => {
|
|
478
|
+
const wrapped = new Set<Logger.Logger<unknown, any>>()
|
|
479
|
+
for (const logger of fiber.getRef(Logger.CurrentLoggers)) wrapped.add(wrap(logger, options))
|
|
480
|
+
return wrapped
|
|
481
|
+
})
|
|
482
|
+
)
|
|
483
|
+
)
|