@thi.ng/bencode 2.1.8 → 2.1.11

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.
Files changed (4) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/decode.js +21 -21
  3. package/encode.js +17 -17
  4. package/package.json +12 -12
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-05-07T11:33:35Z
3
+ - **Last updated**: 2022-06-09T16:14:01Z
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/decode.js CHANGED
@@ -11,35 +11,35 @@ export const decode = (buf, utf8 = true) => {
11
11
  while (!(i = iter.next()).done) {
12
12
  x = i.value;
13
13
  switch (x) {
14
- case 100 /* DICT */:
14
+ case 100 /* Lit.DICT */:
15
15
  ensureNotKey(stack, "dict");
16
- stack.push({ type: 4 /* DICT */, val: {} });
16
+ stack.push({ type: 4 /* Type.DICT */, val: {} });
17
17
  break;
18
- case 108 /* LIST */:
18
+ case 108 /* Lit.LIST */:
19
19
  ensureNotKey(stack, "list");
20
- stack.push({ type: 5 /* LIST */, val: [] });
20
+ stack.push({ type: 5 /* Type.LIST */, val: [] });
21
21
  break;
22
- case 105 /* INT */:
22
+ case 105 /* Lit.INT */:
23
23
  x = collect(stack, readInt(iter, 0));
24
24
  if (x !== undefined) {
25
25
  return x;
26
26
  }
27
27
  break;
28
- case 102 /* FLOAT */:
28
+ case 102 /* Lit.FLOAT */:
29
29
  x = collect(stack, readFloat(iter));
30
30
  if (x !== undefined) {
31
31
  return x;
32
32
  }
33
33
  break;
34
- case 101 /* END */:
34
+ case 101 /* Lit.END */:
35
35
  x = stack.pop();
36
36
  if (x) {
37
37
  const parent = peek(stack);
38
38
  if (parent) {
39
- if (parent.type === 5 /* LIST */) {
39
+ if (parent.type === 5 /* Type.LIST */) {
40
40
  parent.val.push(x.val);
41
41
  }
42
- else if (parent.type === 4 /* DICT */) {
42
+ else if (parent.type === 4 /* Type.DICT */) {
43
43
  parent.val[parent.key] = x.val;
44
44
  parent.key = null;
45
45
  }
@@ -53,8 +53,8 @@ export const decode = (buf, utf8 = true) => {
53
53
  }
54
54
  break;
55
55
  default:
56
- if (x >= 48 /* ZERO */ && x <= 57 /* NINE */) {
57
- x = readBytes(iter, readInt(iter, x - 48 /* ZERO */, 58 /* COLON */));
56
+ if (x >= 48 /* Lit.ZERO */ && x <= 57 /* Lit.NINE */) {
57
+ x = readBytes(iter, readInt(iter, x - 48 /* Lit.ZERO */, 58 /* Lit.COLON */));
58
58
  x = collect(stack, x, utf8);
59
59
  if (x !== undefined) {
60
60
  return x;
@@ -69,13 +69,13 @@ export const decode = (buf, utf8 = true) => {
69
69
  };
70
70
  const ensureNotKey = (stack, type) => {
71
71
  const x = peek(stack);
72
- assert(!x || x.type !== 4 /* DICT */ || x.key, type + " not supported as dict key");
72
+ assert(!x || x.type !== 4 /* Type.DICT */ || x.key, type + " not supported as dict key");
73
73
  };
74
74
  const collect = (stack, x, utf8 = false) => {
75
75
  const parent = peek(stack);
76
76
  if (!parent)
77
77
  return x;
78
- if (parent.type === 5 /* LIST */) {
78
+ if (parent.type === 5 /* Type.LIST */) {
79
79
  parent.val.push(utf8 && isArray(x) ? utf8Decode(x) : x);
80
80
  }
81
81
  else {
@@ -88,16 +88,16 @@ const collect = (stack, x, utf8 = false) => {
88
88
  }
89
89
  }
90
90
  };
91
- const readInt = (iter, acc, end = 101 /* END */) => {
91
+ const readInt = (iter, acc, end = 101 /* Lit.END */) => {
92
92
  let i;
93
93
  let x;
94
94
  let isSigned = false;
95
95
  while (!(i = iter.next()).done) {
96
96
  x = i.value;
97
- if (x >= 48 /* ZERO */ && x <= 57 /* NINE */) {
98
- acc = acc * 10 + x - 48 /* ZERO */;
97
+ if (x >= 48 /* Lit.ZERO */ && x <= 57 /* Lit.NINE */) {
98
+ acc = acc * 10 + x - 48 /* Lit.ZERO */;
99
99
  }
100
- else if (x === 45 /* MINUS */) {
100
+ else if (x === 45 /* Lit.MINUS */) {
101
101
  assert(!isSigned, `invalid int literal`);
102
102
  isSigned = true;
103
103
  }
@@ -116,12 +116,12 @@ const readFloat = (iter) => {
116
116
  let acc = "";
117
117
  while (!(i = iter.next()).done) {
118
118
  x = i.value;
119
- if ((x >= 48 /* ZERO */ && x <= 57 /* NINE */) ||
120
- x === 46 /* DOT */ ||
121
- x === 45 /* MINUS */) {
119
+ if ((x >= 48 /* Lit.ZERO */ && x <= 57 /* Lit.NINE */) ||
120
+ x === 46 /* Lit.DOT */ ||
121
+ x === 45 /* Lit.MINUS */) {
122
122
  acc += String.fromCharCode(x);
123
123
  }
124
- else if (x === 101 /* END */) {
124
+ else if (x === 101 /* Lit.END */) {
125
125
  return parseFloat(acc);
126
126
  }
127
127
  else {
package/encode.js CHANGED
@@ -13,42 +13,42 @@ const FLOAT_RE = /^[0-9.-]+$/;
13
13
  export const encode = (x, cap = 1024) => bytes(cap, encodeBin(x));
14
14
  const encodeBin = defmulti((x) => isNumber(x)
15
15
  ? Math.floor(x) !== x
16
- ? 1 /* FLOAT */
17
- : 0 /* INT */
16
+ ? 1 /* Type.FLOAT */
17
+ : 0 /* Type.INT */
18
18
  : isBoolean(x)
19
- ? 0 /* INT */
19
+ ? 0 /* Type.INT */
20
20
  : isString(x)
21
- ? 2 /* STR */
21
+ ? 2 /* Type.STR */
22
22
  : x instanceof Uint8Array
23
- ? 3 /* BINARY */
23
+ ? 3 /* Type.BINARY */
24
24
  : isArrayLike(x)
25
- ? 5 /* LIST */
25
+ ? 5 /* Type.LIST */
26
26
  : isPlainObject(x)
27
- ? 4 /* DICT */
27
+ ? 4 /* Type.DICT */
28
28
  : unsupported(`unsupported data type: ${x}`), {}, {
29
- [0 /* INT */]: (x) => {
29
+ [0 /* Type.INT */]: (x) => {
30
30
  __ensureValidNumber(x);
31
31
  return [str(`i${Math.floor(x)}e`)];
32
32
  },
33
- [1 /* FLOAT */]: (x) => {
33
+ [1 /* Type.FLOAT */]: (x) => {
34
34
  __ensureValidNumber(x);
35
35
  assert(FLOAT_RE.test(x.toString()), `values requiring exponential notation not allowed (${x})`);
36
36
  return [str(`f${x}e`)];
37
37
  },
38
- [3 /* BINARY */]: (buf) => [
38
+ [3 /* Type.BINARY */]: (buf) => [
39
39
  str(buf.length + ":"),
40
40
  u8array(buf),
41
41
  ],
42
- [2 /* STR */]: (x) => [str(utf8Length(x) + ":" + x)],
43
- [5 /* LIST */]: (x) => [
44
- u8(108 /* LIST */),
42
+ [2 /* Type.STR */]: (x) => [str(utf8Length(x) + ":" + x)],
43
+ [5 /* Type.LIST */]: (x) => [
44
+ u8(108 /* Lit.LIST */),
45
45
  ...mapcat(encodeBin, x),
46
- u8(101 /* END */),
46
+ u8(101 /* Lit.END */),
47
47
  ],
48
- [4 /* DICT */]: (x) => [
49
- u8(100 /* DICT */),
48
+ [4 /* Type.DICT */]: (x) => [
49
+ u8(100 /* Lit.DICT */),
50
50
  ...mapcat((k) => encodeBin(k).concat(encodeBin(x[k])), Object.keys(x).sort()),
51
- u8(101 /* END */),
51
+ u8(101 /* Lit.END */),
52
52
  ],
53
53
  });
54
54
  /** @internal */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/bencode",
3
- "version": "2.1.8",
3
+ "version": "2.1.11",
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.2.3",
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.3",
42
- "@thi.ng/transducers-binary": "^2.1.8"
37
+ "@thi.ng/arrays": "^2.3.0",
38
+ "@thi.ng/checks": "^3.2.1",
39
+ "@thi.ng/defmulti": "^2.1.7",
40
+ "@thi.ng/errors": "^2.1.7",
41
+ "@thi.ng/transducers": "^8.3.6",
42
+ "@thi.ng/transducers-binary": "^2.1.11"
43
43
  },
44
44
  "devDependencies": {
45
- "@microsoft/api-extractor": "^7.23.1",
46
- "@thi.ng/testament": "^0.2.7",
45
+ "@microsoft/api-extractor": "^7.25.0",
46
+ "@thi.ng/testament": "^0.2.8",
47
47
  "rimraf": "^3.0.2",
48
48
  "tools": "^0.0.1",
49
- "typedoc": "^0.22.15",
50
- "typescript": "^4.6.4"
49
+ "typedoc": "^0.22.17",
50
+ "typescript": "^4.7.3"
51
51
  },
52
52
  "keywords": [
53
53
  "array",
@@ -79,5 +79,5 @@
79
79
  "default": "./encode.js"
80
80
  }
81
81
  },
82
- "gitHead": "e23901b8582af71d8a29e0ce4929f15ac509f9e5\n"
82
+ "gitHead": "e64b1ab39ae9bcc494ef006f6329e5182fa2a326\n"
83
83
  }