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