Skip to content

Commit

Permalink
added a BINARY_ACK type
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-roark committed May 31, 2014
1 parent aaf0731 commit ca4f42a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
17 changes: 13 additions & 4 deletions index.js
Expand Up @@ -29,6 +29,7 @@ exports.types = [
'EVENT',
'BINARY_EVENT',
'ACK',
'BINARY_ACK',
'ERROR'
];

Expand Down Expand Up @@ -80,6 +81,14 @@ exports.ERROR = 4;

exports.BINARY_EVENT = 5;

/**
* Packet type `binary ack`. For acks with binary arguments.
*
* @api public
*/

exports.BINARY_ACK = 6;

exports.Encoder = Encoder

/**
Expand All @@ -102,7 +111,7 @@ function Encoder() {};
Encoder.prototype.encode = function(obj, callback){
debug('encoding packet %j', obj);

if (exports.BINARY_EVENT == obj.type || exports.ACK == obj.type) {
if (exports.BINARY_EVENT == obj.type || exports.BINARY_ACK == obj.type) {
encodeAsBinary(obj, callback);
}
else {
Expand All @@ -127,7 +136,7 @@ function encodeAsString(obj) {
str += obj.type;

// attachments if we have them
if (exports.BINARY_EVENT == obj.type || exports.ACK == obj.type) {
if (exports.BINARY_EVENT == obj.type || exports.BINARY_ACK == obj.type) {
str += obj.attachments;
str += '-';
}
Expand Down Expand Up @@ -213,7 +222,7 @@ Decoder.prototype.add = function(obj) {
var packet;
if ('string' == typeof obj) {
packet = decodeString(obj);
if (exports.BINARY_EVENT == packet.type || exports.ACK == packet.type) { // binary packet's json
if (exports.BINARY_EVENT == packet.type || exports.BINARY_ACK == packet.type) { // binary packet's json
this.reconstructor = new BinaryReconstructor(packet);

// no attachments, labeled binary but no binary data to follow
Expand Down Expand Up @@ -259,7 +268,7 @@ function decodeString(str) {
if (null == exports.types[p.type]) return error();

// look up attachments if type binary
if (exports.BINARY_EVENT == p.type || exports.ACK == p.type) {
if (exports.BINARY_EVENT == p.type || exports.BINARY_ACK == p.type) {
p.attachments = '';
while (str.charAt(++i) != '-') {
p.attachments += str.charAt(i);
Expand Down
21 changes: 20 additions & 1 deletion test/blob.js
Expand Up @@ -38,11 +38,30 @@ describe('parser', function() {

var packet = {
type: parser.BINARY_EVENT,
data: {a: 'hi', b: { why: data }, c:'bye'},
data: {a: 'hi', b: { why: data }, c: 'bye'},
id: 999,
nsp: '/deep'
};
helpers.test_bin(packet);
});

it('encodes a binary ack with a blob', function() {
var data;
if (BlobBuilder) {
var bb = new BlobBuilder();
bb.append(new ArrayBuffer(2));
data = bb.getBlob();
} else {
data = new Blob([new ArrayBuffer(2)]);
}

var packet = {
type: parser.BINARY_ACK,
data: {a: 'hi ack', b: { why: data }, c: 'bye ack'},
id: 999,
nsp: '/deep'
};
helpers.test_bin(packet);
})

});
11 changes: 10 additions & 1 deletion test/buffer.js
Expand Up @@ -12,5 +12,14 @@ describe('parser', function() {
id: 23,
nsp: '/cool'
});
});
});

it('encodes a binary ack with Buffer', function() {
helpers.test_bin({
type: parser.BINARY_ACK,
data: ['a', new Buffer('xxx', 'utf8'), {}],
id: 127,
nsp: '/back'
})
});
});

0 comments on commit ca4f42a

Please sign in to comment.