amqplib 0.10.8 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/.claude/commands/lint-disable.md +9 -0
  2. package/.claude/commands/lint-enable.md +12 -0
  3. package/.claude/settings.local.json +16 -0
  4. package/.github/workflows/publish.yml +1 -1
  5. package/.github/workflows/test.yml +1 -1
  6. package/CHANGELOG.md +34 -0
  7. package/CLAUDE.md +12 -0
  8. package/Makefile +5 -12
  9. package/bin/generate-defs.js +382 -369
  10. package/biome.json +51 -0
  11. package/callback_api.js +12 -8
  12. package/channel_api.js +5 -10
  13. package/examples/direct_reply_to_client.js +9 -5
  14. package/examples/direct_reply_to_server.js +8 -6
  15. package/examples/headers.js +13 -10
  16. package/examples/receive_generator.js +13 -20
  17. package/examples/send_generators.js +2 -9
  18. package/examples/ssl.js +2 -3
  19. package/examples/stream_queues/receive_stream.js +39 -36
  20. package/examples/stream_queues/send_stream.js +26 -29
  21. package/examples/tutorials/callback_api/emit_log.js +4 -3
  22. package/examples/tutorials/callback_api/emit_log_direct.js +5 -4
  23. package/examples/tutorials/callback_api/emit_log_topic.js +5 -4
  24. package/examples/tutorials/callback_api/new_task.js +5 -4
  25. package/examples/tutorials/callback_api/receive.js +16 -10
  26. package/examples/tutorials/callback_api/receive_logs.js +17 -11
  27. package/examples/tutorials/callback_api/receive_logs_direct.js +19 -13
  28. package/examples/tutorials/callback_api/receive_logs_topic.js +19 -14
  29. package/examples/tutorials/callback_api/rpc_client.js +20 -16
  30. package/examples/tutorials/callback_api/rpc_server.js +27 -21
  31. package/examples/tutorials/callback_api/send.js +4 -3
  32. package/examples/tutorials/callback_api/worker.js +18 -13
  33. package/examples/tutorials/emit_log.js +4 -6
  34. package/examples/tutorials/emit_log_direct.js +5 -7
  35. package/examples/tutorials/emit_log_topic.js +5 -7
  36. package/examples/tutorials/new_task.js +4 -6
  37. package/examples/tutorials/receive.js +8 -4
  38. package/examples/tutorials/receive_logs.js +10 -6
  39. package/examples/tutorials/receive_logs_direct.js +16 -10
  40. package/examples/tutorials/receive_logs_topic.js +16 -11
  41. package/examples/tutorials/rpc_client.js +17 -15
  42. package/examples/tutorials/rpc_server.js +9 -8
  43. package/examples/tutorials/send.js +4 -6
  44. package/examples/tutorials/worker.js +17 -14
  45. package/examples/waitForConfirms.js +2 -2
  46. package/lefthook.yml +14 -0
  47. package/lib/api_args.js +124 -149
  48. package/lib/bitset.js +28 -18
  49. package/lib/callback_model.js +134 -190
  50. package/lib/channel.js +175 -213
  51. package/lib/channel_model.js +41 -85
  52. package/lib/codec.js +257 -228
  53. package/lib/connect.js +74 -82
  54. package/lib/connection.js +204 -240
  55. package/lib/credentials.js +21 -29
  56. package/lib/defs.js +2559 -1927
  57. package/lib/error.js +5 -6
  58. package/lib/format.js +15 -33
  59. package/lib/frame.js +66 -66
  60. package/lib/heartbeat.js +17 -24
  61. package/lib/mux.js +32 -41
  62. package/package.json +8 -5
  63. package/test/bitset.test.js +65 -0
  64. package/test/callback_api.test.js +438 -0
  65. package/test/channel.test.js +592 -0
  66. package/test/channel_api.test.js +537 -0
  67. package/test/codec.test.js +172 -0
  68. package/test/connect.test.js +196 -0
  69. package/test/connection.test.js +330 -0
  70. package/test/data.test.js +33 -0
  71. package/test/frame.test.js +180 -0
  72. package/test/lib/data.js +249 -0
  73. package/test/lib/util.js +223 -0
  74. package/test/mux.test.js +215 -0
  75. package/test/bitset.js +0 -69
  76. package/test/callback_api.js +0 -379
  77. package/test/channel.js +0 -621
  78. package/test/channel_api.js +0 -606
  79. package/test/codec.js +0 -237
  80. package/test/connect.js +0 -197
  81. package/test/connection.js +0 -390
  82. package/test/data.js +0 -269
  83. package/test/frame.js +0 -191
  84. package/test/mux.js +0 -203
  85. package/test/util.js +0 -217
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const amqp = require('amqplib/callback_api');
4
- const { basename } = require('path');
4
+ const { basename } = require('node:path');
5
5
 
6
6
  const exchange = 'direct_logs';
7
7
  const severities = process.argv.slice(2);
@@ -25,16 +25,21 @@ amqp.connect((err, connection) => {
25
25
  if (err) return bail(err, connection);
26
26
  channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
27
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) => {
28
+ channel.consume(
29
+ queue,
30
+ (message) => {
31
+ if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
32
+ else console.warn(' [x] Consumer cancelled');
33
+ },
34
+ { noAck: true },
35
+ (err) => {
35
36
  if (err) return bail(err, connection);
36
- });
37
- });
37
+ console.log(' [*] Waiting for logs. To exit press CTRL+C.');
38
+ subscribeAll(channel, queue, severities, (err) => {
39
+ if (err) return bail(err, connection);
40
+ });
41
+ },
42
+ );
38
43
  });
39
44
  });
40
45
  });
@@ -51,7 +56,8 @@ function subscribeAll(channel, queue, bindingKeys, cb) {
51
56
 
52
57
  function bail(err, connection) {
53
58
  console.error(err);
54
- if (connection) connection.close(() => {
55
- process.exit(1);
56
- });
59
+ if (connection)
60
+ connection.close(() => {
61
+ process.exit(1);
62
+ });
57
63
  }
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const amqp = require('amqplib/callback_api');
4
- const { basename } = require('path');
4
+ const { basename } = require('node:path');
5
5
 
6
6
  const exchange = 'topic_logs';
7
7
  const severities = process.argv.slice(2);
@@ -25,16 +25,21 @@ amqp.connect((err, connection) => {
25
25
  if (err) return bail(err, connection);
26
26
  channel.assertQueue('', { exclusive: true }, (err, { queue }) => {
27
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) => {
28
+ channel.consume(
29
+ queue,
30
+ (message) => {
31
+ if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
32
+ else console.warn(' [x] Consumer cancelled');
33
+ },
34
+ { noAck: true },
35
+ (err) => {
35
36
  if (err) return bail(err, connection);
36
- });
37
- });
37
+ console.log(' [*] Waiting for logs. To exit press CTRL+C.');
38
+ subscribeAll(channel, queue, severities, (err) => {
39
+ if (err) return bail(err, connection);
40
+ });
41
+ },
42
+ );
38
43
  });
39
44
  });
40
45
  });
@@ -51,8 +56,8 @@ function subscribeAll(channel, queue, bindingKeys, cb) {
51
56
 
52
57
  function bail(err, connection) {
53
58
  console.error(err);
54
- if (connection) connection.close(() => {
55
- process.exit(1);
56
- });
59
+ if (connection)
60
+ connection.close(() => {
61
+ process.exit(1);
62
+ });
57
63
  }
58
-
@@ -1,13 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const amqp = require('amqplib/callback_api');
4
- const { basename } = require('path');
4
+ const { basename } = require('node:path');
5
5
  const { v4: uuid } = require('uuid');
6
6
 
7
7
  const queue = 'rpc_queue';
8
8
 
9
9
  const n = parseInt(process.argv[2], 10);
10
- if (isNaN(n)) {
10
+ if (Number.isNaN(n)) {
11
11
  console.warn('Usage: %s number', basename(process.argv[1]));
12
12
  process.exit(1);
13
13
  }
@@ -20,22 +20,26 @@ amqp.connect((err, connection) => {
20
20
  if (err) return bail(err, connection);
21
21
 
22
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 });
23
+ channel.consume(
24
+ replyTo,
25
+ (message) => {
26
+ if (!message) console.warn(' [x] Consumer cancelled');
27
+ else if (message.properties.correlationId === correlationId) {
28
+ console.log(' [.] Got %d', message.content.toString());
29
+ channel.close(() => {
30
+ connection.close();
31
+ });
32
+ }
33
+ },
34
+ { noAck: true },
35
+ );
32
36
 
33
37
  channel.assertQueue(queue, { durable: false }, (err) => {
34
38
  if (err) return bail(err, connection);
35
39
  console.log(' [x] Requesting fib(%d)', n);
36
40
  channel.sendToQueue(queue, Buffer.from(n.toString()), {
37
41
  correlationId,
38
- replyTo
42
+ replyTo,
39
43
  });
40
44
  });
41
45
  });
@@ -44,8 +48,8 @@ amqp.connect((err, connection) => {
44
48
 
45
49
  function bail(err, connection) {
46
50
  console.error(err);
47
- if (connection) connection.close(() => {
48
- process.exit(1);
49
- });
51
+ if (connection)
52
+ connection.close(() => {
53
+ process.exit(1);
54
+ });
50
55
  }
51
-
@@ -18,18 +18,23 @@ amqp.connect((err, connection) => {
18
18
  channel.assertQueue(queue, { durable: false }, (err) => {
19
19
  if (err) return bail(err, connection);
20
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
- });
21
+ channel.consume(
22
+ queue,
23
+ (message) => {
24
+ const n = parseInt(message.content.toString(), 10);
25
+ console.log(' [.] fib(%d)', n);
26
+ const response = fib(n);
27
+ channel.sendToQueue(message.properties.replyTo, Buffer.from(response.toString()), {
28
+ correlationId: message.properties.correlationId,
29
+ });
30
+ channel.ack(message);
31
+ },
32
+ { noAck: false },
33
+ (err) => {
34
+ if (err) return bail(err, conn);
35
+ console.log(' [x] Awaiting RPC requests. To exit press CTRL+C.');
36
+ },
37
+ );
33
38
  });
34
39
  });
35
40
  });
@@ -37,19 +42,20 @@ amqp.connect((err, connection) => {
37
42
  function fib(n) {
38
43
  // Do it the ridiculous, but not most ridiculous, way. For better,
39
44
  // 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;
43
- a = b; b = c;
45
+ let a = 0;
46
+ let b = 1;
47
+ for (let i = 0; i < n; i++) {
48
+ const c = a + b;
49
+ a = b;
50
+ b = c;
44
51
  }
45
52
  return a;
46
53
  }
47
54
 
48
-
49
55
  function bail(err, connection) {
50
56
  console.error(err);
51
- if (connection) connection.close(() => {
52
- process.exit(1);
53
- });
57
+ if (connection)
58
+ connection.close(() => {
59
+ process.exit(1);
60
+ });
54
61
  }
55
-
@@ -22,7 +22,8 @@ amqp.connect((err, connection) => {
22
22
 
23
23
  function bail(err, connection) {
24
24
  console.error(err);
25
- if (connection) connection.close(() => {
26
- process.exit(1);
27
- });
25
+ if (connection)
26
+ connection.close(() => {
27
+ process.exit(1);
28
+ });
28
29
  }
@@ -17,23 +17,28 @@ amqp.connect((err, connection) => {
17
17
 
18
18
  channel.assertQueue(queue, { durable: true }, (err, { queue }) => {
19
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 });
29
- console.log(" [*] Waiting for messages. To exit press CTRL+C");
20
+ channel.consume(
21
+ queue,
22
+ (message) => {
23
+ const text = message.content.toString();
24
+ console.log(" [x] Received '%s'", text);
25
+ const seconds = text.split('.').length - 1;
26
+ setTimeout(() => {
27
+ console.log(' [x] Done');
28
+ channel.ack(message);
29
+ }, seconds * 1000);
30
+ },
31
+ { noAck: false },
32
+ );
33
+ console.log(' [*] Waiting for messages. To exit press CTRL+C');
30
34
  });
31
35
  });
32
36
  });
33
37
 
34
38
  function bail(err, connection) {
35
39
  console.error(err);
36
- if (connection) connection.close(() => {
37
- process.exit(1);
38
- });
40
+ if (connection)
41
+ connection.close(() => {
42
+ process.exit(1);
43
+ });
39
44
  }
@@ -14,11 +14,9 @@ const text = process.argv.slice(2).join(' ') || 'info: Hello World!';
14
14
  channel.publish(exchange, '', Buffer.from(text));
15
15
  console.log(" [x] Sent '%s'", text);
16
16
  await channel.close();
17
- }
18
- catch (err) {
17
+ } catch (err) {
19
18
  console.warn(err);
20
- }
21
- finally {
19
+ } finally {
22
20
  if (connection) await connection.close();
23
- };
24
- })();
21
+ }
22
+ })();
@@ -4,7 +4,7 @@ const amqp = require('amqplib');
4
4
 
5
5
  const exchange = 'direct_logs';
6
6
  const args = process.argv.slice(2);
7
- const routingKey = (args.length > 0) ? args[0] : 'info';
7
+ const routingKey = args.length > 0 ? args[0] : 'info';
8
8
  const text = args.slice(1).join(' ') || 'Hello World!';
9
9
 
10
10
  (async () => {
@@ -16,11 +16,9 @@ const text = args.slice(1).join(' ') || 'Hello World!';
16
16
  channel.publish(exchange, routingKey, Buffer.from(text));
17
17
  console.log(" [x] Sent %s:'%s'", routingKey, text);
18
18
  await channel.close();
19
- }
20
- catch (err) {
19
+ } catch (err) {
21
20
  console.warn(err);
22
- }
23
- finally {
21
+ } finally {
24
22
  if (connection) await connection.close();
25
- };
26
- })();
23
+ }
24
+ })();
@@ -4,7 +4,7 @@ const amqp = require('amqplib');
4
4
 
5
5
  const exchange = 'topic_logs';
6
6
  const args = process.argv.slice(2);
7
- const routingKeys = (args.length > 0) ? args[0] : 'info';
7
+ const routingKeys = args.length > 0 ? args[0] : 'info';
8
8
  const text = args.slice(1).join(' ') || 'Hello World!';
9
9
 
10
10
  (async () => {
@@ -16,11 +16,9 @@ const text = args.slice(1).join(' ') || 'Hello World!';
16
16
  channel.publish(exchange, routingKeys, Buffer.from(text));
17
17
  console.log(" [x] Sent %s:'%s'", routingKeys, text);
18
18
  await channel.close();
19
- }
20
- catch (err) {
19
+ } catch (err) {
21
20
  console.warn(err);
22
- }
23
- finally {
21
+ } finally {
24
22
  if (connection) await connection.close();
25
- };
26
- })();
23
+ }
24
+ })();
@@ -4,7 +4,7 @@
4
4
  const amqp = require('amqplib');
5
5
 
6
6
  const queue = 'task_queue';
7
- const text = process.argv.slice(2).join(' ') || "Hello World!";
7
+ const text = process.argv.slice(2).join(' ') || 'Hello World!';
8
8
 
9
9
  (async () => {
10
10
  let connection;
@@ -15,11 +15,9 @@ const text = process.argv.slice(2).join(' ') || "Hello World!";
15
15
  channel.sendToQueue(queue, Buffer.from(text), { persistent: true });
16
16
  console.log(" [x] Sent '%s'", text);
17
17
  await channel.close();
18
- }
19
- catch (err) {
18
+ } catch (err) {
20
19
  console.warn(err);
21
- }
22
- finally {
20
+ } finally {
23
21
  await connection.close();
24
- };
22
+ }
25
23
  })();
@@ -9,15 +9,19 @@ const queue = 'hello';
9
9
  const connection = await amqp.connect('amqp://localhost');
10
10
  const channel = await connection.createChannel();
11
11
 
12
- process.once('SIGINT', async () => {
12
+ process.once('SIGINT', async () => {
13
13
  await channel.close();
14
14
  await connection.close();
15
15
  });
16
16
 
17
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 });
18
+ await channel.consume(
19
+ queue,
20
+ (message) => {
21
+ console.log(" [x] Received '%s'", message.content.toString());
22
+ },
23
+ { noAck: true },
24
+ );
21
25
 
22
26
  console.log(' [*] Waiting for messages. To exit press CTRL+C');
23
27
  } catch (err) {
@@ -9,19 +9,23 @@ const exchange = 'logs';
9
9
  const connection = await amqp.connect('amqp://localhost');
10
10
  const channel = await connection.createChannel();
11
11
 
12
- process.once('SIGINT', async () => {
12
+ process.once('SIGINT', async () => {
13
13
  await channel.close();
14
14
  await connection.close();
15
15
  });
16
16
 
17
17
  await channel.assertExchange(exchange, 'fanout', { durable: false });
18
18
  const { queue } = await channel.assertQueue('', { exclusive: true });
19
- await channel.bindQueue(queue, exchange, '')
19
+ await channel.bindQueue(queue, exchange, '');
20
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 });
21
+ await channel.consume(
22
+ queue,
23
+ (message) => {
24
+ if (message) console.log(" [x] '%s'", message.content.toString());
25
+ else console.warn(' [x] Consumer cancelled');
26
+ },
27
+ { noAck: true },
28
+ );
25
29
 
26
30
  console.log(' [*] Waiting for logs. To exit press CTRL+C');
27
31
  } catch (err) {
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const amqp = require('../..');
4
- const { basename } = require('path');
4
+ const { basename } = require('node:path');
5
5
 
6
6
  const exchange = 'direct_logs';
7
7
  const bindingKeys = process.argv.slice(2);
@@ -15,24 +15,30 @@ if (bindingKeys.length < 1) {
15
15
  const connection = await amqp.connect('amqp://localhost');
16
16
  const channel = await connection.createChannel();
17
17
 
18
- process.once('SIGINT', async () => {
18
+ process.once('SIGINT', async () => {
19
19
  await channel.close();
20
20
  await connection.close();
21
21
  });
22
22
 
23
23
  await channel.assertExchange(exchange, 'direct', { durable: false });
24
24
  const { queue } = await channel.assertQueue('', { exclusive: true });
25
- await Promise.all(bindingKeys.map(async (bindingKey) => {
26
- await channel.bindQueue(queue, exchange, bindingKey);
27
- }));
25
+ await Promise.all(
26
+ bindingKeys.map(async (bindingKey) => {
27
+ await channel.bindQueue(queue, exchange, bindingKey);
28
+ }),
29
+ );
28
30
 
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 });
31
+ await channel.consume(
32
+ queue,
33
+ (message) => {
34
+ if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
35
+ else console.warn(' [x] Consumer cancelled');
36
+ },
37
+ { noAck: true },
38
+ );
33
39
 
34
40
  console.log(' [*] Waiting for logs. To exit press CTRL+C.');
35
- } catch(err) {
41
+ } catch (err) {
36
42
  console.warn(err);
37
43
  }
38
44
  })();
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const amqp = require('../..');
4
- const { basename } = require('path');
4
+ const { basename } = require('node:path');
5
5
 
6
6
  const exchange = 'topic_logs';
7
7
  const bindingKeys = process.argv.slice(2);
@@ -15,25 +15,30 @@ if (bindingKeys.length < 1) {
15
15
  const connection = await amqp.connect('amqp://localhost');
16
16
  const channel = await connection.createChannel();
17
17
 
18
- process.once('SIGINT', async () => {
18
+ process.once('SIGINT', async () => {
19
19
  await channel.close();
20
20
  await connection.close();
21
21
  });
22
22
 
23
23
  await channel.assertExchange(exchange, 'topic', { durable: false });
24
24
  const { queue } = await channel.assertQueue('', { exclusive: true });
25
- await Promise.all(bindingKeys.map(async (bindingKey) => {
26
- await channel.bindQueue(queue, exchange, bindingKey);
27
- }));
25
+ await Promise.all(
26
+ bindingKeys.map(async (bindingKey) => {
27
+ await channel.bindQueue(queue, exchange, bindingKey);
28
+ }),
29
+ );
28
30
 
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 });
31
+ await channel.consume(
32
+ queue,
33
+ (message) => {
34
+ if (message) console.log(" [x] %s:'%s'", message.fields.routingKey, message.content.toString());
35
+ else console.warn(' [x] Consumer cancelled');
36
+ },
37
+ { noAck: true },
38
+ );
33
39
 
34
40
  console.log(' [*] Waiting for logs. To exit press CTRL+C.');
35
- }
36
- catch (err) {
41
+ } catch (err) {
37
42
  console.warn(err);
38
43
  }
39
44
  })();
@@ -1,13 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const amqp = require('amqplib');
4
- const { basename } = require('path');
4
+ const { basename } = require('node:path');
5
5
  const { v4: uuid } = require('uuid');
6
6
 
7
7
  const queue = 'rpc_queue';
8
8
 
9
9
  const n = parseInt(process.argv[2], 10);
10
- if (isNaN(n)) {
10
+ if (Number.isNaN(n)) {
11
11
  console.warn('Usage: %s number', basename(process.argv[1]));
12
12
  process.exit(1);
13
13
  }
@@ -22,16 +22,20 @@ if (isNaN(n)) {
22
22
  const requestFib = new Promise(async (resolve) => {
23
23
  const { queue: replyTo } = await channel.assertQueue('', { exclusive: true });
24
24
 
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 });
25
+ await channel.consume(
26
+ replyTo,
27
+ (message) => {
28
+ if (!message) console.warn(' [x] Consumer cancelled');
29
+ else if (message.properties.correlationId === correlationId) {
30
+ resolve(message.content.toString());
31
+ }
32
+ },
33
+ { noAck: true },
34
+ );
31
35
 
32
36
  await channel.assertQueue(queue, { durable: false });
33
37
  console.log(' [x] Requesting fib(%d)', n);
34
- channel.sendToQueue(queue, Buffer.from(n.toString()), {
38
+ channel.sendToQueue(queue, Buffer.from(n.toString()), {
35
39
  correlationId,
36
40
  replyTo,
37
41
  });
@@ -39,11 +43,9 @@ if (isNaN(n)) {
39
43
 
40
44
  const fibN = await requestFib;
41
45
  console.log(' [.] Got %d', fibN);
42
- }
43
- catch (err) {
46
+ } catch (err) {
44
47
  console.warn(err);
45
- }
46
- finally {
48
+ } finally {
47
49
  if (connection) await connection.close();
48
- };
49
- })();
50
+ }
51
+ })();
@@ -9,7 +9,7 @@ const queue = 'rpc_queue';
9
9
  const connection = await amqp.connect('amqp://localhost');
10
10
  const channel = await connection.createChannel();
11
11
 
12
- process.once('SIGINT', async () => {
12
+ process.once('SIGINT', async () => {
13
13
  await channel.close();
14
14
  await connection.close();
15
15
  });
@@ -22,14 +22,13 @@ const queue = 'rpc_queue';
22
22
  console.log(' [.] fib(%d)', n);
23
23
  const response = fib(n);
24
24
  channel.sendToQueue(message.properties.replyTo, Buffer.from(response.toString()), {
25
- correlationId: message.properties.correlationId
25
+ correlationId: message.properties.correlationId,
26
26
  });
27
27
  channel.ack(message);
28
28
  });
29
29
 
30
30
  console.log(' [x] Awaiting RPC requests. To exit press CTRL+C.');
31
- }
32
- catch (err) {
31
+ } catch (err) {
33
32
  console.warn(err);
34
33
  }
35
34
  })();
@@ -37,10 +36,12 @@ const queue = 'rpc_queue';
37
36
  function fib(n) {
38
37
  // Do it the ridiculous, but not most ridiculous, way. For better,
39
38
  // 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;
43
- a = b; b = c;
39
+ let a = 0;
40
+ let b = 1;
41
+ for (let i = 0; i < n; i++) {
42
+ const c = a + b;
43
+ a = b;
44
+ b = c;
44
45
  }
45
46
  return a;
46
47
  }
@@ -21,11 +21,9 @@ const text = 'Hello World!';
21
21
  channel.sendToQueue(queue, Buffer.from(text));
22
22
  console.log(" [x] Sent '%s'", text);
23
23
  await channel.close();
24
- }
25
- catch (err) {
24
+ } catch (err) {
26
25
  console.warn(err);
27
- }
28
- finally {
26
+ } finally {
29
27
  if (connection) await connection.close();
30
- };
31
- })();
28
+ }
29
+ })();