Skip to content

Commit 613db3c

Browse files
committed
add support for data and sequence sections
1 parent 48d2ec1 commit 613db3c

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

lib/message.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ function define_map_section(def) {
6969
define_section(descriptor, unwrap, wrap);
7070
}
7171

72+
function Section(typecode, content) {
73+
this.typecode = typecode;
74+
this.content = content;
75+
}
76+
77+
Section.prototype.described = function () {
78+
return types.described(types.wrap_ulong(this.typecode), types.wrap(this.content));
79+
};
80+
7281
define_composite_section({name:'header',
7382
code:0x70,
7483
fields:[
@@ -101,12 +110,31 @@ define_composite_section({name:'properties',
101110
});
102111
define_map_section({name:'application_properties', code:0x74});
103112

104-
define_section({numeric:0x77, symbolic:'amqp:value:*'},
105-
function(msg, section) { msg.body = types.unwrap(section); },
106-
function(sections, msg) { sections.push(types.described(types.wrap_ulong(0x77), types.wrap(msg.body))); });
113+
define_section({numeric:0x75, symbolic:'amqp:data:binary'}, function (msg, section) { msg.body = new Section(0x75, types.unwrap(section)); });
114+
define_section({numeric:0x76, symbolic:'amqp:amqp-sequence:list'}, function (msg, section) { msg.body = new Section(0x76, types.unwrap(section)); });
115+
define_section({numeric:0x77, symbolic:'amqp:value:*'}, function (msg, section) { msg.body = types.unwrap(section); });
107116

108117
define_map_section({name:'footer', code:0x78});
109118

119+
120+
function wrap_body (sections, msg) {
121+
if (msg.body && msg.body.constructor === Section) {
122+
sections.push(msg.body.described());
123+
} else {
124+
sections.push(types.described(types.wrap_ulong(0x77), types.wrap(msg.body)));
125+
}
126+
}
127+
128+
wrappers.push(wrap_body);
129+
130+
message.data_section = function (data) {
131+
return new Section(0x75, data);
132+
};
133+
134+
message.sequence_section = function (list) {
135+
return new Section(0x76, list);
136+
};
137+
110138
function reverse_lookup(map) {
111139
var reversed = {};
112140
for (var key in map) {

lib/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ types.wrap = function(o) {
384384
return types.wrap_timestamp(o.getTime());
385385
} else if (o instanceof Typed) {
386386
return o;
387+
} else if (o instanceof Buffer) {
388+
return types.wrap_binary(o);
387389
} else if (t === 'undefined' || o === null) {
388390
return new types.Null();
389391
} else if (Array.isArray(o)) {

test/messages.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
var assert = require('assert');
1919
var rhea = require('../lib/container.js');
2020
var amqp_types = require('../lib/types.js');
21+
var amqp_message = require('../lib/message.js');
2122

2223
describe('message content', function() {
2324
var container, sender, listener;
@@ -52,6 +53,16 @@ describe('message content', function() {
5253
it('sends and receives binary body', transfer_test({body:amqp_types.wrap_binary(new Buffer('hello world!'))}, function(message) {
5354
assert.equal(message.body.toString(), 'hello world!');
5455
}));
56+
it('sends and receives body as data section', transfer_test({body:amqp_message.data_section(new Buffer('hello world!'))}, function(message) {
57+
assert.equal(message.body.typecode, 0x75);
58+
assert.equal(message.body.content.toString(), 'hello world!');
59+
}));
60+
it('sends and receives body as sequence section', transfer_test({body:amqp_message.sequence_section(['hello', 1, 'world!'])}, function(message) {
61+
assert.equal(message.body.typecode, 0x76);
62+
assert.equal(message.body.content[0], 'hello');
63+
assert.equal(message.body.content[1], 1);
64+
assert.equal(message.body.content[2], 'world!');
65+
}));
5566
it('sends and receives subject', transfer_test({properties:{subject:'my-subject'}}, function(message) {
5667
assert.equal(message.properties.subject, 'my-subject');
5768
}));

0 commit comments

Comments
 (0)