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 1 commit
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log for amqplib

## Unreleased

* Replace url-parse lib with native URL interface

## Changes in v0.8.0

git log v0.7.1..v0.8.0
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
Loading