index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. var callBind = require('call-bind');
  3. var forEach = require('for-each');
  4. var gOPD = require('gopd');
  5. var hasProto = require('has-proto')();
  6. var isTypedArray = require('is-typed-array');
  7. var typedArrays = require('available-typed-arrays')();
  8. /** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray */
  9. /** @typedef {import('possible-typed-array-names')[number]} TypedArrayNames */
  10. /** @typedef {(value: TypedArray) => number} Getter */
  11. /** @type {Object.<TypedArrayNames, Getter>} */
  12. var getters = {};
  13. var oDP = Object.defineProperty;
  14. if (gOPD) {
  15. /** @type {Getter} */
  16. var getByteLength = function (x) {
  17. return x.byteLength;
  18. };
  19. forEach(typedArrays, function (typedArray) {
  20. // In Safari 7, Typed Array constructors are typeof object
  21. if (typeof global[typedArray] === 'function' || typeof global[typedArray] === 'object') {
  22. var Proto = global[typedArray].prototype;
  23. // @ts-expect-error TS doesn't narrow properly inside callbacks
  24. var descriptor = gOPD(Proto, 'byteLength');
  25. if (!descriptor && hasProto) {
  26. // @ts-expect-error hush, TS, every object has a dunder proto
  27. var superProto = Proto.__proto__; // eslint-disable-line no-proto
  28. // @ts-expect-error TS doesn't narrow properly inside callbacks
  29. descriptor = gOPD(superProto, 'byteLength');
  30. }
  31. // Opera 12.16 has a magic byteLength data property on instances AND on Proto
  32. if (descriptor && descriptor.get) {
  33. getters[typedArray] = callBind(descriptor.get);
  34. } else if (oDP) {
  35. // this is likely an engine where instances have a magic byteLength data property
  36. var arr = new global[typedArray](2);
  37. // @ts-expect-error TS doesn't narrow properly inside callbacks
  38. descriptor = gOPD(arr, 'byteLength');
  39. if (descriptor && descriptor.configurable) {
  40. oDP(arr, 'length', { value: 3 });
  41. }
  42. if (arr.length === 2) {
  43. getters[typedArray] = getByteLength;
  44. }
  45. }
  46. }
  47. });
  48. }
  49. /** @type {Getter} */
  50. var tryTypedArrays = function tryAllTypedArrays(value) {
  51. /** @type {number} */ var foundByteLength;
  52. forEach(getters, /** @type {(getter: Getter) => void} */ function (getter) {
  53. if (typeof foundByteLength !== 'number') {
  54. try {
  55. var byteLength = getter(value);
  56. if (typeof byteLength === 'number') {
  57. foundByteLength = byteLength;
  58. }
  59. } catch (e) {}
  60. }
  61. });
  62. // @ts-expect-error TS can't guarantee the callback is invoked sync
  63. return foundByteLength;
  64. };
  65. /** @type {import('.')} */
  66. module.exports = function typedArrayByteLength(value) {
  67. if (!isTypedArray(value)) {
  68. return false;
  69. }
  70. return tryTypedArrays(value);
  71. };