cborg 2.0.4 → 3.0.0-alpha.1

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 (135) hide show
  1. package/.github/dependabot.yml +16 -0
  2. package/.github/workflows/test-and-release.yml +63 -0
  3. package/CHANGELOG.md +332 -0
  4. package/bench/bench.js +117 -0
  5. package/bench/json.js +124 -0
  6. package/bench/package.json +8 -0
  7. package/lib/bin.js +1 -1
  8. package/package.json +29 -47
  9. package/types/lib/bin.d.ts +1 -0
  10. package/cjs/browser-test/appendix_a.js +0 -643
  11. package/cjs/browser-test/common.js +0 -24
  12. package/cjs/browser-test/node-test-bin.js +0 -348
  13. package/cjs/browser-test/noop-bin-test.js +0 -4
  14. package/cjs/browser-test/test-0uint.js +0 -158
  15. package/cjs/browser-test/test-1negint.js +0 -152
  16. package/cjs/browser-test/test-2bytes.js +0 -254
  17. package/cjs/browser-test/test-3string.js +0 -144
  18. package/cjs/browser-test/test-4array.js +0 -200
  19. package/cjs/browser-test/test-5map.js +0 -667
  20. package/cjs/browser-test/test-6tag.js +0 -75
  21. package/cjs/browser-test/test-7float.js +0 -253
  22. package/cjs/browser-test/test-bl.js +0 -91
  23. package/cjs/browser-test/test-cbor-vectors.js +0 -98
  24. package/cjs/browser-test/test-decode-errors.js +0 -77
  25. package/cjs/browser-test/test-fuzz.js +0 -56
  26. package/cjs/browser-test/test-json.js +0 -281
  27. package/cjs/browser-test/test-length.js +0 -63
  28. package/cjs/cborg.js +0 -14
  29. package/cjs/lib/0uint.js +0 -163
  30. package/cjs/lib/1negint.js +0 -63
  31. package/cjs/lib/2bytes.js +0 -62
  32. package/cjs/lib/3string.js +0 -46
  33. package/cjs/lib/4array.js +0 -51
  34. package/cjs/lib/5map.js +0 -51
  35. package/cjs/lib/6tag.js +0 -36
  36. package/cjs/lib/7float.js +0 -188
  37. package/cjs/lib/bin.js +0 -139
  38. package/cjs/lib/bl.js +0 -77
  39. package/cjs/lib/byte-utils.js +0 -245
  40. package/cjs/lib/common.js +0 -22
  41. package/cjs/lib/decode.js +0 -140
  42. package/cjs/lib/diagnostic.js +0 -124
  43. package/cjs/lib/encode.js +0 -248
  44. package/cjs/lib/is.js +0 -87
  45. package/cjs/lib/json/decode.js +0 -414
  46. package/cjs/lib/json/encode.js +0 -161
  47. package/cjs/lib/json/json.js +0 -12
  48. package/cjs/lib/jump.js +0 -174
  49. package/cjs/lib/length.js +0 -36
  50. package/cjs/lib/token.js +0 -46
  51. package/cjs/node-test/appendix_a.js +0 -643
  52. package/cjs/node-test/common.js +0 -24
  53. package/cjs/node-test/node-test-bin.js +0 -348
  54. package/cjs/node-test/noop-bin-test.js +0 -4
  55. package/cjs/node-test/test-0uint.js +0 -158
  56. package/cjs/node-test/test-1negint.js +0 -152
  57. package/cjs/node-test/test-2bytes.js +0 -254
  58. package/cjs/node-test/test-3string.js +0 -144
  59. package/cjs/node-test/test-4array.js +0 -200
  60. package/cjs/node-test/test-5map.js +0 -667
  61. package/cjs/node-test/test-6tag.js +0 -75
  62. package/cjs/node-test/test-7float.js +0 -253
  63. package/cjs/node-test/test-bl.js +0 -91
  64. package/cjs/node-test/test-cbor-vectors.js +0 -98
  65. package/cjs/node-test/test-decode-errors.js +0 -77
  66. package/cjs/node-test/test-fuzz.js +0 -56
  67. package/cjs/node-test/test-json.js +0 -281
  68. package/cjs/node-test/test-length.js +0 -63
  69. package/cjs/taglib.js +0 -44
  70. package/cli.js +0 -7
  71. package/esm/browser-test/appendix_a.js +0 -640
  72. package/esm/browser-test/common.js +0 -19
  73. package/esm/browser-test/node-test-bin.js +0 -339
  74. package/esm/browser-test/noop-bin-test.js +0 -1
  75. package/esm/browser-test/test-0uint.js +0 -155
  76. package/esm/browser-test/test-1negint.js +0 -149
  77. package/esm/browser-test/test-2bytes.js +0 -252
  78. package/esm/browser-test/test-3string.js +0 -141
  79. package/esm/browser-test/test-4array.js +0 -197
  80. package/esm/browser-test/test-5map.js +0 -664
  81. package/esm/browser-test/test-6tag.js +0 -78
  82. package/esm/browser-test/test-7float.js +0 -250
  83. package/esm/browser-test/test-bl.js +0 -84
  84. package/esm/browser-test/test-cbor-vectors.js +0 -95
  85. package/esm/browser-test/test-decode-errors.js +0 -69
  86. package/esm/browser-test/test-fuzz.js +0 -50
  87. package/esm/browser-test/test-json.js +0 -279
  88. package/esm/browser-test/test-length.js +0 -55
  89. package/esm/cborg.js +0 -12
  90. package/esm/lib/0uint.js +0 -152
  91. package/esm/lib/1negint.js +0 -55
  92. package/esm/lib/2bytes.js +0 -59
  93. package/esm/lib/3string.js +0 -43
  94. package/esm/lib/4array.js +0 -41
  95. package/esm/lib/5map.js +0 -41
  96. package/esm/lib/6tag.js +0 -27
  97. package/esm/lib/7float.js +0 -179
  98. package/esm/lib/bin.js +0 -137
  99. package/esm/lib/bl.js +0 -74
  100. package/esm/lib/byte-utils.js +0 -228
  101. package/esm/lib/common.js +0 -19
  102. package/esm/lib/decode.js +0 -139
  103. package/esm/lib/diagnostic.js +0 -123
  104. package/esm/lib/encode.js +0 -246
  105. package/esm/lib/is.js +0 -81
  106. package/esm/lib/json/decode.js +0 -413
  107. package/esm/lib/json/encode.js +0 -160
  108. package/esm/lib/json/json.js +0 -10
  109. package/esm/lib/jump.js +0 -168
  110. package/esm/lib/length.js +0 -31
  111. package/esm/lib/token.js +0 -43
  112. package/esm/node-test/appendix_a.js +0 -640
  113. package/esm/node-test/common.js +0 -19
  114. package/esm/node-test/node-test-bin.js +0 -339
  115. package/esm/node-test/noop-bin-test.js +0 -1
  116. package/esm/node-test/test-0uint.js +0 -155
  117. package/esm/node-test/test-1negint.js +0 -149
  118. package/esm/node-test/test-2bytes.js +0 -252
  119. package/esm/node-test/test-3string.js +0 -141
  120. package/esm/node-test/test-4array.js +0 -197
  121. package/esm/node-test/test-5map.js +0 -664
  122. package/esm/node-test/test-6tag.js +0 -78
  123. package/esm/node-test/test-7float.js +0 -250
  124. package/esm/node-test/test-bl.js +0 -84
  125. package/esm/node-test/test-cbor-vectors.js +0 -95
  126. package/esm/node-test/test-decode-errors.js +0 -69
  127. package/esm/node-test/test-fuzz.js +0 -50
  128. package/esm/node-test/test-json.js +0 -279
  129. package/esm/node-test/test-length.js +0 -55
  130. package/esm/package.json +0 -4
  131. package/esm/taglib.js +0 -37
  132. package/index.js +0 -1
  133. package/json +0 -1
  134. package/length +0 -1
  135. package/taglib +0 -1
package/cjs/lib/4array.js DELETED
@@ -1,51 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var token = require('./token.js');
6
- var _0uint = require('./0uint.js');
7
- var common = require('./common.js');
8
-
9
- function toToken(_data, _pos, prefix, length) {
10
- return new token.Token(token.Type.array, length, prefix);
11
- }
12
- function decodeArrayCompact(data, pos, minor, _options) {
13
- return toToken(data, pos, 1, minor);
14
- }
15
- function decodeArray8(data, pos, _minor, options) {
16
- return toToken(data, pos, 2, _0uint.readUint8(data, pos + 1, options));
17
- }
18
- function decodeArray16(data, pos, _minor, options) {
19
- return toToken(data, pos, 3, _0uint.readUint16(data, pos + 1, options));
20
- }
21
- function decodeArray32(data, pos, _minor, options) {
22
- return toToken(data, pos, 5, _0uint.readUint32(data, pos + 1, options));
23
- }
24
- function decodeArray64(data, pos, _minor, options) {
25
- const l = _0uint.readUint64(data, pos + 1, options);
26
- if (typeof l === 'bigint') {
27
- throw new Error(`${ common.decodeErrPrefix } 64-bit integer array lengths not supported`);
28
- }
29
- return toToken(data, pos, 9, l);
30
- }
31
- function decodeArrayIndefinite(data, pos, _minor, options) {
32
- if (options.allowIndefinite === false) {
33
- throw new Error(`${ common.decodeErrPrefix } indefinite length items not allowed`);
34
- }
35
- return toToken(data, pos, 1, Infinity);
36
- }
37
- function encodeArray(buf, token$1) {
38
- _0uint.encodeUintValue(buf, token.Type.array.majorEncoded, token$1.value);
39
- }
40
- encodeArray.compareTokens = _0uint.encodeUint.compareTokens;
41
- encodeArray.encodedSize = function encodedSize(token) {
42
- return _0uint.encodeUintValue.encodedSize(token.value);
43
- };
44
-
45
- exports.decodeArray16 = decodeArray16;
46
- exports.decodeArray32 = decodeArray32;
47
- exports.decodeArray64 = decodeArray64;
48
- exports.decodeArray8 = decodeArray8;
49
- exports.decodeArrayCompact = decodeArrayCompact;
50
- exports.decodeArrayIndefinite = decodeArrayIndefinite;
51
- exports.encodeArray = encodeArray;
package/cjs/lib/5map.js DELETED
@@ -1,51 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var token = require('./token.js');
6
- var _0uint = require('./0uint.js');
7
- var common = require('./common.js');
8
-
9
- function toToken(_data, _pos, prefix, length) {
10
- return new token.Token(token.Type.map, length, prefix);
11
- }
12
- function decodeMapCompact(data, pos, minor, _options) {
13
- return toToken(data, pos, 1, minor);
14
- }
15
- function decodeMap8(data, pos, _minor, options) {
16
- return toToken(data, pos, 2, _0uint.readUint8(data, pos + 1, options));
17
- }
18
- function decodeMap16(data, pos, _minor, options) {
19
- return toToken(data, pos, 3, _0uint.readUint16(data, pos + 1, options));
20
- }
21
- function decodeMap32(data, pos, _minor, options) {
22
- return toToken(data, pos, 5, _0uint.readUint32(data, pos + 1, options));
23
- }
24
- function decodeMap64(data, pos, _minor, options) {
25
- const l = _0uint.readUint64(data, pos + 1, options);
26
- if (typeof l === 'bigint') {
27
- throw new Error(`${ common.decodeErrPrefix } 64-bit integer map lengths not supported`);
28
- }
29
- return toToken(data, pos, 9, l);
30
- }
31
- function decodeMapIndefinite(data, pos, _minor, options) {
32
- if (options.allowIndefinite === false) {
33
- throw new Error(`${ common.decodeErrPrefix } indefinite length items not allowed`);
34
- }
35
- return toToken(data, pos, 1, Infinity);
36
- }
37
- function encodeMap(buf, token$1) {
38
- _0uint.encodeUintValue(buf, token.Type.map.majorEncoded, token$1.value);
39
- }
40
- encodeMap.compareTokens = _0uint.encodeUint.compareTokens;
41
- encodeMap.encodedSize = function encodedSize(token) {
42
- return _0uint.encodeUintValue.encodedSize(token.value);
43
- };
44
-
45
- exports.decodeMap16 = decodeMap16;
46
- exports.decodeMap32 = decodeMap32;
47
- exports.decodeMap64 = decodeMap64;
48
- exports.decodeMap8 = decodeMap8;
49
- exports.decodeMapCompact = decodeMapCompact;
50
- exports.decodeMapIndefinite = decodeMapIndefinite;
51
- exports.encodeMap = encodeMap;
package/cjs/lib/6tag.js DELETED
@@ -1,36 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var token = require('./token.js');
6
- var _0uint = require('./0uint.js');
7
-
8
- function decodeTagCompact(_data, _pos, minor, _options) {
9
- return new token.Token(token.Type.tag, minor, 1);
10
- }
11
- function decodeTag8(data, pos, _minor, options) {
12
- return new token.Token(token.Type.tag, _0uint.readUint8(data, pos + 1, options), 2);
13
- }
14
- function decodeTag16(data, pos, _minor, options) {
15
- return new token.Token(token.Type.tag, _0uint.readUint16(data, pos + 1, options), 3);
16
- }
17
- function decodeTag32(data, pos, _minor, options) {
18
- return new token.Token(token.Type.tag, _0uint.readUint32(data, pos + 1, options), 5);
19
- }
20
- function decodeTag64(data, pos, _minor, options) {
21
- return new token.Token(token.Type.tag, _0uint.readUint64(data, pos + 1, options), 9);
22
- }
23
- function encodeTag(buf, token$1) {
24
- _0uint.encodeUintValue(buf, token.Type.tag.majorEncoded, token$1.value);
25
- }
26
- encodeTag.compareTokens = _0uint.encodeUint.compareTokens;
27
- encodeTag.encodedSize = function encodedSize(token) {
28
- return _0uint.encodeUintValue.encodedSize(token.value);
29
- };
30
-
31
- exports.decodeTag16 = decodeTag16;
32
- exports.decodeTag32 = decodeTag32;
33
- exports.decodeTag64 = decodeTag64;
34
- exports.decodeTag8 = decodeTag8;
35
- exports.decodeTagCompact = decodeTagCompact;
36
- exports.encodeTag = encodeTag;
package/cjs/lib/7float.js DELETED
@@ -1,188 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var token = require('./token.js');
6
- var common = require('./common.js');
7
- var _0uint = require('./0uint.js');
8
-
9
- const MINOR_FALSE = 20;
10
- const MINOR_TRUE = 21;
11
- const MINOR_NULL = 22;
12
- const MINOR_UNDEFINED = 23;
13
- function decodeUndefined(_data, _pos, _minor, options) {
14
- if (options.allowUndefined === false) {
15
- throw new Error(`${ common.decodeErrPrefix } undefined values are not supported`);
16
- } else if (options.coerceUndefinedToNull === true) {
17
- return new token.Token(token.Type.null, null, 1);
18
- }
19
- return new token.Token(token.Type.undefined, undefined, 1);
20
- }
21
- function decodeBreak(_data, _pos, _minor, options) {
22
- if (options.allowIndefinite === false) {
23
- throw new Error(`${ common.decodeErrPrefix } indefinite length items not allowed`);
24
- }
25
- return new token.Token(token.Type.break, undefined, 1);
26
- }
27
- function createToken(value, bytes, options) {
28
- if (options) {
29
- if (options.allowNaN === false && Number.isNaN(value)) {
30
- throw new Error(`${ common.decodeErrPrefix } NaN values are not supported`);
31
- }
32
- if (options.allowInfinity === false && (value === Infinity || value === -Infinity)) {
33
- throw new Error(`${ common.decodeErrPrefix } Infinity values are not supported`);
34
- }
35
- }
36
- return new token.Token(token.Type.float, value, bytes);
37
- }
38
- function decodeFloat16(data, pos, _minor, options) {
39
- return createToken(readFloat16(data, pos + 1), 3, options);
40
- }
41
- function decodeFloat32(data, pos, _minor, options) {
42
- return createToken(readFloat32(data, pos + 1), 5, options);
43
- }
44
- function decodeFloat64(data, pos, _minor, options) {
45
- return createToken(readFloat64(data, pos + 1), 9, options);
46
- }
47
- function encodeFloat(buf, token$1, options) {
48
- const float = token$1.value;
49
- if (float === false) {
50
- buf.push([token.Type.float.majorEncoded | MINOR_FALSE]);
51
- } else if (float === true) {
52
- buf.push([token.Type.float.majorEncoded | MINOR_TRUE]);
53
- } else if (float === null) {
54
- buf.push([token.Type.float.majorEncoded | MINOR_NULL]);
55
- } else if (float === undefined) {
56
- buf.push([token.Type.float.majorEncoded | MINOR_UNDEFINED]);
57
- } else {
58
- let decoded;
59
- let success = false;
60
- if (!options || options.float64 !== true) {
61
- encodeFloat16(float);
62
- decoded = readFloat16(ui8a, 1);
63
- if (float === decoded || Number.isNaN(float)) {
64
- ui8a[0] = 249;
65
- buf.push(ui8a.slice(0, 3));
66
- success = true;
67
- } else {
68
- encodeFloat32(float);
69
- decoded = readFloat32(ui8a, 1);
70
- if (float === decoded) {
71
- ui8a[0] = 250;
72
- buf.push(ui8a.slice(0, 5));
73
- success = true;
74
- }
75
- }
76
- }
77
- if (!success) {
78
- encodeFloat64(float);
79
- decoded = readFloat64(ui8a, 1);
80
- ui8a[0] = 251;
81
- buf.push(ui8a.slice(0, 9));
82
- }
83
- }
84
- }
85
- encodeFloat.encodedSize = function encodedSize(token, options) {
86
- const float = token.value;
87
- if (float === false || float === true || float === null || float === undefined) {
88
- return 1;
89
- }
90
- if (!options || options.float64 !== true) {
91
- encodeFloat16(float);
92
- let decoded = readFloat16(ui8a, 1);
93
- if (float === decoded || Number.isNaN(float)) {
94
- return 3;
95
- }
96
- encodeFloat32(float);
97
- decoded = readFloat32(ui8a, 1);
98
- if (float === decoded) {
99
- return 5;
100
- }
101
- }
102
- return 9;
103
- };
104
- const buffer = new ArrayBuffer(9);
105
- const dataView = new DataView(buffer, 1);
106
- const ui8a = new Uint8Array(buffer, 0);
107
- function encodeFloat16(inp) {
108
- if (inp === Infinity) {
109
- dataView.setUint16(0, 31744, false);
110
- } else if (inp === -Infinity) {
111
- dataView.setUint16(0, 64512, false);
112
- } else if (Number.isNaN(inp)) {
113
- dataView.setUint16(0, 32256, false);
114
- } else {
115
- dataView.setFloat32(0, inp);
116
- const valu32 = dataView.getUint32(0);
117
- const exponent = (valu32 & 2139095040) >> 23;
118
- const mantissa = valu32 & 8388607;
119
- if (exponent === 255) {
120
- dataView.setUint16(0, 31744, false);
121
- } else if (exponent === 0) {
122
- dataView.setUint16(0, (inp & 2147483648) >> 16 | mantissa >> 13, false);
123
- } else {
124
- const logicalExponent = exponent - 127;
125
- if (logicalExponent < -24) {
126
- dataView.setUint16(0, 0);
127
- } else if (logicalExponent < -14) {
128
- dataView.setUint16(0, (valu32 & 2147483648) >> 16 | 1 << 24 + logicalExponent, false);
129
- } else {
130
- dataView.setUint16(0, (valu32 & 2147483648) >> 16 | logicalExponent + 15 << 10 | mantissa >> 13, false);
131
- }
132
- }
133
- }
134
- }
135
- function readFloat16(ui8a, pos) {
136
- if (ui8a.length - pos < 2) {
137
- throw new Error(`${ common.decodeErrPrefix } not enough data for float16`);
138
- }
139
- const half = (ui8a[pos] << 8) + ui8a[pos + 1];
140
- if (half === 31744) {
141
- return Infinity;
142
- }
143
- if (half === 64512) {
144
- return -Infinity;
145
- }
146
- if (half === 32256) {
147
- return NaN;
148
- }
149
- const exp = half >> 10 & 31;
150
- const mant = half & 1023;
151
- let val;
152
- if (exp === 0) {
153
- val = mant * 2 ** -24;
154
- } else if (exp !== 31) {
155
- val = (mant + 1024) * 2 ** (exp - 25);
156
- } else {
157
- val = mant === 0 ? Infinity : NaN;
158
- }
159
- return half & 32768 ? -val : val;
160
- }
161
- function encodeFloat32(inp) {
162
- dataView.setFloat32(0, inp, false);
163
- }
164
- function readFloat32(ui8a, pos) {
165
- if (ui8a.length - pos < 4) {
166
- throw new Error(`${ common.decodeErrPrefix } not enough data for float32`);
167
- }
168
- const offset = (ui8a.byteOffset || 0) + pos;
169
- return new DataView(ui8a.buffer, offset, 4).getFloat32(0, false);
170
- }
171
- function encodeFloat64(inp) {
172
- dataView.setFloat64(0, inp, false);
173
- }
174
- function readFloat64(ui8a, pos) {
175
- if (ui8a.length - pos < 8) {
176
- throw new Error(`${ common.decodeErrPrefix } not enough data for float64`);
177
- }
178
- const offset = (ui8a.byteOffset || 0) + pos;
179
- return new DataView(ui8a.buffer, offset, 8).getFloat64(0, false);
180
- }
181
- encodeFloat.compareTokens = _0uint.encodeUint.compareTokens;
182
-
183
- exports.decodeBreak = decodeBreak;
184
- exports.decodeFloat16 = decodeFloat16;
185
- exports.decodeFloat32 = decodeFloat32;
186
- exports.decodeFloat64 = decodeFloat64;
187
- exports.decodeUndefined = decodeUndefined;
188
- exports.encodeFloat = encodeFloat;
package/cjs/lib/bin.js DELETED
@@ -1,139 +0,0 @@
1
- 'use strict';
2
-
3
- var process = require('process');
4
- require('../cborg.js');
5
- var diagnostic = require('./diagnostic.js');
6
- var byteUtils = require('./byte-utils.js');
7
- var encode = require('./encode.js');
8
- var decode = require('./decode.js');
9
-
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
-
12
- var process__default = /*#__PURE__*/_interopDefaultLegacy(process);
13
-
14
- function usage(code) {
15
- console.error('Usage: cborg <command> <args>');
16
- console.error('Valid commands:');
17
- console.error('\tbin2diag [binary input]');
18
- console.error('\tbin2hex [binary input]');
19
- console.error('\tbin2json [--pretty] [binary input]');
20
- console.error('\tdiag2bin [diagnostic input]');
21
- console.error('\tdiag2hex [diagnostic input]');
22
- console.error('\tdiag2json [--pretty] [diagnostic input]');
23
- console.error('\thex2bin [hex input]');
24
- console.error('\thex2diag [hex input]');
25
- console.error('\thex2json [--pretty] [hex input]');
26
- console.error('\tjson2bin \'[json input]\'');
27
- console.error('\tjson2diag \'[json input]\'');
28
- console.error('\tjson2hex \'[json input]\'');
29
- console.error('Input may either be supplied as an argument or piped via stdin');
30
- process__default["default"].exit(code || 0);
31
- }
32
- async function fromStdin() {
33
- const chunks = [];
34
- for await (const chunk of process__default["default"].stdin) {
35
- chunks.push(chunk);
36
- }
37
- return Buffer.concat(chunks);
38
- }
39
- function fromHex(str) {
40
- str = str.replace(/\r?\n/g, '');
41
- if (!/^([0-9a-f]{2})*$/i.test(str)) {
42
- throw new Error('Input string is not hexadecimal format');
43
- }
44
- return byteUtils.fromHex(str);
45
- }
46
- function argvPretty() {
47
- const argv = process__default["default"].argv.filter(s => s !== '--pretty');
48
- const pretty = argv.length !== process__default["default"].argv.length;
49
- return {
50
- argv,
51
- pretty
52
- };
53
- }
54
- async function run() {
55
- const cmd = process__default["default"].argv[2];
56
- switch (cmd) {
57
- case 'help': {
58
- return usage(0);
59
- }
60
- case 'bin2diag': {
61
- const bin = process__default["default"].argv.length < 4 ? await fromStdin() : new TextEncoder().encode(process__default["default"].argv[3]);
62
- for (const line of diagnostic.tokensToDiagnostic(bin)) {
63
- console.log(line);
64
- }
65
- return;
66
- }
67
- case 'bin2hex': {
68
- const bin = process__default["default"].argv.length < 4 ? await fromStdin() : new TextEncoder().encode(process__default["default"].argv[3]);
69
- return console.log(byteUtils.toHex(bin));
70
- }
71
- case 'bin2json': {
72
- const {argv, pretty} = argvPretty();
73
- const bin = argv.length < 4 ? await fromStdin() : new TextEncoder().encode(argv[3]);
74
- return console.log(JSON.stringify(decode.decode(bin), undefined, pretty ? 2 : undefined));
75
- }
76
- case 'diag2bin': {
77
- const bin = diagnostic.fromDiag(process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3]);
78
- return process__default["default"].stdout.write(bin);
79
- }
80
- case 'diag2hex': {
81
- const bin = diagnostic.fromDiag(process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3]);
82
- return console.log(byteUtils.toHex(bin));
83
- }
84
- case 'diag2json': {
85
- const {argv, pretty} = argvPretty();
86
- const bin = diagnostic.fromDiag(argv.length < 4 ? (await fromStdin()).toString() : argv[3]);
87
- return console.log(JSON.stringify(decode.decode(bin), undefined, pretty ? 2 : undefined));
88
- }
89
- case 'hex2bin': {
90
- const bin = fromHex(process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3]);
91
- return process__default["default"].stdout.write(bin);
92
- }
93
- case 'hex2diag': {
94
- const bin = fromHex(process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3]);
95
- for (const line of diagnostic.tokensToDiagnostic(bin)) {
96
- console.log(line);
97
- }
98
- return;
99
- }
100
- case 'hex2json': {
101
- const {argv, pretty} = argvPretty();
102
- const bin = fromHex(argv.length < 4 ? (await fromStdin()).toString() : argv[3]);
103
- return console.log(JSON.stringify(decode.decode(bin), undefined, pretty ? 2 : undefined));
104
- }
105
- case 'json2bin': {
106
- const inp = process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3];
107
- const obj = JSON.parse(inp);
108
- return process__default["default"].stdout.write(encode.encode(obj));
109
- }
110
- case 'json2diag': {
111
- const inp = process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3];
112
- const obj = JSON.parse(inp);
113
- for (const line of diagnostic.tokensToDiagnostic(encode.encode(obj))) {
114
- console.log(line);
115
- }
116
- return;
117
- }
118
- case 'json2hex': {
119
- const inp = process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3];
120
- const obj = JSON.parse(inp);
121
- return console.log(byteUtils.toHex(encode.encode(obj)));
122
- }
123
- default: {
124
- if (process__default["default"].argv.findIndex(a => a.endsWith('mocha')) === -1) {
125
- if (cmd) {
126
- console.error(`Unknown command: '${ cmd }'`);
127
- }
128
- usage(1);
129
- }
130
- }
131
- }
132
- }
133
- run().catch(err => {
134
- console.error(err);
135
- process__default["default"].exit(1);
136
- });
137
- var bin = true;
138
-
139
- module.exports = bin;
package/cjs/lib/bl.js DELETED
@@ -1,77 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var byteUtils = require('./byte-utils.js');
6
-
7
- const defaultChunkSize = 256;
8
- class Bl {
9
- constructor(chunkSize = defaultChunkSize) {
10
- this.chunkSize = chunkSize;
11
- this.cursor = 0;
12
- this.maxCursor = -1;
13
- this.chunks = [];
14
- this._initReuseChunk = null;
15
- }
16
- reset() {
17
- this.cursor = 0;
18
- this.maxCursor = -1;
19
- if (this.chunks.length) {
20
- this.chunks = [];
21
- }
22
- if (this._initReuseChunk !== null) {
23
- this.chunks.push(this._initReuseChunk);
24
- this.maxCursor = this._initReuseChunk.length - 1;
25
- }
26
- }
27
- push(bytes) {
28
- let topChunk = this.chunks[this.chunks.length - 1];
29
- const newMax = this.cursor + bytes.length;
30
- if (newMax <= this.maxCursor + 1) {
31
- const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
32
- topChunk.set(bytes, chunkPos);
33
- } else {
34
- if (topChunk) {
35
- const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
36
- if (chunkPos < topChunk.length) {
37
- this.chunks[this.chunks.length - 1] = topChunk.subarray(0, chunkPos);
38
- this.maxCursor = this.cursor - 1;
39
- }
40
- }
41
- if (bytes.length < 64 && bytes.length < this.chunkSize) {
42
- topChunk = byteUtils.alloc(this.chunkSize);
43
- this.chunks.push(topChunk);
44
- this.maxCursor += topChunk.length;
45
- if (this._initReuseChunk === null) {
46
- this._initReuseChunk = topChunk;
47
- }
48
- topChunk.set(bytes, 0);
49
- } else {
50
- this.chunks.push(bytes);
51
- this.maxCursor += bytes.length;
52
- }
53
- }
54
- this.cursor += bytes.length;
55
- }
56
- toBytes(reset = false) {
57
- let byts;
58
- if (this.chunks.length === 1) {
59
- const chunk = this.chunks[0];
60
- if (reset && this.cursor > chunk.length / 2) {
61
- byts = this.cursor === chunk.length ? chunk : chunk.subarray(0, this.cursor);
62
- this._initReuseChunk = null;
63
- this.chunks = [];
64
- } else {
65
- byts = byteUtils.slice(chunk, 0, this.cursor);
66
- }
67
- } else {
68
- byts = byteUtils.concat(this.chunks, this.cursor);
69
- }
70
- if (reset) {
71
- this.reset();
72
- }
73
- return byts;
74
- }
75
- }
76
-
77
- exports.Bl = Bl;