unnbound-events 0.1.0
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 +96 -0
- package/dist/events/src/index.d.ts +4 -0
- package/dist/events/src/index.js +9 -0
- package/dist/events/src/lib/adapters/express.d.ts +16 -0
- package/dist/events/src/lib/adapters/express.js +38 -0
- package/dist/events/src/lib/adapters/sqs.d.ts +36 -0
- package/dist/events/src/lib/adapters/sqs.js +42 -0
- package/dist/events/src/lib/client.d.ts +2 -0
- package/dist/events/src/lib/client.js +312 -0
- package/dist/events/src/lib/types.d.ts +73 -0
- package/dist/events/src/lib/types.js +2 -0
- package/dist/logger/src/axios.d.ts +9 -0
- package/dist/logger/src/axios.js +74 -0
- package/dist/logger/src/index.d.ts +13 -0
- package/dist/logger/src/index.js +14 -0
- package/dist/logger/src/logger.d.ts +15 -0
- package/dist/logger/src/logger.js +44 -0
- package/dist/logger/src/middleware.d.ts +3 -0
- package/dist/logger/src/middleware.js +72 -0
- package/dist/logger/src/span.d.ts +16 -0
- package/dist/logger/src/span.js +41 -0
- package/dist/logger/src/storage.d.ts +6 -0
- package/dist/logger/src/storage.js +5 -0
- package/dist/logger/src/trace.d.ts +14 -0
- package/dist/logger/src/trace.js +21 -0
- package/dist/logger/src/types.d.ts +61 -0
- package/dist/logger/src/types.js +8 -0
- package/dist/logger/src/utils.d.ts +19 -0
- package/dist/logger/src/utils.js +53 -0
- package/package.json +59 -0
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# Unnbound Events SDK
|
|
2
|
+
|
|
3
|
+
Unified routing for HTTP and SQS with one handler registration API.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add unnbound-events-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Simple Start (Recommended)
|
|
14
|
+
|
|
15
|
+
Register handlers and start with automatic graceful shutdown:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createEventsClient } from 'unnbound-events-sdk';
|
|
19
|
+
|
|
20
|
+
const client = createEventsClient();
|
|
21
|
+
|
|
22
|
+
// Register handlers
|
|
23
|
+
client.post('/email', async (req) => ({ status: 200, body: { ok: true } }));
|
|
24
|
+
client.get('/health', async () => ({ status: 200, body: { status: 'ok' } }));
|
|
25
|
+
|
|
26
|
+
// Start with defaults (HTTP on port 3000, SQS from environment variables)
|
|
27
|
+
await client.start();
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Custom Configuration
|
|
31
|
+
|
|
32
|
+
Configure specific transports as needed:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
// HTTP only
|
|
36
|
+
await client.start({ http: { port: 8080 } });
|
|
37
|
+
|
|
38
|
+
// SQS only
|
|
39
|
+
await client.start({
|
|
40
|
+
sqs: { queueUrl: 'https://sqs.us-west-2.amazonaws.com/123456789012/my-queue' },
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Both with custom settings
|
|
44
|
+
await client.start({
|
|
45
|
+
http: { port: 8080 },
|
|
46
|
+
sqs: {
|
|
47
|
+
queueUrl: 'https://sqs.us-west-2.amazonaws.com/123456789012/my-queue',
|
|
48
|
+
region: 'us-west-2',
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Manual Control
|
|
54
|
+
|
|
55
|
+
For more control over individual transports:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { createEventsClient } from 'unnbound-events-sdk';
|
|
59
|
+
|
|
60
|
+
const client = createEventsClient();
|
|
61
|
+
client.post('/email', async (req) => ({ status: 200, body: { ok: true } }));
|
|
62
|
+
|
|
63
|
+
// Built-in HTTP server
|
|
64
|
+
await client.http({ port: 3000 });
|
|
65
|
+
|
|
66
|
+
// SQS Lambda handler
|
|
67
|
+
export const handler = client.sqs();
|
|
68
|
+
|
|
69
|
+
// SQS long-poll listener (env-driven)
|
|
70
|
+
// Env: SQS_QUEUE_URL, AWS_REGION, AWS_SQS_ENDPOINT (optional)
|
|
71
|
+
await client.sqsListen();
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Message envelope
|
|
75
|
+
|
|
76
|
+
The SQS record `body` should be a JSON string matching this shape:
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
interface QueueEnvelope {
|
|
80
|
+
id: string;
|
|
81
|
+
timestamp: number;
|
|
82
|
+
version: string;
|
|
83
|
+
workflow: string;
|
|
84
|
+
priority: number;
|
|
85
|
+
payload: { type: string; size: number; compressed: boolean };
|
|
86
|
+
request: {
|
|
87
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
88
|
+
path: string;
|
|
89
|
+
headers: Record<string, string>;
|
|
90
|
+
query: Record<string, unknown>;
|
|
91
|
+
body: unknown;
|
|
92
|
+
};
|
|
93
|
+
metadata?: Record<string, unknown>;
|
|
94
|
+
addons?: Record<string, unknown>;
|
|
95
|
+
}
|
|
96
|
+
```
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { createEventsClient } from './lib/client';
|
|
2
|
+
export type { EventsClient, EventRequest, EventResponse, RouteHandler, RouteMatcher, RegisteredRoute, SqsBatchEvent, StartOptions, } from './lib/types';
|
|
3
|
+
export { createExpressMiddleware } from './lib/adapters/express';
|
|
4
|
+
export { createSqsConsumer } from './lib/adapters/sqs';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSqsConsumer = exports.createExpressMiddleware = exports.createEventsClient = void 0;
|
|
4
|
+
var client_1 = require("./lib/client");
|
|
5
|
+
Object.defineProperty(exports, "createEventsClient", { enumerable: true, get: function () { return client_1.createEventsClient; } });
|
|
6
|
+
var express_1 = require("./lib/adapters/express");
|
|
7
|
+
Object.defineProperty(exports, "createExpressMiddleware", { enumerable: true, get: function () { return express_1.createExpressMiddleware; } });
|
|
8
|
+
var sqs_1 = require("./lib/adapters/sqs");
|
|
9
|
+
Object.defineProperty(exports, "createSqsConsumer", { enumerable: true, get: function () { return sqs_1.createSqsConsumer; } });
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type Request = {
|
|
2
|
+
method: string;
|
|
3
|
+
path: string;
|
|
4
|
+
headers: Record<string, string | string[] | undefined>;
|
|
5
|
+
query: Record<string, unknown>;
|
|
6
|
+
body: unknown;
|
|
7
|
+
};
|
|
8
|
+
type Response = {
|
|
9
|
+
status: (code: number) => Response;
|
|
10
|
+
setHeader: (name: string, value: string) => void;
|
|
11
|
+
send: (body: unknown) => void;
|
|
12
|
+
end: () => void;
|
|
13
|
+
};
|
|
14
|
+
import type { EventsClient } from '../types';
|
|
15
|
+
export declare function createExpressMiddleware(client: EventsClient): (req: Request, res: Response) => Promise<void>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createExpressMiddleware = createExpressMiddleware;
|
|
4
|
+
function toEventRequest(req) {
|
|
5
|
+
const method = req.method.toUpperCase();
|
|
6
|
+
const headers = {};
|
|
7
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
8
|
+
if (typeof value === 'string')
|
|
9
|
+
headers[key] = value;
|
|
10
|
+
else if (Array.isArray(value))
|
|
11
|
+
headers[key] = value.join(',');
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
method,
|
|
15
|
+
path: req.path,
|
|
16
|
+
headers,
|
|
17
|
+
query: req.query,
|
|
18
|
+
body: req.body,
|
|
19
|
+
metadata: { source: 'http' },
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function applyResponse(res, response) {
|
|
23
|
+
res.status(response.status);
|
|
24
|
+
if (response.headers) {
|
|
25
|
+
for (const [k, v] of Object.entries(response.headers))
|
|
26
|
+
res.setHeader(k, v);
|
|
27
|
+
}
|
|
28
|
+
if (response.body !== undefined)
|
|
29
|
+
return res.send(response.body);
|
|
30
|
+
return res.end();
|
|
31
|
+
}
|
|
32
|
+
function createExpressMiddleware(client) {
|
|
33
|
+
return async function eventsMiddleware(req, res) {
|
|
34
|
+
const eventReq = toEventRequest(req);
|
|
35
|
+
const eventRes = await client.handle(eventReq);
|
|
36
|
+
applyResponse(res, eventRes);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { EventsClient, HttpMethod } from '../types';
|
|
2
|
+
export interface QueueEnvelope {
|
|
3
|
+
id: string;
|
|
4
|
+
timestamp: number;
|
|
5
|
+
version: string;
|
|
6
|
+
workflow: string;
|
|
7
|
+
priority: number;
|
|
8
|
+
payload: {
|
|
9
|
+
type: string;
|
|
10
|
+
size: number;
|
|
11
|
+
compressed: boolean;
|
|
12
|
+
};
|
|
13
|
+
request: {
|
|
14
|
+
method: HttpMethod;
|
|
15
|
+
path: string;
|
|
16
|
+
headers: Record<string, string>;
|
|
17
|
+
query: Record<string, unknown>;
|
|
18
|
+
body: unknown;
|
|
19
|
+
};
|
|
20
|
+
metadata?: Record<string, unknown>;
|
|
21
|
+
addons?: Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
export type SqsRecord = {
|
|
24
|
+
messageId: string;
|
|
25
|
+
body: string;
|
|
26
|
+
};
|
|
27
|
+
export interface SqsBatchEvent {
|
|
28
|
+
Records: SqsRecord[];
|
|
29
|
+
}
|
|
30
|
+
export declare function createSqsConsumer(client: EventsClient): (event: SqsBatchEvent) => Promise<{
|
|
31
|
+
successes: number;
|
|
32
|
+
failures: Array<{
|
|
33
|
+
messageId: string;
|
|
34
|
+
reason: string;
|
|
35
|
+
}>;
|
|
36
|
+
}>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSqsConsumer = createSqsConsumer;
|
|
4
|
+
function createSqsConsumer(client) {
|
|
5
|
+
return async function consume(event) {
|
|
6
|
+
let successes = 0;
|
|
7
|
+
const failures = [];
|
|
8
|
+
await Promise.all(event.Records.map(async (record) => {
|
|
9
|
+
try {
|
|
10
|
+
const envelope = JSON.parse(record.body);
|
|
11
|
+
const req = {
|
|
12
|
+
method: envelope.request.method,
|
|
13
|
+
path: envelope.request.path,
|
|
14
|
+
headers: envelope.request.headers,
|
|
15
|
+
query: envelope.request.query,
|
|
16
|
+
body: envelope.request.body,
|
|
17
|
+
metadata: {
|
|
18
|
+
source: 'sqs',
|
|
19
|
+
id: envelope.id,
|
|
20
|
+
workflow: envelope.workflow,
|
|
21
|
+
timestamp: envelope.timestamp,
|
|
22
|
+
version: envelope.version,
|
|
23
|
+
...envelope.metadata,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
const res = await client.handle(req);
|
|
27
|
+
if (res.status >= 200 && res.status < 300)
|
|
28
|
+
successes += 1;
|
|
29
|
+
else
|
|
30
|
+
failures.push({
|
|
31
|
+
messageId: record.messageId,
|
|
32
|
+
reason: `Handler returned status ${res.status}`,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
37
|
+
failures.push({ messageId: record.messageId, reason: err.message ?? 'Unknown error' });
|
|
38
|
+
}
|
|
39
|
+
}));
|
|
40
|
+
return { successes, failures };
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createEventsClient = createEventsClient;
|
|
4
|
+
const unnbound_logger_sdk_1 = require("unnbound-logger-sdk");
|
|
5
|
+
const http = (() => {
|
|
6
|
+
try {
|
|
7
|
+
return (require && require('http'));
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
})();
|
|
13
|
+
function matchPath(matcher, path) {
|
|
14
|
+
if (typeof matcher === 'string') {
|
|
15
|
+
return matcher === path;
|
|
16
|
+
}
|
|
17
|
+
if (matcher instanceof RegExp) {
|
|
18
|
+
return matcher.test(path);
|
|
19
|
+
}
|
|
20
|
+
return matcher(path);
|
|
21
|
+
}
|
|
22
|
+
function createEventsClient() {
|
|
23
|
+
const routes = [];
|
|
24
|
+
return {
|
|
25
|
+
on(method, matcher, handler) {
|
|
26
|
+
routes.push({ method, matcher, handler });
|
|
27
|
+
},
|
|
28
|
+
get(matcher, handler) {
|
|
29
|
+
routes.push({ method: 'GET', matcher, handler });
|
|
30
|
+
},
|
|
31
|
+
post(matcher, handler) {
|
|
32
|
+
routes.push({ method: 'POST', matcher, handler });
|
|
33
|
+
},
|
|
34
|
+
put(matcher, handler) {
|
|
35
|
+
routes.push({ method: 'PUT', matcher, handler });
|
|
36
|
+
},
|
|
37
|
+
patch(matcher, handler) {
|
|
38
|
+
routes.push({ method: 'PATCH', matcher, handler });
|
|
39
|
+
},
|
|
40
|
+
delete(matcher, handler) {
|
|
41
|
+
routes.push({ method: 'DELETE', matcher, handler });
|
|
42
|
+
},
|
|
43
|
+
options(matcher, handler) {
|
|
44
|
+
routes.push({ method: 'OPTIONS', matcher, handler });
|
|
45
|
+
},
|
|
46
|
+
head(matcher, handler) {
|
|
47
|
+
routes.push({ method: 'HEAD', matcher, handler });
|
|
48
|
+
},
|
|
49
|
+
async handle(request) {
|
|
50
|
+
const route = routes.find((r) => r.method === request.method && matchPath(r.matcher, request.path));
|
|
51
|
+
if (!route) {
|
|
52
|
+
unnbound_logger_sdk_1.logger.warn({ path: request.path, method: request.method }, 'No route match');
|
|
53
|
+
return { status: 404, body: { message: 'Not Found' } };
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const response = await route.handler(request);
|
|
57
|
+
return response;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
61
|
+
unnbound_logger_sdk_1.logger.error({ err, path: request.path }, 'Handler error');
|
|
62
|
+
return { status: 500, body: { message: 'Internal Server Error' } };
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
async http(options) {
|
|
66
|
+
const port = options?.port ?? 3000;
|
|
67
|
+
const server = http.createServer((req, res) => {
|
|
68
|
+
const method = (req.method || 'GET').toUpperCase();
|
|
69
|
+
const url = new URL(req.url || '/', 'http://localhost');
|
|
70
|
+
const path = url.pathname;
|
|
71
|
+
const query = {};
|
|
72
|
+
url.searchParams.forEach((value, key) => {
|
|
73
|
+
query[key] = value;
|
|
74
|
+
});
|
|
75
|
+
const headers = {};
|
|
76
|
+
for (const [k, v] of Object.entries(req.headers)) {
|
|
77
|
+
if (typeof v === 'string')
|
|
78
|
+
headers[k] = v;
|
|
79
|
+
else if (Array.isArray(v))
|
|
80
|
+
headers[k] = v.join(',');
|
|
81
|
+
}
|
|
82
|
+
let raw = '';
|
|
83
|
+
req.setEncoding('utf8');
|
|
84
|
+
req.on('data', (chunk) => {
|
|
85
|
+
raw += String(chunk);
|
|
86
|
+
});
|
|
87
|
+
req.on('end', async () => {
|
|
88
|
+
let body = undefined;
|
|
89
|
+
const contentType = headers['content-type'] || headers['Content-Type'];
|
|
90
|
+
if (raw) {
|
|
91
|
+
if (contentType && contentType.includes('application/json')) {
|
|
92
|
+
try {
|
|
93
|
+
body = JSON.parse(raw);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
body = raw;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
body = raw;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const eventReq = {
|
|
104
|
+
method,
|
|
105
|
+
path,
|
|
106
|
+
headers,
|
|
107
|
+
query,
|
|
108
|
+
body,
|
|
109
|
+
metadata: { source: 'http' },
|
|
110
|
+
};
|
|
111
|
+
const eventRes = await this.handle(eventReq);
|
|
112
|
+
res.statusCode = eventRes.status;
|
|
113
|
+
if (eventRes.headers) {
|
|
114
|
+
for (const [k, v] of Object.entries(eventRes.headers))
|
|
115
|
+
res.setHeader(k, v);
|
|
116
|
+
}
|
|
117
|
+
if (eventRes.body !== undefined) {
|
|
118
|
+
const bodyStr = typeof eventRes.body === 'string' ? eventRes.body : JSON.stringify(eventRes.body);
|
|
119
|
+
res.end(bodyStr);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
res.end();
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
await new Promise((resolve) => server.listen(port, resolve));
|
|
127
|
+
unnbound_logger_sdk_1.logger.info({ port }, 'HTTP server started');
|
|
128
|
+
return {
|
|
129
|
+
close() {
|
|
130
|
+
return new Promise((resolve, reject) => {
|
|
131
|
+
server.close((err) => {
|
|
132
|
+
if (err instanceof Error) {
|
|
133
|
+
reject(err);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (typeof err === 'string') {
|
|
137
|
+
reject(new Error(err));
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (err != null) {
|
|
141
|
+
reject(new Error('Server close failed'));
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
resolve();
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
sqs() {
|
|
151
|
+
return async (event) => {
|
|
152
|
+
let successes = 0;
|
|
153
|
+
const failures = [];
|
|
154
|
+
await Promise.all(event.Records.map(async (record) => {
|
|
155
|
+
try {
|
|
156
|
+
const envelope = JSON.parse(record.body);
|
|
157
|
+
const req = {
|
|
158
|
+
method: envelope.request.method,
|
|
159
|
+
path: envelope.request.path,
|
|
160
|
+
headers: envelope.request.headers,
|
|
161
|
+
query: envelope.request.query,
|
|
162
|
+
body: envelope.request.body,
|
|
163
|
+
metadata: { source: 'sqs', ...envelope.metadata },
|
|
164
|
+
};
|
|
165
|
+
const res = await this.handle(req);
|
|
166
|
+
if (res.status >= 200 && res.status < 300)
|
|
167
|
+
successes += 1;
|
|
168
|
+
else
|
|
169
|
+
failures.push({
|
|
170
|
+
messageId: record.messageId,
|
|
171
|
+
reason: `Handler returned status ${res.status}`,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
176
|
+
failures.push({
|
|
177
|
+
messageId: record.messageId,
|
|
178
|
+
reason: err.message ?? 'Unknown error',
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}));
|
|
182
|
+
return { successes, failures };
|
|
183
|
+
};
|
|
184
|
+
},
|
|
185
|
+
sqsListen(options) {
|
|
186
|
+
const awsSqs = (() => {
|
|
187
|
+
try {
|
|
188
|
+
return require('@aws-sdk/client-sqs');
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
})();
|
|
194
|
+
if (!awsSqs) {
|
|
195
|
+
throw new Error('@aws-sdk/client-sqs is required to use sqsListen');
|
|
196
|
+
}
|
|
197
|
+
const env = globalThis
|
|
198
|
+
.process?.env ?? {};
|
|
199
|
+
const queueUrl = options?.queueUrl || env.SQS_QUEUE_URL || env.QUEUE_URL;
|
|
200
|
+
if (!queueUrl || queueUrl === undefined) {
|
|
201
|
+
throw new Error('SQS queue URL not provided. Set SQS_QUEUE_URL env or pass options.queueUrl');
|
|
202
|
+
}
|
|
203
|
+
const region = options?.region || env.AWS_REGION || 'us-east-1';
|
|
204
|
+
const endpoint = env.AWS_SQS_ENDPOINT || env.AWS_ENDPOINT_URL || undefined;
|
|
205
|
+
const waitTimeSeconds = options?.waitTimeSeconds ?? 10;
|
|
206
|
+
const maxMessages = options?.maxMessages ?? 10;
|
|
207
|
+
const visibilityTimeout = options?.visibilityTimeoutSeconds;
|
|
208
|
+
const sqs = new awsSqs.SQSClient({ region, endpoint });
|
|
209
|
+
let stopped = false;
|
|
210
|
+
const consume = this.sqs();
|
|
211
|
+
async function loop() {
|
|
212
|
+
while (!stopped) {
|
|
213
|
+
try {
|
|
214
|
+
const params = {
|
|
215
|
+
QueueUrl: queueUrl,
|
|
216
|
+
MaxNumberOfMessages: maxMessages,
|
|
217
|
+
WaitTimeSeconds: waitTimeSeconds,
|
|
218
|
+
...(visibilityTimeout && { VisibilityTimeout: visibilityTimeout }),
|
|
219
|
+
};
|
|
220
|
+
const resp = (await sqs.send(new awsSqs.ReceiveMessageCommand(params)));
|
|
221
|
+
const messages = (resp.Messages ?? []).map((m) => ({
|
|
222
|
+
id: m.MessageId ?? '',
|
|
223
|
+
body: String(m.Body ?? ''),
|
|
224
|
+
receiptHandle: String(m.ReceiptHandle ?? ''),
|
|
225
|
+
}));
|
|
226
|
+
if (messages.length === 0)
|
|
227
|
+
continue;
|
|
228
|
+
const result = await consume({
|
|
229
|
+
Records: messages.map((m) => ({ messageId: m.id, body: m.body })),
|
|
230
|
+
});
|
|
231
|
+
const failed = new Set(result.failures.map((f) => f.messageId));
|
|
232
|
+
const toDelete = messages
|
|
233
|
+
.filter((m) => !failed.has(m.id))
|
|
234
|
+
.map((m) => ({ Id: m.id, ReceiptHandle: m.receiptHandle }));
|
|
235
|
+
for (let i = 0; i < toDelete.length; i += 10) {
|
|
236
|
+
const chunk = toDelete.slice(i, i + 10);
|
|
237
|
+
if (chunk.length > 0) {
|
|
238
|
+
await sqs.send(new awsSqs.DeleteMessageBatchCommand({ QueueUrl: queueUrl, Entries: chunk }));
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
catch (err) {
|
|
243
|
+
unnbound_logger_sdk_1.logger.error({ err }, 'SQS listen loop error');
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
// Start async loop without awaiting
|
|
248
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
249
|
+
loop();
|
|
250
|
+
return Promise.resolve({
|
|
251
|
+
stop() {
|
|
252
|
+
stopped = true;
|
|
253
|
+
return Promise.resolve();
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
},
|
|
257
|
+
async start(options) {
|
|
258
|
+
// Default to starting both HTTP and SQS if no options provided
|
|
259
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
260
|
+
const httpOptions = options?.http ?? { port: 3000 };
|
|
261
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
262
|
+
const sqsOptions = options?.sqs ?? {};
|
|
263
|
+
// Start HTTP server
|
|
264
|
+
let httpServer;
|
|
265
|
+
try {
|
|
266
|
+
httpServer = await this.http(httpOptions);
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
unnbound_logger_sdk_1.logger.warn({ error }, 'Failed to start HTTP server, continuing without it');
|
|
270
|
+
}
|
|
271
|
+
// Start SQS listener
|
|
272
|
+
let sqsListener;
|
|
273
|
+
try {
|
|
274
|
+
sqsListener = await this.sqsListen(sqsOptions);
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
unnbound_logger_sdk_1.logger.warn({ error }, 'Failed to start SQS listener, continuing without it');
|
|
278
|
+
}
|
|
279
|
+
// If neither HTTP nor SQS started successfully, throw an error
|
|
280
|
+
if (!httpServer && !sqsListener) {
|
|
281
|
+
throw new Error('Failed to start any transport. Please check your configuration and try again.');
|
|
282
|
+
}
|
|
283
|
+
// Set up graceful shutdown
|
|
284
|
+
const g = globalThis;
|
|
285
|
+
const shutdown = async () => {
|
|
286
|
+
unnbound_logger_sdk_1.logger.info('Received shutdown signal, closing servers...');
|
|
287
|
+
const promises = [];
|
|
288
|
+
if (httpServer)
|
|
289
|
+
promises.push(httpServer.close());
|
|
290
|
+
if (sqsListener)
|
|
291
|
+
promises.push(sqsListener.stop());
|
|
292
|
+
await Promise.all(promises);
|
|
293
|
+
unnbound_logger_sdk_1.logger.info('All servers closed');
|
|
294
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
295
|
+
g.process?.exit?.(0);
|
|
296
|
+
};
|
|
297
|
+
// Handle SIGINT and SIGTERM
|
|
298
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
299
|
+
g.process?.on?.('SIGINT', shutdown);
|
|
300
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
301
|
+
g.process?.on?.('SIGTERM', shutdown);
|
|
302
|
+
// Log startup information
|
|
303
|
+
const services = [];
|
|
304
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
305
|
+
if (httpServer)
|
|
306
|
+
services.push(`HTTP on port ${httpOptions?.port ?? 3000}`);
|
|
307
|
+
if (sqsListener)
|
|
308
|
+
services.push('SQS listener');
|
|
309
|
+
unnbound_logger_sdk_1.logger.info({ services: services.join(' and ') }, 'Services started. Press Ctrl+C to stop.');
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
2
|
+
export interface EventRequest<Body = unknown, Query = Record<string, unknown>> {
|
|
3
|
+
method: HttpMethod;
|
|
4
|
+
path: string;
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
query: Query;
|
|
7
|
+
body: Body;
|
|
8
|
+
metadata?: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
export interface EventResponse<Body = unknown> {
|
|
11
|
+
status: number;
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
body?: Body;
|
|
14
|
+
}
|
|
15
|
+
export type RouteHandler<ReqBody = unknown, ResBody = unknown> = (request: EventRequest<ReqBody>) => Promise<EventResponse<ResBody>> | EventResponse<ResBody>;
|
|
16
|
+
export type RouteMatcher = string | RegExp | ((path: string) => boolean);
|
|
17
|
+
export interface RegisteredRoute {
|
|
18
|
+
method: HttpMethod;
|
|
19
|
+
matcher: RouteMatcher;
|
|
20
|
+
handler: RouteHandler<any, any>;
|
|
21
|
+
}
|
|
22
|
+
export type SqsRecord = {
|
|
23
|
+
messageId: string;
|
|
24
|
+
body: string;
|
|
25
|
+
};
|
|
26
|
+
export interface SqsBatchEvent {
|
|
27
|
+
Records: SqsRecord[];
|
|
28
|
+
}
|
|
29
|
+
export interface StartOptions {
|
|
30
|
+
http?: {
|
|
31
|
+
port?: number;
|
|
32
|
+
};
|
|
33
|
+
sqs?: {
|
|
34
|
+
queueUrl?: string;
|
|
35
|
+
region?: string;
|
|
36
|
+
waitTimeSeconds?: number;
|
|
37
|
+
maxMessages?: number;
|
|
38
|
+
visibilityTimeoutSeconds?: number;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export interface EventsClient {
|
|
42
|
+
on: (method: HttpMethod, matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
43
|
+
handle: (request: EventRequest<any>) => Promise<EventResponse<any>>;
|
|
44
|
+
get: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
45
|
+
post: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
46
|
+
put: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
47
|
+
patch: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
48
|
+
delete: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
49
|
+
options: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
50
|
+
head: (matcher: RouteMatcher, handler: RouteHandler<any, any>) => void;
|
|
51
|
+
http: (options?: {
|
|
52
|
+
port?: number;
|
|
53
|
+
}) => Promise<{
|
|
54
|
+
close: () => Promise<void>;
|
|
55
|
+
}>;
|
|
56
|
+
sqs: () => (event: SqsBatchEvent) => Promise<{
|
|
57
|
+
successes: number;
|
|
58
|
+
failures: Array<{
|
|
59
|
+
messageId: string;
|
|
60
|
+
reason: string;
|
|
61
|
+
}>;
|
|
62
|
+
}>;
|
|
63
|
+
sqsListen: (options?: {
|
|
64
|
+
queueUrl?: string;
|
|
65
|
+
region?: string;
|
|
66
|
+
waitTimeSeconds?: number;
|
|
67
|
+
maxMessages?: number;
|
|
68
|
+
visibilityTimeoutSeconds?: number;
|
|
69
|
+
}) => Promise<{
|
|
70
|
+
stop: () => Promise<void>;
|
|
71
|
+
}>;
|
|
72
|
+
start: (options?: StartOptions) => Promise<void>;
|
|
73
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Axios } from 'axios';
|
|
2
|
+
import { HttpOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Wraps an axios instance to add tracing and span tracking
|
|
5
|
+
* @param axios - The axios instance to wrap
|
|
6
|
+
* @param options - Configuration options for HTTP tracing
|
|
7
|
+
* @returns The wrapped axios instance with span tracking
|
|
8
|
+
*/
|
|
9
|
+
export declare const traceAxios: (client: Axios, { ignoreTraceRoutes, traceHeaderKey, }?: HttpOptions) => Axios;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.traceAxios = void 0;
|
|
4
|
+
const axios_1 = require("axios");
|
|
5
|
+
const types_1 = require("./types");
|
|
6
|
+
const storage_1 = require("./storage");
|
|
7
|
+
const utils_1 = require("./utils");
|
|
8
|
+
const span_1 = require("./span");
|
|
9
|
+
const buildOutgoingHttpPayload = (config, res) => ({
|
|
10
|
+
type: 'http',
|
|
11
|
+
http: {
|
|
12
|
+
url: [config.baseURL, config.url].filter(Boolean).join(''),
|
|
13
|
+
method: config.method?.toLowerCase() ?? 'get',
|
|
14
|
+
incoming: false,
|
|
15
|
+
request: {
|
|
16
|
+
headers: config.headers,
|
|
17
|
+
body: (0, utils_1.safeJsonParse)(config.data),
|
|
18
|
+
},
|
|
19
|
+
response: res
|
|
20
|
+
? {
|
|
21
|
+
headers: res?.headers,
|
|
22
|
+
status: res.status,
|
|
23
|
+
body: (0, utils_1.safeJsonParse)(res.data),
|
|
24
|
+
}
|
|
25
|
+
: undefined,
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
/**
|
|
29
|
+
* Wraps an axios instance to add tracing and span tracking
|
|
30
|
+
* @param axios - The axios instance to wrap
|
|
31
|
+
* @param options - Configuration options for HTTP tracing
|
|
32
|
+
* @returns The wrapped axios instance with span tracking
|
|
33
|
+
*/
|
|
34
|
+
const traceAxios = (client, { ignoreTraceRoutes = types_1.defaultIgnoreTraceRoutes, traceHeaderKey = types_1.defaultTraceHeaderKey, } = {
|
|
35
|
+
ignoreTraceRoutes: types_1.defaultIgnoreTraceRoutes,
|
|
36
|
+
traceHeaderKey: types_1.defaultTraceHeaderKey,
|
|
37
|
+
}) => {
|
|
38
|
+
const createSpanWrappedRequest = (originalMethod, method) => {
|
|
39
|
+
const { headers: defaultHeaders, ...partialDefaultConfig } = client.defaults;
|
|
40
|
+
const headers = { ...defaultHeaders.common, ...(method && defaultHeaders[method]) };
|
|
41
|
+
const defaultConfig = { ...partialDefaultConfig, headers };
|
|
42
|
+
return async (config) => {
|
|
43
|
+
config = (0, axios_1.mergeConfig)(defaultConfig, config);
|
|
44
|
+
// Determine the actual config and URL
|
|
45
|
+
const url = [config.baseURL, config.url].filter(Boolean).join('');
|
|
46
|
+
// Check if this request should be ignored
|
|
47
|
+
if ((0, utils_1.shouldIgnorePath)(url, ignoreTraceRoutes))
|
|
48
|
+
return originalMethod(config);
|
|
49
|
+
const traceId = storage_1.storage.getStore()?.traceId;
|
|
50
|
+
config = { ...config, headers: { ...config.headers, [traceHeaderKey]: traceId } };
|
|
51
|
+
// Execute the request within a span
|
|
52
|
+
return (0, span_1.startSpan)(`Outgoing HTTP request`, () => originalMethod(config), (options) => {
|
|
53
|
+
if (!options)
|
|
54
|
+
return buildOutgoingHttpPayload(config);
|
|
55
|
+
if (options.error)
|
|
56
|
+
return buildOutgoingHttpPayload(config, (0, axios_1.isAxiosError)(options.error) ? options.error.response : undefined);
|
|
57
|
+
return buildOutgoingHttpPayload(config, options.result);
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
const request = client.request.bind(client);
|
|
62
|
+
const tracedRequest = createSpanWrappedRequest(request);
|
|
63
|
+
client.request = tracedRequest;
|
|
64
|
+
['post', 'put', 'patch'].forEach((method) => {
|
|
65
|
+
const func = createSpanWrappedRequest(request, method);
|
|
66
|
+
client[method] = (url, data, config) => func({ ...config, method, url, data });
|
|
67
|
+
});
|
|
68
|
+
['delete', 'get', 'head', 'options'].forEach((method) => {
|
|
69
|
+
const func = createSpanWrappedRequest(request, method);
|
|
70
|
+
client[method] = (url, config) => func({ ...config, method, url });
|
|
71
|
+
});
|
|
72
|
+
return client;
|
|
73
|
+
};
|
|
74
|
+
exports.traceAxios = traceAxios;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* unnbound-logger
|
|
3
|
+
*
|
|
4
|
+
* A structured logging library built on Pino with TypeScript support.
|
|
5
|
+
* Provides consistent, well-typed logging across different operational contexts.
|
|
6
|
+
*/
|
|
7
|
+
export type { LogLevel, LogType, HttpMethod, Log, HttpPayload, SftpPayload } from './types';
|
|
8
|
+
export type { UnnboundLogger } from './logger';
|
|
9
|
+
export { logger } from './logger';
|
|
10
|
+
export { withTrace, getTraceId } from './trace';
|
|
11
|
+
export { startSpan } from './span';
|
|
12
|
+
export { traceAxios } from './axios';
|
|
13
|
+
export { traceMiddleware } from './middleware';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.traceMiddleware = exports.traceAxios = exports.startSpan = exports.getTraceId = exports.withTrace = exports.logger = void 0;
|
|
4
|
+
var logger_1 = require("./logger");
|
|
5
|
+
Object.defineProperty(exports, "logger", { enumerable: true, get: function () { return logger_1.logger; } });
|
|
6
|
+
var trace_1 = require("./trace");
|
|
7
|
+
Object.defineProperty(exports, "withTrace", { enumerable: true, get: function () { return trace_1.withTrace; } });
|
|
8
|
+
Object.defineProperty(exports, "getTraceId", { enumerable: true, get: function () { return trace_1.getTraceId; } });
|
|
9
|
+
var span_1 = require("./span");
|
|
10
|
+
Object.defineProperty(exports, "startSpan", { enumerable: true, get: function () { return span_1.startSpan; } });
|
|
11
|
+
var axios_1 = require("./axios");
|
|
12
|
+
Object.defineProperty(exports, "traceAxios", { enumerable: true, get: function () { return axios_1.traceAxios; } });
|
|
13
|
+
var middleware_1 = require("./middleware");
|
|
14
|
+
Object.defineProperty(exports, "traceMiddleware", { enumerable: true, get: function () { return middleware_1.traceMiddleware; } });
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import pino from 'pino';
|
|
2
|
+
export interface UnnboundLogger extends Omit<pino.Logger, 'trace' | 'debug' | 'error' | 'info' | 'warn'> {
|
|
3
|
+
trace(object: {}, message: string): void;
|
|
4
|
+
trace(message: string): void;
|
|
5
|
+
debug(object: {}, message: string): void;
|
|
6
|
+
debug(message: string): void;
|
|
7
|
+
info(object: {}, message: string): void;
|
|
8
|
+
info(message: string): void;
|
|
9
|
+
warn(object: {}, message: string): void;
|
|
10
|
+
warn(message: string): void;
|
|
11
|
+
error<O extends {
|
|
12
|
+
err: unknown;
|
|
13
|
+
}>(object: O, message: string): void;
|
|
14
|
+
}
|
|
15
|
+
export declare const logger: UnnboundLogger;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.logger = void 0;
|
|
7
|
+
const pino_1 = __importDefault(require("pino"));
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
9
|
+
const storage_1 = require("./storage");
|
|
10
|
+
exports.logger = (0, pino_1.default)({
|
|
11
|
+
level: process.env.LOG_LEVEL ?? 'info',
|
|
12
|
+
base: {
|
|
13
|
+
environment: process.env.ENVIRONMENT,
|
|
14
|
+
workflowId: process.env.UNNBOUND_WORKFLOW_ID,
|
|
15
|
+
serviceId: process.env.UNNBOUND_SERVICE_ID,
|
|
16
|
+
deploymentId: process.env.UNNBOUND_DEPLOYMENT_ID,
|
|
17
|
+
},
|
|
18
|
+
mixin: () => ({ ...storage_1.storage.getStore(), logId: (0, uuid_1.v4)() }),
|
|
19
|
+
serializers: {
|
|
20
|
+
req: pino_1.default.stdSerializers.req,
|
|
21
|
+
res: pino_1.default.stdSerializers.res,
|
|
22
|
+
err: pino_1.default.stdSerializers.err,
|
|
23
|
+
},
|
|
24
|
+
// Let CloudWatch handle timestamps
|
|
25
|
+
timestamp: false,
|
|
26
|
+
// Change message field from 'msg' to 'message'
|
|
27
|
+
messageKey: 'message',
|
|
28
|
+
formatters: {
|
|
29
|
+
level: (level) => ({ level }),
|
|
30
|
+
},
|
|
31
|
+
redact: {
|
|
32
|
+
paths: [
|
|
33
|
+
'http.request.headers.authorization',
|
|
34
|
+
'http.response.headers.authorization',
|
|
35
|
+
'http.request.headers.Authorization',
|
|
36
|
+
'http.response.headers.Authorization',
|
|
37
|
+
'err.config',
|
|
38
|
+
],
|
|
39
|
+
remove: true,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
process.on('uncaughtException', (err, origin) => {
|
|
43
|
+
exports.logger.error({ err, origin }, `Uncaught exception.`);
|
|
44
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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");
|
|
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}`;
|
|
21
|
+
};
|
|
22
|
+
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,
|
|
40
|
+
},
|
|
41
|
+
});
|
|
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();
|
|
48
|
+
const traceId = req.header(traceHeaderKey) || (0, trace_1.getTraceId)();
|
|
49
|
+
res.setHeader(traceHeaderKey, traceId);
|
|
50
|
+
return await (0, trace_1.withTrace)(async () => {
|
|
51
|
+
return await (0, span_1.startSpan)('Incoming HTTP request', async () => {
|
|
52
|
+
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
|
+
});
|
|
62
|
+
}, (o) => {
|
|
63
|
+
return buildIncomingHttpPayload(req, o
|
|
64
|
+
? {
|
|
65
|
+
statusCode: res.statusCode,
|
|
66
|
+
headers: res.getHeaders(),
|
|
67
|
+
}
|
|
68
|
+
: undefined, res.locals.body);
|
|
69
|
+
});
|
|
70
|
+
}, { traceId });
|
|
71
|
+
};
|
|
72
|
+
exports.traceMiddleware = traceMiddleware;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type LogPayloadGetterOptions<T> = {
|
|
2
|
+
error: unknown;
|
|
3
|
+
result?: never;
|
|
4
|
+
} | {
|
|
5
|
+
error?: never;
|
|
6
|
+
result: T;
|
|
7
|
+
};
|
|
8
|
+
type LogPayloadGetter<T> = object | ((o?: LogPayloadGetterOptions<T>) => object);
|
|
9
|
+
/**
|
|
10
|
+
* Starts a span that tracks the duration of a callback
|
|
11
|
+
* @param spanName - The span name to use for logging
|
|
12
|
+
* @param callback - The async callback to execute
|
|
13
|
+
* @returns The result of the callback
|
|
14
|
+
*/
|
|
15
|
+
export declare const startSpan: <T>(spanName: string, callback: () => Promise<T>, getter?: LogPayloadGetter<T>) => Promise<T>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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");
|
|
8
|
+
/**
|
|
9
|
+
* Starts a span that tracks the duration of a callback
|
|
10
|
+
* @param spanName - The span name to use for logging
|
|
11
|
+
* @param callback - The async callback to execute
|
|
12
|
+
* @returns The result of the callback
|
|
13
|
+
*/
|
|
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
|
+
});
|
|
34
|
+
};
|
|
35
|
+
exports.startSpan = startSpan;
|
|
36
|
+
const getLogPayload = (getter, o) => {
|
|
37
|
+
if (typeof getter !== 'function')
|
|
38
|
+
return getter;
|
|
39
|
+
return getter(o);
|
|
40
|
+
};
|
|
41
|
+
const getDuration = (start) => Math.round(performance.now() - start);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a trace ID
|
|
3
|
+
* @returns A trace ID
|
|
4
|
+
*/
|
|
5
|
+
export declare const getTraceId: () => string;
|
|
6
|
+
/**
|
|
7
|
+
* Runs a callback with a trace ID
|
|
8
|
+
* @param callback - The callback to run
|
|
9
|
+
* @param extra - Extra context to add to the trace
|
|
10
|
+
* @returns The result of the callback
|
|
11
|
+
*/
|
|
12
|
+
export declare const withTrace: <T, E extends {
|
|
13
|
+
traceId?: string;
|
|
14
|
+
}>(callback: () => T, extra?: E) => T;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withTrace = exports.getTraceId = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
const storage_1 = require("./storage");
|
|
6
|
+
/**
|
|
7
|
+
* Generates a trace ID
|
|
8
|
+
* @returns A trace ID
|
|
9
|
+
*/
|
|
10
|
+
const getTraceId = () => (0, uuid_1.v4)();
|
|
11
|
+
exports.getTraceId = getTraceId;
|
|
12
|
+
/**
|
|
13
|
+
* Runs a callback with a trace ID
|
|
14
|
+
* @param callback - The callback to run
|
|
15
|
+
* @param extra - Extra context to add to the trace
|
|
16
|
+
* @returns The result of the callback
|
|
17
|
+
*/
|
|
18
|
+
const withTrace = (callback, extra) => {
|
|
19
|
+
return storage_1.storage.run({ ...storage_1.storage.getStore(), ...extra, traceId: extra?.traceId ?? (0, exports.getTraceId)() }, callback);
|
|
20
|
+
};
|
|
21
|
+
exports.withTrace = withTrace;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for structured logging library
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Available log levels
|
|
6
|
+
*/
|
|
7
|
+
export type LogLevel = 'info' | 'debug' | 'error' | 'warn';
|
|
8
|
+
/**
|
|
9
|
+
* Available log types
|
|
10
|
+
*/
|
|
11
|
+
export type LogType = 'general' | 'http' | 'sftp';
|
|
12
|
+
/**
|
|
13
|
+
* HTTP methods supported for HTTP logging
|
|
14
|
+
*/
|
|
15
|
+
export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
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;
|
|
30
|
+
}
|
|
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
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export type SftpOperation = 'connect' | 'upload' | 'download' | 'list' | 'delete' | 'rename' | 'stat' | 'exists' | 'realPath' | 'get' | 'put' | 'cwd' | 'mkdir' | 'rmdir' | 'chmod' | 'append' | 'uploadDir' | 'downloadDir' | 'close';
|
|
46
|
+
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;
|
|
55
|
+
}
|
|
56
|
+
export interface HttpOptions {
|
|
57
|
+
ignoreTraceRoutes?: string[];
|
|
58
|
+
traceHeaderKey?: string;
|
|
59
|
+
}
|
|
60
|
+
export declare const defaultIgnoreTraceRoutes: string[];
|
|
61
|
+
export declare const defaultTraceHeaderKey = "x-unnbound-trace-id";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Type definitions for structured logging library
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.defaultTraceHeaderKey = exports.defaultIgnoreTraceRoutes = void 0;
|
|
7
|
+
exports.defaultIgnoreTraceRoutes = ['/health', '/healthcheck'];
|
|
8
|
+
exports.defaultTraceHeaderKey = 'x-unnbound-trace-id';
|
|
@@ -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;
|
|
@@ -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
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "unnbound-events",
|
|
3
|
+
"description": "Unified events SDK to handle HTTP routes and SQS messages with a single routing API.",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "jest",
|
|
10
|
+
"test:coverage": "jest --coverage",
|
|
11
|
+
"test:watch": "jest --watch",
|
|
12
|
+
"typecheck": "tsc -noEmit",
|
|
13
|
+
"lint": "eslint --cache --cache-location ./node_modules/.cache/eslint .",
|
|
14
|
+
"lint:fix": "pnpm lint --fix",
|
|
15
|
+
"format": "prettier --write .",
|
|
16
|
+
"format:check": "prettier --check .",
|
|
17
|
+
"prepublishOnly": "pnpm build",
|
|
18
|
+
"start:example:express": "npx --yes tsx --watch examples/express.ts",
|
|
19
|
+
"start:example:sqs": "npx --yes tsx --watch examples/sqs.ts",
|
|
20
|
+
"start:example:http-and-sqs": "npx --yes tsx --watch examples/http-and-sqs.ts"
|
|
21
|
+
},
|
|
22
|
+
"author": "Unnbound Team",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/unnbounddev/unnbound-sdks.git"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/unnbounddev/unnbound-sdks#readme",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/unnbounddev/unnbound-sdks/issues"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"express": "^4.0.0 || ^5.0.0",
|
|
34
|
+
"@aws-sdk/client-sqs": "^3.0.0",
|
|
35
|
+
"unnbound-logger-sdk": "workspace:*"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"express": "^4.0.0 || ^5.0.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"express": {
|
|
42
|
+
"optional": true
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/express": "^4.17.21",
|
|
47
|
+
"@types/jest": "^29.5.12",
|
|
48
|
+
"@types/node": "^22"
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist/**/*",
|
|
52
|
+
"README.md",
|
|
53
|
+
"LICENSE"
|
|
54
|
+
],
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=22"
|
|
57
|
+
},
|
|
58
|
+
"sideEffects": false
|
|
59
|
+
}
|