@thi.ng/leb128 2.1.29 → 3.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-11-23T22:46:54Z
3
+ - **Last updated**: 2022-12-02T14:13:34Z
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
@@ -10,6 +10,7 @@ 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 (however for JS purposes only up to
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
- [/src/leb128.zig](https://github.com/thi-ng/umbrella/tree/develop/packages/leb128/src/leb128.zig)
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
@@ -68,7 +77,7 @@ node --experimental-repl-await
68
77
  > const leb128 = await import("@thi.ng/leb128");
69
78
  ```
70
79
 
71
- Package sizes (gzipped, pre-treeshake): ESM: 1.07 KB
80
+ Package sizes (brotli'd, pre-treeshake): ESM: 876 bytes
72
81
 
73
82
  ## Dependencies
74
83
 
@@ -87,16 +96,16 @@ import * as leb from "@thi.ng/leb128";
87
96
  enc = leb.encodeULEB128(Number.MAX_SAFE_INTEGER);
88
97
  // Uint8Array [ 255, 255, 255, 255, 255, 255, 255, 15 ]
89
98
 
90
- // decoding returns tuple of [value, bytes consumed]
99
+ // decoding returns tuple of [value (bigint), bytes consumed]
91
100
  leb.decodeULEB128(enc);
92
- // [ 9007199254740991, 8 ]
101
+ // [ 9007199254740991n, 8 ]
93
102
 
94
103
  // encode signed int
95
104
  enc = leb.encodeSLEB128(Number.MIN_SAFE_INTEGER)
96
105
  // Uint8Array [ 129, 128, 128, 128, 128, 128, 128, 112 ]
97
106
 
98
107
  leb.decodeSLEB128(enc)
99
- // [ -9007199254740991, 8 ]
108
+ // [ -9007199254740991n, 8 ]
100
109
  ```
101
110
 
102
111
  ## Building the binary
@@ -111,7 +120,7 @@ Requirements:
111
120
  brew install zig binaryen
112
121
 
113
122
  # first run native tests
114
- zig test packages/leb128/src/leb128.zig
123
+ zig test zig/leb128.zig
115
124
  # Test 1/2 min safe integer...OK
116
125
  # Test 2/2 max safe integer...OK
117
126
  # All tests passed.
package/binary.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Generated @ 2021-09-27T18:47:14Z
2
+ * Generated @ 2022-12-02T12:05:56Z
3
3
  *
4
4
  * @internal
5
5
  */
6
- export declare const BINARY = "AGFzbQEAAAABDQNgAXwBf2AAAXxgAAADBgUCAAEAAQUDAQACBioHfwBBgAgLfwBBgAgLfwBBiggLfwBBgAgLfwBBkIgEC38AQQALfwBBAQsH0QENBm1lbW9yeQIAEV9fd2FzbV9jYWxsX2N0b3JzAAASbGViMTI4X2VuY29kZV91X2pzAAEDYnVmAwASbGViMTI4X2RlY29kZV91X2pzAAISbGViMTI4X2VuY29kZV9zX2pzAAMSbGViMTI4X2RlY29kZV9zX2pzAAQMX19kc29faGFuZGxlAwEKX19kYXRhX2VuZAMCDV9fZ2xvYmFsX2Jhc2UDAwtfX2hlYXBfYmFzZQMEDV9fbWVtb3J5X2Jhc2UDBQxfX3RhYmxlX2Jhc2UDBgqWBAUDAAELegICfwF+AkACfiAARAAAAAAAAPBDYyAARAAAAAAAAAAAZnEEQCAAsQwBC0IACyIDQoABWgRAA0AgAUGACGogA6dB/wBxIANCB4giA0IAUiICQQd0cjoAACABQQFqIQEgAg0ACwwBC0GACCADPAAAQQEhAQsgAUH/AXELWwIDfwJ+QXYhAANAAkAgAEUEQEEKIQEMAQsgAUEBaiEBIABBighqLAAAIgJB/wBxrSADhiAEhCEEIABBAWohACADQgd8IQMgAkEASA0BC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ABQf8BcQt+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 @ 2021-09-27T18:47:14Z
3
+ * Generated @ 2022-12-02T12:05:56Z
4
4
  *
5
5
  * @internal
6
6
  */
7
- export const BINARY = "AGFzbQEAAAABDQNgAXwBf2AAAXxgAAADBgUCAAEAAQUDAQACBioHfwBBgAgLfwBBgAgLfwBBiggLfwBBgAgLfwBBkIgEC38AQQALfwBBAQsH0QENBm1lbW9yeQIAEV9fd2FzbV9jYWxsX2N0b3JzAAASbGViMTI4X2VuY29kZV91X2pzAAEDYnVmAwASbGViMTI4X2RlY29kZV91X2pzAAISbGViMTI4X2VuY29kZV9zX2pzAAMSbGViMTI4X2RlY29kZV9zX2pzAAQMX19kc29faGFuZGxlAwEKX19kYXRhX2VuZAMCDV9fZ2xvYmFsX2Jhc2UDAwtfX2hlYXBfYmFzZQMEDV9fbWVtb3J5X2Jhc2UDBQxfX3RhYmxlX2Jhc2UDBgqWBAUDAAELegICfwF+AkACfiAARAAAAAAAAPBDYyAARAAAAAAAAAAAZnEEQCAAsQwBC0IACyIDQoABWgRAA0AgAUGACGogA6dB/wBxIANCB4giA0IAUiICQQd0cjoAACABQQFqIQEgAg0ACwwBC0GACCADPAAAQQEhAQsgAUH/AXELWwIDfwJ+QXYhAANAAkAgAEUEQEEKIQEMAQsgAUEBaiEBIABBighqLAAAIgJB/wBxrSADhiAEhCEEIABBAWohACADQgd8IQMgAkEASA0BCwtBgAggAToAACAEugu7AQIBfgR/AkACfiAAmUQAAAAAAADgQ2MEQCAAsAwBC0KAgICAgICAgIB/CyI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IQQgAMAiAkEATg0AIAEhACAFIQMgAUEJSQ0BCwtBgIDAACABQQFqOgAAIARCfyAFhkIAIAJBwABxQQZ2G0IAIAFB/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
- return U8.slice(0, wasm[op](x));
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
- return [wasm[op](0, 0), U8[0]];
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("leb128_encode_s_js");
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("leb128_decode_s_js");
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("leb128_encode_u_js");
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("leb128_decode_u_js");
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": "2.1.29",
3
+ "version": "3.0.0",
4
4
  "description": "WASM based LEB128 encoder / decoder (signed & unsigned)",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -35,13 +35,13 @@
35
35
  "test": "testament test"
36
36
  },
37
37
  "dependencies": {
38
- "@thi.ng/checks": "^3.3.3",
39
- "@thi.ng/errors": "^2.2.4",
40
- "@thi.ng/transducers-binary": "^2.1.29"
38
+ "@thi.ng/checks": "^3.3.4",
39
+ "@thi.ng/errors": "^2.2.5",
40
+ "@thi.ng/transducers-binary": "^2.1.30"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@microsoft/api-extractor": "^7.33.5",
44
- "@thi.ng/testament": "^0.3.5",
44
+ "@thi.ng/testament": "^0.3.6",
45
45
  "rimraf": "^3.0.2",
46
46
  "tools": "^0.0.1",
47
47
  "typedoc": "^0.23.20",
@@ -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": "75ec32ff7f1b7b5e72e7a04ace24732cd5d6c774\n"
82
+ "gitHead": "7d1e314f207299ff3c1a782d287ad99120b40b85\n"
82
83
  }