Skip to content

Commit

Permalink
Merge pull request #61 from lamchakchan/master
Browse files Browse the repository at this point in the history
Refactored logic around payload transform
  • Loading branch information
Lam Chan authored Mar 4, 2017
2 parents 7a271f7 + e931e81 commit aaf9783
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
getPackageData : require('./getPackageData'),
isMajorCompatible : require('./isMajorCompatible'),
cleanObject : require('./cleanObject'),
parsePayload : require('./parsePayload'),
reduceRouteKey : require('./reduceRouteKey'),
reduceCallback : require('./reduceCallback'),
validateLoggerContract : require('./validateLoggerContract'),
Expand Down
13 changes: 13 additions & 0 deletions lib/helpers/parsePayload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const parsePayload = (payload) => {

return {
message : payload.properties.headers.isBuffer ? payload.content : JSON.parse(payload.content.toString()),
metaData : {
headers : Object.assign({}, payload.properties.headers)
}
};
};

module.exports = parsePayload;
15 changes: 5 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ class BunnyBus extends EventEmitter{

$.logger.trace(payload);

const message = payload.properties.headers.isBuffer ? payload.content : JSON.parse(payload.content.toString());
const routeKey = Helpers.reduceRouteKey(payload, null, message);
const parsedPayload = Helpers.parsePayload(payload);
const routeKey = Helpers.reduceRouteKey(payload, null, parsedPayload.message);
const currentRetryCount = payload.properties.headers.retryCount || -1;
const errorQueue = `${queue}_error`;

Expand All @@ -491,22 +491,17 @@ class BunnyBus extends EventEmitter{
}
else if (currentRetryCount < maxRetryCount) {
if (meta) {
//TODO: put this into a helper function as a refactor
const metadata = {
headers : Object.assign({}, payload.properties.headers)
};

handlers[routeKey](
message,
metadata,
parsedPayload.message,
parsedPayload.metaData,
$._ack.bind(null, payload),
$._reject.bind(null, payload, errorQueue),
$._requeue.bind(null, payload, queue, { routeKey })
);
}
else {
handlers[routeKey](
message,
parsedPayload.message,
$._ack.bind(null, payload),
$._reject.bind(null, payload, errorQueue),
$._requeue.bind(null, payload, queue)
Expand Down
2 changes: 1 addition & 1 deletion test/integration-edge-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('positive integration tests - Callback api', () => {
});
});

it.only('should pass when get pushes a message to a subscribed queue', (done) => {
it('should pass when get pushes a message to a subscribed queue', (done) => {

const message = { event : 'ea', name : 'bunnybus' };
const queueName = 'edge-case-get-to-subscribe';
Expand Down

0 comments on commit aaf9783

Please sign in to comment.