bb38e22a53af7428f6f2c35fdcd4fad82e53887c33fc4f397a2f59a90572fe16954e16fc3ec06f8e87358d7e8cfe9446d603ecd4073ffa7a50376c5a97c57f 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. var forEach = require('for-each');
  3. var callBind = require('call-bind');
  4. var gPO = require('reflect.getprototypeof/polyfill')();
  5. var typedArrays = require('available-typed-arrays')();
  6. /** @typedef {(x: import('.').TypedArray) => number} ByteOffsetGetter */
  7. /** @type {Record<import('.').TypedArrayName, ByteOffsetGetter>} */
  8. var getters = {
  9. // @ts-expect-error TS can't handle __proto__ or `satisfies` in jsdoc
  10. __proto__: null
  11. };
  12. var gOPD = require('gopd');
  13. var oDP = Object.defineProperty;
  14. if (gOPD) {
  15. /** @type {ByteOffsetGetter} */
  16. var getByteOffset = function (x) {
  17. return x.byteOffset;
  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 can't guarantee the callback is invoked sync
  24. var descriptor = gOPD(Proto, 'byteOffset');
  25. if (!descriptor) {
  26. var superProto = gPO(Proto);
  27. // @ts-expect-error TS can't guarantee the callback is invoked sync
  28. descriptor = gOPD(superProto, 'byteOffset');
  29. }
  30. // Opera 12.16 has a magic byteOffset data property on instances AND on Proto
  31. if (descriptor && descriptor.get) {
  32. getters[typedArray] = callBind(descriptor.get);
  33. } else if (oDP) {
  34. // this is likely an engine where instances have a magic byteOffset data property
  35. var arr = new global[typedArray](2);
  36. // @ts-expect-error TS can't guarantee the callback is invoked sync
  37. descriptor = gOPD(arr, 'byteOffset');
  38. if (descriptor && descriptor.configurable) {
  39. oDP(arr, 'length', { value: 3 });
  40. }
  41. if (arr.length === 2) {
  42. getters[typedArray] = getByteOffset;
  43. }
  44. }
  45. }
  46. });
  47. }
  48. /** @type {ByteOffsetGetter} */
  49. var tryTypedArrays = function tryAllTypedArrays(value) {
  50. /** @type {number} */ var foundOffset;
  51. forEach(getters, /** @type {(getter: ByteOffsetGetter) => void} */ function (getter) {
  52. if (typeof foundOffset !== 'number') {
  53. try {
  54. var offset = getter(value);
  55. if (typeof offset === 'number') {
  56. foundOffset = offset;
  57. }
  58. } catch (e) {}
  59. }
  60. });
  61. // @ts-expect-error TS can't guarantee the callback is invoked sync
  62. return foundOffset;
  63. };
  64. var isTypedArray = require('is-typed-array');
  65. /** @type {import('.')} */
  66. module.exports = function typedArrayByteOffset(value) {
  67. if (!isTypedArray(value)) {
  68. return false;
  69. }
  70. return tryTypedArrays(value);
  71. };