From 69a03f671f892b724be1a899a3d90c981e7601c9 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 2 Feb 2024 16:16:42 -0800 Subject: [PATCH] [Fix] TAs can take a DataView in node 0.8; use a simpler check --- .eslintrc | 4 ++++ index.js | 13 ++++--------- package.json | 5 ++--- test/index.js | 3 +++ 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.eslintrc b/.eslintrc index 46f3b12..0b9a855 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,6 +3,10 @@ "extends": "@ljharb", + "globals": { + "DataView": false, + }, + "rules": { "new-cap": ["error", { "capIsNewExceptions": [ diff --git a/index.js b/index.js index 7674285..2d7110f 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,10 @@ var callBind = require('call-bind'); var callBound = require('call-bind/callBound'); var GetIntrinsic = require('get-intrinsic'); -var isTypedArray = require('is-typed-array'); var $ArrayBuffer = GetIntrinsic('ArrayBuffer', true); -var $Float32Array = GetIntrinsic('Float32Array', true); var $byteLength = callBound('ArrayBuffer.prototype.byteLength', true); +var $toString = callBound('Object.prototype.toString'); // in node 0.10, ArrayBuffers have no prototype methods, but have an own slot-checking `slice` method var abSlice = $ArrayBuffer && !$byteLength && new $ArrayBuffer().slice; @@ -29,14 +28,10 @@ module.exports = $byteLength || $abSlice return false; } } - : $Float32Array - // in node 0.8, ArrayBuffers have no prototype or own methods + : $ArrayBuffer + // in node 0.8, ArrayBuffers have no prototype or own methods, but also no Symbol.toStringTag ? function IsArrayBuffer(obj) { - try { - return (new $Float32Array(obj)).buffer === obj && !isTypedArray(obj); - } catch (e) { - return typeof obj === 'object' && e.name === 'RangeError'; - } + return $toString(obj) === '[object ArrayBuffer]'; } : function isArrayBuffer(obj) { // eslint-disable-line no-unused-vars return false; diff --git a/package.json b/package.json index 976ba48..e9703f5 100644 --- a/package.json +++ b/package.json @@ -66,9 +66,8 @@ "startingVersion": "2.0.1" }, "dependencies": { - "call-bind": "^1.0.5", - "get-intrinsic": "^1.2.2", - "is-typed-array": "^1.1.13" + "call-bind": "^1.0.2", + "get-intrinsic": "^1.2.1" }, "publishConfig": { "ignore": [ diff --git a/test/index.js b/test/index.js index 7ddf9d7..40c857f 100644 --- a/test/index.js +++ b/test/index.js @@ -26,6 +26,9 @@ test('isArrayBuffer', function (t) { var ab42 = new ArrayBuffer(42); st.equal(isArrayBuffer(ab42), true, inspect(ab42) + ' is an ArrayBuffer'); + var dv = new DataView(ab42); + st.equal(isArrayBuffer(dv), false, inspect(dv) + ' is not an ArrayBuffer'); + st.end(); });