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