@onify/fake-amqplib 3.0.0 → 3.2.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 +12 -13
- package/index.d.ts +5 -9
- package/index.js +78 -58
- package/main.cjs +78 -58
- package/package.json +13 -9
package/README.md
CHANGED
|
@@ -17,6 +17,7 @@ Mocked version of https://www.npmjs.com/package/amqplib.
|
|
|
17
17
|
RabbitMQ behaviour differs between versions. To specify your version of RabbitMQ you can call `setVersion(minorVersionFloatOrString)`. Default version is 3.5.
|
|
18
18
|
|
|
19
19
|
Example:
|
|
20
|
+
|
|
20
21
|
```js
|
|
21
22
|
var fakeAmqp = require('@onify/fake-amqplib');
|
|
22
23
|
|
|
@@ -30,7 +31,7 @@ var fakeAmqp = require('@onify/fake-amqplib');
|
|
|
30
31
|
|
|
31
32
|
fakeAmqp.setVersion('3.7');
|
|
32
33
|
const conn37 = await fakeAmqp.connect('amqp://rabbit3-7');
|
|
33
|
-
})()
|
|
34
|
+
})();
|
|
34
35
|
```
|
|
35
36
|
|
|
36
37
|
## Mocking amqplib
|
|
@@ -41,7 +42,7 @@ You might want to override `amqplib` with `@onify/fake-amqplib` in tests. This c
|
|
|
41
42
|
|
|
42
43
|
Example on how to mock amqplib when working with commonjs.
|
|
43
44
|
|
|
44
|
-
```
|
|
45
|
+
```js
|
|
45
46
|
const amqplib = require('amqplib');
|
|
46
47
|
const fakeAmqp = require('@onify/fake-amqplib');
|
|
47
48
|
|
|
@@ -50,7 +51,7 @@ amqplib.connect = fakeAmqp.connect;
|
|
|
50
51
|
|
|
51
52
|
or:
|
|
52
53
|
|
|
53
|
-
```
|
|
54
|
+
```js
|
|
54
55
|
const mock = require('mock-require');
|
|
55
56
|
const fakeAmqp = require('@onify/fake-amqplib');
|
|
56
57
|
|
|
@@ -59,7 +60,7 @@ mock('amqplib/callback_api', fakeAmqp);
|
|
|
59
60
|
|
|
60
61
|
or just mock the entire amqplib with:
|
|
61
62
|
|
|
62
|
-
```
|
|
63
|
+
```js
|
|
63
64
|
const mock = require('mock-require');
|
|
64
65
|
const fakeAmqp = require('@onify/fake-amqplib');
|
|
65
66
|
|
|
@@ -75,36 +76,34 @@ Example on how to mock amqplib import when working with modules.
|
|
|
75
76
|
Both amqplib and fake-amqplib have to be quibbled if reset mock is used during testing.
|
|
76
77
|
|
|
77
78
|
_test/setup.js_
|
|
78
|
-
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
79
81
|
import * as fakeAmqpLib from '@onify/fake-amqplib';
|
|
80
82
|
import { connect as fakeConnect } from '@onify/fake-amqplib';
|
|
81
83
|
import quibble from 'quibble';
|
|
82
84
|
|
|
83
85
|
(async () => {
|
|
84
86
|
await quibble.esm('amqplib', { connect: fakeConnect });
|
|
85
|
-
await quibble.esm('amqplib/callback_api', { connect: fakeConnect });
|
|
86
87
|
await quibble.esm('@onify/fake-amqplib', { ...fakeAmqpLib });
|
|
87
88
|
})();
|
|
88
89
|
```
|
|
89
90
|
|
|
90
91
|
_.mocharc.json_ (true for node version < 20)
|
|
92
|
+
|
|
91
93
|
```json
|
|
92
94
|
{
|
|
93
95
|
"recursive": true,
|
|
94
96
|
"require": ["test/setup.js"],
|
|
95
|
-
"node-option": [
|
|
96
|
-
"experimental-specifier-resolution=node",
|
|
97
|
-
"no-warnings",
|
|
98
|
-
"loader=quibble"
|
|
99
|
-
]
|
|
97
|
+
"node-option": ["experimental-specifier-resolution=node", "no-warnings", "loader=quibble"]
|
|
100
98
|
}
|
|
101
99
|
```
|
|
102
100
|
|
|
103
101
|
_test/amqplib-connection-test.js_
|
|
104
|
-
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
105
104
|
import assert from 'node:assert';
|
|
106
105
|
import { connect } from 'amqplib';
|
|
107
|
-
import { connect as connectCb } from 'amqplib
|
|
106
|
+
import { connect as connectCb } from 'amqplib';
|
|
108
107
|
|
|
109
108
|
import { resetMock } from '@onify/fake-amqplib';
|
|
110
109
|
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/// <reference types="amqplib" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
|
|
4
|
-
import { Options, Connection, Channel } from
|
|
5
|
-
import { EventEmitter } from "events";
|
|
4
|
+
import { Options, Connection, Channel } from 'amqplib';
|
|
6
5
|
import { Broker } from 'smqp';
|
|
7
6
|
|
|
8
7
|
export interface FakeAmqplibChannel extends Channel {
|
|
@@ -10,7 +9,7 @@ export interface FakeAmqplibChannel extends Channel {
|
|
|
10
9
|
_channelName: string;
|
|
11
10
|
_broker: Broker;
|
|
12
11
|
_version: number;
|
|
13
|
-
new(broker: Broker, connection: FakeAmqplibConnection): FakeAmqplibChannel;
|
|
12
|
+
new (broker: Broker, connection: FakeAmqplibConnection): FakeAmqplibChannel;
|
|
14
13
|
get _closed(): boolean;
|
|
15
14
|
}
|
|
16
15
|
|
|
@@ -21,7 +20,7 @@ export interface FakeAmqplibConnection extends Connection {
|
|
|
21
20
|
_id: string;
|
|
22
21
|
_broker: Broker;
|
|
23
22
|
_version: number;
|
|
24
|
-
new(broker: Broker, version: number, amqpUrl: string, options?: any): FakeAmqplibConnection;
|
|
23
|
+
new (broker: Broker, version: number, amqpUrl: string, options?: any): FakeAmqplibConnection;
|
|
25
24
|
get _closed(): boolean;
|
|
26
25
|
}
|
|
27
26
|
|
|
@@ -36,14 +35,11 @@ interface SocketOptions {
|
|
|
36
35
|
[x: string]: any;
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
type connectCallback = (
|
|
40
|
-
err: Error,
|
|
41
|
-
connection: FakeAmqplibConnection
|
|
42
|
-
) => void;
|
|
38
|
+
type connectCallback = (err: Error, connection: FakeAmqplibConnection) => void;
|
|
43
39
|
|
|
44
40
|
export class FakeAmqplib {
|
|
45
41
|
connections: FakeAmqplibConnection[];
|
|
46
|
-
constructor(version?: number)
|
|
42
|
+
constructor(version?: number);
|
|
47
43
|
connect(url: string | Options.Connect, socketOptions?: SocketOptions): Promise<FakeAmqplibConnection>;
|
|
48
44
|
connect(url: string | Options.Connect, socketOptions: SocketOptions, callback: connectCallback): void;
|
|
49
45
|
connect(url: string | Options.Connect, callback: (err: Error, connection: FakeAmqplibConnection) => void): void;
|
package/index.js
CHANGED
|
@@ -36,13 +36,23 @@ class FakeAmqpError extends Error {
|
|
|
36
36
|
|
|
37
37
|
class FakeAmqpNotFoundError extends FakeAmqpError {
|
|
38
38
|
constructor(type, name, vhost, killConnection = false) {
|
|
39
|
-
super(
|
|
39
|
+
super(
|
|
40
|
+
`Channel closed by server: 404 (NOT-FOUND) with message "NOT_FOUND - no ${type} '${name}' in vhost '${vhost || '/'}'`,
|
|
41
|
+
404,
|
|
42
|
+
true,
|
|
43
|
+
killConnection,
|
|
44
|
+
);
|
|
40
45
|
}
|
|
41
46
|
}
|
|
42
47
|
|
|
43
48
|
class FakeAmqpUnknownDeliveryTag extends FakeAmqpError {
|
|
44
49
|
constructor(deliveryTag) {
|
|
45
|
-
super(
|
|
50
|
+
super(
|
|
51
|
+
`Channel closed by server: 406 (PRECONDITION-FAILED) with message "PRECONDITION_FAILED - unknown delivery tag ${deliveryTag}`,
|
|
52
|
+
406,
|
|
53
|
+
true,
|
|
54
|
+
false,
|
|
55
|
+
);
|
|
46
56
|
}
|
|
47
57
|
get _emit() {
|
|
48
58
|
return true;
|
|
@@ -64,7 +74,7 @@ export class FakeAmqplibChannel extends EventEmitter {
|
|
|
64
74
|
this[kPrefetch] = 10000;
|
|
65
75
|
this[kChannelPrefetch] = Infinity;
|
|
66
76
|
this[kClosed] = false;
|
|
67
|
-
const channelName = this._channelName = `channel-${generateId()}
|
|
77
|
+
const channelName = (this._channelName = `channel-${generateId()}`);
|
|
68
78
|
this._version = connection._version;
|
|
69
79
|
this._broker = broker;
|
|
70
80
|
|
|
@@ -104,12 +114,12 @@ export class FakeAmqplibChannel extends EventEmitter {
|
|
|
104
114
|
}
|
|
105
115
|
bindExchange(destination, source, ...args) {
|
|
106
116
|
const broker = this._broker;
|
|
107
|
-
return Promise.all([
|
|
117
|
+
return Promise.all([this.checkExchange(source), this.checkExchange(destination)]).then(() => {
|
|
108
118
|
return this._callBroker(broker.bindExchange, source, destination, ...args);
|
|
109
119
|
});
|
|
110
120
|
}
|
|
111
121
|
bindQueue(queue, source, ...args) {
|
|
112
|
-
return Promise.all([
|
|
122
|
+
return Promise.all([this.checkQueue(queue), this.checkExchange(source)]).then(() => {
|
|
113
123
|
return this._callBroker(this._broker.bindQueue, queue, source, ...args);
|
|
114
124
|
});
|
|
115
125
|
}
|
|
@@ -176,15 +186,17 @@ export class FakeAmqplibChannel extends EventEmitter {
|
|
|
176
186
|
if (!Buffer.isBuffer(content)) throw new TypeError('content is not a buffer');
|
|
177
187
|
if (exchange === '') return this.sendToQueue(routingKey, content, options, callback);
|
|
178
188
|
|
|
179
|
-
const args = [
|
|
189
|
+
const args = [this._broker.publish, exchange, routingKey, content];
|
|
180
190
|
|
|
181
191
|
args.push(options, callback);
|
|
182
192
|
|
|
183
|
-
this.checkExchange(exchange)
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
193
|
+
this.checkExchange(exchange)
|
|
194
|
+
.then(() => {
|
|
195
|
+
return this._callBroker(...args);
|
|
196
|
+
})
|
|
197
|
+
.catch((err) => {
|
|
198
|
+
this.emit('error', err);
|
|
199
|
+
});
|
|
188
200
|
|
|
189
201
|
return true;
|
|
190
202
|
}
|
|
@@ -201,15 +213,17 @@ export class FakeAmqplibChannel extends EventEmitter {
|
|
|
201
213
|
sendToQueue(queue, content, options, callback) {
|
|
202
214
|
if (!Buffer.isBuffer(content)) throw new TypeError('content is not a buffer');
|
|
203
215
|
|
|
204
|
-
const args = [
|
|
216
|
+
const args = [this._broker.sendToQueue, queue, content];
|
|
205
217
|
|
|
206
218
|
args.push(options, callback);
|
|
207
219
|
|
|
208
|
-
this.checkQueue(queue)
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
220
|
+
this.checkQueue(queue)
|
|
221
|
+
.then(() => {
|
|
222
|
+
return this._callBroker(...args);
|
|
223
|
+
})
|
|
224
|
+
.catch((err) => {
|
|
225
|
+
this.emit('error', err);
|
|
226
|
+
});
|
|
213
227
|
|
|
214
228
|
return true;
|
|
215
229
|
}
|
|
@@ -266,7 +280,12 @@ export class FakeAmqplibChannel extends EventEmitter {
|
|
|
266
280
|
}
|
|
267
281
|
|
|
268
282
|
if (q.exclusive || (q.options.exclusive && q.options._connectionId !== connId)) {
|
|
269
|
-
throw new FakeAmqpError(
|
|
283
|
+
throw new FakeAmqpError(
|
|
284
|
+
`Channel closed by server: 403 (ACCESS-REFUSED) with message "ACCESS_REFUSED - queue '${queue}' in vhost '${connUrl.pathname}' in exclusive use"`,
|
|
285
|
+
403,
|
|
286
|
+
true,
|
|
287
|
+
true,
|
|
288
|
+
);
|
|
270
289
|
}
|
|
271
290
|
|
|
272
291
|
const consumer = this.consume(queue, onMessage && handler, {
|
|
@@ -425,7 +444,7 @@ export class FakeAmqplibChannel extends EventEmitter {
|
|
|
425
444
|
}
|
|
426
445
|
}
|
|
427
446
|
_callBroker(fn, ...args) {
|
|
428
|
-
let [
|
|
447
|
+
let [poppedCb] = args.slice(-1);
|
|
429
448
|
if (typeof poppedCb === 'function') args.splice(-1);
|
|
430
449
|
else poppedCb = null;
|
|
431
450
|
|
|
@@ -494,30 +513,34 @@ export class FakeAmqplibConfirmChannel extends FakeAmqplibChannel {
|
|
|
494
513
|
if (!Buffer.isBuffer(content)) throw new TypeError('content is not a buffer');
|
|
495
514
|
if (exchange === '') return this.sendToQueue(routingKey, content, options, callback);
|
|
496
515
|
|
|
497
|
-
const args = [
|
|
516
|
+
const args = [this._broker.publish, exchange, routingKey, content];
|
|
498
517
|
|
|
499
518
|
args.push(...addConfirmCallback(this._broker, options, callback));
|
|
500
519
|
|
|
501
|
-
this.checkExchange(exchange)
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
520
|
+
this.checkExchange(exchange)
|
|
521
|
+
.then(() => {
|
|
522
|
+
return this._callBroker(...args);
|
|
523
|
+
})
|
|
524
|
+
.catch((err) => {
|
|
525
|
+
this.emit('error', err);
|
|
526
|
+
});
|
|
506
527
|
|
|
507
528
|
return true;
|
|
508
529
|
}
|
|
509
530
|
sendToQueue(queue, content, options, callback) {
|
|
510
531
|
if (!Buffer.isBuffer(content)) throw new TypeError('content is not a buffer');
|
|
511
532
|
|
|
512
|
-
const args = [
|
|
533
|
+
const args = [this._broker.sendToQueue, queue, content];
|
|
513
534
|
|
|
514
535
|
args.push(...addConfirmCallback(this._broker, options, callback));
|
|
515
536
|
|
|
516
|
-
this.checkQueue(queue)
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
537
|
+
this.checkQueue(queue)
|
|
538
|
+
.then(() => {
|
|
539
|
+
return this._callBroker(...args);
|
|
540
|
+
})
|
|
541
|
+
.catch((err) => {
|
|
542
|
+
this.emit('error', err);
|
|
543
|
+
});
|
|
521
544
|
|
|
522
545
|
return true;
|
|
523
546
|
}
|
|
@@ -644,27 +667,21 @@ function normalizeAmqpUrl(url) {
|
|
|
644
667
|
if (typeof url === 'string') url = new URL(url);
|
|
645
668
|
|
|
646
669
|
if (!(url instanceof URL)) {
|
|
647
|
-
const {
|
|
648
|
-
protocol = 'amqp',
|
|
649
|
-
hostname = 'localhost',
|
|
650
|
-
port = 5672,
|
|
651
|
-
vhost = '/',
|
|
652
|
-
username,
|
|
653
|
-
password,
|
|
654
|
-
...rest
|
|
655
|
-
} = url;
|
|
670
|
+
const { protocol = 'amqp', hostname = 'localhost', port = 5672, vhost = '/', username, password, ...rest } = url;
|
|
656
671
|
let auth = username;
|
|
657
672
|
if (auth && password) {
|
|
658
673
|
auth += `:${password}`;
|
|
659
674
|
}
|
|
660
|
-
url = new URL(
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
675
|
+
url = new URL(
|
|
676
|
+
urlFormat({
|
|
677
|
+
protocol,
|
|
678
|
+
hostname,
|
|
679
|
+
port,
|
|
680
|
+
pathname: vhost,
|
|
681
|
+
slashes: true,
|
|
682
|
+
auth,
|
|
683
|
+
}),
|
|
684
|
+
);
|
|
668
685
|
|
|
669
686
|
for (const k in rest) {
|
|
670
687
|
switch (k) {
|
|
@@ -700,7 +717,7 @@ function addConfirmCallback(broker, options, callback) {
|
|
|
700
717
|
}
|
|
701
718
|
|
|
702
719
|
function confirmCallback() {
|
|
703
|
-
broker.off('message.*', consumerTag);
|
|
720
|
+
broker.off('message.*', { consumerTag });
|
|
704
721
|
switch (undelivered) {
|
|
705
722
|
case 'message.nack':
|
|
706
723
|
return callback(new Error('message nacked'));
|
|
@@ -711,20 +728,23 @@ function addConfirmCallback(broker, options, callback) {
|
|
|
711
728
|
}
|
|
712
729
|
}
|
|
713
730
|
|
|
714
|
-
return [
|
|
731
|
+
return [options, confirmCallback];
|
|
715
732
|
}
|
|
716
733
|
|
|
717
734
|
function allUpToDeliveryTag(q, deliveryTag, op, ...args) {
|
|
718
735
|
const brokerMessages = [];
|
|
719
736
|
|
|
720
|
-
const consumer = q.consume(
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
737
|
+
const consumer = q.consume(
|
|
738
|
+
(_, cmsg) => {
|
|
739
|
+
const msgDeliveryTag = cmsg.fields.deliveryTag;
|
|
740
|
+
if (msgDeliveryTag >= deliveryTag) {
|
|
741
|
+
return q.cancel(cmsg.fields.consumerTag);
|
|
742
|
+
}
|
|
743
|
+
brokerMessages.push(cmsg.content[kSmqp]);
|
|
744
|
+
cmsg[op](...args);
|
|
745
|
+
},
|
|
746
|
+
{ prefetch: Infinity },
|
|
747
|
+
);
|
|
728
748
|
|
|
729
749
|
consumer.cancel();
|
|
730
750
|
|
package/main.cjs
CHANGED
|
@@ -38,13 +38,23 @@ class FakeAmqpError extends Error {
|
|
|
38
38
|
|
|
39
39
|
class FakeAmqpNotFoundError extends FakeAmqpError {
|
|
40
40
|
constructor(type, name, vhost, killConnection = false) {
|
|
41
|
-
super(
|
|
41
|
+
super(
|
|
42
|
+
`Channel closed by server: 404 (NOT-FOUND) with message "NOT_FOUND - no ${type} '${name}' in vhost '${vhost || '/'}'`,
|
|
43
|
+
404,
|
|
44
|
+
true,
|
|
45
|
+
killConnection,
|
|
46
|
+
);
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
class FakeAmqpUnknownDeliveryTag extends FakeAmqpError {
|
|
46
51
|
constructor(deliveryTag) {
|
|
47
|
-
super(
|
|
52
|
+
super(
|
|
53
|
+
`Channel closed by server: 406 (PRECONDITION-FAILED) with message "PRECONDITION_FAILED - unknown delivery tag ${deliveryTag}`,
|
|
54
|
+
406,
|
|
55
|
+
true,
|
|
56
|
+
false,
|
|
57
|
+
);
|
|
48
58
|
}
|
|
49
59
|
get _emit() {
|
|
50
60
|
return true;
|
|
@@ -66,7 +76,7 @@ class FakeAmqplibChannel extends events.EventEmitter {
|
|
|
66
76
|
this[kPrefetch] = 10000;
|
|
67
77
|
this[kChannelPrefetch] = Infinity;
|
|
68
78
|
this[kClosed] = false;
|
|
69
|
-
const channelName = this._channelName = `channel-${generateId()}
|
|
79
|
+
const channelName = (this._channelName = `channel-${generateId()}`);
|
|
70
80
|
this._version = connection._version;
|
|
71
81
|
this._broker = broker;
|
|
72
82
|
|
|
@@ -106,12 +116,12 @@ class FakeAmqplibChannel extends events.EventEmitter {
|
|
|
106
116
|
}
|
|
107
117
|
bindExchange(destination, source, ...args) {
|
|
108
118
|
const broker = this._broker;
|
|
109
|
-
return Promise.all([
|
|
119
|
+
return Promise.all([this.checkExchange(source), this.checkExchange(destination)]).then(() => {
|
|
110
120
|
return this._callBroker(broker.bindExchange, source, destination, ...args);
|
|
111
121
|
});
|
|
112
122
|
}
|
|
113
123
|
bindQueue(queue, source, ...args) {
|
|
114
|
-
return Promise.all([
|
|
124
|
+
return Promise.all([this.checkQueue(queue), this.checkExchange(source)]).then(() => {
|
|
115
125
|
return this._callBroker(this._broker.bindQueue, queue, source, ...args);
|
|
116
126
|
});
|
|
117
127
|
}
|
|
@@ -178,15 +188,17 @@ class FakeAmqplibChannel extends events.EventEmitter {
|
|
|
178
188
|
if (!Buffer.isBuffer(content)) throw new TypeError('content is not a buffer');
|
|
179
189
|
if (exchange === '') return this.sendToQueue(routingKey, content, options, callback);
|
|
180
190
|
|
|
181
|
-
const args = [
|
|
191
|
+
const args = [this._broker.publish, exchange, routingKey, content];
|
|
182
192
|
|
|
183
193
|
args.push(options, callback);
|
|
184
194
|
|
|
185
|
-
this.checkExchange(exchange)
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
195
|
+
this.checkExchange(exchange)
|
|
196
|
+
.then(() => {
|
|
197
|
+
return this._callBroker(...args);
|
|
198
|
+
})
|
|
199
|
+
.catch((err) => {
|
|
200
|
+
this.emit('error', err);
|
|
201
|
+
});
|
|
190
202
|
|
|
191
203
|
return true;
|
|
192
204
|
}
|
|
@@ -203,15 +215,17 @@ class FakeAmqplibChannel extends events.EventEmitter {
|
|
|
203
215
|
sendToQueue(queue, content, options, callback) {
|
|
204
216
|
if (!Buffer.isBuffer(content)) throw new TypeError('content is not a buffer');
|
|
205
217
|
|
|
206
|
-
const args = [
|
|
218
|
+
const args = [this._broker.sendToQueue, queue, content];
|
|
207
219
|
|
|
208
220
|
args.push(options, callback);
|
|
209
221
|
|
|
210
|
-
this.checkQueue(queue)
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
222
|
+
this.checkQueue(queue)
|
|
223
|
+
.then(() => {
|
|
224
|
+
return this._callBroker(...args);
|
|
225
|
+
})
|
|
226
|
+
.catch((err) => {
|
|
227
|
+
this.emit('error', err);
|
|
228
|
+
});
|
|
215
229
|
|
|
216
230
|
return true;
|
|
217
231
|
}
|
|
@@ -268,7 +282,12 @@ class FakeAmqplibChannel extends events.EventEmitter {
|
|
|
268
282
|
}
|
|
269
283
|
|
|
270
284
|
if (q.exclusive || (q.options.exclusive && q.options._connectionId !== connId)) {
|
|
271
|
-
throw new FakeAmqpError(
|
|
285
|
+
throw new FakeAmqpError(
|
|
286
|
+
`Channel closed by server: 403 (ACCESS-REFUSED) with message "ACCESS_REFUSED - queue '${queue}' in vhost '${connUrl.pathname}' in exclusive use"`,
|
|
287
|
+
403,
|
|
288
|
+
true,
|
|
289
|
+
true,
|
|
290
|
+
);
|
|
272
291
|
}
|
|
273
292
|
|
|
274
293
|
const consumer = this.consume(queue, onMessage && handler, {
|
|
@@ -427,7 +446,7 @@ class FakeAmqplibChannel extends events.EventEmitter {
|
|
|
427
446
|
}
|
|
428
447
|
}
|
|
429
448
|
_callBroker(fn, ...args) {
|
|
430
|
-
let [
|
|
449
|
+
let [poppedCb] = args.slice(-1);
|
|
431
450
|
if (typeof poppedCb === 'function') args.splice(-1);
|
|
432
451
|
else poppedCb = null;
|
|
433
452
|
|
|
@@ -496,30 +515,34 @@ class FakeAmqplibConfirmChannel extends FakeAmqplibChannel {
|
|
|
496
515
|
if (!Buffer.isBuffer(content)) throw new TypeError('content is not a buffer');
|
|
497
516
|
if (exchange === '') return this.sendToQueue(routingKey, content, options, callback);
|
|
498
517
|
|
|
499
|
-
const args = [
|
|
518
|
+
const args = [this._broker.publish, exchange, routingKey, content];
|
|
500
519
|
|
|
501
520
|
args.push(...addConfirmCallback(this._broker, options, callback));
|
|
502
521
|
|
|
503
|
-
this.checkExchange(exchange)
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
522
|
+
this.checkExchange(exchange)
|
|
523
|
+
.then(() => {
|
|
524
|
+
return this._callBroker(...args);
|
|
525
|
+
})
|
|
526
|
+
.catch((err) => {
|
|
527
|
+
this.emit('error', err);
|
|
528
|
+
});
|
|
508
529
|
|
|
509
530
|
return true;
|
|
510
531
|
}
|
|
511
532
|
sendToQueue(queue, content, options, callback) {
|
|
512
533
|
if (!Buffer.isBuffer(content)) throw new TypeError('content is not a buffer');
|
|
513
534
|
|
|
514
|
-
const args = [
|
|
535
|
+
const args = [this._broker.sendToQueue, queue, content];
|
|
515
536
|
|
|
516
537
|
args.push(...addConfirmCallback(this._broker, options, callback));
|
|
517
538
|
|
|
518
|
-
this.checkQueue(queue)
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
539
|
+
this.checkQueue(queue)
|
|
540
|
+
.then(() => {
|
|
541
|
+
return this._callBroker(...args);
|
|
542
|
+
})
|
|
543
|
+
.catch((err) => {
|
|
544
|
+
this.emit('error', err);
|
|
545
|
+
});
|
|
523
546
|
|
|
524
547
|
return true;
|
|
525
548
|
}
|
|
@@ -646,27 +669,21 @@ function normalizeAmqpUrl(url$1) {
|
|
|
646
669
|
if (typeof url$1 === 'string') url$1 = new URL(url$1);
|
|
647
670
|
|
|
648
671
|
if (!(url$1 instanceof URL)) {
|
|
649
|
-
const {
|
|
650
|
-
protocol = 'amqp',
|
|
651
|
-
hostname = 'localhost',
|
|
652
|
-
port = 5672,
|
|
653
|
-
vhost = '/',
|
|
654
|
-
username,
|
|
655
|
-
password,
|
|
656
|
-
...rest
|
|
657
|
-
} = url$1;
|
|
672
|
+
const { protocol = 'amqp', hostname = 'localhost', port = 5672, vhost = '/', username, password, ...rest } = url$1;
|
|
658
673
|
let auth = username;
|
|
659
674
|
if (auth && password) {
|
|
660
675
|
auth += `:${password}`;
|
|
661
676
|
}
|
|
662
|
-
url$1 = new URL(
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
677
|
+
url$1 = new URL(
|
|
678
|
+
url.format({
|
|
679
|
+
protocol,
|
|
680
|
+
hostname,
|
|
681
|
+
port,
|
|
682
|
+
pathname: vhost,
|
|
683
|
+
slashes: true,
|
|
684
|
+
auth,
|
|
685
|
+
}),
|
|
686
|
+
);
|
|
670
687
|
|
|
671
688
|
for (const k in rest) {
|
|
672
689
|
switch (k) {
|
|
@@ -702,7 +719,7 @@ function addConfirmCallback(broker, options, callback) {
|
|
|
702
719
|
}
|
|
703
720
|
|
|
704
721
|
function confirmCallback() {
|
|
705
|
-
broker.off('message.*', consumerTag);
|
|
722
|
+
broker.off('message.*', { consumerTag });
|
|
706
723
|
switch (undelivered) {
|
|
707
724
|
case 'message.nack':
|
|
708
725
|
return callback(new Error('message nacked'));
|
|
@@ -713,20 +730,23 @@ function addConfirmCallback(broker, options, callback) {
|
|
|
713
730
|
}
|
|
714
731
|
}
|
|
715
732
|
|
|
716
|
-
return [
|
|
733
|
+
return [options, confirmCallback];
|
|
717
734
|
}
|
|
718
735
|
|
|
719
736
|
function allUpToDeliveryTag(q, deliveryTag, op, ...args) {
|
|
720
737
|
const brokerMessages = [];
|
|
721
738
|
|
|
722
|
-
const consumer = q.consume(
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
739
|
+
const consumer = q.consume(
|
|
740
|
+
(_, cmsg) => {
|
|
741
|
+
const msgDeliveryTag = cmsg.fields.deliveryTag;
|
|
742
|
+
if (msgDeliveryTag >= deliveryTag) {
|
|
743
|
+
return q.cancel(cmsg.fields.consumerTag);
|
|
744
|
+
}
|
|
745
|
+
brokerMessages.push(cmsg.content[kSmqp]);
|
|
746
|
+
cmsg[op](...args);
|
|
747
|
+
},
|
|
748
|
+
{ prefetch: Infinity },
|
|
749
|
+
);
|
|
730
750
|
|
|
731
751
|
consumer.cancel();
|
|
732
752
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onify/fake-amqplib",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Fake amqplib",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"scripts": {
|
|
15
15
|
"test": "mocha",
|
|
16
16
|
"posttest": "npm run lint && npm run dist",
|
|
17
|
-
"lint": "eslint . --cache",
|
|
17
|
+
"lint": "eslint . --cache && prettier . -c --cache",
|
|
18
18
|
"dist": "rollup -c",
|
|
19
19
|
"prepack": "npm run dist",
|
|
20
20
|
"cov:html": "c8 -r html -r text mocha",
|
|
@@ -33,17 +33,21 @@
|
|
|
33
33
|
},
|
|
34
34
|
"license": "MIT",
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"smqp": "^
|
|
36
|
+
"smqp": "^9.0.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@
|
|
40
|
-
"@rollup/plugin-commonjs": "^25.0.5",
|
|
39
|
+
"@rollup/plugin-commonjs": "^28.0.0",
|
|
41
40
|
"@types/amqplib": "^0.10.2",
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
41
|
+
"amqplib": "^0.10.4",
|
|
42
|
+
"c8": "^10.1.2",
|
|
43
|
+
"chai": "^5.1.0",
|
|
44
|
+
"eslint": "^9.0.0",
|
|
45
|
+
"globals": "^15.0.0",
|
|
45
46
|
"mocha": "^10.2.0",
|
|
46
|
-
"
|
|
47
|
+
"prettier": "^3.2.5",
|
|
48
|
+
"quibble": "^0.9.2",
|
|
49
|
+
"rollup": "^4.0.2",
|
|
50
|
+
"texample": "^0.0.6"
|
|
47
51
|
},
|
|
48
52
|
"keywords": [
|
|
49
53
|
"fake",
|