@thi.ng/bencode 2.1.4 → 2.1.7

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**: 2021-12-13T10:26:00Z
3
+ - **Last updated**: 2022-05-07T11:33:35Z
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,14 @@ 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
+ ### [2.1.6](https://github.com/thi-ng/umbrella/tree/@thi.ng/bencode@2.1.6) (2022-04-07)
13
+
14
+ #### 🩹 Bug fixes
15
+
16
+ - fix [#342](https://github.com/thi-ng/umbrella/issues/342), support signed ints ([66615be](https://github.com/thi-ng/umbrella/commit/66615be))
17
+ - add inf/NaN checks
18
+ - add tests
19
+
12
20
  ## [2.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/bencode@2.1.0) (2021-11-17)
13
21
 
14
22
  #### 🚀 Features
package/README.md CHANGED
@@ -30,7 +30,7 @@ decoder for structured data.
30
30
 
31
31
  #### Booleans
32
32
 
33
- Will be converted to `0` or `1`.
33
+ Will be converted to `0` or `1` integers.
34
34
 
35
35
  #### String handling
36
36
 
@@ -40,13 +40,14 @@ Bencode strings (e.g. `len:xxx...`), but are used as is.
40
40
 
41
41
  #### Floating point values
42
42
 
43
- This implementation has optional (non-standard) support for floating
44
- point values. If these are not desired (e.g. for compatibility reasons),
45
- all numeric values MUST be pre-rounded to integers. The encoder only
46
- chooses the custom float encoding iff a number has a fractional part.
47
- Floats are encoded similarly to standard ints (i.e. as text), but using
48
- `f` as prefix. Furthermore, only floats with an absolute value in the
49
- semi-open `[1e-6,1e21)` interval can be encoded.
43
+ This implementation has optional (non-standard) support for floating point
44
+ values. If these are not desired (e.g. for compatibility reasons), all numeric
45
+ values MUST be pre-rounded to integers. The encoder only chooses the custom
46
+ float encoding iff a number has a fractional part. Floats are encoded similarly
47
+ to standard ints (i.e. as text), but using `f` as prefix. Furthermore, only
48
+ floats with an absolute value in the semi-open `[1e-6,1e21)` interval can be
49
+ encoded. Float values requiring exponential notation will throw an error during
50
+ encoding.
50
51
 
51
52
  ### Status
52
53
 
@@ -77,7 +78,7 @@ node --experimental-repl-await
77
78
  > const bencode = await import("@thi.ng/bencode");
78
79
  ```
79
80
 
80
- Package sizes (gzipped, pre-treeshake): ESM: 1.22 KB
81
+ Package sizes (gzipped, pre-treeshake): ESM: 1.30 KB
81
82
 
82
83
  ## Dependencies
83
84
 
@@ -135,4 +136,4 @@ If this project contributes to an academic publication, please cite it as:
135
136
 
136
137
  ## License
137
138
 
138
- © 2016 - 2021 Karsten Schmidt // Apache Software License 2.0
139
+ © 2016 - 2022 Karsten Schmidt // Apache Software License 2.0
package/decode.js CHANGED
@@ -91,13 +91,18 @@ const collect = (stack, x, utf8 = false) => {
91
91
  const readInt = (iter, acc, end = 101 /* END */) => {
92
92
  let i;
93
93
  let x;
94
+ let isSigned = false;
94
95
  while (!(i = iter.next()).done) {
95
96
  x = i.value;
96
97
  if (x >= 48 /* ZERO */ && x <= 57 /* NINE */) {
97
98
  acc = acc * 10 + x - 48 /* ZERO */;
98
99
  }
100
+ else if (x === 45 /* MINUS */) {
101
+ assert(!isSigned, `invalid int literal`);
102
+ isSigned = true;
103
+ }
99
104
  else if (x === end) {
100
- return acc;
105
+ return isSigned ? -acc : acc;
101
106
  }
102
107
  else {
103
108
  illegalState(`expected digit, got 0x${x.toString(16)}`);
package/encode.js CHANGED
@@ -26,9 +26,13 @@ const encodeBin = defmulti((x) => isNumber(x)
26
26
  : isPlainObject(x)
27
27
  ? 4 /* DICT */
28
28
  : unsupported(`unsupported data type: ${x}`), {}, {
29
- [0 /* INT */]: (x) => [str(`i${Math.floor(x)}e`)],
29
+ [0 /* INT */]: (x) => {
30
+ __ensureValidNumber(x);
31
+ return [str(`i${Math.floor(x)}e`)];
32
+ },
30
33
  [1 /* FLOAT */]: (x) => {
31
- assert(FLOAT_RE.test(x.toString()), `exponential notation not allowed (${x})`);
34
+ __ensureValidNumber(x);
35
+ assert(FLOAT_RE.test(x.toString()), `values requiring exponential notation not allowed (${x})`);
32
36
  return [str(`f${x}e`)];
33
37
  },
34
38
  [3 /* BINARY */]: (buf) => [
@@ -47,3 +51,8 @@ const encodeBin = defmulti((x) => isNumber(x)
47
51
  u8(101 /* END */),
48
52
  ],
49
53
  });
54
+ /** @internal */
55
+ const __ensureValidNumber = (x) => {
56
+ assert(isFinite(x), `can't encode infinite value`);
57
+ assert(!isNaN(x), `can't encode NaN`);
58
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/bencode",
3
- "version": "2.1.4",
3
+ "version": "2.1.7",
4
4
  "description": "Bencode binary encoder / decoder with optional UTF8 encoding & floating point support",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,20 +34,20 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/arrays": "^2.1.3",
38
- "@thi.ng/checks": "^3.1.3",
39
- "@thi.ng/defmulti": "^2.1.3",
40
- "@thi.ng/errors": "^2.1.3",
41
- "@thi.ng/transducers": "^8.2.0",
42
- "@thi.ng/transducers-binary": "^2.1.4"
37
+ "@thi.ng/arrays": "^2.2.2",
38
+ "@thi.ng/checks": "^3.1.6",
39
+ "@thi.ng/defmulti": "^2.1.6",
40
+ "@thi.ng/errors": "^2.1.6",
41
+ "@thi.ng/transducers": "^8.3.2",
42
+ "@thi.ng/transducers-binary": "^2.1.7"
43
43
  },
44
44
  "devDependencies": {
45
- "@microsoft/api-extractor": "^7.19.2",
46
- "@thi.ng/testament": "^0.2.3",
45
+ "@microsoft/api-extractor": "^7.23.1",
46
+ "@thi.ng/testament": "^0.2.6",
47
47
  "rimraf": "^3.0.2",
48
48
  "tools": "^0.0.1",
49
- "typedoc": "^0.22.10",
50
- "typescript": "^4.5.3"
49
+ "typedoc": "^0.22.15",
50
+ "typescript": "^4.6.4"
51
51
  },
52
52
  "keywords": [
53
53
  "array",
@@ -70,14 +70,14 @@
70
70
  ],
71
71
  "exports": {
72
72
  ".": {
73
- "import": "./index.js"
73
+ "default": "./index.js"
74
74
  },
75
75
  "./decode": {
76
- "import": "./decode.js"
76
+ "default": "./decode.js"
77
77
  },
78
78
  "./encode": {
79
- "import": "./encode.js"
79
+ "default": "./encode.js"
80
80
  }
81
81
  },
82
- "gitHead": "1ba92c6b9509e74e509b4c0b875fc380a97bbbc1\n"
82
+ "gitHead": "cf084be5fd5932226054d2dd32bad35481379f5d\n"
83
83
  }