amqplib 1.0.0 → 1.0.2
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 +6 -0
- package/README.md +0 -5
- package/lib/connect.js +21 -9
- package/package.json +2 -3
- package/test/connect.test.js +0 -41
- package/test/uri_spec.test.js +210 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Change log for amqplib
|
|
2
2
|
|
|
3
|
+
## v1.0.2
|
|
4
|
+
- Replace url-parse with WHATWG URL API
|
|
5
|
+
|
|
6
|
+
## v1.0.1
|
|
7
|
+
- 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+
|
|
8
|
+
|
|
3
9
|
## v1.0.0
|
|
4
10
|
- **BREAKING**: Update minimum Node.js requirement to v18
|
|
5
11
|
- Introduce Biome formatter for consistent code formatting
|
package/README.md
CHANGED
|
@@ -9,11 +9,6 @@ A library for making AMQP 0-9-1 clients for Node.JS, and an AMQP 0-9-1 client fo
|
|
|
9
9
|
|
|
10
10
|
npm install amqplib
|
|
11
11
|
|
|
12
|
-
## ❤️ Help Support Jack
|
|
13
|
-
One of my close friends, Jack, was recently diagnosed with terminal brain cancer (grade 4 astrocytoma). He’s a young father facing an unimaginably tough road with remarkable courage. Thanks to chemotherapy, radiotherapy, and your support, two of his tumours have stopped showing activity. Donations help Jack continue accessing promising complementary therapies, attend hospital appointments, and spend meaningful time with his children.
|
|
14
|
-
|
|
15
|
-
If you’ve benefited from amqplib, please consider supporting Jack’s journey through his [J Crushing Cancer](https://www.gofundme.com/f/j-crushing-cancer) gofundme page. Thank you - @cressie176
|
|
16
|
-
|
|
17
12
|
## RabbitMQ Compatibility
|
|
18
13
|
|
|
19
14
|
Only `0.10.7` and later versions of this library are compatible with RabbitMQ 4.1.0 (and later releases).
|
package/lib/connect.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
const URL = require('url
|
|
1
|
+
const { URL } = require('node:url');
|
|
2
2
|
const QS = require('node:querystring');
|
|
3
|
+
const net = require('node:net');
|
|
4
|
+
const tls = require('node:tls');
|
|
3
5
|
const Connection = require('./connection').Connection;
|
|
4
6
|
const fmt = require('node:util').format;
|
|
5
7
|
const credentials = require('./credentials');
|
|
@@ -98,7 +100,7 @@ function connect(url, socketOptions, openCallback) {
|
|
|
98
100
|
if (typeof url === 'object') {
|
|
99
101
|
protocol = `${url.protocol || 'amqp'}:`;
|
|
100
102
|
sockopts.host = url.hostname;
|
|
101
|
-
sockopts.servername
|
|
103
|
+
if (!sockopts.servername && !net.isIP(url.hostname)) sockopts.servername = url.hostname;
|
|
102
104
|
sockopts.port = url.port || (protocol === 'amqp:' ? 5672 : 5671);
|
|
103
105
|
|
|
104
106
|
let user;
|
|
@@ -122,14 +124,24 @@ function connect(url, socketOptions, openCallback) {
|
|
|
122
124
|
|
|
123
125
|
fields = openFrames(url.vhost, config, sockopts.credentials || credentials.plain(user, pass), extraClientProperties);
|
|
124
126
|
} else {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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 || '';
|
|
128
135
|
sockopts.host = host;
|
|
129
|
-
sockopts.servername
|
|
136
|
+
if (!sockopts.servername && !net.isIP(host)) sockopts.servername = host;
|
|
130
137
|
sockopts.port = parseInt(parts.port, 10) || (protocol === 'amqp:' ? 5672 : 5671);
|
|
131
138
|
const vhost = parts.pathname ? parts.pathname.substr(1) : null;
|
|
132
|
-
|
|
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);
|
|
133
145
|
}
|
|
134
146
|
|
|
135
147
|
let sockok = false;
|
|
@@ -157,9 +169,9 @@ function connect(url, socketOptions, openCallback) {
|
|
|
157
169
|
}
|
|
158
170
|
|
|
159
171
|
if (protocol === 'amqp:') {
|
|
160
|
-
sock =
|
|
172
|
+
sock = net.connect(sockopts, onConnect);
|
|
161
173
|
} else if (protocol === 'amqps:') {
|
|
162
|
-
sock =
|
|
174
|
+
sock = tls.connect(sockopts, onConnect);
|
|
163
175
|
} else {
|
|
164
176
|
throw new Error(`Expected amqp: or amqps: as the protocol; got ${protocol}`);
|
|
165
177
|
}
|
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.
|
|
5
|
+
"version": "1.0.2",
|
|
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",
|
package/test/connect.test.js
CHANGED
|
@@ -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;
|
|
@@ -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
|
+
});
|