amqplib 0.10.3 → 0.10.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.
Files changed (59) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +7 -4
  2. package/.github/workflows/publish.yml +59 -0
  3. package/.github/workflows/test.yml +15 -2
  4. package/CHANGELOG.md +14 -1
  5. package/Makefile +22 -20
  6. package/README.md +1 -2
  7. package/bin/amqp-rabbitmq-0.9.1.json +483 -0
  8. package/bin/generate-defs.js +5 -5
  9. package/examples/direct_reply_to_client.js +22 -0
  10. package/examples/direct_reply_to_server.js +25 -0
  11. package/examples/headers.js +29 -31
  12. package/examples/ssl.js +16 -10
  13. package/examples/stream_queues/README.md +40 -0
  14. package/examples/stream_queues/package.json +14 -0
  15. package/examples/stream_queues/receive_stream.js +55 -0
  16. package/examples/stream_queues/send_stream.js +38 -0
  17. package/examples/tutorials/callback_api/emit_log.js +22 -22
  18. package/examples/tutorials/callback_api/emit_log_direct.js +24 -24
  19. package/examples/tutorials/callback_api/emit_log_topic.js +23 -20
  20. package/examples/tutorials/callback_api/new_task.js +21 -20
  21. package/examples/tutorials/callback_api/receive.js +27 -21
  22. package/examples/tutorials/callback_api/receive_logs.js +32 -26
  23. package/examples/tutorials/callback_api/receive_logs_direct.js +42 -41
  24. package/examples/tutorials/callback_api/receive_logs_topic.js +43 -40
  25. package/examples/tutorials/callback_api/rpc_client.js +39 -37
  26. package/examples/tutorials/callback_api/rpc_server.js +42 -32
  27. package/examples/tutorials/callback_api/send.js +21 -22
  28. package/examples/tutorials/callback_api/worker.js +29 -25
  29. package/examples/tutorials/emit_log.js +20 -15
  30. package/examples/tutorials/emit_log_direct.js +22 -16
  31. package/examples/tutorials/emit_log_topic.js +22 -15
  32. package/examples/tutorials/new_task.js +20 -13
  33. package/examples/tutorials/receive.js +19 -14
  34. package/examples/tutorials/receive_logs.js +25 -23
  35. package/examples/tutorials/receive_logs_direct.js +29 -35
  36. package/examples/tutorials/receive_logs_topic.js +28 -32
  37. package/examples/tutorials/rpc_client.js +37 -40
  38. package/examples/tutorials/rpc_server.js +36 -29
  39. package/examples/tutorials/send.js +26 -18
  40. package/examples/tutorials/worker.js +27 -21
  41. package/examples/waitForConfirms.js +18 -19
  42. package/lib/callback_model.js +295 -290
  43. package/lib/channel.js +353 -346
  44. package/lib/channel_model.js +5 -1
  45. package/lib/codec.js +26 -2
  46. package/lib/connection.js +544 -532
  47. package/lib/credentials.js +1 -1
  48. package/lib/defs.js +2568 -2457
  49. package/lib/format.js +0 -1
  50. package/lib/heartbeat.js +32 -29
  51. package/lib/mux.js +96 -99
  52. package/package.json +3 -5
  53. package/test/callback_api.js +182 -174
  54. package/test/channel_api.js +455 -445
  55. package/test/codec.js +15 -15
  56. package/test/data.js +15 -0
  57. package/test/frame.js +6 -7
  58. package/test/mux.js +1 -2
  59. package/test/util.js +1 -3
@@ -1,18 +1,25 @@
1
1
  #!/usr/bin/env node
2
2
  // Post a new task to the work queue
3
3
 
4
- var amqp = require('amqplib');
4
+ const amqp = require('amqplib');
5
5
 
6
- amqp.connect('amqp://localhost').then(function(conn) {
7
- return conn.createChannel().then(function(ch) {
8
- var q = 'task_queue';
9
- var ok = ch.assertQueue(q, {durable: true});
6
+ const queue = 'task_queue';
7
+ const text = process.argv.slice(2).join(' ') || "Hello World!";
10
8
 
11
- return ok.then(function() {
12
- var msg = process.argv.slice(2).join(' ') || "Hello World!";
13
- ch.sendToQueue(q, Buffer.from(msg), {deliveryMode: true});
14
- console.log(" [x] Sent '%s'", msg);
15
- return ch.close();
16
- });
17
- }).finally(function() { conn.close(); });
18
- }).catch(console.warn);
9
+ (async () => {
10
+ let connection;
11
+ try {
12
+ connection = await amqp.connect('amqp://localhost');
13
+ const channel = await connection.createChannel();
14
+ await channel.assertQueue(queue, { durable: true });
15
+ channel.sendToQueue(queue, Buffer.from(text), { persistent: true });
16
+ console.log(" [x] Sent '%s'", text);
17
+ await channel.close();
18
+ }
19
+ catch (err) {
20
+ console.warn(err);
21
+ }
22
+ finally {
23
+ await connection.close();
24
+ };
25
+ })();
@@ -1,21 +1,26 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib');
3
+ const amqp = require('amqplib');
4
4
 
5
- amqp.connect('amqp://localhost').then(function(conn) {
6
- process.once('SIGINT', function() { conn.close(); });
7
- return conn.createChannel().then(function(ch) {
5
+ const queue = 'hello';
8
6
 
9
- var ok = ch.assertQueue('hello', {durable: false});
7
+ (async () => {
8
+ try {
9
+ const connection = await amqp.connect('amqp://localhost');
10
+ const channel = await connection.createChannel();
10
11
 
11
- ok = ok.then(function(_qok) {
12
- return ch.consume('hello', function(msg) {
13
- console.log(" [x] Received '%s'", msg.content.toString());
14
- }, {noAck: true});
12
+ process.once('SIGINT', async () => {
13
+ await channel.close();
14
+ await connection.close();
15
15
  });
16
16
 
17
- return ok.then(function(_consumeOk) {
18
- console.log(' [*] Waiting for messages. To exit press CTRL+C');
19
- });
20
- });
21
- }).catch(console.warn);
17
+ await channel.assertQueue(queue, { durable: false });
18
+ await channel.consume(queue, (message) => {
19
+ console.log(" [x] Received '%s'", message.content.toString());
20
+ }, { noAck: true });
21
+
22
+ console.log(' [*] Waiting for messages. To exit press CTRL+C');
23
+ } catch (err) {
24
+ console.warn(err);
25
+ }
26
+ })();
@@ -1,28 +1,30 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib');
3
+ const amqp = require('amqplib');
4
4
 
5
- amqp.connect('amqp://localhost').then(function(conn) {
6
- process.once('SIGINT', function() { conn.close(); });
7
- return conn.createChannel().then(function(ch) {
8
- var ok = ch.assertExchange('logs', 'fanout', {durable: false});
9
- ok = ok.then(function() {
10
- return ch.assertQueue('', {exclusive: true});
11
- });
12
- ok = ok.then(function(qok) {
13
- return ch.bindQueue(qok.queue, 'logs', '').then(function() {
14
- return qok.queue;
15
- });
16
- });
17
- ok = ok.then(function(queue) {
18
- return ch.consume(queue, logMessage, {noAck: true});
19
- });
20
- return ok.then(function() {
21
- console.log(' [*] Waiting for logs. To exit press CTRL+C');
5
+ const exchange = 'logs';
6
+
7
+ (async () => {
8
+ try {
9
+ const connection = await amqp.connect('amqp://localhost');
10
+ const channel = await connection.createChannel();
11
+
12
+ process.once('SIGINT', async () => {
13
+ await channel.close();
14
+ await connection.close();
22
15
  });
23
16
 
24
- function logMessage(msg) {
25
- console.log(" [x] '%s'", msg.content.toString());
26
- }
27
- });
28
- }).catch(console.warn);
17
+ await channel.assertExchange(exchange, 'fanout', { durable: false });
18
+ const { queue } = await channel.assertQueue('', { exclusive: true });
19
+ await channel.bindQueue(queue, exchange, '')
20
+
21
+ await channel.consume(queue, (message) => {
22
+ if (message) console.log(" [x] '%s'", message.content.toString());
23
+ else console.warn(' [x] Consumer cancelled');
24
+ }, { noAck: true });
25
+
26
+ console.log(' [*] Waiting for logs. To exit press CTRL+C');
27
+ } catch (err) {
28
+ console.warn(err);
29
+ }
30
+ })();
@@ -1,44 +1,38 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib');
4
- var basename = require('path').basename;
3
+ const amqp = require('../..');
4
+ const { basename } = require('path');
5
5
 
6
- var severities = process.argv.slice(2);
7
- if (severities.length < 1) {
8
- console.warn('Usage: %s [info] [warning] [error]',
9
- basename(process.argv[1]));
6
+ const exchange = 'direct_logs';
7
+ const bindingKeys = process.argv.slice(2);
8
+ if (bindingKeys.length < 1) {
9
+ console.warn('Usage: %s [info] [warning] [error]', basename(process.argv[1]));
10
10
  process.exit(1);
11
11
  }
12
12
 
13
- amqp.connect('amqp://localhost').then(function(conn) {
14
- process.once('SIGINT', function() { conn.close(); });
15
- return conn.createChannel().then(function(ch) {
16
- var ex = 'direct_logs';
13
+ (async () => {
14
+ try {
15
+ const connection = await amqp.connect('amqp://localhost');
16
+ const channel = await connection.createChannel();
17
17
 
18
- var ok = ch.assertExchange(ex, 'direct', {durable: false});
19
-
20
- ok = ok.then(function() {
21
- return ch.assertQueue('', {exclusive: true});
22
- });
23
-
24
- ok = ok.then(function(qok) {
25
- var queue = qok.queue;
26
- return Promise.all(severities.map(function(sev) {
27
- ch.bindQueue(queue, ex, sev);
28
- })).then(function() { return queue; });
29
- });
30
-
31
- ok = ok.then(function(queue) {
32
- return ch.consume(queue, logMessage, {noAck: true});
33
- });
34
- return ok.then(function() {
35
- console.log(' [*] Waiting for logs. To exit press CTRL+C.');
18
+ process.once('SIGINT', async () => {
19
+ await channel.close();
20
+ await connection.close();
36
21
  });
37
22
 
38
- function logMessage(msg) {
39
- console.log(" [x] %s:'%s'",
40
- msg.fields.routingKey,
41
- msg.content.toString());
42
- }
43
- });
44
- }).catch(console.warn);
23
+ await channel.assertExchange(exchange, 'direct', { durable: false });
24
+ const { queue } = await channel.assertQueue('', { exclusive: true });
25
+ await Promise.all(bindingKeys.map(async (bindingKey) => {
26
+ await channel.bindQueue(queue, exchange, bindingKey);
27
+ }));
28
+
29
+ await channel.consume(queue, (message) => {
30
+ if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
31
+ else console.warn(' [x] Consumer cancelled');
32
+ }, { noAck: true });
33
+
34
+ console.log(' [*] Waiting for logs. To exit press CTRL+C.');
35
+ } catch(err) {
36
+ console.warn(err);
37
+ }
38
+ })();
@@ -1,43 +1,39 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib');
4
- var basename = require('path').basename;
3
+ const amqp = require('../..');
4
+ const { basename } = require('path');
5
5
 
6
- var keys = process.argv.slice(2);
7
- if (keys.length < 1) {
8
- console.log('Usage: %s pattern [pattern...]',
9
- basename(process.argv[1]));
6
+ const exchange = 'topic_logs';
7
+ const bindingKeys = process.argv.slice(2);
8
+ if (bindingKeys.length < 1) {
9
+ console.log('Usage: %s pattern [pattern...]', basename(process.argv[1]));
10
10
  process.exit(1);
11
11
  }
12
12
 
13
- amqp.connect('amqp://localhost').then(function(conn) {
14
- process.once('SIGINT', function() { conn.close(); });
15
- return conn.createChannel().then(function(ch) {
16
- var ex = 'topic_logs';
17
- var ok = ch.assertExchange(ex, 'topic', {durable: false});
13
+ (async () => {
14
+ try {
15
+ const connection = await amqp.connect('amqp://localhost');
16
+ const channel = await connection.createChannel();
18
17
 
19
- ok = ok.then(function() {
20
- return ch.assertQueue('', {exclusive: true});
18
+ process.once('SIGINT', async () => {
19
+ await channel.close();
20
+ await connection.close();
21
21
  });
22
22
 
23
- ok = ok.then(function(qok) {
24
- var queue = qok.queue;
25
- return Promise.all(keys.map(function(rk) {
26
- ch.bindQueue(queue, ex, rk);
27
- })).then(function() { return queue; });
28
- });
23
+ await channel.assertExchange(exchange, 'topic', { durable: false });
24
+ const { queue } = await channel.assertQueue('', { exclusive: true });
25
+ await Promise.all(bindingKeys.map(async (bindingKey) => {
26
+ await channel.bindQueue(queue, exchange, bindingKey);
27
+ }));
29
28
 
30
- ok = ok.then(function(queue) {
31
- return ch.consume(queue, logMessage, {noAck: true});
32
- });
33
- return ok.then(function() {
34
- console.log(' [*] Waiting for logs. To exit press CTRL+C.');
35
- });
29
+ await channel.consume(queue, (message) => {
30
+ if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
31
+ else console.warn(' [x] Consumer cancelled');
32
+ }, { noAck: true });
36
33
 
37
- function logMessage(msg) {
38
- console.log(" [x] %s:'%s'",
39
- msg.fields.routingKey,
40
- msg.content.toString());
41
- }
42
- });
43
- }).catch(console.warn);
34
+ console.log(' [*] Waiting for logs. To exit press CTRL+C.');
35
+ }
36
+ catch (err) {
37
+ console.warn(err);
38
+ }
39
+ })();
@@ -1,52 +1,49 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib');
4
- var basename = require('path').basename;
5
- var uuid = require('node-uuid');
6
-
7
- // I've departed from the form of the original RPC tutorial, which
8
- // needlessly introduces a class definition, and doesn't even
9
- // parameterise the request.
10
-
11
- var n;
12
- try {
13
- if (process.argv.length < 3) throw Error('Too few args');
14
- n = parseInt(process.argv[2]);
15
- }
16
- catch (e) {
17
- console.error(e);
3
+ const amqp = require('amqplib');
4
+ const { basename } = require('path');
5
+ const { v4: uuid } = require('uuid');
6
+
7
+ const queue = 'rpc_queue';
8
+
9
+ const n = parseInt(process.argv[2], 10);
10
+ if (isNaN(n)) {
18
11
  console.warn('Usage: %s number', basename(process.argv[1]));
19
12
  process.exit(1);
20
13
  }
21
14
 
22
- amqp.connect('amqp://localhost').then(function(conn) {
23
- return conn.createChannel().then(function(ch) {
24
- return new Promise(function(resolve) {
25
- var corrId = uuid();
26
- function maybeAnswer(msg) {
27
- if (msg.properties.correlationId === corrId) {
28
- resolve(msg.content.toString());
29
- }
30
- }
15
+ (async () => {
16
+ let connection;
17
+ try {
18
+ connection = await amqp.connect('amqp://localhost');
19
+ const channel = await connection.createChannel();
20
+ const correlationId = uuid();
31
21
 
32
- var ok = ch.assertQueue('', {exclusive: true})
33
- .then(function(qok) { return qok.queue; });
22
+ const requestFib = new Promise(async (resolve) => {
23
+ const { queue: replyTo } = await channel.assertQueue('', { exclusive: true });
34
24
 
35
- ok = ok.then(function(queue) {
36
- return ch.consume(queue, maybeAnswer, {noAck: true})
37
- .then(function() { return queue; });
38
- });
25
+ await channel.consume(replyTo, (message) => {
26
+ if (!message) console.warn(' [x] Consumer cancelled');
27
+ else if (message.properties.correlationId === correlationId) {
28
+ resolve(message.content.toString());
29
+ }
30
+ }, { noAck: true });
39
31
 
40
- ok = ok.then(function(queue) {
41
- console.log(' [x] Requesting fib(%d)', n);
42
- ch.sendToQueue('rpc_queue', Buffer.from(n.toString()), {
43
- correlationId: corrId, replyTo: queue
44
- });
32
+ await channel.assertQueue(queue, { durable: false });
33
+ console.log(' [x] Requesting fib(%d)', n);
34
+ channel.sendToQueue(queue, Buffer.from(n.toString()), {
35
+ correlationId,
36
+ replyTo,
45
37
  });
46
38
  });
47
- })
48
- .then(function(fibN) {
39
+
40
+ const fibN = await requestFib;
49
41
  console.log(' [.] Got %d', fibN);
50
- })
51
- .finally(function() { conn.close(); });
52
- }).catch(console.warn);
42
+ }
43
+ catch (err) {
44
+ console.warn(err);
45
+ }
46
+ finally {
47
+ if (connection) await connection.close();
48
+ };
49
+ })();
@@ -1,39 +1,46 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib');
3
+ const amqp = require('amqplib');
4
+
5
+ const queue = 'rpc_queue';
6
+
7
+ (async () => {
8
+ try {
9
+ const connection = await amqp.connect('amqp://localhost');
10
+ const channel = await connection.createChannel();
11
+
12
+ process.once('SIGINT', async () => {
13
+ await channel.close();
14
+ await connection.close();
15
+ });
16
+
17
+ await channel.assertQueue(queue, { durable: false });
18
+
19
+ channel.prefetch(1);
20
+ await channel.consume(queue, (message) => {
21
+ const n = parseInt(message.content.toString(), 10);
22
+ console.log(' [.] fib(%d)', n);
23
+ const response = fib(n);
24
+ channel.sendToQueue(message.properties.replyTo, Buffer.from(response.toString()), {
25
+ correlationId: message.properties.correlationId
26
+ });
27
+ channel.ack(message);
28
+ });
29
+
30
+ console.log(' [x] Awaiting RPC requests. To exit press CTRL+C.');
31
+ }
32
+ catch (err) {
33
+ console.warn(err);
34
+ }
35
+ })();
4
36
 
5
37
  function fib(n) {
6
38
  // Do it the ridiculous, but not most ridiculous, way. For better,
7
39
  // see http://nayuki.eigenstate.org/page/fast-fibonacci-algorithms
8
- var a = 0, b = 1;
9
- for (var i=0; i < n; i++) {
10
- var c = a + b;
40
+ let a = 0, b = 1;
41
+ for (let i=0; i < n; i++) {
42
+ let c = a + b;
11
43
  a = b; b = c;
12
44
  }
13
45
  return a;
14
46
  }
15
-
16
- amqp.connect('amqp://localhost').then(function(conn) {
17
- process.once('SIGINT', function() { conn.close(); });
18
- return conn.createChannel().then(function(ch) {
19
- var q = 'rpc_queue';
20
- var ok = ch.assertQueue(q, {durable: false});
21
- var ok = ok.then(function() {
22
- ch.prefetch(1);
23
- return ch.consume(q, reply);
24
- });
25
- return ok.then(function() {
26
- console.log(' [x] Awaiting RPC requests');
27
- });
28
-
29
- function reply(msg) {
30
- var n = parseInt(msg.content.toString());
31
- console.log(' [.] fib(%d)', n);
32
- var response = fib(n);
33
- ch.sendToQueue(msg.properties.replyTo,
34
- Buffer.from(response.toString()),
35
- {correlationId: msg.properties.correlationId});
36
- ch.ack(msg);
37
- }
38
- });
39
- }).catch(console.warn);
@@ -1,23 +1,31 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib');
3
+ const amqp = require('amqplib');
4
4
 
5
- amqp.connect('amqp://localhost').then(function(conn) {
6
- return conn.createChannel().then(function(ch) {
7
- var q = 'hello';
8
- var msg = 'Hello World!';
5
+ const queue = 'hello';
6
+ const text = 'Hello World!';
9
7
 
10
- var ok = ch.assertQueue(q, {durable: false});
8
+ (async () => {
9
+ let connection;
10
+ try {
11
+ connection = await amqp.connect('amqp://localhost');
12
+ const channel = await connection.createChannel();
11
13
 
12
- return ok.then(function(_qok) {
13
- // NB: `sentToQueue` and `publish` both return a boolean
14
- // indicating whether it's OK to send again straight away, or
15
- // (when `false`) that you should wait for the event `'drain'`
16
- // to fire before writing again. We're just doing the one write,
17
- // so we'll ignore it.
18
- ch.sendToQueue(q, Buffer.from(msg));
19
- console.log(" [x] Sent '%s'", msg);
20
- return ch.close();
21
- });
22
- }).finally(function() { conn.close(); });
23
- }).catch(console.warn);
14
+ await channel.assertQueue(queue, { durable: false });
15
+
16
+ // NB: `sentToQueue` and `publish` both return a boolean
17
+ // indicating whether it's OK to send again straight away, or
18
+ // (when `false`) that you should wait for the event `'drain'`
19
+ // to fire before writing again. We're just doing the one write,
20
+ // so we'll ignore it.
21
+ channel.sendToQueue(queue, Buffer.from(text));
22
+ console.log(" [x] Sent '%s'", text);
23
+ await channel.close();
24
+ }
25
+ catch (err) {
26
+ console.warn(err);
27
+ }
28
+ finally {
29
+ if (connection) await connection.close();
30
+ };
31
+ })();
@@ -1,28 +1,34 @@
1
1
  #!/usr/bin/env node
2
2
  // Process tasks from the work queue
3
3
 
4
- var amqp = require('amqplib');
4
+ const amqp = require('amqplib');
5
5
 
6
- amqp.connect('amqp://localhost').then(function(conn) {
7
- process.once('SIGINT', function() { conn.close(); });
8
- return conn.createChannel().then(function(ch) {
9
- var ok = ch.assertQueue('task_queue', {durable: true});
10
- ok = ok.then(function() { ch.prefetch(1); });
11
- ok = ok.then(function() {
12
- ch.consume('task_queue', doWork, {noAck: false});
13
- console.log(" [*] Waiting for messages. To exit press CTRL+C");
6
+ const queue = 'task_queue';
7
+
8
+ (async () => {
9
+ try {
10
+ const connection = await amqp.connect('amqp://localhost');
11
+ process.once('SIGINT', async () => {
12
+ await connection.close();
14
13
  });
15
- return ok;
16
14
 
17
- function doWork(msg) {
18
- var body = msg.content.toString();
19
- console.log(" [x] Received '%s'", body);
20
- var secs = body.split('.').length - 1;
21
- //console.log(" [x] Task takes %d seconds", secs);
22
- setTimeout(function() {
15
+ const channel = await connection.createChannel();
16
+ await channel.assertQueue(queue, { durable: true });
17
+
18
+ channel.prefetch(1);
19
+ await channel.consume(queue, (message) => {
20
+ const text = message.content.toString();
21
+ console.log(" [x] Received '%s'", text);
22
+ const seconds = text.split('.').length - 1;
23
+ setTimeout(() => {
23
24
  console.log(" [x] Done");
24
- ch.ack(msg);
25
- }, secs * 1000);
26
- }
27
- });
28
- }).catch(console.warn);
25
+ channel.ack(message);
26
+ }, seconds * 1000);
27
+ }, { noAck: false });
28
+
29
+ console.log(" [*] Waiting for messages. To exit press CTRL+C");
30
+ }
31
+ catch (err) {
32
+ console.warn(err);
33
+ }
34
+ })();
@@ -1,22 +1,21 @@
1
- var amqp = require('../');
1
+ const amqp = require('../');
2
2
 
3
- var NUM_MSGS = 20;
3
+ (async () => {
4
+ let connection;
5
+ try {
6
+ connection = await amqp.connect();
7
+ const channel = await connection.createConfirmChannel();
4
8
 
5
- function mkCallback(i) {
6
- return (i % 2) === 0 ? function(err) {
7
- if (err !== null) { console.error('Message %d failed!', i); }
8
- else { console.log('Message %d confirmed', i); }
9
- } : null;
10
- }
9
+ for (var i=0; i < 20; i++) {
10
+ channel.publish('amq.topic', 'whatever', Buffer.from('blah'));
11
+ };
11
12
 
12
- amqp.connect().then(function(c) {
13
- c.createConfirmChannel().then(function(ch) {
14
- for (var i=0; i < NUM_MSGS; i++) {
15
- ch.publish('amq.topic', 'whatever', Buffer.from('blah'), {}, mkCallback(i));
16
- }
17
- ch.waitForConfirms().then(function() {
18
- console.log('All messages done');
19
- c.close();
20
- }).catch(console.error);
21
- });
22
- });
13
+ await channel.waitForConfirms();
14
+ console.log('All messages done');
15
+ await channel.close();
16
+ } catch (err) {
17
+ console.warn(err);
18
+ } finally {
19
+ if (connection) await connection.close();
20
+ }
21
+ })();