@hackylabs/deep-redact 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -46
- package/dist/cjs/index.js +132 -109
- package/dist/cjs/types.js +2 -0
- package/dist/cjs/utils/redactorUtils.js +219 -0
- package/dist/esm/index.mjs +137 -0
- package/dist/esm/types.mjs +1 -0
- package/dist/esm/utils/redactorUtils.js +219 -0
- package/dist/types/index.d.ts +63 -0
- package/dist/types/types.d.ts +129 -0
- package/dist/types/utils/redactorUtils.d.ts +90 -0
- package/package.json +6 -3
- package/dist/cjs/index.d.ts +0 -34
- package/dist/esm/index.d.ts +0 -34
- package/dist/esm/index.js +0 -125
package/dist/cjs/index.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
export type Types = 'string' | 'number' | 'bigint' | 'boolean' | 'object' | 'function';
|
|
2
|
-
export interface BlacklistKeyConfig {
|
|
3
|
-
fuzzyKeyMatch?: boolean;
|
|
4
|
-
caseSensitiveKeyMatch?: boolean;
|
|
5
|
-
retainStructure?: boolean;
|
|
6
|
-
remove?: boolean;
|
|
7
|
-
key: string | RegExp;
|
|
8
|
-
}
|
|
9
|
-
export interface DeepRedactConfig {
|
|
10
|
-
blacklistedKeys?: Array<string | BlacklistKeyConfig>;
|
|
11
|
-
stringTests?: RegExp[];
|
|
12
|
-
fuzzyKeyMatch?: boolean;
|
|
13
|
-
caseSensitiveKeyMatch?: boolean;
|
|
14
|
-
retainStructure?: boolean;
|
|
15
|
-
replaceStringByLength?: boolean;
|
|
16
|
-
replacement?: string;
|
|
17
|
-
remove?: boolean;
|
|
18
|
-
types?: Types[];
|
|
19
|
-
serialise?: boolean;
|
|
20
|
-
unsupportedTransformer?: (value: unknown) => unknown;
|
|
21
|
-
}
|
|
22
|
-
declare class DeepRedact {
|
|
23
|
-
private circularReference;
|
|
24
|
-
private readonly config;
|
|
25
|
-
constructor(config: DeepRedactConfig);
|
|
26
|
-
private static unsupportedTransformer;
|
|
27
|
-
private removeCircular;
|
|
28
|
-
private redactString;
|
|
29
|
-
private static complexShouldRedact;
|
|
30
|
-
private shouldRedactObjectValue;
|
|
31
|
-
private deepRedact;
|
|
32
|
-
redact: (value: unknown) => unknown;
|
|
33
|
-
}
|
|
34
|
-
export { DeepRedact as default, DeepRedact };
|
package/dist/esm/index.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
export type Types = 'string' | 'number' | 'bigint' | 'boolean' | 'object' | 'function';
|
|
2
|
-
export interface BlacklistKeyConfig {
|
|
3
|
-
fuzzyKeyMatch?: boolean;
|
|
4
|
-
caseSensitiveKeyMatch?: boolean;
|
|
5
|
-
retainStructure?: boolean;
|
|
6
|
-
remove?: boolean;
|
|
7
|
-
key: string | RegExp;
|
|
8
|
-
}
|
|
9
|
-
export interface DeepRedactConfig {
|
|
10
|
-
blacklistedKeys?: Array<string | BlacklistKeyConfig>;
|
|
11
|
-
stringTests?: RegExp[];
|
|
12
|
-
fuzzyKeyMatch?: boolean;
|
|
13
|
-
caseSensitiveKeyMatch?: boolean;
|
|
14
|
-
retainStructure?: boolean;
|
|
15
|
-
replaceStringByLength?: boolean;
|
|
16
|
-
replacement?: string;
|
|
17
|
-
remove?: boolean;
|
|
18
|
-
types?: Types[];
|
|
19
|
-
serialise?: boolean;
|
|
20
|
-
unsupportedTransformer?: (value: unknown) => unknown;
|
|
21
|
-
}
|
|
22
|
-
declare class DeepRedact {
|
|
23
|
-
private circularReference;
|
|
24
|
-
private readonly config;
|
|
25
|
-
constructor(config: DeepRedactConfig);
|
|
26
|
-
private static unsupportedTransformer;
|
|
27
|
-
private removeCircular;
|
|
28
|
-
private redactString;
|
|
29
|
-
private static complexShouldRedact;
|
|
30
|
-
private shouldRedactObjectValue;
|
|
31
|
-
private deepRedact;
|
|
32
|
-
redact: (value: unknown) => unknown;
|
|
33
|
-
}
|
|
34
|
-
export { DeepRedact as default, DeepRedact };
|
package/dist/esm/index.js
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
const normaliseString = (key) => key.toLowerCase().replace(/\W/g, '');
|
|
2
|
-
class DeepRedact {
|
|
3
|
-
constructor(config) {
|
|
4
|
-
var _a, _b;
|
|
5
|
-
this.circularReference = new WeakSet();
|
|
6
|
-
this.config = {
|
|
7
|
-
blacklistedKeys: [],
|
|
8
|
-
stringTests: [],
|
|
9
|
-
fuzzyKeyMatch: false,
|
|
10
|
-
caseSensitiveKeyMatch: true,
|
|
11
|
-
retainStructure: false,
|
|
12
|
-
remove: false,
|
|
13
|
-
replaceStringByLength: false,
|
|
14
|
-
replacement: '[REDACTED]',
|
|
15
|
-
types: ['string'],
|
|
16
|
-
serialise: true,
|
|
17
|
-
unsupportedTransformer: DeepRedact.unsupportedTransformer,
|
|
18
|
-
};
|
|
19
|
-
this.removeCircular = (value) => {
|
|
20
|
-
var _a, _b;
|
|
21
|
-
if (!(value instanceof Object))
|
|
22
|
-
return value;
|
|
23
|
-
if (!((_a = this.circularReference) === null || _a === void 0 ? void 0 : _a.has(value))) {
|
|
24
|
-
(_b = this.circularReference) === null || _b === void 0 ? void 0 : _b.add(value);
|
|
25
|
-
return value;
|
|
26
|
-
}
|
|
27
|
-
return '__circular__';
|
|
28
|
-
};
|
|
29
|
-
this.redactString = (value, parentShouldRedact = false) => {
|
|
30
|
-
if (!this.config.stringTests.some((test) => test.test(value)) && !parentShouldRedact)
|
|
31
|
-
return value;
|
|
32
|
-
if (this.config.replaceStringByLength)
|
|
33
|
-
return this.config.replacement.repeat(value.length);
|
|
34
|
-
return this.config.remove ? undefined : this.config.replacement;
|
|
35
|
-
};
|
|
36
|
-
this.shouldRedactObjectValue = (key) => {
|
|
37
|
-
return this.config.blacklistedKeys.some((redactableKey) => (typeof redactableKey === 'string'
|
|
38
|
-
? key === redactableKey
|
|
39
|
-
: DeepRedact.complexShouldRedact(key, redactableKey)));
|
|
40
|
-
};
|
|
41
|
-
this.deepRedact = (value, parentShouldRedact = false) => {
|
|
42
|
-
if (value === undefined || value === null)
|
|
43
|
-
return value;
|
|
44
|
-
let safeValue = this.removeCircular(value);
|
|
45
|
-
safeValue = this.config.unsupportedTransformer(safeValue);
|
|
46
|
-
if (!(safeValue instanceof Object)) {
|
|
47
|
-
// @ts-expect-error - we already know that safeValue is not a function, symbol, undefined, null, or an object
|
|
48
|
-
if (!this.config.types.includes(typeof safeValue))
|
|
49
|
-
return safeValue;
|
|
50
|
-
if (typeof safeValue === 'string')
|
|
51
|
-
return this.redactString(safeValue, parentShouldRedact);
|
|
52
|
-
if (!parentShouldRedact)
|
|
53
|
-
return safeValue;
|
|
54
|
-
return this.config.remove
|
|
55
|
-
? undefined
|
|
56
|
-
: this.config.replacement;
|
|
57
|
-
}
|
|
58
|
-
if (parentShouldRedact && (!this.config.retainStructure || this.config.remove)) {
|
|
59
|
-
return this.config.remove ? undefined : this.config.replacement;
|
|
60
|
-
}
|
|
61
|
-
if (Array.isArray(safeValue))
|
|
62
|
-
return safeValue.map((val) => this.deepRedact(val, parentShouldRedact));
|
|
63
|
-
return Object.fromEntries(Object.entries(safeValue).map(([key, val]) => {
|
|
64
|
-
const shouldRedact = parentShouldRedact || this.shouldRedactObjectValue(key);
|
|
65
|
-
return [key, this.deepRedact(val, shouldRedact)];
|
|
66
|
-
}));
|
|
67
|
-
};
|
|
68
|
-
this.redact = (value) => {
|
|
69
|
-
this.circularReference = new WeakSet();
|
|
70
|
-
const redacted = this.deepRedact(value);
|
|
71
|
-
this.circularReference = null;
|
|
72
|
-
return this.config.serialise ? JSON.stringify(redacted) : redacted;
|
|
73
|
-
};
|
|
74
|
-
this.config = Object.assign(Object.assign(Object.assign({}, this.config), config), { blacklistedKeys: (_b = (_a = config.blacklistedKeys) === null || _a === void 0 ? void 0 : _a.map((key) => {
|
|
75
|
-
if (typeof key === 'string')
|
|
76
|
-
return key;
|
|
77
|
-
return Object.assign({ fuzzyKeyMatch: this.config.fuzzyKeyMatch, caseSensitiveKeyMatch: this.config.caseSensitiveKeyMatch, retainStructure: this.config.retainStructure, remove: this.config.remove }, key);
|
|
78
|
-
})) !== null && _b !== void 0 ? _b : [] });
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
DeepRedact.unsupportedTransformer = (value) => {
|
|
82
|
-
if (typeof value === 'bigint') {
|
|
83
|
-
return {
|
|
84
|
-
__unsupported: {
|
|
85
|
-
type: 'bigint',
|
|
86
|
-
value: value.toString(),
|
|
87
|
-
radix: 10,
|
|
88
|
-
},
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
if (value instanceof Error) {
|
|
92
|
-
return {
|
|
93
|
-
__unsupported: {
|
|
94
|
-
type: 'error',
|
|
95
|
-
name: value.name,
|
|
96
|
-
message: value.message,
|
|
97
|
-
stack: value.stack,
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
if (value instanceof RegExp) {
|
|
102
|
-
return {
|
|
103
|
-
__unsupported: {
|
|
104
|
-
type: 'regexp',
|
|
105
|
-
source: value.source,
|
|
106
|
-
flags: value.flags,
|
|
107
|
-
},
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
if (value instanceof Date)
|
|
111
|
-
return value.toISOString();
|
|
112
|
-
return value;
|
|
113
|
-
};
|
|
114
|
-
DeepRedact.complexShouldRedact = (key, config) => {
|
|
115
|
-
if (config.key instanceof RegExp)
|
|
116
|
-
return config.key.test(key);
|
|
117
|
-
if (config.fuzzyKeyMatch && config.caseSensitiveKeyMatch)
|
|
118
|
-
return key.includes(config.key);
|
|
119
|
-
if (config.fuzzyKeyMatch && !config.caseSensitiveKeyMatch)
|
|
120
|
-
return normaliseString(key).includes(normaliseString(config.key));
|
|
121
|
-
if (!config.fuzzyKeyMatch && config.caseSensitiveKeyMatch)
|
|
122
|
-
return key === config.key;
|
|
123
|
-
return normaliseString(config.key) === normaliseString(key);
|
|
124
|
-
};
|
|
125
|
-
export { DeepRedact as default, DeepRedact };
|