unnbound-events 1.0.19 → 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.
- package/LICENSE +21 -0
- package/README.md +50 -11
- package/dist/events/src/index.d.ts +9 -1
- package/dist/events/src/index.js +23 -8
- package/dist/events/src/lib/adapters/express.d.ts +12 -10
- package/dist/events/src/lib/adapters/express.js +27 -31
- package/dist/events/src/lib/adapters/sqs.d.ts +27 -27
- package/dist/events/src/lib/adapters/sqs.js +39 -39
- package/dist/events/src/lib/client.js +241 -298
- package/dist/events/src/lib/types.d.ts +45 -58
- package/dist/events/src/lib/types.js +2 -2
- package/dist/lib/client.js +71 -67
- package/dist/lib/types.d.ts +1 -0
- package/dist/logger/src/axios.d.ts +5 -4
- package/dist/logger/src/axios.js +70 -64
- package/dist/logger/src/index.js +50 -14
- package/dist/logger/src/logger.d.ts +18 -12
- package/dist/logger/src/logger.js +41 -39
- package/dist/logger/src/middleware.d.ts +5 -9
- package/dist/logger/src/middleware.js +76 -64
- package/dist/logger/src/span.d.ts +14 -8
- package/dist/logger/src/span.js +35 -28
- package/dist/logger/src/storage.d.ts +2 -2
- package/dist/logger/src/storage.js +3 -3
- package/dist/logger/src/trace.d.ts +8 -2
- package/dist/logger/src/trace.js +12 -5
- package/dist/logger/src/types.d.ts +57 -39
- package/dist/logger/src/types.js +2 -2
- package/dist/logger/src/utils.js +24 -26
- package/package.json +22 -22
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Unnbound Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -132,6 +132,56 @@ API:
|
|
|
132
132
|
- `use(matcher, middleware)` where matcher is `string | RegExp | (path) => boolean`
|
|
133
133
|
- `middleware(req, next) -> Promise<EventResponse>`
|
|
134
134
|
|
|
135
|
+
## Enqueue Events
|
|
136
|
+
|
|
137
|
+
The `enqueue` function allows you to send events to the queue by using the async endpoint.
|
|
138
|
+
|
|
139
|
+
### Basic Usage
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { createEventsClient } from 'unnbound-events-sdk';
|
|
143
|
+
|
|
144
|
+
const client = createEventsClient();
|
|
145
|
+
|
|
146
|
+
client.post('/process-job', async (req) => {
|
|
147
|
+
console.log('Handle job:', req.body);
|
|
148
|
+
|
|
149
|
+
// Split job into multiple tasks
|
|
150
|
+
const tasks = [...];
|
|
151
|
+
|
|
152
|
+
tasks.map(async (task) => {
|
|
153
|
+
await client.enqueue({
|
|
154
|
+
method: 'POST',
|
|
155
|
+
path: '/process-task',
|
|
156
|
+
body: { jobId: '123', data: 'important data' }
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return { status: 200, body: { processed: true } };
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
client.post('/process-task', async (req) => {
|
|
164
|
+
console.log('Handle task:', req.body);
|
|
165
|
+
|
|
166
|
+
return { status: 200, body: { processed: true } };
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
await client.start();
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Scheduled Jobs
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
// Trigger scheduled processing
|
|
176
|
+
setInterval(async () => {
|
|
177
|
+
await client.enqueue({
|
|
178
|
+
method: 'POST',
|
|
179
|
+
path: '/cleanup',
|
|
180
|
+
body: { timestamp: Date.now() },
|
|
181
|
+
});
|
|
182
|
+
}, 60000); // Every minute
|
|
183
|
+
```
|
|
184
|
+
|
|
135
185
|
## Message envelope
|
|
136
186
|
|
|
137
187
|
The SQS record `body` should be a JSON string matching this shape:
|
|
@@ -288,14 +338,3 @@ For S3 payload support, configure:
|
|
|
288
338
|
export UNNBOUND_S3_PAYLOAD_BUCKET="my-payload-bucket"
|
|
289
339
|
export AWS_REGION="us-west-2"
|
|
290
340
|
```
|
|
291
|
-
|
|
292
|
-
### AWS Credentials
|
|
293
|
-
|
|
294
|
-
When running in ECS with a Task Role, no additional credentials are needed. The AWS SDK will automatically use the ECS Task Role credentials.
|
|
295
|
-
|
|
296
|
-
For local development or other environments, set:
|
|
297
|
-
|
|
298
|
-
```bash
|
|
299
|
-
export AWS_ACCESS_KEY_ID="your-access-key"
|
|
300
|
-
export AWS_SECRET_ACCESS_KEY="your-secret-key"
|
|
301
|
-
```
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
export { createEventsClient } from './lib/client';
|
|
2
|
-
export type {
|
|
2
|
+
export type {
|
|
3
|
+
EventsClient,
|
|
4
|
+
EventRequest,
|
|
5
|
+
EventResponse,
|
|
6
|
+
RouteHandler,
|
|
7
|
+
RouteMatcher,
|
|
8
|
+
RegisteredRoute,
|
|
9
|
+
SqsBatchEvent,
|
|
10
|
+
} from './lib/types';
|
|
3
11
|
export { createExpressMiddleware } from './lib/adapters/express';
|
|
4
12
|
export { createSqsConsumer } from './lib/adapters/sqs';
|
package/dist/events/src/index.js
CHANGED
|
@@ -1,9 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
3
|
exports.createSqsConsumer = exports.createExpressMiddleware = exports.createEventsClient = void 0;
|
|
4
|
-
var client_1 = require(
|
|
5
|
-
Object.defineProperty(exports,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
var client_1 = require('./lib/client');
|
|
5
|
+
Object.defineProperty(exports, 'createEventsClient', {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function () {
|
|
8
|
+
return client_1.createEventsClient;
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
var express_1 = require('./lib/adapters/express');
|
|
12
|
+
Object.defineProperty(exports, 'createExpressMiddleware', {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return express_1.createExpressMiddleware;
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
var sqs_1 = require('./lib/adapters/sqs');
|
|
19
|
+
Object.defineProperty(exports, 'createSqsConsumer', {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () {
|
|
22
|
+
return sqs_1.createSqsConsumer;
|
|
23
|
+
},
|
|
24
|
+
});
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
type Request = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
method: string;
|
|
3
|
+
path: string;
|
|
4
|
+
headers: Record<string, string | string[] | undefined>;
|
|
5
|
+
query: Record<string, unknown>;
|
|
6
|
+
body: unknown;
|
|
7
7
|
};
|
|
8
8
|
type Response = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
status: (code: number) => Response;
|
|
10
|
+
setHeader: (name: string, value: string) => void;
|
|
11
|
+
send: (body: unknown) => void;
|
|
12
|
+
end: () => void;
|
|
13
13
|
};
|
|
14
14
|
import type { EventsClient } from '../types';
|
|
15
|
-
export declare function createExpressMiddleware(
|
|
15
|
+
export declare function createExpressMiddleware(
|
|
16
|
+
client: EventsClient
|
|
17
|
+
): (req: Request, res: Response) => Promise<void>;
|
|
16
18
|
export {};
|
|
@@ -1,38 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
3
|
exports.createExpressMiddleware = createExpressMiddleware;
|
|
4
4
|
function toEventRequest(req) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
metadata: { source: 'http' },
|
|
20
|
-
};
|
|
5
|
+
const method = req.method.toUpperCase();
|
|
6
|
+
const headers = {};
|
|
7
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
8
|
+
if (typeof value === 'string') headers[key] = value;
|
|
9
|
+
else if (Array.isArray(value)) headers[key] = value.join(',');
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
method,
|
|
13
|
+
path: req.path,
|
|
14
|
+
headers,
|
|
15
|
+
query: req.query,
|
|
16
|
+
body: req.body,
|
|
17
|
+
metadata: { source: 'http' },
|
|
18
|
+
};
|
|
21
19
|
}
|
|
22
20
|
function applyResponse(res, response) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return res.send(response.body);
|
|
30
|
-
return res.end();
|
|
21
|
+
res.status(response.status);
|
|
22
|
+
if (response.headers) {
|
|
23
|
+
for (const [k, v] of Object.entries(response.headers)) res.setHeader(k, v);
|
|
24
|
+
}
|
|
25
|
+
if (response.body !== undefined) return res.send(response.body);
|
|
26
|
+
return res.end();
|
|
31
27
|
}
|
|
32
28
|
function createExpressMiddleware(client) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
29
|
+
return async function eventsMiddleware(req, res) {
|
|
30
|
+
const eventReq = toEventRequest(req);
|
|
31
|
+
const eventRes = await client.handle(eventReq);
|
|
32
|
+
applyResponse(res, eventRes);
|
|
33
|
+
};
|
|
38
34
|
}
|
|
@@ -1,36 +1,36 @@
|
|
|
1
1
|
import type { EventsClient, HttpMethod } from '../types';
|
|
2
2
|
export interface QueueEnvelope {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
22
|
}
|
|
23
23
|
export type SqsRecord = {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
messageId: string;
|
|
25
|
+
body: string;
|
|
26
26
|
};
|
|
27
27
|
export interface SqsBatchEvent {
|
|
28
|
-
|
|
28
|
+
Records: SqsRecord[];
|
|
29
29
|
}
|
|
30
30
|
export declare function createSqsConsumer(client: EventsClient): (event: SqsBatchEvent) => Promise<{
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
successes: number;
|
|
32
|
+
failures: Array<{
|
|
33
|
+
messageId: string;
|
|
34
|
+
reason: string;
|
|
35
|
+
}>;
|
|
36
36
|
}>;
|
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
3
|
exports.createSqsConsumer = createSqsConsumer;
|
|
4
4
|
function createSqsConsumer(client) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
5
|
+
return async function consume(event) {
|
|
6
|
+
let successes = 0;
|
|
7
|
+
const failures = [];
|
|
8
|
+
await Promise.all(
|
|
9
|
+
event.Records.map(async (record) => {
|
|
10
|
+
try {
|
|
11
|
+
const envelope = JSON.parse(record.body);
|
|
12
|
+
const req = {
|
|
13
|
+
method: envelope.request.method,
|
|
14
|
+
path: envelope.request.path,
|
|
15
|
+
headers: envelope.request.headers,
|
|
16
|
+
query: envelope.request.query,
|
|
17
|
+
body: envelope.request.body,
|
|
18
|
+
metadata: {
|
|
19
|
+
source: 'sqs',
|
|
20
|
+
id: envelope.id,
|
|
21
|
+
workflow: envelope.workflow,
|
|
22
|
+
timestamp: envelope.timestamp,
|
|
23
|
+
version: envelope.version,
|
|
24
|
+
...envelope.metadata,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
const res = await client.handle(req);
|
|
28
|
+
if (res.status >= 200 && res.status < 300) successes += 1;
|
|
29
|
+
else
|
|
30
|
+
failures.push({
|
|
31
|
+
messageId: record.messageId,
|
|
32
|
+
reason: `Handler returned status ${res.status}`,
|
|
33
|
+
});
|
|
34
|
+
} catch (error) {
|
|
35
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
36
|
+
failures.push({ messageId: record.messageId, reason: err.message ?? 'Unknown error' });
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
);
|
|
40
|
+
return { successes, failures };
|
|
41
|
+
};
|
|
42
42
|
}
|