amqp-connection-manager 2.1.2 → 2.3.2

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.
@@ -1,14 +1,13 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true
4
+ value: true
5
5
  });
6
+ exports.default = void 0;
6
7
 
7
- var _events = require('events');
8
+ var _events = require("events");
8
9
 
9
- var _promiseBreaker = require('promise-breaker');
10
-
11
- var _promiseBreaker2 = _interopRequireDefault(_promiseBreaker);
10
+ var _promiseBreaker = _interopRequireDefault(require("promise-breaker"));
12
11
 
13
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
13
 
@@ -25,351 +24,389 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
25
24
  *
26
25
  */
27
26
  class ChannelWrapper extends _events.EventEmitter {
28
- /**
29
- * Adds a new 'setup handler'.
30
- *
31
- * `setup(channel, [cb])` is a function to call when a new underlying channel is created - handy for asserting
32
- * exchanges and queues exists, and whatnot. The `channel` object here is a ConfigChannel from amqplib.
33
- * The `setup` function should return a Promise (or optionally take a callback) - no messages will be sent until
34
- * this Promise resolves.
35
- *
36
- * If there is a connection, `setup()` will be run immediately, and the addSetup Promise/callback won't resolve
37
- * until `setup` is complete. Note that in this case, if the setup throws an error, no 'error' event will
38
- * be emitted, since you can just handle the error here (although the `setup` will still be added for future
39
- * reconnects, even if it throws an error.)
40
- *
41
- * Setup functions should, ideally, not throw errors, but if they do then the ChannelWrapper will emit an 'error'
42
- * event.
43
- *
44
- * @param {function} setup - setup function.
45
- * @param {function} [done] - callback.
46
- * @returns {void | Promise} - Resolves when complete.
47
- */
48
- addSetup(setup, done = null) {
49
- return _promiseBreaker2.default.addCallback(done, (this._settingUp || Promise.resolve()).then(() => {
50
- this._setups.push(setup);
51
- if (this._channel) {
52
- return _promiseBreaker2.default.call(setup, null, this._channel);
53
- } else {
54
- return undefined;
55
- }
56
- }));
27
+ /**
28
+ * Adds a new 'setup handler'.
29
+ *
30
+ * `setup(channel, [cb])` is a function to call when a new underlying channel is created - handy for asserting
31
+ * exchanges and queues exists, and whatnot. The `channel` object here is a ConfigChannel from amqplib.
32
+ * The `setup` function should return a Promise (or optionally take a callback) - no messages will be sent until
33
+ * this Promise resolves.
34
+ *
35
+ * If there is a connection, `setup()` will be run immediately, and the addSetup Promise/callback won't resolve
36
+ * until `setup` is complete. Note that in this case, if the setup throws an error, no 'error' event will
37
+ * be emitted, since you can just handle the error here (although the `setup` will still be added for future
38
+ * reconnects, even if it throws an error.)
39
+ *
40
+ * Setup functions should, ideally, not throw errors, but if they do then the ChannelWrapper will emit an 'error'
41
+ * event.
42
+ *
43
+ * @param {function} setup - setup function.
44
+ * @param {function} [done] - callback.
45
+ * @returns {void | Promise} - Resolves when complete.
46
+ */
47
+ addSetup(setup, done = null) {
48
+ return _promiseBreaker.default.addCallback(done, (this._settingUp || Promise.resolve()).then(() => {
49
+ this._setups.push(setup);
50
+
51
+ if (this._channel) {
52
+ return _promiseBreaker.default.call(setup, this, this._channel);
53
+ } else {
54
+ return undefined;
55
+ }
56
+ }));
57
+ }
58
+ /**
59
+ * Remove a setup function added with `addSetup`. If there is currently a
60
+ * connection, `teardown(channel, [cb])` will be run immediately, and the
61
+ * returned Promise will not resolve until it completes.
62
+ *
63
+ * @param {function} setup - the setup function to remove.
64
+ * @param {function} [teardown] - `function(channel, [cb])` to run to tear
65
+ * down the chanel.
66
+ * @param {function} [done] - Optional callback.
67
+ * @returns {void | Promise} - Resolves when complete.
68
+ */
69
+
70
+
71
+ removeSetup(setup, teardown = null, done = null) {
72
+ return _promiseBreaker.default.addCallback(done, () => {
73
+ this._setups = this._setups.filter(s => s !== setup);
74
+ return (this._settingUp || Promise.resolve()).then(() => this._channel ? _promiseBreaker.default.call(teardown, this, this._channel) : undefined);
75
+ });
76
+ }
77
+ /**
78
+ * Returns a Promise which resolves when this channel next connects.
79
+ * (Mainly here for unit testing...)
80
+ *
81
+ * @param {function} [done] - Optional callback.
82
+ * @returns {void | Promise} - Resolves when connected.
83
+ */
84
+
85
+
86
+ waitForConnect(done = null) {
87
+ return _promiseBreaker.default.addCallback(done, this._channel && !this._settingUp ? Promise.resolve() : new Promise(resolve => this.once('connect', resolve)));
88
+ }
89
+ /*
90
+ * Publish a message to the channel.
91
+ *
92
+ * This works just like amqplib's `publish()`, except if the channel is not
93
+ * connected, this will wait until the channel is connected. Returns a
94
+ * Promise which will only resolve when the message has been succesfully sent.
95
+ * The returned promise will be rejected if `close()` is called on this
96
+ * channel before it can be sent, if `options.json` is set and the message
97
+ * can't be encoded, or if the broker rejects the message for some reason.
98
+ *
99
+ */
100
+
101
+
102
+ publish(exchange, routingKey, content, options, done = null) {
103
+ return _promiseBreaker.default.addCallback(done, new Promise((resolve, reject) => {
104
+ this._messages.push({
105
+ type: 'publish',
106
+ exchange,
107
+ routingKey,
108
+ content,
109
+ options,
110
+ resolve,
111
+ reject
112
+ });
113
+
114
+ this._startWorker();
115
+ }));
116
+ }
117
+ /*
118
+ * Send a message to a queue.
119
+ *
120
+ * This works just like amqplib's `sendToQueue`, except if the channel is not connected, this will wait until the
121
+ * channel is connected. Returns a Promise which will only resolve when the message has been succesfully sent.
122
+ * The returned promise will be rejected only if `close()` is called on this channel before it can be sent.
123
+ *
124
+ * `message` here should be a JSON-able object.
125
+ */
126
+
127
+
128
+ sendToQueue(queue, content, options, done = null) {
129
+ return _promiseBreaker.default.addCallback(done, new Promise((resolve, reject) => {
130
+ this._messages.push({
131
+ type: 'sendToQueue',
132
+ queue,
133
+ content,
134
+ options,
135
+ resolve,
136
+ reject
137
+ });
138
+
139
+ return this._startWorker();
140
+ }));
141
+ }
142
+ /**
143
+ * Create a new ChannelWrapper.
144
+ *
145
+ * @param {AmqpConnectionManager} connectionManager - connection manager which
146
+ * created this channel.
147
+ * @param {Object} [options] -
148
+ * @param {string} [options.name] - A name for this channel. Handy for debugging.
149
+ * @param {function} [options.setup] - A default setup function to call. See
150
+ * `addSetup` for details.
151
+ * @param {boolean} [options.json] - if true, then ChannelWrapper assumes all
152
+ * messages passed to `publish()` and `sendToQueue()` are plain JSON objects.
153
+ * These will be encoded automatically before being sent.
154
+ *
155
+ */
156
+
157
+
158
+ constructor(connectionManager, options = {}) {
159
+ super();
160
+ this._onConnect = this._onConnect.bind(this);
161
+ this._onDisconnect = this._onDisconnect.bind(this);
162
+ this._connectionManager = connectionManager;
163
+ this.name = options.name;
164
+ this.context = {};
165
+ this._json = 'json' in options ? options.json : false; // Place to store queued messages.
166
+
167
+ this._messages = []; // Place to store published, but not yet confirmed messages
168
+
169
+ this._unconfirmedMessages = []; // True if the "worker" is busy sending messages. False if we need to
170
+ // start the worker to get stuff done.
171
+
172
+ this._working = false; // If we're in the process of creating a channel, this is a Promise which
173
+ // will resolve when the channel is set up. Otherwise, this is `null`.
174
+
175
+ this._settingUp = null; // The currently connected channel. Note that not all setup functions
176
+ // have been run on this channel until `@_settingUp` is either null or
177
+ // resolved.
178
+
179
+ this._channel = null; // We kill off workers when we disconnect. Whenever we start a new
180
+ // worker, we bump up the `_workerNumber` - this makes it so if stale
181
+ // workers ever do wake up, they'll know to stop working.
182
+
183
+ this._workerNumber = 0; // Array of setup functions to call.
184
+
185
+ this._setups = [];
186
+
187
+ if (options.setup) {
188
+ this._setups.push(options.setup);
57
189
  }
58
190
 
59
- /**
60
- * Remove a setup function added with `addSetup`. If there is currently a
61
- * connection, `teardown(channel, [cb])` will be run immediately, and the
62
- * returned Promise will not resolve until it completes.
63
- *
64
- * @param {function} setup - the setup function to remove.
65
- * @param {function} [teardown] - `function(channel, [cb])` to run to tear
66
- * down the chanel.
67
- * @param {function} [done] - Optional callback.
68
- * @returns {void | Promise} - Resolves when complete.
69
- */
70
- removeSetup(setup, teardown = null, done = null) {
71
- return _promiseBreaker2.default.addCallback(done, () => {
72
- this._setups = this._setups.filter(s => s !== setup);
73
-
74
- return (this._settingUp || Promise.resolve()).then(() => this._channel ? _promiseBreaker2.default.call(teardown, null, this._channel) : undefined);
75
- });
191
+ if (connectionManager.isConnected()) {
192
+ this._onConnect({
193
+ connection: this._connectionManager._currentConnection
194
+ });
76
195
  }
77
196
 
78
- /**
79
- * Returns a Promise which resolves when this channel next connects.
80
- * (Mainly here for unit testing...)
81
- *
82
- * @param {function} [done] - Optional callback.
83
- * @returns {void | Promise} - Resolves when connected.
84
- */
85
- waitForConnect(done = null) {
86
- return _promiseBreaker2.default.addCallback(done, this._channel && !this._settingUp ? Promise.resolve() : new Promise(resolve => this.once('connect', resolve)));
87
- }
197
+ connectionManager.on('connect', this._onConnect);
198
+ connectionManager.on('disconnect', this._onDisconnect);
199
+ } // Called whenever we connect to the broker.
200
+
201
+
202
+ _onConnect({
203
+ connection
204
+ }) {
205
+ this._connection = connection;
206
+ return connection.createConfirmChannel().then(channel => {
207
+ this._channel = channel;
208
+ channel.on('close', () => this._onChannelClose(channel));
209
+ this._settingUp = Promise.all(this._setups.map(setupFn => // TODO: Use a timeout here to guard against setupFns that never resolve?
210
+ _promiseBreaker.default.call(setupFn, this, channel).catch(err => {
211
+ if (this._channel) {
212
+ this.emit('error', err, {
213
+ name: this.name
214
+ });
215
+ } else {// Don't emit an error if setups failed because the channel was closing.
216
+ }
217
+ }))).then(() => {
218
+ this._settingUp = null;
219
+ return this._channel;
220
+ });
221
+ return this._settingUp;
222
+ }).then(() => {
223
+ if (!this._channel) {
224
+ // Can happen if channel closes while we're setting up.
225
+ return;
226
+ }
227
+
228
+ if (this._unconfirmedMessages.length > 0) {
229
+ // requeu any messages that were left unconfirmed when connection was lost
230
+ while (this._unconfirmedMessages.length) {
231
+ this._messages.push(this._unconfirmedMessages.shift());
232
+ }
233
+ } // Since we just connected, publish any queued messages
88
234
 
89
- /*
90
- * Publish a message to the channel.
91
- *
92
- * This works just like amqplib's `publish()`, except if the channel is not
93
- * connected, this will wait until the channel is connected. Returns a
94
- * Promise which will only resolve when the message has been succesfully sent.
95
- * The returned promise will be rejected if `close()` is called on this
96
- * channel before it can be sent, if `options.json` is set and the message
97
- * can't be encoded, or if the broker rejects the message for some reason.
98
- *
99
- */
100
- publish(exchange, routingKey, content, options, done = null) {
101
- return _promiseBreaker2.default.addCallback(done, new Promise((resolve, reject) => {
102
- this._messages.push({
103
- type: 'publish',
104
- exchange,
105
- routingKey,
106
- content,
107
- options,
108
- resolve,
109
- reject
110
- });
111
- this._startWorker();
112
- }));
113
- }
114
235
 
115
- /*
116
- * Send a message to a queue.
117
- *
118
- * This works just like amqplib's `sendToQueue`, except if the channel is not connected, this will wait until the
119
- * channel is connected. Returns a Promise which will only resolve when the message has been succesfully sent.
120
- * The returned promise will be rejected only if `close()` is called on this channel before it can be sent.
121
- *
122
- * `message` here should be a JSON-able object.
123
- */
124
- sendToQueue(queue, content, options, done = null) {
125
- return _promiseBreaker2.default.addCallback(done, new Promise((resolve, reject) => {
126
- this._messages.push({
127
- type: 'sendToQueue',
128
- queue,
129
- content,
130
- options,
131
- resolve,
132
- reject
133
- });
134
- return this._startWorker();
135
- }));
236
+ this._startWorker();
237
+
238
+ this.emit('connect');
239
+ }).catch(err => {
240
+ this.emit('error', err, {
241
+ name: this.name
242
+ });
243
+ this._settingUp = null;
244
+ this._channel = null;
245
+ });
246
+ } // Called whenever the channel closes.
247
+
248
+
249
+ _onChannelClose(channel) {
250
+ if (this._channel === channel) {
251
+ this._channel = null;
136
252
  }
253
+ } // Wait for another reconnect to create a new channel.
254
+ // Called whenever we disconnect from the AMQP server.
137
255
 
138
- /**
139
- * Create a new ChannelWrapper.
140
- *
141
- * @param {AmqpConnectionManager} connectionManager - connection manager which
142
- * created this channel.
143
- * @param {Object} [options] -
144
- * @param {string} [options.name] - A name for this channel. Handy for debugging.
145
- * @param {function} [options.setup] - A default setup function to call. See
146
- * `addSetup` for details.
147
- * @param {boolean} [options.json] - if true, then ChannelWrapper assumes all
148
- * messages passed to `publish()` and `sendToQueue()` are plain JSON objects.
149
- * These will be encoded automatically before being sent.
150
- *
151
- */
152
- constructor(connectionManager, options = {}) {
153
- super();
154
- this._onConnect = this._onConnect.bind(this);
155
- this._onDisconnect = this._onDisconnect.bind(this);
156
- this._connectionManager = connectionManager;
157
- this.name = options.name;
158
-
159
- this._json = 'json' in options ? options.json : false;
160
-
161
- // Place to store queued messages.
162
- this._messages = [];
163
-
164
- // True if the "worker" is busy sending messages. False if we need to
165
- // start the worker to get stuff done.
166
- this._working = false;
167
-
168
- // If we're in the process of creating a channel, this is a Promise which
169
- // will resolve when the channel is set up. Otherwise, this is `null`.
170
- this._settingUp = null;
171
256
 
172
- // The currently connected channel. Note that not all setup functions
173
- // have been run on this channel until `@_settingUp` is either null or
174
- // resolved.
175
- this._channel = null;
257
+ _onDisconnect() {
258
+ this._channel = null;
259
+ this._settingUp = null; // Kill off the current worker. We never get any kind of error for messages in flight - see
260
+ // https://github.com/squaremo/amqp.node/issues/191.
176
261
 
177
- // We kill off workers when we disconnect. Whenever we start a new
178
- // worker, we bump up the `_workerNumber` - this makes it so if stale
179
- // workers ever do wake up, they'll know to stop working.
180
- this._workerNumber = 0;
262
+ this._working = false;
263
+ } // Returns the number of unsent messages queued on this channel.
181
264
 
182
- // Array of setup functions to call.
183
- this._setups = [];
184
- if (options.setup) {
185
- this._setups.push(options.setup);
186
- }
187
265
 
188
- if (connectionManager.isConnected()) {
189
- this._onConnect({
190
- connection: this._connectionManager._currentConnection
191
- });
192
- }
193
- connectionManager.on('connect', this._onConnect);
194
- connectionManager.on('disconnect', this._onDisconnect);
266
+ queueLength() {
267
+ return this._messages.length;
268
+ } // Destroy this channel.
269
+ //
270
+ // Any unsent messages will have their associated Promises rejected.
271
+ //
272
+
273
+
274
+ close() {
275
+ return Promise.resolve().then(() => {
276
+ this._working = false;
277
+
278
+ if (this._messages.length !== 0) {
279
+ // Reject any unsent messages.
280
+ this._messages.forEach(message => message.reject(new Error('Channel closed')));
281
+ }
282
+
283
+ if (this._unconfirmedMessages.length !== 0) {
284
+ // Reject any unconfirmed messages.
285
+ this._unconfirmedMessages.forEach(message => message.reject(new Error('Channel closed')));
286
+ }
287
+
288
+ this._connectionManager.removeListener('connect', this._onConnect);
289
+
290
+ this._connectionManager.removeListener('disconnect', this._onDisconnect);
291
+
292
+ const answer = this._channel && this._channel.close() || undefined;
293
+ this._channel = null;
294
+ this.emit('close');
295
+ return answer;
296
+ });
297
+ }
298
+
299
+ _shouldPublish() {
300
+ return this._messages.length > 0 && !this._settingUp && this._channel;
301
+ } // Start publishing queued messages, if there isn't already a worker doing this.
302
+
303
+
304
+ _startWorker() {
305
+ if (!this._working && this._shouldPublish()) {
306
+ this._working = true;
307
+ this._workerNumber++;
308
+
309
+ this._publishQueuedMessages(this._workerNumber);
195
310
  }
311
+ }
196
312
 
197
- // Called whenever we connect to the broker.
198
- _onConnect({ connection }) {
199
- this._connection = connection;
313
+ _publishQueuedMessages(workerNumber) {
314
+ if (!this._shouldPublish() || !this._working || workerNumber !== this._workerNumber) {
315
+ // Can't publish anything right now...
316
+ this._working = false;
317
+ return Promise.resolve();
318
+ }
200
319
 
201
- return connection.createConfirmChannel().then(channel => {
202
- this._channel = channel;
203
- channel.on('close', () => this._onChannelClose(channel));
320
+ const channel = this._channel;
204
321
 
205
- this._settingUp = Promise.all(this._setups.map(setupFn =>
206
- // TODO: Use a timeout here to guard against setupFns that never resolve?
207
- _promiseBreaker2.default.call(setupFn, null, channel).catch(err => {
208
- if (this._channel) {
209
- this.emit('error', err, { name: this.name });
322
+ const message = this._messages.shift();
323
+
324
+ this._unconfirmedMessages.push(message);
325
+
326
+ Promise.resolve().then(() => {
327
+ const encodedMessage = this._json ? new Buffer.from(JSON.stringify(message.content)) : message.content;
328
+
329
+ const sendPromise = (() => {
330
+ switch (message.type) {
331
+ case 'publish':
332
+ return new Promise(function (resolve, reject) {
333
+ const result = channel.publish(message.exchange, message.routingKey, encodedMessage, message.options, err => {
334
+ if (err) {
335
+ reject(err);
210
336
  } else {
211
- // Don't emit an error if setups failed because the channel was closing.
337
+ setImmediate(() => resolve(result));
212
338
  }
213
- }))).then(() => {
214
- this._settingUp = null;
215
- return this._channel;
339
+ });
216
340
  });
217
341
 
218
- return this._settingUp;
219
- }).then(() => {
220
- if (!this._channel) {
221
- // Can happen if channel closes while we're setting up.
222
- return;
223
- }
224
-
225
- // Since we just connected, publish any queued messages
226
- this._startWorker();
227
- this.emit('connect');
228
- }).catch(err => {
229
- this.emit('error', err, { name: this.name });
230
- this._settingUp = null;
231
- this._channel = null;
232
- });
233
- }
342
+ case 'sendToQueue':
343
+ return new Promise(function (resolve, reject) {
344
+ const result = channel.sendToQueue(message.queue, encodedMessage, message.options, err => {
345
+ if (err) {
346
+ reject(err);
347
+ } else {
348
+ setImmediate(() => resolve(result));
349
+ }
350
+ });
351
+ });
234
352
 
235
- // Called whenever the channel closes.
236
- _onChannelClose(channel) {
237
- if (this._channel === channel) {
238
- this._channel = null;
353
+ /* istanbul ignore next */
354
+
355
+ default:
356
+ throw new Error(`Unhandled message type ${message.type}`);
239
357
  }
240
- }
241
- // Wait for another reconnect to create a new channel.
358
+ })(); // Send some more!
242
359
 
243
- // Called whenever we disconnect from the AMQP server.
244
- _onDisconnect() {
245
- this._channel = null;
246
- this._settingUp = null;
247
360
 
248
- // Kill off the current worker. We never get any kind of error for messages in flight - see
249
- // https://github.com/squaremo/amqp.node/issues/191.
250
- this._working = false;
251
- }
361
+ this._publishQueuedMessages(workerNumber);
252
362
 
253
- // Returns the number of unsent messages queued on this channel.
254
- queueLength() {
255
- return this._messages.length;
256
- }
363
+ return sendPromise;
364
+ }).then(result => {
365
+ this._unconfirmedMessages.shift();
257
366
 
258
- // Destroy this channel.
259
- //
260
- // Any unsent messages will have their associated Promises rejected.
261
- //
262
- close() {
263
- return Promise.resolve().then(() => {
264
- this._working = false;
265
- if (this._messages.length !== 0) {
266
- // Reject any unsent messages.
267
- this._messages.forEach(message => message.reject(new Error('Channel closed')));
268
- }
269
-
270
- this._connectionManager.removeListener('connect', this._onConnect);
271
- this._connectionManager.removeListener('disconnect', this._onDisconnect);
272
- const answer = this._channel && this._channel.close() || undefined;
273
- this._channel = null;
274
-
275
- this.emit('close');
276
-
277
- return answer;
278
- });
279
- }
367
+ message.resolve(result);
368
+ }, err => {
369
+ if (!this._channel) {
370
+ // Tried to write to a closed channel. Leave the message in the queue and we'll try again when we
371
+ // reconnect.
372
+ this._messages.unshift(this._unconfirmedMessages.shift());
373
+ } else {
374
+ // Something went wrong trying to send this message - could be JSON.stringify failed, could be the
375
+ // broker rejected the message. Either way, reject it back
376
+ this._unconfirmedMessages.shift();
280
377
 
281
- _shouldPublish() {
282
- return this._messages.length > 0 && !this._settingUp && this._channel;
283
- }
378
+ message.reject(err);
379
+ }
380
+ }).catch(
381
+ /* istanbul ignore next */
382
+ err => {
383
+ this.emit('error', err);
384
+ this._working = false;
385
+ });
386
+ return null;
387
+ } // Send an `ack` to the underlying channel.
284
388
 
285
- // Start publishing queued messages, if there isn't already a worker doing this.
286
- _startWorker() {
287
- if (!this._working && this._shouldPublish()) {
288
- this._working = true;
289
- this._workerNumber++;
290
- this._publishQueuedMessages(this._workerNumber);
291
- }
292
- }
293
389
 
294
- _publishQueuedMessages(workerNumber) {
295
- if (!this._shouldPublish() || !this._working || workerNumber !== this._workerNumber) {
296
- // Can't publish anything right now...
297
- this._working = false;
298
- return Promise.resolve();
299
- }
390
+ ack() {
391
+ return this._channel && this._channel.ack.apply(this._channel, arguments);
392
+ } // Send an `ackAll` to the underlying channel.
300
393
 
301
- const channel = this._channel;
302
- const message = this._messages[0];
303
-
304
- Promise.resolve().then(() => {
305
- const encodedMessage = this._json ? new Buffer.from(JSON.stringify(message.content)) : message.content;
306
-
307
- const sendPromise = (() => {
308
- switch (message.type) {
309
- case 'publish':
310
- return new Promise(function (resolve, reject) {
311
- const result = channel.publish(message.exchange, message.routingKey, encodedMessage, message.options, err => {
312
- if (err) {
313
- reject(err);
314
- } else {
315
- setImmediate(() => resolve(result));
316
- }
317
- });
318
- });
319
- case 'sendToQueue':
320
- return new Promise(function (resolve, reject) {
321
- const result = channel.sendToQueue(message.queue, encodedMessage, message.options, err => {
322
- if (err) {
323
- reject(err);
324
- } else {
325
- setImmediate(() => resolve(result));
326
- }
327
- });
328
- });
329
-
330
- /* istanbul ignore next */
331
- default:
332
- throw new Error(`Unhandled message type ${message.type}`);
333
- }
334
- })();
335
-
336
- return sendPromise;
337
- }).then(result => {
338
- this._messages.shift();
339
- message.resolve(result);
340
-
341
- // Send some more!
342
- this._publishQueuedMessages(workerNumber);
343
- }, err => {
344
- if (!this._channel) {
345
- // Tried to write to a closed channel. Leave the message in the queue and we'll try again when we
346
- // reconnect.
347
- } else {
348
- // Something went wrong trying to send this message - could be JSON.stringify failed, could be the
349
- // broker rejected the message. Either way, reject it back
350
- this._messages.shift();
351
- message.reject(err);
352
-
353
- // Send some more!
354
- this._publishQueuedMessages(workerNumber);
355
- }
356
- }).catch( /* istanbul ignore next */err => {
357
- this.emit('error', err);
358
- this._working = false;
359
- });
360
-
361
- return null;
362
- }
363
394
 
364
- // Send an `ack` to the underlying channel.
365
- ack() {
366
- return this._channel && this._channel.ack.apply(this._channel, arguments);
367
- }
395
+ ackAll() {
396
+ return this._channel && this._channel.ackAll.apply(this._channel, arguments);
397
+ } // Send a `nack` to the underlying channel.
398
+
399
+
400
+ nack() {
401
+ return this._channel && this._channel.nack.apply(this._channel, arguments);
402
+ } // Send a `nackAll` to the underlying channel.
403
+
404
+
405
+ nackAll() {
406
+ return this._channel && this._channel.nackAll.apply(this._channel, arguments);
407
+ }
368
408
 
369
- // Send a `nack` to the underlying channel.
370
- nack() {
371
- return this._channel && this._channel.nack.apply(this._channel, arguments);
372
- }
373
409
  }
410
+
374
411
  exports.default = ChannelWrapper;
375
412
  //# sourceMappingURL=ChannelWrapper.js.map