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
@@ -1,228 +0,0 @@
1
- export const useBuffer = globalThis.process && !globalThis.process.browser && globalThis.Buffer && typeof globalThis.Buffer.isBuffer === 'function';
2
- const textDecoder = new TextDecoder();
3
- const textEncoder = new TextEncoder();
4
- function isBuffer(buf) {
5
- return useBuffer && globalThis.Buffer.isBuffer(buf);
6
- }
7
- export function asU8A(buf) {
8
- if (!(buf instanceof Uint8Array)) {
9
- return Uint8Array.from(buf);
10
- }
11
- return isBuffer(buf) ? new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) : buf;
12
- }
13
- export const toString = useBuffer ? (bytes, start, end) => {
14
- return end - start > 64 ? globalThis.Buffer.from(bytes.subarray(start, end)).toString('utf8') : utf8Slice(bytes, start, end);
15
- } : (bytes, start, end) => {
16
- return end - start > 64 ? textDecoder.decode(bytes.subarray(start, end)) : utf8Slice(bytes, start, end);
17
- };
18
- export const fromString = useBuffer ? string => {
19
- return string.length > 64 ? globalThis.Buffer.from(string) : utf8ToBytes(string);
20
- } : string => {
21
- return string.length > 64 ? textEncoder.encode(string) : utf8ToBytes(string);
22
- };
23
- export const fromArray = arr => {
24
- return Uint8Array.from(arr);
25
- };
26
- export const slice = useBuffer ? (bytes, start, end) => {
27
- if (isBuffer(bytes)) {
28
- return new Uint8Array(bytes.subarray(start, end));
29
- }
30
- return bytes.slice(start, end);
31
- } : (bytes, start, end) => {
32
- return bytes.slice(start, end);
33
- };
34
- export const concat = useBuffer ? (chunks, length) => {
35
- chunks = chunks.map(c => c instanceof Uint8Array ? c : globalThis.Buffer.from(c));
36
- return asU8A(globalThis.Buffer.concat(chunks, length));
37
- } : (chunks, length) => {
38
- const out = new Uint8Array(length);
39
- let off = 0;
40
- for (let b of chunks) {
41
- if (off + b.length > out.length) {
42
- b = b.subarray(0, out.length - off);
43
- }
44
- out.set(b, off);
45
- off += b.length;
46
- }
47
- return out;
48
- };
49
- export const alloc = useBuffer ? size => {
50
- return globalThis.Buffer.allocUnsafe(size);
51
- } : size => {
52
- return new Uint8Array(size);
53
- };
54
- export const toHex = useBuffer ? d => {
55
- if (typeof d === 'string') {
56
- return d;
57
- }
58
- return globalThis.Buffer.from(toBytes(d)).toString('hex');
59
- } : d => {
60
- if (typeof d === 'string') {
61
- return d;
62
- }
63
- return Array.prototype.reduce.call(toBytes(d), (p, c) => `${ p }${ c.toString(16).padStart(2, '0') }`, '');
64
- };
65
- export const fromHex = useBuffer ? hex => {
66
- if (hex instanceof Uint8Array) {
67
- return hex;
68
- }
69
- return globalThis.Buffer.from(hex, 'hex');
70
- } : hex => {
71
- if (hex instanceof Uint8Array) {
72
- return hex;
73
- }
74
- if (!hex.length) {
75
- return new Uint8Array(0);
76
- }
77
- return new Uint8Array(hex.split('').map((c, i, d) => i % 2 === 0 ? `0x${ c }${ d[i + 1] }` : '').filter(Boolean).map(e => parseInt(e, 16)));
78
- };
79
- function toBytes(obj) {
80
- if (obj instanceof Uint8Array && obj.constructor.name === 'Uint8Array') {
81
- return obj;
82
- }
83
- if (obj instanceof ArrayBuffer) {
84
- return new Uint8Array(obj);
85
- }
86
- if (ArrayBuffer.isView(obj)) {
87
- return new Uint8Array(obj.buffer, obj.byteOffset, obj.byteLength);
88
- }
89
- throw new Error('Unknown type, must be binary type');
90
- }
91
- export function compare(b1, b2) {
92
- if (isBuffer(b1) && isBuffer(b2)) {
93
- return b1.compare(b2);
94
- }
95
- for (let i = 0; i < b1.length; i++) {
96
- if (b1[i] === b2[i]) {
97
- continue;
98
- }
99
- return b1[i] < b2[i] ? -1 : 1;
100
- }
101
- return 0;
102
- }
103
- function utf8ToBytes(string, units = Infinity) {
104
- let codePoint;
105
- const length = string.length;
106
- let leadSurrogate = null;
107
- const bytes = [];
108
- for (let i = 0; i < length; ++i) {
109
- codePoint = string.charCodeAt(i);
110
- if (codePoint > 55295 && codePoint < 57344) {
111
- if (!leadSurrogate) {
112
- if (codePoint > 56319) {
113
- if ((units -= 3) > -1)
114
- bytes.push(239, 191, 189);
115
- continue;
116
- } else if (i + 1 === length) {
117
- if ((units -= 3) > -1)
118
- bytes.push(239, 191, 189);
119
- continue;
120
- }
121
- leadSurrogate = codePoint;
122
- continue;
123
- }
124
- if (codePoint < 56320) {
125
- if ((units -= 3) > -1)
126
- bytes.push(239, 191, 189);
127
- leadSurrogate = codePoint;
128
- continue;
129
- }
130
- codePoint = (leadSurrogate - 55296 << 10 | codePoint - 56320) + 65536;
131
- } else if (leadSurrogate) {
132
- if ((units -= 3) > -1)
133
- bytes.push(239, 191, 189);
134
- }
135
- leadSurrogate = null;
136
- if (codePoint < 128) {
137
- if ((units -= 1) < 0)
138
- break;
139
- bytes.push(codePoint);
140
- } else if (codePoint < 2048) {
141
- if ((units -= 2) < 0)
142
- break;
143
- bytes.push(codePoint >> 6 | 192, codePoint & 63 | 128);
144
- } else if (codePoint < 65536) {
145
- if ((units -= 3) < 0)
146
- break;
147
- bytes.push(codePoint >> 12 | 224, codePoint >> 6 & 63 | 128, codePoint & 63 | 128);
148
- } else if (codePoint < 1114112) {
149
- if ((units -= 4) < 0)
150
- break;
151
- bytes.push(codePoint >> 18 | 240, codePoint >> 12 & 63 | 128, codePoint >> 6 & 63 | 128, codePoint & 63 | 128);
152
- } else {
153
- throw new Error('Invalid code point');
154
- }
155
- }
156
- return bytes;
157
- }
158
- function utf8Slice(buf, offset, end) {
159
- const res = [];
160
- while (offset < end) {
161
- const firstByte = buf[offset];
162
- let codePoint = null;
163
- let bytesPerSequence = firstByte > 239 ? 4 : firstByte > 223 ? 3 : firstByte > 191 ? 2 : 1;
164
- if (offset + bytesPerSequence <= end) {
165
- let secondByte, thirdByte, fourthByte, tempCodePoint;
166
- switch (bytesPerSequence) {
167
- case 1:
168
- if (firstByte < 128) {
169
- codePoint = firstByte;
170
- }
171
- break;
172
- case 2:
173
- secondByte = buf[offset + 1];
174
- if ((secondByte & 192) === 128) {
175
- tempCodePoint = (firstByte & 31) << 6 | secondByte & 63;
176
- if (tempCodePoint > 127) {
177
- codePoint = tempCodePoint;
178
- }
179
- }
180
- break;
181
- case 3:
182
- secondByte = buf[offset + 1];
183
- thirdByte = buf[offset + 2];
184
- if ((secondByte & 192) === 128 && (thirdByte & 192) === 128) {
185
- tempCodePoint = (firstByte & 15) << 12 | (secondByte & 63) << 6 | thirdByte & 63;
186
- if (tempCodePoint > 2047 && (tempCodePoint < 55296 || tempCodePoint > 57343)) {
187
- codePoint = tempCodePoint;
188
- }
189
- }
190
- break;
191
- case 4:
192
- secondByte = buf[offset + 1];
193
- thirdByte = buf[offset + 2];
194
- fourthByte = buf[offset + 3];
195
- if ((secondByte & 192) === 128 && (thirdByte & 192) === 128 && (fourthByte & 192) === 128) {
196
- tempCodePoint = (firstByte & 15) << 18 | (secondByte & 63) << 12 | (thirdByte & 63) << 6 | fourthByte & 63;
197
- if (tempCodePoint > 65535 && tempCodePoint < 1114112) {
198
- codePoint = tempCodePoint;
199
- }
200
- }
201
- }
202
- }
203
- if (codePoint === null) {
204
- codePoint = 65533;
205
- bytesPerSequence = 1;
206
- } else if (codePoint > 65535) {
207
- codePoint -= 65536;
208
- res.push(codePoint >>> 10 & 1023 | 55296);
209
- codePoint = 56320 | codePoint & 1023;
210
- }
211
- res.push(codePoint);
212
- offset += bytesPerSequence;
213
- }
214
- return decodeCodePointsArray(res);
215
- }
216
- const MAX_ARGUMENTS_LENGTH = 4096;
217
- export function decodeCodePointsArray(codePoints) {
218
- const len = codePoints.length;
219
- if (len <= MAX_ARGUMENTS_LENGTH) {
220
- return String.fromCharCode.apply(String, codePoints);
221
- }
222
- let res = '';
223
- let i = 0;
224
- while (i < len) {
225
- res += String.fromCharCode.apply(String, codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH));
226
- }
227
- return res;
228
- }
package/esm/lib/common.js DELETED
@@ -1,19 +0,0 @@
1
- const decodeErrPrefix = 'CBOR decode error:';
2
- const encodeErrPrefix = 'CBOR encode error:';
3
- const uintMinorPrefixBytes = [];
4
- uintMinorPrefixBytes[23] = 1;
5
- uintMinorPrefixBytes[24] = 2;
6
- uintMinorPrefixBytes[25] = 3;
7
- uintMinorPrefixBytes[26] = 5;
8
- uintMinorPrefixBytes[27] = 9;
9
- function assertEnoughData(data, pos, need) {
10
- if (data.length - pos < need) {
11
- throw new Error(`${ decodeErrPrefix } not enough data for type`);
12
- }
13
- }
14
- export {
15
- decodeErrPrefix,
16
- encodeErrPrefix,
17
- uintMinorPrefixBytes,
18
- assertEnoughData
19
- };
package/esm/lib/decode.js DELETED
@@ -1,139 +0,0 @@
1
- import { decodeErrPrefix } from './common.js';
2
- import { Type } from './token.js';
3
- import {
4
- jump,
5
- quick
6
- } from './jump.js';
7
- const defaultDecodeOptions = {
8
- strict: false,
9
- allowIndefinite: true,
10
- allowUndefined: true,
11
- allowBigInt: true
12
- };
13
- class Tokeniser {
14
- constructor(data, options = {}) {
15
- this.pos = 0;
16
- this.data = data;
17
- this.options = options;
18
- }
19
- done() {
20
- return this.pos >= this.data.length;
21
- }
22
- next() {
23
- const byt = this.data[this.pos];
24
- let token = quick[byt];
25
- if (token === undefined) {
26
- const decoder = jump[byt];
27
- if (!decoder) {
28
- throw new Error(`${ decodeErrPrefix } no decoder for major type ${ byt >>> 5 } (byte 0x${ byt.toString(16).padStart(2, '0') })`);
29
- }
30
- const minor = byt & 31;
31
- token = decoder(this.data, this.pos, minor, this.options);
32
- }
33
- this.pos += token.encodedLength;
34
- return token;
35
- }
36
- }
37
- const DONE = Symbol.for('DONE');
38
- const BREAK = Symbol.for('BREAK');
39
- function tokenToArray(token, tokeniser, options) {
40
- const arr = [];
41
- for (let i = 0; i < token.value; i++) {
42
- const value = tokensToObject(tokeniser, options);
43
- if (value === BREAK) {
44
- if (token.value === Infinity) {
45
- break;
46
- }
47
- throw new Error(`${ decodeErrPrefix } got unexpected break to lengthed array`);
48
- }
49
- if (value === DONE) {
50
- throw new Error(`${ decodeErrPrefix } found array but not enough entries (got ${ i }, expected ${ token.value })`);
51
- }
52
- arr[i] = value;
53
- }
54
- return arr;
55
- }
56
- function tokenToMap(token, tokeniser, options) {
57
- const useMaps = options.useMaps === true;
58
- const obj = useMaps ? undefined : {};
59
- const m = useMaps ? new Map() : undefined;
60
- for (let i = 0; i < token.value; i++) {
61
- const key = tokensToObject(tokeniser, options);
62
- if (key === BREAK) {
63
- if (token.value === Infinity) {
64
- break;
65
- }
66
- throw new Error(`${ decodeErrPrefix } got unexpected break to lengthed map`);
67
- }
68
- if (key === DONE) {
69
- throw new Error(`${ decodeErrPrefix } found map but not enough entries (got ${ i } [no key], expected ${ token.value })`);
70
- }
71
- if (useMaps !== true && typeof key !== 'string') {
72
- throw new Error(`${ decodeErrPrefix } non-string keys not supported (got ${ typeof key })`);
73
- }
74
- if (options.rejectDuplicateMapKeys === true) {
75
- if (useMaps && m.has(key) || !useMaps && key in obj) {
76
- throw new Error(`${ decodeErrPrefix } found repeat map key "${ key }"`);
77
- }
78
- }
79
- const value = tokensToObject(tokeniser, options);
80
- if (value === DONE) {
81
- throw new Error(`${ decodeErrPrefix } found map but not enough entries (got ${ i } [no value], expected ${ token.value })`);
82
- }
83
- if (useMaps) {
84
- m.set(key, value);
85
- } else {
86
- obj[key] = value;
87
- }
88
- }
89
- return useMaps ? m : obj;
90
- }
91
- function tokensToObject(tokeniser, options) {
92
- if (tokeniser.done()) {
93
- return DONE;
94
- }
95
- const token = tokeniser.next();
96
- if (token.type === Type.break) {
97
- return BREAK;
98
- }
99
- if (token.type.terminal) {
100
- return token.value;
101
- }
102
- if (token.type === Type.array) {
103
- return tokenToArray(token, tokeniser, options);
104
- }
105
- if (token.type === Type.map) {
106
- return tokenToMap(token, tokeniser, options);
107
- }
108
- if (token.type === Type.tag) {
109
- if (options.tags && typeof options.tags[token.value] === 'function') {
110
- const tagged = tokensToObject(tokeniser, options);
111
- return options.tags[token.value](tagged);
112
- }
113
- throw new Error(`${ decodeErrPrefix } tag not supported (${ token.value })`);
114
- }
115
- throw new Error('unsupported');
116
- }
117
- function decode(data, options) {
118
- if (!(data instanceof Uint8Array)) {
119
- throw new Error(`${ decodeErrPrefix } data to decode must be a Uint8Array`);
120
- }
121
- options = Object.assign({}, defaultDecodeOptions, options);
122
- const tokeniser = options.tokenizer || new Tokeniser(data, options);
123
- const decoded = tokensToObject(tokeniser, options);
124
- if (decoded === DONE) {
125
- throw new Error(`${ decodeErrPrefix } did not find any content to decode`);
126
- }
127
- if (decoded === BREAK) {
128
- throw new Error(`${ decodeErrPrefix } got unexpected break`);
129
- }
130
- if (!tokeniser.done()) {
131
- throw new Error(`${ decodeErrPrefix } too many terminals, data makes no sense`);
132
- }
133
- return decoded;
134
- }
135
- export {
136
- Tokeniser,
137
- tokensToObject,
138
- decode
139
- };
@@ -1,123 +0,0 @@
1
- import { Tokeniser } from './decode.js';
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();
9
- function* tokensToDiagnostic(inp, width = 100) {
10
- const tokeniser = new Tokeniser(inp, {
11
- retainStringBytes: true,
12
- allowBigInt: true
13
- });
14
- let pos = 0;
15
- const indent = [];
16
- const slc = (start, length) => {
17
- return toHex(inp.slice(pos + start, pos + start + length));
18
- };
19
- while (!tokeniser.done()) {
20
- const token = tokeniser.next();
21
- let margin = ''.padStart(indent.length * 2, ' ');
22
- let vLength = token.encodedLength - 1;
23
- let v = String(token.value);
24
- let outp = `${ margin }${ slc(0, 1) }`;
25
- const str = token.type.name === 'bytes' || token.type.name === 'string';
26
- if (token.type.name === 'string') {
27
- v = v.length;
28
- vLength -= v;
29
- } else if (token.type.name === 'bytes') {
30
- v = token.value.length;
31
- vLength -= v;
32
- }
33
- let multilen;
34
- switch (token.type.name) {
35
- case 'string':
36
- case 'bytes':
37
- case 'map':
38
- case 'array':
39
- multilen = token.type.name === 'string' ? utf8Encoder.encode(token.value).length : token.value.length;
40
- if (multilen >= uintBoundaries[0]) {
41
- if (multilen < uintBoundaries[1]) {
42
- outp += ` ${ slc(1, 1) }`;
43
- } else if (multilen < uintBoundaries[2]) {
44
- outp += ` ${ slc(1, 2) }`;
45
- } else if (multilen < uintBoundaries[3]) {
46
- outp += ` ${ slc(1, 4) }`;
47
- } else if (multilen < uintBoundaries[4]) {
48
- outp += ` ${ slc(1, 8) }`;
49
- }
50
- }
51
- break;
52
- default:
53
- outp += ` ${ slc(1, vLength) }`;
54
- break;
55
- }
56
- outp = outp.padEnd(width / 2, ' ');
57
- outp += `# ${ margin }${ token.type.name }`;
58
- if (token.type.name !== v) {
59
- outp += `(${ v })`;
60
- }
61
- yield outp;
62
- if (str) {
63
- let asString = token.type.name === 'string';
64
- margin += ' ';
65
- let repr = asString ? utf8Encoder.encode(token.value) : token.value;
66
- if (asString && token.byteValue !== undefined) {
67
- if (repr.length !== token.byteValue.length) {
68
- repr = token.byteValue;
69
- asString = false;
70
- }
71
- }
72
- const wh = (width / 2 - margin.length - 1) / 2;
73
- let snip = 0;
74
- while (repr.length - snip > 0) {
75
- const piece = repr.slice(snip, snip + wh);
76
- snip += piece.length;
77
- const st = asString ? utf8Decoder.decode(piece) : piece.reduce((p, c) => {
78
- if (c < 32 || c >= 127 && c < 161 || c === 173) {
79
- return `${ p }\\x${ c.toString(16).padStart(2, '0') }`;
80
- }
81
- return `${ p }${ String.fromCharCode(c) }`;
82
- }, '');
83
- yield `${ margin }${ toHex(piece) }`.padEnd(width / 2, ' ') + `# ${ margin }"${ st }"`;
84
- }
85
- }
86
- if (indent.length) {
87
- indent[indent.length - 1]--;
88
- }
89
- if (!token.type.terminal) {
90
- switch (token.type.name) {
91
- case 'map':
92
- indent.push(token.value * 2);
93
- break;
94
- case 'array':
95
- indent.push(token.value);
96
- break;
97
- case 'tag':
98
- indent.push(1);
99
- break;
100
- default:
101
- throw new Error(`Unknown token type '${ token.type.name }'`);
102
- }
103
- }
104
- while (indent.length && indent[indent.length - 1] <= 0) {
105
- indent.pop();
106
- }
107
- pos += token.encodedLength;
108
- }
109
- }
110
- function fromDiag(input) {
111
- if (typeof input !== 'string') {
112
- throw new TypeError('Expected string input');
113
- }
114
- input = input.replace(/#.*?$/mg, '').replace(/[\s\r\n]+/mg, '');
115
- if (/[^a-f0-9]/i.test(input)) {
116
- throw new TypeError('Input string was not CBOR diagnostic format');
117
- }
118
- return fromHex(input);
119
- }
120
- export {
121
- tokensToDiagnostic,
122
- fromDiag
123
- };