Skip to content

Commit ca4f42a

Browse files
author
Kevin Roark
committed
added a BINARY_ACK type
1 parent aaf0731 commit ca4f42a

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ exports.types = [
2929
'EVENT',
3030
'BINARY_EVENT',
3131
'ACK',
32+
'BINARY_ACK',
3233
'ERROR'
3334
];
3435

@@ -80,6 +81,14 @@ exports.ERROR = 4;
8081

8182
exports.BINARY_EVENT = 5;
8283

84+
/**
85+
* Packet type `binary ack`. For acks with binary arguments.
86+
*
87+
* @api public
88+
*/
89+
90+
exports.BINARY_ACK = 6;
91+
8392
exports.Encoder = Encoder
8493

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

105-
if (exports.BINARY_EVENT == obj.type || exports.ACK == obj.type) {
114+
if (exports.BINARY_EVENT == obj.type || exports.BINARY_ACK == obj.type) {
106115
encodeAsBinary(obj, callback);
107116
}
108117
else {
@@ -127,7 +136,7 @@ function encodeAsString(obj) {
127136
str += obj.type;
128137

129138
// attachments if we have them
130-
if (exports.BINARY_EVENT == obj.type || exports.ACK == obj.type) {
139+
if (exports.BINARY_EVENT == obj.type || exports.BINARY_ACK == obj.type) {
131140
str += obj.attachments;
132141
str += '-';
133142
}
@@ -213,7 +222,7 @@ Decoder.prototype.add = function(obj) {
213222
var packet;
214223
if ('string' == typeof obj) {
215224
packet = decodeString(obj);
216-
if (exports.BINARY_EVENT == packet.type || exports.ACK == packet.type) { // binary packet's json
225+
if (exports.BINARY_EVENT == packet.type || exports.BINARY_ACK == packet.type) { // binary packet's json
217226
this.reconstructor = new BinaryReconstructor(packet);
218227

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

261270
// look up attachments if type binary
262-
if (exports.BINARY_EVENT == p.type || exports.ACK == p.type) {
271+
if (exports.BINARY_EVENT == p.type || exports.BINARY_ACK == p.type) {
263272
p.attachments = '';
264273
while (str.charAt(++i) != '-') {
265274
p.attachments += str.charAt(i);

test/blob.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,30 @@ describe('parser', function() {
3838

3939
var packet = {
4040
type: parser.BINARY_EVENT,
41-
data: {a: 'hi', b: { why: data }, c:'bye'},
41+
data: {a: 'hi', b: { why: data }, c: 'bye'},
4242
id: 999,
4343
nsp: '/deep'
4444
};
4545
helpers.test_bin(packet);
4646
});
4747

48+
it('encodes a binary ack with a blob', function() {
49+
var data;
50+
if (BlobBuilder) {
51+
var bb = new BlobBuilder();
52+
bb.append(new ArrayBuffer(2));
53+
data = bb.getBlob();
54+
} else {
55+
data = new Blob([new ArrayBuffer(2)]);
56+
}
57+
58+
var packet = {
59+
type: parser.BINARY_ACK,
60+
data: {a: 'hi ack', b: { why: data }, c: 'bye ack'},
61+
id: 999,
62+
nsp: '/deep'
63+
};
64+
helpers.test_bin(packet);
65+
})
66+
4867
});

test/buffer.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,14 @@ describe('parser', function() {
1212
id: 23,
1313
nsp: '/cool'
1414
});
15-
});
15+
});
16+
17+
it('encodes a binary ack with Buffer', function() {
18+
helpers.test_bin({
19+
type: parser.BINARY_ACK,
20+
data: ['a', new Buffer('xxx', 'utf8'), {}],
21+
id: 127,
22+
nsp: '/back'
23+
})
24+
});
1625
});

0 commit comments

Comments
 (0)