amqplib 0.10.2 → 0.10.4

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 (55) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
  2. package/.github/workflows/test.yml +15 -2
  3. package/CHANGELOG.md +12 -1
  4. package/Makefile +3 -4
  5. package/README.md +1 -1
  6. package/bin/generate-defs.js +5 -5
  7. package/examples/direct_reply_to_client.js +22 -0
  8. package/examples/direct_reply_to_server.js +25 -0
  9. package/examples/headers.js +29 -31
  10. package/examples/ssl.js +16 -10
  11. package/examples/stream_queues/README.md +40 -0
  12. package/examples/stream_queues/package.json +14 -0
  13. package/examples/stream_queues/receive_stream.js +55 -0
  14. package/examples/stream_queues/send_stream.js +38 -0
  15. package/examples/tutorials/callback_api/emit_log.js +22 -22
  16. package/examples/tutorials/callback_api/emit_log_direct.js +24 -24
  17. package/examples/tutorials/callback_api/emit_log_topic.js +23 -20
  18. package/examples/tutorials/callback_api/new_task.js +21 -20
  19. package/examples/tutorials/callback_api/receive.js +27 -21
  20. package/examples/tutorials/callback_api/receive_logs.js +32 -26
  21. package/examples/tutorials/callback_api/receive_logs_direct.js +42 -41
  22. package/examples/tutorials/callback_api/receive_logs_topic.js +43 -40
  23. package/examples/tutorials/callback_api/rpc_client.js +39 -37
  24. package/examples/tutorials/callback_api/rpc_server.js +42 -32
  25. package/examples/tutorials/callback_api/send.js +21 -22
  26. package/examples/tutorials/callback_api/worker.js +29 -25
  27. package/examples/tutorials/emit_log.js +20 -15
  28. package/examples/tutorials/emit_log_direct.js +22 -16
  29. package/examples/tutorials/emit_log_topic.js +22 -15
  30. package/examples/tutorials/new_task.js +20 -13
  31. package/examples/tutorials/receive.js +19 -14
  32. package/examples/tutorials/receive_logs.js +25 -23
  33. package/examples/tutorials/receive_logs_direct.js +29 -35
  34. package/examples/tutorials/receive_logs_topic.js +28 -32
  35. package/examples/tutorials/rpc_client.js +37 -40
  36. package/examples/tutorials/rpc_server.js +36 -29
  37. package/examples/tutorials/send.js +26 -18
  38. package/examples/tutorials/worker.js +27 -21
  39. package/examples/waitForConfirms.js +18 -19
  40. package/lib/callback_model.js +295 -290
  41. package/lib/channel.js +353 -346
  42. package/lib/channel_model.js +5 -1
  43. package/lib/codec.js +2 -2
  44. package/lib/connection.js +542 -527
  45. package/lib/credentials.js +1 -1
  46. package/lib/defs.js +2568 -2457
  47. package/lib/format.js +0 -1
  48. package/lib/frame.js +1 -1
  49. package/lib/heartbeat.js +32 -29
  50. package/lib/mux.js +96 -99
  51. package/package.json +2 -2
  52. package/test/callback_api.js +182 -174
  53. package/test/channel_api.js +455 -445
  54. package/test/codec.js +4 -4
  55. package/test/frame.js +5 -5
@@ -1,27 +1,28 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
4
 
5
- function bail(err, conn) {
6
- console.error(err);
7
- if (conn) conn.close(function() { process.exit(1); });
8
- }
5
+ const queue = 'task_queue';
6
+ const text = process.argv.slice(2).join(' ') || "Hello World!";
9
7
 
10
- function on_connect(err, conn) {
11
- if (err !== null) return bail(err);
12
-
13
- var q = 'task_queue';
14
-
15
- conn.createChannel(function(err, ch) {
16
- if (err !== null) return bail(err, conn);
17
- ch.assertQueue(q, {durable: true}, function(err, _ok) {
18
- if (err !== null) return bail(err, conn);
19
- var msg = process.argv.slice(2).join(' ') || "Hello World!";
20
- ch.sendToQueue(q, Buffer.from(msg), {persistent: true});
21
- console.log(" [x] Sent '%s'", msg);
22
- ch.close(function() { conn.close(); });
8
+ amqp.connect((err, connection) => {
9
+ if (err) return bail(err);
10
+ connection.createChannel((err, channel) => {
11
+ if (err) return bail(err, connection);
12
+ channel.assertQueue(queue, { durable: true }, (err) => {
13
+ if (err) return bails(err, connection);
14
+ channel.sendToQueue(queue, Buffer.from(text), { persistent: true });
15
+ console.log(" [x] Sent '%s'", text);
16
+ channel.close(() => {
17
+ connection.close();
18
+ });
23
19
  });
24
20
  });
25
- }
21
+ });
26
22
 
27
- amqp.connect(on_connect);
23
+ function bail(err, connection) {
24
+ console.error(err);
25
+ if (connection) connection.close(() => {
26
+ process.exit(1);
27
+ });
28
+ }
@@ -1,30 +1,36 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
4
 
5
- function bail(err, conn) {
6
- console.error(err);
7
- if (conn) conn.close(function() { process.exit(1); });
8
- }
5
+ const queue = 'hello';
9
6
 
10
- function on_connect(err, conn) {
11
- if (err !== null) return bail(err);
12
- process.once('SIGINT', function() { conn.close(); });
13
-
14
- var q = 'hello';
7
+ amqp.connect((err, connection) => {
8
+ if (err) return bail(err);
9
+ connection.createChannel((err, channel) => {
10
+ if (err) return bail(err, connection);
15
11
 
16
- function on_channel_open(err, ch) {
17
- ch.assertQueue(q, {durable: false}, function(err, ok) {
18
- if (err !== null) return bail(err, conn);
19
- ch.consume(q, function(msg) { // message callback
20
- console.log(" [x] Received '%s'", msg.content.toString());
21
- }, {noAck: true}, function(_consumeOk) { // consume callback
22
- console.log(' [*] Waiting for messages. To exit press CTRL+C');
12
+ process.once('SIGINT', () => {
13
+ channel.close(() => {
14
+ connection.close();
23
15
  });
24
16
  });
25
- }
26
17
 
27
- conn.createChannel(on_channel_open);
28
- }
18
+ channel.assertQueue(queue, { durable: false }, (err) => {
19
+ if (err) return bail(err, connection);
20
+ channel.consume(queue, (message) => {
21
+ if (message) console.log(" [x] Received '%s'", message.content.toString());
22
+ else console.warn(' [x] Consumer cancelled');
23
+ }, { noAck: true }, (err) => {
24
+ if (err) return bail(err, connection);
25
+ console.log(" [*] Waiting for logs. To exit press CTRL+C.");
26
+ });
27
+ });
28
+ });
29
+ });
29
30
 
30
- amqp.connect(on_connect);
31
+ function bail(err, connection) {
32
+ console.error(err);
33
+ if (connection) connection.close(() => {
34
+ process.exit(1);
35
+ });
36
+ }
@@ -1,36 +1,42 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
4
 
5
- function bail(err, conn) {
6
- console.error(err);
7
- if (conn) conn.close(function() { process.exit(1); });
8
- }
5
+ const exchange = 'logs';
9
6
 
10
- function on_connect(err, conn) {
11
- if (err !== null) return bail(err);
12
- process.once('SIGINT', function() { conn.close(); });
7
+ amqp.connect((err, connection) => {
8
+ if (err) return bail(err);
9
+ connection.createChannel((err, channel) => {
10
+ if (err) return bail(err, connection);
13
11
 
14
- var ex = 'logs';
15
-
16
- function on_channel_open(err, ch) {
17
- if (err !== null) return bail(err, conn);
18
- ch.assertQueue('', {exclusive: true}, function(err, ok) {
19
- var q = ok.queue;
20
- ch.bindQueue(q, ex, '');
21
- ch.consume(q, logMessage, {noAck: true}, function(err, ok) {
22
- if (err !== null) return bail(err, conn);
23
- console.log(" [*] Waiting for logs. To exit press CTRL+C.");
12
+ process.once('SIGINT', () => {
13
+ channel.close(() => {
14
+ connection.close();
24
15
  });
25
16
  });
26
- }
27
17
 
28
- function logMessage(msg) {
29
- if (msg)
30
- console.log(" [x] '%s'", msg.content.toString());
31
- }
18
+ channel.assertExchange(exchange, 'fanout', { durable: false }, (err, { queue }) => {
19
+ if (err) return bail(err, connection);
20
+ channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
21
+ if (err) return bail(err, connection);
22
+ channel.bindQueue(queue, exchange, '', {}, (err) => {
23
+ if (err) return bail(err, connection);
24
+ channel.consume(queue, (message) => {
25
+ if (message) console.log(" [x] '%s'", message.content.toString());
26
+ else console.warn(' [x] Consumer cancelled');
27
+ }, { noAck: true }, (err) => {
28
+ if (err) return bail(err, connection);
29
+ console.log(" [*] Waiting for logs. To exit press CTRL+C.");
30
+ });
31
+ });
32
+ });
33
+ });
34
+ });
35
+ });
32
36
 
33
- conn.createChannel(on_channel_open);
37
+ function bail(err, connection) {
38
+ console.error(err);
39
+ if (connection) connection.close(() => {
40
+ process.exit(1);
41
+ });
34
42
  }
35
-
36
- amqp.connect(on_connect);
@@ -1,56 +1,57 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
+ const { basename } = require('path');
4
5
 
5
- var basename = require('path').basename;
6
-
7
- var severities = process.argv.slice(2);
6
+ const exchange = 'direct_logs';
7
+ const severities = process.argv.slice(2);
8
8
  if (severities.length < 1) {
9
- console.log('Usage %s [info] [warning] [error]',
10
- basename(process.argv[1]));
9
+ console.log('Usage %s [info] [warning] [error]', basename(process.argv[1]));
11
10
  process.exit(1);
12
11
  }
13
12
 
14
- function bail(err, conn) {
15
- console.error(err);
16
- if (conn) conn.close(function() { process.exit(1); });
17
- }
18
-
19
- function on_connect(err, conn) {
20
- if (err !== null) return bail(err);
21
- process.once('SIGINT', function() { conn.close(); });
22
-
23
- conn.createChannel(function(err, ch) {
24
- if (err !== null) return bail(err, conn);
25
- var ex = 'direct_logs', exopts = {durable: false};
26
-
27
- ch.assertExchange(ex, 'direct', exopts);
28
- ch.assertQueue('', {exclusive: true}, function(err, ok) {
29
- if (err !== null) return bail(err, conn);
30
-
31
- var queue = ok.queue, i = 0;
13
+ amqp.connect((err, connection) => {
14
+ if (err) return bail(err);
15
+ connection.createChannel((err, channel) => {
16
+ if (err) return bail(err, connection);
32
17
 
33
- function sub(err) {
34
- if (err !== null) return bail(err, conn);
35
- else if (i < severities.length) {
36
- ch.bindQueue(queue, ex, severities[i], {}, sub);
37
- i++;
38
- }
39
- }
18
+ process.once('SIGINT', () => {
19
+ channel.close(() => {
20
+ connection.close();
21
+ });
22
+ });
40
23
 
41
- ch.consume(queue, logMessage, {noAck: true}, function(err) {
42
- if (err !== null) return bail(err, conn);
43
- console.log(' [*] Waiting for logs. To exit press CTRL+C.');
44
- sub(null);
24
+ channel.assertExchange(exchange, 'direct', { durable: false }, (err) => {
25
+ if (err) return bail(err, connection);
26
+ channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
27
+ if (err) return bail(err, connection);
28
+ channel.consume(queue, (message) => {
29
+ if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
30
+ else console.warn(' [x] Consumer cancelled');
31
+ }, {noAck: true}, function(err) {
32
+ if (err) return bail(err, connection);
33
+ console.log(' [*] Waiting for logs. To exit press CTRL+C.');
34
+ subscribeAll(channel, queue, severities, (err) => {
35
+ if (err) return bail(err, connection);
36
+ });
37
+ });
45
38
  });
46
39
  });
47
40
  });
41
+ });
42
+
43
+ function subscribeAll(channel, queue, bindingKeys, cb) {
44
+ if (bindingKeys.length === 0) return cb();
45
+ const bindingKey = bindingKeys.shift();
46
+ channel.bindQueue(queue, exchange, bindingKey, {}, (err) => {
47
+ if (err) return cb(err);
48
+ subscribeAll(channel, queue, bindingKeys, cb);
49
+ });
48
50
  }
49
51
 
50
- function logMessage(msg) {
51
- console.log(" [x] %s:'%s'",
52
- msg.fields.routingKey,
53
- msg.content.toString());
52
+ function bail(err, connection) {
53
+ console.error(err);
54
+ if (connection) connection.close(() => {
55
+ process.exit(1);
56
+ });
54
57
  }
55
-
56
- amqp.connect(on_connect);
@@ -1,55 +1,58 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
4
- var basename = require('path').basename;
3
+ const amqp = require('amqplib/callback_api');
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 severities = process.argv.slice(2);
8
+ if (severities.length < 1) {
9
+ console.log('Usage %s [info] [warning] [error]', basename(process.argv[1]));
10
10
  process.exit(1);
11
11
  }
12
12
 
13
- function bail(err, conn) {
14
- console.error(err);
15
- if (conn) conn.close(function() { process.exit(1); });
16
- }
17
-
18
- function on_connect(err, conn) {
19
- if (err !== null) return bail(err);
20
- process.once('SIGINT', function() { conn.close(); });
21
-
22
- conn.createChannel(function(err, ch) {
23
- if (err !== null) return bail(err, conn);
24
- var ex = 'topic_logs', exopts = {durable: false};
25
-
26
- ch.assertExchange(ex, 'topic', exopts);
27
- ch.assertQueue('', {exclusive: true}, function(err, ok) {
28
- if (err !== null) return bail(err, conn);
29
-
30
- var queue = ok.queue, i = 0;
13
+ amqp.connect((err, connection) => {
14
+ if (err) return bail(err);
15
+ connection.createChannel((err, channel) => {
16
+ if (err) return bail(err, connection);
31
17
 
32
- function sub(err) {
33
- if (err !== null) return bail(err, conn);
34
- else if (i < keys.length) {
35
- ch.bindQueue(queue, ex, keys[i], {}, sub);
36
- i++;
37
- }
38
- }
18
+ process.once('SIGINT', () => {
19
+ channel.close(() => {
20
+ connection.close();
21
+ });
22
+ });
39
23
 
40
- ch.consume(queue, logMessage, {noAck: true}, function(err) {
41
- if (err !== null) return bail(err, conn);
42
- console.log(' [*] Waiting for logs. To exit press CTRL+C.');
43
- sub(null);
24
+ channel.assertExchange(exchange, 'topic', { durable: false }, (err) => {
25
+ if (err) return bail(err, connection);
26
+ channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
27
+ if (err) return bail(err, connection);
28
+ channel.consume(queue, (message) => {
29
+ if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
30
+ else console.warn(' [x] Consumer cancelled');
31
+ }, {noAck: true}, function(err) {
32
+ if (err) return bail(err, connection);
33
+ console.log(' [*] Waiting for logs. To exit press CTRL+C.');
34
+ subscribeAll(channel, queue, severities, (err) => {
35
+ if (err) return bail(err, connection);
36
+ });
37
+ });
44
38
  });
45
39
  });
46
40
  });
41
+ });
42
+
43
+ function subscribeAll(channel, queue, bindingKeys, cb) {
44
+ if (bindingKeys.length === 0) return cb();
45
+ const bindingKey = bindingKeys.shift();
46
+ channel.bindQueue(queue, exchange, bindingKey, {}, (err) => {
47
+ if (err) return cb(err);
48
+ subscribeAll(channel, queue, bindingKeys, cb);
49
+ });
47
50
  }
48
51
 
49
- function logMessage(msg) {
50
- console.log(" [x] %s:'%s'",
51
- msg.fields.routingKey,
52
- msg.content.toString());
52
+ function bail(err, connection) {
53
+ console.error(err);
54
+ if (connection) connection.close(() => {
55
+ process.exit(1);
56
+ });
53
57
  }
54
58
 
55
- amqp.connect(on_connect);
@@ -1,49 +1,51 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
4
- var basename = require('path').basename;
5
- var uuid = require('node-uuid');
6
-
7
- var n;
8
- try {
9
- if (process.argv.length < 3) throw Error('Too few args');
10
- n = parseInt(process.argv[2]);
11
- }
12
- catch (e) {
13
- console.error(e);
3
+ const amqp = require('amqplib/callback_api');
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)) {
14
11
  console.warn('Usage: %s number', basename(process.argv[1]));
15
12
  process.exit(1);
16
13
  }
17
14
 
18
- function bail(err, conn) {
19
- console.error(err);
20
- if (conn) conn.close(function() { process.exit(1); });
21
- }
15
+ amqp.connect((err, connection) => {
16
+ if (err) return bail(err);
17
+ connection.createChannel((err, channel) => {
18
+ if (err) return bail(err, connection);
19
+ channel.assertQueue('', { exclusive: true }, (err, { queue: replyTo }) => {
20
+ if (err) return bail(err, connection);
22
21
 
23
- function on_connect(err, conn) {
24
- if (err !== null) return bail(err);
25
- conn.createChannel(function(err, ch) {
26
- if (err !== null) return bail(err, conn);
27
-
28
- var correlationId = uuid();
29
- function maybeAnswer(msg) {
30
- if (msg.properties.correlationId === correlationId) {
31
- console.log(' [.] Got %d', msg.content.toString());
32
- }
33
- else return bail(new Error('Unexpected message'), conn);
34
- ch.close(function() { conn.close(); });
35
- }
36
-
37
- ch.assertQueue('', {exclusive: true}, function(err, ok) {
38
- if (err !== null) return bail(err, conn);
39
- var queue = ok.queue;
40
- ch.consume(queue, maybeAnswer, {noAck:true});
41
- console.log(' [x] Requesting fib(%d)', n);
42
- ch.sendToQueue('rpc_queue', Buffer.from(n.toString()), {
43
- replyTo: queue, correlationId: correlationId
22
+ const correlationId = uuid();
23
+ channel.consume(replyTo, (message) => {
24
+ if (!message) console.warn(' [x] Consumer cancelled');
25
+ else if (message.properties.correlationId === correlationId) {
26
+ console.log(' [.] Got %d', message.content.toString());
27
+ channel.close(() => {
28
+ connection.close();
29
+ })
30
+ }
31
+ }, { noAck: true });
32
+
33
+ channel.assertQueue(queue, { durable: false }, (err) => {
34
+ if (err) return bail(err, connection);
35
+ console.log(' [x] Requesting fib(%d)', n);
36
+ channel.sendToQueue(queue, Buffer.from(n.toString()), {
37
+ correlationId,
38
+ replyTo
39
+ });
44
40
  });
45
41
  });
46
42
  });
43
+ });
44
+
45
+ function bail(err, connection) {
46
+ console.error(err);
47
+ if (connection) connection.close(() => {
48
+ process.exit(1);
49
+ });
47
50
  }
48
51
 
49
- amqp.connect(on_connect);
@@ -1,45 +1,55 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
+
5
+ const queue = 'rpc_queue';
6
+
7
+ amqp.connect((err, connection) => {
8
+ if (err) return bail(err);
9
+ connection.createChannel((err, channel) => {
10
+ if (err) return bail(err, connection);
11
+
12
+ process.once('SIGINT', () => {
13
+ channel.close(() => {
14
+ connection.close();
15
+ });
16
+ });
17
+
18
+ channel.assertQueue(queue, { durable: false }, (err) => {
19
+ if (err) return bail(err, connection);
20
+ channel.prefetch(1);
21
+ channel.consume(queue, (message) => {
22
+ const n = parseInt(message.content.toString(), 10);
23
+ console.log(' [.] fib(%d)', n);
24
+ const response = fib(n);
25
+ channel.sendToQueue(message.properties.replyTo, Buffer.from(response.toString()), {
26
+ correlationId: message.properties.correlationId
27
+ });
28
+ channel.ack(message);
29
+ }, { noAck: false }, function(err) {
30
+ if (err) return bail(err, conn);
31
+ console.log(' [x] Awaiting RPC requests. To exit press CTRL+C.');
32
+ });
33
+ });
34
+ });
35
+ });
4
36
 
5
37
  function fib(n) {
6
- var a = 0, b = 1;
7
- for (var i=0; i < n; i++) {
8
- var c = a + b;
38
+ // Do it the ridiculous, but not most ridiculous, way. For better,
39
+ // see http://nayuki.eigenstate.org/page/fast-fibonacci-algorithms
40
+ let a = 0, b = 1;
41
+ for (let i=0; i < n; i++) {
42
+ let c = a + b;
9
43
  a = b; b = c;
10
44
  }
11
45
  return a;
12
46
  }
13
47
 
14
- function bail(err, conn) {
15
- console.error(err);
16
- if (conn) conn.close(function() { process.exit(1); });
17
- }
18
-
19
- function on_connect(err, conn) {
20
- if (err !== null) return bail(err);
21
-
22
- process.once('SIGINT', function() { conn.close(); });
23
-
24
- var q = 'rpc_queue';
25
48
 
26
- conn.createChannel(function(err, ch) {
27
- ch.assertQueue(q, {durable: false});
28
- ch.prefetch(1);
29
- ch.consume(q, reply, {noAck:false}, function(err) {
30
- if (err !== null) return bail(err, conn);
31
- console.log(' [x] Awaiting RPC requests');
32
- });
33
-
34
- function reply(msg) {
35
- var n = parseInt(msg.content.toString());
36
- console.log(' [.] fib(%d)', n);
37
- ch.sendToQueue(msg.properties.replyTo,
38
- Buffer.from(fib(n).toString()),
39
- {correlationId: msg.properties.correlationId});
40
- ch.ack(msg);
41
- }
49
+ function bail(err, connection) {
50
+ console.error(err);
51
+ if (connection) connection.close(() => {
52
+ process.exit(1);
42
53
  });
43
54
  }
44
55
 
45
- amqp.connect(on_connect);
@@ -1,29 +1,28 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib/callback_api');
3
+ const amqp = require('amqplib/callback_api');
4
4
 
5
- function bail(err, conn) {
6
- console.error(err);
7
- if (conn) conn.close(function() { process.exit(1); });
8
- }
9
-
10
- function on_connect(err, conn) {
11
- if (err !== null) return bail(err);
5
+ const queue = 'hello';
6
+ const text = 'Hello World!';
12
7
 
13
- var q = 'hello';
14
- var msg = 'Hello World!';
15
-
16
- function on_channel_open(err, ch) {
17
- if (err !== null) return bail(err, conn);
18
- ch.assertQueue(q, {durable: false}, function(err, ok) {
19
- if (err !== null) return bail(err, conn);
20
- ch.sendToQueue(q, Buffer.from(msg));
21
- console.log(" [x] Sent '%s'", msg);
22
- ch.close(function() { conn.close(); });
8
+ amqp.connect((err, connection) => {
9
+ if (err) return bail(err);
10
+ connection.createChannel((err, channel) => {
11
+ if (err) return bail(err, connection);
12
+ channel.assertQueue(queue, { durable: false }, (err) => {
13
+ if (err) return bail(err, connection);
14
+ channel.sendToQueue(queue, Buffer.from(text));
15
+ console.log(" [x] Sent '%s'", text);
16
+ channel.close(() => {
17
+ connection.close();
18
+ });
23
19
  });
24
- }
20
+ });
21
+ });
25
22
 
26
- conn.createChannel(on_channel_open);
23
+ function bail(err, connection) {
24
+ console.error(err);
25
+ if (connection) connection.close(() => {
26
+ process.exit(1);
27
+ });
27
28
  }
28
-
29
- amqp.connect(on_connect);