cborg 1.6.1 → 1.8.0
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.
- package/README.md +108 -11
- package/cjs/browser-test/node-test-bin.js +232 -105
- package/cjs/lib/3string.js +12 -8
- package/cjs/lib/bin.js +39 -23
- package/cjs/lib/diagnostic.js +57 -18
- package/cjs/lib/token.js +1 -0
- package/cjs/node-test/node-test-bin.js +232 -105
- package/esm/browser-test/node-test-bin.js +232 -105
- package/esm/lib/3string.js +16 -9
- package/esm/lib/bin.js +43 -24
- package/esm/lib/diagnostic.js +62 -20
- package/esm/lib/token.js +1 -0
- package/esm/node-test/node-test-bin.js +232 -105
- package/interface.ts +1 -0
- package/lib/3string.js +15 -10
- package/lib/bin.js +52 -27
- package/lib/diagnostic.js +74 -23
- package/lib/token.js +2 -0
- package/package.json +1 -1
- package/test/node-test-bin.js +271 -120
- package/types/interface.d.ts +1 -0
- package/types/interface.d.ts.map +1 -1
- package/types/lib/3string.d.ts +2 -2
- package/types/lib/3string.d.ts.map +1 -1
- package/types/lib/diagnostic.d.ts +6 -0
- package/types/lib/diagnostic.d.ts.map +1 -1
- package/types/lib/token.d.ts +2 -0
- package/types/lib/token.d.ts.map +1 -1
package/esm/lib/diagnostic.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { Tokeniser } from './decode.js';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
toHex,
|
|
4
|
+
fromHex
|
|
5
|
+
} from './byte-utils.js';
|
|
6
|
+
import { uintBoundaries } from './0uint.js';
|
|
7
|
+
const utf8Encoder = new TextEncoder();
|
|
8
|
+
const utf8Decoder = new TextDecoder();
|
|
3
9
|
function* tokensToDiagnostic(inp, width = 100) {
|
|
4
|
-
const tokeniser = new Tokeniser(inp);
|
|
10
|
+
const tokeniser = new Tokeniser(inp, { retainStringBytes: true });
|
|
5
11
|
let pos = 0;
|
|
6
12
|
const indent = [];
|
|
7
13
|
const slc = (start, length) => {
|
|
@@ -12,6 +18,7 @@ function* tokensToDiagnostic(inp, width = 100) {
|
|
|
12
18
|
let margin = ''.padStart(indent.length * 2, ' ');
|
|
13
19
|
let vLength = token.encodedLength - 1;
|
|
14
20
|
let v = String(token.value);
|
|
21
|
+
let outp = `${ margin }${ slc(0, 1) }`;
|
|
15
22
|
const str = token.type.name === 'bytes' || token.type.name === 'string';
|
|
16
23
|
if (token.type.name === 'string') {
|
|
17
24
|
v = v.length;
|
|
@@ -20,7 +27,29 @@ function* tokensToDiagnostic(inp, width = 100) {
|
|
|
20
27
|
v = token.value.length;
|
|
21
28
|
vLength -= v;
|
|
22
29
|
}
|
|
23
|
-
let
|
|
30
|
+
let multilen;
|
|
31
|
+
switch (token.type.name) {
|
|
32
|
+
case 'string':
|
|
33
|
+
case 'bytes':
|
|
34
|
+
case 'map':
|
|
35
|
+
case 'array':
|
|
36
|
+
multilen = token.type.name === 'string' ? utf8Encoder.encode(token.value).length : token.value.length;
|
|
37
|
+
if (multilen >= uintBoundaries[0]) {
|
|
38
|
+
if (multilen < uintBoundaries[1]) {
|
|
39
|
+
outp += ` ${ slc(1, 1) }`;
|
|
40
|
+
} else if (multilen < uintBoundaries[2]) {
|
|
41
|
+
outp += ` ${ slc(1, 2) }`;
|
|
42
|
+
} else if (multilen < uintBoundaries[3]) {
|
|
43
|
+
outp += ` ${ slc(1, 4) }`;
|
|
44
|
+
} else if (multilen < uintBoundaries[4]) {
|
|
45
|
+
outp += ` ${ slc(1, 8) }`;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
break;
|
|
49
|
+
default:
|
|
50
|
+
outp += ` ${ slc(1, vLength) }`;
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
24
53
|
outp = outp.padEnd(width / 2, ' ');
|
|
25
54
|
outp += `# ${ margin }${ token.type.name }`;
|
|
26
55
|
if (token.type.name !== v) {
|
|
@@ -28,15 +57,22 @@ function* tokensToDiagnostic(inp, width = 100) {
|
|
|
28
57
|
}
|
|
29
58
|
yield outp;
|
|
30
59
|
if (str) {
|
|
60
|
+
let asString = token.type.name === 'string';
|
|
31
61
|
margin += ' ';
|
|
32
|
-
|
|
62
|
+
let repr = asString ? utf8Encoder.encode(token.value) : token.value;
|
|
63
|
+
if (asString && token.byteValue !== undefined) {
|
|
64
|
+
if (repr.length !== token.byteValue.length) {
|
|
65
|
+
repr = token.byteValue;
|
|
66
|
+
asString = false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
33
69
|
const wh = (width / 2 - margin.length - 1) / 2;
|
|
34
70
|
let snip = 0;
|
|
35
71
|
while (repr.length - snip > 0) {
|
|
36
72
|
const piece = repr.slice(snip, snip + wh);
|
|
37
73
|
snip += piece.length;
|
|
38
|
-
const st =
|
|
39
|
-
if (c < 32 || c ===
|
|
74
|
+
const st = asString ? utf8Decoder.decode(piece) : piece.reduce((p, c) => {
|
|
75
|
+
if (c < 32 || c >= 127 && c < 161 || c === 173) {
|
|
40
76
|
return `${ p }\\x${ c.toString(16).padStart(2, '0') }`;
|
|
41
77
|
}
|
|
42
78
|
return `${ p }${ String.fromCharCode(c) }`;
|
|
@@ -44,17 +80,16 @@ function* tokensToDiagnostic(inp, width = 100) {
|
|
|
44
80
|
yield `${ margin }${ toHex(piece) }`.padEnd(width / 2, ' ') + `# ${ margin }"${ st }"`;
|
|
45
81
|
}
|
|
46
82
|
}
|
|
83
|
+
if (indent.length) {
|
|
84
|
+
indent[indent.length - 1]--;
|
|
85
|
+
}
|
|
47
86
|
if (!token.type.terminal) {
|
|
48
87
|
switch (token.type.name) {
|
|
49
88
|
case 'map':
|
|
50
|
-
|
|
51
|
-
indent.push(token.value * 2);
|
|
52
|
-
}
|
|
89
|
+
indent.push(token.value * 2);
|
|
53
90
|
break;
|
|
54
91
|
case 'array':
|
|
55
|
-
|
|
56
|
-
indent.push(token.value);
|
|
57
|
-
}
|
|
92
|
+
indent.push(token.value);
|
|
58
93
|
break;
|
|
59
94
|
case 'tag':
|
|
60
95
|
indent.push(1);
|
|
@@ -62,17 +97,24 @@ function* tokensToDiagnostic(inp, width = 100) {
|
|
|
62
97
|
default:
|
|
63
98
|
throw new Error(`Unknown token type '${ token.type.name }'`);
|
|
64
99
|
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (indent[indent.length - 1] === 0) {
|
|
69
|
-
indent.pop();
|
|
70
|
-
}
|
|
71
|
-
}
|
|
100
|
+
}
|
|
101
|
+
while (indent.length && indent[indent.length - 1] <= 0) {
|
|
102
|
+
indent.pop();
|
|
72
103
|
}
|
|
73
104
|
pos += token.encodedLength;
|
|
74
105
|
}
|
|
75
106
|
}
|
|
107
|
+
function fromDiag(input) {
|
|
108
|
+
if (typeof input !== 'string') {
|
|
109
|
+
throw new TypeError('Expected string input');
|
|
110
|
+
}
|
|
111
|
+
input = input.replace(/#.*?$/mg, '').replace(/[\s\r\n]+/mg, '');
|
|
112
|
+
if (/[^a-f0-9]/i.test(input)) {
|
|
113
|
+
throw new TypeError('Input string was not CBOR diagnostic format');
|
|
114
|
+
}
|
|
115
|
+
return fromHex(input);
|
|
116
|
+
}
|
|
76
117
|
export {
|
|
77
|
-
tokensToDiagnostic
|
|
118
|
+
tokensToDiagnostic,
|
|
119
|
+
fromDiag
|
|
78
120
|
};
|
package/esm/lib/token.js
CHANGED
|
@@ -2,9 +2,56 @@ import chai from 'chai';
|
|
|
2
2
|
import { exec } from 'child_process';
|
|
3
3
|
import process from 'process';
|
|
4
4
|
import path from 'path';
|
|
5
|
+
import { platform } from 'os';
|
|
5
6
|
import { fileURLToPath } from 'url';
|
|
6
7
|
import bin from '../lib/bin.js';
|
|
7
8
|
const {assert} = chai;
|
|
9
|
+
const fixture1JsonString = '{"a":1,"b":[2,3],"smile":"\uD83D\uDE00"}';
|
|
10
|
+
const fixture1JsonPrettyString = `{
|
|
11
|
+
"a": 1,
|
|
12
|
+
"b": [
|
|
13
|
+
2,
|
|
14
|
+
3
|
|
15
|
+
],
|
|
16
|
+
"smile": "😀"
|
|
17
|
+
}
|
|
18
|
+
`;
|
|
19
|
+
const fixture1HexString = 'a3616101616282020365736d696c6564f09f9880';
|
|
20
|
+
const fixture1Bin = fromHex(fixture1HexString);
|
|
21
|
+
const fixture1BinString = new TextDecoder().decode(fixture1Bin);
|
|
22
|
+
const fixture1DiagnosticString = `a3 # map(3)
|
|
23
|
+
61 # string(1)
|
|
24
|
+
61 # "a"
|
|
25
|
+
01 # uint(1)
|
|
26
|
+
61 # string(1)
|
|
27
|
+
62 # "b"
|
|
28
|
+
82 # array(2)
|
|
29
|
+
02 # uint(2)
|
|
30
|
+
03 # uint(3)
|
|
31
|
+
65 # string(5)
|
|
32
|
+
736d696c65 # "smile"
|
|
33
|
+
64 # string(2)
|
|
34
|
+
f09f9880 # "😀"
|
|
35
|
+
`;
|
|
36
|
+
const fixture2HexString = 'a4616101616282020363627566440102036165736d696c6564f09f9880';
|
|
37
|
+
const fixture2DiagnosticString = `a4 # map(4)
|
|
38
|
+
61 # string(1)
|
|
39
|
+
61 # "a"
|
|
40
|
+
01 # uint(1)
|
|
41
|
+
61 # string(1)
|
|
42
|
+
62 # "b"
|
|
43
|
+
82 # array(2)
|
|
44
|
+
02 # uint(2)
|
|
45
|
+
03 # uint(3)
|
|
46
|
+
63 # string(3)
|
|
47
|
+
627566 # "buf"
|
|
48
|
+
44 # bytes(4)
|
|
49
|
+
01020361 # "\\x01\\x02\\x03a"
|
|
50
|
+
65 # string(5)
|
|
51
|
+
736d696c65 # "smile"
|
|
52
|
+
64 # string(2)
|
|
53
|
+
f09f9880 # "😀"
|
|
54
|
+
`;
|
|
8
55
|
const binPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '../lib/bin.js');
|
|
9
56
|
function fromHex(hex) {
|
|
10
57
|
return new Uint8Array(hex.split('').map((c, i, d) => i % 2 === 0 ? `0x${ c }${ d[i + 1] }` : '').filter(Boolean).map(e => parseInt(e, 16)));
|
|
@@ -39,15 +86,18 @@ describe('Bin', () => {
|
|
|
39
86
|
assert.strictEqual(e.stdout, '');
|
|
40
87
|
assert.strictEqual(e.stderr, `Usage: cborg <command> <args>
|
|
41
88
|
Valid commands:
|
|
42
|
-
\thex2diag [hex input]
|
|
43
|
-
\thex2bin [hex input]
|
|
44
|
-
\thex2json [--pretty] [hex input]
|
|
45
|
-
\tbin2hex [binary input]
|
|
46
89
|
\tbin2diag [binary input]
|
|
90
|
+
\tbin2hex [binary input]
|
|
47
91
|
\tbin2json [--pretty] [binary input]
|
|
48
|
-
\
|
|
49
|
-
\
|
|
92
|
+
\tdiag2bin [diagnostic input]
|
|
93
|
+
\tdiag2hex [diagnostic input]
|
|
94
|
+
\tdiag2json [--pretty] [diagnostic input]
|
|
95
|
+
\thex2bin [hex input]
|
|
96
|
+
\thex2diag [hex input]
|
|
97
|
+
\thex2json [--pretty] [hex input]
|
|
50
98
|
\tjson2bin '[json input]'
|
|
99
|
+
\tjson2diag '[json input]'
|
|
100
|
+
\tjson2hex '[json input]'
|
|
51
101
|
Input may either be supplied as an argument or piped via stdin
|
|
52
102
|
`);
|
|
53
103
|
}
|
|
@@ -61,15 +111,18 @@ Input may either be supplied as an argument or piped via stdin
|
|
|
61
111
|
assert.strictEqual(e.stderr, `Unknown command: 'blip'
|
|
62
112
|
Usage: cborg <command> <args>
|
|
63
113
|
Valid commands:
|
|
64
|
-
\thex2diag [hex input]
|
|
65
|
-
\thex2bin [hex input]
|
|
66
|
-
\thex2json [--pretty] [hex input]
|
|
67
|
-
\tbin2hex [binary input]
|
|
68
114
|
\tbin2diag [binary input]
|
|
115
|
+
\tbin2hex [binary input]
|
|
69
116
|
\tbin2json [--pretty] [binary input]
|
|
70
|
-
\
|
|
71
|
-
\
|
|
117
|
+
\tdiag2bin [diagnostic input]
|
|
118
|
+
\tdiag2hex [diagnostic input]
|
|
119
|
+
\tdiag2json [--pretty] [diagnostic input]
|
|
120
|
+
\thex2bin [hex input]
|
|
121
|
+
\thex2diag [hex input]
|
|
122
|
+
\thex2json [--pretty] [hex input]
|
|
72
123
|
\tjson2bin '[json input]'
|
|
124
|
+
\tjson2diag '[json input]'
|
|
125
|
+
\tjson2hex '[json input]'
|
|
73
126
|
Input may either be supplied as an argument or piped via stdin
|
|
74
127
|
`);
|
|
75
128
|
}
|
|
@@ -79,135 +132,209 @@ Input may either be supplied as an argument or piped via stdin
|
|
|
79
132
|
assert.strictEqual(stdout, '');
|
|
80
133
|
assert.strictEqual(stderr, `Usage: cborg <command> <args>
|
|
81
134
|
Valid commands:
|
|
82
|
-
\thex2diag [hex input]
|
|
83
|
-
\thex2bin [hex input]
|
|
84
|
-
\thex2json [--pretty] [hex input]
|
|
85
|
-
\tbin2hex [binary input]
|
|
86
135
|
\tbin2diag [binary input]
|
|
136
|
+
\tbin2hex [binary input]
|
|
87
137
|
\tbin2json [--pretty] [binary input]
|
|
88
|
-
\
|
|
89
|
-
\
|
|
138
|
+
\tdiag2bin [diagnostic input]
|
|
139
|
+
\tdiag2hex [diagnostic input]
|
|
140
|
+
\tdiag2json [--pretty] [diagnostic input]
|
|
141
|
+
\thex2bin [hex input]
|
|
142
|
+
\thex2diag [hex input]
|
|
143
|
+
\thex2json [--pretty] [hex input]
|
|
90
144
|
\tjson2bin '[json input]'
|
|
145
|
+
\tjson2diag '[json input]'
|
|
146
|
+
\tjson2hex '[json input]'
|
|
91
147
|
Input may either be supplied as an argument or piped via stdin
|
|
92
148
|
`);
|
|
93
149
|
});
|
|
150
|
+
it('bin2diag (stdin)', async () => {
|
|
151
|
+
const {stdout, stderr} = await execBin('bin2diag', fixture1Bin);
|
|
152
|
+
assert.strictEqual(stderr, '');
|
|
153
|
+
assert.strictEqual(stdout, fixture1DiagnosticString);
|
|
154
|
+
});
|
|
155
|
+
it('bin2hex (stdin)', async () => {
|
|
156
|
+
const {stdout, stderr} = await execBin('bin2hex', fixture1Bin);
|
|
157
|
+
assert.strictEqual(stderr, '');
|
|
158
|
+
assert.strictEqual(stdout, `${ fixture1HexString }\n`);
|
|
159
|
+
});
|
|
160
|
+
it('bin2json (stdin)', async () => {
|
|
161
|
+
const {stdout, stderr} = await execBin('bin2json', fixture1Bin);
|
|
162
|
+
assert.strictEqual(stderr, '');
|
|
163
|
+
assert.strictEqual(stdout, `${ fixture1JsonString }\n`);
|
|
164
|
+
});
|
|
165
|
+
it('bin2json pretty (stdin)', async () => {
|
|
166
|
+
const {stdout, stderr} = await execBin('bin2json --pretty', fixture1Bin);
|
|
167
|
+
assert.strictEqual(stderr, '');
|
|
168
|
+
assert.strictEqual(stdout, fixture1JsonPrettyString);
|
|
169
|
+
});
|
|
94
170
|
for (const stdin of [
|
|
95
171
|
true,
|
|
96
172
|
false
|
|
97
173
|
]) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
174
|
+
if (platform() !== 'win32' || stdin) {
|
|
175
|
+
it(`diag2bin${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
176
|
+
const {stdout, stderr} = !stdin ? await execBin(`diag2bin '${ fixture1DiagnosticString }'`) : await execBin('diag2bin', fixture1DiagnosticString);
|
|
177
|
+
assert.strictEqual(stderr, '');
|
|
178
|
+
assert.strictEqual(stdout, fixture1BinString);
|
|
179
|
+
});
|
|
180
|
+
it(`diag2hex${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
181
|
+
const {stdout, stderr} = !stdin ? await execBin(`diag2hex '${ fixture1DiagnosticString }'`) : await execBin('diag2hex', fixture1DiagnosticString);
|
|
182
|
+
assert.strictEqual(stderr, '');
|
|
183
|
+
assert.strictEqual(stdout, `${ fixture1HexString }\n`);
|
|
184
|
+
});
|
|
185
|
+
it(`diag2json${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
186
|
+
const {stdout, stderr} = !stdin ? await execBin(`diag2json '${ fixture1DiagnosticString }'`) : await execBin('diag2json', fixture1DiagnosticString);
|
|
187
|
+
assert.strictEqual(stderr, '');
|
|
188
|
+
assert.strictEqual(stdout, `${ fixture1JsonString }\n`);
|
|
189
|
+
});
|
|
190
|
+
it(`diag2json pretty${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
191
|
+
const {stdout, stderr} = !stdin ? await execBin(`diag2json --pretty '${ fixture1DiagnosticString }'`) : await execBin('diag2json --pretty', fixture1DiagnosticString);
|
|
192
|
+
assert.strictEqual(stderr, '');
|
|
193
|
+
assert.strictEqual(stdout, fixture1JsonPrettyString);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
it(`hex2bin${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
197
|
+
const {stdout, stderr} = !stdin ? await execBin(`hex2bin ${ fixture1HexString }`) : await execBin('hex2bin', fixture1HexString);
|
|
105
198
|
assert.strictEqual(stderr, '');
|
|
106
|
-
assert.strictEqual(stdout,
|
|
107
|
-
"a": 1,
|
|
108
|
-
"b": [
|
|
109
|
-
2,
|
|
110
|
-
3
|
|
111
|
-
],
|
|
112
|
-
"smile": "😀"
|
|
113
|
-
}
|
|
114
|
-
`);
|
|
199
|
+
assert.strictEqual(stdout, fixture1BinString);
|
|
115
200
|
});
|
|
116
201
|
it(`hex2diag${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
117
|
-
const {stdout, stderr} = stdin ? await execBin(
|
|
202
|
+
const {stdout, stderr} = !stdin ? await execBin(`hex2diag ${ fixture2HexString }`) : await execBin('hex2diag', fixture2HexString);
|
|
118
203
|
assert.strictEqual(stderr, '');
|
|
119
|
-
assert.strictEqual(stdout,
|
|
120
|
-
61 # string(1)
|
|
121
|
-
61 # "a"
|
|
122
|
-
01 # uint(1)
|
|
123
|
-
61 # string(1)
|
|
124
|
-
62 # "b"
|
|
125
|
-
82 # array(2)
|
|
126
|
-
02 # uint(2)
|
|
127
|
-
03 # uint(3)
|
|
128
|
-
63 # string(3)
|
|
129
|
-
627566 # "buf"
|
|
130
|
-
44 # bytes(4)
|
|
131
|
-
01020361 # "\\x01\\x02\\x03a"
|
|
132
|
-
65 # string(5)
|
|
133
|
-
736d696c65 # "smile"
|
|
134
|
-
64 f09f # string(2)
|
|
135
|
-
f09f9880 # "😀"
|
|
136
|
-
`);
|
|
204
|
+
assert.strictEqual(stdout, fixture2DiagnosticString);
|
|
137
205
|
});
|
|
138
|
-
it(`
|
|
139
|
-
const {stdout, stderr} = stdin ? await execBin(
|
|
206
|
+
it(`hex2json${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
207
|
+
const {stdout, stderr} = !stdin ? await execBin(`hex2json ${ fixture1HexString }`) : await execBin('hex2json', fixture1HexString);
|
|
140
208
|
assert.strictEqual(stderr, '');
|
|
141
|
-
assert.strictEqual(stdout,
|
|
209
|
+
assert.strictEqual(stdout, `${ fixture1JsonString }\n`);
|
|
142
210
|
});
|
|
143
|
-
it(`
|
|
144
|
-
const {stdout, stderr} = stdin ? await execBin(
|
|
211
|
+
it(`hex2json pretty${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
212
|
+
const {stdout, stderr} = !stdin ? await execBin(`hex2json --pretty ${ fixture1HexString }`) : await execBin('hex2json --pretty', fixture1HexString);
|
|
145
213
|
assert.strictEqual(stderr, '');
|
|
146
|
-
assert.strictEqual(stdout,
|
|
214
|
+
assert.strictEqual(stdout, fixture1JsonPrettyString);
|
|
147
215
|
});
|
|
148
216
|
it(`json2bin${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
149
|
-
const {stdout, stderr} = stdin ? await execBin('json2bin "{\\"a\\":1,\\"b\\":[2,3],\\"smile\\":\\"\uD83D\uDE00\\"}"') : await execBin('json2bin',
|
|
217
|
+
const {stdout, stderr} = !stdin ? await execBin('json2bin "{\\"a\\":1,\\"b\\":[2,3],\\"smile\\":\\"\uD83D\uDE00\\"}"') : await execBin('json2bin', fixture1JsonString);
|
|
150
218
|
assert.strictEqual(stderr, '');
|
|
151
|
-
assert.strictEqual(stdout,
|
|
219
|
+
assert.strictEqual(stdout, fixture1BinString);
|
|
152
220
|
});
|
|
153
221
|
it(`json2diag${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
154
|
-
const {stdout, stderr} = stdin ? await execBin('json2diag "{\\"a\\":1,\\"b\\":[2,3],\\"smile\\":\\"\uD83D\uDE00\\"}"') : await execBin('json2diag',
|
|
222
|
+
const {stdout, stderr} = !stdin ? await execBin('json2diag "{\\"a\\":1,\\"b\\":[2,3],\\"smile\\":\\"\uD83D\uDE00\\"}"') : await execBin('json2diag', fixture1JsonString);
|
|
155
223
|
assert.strictEqual(stderr, '');
|
|
156
|
-
assert.strictEqual(stdout,
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
82 # array(2)
|
|
163
|
-
02 # uint(2)
|
|
164
|
-
03 # uint(3)
|
|
165
|
-
65 # string(5)
|
|
166
|
-
736d696c65 # "smile"
|
|
167
|
-
64 f09f # string(2)
|
|
168
|
-
f09f9880 # "😀"
|
|
169
|
-
`);
|
|
224
|
+
assert.strictEqual(stdout, fixture1DiagnosticString);
|
|
225
|
+
});
|
|
226
|
+
it(`json2hex${ stdin ? ' (stdin)' : '' }`, async () => {
|
|
227
|
+
const {stdout, stderr} = !stdin ? await execBin(`json2hex "${ fixture1JsonString.replace(/"/g, '\\"') }"`) : await execBin('json2hex', fixture1JsonString);
|
|
228
|
+
assert.strictEqual(stderr, '');
|
|
229
|
+
assert.strictEqual(stdout, `${ fixture1HexString }\n`);
|
|
170
230
|
});
|
|
171
231
|
}
|
|
172
|
-
it('
|
|
173
|
-
const {stdout, stderr} = await execBin('
|
|
232
|
+
it('diag indenting', async () => {
|
|
233
|
+
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}');
|
|
174
234
|
assert.strictEqual(stderr, '');
|
|
175
|
-
assert.strictEqual(stdout, `
|
|
235
|
+
assert.strictEqual(stdout, `a6 # map(6)
|
|
176
236
|
61 # string(1)
|
|
177
237
|
61 # "a"
|
|
178
|
-
|
|
238
|
+
80 # array(0)
|
|
179
239
|
61 # string(1)
|
|
180
240
|
62 # "b"
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
241
|
+
a0 # map(0)
|
|
242
|
+
61 # string(1)
|
|
243
|
+
63 # "c"
|
|
244
|
+
a2 # map(2)
|
|
245
|
+
61 # string(1)
|
|
246
|
+
61 # "a"
|
|
247
|
+
01 # uint(1)
|
|
248
|
+
61 # string(1)
|
|
249
|
+
62 # "b"
|
|
250
|
+
a1 # map(1)
|
|
251
|
+
61 # string(1)
|
|
252
|
+
61 # "a"
|
|
253
|
+
a1 # map(1)
|
|
254
|
+
61 # string(1)
|
|
255
|
+
61 # "a"
|
|
256
|
+
a0 # map(0)
|
|
257
|
+
61 # string(1)
|
|
258
|
+
64 # "d"
|
|
259
|
+
a1 # map(1)
|
|
260
|
+
61 # string(1)
|
|
261
|
+
61 # "a"
|
|
262
|
+
a3 # map(3)
|
|
263
|
+
61 # string(1)
|
|
264
|
+
61 # "a"
|
|
265
|
+
a1 # map(1)
|
|
266
|
+
61 # string(1)
|
|
267
|
+
61 # "a"
|
|
268
|
+
01 # uint(1)
|
|
269
|
+
61 # string(1)
|
|
270
|
+
62 # "b"
|
|
271
|
+
02 # uint(2)
|
|
272
|
+
61 # string(1)
|
|
273
|
+
63 # "c"
|
|
274
|
+
80 # array(0)
|
|
275
|
+
61 # string(1)
|
|
276
|
+
65 # "e"
|
|
277
|
+
81 # array(1)
|
|
278
|
+
81 # array(1)
|
|
279
|
+
81 # array(1)
|
|
280
|
+
81 # array(1)
|
|
281
|
+
a1 # map(1)
|
|
282
|
+
61 # string(1)
|
|
283
|
+
61 # "a"
|
|
284
|
+
a0 # map(0)
|
|
285
|
+
61 # string(1)
|
|
286
|
+
66 # "f"
|
|
287
|
+
01 # uint(1)
|
|
188
288
|
`);
|
|
189
289
|
});
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
290
|
+
describe('diag length bytes', () => {
|
|
291
|
+
it('compact', async () => {
|
|
292
|
+
const {stdout, stderr} = await execBin('json2diag', '"aaaaaaaaaaaaaaaaaaaaaaa"');
|
|
293
|
+
assert.strictEqual(stderr, '');
|
|
294
|
+
assert.strictEqual(stdout, `77 # string(23)
|
|
295
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
296
|
+
`);
|
|
297
|
+
});
|
|
298
|
+
it('1-byte', async () => {
|
|
299
|
+
const {stdout, stderr} = await execBin('json2diag', '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"');
|
|
300
|
+
assert.strictEqual(stderr, '');
|
|
301
|
+
assert.strictEqual(stdout, `78 23 # string(35)
|
|
302
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
303
|
+
616161616161616161616161 # "aaaaaaaaaaaa"
|
|
304
|
+
`);
|
|
305
|
+
});
|
|
306
|
+
it('2-byte', async () => {
|
|
307
|
+
const {stdout, stderr} = await execBin('json2diag', '"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"');
|
|
308
|
+
assert.strictEqual(stderr, '');
|
|
309
|
+
assert.strictEqual(stdout, `79 0100 # string(256)
|
|
310
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
311
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
312
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
313
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
314
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
315
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
316
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
317
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
318
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
319
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
320
|
+
6161616161616161616161616161616161616161616161 # "aaaaaaaaaaaaaaaaaaaaaaa"
|
|
321
|
+
616161 # "aaa"
|
|
322
|
+
`);
|
|
323
|
+
});
|
|
194
324
|
});
|
|
195
|
-
it('
|
|
196
|
-
const
|
|
325
|
+
it('diag non-utf8 and non-printable ascii', async () => {
|
|
326
|
+
const input = '7864f55ff8f12508b63ef2bfeca7557ae90df6311a5ec1631b4a1fa843310bd9c3a710eaace5a1bdd72ad0bfe049771c11e756338bd93865e645f1adec9b9c99ef407fbd4fc6859e7904c5ad7dc9bd10a5cc16973d5b28ec1a6dd43d9f82f9f18c3d03418e35';
|
|
327
|
+
let {stdout, stderr} = await execBin(`hex2diag ${ input }`);
|
|
197
328
|
assert.strictEqual(stderr, '');
|
|
198
|
-
assert.strictEqual(stdout, `
|
|
199
|
-
"
|
|
200
|
-
"
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
"smile": "😀"
|
|
205
|
-
}
|
|
329
|
+
assert.strictEqual(stdout, `78 64 # string(86)
|
|
330
|
+
f55ff8f12508b63ef2bfeca7557ae90df6311a5ec1631b # "õ_øñ%\\x08¶>ò¿ì§Uzé\\x0dö1\\x1a^Ác\\x1b"
|
|
331
|
+
4a1fa843310bd9c3a710eaace5a1bdd72ad0bfe049771c # "J\\x1f¨C1\\x0bÙç\\x10ê¬å¡½×*пàIw\\x1c"
|
|
332
|
+
11e756338bd93865e645f1adec9b9c99ef407fbd4fc685 # "\\x11çV3\\x8bÙ8eæEñ\\xadì\\x9b\\x9c\\x99ï@\\x7f½OÆ\\x85"
|
|
333
|
+
9e7904c5ad7dc9bd10a5cc16973d5b28ec1a6dd43d9f82 # "\\x9ey\\x04Å\\xad}ɽ\\x10¥Ì\\x16\\x97=[(ì\\x1amÔ=\\x9f\\x82"
|
|
334
|
+
f9f18c3d03418e35 # "ùñ\\x8c=\\x03A\\x8e5"
|
|
206
335
|
`);
|
|
207
|
-
|
|
208
|
-
it('bin2hex (stdin)', async () => {
|
|
209
|
-
const {stdout, stderr} = await execBin('bin2hex', fromHex('a3616101616282020365736d696c6564f09f9880'));
|
|
336
|
+
({stdout, stderr} = await execBin('diag2hex', stdout));
|
|
210
337
|
assert.strictEqual(stderr, '');
|
|
211
|
-
assert.strictEqual(stdout,
|
|
338
|
+
assert.strictEqual(stdout, `${ input }\n`);
|
|
212
339
|
});
|
|
213
340
|
});
|
package/interface.ts
CHANGED
package/lib/3string.js
CHANGED
|
@@ -2,7 +2,7 @@ import { Token, Type } from './token.js'
|
|
|
2
2
|
import { assertEnoughData, decodeErrPrefix } from './common.js'
|
|
3
3
|
import * as uint from './0uint.js'
|
|
4
4
|
import { encodeBytes } from './2bytes.js'
|
|
5
|
-
import { toString } from './byte-utils.js'
|
|
5
|
+
import { toString, slice } from './byte-utils.js'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @typedef {import('./bl.js').Bl} Bl
|
|
@@ -14,23 +14,28 @@ import { toString } from './byte-utils.js'
|
|
|
14
14
|
* @param {number} pos
|
|
15
15
|
* @param {number} prefix
|
|
16
16
|
* @param {number} length
|
|
17
|
+
* @param {DecodeOptions} options
|
|
17
18
|
* @returns {Token}
|
|
18
19
|
*/
|
|
19
|
-
function toToken (data, pos, prefix, length) {
|
|
20
|
+
function toToken (data, pos, prefix, length, options) {
|
|
20
21
|
const totLength = prefix + length
|
|
21
22
|
assertEnoughData(data, pos, totLength)
|
|
22
|
-
|
|
23
|
+
const tok = new Token(Type.string, toString(data, pos + prefix, pos + totLength), totLength)
|
|
24
|
+
if (options.retainStringBytes === true) {
|
|
25
|
+
tok.byteValue = slice(data, pos + prefix, pos + totLength)
|
|
26
|
+
}
|
|
27
|
+
return tok
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
/**
|
|
26
31
|
* @param {Uint8Array} data
|
|
27
32
|
* @param {number} pos
|
|
28
33
|
* @param {number} minor
|
|
29
|
-
* @param {DecodeOptions}
|
|
34
|
+
* @param {DecodeOptions} options
|
|
30
35
|
* @returns {Token}
|
|
31
36
|
*/
|
|
32
|
-
export function decodeStringCompact (data, pos, minor,
|
|
33
|
-
return toToken(data, pos, 1, minor)
|
|
37
|
+
export function decodeStringCompact (data, pos, minor, options) {
|
|
38
|
+
return toToken(data, pos, 1, minor, options)
|
|
34
39
|
}
|
|
35
40
|
|
|
36
41
|
/**
|
|
@@ -41,7 +46,7 @@ export function decodeStringCompact (data, pos, minor, _options) {
|
|
|
41
46
|
* @returns {Token}
|
|
42
47
|
*/
|
|
43
48
|
export function decodeString8 (data, pos, _minor, options) {
|
|
44
|
-
return toToken(data, pos, 2, uint.readUint8(data, pos + 1, options))
|
|
49
|
+
return toToken(data, pos, 2, uint.readUint8(data, pos + 1, options), options)
|
|
45
50
|
}
|
|
46
51
|
|
|
47
52
|
/**
|
|
@@ -52,7 +57,7 @@ export function decodeString8 (data, pos, _minor, options) {
|
|
|
52
57
|
* @returns {Token}
|
|
53
58
|
*/
|
|
54
59
|
export function decodeString16 (data, pos, _minor, options) {
|
|
55
|
-
return toToken(data, pos, 3, uint.readUint16(data, pos + 1, options))
|
|
60
|
+
return toToken(data, pos, 3, uint.readUint16(data, pos + 1, options), options)
|
|
56
61
|
}
|
|
57
62
|
|
|
58
63
|
/**
|
|
@@ -63,7 +68,7 @@ export function decodeString16 (data, pos, _minor, options) {
|
|
|
63
68
|
* @returns {Token}
|
|
64
69
|
*/
|
|
65
70
|
export function decodeString32 (data, pos, _minor, options) {
|
|
66
|
-
return toToken(data, pos, 5, uint.readUint32(data, pos + 1, options))
|
|
71
|
+
return toToken(data, pos, 5, uint.readUint32(data, pos + 1, options), options)
|
|
67
72
|
}
|
|
68
73
|
|
|
69
74
|
// TODO: maybe we shouldn't support this ..
|
|
@@ -79,7 +84,7 @@ export function decodeString64 (data, pos, _minor, options) {
|
|
|
79
84
|
if (typeof l === 'bigint') {
|
|
80
85
|
throw new Error(`${decodeErrPrefix} 64-bit integer string lengths not supported`)
|
|
81
86
|
}
|
|
82
|
-
return toToken(data, pos, 9, l)
|
|
87
|
+
return toToken(data, pos, 9, l, options)
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
export const encodeString = encodeBytes
|