1 | 'use strict';
|
---|
2 |
|
---|
3 | // / <reference types="node" />
|
---|
4 |
|
---|
5 | var callBind = require('call-bind');
|
---|
6 | var forEach = require('for-each');
|
---|
7 | var gOPD = require('gopd');
|
---|
8 | var hasProto = require('has-proto')();
|
---|
9 | var isTypedArray = require('is-typed-array');
|
---|
10 | var typedArrays = require('possible-typed-array-names');
|
---|
11 |
|
---|
12 | /** @typedef {(value: import('.').TypedArray) => number} TypedArrayLengthGetter */
|
---|
13 | /** @typedef {{ [k in `$${import('.').TypedArrayName}` | '__proto__']: k extends '__proto__' ? null : TypedArrayLengthGetter }} Cache */
|
---|
14 |
|
---|
15 | /** @type {Cache} */
|
---|
16 | // @ts-expect-error TS doesn't seem to have a "will eventually satisfy" type
|
---|
17 | var getters = { __proto__: null };
|
---|
18 | var oDP = Object.defineProperty;
|
---|
19 | if (gOPD) {
|
---|
20 | var getLength = /** @type {TypedArrayLengthGetter} */ function (x) {
|
---|
21 | return x.length;
|
---|
22 | };
|
---|
23 | forEach(typedArrays, /** @type {(typedArray: import('.').TypedArrayName) => void} */ function (typedArray) {
|
---|
24 | var TA = global[typedArray];
|
---|
25 | // In Safari 7, Typed Array constructors are typeof object
|
---|
26 | if (typeof TA === 'function' || typeof TA === 'object') {
|
---|
27 | var Proto = TA.prototype;
|
---|
28 | // @ts-expect-error TS doesn't narrow types inside callbacks, which is weird
|
---|
29 | var descriptor = gOPD(Proto, 'length');
|
---|
30 | if (!descriptor && hasProto) {
|
---|
31 | var superProto = Proto.__proto__; // eslint-disable-line no-proto
|
---|
32 | // @ts-expect-error TS doesn't narrow types inside callbacks, which is weird
|
---|
33 | descriptor = gOPD(superProto, 'length');
|
---|
34 | }
|
---|
35 | // Opera 12.16 has a magic length data property on instances AND on Proto
|
---|
36 | if (descriptor && descriptor.get) {
|
---|
37 | // eslint-disable-next-line no-extra-parens
|
---|
38 | getters[/** @type {`$${import('.').TypedArrayName}`} */ ('$' + typedArray)] = callBind(descriptor.get);
|
---|
39 | } else if (oDP) {
|
---|
40 | // this is likely an engine where instances have a magic length data property
|
---|
41 | var arr = new global[typedArray](2);
|
---|
42 | // @ts-expect-error TS doesn't narrow types inside callbacks, which is weird
|
---|
43 | descriptor = gOPD(arr, 'length');
|
---|
44 | if (descriptor && descriptor.configurable) {
|
---|
45 | oDP(arr, 'length', { value: 3 });
|
---|
46 | }
|
---|
47 | if (arr.length === 2) {
|
---|
48 | // eslint-disable-next-line no-extra-parens
|
---|
49 | getters[/** @type {`$${import('.').TypedArrayName}`} */ ('$' + typedArray)] = getLength;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 | });
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** @type {TypedArrayLengthGetter} */
|
---|
57 | var tryTypedArrays = function tryAllTypedArrays(value) {
|
---|
58 | /** @type {number} */ var foundLength;
|
---|
59 | // @ts-expect-error not sure why this won't work
|
---|
60 | forEach(getters, /** @type {(getter: TypedArrayLengthGetter) => void} */ function (getter) {
|
---|
61 | if (typeof foundLength !== 'number') {
|
---|
62 | try {
|
---|
63 | var length = getter(value);
|
---|
64 | if (typeof length === 'number') {
|
---|
65 | foundLength = length;
|
---|
66 | }
|
---|
67 | } catch (e) {}
|
---|
68 | }
|
---|
69 | });
|
---|
70 | // @ts-expect-error TS can't guarantee the above callback is invoked sync
|
---|
71 | return foundLength;
|
---|
72 | };
|
---|
73 |
|
---|
74 | /** @type {import('.')} */
|
---|
75 | module.exports = function typedArrayLength(value) {
|
---|
76 | if (!isTypedArray(value)) {
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 | return tryTypedArrays(value);
|
---|
80 | };
|
---|