index.js 2.6 KB

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