@thi.ng/leb128 3.0.86 → 3.0.88

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**: 2024-04-08T14:59:29Z
3
+ - **Last updated**: 2024-04-13T16:05:36Z
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.
package/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)
8
8
 
9
9
  > [!NOTE]
10
- > This is one of 191 standalone projects, maintained as part
10
+ > This is one of 192 standalone projects, maintained as part
11
11
  > of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
12
12
  > and anti-framework.
13
13
  >
@@ -39,6 +39,9 @@ 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
+ All encodeInto functions will check the bounds of the target array to
43
+ make sure all the bytes can be written.
44
+
42
45
  References:
43
46
 
44
47
  - https://en.wikipedia.org/wiki/LEB128
@@ -85,7 +88,7 @@ For Node.js REPL:
85
88
  const leb = await import("@thi.ng/leb128");
86
89
  ```
87
90
 
88
- Package sizes (brotli'd, pre-treeshake): ESM: 880 bytes
91
+ Package sizes (brotli'd, pre-treeshake): ESM: 968 bytes
89
92
 
90
93
  ## Dependencies
91
94
 
@@ -109,11 +112,19 @@ leb.decodeULEB128(enc);
109
112
  // [ 9007199254740991n, 8 ]
110
113
 
111
114
  // encode signed int
112
- enc = leb.encodeSLEB128(Number.MIN_SAFE_INTEGER)
115
+ enc = leb.encodeSLEB128(Number.MIN_SAFE_INTEGER);
113
116
  // Uint8Array [ 129, 128, 128, 128, 128, 128, 128, 112 ]
114
117
 
115
- leb.decodeSLEB128(enc)
118
+ leb.decodeSLEB128(enc);
116
119
  // [ -9007199254740991n, 8 ]
120
+
121
+ // when writing into an existing buffer, there needs to be enough bytes to write the value
122
+ const target = new Uint8Array(10);
123
+ const count = leb.encodeULEB128Into(target, Number.MAX_SAFE_INTEGER);
124
+ console.log(target);
125
+ // Uint8Array [ 255, 255, 255, 255, 255, 255, 255, 15, 0, 0 ]
126
+ console.log(count);
127
+ // 8
117
128
  ```
118
129
 
119
130
  ## Building the binary
@@ -142,14 +153,15 @@ yarn test
142
153
 
143
154
  ## Authors
144
155
 
145
- - [Karsten Schmidt](https://thi.ng)
156
+ - [Karsten Schmidt](https://thi.ng) (Main author)
157
+ - [jtenner](https://github.com/jtenner)
146
158
 
147
159
  If this project contributes to an academic publication, please cite it as:
148
160
 
149
161
  ```bibtex
150
162
  @misc{thing-leb128,
151
163
  title = "@thi.ng/leb128",
152
- author = "Karsten Schmidt",
164
+ author = "Karsten Schmidt and others",
153
165
  note = "https://thi.ng/leb128",
154
166
  year = 2019
155
167
  }
package/binary.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Generated @ 2022-12-02T12:00:53Z
2
+ * Generated @ 2024-04-13T13:45:31Z
3
3
  *
4
4
  * @internal
5
5
  */
package/index.d.ts CHANGED
@@ -14,6 +14,19 @@ export declare const encodeSLEB128: (x: bigint | number) => Uint8Array;
14
14
  * @param idx -
15
15
  */
16
16
  export declare const decodeSLEB128: (src: Uint8Array, idx?: number) => [bigint, number];
17
+ /**
18
+ * Takes a destination Uint8Array, a signed integer `x`, and an optional
19
+ * position to encode an LEB128 formatted byte sequence into the destination.
20
+ * 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;
17
30
  /**
18
31
  * Encodes unsigned integer `x` into LEB128 varint format and returns
19
32
  * encoded bytes. Values < 0 will be encoded as zero.
@@ -30,4 +43,17 @@ export declare const encodeULEB128: (x: bigint | number) => Uint8Array;
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
- encodeULEB128
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.86",
3
+ "version": "3.0.88",
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.0",
41
- "@thi.ng/errors": "^2.5.3",
42
- "@thi.ng/transducers-binary": "^2.1.115"
43
+ "@thi.ng/checks": "^3.6.1",
44
+ "@thi.ng/errors": "^2.5.4",
45
+ "@thi.ng/transducers-binary": "^2.1.116"
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": "85ac4bd4d6d89f8e3689e2863d5bea0cecdb371c\n"
88
+ "gitHead": "fd7ab12d565809a2300783881c62b883dbd25c23\n"
86
89
  }