unnbound-events 1.0.20 → 1.0.21

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.
@@ -1,72 +1,88 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.traceMiddleware = void 0;
4
- const span_1 = require("./span");
5
- const types_1 = require("./types");
6
- const utils_1 = require("./utils");
7
- const trace_1 = require("./trace");
4
+ const span_1 = require('./span');
5
+ const types_1 = require('./types');
6
+ const utils_1 = require('./utils');
7
+ const trace_1 = require('./trace');
8
8
  const getFullUrl = (req) => {
9
- const url = req.originalUrl || req.url;
10
- if (url?.startsWith('http://') || url?.startsWith('https://'))
11
- return url;
12
- // Check if we have a workflow URL configured (preferred method)
13
- if (process.env.UNNBOUND_WORKFLOW_URL) {
14
- // Use workflow URL as the base URL
15
- return `${process.env.UNNBOUND_WORKFLOW_URL.replace(/\/$/, '')}${url}`;
16
- }
17
- // Fallback to constructing from request info for incoming requests
18
- const protocol = req.protocol || (req.secure ? 'https' : 'http');
19
- const host = req.get('host') || req.get('x-forwarded-host') || 'localhost';
20
- return `${protocol}://${host}${url}`;
9
+ const url = req.originalUrl || req.url;
10
+ if (url?.startsWith('http://') || url?.startsWith('https://')) return url;
11
+ // Check if we have a workflow URL configured (preferred method)
12
+ if (process.env.UNNBOUND_WORKFLOW_URL) {
13
+ // Use workflow URL as the base URL
14
+ return `${process.env.UNNBOUND_WORKFLOW_URL.replace(/\/$/, '')}${url}`;
15
+ }
16
+ // Fallback to constructing from request info for incoming requests
17
+ const protocol = req.protocol || (req.secure ? 'https' : 'http');
18
+ const host = req.get('host') || req.get('x-forwarded-host') || 'localhost';
19
+ return `${protocol}://${host}${url}`;
21
20
  };
22
21
  const buildIncomingHttpPayload = (req, res, body) => ({
23
- type: 'http',
24
- http: {
25
- url: getFullUrl(req),
26
- method: req.method.toLowerCase(),
27
- ip: (0, utils_1.normalizeIp)(req.ip),
28
- incoming: true,
29
- request: {
30
- headers: req.headers,
31
- body: (0, utils_1.safeJsonParse)(req.body),
32
- },
33
- response: res
34
- ? {
35
- headers: res?.headers,
36
- status: res.statusCode,
37
- body: (0, utils_1.safeJsonParse)(body),
38
- }
39
- : undefined,
22
+ type: 'http',
23
+ http: {
24
+ url: getFullUrl(req),
25
+ method: req.method.toLowerCase(),
26
+ ip: (0, utils_1.normalizeIp)(req.ip),
27
+ incoming: true,
28
+ request: {
29
+ headers: req.headers,
30
+ body: (0, utils_1.safeJsonParse)(req.body),
40
31
  },
32
+ response: res
33
+ ? {
34
+ headers: res?.headers,
35
+ status: res.statusCode,
36
+ body: (0, utils_1.safeJsonParse)(body),
37
+ }
38
+ : undefined,
39
+ },
41
40
  });
42
- const traceMiddleware = ({ ignoreTraceRoutes = types_1.defaultIgnoreTraceRoutes, traceHeaderKey = types_1.defaultTraceHeaderKey, } = {
43
- ignoreTraceRoutes: types_1.defaultIgnoreTraceRoutes,
44
- traceHeaderKey: types_1.defaultTraceHeaderKey,
45
- }) => async (req, res, next) => {
46
- if ((0, utils_1.shouldIgnorePath)(req.path, ignoreTraceRoutes))
47
- return next();
41
+ const traceMiddleware =
42
+ (
43
+ {
44
+ ignoreTraceRoutes = types_1.defaultIgnoreTraceRoutes,
45
+ traceHeaderKey = types_1.defaultTraceHeaderKey,
46
+ } = {
47
+ ignoreTraceRoutes: types_1.defaultIgnoreTraceRoutes,
48
+ traceHeaderKey: types_1.defaultTraceHeaderKey,
49
+ }
50
+ ) =>
51
+ async (req, res, next) => {
52
+ if ((0, utils_1.shouldIgnorePath)(req.path, ignoreTraceRoutes)) return next();
48
53
  const traceId = req.header(traceHeaderKey) || (0, trace_1.getTraceId)();
49
54
  res.setHeader(traceHeaderKey, traceId);
50
- return await (0, trace_1.withTrace)(async () => {
51
- return await (0, span_1.startSpan)('Incoming HTTP request', async () => {
55
+ return await (0, trace_1.withTrace)(
56
+ async () => {
57
+ return await (0, span_1.startSpan)(
58
+ 'Incoming HTTP request',
59
+ async () => {
52
60
  return new Promise((resolve) => {
53
- // Capture response body for logging
54
- const send = res.send.bind(res);
55
- res.send = (body) => {
56
- res.locals.body = body;
57
- return send(body);
58
- };
59
- res.on('finish', () => resolve());
60
- return next();
61
+ // Capture response body for logging
62
+ const send = res.send.bind(res);
63
+ res.send = (body) => {
64
+ res.locals.body = body;
65
+ return send(body);
66
+ };
67
+ res.on('finish', () => resolve());
68
+ return next();
61
69
  });
62
- }, (o) => {
63
- return buildIncomingHttpPayload(req, o
70
+ },
71
+ (o) => {
72
+ return buildIncomingHttpPayload(
73
+ req,
74
+ o
64
75
  ? {
65
76
  statusCode: res.statusCode,
66
77
  headers: res.getHeaders(),
67
- }
68
- : undefined, res.locals.body);
69
- });
70
- }, { traceId });
71
- };
78
+ }
79
+ : undefined,
80
+ res.locals.body
81
+ );
82
+ }
83
+ );
84
+ },
85
+ { traceId }
86
+ );
87
+ };
72
88
  exports.traceMiddleware = traceMiddleware;
@@ -1,10 +1,12 @@
1
- type LogPayloadGetterOptions<T> = {
2
- error: unknown;
3
- result?: never;
4
- } | {
5
- error?: never;
6
- result: T;
7
- };
1
+ type LogPayloadGetterOptions<T> =
2
+ | {
3
+ error: unknown;
4
+ result?: never;
5
+ }
6
+ | {
7
+ error?: never;
8
+ result: T;
9
+ };
8
10
  type LogPayloadGetter<T> = object | ((o?: LogPayloadGetterOptions<T>) => object);
9
11
  /**
10
12
  * Starts a span that tracks the duration of a callback
@@ -12,5 +14,9 @@ type LogPayloadGetter<T> = object | ((o?: LogPayloadGetterOptions<T>) => object)
12
14
  * @param callback - The async callback to execute
13
15
  * @returns The result of the callback
14
16
  */
15
- export declare const startSpan: <T>(spanName: string, callback: () => Promise<T>, getter?: LogPayloadGetter<T>) => Promise<T>;
17
+ export declare const startSpan: <T>(
18
+ spanName: string,
19
+ callback: () => Promise<T>,
20
+ getter?: LogPayloadGetter<T>
21
+ ) => Promise<T>;
16
22
  export {};
@@ -1,10 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.startSpan = void 0;
4
- const uuid_1 = require("uuid");
5
- const storage_1 = require("./storage");
6
- const trace_1 = require("./trace");
7
- const logger_1 = require("./logger");
4
+ const uuid_1 = require('uuid');
5
+ const storage_1 = require('./storage');
6
+ const trace_1 = require('./trace');
7
+ const logger_1 = require('./logger');
8
8
  /**
9
9
  * Starts a span that tracks the duration of a callback
10
10
  * @param spanName - The span name to use for logging
@@ -12,30 +12,37 @@ const logger_1 = require("./logger");
12
12
  * @returns The result of the callback
13
13
  */
14
14
  const startSpan = async (spanName, callback, getter) => {
15
- const spanId = (0, uuid_1.v4)();
16
- const start = performance.now();
17
- const previous = storage_1.storage.getStore();
18
- return storage_1.storage.run({
19
- ...previous,
20
- traceId: previous?.traceId ?? (0, trace_1.getTraceId)(),
21
- spanId: previous?.spanId ? `${spanId} ${previous?.spanId}` : spanId,
22
- }, async () => {
23
- logger_1.logger.info({ ...getLogPayload(getter) }, `${spanName} started.`);
24
- try {
25
- const result = await callback();
26
- logger_1.logger.info({ ...getLogPayload(getter, { result }), duration: getDuration(start) }, `${spanName} completed.`);
27
- return result;
28
- }
29
- catch (error) {
30
- logger_1.logger.error({ ...getLogPayload(getter, { error }), err: error, duration: getDuration(start) }, `${spanName} failed.`);
31
- throw error;
32
- }
33
- });
15
+ const spanId = (0, uuid_1.v4)();
16
+ const start = performance.now();
17
+ const previous = storage_1.storage.getStore();
18
+ return storage_1.storage.run(
19
+ {
20
+ ...previous,
21
+ traceId: previous?.traceId ?? (0, trace_1.getTraceId)(),
22
+ spanId: previous?.spanId ? `${spanId} ${previous?.spanId}` : spanId,
23
+ },
24
+ async () => {
25
+ logger_1.logger.info({ ...getLogPayload(getter) }, `${spanName} started.`);
26
+ try {
27
+ const result = await callback();
28
+ logger_1.logger.info(
29
+ { ...getLogPayload(getter, { result }), duration: getDuration(start) },
30
+ `${spanName} completed.`
31
+ );
32
+ return result;
33
+ } catch (error) {
34
+ logger_1.logger.error(
35
+ { ...getLogPayload(getter, { error }), err: error, duration: getDuration(start) },
36
+ `${spanName} failed.`
37
+ );
38
+ throw error;
39
+ }
40
+ }
41
+ );
34
42
  };
35
43
  exports.startSpan = startSpan;
36
44
  const getLogPayload = (getter, o) => {
37
- if (typeof getter !== 'function')
38
- return getter;
39
- return getter(o);
45
+ if (typeof getter !== 'function') return getter;
46
+ return getter(o);
40
47
  };
41
48
  const getDuration = (start) => Math.round(performance.now() - start);
@@ -1,6 +1,6 @@
1
1
  import { AsyncLocalStorage } from 'async_hooks';
2
2
  export interface Context {
3
- traceId?: string;
4
- spanId?: string;
3
+ traceId?: string;
4
+ spanId?: string;
5
5
  }
6
6
  export declare const storage: AsyncLocalStorage<Context>;
@@ -1,5 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.storage = void 0;
4
- const async_hooks_1 = require("async_hooks");
4
+ const async_hooks_1 = require('async_hooks');
5
5
  exports.storage = new async_hooks_1.AsyncLocalStorage();
@@ -9,6 +9,12 @@ export declare const getTraceId: () => string;
9
9
  * @param extra - Extra context to add to the trace
10
10
  * @returns The result of the callback
11
11
  */
12
- export declare const withTrace: <T, E extends {
12
+ export declare const withTrace: <
13
+ T,
14
+ E extends {
13
15
  traceId?: string;
14
- }>(callback: () => T, extra?: E) => T;
16
+ },
17
+ >(
18
+ callback: () => T,
19
+ extra?: E
20
+ ) => T;
@@ -1,8 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.withTrace = exports.getTraceId = void 0;
4
- const uuid_1 = require("uuid");
5
- const storage_1 = require("./storage");
4
+ const uuid_1 = require('uuid');
5
+ const storage_1 = require('./storage');
6
6
  /**
7
7
  * Generates a trace ID
8
8
  * @returns A trace ID
@@ -16,6 +16,13 @@ exports.getTraceId = getTraceId;
16
16
  * @returns The result of the callback
17
17
  */
18
18
  const withTrace = (callback, extra) => {
19
- return storage_1.storage.run({ ...storage_1.storage.getStore(), ...extra, traceId: extra?.traceId ?? (0, exports.getTraceId)() }, callback);
19
+ return storage_1.storage.run(
20
+ {
21
+ ...storage_1.storage.getStore(),
22
+ ...extra,
23
+ traceId: extra?.traceId ?? (0, exports.getTraceId)(),
24
+ },
25
+ callback
26
+ );
20
27
  };
21
28
  exports.withTrace = withTrace;
@@ -14,48 +14,67 @@ export type LogType = 'general' | 'http' | 'sftp';
14
14
  */
15
15
  export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
16
16
  export interface Log<T extends LogType = 'general'> {
17
- logId: string;
18
- level: LogLevel;
19
- message: string;
20
- type: T;
21
- traceId?: string;
22
- spanId?: string;
23
- serviceId?: string;
24
- deploymentId?: string;
25
- workflowId?: string;
26
- http: T extends 'http' ? HttpPayload : never;
27
- sftp: T extends 'sftp' ? SftpPayload : never;
28
- duration?: number;
29
- err?: unknown;
17
+ logId: string;
18
+ level: LogLevel;
19
+ message: string;
20
+ type: T;
21
+ traceId?: string;
22
+ spanId?: string;
23
+ serviceId?: string;
24
+ deploymentId?: string;
25
+ workflowId?: string;
26
+ http: T extends 'http' ? HttpPayload : never;
27
+ sftp: T extends 'sftp' ? SftpPayload : never;
28
+ duration?: number;
29
+ err?: unknown;
30
30
  }
31
31
  export interface HttpPayload {
32
- url: string;
33
- method: HttpMethod;
34
- ip?: string;
35
- request: {
36
- headers: Record<string, string | string[]>;
37
- body?: unknown;
38
- };
39
- response?: {
40
- headers: Record<string, string | string[]>;
41
- status: number;
42
- body?: unknown;
43
- };
32
+ url: string;
33
+ method: HttpMethod;
34
+ ip?: string;
35
+ request: {
36
+ headers: Record<string, string | string[]>;
37
+ body?: unknown;
38
+ };
39
+ response?: {
40
+ headers: Record<string, string | string[]>;
41
+ status: number;
42
+ body?: unknown;
43
+ };
44
44
  }
45
- export type SftpOperation = 'connect' | 'upload' | 'download' | 'list' | 'delete' | 'rename' | 'stat' | 'exists' | 'realPath' | 'get' | 'put' | 'cwd' | 'mkdir' | 'rmdir' | 'chmod' | 'append' | 'uploadDir' | 'downloadDir' | 'close';
45
+ export type SftpOperation =
46
+ | 'connect'
47
+ | 'upload'
48
+ | 'download'
49
+ | 'list'
50
+ | 'delete'
51
+ | 'rename'
52
+ | 'stat'
53
+ | 'exists'
54
+ | 'realPath'
55
+ | 'get'
56
+ | 'put'
57
+ | 'cwd'
58
+ | 'mkdir'
59
+ | 'rmdir'
60
+ | 'chmod'
61
+ | 'append'
62
+ | 'uploadDir'
63
+ | 'downloadDir'
64
+ | 'close';
46
65
  export interface SftpPayload {
47
- host: string;
48
- operation: SftpOperation;
49
- path?: string;
50
- bytes?: number;
51
- destinationPath?: string;
52
- files?: string[];
53
- content?: string;
54
- exists?: string | false;
66
+ host: string;
67
+ operation: SftpOperation;
68
+ path?: string;
69
+ bytes?: number;
70
+ destinationPath?: string;
71
+ files?: string[];
72
+ content?: string;
73
+ exists?: string | false;
55
74
  }
56
75
  export interface HttpOptions {
57
- ignoreTraceRoutes?: string[];
58
- traceHeaderKey?: string;
76
+ ignoreTraceRoutes?: string[];
77
+ traceHeaderKey?: string;
59
78
  }
60
79
  export declare const defaultIgnoreTraceRoutes: string[];
61
- export declare const defaultTraceHeaderKey = "x-unnbound-trace-id";
80
+ export declare const defaultTraceHeaderKey = 'x-unnbound-trace-id';
@@ -1,8 +1,8 @@
1
- "use strict";
1
+ 'use strict';
2
2
  /**
3
3
  * Type definitions for structured logging library
4
4
  */
5
- Object.defineProperty(exports, "__esModule", { value: true });
5
+ Object.defineProperty(exports, '__esModule', { value: true });
6
6
  exports.defaultTraceHeaderKey = exports.defaultIgnoreTraceRoutes = void 0;
7
7
  exports.defaultIgnoreTraceRoutes = ['/health', '/healthcheck'];
8
8
  exports.defaultTraceHeaderKey = 'x-unnbound-trace-id';
@@ -1,5 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
3
  exports.shouldIgnorePath = void 0;
4
4
  exports.safeJsonParse = safeJsonParse;
5
5
  exports.normalizeIp = normalizeIp;
@@ -10,15 +10,15 @@ exports.normalizeIp = normalizeIp;
10
10
  * @returns boolean indicating if the path should be ignored
11
11
  */
12
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
- });
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
22
  };
23
23
  exports.shouldIgnorePath = shouldIgnorePath;
24
24
  /**
@@ -27,15 +27,14 @@ exports.shouldIgnorePath = shouldIgnorePath;
27
27
  * @returns Parsed JSON object or the original data.
28
28
  */
29
29
  function safeJsonParse(data) {
30
- if (typeof data === 'string') {
31
- try {
32
- return JSON.parse(data);
33
- }
34
- catch {
35
- return data;
36
- }
30
+ if (typeof data === 'string') {
31
+ try {
32
+ return JSON.parse(data);
33
+ } catch {
34
+ return data;
37
35
  }
38
- return data;
36
+ }
37
+ return data;
39
38
  }
40
39
  /**
41
40
  * Normalizes IP addresses by removing IPv6 mapping prefix for IPv4 addresses.
@@ -43,11 +42,10 @@ function safeJsonParse(data) {
43
42
  * @returns Normalized IP address string.
44
43
  */
45
44
  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;
45
+ if (!ip) return ip;
46
+ // Remove IPv4-mapped IPv6 prefix (::ffff:) to get clean IPv4 address
47
+ if (ip.startsWith('::ffff:')) {
48
+ return ip.substring(7);
49
+ }
50
+ return ip;
53
51
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unnbound-events",
3
3
  "description": "Unified events SDK to handle HTTP routes and SQS messages with a single routing API.",
4
- "version": "1.0.20",
4
+ "version": "1.0.21",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "author": "Unnbound Team",
@@ -19,7 +19,7 @@
19
19
  "@aws-sdk/client-sqs": "^3.0.0",
20
20
  "axios": "^1.12.2",
21
21
  "express": "^4.0.0 || ^5.0.0",
22
- "unnbound-logger-sdk": "3.0.22"
22
+ "unnbound-logger-sdk": "3.0.23"
23
23
  },
24
24
  "peerDependencies": {
25
25
  "express": "^4.0.0 || ^5.0.0"