index.js 2.7 KB

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