@thi.ng/bencode 2.1.5 → 2.1.6
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 +9 -1
- package/README.md +10 -9
- package/decode.js +6 -1
- package/encode.js +11 -2
- package/package.json +12 -12
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2022-
|
|
3
|
+
- **Last updated**: 2022-04-07T14:17:30Z
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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.
|
|
81
|
+
Package sizes (gzipped, pre-treeshake): ESM: 1.30 KB
|
|
81
82
|
|
|
82
83
|
## Dependencies
|
|
83
84
|
|
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) =>
|
|
29
|
+
[0 /* INT */]: (x) => {
|
|
30
|
+
__ensureValidNumber(x);
|
|
31
|
+
return [str(`i${Math.floor(x)}e`)];
|
|
32
|
+
},
|
|
30
33
|
[1 /* FLOAT */]: (x) => {
|
|
31
|
-
|
|
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.
|
|
3
|
+
"version": "2.1.6",
|
|
4
4
|
"description": "Bencode binary encoder / decoder with optional UTF8 encoding & floating point support",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -34,16 +34,16 @@
|
|
|
34
34
|
"test": "testament test"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@thi.ng/arrays": "^2.2.
|
|
38
|
-
"@thi.ng/checks": "^3.1.
|
|
39
|
-
"@thi.ng/defmulti": "^2.1.
|
|
40
|
-
"@thi.ng/errors": "^2.1.
|
|
41
|
-
"@thi.ng/transducers": "^8.3.
|
|
42
|
-
"@thi.ng/transducers-binary": "^2.1.
|
|
37
|
+
"@thi.ng/arrays": "^2.2.1",
|
|
38
|
+
"@thi.ng/checks": "^3.1.5",
|
|
39
|
+
"@thi.ng/defmulti": "^2.1.5",
|
|
40
|
+
"@thi.ng/errors": "^2.1.5",
|
|
41
|
+
"@thi.ng/transducers": "^8.3.1",
|
|
42
|
+
"@thi.ng/transducers-binary": "^2.1.6"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@microsoft/api-extractor": "^7.19.4",
|
|
46
|
-
"@thi.ng/testament": "^0.2.
|
|
46
|
+
"@thi.ng/testament": "^0.2.5",
|
|
47
47
|
"rimraf": "^3.0.2",
|
|
48
48
|
"tools": "^0.0.1",
|
|
49
49
|
"typedoc": "^0.22.13",
|
|
@@ -70,14 +70,14 @@
|
|
|
70
70
|
],
|
|
71
71
|
"exports": {
|
|
72
72
|
".": {
|
|
73
|
-
"
|
|
73
|
+
"default": "./index.js"
|
|
74
74
|
},
|
|
75
75
|
"./decode": {
|
|
76
|
-
"
|
|
76
|
+
"default": "./decode.js"
|
|
77
77
|
},
|
|
78
78
|
"./encode": {
|
|
79
|
-
"
|
|
79
|
+
"default": "./encode.js"
|
|
80
80
|
}
|
|
81
81
|
},
|
|
82
|
-
"gitHead": "
|
|
82
|
+
"gitHead": "5ee1feb590dd935593b1dd4e7f38a3ed3ba64765\n"
|
|
83
83
|
}
|