Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace url-parse lib with native URL interface #678

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change log for amqplib

## Unreleased
* Replace url-parse lib with native URL interface

## Chagnes in v0.10.0
* Use Native promises ([PR
689](https://github.com/amqp-node/amqplib/pull/689), thank you
Expand Down
32 changes: 12 additions & 20 deletions lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

'use strict';

var URL = require('url-parse');
var QS = require('querystring');
var Connection = require('./connection').Connection;
var fmt = require('util').format;
Expand Down Expand Up @@ -43,16 +42,16 @@ var CLIENT_PROPERTIES = {
};

// Construct the main frames used in the opening handshake
function openFrames(vhost, query, credentials, extraClientProperties) {
function openFrames(vhost, searchParams, credentials, extraClientProperties) {
if (!vhost)
vhost = '/';
else
vhost = QS.unescape(vhost);

var query = query || {};
var searchParams = searchParams || new URLSearchParams();

function intOrDefault(val, def) {
return (val === undefined) ? def : parseInt(val);
return (val === null) ? def : parseInt(val);
}

var clientProperties = Object.create(CLIENT_PROPERTIES);
Expand All @@ -62,12 +61,12 @@ function openFrames(vhost, query, credentials, extraClientProperties) {
'clientProperties': copyInto(extraClientProperties, clientProperties),
'mechanism': credentials.mechanism,
'response': credentials.response(),
'locale': query.locale || 'en_US',
'locale': searchParams.get('locale') || 'en_US',

// tune-ok
'channelMax': intOrDefault(query.channelMax, 0),
'frameMax': intOrDefault(query.frameMax, 0x1000),
'heartbeat': intOrDefault(query.heartbeat, 0),
'channelMax': intOrDefault(searchParams.get('channelMax'), 0),
'frameMax': intOrDefault(searchParams.get('frameMax'), 0x1000),
'heartbeat': intOrDefault(searchParams.get('heartbeat'), 0),

// open
'virtualHost': vhost,
Expand Down Expand Up @@ -104,37 +103,30 @@ function connect(url, socketOptions, openCallback) {

var protocol, fields;
if (typeof url === 'object') {
protocol = (url.protocol || 'amqp') + ':';
protocol = url.protocol || 'amqp:';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

protocol no longer gets : appended to it, is it intentional?

sockopts.host = url.hostname;
sockopts.servername = url.hostname;
sockopts.port = url.port || ((protocol === 'amqp:') ? 5672 : 5671);

var user, pass;
// Only default if both are missing, to have the same behaviour as
// the stringly URL.
if (url.username == undefined && url.password == undefined) {
if (!url.username && !url.password) {
user = 'guest'; pass = 'guest';
} else {
user = url.username || '';
pass = url.password || '';
}

var config = {
locale: url.locale,
channelMax: url.channelMax,
frameMax: url.frameMax,
heartbeat: url.heartbeat,
};

fields = openFrames(url.vhost, config, sockopts.credentials || credentials.plain(user, pass), extraClientProperties);
fields = openFrames(url.vhost, url.searchParams, sockopts.credentials || credentials.plain(user, pass), extraClientProperties);
Copy link
Author

@jonaswalden jonaswalden Apr 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CAUTION (maybe) vhost used here is not a part of a URL instance.
Then again it wasn't a part of the legacy url.parse / url-parse result either so this was undefined anyway. The only place I found where it wasn't undefined was in the bad URL test.

Copy link
Collaborator

@kibertoad kibertoad Jan 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sounds like a bug. can we fix it? vhost is important
or you mean that there is no way to include vhost in url?
amqp://${config.username}:${config.password}@${config.hostname}:${config.port}/${config.vhost} is a commonly used structure for amqp urls, and, to my knowledge, currently it works.

} else {
var parts = URL(url, true); // yes, parse the query string
var parts = new URL(url);
protocol = parts.protocol;
sockopts.host = parts.hostname;
sockopts.servername = parts.hostname;
sockopts.port = parseInt(parts.port) || ((protocol === 'amqp:') ? 5672 : 5671);
var vhost = parts.pathname ? parts.pathname.substr(1) : null;
fields = openFrames(vhost, parts.query, sockopts.credentials || credentialsFromUrl(parts), extraClientProperties);
fields = openFrames(vhost, parts.searchParams, sockopts.credentials || credentialsFromUrl(parts), extraClientProperties);
}

var sockok = false;
Expand Down
43 changes: 2 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"dependencies": {
"bitsyntax": "~0.1.0",
"buffer-more-ints": "~1.0.0",
"readable-stream": "1.x >=1.1.9",
"url-parse": "~1.5.10"
"readable-stream": "1.x >=1.1.9"
},
"devDependencies": {
"claire": "0.4.1",
Expand Down
143 changes: 66 additions & 77 deletions test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ var defs = require('../lib/defs');
var assert = require('assert');
var util = require('./util');
var net = require('net');
var parseUrl = require('url').parse;
var fail = util.fail, succeed = util.succeed, latch = util.latch,
kCallback = util.kCallback,
succeedIfAttributeEquals = util.succeedIfAttributeEquals;
var format = require('util').format;

var URL = process.env.URL || 'amqp://localhost';
var baseURL = process.env.URL || 'amqp://localhost';

var urlparse = require('url-parse');

suite("Credentials", function() {

Expand All @@ -29,27 +29,27 @@ suite("Credentials", function() {
}

test("no creds", function(done) {
var parts = urlparse('amqp://localhost');
var parts = new URL('amqp://localhost');
var creds = credentialsFromUrl(parts);
checkCreds(creds, 'guest', 'guest', done);
});
test("usual user:pass", function(done) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add test case for the vhost, using the
amqp://${config.username}:${config.password}@${config.hostname}:${config.port}/${config.vhost} structure?

var parts = urlparse('amqp://user:pass@localhost')
var parts = new URL('amqp://user:pass@localhost')
var creds = credentialsFromUrl(parts);
checkCreds(creds, 'user', 'pass', done);
});
test("missing user", function(done) {
var parts = urlparse('amqps://:password@localhost');
var parts = new URL('amqps://:password@localhost');
var creds = credentialsFromUrl(parts);
checkCreds(creds, '', 'password', done);
});
test("missing password", function(done) {
var parts = urlparse('amqps://username:@localhost');
var parts = new URL('amqps://username:@localhost');
var creds = credentialsFromUrl(parts);
checkCreds(creds, 'username', '', done);
});
test("escaped colons", function(done) {
var parts = urlparse('amqp://user%3Aname:pass%3Aword@localhost')
var parts = new URL('amqp://user%3Aname:pass%3Aword@localhost')
var creds = credentialsFromUrl(parts);
checkCreds(creds, 'user:name', 'pass:word', done);
});
Expand All @@ -69,83 +69,72 @@ suite("Connect API", function() {
});
});

test("wrongly typed open option", function(done) {
var url = require('url');
var parts = url.parse(URL, true);
var q = parts.query || {};
q.frameMax = 'NOT A NUMBER';
parts.query = q;
var u = url.format(parts);
connect(u, {}, kCallback(fail(done), succeed(done)));
});

test("serverProperties", function(done) {
var url = require('url');
var parts = url.parse(URL, true);
var config = parts.query || {};
connect(config, {}, function(err, connection) {
if (err) { return done(err); }
assert.equal(connection.serverProperties.product, 'RabbitMQ');
done();
});
});
[
["As instance of URL", url => url ],
["As string", url => url.href ],
].forEach(([suiteDescription, processURL]) => {
suite(suiteDescription, () => {
test("wrongly typed open option", function(done) {
var url = new URL(baseURL);
url.searchParams.set('frameMax', 'NOT A NUMBER');
connect(processURL(url), {}, kCallback(fail(done), succeed(done)));
});

test("using custom heartbeat option", function(done) {
var url = require('url');
var parts = url.parse(URL, true);
var config = parts.query || {};
config.heartbeat = 20;
connect(config, {}, kCallback(succeedIfAttributeEquals('heartbeat', 20, done), fail(done)));
});
test("serverProperties", function(done) {
var url = new URL(baseURL);
connect(processURL(url), {}, function(err, connection) {
if (err) { return done(err); }
assert.equal(connection.serverProperties.product, 'RabbitMQ');
done();
});
});

test("wrongly typed heartbeat option", function(done) {
var url = require('url');
var parts = url.parse(URL, true);
var config = parts.query || {};
config.heartbeat = 'NOT A NUMBER';
connect(config, {}, kCallback(fail(done), succeed(done)));
});
test("using custom heartbeat option", function(done) {
var url = new URL(baseURL);
url.searchParams.set('heartbeat', 20);
connect(processURL(url), {}, kCallback(succeedIfAttributeEquals('heartbeat', 20, done), fail(done)));
});

test("using plain credentials", function(done) {
var url = require('url');
var parts = url.parse(URL, true);
var u = 'guest', p = 'guest';
if (parts.auth) {
var auth = parts.auth.split(":");
u = auth[0], p = auth[1];
}
connect(URL, {credentials: require('../lib/credentials').plain(u, p)},
kCallback(succeed(done), fail(done)));
});
test("wrongly typed heartbeat option", function(done) {
var url = new URL(baseURL);
url.searchParams.set('heartbeat', 'NOT A NUMBER');
connect(processURL(url), {}, kCallback(fail(done), succeed(done)));
});

test("using amqplain credentials", function(done) {
var url = require('url');
var parts = url.parse(URL, true);
var u = 'guest', p = 'guest';
if (parts.auth) {
var auth = parts.auth.split(":");
u = auth[0], p = auth[1];
}
connect(URL, {credentials: require('../lib/credentials').amqplain(u, p)},
kCallback(succeed(done), fail(done)));
});
test("using plain credentials", function(done) {
var url = new URL(baseURL);
var u = url.username || 'guest';
var p = url.password || 'guest';
connect(processURL(url), {credentials: require('../lib/credentials').plain(u, p)},
kCallback(succeed(done), fail(done)));
});

test("using unsupported mechanism", function(done) {
var creds = {
mechanism: 'UNSUPPORTED',
response: function() { return Buffer.from(''); }
};
connect(URL, {credentials: creds},
kCallback(fail(done), succeed(done)));
});
test("using amqplain credentials", function(done) {
var url = new URL(baseURL);
var u = url.username || 'guest';
var p = url.password || 'guest';
connect(processURL(url), {credentials: require('../lib/credentials').amqplain(u, p)},
kCallback(succeed(done), fail(done)));
});

test("with a given connection timeout", function(done) {
var timeoutServer = net.createServer(function() {}).listen(31991);
test("using unsupported mechanism", function(done) {
var creds = {
mechanism: 'UNSUPPORTED',
response: function() { return Buffer.from(''); }
};
connect(processURL(baseURL), {credentials: creds},
kCallback(fail(done), succeed(done)));
});

connect('amqp://localhost:31991', {timeout: 50}, function(err, val) {
timeoutServer.close();
if (val) done(new Error('Expected connection timeout, did not'));
else done();
test("with a given connection timeout", function(done) {
var timeoutServer = net.createServer(function() {}).listen(31991);
var url = new URL('amqp://localhost:31991');
connect(processURL(url), {timeout: 50}, function(err, val) {
timeoutServer.close();
if (val) done(new Error('Expected connection timeout, did not'));
else done();
});
});
});
});
});
Expand Down