Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from NGPVAN/devops/DO-5053-allow-additional-pr…
Browse files Browse the repository at this point in the history
…operties-on-connection

DO-5053 allow additional properties on connection
  • Loading branch information
ojintoad authored Aug 24, 2020
2 parents 8f14e80 + 378c6eb commit f2c6921
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ if (sent.ok) {
}
```

#### Expanded options

amqp.channel supports two parameters meant to modify connection options passed on to amqplib.

The first is an object called `socketOptions` is the equivalent to the same named property on the
[channel#connect method](http://www.squaremobius.net/amqp.node/channel_api.html#connect).

The second is called `defaultServernameToHostname`. This property is meant to signal that the servername property on the
socketoptions should be defaulted to the hostname from the url passed in. This is meant to simplify correctly configuring
connections over TLS for SNI. See the following for more details:

https://github.com/nodejs/node/issues/28167#issuecomment-500779815
https://github.com/squaremo/amqp.node/issues/331


## Real World Example

Say you wanted to listen to the `'foo'` exchange and send a different message to the `'bar'` queue every time the message's `baz` property contained the word `'qux'`.
Expand Down
17 changes: 14 additions & 3 deletions channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ var noop = function(){},
util = require('util'),
amqp = require('amqplib'),
Promise = require('bluebird'),
simplify = require('./simplify');
simplify = require('./simplify'),
uriJs = require('uri-js');

module.exports = function createChannel(url, assertions, log){
module.exports = function createChannel(url, assertions, log, socketOptions, defaultServernameToHostname){
assertions = assertions || {};
log = log || { info: noop, warn: noop, error: noop };
socketOptions = socketOptions || {};

return amqp.connect(url).then(openChannel);
//SNI for TLS requires the servername be specified, see:
//https://github.com/nodejs/node/issues/28167#issuecomment-500779815
//https://github.com/squaremo/amqp.node/issues/331
//This implements the default parsing everyone expects without breaking the api
if (defaultServernameToHostname) {
var host = uriJs.parse(url).host;
socketOptions.servername = host;
}

return amqp.connect(url, socketOptions).then(openChannel);

function openChannel(connection) {
var amqp = require('url').parse(url);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "channel.js",
"dependencies": {
"amqplib": "^0.3.2",
"bluebird": "^2.9.24"
"bluebird": "^2.9.24",
"uri-js": "^4.2.2"
},
"devDependencies": {
"chai": "^2.2.0",
Expand All @@ -21,7 +22,7 @@
"sinon-chai": "^2.7.0"
},
"scripts": {
"test": "mocha test",
"test": "node ./node_modules/mocha/bin/mocha test",
"test-cov": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"coverage": "istanbul cover _mocha test --recursive -- -u exports -R spec && open coverage/lcov-report/amqp.channel/index.html"
},
Expand Down
4 changes: 2 additions & 2 deletions test/test.channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('Channel', function() {

it('should connect to the correct RabbitMQ URL', function(){
expect(connect).to.have.been.calledOnce
.and.to.have.been.calledWithExactly(amqpUrl);
.and.to.have.been.calledWithExactly(amqpUrl, {});
});

it('should create a Channel on the Connection', function(){
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Channel', function() {

it('should connect to the correct RabbitMQ URL', function(){
expect(connect).to.have.been.calledOnce
.and.to.have.been.calledWithExactly(amqpUrl);
.and.to.have.been.calledWithExactly(amqpUrl, {});
});

it('should create a Channel on the Connection', function(){
Expand Down

0 comments on commit f2c6921

Please sign in to comment.