index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var callBind = require('call-bind');
  3. var callBound = require('call-bind/callBound');
  4. var GetIntrinsic = require('get-intrinsic');
  5. var $ArrayBuffer = GetIntrinsic('%ArrayBuffer%', true);
  6. /** @type {undefined | ((receiver: ArrayBuffer) => number) | ((receiver: unknown) => never)} */
  7. var $byteLength = callBound('ArrayBuffer.prototype.byteLength', true);
  8. var $toString = callBound('Object.prototype.toString');
  9. // in node 0.10, ArrayBuffers have no prototype methods, but have an own slot-checking `slice` method
  10. var abSlice = !!$ArrayBuffer && !$byteLength && new $ArrayBuffer(0).slice;
  11. var $abSlice = !!abSlice && callBind(abSlice);
  12. /** @type {import('.')} */
  13. module.exports = $byteLength || $abSlice
  14. ? function isArrayBuffer(obj) {
  15. if (!obj || typeof obj !== 'object') {
  16. return false;
  17. }
  18. try {
  19. if ($byteLength) {
  20. // @ts-expect-error no idea why TS can't handle the overload
  21. $byteLength(obj);
  22. } else {
  23. // @ts-expect-error TS chooses not to type-narrow inside a closure
  24. $abSlice(obj, 0);
  25. }
  26. return true;
  27. } catch (e) {
  28. return false;
  29. }
  30. }
  31. : $ArrayBuffer
  32. // in node 0.8, ArrayBuffers have no prototype or own methods, but also no Symbol.toStringTag
  33. ? function isArrayBuffer(obj) {
  34. return $toString(obj) === '[object ArrayBuffer]';
  35. }
  36. : function isArrayBuffer(obj) { // eslint-disable-line no-unused-vars
  37. return false;
  38. };