@thi.ng/leb128 2.1.30 → 3.0.1
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 +14 -1
- package/README.md +23 -17
- package/binary.d.ts +2 -2
- package/binary.js +2 -2
- package/index.d.ts +4 -4
- package/index.js +15 -8
- package/package.json +11 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2022-
|
|
3
|
+
- **Last updated**: 2022-12-16T12:52:25Z
|
|
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,19 @@ 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.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/leb128@3.0.0) (2022-12-02)
|
|
13
|
+
|
|
14
|
+
#### 🛑 Breaking changes
|
|
15
|
+
|
|
16
|
+
- add bigint support ([0440c34](https://github.com/thi-ng/umbrella/commit/0440c34))
|
|
17
|
+
- BREAKING CHANGE: update decode return type
|
|
18
|
+
- update all encode to accept bigint or number, cast to correct u64/i64 range
|
|
19
|
+
- update decode result to [bigint, number] tuple
|
|
20
|
+
- rebuild binary (~400 bytes smaller)
|
|
21
|
+
- move zig source to /zig
|
|
22
|
+
- update tests
|
|
23
|
+
- update readme
|
|
24
|
+
|
|
12
25
|
## [2.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/leb128@2.1.0) (2021-11-17)
|
|
13
26
|
|
|
14
27
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
<!-- This file is generated - DO NOT EDIT! -->
|
|
2
2
|
|
|
3
|
-
# 
|
|
3
|
+
# 
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@thi.ng/leb128)
|
|
6
6
|

|
|
7
|
-
[](https://mastodon.thi.ng/@toxi)
|
|
8
8
|
|
|
9
9
|
This project is part of the
|
|
10
10
|
[@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo.
|
|
11
11
|
|
|
12
12
|
- [About](#about)
|
|
13
|
+
- [Breaking changes](#breaking-changes)
|
|
13
14
|
- [Status](#status)
|
|
14
15
|
- [Installation](#installation)
|
|
15
16
|
- [Dependencies](#dependencies)
|
|
@@ -22,14 +23,13 @@ This project is part of the
|
|
|
22
23
|
|
|
23
24
|
WASM based [Little Endian Base
|
|
24
25
|
128](https://en.wikipedia.org/wiki/LEB128) varint encoding / decoding,
|
|
25
|
-
supporting (u)int64 range
|
|
26
|
-
`MAX_SAFE_INTEGER`).
|
|
26
|
+
supporting the full (u)int64 range.
|
|
27
27
|
|
|
28
28
|
The WASM binary (~860 bytes) is embedded as base64 string in the
|
|
29
29
|
TypeScript source to make it easier to use in both browser & node
|
|
30
30
|
environments. The source code of the actual implementation (written in
|
|
31
31
|
[Zig](https://ziglang.org)) is included in
|
|
32
|
-
[/
|
|
32
|
+
[/zig/leb128.zig](https://github.com/thi-ng/umbrella/tree/develop/packages/leb128/zig/leb128.zig)
|
|
33
33
|
|
|
34
34
|
All public functions throw an error if the WASM module could not be
|
|
35
35
|
initialized.
|
|
@@ -39,6 +39,15 @@ References:
|
|
|
39
39
|
- https://en.wikipedia.org/wiki/LEB128
|
|
40
40
|
- http://webassembly.github.io/spec/core/binary/values.html#integers
|
|
41
41
|
|
|
42
|
+
## Breaking changes
|
|
43
|
+
|
|
44
|
+
v3.0.0 introduces JS `bigint` support and both decode functions return a tuple
|
|
45
|
+
of `[bigint, number]` with the `bigint` being the decoded value and the 2nd item
|
|
46
|
+
the number of bytes consumed. Simarly, the encode functions now accept a JS
|
|
47
|
+
number or bigint arg.
|
|
48
|
+
|
|
49
|
+
Furthermore, all values to be encoded/decoded are cast to i64/u64 range now.
|
|
50
|
+
|
|
42
51
|
## Status
|
|
43
52
|
|
|
44
53
|
**STABLE** - used in production
|
|
@@ -61,14 +70,11 @@ ES module import:
|
|
|
61
70
|
|
|
62
71
|
For Node.js REPL:
|
|
63
72
|
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
node --experimental-repl-await
|
|
67
|
-
|
|
68
|
-
> const leb128 = await import("@thi.ng/leb128");
|
|
73
|
+
```js
|
|
74
|
+
const leb128 = await import("@thi.ng/leb128");
|
|
69
75
|
```
|
|
70
76
|
|
|
71
|
-
Package sizes (brotli'd, pre-treeshake): ESM:
|
|
77
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 876 bytes
|
|
72
78
|
|
|
73
79
|
## Dependencies
|
|
74
80
|
|
|
@@ -87,16 +93,16 @@ import * as leb from "@thi.ng/leb128";
|
|
|
87
93
|
enc = leb.encodeULEB128(Number.MAX_SAFE_INTEGER);
|
|
88
94
|
// Uint8Array [ 255, 255, 255, 255, 255, 255, 255, 15 ]
|
|
89
95
|
|
|
90
|
-
// decoding returns tuple of [value, bytes consumed]
|
|
96
|
+
// decoding returns tuple of [value (bigint), bytes consumed]
|
|
91
97
|
leb.decodeULEB128(enc);
|
|
92
|
-
// [
|
|
98
|
+
// [ 9007199254740991n, 8 ]
|
|
93
99
|
|
|
94
100
|
// encode signed int
|
|
95
101
|
enc = leb.encodeSLEB128(Number.MIN_SAFE_INTEGER)
|
|
96
102
|
// Uint8Array [ 129, 128, 128, 128, 128, 128, 128, 112 ]
|
|
97
103
|
|
|
98
104
|
leb.decodeSLEB128(enc)
|
|
99
|
-
// [ -
|
|
105
|
+
// [ -9007199254740991n, 8 ]
|
|
100
106
|
```
|
|
101
107
|
|
|
102
108
|
## Building the binary
|
|
@@ -111,7 +117,7 @@ Requirements:
|
|
|
111
117
|
brew install zig binaryen
|
|
112
118
|
|
|
113
119
|
# first run native tests
|
|
114
|
-
zig test
|
|
120
|
+
zig test zig/leb128.zig
|
|
115
121
|
# Test 1/2 min safe integer...OK
|
|
116
122
|
# Test 2/2 max safe integer...OK
|
|
117
123
|
# All tests passed.
|
|
@@ -125,7 +131,7 @@ yarn test
|
|
|
125
131
|
|
|
126
132
|
## Authors
|
|
127
133
|
|
|
128
|
-
Karsten Schmidt
|
|
134
|
+
- [Karsten Schmidt](https://thi.ng)
|
|
129
135
|
|
|
130
136
|
If this project contributes to an academic publication, please cite it as:
|
|
131
137
|
|
|
@@ -140,4 +146,4 @@ If this project contributes to an academic publication, please cite it as:
|
|
|
140
146
|
|
|
141
147
|
## License
|
|
142
148
|
|
|
143
|
-
© 2019 - 2022 Karsten Schmidt // Apache
|
|
149
|
+
© 2019 - 2022 Karsten Schmidt // Apache License 2.0
|
package/binary.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Generated @
|
|
2
|
+
* Generated @ 2022-12-02T12:00:53Z
|
|
3
3
|
*
|
|
4
4
|
* @internal
|
|
5
5
|
*/
|
|
6
|
-
export declare const BINARY = "
|
|
6
|
+
export declare const BINARY = "AGFzbQEAAAABCgJgAX4Bf2AAAX4DBQQAAQABBQMBABEGCQF/AEGAgMAACwdYBgZtZW1vcnkCAA9sZWIxMjhFbmNvZGVVNjQAAANidWYDAA9sZWIxMjhEZWNvZGVVNjQAAQ9sZWIxMjhFbmNvZGVJNjQAAg9sZWIxMjhEZWNvZGVJNjQAAwrBAwRbAQJ/AkAgAEKAAVoEQANAIAFBgIBAayAAp0H/AHEgAEL/AFZBB3RyOgAAIAFBAWohASAAQoABVCAAQgeIIQBFDQALDAELQYCAwAAgADwAAEEBIQELIAFB/wFxC1ECA38CfgNAAkAgAEEBaiECIABBgIBAaywAACIBQf8Aca0gA4YgBIQhBCABQQBODQAgA0IHfCEDIABBCUkgAiEADQELC0GAgMAAIAI6AAAgBAuRAQEDfwJAIABCQH1CgAFaBEBBASECA0AgAkEBcUUNAiABQYCAQGtBAEGAfyAApyICQcAAcSIDRSAAQoABVHEgA0EGdiAAQgeHIgBCf1FxciIDGyACQf8AcXI6AAAgA0UhAiABQQFqIQEMAAsAC0GAgMAAIABCOYinQcAAcSAAp0E/cXI6AABBASEBCyABQf8BcQt+AgN/A35BfyEAA0ACQCAAQQFqIQEgA0IHfCEFIABBgYDAAGotAAAiAEH/AHGtIAOGIASEIQQgAMAiAkEATg0AIAEhACAFIQMgAUEJSQ0BCwtBgIDAACABQQFqOgAAIARCfyAFhkIAIAJBwABxQQZ2G0IAIAFB/wFxQQlJG4QL";
|
|
7
7
|
//# sourceMappingURL=binary.d.ts.map
|
package/binary.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// thing:no-export
|
|
2
2
|
/**
|
|
3
|
-
* Generated @
|
|
3
|
+
* Generated @ 2022-12-02T12:00:53Z
|
|
4
4
|
*
|
|
5
5
|
* @internal
|
|
6
6
|
*/
|
|
7
|
-
export const BINARY = "
|
|
7
|
+
export const BINARY = "AGFzbQEAAAABCgJgAX4Bf2AAAX4DBQQAAQABBQMBABEGCQF/AEGAgMAACwdYBgZtZW1vcnkCAA9sZWIxMjhFbmNvZGVVNjQAAANidWYDAA9sZWIxMjhEZWNvZGVVNjQAAQ9sZWIxMjhFbmNvZGVJNjQAAg9sZWIxMjhEZWNvZGVJNjQAAwrBAwRbAQJ/AkAgAEKAAVoEQANAIAFBgIBAayAAp0H/AHEgAEL/AFZBB3RyOgAAIAFBAWohASAAQoABVCAAQgeIIQBFDQALDAELQYCAwAAgADwAAEEBIQELIAFB/wFxC1ECA38CfgNAAkAgAEEBaiECIABBgIBAaywAACIBQf8Aca0gA4YgBIQhBCABQQBODQAgA0IHfCEDIABBCUkgAiEADQELC0GAgMAAIAI6AAAgBAuRAQEDfwJAIABCQH1CgAFaBEBBASECA0AgAkEBcUUNAiABQYCAQGtBAEGAfyAApyICQcAAcSIDRSAAQoABVHEgA0EGdiAAQgeHIgBCf1FxciIDGyACQf8AcXI6AAAgA0UhAiABQQFqIQEMAAsAC0GAgMAAIABCOYinQcAAcSAAp0E/cXI6AABBASEBCyABQf8BcQt+AgN/A35BfyEAA0ACQCAAQQFqIQEgA0IHfCEFIABBgYDAAGotAAAiAEH/AHGtIAOGIASEIQQgAMAiAkEATg0AIAEhACAFIQMgAUEJSQ0BCwtBgIDAACABQQFqOgAAIARCfyAFhkIAIAJBwABxQQZ2G0IAIAFB/wFxQQlJG4QL";
|
package/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
* @param x -
|
|
6
6
|
*/
|
|
7
|
-
export declare const encodeSLEB128: (x: number) => Uint8Array;
|
|
7
|
+
export declare const encodeSLEB128: (x: bigint | number) => Uint8Array;
|
|
8
8
|
/**
|
|
9
9
|
* Takes Uint8Array with LEB128 encoded signed varint and an optional
|
|
10
10
|
* start index to decode from. Returns 2-tuple of decoded value and
|
|
@@ -13,14 +13,14 @@ export declare const encodeSLEB128: (x: number) => Uint8Array;
|
|
|
13
13
|
* @param src -
|
|
14
14
|
* @param idx -
|
|
15
15
|
*/
|
|
16
|
-
export declare const decodeSLEB128: (src: Uint8Array, idx?: number) => number
|
|
16
|
+
export declare const decodeSLEB128: (src: Uint8Array, idx?: number) => [bigint, number];
|
|
17
17
|
/**
|
|
18
18
|
* Encodes unsigned integer `x` into LEB128 varint format and returns
|
|
19
19
|
* encoded bytes. Values < 0 will be encoded as zero.
|
|
20
20
|
*
|
|
21
21
|
* @param x -
|
|
22
22
|
*/
|
|
23
|
-
export declare const encodeULEB128: (x: number) => Uint8Array;
|
|
23
|
+
export declare const encodeULEB128: (x: bigint | number) => Uint8Array;
|
|
24
24
|
/**
|
|
25
25
|
* Takes Uint8Array with LEB128 encoded unsigned varint and an optional
|
|
26
26
|
* start index to decode from. Returns 2-tuple of decoded value and
|
|
@@ -29,5 +29,5 @@ export declare const encodeULEB128: (x: number) => Uint8Array;
|
|
|
29
29
|
* @param src -
|
|
30
30
|
* @param idx -
|
|
31
31
|
*/
|
|
32
|
-
export declare const decodeULEB128: (src: Uint8Array, idx?: number) => number
|
|
32
|
+
export declare const decodeULEB128: (src: Uint8Array, idx?: number) => [bigint, number];
|
|
33
33
|
//# sourceMappingURL=index.d.ts.map
|
package/index.js
CHANGED
|
@@ -11,14 +11,21 @@ if (hasWASM()) {
|
|
|
11
11
|
U8 = new Uint8Array(wasm.memory.buffer, wasm.buf, 16);
|
|
12
12
|
}
|
|
13
13
|
const ensureWASM = () => !wasm && unsupported("WASM module unavailable");
|
|
14
|
-
const encode = (op) => (x) => {
|
|
14
|
+
const encode = (op, signed) => (x) => {
|
|
15
15
|
ensureWASM();
|
|
16
|
-
|
|
16
|
+
const value = signed
|
|
17
|
+
? BigInt.asIntN(64, BigInt(x))
|
|
18
|
+
: BigInt.asUintN(64, BigInt(x));
|
|
19
|
+
return U8.slice(0, wasm[op](value));
|
|
17
20
|
};
|
|
18
|
-
const decode = (op) => (src, idx = 0) => {
|
|
21
|
+
const decode = (op, signed) => (src, idx = 0) => {
|
|
19
22
|
ensureWASM();
|
|
20
23
|
U8.set(src.subarray(idx, Math.min(idx + 10, src.length)), 0);
|
|
21
|
-
|
|
24
|
+
const value = wasm[op](0, 0);
|
|
25
|
+
return [
|
|
26
|
+
signed ? BigInt.asIntN(64, value) : BigInt.asUintN(64, value),
|
|
27
|
+
U8[0],
|
|
28
|
+
];
|
|
22
29
|
};
|
|
23
30
|
/**
|
|
24
31
|
* Encodes signed integer `x` into LEB128 varint format and returns
|
|
@@ -26,7 +33,7 @@ const decode = (op) => (src, idx = 0) => {
|
|
|
26
33
|
*
|
|
27
34
|
* @param x -
|
|
28
35
|
*/
|
|
29
|
-
export const encodeSLEB128 = encode("
|
|
36
|
+
export const encodeSLEB128 = encode("leb128EncodeI64", true);
|
|
30
37
|
/**
|
|
31
38
|
* Takes Uint8Array with LEB128 encoded signed varint and an optional
|
|
32
39
|
* start index to decode from. Returns 2-tuple of decoded value and
|
|
@@ -35,14 +42,14 @@ export const encodeSLEB128 = encode("leb128_encode_s_js");
|
|
|
35
42
|
* @param src -
|
|
36
43
|
* @param idx -
|
|
37
44
|
*/
|
|
38
|
-
export const decodeSLEB128 = decode("
|
|
45
|
+
export const decodeSLEB128 = decode("leb128DecodeI64", true);
|
|
39
46
|
/**
|
|
40
47
|
* Encodes unsigned integer `x` into LEB128 varint format and returns
|
|
41
48
|
* encoded bytes. Values < 0 will be encoded as zero.
|
|
42
49
|
*
|
|
43
50
|
* @param x -
|
|
44
51
|
*/
|
|
45
|
-
export const encodeULEB128 = encode("
|
|
52
|
+
export const encodeULEB128 = encode("leb128EncodeU64", false);
|
|
46
53
|
/**
|
|
47
54
|
* Takes Uint8Array with LEB128 encoded unsigned varint and an optional
|
|
48
55
|
* start index to decode from. Returns 2-tuple of decoded value and
|
|
@@ -51,4 +58,4 @@ export const encodeULEB128 = encode("leb128_encode_u_js");
|
|
|
51
58
|
* @param src -
|
|
52
59
|
* @param idx -
|
|
53
60
|
*/
|
|
54
|
-
export const decodeULEB128 = decode("
|
|
61
|
+
export const decodeULEB128 = decode("leb128DecodeU64", false);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/leb128",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "WASM based LEB128 encoder / decoder (signed & unsigned)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"url": "https://patreon.com/thing_umbrella"
|
|
22
22
|
}
|
|
23
23
|
],
|
|
24
|
-
"author": "Karsten Schmidt
|
|
24
|
+
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
27
|
"build": "yarn clean && tsc --declaration",
|
|
@@ -35,17 +35,17 @@
|
|
|
35
35
|
"test": "testament test"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@thi.ng/checks": "^3.3.
|
|
39
|
-
"@thi.ng/errors": "^2.2.
|
|
40
|
-
"@thi.ng/transducers-binary": "^2.1.
|
|
38
|
+
"@thi.ng/checks": "^3.3.5",
|
|
39
|
+
"@thi.ng/errors": "^2.2.6",
|
|
40
|
+
"@thi.ng/transducers-binary": "^2.1.31"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@microsoft/api-extractor": "^7.33.
|
|
44
|
-
"@thi.ng/testament": "^0.3.
|
|
43
|
+
"@microsoft/api-extractor": "^7.33.7",
|
|
44
|
+
"@thi.ng/testament": "^0.3.7",
|
|
45
45
|
"rimraf": "^3.0.2",
|
|
46
46
|
"tools": "^0.0.1",
|
|
47
|
-
"typedoc": "^0.23.
|
|
48
|
-
"typescript": "^4.
|
|
47
|
+
"typedoc": "^0.23.22",
|
|
48
|
+
"typescript": "^4.9.4"
|
|
49
49
|
},
|
|
50
50
|
"keywords": [
|
|
51
51
|
"64bit",
|
|
@@ -58,6 +58,7 @@
|
|
|
58
58
|
"varint",
|
|
59
59
|
"wasm",
|
|
60
60
|
"webassembly",
|
|
61
|
+
"zig",
|
|
61
62
|
"ziglang"
|
|
62
63
|
],
|
|
63
64
|
"publishConfig": {
|
|
@@ -78,5 +79,5 @@
|
|
|
78
79
|
"thi.ng": {
|
|
79
80
|
"year": 2019
|
|
80
81
|
},
|
|
81
|
-
"gitHead": "
|
|
82
|
+
"gitHead": "f445a9cc8022bcdebbf6ff91fd66ced016d72f01\n"
|
|
82
83
|
}
|