Skip to content

Commit

Permalink
Handle Node.js <= 0.12 Buffers not based on ArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed May 1, 2019
1 parent 36d50aa commit 3959376
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion support/types.js
Expand Up @@ -115,7 +115,10 @@ function isUint8Array(value) {
} else {
return (
ObjectToString(value) === '[object Uint8Array]' ||
isBuffer(value)
// If it's a Buffer instance _and_ has a `.buffer` property,
// this is an ArrayBuffer based buffer; thus it's an Uint8Array
// (Old Node.js had a custom non-Uint8Array implementation)
isBuffer(value) && value.buffer !== undefined
);
}
}
Expand Down
9 changes: 6 additions & 3 deletions test/node/types.js
Expand Up @@ -175,6 +175,9 @@ if (isBuggyFirefox) {
console.log('skipping fake typed array tests because they do not work in FF')
}

// Old Node.js had a fully custom Buffer implementation, newer are based on ArrayBuffer
// This is important for the ArrayBuffer and typed array tests
var isBufferBasedOnArrayBuffer = Buffer.alloc(1).buffer !== undefined;
{
var primitive = function primitive() { return true; };
var arrayBuffer = function arrayBuffer() { return new ArrayBuffer(1); };
Expand Down Expand Up @@ -394,7 +397,7 @@ if (isBuggyFirefox) {

var expected = {
isArrayBufferView: [
buffer,
isBufferBasedOnArrayBuffer ? buffer : undefined,
dataView,
stealthyDataView,
uint8Array,
Expand All @@ -421,7 +424,7 @@ if (isBuggyFirefox) {
stealthyBigUint64Array
],
isTypedArray: [
buffer,
isBufferBasedOnArrayBuffer ? buffer : undefined,
uint8Array,
stealthyUint8Array,
uint8ClampedArray,
Expand All @@ -446,7 +449,7 @@ if (isBuggyFirefox) {
stealthyBigUint64Array
],
isUint8Array: [
buffer,
isBufferBasedOnArrayBuffer ? buffer : undefined,
uint8Array,
stealthyUint8Array
],
Expand Down

0 comments on commit 3959376

Please sign in to comment.