@adatechnology/logger 0.0.2

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.
@@ -0,0 +1,11 @@
1
+ // Tipos leves que representam requests/responses de diferentes adapters (Express/Fastify)
2
+ export type RequestLike = {
3
+ headers?: Record<string, unknown> | undefined;
4
+ [key: string]: unknown;
5
+ };
6
+
7
+ export type ResponseLike = {
8
+ [key: string]: unknown;
9
+ };
10
+
11
+ export type NextFunctionLike = (err?: unknown) => void;
@@ -0,0 +1,96 @@
1
+ import {
2
+ Obfuscator,
3
+ SensitiveEntry,
4
+ } from "./implementations/winston/winston.logger.types";
5
+ import { DefaultObfuscatorParams } from "./obfuscator.types";
6
+ import { DEFAULT_SENSITIVE_KEYS, MASK } from "./logger.constant";
7
+
8
+ function isObject(value: unknown) {
9
+ return (
10
+ value !== null && typeof value === "object" && !Array.isArray(value as any)
11
+ );
12
+ }
13
+
14
+ function maskString(str: string) {
15
+ if (str.length <= MASK.MIN_LENGTH) return MASK.REPLACEMENT;
16
+ return (
17
+ str.slice(0, MASK.EDGE_CHARS) +
18
+ MASK.REPLACEMENT +
19
+ str.slice(-MASK.EDGE_CHARS)
20
+ );
21
+ }
22
+
23
+ function normalizeEntries(entries?: SensitiveEntry[]) {
24
+ const keys: string[] = [];
25
+ const custom = new Map<string, Obfuscator>();
26
+ if (!entries || entries.length === 0) return { keys, custom };
27
+ for (const entry of entries) {
28
+ if (typeof entry === "string") {
29
+ keys.push(entry);
30
+ } else if (
31
+ entry &&
32
+ typeof entry === "object" &&
33
+ "key" in entry &&
34
+ typeof entry.key === "string" &&
35
+ typeof entry.obfuscator === "function"
36
+ ) {
37
+ custom.set(entry.key.toLowerCase(), entry.obfuscator);
38
+ }
39
+ }
40
+ return { keys, custom };
41
+ }
42
+
43
+ export function defaultObfuscator(params: DefaultObfuscatorParams): unknown {
44
+ const { obj, entries } = params;
45
+ if (obj == null) return obj;
46
+ if (Array.isArray(obj)) {
47
+ return obj.map((item) => defaultObfuscator({ obj: item, entries }));
48
+ }
49
+ if (!isObject(obj)) return obj;
50
+
51
+ const { keys: extraKeys, custom } = normalizeEntries(entries);
52
+ const keys = DEFAULT_SENSITIVE_KEYS.concat(extraKeys || []);
53
+
54
+ function splitIntoWords(k: string) {
55
+ // insert space between camelCase boundaries, replace separators with space
56
+ const withSpaces = k.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[_\-.]+/g, ' ');
57
+ return withSpaces.split(/\s+/).filter(Boolean);
58
+ }
59
+
60
+ const out: Record<string, unknown> = {};
61
+ for (const key of Object.keys(obj as Record<string, unknown>)) {
62
+ const value = (obj as Record<string, unknown>)[key];
63
+ const lowerKey = key.toLowerCase();
64
+ if (custom.has(lowerKey)) {
65
+ try {
66
+ out[key] = custom.get(lowerKey)!(value);
67
+ } catch (error) {
68
+ out[key] = MASK.REPLACEMENT;
69
+ }
70
+ } else if (
71
+ // match when key exactly equals a sensitive token OR
72
+ // when any token equals one of the camelCase/sep-separated words
73
+ keys.some((s) => {
74
+ const sens = s.toLowerCase();
75
+ if (lowerKey === sens) return true;
76
+ const words = splitIntoWords(lowerKey);
77
+ return words.some((w) => w === sens);
78
+ })
79
+ ) {
80
+ // mask value using default strategy
81
+ if (typeof value === "string") out[key] = maskString(value);
82
+ else out[key] = MASK.REPLACEMENT;
83
+ } else if (isObject(value) || Array.isArray(value)) {
84
+ out[key] = defaultObfuscator({ obj: value, entries });
85
+ } else {
86
+ out[key] = value;
87
+ }
88
+ }
89
+ return out;
90
+ }
91
+
92
+ export function buildDefaultObfuscator(
93
+ additional?: SensitiveEntry[],
94
+ ): Obfuscator {
95
+ return (v: any) => defaultObfuscator({ obj: v, entries: additional });
96
+ }
@@ -0,0 +1,6 @@
1
+ import { SensitiveEntry } from "./implementations/winston/winston.logger.types";
2
+
3
+ export type DefaultObfuscatorParams = {
4
+ obj: unknown;
5
+ entries?: SensitiveEntry[];
6
+ };
@@ -0,0 +1,7 @@
1
+ export const DEFAULT_HTTP_REQUEST_ID_HEADER = "x-request-id";
2
+ export const DEFAULT_HTTP_REQUEST_ID_FALLBACKS = ["x-correlation-id"];
3
+
4
+ export const HEADERS_PARAMS = {
5
+ REQUEST_ID: DEFAULT_HTTP_REQUEST_ID_HEADER,
6
+ FALLBACKS: DEFAULT_HTTP_REQUEST_ID_FALLBACKS,
7
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "module": "CommonJS",
5
+ "moduleResolution": "node",
6
+ "outDir": "dist",
7
+ "rootDir": "src",
8
+ "declarationDir": "dist/types",
9
+ "declaration": true,
10
+ "composite": true
11
+ },
12
+ "include": ["./src/**/*.ts"],
13
+ "exclude": ["node_modules", "dist"]
14
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "composite": false
5
+ }
6
+ }