typed-array-byte-offset 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/index.d.ts +17 -0
- package/index.js +16 -5
- package/package.json +27 -11
- package/test/index.js +3 -13
- package/tsconfig.json +10 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,28 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [v1.0.3](https://github.com/inspect-js/typed-array-byte-offset/compare/v1.0.2...v1.0.3) - 2024-11-21
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- [Fix] avoid relying on `__proto__` accessor [`#4`](https://github.com/inspect-js/typed-array-byte-offset/issues/4)
|
|
13
|
+
|
|
14
|
+
### Commits
|
|
15
|
+
|
|
16
|
+
- [types] use shared config [`10b0823`](https://github.com/inspect-js/typed-array-byte-offset/commit/10b0823ecc13b95920cfa8f27fe61af5678fb67b)
|
|
17
|
+
- [actions] split out node 10-20, and 20+ [`11554a9`](https://github.com/inspect-js/typed-array-byte-offset/commit/11554a96ca11b85c7ad87118e1d811bfde2b9f32)
|
|
18
|
+
- [Dev Deps] update `@arethetypeswrong/cli`, `@ljharb/eslint-config`, `@types/object-inspect`, `auto-changelog`, `object-inspect`, `tape` [`c39dd06`](https://github.com/inspect-js/typed-array-byte-offset/commit/c39dd06d2868a724463722ff2f416b5c41171140)
|
|
19
|
+
- [Tests] run tsc and `@arethetypeswrong/cli` in CI [`0b984aa`](https://github.com/inspect-js/typed-array-byte-offset/commit/0b984aa64c86f4bcb476b716cdd16d67c39b68ca)
|
|
20
|
+
- [Tests] replace `aud` with `npm audit` [`512b59d`](https://github.com/inspect-js/typed-array-byte-offset/commit/512b59df0e567592282795bfec331193d828f2fc)
|
|
21
|
+
|
|
22
|
+
## [v1.0.2](https://github.com/inspect-js/typed-array-byte-offset/compare/v1.0.1...v1.0.2) - 2024-02-20
|
|
23
|
+
|
|
24
|
+
### Commits
|
|
25
|
+
|
|
26
|
+
- add types [`9eecdd2`](https://github.com/inspect-js/typed-array-byte-offset/commit/9eecdd245b089610d6ad49ef63c9df2b58c3e8a6)
|
|
27
|
+
- [actions] skip ls check on node < 10; remove redundant finisher [`4fb4c91`](https://github.com/inspect-js/typed-array-byte-offset/commit/4fb4c912f5eb8034f4e3705b30f3f7dcc7080039)
|
|
28
|
+
- [Deps] update `available-typed-arrays`, `has-proto` [`805cee2`](https://github.com/inspect-js/typed-array-byte-offset/commit/805cee207d73e12d526ff23d2c161f38283a1ed9)
|
|
29
|
+
|
|
8
30
|
## [v1.0.1](https://github.com/inspect-js/typed-array-byte-offset/compare/v1.0.0...v1.0.1) - 2024-02-17
|
|
9
31
|
|
|
10
32
|
### Commits
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
type TypedArray =
|
|
2
|
+
| Int8Array
|
|
3
|
+
| Uint8Array
|
|
4
|
+
| Uint8ClampedArray
|
|
5
|
+
| Int16Array
|
|
6
|
+
| Uint16Array
|
|
7
|
+
| Int32Array
|
|
8
|
+
| Uint32Array
|
|
9
|
+
| Float32Array
|
|
10
|
+
| Float64Array
|
|
11
|
+
| BigInt64Array
|
|
12
|
+
| BigUint64Array;
|
|
13
|
+
|
|
14
|
+
declare function typedArrayByteOffset(value: TypedArray): number;
|
|
15
|
+
declare function typedArrayByteOffset(value: unknown): false;
|
|
16
|
+
|
|
17
|
+
export = typedArrayByteOffset;
|
package/index.js
CHANGED
|
@@ -2,15 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
var forEach = require('for-each');
|
|
4
4
|
var callBind = require('call-bind');
|
|
5
|
+
var gPO = require('reflect.getprototypeof/polyfill')();
|
|
5
6
|
|
|
6
7
|
var typedArrays = require('available-typed-arrays')();
|
|
7
8
|
|
|
9
|
+
/** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray */
|
|
10
|
+
/** @typedef {(x: TypedArray) => number} ByteOffsetGetter */
|
|
11
|
+
|
|
12
|
+
/** @type {Object.<typeof typedArrays, ByteOffsetGetter>} */
|
|
8
13
|
var getters = {};
|
|
9
|
-
var hasProto = require('has-proto')();
|
|
10
14
|
|
|
11
15
|
var gOPD = require('gopd');
|
|
12
16
|
var oDP = Object.defineProperty;
|
|
13
17
|
if (gOPD) {
|
|
18
|
+
/** @type {ByteOffsetGetter} */
|
|
14
19
|
var getByteOffset = function (x) {
|
|
15
20
|
return x.byteOffset;
|
|
16
21
|
};
|
|
@@ -18,9 +23,11 @@ if (gOPD) {
|
|
|
18
23
|
// In Safari 7, Typed Array constructors are typeof object
|
|
19
24
|
if (typeof global[typedArray] === 'function' || typeof global[typedArray] === 'object') {
|
|
20
25
|
var Proto = global[typedArray].prototype;
|
|
26
|
+
// @ts-expect-error TS can't guarantee the callback is invoked sync
|
|
21
27
|
var descriptor = gOPD(Proto, 'byteOffset');
|
|
22
|
-
if (!descriptor
|
|
23
|
-
var superProto = Proto
|
|
28
|
+
if (!descriptor) {
|
|
29
|
+
var superProto = gPO(Proto);
|
|
30
|
+
// @ts-expect-error TS can't guarantee the callback is invoked sync
|
|
24
31
|
descriptor = gOPD(superProto, 'byteOffset');
|
|
25
32
|
}
|
|
26
33
|
// Opera 12.16 has a magic byteOffset data property on instances AND on Proto
|
|
@@ -29,6 +36,7 @@ if (gOPD) {
|
|
|
29
36
|
} else if (oDP) {
|
|
30
37
|
// this is likely an engine where instances have a magic byteOffset data property
|
|
31
38
|
var arr = new global[typedArray](2);
|
|
39
|
+
// @ts-expect-error TS can't guarantee the callback is invoked sync
|
|
32
40
|
descriptor = gOPD(arr, 'byteOffset');
|
|
33
41
|
if (descriptor && descriptor.configurable) {
|
|
34
42
|
oDP(arr, 'length', { value: 3 });
|
|
@@ -41,9 +49,10 @@ if (gOPD) {
|
|
|
41
49
|
});
|
|
42
50
|
}
|
|
43
51
|
|
|
52
|
+
/** @type {ByteOffsetGetter} */
|
|
44
53
|
var tryTypedArrays = function tryAllTypedArrays(value) {
|
|
45
|
-
var foundOffset;
|
|
46
|
-
forEach(getters, function (getter) {
|
|
54
|
+
/** @type {number} */ var foundOffset;
|
|
55
|
+
forEach(getters, /** @type {(getter: ByteOffsetGetter) => void} */ function (getter) {
|
|
47
56
|
if (typeof foundOffset !== 'number') {
|
|
48
57
|
try {
|
|
49
58
|
var offset = getter(value);
|
|
@@ -53,11 +62,13 @@ var tryTypedArrays = function tryAllTypedArrays(value) {
|
|
|
53
62
|
} catch (e) {}
|
|
54
63
|
}
|
|
55
64
|
});
|
|
65
|
+
// @ts-expect-error TS can't guarantee the callback is invoked sync
|
|
56
66
|
return foundOffset;
|
|
57
67
|
};
|
|
58
68
|
|
|
59
69
|
var isTypedArray = require('is-typed-array');
|
|
60
70
|
|
|
71
|
+
/** @type {import('.')} */
|
|
61
72
|
module.exports = function typedArrayByteOffset(value) {
|
|
62
73
|
if (!isTypedArray(value)) {
|
|
63
74
|
return false;
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typed-array-byte-offset",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Robustly get the byte offset of a Typed Array",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": "./index.js",
|
|
8
8
|
"./package.json": "./package.json"
|
|
9
9
|
},
|
|
10
|
+
"types": "./index.d.ts",
|
|
10
11
|
"sideEffects": false,
|
|
11
12
|
"scripts": {
|
|
12
13
|
"prepack": "npmignore --auto --commentLines=autogenerated",
|
|
@@ -15,9 +16,10 @@
|
|
|
15
16
|
"pretest": "npm run lint",
|
|
16
17
|
"prelint": "evalmd README.md",
|
|
17
18
|
"lint": "eslint --ext=js,mjs .",
|
|
19
|
+
"postlint": "tsc -p . && attw -P",
|
|
18
20
|
"tests-only": "nyc tape 'test/**/*.js'",
|
|
19
21
|
"test": "npm run tests-only",
|
|
20
|
-
"posttest": "
|
|
22
|
+
"posttest": "npx npm@'>=10.2' audit --production",
|
|
21
23
|
"version": "auto-changelog && git add CHANGELOG.md",
|
|
22
24
|
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
|
|
23
25
|
},
|
|
@@ -55,17 +57,28 @@
|
|
|
55
57
|
},
|
|
56
58
|
"homepage": "https://github.com/inspect-js/typed-array-byte-offset#readme",
|
|
57
59
|
"dependencies": {
|
|
58
|
-
"available-typed-arrays": "^1.0.
|
|
60
|
+
"available-typed-arrays": "^1.0.7",
|
|
59
61
|
"call-bind": "^1.0.7",
|
|
60
62
|
"for-each": "^0.3.3",
|
|
61
63
|
"gopd": "^1.0.1",
|
|
62
|
-
"has-proto": "^1.0.
|
|
63
|
-
"is-typed-array": "^1.1.13"
|
|
64
|
+
"has-proto": "^1.0.3",
|
|
65
|
+
"is-typed-array": "^1.1.13",
|
|
66
|
+
"reflect.getprototypeof": "^1.0.6"
|
|
64
67
|
},
|
|
65
68
|
"devDependencies": {
|
|
66
|
-
"@
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
+
"@arethetypeswrong/cli": "^0.17.0",
|
|
70
|
+
"@ljharb/eslint-config": "^21.1.1",
|
|
71
|
+
"@ljharb/tsconfig": "^0.2.0",
|
|
72
|
+
"@types/call-bind": "^1.0.5",
|
|
73
|
+
"@types/es-abstract": "^1.17.9",
|
|
74
|
+
"@types/for-each": "^0.3.3",
|
|
75
|
+
"@types/gopd": "^1.0.3",
|
|
76
|
+
"@types/is-callable": "^1.1.2",
|
|
77
|
+
"@types/make-arrow-function": "^1.2.2",
|
|
78
|
+
"@types/make-generator-function": "^2.0.3",
|
|
79
|
+
"@types/object-inspect": "^1.13.0",
|
|
80
|
+
"@types/tape": "^5.6.4",
|
|
81
|
+
"auto-changelog": "^2.5.0",
|
|
69
82
|
"eslint": "=8.8.0",
|
|
70
83
|
"evalmd": "^0.0.19",
|
|
71
84
|
"in-publish": "^2.0.1",
|
|
@@ -74,9 +87,11 @@
|
|
|
74
87
|
"make-generator-function": "^2.0.0",
|
|
75
88
|
"npmignore": "^0.3.1",
|
|
76
89
|
"nyc": "^10.3.2",
|
|
77
|
-
"object-inspect": "^1.13.
|
|
90
|
+
"object-inspect": "^1.13.3",
|
|
91
|
+
"possible-typed-array-names": "^1.0.0",
|
|
78
92
|
"safe-publish-latest": "^2.0.0",
|
|
79
|
-
"tape": "^5.
|
|
93
|
+
"tape": "^5.9.0",
|
|
94
|
+
"typescript": "next"
|
|
80
95
|
},
|
|
81
96
|
"engines": {
|
|
82
97
|
"node": ">= 0.4"
|
|
@@ -94,7 +109,8 @@
|
|
|
94
109
|
},
|
|
95
110
|
"publishConfig": {
|
|
96
111
|
"ignore": [
|
|
97
|
-
".github/workflows"
|
|
112
|
+
".github/workflows",
|
|
113
|
+
"types"
|
|
98
114
|
]
|
|
99
115
|
}
|
|
100
116
|
}
|
package/test/index.js
CHANGED
|
@@ -8,22 +8,11 @@ var arrowFn = require('make-arrow-function')();
|
|
|
8
8
|
var forEach = require('for-each');
|
|
9
9
|
var inspect = require('object-inspect');
|
|
10
10
|
|
|
11
|
-
var typedArrayNames =
|
|
12
|
-
'Int8Array',
|
|
13
|
-
'Uint8Array',
|
|
14
|
-
'Uint8ClampedArray',
|
|
15
|
-
'Int16Array',
|
|
16
|
-
'Uint16Array',
|
|
17
|
-
'Int32Array',
|
|
18
|
-
'Uint32Array',
|
|
19
|
-
'Float32Array',
|
|
20
|
-
'Float64Array',
|
|
21
|
-
'BigInt64Array',
|
|
22
|
-
'BigUint64Array'
|
|
23
|
-
];
|
|
11
|
+
var typedArrayNames = require('possible-typed-array-names');
|
|
24
12
|
|
|
25
13
|
test('not arrays', function (t) {
|
|
26
14
|
t.test('non-number/string primitives', function (st) {
|
|
15
|
+
// @ts-expect-error
|
|
27
16
|
st.equal(false, typedArrayByteOffset(), 'undefined is not typed array');
|
|
28
17
|
st.equal(false, typedArrayByteOffset(null), 'null is not typed array');
|
|
29
18
|
st.equal(false, typedArrayByteOffset(false), 'false is not typed array');
|
|
@@ -78,6 +67,7 @@ test('Typed Arrays', function (t) {
|
|
|
78
67
|
var buffer = new ArrayBuffer(length);
|
|
79
68
|
var TypedArray = global[typedArray];
|
|
80
69
|
if (isCallable(TypedArray)) {
|
|
70
|
+
// @ts-expect-error hush, TS, TAs can take an optional byte offset arg
|
|
81
71
|
var arr = new TypedArray(buffer, byteOffset);
|
|
82
72
|
t.equal(typedArrayByteOffset(arr), byteOffset, 'new ' + typedArray + '(new ArrayBuffer(' + length + '), ' + byteOffset + ') is typed array of byte offset ' + byteOffset);
|
|
83
73
|
} else {
|