unnbound-logger-sdk 3.0.0 → 3.0.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/dist/axios.d.ts +9 -0
- package/dist/axios.js +74 -0
- package/dist/http.d.ts +0 -0
- package/dist/http.js +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/logger.d.ts +15 -0
- package/dist/logger.js +44 -0
- package/dist/middleware.d.ts +3 -0
- package/dist/middleware.js +70 -0
- package/dist/sftp.d.ts +10 -0
- package/dist/sftp.js +13 -0
- package/dist/span.d.ts +16 -0
- package/dist/span.js +41 -0
- package/dist/storage.d.ts +10 -0
- package/dist/storage.js +10 -0
- package/dist/types.d.ts +63 -0
- package/dist/types.js +8 -0
- package/dist/unnbound-logger.d.ts +114 -0
- package/dist/unnbound-logger.js +475 -0
- package/dist/utils/http-status-messages.d.ts +9 -0
- package/dist/utils/http-status-messages.js +160 -0
- package/dist/utils/logger-utils.d.ts +34 -0
- package/dist/utils/logger-utils.js +96 -0
- package/dist/utils/trace-context.d.ts +10 -0
- package/dist/utils/trace-context.js +23 -0
- package/dist/utils/with-trace.d.ts +7 -0
- package/dist/utils/with-trace.js +20 -0
- package/dist/utils.d.ts +19 -0
- package/dist/utils.js +53 -0
- package/package.json +3 -2
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateTimestamp = generateTimestamp;
|
|
4
|
+
exports.getTraceId = getTraceId;
|
|
5
|
+
exports.clearTraceId = clearTraceId;
|
|
6
|
+
exports.filterHeaders = filterHeaders;
|
|
7
|
+
exports.safeJsonParse = safeJsonParse;
|
|
8
|
+
exports.normalizeIp = normalizeIp;
|
|
9
|
+
/**
|
|
10
|
+
* Utility functions for logging
|
|
11
|
+
*/
|
|
12
|
+
const uuid_1 = require("uuid");
|
|
13
|
+
/**
|
|
14
|
+
* Generates the current timestamp in ISO format
|
|
15
|
+
* @returns ISO timestamp string
|
|
16
|
+
*/
|
|
17
|
+
function generateTimestamp() {
|
|
18
|
+
return new Date().toISOString();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Stores trace IDs by workflow ID for consistent tracking
|
|
22
|
+
*/
|
|
23
|
+
const traceIdMap = new Map();
|
|
24
|
+
/**
|
|
25
|
+
* Gets or creates a trace ID for a workflow
|
|
26
|
+
* @param workflowId - The workflow ID to get a trace ID for
|
|
27
|
+
* @returns A trace ID associated with the workflow
|
|
28
|
+
*/
|
|
29
|
+
function getTraceId(workflowId) {
|
|
30
|
+
if (!traceIdMap.has(workflowId)) {
|
|
31
|
+
traceIdMap.set(workflowId, (0, uuid_1.v4)());
|
|
32
|
+
}
|
|
33
|
+
return traceIdMap.get(workflowId);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Clears a trace ID from the map
|
|
37
|
+
* @param workflowId - The workflow ID to clear
|
|
38
|
+
*/
|
|
39
|
+
function clearTraceId(workflowId) {
|
|
40
|
+
traceIdMap.delete(workflowId);
|
|
41
|
+
}
|
|
42
|
+
// --- Header Allow-Listing ---
|
|
43
|
+
const ALLOWED_HEADERS = new Set([
|
|
44
|
+
'content-type',
|
|
45
|
+
'accept',
|
|
46
|
+
'user-agent',
|
|
47
|
+
'host',
|
|
48
|
+
'x-forwarded-for',
|
|
49
|
+
'x-request-id',
|
|
50
|
+
'content-length',
|
|
51
|
+
'cache-control',
|
|
52
|
+
]);
|
|
53
|
+
/**
|
|
54
|
+
* Filters an object of headers, returning a new object with only the allowed headers.
|
|
55
|
+
* @param headers The original headers object.
|
|
56
|
+
* @returns A new object containing only the headers from the allow-list.
|
|
57
|
+
*/
|
|
58
|
+
function filterHeaders(headers) {
|
|
59
|
+
const filtered = {};
|
|
60
|
+
for (const key in headers) {
|
|
61
|
+
if (ALLOWED_HEADERS.has(key.toLowerCase())) {
|
|
62
|
+
filtered[key] = String(headers[key]);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return filtered;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Safely parses JSON strings, returning the original data if parsing fails or if it's not a string.
|
|
69
|
+
* @param data The data to potentially parse as JSON.
|
|
70
|
+
* @returns Parsed JSON object or the original data.
|
|
71
|
+
*/
|
|
72
|
+
function safeJsonParse(data) {
|
|
73
|
+
if (typeof data === 'string') {
|
|
74
|
+
try {
|
|
75
|
+
return JSON.parse(data);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return data;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return data;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Normalizes IP addresses by removing IPv6 mapping prefix for IPv4 addresses.
|
|
85
|
+
* @param ip The IP address to normalize.
|
|
86
|
+
* @returns Normalized IP address string.
|
|
87
|
+
*/
|
|
88
|
+
function normalizeIp(ip) {
|
|
89
|
+
if (!ip)
|
|
90
|
+
return ip;
|
|
91
|
+
// Remove IPv4-mapped IPv6 prefix (::ffff:) to get clean IPv4 address
|
|
92
|
+
if (ip.startsWith('::ffff:')) {
|
|
93
|
+
return ip.substring(7);
|
|
94
|
+
}
|
|
95
|
+
return ip;
|
|
96
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare class TraceContextManager {
|
|
2
|
+
private static instance;
|
|
3
|
+
private storage;
|
|
4
|
+
private constructor();
|
|
5
|
+
static getInstance(): TraceContextManager;
|
|
6
|
+
run<T>(traceId: string, callback: () => T): T;
|
|
7
|
+
getTraceId(): string | undefined;
|
|
8
|
+
}
|
|
9
|
+
export declare const traceContext: TraceContextManager;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.traceContext = void 0;
|
|
4
|
+
const async_hooks_1 = require("async_hooks");
|
|
5
|
+
class TraceContextManager {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.storage = new async_hooks_1.AsyncLocalStorage();
|
|
8
|
+
}
|
|
9
|
+
static getInstance() {
|
|
10
|
+
if (!TraceContextManager.instance) {
|
|
11
|
+
TraceContextManager.instance = new TraceContextManager();
|
|
12
|
+
}
|
|
13
|
+
return TraceContextManager.instance;
|
|
14
|
+
}
|
|
15
|
+
run(traceId, callback) {
|
|
16
|
+
return this.storage.run({ traceId }, callback);
|
|
17
|
+
}
|
|
18
|
+
getTraceId() {
|
|
19
|
+
const context = this.storage.getStore();
|
|
20
|
+
return context?.traceId;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.traceContext = TraceContextManager.getInstance();
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Higher-order function that wraps a function with trace context
|
|
3
|
+
* @param fn - Function to wrap with trace context
|
|
4
|
+
* @param traceId - Optional trace ID to use (will generate new one if not provided)
|
|
5
|
+
* @returns Wrapped function that maintains trace context
|
|
6
|
+
*/
|
|
7
|
+
export declare function withTrace<T extends (...args: any[]) => any>(fn: T, traceId?: string): T;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withTrace = withTrace;
|
|
4
|
+
/**
|
|
5
|
+
* Utility function to wrap a function with trace context
|
|
6
|
+
*/
|
|
7
|
+
const uuid_1 = require("uuid");
|
|
8
|
+
const trace_context_1 = require("./trace-context");
|
|
9
|
+
/**
|
|
10
|
+
* Higher-order function that wraps a function with trace context
|
|
11
|
+
* @param fn - Function to wrap with trace context
|
|
12
|
+
* @param traceId - Optional trace ID to use (will generate new one if not provided)
|
|
13
|
+
* @returns Wrapped function that maintains trace context
|
|
14
|
+
*/
|
|
15
|
+
function withTrace(fn, traceId) {
|
|
16
|
+
const id = traceId || (0, uuid_1.v4)();
|
|
17
|
+
return ((...args) => {
|
|
18
|
+
return trace_context_1.traceContext.run(id, () => fn(...args));
|
|
19
|
+
});
|
|
20
|
+
}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a path matches any of the ignore patterns
|
|
3
|
+
* @param path - The path to check
|
|
4
|
+
* @param patterns - Array of glob patterns to match against
|
|
5
|
+
* @returns boolean indicating if the path should be ignored
|
|
6
|
+
*/
|
|
7
|
+
export declare const shouldIgnorePath: (path: string, patterns: string[]) => boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Safely parses JSON strings, returning the original data if parsing fails or if it's not a string.
|
|
10
|
+
* @param data The data to potentially parse as JSON.
|
|
11
|
+
* @returns Parsed JSON object or the original data.
|
|
12
|
+
*/
|
|
13
|
+
export declare function safeJsonParse(data: any): any;
|
|
14
|
+
/**
|
|
15
|
+
* Normalizes IP addresses by removing IPv6 mapping prefix for IPv4 addresses.
|
|
16
|
+
* @param ip The IP address to normalize.
|
|
17
|
+
* @returns Normalized IP address string.
|
|
18
|
+
*/
|
|
19
|
+
export declare function normalizeIp(ip: string | undefined): string | undefined;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shouldIgnorePath = void 0;
|
|
4
|
+
exports.safeJsonParse = safeJsonParse;
|
|
5
|
+
exports.normalizeIp = normalizeIp;
|
|
6
|
+
/**
|
|
7
|
+
* Checks if a path matches any of the ignore patterns
|
|
8
|
+
* @param path - The path to check
|
|
9
|
+
* @param patterns - Array of glob patterns to match against
|
|
10
|
+
* @returns boolean indicating if the path should be ignored
|
|
11
|
+
*/
|
|
12
|
+
const shouldIgnorePath = (path, patterns) => {
|
|
13
|
+
return patterns.some((pattern) => {
|
|
14
|
+
// Convert glob pattern to regex
|
|
15
|
+
const regexPattern = pattern
|
|
16
|
+
.replace(/\./g, '\\.') // Escape dots
|
|
17
|
+
.replace(/\*/g, '.*') // Convert * to .*
|
|
18
|
+
.replace(/\?/g, '.'); // Convert ? to .
|
|
19
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
20
|
+
return regex.test(path);
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
exports.shouldIgnorePath = shouldIgnorePath;
|
|
24
|
+
/**
|
|
25
|
+
* Safely parses JSON strings, returning the original data if parsing fails or if it's not a string.
|
|
26
|
+
* @param data The data to potentially parse as JSON.
|
|
27
|
+
* @returns Parsed JSON object or the original data.
|
|
28
|
+
*/
|
|
29
|
+
function safeJsonParse(data) {
|
|
30
|
+
if (typeof data === 'string') {
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(data);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return data;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Normalizes IP addresses by removing IPv6 mapping prefix for IPv4 addresses.
|
|
42
|
+
* @param ip The IP address to normalize.
|
|
43
|
+
* @returns Normalized IP address string.
|
|
44
|
+
*/
|
|
45
|
+
function normalizeIp(ip) {
|
|
46
|
+
if (!ip)
|
|
47
|
+
return ip;
|
|
48
|
+
// Remove IPv4-mapped IPv6 prefix (::ffff:) to get clean IPv4 address
|
|
49
|
+
if (ip.startsWith('::ffff:')) {
|
|
50
|
+
return ip.substring(7);
|
|
51
|
+
}
|
|
52
|
+
return ip;
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unnbound-logger-sdk",
|
|
3
3
|
"description": "A structured logging library with TypeScript support using Pino. Provides consistent, well-typed logging with automatic logId, workflowId, traceId, and deploymentId tracking across operational contexts.",
|
|
4
|
-
"version": "3.0.
|
|
4
|
+
"version": "3.0.1",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"keywords": [
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
}
|
|
51
51
|
},
|
|
52
52
|
"files": [
|
|
53
|
-
"dist
|
|
53
|
+
"dist/**/*",
|
|
54
54
|
"README.md",
|
|
55
55
|
"LICENSE"
|
|
56
56
|
],
|
|
@@ -63,6 +63,7 @@
|
|
|
63
63
|
"test": "jest",
|
|
64
64
|
"test:coverage": "jest --coverage",
|
|
65
65
|
"test:watch": "jest --watch",
|
|
66
|
+
"typecheck": "tsc -noEmit",
|
|
66
67
|
"lint": "eslint --cache --cache-location ./node_modules/.cache/eslint .",
|
|
67
68
|
"lint:fix": "pnpm lint --fix",
|
|
68
69
|
"format": "prettier --write .",
|