Skip to content

Commit 0ae9a4f

Browse files
committed
prevent direct Buffer reference that breaks browserify
1 parent d7c0eda commit 0ae9a4f

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

binary.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
var isArray = require('isarray');
8+
var isBuf = require('./is-buffer');
89

910
/**
1011
* Replaces every Buffer | ArrayBuffer in packet with a numbered placeholder.
@@ -138,13 +139,3 @@ exports.removeBlobs = function(data, callback) {
138139
callback(bloblessData);
139140
}
140141
};
141-
142-
/**
143-
* Returns true if obj is a buffer or an arraybuffer.
144-
*
145-
* @api private
146-
*/
147-
function isBuf(obj) {
148-
return (global.Buffer && Buffer.isBuffer(obj)) ||
149-
(global.ArrayBuffer && obj instanceof ArrayBuffer);
150-
}

index.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var json = require('json3');
88
var isArray = require('isarray');
99
var Emitter = require('component-emitter');
1010
var binary = require('./binary');
11+
var isBuf = require('./is-buffer');
1112

1213
/**
1314
* Protocol version.
@@ -89,14 +90,29 @@ exports.BINARY_EVENT = 5;
8990

9091
exports.BINARY_ACK = 6;
9192

92-
exports.Encoder = Encoder
93+
/**
94+
* Encoder constructor.
95+
*
96+
* @api public
97+
*/
98+
99+
exports.Encoder = Encoder;
100+
101+
/**
102+
* Decoder constructor.
103+
*
104+
* @api public
105+
*/
106+
107+
exports.Decoder = Decoder;
93108

94109
/**
95110
* A socket.io Encoder instance
96111
*
97112
* @api public
98113
*/
99-
function Encoder() {};
114+
115+
function Encoder() {}
100116

101117
/**
102118
* Encode a packet as a single string if non-binary, or as a
@@ -191,8 +207,6 @@ function encodeAsBinary(obj, callback) {
191207
binary.removeBlobs(obj, writeEncoding);
192208
}
193209

194-
exports.Decoder = Decoder
195-
196210
/**
197211
* A socket.io Decoder instance
198212
*
@@ -233,9 +247,7 @@ Decoder.prototype.add = function(obj) {
233247
this.emit('decoded', packet);
234248
}
235249
}
236-
else if ((global.Buffer && Buffer.isBuffer(obj)) ||
237-
(global.ArrayBuffer && obj instanceof ArrayBuffer) ||
238-
obj.base64) { // raw binary data
250+
else if (isBuf(obj) || obj.base64) { // raw binary data
239251
if (!this.reconstructor) {
240252
throw new Error('got binary data when not reconstructing a packet');
241253
} else {
@@ -249,7 +261,7 @@ Decoder.prototype.add = function(obj) {
249261
else {
250262
throw new Error('Unknown type: ' + obj);
251263
}
252-
}
264+
};
253265

254266
/**
255267
* Decode a packet String (JSON data)
@@ -316,7 +328,7 @@ function decodeString(str) {
316328

317329
debug('decoded %s as %j', str, p);
318330
return p;
319-
};
331+
}
320332

321333
/**
322334
* Deallocates a parser's resources
@@ -328,7 +340,7 @@ Decoder.prototype.destroy = function() {
328340
if (this.reconstructor) {
329341
this.reconstructor.finishedReconstruction();
330342
}
331-
}
343+
};
332344

333345
/**
334346
* A manager of a binary event's 'buffer sequence'. Should
@@ -363,7 +375,7 @@ BinaryReconstructor.prototype.takeBinaryData = function(binData) {
363375
return packet;
364376
}
365377
return null;
366-
}
378+
};
367379

368380
/**
369381
* Cleans up binary packet reconstruction variables.
@@ -374,7 +386,7 @@ BinaryReconstructor.prototype.takeBinaryData = function(binData) {
374386
BinaryReconstructor.prototype.finishedReconstruction = function() {
375387
this.reconPack = null;
376388
this.buffers = [];
377-
}
389+
};
378390

379391
function error(data){
380392
return {

is-buffer.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
module.exports = isBuf;
3+
4+
/**
5+
* Returns true if obj is a buffer or an arraybuffer.
6+
*
7+
* @api private
8+
*/
9+
10+
function isBuf(obj) {
11+
return (global.Buffer && global.Buffer.isBuffer(obj)) ||
12+
(global.ArrayBuffer && obj instanceof ArrayBuffer);
13+
}

0 commit comments

Comments
 (0)