cborg 1.10.2 → 2.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 (140) 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/4array.d.ts +1 -1
  10. package/types/lib/5map.d.ts +1 -1
  11. package/types/lib/6tag.d.ts +1 -1
  12. package/types/lib/7float.d.ts +1 -1
  13. package/types/lib/bin.d.ts +1 -0
  14. package/types/lib/token.d.ts +13 -13
  15. package/cjs/browser-test/appendix_a.js +0 -643
  16. package/cjs/browser-test/common.js +0 -24
  17. package/cjs/browser-test/node-test-bin.js +0 -348
  18. package/cjs/browser-test/noop-bin-test.js +0 -4
  19. package/cjs/browser-test/test-0uint.js +0 -158
  20. package/cjs/browser-test/test-1negint.js +0 -152
  21. package/cjs/browser-test/test-2bytes.js +0 -254
  22. package/cjs/browser-test/test-3string.js +0 -144
  23. package/cjs/browser-test/test-4array.js +0 -200
  24. package/cjs/browser-test/test-5map.js +0 -667
  25. package/cjs/browser-test/test-6tag.js +0 -75
  26. package/cjs/browser-test/test-7float.js +0 -253
  27. package/cjs/browser-test/test-bl.js +0 -91
  28. package/cjs/browser-test/test-cbor-vectors.js +0 -98
  29. package/cjs/browser-test/test-decode-errors.js +0 -77
  30. package/cjs/browser-test/test-fuzz.js +0 -56
  31. package/cjs/browser-test/test-json.js +0 -281
  32. package/cjs/browser-test/test-length.js +0 -63
  33. package/cjs/cborg.js +0 -14
  34. package/cjs/lib/0uint.js +0 -163
  35. package/cjs/lib/1negint.js +0 -63
  36. package/cjs/lib/2bytes.js +0 -62
  37. package/cjs/lib/3string.js +0 -46
  38. package/cjs/lib/4array.js +0 -51
  39. package/cjs/lib/5map.js +0 -51
  40. package/cjs/lib/6tag.js +0 -36
  41. package/cjs/lib/7float.js +0 -188
  42. package/cjs/lib/bin.js +0 -139
  43. package/cjs/lib/bl.js +0 -77
  44. package/cjs/lib/byte-utils.js +0 -245
  45. package/cjs/lib/common.js +0 -22
  46. package/cjs/lib/decode.js +0 -140
  47. package/cjs/lib/diagnostic.js +0 -124
  48. package/cjs/lib/encode.js +0 -248
  49. package/cjs/lib/is.js +0 -87
  50. package/cjs/lib/json/decode.js +0 -414
  51. package/cjs/lib/json/encode.js +0 -161
  52. package/cjs/lib/json/json.js +0 -12
  53. package/cjs/lib/jump.js +0 -174
  54. package/cjs/lib/length.js +0 -36
  55. package/cjs/lib/token.js +0 -46
  56. package/cjs/node-test/appendix_a.js +0 -643
  57. package/cjs/node-test/common.js +0 -24
  58. package/cjs/node-test/node-test-bin.js +0 -348
  59. package/cjs/node-test/noop-bin-test.js +0 -4
  60. package/cjs/node-test/test-0uint.js +0 -158
  61. package/cjs/node-test/test-1negint.js +0 -152
  62. package/cjs/node-test/test-2bytes.js +0 -254
  63. package/cjs/node-test/test-3string.js +0 -144
  64. package/cjs/node-test/test-4array.js +0 -200
  65. package/cjs/node-test/test-5map.js +0 -667
  66. package/cjs/node-test/test-6tag.js +0 -75
  67. package/cjs/node-test/test-7float.js +0 -253
  68. package/cjs/node-test/test-bl.js +0 -91
  69. package/cjs/node-test/test-cbor-vectors.js +0 -98
  70. package/cjs/node-test/test-decode-errors.js +0 -77
  71. package/cjs/node-test/test-fuzz.js +0 -56
  72. package/cjs/node-test/test-json.js +0 -281
  73. package/cjs/node-test/test-length.js +0 -63
  74. package/cjs/taglib.js +0 -44
  75. package/cli.js +0 -7
  76. package/esm/browser-test/appendix_a.js +0 -640
  77. package/esm/browser-test/common.js +0 -19
  78. package/esm/browser-test/node-test-bin.js +0 -339
  79. package/esm/browser-test/noop-bin-test.js +0 -1
  80. package/esm/browser-test/test-0uint.js +0 -155
  81. package/esm/browser-test/test-1negint.js +0 -149
  82. package/esm/browser-test/test-2bytes.js +0 -252
  83. package/esm/browser-test/test-3string.js +0 -141
  84. package/esm/browser-test/test-4array.js +0 -197
  85. package/esm/browser-test/test-5map.js +0 -664
  86. package/esm/browser-test/test-6tag.js +0 -78
  87. package/esm/browser-test/test-7float.js +0 -250
  88. package/esm/browser-test/test-bl.js +0 -84
  89. package/esm/browser-test/test-cbor-vectors.js +0 -95
  90. package/esm/browser-test/test-decode-errors.js +0 -69
  91. package/esm/browser-test/test-fuzz.js +0 -50
  92. package/esm/browser-test/test-json.js +0 -279
  93. package/esm/browser-test/test-length.js +0 -55
  94. package/esm/cborg.js +0 -12
  95. package/esm/lib/0uint.js +0 -152
  96. package/esm/lib/1negint.js +0 -55
  97. package/esm/lib/2bytes.js +0 -59
  98. package/esm/lib/3string.js +0 -43
  99. package/esm/lib/4array.js +0 -41
  100. package/esm/lib/5map.js +0 -41
  101. package/esm/lib/6tag.js +0 -27
  102. package/esm/lib/7float.js +0 -179
  103. package/esm/lib/bin.js +0 -137
  104. package/esm/lib/bl.js +0 -74
  105. package/esm/lib/byte-utils.js +0 -228
  106. package/esm/lib/common.js +0 -19
  107. package/esm/lib/decode.js +0 -139
  108. package/esm/lib/diagnostic.js +0 -123
  109. package/esm/lib/encode.js +0 -246
  110. package/esm/lib/is.js +0 -81
  111. package/esm/lib/json/decode.js +0 -413
  112. package/esm/lib/json/encode.js +0 -160
  113. package/esm/lib/json/json.js +0 -10
  114. package/esm/lib/jump.js +0 -168
  115. package/esm/lib/length.js +0 -31
  116. package/esm/lib/token.js +0 -43
  117. package/esm/node-test/appendix_a.js +0 -640
  118. package/esm/node-test/common.js +0 -19
  119. package/esm/node-test/node-test-bin.js +0 -339
  120. package/esm/node-test/noop-bin-test.js +0 -1
  121. package/esm/node-test/test-0uint.js +0 -155
  122. package/esm/node-test/test-1negint.js +0 -149
  123. package/esm/node-test/test-2bytes.js +0 -252
  124. package/esm/node-test/test-3string.js +0 -141
  125. package/esm/node-test/test-4array.js +0 -197
  126. package/esm/node-test/test-5map.js +0 -664
  127. package/esm/node-test/test-6tag.js +0 -78
  128. package/esm/node-test/test-7float.js +0 -250
  129. package/esm/node-test/test-bl.js +0 -84
  130. package/esm/node-test/test-cbor-vectors.js +0 -95
  131. package/esm/node-test/test-decode-errors.js +0 -69
  132. package/esm/node-test/test-fuzz.js +0 -50
  133. package/esm/node-test/test-json.js +0 -279
  134. package/esm/node-test/test-length.js +0 -55
  135. package/esm/package.json +0 -4
  136. package/esm/taglib.js +0 -37
  137. package/index.js +0 -1
  138. package/json +0 -1
  139. package/length +0 -1
  140. package/taglib +0 -1
@@ -1,348 +0,0 @@
1
- 'use strict';
2
-
3
- var chai = require('chai');
4
- var child_process = require('child_process');
5
- var process = require('process');
6
- var path = require('path');
7
- var os = require('os');
8
- var url = require('url');
9
-
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
-
12
- var chai__default = /*#__PURE__*/_interopDefaultLegacy(chai);
13
- var process__default = /*#__PURE__*/_interopDefaultLegacy(process);
14
- var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
15
-
16
- const {assert} = chai__default["default"];
17
- const fixture1JsonString = '{"a":1,"b":[2,3],"smile":"\uD83D\uDE00"}';
18
- const fixture1JsonPrettyString = `{
19
- "a": 1,
20
- "b": [
21
- 2,
22
- 3
23
- ],
24
- "smile": "😀"
25
- }
26
- `;
27
- const fixture1HexString = 'a3616101616282020365736d696c6564f09f9880';
28
- const fixture1Bin = fromHex(fixture1HexString);
29
- const fixture1BinString = new TextDecoder().decode(fixture1Bin);
30
- const fixture1DiagnosticString = `a3 # map(3)
31
- 61 # string(1)
32
- 61 # "a"
33
- 01 # uint(1)
34
- 61 # string(1)
35
- 62 # "b"
36
- 82 # array(2)
37
- 02 # uint(2)
38
- 03 # uint(3)
39
- 65 # string(5)
40
- 736d696c65 # "smile"
41
- 64 # string(2)
42
- f09f9880 # "😀"
43
- `;
44
- const fixture2HexString = 'a4616101616282020363627566440102036165736d696c6564f09f9880';
45
- const fixture2DiagnosticString = `a4 # map(4)
46
- 61 # string(1)
47
- 61 # "a"
48
- 01 # uint(1)
49
- 61 # string(1)
50
- 62 # "b"
51
- 82 # array(2)
52
- 02 # uint(2)
53
- 03 # uint(3)
54
- 63 # string(3)
55
- 627566 # "buf"
56
- 44 # bytes(4)
57
- 01020361 # "\\x01\\x02\\x03a"
58
- 65 # string(5)
59
- 736d696c65 # "smile"
60
- 64 # string(2)
61
- f09f9880 # "😀"
62
- `;
63
- const binPath = path__default["default"].join(path__default["default"].dirname(url.fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('browser-test/node-test-bin.js', document.baseURI).href)))), '../lib/bin.js');
64
- function fromHex(hex) {
65
- return new Uint8Array(hex.split('').map((c, i, d) => i % 2 === 0 ? `0x${ c }${ d[i + 1] }` : '').filter(Boolean).map(e => parseInt(e, 16)));
66
- }
67
- async function execBin(cmd, stdin) {
68
- return new Promise((resolve, reject) => {
69
- const cp = child_process.exec(`"${ process__default["default"].execPath }" "${ binPath }" ${ cmd }`, (err, stdout, stderr) => {
70
- if (err) {
71
- err.stdout = stdout;
72
- err.stderr = stderr;
73
- return reject(err);
74
- }
75
- resolve({
76
- stdout,
77
- stderr
78
- });
79
- });
80
- if (stdin != null) {
81
- cp.on('spawn', () => {
82
- cp.stdin.write(stdin);
83
- cp.stdin.end();
84
- });
85
- }
86
- });
87
- }
88
- describe('Bin', () => {
89
- it('usage', async () => {
90
- try {
91
- await execBin('');
92
- assert.fail('should have errored');
93
- } catch (e) {
94
- assert.strictEqual(e.stdout, '');
95
- assert.strictEqual(e.stderr, `Usage: cborg <command> <args>
96
- Valid commands:
97
- \tbin2diag [binary input]
98
- \tbin2hex [binary input]
99
- \tbin2json [--pretty] [binary input]
100
- \tdiag2bin [diagnostic input]
101
- \tdiag2hex [diagnostic input]
102
- \tdiag2json [--pretty] [diagnostic input]
103
- \thex2bin [hex input]
104
- \thex2diag [hex input]
105
- \thex2json [--pretty] [hex input]
106
- \tjson2bin '[json input]'
107
- \tjson2diag '[json input]'
108
- \tjson2hex '[json input]'
109
- Input may either be supplied as an argument or piped via stdin
110
- `);
111
- }
112
- });
113
- it('bad cmd', async () => {
114
- try {
115
- await execBin('blip');
116
- assert.fail('should have errored');
117
- } catch (e) {
118
- assert.strictEqual(e.stdout, '');
119
- assert.strictEqual(e.stderr, `Unknown command: 'blip'
120
- Usage: cborg <command> <args>
121
- Valid commands:
122
- \tbin2diag [binary input]
123
- \tbin2hex [binary input]
124
- \tbin2json [--pretty] [binary input]
125
- \tdiag2bin [diagnostic input]
126
- \tdiag2hex [diagnostic input]
127
- \tdiag2json [--pretty] [diagnostic input]
128
- \thex2bin [hex input]
129
- \thex2diag [hex input]
130
- \thex2json [--pretty] [hex input]
131
- \tjson2bin '[json input]'
132
- \tjson2diag '[json input]'
133
- \tjson2hex '[json input]'
134
- Input may either be supplied as an argument or piped via stdin
135
- `);
136
- }
137
- });
138
- it('help', async () => {
139
- const {stdout, stderr} = await execBin('help');
140
- assert.strictEqual(stdout, '');
141
- assert.strictEqual(stderr, `Usage: cborg <command> <args>
142
- Valid commands:
143
- \tbin2diag [binary input]
144
- \tbin2hex [binary input]
145
- \tbin2json [--pretty] [binary input]
146
- \tdiag2bin [diagnostic input]
147
- \tdiag2hex [diagnostic input]
148
- \tdiag2json [--pretty] [diagnostic input]
149
- \thex2bin [hex input]
150
- \thex2diag [hex input]
151
- \thex2json [--pretty] [hex input]
152
- \tjson2bin '[json input]'
153
- \tjson2diag '[json input]'
154
- \tjson2hex '[json input]'
155
- Input may either be supplied as an argument or piped via stdin
156
- `);
157
- });
158
- it('bin2diag (stdin)', async () => {
159
- const {stdout, stderr} = await execBin('bin2diag', fixture1Bin);
160
- assert.strictEqual(stderr, '');
161
- assert.strictEqual(stdout, fixture1DiagnosticString);
162
- });
163
- it('bin2hex (stdin)', async () => {
164
- const {stdout, stderr} = await execBin('bin2hex', fixture1Bin);
165
- assert.strictEqual(stderr, '');
166
- assert.strictEqual(stdout, `${ fixture1HexString }\n`);
167
- });
168
- it('bin2json (stdin)', async () => {
169
- const {stdout, stderr} = await execBin('bin2json', fixture1Bin);
170
- assert.strictEqual(stderr, '');
171
- assert.strictEqual(stdout, `${ fixture1JsonString }\n`);
172
- });
173
- it('bin2json pretty (stdin)', async () => {
174
- const {stdout, stderr} = await execBin('bin2json --pretty', fixture1Bin);
175
- assert.strictEqual(stderr, '');
176
- assert.strictEqual(stdout, fixture1JsonPrettyString);
177
- });
178
- for (const stdin of [
179
- true,
180
- false
181
- ]) {
182
- if (os.platform() !== 'win32' || stdin) {
183
- it(`diag2bin${ stdin ? ' (stdin)' : '' }`, async () => {
184
- const {stdout, stderr} = !stdin ? await execBin(`diag2bin '${ fixture1DiagnosticString }'`) : await execBin('diag2bin', fixture1DiagnosticString);
185
- assert.strictEqual(stderr, '');
186
- assert.strictEqual(stdout, fixture1BinString);
187
- });
188
- it(`diag2hex${ stdin ? ' (stdin)' : '' }`, async () => {
189
- const {stdout, stderr} = !stdin ? await execBin(`diag2hex '${ fixture1DiagnosticString }'`) : await execBin('diag2hex', fixture1DiagnosticString);
190
- assert.strictEqual(stderr, '');
191
- assert.strictEqual(stdout, `${ fixture1HexString }\n`);
192
- });
193
- it(`diag2json${ stdin ? ' (stdin)' : '' }`, async () => {
194
- const {stdout, stderr} = !stdin ? await execBin(`diag2json '${ fixture1DiagnosticString }'`) : await execBin('diag2json', fixture1DiagnosticString);
195
- assert.strictEqual(stderr, '');
196
- assert.strictEqual(stdout, `${ fixture1JsonString }\n`);
197
- });
198
- it(`diag2json pretty${ stdin ? ' (stdin)' : '' }`, async () => {
199
- const {stdout, stderr} = !stdin ? await execBin(`diag2json --pretty '${ fixture1DiagnosticString }'`) : await execBin('diag2json --pretty', fixture1DiagnosticString);
200
- assert.strictEqual(stderr, '');
201
- assert.strictEqual(stdout, fixture1JsonPrettyString);
202
- });
203
- }
204
- it(`hex2bin${ stdin ? ' (stdin)' : '' }`, async () => {
205
- const {stdout, stderr} = !stdin ? await execBin(`hex2bin ${ fixture1HexString }`) : await execBin('hex2bin', fixture1HexString);
206
- assert.strictEqual(stderr, '');
207
- assert.strictEqual(stdout, fixture1BinString);
208
- });
209
- it(`hex2diag${ stdin ? ' (stdin)' : '' }`, async () => {
210
- const {stdout, stderr} = !stdin ? await execBin(`hex2diag ${ fixture2HexString }`) : await execBin('hex2diag', fixture2HexString);
211
- assert.strictEqual(stderr, '');
212
- assert.strictEqual(stdout, fixture2DiagnosticString);
213
- });
214
- it(`hex2json${ stdin ? ' (stdin)' : '' }`, async () => {
215
- const {stdout, stderr} = !stdin ? await execBin(`hex2json ${ fixture1HexString }`) : await execBin('hex2json', fixture1HexString);
216
- assert.strictEqual(stderr, '');
217
- assert.strictEqual(stdout, `${ fixture1JsonString }\n`);
218
- });
219
- it(`hex2json pretty${ stdin ? ' (stdin)' : '' }`, async () => {
220
- const {stdout, stderr} = !stdin ? await execBin(`hex2json --pretty ${ fixture1HexString }`) : await execBin('hex2json --pretty', fixture1HexString);
221
- assert.strictEqual(stderr, '');
222
- assert.strictEqual(stdout, fixture1JsonPrettyString);
223
- });
224
- it(`json2bin${ stdin ? ' (stdin)' : '' }`, async () => {
225
- const {stdout, stderr} = !stdin ? await execBin('json2bin "{\\"a\\":1,\\"b\\":[2,3],\\"smile\\":\\"\uD83D\uDE00\\"}"') : await execBin('json2bin', fixture1JsonString);
226
- assert.strictEqual(stderr, '');
227
- assert.strictEqual(stdout, fixture1BinString);
228
- });
229
- it(`json2diag${ stdin ? ' (stdin)' : '' }`, async () => {
230
- const {stdout, stderr} = !stdin ? await execBin('json2diag "{\\"a\\":1,\\"b\\":[2,3],\\"smile\\":\\"\uD83D\uDE00\\"}"') : await execBin('json2diag', fixture1JsonString);
231
- assert.strictEqual(stderr, '');
232
- assert.strictEqual(stdout, fixture1DiagnosticString);
233
- });
234
- it(`json2hex${ stdin ? ' (stdin)' : '' }`, async () => {
235
- const {stdout, stderr} = !stdin ? await execBin(`json2hex "${ fixture1JsonString.replace(/"/g, '\\"') }"`) : await execBin('json2hex', fixture1JsonString);
236
- assert.strictEqual(stderr, '');
237
- assert.strictEqual(stdout, `${ fixture1HexString }\n`);
238
- });
239
- }
240
- it('diag indenting', async () => {
241
- const {stdout, stderr} = await execBin('json2diag', '{"a":[],"b":{},"c":{"a":1,"b":{"a":{"a":{}}}},"d":{"a":{"a":{"a":1},"b":2,"c":[]}},"e":[[[[{"a":{}}]]]],"f":1}');
242
- assert.strictEqual(stderr, '');
243
- assert.strictEqual(stdout, `a6 # map(6)
244
- 61 # string(1)
245
- 61 # "a"
246
- 80 # array(0)
247
- 61 # string(1)
248
- 62 # "b"
249
- a0 # map(0)
250
- 61 # string(1)
251
- 63 # "c"
252
- a2 # map(2)
253
- 61 # string(1)
254
- 61 # "a"
255
- 01 # uint(1)
256
- 61 # string(1)
257
- 62 # "b"
258
- a1 # map(1)
259
- 61 # string(1)
260
- 61 # "a"
261
- a1 # map(1)
262
- 61 # string(1)
263
- 61 # "a"
264
- a0 # map(0)
265
- 61 # string(1)
266
- 64 # "d"
267
- a1 # map(1)
268
- 61 # string(1)
269
- 61 # "a"
270
- a3 # map(3)
271
- 61 # string(1)
272
- 61 # "a"
273
- a1 # map(1)
274
- 61 # string(1)
275
- 61 # "a"
276
- 01 # uint(1)
277
- 61 # string(1)
278
- 62 # "b"
279
- 02 # uint(2)
280
- 61 # string(1)
281
- 63 # "c"
282
- 80 # array(0)
283
- 61 # string(1)
284
- 65 # "e"
285
- 81 # array(1)
286
- 81 # array(1)
287
- 81 # array(1)
288
- 81 # array(1)
289
- a1 # map(1)
290
- 61 # string(1)
291
- 61 # "a"
292
- a0 # map(0)
293
- 61 # string(1)
294
- 66 # "f"
295
- 01 # uint(1)
296
- `);
297
- });
298
- describe('diag length bytes', () => {
299
- it('compact', async () => {
300
- const {stdout, stderr} = await execBin('json2diag', '"aaaaaaaaaaaaaaaaaaaaaaa"');
301
- assert.strictEqual(stderr, '');
302
- assert.strictEqual(stdout, `77 # string(23)
303
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
304
- `);
305
- });
306
- it('1-byte', async () => {
307
- const {stdout, stderr} = await execBin('json2diag', '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"');
308
- assert.strictEqual(stderr, '');
309
- assert.strictEqual(stdout, `78 23 # string(35)
310
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
311
- 616161616161616161616161 # "aaaaaaaaaaaa"
312
- `);
313
- });
314
- it('2-byte', async () => {
315
- const {stdout, stderr} = await execBin('json2diag', '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"');
316
- assert.strictEqual(stderr, '');
317
- assert.strictEqual(stdout, `79 0100 # string(256)
318
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
319
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
320
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
321
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
322
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
323
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
324
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
325
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
326
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
327
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
328
- 6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
329
- 616161 # "aaa"
330
- `);
331
- });
332
- });
333
- it('diag non-utf8 and non-printable ascii', async () => {
334
- const input = '7864f55ff8f12508b63ef2bfeca7557ae90df6311a5ec1631b4a1fa843310bd9c3a710eaace5a1bdd72ad0bfe049771c11e756338bd93865e645f1adec9b9c99ef407fbd4fc6859e7904c5ad7dc9bd10a5cc16973d5b28ec1a6dd43d9f82f9f18c3d03418e35';
335
- let {stdout, stderr} = await execBin(`hex2diag ${ input }`);
336
- assert.strictEqual(stderr, '');
337
- assert.strictEqual(stdout, `78 64 # string(86)
338
- f55ff8f12508b63ef2bfeca7557ae90df6311a5ec1631b # "õ_øñ%\\x08¶>ò¿ì§Uzé\\x0dö1\\x1a^Ác\\x1b"
339
- 4a1fa843310bd9c3a710eaace5a1bdd72ad0bfe049771c # "J\\x1f¨C1\\x0bÙç\\x10ê¬å¡½×*пàIw\\x1c"
340
- 11e756338bd93865e645f1adec9b9c99ef407fbd4fc685 # "\\x11çV3\\x8bÙ8eæEñ\\xadì\\x9b\\x9c\\x99ï@\\x7f½OÆ\\x85"
341
- 9e7904c5ad7dc9bd10a5cc16973d5b28ec1a6dd43d9f82 # "\\x9ey\\x04Å\\xad}ɽ\\x10¥Ì\\x16\\x97=[(ì\\x1amÔ=\\x9f\\x82"
342
- f9f18c3d03418e35 # "ùñ\\x8c=\\x03A\\x8e5"
343
- `);
344
- ({stdout, stderr} = await execBin('diag2hex', stdout));
345
- assert.strictEqual(stderr, '');
346
- assert.strictEqual(stdout, `${ input }\n`);
347
- });
348
- });
@@ -1,4 +0,0 @@
1
- 'use strict';
2
-
3
- require('../lib/bin.js');
4
-
@@ -1,158 +0,0 @@
1
- 'use strict';
2
-
3
- var chai = require('chai');
4
- require('../cborg.js');
5
- var byteUtils = require('../lib/byte-utils.js');
6
- var decode = require('../lib/decode.js');
7
- var encode = require('../lib/encode.js');
8
-
9
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
-
11
- var chai__default = /*#__PURE__*/_interopDefaultLegacy(chai);
12
-
13
- const {assert} = chai__default["default"];
14
- const fixtures = [
15
- {
16
- data: '00',
17
- expected: 0,
18
- type: 'uint8'
19
- },
20
- {
21
- data: '02',
22
- expected: 2,
23
- type: 'uint8'
24
- },
25
- {
26
- data: '18ff',
27
- expected: 255,
28
- type: 'uint8'
29
- },
30
- {
31
- data: '1901f4',
32
- expected: 500,
33
- type: 'uint16'
34
- },
35
- {
36
- data: '1900ff',
37
- expected: 255,
38
- type: 'uint16',
39
- strict: false
40
- },
41
- {
42
- data: '19ffff',
43
- expected: 65535,
44
- type: 'uint16'
45
- },
46
- {
47
- data: '1a000000ff',
48
- expected: 255,
49
- type: 'uint32',
50
- strict: false
51
- },
52
- {
53
- data: '1a00010000',
54
- expected: 65536,
55
- type: 'uint32'
56
- },
57
- {
58
- data: '1a000f4240',
59
- expected: 1000000,
60
- type: 'uint32'
61
- },
62
- {
63
- data: '1aa5f702b3',
64
- expected: 2784428723,
65
- type: 'uint32'
66
- },
67
- {
68
- data: '1b00000000000000ff',
69
- expected: 255,
70
- type: 'uint64',
71
- strict: false
72
- },
73
- {
74
- data: '1b0016db6db6db6db7',
75
- expected: Number.MAX_SAFE_INTEGER / 1.4,
76
- type: 'uint64'
77
- },
78
- {
79
- data: '1b001fffffffffffff',
80
- expected: Number.MAX_SAFE_INTEGER,
81
- type: 'uint64'
82
- },
83
- {
84
- data: '1ba5f702b3a5f702b3',
85
- expected: BigInt('11959030306112471731'),
86
- type: 'uint64'
87
- },
88
- {
89
- data: '1bffffffffffffffff',
90
- expected: BigInt('18446744073709551615'),
91
- type: 'uint64'
92
- }
93
- ];
94
- describe('uint', () => {
95
- describe('decode', () => {
96
- for (const fixture of fixtures) {
97
- const data = byteUtils.fromHex(fixture.data);
98
- it(`should decode ${ fixture.type }=${ fixture.expected }`, () => {
99
- assert.ok(decode.decode(data) === fixture.expected, `decode ${ fixture.type } ${ decode.decode(data) } != ${ fixture.expected }`);
100
- if (fixture.strict === false) {
101
- assert.throws(() => decode.decode(data, { strict: true }), Error, 'CBOR decode error: integer encoded in more bytes than necessary (strict decode)');
102
- } else {
103
- assert.ok(decode.decode(data, { strict: true }) === fixture.expected, `decode ${ fixture.type }`);
104
- }
105
- });
106
- }
107
- });
108
- it('should throw error', () => {
109
- assert.throws(() => decode.decode(byteUtils.fromHex('1ca5f702b3a5f702b3')), Error, 'CBOR decode error: encountered invalid minor (28) for major 0');
110
- assert.throws(() => decode.decode(byteUtils.fromHex('1ba5f702b3a5f702')), Error, 'CBOR decode error: not enough data for type');
111
- });
112
- describe('encode', () => {
113
- for (const fixture of fixtures) {
114
- it(`should encode ${ fixture.type }=${ fixture.expected }`, () => {
115
- if (fixture.strict === false) {
116
- assert.notStrictEqual(byteUtils.toHex(encode.encode(fixture.expected)), fixture.data, `encode ${ fixture.type } !strict`);
117
- } else {
118
- assert.strictEqual(byteUtils.toHex(encode.encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`);
119
- }
120
- });
121
- }
122
- });
123
- describe('roundtrip', () => {
124
- for (const fixture of fixtures) {
125
- if (fixture.strict !== false) {
126
- it(`should roundtrip ${ fixture.type }=${ fixture.expected }`, () => {
127
- assert.ok(decode.decode(encode.encode(fixture.expected)) === fixture.expected, `roundtrip ${ fixture.type }`);
128
- });
129
- }
130
- }
131
- });
132
- describe('toobig', () => {
133
- it('bigger than 64-bit', () => {
134
- assert.doesNotThrow(() => encode.encode(BigInt('18446744073709551615')));
135
- assert.throws(() => encode.encode(BigInt('18446744073709551616')), /BigInt larger than allowable range/);
136
- });
137
- it('disallow BigInt', () => {
138
- for (const fixture of fixtures) {
139
- const data = byteUtils.fromHex(fixture.data);
140
- if (!Number.isSafeInteger(fixture.expected)) {
141
- assert.throws(() => decode.decode(data, { allowBigInt: false }), /safe integer range/);
142
- } else {
143
- assert.ok(decode.decode(data, { allowBigInt: false }) === fixture.expected, `decode ${ fixture.type }`);
144
- }
145
- }
146
- });
147
- });
148
- describe('toosmall', () => {
149
- for (const fixture of fixtures) {
150
- if (fixture.strict !== false && typeof fixture.expected === 'number') {
151
- const small = BigInt(fixture.expected);
152
- it(`should encode ${ small }n`, () => {
153
- assert.strictEqual(byteUtils.toHex(encode.encode(BigInt(small))), fixture.data, `encode ${ small }`);
154
- });
155
- }
156
- }
157
- });
158
- });
@@ -1,152 +0,0 @@
1
- 'use strict';
2
-
3
- var chai = require('chai');
4
- require('../cborg.js');
5
- var byteUtils = require('../lib/byte-utils.js');
6
- var decode = require('../lib/decode.js');
7
- var encode = require('../lib/encode.js');
8
-
9
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
-
11
- var chai__default = /*#__PURE__*/_interopDefaultLegacy(chai);
12
-
13
- const {assert} = chai__default["default"];
14
- const fixtures = [
15
- {
16
- data: '20',
17
- expected: -1,
18
- type: 'negint8'
19
- },
20
- {
21
- data: '22',
22
- expected: -3,
23
- type: 'negint8'
24
- },
25
- {
26
- data: '3863',
27
- expected: -100,
28
- type: 'negint8'
29
- },
30
- {
31
- data: '38ff',
32
- expected: -256,
33
- type: 'negint8'
34
- },
35
- {
36
- data: '3900ff',
37
- expected: -256,
38
- type: 'negint16',
39
- strict: false
40
- },
41
- {
42
- data: '3901f4',
43
- expected: -501,
44
- type: 'negint16'
45
- },
46
- {
47
- data: '3a000000ff',
48
- expected: -256,
49
- type: 'negint32',
50
- strict: false
51
- },
52
- {
53
- data: '3aa5f702b3',
54
- expected: -2784428724,
55
- type: 'negint32'
56
- },
57
- {
58
- data: '3b00000000000000ff',
59
- expected: -256,
60
- type: 'negint32',
61
- strict: false
62
- },
63
- {
64
- data: '3b0016db6db6db6db7',
65
- expected: Number.MIN_SAFE_INTEGER / 1.4 - 1,
66
- type: 'negint64'
67
- },
68
- {
69
- data: '3b001ffffffffffffe',
70
- expected: Number.MIN_SAFE_INTEGER,
71
- type: 'negint64'
72
- },
73
- {
74
- data: '3b001fffffffffffff',
75
- expected: BigInt('-9007199254740992'),
76
- type: 'negint64'
77
- },
78
- {
79
- data: '3b0020000000000000',
80
- expected: BigInt('-9007199254740993'),
81
- type: 'negint64'
82
- },
83
- {
84
- data: '3ba5f702b3a5f702b3',
85
- expected: BigInt('-11959030306112471732'),
86
- type: 'negint64'
87
- },
88
- {
89
- data: '3bffffffffffffffff',
90
- expected: BigInt('-18446744073709551616'),
91
- type: 'negint64'
92
- }
93
- ];
94
- describe('negint', () => {
95
- describe('decode', () => {
96
- for (const fixture of fixtures) {
97
- const data = byteUtils.fromHex(fixture.data);
98
- it(`should decode ${ fixture.type }=${ fixture.expected }`, () => {
99
- assert.ok(decode.decode(data) === fixture.expected, `decode ${ fixture.type } (${ decode.decode(data) } != ${ fixture.expected })`);
100
- if (fixture.strict === false) {
101
- assert.throws(() => decode.decode(data, { strict: true }), Error, 'CBOR decode error: integer encoded in more bytes than necessary (strict decode)');
102
- } else {
103
- assert.strictEqual(decode.decode(data, { strict: true }), fixture.expected, `decode ${ fixture.type }`);
104
- }
105
- });
106
- }
107
- });
108
- describe('encode', () => {
109
- for (const fixture of fixtures) {
110
- it(`should encode ${ fixture.type }=${ fixture.expected }`, () => {
111
- if (fixture.strict === false) {
112
- assert.notStrictEqual(byteUtils.toHex(encode.encode(fixture.expected)), fixture.data, `encode ${ fixture.type } !strict`);
113
- } else {
114
- assert.strictEqual(byteUtils.toHex(encode.encode(fixture.expected)), fixture.data, `encode ${ fixture.type }`);
115
- }
116
- });
117
- }
118
- });
119
- describe('roundtrip', () => {
120
- for (const fixture of fixtures) {
121
- it(`should roundtrip ${ fixture.type }=${ fixture.expected }`, () => {
122
- assert.ok(decode.decode(encode.encode(fixture.expected)) === fixture.expected, `roundtrip ${ fixture.type }`);
123
- });
124
- }
125
- });
126
- describe('toobig', () => {
127
- it('bigger than 64-bit', () => {
128
- assert.doesNotThrow(() => encode.encode(BigInt('-18446744073709551616')));
129
- assert.throws(() => encode.encode(BigInt('-18446744073709551617')), /BigInt larger than allowable range/);
130
- });
131
- it('disallow BigInt', () => {
132
- for (const fixture of fixtures) {
133
- const data = byteUtils.fromHex(fixture.data);
134
- if (!Number.isSafeInteger(fixture.expected)) {
135
- assert.throws(() => decode.decode(data, { allowBigInt: false }), /safe integer range/);
136
- } else {
137
- assert.ok(decode.decode(data, { allowBigInt: false }) === fixture.expected, `decode ${ fixture.type }`);
138
- }
139
- }
140
- });
141
- });
142
- describe('toosmall', () => {
143
- for (const fixture of fixtures) {
144
- if (fixture.strict !== false && typeof fixture.expected === 'number') {
145
- const small = BigInt(fixture.expected);
146
- it(`should encode ${ small }n`, () => {
147
- assert.strictEqual(byteUtils.toHex(encode.encode(BigInt(small))), fixture.data, `encode ${ small }`);
148
- });
149
- }
150
- }
151
- });
152
- });