Skip to content

Commit

Permalink
allow uuid and char-utf32 to be sent/received
Browse files Browse the repository at this point in the history
  • Loading branch information
grs committed Jan 18, 2017
1 parent 5820034 commit 5b97ff1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
8 changes: 7 additions & 1 deletion lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ define_type('Double', 0x82, buffer_doublebe_ops());
define_type('Decimal32', 0x74);
define_type('Decimal64', 0x84);
define_type('Decimal128', 0x94);
define_type('CharUTF32', 0x73);
define_type('CharUTF32', 0x73, buffer_uint32be_ops());
define_type('Timestamp', 0x83, {'write':write_long, 'read':read_long});//TODO: convert to/from Date
define_type('Uuid', 0x98);//TODO: convert to/from stringified form?
define_type('Vbin8', 0xa0);
Expand Down Expand Up @@ -392,6 +392,12 @@ types.wrap_double = function(l) {
types.wrap_timestamp = function(l) {
return new types.Timestamp(l);
};
types.wrap_char = function(v) {
return new types.CharUTF32(v);
};
types.wrap_uuid = function(v) {
return new types.Uuid(v);
};
types.wrap_binary = function (s) {
return s.length > 255 ? new types.Vbin32(s) : new types.Vbin8(s);
};
Expand Down
36 changes: 30 additions & 6 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,39 @@
*/
'use strict';

var errors = require('./errors.js');

var util = {};

util.generate_uuid = function () {
// from http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript:
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
return uuid;
return util.uuid_to_string(util.uuid4());
};

util.uuid4 = function () {
var bytes = new Buffer(16);
for (var i = 0; i < bytes.length; i++) {
bytes[i] = Math.random()*255|0;
}

// From RFC4122, the version bits are set to 0100
bytes[7] &= 0x0F;
bytes[7] |= 0x40;

// From RFC4122, the top two bits of byte 8 get set to 01
bytes[8] &= 0x3F;
bytes[8] |= 0x80;

return bytes;
};


util.uuid_to_string = function (buffer) {
if (buffer.length === 16) {
var chunks = [buffer.slice(0, 4), buffer.slice(4, 6), buffer.slice(6, 8), buffer.slice(8, 10), buffer.slice(10, 16)];
return chunks.map(function (b) { return b.toString('hex'); }).join('-');
} else {
throw new errors.TypeError('Not a UUID, expecting 16 byte buffer');
}
};

util.clone = function (o) {
Expand Down
8 changes: 8 additions & 0 deletions test/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var assert = require('assert');
var rhea = require('../lib/container.js');
var amqp_types = require('../lib/types.js');
var amqp_message = require('../lib/message.js');
var rhea_util = require('../lib/util.js');

describe('message content', function() {
var container, sender, listener;
Expand Down Expand Up @@ -84,6 +85,13 @@ describe('message content', function() {
it('sends and receives ulong property', transfer_test({application_properties:{bigneg:-1234567898765}}, function(message) {
assert.equal(message.application_properties.bigneg, -1234567898765);
}));
it('sends and receives char property', transfer_test({application_properties:{'x':amqp_types.wrap_char(0x2603)}}, function(message) {
assert.equal(message.application_properties.x, 0x2603);
}));
var test_uuid = rhea_util.uuid4();
it('sends and receives a uuid property', transfer_test({application_properties:{'x':amqp_types.wrap_uuid(test_uuid)}}, function(message) {
assert.equal(rhea_util.uuid_to_string(message.application_properties.x), rhea_util.uuid_to_string(test_uuid));
}));
it('sends and receives string message annotation', transfer_test({message_annotations:{colour:'blue'}}, function(message) {
assert.equal(message.message_annotations.colour, 'blue');
}));
Expand Down

0 comments on commit 5b97ff1

Please sign in to comment.