@logtape/redaction 0.11.0-dev.174 → 0.12.0-dev.181

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 (54) hide show
  1. package/deno.json +34 -0
  2. package/dist/field.cjs +101 -0
  3. package/dist/field.d.cts +88 -0
  4. package/dist/field.d.cts.map +1 -0
  5. package/{types → dist}/field.d.ts +29 -33
  6. package/dist/field.d.ts.map +1 -0
  7. package/dist/field.js +100 -0
  8. package/dist/field.js.map +1 -0
  9. package/dist/mod.cjs +11 -0
  10. package/dist/mod.d.cts +3 -0
  11. package/dist/mod.d.ts +3 -0
  12. package/dist/mod.js +4 -0
  13. package/dist/pattern.cjs +75 -0
  14. package/dist/pattern.d.cts +126 -0
  15. package/dist/pattern.d.cts.map +1 -0
  16. package/{types → dist}/pattern.d.ts +27 -21
  17. package/dist/pattern.d.ts.map +1 -0
  18. package/dist/pattern.js +70 -0
  19. package/dist/pattern.js.map +1 -0
  20. package/field.test.ts +217 -0
  21. package/field.ts +164 -0
  22. package/mod.ts +8 -0
  23. package/package.json +28 -25
  24. package/pattern.test.ts +351 -0
  25. package/pattern.ts +209 -0
  26. package/tsdown.config.ts +11 -0
  27. package/esm/field.js +0 -112
  28. package/esm/mod.js +0 -2
  29. package/esm/package.json +0 -3
  30. package/esm/pattern.js +0 -84
  31. package/script/field.js +0 -118
  32. package/script/mod.js +0 -21
  33. package/script/package.json +0 -3
  34. package/script/pattern.js +0 -88
  35. package/types/_dnt.test_shims.d.ts.map +0 -1
  36. package/types/deps/jsr.io/@std/assert/0.222.1/_constants.d.ts.map +0 -1
  37. package/types/deps/jsr.io/@std/assert/0.222.1/_diff.d.ts.map +0 -1
  38. package/types/deps/jsr.io/@std/assert/0.222.1/_format.d.ts.map +0 -1
  39. package/types/deps/jsr.io/@std/assert/0.222.1/assert.d.ts.map +0 -1
  40. package/types/deps/jsr.io/@std/assert/0.222.1/assert_equals.d.ts.map +0 -1
  41. package/types/deps/jsr.io/@std/assert/0.222.1/assert_exists.d.ts.map +0 -1
  42. package/types/deps/jsr.io/@std/assert/0.222.1/assert_false.d.ts.map +0 -1
  43. package/types/deps/jsr.io/@std/assert/0.222.1/assert_is_error.d.ts.map +0 -1
  44. package/types/deps/jsr.io/@std/assert/0.222.1/assert_match.d.ts.map +0 -1
  45. package/types/deps/jsr.io/@std/assert/0.222.1/assert_throws.d.ts.map +0 -1
  46. package/types/deps/jsr.io/@std/assert/0.222.1/assertion_error.d.ts.map +0 -1
  47. package/types/deps/jsr.io/@std/assert/0.222.1/equal.d.ts.map +0 -1
  48. package/types/deps/jsr.io/@std/fmt/0.222.1/colors.d.ts.map +0 -1
  49. package/types/field.d.ts.map +0 -1
  50. package/types/field.test.d.ts.map +0 -1
  51. package/types/mod.d.ts +0 -3
  52. package/types/mod.d.ts.map +0 -1
  53. package/types/pattern.d.ts.map +0 -1
  54. package/types/pattern.test.d.ts.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pattern.d.cts","names":[],"sources":["../pattern.ts"],"sourcesContent":[],"mappings":";;;;;;AAWA;AAsBA;AAUA;AASa,UAzCI,gBAAA,CAyCY;EAUhB;AASb;AASA;AAoCA;EAA+B,SAAA,OAAA,EApGX,MAoGW;EAAA;;;AAGf;AAmChB;EAA+B,SAAA,WAAA,EAAA,MAAA,GAAA,CAAA,CAAA,KAAA,EAAA,MAAA,EAAA,GAAA,IAAA,EAAA,SAAA,GAAA,EAAA,EAAA,GAAA,MAAA,CAAA;;;;AAGZ;;cA5HN,uBAAuB;;;;;cAUvB,4BAA4B;;;;;cAS5B,gBAAgB;;;;;;cAUhB,gBAAgB;;;;;cAShB,aAAa;;;;;KASd,iBAAA,YAA6B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoCzB,eAAA,YACH,yBACD,oBACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmCa,eAAA,YACH,4BACD,oBACT"}
@@ -1,53 +1,56 @@
1
- import type { ConsoleFormatter, TextFormatter } from "@logtape/logtape";
1
+ import { ConsoleFormatter, TextFormatter } from "@logtape/logtape";
2
+
3
+ //#region pattern.d.ts
4
+
2
5
  /**
3
6
  * A redaction pattern, which is a pair of regular expression and replacement
4
7
  * string or function.
5
8
  * @since 0.10.0
6
9
  */
7
- export interface RedactionPattern {
8
- /**
9
- * The regular expression to match against. Note that it must have the
10
- * `g` (global) flag set, otherwise it will throw a `TypeError`.
11
- */
12
- readonly pattern: RegExp;
13
- /**
14
- * The replacement string or function. If the replacement is a function,
15
- * it will be called with the matched string and any capture groups (the same
16
- * signature as `String.prototype.replaceAll()`).
17
- */
18
- readonly replacement: string | ((match: string, ...rest: readonly any[]) => string);
10
+ interface RedactionPattern {
11
+ /**
12
+ * The regular expression to match against. Note that it must have the
13
+ * `g` (global) flag set, otherwise it will throw a `TypeError`.
14
+ */
15
+ readonly pattern: RegExp;
16
+ /**
17
+ * The replacement string or function. If the replacement is a function,
18
+ * it will be called with the matched string and any capture groups (the same
19
+ * signature as `String.prototype.replaceAll()`).
20
+ */
21
+ readonly replacement: string | ((match: string, ...rest: readonly any[]) => string);
19
22
  }
20
23
  /**
21
24
  * A redaction pattern for email addresses.
22
25
  * @since 0.10.0
23
26
  */
24
- export declare const EMAIL_ADDRESS_PATTERN: RedactionPattern;
27
+ declare const EMAIL_ADDRESS_PATTERN: RedactionPattern;
25
28
  /**
26
29
  * A redaction pattern for credit card numbers (including American Express).
27
30
  * @since 0.10.0
28
31
  */
29
- export declare const CREDIT_CARD_NUMBER_PATTERN: RedactionPattern;
32
+ declare const CREDIT_CARD_NUMBER_PATTERN: RedactionPattern;
30
33
  /**
31
34
  * A redaction pattern for U.S. Social Security numbers.
32
35
  * @since 0.10.0
33
36
  */
34
- export declare const US_SSN_PATTERN: RedactionPattern;
37
+ declare const US_SSN_PATTERN: RedactionPattern;
35
38
  /**
36
39
  * A redaction pattern for South Korean resident registration numbers
37
40
  * (住民登錄番號).
38
41
  * @since 0.10.0
39
42
  */
40
- export declare const KR_RRN_PATTERN: RedactionPattern;
43
+ declare const KR_RRN_PATTERN: RedactionPattern;
41
44
  /**
42
45
  * A redaction pattern for JSON Web Tokens (JWT).
43
46
  * @since 0.10.0
44
47
  */
45
- export declare const JWT_PATTERN: RedactionPattern;
48
+ declare const JWT_PATTERN: RedactionPattern;
46
49
  /**
47
50
  * A list of {@link RedactionPattern}s.
48
51
  * @since 0.10.0
49
52
  */
50
- export type RedactionPatterns = readonly RedactionPattern[];
53
+ type RedactionPatterns = readonly RedactionPattern[];
51
54
  /**
52
55
  * Applies data redaction to a {@link TextFormatter}.
53
56
  *
@@ -82,7 +85,7 @@ export type RedactionPatterns = readonly RedactionPattern[];
82
85
  * @returns The redacted text formatter.
83
86
  * @since 0.10.0
84
87
  */
85
- export declare function redactByPattern(formatter: TextFormatter, patterns: RedactionPatterns): TextFormatter;
88
+ declare function redactByPattern(formatter: TextFormatter, patterns: RedactionPatterns): TextFormatter;
86
89
  /**
87
90
  * Applies data redaction to a {@link ConsoleFormatter}.
88
91
  *
@@ -116,5 +119,8 @@ export declare function redactByPattern(formatter: TextFormatter, patterns: Reda
116
119
  * @returns The redacted console formatter.
117
120
  * @since 0.10.0
118
121
  */
119
- export declare function redactByPattern(formatter: ConsoleFormatter, patterns: RedactionPatterns): ConsoleFormatter;
122
+ declare function redactByPattern(formatter: ConsoleFormatter, patterns: RedactionPatterns): ConsoleFormatter;
123
+ //# sourceMappingURL=pattern.d.ts.map
124
+ //#endregion
125
+ export { CREDIT_CARD_NUMBER_PATTERN, EMAIL_ADDRESS_PATTERN, JWT_PATTERN, KR_RRN_PATTERN, RedactionPattern, RedactionPatterns, US_SSN_PATTERN, redactByPattern };
120
126
  //# sourceMappingURL=pattern.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pattern.d.ts","names":[],"sources":["../pattern.ts"],"sourcesContent":[],"mappings":";;;;;;AAWA;AAsBA;AAUA;AASa,UAzCI,gBAAA,CAyCY;EAUhB;AASb;AASA;AAoCA;EAA+B,SAAA,OAAA,EApGX,MAoGW;EAAA;;;AAGf;AAmChB;EAA+B,SAAA,WAAA,EAAA,MAAA,GAAA,CAAA,CAAA,KAAA,EAAA,MAAA,EAAA,GAAA,IAAA,EAAA,SAAA,GAAA,EAAA,EAAA,GAAA,MAAA,CAAA;;;;AAGZ;;cA5HN,uBAAuB;;;;;cAUvB,4BAA4B;;;;;cAS5B,gBAAgB;;;;;;cAUhB,gBAAgB;;;;;cAShB,aAAa;;;;;KASd,iBAAA,YAA6B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoCzB,eAAA,YACH,yBACD,oBACT;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAmCa,eAAA,YACH,4BACD,oBACT"}
@@ -0,0 +1,70 @@
1
+ //#region pattern.ts
2
+ /**
3
+ * A redaction pattern for email addresses.
4
+ * @since 0.10.0
5
+ */
6
+ const EMAIL_ADDRESS_PATTERN = {
7
+ pattern: /[\p{L}0-9.!#$%&'*+/=?^_`{|}~-]+@[\p{L}0-9](?:[\p{L}0-9-]{0,61}[\p{L}0-9])?(?:\.[\p{L}0-9](?:[\p{L}0-9-]{0,61}[\p{L}0-9])?)+/gu,
8
+ replacement: "REDACTED@EMAIL.ADDRESS"
9
+ };
10
+ /**
11
+ * A redaction pattern for credit card numbers (including American Express).
12
+ * @since 0.10.0
13
+ */
14
+ const CREDIT_CARD_NUMBER_PATTERN = {
15
+ pattern: /(?:\d{4}-){3}\d{4}|(?:\d{4}-){2}\d{6}/g,
16
+ replacement: "XXXX-XXXX-XXXX-XXXX"
17
+ };
18
+ /**
19
+ * A redaction pattern for U.S. Social Security numbers.
20
+ * @since 0.10.0
21
+ */
22
+ const US_SSN_PATTERN = {
23
+ pattern: /\d{3}-\d{2}-\d{4}/g,
24
+ replacement: "XXX-XX-XXXX"
25
+ };
26
+ /**
27
+ * A redaction pattern for South Korean resident registration numbers
28
+ * (住民登錄番號).
29
+ * @since 0.10.0
30
+ */
31
+ const KR_RRN_PATTERN = {
32
+ pattern: /\d{6}-\d{7}/g,
33
+ replacement: "XXXXXX-XXXXXXX"
34
+ };
35
+ /**
36
+ * A redaction pattern for JSON Web Tokens (JWT).
37
+ * @since 0.10.0
38
+ */
39
+ const JWT_PATTERN = {
40
+ pattern: /eyJ[a-zA-Z0-9_-]*\.[a-zA-Z0-9_-]*\.[a-zA-Z0-9_-]*/g,
41
+ replacement: "[JWT REDACTED]"
42
+ };
43
+ function redactByPattern(formatter, patterns) {
44
+ for (const { pattern } of patterns) if (!pattern.global) throw new TypeError(`Pattern ${pattern} does not have the global flag set.`);
45
+ function replaceString(str) {
46
+ for (const p of patterns) str = typeof p.replacement === "string" ? str.replaceAll(p.pattern, p.replacement) : str.replaceAll(p.pattern, p.replacement);
47
+ return str;
48
+ }
49
+ function replaceObject(object) {
50
+ if (typeof object === "string") return replaceString(object);
51
+ else if (Array.isArray(object)) return object.map(replaceObject);
52
+ else if (typeof object === "object" && object !== null) {
53
+ if (Object.getPrototypeOf(object) === Object.prototype || Object.getPrototypeOf(object) === null) {
54
+ const redacted = {};
55
+ for (const key in object) redacted[key] = replaceObject(object[key]);
56
+ return redacted;
57
+ }
58
+ }
59
+ return object;
60
+ }
61
+ return (record) => {
62
+ const output = formatter(record);
63
+ if (typeof output === "string") return replaceString(output);
64
+ return output.map(replaceObject);
65
+ };
66
+ }
67
+
68
+ //#endregion
69
+ export { CREDIT_CARD_NUMBER_PATTERN, EMAIL_ADDRESS_PATTERN, JWT_PATTERN, KR_RRN_PATTERN, US_SSN_PATTERN, redactByPattern };
70
+ //# sourceMappingURL=pattern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pattern.js","names":["EMAIL_ADDRESS_PATTERN: RedactionPattern","CREDIT_CARD_NUMBER_PATTERN: RedactionPattern","US_SSN_PATTERN: RedactionPattern","KR_RRN_PATTERN: RedactionPattern","JWT_PATTERN: RedactionPattern","formatter: TextFormatter | ConsoleFormatter","patterns: RedactionPatterns","str: string","object: unknown","redacted: Record<string, unknown>","record: LogRecord"],"sources":["../pattern.ts"],"sourcesContent":["import type {\n ConsoleFormatter,\n LogRecord,\n TextFormatter,\n} from \"@logtape/logtape\";\n\n/**\n * A redaction pattern, which is a pair of regular expression and replacement\n * string or function.\n * @since 0.10.0\n */\nexport interface RedactionPattern {\n /**\n * The regular expression to match against. Note that it must have the\n * `g` (global) flag set, otherwise it will throw a `TypeError`.\n */\n readonly pattern: RegExp;\n\n /**\n * The replacement string or function. If the replacement is a function,\n * it will be called with the matched string and any capture groups (the same\n * signature as `String.prototype.replaceAll()`).\n */\n readonly replacement:\n | string\n // deno-lint-ignore no-explicit-any\n | ((match: string, ...rest: readonly any[]) => string);\n}\n\n/**\n * A redaction pattern for email addresses.\n * @since 0.10.0\n */\nexport const EMAIL_ADDRESS_PATTERN: RedactionPattern = {\n pattern:\n /[\\p{L}0-9.!#$%&'*+/=?^_`{|}~-]+@[\\p{L}0-9](?:[\\p{L}0-9-]{0,61}[\\p{L}0-9])?(?:\\.[\\p{L}0-9](?:[\\p{L}0-9-]{0,61}[\\p{L}0-9])?)+/gu,\n replacement: \"REDACTED@EMAIL.ADDRESS\",\n};\n\n/**\n * A redaction pattern for credit card numbers (including American Express).\n * @since 0.10.0\n */\nexport const CREDIT_CARD_NUMBER_PATTERN: RedactionPattern = {\n pattern: /(?:\\d{4}-){3}\\d{4}|(?:\\d{4}-){2}\\d{6}/g,\n replacement: \"XXXX-XXXX-XXXX-XXXX\",\n};\n\n/**\n * A redaction pattern for U.S. Social Security numbers.\n * @since 0.10.0\n */\nexport const US_SSN_PATTERN: RedactionPattern = {\n pattern: /\\d{3}-\\d{2}-\\d{4}/g,\n replacement: \"XXX-XX-XXXX\",\n};\n\n/**\n * A redaction pattern for South Korean resident registration numbers\n * (住民登錄番號).\n * @since 0.10.0\n */\nexport const KR_RRN_PATTERN: RedactionPattern = {\n pattern: /\\d{6}-\\d{7}/g,\n replacement: \"XXXXXX-XXXXXXX\",\n};\n\n/**\n * A redaction pattern for JSON Web Tokens (JWT).\n * @since 0.10.0\n */\nexport const JWT_PATTERN: RedactionPattern = {\n pattern: /eyJ[a-zA-Z0-9_-]*\\.[a-zA-Z0-9_-]*\\.[a-zA-Z0-9_-]*/g,\n replacement: \"[JWT REDACTED]\",\n};\n\n/**\n * A list of {@link RedactionPattern}s.\n * @since 0.10.0\n */\nexport type RedactionPatterns = readonly RedactionPattern[];\n\n/**\n * Applies data redaction to a {@link TextFormatter}.\n *\n * Note that there are some built-in redaction patterns:\n *\n * - {@link CREDIT_CARD_NUMBER_PATTERN}\n * - {@link EMAIL_ADDRESS_PATTERN}\n * - {@link JWT_PATTERN}\n * - {@link KR_RRN_PATTERN}\n * - {@link US_SSN_PATTERN}\n *\n * @example\n * ```ts\n * import { getFileSink } from \"@logtape/file\";\n * import { getAnsiColorFormatter } from \"@logtape/logtape\";\n * import {\n * CREDIT_CARD_NUMBER_PATTERN,\n * EMAIL_ADDRESS_PATTERN,\n * JWT_PATTERN,\n * redactByPattern,\n * } from \"@logtape/redaction\";\n *\n * const formatter = redactByPattern(getAnsiConsoleFormatter(), [\n * CREDIT_CARD_NUMBER_PATTERN,\n * EMAIL_ADDRESS_PATTERN,\n * JWT_PATTERN,\n * ]);\n * const sink = getFileSink(\"my-app.log\", { formatter });\n * ```\n * @param formatter The text formatter to apply redaction to.\n * @param patterns The redaction patterns to apply.\n * @returns The redacted text formatter.\n * @since 0.10.0\n */\nexport function redactByPattern(\n formatter: TextFormatter,\n patterns: RedactionPatterns,\n): TextFormatter;\n\n/**\n * Applies data redaction to a {@link ConsoleFormatter}.\n *\n * Note that there are some built-in redaction patterns:\n *\n * - {@link CREDIT_CARD_NUMBER_PATTERN}\n * - {@link EMAIL_ADDRESS_PATTERN}\n * - {@link JWT_PATTERN}\n * - {@link KR_RRN_PATTERN}\n * - {@link US_SSN_PATTERN}\n *\n * @example\n * ```ts\n * import { defaultConsoleFormatter, getConsoleSink } from \"@logtape/logtape\";\n * import {\n * CREDIT_CARD_NUMBER_PATTERN,\n * EMAIL_ADDRESS_PATTERN,\n * JWT_PATTERN,\n * redactByPattern,\n * } from \"@logtape/redaction\";\n *\n * const formatter = redactByPattern(defaultConsoleFormatter, [\n * CREDIT_CARD_NUMBER_PATTERN,\n * EMAIL_ADDRESS_PATTERN,\n * JWT_PATTERN,\n * ]);\n * const sink = getConsoleSink({ formatter });\n * ```\n * @param formatter The console formatter to apply redaction to.\n * @param patterns The redaction patterns to apply.\n * @returns The redacted console formatter.\n * @since 0.10.0\n */\nexport function redactByPattern(\n formatter: ConsoleFormatter,\n patterns: RedactionPatterns,\n): ConsoleFormatter;\n\nexport function redactByPattern(\n formatter: TextFormatter | ConsoleFormatter,\n patterns: RedactionPatterns,\n): (record: LogRecord) => string | readonly unknown[] {\n for (const { pattern } of patterns) {\n if (!pattern.global) {\n throw new TypeError(\n `Pattern ${pattern} does not have the global flag set.`,\n );\n }\n }\n\n function replaceString(str: string): string {\n for (const p of patterns) {\n // The following ternary operator may seem strange, but it's for\n // making TypeScript happy:\n str = typeof p.replacement === \"string\"\n ? str.replaceAll(p.pattern, p.replacement)\n : str.replaceAll(p.pattern, p.replacement);\n }\n return str;\n }\n\n function replaceObject(object: unknown): unknown {\n if (typeof object === \"string\") return replaceString(object);\n else if (Array.isArray(object)) return object.map(replaceObject);\n else if (typeof object === \"object\" && object !== null) {\n // Check if object is a vanilla object:\n if (\n Object.getPrototypeOf(object) === Object.prototype ||\n Object.getPrototypeOf(object) === null\n ) {\n const redacted: Record<string, unknown> = {};\n for (const key in object) {\n redacted[key] =\n // @ts-ignore: object always has key\n replaceObject(object[key]);\n }\n return redacted;\n }\n }\n return object;\n }\n\n return (record: LogRecord) => {\n const output = formatter(record);\n if (typeof output === \"string\") return replaceString(output);\n return output.map(replaceObject);\n };\n}\n"],"mappings":";;;;;AAiCA,MAAaA,wBAA0C;CACrD,SACE;CACF,aAAa;AACd;;;;;AAMD,MAAaC,6BAA+C;CAC1D,SAAS;CACT,aAAa;AACd;;;;;AAMD,MAAaC,iBAAmC;CAC9C,SAAS;CACT,aAAa;AACd;;;;;;AAOD,MAAaC,iBAAmC;CAC9C,SAAS;CACT,aAAa;AACd;;;;;AAMD,MAAaC,cAAgC;CAC3C,SAAS;CACT,aAAa;AACd;AAqFD,SAAgB,gBACdC,WACAC,UACoD;AACpD,MAAK,MAAM,EAAE,SAAS,IAAI,SACxB,MAAK,QAAQ,OACX,OAAM,IAAI,WACP,UAAU,QAAQ;CAKzB,SAAS,cAAcC,KAAqB;AAC1C,OAAK,MAAM,KAAK,SAGd,cAAa,EAAE,gBAAgB,WAC3B,IAAI,WAAW,EAAE,SAAS,EAAE,YAAY,GACxC,IAAI,WAAW,EAAE,SAAS,EAAE,YAAY;AAE9C,SAAO;CACR;CAED,SAAS,cAAcC,QAA0B;AAC/C,aAAW,WAAW,SAAU,QAAO,cAAc,OAAO;WACnD,MAAM,QAAQ,OAAO,CAAE,QAAO,OAAO,IAAI,cAAc;kBAChD,WAAW,YAAY,WAAW,MAEhD;OACE,OAAO,eAAe,OAAO,KAAK,OAAO,aACzC,OAAO,eAAe,OAAO,KAAK,MAClC;IACA,MAAMC,WAAoC,CAAE;AAC5C,SAAK,MAAM,OAAO,OAChB,UAAS,OAEP,cAAc,OAAO,KAAK;AAE9B,WAAO;GACR;;AAEH,SAAO;CACR;AAED,QAAO,CAACC,WAAsB;EAC5B,MAAM,SAAS,UAAU,OAAO;AAChC,aAAW,WAAW,SAAU,QAAO,cAAc,OAAO;AAC5D,SAAO,OAAO,IAAI,cAAc;CACjC;AACF"}
package/field.test.ts ADDED
@@ -0,0 +1,217 @@
1
+ import { suite } from "@hongminhee/suite";
2
+ import type { LogRecord, Sink } from "@logtape/logtape";
3
+ import { assert } from "@std/assert/assert";
4
+ import { assertEquals } from "@std/assert/equals";
5
+ import { assertExists } from "@std/assert/exists";
6
+ import { assertFalse } from "@std/assert/false";
7
+ import {
8
+ type FieldPatterns,
9
+ redactByField,
10
+ redactProperties,
11
+ shouldFieldRedacted,
12
+ } from "./field.ts";
13
+
14
+ const test = suite(import.meta);
15
+
16
+ test("shouldFieldRedacted()", () => {
17
+ { // matches string pattern
18
+ const fieldPatterns: FieldPatterns = ["password", "secret"];
19
+ assertEquals(shouldFieldRedacted("password", fieldPatterns), true);
20
+ assertEquals(shouldFieldRedacted("secret", fieldPatterns), true);
21
+ assertEquals(shouldFieldRedacted("username", fieldPatterns), false);
22
+ }
23
+
24
+ { // matches regex pattern
25
+ const fieldPatterns: FieldPatterns = [/pass/i, /secret/i];
26
+ assertEquals(shouldFieldRedacted("password", fieldPatterns), true);
27
+ assertEquals(shouldFieldRedacted("secretKey", fieldPatterns), true);
28
+ assertEquals(shouldFieldRedacted("myPassword", fieldPatterns), true);
29
+ assertEquals(shouldFieldRedacted("username", fieldPatterns), false);
30
+ }
31
+
32
+ { // case sensitivity in regex
33
+ const caseSensitivePatterns: FieldPatterns = [/pass/, /secret/];
34
+ const caseInsensitivePatterns: FieldPatterns = [/pass/i, /secret/i];
35
+
36
+ assertEquals(shouldFieldRedacted("Password", caseSensitivePatterns), false);
37
+ assertEquals(
38
+ shouldFieldRedacted("Password", caseInsensitivePatterns),
39
+ true,
40
+ );
41
+ }
42
+ });
43
+
44
+ test("redactProperties()", () => {
45
+ { // delete action (default)
46
+ const properties = {
47
+ username: "user123",
48
+ password: "secret123",
49
+ email: "user@example.com",
50
+ message: "Hello world",
51
+ };
52
+
53
+ const result = redactProperties(properties, {
54
+ fieldPatterns: ["password", "email"],
55
+ });
56
+
57
+ assert("username" in result);
58
+ assertFalse("password" in result);
59
+ assertFalse("email" in result);
60
+ assert("message" in result);
61
+
62
+ const nestedObject = {
63
+ ...properties,
64
+ nested: {
65
+ foo: "bar",
66
+ baz: "qux",
67
+ passphrase: "asdf",
68
+ },
69
+ };
70
+ const result2 = redactProperties(nestedObject, {
71
+ fieldPatterns: ["password", "email", "passphrase"],
72
+ });
73
+
74
+ assert("username" in result2);
75
+ assertFalse("password" in result2);
76
+ assertFalse("email" in result2);
77
+ assert("message" in result2);
78
+ assert("nested" in result2);
79
+ assert(typeof result2.nested === "object");
80
+ assertExists(result2.nested);
81
+ assert("foo" in result2.nested);
82
+ assert("baz" in result2.nested);
83
+ assertFalse("passphrase" in result2.nested);
84
+ }
85
+
86
+ { // custom action function
87
+ const properties = {
88
+ username: "user123",
89
+ password: "secret123",
90
+ token: "abc123",
91
+ message: "Hello world",
92
+ };
93
+
94
+ const result = redactProperties(properties, {
95
+ fieldPatterns: [/password/i, /token/i],
96
+ action: () => "REDACTED",
97
+ });
98
+
99
+ assertEquals(result.username, "user123");
100
+ assertEquals(result.password, "REDACTED");
101
+ assertEquals(result.token, "REDACTED");
102
+ assertEquals(result.message, "Hello world");
103
+ }
104
+
105
+ { // preserves other properties
106
+ const properties = {
107
+ username: "user123",
108
+ data: { nested: "value" },
109
+ sensitive: "hidden",
110
+ };
111
+
112
+ const result = redactProperties(properties, {
113
+ fieldPatterns: ["sensitive"],
114
+ });
115
+
116
+ assertEquals(result.username, "user123");
117
+ assertEquals(result.data, { nested: "value" });
118
+ assertFalse("sensitive" in result);
119
+ }
120
+ });
121
+
122
+ test("redactByField()", async () => {
123
+ { // wraps sink and redacts properties
124
+ const records: LogRecord[] = [];
125
+ const originalSink: Sink = (record) => records.push(record);
126
+
127
+ const wrappedSink = redactByField(originalSink, {
128
+ fieldPatterns: ["password", "token"],
129
+ });
130
+
131
+ const record: LogRecord = {
132
+ level: "info",
133
+ category: ["test"],
134
+ message: ["Test message"],
135
+ rawMessage: "Test message",
136
+ timestamp: Date.now(),
137
+ properties: {
138
+ username: "user123",
139
+ password: "secret123",
140
+ token: "abc123",
141
+ },
142
+ };
143
+
144
+ wrappedSink(record);
145
+
146
+ assertEquals(records.length, 1);
147
+ assert("username" in records[0].properties);
148
+ assertFalse("password" in records[0].properties);
149
+ assertFalse("token" in records[0].properties);
150
+ }
151
+
152
+ { // uses default field patterns when not specified
153
+ const records: LogRecord[] = [];
154
+ const originalSink: Sink = (record) => records.push(record);
155
+
156
+ const wrappedSink = redactByField(originalSink);
157
+
158
+ const record: LogRecord = {
159
+ level: "info",
160
+ category: ["test"],
161
+ message: ["Test message"],
162
+ rawMessage: "Test message",
163
+ timestamp: Date.now(),
164
+ properties: {
165
+ username: "user123",
166
+ password: "secret123",
167
+ email: "user@example.com",
168
+ apiKey: "xyz789",
169
+ },
170
+ };
171
+
172
+ wrappedSink(record);
173
+
174
+ assertEquals(records.length, 1);
175
+ assert("username" in records[0].properties);
176
+ assertFalse("password" in records[0].properties);
177
+ assertFalse("email" in records[0].properties);
178
+ assertFalse("apiKey" in records[0].properties);
179
+ }
180
+
181
+ { // preserves Disposable behavior
182
+ let disposed = false;
183
+ const originalSink: Sink & Disposable = Object.assign(
184
+ (_record: LogRecord) => {},
185
+ {
186
+ [Symbol.dispose]: () => {
187
+ disposed = true;
188
+ },
189
+ },
190
+ );
191
+
192
+ const wrappedSink = redactByField(originalSink) as Sink & Disposable;
193
+
194
+ assert(Symbol.dispose in wrappedSink);
195
+ wrappedSink[Symbol.dispose]();
196
+ assert(disposed);
197
+ }
198
+
199
+ { // preserves AsyncDisposable behavior
200
+ let disposed = false;
201
+ const originalSink: Sink & AsyncDisposable = Object.assign(
202
+ (_record: LogRecord) => {},
203
+ {
204
+ [Symbol.asyncDispose]: () => {
205
+ disposed = true;
206
+ return Promise.resolve();
207
+ },
208
+ },
209
+ );
210
+
211
+ const wrappedSink = redactByField(originalSink) as Sink & AsyncDisposable;
212
+
213
+ assert(Symbol.asyncDispose in wrappedSink);
214
+ await wrappedSink[Symbol.asyncDispose]();
215
+ assert(disposed);
216
+ }
217
+ });
package/field.ts ADDED
@@ -0,0 +1,164 @@
1
+ import type { LogRecord, Sink } from "@logtape/logtape";
2
+
3
+ /**
4
+ * The type for a field pattern used in redaction. A string or a regular
5
+ * expression that matches field names.
6
+ * @since 0.10.0
7
+ */
8
+ export type FieldPattern = string | RegExp;
9
+
10
+ /**
11
+ * An array of field patterns used for redaction. Each pattern can be
12
+ * a string or a regular expression that matches field names.
13
+ * @since 0.10.0
14
+ */
15
+ export type FieldPatterns = FieldPattern[];
16
+
17
+ /**
18
+ * Default field patterns for redaction. These patterns will match
19
+ * common sensitive fields such as passwords, tokens, and personal
20
+ * information.
21
+ * @since 0.10.0
22
+ */
23
+ export const DEFAULT_REDACT_FIELDS: FieldPatterns = [
24
+ /pass(?:code|phrase|word)/i,
25
+ /secret/i,
26
+ /token/i,
27
+ /key/i,
28
+ /credential/i,
29
+ /auth/i,
30
+ /signature/i,
31
+ /sensitive/i,
32
+ /private/i,
33
+ /ssn/i,
34
+ /email/i,
35
+ /phone/i,
36
+ /address/i,
37
+ ];
38
+
39
+ /**
40
+ * Options for redacting fields in a {@link LogRecord}. Used by
41
+ * the {@link redactByField} function.
42
+ * @since 0.10.0
43
+ */
44
+ export interface FieldRedactionOptions {
45
+ /**
46
+ * The field patterns to match against. This can be an array of
47
+ * strings or regular expressions. If a field matches any of the
48
+ * patterns, it will be redacted.
49
+ * @defaultValue {@link DEFAULT_REDACT_FIELDS}
50
+ */
51
+ readonly fieldPatterns: FieldPatterns;
52
+
53
+ /**
54
+ * The action to perform on the matched fields. If not provided,
55
+ * the default action is to delete the field from the properties.
56
+ * If a function is provided, it will be called with the
57
+ * value of the field, and the return value will be used to replace
58
+ * the field in the properties.
59
+ * If the action is `"delete"`, the field will be removed from the
60
+ * properties.
61
+ * @default `"delete"`
62
+ */
63
+ readonly action?: "delete" | ((value: unknown) => unknown);
64
+ }
65
+
66
+ /**
67
+ * Redacts properties in a {@link LogRecord} based on the provided field
68
+ * patterns and action.
69
+ *
70
+ * Note that it is a decorator which wraps the sink and redacts properties
71
+ * before passing them to the sink.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * import { getConsoleSink } from "@logtape/logtape";
76
+ * import { redactByField } from "@logtape/redaction";
77
+ *
78
+ * const sink = redactByField(getConsoleSink());
79
+ * ```
80
+ *
81
+ * @param sink The sink to wrap.
82
+ * @param options The redaction options.
83
+ * @returns The wrapped sink.
84
+ * @since 0.10.0
85
+ */
86
+ export function redactByField(
87
+ sink: Sink | Sink & Disposable | Sink & AsyncDisposable,
88
+ options: FieldRedactionOptions | FieldPatterns = DEFAULT_REDACT_FIELDS,
89
+ ): Sink | Sink & Disposable | Sink & AsyncDisposable {
90
+ const opts = Array.isArray(options) ? { fieldPatterns: options } : options;
91
+ const wrapped = (record: LogRecord) => {
92
+ sink({ ...record, properties: redactProperties(record.properties, opts) });
93
+ };
94
+ if (Symbol.dispose in sink) wrapped[Symbol.dispose] = sink[Symbol.dispose];
95
+ if (Symbol.asyncDispose in sink) {
96
+ wrapped[Symbol.asyncDispose] = sink[Symbol.asyncDispose];
97
+ }
98
+ return wrapped;
99
+ }
100
+
101
+ /**
102
+ * Redacts properties from an object based on specified field patterns.
103
+ *
104
+ * This function creates a shallow copy of the input object and applies
105
+ * redaction rules to its properties. For properties that match the redaction
106
+ * patterns, the function either removes them or transforms their values based
107
+ * on the provided action.
108
+ *
109
+ * The redaction process is recursive and will be applied to nested objects
110
+ * as well, allowing for deep redaction of sensitive data in complex object
111
+ * structures.
112
+ * @param properties The properties to redact.
113
+ * @param options The redaction options.
114
+ * @returns The redacted properties.
115
+ * @since 0.10.0
116
+ */
117
+ export function redactProperties(
118
+ properties: Record<string, unknown>,
119
+ options: FieldRedactionOptions,
120
+ ): Record<string, unknown> {
121
+ const copy = { ...properties };
122
+ for (const field in copy) {
123
+ if (shouldFieldRedacted(field, options.fieldPatterns)) {
124
+ if (options.action == null || options.action === "delete") {
125
+ delete copy[field];
126
+ } else {
127
+ copy[field] = options.action(copy[field]);
128
+ }
129
+ continue;
130
+ }
131
+ const value = copy[field];
132
+ // Check if value is a vanilla object:
133
+ if (
134
+ typeof value === "object" && value !== null &&
135
+ (Object.getPrototypeOf(value) === Object.prototype ||
136
+ Object.getPrototypeOf(value) === null)
137
+ ) {
138
+ // @ts-ignore: value is always Record<string, unknown>
139
+ copy[field] = redactProperties(value, options);
140
+ }
141
+ }
142
+ return copy;
143
+ }
144
+
145
+ /**
146
+ * Checks if a field should be redacted based on the provided field patterns.
147
+ * @param field The field name to check.
148
+ * @param fieldPatterns The field patterns to match against.
149
+ * @returns `true` if the field should be redacted, `false` otherwise.
150
+ * @since 0.10.0
151
+ */
152
+ export function shouldFieldRedacted(
153
+ field: string,
154
+ fieldPatterns: FieldPatterns,
155
+ ): boolean {
156
+ for (const fieldPattern of fieldPatterns) {
157
+ if (typeof fieldPattern === "string") {
158
+ if (fieldPattern === field) return true;
159
+ } else {
160
+ if (fieldPattern.test(field)) return true;
161
+ }
162
+ }
163
+ return false;
164
+ }
package/mod.ts ADDED
@@ -0,0 +1,8 @@
1
+ export {
2
+ DEFAULT_REDACT_FIELDS,
3
+ type FieldPattern,
4
+ type FieldPatterns,
5
+ type FieldRedactionOptions,
6
+ redactByField,
7
+ } from "./field.ts";
8
+ export * from "./pattern.ts";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logtape/redaction",
3
- "version": "0.11.0-dev.174+ce9c5154",
3
+ "version": "0.12.0-dev.181+e4f33b9d",
4
4
  "description": "Redact sensitive data from log messages",
5
5
  "keywords": [
6
6
  "logging",
@@ -11,6 +11,7 @@
11
11
  "masking",
12
12
  "sensitive"
13
13
  ],
14
+ "license": "MIT",
14
15
  "author": {
15
16
  "name": "Hong Minhee",
16
17
  "email": "hong@minhee.org",
@@ -20,38 +21,40 @@
20
21
  "repository": {
21
22
  "type": "git",
22
23
  "url": "git+https://github.com/dahlia/logtape.git",
23
- "directory": "file/"
24
+ "directory": "redaction/"
24
25
  },
25
- "license": "MIT",
26
26
  "bugs": {
27
27
  "url": "https://github.com/dahlia/logtape/issues"
28
28
  },
29
- "main": "./script/mod.js",
30
- "module": "./esm/mod.js",
31
- "types": "./types/mod.d.ts",
29
+ "funding": [
30
+ "https://github.com/sponsors/dahlia"
31
+ ],
32
+ "type": "module",
33
+ "module": "./dist/mod.js",
34
+ "main": "./dist/mod.cjs",
35
+ "types": "./dist/mod.d.ts",
32
36
  "exports": {
33
37
  ".": {
34
- "import": {
35
- "types": "./types/mod.d.ts",
36
- "default": "./esm/mod.js"
37
- },
38
- "require": {
39
- "types": "./types/mod.d.ts",
40
- "default": "./script/mod.js"
41
- }
42
- }
38
+ "import": "./dist/mod.js",
39
+ "require": "./dist/mod.cjs",
40
+ "types": "./dist/mod.d.ts"
41
+ },
42
+ "./package.json": "./package.json"
43
43
  },
44
- "scripts": {
45
- "test": "node test_runner.js"
44
+ "peerDependencies": {
45
+ "@logtape/logtape": "0.12.0-dev.181+e4f33b9d"
46
46
  },
47
- "funding": [
48
- "https://github.com/sponsors/dahlia"
49
- ],
50
47
  "devDependencies": {
51
- "@types/node": "^20.9.0",
52
- "picocolors": "^1.0.0",
53
- "@deno/shim-deno": "~0.18.0",
54
- "@logtape/logtape": "^0.11.0-dev.174"
48
+ "@hongminhee/suite": "^0.6.2",
49
+ "@std/assert": "npm:@jsr/std__assert@^1.0.13",
50
+ "tsdown": "^0.12.7",
51
+ "typescript": "^5.8.3"
55
52
  },
56
- "_generatedBy": "dnt@dev"
53
+ "scripts": {
54
+ "build": "tsdown",
55
+ "test": "tsdown && node --experimental-transform-types --test",
56
+ "test:bun": "tsdown && bun test",
57
+ "test:deno": "deno test",
58
+ "test-all": "tsdown && node --experimental-transform-types --test && bun test && deno test"
59
+ }
57
60
  }