@platform-sec/sensitive-string-core 0.6.0 → 0.6.1

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 (153) hide show
  1. package/dist/bson.d.ts +28 -0
  2. package/dist/bson.d.ts.map +1 -0
  3. package/dist/bson.js +37 -0
  4. package/dist/bson.js.map +1 -0
  5. package/dist/classification.d.ts +69 -0
  6. package/dist/classification.d.ts.map +1 -0
  7. package/dist/classification.js +104 -0
  8. package/dist/classification.js.map +1 -0
  9. package/dist/compare.d.ts +36 -0
  10. package/dist/compare.d.ts.map +1 -0
  11. package/dist/compare.js +123 -0
  12. package/dist/compare.js.map +1 -0
  13. package/dist/context.d.ts +78 -0
  14. package/dist/context.d.ts.map +1 -0
  15. package/dist/context.js +110 -0
  16. package/dist/context.js.map +1 -0
  17. package/dist/derive.d.ts +110 -0
  18. package/dist/derive.d.ts.map +1 -0
  19. package/dist/derive.js +153 -0
  20. package/dist/derive.js.map +1 -0
  21. package/dist/destination.d.ts +120 -0
  22. package/dist/destination.d.ts.map +1 -0
  23. package/dist/destination.js +352 -0
  24. package/dist/destination.js.map +1 -0
  25. package/dist/deterministic.d.ts +145 -0
  26. package/dist/deterministic.d.ts.map +1 -0
  27. package/dist/deterministic.js +352 -0
  28. package/dist/deterministic.js.map +1 -0
  29. package/dist/dev-mode.d.ts +177 -0
  30. package/dist/dev-mode.d.ts.map +1 -0
  31. package/dist/dev-mode.js +503 -0
  32. package/dist/dev-mode.js.map +1 -0
  33. package/dist/environment.d.ts +181 -0
  34. package/dist/environment.d.ts.map +1 -0
  35. package/dist/environment.js +558 -0
  36. package/dist/environment.js.map +1 -0
  37. package/dist/errors.d.ts +23 -0
  38. package/dist/errors.d.ts.map +1 -0
  39. package/dist/errors.js +21 -0
  40. package/dist/errors.js.map +1 -0
  41. package/dist/global-state.d.ts +8 -0
  42. package/dist/global-state.d.ts.map +1 -0
  43. package/dist/global-state.js +22 -0
  44. package/dist/global-state.js.map +1 -0
  45. package/dist/hash.d.ts +74 -0
  46. package/dist/hash.d.ts.map +1 -0
  47. package/dist/hash.js +253 -0
  48. package/dist/hash.js.map +1 -0
  49. package/dist/hygiene.d.ts +242 -0
  50. package/dist/hygiene.d.ts.map +1 -0
  51. package/dist/hygiene.js +257 -0
  52. package/dist/hygiene.js.map +1 -0
  53. package/dist/index.d.ts +85 -0
  54. package/dist/index.d.ts.map +1 -0
  55. package/dist/index.js +233 -0
  56. package/dist/index.js.map +1 -0
  57. package/dist/mask.d.ts +51 -0
  58. package/dist/mask.d.ts.map +1 -0
  59. package/dist/mask.js +461 -0
  60. package/dist/mask.js.map +1 -0
  61. package/dist/own-property.d.ts +20 -0
  62. package/dist/own-property.d.ts.map +1 -0
  63. package/dist/own-property.js +34 -0
  64. package/dist/own-property.js.map +1 -0
  65. package/dist/patterns.d.ts +12 -0
  66. package/dist/patterns.d.ts.map +1 -0
  67. package/dist/patterns.js +19 -0
  68. package/dist/patterns.js.map +1 -0
  69. package/dist/policy.d.ts +51 -0
  70. package/dist/policy.d.ts.map +1 -0
  71. package/dist/policy.js +181 -0
  72. package/dist/policy.js.map +1 -0
  73. package/dist/probe.d.ts +141 -0
  74. package/dist/probe.d.ts.map +1 -0
  75. package/dist/probe.js +213 -0
  76. package/dist/probe.js.map +1 -0
  77. package/dist/redact-copy.d.ts +61 -0
  78. package/dist/redact-copy.d.ts.map +1 -0
  79. package/dist/redact-copy.js +217 -0
  80. package/dist/redact-copy.js.map +1 -0
  81. package/dist/redact.d.ts +141 -0
  82. package/dist/redact.d.ts.map +1 -0
  83. package/dist/redact.js +514 -0
  84. package/dist/redact.js.map +1 -0
  85. package/dist/redaction-marker.d.ts +48 -0
  86. package/dist/redaction-marker.d.ts.map +1 -0
  87. package/dist/redaction-marker.js +138 -0
  88. package/dist/redaction-marker.js.map +1 -0
  89. package/dist/redaction-observer.d.ts +108 -0
  90. package/dist/redaction-observer.d.ts.map +1 -0
  91. package/dist/redaction-observer.js +103 -0
  92. package/dist/redaction-observer.js.map +1 -0
  93. package/dist/redaction-scope.d.ts +21 -0
  94. package/dist/redaction-scope.d.ts.map +1 -0
  95. package/dist/redaction-scope.js +44 -0
  96. package/dist/redaction-scope.js.map +1 -0
  97. package/dist/reference.d.ts +101 -0
  98. package/dist/reference.d.ts.map +1 -0
  99. package/dist/reference.js +123 -0
  100. package/dist/reference.js.map +1 -0
  101. package/dist/regexp.d.ts +25 -0
  102. package/dist/regexp.d.ts.map +1 -0
  103. package/dist/regexp.js +61 -0
  104. package/dist/regexp.js.map +1 -0
  105. package/dist/release.d.ts +88 -0
  106. package/dist/release.d.ts.map +1 -0
  107. package/dist/release.js +108 -0
  108. package/dist/release.js.map +1 -0
  109. package/dist/reveal-budget.d.ts +148 -0
  110. package/dist/reveal-budget.d.ts.map +1 -0
  111. package/dist/reveal-budget.js +314 -0
  112. package/dist/reveal-budget.js.map +1 -0
  113. package/dist/revocation.d.ts +168 -0
  114. package/dist/revocation.d.ts.map +1 -0
  115. package/dist/revocation.js +541 -0
  116. package/dist/revocation.js.map +1 -0
  117. package/dist/sealing.d.ts +283 -0
  118. package/dist/sealing.d.ts.map +1 -0
  119. package/dist/sealing.js +523 -0
  120. package/dist/sealing.js.map +1 -0
  121. package/dist/sensitive-string.d.ts +919 -0
  122. package/dist/sensitive-string.d.ts.map +1 -0
  123. package/dist/sensitive-string.js +2260 -0
  124. package/dist/sensitive-string.js.map +1 -0
  125. package/dist/serialized.d.ts +33 -0
  126. package/dist/serialized.d.ts.map +1 -0
  127. package/dist/serialized.js +65 -0
  128. package/dist/serialized.js.map +1 -0
  129. package/dist/template.d.ts +69 -0
  130. package/dist/template.d.ts.map +1 -0
  131. package/dist/template.js +158 -0
  132. package/dist/template.js.map +1 -0
  133. package/dist/ttl.d.ts +104 -0
  134. package/dist/ttl.d.ts.map +1 -0
  135. package/dist/ttl.js +330 -0
  136. package/dist/ttl.js.map +1 -0
  137. package/dist/types.d.ts +859 -0
  138. package/dist/types.d.ts.map +1 -0
  139. package/dist/types.js +13 -0
  140. package/dist/types.js.map +1 -0
  141. package/dist/winston.d.ts +114 -0
  142. package/dist/winston.d.ts.map +1 -0
  143. package/dist/winston.js +170 -0
  144. package/dist/winston.js.map +1 -0
  145. package/dist/worker.d.ts +398 -0
  146. package/dist/worker.d.ts.map +1 -0
  147. package/dist/worker.js +861 -0
  148. package/dist/worker.js.map +1 -0
  149. package/dist/wrap.d.ts +287 -0
  150. package/dist/wrap.d.ts.map +1 -0
  151. package/dist/wrap.js +553 -0
  152. package/dist/wrap.js.map +1 -0
  153. package/package.json +10 -10
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.redactCopy = redactCopy;
4
+ const sensitive_string_js_1 = require("./sensitive-string.js");
5
+ const probe_js_1 = require("./probe.js");
6
+ const serialized_js_1 = require("./serialized.js");
7
+ const own_property_js_1 = require("./own-property.js");
8
+ /**
9
+ * Copy a structure with every `SensitiveString` and every serialized sensitive
10
+ * value replaced by its redaction, leaving everything else usable.
11
+ *
12
+ * This is the walk for a response body or any other value that a serializer
13
+ * other than `JSON.stringify` is about to see. `toJSON()` already redacts on
14
+ * the JSON path; what it cannot cover is a serializer that copies public
15
+ * properties (NestJS's `ClassSerializerInterceptor`, for one, which copies
16
+ * `klass` and `label` and drops the marker), and a `serialize()` result placed
17
+ * in a body, whose raw `value` `JSON.stringify` emits faithfully. Both are
18
+ * closed here: an instance becomes the class's redaction, and any object
19
+ * carrying the `__sensitive: true` marker becomes the redaction of the class it
20
+ * names, or `'[REDACTED]'` when that class is not registered.
21
+ *
22
+ * `redactInStructure` is the sibling for log lines. It flattens class
23
+ * instances into plain objects, replaces a repeat visit with `'[Circular]'`,
24
+ * and stringifies exotic objects, which is right for a log record and wrong
25
+ * for a body that still has to serialize as the class it was. This walk instead:
26
+ *
27
+ * - never mutates its input, since an entity may be shared with an ORM identity
28
+ * map or a cache;
29
+ * - keeps each object's prototype, so an entity `toJSON()` and a serializer's
30
+ * `instanceof` checks still see the class;
31
+ * - memoizes copies, so an object referenced twice stays one shared reference
32
+ * and a genuine cycle produces a cyclic copy that fails at `JSON.stringify`
33
+ * exactly as the original would;
34
+ * - passes through by reference what a property copy would break or that no
35
+ * serializer can walk: `Date`, `WeakMap`, `WeakSet`, `RegExp`, `URL`,
36
+ * `Error`, `Promise`, `ArrayBuffer` and its views (`Buffer` among them),
37
+ * boxed primitives, and anything with a `pipe()` method;
38
+ * - copies a `Map` or a `Set` entry by entry, keys included, keeping its
39
+ * prototype and memoizing it like an object, because either can hold a
40
+ * `serialize()` result and `v8.serialize`, `structuredClone`, a worker's
41
+ * `postMessage` and `util.inspect` all walk entries. `JSON.stringify` does
42
+ * not, which is why they used to pass through, and was the wrong question
43
+ * for a walk whose purpose is every other serializer.
44
+ *
45
+ * Only own enumerable string-keyed properties are visited, which is what
46
+ * `JSON.stringify` visits. A value reachable only through a prototype getter,
47
+ * a symbol key, or a non-enumerable property is not copied; on the JSON path it
48
+ * is still redacted by `toJSON()`. A key named `__proto__` is copied as the
49
+ * own data property it was, so an entity keeps the prototype it had rather
50
+ * than one the payload supplied: see `assignOwn`.
51
+ *
52
+ * @param value - The value to copy
53
+ * @returns A copy with every sensitive value redacted, or the value itself when
54
+ * it is a primitive or a pass-through object
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * const body = {
59
+ * user: userEntity, // class instance holding SensitiveString fields
60
+ * token: secret.serialize(),
61
+ * };
62
+ *
63
+ * redactCopy(body);
64
+ * // { user: UserEntity { ..., apiKey: '[REDACTED:credential]' }, token: '[REDACTED:credential]' }
65
+ * ```
66
+ */
67
+ function redactCopy(value) {
68
+ return copyRedacted(value, new WeakMap());
69
+ }
70
+ function copyRedacted(value, copies) {
71
+ if (value === null || typeof value !== 'object') {
72
+ return value;
73
+ }
74
+ // Read once, used twice: this answers both "will every `instanceof` below
75
+ // throw" and, further down, "what prototype does the copy get". An object
76
+ // that refuses to name its prototype has to be caught before the first
77
+ // `instanceof`, which walks the chain and would throw.
78
+ const prototype = (0, probe_js_1.probePrototype)(value);
79
+ if (prototype === undefined) {
80
+ return probe_js_1.UNREADABLE;
81
+ }
82
+ // An instance redacts itself, through the class's own rendering so an
83
+ // observer sees it under the 'structure' surface rather than as a coercion.
84
+ // Guarded inside the binding, because a Proxy around one answers
85
+ // `instanceof` through the prototype it forwards and then fails the brand
86
+ // check. The marker loses nothing, since the redaction is the whole of what
87
+ // the copy keeps.
88
+ if (value instanceof sensitive_string_js_1.SensitiveString) {
89
+ return (0, sensitive_string_js_1.renderStructureRedaction)(value) ?? probe_js_1.UNREADABLE;
90
+ }
91
+ if ((0, serialized_js_1.carriesSerializedMarker)(value)) {
92
+ return (0, serialized_js_1.redactSerialized)(value);
93
+ }
94
+ if (passesThrough(value)) {
95
+ return value;
96
+ }
97
+ const existing = copies.get(value);
98
+ if (existing !== undefined) {
99
+ return existing;
100
+ }
101
+ // Iteration is the array's own code once a Proxy stands in front of it,
102
+ // which may refuse `length`, an index, or the iterator. The marker stands in
103
+ // for a partial copy, memoized so a second reference gets the same answer.
104
+ if (Array.isArray(value)) {
105
+ const copy = [];
106
+ copies.set(value, copy);
107
+ try {
108
+ for (const item of value) {
109
+ copy.push(copyRedacted(item, copies));
110
+ }
111
+ }
112
+ catch {
113
+ copies.set(value, probe_js_1.UNREADABLE);
114
+ return probe_js_1.UNREADABLE;
115
+ }
116
+ return copy;
117
+ }
118
+ // Memoized before the entries are walked, like an object, so a collection
119
+ // referenced twice stays one reference and a cycle through one stays a
120
+ // cycle. Iteration is the collection's own code: a Proxy around a Map throws
121
+ // from `entries()`, and a subclass may throw from its iterator. A partial
122
+ // copy would claim to be the whole, so the marker stands in, memoized too so
123
+ // a second reference gets the same answer.
124
+ if (value instanceof Map) {
125
+ const copy = new Map();
126
+ copies.set(value, copy);
127
+ try {
128
+ for (const [key, entry] of value) {
129
+ copy.set(copyRedacted(key, copies), copyRedacted(entry, copies));
130
+ }
131
+ }
132
+ catch {
133
+ copies.set(value, probe_js_1.UNREADABLE);
134
+ return probe_js_1.UNREADABLE;
135
+ }
136
+ return finishCollection(value, copy, prototype, copies);
137
+ }
138
+ if (value instanceof Set) {
139
+ const copy = new Set();
140
+ copies.set(value, copy);
141
+ try {
142
+ for (const entry of value) {
143
+ copy.add(copyRedacted(entry, copies));
144
+ }
145
+ }
146
+ catch {
147
+ copies.set(value, probe_js_1.UNREADABLE);
148
+ return probe_js_1.UNREADABLE;
149
+ }
150
+ return finishCollection(value, copy, prototype, copies);
151
+ }
152
+ // An object that will not be enumerated cannot be copied, and an empty copy
153
+ // of the right class would be worse than saying so: a serializer downstream
154
+ // would emit it as a complete object with no fields. The marker is what
155
+ // `redactInStructure` substitutes in the same situation.
156
+ const entries = (0, probe_js_1.probeEntries)(value);
157
+ if (entries === undefined) {
158
+ return probe_js_1.UNREADABLE;
159
+ }
160
+ // Plain objects, null-prototype objects, and class instances alike, the
161
+ // prototype carried over so a serializer downstream still sees the class.
162
+ const copy = Object.create(prototype);
163
+ copies.set(value, copy);
164
+ for (const [key, child] of entries) {
165
+ (0, own_property_js_1.assignOwn)(copy, key, copyRedacted(child, copies));
166
+ }
167
+ return copy;
168
+ }
169
+ /**
170
+ * Give a copied collection the prototype and the own properties the original
171
+ * had.
172
+ *
173
+ * A subclass instance answers `instanceof` for its class, and a serializer
174
+ * downstream may check that. Its own enumerable properties are the one part of
175
+ * a Map or a Set that `JSON.stringify` does emit, so they are copied and
176
+ * redacted as an object's are.
177
+ */
178
+ function finishCollection(original, copy, prototype, copies) {
179
+ if (prototype !== Object.getPrototypeOf(copy)) {
180
+ Object.setPrototypeOf(copy, prototype);
181
+ }
182
+ const entries = (0, probe_js_1.probeEntries)(original);
183
+ if (entries === undefined) {
184
+ copies.set(original, probe_js_1.UNREADABLE);
185
+ return probe_js_1.UNREADABLE;
186
+ }
187
+ for (const [key, child] of entries) {
188
+ (0, own_property_js_1.assignOwn)(copy, key, copyRedacted(child, copies));
189
+ }
190
+ return copy;
191
+ }
192
+ /**
193
+ * Objects whose state lives in internal slots, or that no serializer can walk.
194
+ *
195
+ * Copying the prototype and the own properties of any of these would produce
196
+ * something that answers `instanceof` and then throws on use, or lose the
197
+ * message of an Error. A `WeakMap` or `WeakSet` cannot be enumerated by anyone,
198
+ * this walk included, and `structuredClone` refuses both, so nothing inside one
199
+ * reaches a serializer. `Map` and `Set` are not here: either can hold anything,
200
+ * and a serializer that walks entries finds it, so they are copied above.
201
+ */
202
+ function passesThrough(value) {
203
+ return (value instanceof Date ||
204
+ value instanceof WeakMap ||
205
+ value instanceof WeakSet ||
206
+ value instanceof RegExp ||
207
+ value instanceof URL ||
208
+ value instanceof Error ||
209
+ value instanceof Promise ||
210
+ value instanceof ArrayBuffer ||
211
+ ArrayBuffer.isView(value) ||
212
+ value instanceof String ||
213
+ value instanceof Number ||
214
+ value instanceof Boolean ||
215
+ typeof (0, probe_js_1.probeProperty)(value, 'pipe') === 'function');
216
+ }
217
+ //# sourceMappingURL=redact-copy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redact-copy.js","sourceRoot":"","sources":["../src/redact-copy.ts"],"names":[],"mappings":";;AAgEA,gCAEC;AAlED,+DAAkF;AAClF,yCAAqF;AACrF,mDAA4E;AAC5E,uDAA8C;AAE9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,SAAgB,UAAU,CAAC,KAAc;IACvC,OAAO,YAAY,CAAC,KAAK,EAAE,IAAI,OAAO,EAAE,CAAC,CAAC;AAC5C,CAAC;AAED,SAAS,YAAY,CAAC,KAAc,EAAE,MAAgC;IACpE,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAChD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,0EAA0E;IAC1E,0EAA0E;IAC1E,uEAAuE;IACvE,uDAAuD;IACvD,MAAM,SAAS,GAAG,IAAA,yBAAc,EAAC,KAAK,CAA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;QACrB,KAAK,YAAY,OAAO;QACxB,KAAK,YAAY,OAAO;QACxB,KAAK,YAAY,MAAM;QACvB,KAAK,YAAY,GAAG;QACpB,KAAK,YAAY,KAAK;QACtB,KAAK,YAAY,OAAO;QACxB,KAAK,YAAY,WAAW;QAC5B,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC;QACzB,KAAK,YAAY,MAAM;QACvB,KAAK,YAAY,MAAM;QACvB,KAAK,YAAY,OAAO;QACxB,OAAO,IAAA,wBAAa,EAAC,KAAK,EAAE,MAAM,CAAC,KAAK,UAAU,CACnD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,141 @@
1
+ import type { SensitivityClass } from './types.js';
2
+ /**
3
+ * Recursively redact SensitiveString instances in a data structure.
4
+ *
5
+ * Handles:
6
+ * - Primitive values (returned as-is)
7
+ * - SensitiveString instances (redacted via toString())
8
+ * - Serialized sensitive values, the `{ __sensitive: true, klass, value }` shape
9
+ * `serialize()` produces (replaced by the redaction of the class they name,
10
+ * or '[REDACTED]' for a class that is not registered)
11
+ * - Arrays, Map, and Set (recursively processed)
12
+ * - Date (preserved as-is)
13
+ * - Error (name, message, and stack copied into a plain object)
14
+ * - Plain objects and class instances (own enumerable properties recursively
15
+ * processed into a new plain object)
16
+ * - Objects with a toJSON() method (their JSON form, recursively processed; a
17
+ * reveal() from inside the method is refused, and the object's own
18
+ * properties are walked instead)
19
+ * - Other exotic objects such as typed arrays or promises (string form)
20
+ * - Circular references (replaced with '[Circular]')
21
+ *
22
+ * @param obj - The value to process
23
+ * @returns A new structure with all SensitiveString instances redacted
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const data = {
28
+ * user: 'alice',
29
+ * token: new SensitiveString('secret123', 'credential', 'token'),
30
+ * nested: {
31
+ * email: new SensitiveString('alice@example.com', 'sensitive', 'email')
32
+ * }
33
+ * };
34
+ *
35
+ * const safe = redactInStructure(data);
36
+ * // {
37
+ * // user: 'alice',
38
+ * // token: '[REDACTED:credential]',
39
+ * // nested: { email: '[REDACTED:sensitive]' }
40
+ * // }
41
+ * ```
42
+ */
43
+ export declare function redactInStructure(obj: unknown): unknown;
44
+ /**
45
+ * Prepare a value for safe logging.
46
+ * Wrapper around redactInStructure with a clearer name for log contexts.
47
+ *
48
+ * @param obj - The value to make safe for logging
49
+ * @returns A new structure with all SensitiveString instances redacted
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * console.log('Processing request:', safeLog(payload));
54
+ * ```
55
+ */
56
+ export declare function safeLog<T>(obj: T): unknown;
57
+ /**
58
+ * Options for safe JSON stringify.
59
+ */
60
+ export interface SafeStringifyOptions {
61
+ /** Pretty print with indentation */
62
+ pretty?: boolean;
63
+ /** Custom replacer function (runs after redaction) */
64
+ replacer?: (key: string, value: unknown) => unknown;
65
+ }
66
+ /**
67
+ * Safely stringify a value to JSON with all sensitive data redacted.
68
+ *
69
+ * Always returns a string and never throws on the input's shape: a BigInt
70
+ * becomes its decimal string, and a value JSON cannot represent at all becomes
71
+ * 'null'. Both are cases where JSON.stringify alone would take a logging call
72
+ * down or hand back an undefined that its own type declaration denies.
73
+ *
74
+ * @param obj - The value to stringify
75
+ * @param options - Stringify options
76
+ * @returns JSON string with redacted sensitive values, or the string 'null'
77
+ * for a value JSON cannot represent
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const json = safeStringify({
82
+ * user: 'alice',
83
+ * token: new SensitiveString('secret', 'credential', 'token')
84
+ * });
85
+ * // '{"user":"alice","token":"[REDACTED:credential]"}'
86
+ * ```
87
+ */
88
+ export declare function safeStringify(obj: unknown, options?: SafeStringifyOptions): string;
89
+ /**
90
+ * Create a logger-compatible serializer that redacts sensitive values.
91
+ * Can be used with pino, winston, or other logging libraries that support
92
+ * custom serializers.
93
+ *
94
+ * @returns A serializer function compatible with most logging libraries
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * // With pino
99
+ * const logger = pino({
100
+ * serializers: {
101
+ * req: createSensitiveSerializer(),
102
+ * payload: createSensitiveSerializer(),
103
+ * }
104
+ * });
105
+ * ```
106
+ */
107
+ export declare function createSensitiveSerializer(): (value: unknown) => unknown;
108
+ /**
109
+ * Check if a value contains any sensitive value: a SensitiveString instance,
110
+ * or a serialized one, which holds the raw value in the open.
111
+ * Useful for validation or debugging.
112
+ *
113
+ * Walks arrays, Map and Set entries, plain objects, and the own enumerable
114
+ * properties of class instances.
115
+ *
116
+ * @param obj - The value to check
117
+ * @returns true if any SensitiveString or serialized sensitive value is found
118
+ */
119
+ export declare function containsSensitive(obj: unknown): boolean;
120
+ /**
121
+ * Collect the sensitivity classes present anywhere in a value.
122
+ *
123
+ * The traversal matches {@link containsSensitive} exactly: arrays, Map keys and
124
+ * values, Set values, plain objects, and the own enumerable properties of class
125
+ * instances. It answers "which classes are in here" where `containsSensitive`
126
+ * answers "is anything in here", so a caller can act on `credential` without
127
+ * acting on `sensitive`. A serialized value contributes the class it names when
128
+ * that class is registered; one naming an unregistered class is counted by
129
+ * `containsSensitive` but has no class to contribute here.
130
+ *
131
+ * @param obj - The value to inspect
132
+ * @returns The set of sensitivity classes found, empty when there are none
133
+ *
134
+ * @example
135
+ * ```ts
136
+ * const classes = sensitiveClassesIn({ token: apiKey, email: userEmail });
137
+ * classes.has('credential'); // true
138
+ * ```
139
+ */
140
+ export declare function sensitiveClassesIn(obj: unknown): Set<SensitivityClass>;
141
+ //# sourceMappingURL=redact.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redact.d.ts","sourceRoot":"","sources":["../src/redact.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAoEnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAEvD;AA4JD;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,OAAO,CAE1C;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,oCAAoC;IACpC,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,sDAAsD;IACtD,QAAQ,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;CACrD;AAyCD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,GAAE,oBAAyB,GAAG,MAAM,CAkBtF;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,yBAAyB,IAAI,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAEvE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,OAAO,CAEvD;AA2ED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,CAAC,gBAAgB,CAAC,CAEtE"}