Skip to content

Commit 6b356eb

Browse files
[fix] Properly detect typed arrays (#85)
ArrayBuffer.isView method is not defined in IE10.
1 parent f9c0625 commit 6b356eb

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

is-buffer.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11

22
module.exports = isBuf;
33

4+
var withNativeBuffer = typeof global.Buffer === 'function' && typeof global.Buffer.isBuffer === 'function';
5+
var withNativeArrayBuffer = typeof global.ArrayBuffer === 'function';
6+
7+
var isView = (function () {
8+
if (typeof global.ArrayBuffer.isView === 'function') {
9+
return global.ArrayBuffer.isView;
10+
} else {
11+
return function (obj) { return obj.buffer instanceof global.ArrayBuffer; };
12+
}
13+
})();
14+
415
/**
516
* Returns true if obj is a buffer or an arraybuffer.
617
*
718
* @api private
819
*/
920

1021
function isBuf(obj) {
11-
return (global.Buffer && global.Buffer.isBuffer(obj)) ||
12-
(global.ArrayBuffer && (obj instanceof ArrayBuffer || ArrayBuffer.isView(obj)));
22+
return (withNativeBuffer && global.Buffer.isBuffer(obj)) ||
23+
(withNativeArrayBuffer && (obj instanceof global.ArrayBuffer || isView(obj)));
1324
}

test/arraybuffer.js

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ describe('parser', function() {
1414
helpers.test_bin(packet);
1515
});
1616

17+
it('encodes a TypedArray', function() {
18+
var array = new Uint8Array(5);
19+
for (var i = 0; i < array.length; i++) array[i] = i;
20+
21+
var packet = {
22+
type: parser.BINARY_EVENT,
23+
data: ['a', array],
24+
id: 0,
25+
nsp: '/'
26+
};
27+
helpers.test_bin(packet);
28+
});
29+
1730
it('encodes ArrayBuffers deep in JSON', function() {
1831
var packet = {
1932
type: parser.BINARY_EVENT,

0 commit comments

Comments
 (0)