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
package/lib/connect.js CHANGED
@@ -1,22 +1,14 @@
1
- //
2
- //
3
- //
4
-
5
- // General-purpose API for glueing everything together.
6
-
7
- 'use strict';
8
-
9
- var URL = require('url-parse');
10
- var QS = require('querystring');
11
- var Connection = require('./connection').Connection;
12
- var fmt = require('util').format;
13
- var credentials = require('./credentials');
1
+ const URL = require('url-parse');
2
+ const QS = require('node:querystring');
3
+ const Connection = require('./connection').Connection;
4
+ const fmt = require('node:util').format;
5
+ const credentials = require('./credentials');
14
6
 
15
7
  function copyInto(obj, target) {
16
- var keys = Object.keys(obj);
17
- var i = keys.length;
8
+ const keys = Object.keys(obj);
9
+ let i = keys.length;
18
10
  while (i--) {
19
- var k = keys[i];
11
+ const k = keys[i];
20
12
  target[k] = obj[k];
21
13
  }
22
14
  return target;
@@ -27,61 +19,60 @@ function clone(obj) {
27
19
  return copyInto(obj, {});
28
20
  }
29
21
 
30
- var CLIENT_PROPERTIES = {
31
- "product": "amqplib",
32
- "version": require('../package.json').version,
33
- "platform": fmt('Node.JS %s', process.version),
34
- "information": "https://amqp-node.github.io/amqplib/",
35
- "capabilities": {
36
- "publisher_confirms": true,
37
- "exchange_exchange_bindings": true,
38
- "basic.nack": true,
39
- "consumer_cancel_notify": true,
40
- "connection.blocked": true,
41
- "authentication_failure_close": true
42
- }
22
+ const CLIENT_PROPERTIES = {
23
+ product: 'amqplib',
24
+ version: require('../package.json').version,
25
+ platform: fmt('Node.JS %s', process.version),
26
+ information: 'https://amqp-node.github.io/amqplib/',
27
+ capabilities: {
28
+ publisher_confirms: true,
29
+ exchange_exchange_bindings: true,
30
+ 'basic.nack': true,
31
+ consumer_cancel_notify: true,
32
+ 'connection.blocked': true,
33
+ authentication_failure_close: true,
34
+ },
43
35
  };
44
36
 
45
37
  // Construct the main frames used in the opening handshake
46
38
  function openFrames(vhost, query, credentials, extraClientProperties) {
47
- if (!vhost)
48
- vhost = '/';
49
- else
50
- vhost = QS.unescape(vhost);
39
+ if (!vhost) vhost = '/';
40
+ else vhost = QS.unescape(vhost);
51
41
 
52
- var query = query || {};
42
+ query = query || {};
53
43
 
54
44
  function intOrDefault(val, def) {
55
- return (val === undefined) ? def : parseInt(val);
45
+ return val === undefined ? def : parseInt(val, 10);
56
46
  }
57
47
 
58
- var clientProperties = Object.create(CLIENT_PROPERTIES);
48
+ const clientProperties = Object.create(CLIENT_PROPERTIES);
59
49
 
60
50
  return {
61
51
  // start-ok
62
- 'clientProperties': copyInto(extraClientProperties, clientProperties),
63
- 'mechanism': credentials.mechanism,
64
- 'response': credentials.response(),
65
- 'locale': query.locale || 'en_US',
52
+ clientProperties: copyInto(extraClientProperties, clientProperties),
53
+ mechanism: credentials.mechanism,
54
+ response: credentials.response(),
55
+ locale: query.locale || 'en_US',
66
56
 
67
57
  // tune-ok
68
- 'channelMax': intOrDefault(query.channelMax, 0),
69
- 'frameMax': intOrDefault(query.frameMax, 131072),
70
- 'heartbeat': intOrDefault(query.heartbeat, 0),
58
+ channelMax: intOrDefault(query.channelMax, 0),
59
+ frameMax: intOrDefault(query.frameMax, 131072),
60
+ heartbeat: intOrDefault(query.heartbeat, 0),
71
61
 
72
62
  // open
73
- 'virtualHost': vhost,
74
- 'capabilities': '',
75
- 'insist': 0
63
+ virtualHost: vhost,
64
+ capabilities: '',
65
+ insist: 0,
76
66
  };
77
67
  }
78
68
 
79
69
  // Decide on credentials based on what we're supplied.
80
70
  function credentialsFromUrl(parts) {
81
- var user = 'guest', passwd = 'guest';
82
- if (parts.username != '' || parts.password != '') {
83
- user = (parts.username) ? unescape(parts.username) : '';
84
- passwd = (parts.password) ? unescape(parts.password) : '';
71
+ let user = 'guest';
72
+ let passwd = 'guest';
73
+ if (parts.username !== '' || parts.password !== '') {
74
+ user = parts.username ? unescape(parts.username) : '';
75
+ passwd = parts.password ? unescape(parts.password) : '';
85
76
  }
86
77
  return credentials.plain(user, passwd);
87
78
  }
@@ -91,35 +82,38 @@ function connect(url, socketOptions, openCallback) {
91
82
  // copies only properties mentioned in `Object.keys()`, when
92
83
  // processing the options. So I have to make copies too, rather
93
84
  // than using `Object.create()`.
94
- var sockopts = clone(socketOptions || {});
85
+ const sockopts = clone(socketOptions || {});
95
86
  url = url || 'amqp://localhost';
96
87
 
97
- var noDelay = !!sockopts.noDelay;
98
- var timeout = sockopts.timeout;
99
- var keepAlive = !!sockopts.keepAlive;
88
+ const noDelay = !!sockopts.noDelay;
89
+ const timeout = sockopts.timeout;
90
+ const keepAlive = !!sockopts.keepAlive;
100
91
  // 0 is default for node
101
- var keepAliveDelay = sockopts.keepAliveDelay || 0;
92
+ const keepAliveDelay = sockopts.keepAliveDelay || 0;
102
93
 
103
- var extraClientProperties = sockopts.clientProperties || {};
94
+ const extraClientProperties = sockopts.clientProperties || {};
104
95
 
105
- var protocol, fields;
96
+ let protocol;
97
+ let fields;
106
98
  if (typeof url === 'object') {
107
- protocol = (url.protocol || 'amqp') + ':';
99
+ protocol = `${url.protocol || 'amqp'}:`;
108
100
  sockopts.host = url.hostname;
109
101
  sockopts.servername = sockopts.servername || url.hostname;
110
- sockopts.port = url.port || ((protocol === 'amqp:') ? 5672 : 5671);
102
+ sockopts.port = url.port || (protocol === 'amqp:' ? 5672 : 5671);
111
103
 
112
- var user, pass;
104
+ let user;
105
+ let pass;
113
106
  // Only default if both are missing, to have the same behaviour as
114
107
  // the stringly URL.
115
- if (url.username == undefined && url.password == undefined) {
116
- user = 'guest'; pass = 'guest';
108
+ if (url.username === undefined && url.password === undefined) {
109
+ user = 'guest';
110
+ pass = 'guest';
117
111
  } else {
118
112
  user = url.username || '';
119
113
  pass = url.password || '';
120
114
  }
121
115
 
122
- var config = {
116
+ const config = {
123
117
  locale: url.locale,
124
118
  channelMax: url.channelMax,
125
119
  frameMax: url.frameMax,
@@ -128,25 +122,26 @@ function connect(url, socketOptions, openCallback) {
128
122
 
129
123
  fields = openFrames(url.vhost, config, sockopts.credentials || credentials.plain(user, pass), extraClientProperties);
130
124
  } else {
131
- var parts = URL(url, true); // yes, parse the query string
125
+ const parts = URL(url, true); // yes, parse the query string
126
+ const host = parts.hostname.replace(/^\[|\]$/g, '');
132
127
  protocol = parts.protocol;
133
- sockopts.host = parts.hostname;
134
- sockopts.servername = sockopts.servername || parts.hostname;
135
- sockopts.port = parseInt(parts.port) || ((protocol === 'amqp:') ? 5672 : 5671);
136
- var vhost = parts.pathname ? parts.pathname.substr(1) : null;
128
+ sockopts.host = host;
129
+ sockopts.servername = sockopts.servername || host;
130
+ sockopts.port = parseInt(parts.port, 10) || (protocol === 'amqp:' ? 5672 : 5671);
131
+ const vhost = parts.pathname ? parts.pathname.substr(1) : null;
137
132
  fields = openFrames(vhost, parts.query, sockopts.credentials || credentialsFromUrl(parts), extraClientProperties);
138
133
  }
139
134
 
140
- var sockok = false;
141
- var sock;
135
+ let sockok = false;
136
+ let sock;
142
137
 
143
138
  function onConnect() {
144
139
  sockok = true;
145
140
  sock.setNoDelay(noDelay);
146
141
  if (keepAlive) sock.setKeepAlive(keepAlive, keepAliveDelay);
147
142
 
148
- var c = new Connection(sock);
149
- c.open(fields, function(err, ok) {
143
+ const c = new Connection(sock);
144
+ c.open(fields, (err, _ok) => {
150
145
  // disable timeout once the connection is open, we don't want
151
146
  // it fouling things
152
147
  if (timeout) sock.setTimeout(0);
@@ -162,27 +157,24 @@ function connect(url, socketOptions, openCallback) {
162
157
  }
163
158
 
164
159
  if (protocol === 'amqp:') {
165
- sock = require('net').connect(sockopts, onConnect);
166
- }
167
- else if (protocol === 'amqps:') {
168
- sock = require('tls').connect(sockopts, onConnect);
169
- }
170
- else {
171
- throw new Error("Expected amqp: or amqps: as the protocol; got " + protocol);
160
+ sock = require('node:net').connect(sockopts, onConnect);
161
+ } else if (protocol === 'amqps:') {
162
+ sock = require('node:tls').connect(sockopts, onConnect);
163
+ } else {
164
+ throw new Error(`Expected amqp: or amqps: as the protocol; got ${protocol}`);
172
165
  }
173
166
 
174
167
  if (timeout) {
175
- sock.setTimeout(timeout, function() {
168
+ sock.setTimeout(timeout, () => {
176
169
  sock.end();
177
170
  sock.destroy();
178
171
  openCallback(new Error('connect ETIMEDOUT'));
179
172
  });
180
173
  }
181
174
 
182
- sock.once('error', function(err) {
175
+ sock.once('error', (err) => {
183
176
  if (!sockok) openCallback(err);
184
177
  });
185
-
186
178
  }
187
179
 
188
180
  module.exports.connect = connect;