@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.
Files changed (257) hide show
  1. package/CHANGELOG.md +149 -0
  2. package/LICENSE +21 -0
  3. package/README.md +206 -2
  4. package/dist/cjs/CallFact.d.ts +129 -0
  5. package/dist/cjs/CallFact.d.ts.map +1 -0
  6. package/dist/cjs/CallFact.js +106 -0
  7. package/dist/cjs/CallFact.js.map +7 -0
  8. package/dist/cjs/Consensus.d.ts +440 -0
  9. package/dist/cjs/Consensus.d.ts.map +1 -0
  10. package/dist/cjs/Consensus.js +251 -0
  11. package/dist/cjs/Consensus.js.map +7 -0
  12. package/dist/cjs/EngineEvent.d.ts +890 -0
  13. package/dist/cjs/EngineEvent.d.ts.map +1 -0
  14. package/dist/cjs/EngineEvent.js +383 -0
  15. package/dist/cjs/EngineEvent.js.map +7 -0
  16. package/dist/cjs/ExecutionFact.d.ts +216 -0
  17. package/dist/cjs/ExecutionFact.d.ts.map +1 -0
  18. package/dist/cjs/ExecutionFact.js +303 -0
  19. package/dist/cjs/ExecutionFact.js.map +7 -0
  20. package/dist/cjs/Journal.d.ts +617 -0
  21. package/dist/cjs/Journal.d.ts.map +1 -0
  22. package/dist/cjs/Journal.js +190 -0
  23. package/dist/cjs/Journal.js.map +7 -0
  24. package/dist/cjs/JournalEvent.d.ts +421 -0
  25. package/dist/cjs/JournalEvent.d.ts.map +1 -0
  26. package/dist/cjs/JournalEvent.js +281 -0
  27. package/dist/cjs/JournalEvent.js.map +7 -0
  28. package/dist/cjs/JournalGeneration.d.ts +34 -0
  29. package/dist/cjs/JournalGeneration.d.ts.map +1 -0
  30. package/dist/cjs/JournalGeneration.js +72 -0
  31. package/dist/cjs/JournalGeneration.js.map +7 -0
  32. package/dist/cjs/JournalMetrics.d.ts +72 -0
  33. package/dist/cjs/JournalMetrics.d.ts.map +1 -0
  34. package/dist/cjs/JournalMetrics.js +55 -0
  35. package/dist/cjs/JournalMetrics.js.map +7 -0
  36. package/dist/cjs/Migrations.d.ts +42 -0
  37. package/dist/cjs/Migrations.d.ts.map +1 -0
  38. package/dist/cjs/Migrations.js +58 -0
  39. package/dist/cjs/Migrations.js.map +7 -0
  40. package/dist/cjs/OwnerId.d.ts +40 -0
  41. package/dist/cjs/OwnerId.d.ts.map +1 -0
  42. package/dist/cjs/OwnerId.js +40 -0
  43. package/dist/cjs/OwnerId.js.map +7 -0
  44. package/dist/cjs/Projection.d.ts +29 -0
  45. package/dist/cjs/Projection.d.ts.map +1 -0
  46. package/dist/cjs/Projection.js +25 -0
  47. package/dist/cjs/Projection.js.map +7 -0
  48. package/dist/cjs/RedactedLogger.d.ts +121 -0
  49. package/dist/cjs/RedactedLogger.d.ts.map +1 -0
  50. package/dist/cjs/RedactedLogger.js +235 -0
  51. package/dist/cjs/RedactedLogger.js.map +7 -0
  52. package/dist/cjs/Redaction.d.ts +350 -0
  53. package/dist/cjs/Redaction.d.ts.map +1 -0
  54. package/dist/cjs/Redaction.js +928 -0
  55. package/dist/cjs/Redaction.js.map +7 -0
  56. package/dist/cjs/SqlConsensus.d.ts +44 -0
  57. package/dist/cjs/SqlConsensus.d.ts.map +1 -0
  58. package/dist/cjs/SqlConsensus.js +243 -0
  59. package/dist/cjs/SqlConsensus.js.map +7 -0
  60. package/dist/cjs/SqlJournal.d.ts +173 -0
  61. package/dist/cjs/SqlJournal.d.ts.map +1 -0
  62. package/dist/cjs/SqlJournal.js +1568 -0
  63. package/dist/cjs/SqlJournal.js.map +7 -0
  64. package/dist/cjs/StepFact.d.ts +66 -0
  65. package/dist/cjs/StepFact.d.ts.map +1 -0
  66. package/dist/cjs/StepFact.js +68 -0
  67. package/dist/cjs/StepFact.js.map +7 -0
  68. package/dist/cjs/index.d.ts +95 -0
  69. package/dist/cjs/index.d.ts.map +1 -0
  70. package/dist/cjs/index.js +63 -0
  71. package/dist/cjs/index.js.map +7 -0
  72. package/dist/cjs/internal/JournalInternalFault.d.ts +37 -0
  73. package/dist/cjs/internal/JournalInternalFault.d.ts.map +1 -0
  74. package/dist/cjs/internal/JournalInternalFault.js +43 -0
  75. package/dist/cjs/internal/JournalInternalFault.js.map +7 -0
  76. package/dist/cjs/migrations/0001_initial.d.ts +15 -0
  77. package/dist/cjs/migrations/0001_initial.d.ts.map +1 -0
  78. package/dist/cjs/migrations/0001_initial.js +54 -0
  79. package/dist/cjs/migrations/0001_initial.js.map +7 -0
  80. package/dist/cjs/migrations/0002_checkpoints.d.ts +27 -0
  81. package/dist/cjs/migrations/0002_checkpoints.d.ts.map +1 -0
  82. package/dist/cjs/migrations/0002_checkpoints.js +48 -0
  83. package/dist/cjs/migrations/0002_checkpoints.js.map +7 -0
  84. package/dist/cjs/migrations/0003_startup_index.d.ts +15 -0
  85. package/dist/cjs/migrations/0003_startup_index.d.ts.map +1 -0
  86. package/dist/cjs/migrations/0003_startup_index.js +40 -0
  87. package/dist/cjs/migrations/0003_startup_index.js.map +7 -0
  88. package/dist/cjs/migrations/0004_dedup.d.ts +15 -0
  89. package/dist/cjs/migrations/0004_dedup.d.ts.map +1 -0
  90. package/dist/cjs/migrations/0004_dedup.js +57 -0
  91. package/dist/cjs/migrations/0004_dedup.js.map +7 -0
  92. package/dist/cjs/migrations/0005_run_event_type.d.ts +15 -0
  93. package/dist/cjs/migrations/0005_run_event_type.d.ts.map +1 -0
  94. package/dist/cjs/migrations/0005_run_event_type.js +40 -0
  95. package/dist/cjs/migrations/0005_run_event_type.js.map +7 -0
  96. package/dist/cjs/migrations/0006_consensus.d.ts +22 -0
  97. package/dist/cjs/migrations/0006_consensus.d.ts.map +1 -0
  98. package/dist/cjs/migrations/0006_consensus.js +84 -0
  99. package/dist/cjs/migrations/0006_consensus.js.map +7 -0
  100. package/dist/cjs/package.json +1 -0
  101. package/dist/cjs/test/Notifying.d.ts +65 -0
  102. package/dist/cjs/test/Notifying.d.ts.map +1 -0
  103. package/dist/cjs/test/Notifying.js +62 -0
  104. package/dist/cjs/test/Notifying.js.map +7 -0
  105. package/dist/cjs/test/TestJournal.d.ts +43 -0
  106. package/dist/cjs/test/TestJournal.d.ts.map +1 -0
  107. package/dist/cjs/test/TestJournal.js +43 -0
  108. package/dist/cjs/test/TestJournal.js.map +7 -0
  109. package/dist/esm/CallFact.d.ts +129 -0
  110. package/dist/esm/CallFact.d.ts.map +1 -0
  111. package/dist/esm/CallFact.js +96 -0
  112. package/dist/esm/CallFact.js.map +1 -0
  113. package/dist/esm/Consensus.d.ts +440 -0
  114. package/dist/esm/Consensus.d.ts.map +1 -0
  115. package/dist/esm/Consensus.js +454 -0
  116. package/dist/esm/Consensus.js.map +1 -0
  117. package/dist/esm/EngineEvent.d.ts +890 -0
  118. package/dist/esm/EngineEvent.d.ts.map +1 -0
  119. package/dist/esm/EngineEvent.js +614 -0
  120. package/dist/esm/EngineEvent.js.map +1 -0
  121. package/dist/esm/ExecutionFact.d.ts +216 -0
  122. package/dist/esm/ExecutionFact.d.ts.map +1 -0
  123. package/dist/esm/ExecutionFact.js +374 -0
  124. package/dist/esm/ExecutionFact.js.map +1 -0
  125. package/dist/esm/Journal.d.ts +617 -0
  126. package/dist/esm/Journal.d.ts.map +1 -0
  127. package/dist/esm/Journal.js +323 -0
  128. package/dist/esm/Journal.js.map +1 -0
  129. package/dist/esm/JournalEvent.d.ts +421 -0
  130. package/dist/esm/JournalEvent.d.ts.map +1 -0
  131. package/dist/esm/JournalEvent.js +517 -0
  132. package/dist/esm/JournalEvent.js.map +1 -0
  133. package/dist/esm/JournalGeneration.d.ts +34 -0
  134. package/dist/esm/JournalGeneration.d.ts.map +1 -0
  135. package/dist/esm/JournalGeneration.js +65 -0
  136. package/dist/esm/JournalGeneration.js.map +1 -0
  137. package/dist/esm/JournalMetrics.d.ts +72 -0
  138. package/dist/esm/JournalMetrics.d.ts.map +1 -0
  139. package/dist/esm/JournalMetrics.js +79 -0
  140. package/dist/esm/JournalMetrics.js.map +1 -0
  141. package/dist/esm/Migrations.d.ts +42 -0
  142. package/dist/esm/Migrations.d.ts.map +1 -0
  143. package/dist/esm/Migrations.js +59 -0
  144. package/dist/esm/Migrations.js.map +1 -0
  145. package/dist/esm/OwnerId.d.ts +40 -0
  146. package/dist/esm/OwnerId.d.ts.map +1 -0
  147. package/dist/esm/OwnerId.js +33 -0
  148. package/dist/esm/OwnerId.js.map +1 -0
  149. package/dist/esm/Projection.d.ts +29 -0
  150. package/dist/esm/Projection.d.ts.map +1 -0
  151. package/dist/esm/Projection.js +13 -0
  152. package/dist/esm/Projection.js.map +1 -0
  153. package/dist/esm/RedactedLogger.d.ts +121 -0
  154. package/dist/esm/RedactedLogger.d.ts.map +1 -0
  155. package/dist/esm/RedactedLogger.js +445 -0
  156. package/dist/esm/RedactedLogger.js.map +1 -0
  157. package/dist/esm/Redaction.d.ts +350 -0
  158. package/dist/esm/Redaction.d.ts.map +1 -0
  159. package/dist/esm/Redaction.js +1584 -0
  160. package/dist/esm/Redaction.js.map +1 -0
  161. package/dist/esm/SqlConsensus.d.ts +44 -0
  162. package/dist/esm/SqlConsensus.d.ts.map +1 -0
  163. package/dist/esm/SqlConsensus.js +252 -0
  164. package/dist/esm/SqlConsensus.js.map +1 -0
  165. package/dist/esm/SqlJournal.d.ts +173 -0
  166. package/dist/esm/SqlJournal.d.ts.map +1 -0
  167. package/dist/esm/SqlJournal.js +1858 -0
  168. package/dist/esm/SqlJournal.js.map +1 -0
  169. package/dist/esm/StepFact.d.ts +66 -0
  170. package/dist/esm/StepFact.d.ts.map +1 -0
  171. package/dist/esm/StepFact.js +50 -0
  172. package/dist/esm/StepFact.js.map +1 -0
  173. package/dist/esm/index.d.ts +95 -0
  174. package/dist/esm/index.d.ts.map +1 -0
  175. package/dist/esm/index.js +95 -0
  176. package/dist/esm/index.js.map +1 -0
  177. package/dist/esm/internal/JournalInternalFault.d.ts +37 -0
  178. package/dist/esm/internal/JournalInternalFault.d.ts.map +1 -0
  179. package/dist/esm/internal/JournalInternalFault.js +28 -0
  180. package/dist/esm/internal/JournalInternalFault.js.map +1 -0
  181. package/dist/esm/migrations/0001_initial.d.ts +15 -0
  182. package/dist/esm/migrations/0001_initial.d.ts.map +1 -0
  183. package/dist/esm/migrations/0001_initial.js +32 -0
  184. package/dist/esm/migrations/0001_initial.js.map +1 -0
  185. package/dist/esm/migrations/0002_checkpoints.d.ts +27 -0
  186. package/dist/esm/migrations/0002_checkpoints.d.ts.map +1 -0
  187. package/dist/esm/migrations/0002_checkpoints.js +38 -0
  188. package/dist/esm/migrations/0002_checkpoints.js.map +1 -0
  189. package/dist/esm/migrations/0003_startup_index.d.ts +15 -0
  190. package/dist/esm/migrations/0003_startup_index.d.ts.map +1 -0
  191. package/dist/esm/migrations/0003_startup_index.js +18 -0
  192. package/dist/esm/migrations/0003_startup_index.js.map +1 -0
  193. package/dist/esm/migrations/0004_dedup.d.ts +15 -0
  194. package/dist/esm/migrations/0004_dedup.d.ts.map +1 -0
  195. package/dist/esm/migrations/0004_dedup.js +38 -0
  196. package/dist/esm/migrations/0004_dedup.js.map +1 -0
  197. package/dist/esm/migrations/0005_run_event_type.d.ts +15 -0
  198. package/dist/esm/migrations/0005_run_event_type.d.ts.map +1 -0
  199. package/dist/esm/migrations/0005_run_event_type.js +18 -0
  200. package/dist/esm/migrations/0005_run_event_type.js.map +1 -0
  201. package/dist/esm/migrations/0006_consensus.d.ts +22 -0
  202. package/dist/esm/migrations/0006_consensus.d.ts.map +1 -0
  203. package/dist/esm/migrations/0006_consensus.js +69 -0
  204. package/dist/esm/migrations/0006_consensus.js.map +1 -0
  205. package/dist/esm/test/Notifying.d.ts +65 -0
  206. package/dist/esm/test/Notifying.d.ts.map +1 -0
  207. package/dist/esm/test/Notifying.js +62 -0
  208. package/dist/esm/test/Notifying.js.map +1 -0
  209. package/dist/esm/test/TestJournal.d.ts +43 -0
  210. package/dist/esm/test/TestJournal.d.ts.map +1 -0
  211. package/dist/esm/test/TestJournal.js +27 -0
  212. package/dist/esm/test/TestJournal.js.map +1 -0
  213. package/docs/README.md +164 -0
  214. package/docs/api.md +639 -0
  215. package/docs/concepts/compaction.md +157 -0
  216. package/docs/concepts/idempotency.md +127 -0
  217. package/docs/concepts/owner-fence.md +124 -0
  218. package/docs/concepts/redaction.md +218 -0
  219. package/docs/concepts/state-event-authority.md +159 -0
  220. package/docs/concepts/two-channels.md +167 -0
  221. package/docs/guides/commit-state-and-entry.md +114 -0
  222. package/docs/guides/compact-a-run.md +134 -0
  223. package/docs/guides/fold-a-projection.md +106 -0
  224. package/docs/guides/read-a-run.md +139 -0
  225. package/docs/guides/redact-log-output.md +94 -0
  226. package/docs/guides/testing.md +124 -0
  227. package/docs/guides/write-lifecycle-events.md +110 -0
  228. package/docs/installation.md +133 -0
  229. package/docs/quickstart.md +143 -0
  230. package/docs/troubleshooting.md +214 -0
  231. package/package.json +263 -3
  232. package/src/CallFact.ts +117 -0
  233. package/src/Consensus.ts +683 -0
  234. package/src/EngineEvent.ts +823 -0
  235. package/src/ExecutionFact.ts +433 -0
  236. package/src/Journal.ts +688 -0
  237. package/src/JournalEvent.ts +654 -0
  238. package/src/JournalGeneration.ts +77 -0
  239. package/src/JournalMetrics.ts +89 -0
  240. package/src/Migrations.ts +62 -0
  241. package/src/OwnerId.ts +42 -0
  242. package/src/Projection.ts +31 -0
  243. package/src/RedactedLogger.ts +483 -0
  244. package/src/Redaction.ts +1784 -0
  245. package/src/SqlConsensus.ts +344 -0
  246. package/src/SqlJournal.ts +2732 -0
  247. package/src/StepFact.ts +69 -0
  248. package/src/index.ts +109 -0
  249. package/src/internal/JournalInternalFault.ts +40 -0
  250. package/src/migrations/0001_initial.ts +41 -0
  251. package/src/migrations/0002_checkpoints.ts +46 -0
  252. package/src/migrations/0003_startup_index.ts +19 -0
  253. package/src/migrations/0004_dedup.ts +43 -0
  254. package/src/migrations/0005_run_event_type.ts +19 -0
  255. package/src/migrations/0006_consensus.ts +83 -0
  256. package/src/test/Notifying.ts +101 -0
  257. package/src/test/TestJournal.ts +52 -0
@@ -0,0 +1,1784 @@
1
+ /**
2
+ * Payload redaction for durable journal writes.
3
+ *
4
+ * The journal is permanent and broadly readable: every entry is replayed to
5
+ * sync subscribers and to time-travel consumers. A secret that reaches
6
+ * `payload_json` is therefore not a transient leak, so redaction happens once,
7
+ * on the journal write path, rather than at each reader.
8
+ *
9
+ * Redaction is an **observability** concern and is confined to journal events
10
+ * and export/display surfaces. It is deliberately not applied to executable
11
+ * state, `flows_runs.state_json`, attempt checkpoints, errors, outcomes, and
12
+ * cache results, because those are decoded and re-entered on resume: a
13
+ * placeholder there resumes the flow with the wrong data, and replacing a
14
+ * non-string value with a placeholder string makes the persisted state fail
15
+ * schema decode outright, leaving the run undrivable (issue #72). A value
16
+ * that must never reach durable executable state is a `Redacted` field in the
17
+ * caller's own schema, not a name-suffix guess made at the storage seam.
18
+ *
19
+ * The rule set is a best-effort textual net over credential shapes seen in
20
+ * real bug reports, plus structural redaction by sensitive field name. It is
21
+ * finite, so a value that must never persist belongs in a `Redacted` field of
22
+ * the caller's own schema. See https://journal.smithers.sh/concepts/redaction/
23
+ * for the rule set and how to replace it.
24
+ *
25
+ * @since 0.1.0
26
+ */
27
+
28
+ import * as Result from "effect/Result"
29
+ import * as Schema from "effect/Schema"
30
+
31
+ /** JSON text carrying an arbitrary decoded value. */
32
+ const UnknownFromJsonString = Schema.fromJsonString(Schema.Unknown)
33
+
34
+ /**
35
+ * A textual redaction rule.
36
+ *
37
+ * `replace` is the substitution for a matched span; when omitted the whole
38
+ * match is replaced by the placeholder.
39
+ *
40
+ * `rewrite`, when set, rewrites the text itself instead. It is for a rule
41
+ * whose extent is not regular: where a credential's value ends depends on
42
+ * quotes, escapes and brackets a regular expression cannot count. `pattern`
43
+ * then names what the rule looks for, and text `pattern` does not match is
44
+ * left unchanged without calling `rewrite`.
45
+ *
46
+ * @since 0.1.0
47
+ * @category models
48
+ */
49
+ export interface Rule {
50
+ readonly id: string
51
+ readonly pattern: RegExp
52
+ readonly replace?: string | undefined
53
+ readonly rewrite?: ((text: string) => string) | undefined
54
+ }
55
+
56
+ /**
57
+ * The placeholder written in place of a redacted value.
58
+ *
59
+ * @since 0.1.0
60
+ * @category constants
61
+ */
62
+ export const placeholder = "[REDACTED]"
63
+
64
+ const isQuote = (char: string | undefined): boolean => char === "\"" || char === "'" || char === "`"
65
+
66
+ const closers: Readonly<Record<string, string>> = { "{": "}", "[": "]", "(": ")" }
67
+
68
+ /**
69
+ * Whether the last {@link skipQuoted} or {@link skipContainer} found its
70
+ * closer. Both return the end of the text for a value that never closes, and
71
+ * for one that closes on the text's last character, so the end alone cannot
72
+ * say which.
73
+ */
74
+ let lastClosed = false
75
+
76
+ /**
77
+ * The index just past the quote that closes the string opened at `open`. A
78
+ * quote nested n strings deep sits behind `depth` = 2^n - 1 backslashes, and
79
+ * the quote that closes it has that count modulo 2^(n+1). A string that is
80
+ * never closed, cut by a bound or by its producer, runs to the end of the
81
+ * text. A caller that knows the backslashes were doubled again passes a larger
82
+ * `modulus`.
83
+ */
84
+ const skipQuoted = (
85
+ text: string,
86
+ open: number,
87
+ depth: number,
88
+ modulus = 2 * (depth + 1),
89
+ limit = text.length
90
+ ): number => {
91
+ let end = open + 1
92
+ while (end < limit) {
93
+ if (text[end] === text[open]) {
94
+ let slashes = 0
95
+ for (let i = end - 1; i > open && text[i] === "\\"; i--) slashes++
96
+ if (slashes % modulus === depth) {
97
+ lastClosed = true
98
+ return end + 1
99
+ }
100
+ }
101
+ end++
102
+ }
103
+ lastClosed = false
104
+ return end
105
+ }
106
+
107
+ /**
108
+ * The index just past the bracket that balances the one at `open`, skipping
109
+ * quoted strings at any escape depth. An unbalanced container runs to the end
110
+ * of the text.
111
+ */
112
+ const skipContainer = (text: string, open: number, limit = text.length): number => {
113
+ const pending: Array<string> = []
114
+ let end = open
115
+ while (end < limit) {
116
+ const char = text[end]!
117
+ if (isQuote(char)) {
118
+ let depth = 0
119
+ while (text[end - 1 - depth] === "\\") depth++
120
+ end = skipQuoted(text, end, depth, 2 * (depth + 1), limit)
121
+ if (!lastClosed) return end
122
+ continue
123
+ }
124
+ const closer = closers[char]
125
+ if (closer !== undefined) pending.push(closer)
126
+ else if (char === pending.at(-1)) {
127
+ pending.pop()
128
+ if (pending.length === 0) {
129
+ lastClosed = true
130
+ return end + 1
131
+ }
132
+ }
133
+ end++
134
+ }
135
+ lastClosed = false
136
+ return end
137
+ }
138
+
139
+ /**
140
+ * A bracket or string still open at some point of the text: a bracket by its
141
+ * closer, a string by its quote and escape depth. `string` is the index of the
142
+ * innermost open string at or below this entry, -1 when there is none, so
143
+ * finding the innermost string costs one read however deep the brackets
144
+ * above it are nested. A scan from the top made a text of n brackets and then
145
+ * n quotes cost n² (#3088).
146
+ */
147
+ interface Open {
148
+ readonly close: string
149
+ readonly depth?: number
150
+ readonly string: number
151
+ }
152
+
153
+ /**
154
+ * The escape depth of a quote behind `slashes` backslashes: 2^k - 1 for its k
155
+ * trailing escaped levels, so `\\"` (an escaped backslash, then a quote) is
156
+ * depth 0 and `\\\"` is depth 1.
157
+ */
158
+ const escapeDepth = (slashes: number): number => (slashes ^ (slashes + 1)) >> 1
159
+
160
+ const innermostString = (stack: ReadonlyArray<Open>): number => stack.at(-1)?.string ?? -1
161
+
162
+ /**
163
+ * Updates `stack` with the brackets and strings that `text[from, to)` opens
164
+ * and closes. A quote closes the innermost open string it can close at that
165
+ * string's escape depth, and otherwise opens a string at its own depth. The
166
+ * other quote characters inside a string are content (`"don't"`).
167
+ */
168
+ const track = (stack: Array<Open>, text: string, from: number, to: number): void => {
169
+ for (let i = from; i < to; i++) {
170
+ const char = text[i]!
171
+ if (isQuote(char)) {
172
+ let slashes = 0
173
+ while (text[i - 1 - slashes] === "\\") slashes++
174
+ const inner = innermostString(stack)
175
+ if (inner === -1) {
176
+ stack.push({ close: char, depth: escapeDepth(slashes), string: stack.length })
177
+ continue
178
+ }
179
+ if (stack[inner]!.close !== char) continue
180
+ // Only strings can close, so walk the strings alone, innermost first.
181
+ let closes = inner
182
+ while (closes !== -1) {
183
+ const depth = stack[closes]!.depth!
184
+ if (slashes % (2 * (depth + 1)) === depth) break
185
+ closes = closes === 0 ? -1 : stack[closes - 1]!.string
186
+ }
187
+ if (closes === -1) stack.push({ close: char, depth: escapeDepth(slashes), string: stack.length })
188
+ else stack.length = closes
189
+ continue
190
+ }
191
+ const closer = closers[char]
192
+ if (closer !== undefined) stack.push({ close: closer, string: innermostString(stack) })
193
+ else if (stack.at(-1)?.depth === undefined && char === stack.at(-1)?.close) stack.pop()
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Whether the closer at `end` closes the bracket open before the name: it and
199
+ * any closers right after it balance the enclosing brackets, and a separator
200
+ * or the end of the text follows (`[{token:abc}]`, not `Tr0ub4dor)]&3`).
201
+ */
202
+ const closesEnclosing = (text: string, end: number, stack: ReadonlyArray<Open>): boolean => {
203
+ let at = end
204
+ for (let k = stack.length - 1; k >= 0 && stack[k]!.depth === undefined && text[at] === stack[k]!.close; k--) at++
205
+ return at > end && /^(?:[\s,;"'`]|\\+["'`]|$)/.test(text.slice(at, at + 2))
206
+ }
207
+
208
+ /** Credential words a name ends in. */
209
+ const credentialWord = "(?:KEY|TOKEN|SECRET|PASSWORD|PASSPHRASE|CREDENTIAL)S?"
210
+
211
+ /**
212
+ * A name's separator: `=`, `:` or `=>`, after any quote that closes the name.
213
+ * In a durable row it stays on the name's line; a diagnostic also reads a
214
+ * value on the next line (`password:` then the value), which is how a stream
215
+ * redactor sees it arrive.
216
+ */
217
+ const separator = (space: string): string => String.raw`(?:\\*["'\x60])?${space}(?:=>|[=:])${space}`
218
+
219
+ /**
220
+ * A credential name and its separator. Start once per identifier, including
221
+ * leading underscores, so a name containing many underscores cannot force
222
+ * repeated scans of its suffix. The name may close a quote of its own
223
+ * (`'password':`, `\"token\":`), so a key in a Python repr, an inspected
224
+ * `Map` or escaped JSON meets the rule too.
225
+ */
226
+ const credentialName = String.raw`(?<![A-Za-z0-9_-])([A-Za-z0-9_-]*${credentialWord})`
227
+
228
+ const credentialNames = new RegExp(credentialName + separator("[ \\t]*"), "gi")
229
+
230
+ const diagnosticNames = new RegExp(credentialName + separator(String.raw`\s*`), "gi")
231
+
232
+ /** Case-insensitive authorization names, without making scheme words prose matches. */
233
+ const authorizationName = "(?:proxy-)?authorization".replace(/[a-z]/g, (char) => `[${char}${char.toUpperCase()}]`)
234
+
235
+ /** Complete header names whose whole value is a credential whatever its scheme. */
236
+ const headerWord = String.raw`${authorizationName}|${
237
+ "(?:set-)?cookies?".replace(/[a-z]/g, (char) => `[${char}${char.toUpperCase()}]`)
238
+ }`
239
+
240
+ // Apostrophes and backticks open quoted names only outside a header token.
241
+ const headerName = String
242
+ .raw`(?<![A-Za-z0-9!#$%&*+.^_|~-])(?<![A-Za-z0-9!#$%&'*+.^_\x60|~-]['\x60])(${headerWord})`
243
+
244
+ const headerNames = new RegExp(
245
+ String.raw`((?:proxy-)?authorization|(?:set-)?cookies?)${separator(String.raw`\s*`)}`,
246
+ "gi"
247
+ )
248
+
249
+ /** Whitespace headers require a scheme spelling, so ordinary authorization prose survives. */
250
+ const diagnosticHeaderNames = new RegExp(
251
+ // A trailing token quote requires an opener; double quotes are never token characters.
252
+ `${headerName}(?:` +
253
+ String.raw`(?:(?<=["'\x60](?:${headerWord}))|(?!\\*['\x60]))${separator(String.raw`\s*`)}|` +
254
+ String.raw`(?<=${authorizationName})\s+(?=(?:Token|TOKEN|[Bb]earer|BEARER|Basic|BASIC)[ \t]+\S))`,
255
+ "g"
256
+ )
257
+
258
+ /** Any credential or header name, to stop a bare value before the next pair. */
259
+ const anyName = new RegExp(`${credentialNames.source}|${headerNames.source}`, "gi")
260
+
261
+ /** A credential or header name and its separator ending the text before a quote. */
262
+ const nameBefore = new RegExp(`(?:${credentialNames.source}|${headerNames.source})$`, "i")
263
+
264
+ /**
265
+ * The index of the next credential or header name at or after `from`; with
266
+ * `quoted`, only a name written right after a quote.
267
+ */
268
+ const nextName = (text: string, from: number, quoted: boolean): number => {
269
+ // No name starts between a search's start and what it found, so a later
270
+ // search from inside that span has the same answer. Values are read left to
271
+ // right, so this keeps every search over one text linear in total.
272
+ const cached = nameSearches[quoted ? 1 : 0]!
273
+ if (cached.text === text && from >= cached.from && from <= cached.found) return cached.found
274
+ let found = text.length
275
+ anyName.lastIndex = from
276
+ for (let match = anyName.exec(text); match !== null; match = anyName.exec(text)) {
277
+ if (!quoted || isQuote(text[match.index - 1])) {
278
+ found = match.index
279
+ break
280
+ }
281
+ }
282
+ nameSearches[quoted ? 1 : 0] = { text, from, found }
283
+ return found
284
+ }
285
+
286
+ /** The last search for any name and for a quoted name. */
287
+ const nameSearches = [{ text: "", from: 0, found: 0 }, { text: "", from: 0, found: 0 }]
288
+
289
+ /**
290
+ * How far a value may run.
291
+ *
292
+ * A diagnostic is read once, so it errs toward hiding: a bare value runs over
293
+ * several words to the end of its line, and a string or container the text
294
+ * never closes runs to the end of the text, which is how a value cut by a
295
+ * bound still ends redacted. A journal row is permanent, so there a bare value
296
+ * is one token, `Foo {`, `[Object: null prototype] {` and `new X(` included,
297
+ * and an unclosed string or container stops at the end of its line: a stray
298
+ * backtick in an agent's Markdown must not erase the rest of the row.
299
+ */
300
+ interface Scope {
301
+ readonly header: boolean
302
+ readonly diagnostic: boolean
303
+ }
304
+
305
+ /** The end of the line `index` is on. */
306
+ const lineEnd = (text: string, index: number): number => {
307
+ // Every index up to a line's end shares that end, so the last answer serves
308
+ // each later value on the same line.
309
+ if (lastLine.text === text && index >= lastLine.from && index <= lastLine.end) return lastLine.end
310
+ newline.lastIndex = index
311
+ const end = newline.exec(text)?.index ?? text.length
312
+ lastLine = { text, from: index, end }
313
+ return end
314
+ }
315
+
316
+ const newline = /[\r\n]/g
317
+
318
+ let lastLine = { text: "", from: 0, end: 0 }
319
+
320
+ /**
321
+ * Whether the whitespace at `end` continues the one-token value that began at
322
+ * `start`: `util.inspect` separates a constructor or a marker from its
323
+ * contents (`Foo {`, `Map(1) {`, `[Object: null prototype] {`, `<ref *1> {`,
324
+ * `new Password(`).
325
+ */
326
+ const continuesAcrossSpace = (text: string, start: number, end: number): number | undefined => {
327
+ const run = text.slice(start, end)
328
+ let next = end
329
+ while (text[next] === " " || text[next] === "\t") next++
330
+ if (run === "new") return next
331
+ const prefix = /^[\w$.]+$/.test(run) || /[)\]>]$/.test(run)
332
+ return prefix && closers[text[next] ?? ""] !== undefined ? next : undefined
333
+ }
334
+
335
+ /**
336
+ * Whether the `,`, `;` or `&` at `end` separates pairs rather than sitting
337
+ * inside a value (`Zq7;Syn,thetic`, `Tr0ub(4dor)&3`): a space or another name
338
+ * follows it, and for `&`, a query parameter (`&page=2`).
339
+ */
340
+ const separatesPairs = (text: string, end: number): boolean =>
341
+ text[end] === "&"
342
+ ? /^&[A-Za-z_][\w.-]*=/.test(text.slice(end, end + 64))
343
+ : /^[,;](?:\s|$|\s*\\*["'`]?[A-Za-z_][\w.-]*\\*["'`]?\s*(?:=>|[=:]))/.test(text.slice(end, end + 64))
344
+
345
+ /**
346
+ * How far past its line every durable container may read, whatever the
347
+ * {@link Budget} has left: a pretty-printed credential object of this size
348
+ * is read whole on any row.
349
+ */
350
+ const containerWindow = 4096
351
+
352
+ /**
353
+ * How many characters durable container scans past a line may read over one
354
+ * text, in total: {@link containerBudget} times the text's length. A fixed
355
+ * window alone left a secret more than 4 KiB inside a credential-named
356
+ * container in the row, and extending it without a total made a text of
357
+ * unclosed brackets cost the square of its length (#3088). With the budget a
358
+ * large container is read whole, and a row of unclosed brackets still costs
359
+ * a constant multiple of its length plus {@link containerWindow} per value.
360
+ */
361
+ interface Budget {
362
+ remaining: number
363
+ }
364
+
365
+ const containerBudget = 4
366
+
367
+ /**
368
+ * The end of the bare value at `start`, read as a run of tokens: words,
369
+ * balanced containers anywhere in it (`{bcrypt}$2a$...`, `Tr0ub(4dor)&3`,
370
+ * `Some("x")`), inspect markers (`<Buffer 73 65>`, `<ref *1>`), and quoted
371
+ * parts. `stack` is what is open before the name: the value stops at a quote
372
+ * that closes the enclosing string, at a closer that closes the enclosing
373
+ * bracket, and before the next credential name.
374
+ */
375
+ const bareValueEnd = (
376
+ text: string,
377
+ start: number,
378
+ scope: Scope,
379
+ stack: ReadonlyArray<Open>,
380
+ budget: Budget
381
+ ): number => {
382
+ const inner = stack[innermostString(stack)]
383
+ // A durable value never reads an unclosed quote past its line, and one it
384
+ // cannot close stops at its line. A bracket reads past 4 KiB only while
385
+ // the text's scan budget lasts: each unbalanced bracket costs a bounded
386
+ // scan, and all of them together a bounded multiple of the text.
387
+ const bound = scope.diagnostic ? text.length : lineEnd(text, start)
388
+ const window = scope.diagnostic
389
+ ? text.length
390
+ : Math.min(text.length, start + Math.max(containerWindow, budget.remaining))
391
+ // Never before the opener: a closed container may already have carried the
392
+ // value past its first line.
393
+ const unclosed = (open: number, end: number): number => Math.max(open, Math.min(end, bound))
394
+ // A durable container first reads its own line. A pretty-printer only
395
+ // continues one on the next line after the opener or a member's comma
396
+ // (`apiKey: {`, `credentials: { a: 1,`), so only then does the scan read on
397
+ // through the window.
398
+ const container = (open: number): number => {
399
+ if (scope.diagnostic) return skipContainer(text, open, window)
400
+ const end = skipContainer(text, open, bound)
401
+ if (lastClosed || !/[[{(,]\s*$/.test(text.slice(open, bound))) return end
402
+ const past = skipContainer(text, open, window)
403
+ budget.remaining -= past - open
404
+ return past
405
+ }
406
+ let end = start
407
+ // The value's own first token is never the next name: in
408
+ // `api_key=lowercase-secret:end` the value only looks like a `name:` pair.
409
+ let limit = nextName(text, start + 1, scope.header)
410
+ // A header value that is one container ends with it: `"cookie":["sid=a"],`.
411
+ if (scope.header && closers[text[start]!] !== undefined) {
412
+ end = container(start)
413
+ if (!/^[^\s,;"'`\\)\]}]/.test(text[end] ?? " ")) return lastClosed ? end : unclosed(start, end)
414
+ }
415
+ while (end < text.length) {
416
+ if (end >= limit) {
417
+ if (end === limit) break
418
+ limit = nextName(text, end, scope.header)
419
+ continue
420
+ }
421
+ const char = text[end]!
422
+ if (closers[char] !== undefined) {
423
+ const open = end
424
+ end = container(open)
425
+ // An unbalanced container runs on, spaces included.
426
+ if (!lastClosed) return unclosed(open, end)
427
+ continue
428
+ }
429
+ if (char === "<") {
430
+ angleClose.lastIndex = end
431
+ const close = angleClose.exec(text)?.index
432
+ if (close !== undefined && text[close] === ">") {
433
+ end = close + 1
434
+ continue
435
+ }
436
+ }
437
+ let depth = 0
438
+ while (text[end + depth] === "\\") depth++
439
+ const quote = text[end + depth]
440
+ if (isQuote(quote)) {
441
+ // A quote at the enclosing string's depth or shallower closes it,
442
+ // whatever precedes it (`Basic abc=`), unless a credential name and
443
+ // separator precede it (`credential:"`). Inside a bracket, a field's
444
+ // value ends at a quote (`{\"token\":abc\"}`). Any other quote opens a
445
+ // quoted part of the value (`username="admin"`, `it's`).
446
+ const closesString = inner !== undefined && quote === inner.close &&
447
+ depth % (2 * (inner.depth! + 1)) <= inner.depth! &&
448
+ !nameBefore.test(text.slice(Math.max(start, end - 256), end))
449
+ if (closesString || (!scope.header && inner === undefined && stack.length > 0)) break
450
+ // A quote between two word characters is an apostrophe (`it's`).
451
+ if (depth === 0 && /\w/.test(text[end - 1] ?? "") && /\w/.test(text[end + 1] ?? "")) {
452
+ end++
453
+ continue
454
+ }
455
+ const open = end
456
+ end = skipQuoted(text, end + depth, depth, 2 * (depth + 1), bound)
457
+ if (!lastClosed) return unclosed(open, end)
458
+ continue
459
+ }
460
+ if (char === "\r" || char === "\n") break
461
+ // Inside a bracket a `,` or `;` separates members; in raw text it does
462
+ // when a space or another name follows.
463
+ if (
464
+ !scope.header &&
465
+ ((char === "&" && separatesPairs(text, end)) ||
466
+ ((char === "," || char === ";") && (stack.length > 0 || separatesPairs(text, end))))
467
+ ) break
468
+ if (!scope.header && !scope.diagnostic && (char === " " || char === "\t")) {
469
+ const next = continuesAcrossSpace(text, start, end)
470
+ if (next === undefined) break
471
+ end = next
472
+ continue
473
+ }
474
+ if (closesEnclosing(text, end, stack)) break
475
+ end++
476
+ }
477
+ while (end > start && /\s/.test(text[end - 1]!)) end--
478
+ return end
479
+ }
480
+
481
+ /**
482
+ * Whether a name ending in `key` or `keys` is not a credential's: it counts
483
+ * only where {@link isSensitiveKey} agrees, as it does for the same name as an
484
+ * object key (`apiKeys` counts; `keys`, `sortKeys`, `idempotencyKey` and
485
+ * `monkey` do not).
486
+ */
487
+ const plainKeyName = (name: string): boolean => /keys?$/i.test(name) && !isSensitiveKey(name)
488
+
489
+ /**
490
+ * The end of a credential that is a URL's password (`//x-access-token:secret@`):
491
+ * the `@` that closes the userinfo, so the host and path stay readable. Any
492
+ * other bare value keeps its end.
493
+ */
494
+ const userinfoEnd = (text: string, start: number, end: number): number => {
495
+ if (!/\/\/[^\s/@?#]*[:=]$/.test(text.slice(Math.max(0, start - 128), start))) return end
496
+ const value = text.slice(start, end)
497
+ const authority = value.search(/[/?#]/)
498
+ const at = value.lastIndexOf("@", authority < 0 ? value.length : authority)
499
+ return at > 0 ? start + at : end
500
+ }
501
+
502
+ /**
503
+ * Whether a name that matched a credential word names a credential: not a
504
+ * {@link plainKeyName}, and not a count under a plural `tokens` name
505
+ * (`max_tokens: 4096`), which is accounting.
506
+ */
507
+ const namesCredential = (name: string, value: string): boolean =>
508
+ !plainKeyName(name) &&
509
+ !(/tokens$/i.test(name) && /^["'`]?\d+(?![\w.])/.test(value))
510
+
511
+ /** The `>` that closes an inspect marker, or the line end that means there is none. */
512
+ const angleClose = /[>\r\n]/g
513
+
514
+ /**
515
+ * The `+` with which `util.inspect` continues a long string on the next line,
516
+ * its newline raw or escaped (`\\n`) when the inspected text was itself
517
+ * written into a string.
518
+ */
519
+ const continuation = /(?:\s|\\+[nrt])*\+(?:\s|\\+[nrt])*/y
520
+
521
+ /** A value an earlier pass already replaced. */
522
+ const alreadyRedacted = /^\[REDACTED\]$/
523
+
524
+ /**
525
+ * Redacts the value after each name `names` matches. A quoted value is
526
+ * consumed whole at any string-escape depth, with the pieces `util.inspect`
527
+ * joins with `+` across lines, and its quotes are kept around the
528
+ * placeholder; a bare value is consumed as a run of tokens. A name that ends
529
+ * in `key` counts only where {@link isSensitiveKey} agrees, so `sortKey` and
530
+ * `idempotencyKey` keep their values, and a count under a plural `tokens`
531
+ * name, `max_tokens: 4096`, is accounting and is kept too.
532
+ */
533
+ const redactValues = (text: string, names: RegExp, scope: Scope): string => {
534
+ const keys = new RegExp(names.source, names.flags)
535
+ let output = ""
536
+ let position = 0
537
+ // What is open before the current value. Values are skipped, so a bracket
538
+ // or quote inside a credential never counts.
539
+ const stack: Array<Open> = []
540
+ const budget: Budget = { remaining: containerBudget * text.length }
541
+ let scanned = 0
542
+ for (let match = keys.exec(text); match !== null; match = keys.exec(text)) {
543
+ const name = match[1]!
544
+ if (!scope.header && plainKeyName(name)) continue
545
+ track(stack, text, scanned, keys.lastIndex)
546
+ scanned = keys.lastIndex
547
+ let start = keys.lastIndex
548
+ let depth = 0
549
+ while (text[start + depth] === "\\") depth++
550
+ const opener = text[start + depth]
551
+ const quote = isQuote(opener) ? opener! : undefined
552
+ let end: number
553
+ if (quote !== undefined) {
554
+ start += depth
555
+ const inner = stack[innermostString(stack)]
556
+ if (inner !== undefined) {
557
+ // Inside a string written with another quote character, every
558
+ // backslash is doubled again: `inspect` renders `"a\"b"` as
559
+ // `'"a\\"b"'`.
560
+ end = skipQuoted(text, start, depth, (inner.close === quote ? 1 : 2 * (inner.depth! + 1)) * 2 * (depth + 1))
561
+ } else {
562
+ // No enclosing string is visible: raw text, or a string `track` cannot
563
+ // see. Close no earlier than a doubled string would, but never past the
564
+ // next credential name: at the undoubled close when only separators
565
+ // precede that name (`"abc\\", password=`), else at the name.
566
+ const undoubled = skipQuoted(text, start, depth)
567
+ end = skipQuoted(text, start, depth, Math.max(4, 2 * (depth + 1)))
568
+ const next = nextName(text, undoubled, false)
569
+ if (next < end) end = /^[\s,;&]*$/.test(text.slice(undoubled, next)) ? undoubled : next
570
+ }
571
+ const closed = end - start > 1 && text[end - 1] === quote
572
+ if (!closed && !scope.diagnostic) end = Math.min(end, lineEnd(text, start))
573
+ // `util.inspect` continues a long string on the next line with `+`.
574
+ if (depth === 0) {
575
+ for (continuation.lastIndex = end; continuation.test(text); continuation.lastIndex = end) {
576
+ const piece = continuation.lastIndex
577
+ if (!isQuote(text[piece])) break
578
+ end = skipQuoted(text, piece, 0)
579
+ }
580
+ }
581
+ if (alreadyRedacted.test(text.slice(start + 1, end - (text[end - 1] === quote ? 1 + depth : 0)))) continue
582
+ } else {
583
+ end = bareValueEnd(text, start, scope, stack, budget)
584
+ end = userinfoEnd(text, start, end)
585
+ // In a journal row a placeholder then a space is a value an earlier pass
586
+ // already bounded: what follows it is the next part of the line,
587
+ // `{"statusCode":401}`. A diagnostic value runs over several words, so
588
+ // there the words after a placeholder are still the value.
589
+ const bounded = !scope.diagnostic && text.startsWith(placeholder, start) &&
590
+ /\s/.test(text[start + placeholder.length] ?? "")
591
+ const kept = end === start || bounded || alreadyRedacted.test(text.slice(start, end)) ||
592
+ (/tokens$/i.test(name) && /^\d+(?![\w.])/.test(text.slice(start, end))) ||
593
+ // A Bearer or Basic value the default rules already replaced keeps its scheme.
594
+ (scope.header && /^(?:bearer|basic)\s+\[REDACTED/i.test(text.slice(start, start + 24)))
595
+ if (kept) continue
596
+ }
597
+ const escape = quote === undefined ? "" : "\\".repeat(depth)
598
+ const closed = quote !== undefined && end - start > 1 && text[end - 1] === quote
599
+ output += text.slice(position, keys.lastIndex) + escape + (quote ?? "") + placeholder +
600
+ (closed ? escape + quote : "")
601
+ position = end
602
+ scanned = end
603
+ keys.lastIndex = end
604
+ }
605
+ return output + text.slice(position)
606
+ }
607
+
608
+ /**
609
+ * What separates a flag from its value: whitespace on a command line, or
610
+ * `", "` between two elements of a JSON or inspected array.
611
+ */
612
+ const argumentSeparator = String.raw`(?:\s+|["'\x60]\s*,\s*["'\x60])`
613
+
614
+ /** A credential flag and the separator before its value. */
615
+ const credentialFlag = new RegExp(
616
+ String.raw`(?<![A-Za-z0-9_-])(--?[A-Za-z0-9_-]*${credentialWord})${argumentSeparator}`,
617
+ "gi"
618
+ )
619
+
620
+ /** `-p` and the separator before its value, unless the value is a path or a long option. */
621
+ const shortPasswordFlag = new RegExp(
622
+ String.raw`(?<![^\s"'\x60,[(])-p${argumentSeparator}(?!(?:\.{0,2}|~)\/|--)`,
623
+ "g"
624
+ )
625
+
626
+ /**
627
+ * A flag that always takes a password, and the separator before it: `plink
628
+ * -pw`, openssl's `-pass`, `-passin` and `-passout` (`pass:secret`), `-passwd`.
629
+ */
630
+ const passwordFlag = new RegExp(
631
+ String.raw`(?<![^\s"'\x60,[(])-p(?:w|ass(?:in|out|wd)?)${argumentSeparator}`,
632
+ "g"
633
+ )
634
+
635
+ /** `-p` with a value attached, unless the word is a known single-dash option. */
636
+ const attachedPasswordFlag = new RegExp(
637
+ String
638
+ .raw`(?<![^\s"'\x60,[(])-p(?!(?:rint\w*|rune|erm|ath|threads?|edantic[\w-]*|ipe|ie|g|retty|lain|rogress|rofile|arents|reserve|ass(?:in|out|wd)?|w)(?![^\s"'\x60,\]]))(?=[^\s,\]])`,
639
+ "g"
640
+ )
641
+
642
+ /** `-u` or `--user` and the separator before its value, or `-u` with its value attached (`-uadmin:pw`). */
643
+ const userFlag = new RegExp(
644
+ String.raw`(?<![^\s"'\x60,[(])(?:(?:-u|--user)(?:=|${argumentSeparator})|-u(?=[^\s=-]))`,
645
+ "g"
646
+ )
647
+
648
+ /**
649
+ * A flag that takes a password only under one command, and the separator
650
+ * before its value: `zip -P` (a port to `ssh` and `mysql`) and `redis-cli -a`
651
+ * or `--pass`. The command is looked for at most 256 characters back on the
652
+ * same command line.
653
+ */
654
+ const commandPasswordFlag = new RegExp(
655
+ String
656
+ .raw`\b(?:(?:(?:un)?zip|zipcloak)\b[^\r\n|;&]{0,256}?[\s"'\x60,[(]-P|redis-cli\b[^\r\n|;&]{0,256}?[\s"'\x60,[(](?:-a|--pass))${argumentSeparator}`,
657
+ "g"
658
+ )
659
+
660
+ /**
661
+ * An HTTP header written as a name and value pair of an array
662
+ * (`[['Authorization', 'Token x']]`), the shape `Headers` entries and many
663
+ * clients log, up to the quote that opens the value.
664
+ */
665
+ const headerPair = /(?<![A-Za-z0-9_-])(?:(?:proxy-)?authorization|(?:set-)?cookies?)\\*["'`]\s*,\s*(?=\\*["'`])/gi
666
+
667
+ /** Redacts the quoted value of each {@link headerPair}, keeping its quotes. */
668
+ const redactHeaderPairs = (text: string): string => {
669
+ const pattern = new RegExp(headerPair.source, headerPair.flags)
670
+ let output = ""
671
+ let position = 0
672
+ for (let match = pattern.exec(text); match !== null; match = pattern.exec(text)) {
673
+ let depth = 0
674
+ while (text[pattern.lastIndex + depth] === "\\") depth++
675
+ const open = pattern.lastIndex + depth
676
+ const end = skipQuoted(text, open, depth)
677
+ const close = lastClosed ? end - 1 - depth : end
678
+ if (alreadyRedacted.test(text.slice(open + 1, close))) continue
679
+ output += text.slice(position, open + 1) + placeholder
680
+ position = close
681
+ pattern.lastIndex = end
682
+ }
683
+ return output + text.slice(position)
684
+ }
685
+
686
+ /** The placeholder alone, bare, quoted or escaped: a second pass leaves it alone. */
687
+ const redactedArgument = /^(?:\\*(["'`]))?\[REDACTED\](?:\\*\1)?$/
688
+
689
+ /**
690
+ * The end of the unquoted argument at `start`. It runs to whitespace or a
691
+ * quote, and on through a quoted part written against it (`abc'def'`, which a
692
+ * shell reads as one argument) when that part closes before any whitespace
693
+ * and holds none of `,:{}[]`. Any other quote ends it: the close of a string
694
+ * the command line sits in (`{"cmd":"mysql -p abc","next":1}`).
695
+ */
696
+ const wordEnd = (text: string, start: number): number => {
697
+ let end = start
698
+ while (end < text.length && !/\s/.test(text[end]!)) {
699
+ const quote = text[end]!
700
+ if (!isQuote(quote)) {
701
+ end++
702
+ continue
703
+ }
704
+ let close = end + 1
705
+ while (close < text.length && !/[\s"'`,:{}[\]]/.test(text[close]!)) close++
706
+ if (close === end + 1 || text[close] !== quote) break
707
+ end = close + 1
708
+ }
709
+ return end
710
+ }
711
+
712
+ /**
713
+ * Redacts the value after each flag `flags` matches: a quoted value, or one
714
+ * argv word, which ends at whitespace or a quote, so the same rule reads a
715
+ * command line joined by spaces, a JSON array and an inspected array. A comma
716
+ * or a `]` is part of a password on a command line (`-p abc,def`); in an
717
+ * array the element's closing quote ends it first.
718
+ */
719
+ const redactArguments = (
720
+ text: string,
721
+ flags: RegExp,
722
+ /** Whether this flag and value are a credential at all. */
723
+ accept?: (match: RegExpExecArray, value: string) => boolean
724
+ ): string => {
725
+ const pattern = new RegExp(flags.source, flags.flags)
726
+ let output = ""
727
+ let position = 0
728
+ for (let match = pattern.exec(text); match !== null; match = pattern.exec(text)) {
729
+ const start = pattern.lastIndex
730
+ let depth = 0
731
+ while (text[start + depth] === "\\") depth++
732
+ let end = start
733
+ if (isQuote(text[start + depth])) end = skipQuoted(text, start + depth, depth)
734
+ else end = wordEnd(text, start)
735
+ const value = text.slice(start, end)
736
+ if (end === start || redactedArgument.test(value) || accept?.(match, value) === false) continue
737
+ output += text.slice(position, start) + placeholder
738
+ position = end
739
+ pattern.lastIndex = end
740
+ }
741
+ return output + text.slice(position)
742
+ }
743
+
744
+ const durable: Scope = { header: false, diagnostic: false }
745
+
746
+ /**
747
+ * Best-effort textual rules for credential shapes observed in real reports.
748
+ * Every replacement reaches a fixed point so replaying or exporting an entry
749
+ * cannot mutate it again.
750
+ *
751
+ * @since 0.1.0
752
+ * @category constants
753
+ */
754
+ export const defaultRules: ReadonlyArray<Rule> = [
755
+ {
756
+ id: "private-key-block",
757
+ // Stop an unterminated block at the next header so repeated headers cannot
758
+ // each rescan the remaining input. A valid PEM body contains no headers.
759
+ // A block with no footer is a key a bound or a producer truncated, and its
760
+ // body is as secret as a whole one's, so it runs to the next header or to
761
+ // the end of the text. It runs first: a block inside URL userinfo, once
762
+ // replaced, is userinfo the URL rule then reads whole, so a second pass
763
+ // finds nothing left to change.
764
+ pattern:
765
+ /-----BEGIN[^-]*PRIVATE KEY(?: BLOCK)?-----(?:(?!-----BEGIN)[\s\S])*?(?:-----END[^-]*-----|(?=-----BEGIN)|$)/g
766
+ },
767
+ // Git HTTPS OAuth credentials place the secret in the username. Match
768
+ // before password rewriting removes the x-oauth-basic marker.
769
+ {
770
+ id: "git-oauth-userinfo",
771
+ pattern: /(https?:\/\/)[^\s/@:"'\\]+:x-oauth-basic@/gi,
772
+ replace: "$1[REDACTED]@"
773
+ },
774
+ {
775
+ id: "url-credentials",
776
+ // The scheme is bounded rather than open. `-` is not a word character, so
777
+ // `\b[a-z][a-z0-9+.-]*` starts a fresh scan after every hyphen, and on a
778
+ // long run of that shape the rule rescanned the tail from each one: 400 kB
779
+ // of `aaaaaaaa-` cost 11 seconds, on the path every journal write and every
780
+ // log line takes. A scheme longer than 30 characters is not one this net is
781
+ // for, and the cap makes each start position cost a constant.
782
+ pattern: /\b([a-z][a-z0-9+.-]{0,30}:\/\/[^\s:@/]+):(?!\[REDACTED\]@)[^\s:@/]+@/gi,
783
+ replace: "$1:[REDACTED]@"
784
+ },
785
+ {
786
+ id: "bearer-token",
787
+ pattern: /\bBearer\s+[A-Za-z0-9._~+/-]{4,}=*/gi,
788
+ replace: "Bearer [REDACTED_TOKEN]"
789
+ },
790
+ {
791
+ id: "basic-authorization",
792
+ pattern: /\bBasic\s+[A-Za-z0-9+/]{4,}={0,2}/gi,
793
+ replace: "Basic [REDACTED_TOKEN]"
794
+ },
795
+ {
796
+ id: "jwt",
797
+ // Check the three segments once per token before looking for a header.
798
+ // Starting at every `-eyJ` would rescan a malformed token's entire tail.
799
+ // Preserve any prefix so a JWT following a hyphen still gets redacted.
800
+ pattern:
801
+ /(?<![A-Za-z0-9_-])(?=[A-Za-z0-9_-]*\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-])([A-Za-z0-9_-]*?)\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+/g,
802
+ replace: "$1[REDACTED_TOKEN]"
803
+ },
804
+ {
805
+ id: "api-key",
806
+ // Not `\b`, for the reason the assignment rule below gives: an underscore
807
+ // is a word character, so `\bsk` never fires after `ANTHROPIC_` or after
808
+ // Effect's log-span sanitizer folds `token=` into `token_`. The lookbehind
809
+ // excludes only letters and digits, so a key still reads as a key when an
810
+ // underscore or a hyphen runs into it.
811
+ pattern: /(?<![A-Za-z0-9])(?:sk|pk)[-_][A-Za-z0-9][A-Za-z0-9_-]{7,}/g,
812
+ replace: "[REDACTED_API_KEY]"
813
+ },
814
+ {
815
+ id: "github-token",
816
+ pattern: /\bgh[pousr]_[A-Za-z0-9]{20,}(?![A-Za-z0-9])/g,
817
+ replace: "[REDACTED]"
818
+ },
819
+ {
820
+ id: "github-fine-grained-token",
821
+ pattern: /\bgithub_pat_[A-Za-z0-9_]{20,}(?![A-Za-z0-9_])/g,
822
+ replace: "[REDACTED]"
823
+ },
824
+ {
825
+ id: "aws-access-key",
826
+ pattern: /\bAKIA[0-9A-Z]{16}\b/g,
827
+ replace: "[REDACTED]"
828
+ },
829
+ {
830
+ id: "slack-token",
831
+ pattern: /\bxox[baprs]-[A-Za-z0-9-]{10,}(?![A-Za-z0-9-])/g,
832
+ replace: "[REDACTED]"
833
+ },
834
+ {
835
+ id: "google-api-key",
836
+ pattern: /\bAIza[0-9A-Za-z_-]{35,}(?![0-9A-Za-z_-])/g,
837
+ replace: "[REDACTED]"
838
+ },
839
+ {
840
+ id: "assignment",
841
+ // A credential name, `:`, `=` or `=>`, and its whole value: quoted at any
842
+ // escape depth, a container, or several bare words. See `redactValues`.
843
+ pattern: credentialNames,
844
+ rewrite: (text) => redactValues(text, credentialNames, durable)
845
+ },
846
+ {
847
+ id: "credential-flag",
848
+ // `--password hunter2`, `--api-key "a b"`, or the same pair as two argv
849
+ // elements. `--token=x` is an assignment above; this is the spelling a
850
+ // command line separates with a space. The next word is the value even
851
+ // when it starts with `-`: a credential flag always takes one.
852
+ pattern: credentialFlag,
853
+ rewrite: (text) =>
854
+ redactArguments(text, credentialFlag, (match, value) => namesCredential(match[1]!.replace(/^-+/, ""), value))
855
+ },
856
+ {
857
+ id: "cookie-session-assignment",
858
+ pattern:
859
+ /(?<![A-Za-z0-9-])((?:COOKIE|SESSION)S?)(\s*=\s*)(?!\[REDACTED\])("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|(?!["'])[^\s;,]+)/gi,
860
+ replace: "$1$2[REDACTED]"
861
+ }
862
+ ]
863
+
864
+ /**
865
+ * The rules for a diagnostic: {@link defaultRules} and the spellings too broad
866
+ * for a durable row.
867
+ *
868
+ * A log line, a stderr message, and an error handed to a remote caller are
869
+ * read once, so a false positive costs a word of context. A journal row is
870
+ * permanent, so the same false positive destroys data. These rules sit on the
871
+ * diagnostic side of that line: an authorization header in any scheme, bare
872
+ * URL userinfo (`https://token@host`), a signed query parameter, and the value
873
+ * of a `-p` flag, which is a password to `mysql` and `sshpass` and a path to
874
+ * `mkdir`. A path, a port and a lowercase word attached to `-p` (`-print`,
875
+ * `-path`) are left alone.
876
+ *
877
+ * @since 1.0.0
878
+ * @category constants
879
+ */
880
+ export const diagnosticRules: ReadonlyArray<Rule> = [
881
+ ...defaultRules.map((rule): Rule =>
882
+ rule.id === "assignment"
883
+ ? {
884
+ id: "assignment",
885
+ pattern: diagnosticNames,
886
+ rewrite: (text) => redactValues(text, diagnosticNames, { header: false, diagnostic: true })
887
+ }
888
+ : rule
889
+ ),
890
+ {
891
+ id: "authorization-header",
892
+ // The header value is a credential whatever its scheme (`Token`, a cookie
893
+ // list, `Digest` with quoted parameters), so the value runs to the end of
894
+ // its line or the close of the string it was written in.
895
+ // A lookbehind-free superset lets the compiled prefilter skip ordinary prose.
896
+ pattern: /(?:proxy-)?authorization|(?:set-)?cookies?/gi,
897
+ rewrite: (text) => redactValues(text, diagnosticHeaderNames, { header: true, diagnostic: true })
898
+ },
899
+ {
900
+ id: "bearer-or-basic",
901
+ // A token shorter than the default rules' minimum. A lowercase `basic` is
902
+ // prose ("a basic example"), so only the scheme's own spellings count.
903
+ pattern: /(\b(?:[Bb]earer|BEARER|Basic|BASIC)\s+)(?!\[REDACTED)[^\s,;"'\\]+/g,
904
+ replace: "$1[REDACTED]"
905
+ },
906
+ {
907
+ id: "password-flag",
908
+ // The whole next argument, a path or a leading `-` included.
909
+ pattern: passwordFlag,
910
+ rewrite: (text) => redactArguments(text, passwordFlag)
911
+ },
912
+ {
913
+ id: "sshpass-password",
914
+ // `sshpass -p` always takes a value, even one that starts with `--`,
915
+ // which `mysql -p --database x` reads as the next option.
916
+ pattern: /(\bsshpass(?:\s+-[^\sp]\S*){0,8}\s+-p\s+)--(?!\[REDACTED\])[^\s"'`]*/g,
917
+ replace: "$1[REDACTED]"
918
+ },
919
+ {
920
+ id: "user-flag",
921
+ // `curl -u user:pass`, `--user=user:pass`, quoted or not: the whole
922
+ // argument, commas included, when it carries a `:`.
923
+ pattern: userFlag,
924
+ rewrite: (text) => redactArguments(text, userFlag, (_match, value) => value.includes(":"))
925
+ },
926
+ {
927
+ id: "header-pair",
928
+ pattern: headerPair,
929
+ rewrite: redactHeaderPairs
930
+ },
931
+ {
932
+ id: "command-password-flag",
933
+ // The whole next argument, whatever it starts with.
934
+ pattern: commandPasswordFlag,
935
+ rewrite: (text) => redactArguments(text, commandPasswordFlag)
936
+ },
937
+ {
938
+ id: "url-userinfo",
939
+ pattern: /(\/\/)(?!\[REDACTED\]@)(?![^/@\s"'\\]*:\[REDACTED\]@)[^/@\s"'\\]+@/g,
940
+ replace: "$1[REDACTED]@"
941
+ },
942
+ {
943
+ id: "signed-query",
944
+ pattern: /([?&][\w.-]*(?:key|sig|signature|auth|credential)=)(?!\[REDACTED\])[^&#\s"'\\]+/gi,
945
+ replace: "$1[REDACTED]"
946
+ },
947
+ {
948
+ id: "short-password-flag",
949
+ // A path is kept, and so is a flag: `mysql -p --database x` prompts for
950
+ // the password rather than taking one.
951
+ pattern: shortPasswordFlag,
952
+ rewrite: (text) => redactArguments(text, shortPasswordFlag)
953
+ },
954
+ {
955
+ id: "attached-password-flag",
956
+ // `-psecret`. The single-dash words that start with `p` and are not a
957
+ // password, `find -print` and the compilers' `-pthread` among them, are kept.
958
+ pattern: attachedPasswordFlag,
959
+ rewrite: (text) => redactArguments(text, attachedPasswordFlag)
960
+ }
961
+ ]
962
+
963
+ /**
964
+ * Redacts one diagnostic value with {@link diagnosticRules}: a log line, a
965
+ * stderr message, an error handed back to a caller.
966
+ *
967
+ * Every diagnostic path shares this one function, and it runs on the value
968
+ * before anything renders or bounds it. Redacting a rendering line by line, or
969
+ * after a bound, is how a secret escaped: `util.inspect` splits a long string
970
+ * over several lines, and a bound cuts a quoted value before its closing
971
+ * quote. A value past {@link maxDepth} is named rather than thrown.
972
+ *
973
+ * @since 1.0.0
974
+ * @category redaction
975
+ */
976
+ export const redactDiagnostic = (value: unknown): unknown =>
977
+ redact(value, { rules: diagnosticRules, onTooDeep: "name" })
978
+
979
+ /**
980
+ * Credential names this module refuses to persist, matched as suffixes of the
981
+ * separator-free lowercase form of a key.
982
+ *
983
+ * The list is the union of the names `packages/smithers/src/Bug.ts` redacts
984
+ * structurally, because the journal is the PERMANENT side of that pair: a bug
985
+ * report is one upload an operator reviews before it leaves the machine, while
986
+ * `flows_journal_events.payload_json` is replayed verbatim to every sync
987
+ * subscriber and time-travel consumer forever. The journal redacting less than
988
+ * the report inverts the risk, and it did: `credential`, `credentials`, `dsn`,
989
+ * and `connectionString` all round-tripped through a durable row in clear.
990
+ *
991
+ * Two families are spelled out rather than reduced to a shorter word, because
992
+ * these are SUFFIX tests: `connectionString` does not end in `connection`, and
993
+ * `secretKey` does not end in `secret`. A bare `key` suffix is not on the list
994
+ * for the opposite reason: it would redact `monkey` and `turkey`.
995
+ *
996
+ * `signature` stays, and a field it costs is renamed rather than excused. A
997
+ * name test cannot tell a MAC computed with a secret from a digest over public
998
+ * input, and both are called `signature`: `WorkspaceShare` signs its claims
999
+ * with `ShareSigner.signHmac` and that value IS the share's authorization,
1000
+ * `GrantStore` carries envelope signatures, and the GitHub, Linear and
1001
+ * Telegram webhook readers all verify a header by that name. Against those, a
1002
+ * digest whose whole purpose is to be reconciled costs nothing to rename and
1003
+ * everything to leave unreadable -- see
1004
+ * `AgentEvent.VacuousVerificationObserved.callDigest`, which was `signature`
1005
+ * and reached durable rows as the placeholder.
1006
+ */
1007
+ const sensitiveKeySuffixes = [
1008
+ "auth",
1009
+ "authorization",
1010
+ "cookie",
1011
+ "apikey",
1012
+ "token",
1013
+ "password",
1014
+ "passphrase",
1015
+ "secret",
1016
+ "secretaccesskey",
1017
+ "credential",
1018
+ "dsn",
1019
+ "connection",
1020
+ "connectionstring",
1021
+ "connectionuri",
1022
+ "connectionurl",
1023
+ "secretkey",
1024
+ "privatekey",
1025
+ "sshkey",
1026
+ "signingkey",
1027
+ "signature",
1028
+ "encryptionkey",
1029
+ "session",
1030
+ "sessionkey"
1031
+ ]
1032
+
1033
+ /**
1034
+ * A trailing `key` that is a word of its own.
1035
+ *
1036
+ * Read against the ORIGINAL key, where the separators that make `key` a word
1037
+ * still exist: `key`, `api_key`, `x-api-key`, `signing-key`. This is the shape
1038
+ * `packages/smithers/src/Bug.ts` matches, and stopping here is deliberate. Treating
1039
+ * a camel-case hump as a separator too, `/[a-z0-9]Key$/`, looks equivalent and
1040
+ * is not: it redacted `idempotencyKey`, the durable identity an effect boundary
1041
+ * replays on, out of `@smthrs/engine-store`'s journal rows. A credential-named
1042
+ * key is covered by {@link sensitiveKeySuffixes} instead, where the word before
1043
+ * `key` has to be one that names a secret.
1044
+ */
1045
+ const trailingKeyWord = /(?:^|[^A-Za-z0-9])key$/i
1046
+
1047
+ const canonicalKey = (key: string): string => key.toLowerCase().replace(/[^a-z0-9]/g, "").replace(/s$/, "")
1048
+
1049
+ /** Numeric accounting fields that contain counts, never bearer material. */
1050
+ const tokenCounterKeys: ReadonlySet<string> = new Set([
1051
+ "inputtoken",
1052
+ "outputtoken",
1053
+ "cachedinputtoken",
1054
+ "reasoningtoken",
1055
+ "totaltoken"
1056
+ ])
1057
+
1058
+ /**
1059
+ * A bare plural `tokens` is a count too: `Envelope.budget.tokens` is the run's
1060
+ * token ceiling, and redacting it erased every journaled budget. A singular
1061
+ * `token` stays bearer material even when it is numeric.
1062
+ */
1063
+ const isTokenCount = (key: string, value: unknown): boolean =>
1064
+ typeof value === "number" && Number.isSafeInteger(value) && value >= 0 &&
1065
+ (tokenCounterKeys.has(canonicalKey(key)) || key.toLowerCase() === "tokens")
1066
+
1067
+ /**
1068
+ * Whether a field name names a credential.
1069
+ *
1070
+ * Case and separators are ignored, so `api_key`, `apiKey`, and `x-api-key` are
1071
+ * all recognised, one trailing plural `s` is stripped so `credentials` reads as
1072
+ * `credential`, and a trailing `key` counts where it is a word of its own or
1073
+ * where the word before it names a secret.
1074
+ *
1075
+ * The rule is a suffix test, not a substring test. `tokenizer`, `secretary`,
1076
+ * `monkey`, and `idempotencyKey` are ordinary field names, and replacing their
1077
+ * values would destroy data in a permanent row without protecting anything.
1078
+ * That is the one place this rule is deliberately narrower than
1079
+ * `packages/smithers/src/Bug.ts`'s substring form, which can afford the false
1080
+ * positives because its output is a single report an operator reads once.
1081
+ *
1082
+ * @since 0.1.0
1083
+ * @category predicates
1084
+ */
1085
+ export const isSensitiveKey = (key: string): boolean => {
1086
+ if (trailingKeyWord.test(key)) return true
1087
+ const canonical = canonicalKey(key)
1088
+ return sensitiveKeySuffixes.some((suffix) => canonical.endsWith(suffix))
1089
+ }
1090
+
1091
+ const redactMember = (
1092
+ key: string,
1093
+ value: unknown,
1094
+ walk: (value: unknown) => unknown
1095
+ ): unknown => isSensitiveKey(key) && !isTokenCount(key, value) ? placeholder : walk(value)
1096
+
1097
+ /**
1098
+ * A field name with a credential in it replaced outright, not rewritten in place.
1099
+ *
1100
+ * Redacting part of a key changes what the key reads as. `secret=sk-` rewrites
1101
+ * to `secret=[REDACTED]`, which now ENDS in a sensitive name, so a second
1102
+ * application would replace a value the first one left alone and redaction
1103
+ * would stop being a fixed point. Naming the whole key holds the verdict still:
1104
+ * `[REDACTED]` matches no rule and names no credential. Two such keys collapse
1105
+ * into one and the later member wins, which is the fidelity trade every other
1106
+ * marker here makes.
1107
+ */
1108
+ const redactKey = (key: string, rules: ReadonlyArray<Rule>): string => {
1109
+ const redacted = redactString(key, rules)
1110
+ return redacted === key ? key : placeholder
1111
+ }
1112
+
1113
+ /**
1114
+ * How many times {@link redactString} re-applies the rules to reach a fixed
1115
+ * point. Rules feed each other: an assignment redacted inside URL userinfo
1116
+ * leaves userinfo the URL rule only then matches. Text no rule touches costs
1117
+ * one pass, and the default rules settle within three. Text that has not
1118
+ * settled after the last pass is replaced whole.
1119
+ */
1120
+ const fixedPointPasses = 8
1121
+
1122
+ /**
1123
+ * A copy of `pattern` that matches wherever `pattern` does, with no
1124
+ * lookbehind, or `null`. JavaScriptCore runs a pattern that has a lookbehind
1125
+ * in its interpreter, 30 to 70 times slower than compiled: 40 kB of prose cost
1126
+ * a Bun process 300 ms per diagnostic, where Node took 1 ms. Text the copy
1127
+ * never matches is text the rule cannot change, so the rule is skipped.
1128
+ *
1129
+ * Only a one-character negative lookbehind that starts the pattern is
1130
+ * rewritten, as a character the copy consumes, which keeps a name scan
1131
+ * starting once per identifier. Anywhere else, after `\b` or inside a
1132
+ * repeated group, the consumed character would make the copy stricter than
1133
+ * the rule, so any other lookbehind or a backreference leaves the rule
1134
+ * unfiltered.
1135
+ */
1136
+ const candidate = (pattern: RegExp): RegExp | null => {
1137
+ const cached = candidates.get(pattern)
1138
+ if (cached !== undefined) return cached
1139
+ const leading = /^\(\?<!\[(\^?)((?:\\.|[^\]\\])*)\]\)/.exec(pattern.source)
1140
+ const rest = leading === null ? pattern.source : pattern.source.slice(leading[0].length)
1141
+ const compiled = /\(\?<[!=]|\\(?:[1-9]|k<)/.test(rest)
1142
+ ? null
1143
+ : new RegExp(
1144
+ leading === null ? rest : `(?:^|[${leading[1] === "" ? "^" : ""}${leading[2]}])${rest}`,
1145
+ pattern.flags.replace(/[gy]/g, "")
1146
+ )
1147
+ candidates.set(pattern, compiled)
1148
+ return compiled
1149
+ }
1150
+
1151
+ const candidates = new WeakMap<RegExp, RegExp | null>()
1152
+
1153
+ const applyRule = (text: string, rule: Rule): string => {
1154
+ if (candidate(rule.pattern)?.test(text) === false) return text
1155
+ return rule.rewrite === undefined ? text.replace(rule.pattern, rule.replace ?? placeholder) : rule.rewrite(text)
1156
+ }
1157
+
1158
+ const redactString = (value: string, rules: ReadonlyArray<Rule>): string => {
1159
+ let text = value
1160
+ for (let pass = 0; pass < fixedPointPasses; pass++) {
1161
+ const next = rules.reduce(applyRule, text)
1162
+ if (next === text) return text
1163
+ text = next
1164
+ }
1165
+ // Rules that never settle would let an exported row keep changing, and
1166
+ // what they left may still hold a credential. Fail closed.
1167
+ return placeholder
1168
+ }
1169
+
1170
+ /**
1171
+ * Maximum number of container edges traversed from a redaction root.
1172
+ *
1173
+ * Journal event schemas never approach 256 nested containers. This bound is
1174
+ * far above practical payloads while keeping traversal safely below Node's
1175
+ * call-stack limit for hostile input.
1176
+ *
1177
+ * @since 1.0.0
1178
+ * @category constants
1179
+ */
1180
+ export const maxDepth = 256
1181
+
1182
+ /**
1183
+ * A payload nested deeper than {@link maxDepth}, under `onTooDeep: "throw"`.
1184
+ * `code` is always `depth_exceeded`; `maxDepth` is the bound that was crossed.
1185
+ *
1186
+ * @since 1.0.0
1187
+ * @category errors
1188
+ */
1189
+ export class RedactionDepthError extends Schema.TaggedError<RedactionDepthError>()(
1190
+ "@smthrs/journal/RedactionDepthError",
1191
+ { code: Schema.Literal("depth_exceeded"), maxDepth: Schema.Number, message: Schema.String }
1192
+ ) {
1193
+ override readonly name = "RedactionDepthError"
1194
+ }
1195
+
1196
+ /**
1197
+ * Options for {@link redact}.
1198
+ *
1199
+ * @since 0.1.0
1200
+ * @category models
1201
+ */
1202
+ export interface Options {
1203
+ readonly rules?: ReadonlyArray<Rule> | undefined
1204
+ /**
1205
+ * What a value nested past {@link maxDepth} does.
1206
+ *
1207
+ * `"throw"`, the default, is the journal's contract: a payload that deep is a
1208
+ * caller bug, and a durable row quietly truncated to a marker is worse than a
1209
+ * refused write. `"name"` is what a LOGGER wants, because a throw there is
1210
+ * caught one frame up and replaces EVERY argument on the line with
1211
+ * `[Unrenderable]`, so one deep member would cost the operator the whole line.
1212
+ */
1213
+ readonly onTooDeep?: "throw" | "name" | undefined
1214
+ }
1215
+
1216
+ /**
1217
+ * How many bytes a view may hold, and how many own members {@link redact} reads
1218
+ * off it, before it stops reading them.
1219
+ *
1220
+ * Enumerating a view's own properties materialises one pair per byte, so the
1221
+ * walk costs the buffer's size even though the rendering does not. Both halves
1222
+ * of the bound carry load, and neither is a bound on its own. A size alone is
1223
+ * not, because `byteLength` was read as an ordinary property and an ordinary
1224
+ * property can be shadowed: a pooled chunk that reports the bytes it has used
1225
+ * rather than the bytes it holds is a plain pattern, not an adversarial one,
1226
+ * and a 4 MB chunk reporting 12 walked one property per byte anyway. A count
1227
+ * alone is not, because a caller can hang a million properties on a ten-byte
1228
+ * view. So the size is read from the value's own internal slot, where nothing
1229
+ * a caller writes can answer for it, and the members are counted as they are
1230
+ * walked.
1231
+ *
1232
+ * @since 0.1.0
1233
+ * @category redaction
1234
+ */
1235
+ export const binaryWalkLimit = 65_536
1236
+
1237
+ /**
1238
+ * The byte-length getters, taken from the prototypes rather than from a value.
1239
+ *
1240
+ * `%TypedArray%.prototype`, `DataView.prototype` and `ArrayBuffer.prototype`
1241
+ * each define `byteLength` as an accessor over an internal slot, and applying
1242
+ * one to a value that has no such slot throws. Reading the size this way is
1243
+ * therefore a brand check as well as a measurement: a caller's own `byteLength`
1244
+ * property, accessor or not, never answers it.
1245
+ */
1246
+ const byteLengthGetters = [
1247
+ Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype) as object, "byteLength")!.get!,
1248
+ Object.getOwnPropertyDescriptor(DataView.prototype, "byteLength")!.get!,
1249
+ Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, "byteLength")!.get!
1250
+ ] as ReadonlyArray<() => number>
1251
+
1252
+ /** A value's byte length from its own internal slot, or `undefined` when it has none. */
1253
+ const byteLength = (node: object): number | undefined => {
1254
+ for (const getter of byteLengthGetters) {
1255
+ try {
1256
+ return Reflect.apply(getter, node, [])
1257
+ } catch {
1258
+ // Not that kind of view. Another getter answers, or none does and the
1259
+ // bytes are named without the members being read.
1260
+ }
1261
+ }
1262
+ return undefined
1263
+ }
1264
+
1265
+ /** The prototypes every binary view inherits from. */
1266
+ const binaryPrototypes: ReadonlySet<unknown> = new Set([
1267
+ Object.getPrototypeOf(Uint8Array.prototype),
1268
+ DataView.prototype,
1269
+ ArrayBuffer.prototype
1270
+ ])
1271
+
1272
+ /** How many prototypes {@link isBinary} climbs before it stops asking. */
1273
+ const prototypeWalkLimit = 64
1274
+
1275
+ /**
1276
+ * Whether `node` is a binary view, including one held behind a proxy.
1277
+ *
1278
+ * `ArrayBuffer.isView` reads an internal slot, and a proxy has none of its own,
1279
+ * so it answers `false` for a proxy over a view. That sent a proxied 2 MB
1280
+ * buffer to the object branch, which rebuilt it one key per byte: 2,000 ms and
1281
+ * 22.9 million characters for one logged value. A proxy forwards
1282
+ * `getPrototypeOf` to its target, so the prototype chain answers where the
1283
+ * brand check cannot. The climb is capped because a proxy is free to hand back
1284
+ * a fresh object every time it is asked, which is an endless chain.
1285
+ */
1286
+ const isBinary = (node: object): boolean => {
1287
+ if (ArrayBuffer.isView(node) || node instanceof ArrayBuffer) return true
1288
+ try {
1289
+ let prototype = Object.getPrototypeOf(node) as object | null
1290
+ for (let step = 0; prototype !== null && step < prototypeWalkLimit; step++) {
1291
+ if (binaryPrototypes.has(prototype)) return true
1292
+ prototype = Object.getPrototypeOf(prototype) as object | null
1293
+ }
1294
+ } catch {
1295
+ // A revoked proxy refuses to be asked. It is not a view, and the object
1296
+ // branch names it.
1297
+ }
1298
+ return false
1299
+ }
1300
+
1301
+ /** Own keys of a binary view that name one of its bytes rather than a property. */
1302
+ const indexKey = /^(?:0|[1-9][0-9]*)$/
1303
+
1304
+ /** `Uint8Array 1024 bytes`, or the bare marker when the value will not say. */
1305
+ const describeBinary = (node: object, size: number | undefined): string => {
1306
+ if (size === undefined) return binaryMarker
1307
+ try {
1308
+ return `${node.constructor.name} ${size} bytes`
1309
+ } catch {
1310
+ return binaryMarker
1311
+ }
1312
+ }
1313
+
1314
+ /**
1315
+ * The key {@link redact} files a binary view's size under.
1316
+ *
1317
+ * @since 0.1.0
1318
+ * @category redaction
1319
+ */
1320
+ export const binaryMarker = "[Binary]"
1321
+
1322
+ /**
1323
+ * What {@link redact} writes in place of a function or a class object.
1324
+ *
1325
+ * @since 0.1.0
1326
+ * @category redaction
1327
+ */
1328
+ export const functionMarker = "[Function]"
1329
+
1330
+ /**
1331
+ * What {@link redact} writes in place of a symbol.
1332
+ *
1333
+ * @since 0.1.0
1334
+ * @category redaction
1335
+ */
1336
+ export const symbolMarker = "[Symbol]"
1337
+
1338
+ /**
1339
+ * What {@link redact} writes past {@link maxDepth} under `onTooDeep: "name"`.
1340
+ *
1341
+ * @since 0.1.0
1342
+ * @category redaction
1343
+ */
1344
+ export const depthMarker = "[Deep]"
1345
+
1346
+ /**
1347
+ * Returns `value` with credentials removed.
1348
+ *
1349
+ * Objects and arrays are rebuilt: a field whose name {@link isSensitiveKey}
1350
+ * is replaced wholesale, every other string is run through the textual rules,
1351
+ * and a cycle is collapsed to `"[Circular]"` so the result always encodes.
1352
+ * A number, a boolean, `null` and `undefined` are returned untouched, because
1353
+ * redacting one destroys data without protecting anything. A function and a
1354
+ * symbol are NAMED, {@link functionMarker} and {@link symbolMarker}: a body, an
1355
+ * own property and a description are all text a renderer prints, and none of it
1356
+ * can be rewritten in place.
1357
+ *
1358
+ * Traversal accepts at most {@link maxDepth} container edges. A deeper value
1359
+ * throws by default, so the journal boundary can report a typed `invalid_event`
1360
+ * instead of overflowing the runtime stack. {@link Options.onTooDeep} set to
1361
+ * `"name"` writes {@link depthMarker} in its place instead, which is what a
1362
+ * logger wants.
1363
+ *
1364
+ * @since 0.1.0
1365
+ * @category redaction
1366
+ */
1367
+ export const redact = (value: unknown, options?: Options): unknown => {
1368
+ const onTooDeep = options?.onTooDeep ?? "throw"
1369
+ const rules = globalRules(options?.rules ?? defaultRules)
1370
+ /**
1371
+ * A binary view named by its type and size, with its own properties walked.
1372
+ *
1373
+ * Its bytes are not text the rules can read, and rebuilding it from its
1374
+ * entries wrote one key per byte: a 100 kB buffer rendered 1.4 MB. Handing
1375
+ * the view back untouched is not the answer either, because `redact` is the
1376
+ * journal's own write path, so a caller's `apiKey` property hung on a buffer
1377
+ * went into a durable row in clear. Name the bytes, keep walking the text.
1378
+ */
1379
+ const binary = (node: object, ancestors: WeakSet<object>, depth: number): unknown => {
1380
+ const size = byteLength(node)
1381
+ // The description is text read off the value, so it meets the rules like
1382
+ // any other text: a class name is caller data, and a view whose
1383
+ // constructor is named after a credential wrote it into a durable row.
1384
+ const entries: Array<[string, unknown]> = [[binaryMarker, redactString(describeBinary(node, size), rules)]]
1385
+ // `Object.entries` on a view materialises one pair per byte before the
1386
+ // index filter can discard them, which cost 312 ms for 1 MB on the journal
1387
+ // write path. Past the bound the bytes are named and nothing else is read,
1388
+ // so a member a caller hung on a large view is dropped rather than shown.
1389
+ // A value that will not answer from its internal slot, such as a proxy over
1390
+ // a view, is named and never enumerated at all.
1391
+ if (size !== undefined && size <= binaryWalkLimit) {
1392
+ let walked = 0
1393
+ for (const [key, field] of Object.entries(node as Record<string, unknown>)) {
1394
+ // An index is one of the bytes just named, not a property a caller set.
1395
+ if (indexKey.test(key)) continue
1396
+ // The size bounds the bytes, not the properties: a caller can hang a
1397
+ // million members on a ten-byte view, and each one costs three rule
1398
+ // scans over its name and a walk of its value.
1399
+ if (walked >= binaryWalkLimit) break
1400
+ walked++
1401
+ entries.push([
1402
+ redactKey(key, rules),
1403
+ redactMember(key, field, (value) => walk(value, ancestors, depth + 1, key))
1404
+ ])
1405
+ }
1406
+ }
1407
+ // `Object.fromEntries`, not `named[key] = …`, for the reason the object
1408
+ // branch below gives: a literal `__proto__` key would route through the
1409
+ // inherited setter and the member would vanish from the row.
1410
+ return Object.fromEntries(entries)
1411
+ }
1412
+
1413
+ const walk = (node: unknown, ancestors: WeakSet<object>, depth: number, key: string): unknown => {
1414
+ if (depth > maxDepth) {
1415
+ if (onTooDeep === "name") return depthMarker
1416
+ throw new RedactionDepthError({
1417
+ code: "depth_exceeded",
1418
+ maxDepth,
1419
+ message: `redaction depth exceeds ${maxDepth}`
1420
+ })
1421
+ }
1422
+ if (typeof node === "string") return redactString(node, rules)
1423
+ // A function, a class object or a symbol carries text a walk never reaches
1424
+ // (own properties, a description, a body), and the renderer prints it. None
1425
+ // of it can be redacted in place, so the value is named instead.
1426
+ if (typeof node === "function") return functionMarker
1427
+ if (typeof node === "symbol") return symbolMarker
1428
+ if (node === null || typeof node !== "object") return node
1429
+ if (isBinary(node)) return binary(node, ancestors, depth)
1430
+ if (ancestors.has(node)) return "[Circular]"
1431
+ // A journal row is bounded by its schema, but a LOGGED value is arbitrary:
1432
+ // the logger sends every non-Error value here, and a chain deep enough to
1433
+ // exhaust the stack would throw while the line is rendering, killing the
1434
+ // run the line describes. Past the cap the value is named, the way a cycle
1435
+ // is named.
1436
+ ancestors.add(node)
1437
+ try {
1438
+ const toJSON = (node as { toJSON?: unknown }).toJSON
1439
+ // JSON.stringify passes the containing key (or "" at the root) to toJSON.
1440
+ // Reuse it when walking the replacement; siblings may share this object.
1441
+ if (typeof toJSON === "function") return walk(toJSON.call(node, key), ancestors, depth, key)
1442
+ if (Array.isArray(node)) {
1443
+ // `map` invokes the input's species constructor, which can restore
1444
+ // credential fields and inspection hooks after the elements are walked.
1445
+ // Rebuild a plain array, preserving the original length and holes.
1446
+ const length = node.length
1447
+ const result = new Array<unknown>(length)
1448
+ const elements = new Array<unknown>(length)
1449
+ for (let index = 0; index < length; index++) {
1450
+ if (!(index in node)) continue
1451
+ elements[index] = node[index]
1452
+ result[index] = walk(elements[index], ancestors, depth + 1, String(index))
1453
+ }
1454
+ // An argv keeps a flag and its value in two elements, so no rule sees
1455
+ // `--password hunter2` whole. The element after a flag is a credential
1456
+ // when its name says so (`--token=x` carries its own, and a count under
1457
+ // `--max-tokens` is kept), or when the active rules redact it read
1458
+ // after the flag and the elements before it (`-p`, `-pw`, `-passin`,
1459
+ // `sshpass -p`) differently than alone.
1460
+ for (let index = 0; index + 1 < length; index++) {
1461
+ // The values the first loop read, never a second read: a proxy may
1462
+ // answer a second `get` differently.
1463
+ const flag = elements[index]
1464
+ const value = elements[index + 1]
1465
+ if (typeof flag !== "string" || typeof value !== "string" || !/^-[^=\s]+$/.test(flag)) continue
1466
+ const name = flag.replace(/^-+/, "")
1467
+ const before = elements.slice(Math.max(0, index - 3), index + 1).filter((element) =>
1468
+ typeof element === "string"
1469
+ ).join(" ")
1470
+ const consumes = (isSensitiveKey(name) && namesCredential(name, value)) ||
1471
+ redactString(`${before} ${value}`, rules) !== `${redactString(before, rules)} ${redactString(value, rules)}`
1472
+ if (consumes) result[index + 1] = placeholder
1473
+ }
1474
+ return result
1475
+ }
1476
+ // `result[key] = …` routes a literal `__proto__` key through the
1477
+ // inherited setter, so the field would silently become the result's
1478
+ // prototype instead of a member: the payload loses data and redaction
1479
+ // stops being a fixed point. `Object.fromEntries` defines own data
1480
+ // properties and has no such hole.
1481
+ return Object.fromEntries(
1482
+ Object.entries(node as Record<string, unknown>).map((
1483
+ [key, field]
1484
+ ) => [
1485
+ // The KEY is text too. Rewriting only values let a credential used as
1486
+ // a log annotation key reach the operator, since Effect renders an
1487
+ // annotation as `key=value`, and become an OTLP span attribute name.
1488
+ redactKey(key, rules),
1489
+ redactMember(key, field, (value) => walk(value, ancestors, depth + 1, key))
1490
+ ])
1491
+ )
1492
+ } finally {
1493
+ ancestors.delete(node)
1494
+ }
1495
+ }
1496
+ return walk(value, new WeakSet(), 0, "")
1497
+ }
1498
+
1499
+ /**
1500
+ * A redaction function, as the journal consumes it.
1501
+ *
1502
+ * @since 0.1.0
1503
+ * @category models
1504
+ */
1505
+ export type Redactor = (value: unknown) => unknown
1506
+
1507
+ /**
1508
+ * Builds a redactor over a rule set.
1509
+ *
1510
+ * @since 0.1.0
1511
+ * @category constructors
1512
+ */
1513
+ export const make = (options?: Options): Redactor => (value) => redact(value, options)
1514
+
1515
+ /**
1516
+ * Applies a redactor to an already-encoded JSON string, returning the
1517
+ * re-encoded result.
1518
+ *
1519
+ * For export and display surfaces that hold a column verbatim, a rendered
1520
+ * `state_json`, a support bundle, where the value is already encoded and must
1521
+ * not be decoded into the executable path. A string that does not parse is
1522
+ * returned untouched: validation is the caller's, and rejecting here would
1523
+ * turn a redaction concern into a schema error. Once parsing succeeds, a
1524
+ * throwing redactor or an encoding failure returns the valid JSON string
1525
+ * `"[REDACTED]"`; the original parsed text is never returned.
1526
+ *
1527
+ * @since 0.1.0
1528
+ * @category redaction
1529
+ */
1530
+ export const redactJsonString = (json: string, redactor: Redactor): string => {
1531
+ const decoded = Schema.decodeUnknownResult(UnknownFromJsonString)(json)
1532
+ if (Result.isFailure(decoded)) return json
1533
+ const attempted = Result.try(() => Schema.encodeUnknownResult(UnknownFromJsonString)(redactor(decoded.success)))
1534
+ if (Result.isFailure(attempted)) return JSON.stringify(placeholder)
1535
+ return Result.isSuccess(attempted.success) ? attempted.success.success : JSON.stringify(placeholder)
1536
+ }
1537
+
1538
+ /**
1539
+ * Redacts a stream one line at a time.
1540
+ *
1541
+ * @since 1.0.0
1542
+ * @category models
1543
+ */
1544
+ export interface LineRedactor {
1545
+ /**
1546
+ * Takes the rest of the current line, without its newline, and returns the
1547
+ * lines now safe to emit: none while a value is still open.
1548
+ */
1549
+ readonly line: (line: string) => ReadonlyArray<string>
1550
+ /**
1551
+ * Takes part of a line whose newline has not arrived. A line longer than
1552
+ * {@link maxPartialLine} is never emitted: {@link omittedLine} stands for it.
1553
+ */
1554
+ readonly part: (text: string) => void
1555
+ /** What {@link LineRedactor.flush} would emit now, without ending the stream: for a rendered snapshot. */
1556
+ readonly peek: () => ReadonlyArray<string>
1557
+ /** Ends the stream and returns whatever is still held, redacted as one block. */
1558
+ readonly flush: () => ReadonlyArray<string>
1559
+ }
1560
+
1561
+ /**
1562
+ * Lines a {@link lineRedactor} holds for one open value. Past this, or past
1563
+ * 64 KiB, it stops holding: it emits what it held, redacted, and withholds
1564
+ * every later line until the stream ends, when one placeholder stands for
1565
+ * them. Dropping lines from the middle instead changed which brackets and
1566
+ * quotes were open, and released a later secret in clear.
1567
+ *
1568
+ * @since 1.0.0
1569
+ * @category constants
1570
+ */
1571
+ export const maxHeldLines = 256
1572
+
1573
+ const maxHeldBytes = 64 * 1024
1574
+
1575
+ /** Held lines a {@link lineRedactor} asks about one by one before it asks only as the block grows by half. */
1576
+ const eagerHeldLines = 16
1577
+
1578
+ /**
1579
+ * The most brackets and strings the dropped middle of an overlong line may
1580
+ * leave open before a {@link lineRedactor} stops reading it and treats the
1581
+ * line as leaving a value open. Without it the middle's state grew by one
1582
+ * entry per bracket: 32 MiB of `[` held 1.7 GB (#3088).
1583
+ */
1584
+ const maxDroppedDepth = 1024
1585
+
1586
+ /**
1587
+ * The longest line a {@link lineRedactor} reads whole. A longer line keeps
1588
+ * its first and last 16 KiB; its middle is scanned for what it leaves open
1589
+ * and dropped.
1590
+ *
1591
+ * @since 1.0.0
1592
+ * @category constants
1593
+ */
1594
+ export const maxPartialLine = 32 * 1024
1595
+
1596
+ /**
1597
+ * What a {@link lineRedactor} emits in place of a line longer than
1598
+ * {@link maxPartialLine}.
1599
+ *
1600
+ * @since 1.0.0
1601
+ * @category constants
1602
+ */
1603
+ export const omittedLine = "[overlong line omitted]"
1604
+
1605
+ /**
1606
+ * A next line no rule redacts alone, which an open value swallows. The value
1607
+ * is open when the redacted text no longer ENDS with it: text before it may
1608
+ * contain the same word.
1609
+ */
1610
+ const continuationProbe = "\n'probe'"
1611
+
1612
+ /** Rules with the global flag each rule needs to replace every match. */
1613
+ const globalRules = (rules: ReadonlyArray<Rule>): ReadonlyArray<Rule> =>
1614
+ rules.map((rule) =>
1615
+ rule.pattern.flags.includes("g")
1616
+ ? rule
1617
+ : { ...rule, pattern: new RegExp(rule.pattern.source, `${rule.pattern.flags}g`) }
1618
+ )
1619
+
1620
+ /**
1621
+ * A redactor for a stream that must be emitted line by line: a live build
1622
+ * log, a child's stderr.
1623
+ *
1624
+ * Redacting each line alone is the flaw that leaked a private key's body, and
1625
+ * the tail of every string `util.inspect` split over several lines: only the
1626
+ * line holding the name was redacted. This one asks the rules themselves
1627
+ * whether a line leaves a value open, by checking whether the value swallows
1628
+ * a probe placed on the next line, and holds lines until it closes. The held
1629
+ * block is then redacted whole, exactly as {@link redact} redacts a string.
1630
+ *
1631
+ * A line longer than {@link maxPartialLine} is read as its first and last
1632
+ * 16 KiB. Its middle is dropped, but a bracket, quote or key block the middle
1633
+ * leaves open would have opened a value the kept ends cannot show, so then
1634
+ * the rest of the stream is withheld. Such a line is never emitted, only
1635
+ * {@link omittedLine}, and neither is any block it was held in.
1636
+ *
1637
+ * Its state is bounded whatever the stream does: the line still arriving is
1638
+ * a 16 KiB head, a 16 KiB tail and at most 1024 open brackets or strings of
1639
+ * its middle, a middle nested deeper counting as one left open; the held
1640
+ * block is bounded by {@link maxHeldLines} and 64 KiB. Its cost is linear:
1641
+ * past the first 16 held lines the block is re-read only once it has grown
1642
+ * by half, on Node and on Bun alike (#3088).
1643
+ *
1644
+ * `rules` defaults to {@link diagnosticRules}. The array is read on every
1645
+ * line, so a caller may add a rule, a session secret, as the stream runs.
1646
+ *
1647
+ * @since 1.0.0
1648
+ * @category constructors
1649
+ */
1650
+ export const lineRedactor = (rules: ReadonlyArray<Rule> = diagnosticRules): LineRedactor => {
1651
+ const held: Array<string> = []
1652
+ let heldBytes = 0
1653
+ let askAt = 0
1654
+ let heldOverlong = false
1655
+ let withheld = false
1656
+ // The line still arriving: whole up to the bound, then a head, a rolling
1657
+ // tail, and what the dropped middle leaves open.
1658
+ const edge = maxPartialLine / 2
1659
+ let head = ""
1660
+ let tail = ""
1661
+ let overlong = false
1662
+ let middle: Array<Open> = []
1663
+ let middleDeep = false
1664
+ let middleKey = false
1665
+ let carry = ""
1666
+ const resetPartial = () => {
1667
+ head = ""
1668
+ tail = ""
1669
+ overlong = false
1670
+ middle = []
1671
+ middleDeep = false
1672
+ middleKey = false
1673
+ carry = ""
1674
+ }
1675
+ const scanDropped = (dropped: string) => {
1676
+ // What the middle leaves open is read in slices, and once it is nested
1677
+ // past `maxDroppedDepth` it is not read at all: the line is then already
1678
+ // risky, and the stack held one entry per bracket of a line of any length.
1679
+ for (let from = 0; from < dropped.length && !middleDeep; from += edge) {
1680
+ track(middle, dropped, from, Math.min(dropped.length, from + edge))
1681
+ if (middle.length > maxDroppedDepth) {
1682
+ middleDeep = true
1683
+ middle = []
1684
+ }
1685
+ }
1686
+ const seen = carry + dropped
1687
+ const begin = seen.search(/-----BEGIN[^-]*PRIVATE KEY/)
1688
+ const lastBegin = seen.lastIndexOf("-----BEGIN")
1689
+ const lastEnd = seen.lastIndexOf("-----END")
1690
+ if (begin !== -1 || lastEnd !== -1) middleKey = lastBegin > lastEnd
1691
+ carry = seen.slice(-64)
1692
+ }
1693
+ const redacted = (text: string) => redactString(text, globalRules(rules))
1694
+ const release = (): ReadonlyArray<string> => {
1695
+ const text = redacted(held.join("\n"))
1696
+ const overlongBlock = heldOverlong
1697
+ held.length = 0
1698
+ heldBytes = 0
1699
+ askAt = 0
1700
+ heldOverlong = false
1701
+ return overlongBlock ? [omittedLine] : text.split("\n")
1702
+ }
1703
+ const part = (text: string): void => {
1704
+ if (withheld) return
1705
+ if (!overlong) {
1706
+ head += text
1707
+ if (head.length <= maxPartialLine) return
1708
+ overlong = true
1709
+ tail = head.slice(edge)
1710
+ head = head.slice(0, edge)
1711
+ } else tail += text
1712
+ if (tail.length > edge) {
1713
+ scanDropped(tail.slice(0, tail.length - edge))
1714
+ tail = tail.slice(-edge)
1715
+ }
1716
+ }
1717
+ const line = (rest: string): ReadonlyArray<string> => {
1718
+ if (withheld) {
1719
+ resetPartial()
1720
+ return []
1721
+ }
1722
+ part(rest)
1723
+ let text = head
1724
+ if (overlong) {
1725
+ const risky = middle.length > 0 || middleDeep || middleKey
1726
+ text = `${head} ${tail}`
1727
+ resetPartial()
1728
+ if (risky) {
1729
+ const before = held.length === 0 ? [] : [placeholder]
1730
+ held.length = 0
1731
+ heldBytes = 0
1732
+ askAt = 0
1733
+ heldOverlong = false
1734
+ withheld = true
1735
+ return [...before, omittedLine]
1736
+ }
1737
+ heldOverlong = true
1738
+ } else resetPartial()
1739
+ held.push(text)
1740
+ heldBytes += text.length + 1
1741
+ // Asking after every line re-redacted the whole block per line, so a
1742
+ // block of n bytes cost n² (#3088). Past the first lines the question is
1743
+ // asked again only once the block has grown by half, which keeps the
1744
+ // cost linear; a block that closed in between is released with the
1745
+ // lines after it, redacted as one block, which hides no less.
1746
+ if (held.length <= eagerHeldLines || heldBytes >= askAt) {
1747
+ if (redacted(held.join("\n") + continuationProbe).endsWith(continuationProbe)) return release()
1748
+ askAt = heldBytes + (heldBytes >> 1)
1749
+ }
1750
+ if (held.length <= maxHeldLines && heldBytes <= maxHeldBytes) return []
1751
+ withheld = true
1752
+ return release()
1753
+ }
1754
+ const peek = (): ReadonlyArray<string> => {
1755
+ if (withheld) return [placeholder]
1756
+ const pending = overlong ? [omittedLine] : head === "" ? [] : [head]
1757
+ if (heldOverlong) return [omittedLine]
1758
+ const text = [...held, ...(overlong ? [] : pending)]
1759
+ const out = text.length === 0 ? [] : redacted(text.join("\n")).split("\n")
1760
+ return overlong ? [...out, omittedLine] : out
1761
+ }
1762
+ return {
1763
+ line,
1764
+ part,
1765
+ peek,
1766
+ flush: () => {
1767
+ const out = head !== "" || overlong ? [...line("")] : []
1768
+ if (withheld) {
1769
+ withheld = false
1770
+ return [...out, placeholder]
1771
+ }
1772
+ return held.length === 0 ? out : [...out, ...release()]
1773
+ }
1774
+ }
1775
+ }
1776
+
1777
+ /**
1778
+ * The identity redactor, for a caller that persists payloads verbatim by
1779
+ * choice: a trusted single-tenant store, or a suite asserting on raw input.
1780
+ *
1781
+ * @since 0.1.0
1782
+ * @category constructors
1783
+ */
1784
+ export const makeNoop = (): Redactor => (value) => value