amqplib 0.10.3 → 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 (54) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +7 -4
  2. package/.github/workflows/test.yml +15 -2
  3. package/CHANGELOG.md +6 -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/heartbeat.js +32 -29
  49. package/lib/mux.js +96 -99
  50. package/package.json +1 -1
  51. package/test/callback_api.js +182 -174
  52. package/test/channel_api.js +455 -445
  53. package/test/codec.js +4 -4
  54. package/test/frame.js +5 -5
@@ -1,35 +1,39 @@
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';
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 = 'task_queue';
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
+ });
15
17
 
16
- conn.createChannel(function(err, ch) {
17
- if (err !== null) return bail(err, conn);
18
- ch.assertQueue(q, {durable: true}, function(err, _ok) {
19
- ch.consume(q, doWork, {noAck: false});
18
+ channel.assertQueue(queue, { durable: true }, (err, { queue }) => {
19
+ if (err) return bail(err, connection);
20
+ channel.consume(queue, (message) => {
21
+ const text = message.content.toString();
22
+ console.log(" [x] Received '%s'", text);
23
+ const seconds = text.split('.').length - 1;
24
+ setTimeout(() => {
25
+ console.log(" [x] Done");
26
+ channel.ack(message);
27
+ }, seconds * 1000);
28
+ }, { noAck: false });
20
29
  console.log(" [*] Waiting for messages. To exit press CTRL+C");
21
30
  });
31
+ });
32
+ });
22
33
 
23
- function doWork(msg) {
24
- var body = msg.content.toString();
25
- console.log(" [x] Received '%s'", body);
26
- var secs = body.split('.').length - 1;
27
- setTimeout(function() {
28
- console.log(" [x] Done");
29
- ch.ack(msg);
30
- }, secs * 1000);
31
- }
34
+ function bail(err, connection) {
35
+ console.error(err);
36
+ if (connection) connection.close(() => {
37
+ process.exit(1);
32
38
  });
33
39
  }
34
-
35
- amqp.connect(on_connect);
@@ -1,19 +1,24 @@
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 ex = 'logs';
8
- var ok = ch.assertExchange(ex, 'fanout', {durable: false})
5
+ const exchange = 'logs';
6
+ const text = process.argv.slice(2).join(' ') || 'info: Hello World!';
9
7
 
10
- var message = process.argv.slice(2).join(' ') ||
11
- 'info: Hello World!';
12
-
13
- return ok.then(function() {
14
- ch.publish(ex, '', Buffer.from(message));
15
- console.log(" [x] Sent '%s'", message);
16
- return ch.close();
17
- });
18
- }).finally(function() { conn.close(); });
19
- }).catch(console.warn);
8
+ (async () => {
9
+ let connection;
10
+ try {
11
+ connection = await amqp.connect('amqp://localhost');
12
+ const channel = await connection.createChannel();
13
+ await channel.assertExchange(exchange, 'fanout', { durable: false });
14
+ channel.publish(exchange, '', Buffer.from(text));
15
+ console.log(" [x] Sent '%s'", text);
16
+ await channel.close();
17
+ }
18
+ catch (err) {
19
+ console.warn(err);
20
+ }
21
+ finally {
22
+ if (connection) await connection.close();
23
+ };
24
+ })();
@@ -1,20 +1,26 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib');
3
+ const amqp = require('amqplib');
4
4
 
5
- var args = process.argv.slice(2);
6
- var severity = (args.length > 0) ? args[0] : 'info';
7
- var message = args.slice(1).join(' ') || 'Hello World!';
5
+ const exchange = 'direct_logs';
6
+ const args = process.argv.slice(2);
7
+ const routingKey = (args.length > 0) ? args[0] : 'info';
8
+ const text = args.slice(1).join(' ') || 'Hello World!';
8
9
 
9
- amqp.connect('amqp://localhost').then(function(conn) {
10
- return conn.createChannel().then(function(ch) {
11
- var ex = 'direct_logs';
12
- var ok = ch.assertExchange(ex, 'direct', {durable: false});
13
-
14
- return ok.then(function() {
15
- ch.publish(ex, severity, Buffer.from(message));
16
- console.log(" [x] Sent %s:'%s'", severity, message);
17
- return ch.close();
18
- });
19
- }).finally(function() { conn.close(); });
20
- }).catch(console.warn);
10
+ (async () => {
11
+ let connection;
12
+ try {
13
+ connection = await amqp.connect('amqp://localhost');
14
+ const channel = await connection.createChannel();
15
+ await channel.assertExchange(exchange, 'direct', { durable: false });
16
+ channel.publish(exchange, routingKey, Buffer.from(text));
17
+ console.log(" [x] Sent %s:'%s'", routingKey, text);
18
+ await channel.close();
19
+ }
20
+ catch (err) {
21
+ console.warn(err);
22
+ }
23
+ finally {
24
+ if (connection) await connection.close();
25
+ };
26
+ })();
@@ -1,19 +1,26 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- var amqp = require('amqplib');
3
+ const amqp = require('amqplib');
4
4
 
5
- var args = process.argv.slice(2);
6
- var key = (args.length > 0) ? args[0] : 'info';
7
- var message = args.slice(1).join(' ') || 'Hello World!';
5
+ const exchange = 'topic_logs';
6
+ const args = process.argv.slice(2);
7
+ const routingKeys = (args.length > 0) ? args[0] : 'info';
8
+ const text = args.slice(1).join(' ') || 'Hello World!';
8
9
 
9
- amqp.connect('amqp://localhost').then(function(conn) {
10
- return conn.createChannel().then(function(ch) {
11
- var ex = 'topic_logs';
12
- var ok = ch.assertExchange(ex, 'topic', {durable: false});
13
- return ok.then(function() {
14
- ch.publish(ex, key, Buffer.from(message));
15
- console.log(" [x] Sent %s:'%s'", key, message);
16
- return ch.close();
17
- });
18
- }).finally(function() { conn.close(); })
19
- }).catch(console.log);
10
+ (async () => {
11
+ let connection;
12
+ try {
13
+ connection = await amqp.connect('amqp://localhost');
14
+ const channel = await connection.createChannel();
15
+ await channel.assertExchange(exchange, 'topic', { durable: false });
16
+ channel.publish(exchange, routingKeys, Buffer.from(text));
17
+ console.log(" [x] Sent %s:'%s'", routingKeys, text);
18
+ await channel.close();
19
+ }
20
+ catch (err) {
21
+ console.warn(err);
22
+ }
23
+ finally {
24
+ if (connection) await connection.close();
25
+ };
26
+ })();
@@ -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);