-
Notifications
You must be signed in to change notification settings - Fork 476
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
|
@@ -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, | ||
|
@@ -104,37 +103,30 @@ function connect(url, socketOptions, openCallback) { | |
|
||
var protocol, fields; | ||
if (typeof url === 'object') { | ||
protocol = (url.protocol || 'amqp') + ':'; | ||
protocol = url.protocol || 'amqp:'; | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CAUTION (maybe) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this sounds like a bug. can we fix it? |
||
} 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; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
|
||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add test case for the vhost, using the |
||
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); | ||
}); | ||
|
@@ -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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
There was a problem hiding this comment.
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?