Skip to content

Commit e488763

Browse files
committed
[Fix] in node 0.8, TAs do not coerce Uint8Arrays to an ArrayBuffer properly
1 parent 8eebfa2 commit e488763

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var callBind = require('call-bind');
44
var callBound = require('call-bind/callBound');
55
var GetIntrinsic = require('get-intrinsic');
6+
var isTypedArray = require('is-typed-array');
67

78
var $ArrayBuffer = GetIntrinsic('ArrayBuffer', true);
89
var $Float32Array = GetIntrinsic('Float32Array', true);
@@ -32,7 +33,7 @@ module.exports = $byteLength || $abSlice
3233
// in node 0.8, ArrayBuffers have no prototype or own methods
3334
? function IsArrayBuffer(obj) {
3435
try {
35-
return (new $Float32Array(obj)).buffer === obj;
36+
return (new $Float32Array(obj)).buffer === obj && !isTypedArray(obj);
3637
} catch (e) {
3738
return false;
3839
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@ljharb/eslint-config": "^21.0.1",
4646
"aud": "^2.0.2",
4747
"auto-changelog": "^2.4.0",
48+
"available-typed-arrays": "^1.0.5",
4849
"es-value-fixtures": "^1.4.2",
4950
"eslint": "=8.8.0",
5051
"for-each": "^0.3.3",
@@ -66,7 +67,8 @@
6667
},
6768
"dependencies": {
6869
"call-bind": "^1.0.2",
69-
"get-intrinsic": "^1.1.3"
70+
"get-intrinsic": "^1.1.3",
71+
"is-typed-array": "^1.1.10"
7072
},
7173
"publishConfig": {
7274
"ignore": [

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var test = require('tape');
44
var inspect = require('object-inspect');
55
var forEach = require('for-each');
66
var v = require('es-value-fixtures');
7+
var availableTypedArrays = require('available-typed-arrays')();
78

89
var isArrayBuffer = require('..');
910

@@ -26,5 +27,15 @@ test('isArrayBuffer', function (t) {
2627
st.end();
2728
});
2829

30+
t.test('Typed Arrays', { skip: availableTypedArrays.length === 0 }, function (st) {
31+
forEach(availableTypedArrays, function (TypedArray) {
32+
var ta = new global[TypedArray](0);
33+
st.equal(isArrayBuffer(ta.buffer), true, inspect(ta.buffer) + ', the TA\'s buffer, is an ArrayBuffer');
34+
st.equal(isArrayBuffer(ta), false, inspect(ta) + ' is not an ArrayBuffer');
35+
});
36+
37+
st.end();
38+
});
39+
2940
t.end();
3041
});

0 commit comments

Comments
 (0)