index.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 {(value: import('.').TypedArray) => number} TypedArrayLengthGetter */
  10. /** @typedef {{ [k in `$${import('.').TypedArrayName}` | '__proto__']: k extends '__proto__' ? null : TypedArrayLengthGetter }} Cache */
  11. /** @type {Cache} */
  12. // @ts-expect-error TS doesn't seem to have a "will eventually satisfy" type
  13. var getters = { __proto__: null };
  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: import('.').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. // eslint-disable-next-line no-extra-parens
  34. getters[/** @type {`$${import('.').TypedArrayName}`} */ ('$' + typedArray)] = callBind(descriptor.get);
  35. } else if (oDP) {
  36. // this is likely an engine where instances have a magic length data property
  37. var arr = new global[typedArray](2);
  38. // @ts-expect-error TS doesn't narrow types inside callbacks, which is weird
  39. descriptor = gOPD(arr, 'length');
  40. if (descriptor && descriptor.configurable) {
  41. oDP(arr, 'length', { value: 3 });
  42. }
  43. if (arr.length === 2) {
  44. // eslint-disable-next-line no-extra-parens
  45. getters[/** @type {`$${import('.').TypedArrayName}`} */ ('$' + typedArray)] = getLength;
  46. }
  47. }
  48. }
  49. });
  50. }
  51. /** @type {TypedArrayLengthGetter} */
  52. var tryTypedArrays = function tryAllTypedArrays(value) {
  53. /** @type {number} */ var foundLength;
  54. // @ts-expect-error not sure why this won't work
  55. forEach(getters, /** @type {(getter: TypedArrayLengthGetter) => void} */ function (getter) {
  56. if (typeof foundLength !== 'number') {
  57. try {
  58. var length = getter(value);
  59. if (typeof length === 'number') {
  60. foundLength = length;
  61. }
  62. } catch (e) {}
  63. }
  64. });
  65. // @ts-expect-error TS can't guarantee the above callback is invoked sync
  66. return foundLength;
  67. };
  68. /** @type {import('.')} */
  69. module.exports = function typedArrayLength(value) {
  70. if (!isTypedArray(value)) {
  71. return false;
  72. }
  73. return tryTypedArrays(value);
  74. };