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/cjs/lib/bin.js
CHANGED
|
@@ -14,15 +14,18 @@ var process__default = /*#__PURE__*/_interopDefaultLegacy(process);
|
|
|
14
14
|
function usage(code) {
|
|
15
15
|
console.error('Usage: cborg <command> <args>');
|
|
16
16
|
console.error('Valid commands:');
|
|
17
|
-
console.error('\thex2diag [hex input]');
|
|
18
|
-
console.error('\thex2bin [hex input]');
|
|
19
|
-
console.error('\thex2json [--pretty] [hex input]');
|
|
20
|
-
console.error('\tbin2hex [binary input]');
|
|
21
17
|
console.error('\tbin2diag [binary input]');
|
|
18
|
+
console.error('\tbin2hex [binary input]');
|
|
22
19
|
console.error('\tbin2json [--pretty] [binary input]');
|
|
23
|
-
console.error('\
|
|
24
|
-
console.error('\
|
|
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]');
|
|
25
26
|
console.error('\tjson2bin \'[json input]\'');
|
|
27
|
+
console.error('\tjson2diag \'[json input]\'');
|
|
28
|
+
console.error('\tjson2hex \'[json input]\'');
|
|
26
29
|
console.error('Input may either be supplied as an argument or piped via stdin');
|
|
27
30
|
process__default["default"].exit(code || 0);
|
|
28
31
|
}
|
|
@@ -54,22 +57,13 @@ async function run() {
|
|
|
54
57
|
case 'help': {
|
|
55
58
|
return usage(0);
|
|
56
59
|
}
|
|
57
|
-
case '
|
|
58
|
-
const
|
|
59
|
-
const bin = fromHex(argv.length < 4 ? (await fromStdin()).toString() : argv[3]);
|
|
60
|
-
return console.log(JSON.stringify(decode.decode(bin), undefined, pretty ? 2 : undefined));
|
|
61
|
-
}
|
|
62
|
-
case 'hex2diag': {
|
|
63
|
-
const bin = fromHex(process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3]);
|
|
60
|
+
case 'bin2diag': {
|
|
61
|
+
const bin = process__default["default"].argv.length < 4 ? await fromStdin() : new TextEncoder().encode(process__default["default"].argv[3]);
|
|
64
62
|
for (const line of diagnostic.tokensToDiagnostic(bin)) {
|
|
65
63
|
console.log(line);
|
|
66
64
|
}
|
|
67
65
|
return;
|
|
68
66
|
}
|
|
69
|
-
case 'hex2bin': {
|
|
70
|
-
const bin = fromHex(process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3]);
|
|
71
|
-
return process__default["default"].stdout.write(bin);
|
|
72
|
-
}
|
|
73
67
|
case 'bin2hex': {
|
|
74
68
|
const bin = process__default["default"].argv.length < 4 ? await fromStdin() : new TextEncoder().encode(process__default["default"].argv[3]);
|
|
75
69
|
return console.log(byteUtils.toHex(bin));
|
|
@@ -79,17 +73,39 @@ async function run() {
|
|
|
79
73
|
const bin = argv.length < 4 ? await fromStdin() : new TextEncoder().encode(argv[3]);
|
|
80
74
|
return console.log(JSON.stringify(decode.decode(bin), undefined, pretty ? 2 : undefined));
|
|
81
75
|
}
|
|
82
|
-
case '
|
|
83
|
-
const bin = process__default["default"].argv.length < 4 ? await fromStdin() :
|
|
76
|
+
case 'diag2bin': {
|
|
77
|
+
const bin = diagnostic.fromDiag(process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3]);
|
|
78
|
+
return process__default["default"].stdout.write(bin);
|
|
79
|
+
}
|
|
80
|
+
case 'diag2hex': {
|
|
81
|
+
const bin = diagnostic.fromDiag(process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3]);
|
|
82
|
+
return console.log(byteUtils.toHex(bin));
|
|
83
|
+
}
|
|
84
|
+
case 'diag2json': {
|
|
85
|
+
const {argv, pretty} = argvPretty();
|
|
86
|
+
const bin = diagnostic.fromDiag(argv.length < 4 ? (await fromStdin()).toString() : argv[3]);
|
|
87
|
+
return console.log(JSON.stringify(decode.decode(bin), undefined, pretty ? 2 : undefined));
|
|
88
|
+
}
|
|
89
|
+
case 'hex2bin': {
|
|
90
|
+
const bin = fromHex(process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3]);
|
|
91
|
+
return process__default["default"].stdout.write(bin);
|
|
92
|
+
}
|
|
93
|
+
case 'hex2diag': {
|
|
94
|
+
const bin = fromHex(process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3]);
|
|
84
95
|
for (const line of diagnostic.tokensToDiagnostic(bin)) {
|
|
85
96
|
console.log(line);
|
|
86
97
|
}
|
|
87
98
|
return;
|
|
88
99
|
}
|
|
89
|
-
case '
|
|
100
|
+
case 'hex2json': {
|
|
101
|
+
const {argv, pretty} = argvPretty();
|
|
102
|
+
const bin = fromHex(argv.length < 4 ? (await fromStdin()).toString() : argv[3]);
|
|
103
|
+
return console.log(JSON.stringify(decode.decode(bin), undefined, pretty ? 2 : undefined));
|
|
104
|
+
}
|
|
105
|
+
case 'json2bin': {
|
|
90
106
|
const inp = process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3];
|
|
91
107
|
const obj = JSON.parse(inp);
|
|
92
|
-
return
|
|
108
|
+
return process__default["default"].stdout.write(encode.encode(obj));
|
|
93
109
|
}
|
|
94
110
|
case 'json2diag': {
|
|
95
111
|
const inp = process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3];
|
|
@@ -99,10 +115,10 @@ async function run() {
|
|
|
99
115
|
}
|
|
100
116
|
return;
|
|
101
117
|
}
|
|
102
|
-
case '
|
|
118
|
+
case 'json2hex': {
|
|
103
119
|
const inp = process__default["default"].argv.length < 4 ? (await fromStdin()).toString() : process__default["default"].argv[3];
|
|
104
120
|
const obj = JSON.parse(inp);
|
|
105
|
-
return
|
|
121
|
+
return console.log(byteUtils.toHex(encode.encode(obj)));
|
|
106
122
|
}
|
|
107
123
|
default: {
|
|
108
124
|
if (process__default["default"].argv.findIndex(a => a.endsWith('mocha')) === -1) {
|
package/cjs/lib/diagnostic.js
CHANGED
|
@@ -4,9 +4,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var decode = require('./decode.js');
|
|
6
6
|
var byteUtils = require('./byte-utils.js');
|
|
7
|
+
var _0uint = require('./0uint.js');
|
|
7
8
|
|
|
9
|
+
const utf8Encoder = new TextEncoder();
|
|
10
|
+
const utf8Decoder = new TextDecoder();
|
|
8
11
|
function* tokensToDiagnostic(inp, width = 100) {
|
|
9
|
-
const tokeniser = new decode.Tokeniser(inp);
|
|
12
|
+
const tokeniser = new decode.Tokeniser(inp, { retainStringBytes: true });
|
|
10
13
|
let pos = 0;
|
|
11
14
|
const indent = [];
|
|
12
15
|
const slc = (start, length) => {
|
|
@@ -17,6 +20,7 @@ function* tokensToDiagnostic(inp, width = 100) {
|
|
|
17
20
|
let margin = ''.padStart(indent.length * 2, ' ');
|
|
18
21
|
let vLength = token.encodedLength - 1;
|
|
19
22
|
let v = String(token.value);
|
|
23
|
+
let outp = `${ margin }${ slc(0, 1) }`;
|
|
20
24
|
const str = token.type.name === 'bytes' || token.type.name === 'string';
|
|
21
25
|
if (token.type.name === 'string') {
|
|
22
26
|
v = v.length;
|
|
@@ -25,7 +29,29 @@ function* tokensToDiagnostic(inp, width = 100) {
|
|
|
25
29
|
v = token.value.length;
|
|
26
30
|
vLength -= v;
|
|
27
31
|
}
|
|
28
|
-
let
|
|
32
|
+
let multilen;
|
|
33
|
+
switch (token.type.name) {
|
|
34
|
+
case 'string':
|
|
35
|
+
case 'bytes':
|
|
36
|
+
case 'map':
|
|
37
|
+
case 'array':
|
|
38
|
+
multilen = token.type.name === 'string' ? utf8Encoder.encode(token.value).length : token.value.length;
|
|
39
|
+
if (multilen >= _0uint.uintBoundaries[0]) {
|
|
40
|
+
if (multilen < _0uint.uintBoundaries[1]) {
|
|
41
|
+
outp += ` ${ slc(1, 1) }`;
|
|
42
|
+
} else if (multilen < _0uint.uintBoundaries[2]) {
|
|
43
|
+
outp += ` ${ slc(1, 2) }`;
|
|
44
|
+
} else if (multilen < _0uint.uintBoundaries[3]) {
|
|
45
|
+
outp += ` ${ slc(1, 4) }`;
|
|
46
|
+
} else if (multilen < _0uint.uintBoundaries[4]) {
|
|
47
|
+
outp += ` ${ slc(1, 8) }`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
break;
|
|
51
|
+
default:
|
|
52
|
+
outp += ` ${ slc(1, vLength) }`;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
29
55
|
outp = outp.padEnd(width / 2, ' ');
|
|
30
56
|
outp += `# ${ margin }${ token.type.name }`;
|
|
31
57
|
if (token.type.name !== v) {
|
|
@@ -33,15 +59,22 @@ function* tokensToDiagnostic(inp, width = 100) {
|
|
|
33
59
|
}
|
|
34
60
|
yield outp;
|
|
35
61
|
if (str) {
|
|
62
|
+
let asString = token.type.name === 'string';
|
|
36
63
|
margin += ' ';
|
|
37
|
-
|
|
64
|
+
let repr = asString ? utf8Encoder.encode(token.value) : token.value;
|
|
65
|
+
if (asString && token.byteValue !== undefined) {
|
|
66
|
+
if (repr.length !== token.byteValue.length) {
|
|
67
|
+
repr = token.byteValue;
|
|
68
|
+
asString = false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
38
71
|
const wh = (width / 2 - margin.length - 1) / 2;
|
|
39
72
|
let snip = 0;
|
|
40
73
|
while (repr.length - snip > 0) {
|
|
41
74
|
const piece = repr.slice(snip, snip + wh);
|
|
42
75
|
snip += piece.length;
|
|
43
|
-
const st =
|
|
44
|
-
if (c < 32 || c ===
|
|
76
|
+
const st = asString ? utf8Decoder.decode(piece) : piece.reduce((p, c) => {
|
|
77
|
+
if (c < 32 || c >= 127 && c < 161 || c === 173) {
|
|
45
78
|
return `${ p }\\x${ c.toString(16).padStart(2, '0') }`;
|
|
46
79
|
}
|
|
47
80
|
return `${ p }${ String.fromCharCode(c) }`;
|
|
@@ -49,17 +82,16 @@ function* tokensToDiagnostic(inp, width = 100) {
|
|
|
49
82
|
yield `${ margin }${ byteUtils.toHex(piece) }`.padEnd(width / 2, ' ') + `# ${ margin }"${ st }"`;
|
|
50
83
|
}
|
|
51
84
|
}
|
|
85
|
+
if (indent.length) {
|
|
86
|
+
indent[indent.length - 1]--;
|
|
87
|
+
}
|
|
52
88
|
if (!token.type.terminal) {
|
|
53
89
|
switch (token.type.name) {
|
|
54
90
|
case 'map':
|
|
55
|
-
|
|
56
|
-
indent.push(token.value * 2);
|
|
57
|
-
}
|
|
91
|
+
indent.push(token.value * 2);
|
|
58
92
|
break;
|
|
59
93
|
case 'array':
|
|
60
|
-
|
|
61
|
-
indent.push(token.value);
|
|
62
|
-
}
|
|
94
|
+
indent.push(token.value);
|
|
63
95
|
break;
|
|
64
96
|
case 'tag':
|
|
65
97
|
indent.push(1);
|
|
@@ -67,16 +99,23 @@ function* tokensToDiagnostic(inp, width = 100) {
|
|
|
67
99
|
default:
|
|
68
100
|
throw new Error(`Unknown token type '${ token.type.name }'`);
|
|
69
101
|
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
if (indent[indent.length - 1] === 0) {
|
|
74
|
-
indent.pop();
|
|
75
|
-
}
|
|
76
|
-
}
|
|
102
|
+
}
|
|
103
|
+
while (indent.length && indent[indent.length - 1] <= 0) {
|
|
104
|
+
indent.pop();
|
|
77
105
|
}
|
|
78
106
|
pos += token.encodedLength;
|
|
79
107
|
}
|
|
80
108
|
}
|
|
109
|
+
function fromDiag(input) {
|
|
110
|
+
if (typeof input !== 'string') {
|
|
111
|
+
throw new TypeError('Expected string input');
|
|
112
|
+
}
|
|
113
|
+
input = input.replace(/#.*?$/mg, '').replace(/[\s\r\n]+/mg, '');
|
|
114
|
+
if (/[^a-f0-9]/i.test(input)) {
|
|
115
|
+
throw new TypeError('Input string was not CBOR diagnostic format');
|
|
116
|
+
}
|
|
117
|
+
return byteUtils.fromHex(input);
|
|
118
|
+
}
|
|
81
119
|
|
|
120
|
+
exports.fromDiag = fromDiag;
|
|
82
121
|
exports.tokensToDiagnostic = tokensToDiagnostic;
|