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,279 +0,0 @@
1
- import { assert } from 'chai';
2
- import {
3
- decode,
4
- encode
5
- } from '../lib/json/json.js';
6
- const toBytes = str => new TextEncoder().encode(str);
7
- function verifyRoundTrip(obj, sorting) {
8
- const encoded = new TextDecoder().decode(encode(obj, sorting === false ? { mapSorter: null } : undefined));
9
- const json = JSON.stringify(obj);
10
- assert.strictEqual(encoded, json);
11
- const decoded = decode(toBytes(JSON.stringify(obj)));
12
- assert.deepStrictEqual(decoded, obj);
13
- }
14
- function verifyEncodedForm(testCase) {
15
- const obj = JSON.parse(testCase);
16
- const encoded = encode(obj);
17
- assert.strictEqual(new TextDecoder().decode(encoded), JSON.stringify(obj));
18
- const decoded = decode(encoded);
19
- assert.deepStrictEqual(decoded, obj);
20
- const decoded2 = decode(toBytes(testCase));
21
- assert.deepStrictEqual(decoded2, obj);
22
- }
23
- describe('json basics', () => {
24
- it('can round-trip basic literals', () => {
25
- const testCases = [
26
- 'null',
27
- 'true',
28
- 'false',
29
- '0',
30
- '9007199254740991',
31
- '-9007199254740991',
32
- JSON.stringify(Number.MAX_VALUE),
33
- JSON.stringify(Number.MIN_VALUE)
34
- ];
35
- for (const testCase of testCases) {
36
- verifyEncodedForm(testCase);
37
- }
38
- assert.strictEqual(decode(toBytes('1E1')), 10);
39
- assert.strictEqual(decode(toBytes('0.1e1')), 1);
40
- assert.strictEqual(decode(toBytes('1e-1')), 0.1);
41
- assert.strictEqual(decode(toBytes('1e+00')), 1);
42
- assert.strictEqual(decode(toBytes('10.0')), 10);
43
- assert.deepStrictEqual(decode(toBytes('[-10.0,1.0,0.0,100.0]')), [
44
- -10,
45
- 1,
46
- 0,
47
- 100
48
- ]);
49
- verifyRoundTrip(true);
50
- verifyRoundTrip(false);
51
- verifyRoundTrip(null);
52
- verifyRoundTrip(100);
53
- verifyRoundTrip(-100);
54
- verifyRoundTrip(1.11);
55
- verifyRoundTrip(-100.11111);
56
- verifyRoundTrip(11100000000);
57
- verifyRoundTrip(1.0011111e-18);
58
- });
59
- it('handles large integers as BigInt', () => {
60
- const verify = (inp, str) => {
61
- if (str === undefined) {
62
- str = String(inp);
63
- }
64
- assert.strictEqual(decode(toBytes(str), { allowBigInt: true }), inp);
65
- assert.strictEqual(decode(toBytes(str)), parseFloat(str));
66
- };
67
- verify(Number.MAX_SAFE_INTEGER);
68
- verify(-Number.MAX_SAFE_INTEGER);
69
- verify(BigInt('9007199254740992'));
70
- verify(BigInt('9007199254740993'));
71
- verify(BigInt('11959030306112471731'));
72
- verify(BigInt('18446744073709551615'));
73
- verify(BigInt('9223372036854775807'));
74
- verify(BigInt('-9007199254740992'));
75
- verify(BigInt('-9007199254740993'));
76
- verify(BigInt('-9223372036854776000'));
77
- verify(BigInt('-11959030306112471732'));
78
- verify(BigInt('-18446744073709551616'));
79
- verify(-9007199254740992, '-9007199254740992.0');
80
- verify(-9223372036854776000, '-9223372036854776000.0');
81
- verify(-18446744073709552000, '-18446744073709551616.0');
82
- });
83
- it('can round-trip string literals', () => {
84
- const testCases = [
85
- JSON.stringify(''),
86
- JSON.stringify(' '),
87
- JSON.stringify('"'),
88
- JSON.stringify('\\'),
89
- JSON.stringify('\b\f\n\r\t'),
90
- JSON.stringify('"'),
91
- JSON.stringify('" %22 0x22 034 "'),
92
- '"\uD83D\uDE00"'
93
- ];
94
- for (const testCase of testCases) {
95
- verifyEncodedForm(testCase);
96
- }
97
- assert.strictEqual(decode(toBytes('"/ & \\/"')), '/ & /');
98
- verifyRoundTrip('this is a string');
99
- verifyRoundTrip('this \uD834\uDD1E is a \u263A\u263A \u2663 string ̐ ̀\n\r');
100
- verifyRoundTrip('');
101
- verifyRoundTrip('foo\\bar\nbaz\tbop\rbing"bip\'bang');
102
- });
103
- it('can round-trip array literals', () => {
104
- const testCases = [
105
- '[]',
106
- '[null]',
107
- '[true, false]',
108
- '[ \n 0,1, 2\n , 3,\n4] \n ',
109
- '[-10.0, 1.0, 0.0, 100.0]',
110
- '[["2 deep"]]'
111
- ];
112
- for (const testCase of testCases) {
113
- verifyEncodedForm(testCase);
114
- }
115
- verifyRoundTrip([
116
- 1,
117
- 2,
118
- 3,
119
- 'string',
120
- true,
121
- 4
122
- ]);
123
- verifyRoundTrip([
124
- 1,
125
- 2,
126
- 3,
127
- 'string',
128
- true,
129
- [
130
- 'and',
131
- 'a',
132
- 'nested',
133
- 'array',
134
- true
135
- ],
136
- 4
137
- ]);
138
- });
139
- it('can round-trip object literals', () => {
140
- const testCases = [
141
- '{}',
142
- '\n {\n "\\b"\n :\n""\n }\n ',
143
- '{"":""}',
144
- '{"1":{"2":0,"3":"deep"}}'
145
- ];
146
- for (const testCase of testCases) {
147
- verifyEncodedForm(testCase);
148
- }
149
- });
150
- it('will sort map keys', () => {
151
- const unsorted = {
152
- one: 1,
153
- two: 2,
154
- three: 3.1,
155
- str: 'string',
156
- bool: true,
157
- four: 4
158
- };
159
- verifyRoundTrip(unsorted, false);
160
- assert.strictEqual(new TextDecoder().decode(encode(unsorted)), '{"bool":true,"four":4,"one":1,"str":"string","three":3.1,"two":2}');
161
- });
162
- it('can handle novel cases', () => {
163
- assert.strictEqual(decode(toBytes('"this \\uD834\\uDD1E is a \\u263a\\u263a string"')), 'this \uD834\uDD1E is a \u263A\u263A string');
164
- verifyRoundTrip({
165
- one: 1,
166
- two: 2,
167
- three: 3.1,
168
- str: 'string',
169
- arr: [
170
- 'and',
171
- 'a',
172
- 'nested',
173
- [],
174
- 'array',
175
- [
176
- true,
177
- 1
178
- ],
179
- false
180
- ],
181
- bool: true,
182
- obj: {
183
- nested: 'object',
184
- a: [],
185
- o: {}
186
- },
187
- four: 4
188
- }, false);
189
- verifyRoundTrip([
190
- false,
191
- [
192
- {
193
- '#nFzU': {},
194
- '\\w>': -0.9441451951197325,
195
- '\t\'': '\'JB+2Wg\tw"IrM*#e^L/d&4rrzUuwq(1mH6aVRredB&Bfs]S"KqK(Tz1Q"URBAfw',
196
- '\n@FrfM': 'M[D]q&'
197
- },
198
- 'J4>\'Xdc+u2$%',
199
- 4227406737130333
200
- ]
201
- ], false);
202
- verifyRoundTrip([
203
- 0.12995619865708727,
204
- -4973404279772543,
205
- {
206
- drG2: [true],
207
- ';#K^Qf>V': null,
208
- '`2=': 'ecc<e/$+-.;U>Gr5RdZDJ\n5+:{=QHNN.tVVN~dX$FWFwu`6>"&=tW!*1*^\u263A)JFM1p|}&X.B|${*\\f@!w2\u263A+'
209
- }
210
- ], false);
211
- assert.strictEqual(`${ decode(encode(9007199254740991)) }`, '9007199254740991');
212
- assert.strictEqual(`${ decode(encode(9007199254740992)) }`, '9007199254740992');
213
- assert.strictEqual(`${ decode(encode(900719925474099100)) }`, '900719925474099100');
214
- });
215
- it('should throw on bad types', () => {
216
- assert.throws(() => encode(new Uint8Array([
217
- 1,
218
- 2
219
- ])), /CBOR encode error: unsupported type: Uint8Array/);
220
- assert.throws(() => encode({
221
- boop: new Uint8Array([
222
- 1,
223
- 2
224
- ])
225
- }), /CBOR encode error: unsupported type: Uint8Array/);
226
- assert.throws(() => encode(undefined), /CBOR encode error: unsupported type: undefined/);
227
- assert.throws(() => encode(new Map([
228
- [
229
- 1,
230
- 2
231
- ],
232
- [
233
- 2,
234
- 3
235
- ]
236
- ])), /CBOR encode error: non-string map keys are not supported/);
237
- assert.throws(() => encode(new Map([
238
- [
239
- [
240
- 'foo',
241
- 'bar'
242
- ],
243
- 2
244
- ],
245
- [
246
- [
247
- 'bar',
248
- 'foo'
249
- ],
250
- 3
251
- ]
252
- ])), /CBOR encode error: complex map keys are not supported/);
253
- });
254
- it('should throw on bad decode failure modes', () => {
255
- assert.throws(() => decode(toBytes('{"a":1 & "b":2}')), 'CBOR decode error: unexpected character at position 7, was expecting object delimiter but found \'&\'');
256
- assert.throws(() => decode(toBytes('{"a":1,"b"!2}')), 'CBOR decode error: unexpected character at position 10, was expecting key/value delimiter \':\' but found \'!\'');
257
- assert.throws(() => decode(toBytes('[1,2&3]')), 'CBOR decode error: unexpected character at position 4, was expecting array delimiter but found \'&\'');
258
- assert.throws(() => decode(toBytes('{"a":!}')), 'CBOR decode error: unexpected character at position 5');
259
- assert.throws(() => decode(toBytes('"abc')), 'CBOR decode error: unexpected end of string at position 4');
260
- assert.throws(() => decode(toBytes('"ab\\xc"')), 'CBOR decode error: unexpected string escape character at position 5');
261
- assert.throws(() => decode(toBytes('"ab\x1Ec"')), 'CBOR decode error: invalid control character at position 3');
262
- assert.throws(() => decode(toBytes('"ab\\')), 'CBOR decode error: unexpected string termination at position 4');
263
- assert.throws(() => decode(toBytes('"\u263A').subarray(0, 3)), 'CBOR decode error: unexpected unicode sequence at position 1');
264
- assert.throws(() => decode(toBytes('"\\uxyza"')), 'CBOR decode error: unexpected unicode escape character at position 3');
265
- assert.throws(() => decode(toBytes('"\\u11"')), 'CBOR decode error: unexpected end of unicode escape sequence at position 3');
266
- assert.throws(() => decode(toBytes('-boop')), 'CBOR decode error: unexpected token at position 1');
267
- assert.throws(() => decode(toBytes('{"v":nope}')), 'CBOR decode error: unexpected token at position 7, expected to find \'null\'');
268
- assert.throws(() => decode(toBytes('[n]')), 'CBOR decode error: unexpected end of input at position 1');
269
- assert.throws(() => decode(toBytes('{"v":truu}')), 'CBOR decode error: unexpected token at position 9, expected to find \'true\'');
270
- assert.throws(() => decode(toBytes('[tr]')), 'CBOR decode error: unexpected end of input at position 1');
271
- assert.throws(() => decode(toBytes('{"v":flase}')), 'CBOR decode error: unexpected token at position 7, expected to find \'false\'');
272
- assert.throws(() => decode(toBytes('[fa]')), 'CBOR decode error: unexpected end of input at position 1');
273
- assert.throws(() => decode(toBytes('-0..1')), 'CBOR decode error: unexpected token at position 3');
274
- });
275
- it('should throw when rejectDuplicateMapKeys enabled on duplicate keys', () => {
276
- assert.deepStrictEqual(decode(toBytes('{"foo":1,"foo":2}')), { foo: 2 });
277
- assert.throws(() => decode(toBytes('{"foo":1,"foo":2}'), { rejectDuplicateMapKeys: true }), /CBOR decode error: found repeat map key "foo"/);
278
- });
279
- });
@@ -1,55 +0,0 @@
1
- import chai from 'chai';
2
- import { garbage } from 'ipld-garbage';
3
- import { uintBoundaries } from '../lib/0uint.js';
4
- import { encode } from '../cborg.js';
5
- import { encodedLength } from '../lib/length.js';
6
- import { dateEncoder } from './common.js';
7
- const {assert} = chai;
8
- function verifyLength(object, options) {
9
- const len = encodedLength(object, options);
10
- const encoded = encode(object, options);
11
- const actual = encoded.length;
12
- assert.strictEqual(actual, len, JSON.stringify(object));
13
- }
14
- describe('encodedLength', () => {
15
- it('int boundaries', () => {
16
- for (let ii = 0; ii < 4; ii++) {
17
- verifyLength(uintBoundaries[ii]);
18
- verifyLength(uintBoundaries[ii] - 1);
19
- verifyLength(uintBoundaries[ii] + 1);
20
- verifyLength(-1 * uintBoundaries[ii]);
21
- verifyLength(-1 * uintBoundaries[ii] - 1);
22
- verifyLength(-1 * uintBoundaries[ii] + 1);
23
- }
24
- });
25
- it('tags', () => {
26
- verifyLength({ date: new Date('2013-03-21T20:04:00Z') }, { typeEncoders: { Date: dateEncoder } });
27
- });
28
- it('floats', () => {
29
- verifyLength(0.5);
30
- verifyLength(0.5, { float64: true });
31
- verifyLength(8.940696716308594e-8);
32
- verifyLength(8.940696716308594e-8, { float64: true });
33
- });
34
- it('small garbage', function () {
35
- this.timeout(10000);
36
- for (let ii = 0; ii < 1000; ii++) {
37
- const gbg = garbage(1 << 6, { weights: { CID: 0 } });
38
- verifyLength(gbg);
39
- }
40
- });
41
- it('medium garbage', function () {
42
- this.timeout(10000);
43
- for (let ii = 0; ii < 100; ii++) {
44
- const gbg = garbage(1 << 16, { weights: { CID: 0 } });
45
- verifyLength(gbg);
46
- }
47
- });
48
- it('large garbage', function () {
49
- this.timeout(10000);
50
- for (let ii = 0; ii < 10; ii++) {
51
- const gbg = garbage(1 << 20, { weights: { CID: 0 } });
52
- verifyLength(gbg);
53
- }
54
- });
55
- });
package/esm/cborg.js DELETED
@@ -1,12 +0,0 @@
1
- import { encode } from './lib/encode.js';
2
- import { decode } from './lib/decode.js';
3
- import {
4
- Token,
5
- Type
6
- } from './lib/token.js';
7
- export {
8
- decode,
9
- encode,
10
- Token,
11
- Type
12
- };
package/esm/lib/0uint.js DELETED
@@ -1,152 +0,0 @@
1
- import {
2
- Token,
3
- Type
4
- } from './token.js';
5
- import {
6
- decodeErrPrefix,
7
- assertEnoughData
8
- } from './common.js';
9
- export const uintBoundaries = [
10
- 24,
11
- 256,
12
- 65536,
13
- 4294967296,
14
- BigInt('18446744073709551616')
15
- ];
16
- export function readUint8(data, offset, options) {
17
- assertEnoughData(data, offset, 1);
18
- const value = data[offset];
19
- if (options.strict === true && value < uintBoundaries[0]) {
20
- throw new Error(`${ decodeErrPrefix } integer encoded in more bytes than necessary (strict decode)`);
21
- }
22
- return value;
23
- }
24
- export function readUint16(data, offset, options) {
25
- assertEnoughData(data, offset, 2);
26
- const value = data[offset] << 8 | data[offset + 1];
27
- if (options.strict === true && value < uintBoundaries[1]) {
28
- throw new Error(`${ decodeErrPrefix } integer encoded in more bytes than necessary (strict decode)`);
29
- }
30
- return value;
31
- }
32
- export function readUint32(data, offset, options) {
33
- assertEnoughData(data, offset, 4);
34
- const value = data[offset] * 16777216 + (data[offset + 1] << 16) + (data[offset + 2] << 8) + data[offset + 3];
35
- if (options.strict === true && value < uintBoundaries[2]) {
36
- throw new Error(`${ decodeErrPrefix } integer encoded in more bytes than necessary (strict decode)`);
37
- }
38
- return value;
39
- }
40
- export function readUint64(data, offset, options) {
41
- assertEnoughData(data, offset, 8);
42
- const hi = data[offset] * 16777216 + (data[offset + 1] << 16) + (data[offset + 2] << 8) + data[offset + 3];
43
- const lo = data[offset + 4] * 16777216 + (data[offset + 5] << 16) + (data[offset + 6] << 8) + data[offset + 7];
44
- const value = (BigInt(hi) << BigInt(32)) + BigInt(lo);
45
- if (options.strict === true && value < uintBoundaries[3]) {
46
- throw new Error(`${ decodeErrPrefix } integer encoded in more bytes than necessary (strict decode)`);
47
- }
48
- if (value <= Number.MAX_SAFE_INTEGER) {
49
- return Number(value);
50
- }
51
- if (options.allowBigInt === true) {
52
- return value;
53
- }
54
- throw new Error(`${ decodeErrPrefix } integers outside of the safe integer range are not supported`);
55
- }
56
- export function decodeUint8(data, pos, _minor, options) {
57
- return new Token(Type.uint, readUint8(data, pos + 1, options), 2);
58
- }
59
- export function decodeUint16(data, pos, _minor, options) {
60
- return new Token(Type.uint, readUint16(data, pos + 1, options), 3);
61
- }
62
- export function decodeUint32(data, pos, _minor, options) {
63
- return new Token(Type.uint, readUint32(data, pos + 1, options), 5);
64
- }
65
- export function decodeUint64(data, pos, _minor, options) {
66
- return new Token(Type.uint, readUint64(data, pos + 1, options), 9);
67
- }
68
- export function encodeUint(buf, token) {
69
- return encodeUintValue(buf, 0, token.value);
70
- }
71
- export function encodeUintValue(buf, major, uint) {
72
- if (uint < uintBoundaries[0]) {
73
- const nuint = Number(uint);
74
- buf.push([major | nuint]);
75
- } else if (uint < uintBoundaries[1]) {
76
- const nuint = Number(uint);
77
- buf.push([
78
- major | 24,
79
- nuint
80
- ]);
81
- } else if (uint < uintBoundaries[2]) {
82
- const nuint = Number(uint);
83
- buf.push([
84
- major | 25,
85
- nuint >>> 8,
86
- nuint & 255
87
- ]);
88
- } else if (uint < uintBoundaries[3]) {
89
- const nuint = Number(uint);
90
- buf.push([
91
- major | 26,
92
- nuint >>> 24 & 255,
93
- nuint >>> 16 & 255,
94
- nuint >>> 8 & 255,
95
- nuint & 255
96
- ]);
97
- } else {
98
- const buint = BigInt(uint);
99
- if (buint < uintBoundaries[4]) {
100
- const set = [
101
- major | 27,
102
- 0,
103
- 0,
104
- 0,
105
- 0,
106
- 0,
107
- 0,
108
- 0
109
- ];
110
- let lo = Number(buint & BigInt(4294967295));
111
- let hi = Number(buint >> BigInt(32) & BigInt(4294967295));
112
- set[8] = lo & 255;
113
- lo = lo >> 8;
114
- set[7] = lo & 255;
115
- lo = lo >> 8;
116
- set[6] = lo & 255;
117
- lo = lo >> 8;
118
- set[5] = lo & 255;
119
- set[4] = hi & 255;
120
- hi = hi >> 8;
121
- set[3] = hi & 255;
122
- hi = hi >> 8;
123
- set[2] = hi & 255;
124
- hi = hi >> 8;
125
- set[1] = hi & 255;
126
- buf.push(set);
127
- } else {
128
- throw new Error(`${ decodeErrPrefix } encountered BigInt larger than allowable range`);
129
- }
130
- }
131
- }
132
- encodeUint.encodedSize = function encodedSize(token) {
133
- return encodeUintValue.encodedSize(token.value);
134
- };
135
- encodeUintValue.encodedSize = function encodedSize(uint) {
136
- if (uint < uintBoundaries[0]) {
137
- return 1;
138
- }
139
- if (uint < uintBoundaries[1]) {
140
- return 2;
141
- }
142
- if (uint < uintBoundaries[2]) {
143
- return 3;
144
- }
145
- if (uint < uintBoundaries[3]) {
146
- return 5;
147
- }
148
- return 9;
149
- };
150
- encodeUint.compareTokens = function compareTokens(tok1, tok2) {
151
- return tok1.value < tok2.value ? -1 : tok1.value > tok2.value ? 1 : 0;
152
- };
@@ -1,55 +0,0 @@
1
- import {
2
- Token,
3
- Type
4
- } from './token.js';
5
- import * as uint from './0uint.js';
6
- import { decodeErrPrefix } from './common.js';
7
- export function decodeNegint8(data, pos, _minor, options) {
8
- return new Token(Type.negint, -1 - uint.readUint8(data, pos + 1, options), 2);
9
- }
10
- export function decodeNegint16(data, pos, _minor, options) {
11
- return new Token(Type.negint, -1 - uint.readUint16(data, pos + 1, options), 3);
12
- }
13
- export function decodeNegint32(data, pos, _minor, options) {
14
- return new Token(Type.negint, -1 - uint.readUint32(data, pos + 1, options), 5);
15
- }
16
- const neg1b = BigInt(-1);
17
- const pos1b = BigInt(1);
18
- export function decodeNegint64(data, pos, _minor, options) {
19
- const int = uint.readUint64(data, pos + 1, options);
20
- if (typeof int !== 'bigint') {
21
- const value = -1 - int;
22
- if (value >= Number.MIN_SAFE_INTEGER) {
23
- return new Token(Type.negint, value, 9);
24
- }
25
- }
26
- if (options.allowBigInt !== true) {
27
- throw new Error(`${ decodeErrPrefix } integers outside of the safe integer range are not supported`);
28
- }
29
- return new Token(Type.negint, neg1b - BigInt(int), 9);
30
- }
31
- export function encodeNegint(buf, token) {
32
- const negint = token.value;
33
- const unsigned = typeof negint === 'bigint' ? negint * neg1b - pos1b : negint * -1 - 1;
34
- uint.encodeUintValue(buf, token.type.majorEncoded, unsigned);
35
- }
36
- encodeNegint.encodedSize = function encodedSize(token) {
37
- const negint = token.value;
38
- const unsigned = typeof negint === 'bigint' ? negint * neg1b - pos1b : negint * -1 - 1;
39
- if (unsigned < uint.uintBoundaries[0]) {
40
- return 1;
41
- }
42
- if (unsigned < uint.uintBoundaries[1]) {
43
- return 2;
44
- }
45
- if (unsigned < uint.uintBoundaries[2]) {
46
- return 3;
47
- }
48
- if (unsigned < uint.uintBoundaries[3]) {
49
- return 5;
50
- }
51
- return 9;
52
- };
53
- encodeNegint.compareTokens = function compareTokens(tok1, tok2) {
54
- return tok1.value < tok2.value ? 1 : tok1.value > tok2.value ? -1 : 0;
55
- };
package/esm/lib/2bytes.js DELETED
@@ -1,59 +0,0 @@
1
- import {
2
- Token,
3
- Type
4
- } from './token.js';
5
- import {
6
- assertEnoughData,
7
- decodeErrPrefix
8
- } from './common.js';
9
- import * as uint from './0uint.js';
10
- import {
11
- compare,
12
- fromString,
13
- slice
14
- } from './byte-utils.js';
15
- function toToken(data, pos, prefix, length) {
16
- assertEnoughData(data, pos, prefix + length);
17
- const buf = slice(data, pos + prefix, pos + prefix + length);
18
- return new Token(Type.bytes, buf, prefix + length);
19
- }
20
- export function decodeBytesCompact(data, pos, minor, _options) {
21
- return toToken(data, pos, 1, minor);
22
- }
23
- export function decodeBytes8(data, pos, _minor, options) {
24
- return toToken(data, pos, 2, uint.readUint8(data, pos + 1, options));
25
- }
26
- export function decodeBytes16(data, pos, _minor, options) {
27
- return toToken(data, pos, 3, uint.readUint16(data, pos + 1, options));
28
- }
29
- export function decodeBytes32(data, pos, _minor, options) {
30
- return toToken(data, pos, 5, uint.readUint32(data, pos + 1, options));
31
- }
32
- export function decodeBytes64(data, pos, _minor, options) {
33
- const l = uint.readUint64(data, pos + 1, options);
34
- if (typeof l === 'bigint') {
35
- throw new Error(`${ decodeErrPrefix } 64-bit integer bytes lengths not supported`);
36
- }
37
- return toToken(data, pos, 9, l);
38
- }
39
- function tokenBytes(token) {
40
- if (token.encodedBytes === undefined) {
41
- token.encodedBytes = token.type === Type.string ? fromString(token.value) : token.value;
42
- }
43
- return token.encodedBytes;
44
- }
45
- export function encodeBytes(buf, token) {
46
- const bytes = tokenBytes(token);
47
- uint.encodeUintValue(buf, token.type.majorEncoded, bytes.length);
48
- buf.push(bytes);
49
- }
50
- encodeBytes.encodedSize = function encodedSize(token) {
51
- const bytes = tokenBytes(token);
52
- return uint.encodeUintValue.encodedSize(bytes.length) + bytes.length;
53
- };
54
- encodeBytes.compareTokens = function compareTokens(tok1, tok2) {
55
- return compareBytes(tokenBytes(tok1), tokenBytes(tok2));
56
- };
57
- export function compareBytes(b1, b2) {
58
- return b1.length < b2.length ? -1 : b1.length > b2.length ? 1 : compare(b1, b2);
59
- }
@@ -1,43 +0,0 @@
1
- import {
2
- Token,
3
- Type
4
- } from './token.js';
5
- import {
6
- assertEnoughData,
7
- decodeErrPrefix
8
- } from './common.js';
9
- import * as uint from './0uint.js';
10
- import { encodeBytes } from './2bytes.js';
11
- import {
12
- toString,
13
- slice
14
- } from './byte-utils.js';
15
- function toToken(data, pos, prefix, length, options) {
16
- const totLength = prefix + length;
17
- assertEnoughData(data, pos, totLength);
18
- const tok = new Token(Type.string, toString(data, pos + prefix, pos + totLength), totLength);
19
- if (options.retainStringBytes === true) {
20
- tok.byteValue = slice(data, pos + prefix, pos + totLength);
21
- }
22
- return tok;
23
- }
24
- export function decodeStringCompact(data, pos, minor, options) {
25
- return toToken(data, pos, 1, minor, options);
26
- }
27
- export function decodeString8(data, pos, _minor, options) {
28
- return toToken(data, pos, 2, uint.readUint8(data, pos + 1, options), options);
29
- }
30
- export function decodeString16(data, pos, _minor, options) {
31
- return toToken(data, pos, 3, uint.readUint16(data, pos + 1, options), options);
32
- }
33
- export function decodeString32(data, pos, _minor, options) {
34
- return toToken(data, pos, 5, uint.readUint32(data, pos + 1, options), options);
35
- }
36
- export function decodeString64(data, pos, _minor, options) {
37
- const l = uint.readUint64(data, pos + 1, options);
38
- if (typeof l === 'bigint') {
39
- throw new Error(`${ decodeErrPrefix } 64-bit integer string lengths not supported`);
40
- }
41
- return toToken(data, pos, 9, l, options);
42
- }
43
- export const encodeString = encodeBytes;