Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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