@hackylabs/deep-redact 2.2.1 → 3.0.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 (36) hide show
  1. package/README.md +23 -73
  2. package/dist/cjs/index.js +10 -138
  3. package/dist/cjs/utils/index.js +429 -0
  4. package/dist/cjs/utils/standardTransformers/bigint.js +10 -0
  5. package/dist/cjs/utils/standardTransformers/date.js +9 -0
  6. package/dist/cjs/utils/standardTransformers/error.js +16 -0
  7. package/dist/cjs/utils/standardTransformers/index.js +19 -0
  8. package/dist/cjs/utils/standardTransformers/map.js +9 -0
  9. package/dist/cjs/utils/standardTransformers/regex.js +15 -0
  10. package/dist/cjs/utils/standardTransformers/set.js +9 -0
  11. package/dist/cjs/utils/standardTransformers/url.js +9 -0
  12. package/dist/esm/index.mjs +9 -135
  13. package/dist/esm/utils/index.mjs +423 -0
  14. package/dist/esm/utils/standardTransformers/bigint.js +6 -0
  15. package/dist/esm/utils/standardTransformers/date.js +5 -0
  16. package/dist/esm/utils/standardTransformers/error.js +12 -0
  17. package/dist/esm/utils/standardTransformers/index.js +16 -0
  18. package/dist/esm/utils/standardTransformers/map.js +5 -0
  19. package/dist/esm/utils/standardTransformers/regex.js +11 -0
  20. package/dist/esm/utils/standardTransformers/set.js +5 -0
  21. package/dist/esm/utils/standardTransformers/url.js +5 -0
  22. package/dist/types/index.d.ts +3 -41
  23. package/dist/types/types.d.ts +48 -17
  24. package/dist/types/utils/index.d.ts +130 -0
  25. package/dist/types/utils/standardTransformers/bigint.d.ts +2 -0
  26. package/dist/types/utils/standardTransformers/date.d.ts +2 -0
  27. package/dist/types/utils/standardTransformers/error.d.ts +2 -0
  28. package/dist/types/utils/standardTransformers/index.d.ts +2 -0
  29. package/dist/types/utils/standardTransformers/map.d.ts +2 -0
  30. package/dist/types/utils/standardTransformers/regex.d.ts +2 -0
  31. package/dist/types/utils/standardTransformers/set.d.ts +2 -0
  32. package/dist/types/utils/standardTransformers/url.d.ts +2 -0
  33. package/package.json +66 -13
  34. package/dist/cjs/utils/redactorUtils.js +0 -252
  35. package/dist/esm/utils/redactorUtils.mjs +0 -253
  36. package/dist/types/utils/redactorUtils.d.ts +0 -91
@@ -1,252 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const defaultConfig = {
4
- stringTests: [],
5
- blacklistedKeys: [],
6
- partialStringTests: [],
7
- blacklistedKeysTransformed: [],
8
- fuzzyKeyMatch: false,
9
- caseSensitiveKeyMatch: true,
10
- retainStructure: false,
11
- remove: false,
12
- replaceStringByLength: false,
13
- replacement: '[REDACTED]',
14
- types: ['string'],
15
- };
16
- class RedactorUtils {
17
- constructor(customConfig) {
18
- var _a, _b, _c, _d;
19
- /**
20
- * The configuration for the redaction.
21
- * @private
22
- */
23
- this.config = defaultConfig;
24
- /**
25
- * Get the configuration for an object key. This will check the key against the transformed blacklisted keys.
26
- * @private
27
- * @param {string} key The key of the configuration to get.
28
- * @returns {Required<BlacklistKeyConfig> | undefined} The configuration for the key.
29
- */
30
- this.getBlacklistedKeyConfig = (key) => {
31
- var _a;
32
- if (!key)
33
- return undefined;
34
- return (_a = this.config.blacklistedKeysTransformed) === null || _a === void 0 ? void 0 : _a.find((redactableKey) => {
35
- return RedactorUtils.complexKeyMatch(key, redactableKey);
36
- });
37
- };
38
- /**
39
- * Get the recursion configuration for a key. This will check the key against the transformed blacklisted keys.
40
- * If the key is found, the configuration for the key will be returned, otherwise undefined.
41
- * @private
42
- * @param {string} key The key of the configuration to get.
43
- * @returns {Required<Pick<BlacklistKeyConfig, 'remove' | 'replacement' | 'retainStructure'>>} The configuration for the key.
44
- */
45
- this.getRecursionConfig = (key) => {
46
- const fallback = {
47
- remove: this.config.remove,
48
- replacement: this.config.replacement,
49
- retainStructure: this.config.retainStructure,
50
- };
51
- if (!key)
52
- return fallback;
53
- const blacklistedKeyConfig = this.getBlacklistedKeyConfig(key);
54
- if (!blacklistedKeyConfig)
55
- return fallback;
56
- return {
57
- remove: blacklistedKeyConfig.remove,
58
- replacement: blacklistedKeyConfig.replacement,
59
- retainStructure: blacklistedKeyConfig.retainStructure,
60
- };
61
- };
62
- /**
63
- * Determine if a key should be redacted. This will check the key against the blacklisted keys, using the default configuration.
64
- * @private
65
- * @param {string} key The key to check.
66
- * @returns {boolean} Whether the key should be redacted.
67
- */
68
- this.shouldRedactObjectValue = (key) => {
69
- if (!key)
70
- return false;
71
- return this.config.blacklistedKeysTransformed.some((redactableKey) => {
72
- return RedactorUtils.complexKeyMatch(key, redactableKey);
73
- });
74
- };
75
- /**
76
- * Redact a string. This will redact the string based on the configuration, redacting the string if it matches a pattern or if the parent key should be redacted.
77
- * @private
78
- * @param value
79
- * @param replacement
80
- * @param remove
81
- * @param shouldRedact
82
- */
83
- this.redactString = (value, replacement, remove, shouldRedact) => {
84
- if (!value || typeof value !== 'string')
85
- return value;
86
- const maybePartiallyRedacted = this.partialStringRedact(value);
87
- const { stringTests } = this.config;
88
- if (!shouldRedact) {
89
- const result = stringTests === null || stringTests === void 0 ? void 0 : stringTests.map((test) => {
90
- if (test instanceof RegExp) {
91
- if (!test.test(maybePartiallyRedacted))
92
- return maybePartiallyRedacted;
93
- if (remove)
94
- return undefined;
95
- if (typeof replacement === 'function')
96
- return replacement(maybePartiallyRedacted);
97
- if (this.config.replaceStringByLength)
98
- return replacement.repeat(maybePartiallyRedacted.length);
99
- return replacement;
100
- }
101
- if (remove && test.pattern.test(maybePartiallyRedacted))
102
- return undefined;
103
- return test.replacer(maybePartiallyRedacted, test.pattern);
104
- }).filter(Boolean)[0];
105
- if (result)
106
- return result;
107
- if (remove)
108
- return undefined;
109
- return maybePartiallyRedacted;
110
- }
111
- if (remove)
112
- return undefined;
113
- if (typeof replacement === 'function')
114
- return replacement(maybePartiallyRedacted);
115
- if (this.config.replaceStringByLength)
116
- return replacement.repeat(maybePartiallyRedacted.length);
117
- return replacement;
118
- };
119
- /**
120
- * Redact a primitive value. This will redact the value if it is a supported type, not an object or array, otherwise it will return the value unchanged.
121
- * @private
122
- * @param {unknown} value The value to redact.
123
- * @param {Transformer | string} replacement The replacement value for redacted data.
124
- * @param {boolean} remove Whether the redacted data should be removed.
125
- * @param {boolean} shouldRedact Whether the value should be redacted based on the parent key.
126
- * @returns {unknown} The redacted value.
127
- */
128
- this.redactPrimitive = (value, replacement, remove, shouldRedact) => {
129
- if (!this.config.types.includes(typeof value))
130
- return value;
131
- if (remove && shouldRedact && typeof value !== 'string')
132
- return undefined;
133
- if (typeof value === 'string')
134
- return this.redactString(value, replacement, remove, shouldRedact);
135
- if (!shouldRedact)
136
- return value;
137
- if (typeof replacement === 'function')
138
- return replacement(value);
139
- return replacement;
140
- };
141
- /**
142
- * Redact an array. This will redact each value in the array using the `recurse` method.
143
- * @private
144
- * @param {unknown[]} value The array to redact.
145
- * @returns {unknown[]} The redacted array.
146
- */
147
- this.redactArray = (value) => value.map((val) => this.recurse(val));
148
- /**
149
- * Redact an object. This will recursively redact the object based on the configuration, redacting the keys and values as required.
150
- * @param {Object} value The object to redact.
151
- * @param {string | null} key The key of the object if it is part of another object.
152
- * @param {boolean} parentShouldRedact Whether the item should be redacted based on the key within the parent object.
153
- */
154
- this.redactObject = (value, key, parentShouldRedact) => {
155
- return Object.fromEntries(Object.entries(value).map(([prop, val]) => {
156
- const shouldRedact = parentShouldRedact || this.shouldRedactObjectValue(prop);
157
- if (shouldRedact) {
158
- const { remove } = this.getRecursionConfig(prop);
159
- if (remove)
160
- return [];
161
- }
162
- return [prop, this.recurse(val, key !== null && key !== void 0 ? key : prop, shouldRedact)];
163
- }).filter(([prop]) => prop !== undefined));
164
- };
165
- this.partialStringRedact = (value) => {
166
- const { partialStringTests } = this.config;
167
- if (partialStringTests.length === 0)
168
- return value;
169
- let result = value;
170
- partialStringTests.forEach((test) => {
171
- result = test.replacer(result, test.pattern);
172
- });
173
- return result;
174
- };
175
- /**
176
- * Redact a value. If the value is an object or array, the redaction will be performed recursively, otherwise the value will be redacted if it is a supported type using the `replace` method.
177
- * @private
178
- * @param {unknown} value The value to redact.
179
- * @param {string | null} key The key of the value if it is part of an object.
180
- * @param {boolean} parentShouldRedact Whether the parent object should be redacted.
181
- * @returns {unknown} The redacted value.
182
- */
183
- this.recurse = (value, key, parentShouldRedact) => {
184
- if (value === null)
185
- return value;
186
- const { remove, replacement, retainStructure } = this.getRecursionConfig(key);
187
- if (!(value instanceof Object))
188
- return this.redactPrimitive(value, replacement, remove, Boolean(key && parentShouldRedact));
189
- if (parentShouldRedact) {
190
- if (!retainStructure) {
191
- return typeof replacement === 'function'
192
- ? replacement(value)
193
- : replacement;
194
- }
195
- }
196
- if (Array.isArray(value))
197
- return this.redactArray(value);
198
- return this.redactObject(value, key, parentShouldRedact);
199
- };
200
- this.config = Object.assign(Object.assign(Object.assign({}, defaultConfig), customConfig), { partialStringTests: (_a = customConfig.partialStringTests) !== null && _a !== void 0 ? _a : [], blacklistedKeys: (_b = customConfig.blacklistedKeys) !== null && _b !== void 0 ? _b : [], blacklistedKeysTransformed: (_d = (_c = customConfig.blacklistedKeys) === null || _c === void 0 ? void 0 : _c.map((key) => {
201
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
202
- const isObject = !(typeof key === 'string' || key instanceof RegExp);
203
- const setKey = isObject ? key.key : key;
204
- const fallback = {
205
- fuzzyKeyMatch: (_a = customConfig.fuzzyKeyMatch) !== null && _a !== void 0 ? _a : defaultConfig.fuzzyKeyMatch,
206
- caseSensitiveKeyMatch: (_b = customConfig.caseSensitiveKeyMatch) !== null && _b !== void 0 ? _b : defaultConfig.caseSensitiveKeyMatch,
207
- retainStructure: (_c = customConfig.retainStructure) !== null && _c !== void 0 ? _c : defaultConfig.retainStructure,
208
- replacement: (_d = customConfig.replacement) !== null && _d !== void 0 ? _d : defaultConfig.replacement,
209
- remove: (_e = customConfig.remove) !== null && _e !== void 0 ? _e : defaultConfig.remove,
210
- key: setKey,
211
- };
212
- if (isObject) {
213
- return {
214
- fuzzyKeyMatch: (_f = key.fuzzyKeyMatch) !== null && _f !== void 0 ? _f : fallback.fuzzyKeyMatch,
215
- caseSensitiveKeyMatch: (_g = key.caseSensitiveKeyMatch) !== null && _g !== void 0 ? _g : fallback.caseSensitiveKeyMatch,
216
- retainStructure: (_h = key.retainStructure) !== null && _h !== void 0 ? _h : fallback.retainStructure,
217
- replacement: (_j = key.replacement) !== null && _j !== void 0 ? _j : fallback.replacement,
218
- remove: (_k = key.remove) !== null && _k !== void 0 ? _k : fallback.remove,
219
- key: setKey,
220
- };
221
- }
222
- return fallback;
223
- })) !== null && _d !== void 0 ? _d : [] });
224
- }
225
- }
226
- /**
227
- * Normalise a string for comparison. This will convert the string to lowercase and remove any non-word characters.
228
- * @private
229
- * @param str The string to normalise.
230
- * @returns {string} The normalised string.
231
- */
232
- RedactorUtils.normaliseString = (str) => str.toLowerCase().replaceAll(/\W/g, '');
233
- /**
234
- * Determine if a key matches a given blacklistedKeyConfig. This will check the key against the blacklisted keys,
235
- * using the configuration option for the given key falling back to the default configuration.
236
- * @private
237
- * @param {string} key The key to check.
238
- * @param {BlacklistKeyConfig} blacklistKeyConfig The configuration for the key.
239
- * @returns {boolean} Whether the key should be redacted.
240
- */
241
- RedactorUtils.complexKeyMatch = (key, blacklistKeyConfig) => {
242
- if (blacklistKeyConfig.key instanceof RegExp)
243
- return blacklistKeyConfig.key.test(key);
244
- if (blacklistKeyConfig.fuzzyKeyMatch && blacklistKeyConfig.caseSensitiveKeyMatch)
245
- return key.includes(blacklistKeyConfig.key);
246
- if (blacklistKeyConfig.fuzzyKeyMatch && !blacklistKeyConfig.caseSensitiveKeyMatch)
247
- return RedactorUtils.normaliseString(key).includes(RedactorUtils.normaliseString(blacklistKeyConfig.key));
248
- if (!blacklistKeyConfig.fuzzyKeyMatch && blacklistKeyConfig.caseSensitiveKeyMatch)
249
- return key === blacklistKeyConfig.key;
250
- return RedactorUtils.normaliseString(blacklistKeyConfig.key) === RedactorUtils.normaliseString(key);
251
- };
252
- exports.default = RedactorUtils;
@@ -1,253 +0,0 @@
1
- const defaultConfig = {
2
- stringTests: [],
3
- blacklistedKeys: [],
4
- partialStringTests: [],
5
- blacklistedKeysTransformed: [],
6
- fuzzyKeyMatch: false,
7
- caseSensitiveKeyMatch: true,
8
- retainStructure: false,
9
- remove: false,
10
- replaceStringByLength: false,
11
- replacement: '[REDACTED]',
12
- types: ['string'],
13
- };
14
- class RedactorUtils {
15
- /**
16
- * The configuration for the redaction.
17
- * @private
18
- */
19
- config = defaultConfig;
20
- constructor(customConfig) {
21
- this.config = {
22
- ...defaultConfig,
23
- ...customConfig,
24
- partialStringTests: customConfig.partialStringTests ?? [],
25
- blacklistedKeys: customConfig.blacklistedKeys ?? [],
26
- blacklistedKeysTransformed: customConfig.blacklistedKeys?.map((key) => {
27
- const isObject = !(typeof key === 'string' || key instanceof RegExp);
28
- const setKey = isObject ? key.key : key;
29
- const fallback = {
30
- fuzzyKeyMatch: customConfig.fuzzyKeyMatch ?? defaultConfig.fuzzyKeyMatch,
31
- caseSensitiveKeyMatch: customConfig.caseSensitiveKeyMatch ?? defaultConfig.caseSensitiveKeyMatch,
32
- retainStructure: customConfig.retainStructure ?? defaultConfig.retainStructure,
33
- replacement: customConfig.replacement ?? defaultConfig.replacement,
34
- remove: customConfig.remove ?? defaultConfig.remove,
35
- key: setKey,
36
- };
37
- if (isObject) {
38
- return {
39
- fuzzyKeyMatch: key.fuzzyKeyMatch ?? fallback.fuzzyKeyMatch,
40
- caseSensitiveKeyMatch: key.caseSensitiveKeyMatch ?? fallback.caseSensitiveKeyMatch,
41
- retainStructure: key.retainStructure ?? fallback.retainStructure,
42
- replacement: key.replacement ?? fallback.replacement,
43
- remove: key.remove ?? fallback.remove,
44
- key: setKey,
45
- };
46
- }
47
- return fallback;
48
- }) ?? [],
49
- };
50
- }
51
- /**
52
- * Normalise a string for comparison. This will convert the string to lowercase and remove any non-word characters.
53
- * @private
54
- * @param str The string to normalise.
55
- * @returns {string} The normalised string.
56
- */
57
- static normaliseString = (str) => str.toLowerCase().replaceAll(/\W/g, '');
58
- /**
59
- * Determine if a key matches a given blacklistedKeyConfig. This will check the key against the blacklisted keys,
60
- * using the configuration option for the given key falling back to the default configuration.
61
- * @private
62
- * @param {string} key The key to check.
63
- * @param {BlacklistKeyConfig} blacklistKeyConfig The configuration for the key.
64
- * @returns {boolean} Whether the key should be redacted.
65
- */
66
- static complexKeyMatch = (key, blacklistKeyConfig) => {
67
- if (blacklistKeyConfig.key instanceof RegExp)
68
- return blacklistKeyConfig.key.test(key);
69
- if (blacklistKeyConfig.fuzzyKeyMatch && blacklistKeyConfig.caseSensitiveKeyMatch)
70
- return key.includes(blacklistKeyConfig.key);
71
- if (blacklistKeyConfig.fuzzyKeyMatch && !blacklistKeyConfig.caseSensitiveKeyMatch)
72
- return RedactorUtils.normaliseString(key).includes(RedactorUtils.normaliseString(blacklistKeyConfig.key));
73
- if (!blacklistKeyConfig.fuzzyKeyMatch && blacklistKeyConfig.caseSensitiveKeyMatch)
74
- return key === blacklistKeyConfig.key;
75
- return RedactorUtils.normaliseString(blacklistKeyConfig.key) === RedactorUtils.normaliseString(key);
76
- };
77
- /**
78
- * Get the configuration for an object key. This will check the key against the transformed blacklisted keys.
79
- * @private
80
- * @param {string} key The key of the configuration to get.
81
- * @returns {Required<BlacklistKeyConfig> | undefined} The configuration for the key.
82
- */
83
- getBlacklistedKeyConfig = (key) => {
84
- if (!key)
85
- return undefined;
86
- return this.config.blacklistedKeysTransformed?.find((redactableKey) => {
87
- return RedactorUtils.complexKeyMatch(key, redactableKey);
88
- });
89
- };
90
- /**
91
- * Get the recursion configuration for a key. This will check the key against the transformed blacklisted keys.
92
- * If the key is found, the configuration for the key will be returned, otherwise undefined.
93
- * @private
94
- * @param {string} key The key of the configuration to get.
95
- * @returns {Required<Pick<BlacklistKeyConfig, 'remove' | 'replacement' | 'retainStructure'>>} The configuration for the key.
96
- */
97
- getRecursionConfig = (key) => {
98
- const fallback = {
99
- remove: this.config.remove,
100
- replacement: this.config.replacement,
101
- retainStructure: this.config.retainStructure,
102
- };
103
- if (!key)
104
- return fallback;
105
- const blacklistedKeyConfig = this.getBlacklistedKeyConfig(key);
106
- if (!blacklistedKeyConfig)
107
- return fallback;
108
- return {
109
- remove: blacklistedKeyConfig.remove,
110
- replacement: blacklistedKeyConfig.replacement,
111
- retainStructure: blacklistedKeyConfig.retainStructure,
112
- };
113
- };
114
- /**
115
- * Determine if a key should be redacted. This will check the key against the blacklisted keys, using the default configuration.
116
- * @private
117
- * @param {string} key The key to check.
118
- * @returns {boolean} Whether the key should be redacted.
119
- */
120
- shouldRedactObjectValue = (key) => {
121
- if (!key)
122
- return false;
123
- return this.config.blacklistedKeysTransformed.some((redactableKey) => {
124
- return RedactorUtils.complexKeyMatch(key, redactableKey);
125
- });
126
- };
127
- /**
128
- * Redact a string. This will redact the string based on the configuration, redacting the string if it matches a pattern or if the parent key should be redacted.
129
- * @private
130
- * @param value
131
- * @param replacement
132
- * @param remove
133
- * @param shouldRedact
134
- */
135
- redactString = (value, replacement, remove, shouldRedact) => {
136
- if (!value || typeof value !== 'string')
137
- return value;
138
- const maybePartiallyRedacted = this.partialStringRedact(value);
139
- const { stringTests } = this.config;
140
- if (!shouldRedact) {
141
- const result = stringTests?.map((test) => {
142
- if (test instanceof RegExp) {
143
- if (!test.test(maybePartiallyRedacted))
144
- return maybePartiallyRedacted;
145
- if (remove)
146
- return undefined;
147
- if (typeof replacement === 'function')
148
- return replacement(maybePartiallyRedacted);
149
- if (this.config.replaceStringByLength)
150
- return replacement.repeat(maybePartiallyRedacted.length);
151
- return replacement;
152
- }
153
- if (remove && test.pattern.test(maybePartiallyRedacted))
154
- return undefined;
155
- return test.replacer(maybePartiallyRedacted, test.pattern);
156
- }).filter(Boolean)[0];
157
- if (result)
158
- return result;
159
- if (remove)
160
- return undefined;
161
- return maybePartiallyRedacted;
162
- }
163
- if (remove)
164
- return undefined;
165
- if (typeof replacement === 'function')
166
- return replacement(maybePartiallyRedacted);
167
- if (this.config.replaceStringByLength)
168
- return replacement.repeat(maybePartiallyRedacted.length);
169
- return replacement;
170
- };
171
- /**
172
- * Redact a primitive value. This will redact the value if it is a supported type, not an object or array, otherwise it will return the value unchanged.
173
- * @private
174
- * @param {unknown} value The value to redact.
175
- * @param {Transformer | string} replacement The replacement value for redacted data.
176
- * @param {boolean} remove Whether the redacted data should be removed.
177
- * @param {boolean} shouldRedact Whether the value should be redacted based on the parent key.
178
- * @returns {unknown} The redacted value.
179
- */
180
- redactPrimitive = (value, replacement, remove, shouldRedact) => {
181
- if (!this.config.types.includes(typeof value))
182
- return value;
183
- if (remove && shouldRedact && typeof value !== 'string')
184
- return undefined;
185
- if (typeof value === 'string')
186
- return this.redactString(value, replacement, remove, shouldRedact);
187
- if (!shouldRedact)
188
- return value;
189
- if (typeof replacement === 'function')
190
- return replacement(value);
191
- return replacement;
192
- };
193
- /**
194
- * Redact an array. This will redact each value in the array using the `recurse` method.
195
- * @private
196
- * @param {unknown[]} value The array to redact.
197
- * @returns {unknown[]} The redacted array.
198
- */
199
- redactArray = (value) => value.map((val) => this.recurse(val));
200
- /**
201
- * Redact an object. This will recursively redact the object based on the configuration, redacting the keys and values as required.
202
- * @param {Object} value The object to redact.
203
- * @param {string | null} key The key of the object if it is part of another object.
204
- * @param {boolean} parentShouldRedact Whether the item should be redacted based on the key within the parent object.
205
- */
206
- redactObject = (value, key, parentShouldRedact) => {
207
- return Object.fromEntries(Object.entries(value).map(([prop, val]) => {
208
- const shouldRedact = parentShouldRedact || this.shouldRedactObjectValue(prop);
209
- if (shouldRedact) {
210
- const { remove } = this.getRecursionConfig(prop);
211
- if (remove)
212
- return [];
213
- }
214
- return [prop, this.recurse(val, key ?? prop, shouldRedact)];
215
- }).filter(([prop]) => prop !== undefined));
216
- };
217
- partialStringRedact = (value) => {
218
- const { partialStringTests } = this.config;
219
- if (partialStringTests.length === 0)
220
- return value;
221
- let result = value;
222
- partialStringTests.forEach((test) => {
223
- result = test.replacer(result, test.pattern);
224
- });
225
- return result;
226
- };
227
- /**
228
- * Redact a value. If the value is an object or array, the redaction will be performed recursively, otherwise the value will be redacted if it is a supported type using the `replace` method.
229
- * @private
230
- * @param {unknown} value The value to redact.
231
- * @param {string | null} key The key of the value if it is part of an object.
232
- * @param {boolean} parentShouldRedact Whether the parent object should be redacted.
233
- * @returns {unknown} The redacted value.
234
- */
235
- recurse = (value, key, parentShouldRedact) => {
236
- if (value === null)
237
- return value;
238
- const { remove, replacement, retainStructure } = this.getRecursionConfig(key);
239
- if (!(value instanceof Object))
240
- return this.redactPrimitive(value, replacement, remove, Boolean(key && parentShouldRedact));
241
- if (parentShouldRedact) {
242
- if (!retainStructure) {
243
- return typeof replacement === 'function'
244
- ? replacement(value)
245
- : replacement;
246
- }
247
- }
248
- if (Array.isArray(value))
249
- return this.redactArray(value);
250
- return this.redactObject(value, key, parentShouldRedact);
251
- };
252
- }
253
- export default RedactorUtils;
@@ -1,91 +0,0 @@
1
- import { RedactorUtilsConfig } from '../types';
2
- declare class RedactorUtils {
3
- /**
4
- * The configuration for the redaction.
5
- * @private
6
- */
7
- private readonly config;
8
- constructor(customConfig: Omit<RedactorUtilsConfig, 'blacklistedKeysTransformed'>);
9
- /**
10
- * Normalise a string for comparison. This will convert the string to lowercase and remove any non-word characters.
11
- * @private
12
- * @param str The string to normalise.
13
- * @returns {string} The normalised string.
14
- */
15
- private static normaliseString;
16
- /**
17
- * Determine if a key matches a given blacklistedKeyConfig. This will check the key against the blacklisted keys,
18
- * using the configuration option for the given key falling back to the default configuration.
19
- * @private
20
- * @param {string} key The key to check.
21
- * @param {BlacklistKeyConfig} blacklistKeyConfig The configuration for the key.
22
- * @returns {boolean} Whether the key should be redacted.
23
- */
24
- private static complexKeyMatch;
25
- /**
26
- * Get the configuration for an object key. This will check the key against the transformed blacklisted keys.
27
- * @private
28
- * @param {string} key The key of the configuration to get.
29
- * @returns {Required<BlacklistKeyConfig> | undefined} The configuration for the key.
30
- */
31
- private getBlacklistedKeyConfig;
32
- /**
33
- * Get the recursion configuration for a key. This will check the key against the transformed blacklisted keys.
34
- * If the key is found, the configuration for the key will be returned, otherwise undefined.
35
- * @private
36
- * @param {string} key The key of the configuration to get.
37
- * @returns {Required<Pick<BlacklistKeyConfig, 'remove' | 'replacement' | 'retainStructure'>>} The configuration for the key.
38
- */
39
- private getRecursionConfig;
40
- /**
41
- * Determine if a key should be redacted. This will check the key against the blacklisted keys, using the default configuration.
42
- * @private
43
- * @param {string} key The key to check.
44
- * @returns {boolean} Whether the key should be redacted.
45
- */
46
- private shouldRedactObjectValue;
47
- /**
48
- * Redact a string. This will redact the string based on the configuration, redacting the string if it matches a pattern or if the parent key should be redacted.
49
- * @private
50
- * @param value
51
- * @param replacement
52
- * @param remove
53
- * @param shouldRedact
54
- */
55
- private redactString;
56
- /**
57
- * Redact a primitive value. This will redact the value if it is a supported type, not an object or array, otherwise it will return the value unchanged.
58
- * @private
59
- * @param {unknown} value The value to redact.
60
- * @param {Transformer | string} replacement The replacement value for redacted data.
61
- * @param {boolean} remove Whether the redacted data should be removed.
62
- * @param {boolean} shouldRedact Whether the value should be redacted based on the parent key.
63
- * @returns {unknown} The redacted value.
64
- */
65
- private redactPrimitive;
66
- /**
67
- * Redact an array. This will redact each value in the array using the `recurse` method.
68
- * @private
69
- * @param {unknown[]} value The array to redact.
70
- * @returns {unknown[]} The redacted array.
71
- */
72
- private redactArray;
73
- /**
74
- * Redact an object. This will recursively redact the object based on the configuration, redacting the keys and values as required.
75
- * @param {Object} value The object to redact.
76
- * @param {string | null} key The key of the object if it is part of another object.
77
- * @param {boolean} parentShouldRedact Whether the item should be redacted based on the key within the parent object.
78
- */
79
- private redactObject;
80
- partialStringRedact: (value: string) => string;
81
- /**
82
- * Redact a value. If the value is an object or array, the redaction will be performed recursively, otherwise the value will be redacted if it is a supported type using the `replace` method.
83
- * @private
84
- * @param {unknown} value The value to redact.
85
- * @param {string | null} key The key of the value if it is part of an object.
86
- * @param {boolean} parentShouldRedact Whether the parent object should be redacted.
87
- * @returns {unknown} The redacted value.
88
- */
89
- recurse: (value: unknown, key?: string | null, parentShouldRedact?: boolean) => unknown;
90
- }
91
- export default RedactorUtils;