unnbound-events 1.0.6 → 1.0.7

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.
@@ -6,37 +6,20 @@ export interface QueueEnvelope {
6
6
  workflow: string;
7
7
  priority: number;
8
8
  payload: {
9
- type: 'inline' | 's3-single' | 's3-multipart';
9
+ type: string;
10
10
  size: number;
11
11
  compressed: boolean;
12
- compressionType?: 'brotli' | 'gzip';
13
- location?: S3Reference;
14
12
  };
15
13
  request: {
16
14
  method: HttpMethod;
17
15
  path: string;
18
16
  headers: Record<string, string>;
19
- query: Record<string, string>;
20
- body?: unknown;
17
+ query: Record<string, unknown>;
18
+ body: unknown;
21
19
  };
22
- metadata?: QueueMetadata;
20
+ metadata?: Record<string, unknown>;
23
21
  addons?: Record<string, unknown>;
24
22
  }
25
- export interface QueueMetadata {
26
- correlationId: string;
27
- traceId: string;
28
- sourceIp: string;
29
- userAgent: string;
30
- receivedAt: number;
31
- processedAt: number;
32
- [key: string]: unknown;
33
- }
34
- export interface S3Reference {
35
- bucket: string;
36
- key: string;
37
- region?: string;
38
- versionId?: string;
39
- }
40
23
  export type SqsRecord = {
41
24
  messageId: string;
42
25
  body: string;
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createSqsConsumer = createSqsConsumer;
4
- const unnbound_logger_sdk_1 = require("unnbound-logger-sdk");
5
4
  function createSqsConsumer(client) {
6
5
  return async function consume(event) {
7
6
  let successes = 0;
@@ -9,34 +8,29 @@ function createSqsConsumer(client) {
9
8
  await Promise.all(event.Records.map(async (record) => {
10
9
  try {
11
10
  const envelope = JSON.parse(record.body);
12
- const traceContext = envelope.metadata?.traceId
13
- ? { traceId: envelope.metadata.traceId }
14
- : undefined;
15
- await (0, unnbound_logger_sdk_1.withTrace)(async () => {
16
- const req = {
17
- method: envelope.request.method,
18
- path: envelope.request.path,
19
- headers: envelope.request.headers,
20
- query: envelope.request.query,
21
- body: envelope.request.body,
22
- metadata: {
23
- source: 'sqs',
24
- id: envelope.id,
25
- workflow: envelope.workflow,
26
- timestamp: envelope.timestamp,
27
- version: envelope.version,
28
- ...envelope.metadata,
29
- },
30
- };
31
- const res = await client.handle(req);
32
- if (res.status >= 200 && res.status < 300)
33
- successes += 1;
34
- else
35
- failures.push({
36
- messageId: record.messageId,
37
- reason: `Handler returned status ${res.status}`,
38
- });
39
- }, traceContext);
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
+ });
40
34
  }
41
35
  catch (error) {
42
36
  const err = error instanceof Error ? error : new Error(String(error));
@@ -22,9 +22,32 @@ function matchPath(matcher, path) {
22
22
  function isMiddleware(value) {
23
23
  return typeof value === 'function' && value.length >= 2;
24
24
  }
25
+ const TRACE_HEADER_KEY = 'x-unnbound-trace-id';
26
+ const getHeaderValue = (headers, name) => {
27
+ const needle = name.toLowerCase();
28
+ for (const [key, value] of Object.entries(headers)) {
29
+ if (key.toLowerCase() === needle)
30
+ return value;
31
+ }
32
+ return undefined;
33
+ };
25
34
  function createEventsClient() {
26
35
  const routes = [];
27
36
  const middlewares = [];
37
+ const traceContextMiddleware = async (request, next) => {
38
+ const metadataTraceId = typeof request.metadata?.traceId === 'string' ? request.metadata.traceId : undefined;
39
+ const headerTraceId = getHeaderValue(request.headers, TRACE_HEADER_KEY);
40
+ const traceId = metadataTraceId ?? headerTraceId;
41
+ const context = traceId ? { traceId } : undefined;
42
+ const enrichedRequest = traceId
43
+ ? {
44
+ ...request,
45
+ metadata: { ...request.metadata, traceId },
46
+ }
47
+ : request;
48
+ return await (0, unnbound_logger_sdk_1.withTrace)(async () => next(enrichedRequest), context);
49
+ };
50
+ middlewares.push({ fn: traceContextMiddleware });
28
51
  return {
29
52
  on(method, matcher, handler) {
30
53
  routes.push({ method, matcher, handler });
@@ -65,10 +88,10 @@ function createEventsClient() {
65
88
  unnbound_logger_sdk_1.logger.warn({ path: request.path, method: request.method }, 'No route match');
66
89
  return { status: 404, body: { message: 'Not Found' } };
67
90
  }
91
+ // Build middleware chain for this request (middleware added later will run earlier)
68
92
  const executeHandler = async (req) => {
69
93
  return await route.handler(req);
70
94
  };
71
- // Build middleware chain for this request
72
95
  const applicable = middlewares.filter((m) => !m.matcher || matchPath(m.matcher, request.path));
73
96
  const composed = applicable.reduceRight((next, m) => async (req) => m.fn(req, next), executeHandler);
74
97
  try {
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.6",
4
+ "version": "1.0.7",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {