Skip to content

Commit

Permalink
Merge pull request postwait#102 from jamescarr/array-support
Browse files Browse the repository at this point in the history
Constant Extraction
  • Loading branch information
postwait committed Jun 4, 2012
2 parents 05e42ae + 8337a80 commit 0573df5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
37 changes: 20 additions & 17 deletions amqp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ var events = require('events'),
jspack = require('./jspack').jspack,
Buffer = require('buffer').Buffer,
Promise = require('./promise').Promise,
URL = require('url');

URL = require('url'),
AMQPTypes = require('./constants').AMQPTypes,
Indicators = require('./constants').Indicators,
FrameType = require('./constants').FrameType;

function mixin () {
// copy reference to target object
var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, source;
Expand Down Expand Up @@ -181,20 +184,20 @@ function AMQPParser (version, type) {

function frameEnd(data) {
if (data.length > 0) {
if (data[0] === 206) {
if (data[0] === Indicators.FRAME_END) {
switch (frameType) {
case 1:
case FrameType.METHOD:
self._parseMethodFrame(frameChannel, frameBuffer);
break;
case 2:
case FrameType.HEADER:
self._parseHeaderFrame(frameChannel, frameBuffer);
break;
case 3:
case FrameType.BODY:
if (self.onContent) {
self.onContent(frameChannel, frameBuffer);
}
break;
case 8:
case FrameType.HEARTBEAT:
debug("hearbeat");
if (self.onHeartBeat) self.onHeartBeat();
break;
Expand Down Expand Up @@ -290,55 +293,55 @@ function parseTable (buffer) {
while (buffer.read < length) {
var field = parseShortString(buffer);
switch (buffer[buffer.read++]) {
case 'S'.charCodeAt(0):
case AMQPTypes.STRING:
table[field] = parseLongString(buffer);
break;

case 'I'.charCodeAt(0):
case AMQPTypes.INTEGER:
table[field] = parseInt(buffer, 4);
break;

case 'D'.charCodeAt(0):
case AMQPTypes.DECIMAL:
var dec = parseInt(buffer, 1);
var num = parseInt(buffer, 4);
table[field] = num / (dec * 10);
break;

case 'd'.charCodeAt(0):
case AMQPTypes._64BIT_FLOAT:
var b = [];
for (var i = 0; i < 8; ++i)
b[i] = buffer[buffer.read++];

table[field] = (new jspack(true)).Unpack('d', b);
break;

case 'f'.charCodeAt(0):
case AMQPTypes._32BIT_FLOAT:
var b = [];
for (var i = 0; i < 4; ++i)
b[i] = buffer[buffer.read++];

table[field] = (new jspack(true)).Unpack('f', b);
break;

case 'T'.charCodeAt(0):
case AMQPTypes.TIME:
var int = parseInt(buffer, 8);
table[field] = new Date();
table[field].setTime(int * 1000);
break;

case 'F'.charCodeAt(0):
case AMQPTypes.HASH:
table[field] = parseTable(buffer);
break;

case 'l'.charCodeAt(0):
case AMQPTypes.SIGNED_64BIT:
table[field] = parseInt(buffer, 8);
break;

case 't'.charCodeAt(0):
case AMQPTypes.BOOLEAN:
table[field] = (parseInt(buffer, 1) > 0);
break;

case 'x'.charCodeAt(0):
case AMQPTypes.BYTE_ARRAY:
var len = parseInt(buffer, 4);
var buf = new Buffer(len);
buffer.copy(buf, 0, buffer.read, buffer.read + len);
Expand Down
32 changes: 32 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
AMQPTypes: Object.freeze({
STRING: 'S'.charCodeAt(0)
, INTEGER: 'I'.charCodeAt(0)
, HASH: 'F'.charCodeAt(0)
, TIME: 'T'.charCodeAt(0)
, DECIMAL: 'D'.charCodeAt(0)
, BOOLEAN: 't'.charCodeAt(0)
, SIGNED_8BIT: 'b'.charCodeAt(0)
, SIGNED_16BIT: 's'.charCodeAt(0)
, SIGNED_64BIT: 'l'.charCodeAt(0)
, _32BIT_FLOAT: 'f'.charCodeAt(0)
, _64BIT_FLOAT: 'd'.charCodeAt(0)
, VOID: 'v'.charCodeAt(0)
, BYTE_ARRAY: 'x'.charCodeAt(0)
, ARRAY: 'A'.charCodeAt(0)
, TEN: '10'.charCodeAt(0)
, BOOLEAN_TRUE: '\x01'
, BOOLEAN_FALSE:'\x00'

})
, Indicators: Object.freeze({
FRAME_END: 206
})
, FrameType: Object.freeze({
METHOD: 1
, HEADER: 2
, BODY: 3
, HEARTBEAT: 8
})
}

5 changes: 4 additions & 1 deletion test/test-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ connection.on('ready', function () {
exchange.publish("to.me", body, { headers: {
foo: 'bar',
bar: 'foo',
number: '123'
number: '123',
stuff: [{x:1}, {x:2}]
} });

setTimeout(function () {
Expand All @@ -32,6 +33,8 @@ connection.on('ready', function () {
assert.equal('bar', m.headers['foo']);
assert.equal('foo', m.headers['bar']);
assert.equal('123', m.headers['number'].toString());
assert.equal(1, m.headers['stuff'][0].x);
assert.equal(2, m.headers['stuff'][1].x);
})
})
});
Expand Down

0 comments on commit 0573df5

Please sign in to comment.