cborg 1.4.2 → 1.5.3

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 (35) hide show
  1. package/cjs/browser-test/node-test-bin.js +133 -50
  2. package/cjs/browser-test/test-0uint.js +1 -1
  3. package/cjs/browser-test/test-1negint.js +1 -1
  4. package/cjs/browser-test/test-2bytes.js +1 -1
  5. package/cjs/browser-test/test-3string.js +1 -1
  6. package/cjs/browser-test/test-4array.js +1 -1
  7. package/cjs/browser-test/test-5map.js +1 -1
  8. package/cjs/browser-test/test-6tag.js +1 -1
  9. package/cjs/browser-test/test-7float.js +1 -1
  10. package/cjs/browser-test/test-bl.js +1 -1
  11. package/cjs/browser-test/test-cbor-vectors.js +1 -1
  12. package/cjs/browser-test/test-decode-errors.js +1 -1
  13. package/cjs/browser-test/test-fuzz.js +1 -1
  14. package/cjs/lib/bin.js +100 -37
  15. package/cjs/node-test/node-test-bin.js +133 -50
  16. package/cjs/node-test/test-0uint.js +1 -1
  17. package/cjs/node-test/test-1negint.js +1 -1
  18. package/cjs/node-test/test-2bytes.js +1 -1
  19. package/cjs/node-test/test-3string.js +1 -1
  20. package/cjs/node-test/test-4array.js +1 -1
  21. package/cjs/node-test/test-5map.js +1 -1
  22. package/cjs/node-test/test-6tag.js +1 -1
  23. package/cjs/node-test/test-7float.js +1 -1
  24. package/cjs/node-test/test-bl.js +1 -1
  25. package/cjs/node-test/test-cbor-vectors.js +1 -1
  26. package/cjs/node-test/test-decode-errors.js +1 -1
  27. package/cjs/node-test/test-fuzz.js +1 -1
  28. package/esm/browser-test/node-test-bin.js +131 -48
  29. package/esm/lib/bin.js +99 -36
  30. package/esm/node-test/node-test-bin.js +131 -48
  31. package/lib/bin.js +125 -40
  32. package/package.json +1 -1
  33. package/test/node-test-bin.js +155 -50
  34. package/types/lib/common.d.ts +1 -1
  35. package/types/lib/common.d.ts.map +1 -1
@@ -12,9 +12,16 @@ const { assert } = chai
12
12
 
13
13
  const binPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '../lib/bin.js')
14
14
 
15
- async function execBin (cmd) {
15
+ function fromHex (hex) {
16
+ return new Uint8Array(hex.split('')
17
+ .map((/** @type {string} */ c, /** @type {number} */ i, /** @type {string[]} */ d) => i % 2 === 0 ? `0x${c}${d[i + 1]}` : '')
18
+ .filter(Boolean)
19
+ .map((/** @type {string} */ e) => parseInt(e, 16)))
20
+ }
21
+
22
+ async function execBin (cmd, stdin) {
16
23
  return new Promise((resolve, reject) => {
17
- exec(`"${process.execPath}" "${binPath}" ${cmd}`, (err, stdout, stderr) => {
24
+ const cp = exec(`"${process.execPath}" "${binPath}" ${cmd}`, (err, stdout, stderr) => {
18
25
  if (err) {
19
26
  err.stdout = stdout
20
27
  err.stderr = stderr
@@ -22,6 +29,12 @@ async function execBin (cmd) {
22
29
  }
23
30
  resolve({ stdout, stderr })
24
31
  })
32
+ if (stdin != null) {
33
+ cp.on('spawn', () => {
34
+ cp.stdin.write(stdin)
35
+ cp.stdin.end()
36
+ })
37
+ }
25
38
  })
26
39
  }
27
40
 
@@ -35,9 +48,16 @@ describe('Bin', () => {
35
48
  assert.strictEqual(e.stderr,
36
49
  `Usage: cborg <command> <args>
37
50
  Valid commands:
38
- \thex2diag <hex input>
39
- \thex2json [--pretty] <hex input>
40
- \tjson2hex '<json input>'
51
+ \thex2diag [hex input]
52
+ \thex2bin [hex input]
53
+ \thex2json [--pretty] [hex input]
54
+ \tbin2hex [binary input]
55
+ \tbin2diag [binary input]
56
+ \tbin2json [--pretty] [binary input]
57
+ \tjson2hex '[json input]'
58
+ \tjson2diag '[json input]'
59
+ \tjson2bin '[json input]'
60
+ Input may either be supplied as an argument or piped via stdin
41
61
  `)
42
62
  }
43
63
  })
@@ -52,9 +72,16 @@ Valid commands:
52
72
  `Unknown command: 'blip'
53
73
  Usage: cborg <command> <args>
54
74
  Valid commands:
55
- \thex2diag <hex input>
56
- \thex2json [--pretty] <hex input>
57
- \tjson2hex '<json input>'
75
+ \thex2diag [hex input]
76
+ \thex2bin [hex input]
77
+ \thex2json [--pretty] [hex input]
78
+ \tbin2hex [binary input]
79
+ \tbin2diag [binary input]
80
+ \tbin2json [--pretty] [binary input]
81
+ \tjson2hex '[json input]'
82
+ \tjson2diag '[json input]'
83
+ \tjson2bin '[json input]'
84
+ Input may either be supplied as an argument or piped via stdin
58
85
  `)
59
86
  }
60
87
  })
@@ -65,30 +92,34 @@ Valid commands:
65
92
  assert.strictEqual(stderr,
66
93
  `Usage: cborg <command> <args>
67
94
  Valid commands:
68
- \thex2diag <hex input>
69
- \thex2json [--pretty] <hex input>
70
- \tjson2hex '<json input>'
95
+ \thex2diag [hex input]
96
+ \thex2bin [hex input]
97
+ \thex2json [--pretty] [hex input]
98
+ \tbin2hex [binary input]
99
+ \tbin2diag [binary input]
100
+ \tbin2json [--pretty] [binary input]
101
+ \tjson2hex '[json input]'
102
+ \tjson2diag '[json input]'
103
+ \tjson2bin '[json input]'
104
+ Input may either be supplied as an argument or piped via stdin
71
105
  `)
72
106
  })
73
107
 
74
- it('hex2json', async () => {
75
- const { stdout, stderr } = await execBin('hex2json a3616101616282020365736d696c6564f09f9880')
76
- assert.strictEqual(stderr, '')
77
- assert.strictEqual(stdout, '{"a":1,"b":[2,3],"smile":"😀"}\n')
78
-
79
- try {
80
- await execBin('hex2json')
81
- assert.fail('should have errored')
82
- } catch (e) {
83
- assert.strictEqual(e.stdout, '')
84
- assert.isTrue(e.stderr.startsWith('hex2json requires a hexadecimal input string\nUsage: '))
85
- }
86
- })
108
+ for (const stdin of [true, false]) {
109
+ it(`hex2json${stdin ? ' (stdin)' : ''}`, async () => {
110
+ const { stdout, stderr } = stdin
111
+ ? await execBin('hex2json a3616101616282020365736d696c6564f09f9880')
112
+ : await execBin('hex2json', 'a3616101616282020365736d696c6564f09f9880')
113
+ assert.strictEqual(stderr, '')
114
+ assert.strictEqual(stdout, '{"a":1,"b":[2,3],"smile":"😀"}\n')
115
+ })
87
116
 
88
- it('hex2json pretty', async () => {
89
- const { stdout, stderr } = await execBin('hex2json --pretty a3616101616282020365736d696c6564f09f9880')
90
- assert.strictEqual(stderr, '')
91
- assert.strictEqual(stdout,
117
+ it(`hex2json pretty${stdin ? ' (stdin)' : ''}`, async () => {
118
+ const { stdout, stderr } = stdin
119
+ ? await execBin('hex2json --pretty a3616101616282020365736d696c6564f09f9880')
120
+ : await execBin('hex2json --pretty', 'a3616101616282020365736d696c6564f09f9880')
121
+ assert.strictEqual(stderr, '')
122
+ assert.strictEqual(stdout,
92
123
  `{
93
124
  "a": 1,
94
125
  "b": [
@@ -98,12 +129,14 @@ Valid commands:
98
129
  "smile": "😀"
99
130
  }
100
131
  `)
101
- })
132
+ })
102
133
 
103
- it('hex2diag', async () => {
104
- const { stdout, stderr } = await execBin('hex2diag a4616101616282020363627566440102036165736d696c6564f09f9880')
105
- assert.strictEqual(stderr, '')
106
- assert.strictEqual(stdout,
134
+ it(`hex2diag${stdin ? ' (stdin)' : ''}`, async () => {
135
+ const { stdout, stderr } = stdin
136
+ ? await execBin('hex2diag a4616101616282020363627566440102036165736d696c6564f09f9880')
137
+ : await execBin('hex2diag', 'a4616101616282020363627566440102036165736d696c6564f09f9880')
138
+ assert.strictEqual(stderr, '')
139
+ assert.strictEqual(stdout,
107
140
  `a4 # map(4)
108
141
  61 # string(1)
109
142
  61 # "a"
@@ -122,27 +155,99 @@ Valid commands:
122
155
  64 f09f # string(2)
123
156
  f09f9880 # "😀"
124
157
  `)
158
+ })
125
159
 
126
- try {
127
- await execBin('hex2diag')
128
- assert.fail('should have errored')
129
- } catch (e) {
130
- assert.strictEqual(e.stdout, '')
131
- assert.isTrue(e.stderr.startsWith('hex2diag requires a hexadecimal input string\nUsage: '))
132
- }
160
+ it(`hex2bin${stdin ? ' (stdin)' : ''}`, async () => {
161
+ const { stdout, stderr } = stdin
162
+ ? await execBin('hex2bin a3616101616282020365736d696c6564f09f9880')
163
+ : await execBin('hex2bin', 'a3616101616282020365736d696c6564f09f9880')
164
+ assert.strictEqual(stderr, '')
165
+ assert.strictEqual(stdout, new TextDecoder().decode(fromHex('a3616101616282020365736d696c6564f09f9880')))
166
+ })
167
+
168
+ it(`json2hex${stdin ? ' (stdin)' : ''}`, async () => {
169
+ const { stdout, stderr } = stdin
170
+ ? await execBin('json2hex "{\\"a\\":1,\\"b\\":[2,3],\\"smile\\":\\"😀\\"}"')
171
+ : await execBin('json2hex', '{"a":1,"b":[2,3],"smile":"😀"}')
172
+ assert.strictEqual(stderr, '')
173
+ assert.strictEqual(stdout, 'a3616101616282020365736d696c6564f09f9880\n')
174
+ })
175
+
176
+ it(`json2bin${stdin ? ' (stdin)' : ''}`, async () => {
177
+ const { stdout, stderr } = stdin
178
+ ? await execBin('json2bin "{\\"a\\":1,\\"b\\":[2,3],\\"smile\\":\\"😀\\"}"')
179
+ : await execBin('json2bin', '{"a":1,"b":[2,3],"smile":"😀"}')
180
+ assert.strictEqual(stderr, '')
181
+ assert.strictEqual(stdout, new TextDecoder().decode(fromHex('a3616101616282020365736d696c6564f09f9880')))
182
+ })
183
+
184
+ it(`json2diag${stdin ? ' (stdin)' : ''}`, async () => {
185
+ const { stdout, stderr } = stdin
186
+ ? await execBin('json2diag "{\\"a\\":1,\\"b\\":[2,3],\\"smile\\":\\"😀\\"}"')
187
+ : await execBin('json2diag', '{"a":1,"b":[2,3],"smile":"😀"}')
188
+ assert.strictEqual(stderr, '')
189
+ assert.strictEqual(stdout,
190
+ `a3 # map(3)
191
+ 61 # string(1)
192
+ 61 # "a"
193
+ 01 # uint(1)
194
+ 61 # string(1)
195
+ 62 # "b"
196
+ 82 # array(2)
197
+ 02 # uint(2)
198
+ 03 # uint(3)
199
+ 65 # string(5)
200
+ 736d696c65 # "smile"
201
+ 64 f09f # string(2)
202
+ f09f9880 # "😀"
203
+ `)
204
+ })
205
+ }
206
+
207
+ it('bin2diag (stdin)', async () => {
208
+ const { stdout, stderr } = await execBin('bin2diag', fromHex('a3616101616282020365736d696c6564f09f9880'))
209
+ assert.strictEqual(stderr, '')
210
+ assert.strictEqual(stdout,
211
+ `a3 # map(3)
212
+ 61 # string(1)
213
+ 61 # "a"
214
+ 01 # uint(1)
215
+ 61 # string(1)
216
+ 62 # "b"
217
+ 82 # array(2)
218
+ 02 # uint(2)
219
+ 03 # uint(3)
220
+ 65 # string(5)
221
+ 736d696c65 # "smile"
222
+ 64 f09f # string(2)
223
+ f09f9880 # "😀"
224
+ `)
133
225
  })
134
226
 
135
- it('json2hex', async () => {
136
- const { stdout, stderr } = await execBin('json2hex "{\\"a\\":1,\\"b\\":[2,3],\\"smile\\":\\"😀\\"}"')
227
+ it('bin2json (stdin)', async () => {
228
+ const { stdout, stderr } = await execBin('bin2json', fromHex('a3616101616282020365736d696c6564f09f9880'))
137
229
  assert.strictEqual(stderr, '')
138
- assert.strictEqual(stdout, 'a3616101616282020365736d696c6564f09f9880\n')
230
+ assert.strictEqual(stdout, '{"a":1,"b":[2,3],"smile":"😀"}\n')
231
+ })
139
232
 
140
- try {
141
- await execBin('json2hex')
142
- assert.fail('should have errored')
143
- } catch (e) {
144
- assert.strictEqual(e.stdout, '')
145
- assert.isTrue(e.stderr.startsWith('json2hex requires a JSON input string\nUsage: '))
146
- }
233
+ it('bin2json pretty (stdin)', async () => {
234
+ const { stdout, stderr } = await execBin('bin2json --pretty', fromHex('a3616101616282020365736d696c6564f09f9880'))
235
+ assert.strictEqual(stderr, '')
236
+ assert.strictEqual(stdout,
237
+ `{
238
+ "a": 1,
239
+ "b": [
240
+ 2,
241
+ 3
242
+ ],
243
+ "smile": "😀"
244
+ }
245
+ `)
246
+ })
247
+
248
+ it('bin2hex (stdin)', async () => {
249
+ const { stdout, stderr } = await execBin('bin2hex', fromHex('a3616101616282020365736d696c6564f09f9880'))
250
+ assert.strictEqual(stderr, '')
251
+ assert.strictEqual(stdout, 'a3616101616282020365736d696c6564f09f9880\n')
147
252
  })
148
253
  })
@@ -1,6 +1,6 @@
1
1
  export const decodeErrPrefix: "CBOR decode error:";
2
2
  export const encodeErrPrefix: "CBOR encode error:";
3
- export let uintMinorPrefixBytes: any[];
3
+ export const uintMinorPrefixBytes: any[];
4
4
  /**
5
5
  * @param {Uint8Array} data
6
6
  * @param {number} pos
@@ -1 +1 @@
1
- {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../lib/common.js"],"names":[],"mappings":"AAAA,mDAA4C;AAC5C,mDAA4C;AAE5C,uCAA+B;AAO/B;;;;GAIG;AACH,uCAJW,UAAU,OACV,MAAM,QACN,MAAM,QAMhB"}
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../lib/common.js"],"names":[],"mappings":"AAAA,mDAA4C;AAC5C,mDAA4C;AAE5C,yCAA+B;AAO/B;;;;GAIG;AACH,uCAJW,UAAU,OACV,MAAM,QACN,MAAM,QAMhB"}