@amqp-contract/worker 0.0.4 → 0.0.5
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 +4 -24
- package/dist/index.cjs +36 -44
- package/dist/index.d.cts +27 -27
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +27 -27
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +36 -43
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ pnpm add @amqp-contract/worker amqplib
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
|
-
import {
|
|
16
|
+
import { TypedAmqpWorker } from '@amqp-contract/worker';
|
|
17
17
|
import { connect } from 'amqplib';
|
|
18
18
|
import { contract } from './contract';
|
|
19
19
|
|
|
@@ -21,7 +21,7 @@ import { contract } from './contract';
|
|
|
21
21
|
const connection = await connect('amqp://localhost');
|
|
22
22
|
|
|
23
23
|
// Create worker from contract with handlers (automatically connects and starts consuming)
|
|
24
|
-
const worker = await
|
|
24
|
+
const worker = await TypedAmqpWorker.create({
|
|
25
25
|
contract,
|
|
26
26
|
handlers: {
|
|
27
27
|
processOrder: async (message) => {
|
|
@@ -40,7 +40,7 @@ const worker = await createWorker({
|
|
|
40
40
|
|
|
41
41
|
## API
|
|
42
42
|
|
|
43
|
-
### `
|
|
43
|
+
### `TypedAmqpWorker.create(options)`
|
|
44
44
|
|
|
45
45
|
Create a type-safe AMQP worker from a contract with message handlers. Automatically connects and starts consuming all messages.
|
|
46
46
|
|
|
@@ -50,27 +50,7 @@ Create a type-safe AMQP worker from a contract with message handlers. Automatica
|
|
|
50
50
|
- `options.handlers` - Object with handler functions for each consumer
|
|
51
51
|
- `options.connection` - amqplib Connection object
|
|
52
52
|
|
|
53
|
-
### `
|
|
54
|
-
|
|
55
|
-
Connect to an AMQP broker and set up all exchanges, queues, and bindings defined in the contract.
|
|
56
|
-
|
|
57
|
-
**Note:** When using `createWorker()`, this is called automatically.
|
|
58
|
-
|
|
59
|
-
### `AmqpWorker.consume(consumerName)`
|
|
60
|
-
|
|
61
|
-
Start consuming messages for a specific consumer.
|
|
62
|
-
|
|
63
|
-
### `AmqpWorker.consumeAll()`
|
|
64
|
-
|
|
65
|
-
Start consuming messages for all consumers defined in the contract.
|
|
66
|
-
|
|
67
|
-
**Note:** When using `createWorker()`, this is called automatically.
|
|
68
|
-
|
|
69
|
-
### `AmqpWorker.stopConsuming()`
|
|
70
|
-
|
|
71
|
-
Stop consuming messages from all consumers.
|
|
72
|
-
|
|
73
|
-
### `AmqpWorker.close()`
|
|
53
|
+
### `TypedAmqpWorker.close()`
|
|
74
54
|
|
|
75
55
|
Stop consuming and close the channel and connection.
|
|
76
56
|
|
package/dist/index.cjs
CHANGED
|
@@ -3,39 +3,64 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Type-safe AMQP worker for consuming messages
|
|
5
5
|
*/
|
|
6
|
-
var
|
|
6
|
+
var TypedAmqpWorker = class TypedAmqpWorker {
|
|
7
7
|
channel = null;
|
|
8
|
-
connection = null;
|
|
9
8
|
consumerTags = [];
|
|
10
|
-
constructor(contract, handlers) {
|
|
9
|
+
constructor(contract, handlers, connection) {
|
|
11
10
|
this.contract = contract;
|
|
12
11
|
this.handlers = handlers;
|
|
12
|
+
this.connection = connection;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Create a type-safe AMQP worker from a contract
|
|
16
|
+
* The worker will automatically connect and start consuming all messages
|
|
17
|
+
*/
|
|
18
|
+
static async create(options) {
|
|
19
|
+
const worker = new TypedAmqpWorker(options.contract, options.handlers, options.connection);
|
|
20
|
+
await worker.init();
|
|
21
|
+
await worker.consumeAll();
|
|
22
|
+
return worker;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Close the connection
|
|
26
|
+
*/
|
|
27
|
+
async close() {
|
|
28
|
+
await this.stopConsuming();
|
|
29
|
+
if (this.channel) await this.channel.close();
|
|
30
|
+
await this.connection.close();
|
|
13
31
|
}
|
|
14
32
|
/**
|
|
15
33
|
* Connect to AMQP broker
|
|
16
34
|
*/
|
|
17
|
-
async
|
|
18
|
-
this.
|
|
19
|
-
this.
|
|
20
|
-
if (this.contract.exchanges && this.channel) for (const exchange of Object.values(this.contract.exchanges)) await this.channel.assertExchange(exchange.name, exchange.type, {
|
|
35
|
+
async init() {
|
|
36
|
+
this.channel = await this.connection.createChannel();
|
|
37
|
+
if (this.contract.exchanges) for (const exchange of Object.values(this.contract.exchanges)) await this.channel.assertExchange(exchange.name, exchange.type, {
|
|
21
38
|
durable: exchange.durable,
|
|
22
39
|
autoDelete: exchange.autoDelete,
|
|
23
40
|
internal: exchange.internal,
|
|
24
41
|
arguments: exchange.arguments
|
|
25
42
|
});
|
|
26
|
-
if (this.contract.queues
|
|
43
|
+
if (this.contract.queues) for (const queue of Object.values(this.contract.queues)) await this.channel.assertQueue(queue.name, {
|
|
27
44
|
durable: queue.durable,
|
|
28
45
|
exclusive: queue.exclusive,
|
|
29
46
|
autoDelete: queue.autoDelete,
|
|
30
47
|
arguments: queue.arguments
|
|
31
48
|
});
|
|
32
|
-
if (this.contract.bindings
|
|
49
|
+
if (this.contract.bindings) for (const binding of Object.values(this.contract.bindings)) await this.channel.bindQueue(binding.queue, binding.exchange, binding.routingKey ?? "", binding.arguments);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Start consuming messages for all consumers
|
|
53
|
+
*/
|
|
54
|
+
async consumeAll() {
|
|
55
|
+
if (!this.contract.consumers) throw new Error("No consumers defined in contract");
|
|
56
|
+
const consumerNames = Object.keys(this.contract.consumers);
|
|
57
|
+
for (const consumerName of consumerNames) await this.consume(consumerName);
|
|
33
58
|
}
|
|
34
59
|
/**
|
|
35
60
|
* Start consuming messages for a specific consumer
|
|
36
61
|
*/
|
|
37
62
|
async consume(consumerName) {
|
|
38
|
-
if (!this.channel) throw new Error("Worker not
|
|
63
|
+
if (!this.channel) throw new Error("Worker not initialized. Use TypedAmqpWorker.create() to obtain an initialized worker instance.");
|
|
39
64
|
const consumers = this.contract.consumers;
|
|
40
65
|
if (!consumers) throw new Error("No consumers defined in contract");
|
|
41
66
|
const consumer = consumers[consumerName];
|
|
@@ -64,14 +89,6 @@ var AmqpWorker = class {
|
|
|
64
89
|
this.consumerTags.push(result.consumerTag);
|
|
65
90
|
}
|
|
66
91
|
/**
|
|
67
|
-
* Start consuming messages for all consumers
|
|
68
|
-
*/
|
|
69
|
-
async consumeAll() {
|
|
70
|
-
if (!this.contract.consumers) throw new Error("No consumers defined in contract");
|
|
71
|
-
const consumerNames = Object.keys(this.contract.consumers);
|
|
72
|
-
for (const consumerName of consumerNames) await this.consume(consumerName);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
92
|
* Stop consuming messages
|
|
76
93
|
*/
|
|
77
94
|
async stopConsuming() {
|
|
@@ -79,32 +96,7 @@ var AmqpWorker = class {
|
|
|
79
96
|
for (const tag of this.consumerTags) await this.channel.cancel(tag);
|
|
80
97
|
this.consumerTags = [];
|
|
81
98
|
}
|
|
82
|
-
/**
|
|
83
|
-
* Close the connection
|
|
84
|
-
*/
|
|
85
|
-
async close() {
|
|
86
|
-
await this.stopConsuming();
|
|
87
|
-
if (this.channel) {
|
|
88
|
-
await this.channel.close();
|
|
89
|
-
this.channel = null;
|
|
90
|
-
}
|
|
91
|
-
if (this.connection) {
|
|
92
|
-
await this.connection.close();
|
|
93
|
-
this.connection = null;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
99
|
};
|
|
97
|
-
/**
|
|
98
|
-
* Create a type-safe AMQP worker from a contract
|
|
99
|
-
* The worker will automatically connect and start consuming all messages
|
|
100
|
-
*/
|
|
101
|
-
async function createWorker(options) {
|
|
102
|
-
const worker = new AmqpWorker(options.contract, options.handlers);
|
|
103
|
-
await worker.connect(options.connection);
|
|
104
|
-
await worker.consumeAll();
|
|
105
|
-
return worker;
|
|
106
|
-
}
|
|
107
100
|
|
|
108
101
|
//#endregion
|
|
109
|
-
exports.
|
|
110
|
-
exports.createWorker = createWorker;
|
|
102
|
+
exports.TypedAmqpWorker = TypedAmqpWorker;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
1
|
import { ChannelModel } from "amqplib";
|
|
2
|
-
import { ContractDefinition,
|
|
2
|
+
import { ContractDefinition, WorkerInferConsumerHandlers } from "@amqp-contract/contract";
|
|
3
3
|
|
|
4
4
|
//#region src/worker.d.ts
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Options for creating a worker
|
|
8
|
+
*/
|
|
9
|
+
interface CreateWorkerOptions<TContract extends ContractDefinition> {
|
|
10
|
+
contract: TContract;
|
|
11
|
+
handlers: WorkerInferConsumerHandlers<TContract>;
|
|
12
|
+
connection: ChannelModel;
|
|
13
|
+
}
|
|
6
14
|
/**
|
|
7
15
|
* Type-safe AMQP worker for consuming messages
|
|
8
16
|
*/
|
|
9
|
-
declare class
|
|
17
|
+
declare class TypedAmqpWorker<TContract extends ContractDefinition> {
|
|
10
18
|
private readonly contract;
|
|
11
19
|
private readonly handlers;
|
|
20
|
+
private readonly connection;
|
|
12
21
|
private channel;
|
|
13
|
-
private connection;
|
|
14
22
|
private consumerTags;
|
|
15
|
-
constructor(
|
|
23
|
+
private constructor();
|
|
16
24
|
/**
|
|
17
|
-
*
|
|
25
|
+
* Create a type-safe AMQP worker from a contract
|
|
26
|
+
* The worker will automatically connect and start consuming all messages
|
|
18
27
|
*/
|
|
19
|
-
|
|
28
|
+
static create<TContract extends ContractDefinition>(options: CreateWorkerOptions<TContract>): Promise<TypedAmqpWorker<TContract>>;
|
|
20
29
|
/**
|
|
21
|
-
*
|
|
30
|
+
* Close the connection
|
|
31
|
+
*/
|
|
32
|
+
close(): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Connect to AMQP broker
|
|
22
35
|
*/
|
|
23
|
-
|
|
36
|
+
private init;
|
|
24
37
|
/**
|
|
25
38
|
* Start consuming messages for all consumers
|
|
26
39
|
*/
|
|
27
|
-
consumeAll
|
|
40
|
+
private consumeAll;
|
|
28
41
|
/**
|
|
29
|
-
*
|
|
42
|
+
* Start consuming messages for a specific consumer
|
|
30
43
|
*/
|
|
31
|
-
|
|
44
|
+
private consume;
|
|
32
45
|
/**
|
|
33
|
-
*
|
|
46
|
+
* Stop consuming messages
|
|
34
47
|
*/
|
|
35
|
-
|
|
48
|
+
private stopConsuming;
|
|
36
49
|
}
|
|
37
|
-
/**
|
|
38
|
-
* Options for creating a worker
|
|
39
|
-
*/
|
|
40
|
-
interface CreateWorkerOptions<TContract extends ContractDefinition> {
|
|
41
|
-
contract: TContract;
|
|
42
|
-
handlers: WorkerInferConsumerHandlers<TContract>;
|
|
43
|
-
connection: ChannelModel;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Create a type-safe AMQP worker from a contract
|
|
47
|
-
* The worker will automatically connect and start consuming all messages
|
|
48
|
-
*/
|
|
49
|
-
declare function createWorker<TContract extends ContractDefinition>(options: CreateWorkerOptions<TContract>): Promise<AmqpWorker<TContract>>;
|
|
50
50
|
//#endregion
|
|
51
|
-
export {
|
|
51
|
+
export { type CreateWorkerOptions, TypedAmqpWorker };
|
|
52
52
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/worker.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/worker.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAAuD,UAAtC,mBAAsC,CAAA,kBAAA,kBAAA,CAAA,CAAA;EAC3C,QAAA,EAAA,SAAA;EAC4B,QAAA,EAA5B,2BAA4B,CAAA,SAAA,CAAA;EAA5B,UAAA,EACE,YADF;;;AAOZ;;AAcwC,cAd3B,eAc2B,CAAA,kBAdO,kBAcP,CAAA,CAAA;EACP,iBAAA,QAAA;EAApB,iBAAA,QAAA;EACgB,iBAAA,UAAA;EAAhB,QAAA,OAAA;EAAR,QAAA,YAAA;EAUY,QAAA,WAAA,CAAA;EAAO;;;;kCAZgB,6BAC3B,oBAAoB,aAC5B,QAAQ,gBAAgB;;;;WAUZ"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
1
|
import { ChannelModel } from "amqplib";
|
|
2
|
-
import { ContractDefinition,
|
|
2
|
+
import { ContractDefinition, WorkerInferConsumerHandlers } from "@amqp-contract/contract";
|
|
3
3
|
|
|
4
4
|
//#region src/worker.d.ts
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Options for creating a worker
|
|
8
|
+
*/
|
|
9
|
+
interface CreateWorkerOptions<TContract extends ContractDefinition> {
|
|
10
|
+
contract: TContract;
|
|
11
|
+
handlers: WorkerInferConsumerHandlers<TContract>;
|
|
12
|
+
connection: ChannelModel;
|
|
13
|
+
}
|
|
6
14
|
/**
|
|
7
15
|
* Type-safe AMQP worker for consuming messages
|
|
8
16
|
*/
|
|
9
|
-
declare class
|
|
17
|
+
declare class TypedAmqpWorker<TContract extends ContractDefinition> {
|
|
10
18
|
private readonly contract;
|
|
11
19
|
private readonly handlers;
|
|
20
|
+
private readonly connection;
|
|
12
21
|
private channel;
|
|
13
|
-
private connection;
|
|
14
22
|
private consumerTags;
|
|
15
|
-
constructor(
|
|
23
|
+
private constructor();
|
|
16
24
|
/**
|
|
17
|
-
*
|
|
25
|
+
* Create a type-safe AMQP worker from a contract
|
|
26
|
+
* The worker will automatically connect and start consuming all messages
|
|
18
27
|
*/
|
|
19
|
-
|
|
28
|
+
static create<TContract extends ContractDefinition>(options: CreateWorkerOptions<TContract>): Promise<TypedAmqpWorker<TContract>>;
|
|
20
29
|
/**
|
|
21
|
-
*
|
|
30
|
+
* Close the connection
|
|
31
|
+
*/
|
|
32
|
+
close(): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Connect to AMQP broker
|
|
22
35
|
*/
|
|
23
|
-
|
|
36
|
+
private init;
|
|
24
37
|
/**
|
|
25
38
|
* Start consuming messages for all consumers
|
|
26
39
|
*/
|
|
27
|
-
consumeAll
|
|
40
|
+
private consumeAll;
|
|
28
41
|
/**
|
|
29
|
-
*
|
|
42
|
+
* Start consuming messages for a specific consumer
|
|
30
43
|
*/
|
|
31
|
-
|
|
44
|
+
private consume;
|
|
32
45
|
/**
|
|
33
|
-
*
|
|
46
|
+
* Stop consuming messages
|
|
34
47
|
*/
|
|
35
|
-
|
|
48
|
+
private stopConsuming;
|
|
36
49
|
}
|
|
37
|
-
/**
|
|
38
|
-
* Options for creating a worker
|
|
39
|
-
*/
|
|
40
|
-
interface CreateWorkerOptions<TContract extends ContractDefinition> {
|
|
41
|
-
contract: TContract;
|
|
42
|
-
handlers: WorkerInferConsumerHandlers<TContract>;
|
|
43
|
-
connection: ChannelModel;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Create a type-safe AMQP worker from a contract
|
|
47
|
-
* The worker will automatically connect and start consuming all messages
|
|
48
|
-
*/
|
|
49
|
-
declare function createWorker<TContract extends ContractDefinition>(options: CreateWorkerOptions<TContract>): Promise<AmqpWorker<TContract>>;
|
|
50
50
|
//#endregion
|
|
51
|
-
export {
|
|
51
|
+
export { type CreateWorkerOptions, TypedAmqpWorker };
|
|
52
52
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/worker.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/worker.ts"],"sourcesContent":[],"mappings":";;;;;;;AAUA;AAAuD,UAAtC,mBAAsC,CAAA,kBAAA,kBAAA,CAAA,CAAA;EAC3C,QAAA,EAAA,SAAA;EAC4B,QAAA,EAA5B,2BAA4B,CAAA,SAAA,CAAA;EAA5B,UAAA,EACE,YADF;;;AAOZ;;AAcwC,cAd3B,eAc2B,CAAA,kBAdO,kBAcP,CAAA,CAAA;EACP,iBAAA,QAAA;EAApB,iBAAA,QAAA;EACgB,iBAAA,UAAA;EAAhB,QAAA,OAAA;EAAR,QAAA,YAAA;EAUY,QAAA,WAAA,CAAA;EAAO;;;;kCAZgB,6BAC3B,oBAAoB,aAC5B,QAAQ,gBAAgB;;;;WAUZ"}
|
package/dist/index.mjs
CHANGED
|
@@ -2,39 +2,64 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Type-safe AMQP worker for consuming messages
|
|
4
4
|
*/
|
|
5
|
-
var
|
|
5
|
+
var TypedAmqpWorker = class TypedAmqpWorker {
|
|
6
6
|
channel = null;
|
|
7
|
-
connection = null;
|
|
8
7
|
consumerTags = [];
|
|
9
|
-
constructor(contract, handlers) {
|
|
8
|
+
constructor(contract, handlers, connection) {
|
|
10
9
|
this.contract = contract;
|
|
11
10
|
this.handlers = handlers;
|
|
11
|
+
this.connection = connection;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create a type-safe AMQP worker from a contract
|
|
15
|
+
* The worker will automatically connect and start consuming all messages
|
|
16
|
+
*/
|
|
17
|
+
static async create(options) {
|
|
18
|
+
const worker = new TypedAmqpWorker(options.contract, options.handlers, options.connection);
|
|
19
|
+
await worker.init();
|
|
20
|
+
await worker.consumeAll();
|
|
21
|
+
return worker;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Close the connection
|
|
25
|
+
*/
|
|
26
|
+
async close() {
|
|
27
|
+
await this.stopConsuming();
|
|
28
|
+
if (this.channel) await this.channel.close();
|
|
29
|
+
await this.connection.close();
|
|
12
30
|
}
|
|
13
31
|
/**
|
|
14
32
|
* Connect to AMQP broker
|
|
15
33
|
*/
|
|
16
|
-
async
|
|
17
|
-
this.
|
|
18
|
-
this.
|
|
19
|
-
if (this.contract.exchanges && this.channel) for (const exchange of Object.values(this.contract.exchanges)) await this.channel.assertExchange(exchange.name, exchange.type, {
|
|
34
|
+
async init() {
|
|
35
|
+
this.channel = await this.connection.createChannel();
|
|
36
|
+
if (this.contract.exchanges) for (const exchange of Object.values(this.contract.exchanges)) await this.channel.assertExchange(exchange.name, exchange.type, {
|
|
20
37
|
durable: exchange.durable,
|
|
21
38
|
autoDelete: exchange.autoDelete,
|
|
22
39
|
internal: exchange.internal,
|
|
23
40
|
arguments: exchange.arguments
|
|
24
41
|
});
|
|
25
|
-
if (this.contract.queues
|
|
42
|
+
if (this.contract.queues) for (const queue of Object.values(this.contract.queues)) await this.channel.assertQueue(queue.name, {
|
|
26
43
|
durable: queue.durable,
|
|
27
44
|
exclusive: queue.exclusive,
|
|
28
45
|
autoDelete: queue.autoDelete,
|
|
29
46
|
arguments: queue.arguments
|
|
30
47
|
});
|
|
31
|
-
if (this.contract.bindings
|
|
48
|
+
if (this.contract.bindings) for (const binding of Object.values(this.contract.bindings)) await this.channel.bindQueue(binding.queue, binding.exchange, binding.routingKey ?? "", binding.arguments);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Start consuming messages for all consumers
|
|
52
|
+
*/
|
|
53
|
+
async consumeAll() {
|
|
54
|
+
if (!this.contract.consumers) throw new Error("No consumers defined in contract");
|
|
55
|
+
const consumerNames = Object.keys(this.contract.consumers);
|
|
56
|
+
for (const consumerName of consumerNames) await this.consume(consumerName);
|
|
32
57
|
}
|
|
33
58
|
/**
|
|
34
59
|
* Start consuming messages for a specific consumer
|
|
35
60
|
*/
|
|
36
61
|
async consume(consumerName) {
|
|
37
|
-
if (!this.channel) throw new Error("Worker not
|
|
62
|
+
if (!this.channel) throw new Error("Worker not initialized. Use TypedAmqpWorker.create() to obtain an initialized worker instance.");
|
|
38
63
|
const consumers = this.contract.consumers;
|
|
39
64
|
if (!consumers) throw new Error("No consumers defined in contract");
|
|
40
65
|
const consumer = consumers[consumerName];
|
|
@@ -63,14 +88,6 @@ var AmqpWorker = class {
|
|
|
63
88
|
this.consumerTags.push(result.consumerTag);
|
|
64
89
|
}
|
|
65
90
|
/**
|
|
66
|
-
* Start consuming messages for all consumers
|
|
67
|
-
*/
|
|
68
|
-
async consumeAll() {
|
|
69
|
-
if (!this.contract.consumers) throw new Error("No consumers defined in contract");
|
|
70
|
-
const consumerNames = Object.keys(this.contract.consumers);
|
|
71
|
-
for (const consumerName of consumerNames) await this.consume(consumerName);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
91
|
* Stop consuming messages
|
|
75
92
|
*/
|
|
76
93
|
async stopConsuming() {
|
|
@@ -78,32 +95,8 @@ var AmqpWorker = class {
|
|
|
78
95
|
for (const tag of this.consumerTags) await this.channel.cancel(tag);
|
|
79
96
|
this.consumerTags = [];
|
|
80
97
|
}
|
|
81
|
-
/**
|
|
82
|
-
* Close the connection
|
|
83
|
-
*/
|
|
84
|
-
async close() {
|
|
85
|
-
await this.stopConsuming();
|
|
86
|
-
if (this.channel) {
|
|
87
|
-
await this.channel.close();
|
|
88
|
-
this.channel = null;
|
|
89
|
-
}
|
|
90
|
-
if (this.connection) {
|
|
91
|
-
await this.connection.close();
|
|
92
|
-
this.connection = null;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
98
|
};
|
|
96
|
-
/**
|
|
97
|
-
* Create a type-safe AMQP worker from a contract
|
|
98
|
-
* The worker will automatically connect and start consuming all messages
|
|
99
|
-
*/
|
|
100
|
-
async function createWorker(options) {
|
|
101
|
-
const worker = new AmqpWorker(options.contract, options.handlers);
|
|
102
|
-
await worker.connect(options.connection);
|
|
103
|
-
await worker.consumeAll();
|
|
104
|
-
return worker;
|
|
105
|
-
}
|
|
106
99
|
|
|
107
100
|
//#endregion
|
|
108
|
-
export {
|
|
101
|
+
export { TypedAmqpWorker };
|
|
109
102
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["contract: TContract","handlers: WorkerInferConsumerHandlers<TContract>"],"sources":["../src/worker.ts"],"sourcesContent":["import type { Channel, ChannelModel, ConsumeMessage } from \"amqplib\";\nimport type {\n ContractDefinition,\n InferConsumerNames,\n WorkerInferConsumerHandlers,\n} from \"@amqp-contract/contract\";\n\n/**\n * Type-safe AMQP worker for consuming messages\n */\nexport class
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["contract: TContract","handlers: WorkerInferConsumerHandlers<TContract>","connection: ChannelModel"],"sources":["../src/worker.ts"],"sourcesContent":["import type { Channel, ChannelModel, ConsumeMessage } from \"amqplib\";\nimport type {\n ContractDefinition,\n InferConsumerNames,\n WorkerInferConsumerHandlers,\n} from \"@amqp-contract/contract\";\n\n/**\n * Options for creating a worker\n */\nexport interface CreateWorkerOptions<TContract extends ContractDefinition> {\n contract: TContract;\n handlers: WorkerInferConsumerHandlers<TContract>;\n connection: ChannelModel;\n}\n\n/**\n * Type-safe AMQP worker for consuming messages\n */\nexport class TypedAmqpWorker<TContract extends ContractDefinition> {\n private channel: Channel | null = null;\n private consumerTags: string[] = [];\n\n private constructor(\n private readonly contract: TContract,\n private readonly handlers: WorkerInferConsumerHandlers<TContract>,\n private readonly connection: ChannelModel,\n ) {}\n\n /**\n * Create a type-safe AMQP worker from a contract\n * The worker will automatically connect and start consuming all messages\n */\n static async create<TContract extends ContractDefinition>(\n options: CreateWorkerOptions<TContract>,\n ): Promise<TypedAmqpWorker<TContract>> {\n const worker = new TypedAmqpWorker(options.contract, options.handlers, options.connection);\n await worker.init();\n await worker.consumeAll();\n return worker;\n }\n\n /**\n * Close the connection\n */\n async close(): Promise<void> {\n await this.stopConsuming();\n\n if (this.channel) {\n await this.channel.close();\n }\n\n await this.connection.close();\n }\n\n /**\n * Connect to AMQP broker\n */\n private async init(): Promise<void> {\n this.channel = await this.connection.createChannel();\n\n // Setup exchanges\n if (this.contract.exchanges) {\n for (const exchange of Object.values(this.contract.exchanges)) {\n await this.channel.assertExchange(exchange.name, exchange.type, {\n durable: exchange.durable,\n autoDelete: exchange.autoDelete,\n internal: exchange.internal,\n arguments: exchange.arguments,\n });\n }\n }\n\n // Setup queues\n if (this.contract.queues) {\n for (const queue of Object.values(this.contract.queues)) {\n await this.channel.assertQueue(queue.name, {\n durable: queue.durable,\n exclusive: queue.exclusive,\n autoDelete: queue.autoDelete,\n arguments: queue.arguments,\n });\n }\n }\n\n // Setup bindings\n if (this.contract.bindings) {\n for (const binding of Object.values(this.contract.bindings)) {\n await this.channel.bindQueue(\n binding.queue,\n binding.exchange,\n binding.routingKey ?? \"\",\n binding.arguments,\n );\n }\n }\n }\n\n /**\n * Start consuming messages for all consumers\n */\n private async consumeAll(): Promise<void> {\n if (!this.contract.consumers) {\n throw new Error(\"No consumers defined in contract\");\n }\n\n const consumerNames = Object.keys(this.contract.consumers) as InferConsumerNames<TContract>[];\n\n for (const consumerName of consumerNames) {\n await this.consume(consumerName);\n }\n }\n\n /**\n * Start consuming messages for a specific consumer\n */\n private async consume<TName extends InferConsumerNames<TContract>>(\n consumerName: TName,\n ): Promise<void> {\n if (!this.channel) {\n throw new Error(\n \"Worker not initialized. Use TypedAmqpWorker.create() to obtain an initialized worker instance.\",\n );\n }\n\n const consumers = this.contract.consumers as Record<string, unknown>;\n if (!consumers) {\n throw new Error(\"No consumers defined in contract\");\n }\n\n const consumer = consumers[consumerName as string];\n if (!consumer || typeof consumer !== \"object\") {\n throw new Error(`Consumer \"${String(consumerName)}\" not found in contract`);\n }\n\n const consumerDef = consumer as {\n queue: string;\n message: { \"~standard\": { validate: (value: unknown) => unknown } };\n prefetch?: number;\n noAck?: boolean;\n };\n\n const handler = this.handlers[consumerName];\n if (!handler) {\n throw new Error(`Handler for \"${String(consumerName)}\" not provided`);\n }\n\n // Set prefetch if specified\n if (consumerDef.prefetch !== undefined) {\n await this.channel.prefetch(consumerDef.prefetch);\n }\n\n // Start consuming\n const result = await this.channel.consume(\n consumerDef.queue,\n async (msg: ConsumeMessage | null) => {\n if (!msg) {\n return;\n }\n\n try {\n // Parse message\n const content = JSON.parse(msg.content.toString());\n\n // Validate message using schema\n const validation = consumerDef.message[\"~standard\"].validate(content);\n if (\n typeof validation === \"object\" &&\n validation !== null &&\n \"issues\" in validation &&\n validation.issues\n ) {\n console.error(\"Message validation failed:\", validation.issues);\n // Reject message with no requeue\n this.channel?.nack(msg, false, false);\n return;\n }\n\n const validatedMessage =\n typeof validation === \"object\" && validation !== null && \"value\" in validation\n ? validation.value\n : content;\n\n // Call handler\n await handler(validatedMessage);\n\n // Acknowledge message if not in noAck mode\n if (!consumerDef.noAck) {\n this.channel?.ack(msg);\n }\n } catch (error) {\n console.error(\"Error processing message:\", error);\n // Reject message and requeue\n this.channel?.nack(msg, false, true);\n }\n },\n {\n noAck: consumerDef.noAck ?? false,\n },\n );\n\n this.consumerTags.push(result.consumerTag);\n }\n\n /**\n * Stop consuming messages\n */\n private async stopConsuming(): Promise<void> {\n if (!this.channel) {\n return;\n }\n\n for (const tag of this.consumerTags) {\n await this.channel.cancel(tag);\n }\n\n this.consumerTags = [];\n }\n}\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amqp-contract/worker",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Worker utilities for consuming messages using amqp-contract",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"amqp",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"dist"
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@amqp-contract/contract": "0.0.
|
|
44
|
+
"@amqp-contract/contract": "0.0.5"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/amqplib": "0.10.8",
|
|
@@ -52,8 +52,8 @@
|
|
|
52
52
|
"typescript": "5.9.3",
|
|
53
53
|
"vitest": "4.0.16",
|
|
54
54
|
"zod": "4.2.1",
|
|
55
|
-
"@amqp-contract/client": "0.0.
|
|
56
|
-
"@amqp-contract/testing": "0.0.
|
|
55
|
+
"@amqp-contract/client": "0.0.5",
|
|
56
|
+
"@amqp-contract/testing": "0.0.5",
|
|
57
57
|
"@amqp-contract/tsconfig": "0.0.0"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|