Skip to content

Commit

Permalink
[fix] Properly detect typed arrays (#85)
Browse files Browse the repository at this point in the history
ArrayBuffer.isView method is not defined in IE10.
  • Loading branch information
darrachequesne authored Feb 28, 2018
1 parent f9c0625 commit 6b356eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
15 changes: 13 additions & 2 deletions is-buffer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@

module.exports = isBuf;

var withNativeBuffer = typeof global.Buffer === 'function' && typeof global.Buffer.isBuffer === 'function';
var withNativeArrayBuffer = typeof global.ArrayBuffer === 'function';

var isView = (function () {
if (typeof global.ArrayBuffer.isView === 'function') {
return global.ArrayBuffer.isView;
} else {
return function (obj) { return obj.buffer instanceof global.ArrayBuffer; };
}
})();

/**
* Returns true if obj is a buffer or an arraybuffer.
*
* @api private
*/

function isBuf(obj) {
return (global.Buffer && global.Buffer.isBuffer(obj)) ||
(global.ArrayBuffer && (obj instanceof ArrayBuffer || ArrayBuffer.isView(obj)));
return (withNativeBuffer && global.Buffer.isBuffer(obj)) ||
(withNativeArrayBuffer && (obj instanceof global.ArrayBuffer || isView(obj)));
}
13 changes: 13 additions & 0 deletions test/arraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ describe('parser', function() {
helpers.test_bin(packet);
});

it('encodes a TypedArray', function() {
var array = new Uint8Array(5);
for (var i = 0; i < array.length; i++) array[i] = i;

var packet = {
type: parser.BINARY_EVENT,
data: ['a', array],
id: 0,
nsp: '/'
};
helpers.test_bin(packet);
});

it('encodes ArrayBuffers deep in JSON', function() {
var packet = {
type: parser.BINARY_EVENT,
Expand Down

0 comments on commit 6b356eb

Please sign in to comment.