Skip to content

Commit

Permalink
feat(policy): move parseLinkAddress into Policy
Browse files Browse the repository at this point in the history
This allows users to customize the behavior of link address parsing
for their favorite broker implementation.

closes #267
  • Loading branch information
mbroadst committed Oct 20, 2016
1 parent 728af3e commit d1ae649
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
10 changes: 5 additions & 5 deletions lib/amqp_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ AMQPClient.prototype.createSender = function(address, policyOverrides) {
throw new Error('Must connect before creating links');
}

address = u.parseLinkAddress(address || this._defaultQueue, this.policy);
address = this.policy.parseLinkAddress(address || this._defaultQueue);
policyOverrides = policyOverrides || {};

var linkName = u.linkName(address.name, policyOverrides),
Expand Down Expand Up @@ -255,7 +255,7 @@ AMQPClient.prototype.createReceiver = function(address, policyOverrides) {
throw new Error('Must connect before creating links');
}

address = u.parseLinkAddress(address || this._defaultQueue, this.policy);
address = this.policy.parseLinkAddress(address || this._defaultQueue);
policyOverrides = policyOverrides || {};

var linkName = u.linkName(address.name, policyOverrides),
Expand All @@ -267,9 +267,9 @@ AMQPClient.prototype.createReceiver = function(address, policyOverrides) {
}
}, policyOverrides, this.policy.receiverLink);

// if the policy supports the defaultSubjects feature, and a subject has been
// provided then automatically set up a filter to match on that subject.
if (this.policy.defaultSubjects && !!address.subject) {
// if a subject has been provided then automatically set up a filter to
// match on that subject.
if (!!address.subject) {
if (address.subject === 'undefined' || address.subject === 'null') {
throw new errors.InvalidSubjectError(address.subject);
}
Expand Down
22 changes: 22 additions & 0 deletions lib/policies/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ function Policy(overrides) {
return Object.seal(this);
}

/**
* Parses a link address used for creating Sender and Receiver links.
*
* The resulting object has a required `name` property (used as the source
* address in the attach performative), as well as an optional `subject` property
* which (if specified) will automatically create a source filter.
*
* @param {String} the address to parse
* @return {Object}
*/
Policy.prototype.parseLinkAddress = function(address) {
// @todo: this "parsing" should be far more rigorous
if (!this.defaultSubjects) {
return { name: address };
}

var parts = address.split('/');
var result = { name: parts.shift() };
if (parts.length) result.subject = parts.shift();
return result;
};

/**
* Parses an address for use when connecting to an AMQP 1.0 broker
*
Expand Down
12 changes: 0 additions & 12 deletions lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,6 @@ function bufferEquals(lhs, rhs, offset1, offset2, size) {

utilities.bufferEquals = bufferEquals;

// @todo: this "parsing" should be far more rigorous
utilities.parseLinkAddress = function(address, policy) {
if (policy && !policy.defaultSubjects) {
return { name: address };
}

var parts = address.split('/');
var result = { name: parts.shift() };
if (parts.length) result.subject = parts.shift();
return result;
};

function deepMerge() {
var args = Array.prototype.slice.call(arguments);
var helper = function(tgt, src, key) {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/address.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ describe('Address Parsing', function() {
});

it('should support links with names prefixed with `topic://` and `queue://`', function() {
var address = u.parseLinkAddress('topic://mytopic', ActiveMQPolicy);
var policy = ActiveMQPolicy;
var address = policy.parseLinkAddress('topic://mytopic');
expect(address.name).to.eql('topic://mytopic');
var linkName = u.linkName(address.name, {});
var nonUniqueLinkNamePart = linkName.split('_')[0];
Expand Down
6 changes: 4 additions & 2 deletions test/unit/test_utilities.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var expect = require('chai').expect,
var Policy = require('../../lib/policies/policy'),
expect = require('chai').expect,
u = require('../../lib/utilities'),
tu = require('./../testing_utils');

Expand Down Expand Up @@ -157,7 +158,8 @@ describe('Utilities', function() {
}
].forEach(function(testCase) {
it('should match ' + testCase.description, function() {
var result = u.parseLinkAddress(testCase.address);
var policy = new Policy();
var result = policy.parseLinkAddress(testCase.address);
expect(result).to.eql(testCase.expected);
});
});
Expand Down

0 comments on commit d1ae649

Please sign in to comment.