Skip to content

Commit 3959376

Browse files
committed
Handle Node.js <= 0.12 Buffers not based on ArrayBuffer
1 parent 36d50aa commit 3959376

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

support/types.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ function isUint8Array(value) {
115115
} else {
116116
return (
117117
ObjectToString(value) === '[object Uint8Array]' ||
118-
isBuffer(value)
118+
// If it's a Buffer instance _and_ has a `.buffer` property,
119+
// this is an ArrayBuffer based buffer; thus it's an Uint8Array
120+
// (Old Node.js had a custom non-Uint8Array implementation)
121+
isBuffer(value) && value.buffer !== undefined
119122
);
120123
}
121124
}

test/node/types.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ if (isBuggyFirefox) {
175175
console.log('skipping fake typed array tests because they do not work in FF')
176176
}
177177

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

395398
var expected = {
396399
isArrayBufferView: [
397-
buffer,
400+
isBufferBasedOnArrayBuffer ? buffer : undefined,
398401
dataView,
399402
stealthyDataView,
400403
uint8Array,
@@ -421,7 +424,7 @@ if (isBuggyFirefox) {
421424
stealthyBigUint64Array
422425
],
423426
isTypedArray: [
424-
buffer,
427+
isBufferBasedOnArrayBuffer ? buffer : undefined,
425428
uint8Array,
426429
stealthyUint8Array,
427430
uint8ClampedArray,
@@ -446,7 +449,7 @@ if (isBuggyFirefox) {
446449
stealthyBigUint64Array
447450
],
448451
isUint8Array: [
449-
buffer,
452+
isBufferBasedOnArrayBuffer ? buffer : undefined,
450453
uint8Array,
451454
stealthyUint8Array
452455
],

0 commit comments

Comments
 (0)