cborg 1.8.1 → 1.9.2

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 (64) hide show
  1. package/README.md +15 -0
  2. package/cjs/browser-test/common.js +24 -0
  3. package/cjs/browser-test/node-test-bin.js +0 -1
  4. package/cjs/browser-test/noop-bin-test.js +4 -0
  5. package/cjs/browser-test/test-6tag.js +5 -19
  6. package/cjs/browser-test/test-length.js +63 -0
  7. package/cjs/lib/4array.js +3 -0
  8. package/cjs/lib/5map.js +3 -0
  9. package/cjs/lib/6tag.js +3 -0
  10. package/cjs/lib/7float.js +1 -2
  11. package/cjs/lib/bl.js +3 -1
  12. package/cjs/lib/encode.js +15 -9
  13. package/cjs/lib/length.js +36 -0
  14. package/cjs/node-test/common.js +24 -0
  15. package/cjs/node-test/node-test-bin.js +0 -1
  16. package/cjs/node-test/noop-bin-test.js +4 -0
  17. package/cjs/node-test/test-6tag.js +5 -19
  18. package/cjs/node-test/test-length.js +63 -0
  19. package/esm/browser-test/common.js +19 -0
  20. package/esm/browser-test/node-test-bin.js +0 -1
  21. package/esm/browser-test/noop-bin-test.js +1 -0
  22. package/esm/browser-test/test-6tag.js +4 -15
  23. package/esm/browser-test/test-length.js +55 -0
  24. package/esm/lib/4array.js +4 -1
  25. package/esm/lib/5map.js +4 -1
  26. package/esm/lib/6tag.js +4 -1
  27. package/esm/lib/7float.js +1 -2
  28. package/esm/lib/bl.js +3 -1
  29. package/esm/lib/encode.js +14 -9
  30. package/esm/lib/length.js +31 -0
  31. package/esm/node-test/common.js +19 -0
  32. package/esm/node-test/node-test-bin.js +0 -1
  33. package/esm/node-test/noop-bin-test.js +1 -0
  34. package/esm/node-test/test-6tag.js +4 -15
  35. package/esm/node-test/test-length.js +55 -0
  36. package/interface.ts +1 -0
  37. package/length +1 -0
  38. package/lib/4array.js +8 -0
  39. package/lib/5map.js +8 -0
  40. package/lib/6tag.js +8 -0
  41. package/lib/7float.js +1 -2
  42. package/lib/bl.js +3 -1
  43. package/lib/byte-utils.js +4 -4
  44. package/lib/encode.js +16 -10
  45. package/lib/length.js +61 -0
  46. package/package.json +13 -4
  47. package/test/common.js +18 -0
  48. package/test/node-test-bin.js +0 -2
  49. package/test/noop-bin-test.js +3 -0
  50. package/test/test-6tag.js +1 -17
  51. package/test/test-length.js +65 -0
  52. package/types/interface.d.ts.map +1 -1
  53. package/types/lib/4array.d.ts +5 -0
  54. package/types/lib/4array.d.ts.map +1 -1
  55. package/types/lib/5map.d.ts +5 -0
  56. package/types/lib/5map.d.ts.map +1 -1
  57. package/types/lib/6tag.d.ts +5 -0
  58. package/types/lib/6tag.d.ts.map +1 -1
  59. package/types/lib/7float.d.ts.map +1 -1
  60. package/types/lib/bl.d.ts.map +1 -1
  61. package/types/lib/encode.d.ts +2 -0
  62. package/types/lib/encode.d.ts.map +1 -1
  63. package/types/lib/length.d.ts +27 -0
  64. package/types/lib/length.d.ts.map +1 -0
package/README.md CHANGED
@@ -27,6 +27,7 @@
27
27
  * [Options](#options)
28
28
  * [`decode(data[, options])`](#decodedata-options)
29
29
  * [Options](#options-1)
30
+ * [`encodedLength(data[, options])`](#encodedlengthdata-options)
30
31
  * [Type encoders](#type-encoders)
31
32
  * [Tag decoders](#tag-decoders)
32
33
  * [Deterministic encoding recommendations](#deterministic-encoding-recommendations)
@@ -250,6 +251,20 @@ Decode valid CBOR bytes from a `Uint8Array` (or `Buffer`) and return a JavaScrip
250
251
  * `tags` (array): a mapping of tag number to tag decoder function. By default no tags are supported. See [Tag decoders](#tag-decoders).
251
252
  * `tokenizer` (object): an object with two methods, `next()` which returns a `Token` and `done()` which returns a `boolean`. Can be used to implement custom input decoding. See the source code for examples.
252
253
 
254
+ ### `encodedLength(data[, options])`
255
+
256
+ ```js
257
+ import { encodedLength } from 'cborg/length'
258
+ ```
259
+
260
+ ```js
261
+ const { encodedLength } = require('cborg/length')
262
+ ```
263
+
264
+ Calculate the byte length of the given data when encoded as CBOR with the options provided. The options are the same as for an `encode()` call. This calculation will be accurate if the same options are used as when performing a normal `encode()`. Some encode options can change the encoding output length.
265
+
266
+ A `tokensToLength()` function is available which deals directly with a tokenized form of the object, but this only recommended for advanced users.
267
+
253
268
  ### Type encoders
254
269
 
255
270
  The `typeEncoders` property to the `options` argument to `encode()` allows you to add additional functionality to cborg, or override existing functionality.
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var token = require('../lib/token.js');
6
+
7
+ function dateDecoder(obj) {
8
+ if (typeof obj !== 'string') {
9
+ throw new Error('expected string for tag 1');
10
+ }
11
+ return new Date(obj);
12
+ }
13
+ function dateEncoder(obj) {
14
+ if (!(obj instanceof Date)) {
15
+ throw new Error('expected Date for "Date" encoder');
16
+ }
17
+ return [
18
+ new token.Token(token.Type.tag, 0),
19
+ new token.Token(token.Type.string, obj.toISOString().replace(/\.000Z$/, 'Z'))
20
+ ];
21
+ }
22
+
23
+ exports.dateDecoder = dateDecoder;
24
+ exports.dateEncoder = dateEncoder;
@@ -6,7 +6,6 @@ var process = require('process');
6
6
  var path = require('path');
7
7
  var os = require('os');
8
8
  var url = require('url');
9
- require('../lib/bin.js');
10
9
 
11
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
11
 
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ require('../lib/bin.js');
4
+
@@ -4,6 +4,7 @@ var chai = require('chai');
4
4
  var token = require('../lib/token.js');
5
5
  require('../cborg.js');
6
6
  var byteUtils = require('../lib/byte-utils.js');
7
+ var common = require('./common.js');
7
8
  var encode = require('../lib/encode.js');
8
9
  var decode = require('../lib/decode.js');
9
10
 
@@ -12,21 +13,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
12
13
  var chai__default = /*#__PURE__*/_interopDefaultLegacy(chai);
13
14
 
14
15
  const {assert} = chai__default["default"];
15
- function dateDecoder(obj) {
16
- if (typeof obj !== 'string') {
17
- throw new Error('expected string for tag 1');
18
- }
19
- return new Date(obj);
20
- }
21
- function dateEncoder(obj) {
22
- if (!(obj instanceof Date)) {
23
- throw new Error('expected Date for "Date" encoder');
24
- }
25
- return [
26
- new token.Token(token.Type.tag, 0),
27
- new token.Token(token.Type.string, obj.toISOString().replace(/\.000Z$/, 'Z'))
28
- ];
29
- }
30
16
  function Uint16ArrayDecoder(obj) {
31
17
  if (typeof obj !== 'string') {
32
18
  throw new Error('expected string for tag 23');
@@ -46,8 +32,8 @@ function Uint16ArrayEncoder(obj) {
46
32
  describe('tag', () => {
47
33
  it('date', () => {
48
34
  assert.throws(() => encode.encode({ d: new Date() }), /unsupported type: Date/);
49
- assert.equal(byteUtils.toHex(encode.encode(new Date('2013-03-21T20:04:00Z'), { typeEncoders: { Date: dateEncoder } })), 'c074323031332d30332d32315432303a30343a30305a');
50
- const decodedDate = decode.decode(byteUtils.fromHex('c074323031332d30332d32315432303a30343a30305a'), { tags: { 0: dateDecoder } });
35
+ assert.equal(byteUtils.toHex(encode.encode(new Date('2013-03-21T20:04:00Z'), { typeEncoders: { Date: common.dateEncoder } })), 'c074323031332d30332d32315432303a30343a30305a');
36
+ const decodedDate = decode.decode(byteUtils.fromHex('c074323031332d30332d32315432303a30343a30305a'), { tags: { 0: common.dateDecoder } });
51
37
  assert.instanceOf(decodedDate, Date);
52
38
  assert.equal(decodedDate.toISOString(), new Date('2013-03-21T20:04:00Z').toISOString());
53
39
  });
@@ -69,12 +55,12 @@ describe('tag', () => {
69
55
  const verify = (hex, strict) => {
70
56
  if (!strict) {
71
57
  assert.throws(() => decode.decode(byteUtils.fromHex(hex), {
72
- tags: { 8: dateDecoder },
58
+ tags: { 8: common.dateDecoder },
73
59
  strict: true
74
60
  }), /integer encoded in more bytes than necessary/);
75
61
  }
76
62
  const decodedDate = decode.decode(byteUtils.fromHex(hex), {
77
- tags: { 8: dateDecoder },
63
+ tags: { 8: common.dateDecoder },
78
64
  strict
79
65
  });
80
66
  assert.instanceOf(decodedDate, Date);
@@ -0,0 +1,63 @@
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/lib/4array.js CHANGED
@@ -38,6 +38,9 @@ function encodeArray(buf, token$1) {
38
38
  _0uint.encodeUintValue(buf, token.Type.array.majorEncoded, token$1.value);
39
39
  }
40
40
  encodeArray.compareTokens = _0uint.encodeUint.compareTokens;
41
+ encodeArray.encodedSize = function encodedSize(token) {
42
+ return _0uint.encodeUintValue.encodedSize(token.value);
43
+ };
41
44
 
42
45
  exports.decodeArray16 = decodeArray16;
43
46
  exports.decodeArray32 = decodeArray32;
package/cjs/lib/5map.js CHANGED
@@ -38,6 +38,9 @@ function encodeMap(buf, token$1) {
38
38
  _0uint.encodeUintValue(buf, token.Type.map.majorEncoded, token$1.value);
39
39
  }
40
40
  encodeMap.compareTokens = _0uint.encodeUint.compareTokens;
41
+ encodeMap.encodedSize = function encodedSize(token) {
42
+ return _0uint.encodeUintValue.encodedSize(token.value);
43
+ };
41
44
 
42
45
  exports.decodeMap16 = decodeMap16;
43
46
  exports.decodeMap32 = decodeMap32;
package/cjs/lib/6tag.js CHANGED
@@ -24,6 +24,9 @@ function encodeTag(buf, token$1) {
24
24
  _0uint.encodeUintValue(buf, token.Type.tag.majorEncoded, token$1.value);
25
25
  }
26
26
  encodeTag.compareTokens = _0uint.encodeUint.compareTokens;
27
+ encodeTag.encodedSize = function encodedSize(token) {
28
+ return _0uint.encodeUintValue.encodedSize(token.value);
29
+ };
27
30
 
28
31
  exports.decodeTag16 = decodeTag16;
29
32
  exports.decodeTag32 = decodeTag32;
package/cjs/lib/7float.js CHANGED
@@ -87,10 +87,9 @@ encodeFloat.encodedSize = function encodedSize(token, options) {
87
87
  if (float === false || float === true || float === null || float === undefined) {
88
88
  return 1;
89
89
  }
90
- let decoded;
91
90
  if (!options || options.float64 !== true) {
92
91
  encodeFloat16(float);
93
- decoded = readFloat16(ui8a, 1);
92
+ let decoded = readFloat16(ui8a, 1);
94
93
  if (float === decoded || Number.isNaN(float)) {
95
94
  return 3;
96
95
  }
package/cjs/lib/bl.js CHANGED
@@ -14,9 +14,11 @@ class Bl {
14
14
  this._initReuseChunk = null;
15
15
  }
16
16
  reset() {
17
- this.chunks = [];
18
17
  this.cursor = 0;
19
18
  this.maxCursor = -1;
19
+ if (this.chunks.length) {
20
+ this.chunks = [];
21
+ }
20
22
  if (this._initReuseChunk !== null) {
21
23
  this.chunks.push(this._initReuseChunk);
22
24
  this.maxCursor = this._initReuseChunk.length - 1;
package/cjs/lib/encode.js CHANGED
@@ -22,15 +22,19 @@ const defaultEncodeOptions = {
22
22
  mapSorter,
23
23
  quickEncodeToken: jump.quickEncodeToken
24
24
  };
25
- const cborEncoders = [];
26
- cborEncoders[token.Type.uint.major] = _0uint.encodeUint;
27
- cborEncoders[token.Type.negint.major] = _1negint.encodeNegint;
28
- cborEncoders[token.Type.bytes.major] = _2bytes.encodeBytes;
29
- cborEncoders[token.Type.string.major] = _3string.encodeString;
30
- cborEncoders[token.Type.array.major] = _4array.encodeArray;
31
- cborEncoders[token.Type.map.major] = _5map.encodeMap;
32
- cborEncoders[token.Type.tag.major] = _6tag.encodeTag;
33
- cborEncoders[token.Type.float.major] = _7float.encodeFloat;
25
+ function makeCborEncoders() {
26
+ const encoders = [];
27
+ encoders[token.Type.uint.major] = _0uint.encodeUint;
28
+ encoders[token.Type.negint.major] = _1negint.encodeNegint;
29
+ encoders[token.Type.bytes.major] = _2bytes.encodeBytes;
30
+ encoders[token.Type.string.major] = _3string.encodeString;
31
+ encoders[token.Type.array.major] = _4array.encodeArray;
32
+ encoders[token.Type.map.major] = _5map.encodeMap;
33
+ encoders[token.Type.tag.major] = _6tag.encodeTag;
34
+ encoders[token.Type.float.major] = _7float.encodeFloat;
35
+ return encoders;
36
+ }
37
+ const cborEncoders = makeCborEncoders();
34
38
  const buf = new bl.Bl();
35
39
  class Ref {
36
40
  constructor(obj, parent) {
@@ -228,6 +232,7 @@ function encodeCustom(data, encoders, options) {
228
232
  return byteUtils.asU8A(buf.chunks[0]);
229
233
  }
230
234
  }
235
+ buf.reset();
231
236
  tokensToEncoded(buf, tokens, encoders, options);
232
237
  return buf.toBytes(true);
233
238
  }
@@ -239,4 +244,5 @@ function encode(data, options) {
239
244
  exports.Ref = Ref;
240
245
  exports.encode = encode;
241
246
  exports.encodeCustom = encodeCustom;
247
+ exports.makeCborEncoders = makeCborEncoders;
242
248
  exports.objectToTokens = objectToTokens;
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var encode = require('./encode.js');
6
+ var jump = require('./jump.js');
7
+
8
+ const cborEncoders = encode.makeCborEncoders();
9
+ const defaultEncodeOptions = {
10
+ float64: false,
11
+ quickEncodeToken: jump.quickEncodeToken
12
+ };
13
+ function encodedLength(data, options) {
14
+ options = Object.assign({}, defaultEncodeOptions, options);
15
+ options.mapSorter = undefined;
16
+ const tokens = encode.objectToTokens(data, options);
17
+ return tokensToLength(tokens, cborEncoders, options);
18
+ }
19
+ function tokensToLength(tokens, encoders = cborEncoders, options = defaultEncodeOptions) {
20
+ if (Array.isArray(tokens)) {
21
+ let len = 0;
22
+ for (const token of tokens) {
23
+ len += tokensToLength(token, encoders, options);
24
+ }
25
+ return len;
26
+ } else {
27
+ const encoder = encoders[tokens.type.major];
28
+ if (encoder.encodedSize === undefined || typeof encoder.encodedSize !== 'function') {
29
+ throw new Error(`Encoder for ${ tokens.type.name } does not have an encodedSize()`);
30
+ }
31
+ return encoder.encodedSize(tokens, options);
32
+ }
33
+ }
34
+
35
+ exports.encodedLength = encodedLength;
36
+ exports.tokensToLength = tokensToLength;
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var token = require('../lib/token.js');
6
+
7
+ function dateDecoder(obj) {
8
+ if (typeof obj !== 'string') {
9
+ throw new Error('expected string for tag 1');
10
+ }
11
+ return new Date(obj);
12
+ }
13
+ function dateEncoder(obj) {
14
+ if (!(obj instanceof Date)) {
15
+ throw new Error('expected Date for "Date" encoder');
16
+ }
17
+ return [
18
+ new token.Token(token.Type.tag, 0),
19
+ new token.Token(token.Type.string, obj.toISOString().replace(/\.000Z$/, 'Z'))
20
+ ];
21
+ }
22
+
23
+ exports.dateDecoder = dateDecoder;
24
+ exports.dateEncoder = dateEncoder;
@@ -6,7 +6,6 @@ var process = require('process');
6
6
  var path = require('path');
7
7
  var os = require('os');
8
8
  var url = require('url');
9
- require('../lib/bin.js');
10
9
 
11
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
11
 
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+
3
+ require('../lib/bin.js');
4
+
@@ -4,6 +4,7 @@ var chai = require('chai');
4
4
  var token = require('../lib/token.js');
5
5
  require('../cborg.js');
6
6
  var byteUtils = require('../lib/byte-utils.js');
7
+ var common = require('./common.js');
7
8
  var encode = require('../lib/encode.js');
8
9
  var decode = require('../lib/decode.js');
9
10
 
@@ -12,21 +13,6 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
12
13
  var chai__default = /*#__PURE__*/_interopDefaultLegacy(chai);
13
14
 
14
15
  const {assert} = chai__default["default"];
15
- function dateDecoder(obj) {
16
- if (typeof obj !== 'string') {
17
- throw new Error('expected string for tag 1');
18
- }
19
- return new Date(obj);
20
- }
21
- function dateEncoder(obj) {
22
- if (!(obj instanceof Date)) {
23
- throw new Error('expected Date for "Date" encoder');
24
- }
25
- return [
26
- new token.Token(token.Type.tag, 0),
27
- new token.Token(token.Type.string, obj.toISOString().replace(/\.000Z$/, 'Z'))
28
- ];
29
- }
30
16
  function Uint16ArrayDecoder(obj) {
31
17
  if (typeof obj !== 'string') {
32
18
  throw new Error('expected string for tag 23');
@@ -46,8 +32,8 @@ function Uint16ArrayEncoder(obj) {
46
32
  describe('tag', () => {
47
33
  it('date', () => {
48
34
  assert.throws(() => encode.encode({ d: new Date() }), /unsupported type: Date/);
49
- assert.equal(byteUtils.toHex(encode.encode(new Date('2013-03-21T20:04:00Z'), { typeEncoders: { Date: dateEncoder } })), 'c074323031332d30332d32315432303a30343a30305a');
50
- const decodedDate = decode.decode(byteUtils.fromHex('c074323031332d30332d32315432303a30343a30305a'), { tags: { 0: dateDecoder } });
35
+ assert.equal(byteUtils.toHex(encode.encode(new Date('2013-03-21T20:04:00Z'), { typeEncoders: { Date: common.dateEncoder } })), 'c074323031332d30332d32315432303a30343a30305a');
36
+ const decodedDate = decode.decode(byteUtils.fromHex('c074323031332d30332d32315432303a30343a30305a'), { tags: { 0: common.dateDecoder } });
51
37
  assert.instanceOf(decodedDate, Date);
52
38
  assert.equal(decodedDate.toISOString(), new Date('2013-03-21T20:04:00Z').toISOString());
53
39
  });
@@ -69,12 +55,12 @@ describe('tag', () => {
69
55
  const verify = (hex, strict) => {
70
56
  if (!strict) {
71
57
  assert.throws(() => decode.decode(byteUtils.fromHex(hex), {
72
- tags: { 8: dateDecoder },
58
+ tags: { 8: common.dateDecoder },
73
59
  strict: true
74
60
  }), /integer encoded in more bytes than necessary/);
75
61
  }
76
62
  const decodedDate = decode.decode(byteUtils.fromHex(hex), {
77
- tags: { 8: dateDecoder },
63
+ tags: { 8: common.dateDecoder },
78
64
  strict
79
65
  });
80
66
  assert.instanceOf(decodedDate, Date);
@@ -0,0 +1,63 @@
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
+ });
@@ -0,0 +1,19 @@
1
+ import {
2
+ Token,
3
+ Type
4
+ } from '../lib/token.js';
5
+ export function dateDecoder(obj) {
6
+ if (typeof obj !== 'string') {
7
+ throw new Error('expected string for tag 1');
8
+ }
9
+ return new Date(obj);
10
+ }
11
+ export function dateEncoder(obj) {
12
+ if (!(obj instanceof Date)) {
13
+ throw new Error('expected Date for "Date" encoder');
14
+ }
15
+ return [
16
+ new Token(Type.tag, 0),
17
+ new Token(Type.string, obj.toISOString().replace(/\.000Z$/, 'Z'))
18
+ ];
19
+ }
@@ -4,7 +4,6 @@ import process from 'process';
4
4
  import path from 'path';
5
5
  import { platform } from 'os';
6
6
  import { fileURLToPath } from 'url';
7
- import bin from '../lib/bin.js';
8
7
  const {assert} = chai;
9
8
  const fixture1JsonString = '{"a":1,"b":[2,3],"smile":"\uD83D\uDE00"}';
10
9
  const fixture1JsonPrettyString = `{
@@ -0,0 +1 @@
1
+ import bin from '../lib/bin.js';
@@ -11,22 +11,11 @@ import {
11
11
  fromHex,
12
12
  toHex
13
13
  } from '../lib/byte-utils.js';
14
+ import {
15
+ dateDecoder,
16
+ dateEncoder
17
+ } from './common.js';
14
18
  const {assert} = chai;
15
- function dateDecoder(obj) {
16
- if (typeof obj !== 'string') {
17
- throw new Error('expected string for tag 1');
18
- }
19
- return new Date(obj);
20
- }
21
- function dateEncoder(obj) {
22
- if (!(obj instanceof Date)) {
23
- throw new Error('expected Date for "Date" encoder');
24
- }
25
- return [
26
- new Token(Type.tag, 0),
27
- new Token(Type.string, obj.toISOString().replace(/\.000Z$/, 'Z'))
28
- ];
29
- }
30
19
  function Uint16ArrayDecoder(obj) {
31
20
  if (typeof obj !== 'string') {
32
21
  throw new Error('expected string for tag 23');
@@ -0,0 +1,55 @@
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/lib/4array.js CHANGED
@@ -35,4 +35,7 @@ export function decodeArrayIndefinite(data, pos, _minor, options) {
35
35
  export function encodeArray(buf, token) {
36
36
  uint.encodeUintValue(buf, Type.array.majorEncoded, token.value);
37
37
  }
38
- encodeArray.compareTokens = uint.encodeUint.compareTokens;
38
+ encodeArray.compareTokens = uint.encodeUint.compareTokens;
39
+ encodeArray.encodedSize = function encodedSize(token) {
40
+ return uint.encodeUintValue.encodedSize(token.value);
41
+ };
package/esm/lib/5map.js CHANGED
@@ -35,4 +35,7 @@ export function decodeMapIndefinite(data, pos, _minor, options) {
35
35
  export function encodeMap(buf, token) {
36
36
  uint.encodeUintValue(buf, Type.map.majorEncoded, token.value);
37
37
  }
38
- encodeMap.compareTokens = uint.encodeUint.compareTokens;
38
+ encodeMap.compareTokens = uint.encodeUint.compareTokens;
39
+ encodeMap.encodedSize = function encodedSize(token) {
40
+ return uint.encodeUintValue.encodedSize(token.value);
41
+ };
package/esm/lib/6tag.js CHANGED
@@ -21,4 +21,7 @@ export function decodeTag64(data, pos, _minor, options) {
21
21
  export function encodeTag(buf, token) {
22
22
  uint.encodeUintValue(buf, Type.tag.majorEncoded, token.value);
23
23
  }
24
- encodeTag.compareTokens = uint.encodeUint.compareTokens;
24
+ encodeTag.compareTokens = uint.encodeUint.compareTokens;
25
+ encodeTag.encodedSize = function encodedSize(token) {
26
+ return uint.encodeUintValue.encodedSize(token.value);
27
+ };
package/esm/lib/7float.js CHANGED
@@ -85,10 +85,9 @@ encodeFloat.encodedSize = function encodedSize(token, options) {
85
85
  if (float === false || float === true || float === null || float === undefined) {
86
86
  return 1;
87
87
  }
88
- let decoded;
89
88
  if (!options || options.float64 !== true) {
90
89
  encodeFloat16(float);
91
- decoded = readFloat16(ui8a, 1);
90
+ let decoded = readFloat16(ui8a, 1);
92
91
  if (float === decoded || Number.isNaN(float)) {
93
92
  return 3;
94
93
  }
package/esm/lib/bl.js CHANGED
@@ -13,9 +13,11 @@ export class Bl {
13
13
  this._initReuseChunk = null;
14
14
  }
15
15
  reset() {
16
- this.chunks = [];
17
16
  this.cursor = 0;
18
17
  this.maxCursor = -1;
18
+ if (this.chunks.length) {
19
+ this.chunks = [];
20
+ }
19
21
  if (this._initReuseChunk !== null) {
20
22
  this.chunks.push(this._initReuseChunk);
21
23
  this.maxCursor = this._initReuseChunk.length - 1;