@thi.ng/leb128 3.0.87 → 3.1.0
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 +11 -1
- package/README.md +29 -10
- package/binary.d.ts +1 -1
- package/index.d.ts +36 -10
- package/index.js +14 -1
- package/package.json +8 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-04-
|
|
3
|
+
- **Last updated**: 2024-04-20T14:42:45Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,16 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [3.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/leb128@3.1.0) (2024-04-20)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- update docs, add encode[SU]LEB128Into() fns ([44f927b](https://github.com/thi-ng/umbrella/commit/44f927b))
|
|
17
|
+
- update/fix doc strings
|
|
18
|
+
- retroactively document new functions by @jtenner's PR ([#460](https://github.com/thi-ng/umbrella/issues/460))
|
|
19
|
+
- add encodeSLEB128Into()
|
|
20
|
+
- add encodeULEB128Into()
|
|
21
|
+
|
|
12
22
|
### [3.0.47](https://github.com/thi-ng/umbrella/tree/@thi.ng/leb128@3.0.47) (2023-11-09)
|
|
13
23
|
|
|
14
24
|
#### ♻️ Refactoring
|
package/README.md
CHANGED
|
@@ -39,6 +39,10 @@ environments. The source code of the actual implementation (written in
|
|
|
39
39
|
All public functions throw an error if the WASM module could not be
|
|
40
40
|
initialized.
|
|
41
41
|
|
|
42
|
+
The `encodeSLEB128Into()` and `encodeULEB128Into()` functions will check the
|
|
43
|
+
bounds of the target array to ensure all bytes can be written and will
|
|
44
|
+
throw an error if the result would go out of bounds.
|
|
45
|
+
|
|
42
46
|
References:
|
|
43
47
|
|
|
44
48
|
- https://en.wikipedia.org/wiki/LEB128
|
|
@@ -74,10 +78,10 @@ import * as leb from "@thi.ng/leb128";
|
|
|
74
78
|
Browser ESM import:
|
|
75
79
|
|
|
76
80
|
```html
|
|
77
|
-
<script type="module" src="https://
|
|
81
|
+
<script type="module" src="https://esm.run/@thi.ng/leb128"></script>
|
|
78
82
|
```
|
|
79
83
|
|
|
80
|
-
[
|
|
84
|
+
[JSDelivr documentation](https://www.jsdelivr.com/)
|
|
81
85
|
|
|
82
86
|
For Node.js REPL:
|
|
83
87
|
|
|
@@ -85,7 +89,7 @@ For Node.js REPL:
|
|
|
85
89
|
const leb = await import("@thi.ng/leb128");
|
|
86
90
|
```
|
|
87
91
|
|
|
88
|
-
Package sizes (brotli'd, pre-treeshake): ESM:
|
|
92
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 968 bytes
|
|
89
93
|
|
|
90
94
|
## Dependencies
|
|
91
95
|
|
|
@@ -97,23 +101,37 @@ Package sizes (brotli'd, pre-treeshake): ESM: 880 bytes
|
|
|
97
101
|
|
|
98
102
|
[Generated API docs](https://docs.thi.ng/umbrella/leb128/)
|
|
99
103
|
|
|
100
|
-
```ts
|
|
104
|
+
```ts tangle:export/readme1.ts
|
|
101
105
|
import * as leb from "@thi.ng/leb128";
|
|
102
106
|
|
|
103
107
|
// if WASM is unavailable, the encode/decode functions will throw an error
|
|
104
|
-
|
|
108
|
+
let encoded = leb.encodeULEB128(Number.MAX_SAFE_INTEGER);
|
|
109
|
+
|
|
110
|
+
console.log(encoded);
|
|
105
111
|
// Uint8Array [ 255, 255, 255, 255, 255, 255, 255, 15 ]
|
|
106
112
|
|
|
107
113
|
// decoding returns tuple of [value (bigint), bytes consumed]
|
|
108
|
-
leb.decodeULEB128(
|
|
114
|
+
console.log(leb.decodeULEB128(encoded));
|
|
109
115
|
// [ 9007199254740991n, 8 ]
|
|
110
116
|
|
|
111
117
|
// encode signed int
|
|
112
|
-
|
|
118
|
+
encoded = leb.encodeSLEB128(Number.MIN_SAFE_INTEGER);
|
|
119
|
+
|
|
120
|
+
console.log(encoded)
|
|
113
121
|
// Uint8Array [ 129, 128, 128, 128, 128, 128, 128, 112 ]
|
|
114
122
|
|
|
115
|
-
leb.decodeSLEB128(
|
|
123
|
+
console.log(leb.decodeSLEB128(encoded));
|
|
116
124
|
// [ -9007199254740991n, 8 ]
|
|
125
|
+
|
|
126
|
+
// when writing into an existing buffer, there needs to be enough bytes to write the value
|
|
127
|
+
const target = new Uint8Array(10);
|
|
128
|
+
const count = leb.encodeULEB128Into(target, Number.MAX_SAFE_INTEGER);
|
|
129
|
+
|
|
130
|
+
console.log(target);
|
|
131
|
+
// Uint8Array [ 255, 255, 255, 255, 255, 255, 255, 15, 0, 0 ]
|
|
132
|
+
|
|
133
|
+
console.log(count);
|
|
134
|
+
// 8
|
|
117
135
|
```
|
|
118
136
|
|
|
119
137
|
## Building the binary
|
|
@@ -142,14 +160,15 @@ yarn test
|
|
|
142
160
|
|
|
143
161
|
## Authors
|
|
144
162
|
|
|
145
|
-
- [Karsten Schmidt](https://thi.ng)
|
|
163
|
+
- [Karsten Schmidt](https://thi.ng) (Main author)
|
|
164
|
+
- [jtenner](https://github.com/jtenner)
|
|
146
165
|
|
|
147
166
|
If this project contributes to an academic publication, please cite it as:
|
|
148
167
|
|
|
149
168
|
```bibtex
|
|
150
169
|
@misc{thing-leb128,
|
|
151
170
|
title = "@thi.ng/leb128",
|
|
152
|
-
author = "Karsten Schmidt",
|
|
171
|
+
author = "Karsten Schmidt and others",
|
|
153
172
|
note = "https://thi.ng/leb128",
|
|
154
173
|
year = 2019
|
|
155
174
|
}
|
package/binary.d.ts
CHANGED
package/index.d.ts
CHANGED
|
@@ -1,33 +1,59 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Encodes signed integer `x` into LEB128 varint format and returns
|
|
3
|
-
*
|
|
2
|
+
* Encodes signed integer `x` into LEB128 varint format and returns encoded
|
|
3
|
+
* bytes. Values will be coerced to i64 range prior to encoding.
|
|
4
4
|
*
|
|
5
5
|
* @param x -
|
|
6
6
|
*/
|
|
7
7
|
export declare const encodeSLEB128: (x: bigint | number) => Uint8Array;
|
|
8
8
|
/**
|
|
9
|
-
* Takes Uint8Array with LEB128 encoded signed varint and an optional
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* Takes an `Uint8Array` with LEB128 encoded signed varint and an optional start
|
|
10
|
+
* index to decode from. Returns 2-tuple of decoded value and number of bytes
|
|
11
|
+
* consumed. Consumes up to 10 bytes from `src`.
|
|
12
12
|
*
|
|
13
13
|
* @param src -
|
|
14
14
|
* @param idx -
|
|
15
15
|
*/
|
|
16
16
|
export declare const decodeSLEB128: (src: Uint8Array, idx?: number) => [bigint, number];
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
18
|
+
* Takes a destination `Uint8Array`, a signed integer `x`, and an optional
|
|
19
|
+
* index/position to encode an LEB128 formatted byte sequence into the
|
|
20
|
+
* destination. Returns the number of bytes written.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* Also see {@link encodeSLEB128}.
|
|
24
|
+
*
|
|
25
|
+
* @param dst -
|
|
26
|
+
* @param x -
|
|
27
|
+
* @param pos -
|
|
28
|
+
*/
|
|
29
|
+
export declare const encodeSLEB128Into: (dst: Uint8Array, x: bigint | number, pos?: number) => number;
|
|
30
|
+
/**
|
|
31
|
+
* Encodes unsigned integer `x` into LEB128 varint format and returns encoded
|
|
32
|
+
* bytes. Values will be coerced to u64 range prior to encoding.
|
|
20
33
|
*
|
|
21
34
|
* @param x -
|
|
22
35
|
*/
|
|
23
36
|
export declare const encodeULEB128: (x: bigint | number) => Uint8Array;
|
|
24
37
|
/**
|
|
25
|
-
* Takes Uint8Array with LEB128 encoded unsigned varint and an optional
|
|
26
|
-
* start index to decode from. Returns 2-tuple of decoded value and
|
|
27
|
-
*
|
|
38
|
+
* Takes an `Uint8Array` with LEB128 encoded unsigned varint and an optional
|
|
39
|
+
* start index to decode from. Returns 2-tuple of decoded value and number of
|
|
40
|
+
* bytes consumed. Consumes up to 10 bytes from `src`.
|
|
28
41
|
*
|
|
29
42
|
* @param src -
|
|
30
43
|
* @param idx -
|
|
31
44
|
*/
|
|
32
45
|
export declare const decodeULEB128: (src: Uint8Array, idx?: number) => [bigint, number];
|
|
46
|
+
/**
|
|
47
|
+
* Takes a destination Uint8Array, an unsigned integer `x`, and an optional
|
|
48
|
+
* position to encode an LEB128 formatted byte sequence into the destination.
|
|
49
|
+
* Returns the number of bytes written.
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* Also see {@link encodeULEB128}.
|
|
53
|
+
*
|
|
54
|
+
* @param dst -
|
|
55
|
+
* @param x -
|
|
56
|
+
* @param pos -
|
|
57
|
+
*/
|
|
58
|
+
export declare const encodeULEB128Into: (dst: Uint8Array, x: bigint | number, pos?: number) => number;
|
|
33
59
|
//# sourceMappingURL=index.d.ts.map
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { hasWASM } from "@thi.ng/checks/has-wasm";
|
|
2
2
|
import { unsupported } from "@thi.ng/errors/unsupported";
|
|
3
|
+
import { ensureIndex } from "@thi.ng/errors/out-of-bounds";
|
|
3
4
|
import { base64Decode } from "@thi.ng/transducers-binary/base64";
|
|
4
5
|
import { BINARY } from "./binary.js";
|
|
5
6
|
let wasm;
|
|
@@ -17,6 +18,14 @@ const encode = (op, signed) => (x) => {
|
|
|
17
18
|
const value = signed ? BigInt.asIntN(64, BigInt(x)) : BigInt.asUintN(64, BigInt(x));
|
|
18
19
|
return U8.slice(0, wasm[op](value));
|
|
19
20
|
};
|
|
21
|
+
const encodeInto = (op, signed) => (dst, x, pos = 0) => {
|
|
22
|
+
ensureWASM();
|
|
23
|
+
const value = signed ? BigInt.asIntN(64, BigInt(x)) : BigInt.asUintN(64, BigInt(x));
|
|
24
|
+
const size = wasm[op](value);
|
|
25
|
+
ensureIndex(pos, 0, dst.length - size + 1);
|
|
26
|
+
dst.set(U8.subarray(0, size), pos);
|
|
27
|
+
return size;
|
|
28
|
+
};
|
|
20
29
|
const decode = (op, signed) => (src, idx = 0) => {
|
|
21
30
|
ensureWASM();
|
|
22
31
|
U8.set(src.subarray(idx, Math.min(idx + 10, src.length)), 0);
|
|
@@ -28,11 +37,15 @@ const decode = (op, signed) => (src, idx = 0) => {
|
|
|
28
37
|
};
|
|
29
38
|
const encodeSLEB128 = encode("leb128EncodeI64", true);
|
|
30
39
|
const decodeSLEB128 = decode("leb128DecodeI64", true);
|
|
40
|
+
const encodeSLEB128Into = encodeInto("leb128EncodeI64", true);
|
|
31
41
|
const encodeULEB128 = encode("leb128EncodeU64", false);
|
|
32
42
|
const decodeULEB128 = decode("leb128DecodeU64", false);
|
|
43
|
+
const encodeULEB128Into = encodeInto("leb128EncodeU64", true);
|
|
33
44
|
export {
|
|
34
45
|
decodeSLEB128,
|
|
35
46
|
decodeULEB128,
|
|
36
47
|
encodeSLEB128,
|
|
37
|
-
|
|
48
|
+
encodeSLEB128Into,
|
|
49
|
+
encodeULEB128,
|
|
50
|
+
encodeULEB128Into
|
|
38
51
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/leb128",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "WASM based LEB128 encoder / decoder (signed & unsigned)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -22,6 +22,9 @@
|
|
|
22
22
|
}
|
|
23
23
|
],
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
|
+
"contributors": [
|
|
26
|
+
"jtenner (https://github.com/jtenner)"
|
|
27
|
+
],
|
|
25
28
|
"license": "Apache-2.0",
|
|
26
29
|
"scripts": {
|
|
27
30
|
"build": "yarn build:esbuild && yarn build:decl",
|
|
@@ -37,9 +40,9 @@
|
|
|
37
40
|
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
38
41
|
},
|
|
39
42
|
"dependencies": {
|
|
40
|
-
"@thi.ng/checks": "^3.6.
|
|
41
|
-
"@thi.ng/errors": "^2.5.
|
|
42
|
-
"@thi.ng/transducers-binary": "^2.1.
|
|
43
|
+
"@thi.ng/checks": "^3.6.2",
|
|
44
|
+
"@thi.ng/errors": "^2.5.5",
|
|
45
|
+
"@thi.ng/transducers-binary": "^2.1.117"
|
|
43
46
|
},
|
|
44
47
|
"devDependencies": {
|
|
45
48
|
"@microsoft/api-extractor": "^7.43.0",
|
|
@@ -82,5 +85,5 @@
|
|
|
82
85
|
"alias": "leb",
|
|
83
86
|
"year": 2019
|
|
84
87
|
},
|
|
85
|
-
"gitHead": "
|
|
88
|
+
"gitHead": "8339d05ecc857e529c7325a9839c0063b89e728d\n"
|
|
86
89
|
}
|