Skip to content

Commit 5b97ff1

Browse files
committed
allow uuid and char-utf32 to be sent/received
1 parent 5820034 commit 5b97ff1

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

lib/types.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ define_type('Double', 0x82, buffer_doublebe_ops());
296296
define_type('Decimal32', 0x74);
297297
define_type('Decimal64', 0x84);
298298
define_type('Decimal128', 0x94);
299-
define_type('CharUTF32', 0x73);
299+
define_type('CharUTF32', 0x73, buffer_uint32be_ops());
300300
define_type('Timestamp', 0x83, {'write':write_long, 'read':read_long});//TODO: convert to/from Date
301301
define_type('Uuid', 0x98);//TODO: convert to/from stringified form?
302302
define_type('Vbin8', 0xa0);
@@ -392,6 +392,12 @@ types.wrap_double = function(l) {
392392
types.wrap_timestamp = function(l) {
393393
return new types.Timestamp(l);
394394
};
395+
types.wrap_char = function(v) {
396+
return new types.CharUTF32(v);
397+
};
398+
types.wrap_uuid = function(v) {
399+
return new types.Uuid(v);
400+
};
395401
types.wrap_binary = function (s) {
396402
return s.length > 255 ? new types.Vbin32(s) : new types.Vbin8(s);
397403
};

lib/util.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,39 @@
1515
*/
1616
'use strict';
1717

18+
var errors = require('./errors.js');
19+
1820
var util = {};
1921

2022
util.generate_uuid = function () {
21-
// from http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript:
22-
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
23-
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
24-
return v.toString(16);
25-
});
26-
return uuid;
23+
return util.uuid_to_string(util.uuid4());
24+
};
25+
26+
util.uuid4 = function () {
27+
var bytes = new Buffer(16);
28+
for (var i = 0; i < bytes.length; i++) {
29+
bytes[i] = Math.random()*255|0;
30+
}
31+
32+
// From RFC4122, the version bits are set to 0100
33+
bytes[7] &= 0x0F;
34+
bytes[7] |= 0x40;
35+
36+
// From RFC4122, the top two bits of byte 8 get set to 01
37+
bytes[8] &= 0x3F;
38+
bytes[8] |= 0x80;
39+
40+
return bytes;
41+
};
42+
43+
44+
util.uuid_to_string = function (buffer) {
45+
if (buffer.length === 16) {
46+
var chunks = [buffer.slice(0, 4), buffer.slice(4, 6), buffer.slice(6, 8), buffer.slice(8, 10), buffer.slice(10, 16)];
47+
return chunks.map(function (b) { return b.toString('hex'); }).join('-');
48+
} else {
49+
throw new errors.TypeError('Not a UUID, expecting 16 byte buffer');
50+
}
2751
};
2852

2953
util.clone = function (o) {

test/messages.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var assert = require('assert');
1919
var rhea = require('../lib/container.js');
2020
var amqp_types = require('../lib/types.js');
2121
var amqp_message = require('../lib/message.js');
22+
var rhea_util = require('../lib/util.js');
2223

2324
describe('message content', function() {
2425
var container, sender, listener;
@@ -84,6 +85,13 @@ describe('message content', function() {
8485
it('sends and receives ulong property', transfer_test({application_properties:{bigneg:-1234567898765}}, function(message) {
8586
assert.equal(message.application_properties.bigneg, -1234567898765);
8687
}));
88+
it('sends and receives char property', transfer_test({application_properties:{'x':amqp_types.wrap_char(0x2603)}}, function(message) {
89+
assert.equal(message.application_properties.x, 0x2603);
90+
}));
91+
var test_uuid = rhea_util.uuid4();
92+
it('sends and receives a uuid property', transfer_test({application_properties:{'x':amqp_types.wrap_uuid(test_uuid)}}, function(message) {
93+
assert.equal(rhea_util.uuid_to_string(message.application_properties.x), rhea_util.uuid_to_string(test_uuid));
94+
}));
8795
it('sends and receives string message annotation', transfer_test({message_annotations:{colour:'blue'}}, function(message) {
8896
assert.equal(message.message_annotations.colour, 'blue');
8997
}));

0 commit comments

Comments
 (0)