Skip to content

Commit

Permalink
Merge pull request #23 from radiofrance/opts-protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
zckrs authored Apr 5, 2022
2 parents cbff76c + f79b93b commit 7dd4384
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ Default: `guest`

Password associate to username.

##### protocol

Type: `amqp|amqps`<br>
Default: `amqp`

Connection protocol.

##### hostname

Type: `string`<br>
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class RabQ extends EventEmitter {

validateOptions(opts);

this.protocol = opts.protocol || 'amqp';
this.hostname = opts.hostname || 'localhost';
this.port = opts.port || 5672;
this.username = opts.username || 'guest';
Expand Down Expand Up @@ -64,10 +65,11 @@ class RabQ extends EventEmitter {
level: 'info',
uuid: null,
token: null,
msg: `Connecting to RabbitMQ ... (amqp://${this.hostname}:${this.port}/${encodeURIComponent(this.vhost)})`
msg: `Connecting to RabbitMQ ... (${this.protocol}://${this.hostname}:${this.port}/${encodeURIComponent(this.vhost)})`
});

const settings = {
protocol: this.protocol,
hostname: this.hostname,
port: this.port,
username: this.username,
Expand Down
2 changes: 1 addition & 1 deletion lib/get-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const amqp = require('amqplib');

module.exports = settings => {
const uri = 'amqp://' + encodeURIComponent(settings.username) + ':' + encodeURIComponent(settings.password) + '@' + settings.hostname + ':' + settings.port + '/' + encodeURIComponent(settings.vhost);
const uri = settings.protocol + '://' + encodeURIComponent(settings.username) + ':' + encodeURIComponent(settings.password) + '@' + settings.hostname + ':' + settings.port + '/' + encodeURIComponent(settings.vhost);

return amqp.connect(uri)
.then(conn => conn.createConfirmChannel()
Expand Down
3 changes: 3 additions & 0 deletions lib/validate-options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

module.exports = (opts = {}) => {
if (opts.protocol && opts.protocol !== 'amqp' && opts.protocol !== 'amqps') {
throw new Error(`opts.protocol should be 'amqp' or 'amqps'. Currently "${typeof opts.protocol}".`);
}
if (opts.hostname && typeof opts.hostname !== 'string') {
throw new Error(`opts.hostname should be a string. Currently "${typeof opts.hostname}".`);
}
Expand Down
1 change: 1 addition & 0 deletions test/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"protocol": "amqp",
"username": "guest",
"password": "guest",
"hostname": "127.0.0.1",
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test('constructor with default options', t => {
t.notThrows(() => {
const p = new RabQ(minimalOptions);
t.deepEqual({
protocol: p.protocol,
username: p.username,
password: p.password,
hostname: p.hostname,
Expand Down

0 comments on commit 7dd4384

Please sign in to comment.