@uniglot/wont-let-you-see 0.2.0 → 0.3.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.
- package/README.md +106 -40
- package/dist/index.js +113 -92
- package/package.json +1 -1
- package/patterns/aws.json +28 -0
- package/patterns/common.json +18 -0
- package/patterns/kubernetes.json +8 -0
- package/src/__tests__/masker.test.ts +64 -0
- package/src/__tests__/patterns.test.ts +135 -61
- package/src/masker.ts +62 -58
- package/src/patterns.ts +108 -45
package/src/patterns.ts
CHANGED
|
@@ -1,45 +1,108 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
1
|
+
import { readdirSync, readFileSync } from "fs";
|
|
2
|
+
import { join, dirname } from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Pattern definition in JSON files.
|
|
7
|
+
* Can be:
|
|
8
|
+
* - A regex string: "^vpc-[0-9a-f]{8,17}$"
|
|
9
|
+
* - An object with pattern and optional contextual flag: { "pattern": "...", "contextual": true }
|
|
10
|
+
* - An object with exact string match: { "exact": "literal-value" }
|
|
11
|
+
*/
|
|
12
|
+
export type PatternDefinition =
|
|
13
|
+
| string
|
|
14
|
+
| { pattern: string; contextual?: boolean }
|
|
15
|
+
| { exact: string };
|
|
16
|
+
|
|
17
|
+
export interface PatternFile {
|
|
18
|
+
[patternName: string]: PatternDefinition;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface LoadedPattern {
|
|
22
|
+
name: string;
|
|
23
|
+
pattern: RegExp;
|
|
24
|
+
isContextual: boolean;
|
|
25
|
+
isExact: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let cachedPatterns: LoadedPattern[] | null = null;
|
|
29
|
+
|
|
30
|
+
function getPackagePatternsDir(): string {
|
|
31
|
+
const currentFile = fileURLToPath(import.meta.url);
|
|
32
|
+
const srcDir = dirname(currentFile);
|
|
33
|
+
const packageRoot = dirname(srcDir);
|
|
34
|
+
return join(packageRoot, "patterns");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function parsePatternDefinition(
|
|
38
|
+
name: string,
|
|
39
|
+
definition: PatternDefinition,
|
|
40
|
+
): LoadedPattern {
|
|
41
|
+
if (typeof definition === "string") {
|
|
42
|
+
return {
|
|
43
|
+
name,
|
|
44
|
+
pattern: new RegExp(definition),
|
|
45
|
+
isContextual: false,
|
|
46
|
+
isExact: false,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if ("exact" in definition) {
|
|
51
|
+
const escaped = definition.exact.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
52
|
+
return {
|
|
53
|
+
name,
|
|
54
|
+
pattern: new RegExp(escaped),
|
|
55
|
+
isContextual: false,
|
|
56
|
+
isExact: true,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
name,
|
|
62
|
+
pattern: new RegExp(definition.pattern),
|
|
63
|
+
isContextual: definition.contextual ?? false,
|
|
64
|
+
isExact: false,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function loadPatternFile(filePath: string): LoadedPattern[] {
|
|
69
|
+
const content = readFileSync(filePath, "utf-8");
|
|
70
|
+
const data: PatternFile = JSON.parse(content);
|
|
71
|
+
const patterns: LoadedPattern[] = [];
|
|
72
|
+
|
|
73
|
+
for (const [name, definition] of Object.entries(data)) {
|
|
74
|
+
patterns.push(parsePatternDefinition(name, definition));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return patterns;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function loadPatterns(): LoadedPattern[] {
|
|
81
|
+
if (cachedPatterns) {
|
|
82
|
+
return cachedPatterns;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const patternsDir = getPackagePatternsDir();
|
|
86
|
+
const patterns: LoadedPattern[] = [];
|
|
87
|
+
|
|
88
|
+
const files = readdirSync(patternsDir)
|
|
89
|
+
.filter((f) => f.endsWith(".json"))
|
|
90
|
+
.sort();
|
|
91
|
+
|
|
92
|
+
for (const file of files) {
|
|
93
|
+
const filePath = join(patternsDir, file);
|
|
94
|
+
const filePatterns = loadPatternFile(filePath);
|
|
95
|
+
patterns.push(...filePatterns);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
cachedPatterns = patterns;
|
|
99
|
+
return patterns;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function resetPatternCache(): void {
|
|
103
|
+
cachedPatterns = null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function getPatternByName(name: string): LoadedPattern | undefined {
|
|
107
|
+
return loadPatterns().find((p) => p.name === name);
|
|
108
|
+
}
|