amqplib 1.0.1 → 1.0.3

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change log for amqplib
2
2
 
3
+ ## v1.0.3
4
+ - Fix AssertionError crash when backpressure occurs while draining newStreams in Mux (fixes #841)
5
+
6
+ ## v1.0.2
7
+ - Replace url-parse with WHATWG URL API
8
+
3
9
  ## v1.0.1
4
10
  - Fix TLS connection failure when connecting to an AMQPS broker using an IP address (IPv4 or IPv6) — setting servername to an IP is not permitted by TLS SNI and throws ERR_INVALID_ARG_VALUE in Node.js 25+
5
11
 
package/lib/connect.js CHANGED
@@ -1,4 +1,4 @@
1
- const URL = require('url-parse');
1
+ const { URL } = require('node:url');
2
2
  const QS = require('node:querystring');
3
3
  const net = require('node:net');
4
4
  const tls = require('node:tls');
@@ -124,14 +124,24 @@ function connect(url, socketOptions, openCallback) {
124
124
 
125
125
  fields = openFrames(url.vhost, config, sockopts.credentials || credentials.plain(user, pass), extraClientProperties);
126
126
  } else {
127
- const parts = URL(url, true); // yes, parse the query string
128
- const host = parts.hostname.replace(/^\[|\]$/g, '');
129
- protocol = parts.protocol;
127
+ let parts;
128
+ try {
129
+ parts = new URL(url);
130
+ } catch (_e) {
131
+ parts = {};
132
+ }
133
+ const host = (parts.hostname || '').replace(/^\[|\]$/g, '');
134
+ protocol = parts.protocol || '';
130
135
  sockopts.host = host;
131
136
  if (!sockopts.servername && !net.isIP(host)) sockopts.servername = host;
132
137
  sockopts.port = parseInt(parts.port, 10) || (protocol === 'amqp:' ? 5672 : 5671);
133
138
  const vhost = parts.pathname ? parts.pathname.substr(1) : null;
134
- fields = openFrames(vhost, parts.query, sockopts.credentials || credentialsFromUrl(parts), extraClientProperties);
139
+ const query = QS.parse(parts.search ? parts.search.slice(1) : '');
140
+ // QS.parse returns arrays for duplicate keys; match url-parse's first-value-wins behavior.
141
+ for (const key in query) {
142
+ if (Array.isArray(query[key])) query[key] = query[key][0];
143
+ }
144
+ fields = openFrames(vhost, query, sockopts.credentials || credentialsFromUrl(parts), extraClientProperties);
135
145
  }
136
146
 
137
147
  let sockok = false;
package/lib/mux.js CHANGED
@@ -40,14 +40,13 @@ class Mux {
40
40
  // Try to read a chunk from each stream in turn, until all streams
41
41
  // are empty, or we exhaust our ability to accept chunks.
42
42
  function roundrobin(streams) {
43
- let stream = streams.shift();
44
- while (accepting && stream) {
43
+ while (accepting && streams.length > 0) {
44
+ const stream = streams.shift();
45
45
  const chunk = stream.read();
46
46
  if (chunk !== null) {
47
47
  accepting = out.write(chunk);
48
48
  streams.push(stream);
49
49
  }
50
- stream = streams.shift();
51
50
  }
52
51
  }
53
52
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "amqplib",
3
3
  "homepage": "http://amqp-node.github.io/amqplib/",
4
4
  "main": "./channel_api.js",
5
- "version": "1.0.1",
5
+ "version": "1.0.3",
6
6
  "description": "An AMQP 0-9-1 (e.g., RabbitMQ) library and client.",
7
7
  "repository": {
8
8
  "type": "git",
@@ -12,8 +12,7 @@
12
12
  "node": ">=18"
13
13
  },
14
14
  "dependencies": {
15
- "buffer-more-ints": "~1.0.0",
16
- "url-parse": "~1.5.10"
15
+ "buffer-more-ints": "~1.0.0"
17
16
  },
18
17
  "devDependencies": {
19
18
  "@biomejs/biome": "^2.2.2",
@@ -2,53 +2,12 @@ const { describe, it, afterEach } = require('node:test');
2
2
  const assert = require('node:assert');
3
3
  const net = require('node:net');
4
4
  const connect = require('../lib/connect').connect;
5
- const credentialsFromUrl = require('../lib/connect').credentialsFromUrl;
6
5
  const defs = require('../lib/defs');
7
6
  const { plain, amqplain } = require('../lib/credentials')
8
7
  const { latch, runServer } = require('./lib/util');
9
8
 
10
- const urlparse = require('url-parse');
11
-
12
9
  describe('Connect', () => {
13
10
 
14
- describe('Credentials', () => {
15
- function assertCredentials(creds, username, password) {
16
- assert.strictEqual(creds.mechanism, 'PLAIN');
17
- assert.strictEqual(creds.username, username);
18
- assert.strictEqual(creds.password, password);
19
- }
20
-
21
- it('no creds', () => {
22
- const parts = urlparse('amqp://localhost');
23
- const creds = credentialsFromUrl(parts);
24
- assertCredentials(creds, 'guest', 'guest');
25
- });
26
-
27
- it('usual user:pass', () => {
28
- const parts = urlparse('amqp://user:pass@localhost');
29
- const creds = credentialsFromUrl(parts);
30
- assertCredentials(creds, 'user', 'pass');
31
- });
32
-
33
- it('missing user', () => {
34
- const parts = urlparse('amqps://:password@localhost');
35
- const creds = credentialsFromUrl(parts);
36
- assertCredentials(creds, '', 'password');
37
- });
38
-
39
- it('missing password', () => {
40
- const parts = urlparse('amqps://username:@localhost');
41
- const creds = credentialsFromUrl(parts);
42
- assertCredentials(creds, 'username', '');
43
- });
44
-
45
- it('escaped colons', () => {
46
- const parts = urlparse('amqp://user%3Aname:pass%3Aword@localhost');
47
- const creds = credentialsFromUrl(parts);
48
- assertCredentials(creds, 'user:name', 'pass:word');
49
- });
50
- });
51
-
52
11
  describe('Connect API', () => {
53
12
 
54
13
  let connection;
package/test/mux.test.js CHANGED
@@ -184,6 +184,49 @@ describe('mux', () => {
184
184
  });
185
185
  });
186
186
 
187
+ it('backpressure does not crash when newStreams is drained', () => {
188
+ // Regression test: out.write() returns false (backpressure) while
189
+ // draining newStreams. The buggy code does an unconditional shift at the
190
+ // bottom of the loop, so if the next stream reads null (empty) it gets
191
+ // discarded without being pushed back, leaving newStreams empty and
192
+ // triggering assert(this.newStreams.length > 0) in the else-branch.
193
+ //
194
+ // Scenario: newStreams = [input1, input2], input1 has data, input2 empty.
195
+ // - shift input1, read 1, write → backpressure (false), push input1 back
196
+ // - (buggy) unconditional shift takes input2, loop condition false → exit
197
+ // - newStreams = [input1], length > 0, assert passes ← not the bug path
198
+ //
199
+ // The crash path requires backpressure AND the shifted stream reading null
200
+ // with no other streams remaining. Use a single stream that has data, then
201
+ // engineer a second read() on the same stream returning null after the
202
+ // write so the stream is not pushed back, emptying newStreams entirely.
203
+ const input1 = stream();
204
+ const output = stream();
205
+ const mux = new Mux(output);
206
+
207
+ mux.newStreams.push(input1);
208
+ input1.write(1);
209
+
210
+ // After the first write (which triggers backpressure), wrap read() so
211
+ // the stream appears empty on the next call — simulating a stream that
212
+ // had one item buffered and is now drained within the same roundrobin pass.
213
+ const originalRead = input1.read.bind(input1);
214
+ let readCount = 0;
215
+ input1.read = (size) => {
216
+ readCount++;
217
+ if (readCount > 1) return null; // appear empty after first read
218
+ return originalRead(size);
219
+ };
220
+
221
+ const originalWrite = output.write.bind(output);
222
+ output.write = (...args) => {
223
+ originalWrite(...args);
224
+ return false; // signal backpressure immediately
225
+ };
226
+
227
+ assert.doesNotThrow(() => mux._readIncoming());
228
+ });
229
+
187
230
  it('roundrobin', (_t, done) => {
188
231
  const input1 = stream();
189
232
  const input2 = stream();
@@ -0,0 +1,210 @@
1
+ const { describe, it } = require('node:test');
2
+ const assert = require('node:assert');
3
+ const net = require('node:net');
4
+ const connect = require('../lib/connect').connect;
5
+ const defs = require('../lib/defs');
6
+ const { runServer } = require('./lib/util');
7
+
8
+ function captureHandshake(url, callback) {
9
+ let server;
10
+ const captured = {};
11
+
12
+ server = net.createServer((socket) => {
13
+ socket.once('data', (_header) => {
14
+ runServer(socket, (send, wait) => {
15
+ send(defs.ConnectionStart, {
16
+ versionMajor: 0,
17
+ versionMinor: 9,
18
+ serverProperties: {},
19
+ mechanisms: Buffer.from('PLAIN'),
20
+ locales: Buffer.from('en_US'),
21
+ });
22
+ wait(defs.ConnectionStartOk)().then((startOk) => {
23
+ const parts = startOk.fields.response.toString().split('\0');
24
+ captured.username = parts[1];
25
+ captured.password = parts[2];
26
+ send(defs.ConnectionTune, { channelMax: 0, heartbeat: 0, frameMax: 0 });
27
+ return wait(defs.ConnectionTuneOk)();
28
+ }).then(() => {
29
+ return wait(defs.ConnectionOpen)();
30
+ }).then((open) => {
31
+ captured.vhost = open.fields.virtualHost;
32
+ send(defs.ConnectionClose, {
33
+ replyCode: 200,
34
+ replyText: 'ok',
35
+ classId: 0,
36
+ methodId: 0,
37
+ });
38
+ }).catch((err) => {
39
+ server.close();
40
+ callback(err);
41
+ });
42
+ });
43
+
44
+ socket.once('end', () => {
45
+ server.close(() => callback(null, captured));
46
+ });
47
+ });
48
+ });
49
+
50
+ server.listen(0, () => {
51
+ const port = server.address().port;
52
+ const urlWithPort = url.replace('TESTPORT', String(port));
53
+ connect(urlWithPort, {}, (_err) => {});
54
+ });
55
+ }
56
+
57
+ describe('AMQP URI spec', () => {
58
+
59
+ describe('credentials', () => {
60
+
61
+ it('no userinfo defaults to guest/guest', (_t, done) => {
62
+ captureHandshake('amqp://localhost:TESTPORT', (err, result) => {
63
+ assert.ifError(err);
64
+ assert.strictEqual(result.username, 'guest');
65
+ assert.strictEqual(result.password, 'guest');
66
+ done();
67
+ });
68
+ });
69
+
70
+ it('plain user:pass', (_t, done) => {
71
+ captureHandshake('amqp://user:pass@localhost:TESTPORT', (err, result) => {
72
+ assert.ifError(err);
73
+ assert.strictEqual(result.username, 'user');
74
+ assert.strictEqual(result.password, 'pass');
75
+ done();
76
+ });
77
+ });
78
+
79
+ it('empty username', (_t, done) => {
80
+ captureHandshake('amqp://:password@localhost:TESTPORT', (err, result) => {
81
+ assert.ifError(err);
82
+ assert.strictEqual(result.username, '');
83
+ assert.strictEqual(result.password, 'password');
84
+ done();
85
+ });
86
+ });
87
+
88
+ it('empty password', (_t, done) => {
89
+ captureHandshake('amqp://username:@localhost:TESTPORT', (err, result) => {
90
+ assert.ifError(err);
91
+ assert.strictEqual(result.username, 'username');
92
+ assert.strictEqual(result.password, '');
93
+ done();
94
+ });
95
+ });
96
+
97
+ // Issue #385: colon in username or password must be percent-encoded
98
+ it('percent-encoded colon (%3A) in username and password', (_t, done) => {
99
+ captureHandshake('amqp://user%3Aname:pass%3Aword@localhost:TESTPORT', (err, result) => {
100
+ assert.ifError(err);
101
+ assert.strictEqual(result.username, 'user:name');
102
+ assert.strictEqual(result.password, 'pass:word');
103
+ done();
104
+ });
105
+ });
106
+
107
+ // Issue #483: @ in password must be percent-encoded as %40
108
+ it('percent-encoded @ (%40) in password', (_t, done) => {
109
+ captureHandshake('amqp://user:p%40ssword@localhost:TESTPORT', (err, result) => {
110
+ assert.ifError(err);
111
+ assert.strictEqual(result.username, 'user');
112
+ assert.strictEqual(result.password, 'p@ssword');
113
+ done();
114
+ });
115
+ });
116
+
117
+ // RabbitMQ URI spec example: amqp://user:passw%23rd@host:10000/vhost
118
+ it('percent-encoded # (%23) in password', (_t, done) => {
119
+ captureHandshake('amqp://user:passw%23rd@localhost:TESTPORT', (err, result) => {
120
+ assert.ifError(err);
121
+ assert.strictEqual(result.username, 'user');
122
+ assert.strictEqual(result.password, 'passw#rd');
123
+ done();
124
+ });
125
+ });
126
+
127
+ it('percent-encoded % (%25) in password', (_t, done) => {
128
+ captureHandshake('amqp://user:pass%25word@localhost:TESTPORT', (err, result) => {
129
+ assert.ifError(err);
130
+ assert.strictEqual(result.username, 'user');
131
+ assert.strictEqual(result.password, 'pass%word');
132
+ done();
133
+ });
134
+ });
135
+
136
+ // RabbitMQ URI spec example: amqp://user%61:%61pass@ho%61st:10000/v%2fhost
137
+ it('percent-encoded letters (%61 = "a") in username and password', (_t, done) => {
138
+ captureHandshake('amqp://user%61:%61pass@localhost:TESTPORT', (err, result) => {
139
+ assert.ifError(err);
140
+ assert.strictEqual(result.username, 'usera');
141
+ assert.strictEqual(result.password, 'apass');
142
+ done();
143
+ });
144
+ });
145
+
146
+ });
147
+
148
+ describe('vhost', () => {
149
+
150
+ it('no path defaults to vhost /', (_t, done) => {
151
+ captureHandshake('amqp://localhost:TESTPORT', (err, result) => {
152
+ assert.ifError(err);
153
+ assert.strictEqual(result.vhost, '/');
154
+ done();
155
+ });
156
+ });
157
+
158
+ it('explicit / path gives vhost /', (_t, done) => {
159
+ captureHandshake('amqp://localhost:TESTPORT/', (err, result) => {
160
+ assert.ifError(err);
161
+ assert.strictEqual(result.vhost, '/');
162
+ done();
163
+ });
164
+ });
165
+
166
+ it('named vhost', (_t, done) => {
167
+ captureHandshake('amqp://localhost:TESTPORT/myvhost', (err, result) => {
168
+ assert.ifError(err);
169
+ assert.strictEqual(result.vhost, 'myvhost');
170
+ done();
171
+ });
172
+ });
173
+
174
+ // The RabbitMQ spec requires %2F to represent a literal / in a vhost
175
+ it('percent-encoded slash (%2F) in vhost decodes to /', (_t, done) => {
176
+ captureHandshake('amqp://localhost:TESTPORT/%2F', (err, result) => {
177
+ assert.ifError(err);
178
+ assert.strictEqual(result.vhost, '/');
179
+ done();
180
+ });
181
+ });
182
+
183
+ it('percent-encoded slash lowercase (%2f) in vhost decodes to /', (_t, done) => {
184
+ captureHandshake('amqp://localhost:TESTPORT/%2f', (err, result) => {
185
+ assert.ifError(err);
186
+ assert.strictEqual(result.vhost, '/');
187
+ done();
188
+ });
189
+ });
190
+
191
+ // RabbitMQ URI spec example: /v%2fhost => "v/host"
192
+ it('mixed percent-encoding in vhost (v%2Fhost decodes to "v/host")', (_t, done) => {
193
+ captureHandshake('amqp://localhost:TESTPORT/v%2Fhost', (err, result) => {
194
+ assert.ifError(err);
195
+ assert.strictEqual(result.vhost, 'v/host');
196
+ done();
197
+ });
198
+ });
199
+
200
+ it('percent-encoded space (%20) in vhost', (_t, done) => {
201
+ captureHandshake('amqp://localhost:TESTPORT/my%20vhost', (err, result) => {
202
+ assert.ifError(err);
203
+ assert.strictEqual(result.vhost, 'my vhost');
204
+ done();
205
+ });
206
+ });
207
+
208
+ });
209
+
210
+ });
@@ -1,16 +0,0 @@
1
- {
2
- "permissions": {
3
- "allow": [
4
- "Bash(npx oxlint:*)",
5
- "Bash(npm t)",
6
- "Bash(git add:*)",
7
- "Bash(git commit:*)",
8
- "Bash(npm run lint:*)",
9
- "Bash(git checkout:*)",
10
- "Bash(npx biome lint:*)",
11
- "Bash(node:*)"
12
- ],
13
- "deny": [],
14
- "ask": []
15
- }
16
- }