@lidofinance/satanizer 0.16.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 ADDED
@@ -0,0 +1,79 @@
1
+ # @lidofinance/satanizer
2
+
3
+ Zero dependecies tool, which masks secrets 🕵️
4
+
5
+ ## Installation
6
+
7
+ `yarn add @lidofinance/satanizer`
8
+
9
+ ## Usage
10
+
11
+ There are two types of usage, you can provide secrets first and then appy mask function.
12
+
13
+ ```ts
14
+ import { satanizer } from '@lidofinance/satanizer';
15
+
16
+ const mask = satanizer([/0x[a-zA-Z0-9]+/])
17
+ const target = 'qwe 0xABC1 asd'
18
+ const result = 'qwe ****** asd'
19
+
20
+ expect(mask(target)).toBe(result)
21
+ ```
22
+
23
+ Alternatively you can execute satanizer right away.
24
+
25
+ ```ts
26
+ import { satanizer } from '@lidofinance/satanizer';
27
+
28
+ const target = 'qwe secret asd'
29
+ const result = 'qwe ****** asd'
30
+
31
+ expect(satanizer(['secret'], target)).toBe(result)
32
+ ```
33
+
34
+ Pattern can be string or regular expression.
35
+
36
+ ```ts
37
+ import { satanizer } from '@lidofinance/satanizer';
38
+
39
+ assert(satanizer(['secret', /0x[s]ecret/], 'qwe secret 0xsecret asd') === 'qwe ****** ******** asd')
40
+ ```
41
+
42
+ Target can be a anything - string, object, array or Error.
43
+
44
+ ```ts
45
+ import { satanizer } from '@lidofinance/satanizer';
46
+
47
+ const mask = satanizer(['secret'])
48
+
49
+ const target = { message: `there are secret` }
50
+ const result = { message: `there are ******` }
51
+ expect(mask(target)).toMatchObject(result)
52
+
53
+ const target = ['some secret item', 'some item']
54
+ const result = ['some ****** item', 'some item']
55
+ expect(mask(target)).toMatchObject(result)
56
+
57
+ const target = new Error(`there are secret`)
58
+ const result = { message: `there are ******` }
59
+ expect(mask(target)).toMatchObject(result)
60
+ ```
61
+
62
+ ## Configuration
63
+
64
+ You can specify as well as your own secrets, as well as use some pre-build secrets patterns:
65
+
66
+ * `blockchainAddress` (ETH and others).
67
+ * `ensAddress`.
68
+ * `ipAddress`.
69
+ * `macAddress`.
70
+ * `emailAddress`.
71
+
72
+ You can use them one by one or all together.
73
+
74
+ ```ts
75
+ import { commonPatterns } from '@lidofinance/satanizer'
76
+
77
+ const secrets = [/* your secrets */]
78
+ const mask = satanizer([...commonPatterns, ...secrets])
79
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,84 @@
1
+ function $parcel$exportWildcard(dest, source) {
2
+ Object.keys(source).forEach(function(key) {
3
+ if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
4
+ return;
5
+ }
6
+
7
+ Object.defineProperty(dest, key, {
8
+ enumerable: true,
9
+ get: function get() {
10
+ return source[key];
11
+ }
12
+ });
13
+ });
14
+
15
+ return dest;
16
+ }
17
+ function $parcel$export(e, n, v, s) {
18
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
19
+ }
20
+ var $6181fcac47f6fbd9$exports = {};
21
+
22
+ $parcel$export($6181fcac47f6fbd9$exports, "satanizer", () => $6181fcac47f6fbd9$export$4aabe16cd431674e);
23
+ // Escape string to be safely used in RegExp constructor
24
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
25
+ const $6181fcac47f6fbd9$var$escapeRegExp = (string)=>string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
26
+ ;
27
+ const $6181fcac47f6fbd9$var$patternToRegex = (pattern)=>// Need to make regex global to work with MatchAll
28
+ pattern instanceof RegExp ? new RegExp(pattern.source, "g") : new RegExp($6181fcac47f6fbd9$var$escapeRegExp(pattern), "g");
29
+ const $6181fcac47f6fbd9$var$matchIndexes = (regex, input)=>Array.from(input.matchAll(regex)).filter((item)=>item != null)// Need to force types, because ts doesn't know it is the match
30
+ .map((match)=>[
31
+ match.index,
32
+ match.index + match[0].length
33
+ ]);
34
+ const $6181fcac47f6fbd9$var$getMask = (length)=>Array.from({
35
+ length: length
36
+ }).fill("*").join("");
37
+ const $6181fcac47f6fbd9$var$errorToObj = (error)=>Object.getOwnPropertyNames(error).reduce((acc, cur)=>({
38
+ ...acc,
39
+ [cur]: error[cur]
40
+ }), {});
41
+ const $6181fcac47f6fbd9$var$stringify = (data)=>JSON.stringify(data instanceof Error ? $6181fcac47f6fbd9$var$errorToObj(data) : data);
42
+ const $6181fcac47f6fbd9$var$satanizerFactory = (patterns)=>(message)=>{
43
+ // This is a bit dirty workaround right now, just to not iterate over all keys/values and all possible edge cases.
44
+ // There are a few issues with it, added tests about it and marked them as failing.
45
+ const stringified = $6181fcac47f6fbd9$var$stringify(message ?? null);
46
+ const safeStringifiedMessage = patterns.map($6181fcac47f6fbd9$var$patternToRegex)// Need to find all indexes first, otherwise we may miss overlaps
47
+ .flatMap((regex)=>$6181fcac47f6fbd9$var$matchIndexes(regex, stringified))// Now it's safe to replace every match with the mask
48
+ .reduce((acc, [start, end])=>acc.slice(0, start) + $6181fcac47f6fbd9$var$getMask(end - start) + acc.slice(end), stringified);
49
+ return JSON.parse(safeStringifiedMessage);
50
+ };
51
+ function $6181fcac47f6fbd9$export$4aabe16cd431674e(patterns, ...args) {
52
+ if (args.length === 1) return $6181fcac47f6fbd9$var$satanizerFactory(patterns)(args[0]);
53
+ return $6181fcac47f6fbd9$var$satanizerFactory(patterns);
54
+ }
55
+
56
+
57
+ var $2f2af15db2572749$exports = {};
58
+
59
+ $parcel$export($2f2af15db2572749$exports, "blockchainAddress", () => $2f2af15db2572749$export$1e20809441cc69a);
60
+ $parcel$export($2f2af15db2572749$exports, "ensAddress", () => $2f2af15db2572749$export$6a3b35e38f46684a);
61
+ $parcel$export($2f2af15db2572749$exports, "ipAddress", () => $2f2af15db2572749$export$450e57019586b28f);
62
+ $parcel$export($2f2af15db2572749$exports, "macAddress", () => $2f2af15db2572749$export$4aae620858fd13ea);
63
+ $parcel$export($2f2af15db2572749$exports, "emailAddress", () => $2f2af15db2572749$export$642aeeb6efe3bed1);
64
+ $parcel$export($2f2af15db2572749$exports, "commonPatterns", () => $2f2af15db2572749$export$a83d9d00fb7126e2);
65
+ // May caught ids, maybe needs to test this a bit more
66
+ const $2f2af15db2572749$export$1e20809441cc69a = /(0x)?[a-zA-Z0-9]{40,}/;
67
+ const $2f2af15db2572749$export$6a3b35e38f46684a = /[a-zA-Z0-9.]+\.eth/;
68
+ const $2f2af15db2572749$export$450e57019586b28f = /\d+\.\d+\.\d+\.\d+/;
69
+ const $2f2af15db2572749$export$4aae620858fd13ea = /([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})/;
70
+ const $2f2af15db2572749$export$642aeeb6efe3bed1 = /[^@\s]+@[^@\s]+\.[^@\s]+/;
71
+ const $2f2af15db2572749$export$a83d9d00fb7126e2 = [
72
+ $2f2af15db2572749$export$1e20809441cc69a,
73
+ $2f2af15db2572749$export$6a3b35e38f46684a,
74
+ $2f2af15db2572749$export$450e57019586b28f,
75
+ $2f2af15db2572749$export$4aae620858fd13ea,
76
+ $2f2af15db2572749$export$642aeeb6efe3bed1
77
+ ];
78
+
79
+
80
+ $parcel$exportWildcard(module.exports, $6181fcac47f6fbd9$exports);
81
+ $parcel$exportWildcard(module.exports, $2f2af15db2572749$exports);
82
+
83
+
84
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;;;;;;;;;;;;;;;ACEA,wDAAwD;AACxD,6FAA6F;AAC7F,MAAM,qCAAe,CAAC,SAA2B,OAAO,OAAO,CAAC,uBAAuB,QAAQ,oCAAoC;;AAInI,MAAM,uCAAiB,CAAC,UACtB,kDAAkD;IAClD,mBAAmB,SAAS,IAAI,OAAO,QAAQ,MAAM,EAAE,OAAO,IAAI,OAAO,mCAAa,UAAU,IAAI;AAEtG,MAAM,qCAAe,CAAC,OAAe,QACnC,MAAM,IAAI,CAAC,MAAM,QAAQ,CAAC,QACvB,MAAM,CAAC,CAAC,OAAS,QAAQ,IAAI,CAC9B,+DAA+D;KAC9D,GAAG,CAA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/core/satanazier/src/satanizer.ts","packages/core/satanazier/src/common-patterns.ts"],"sourcesContent":["export * from './satanizer'\nexport * from './common-patterns'\n","import { OmitFunctions } from './omit-functions'\n\n// Escape string to be safely used in RegExp constructor\n// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping\nconst escapeRegExp = (string: string): string => string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&') // $& means the whole matched string\n\nexport type Pattern = RegExp | string\n\nconst patternToRegex = (pattern: Pattern): RegExp =>\n // Need to make regex global to work with MatchAll\n pattern instanceof RegExp ? new RegExp(pattern.source, 'g') : new RegExp(escapeRegExp(pattern), 'g')\n\nconst matchIndexes = (regex: RegExp, input: string) =>\n Array.from(input.matchAll(regex))\n .filter((item) => item != null)\n // Need to force types, because ts doesn't know it is the match\n .map((match) => [match.index as number, (match.index as number) + match[0].length])\n\nconst getMask = (length: number) => Array.from({ length }).fill('*').join('')\n\nconst errorToObj = <T extends Error>(error: T) =>\n Object.getOwnPropertyNames(error).reduce((acc, cur) => ({ ...acc, [cur]: error[cur as keyof T] }), {})\n\nconst stringify = (data: unknown) => JSON.stringify(data instanceof Error ? errorToObj(data) : data)\n\nconst satanizerFactory = (patterns: Pattern[]) => (message: any) => {\n // This is a bit dirty workaround right now, just to not iterate over all keys/values and all possible edge cases.\n // There are a few issues with it, added tests about it and marked them as failing.\n const stringified = stringify(message ?? null)\n const safeStringifiedMessage = patterns\n .map(patternToRegex)\n // Need to find all indexes first, otherwise we may miss overlaps\n .flatMap((regex) => matchIndexes(regex, stringified))\n // Now it's safe to replace every match with the mask\n .reduce((acc, [start, end]) => acc.slice(0, start) + getMask(end - start) + acc.slice(end), stringified)\n return JSON.parse(safeStringifiedMessage)\n}\n\n// Overload type definition to accept both types of call.\nexport function satanizer<T>(pattern: Pattern[]): (input: T) => OmitFunctions<T> // this is a bit incorrect since we don't perserve methods\nexport function satanizer<T>(pattern: Pattern[], input: T): OmitFunctions<T>\nexport function satanizer<T>(patterns: Pattern[], ...args: [] | [OmitFunctions<T>]) {\n if (args.length === 1) {\n return satanizerFactory(patterns)(args[0])\n }\n return satanizerFactory(patterns)\n}\n","// May caught ids, maybe needs to test this a bit more\nexport const blockchainAddress = /(0x)?[a-zA-Z0-9]{40,}/\nexport const ensAddress = /[a-zA-Z0-9.]+\\.eth/\n// It's not really valid, but it's still ok for secrets\nexport const ipAddress = /\\d+\\.\\d+\\.\\d+\\.\\d+/\nexport const macAddress = /([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})/\n// Also this one is hugely accpeting, but it's ok for secrets\nexport const emailAddress = /[^@\\s]+@[^@\\s]+\\.[^@\\s]+/\n\nexport const commonPatterns = [blockchainAddress, ensAddress, ipAddress, macAddress, emailAddress]\n"],"names":[],"version":3,"file":"index.cjs.map","sourceRoot":"../../../../"}
@@ -0,0 +1,12 @@
1
+ import { OmitFunctions } from "./omit-functions";
2
+ export type Pattern = RegExp | string;
3
+ export function satanizer<T>(pattern: Pattern[]): (input: T) => OmitFunctions<T>;
4
+ export function satanizer<T>(pattern: Pattern[], input: T): OmitFunctions<T>;
5
+ export const blockchainAddress: RegExp;
6
+ export const ensAddress: RegExp;
7
+ export const ipAddress: RegExp;
8
+ export const macAddress: RegExp;
9
+ export const emailAddress: RegExp;
10
+ export const commonPatterns: RegExp[];
11
+
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"mappings":";AAMA,sBAAsB,MAAM,GAAG,MAAM,CAAA;AAiCrC,0BAA0B,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,CAAC,KAAK,EAAE,CAAC,KAAK,cAAc,CAAC,CAAC,CAAA;AAChF,0BAA0B,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,GAAG,cAAc,CAAC,CAAC,CAAA;ACvC5E,OAAO,MAAM,yBAA2C,CAAA;AACxD,OAAO,MAAM,kBAAiC,CAAA;AAE9C,OAAO,MAAM,iBAAgC,CAAA;AAC7C,OAAO,MAAM,kBAAsD,CAAA;AAEnE,OAAO,MAAM,oBAAyC,CAAA;AAEtD,OAAO,MAAM,wBAAqF,CAAA","sources":["packages/core/satanazier/src/src/satanizer.ts","packages/core/satanazier/src/src/common-patterns.ts","packages/core/satanazier/src/src/index.ts","packages/core/satanazier/src/index.ts"],"sourcesContent":[null,null,null,"export * from './satanizer'\nexport * from './common-patterns'\n"],"names":[],"version":3,"file":"index.d.ts.map","sourceRoot":"../../../../"}
package/dist/index.mjs ADDED
@@ -0,0 +1,67 @@
1
+ function $parcel$export(e, n, v, s) {
2
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
3
+ }
4
+ var $634bfa66b85378ba$exports = {};
5
+
6
+ $parcel$export($634bfa66b85378ba$exports, "satanizer", () => $634bfa66b85378ba$export$4aabe16cd431674e);
7
+ // Escape string to be safely used in RegExp constructor
8
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
9
+ const $634bfa66b85378ba$var$escapeRegExp = (string)=>string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") // $& means the whole matched string
10
+ ;
11
+ const $634bfa66b85378ba$var$patternToRegex = (pattern)=>// Need to make regex global to work with MatchAll
12
+ pattern instanceof RegExp ? new RegExp(pattern.source, "g") : new RegExp($634bfa66b85378ba$var$escapeRegExp(pattern), "g");
13
+ const $634bfa66b85378ba$var$matchIndexes = (regex, input)=>Array.from(input.matchAll(regex)).filter((item)=>item != null)// Need to force types, because ts doesn't know it is the match
14
+ .map((match)=>[
15
+ match.index,
16
+ match.index + match[0].length
17
+ ]);
18
+ const $634bfa66b85378ba$var$getMask = (length)=>Array.from({
19
+ length: length
20
+ }).fill("*").join("");
21
+ const $634bfa66b85378ba$var$errorToObj = (error)=>Object.getOwnPropertyNames(error).reduce((acc, cur)=>({
22
+ ...acc,
23
+ [cur]: error[cur]
24
+ }), {});
25
+ const $634bfa66b85378ba$var$stringify = (data)=>JSON.stringify(data instanceof Error ? $634bfa66b85378ba$var$errorToObj(data) : data);
26
+ const $634bfa66b85378ba$var$satanizerFactory = (patterns)=>(message)=>{
27
+ // This is a bit dirty workaround right now, just to not iterate over all keys/values and all possible edge cases.
28
+ // There are a few issues with it, added tests about it and marked them as failing.
29
+ const stringified = $634bfa66b85378ba$var$stringify(message ?? null);
30
+ const safeStringifiedMessage = patterns.map($634bfa66b85378ba$var$patternToRegex)// Need to find all indexes first, otherwise we may miss overlaps
31
+ .flatMap((regex)=>$634bfa66b85378ba$var$matchIndexes(regex, stringified))// Now it's safe to replace every match with the mask
32
+ .reduce((acc, [start, end])=>acc.slice(0, start) + $634bfa66b85378ba$var$getMask(end - start) + acc.slice(end), stringified);
33
+ return JSON.parse(safeStringifiedMessage);
34
+ };
35
+ function $634bfa66b85378ba$export$4aabe16cd431674e(patterns, ...args) {
36
+ if (args.length === 1) return $634bfa66b85378ba$var$satanizerFactory(patterns)(args[0]);
37
+ return $634bfa66b85378ba$var$satanizerFactory(patterns);
38
+ }
39
+
40
+
41
+ var $a4f22c43dff7d58a$exports = {};
42
+
43
+ $parcel$export($a4f22c43dff7d58a$exports, "blockchainAddress", () => $a4f22c43dff7d58a$export$1e20809441cc69a);
44
+ $parcel$export($a4f22c43dff7d58a$exports, "ensAddress", () => $a4f22c43dff7d58a$export$6a3b35e38f46684a);
45
+ $parcel$export($a4f22c43dff7d58a$exports, "ipAddress", () => $a4f22c43dff7d58a$export$450e57019586b28f);
46
+ $parcel$export($a4f22c43dff7d58a$exports, "macAddress", () => $a4f22c43dff7d58a$export$4aae620858fd13ea);
47
+ $parcel$export($a4f22c43dff7d58a$exports, "emailAddress", () => $a4f22c43dff7d58a$export$642aeeb6efe3bed1);
48
+ $parcel$export($a4f22c43dff7d58a$exports, "commonPatterns", () => $a4f22c43dff7d58a$export$a83d9d00fb7126e2);
49
+ // May caught ids, maybe needs to test this a bit more
50
+ const $a4f22c43dff7d58a$export$1e20809441cc69a = /(0x)?[a-zA-Z0-9]{40,}/;
51
+ const $a4f22c43dff7d58a$export$6a3b35e38f46684a = /[a-zA-Z0-9.]+\.eth/;
52
+ const $a4f22c43dff7d58a$export$450e57019586b28f = /\d+\.\d+\.\d+\.\d+/;
53
+ const $a4f22c43dff7d58a$export$4aae620858fd13ea = /([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})/;
54
+ const $a4f22c43dff7d58a$export$642aeeb6efe3bed1 = /[^@\s]+@[^@\s]+\.[^@\s]+/;
55
+ const $a4f22c43dff7d58a$export$a83d9d00fb7126e2 = [
56
+ $a4f22c43dff7d58a$export$1e20809441cc69a,
57
+ $a4f22c43dff7d58a$export$6a3b35e38f46684a,
58
+ $a4f22c43dff7d58a$export$450e57019586b28f,
59
+ $a4f22c43dff7d58a$export$4aae620858fd13ea,
60
+ $a4f22c43dff7d58a$export$642aeeb6efe3bed1
61
+ ];
62
+
63
+
64
+
65
+
66
+ export {$634bfa66b85378ba$export$4aabe16cd431674e as satanizer, $a4f22c43dff7d58a$export$1e20809441cc69a as blockchainAddress, $a4f22c43dff7d58a$export$6a3b35e38f46684a as ensAddress, $a4f22c43dff7d58a$export$450e57019586b28f as ipAddress, $a4f22c43dff7d58a$export$4aae620858fd13ea as macAddress, $a4f22c43dff7d58a$export$642aeeb6efe3bed1 as emailAddress, $a4f22c43dff7d58a$export$a83d9d00fb7126e2 as commonPatterns};
67
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;ACEA,wDAAwD;AACxD,6FAA6F;AAC7F,MAAM,qCAAe,CAAC,SAA2B,OAAO,OAAO,CAAC,uBAAuB,QAAQ,oCAAoC;;AAInI,MAAM,uCAAiB,CAAC,UACtB,kDAAkD;IAClD,mBAAmB,SAAS,IAAI,OAAO,QAAQ,MAAM,EAAE,OAAO,IAAI,OAAO,mCAAa,UAAU,IAAI;AAEtG,MAAM,qCAAe,CAAC,OAAe,QACnC,MAAM,IAAI,CAAC,MAAM,QAAQ,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,MAAM,4CAAiB;IAAC;IAAmB;IAAY;IAAW;IAAY;CAAa;","sources":["packages/core/satanazier/src/index.ts","packages/core/satanazier/src/satanizer.ts","packages/core/satanazier/src/common-patterns.ts"],"sourcesContent":["export * from './satanizer'\nexport * from './common-patterns'\n","import { OmitFunctions } from './omit-functions'\n\n// Escape string to be safely used in RegExp constructor\n// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping\nconst escapeRegExp = (string: string): string => string.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&') // $& means the whole matched string\n\nexport type Pattern = RegExp | string\n\nconst patternToRegex = (pattern: Pattern): RegExp =>\n // Need to make regex global to work with MatchAll\n pattern instanceof RegExp ? new RegExp(pattern.source, 'g') : new RegExp(escapeRegExp(pattern), 'g')\n\nconst matchIndexes = (regex: RegExp, input: string) =>\n Array.from(input.matchAll(regex))\n .filter((item) => item != null)\n // Need to force types, because ts doesn't know it is the match\n .map((match) => [match.index as number, (match.index as number) + match[0].length])\n\nconst getMask = (length: number) => Array.from({ length }).fill('*').join('')\n\nconst errorToObj = <T extends Error>(error: T) =>\n Object.getOwnPropertyNames(error).reduce((acc, cur) => ({ ...acc, [cur]: error[cur as keyof T] }), {})\n\nconst stringify = (data: unknown) => JSON.stringify(data instanceof Error ? errorToObj(data) : data)\n\nconst satanizerFactory = (patterns: Pattern[]) => (message: any) => {\n // This is a bit dirty workaround right now, just to not iterate over all keys/values and all possible edge cases.\n // There are a few issues with it, added tests about it and marked them as failing.\n const stringified = stringify(message ?? null)\n const safeStringifiedMessage = patterns\n .map(patternToRegex)\n // Need to find all indexes first, otherwise we may miss overlaps\n .flatMap((regex) => matchIndexes(regex, stringified))\n // Now it's safe to replace every match with the mask\n .reduce((acc, [start, end]) => acc.slice(0, start) + getMask(end - start) + acc.slice(end), stringified)\n return JSON.parse(safeStringifiedMessage)\n}\n\n// Overload type definition to accept both types of call.\nexport function satanizer<T>(pattern: Pattern[]): (input: T) => OmitFunctions<T> // this is a bit incorrect since we don't perserve methods\nexport function satanizer<T>(pattern: Pattern[], input: T): OmitFunctions<T>\nexport function satanizer<T>(patterns: Pattern[], ...args: [] | [OmitFunctions<T>]) {\n if (args.length === 1) {\n return satanizerFactory(patterns)(args[0])\n }\n return satanizerFactory(patterns)\n}\n","// May caught ids, maybe needs to test this a bit more\nexport const blockchainAddress = /(0x)?[a-zA-Z0-9]{40,}/\nexport const ensAddress = /[a-zA-Z0-9.]+\\.eth/\n// It's not really valid, but it's still ok for secrets\nexport const ipAddress = /\\d+\\.\\d+\\.\\d+\\.\\d+/\nexport const macAddress = /([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})/\n// Also this one is hugely accpeting, but it's ok for secrets\nexport const emailAddress = /[^@\\s]+@[^@\\s]+\\.[^@\\s]+/\n\nexport const commonPatterns = [blockchainAddress, ensAddress, ipAddress, macAddress, emailAddress]\n"],"names":[],"version":3,"file":"index.mjs.map","sourceRoot":"../../../../"}
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@lidofinance/satanizer",
3
+ "description": "Tool, which mask secrets",
4
+ "repository": "git@github.com:lidofinance/warehouse.git",
5
+ "license": "MIT",
6
+ "version": "0.16.0",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "engines": {
11
+ "node": ">= 16"
12
+ },
13
+ "source": "./src/index.ts",
14
+ "main": "dist/index.cjs",
15
+ "module": "dist/index.mjs",
16
+ "types": "dist/index.d.ts",
17
+ "scripts": {
18
+ "build": "parcel build",
19
+ "lint": "eslint . && prettier --check src",
20
+ "lint:fix": "eslint --fix . && prettier --check src --write",
21
+ "types": "tsc --noEmit",
22
+ "test": "jest && tsd -f 'src/*.test-d.ts'"
23
+ },
24
+ "devDependencies": {
25
+ "@lidofinance/config-prettier": "~0.16.0",
26
+ "@types/jest": "^29.2.4",
27
+ "@types/node": "^18.11.17",
28
+ "jest": "^29.3.1",
29
+ "ts-jest": "^29.0.3",
30
+ "tsd": "^0.25.0",
31
+ "typescript": "4.9"
32
+ }
33
+ }