cborg 2.0.5 → 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/esm/lib/4array.js DELETED
@@ -1,41 +0,0 @@
1
- import {
2
- Token,
3
- Type
4
- } from './token.js';
5
- import * as uint from './0uint.js';
6
- import { decodeErrPrefix } from './common.js';
7
- function toToken(_data, _pos, prefix, length) {
8
- return new Token(Type.array, length, prefix);
9
- }
10
- export function decodeArrayCompact(data, pos, minor, _options) {
11
- return toToken(data, pos, 1, minor);
12
- }
13
- export function decodeArray8(data, pos, _minor, options) {
14
- return toToken(data, pos, 2, uint.readUint8(data, pos + 1, options));
15
- }
16
- export function decodeArray16(data, pos, _minor, options) {
17
- return toToken(data, pos, 3, uint.readUint16(data, pos + 1, options));
18
- }
19
- export function decodeArray32(data, pos, _minor, options) {
20
- return toToken(data, pos, 5, uint.readUint32(data, pos + 1, options));
21
- }
22
- export function decodeArray64(data, pos, _minor, options) {
23
- const l = uint.readUint64(data, pos + 1, options);
24
- if (typeof l === 'bigint') {
25
- throw new Error(`${ decodeErrPrefix } 64-bit integer array lengths not supported`);
26
- }
27
- return toToken(data, pos, 9, l);
28
- }
29
- export function decodeArrayIndefinite(data, pos, _minor, options) {
30
- if (options.allowIndefinite === false) {
31
- throw new Error(`${ decodeErrPrefix } indefinite length items not allowed`);
32
- }
33
- return toToken(data, pos, 1, Infinity);
34
- }
35
- export function encodeArray(buf, token) {
36
- uint.encodeUintValue(buf, Type.array.majorEncoded, token.value);
37
- }
38
- encodeArray.compareTokens = uint.encodeUint.compareTokens;
39
- encodeArray.encodedSize = function encodedSize(token) {
40
- return uint.encodeUintValue.encodedSize(token.value);
41
- };
package/esm/lib/5map.js DELETED
@@ -1,41 +0,0 @@
1
- import {
2
- Token,
3
- Type
4
- } from './token.js';
5
- import * as uint from './0uint.js';
6
- import { decodeErrPrefix } from './common.js';
7
- function toToken(_data, _pos, prefix, length) {
8
- return new Token(Type.map, length, prefix);
9
- }
10
- export function decodeMapCompact(data, pos, minor, _options) {
11
- return toToken(data, pos, 1, minor);
12
- }
13
- export function decodeMap8(data, pos, _minor, options) {
14
- return toToken(data, pos, 2, uint.readUint8(data, pos + 1, options));
15
- }
16
- export function decodeMap16(data, pos, _minor, options) {
17
- return toToken(data, pos, 3, uint.readUint16(data, pos + 1, options));
18
- }
19
- export function decodeMap32(data, pos, _minor, options) {
20
- return toToken(data, pos, 5, uint.readUint32(data, pos + 1, options));
21
- }
22
- export function decodeMap64(data, pos, _minor, options) {
23
- const l = uint.readUint64(data, pos + 1, options);
24
- if (typeof l === 'bigint') {
25
- throw new Error(`${ decodeErrPrefix } 64-bit integer map lengths not supported`);
26
- }
27
- return toToken(data, pos, 9, l);
28
- }
29
- export function decodeMapIndefinite(data, pos, _minor, options) {
30
- if (options.allowIndefinite === false) {
31
- throw new Error(`${ decodeErrPrefix } indefinite length items not allowed`);
32
- }
33
- return toToken(data, pos, 1, Infinity);
34
- }
35
- export function encodeMap(buf, token) {
36
- uint.encodeUintValue(buf, Type.map.majorEncoded, token.value);
37
- }
38
- encodeMap.compareTokens = uint.encodeUint.compareTokens;
39
- encodeMap.encodedSize = function encodedSize(token) {
40
- return uint.encodeUintValue.encodedSize(token.value);
41
- };
package/esm/lib/6tag.js DELETED
@@ -1,27 +0,0 @@
1
- import {
2
- Token,
3
- Type
4
- } from './token.js';
5
- import * as uint from './0uint.js';
6
- export function decodeTagCompact(_data, _pos, minor, _options) {
7
- return new Token(Type.tag, minor, 1);
8
- }
9
- export function decodeTag8(data, pos, _minor, options) {
10
- return new Token(Type.tag, uint.readUint8(data, pos + 1, options), 2);
11
- }
12
- export function decodeTag16(data, pos, _minor, options) {
13
- return new Token(Type.tag, uint.readUint16(data, pos + 1, options), 3);
14
- }
15
- export function decodeTag32(data, pos, _minor, options) {
16
- return new Token(Type.tag, uint.readUint32(data, pos + 1, options), 5);
17
- }
18
- export function decodeTag64(data, pos, _minor, options) {
19
- return new Token(Type.tag, uint.readUint64(data, pos + 1, options), 9);
20
- }
21
- export function encodeTag(buf, token) {
22
- uint.encodeUintValue(buf, Type.tag.majorEncoded, token.value);
23
- }
24
- encodeTag.compareTokens = uint.encodeUint.compareTokens;
25
- encodeTag.encodedSize = function encodedSize(token) {
26
- return uint.encodeUintValue.encodedSize(token.value);
27
- };
package/esm/lib/7float.js DELETED
@@ -1,179 +0,0 @@
1
- import {
2
- Token,
3
- Type
4
- } from './token.js';
5
- import { decodeErrPrefix } from './common.js';
6
- import { encodeUint } from './0uint.js';
7
- const MINOR_FALSE = 20;
8
- const MINOR_TRUE = 21;
9
- const MINOR_NULL = 22;
10
- const MINOR_UNDEFINED = 23;
11
- export function decodeUndefined(_data, _pos, _minor, options) {
12
- if (options.allowUndefined === false) {
13
- throw new Error(`${ decodeErrPrefix } undefined values are not supported`);
14
- } else if (options.coerceUndefinedToNull === true) {
15
- return new Token(Type.null, null, 1);
16
- }
17
- return new Token(Type.undefined, undefined, 1);
18
- }
19
- export function decodeBreak(_data, _pos, _minor, options) {
20
- if (options.allowIndefinite === false) {
21
- throw new Error(`${ decodeErrPrefix } indefinite length items not allowed`);
22
- }
23
- return new Token(Type.break, undefined, 1);
24
- }
25
- function createToken(value, bytes, options) {
26
- if (options) {
27
- if (options.allowNaN === false && Number.isNaN(value)) {
28
- throw new Error(`${ decodeErrPrefix } NaN values are not supported`);
29
- }
30
- if (options.allowInfinity === false && (value === Infinity || value === -Infinity)) {
31
- throw new Error(`${ decodeErrPrefix } Infinity values are not supported`);
32
- }
33
- }
34
- return new Token(Type.float, value, bytes);
35
- }
36
- export function decodeFloat16(data, pos, _minor, options) {
37
- return createToken(readFloat16(data, pos + 1), 3, options);
38
- }
39
- export function decodeFloat32(data, pos, _minor, options) {
40
- return createToken(readFloat32(data, pos + 1), 5, options);
41
- }
42
- export function decodeFloat64(data, pos, _minor, options) {
43
- return createToken(readFloat64(data, pos + 1), 9, options);
44
- }
45
- export function encodeFloat(buf, token, options) {
46
- const float = token.value;
47
- if (float === false) {
48
- buf.push([Type.float.majorEncoded | MINOR_FALSE]);
49
- } else if (float === true) {
50
- buf.push([Type.float.majorEncoded | MINOR_TRUE]);
51
- } else if (float === null) {
52
- buf.push([Type.float.majorEncoded | MINOR_NULL]);
53
- } else if (float === undefined) {
54
- buf.push([Type.float.majorEncoded | MINOR_UNDEFINED]);
55
- } else {
56
- let decoded;
57
- let success = false;
58
- if (!options || options.float64 !== true) {
59
- encodeFloat16(float);
60
- decoded = readFloat16(ui8a, 1);
61
- if (float === decoded || Number.isNaN(float)) {
62
- ui8a[0] = 249;
63
- buf.push(ui8a.slice(0, 3));
64
- success = true;
65
- } else {
66
- encodeFloat32(float);
67
- decoded = readFloat32(ui8a, 1);
68
- if (float === decoded) {
69
- ui8a[0] = 250;
70
- buf.push(ui8a.slice(0, 5));
71
- success = true;
72
- }
73
- }
74
- }
75
- if (!success) {
76
- encodeFloat64(float);
77
- decoded = readFloat64(ui8a, 1);
78
- ui8a[0] = 251;
79
- buf.push(ui8a.slice(0, 9));
80
- }
81
- }
82
- }
83
- encodeFloat.encodedSize = function encodedSize(token, options) {
84
- const float = token.value;
85
- if (float === false || float === true || float === null || float === undefined) {
86
- return 1;
87
- }
88
- if (!options || options.float64 !== true) {
89
- encodeFloat16(float);
90
- let decoded = readFloat16(ui8a, 1);
91
- if (float === decoded || Number.isNaN(float)) {
92
- return 3;
93
- }
94
- encodeFloat32(float);
95
- decoded = readFloat32(ui8a, 1);
96
- if (float === decoded) {
97
- return 5;
98
- }
99
- }
100
- return 9;
101
- };
102
- const buffer = new ArrayBuffer(9);
103
- const dataView = new DataView(buffer, 1);
104
- const ui8a = new Uint8Array(buffer, 0);
105
- function encodeFloat16(inp) {
106
- if (inp === Infinity) {
107
- dataView.setUint16(0, 31744, false);
108
- } else if (inp === -Infinity) {
109
- dataView.setUint16(0, 64512, false);
110
- } else if (Number.isNaN(inp)) {
111
- dataView.setUint16(0, 32256, false);
112
- } else {
113
- dataView.setFloat32(0, inp);
114
- const valu32 = dataView.getUint32(0);
115
- const exponent = (valu32 & 2139095040) >> 23;
116
- const mantissa = valu32 & 8388607;
117
- if (exponent === 255) {
118
- dataView.setUint16(0, 31744, false);
119
- } else if (exponent === 0) {
120
- dataView.setUint16(0, (inp & 2147483648) >> 16 | mantissa >> 13, false);
121
- } else {
122
- const logicalExponent = exponent - 127;
123
- if (logicalExponent < -24) {
124
- dataView.setUint16(0, 0);
125
- } else if (logicalExponent < -14) {
126
- dataView.setUint16(0, (valu32 & 2147483648) >> 16 | 1 << 24 + logicalExponent, false);
127
- } else {
128
- dataView.setUint16(0, (valu32 & 2147483648) >> 16 | logicalExponent + 15 << 10 | mantissa >> 13, false);
129
- }
130
- }
131
- }
132
- }
133
- function readFloat16(ui8a, pos) {
134
- if (ui8a.length - pos < 2) {
135
- throw new Error(`${ decodeErrPrefix } not enough data for float16`);
136
- }
137
- const half = (ui8a[pos] << 8) + ui8a[pos + 1];
138
- if (half === 31744) {
139
- return Infinity;
140
- }
141
- if (half === 64512) {
142
- return -Infinity;
143
- }
144
- if (half === 32256) {
145
- return NaN;
146
- }
147
- const exp = half >> 10 & 31;
148
- const mant = half & 1023;
149
- let val;
150
- if (exp === 0) {
151
- val = mant * 2 ** -24;
152
- } else if (exp !== 31) {
153
- val = (mant + 1024) * 2 ** (exp - 25);
154
- } else {
155
- val = mant === 0 ? Infinity : NaN;
156
- }
157
- return half & 32768 ? -val : val;
158
- }
159
- function encodeFloat32(inp) {
160
- dataView.setFloat32(0, inp, false);
161
- }
162
- function readFloat32(ui8a, pos) {
163
- if (ui8a.length - pos < 4) {
164
- throw new Error(`${ decodeErrPrefix } not enough data for float32`);
165
- }
166
- const offset = (ui8a.byteOffset || 0) + pos;
167
- return new DataView(ui8a.buffer, offset, 4).getFloat32(0, false);
168
- }
169
- function encodeFloat64(inp) {
170
- dataView.setFloat64(0, inp, false);
171
- }
172
- function readFloat64(ui8a, pos) {
173
- if (ui8a.length - pos < 8) {
174
- throw new Error(`${ decodeErrPrefix } not enough data for float64`);
175
- }
176
- const offset = (ui8a.byteOffset || 0) + pos;
177
- return new DataView(ui8a.buffer, offset, 8).getFloat64(0, false);
178
- }
179
- encodeFloat.compareTokens = encodeUint.compareTokens;
package/esm/lib/bin.js DELETED
@@ -1,137 +0,0 @@
1
- import process from 'process';
2
- import {
3
- decode,
4
- encode
5
- } from '../cborg.js';
6
- import {
7
- tokensToDiagnostic,
8
- fromDiag
9
- } from './diagnostic.js';
10
- import {
11
- fromHex as _fromHex,
12
- toHex
13
- } from './byte-utils.js';
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.exit(code || 0);
31
- }
32
- async function fromStdin() {
33
- const chunks = [];
34
- for await (const chunk of process.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 _fromHex(str);
45
- }
46
- function argvPretty() {
47
- const argv = process.argv.filter(s => s !== '--pretty');
48
- const pretty = argv.length !== process.argv.length;
49
- return {
50
- argv,
51
- pretty
52
- };
53
- }
54
- async function run() {
55
- const cmd = process.argv[2];
56
- switch (cmd) {
57
- case 'help': {
58
- return usage(0);
59
- }
60
- case 'bin2diag': {
61
- const bin = process.argv.length < 4 ? await fromStdin() : new TextEncoder().encode(process.argv[3]);
62
- for (const line of tokensToDiagnostic(bin)) {
63
- console.log(line);
64
- }
65
- return;
66
- }
67
- case 'bin2hex': {
68
- const bin = process.argv.length < 4 ? await fromStdin() : new TextEncoder().encode(process.argv[3]);
69
- return console.log(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(bin), undefined, pretty ? 2 : undefined));
75
- }
76
- case 'diag2bin': {
77
- const bin = fromDiag(process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3]);
78
- return process.stdout.write(bin);
79
- }
80
- case 'diag2hex': {
81
- const bin = fromDiag(process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3]);
82
- return console.log(toHex(bin));
83
- }
84
- case 'diag2json': {
85
- const {argv, pretty} = argvPretty();
86
- const bin = fromDiag(argv.length < 4 ? (await fromStdin()).toString() : argv[3]);
87
- return console.log(JSON.stringify(decode(bin), undefined, pretty ? 2 : undefined));
88
- }
89
- case 'hex2bin': {
90
- const bin = fromHex(process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3]);
91
- return process.stdout.write(bin);
92
- }
93
- case 'hex2diag': {
94
- const bin = fromHex(process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3]);
95
- for (const line of 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(bin), undefined, pretty ? 2 : undefined));
104
- }
105
- case 'json2bin': {
106
- const inp = process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3];
107
- const obj = JSON.parse(inp);
108
- return process.stdout.write(encode(obj));
109
- }
110
- case 'json2diag': {
111
- const inp = process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3];
112
- const obj = JSON.parse(inp);
113
- for (const line of tokensToDiagnostic(encode(obj))) {
114
- console.log(line);
115
- }
116
- return;
117
- }
118
- case 'json2hex': {
119
- const inp = process.argv.length < 4 ? (await fromStdin()).toString() : process.argv[3];
120
- const obj = JSON.parse(inp);
121
- return console.log(toHex(encode(obj)));
122
- }
123
- default: {
124
- if (process.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.exit(1);
136
- });
137
- export default true;
package/esm/lib/bl.js DELETED
@@ -1,74 +0,0 @@
1
- import {
2
- alloc,
3
- concat,
4
- slice
5
- } from './byte-utils.js';
6
- const defaultChunkSize = 256;
7
- export class Bl {
8
- constructor(chunkSize = defaultChunkSize) {
9
- this.chunkSize = chunkSize;
10
- this.cursor = 0;
11
- this.maxCursor = -1;
12
- this.chunks = [];
13
- this._initReuseChunk = null;
14
- }
15
- reset() {
16
- this.cursor = 0;
17
- this.maxCursor = -1;
18
- if (this.chunks.length) {
19
- this.chunks = [];
20
- }
21
- if (this._initReuseChunk !== null) {
22
- this.chunks.push(this._initReuseChunk);
23
- this.maxCursor = this._initReuseChunk.length - 1;
24
- }
25
- }
26
- push(bytes) {
27
- let topChunk = this.chunks[this.chunks.length - 1];
28
- const newMax = this.cursor + bytes.length;
29
- if (newMax <= this.maxCursor + 1) {
30
- const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
31
- topChunk.set(bytes, chunkPos);
32
- } else {
33
- if (topChunk) {
34
- const chunkPos = topChunk.length - (this.maxCursor - this.cursor) - 1;
35
- if (chunkPos < topChunk.length) {
36
- this.chunks[this.chunks.length - 1] = topChunk.subarray(0, chunkPos);
37
- this.maxCursor = this.cursor - 1;
38
- }
39
- }
40
- if (bytes.length < 64 && bytes.length < this.chunkSize) {
41
- topChunk = alloc(this.chunkSize);
42
- this.chunks.push(topChunk);
43
- this.maxCursor += topChunk.length;
44
- if (this._initReuseChunk === null) {
45
- this._initReuseChunk = topChunk;
46
- }
47
- topChunk.set(bytes, 0);
48
- } else {
49
- this.chunks.push(bytes);
50
- this.maxCursor += bytes.length;
51
- }
52
- }
53
- this.cursor += bytes.length;
54
- }
55
- toBytes(reset = false) {
56
- let byts;
57
- if (this.chunks.length === 1) {
58
- const chunk = this.chunks[0];
59
- if (reset && this.cursor > chunk.length / 2) {
60
- byts = this.cursor === chunk.length ? chunk : chunk.subarray(0, this.cursor);
61
- this._initReuseChunk = null;
62
- this.chunks = [];
63
- } else {
64
- byts = slice(chunk, 0, this.cursor);
65
- }
66
- } else {
67
- byts = concat(this.chunks, this.cursor);
68
- }
69
- if (reset) {
70
- this.reset();
71
- }
72
- return byts;
73
- }
74
- }