lib0 0.2.47 → 0.2.50

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 (65) hide show
  1. package/README.md +2 -1
  2. package/broadcastchannel.d.ts +3 -3
  3. package/broadcastchannel.d.ts.map +1 -1
  4. package/broadcastchannel.js +5 -4
  5. package/decoding.js +2 -2
  6. package/dist/{broadcastchannel-7da37795.cjs → broadcastchannel-6da71c2f.cjs} +7 -6
  7. package/dist/broadcastchannel-6da71c2f.cjs.map +1 -0
  8. package/dist/broadcastchannel.cjs +2 -2
  9. package/dist/broadcastchannel.d.ts +3 -3
  10. package/dist/broadcastchannel.d.ts.map +1 -1
  11. package/dist/{buffer-b0dea3b0.cjs → buffer-c98f67d5.cjs} +4 -4
  12. package/dist/buffer-c98f67d5.cjs.map +1 -0
  13. package/dist/buffer.cjs +1 -1
  14. package/dist/decoding.cjs +1 -1
  15. package/dist/encoding.cjs +1 -1
  16. package/dist/encoding.test.d.ts +1 -0
  17. package/dist/encoding.test.d.ts.map +1 -1
  18. package/dist/index.cjs +5 -5
  19. package/dist/{iterator-fe01d209.cjs → iterator-9fc627c1.cjs} +1 -4
  20. package/dist/iterator-9fc627c1.cjs.map +1 -0
  21. package/dist/iterator.cjs +1 -1
  22. package/dist/iterator.d.ts.map +1 -1
  23. package/dist/metric.d.ts.map +1 -1
  24. package/dist/{prng-25602bac.cjs → prng-bbec83e2.cjs} +3 -3
  25. package/dist/prng-bbec83e2.cjs.map +1 -0
  26. package/dist/prng.cjs +2 -2
  27. package/dist/prng.d.ts +1 -1
  28. package/dist/random.cjs +7 -1
  29. package/dist/random.cjs.map +1 -1
  30. package/dist/random.d.ts +1 -0
  31. package/dist/random.d.ts.map +1 -1
  32. package/dist/random.test.d.ts +1 -0
  33. package/dist/random.test.d.ts.map +1 -1
  34. package/dist/test.cjs +61 -15
  35. package/dist/test.cjs.map +1 -1
  36. package/dist/test.js +87 -41
  37. package/dist/test.js.map +1 -1
  38. package/dist/testing.cjs +2 -2
  39. package/dist/{tree-92f764b3.cjs → tree-b67c7947.cjs} +9 -2
  40. package/dist/tree-b67c7947.cjs.map +1 -0
  41. package/dist/tree.cjs +1 -1
  42. package/dist/tree.d.ts +13 -5
  43. package/dist/tree.d.ts.map +1 -1
  44. package/encoding.js +1 -1
  45. package/encoding.test.d.ts +1 -0
  46. package/encoding.test.d.ts.map +1 -1
  47. package/iterator.d.ts.map +1 -1
  48. package/iterator.js +0 -3
  49. package/metric.d.ts.map +1 -1
  50. package/package.json +1 -1
  51. package/prng.d.ts +1 -1
  52. package/prng.js +1 -1
  53. package/random.d.ts +1 -0
  54. package/random.d.ts.map +1 -1
  55. package/random.js +6 -1
  56. package/random.test.d.ts +1 -0
  57. package/random.test.d.ts.map +1 -1
  58. package/tree.d.ts +13 -5
  59. package/tree.d.ts.map +1 -1
  60. package/tree.js +8 -1
  61. package/dist/broadcastchannel-7da37795.cjs.map +0 -1
  62. package/dist/buffer-b0dea3b0.cjs.map +0 -1
  63. package/dist/iterator-fe01d209.cjs.map +0 -1
  64. package/dist/prng-25602bac.cjs.map +0 -1
  65. package/dist/tree-92f764b3.cjs.map +0 -1
package/dist/random.cjs CHANGED
@@ -3,13 +3,18 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var math = require('./math-08e068f9.cjs');
6
+ var binary = require('./binary-ac8e39e2.cjs');
6
7
  var isomorphic_js = require('isomorphic.js');
7
8
 
8
9
  const rand = Math.random;
9
10
 
10
- /* istanbul ignore next */
11
11
  const uint32 = () => new Uint32Array(isomorphic_js.cryptoRandomBuffer(4))[0];
12
12
 
13
+ const uint53 = () => {
14
+ const arr = new Uint32Array(isomorphic_js.cryptoRandomBuffer(8));
15
+ return (arr[0] & binary.BITS21) * (binary.BITS32 + 1) + (arr[1] >>> 0)
16
+ };
17
+
13
18
  /**
14
19
  * @template T
15
20
  * @param {Array<T>} arr
@@ -26,5 +31,6 @@ const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c
26
31
  exports.oneOf = oneOf;
27
32
  exports.rand = rand;
28
33
  exports.uint32 = uint32;
34
+ exports.uint53 = uint53;
29
35
  exports.uuidv4 = uuidv4;
30
36
  //# sourceMappingURL=random.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"random.cjs","sources":["../random.js"],"sourcesContent":["\n/**\n * Isomorphic module for true random numbers / buffers / uuids.\n *\n * Attention: falls back to Math.random if the browser does not support crypto.\n *\n * @module random\n */\n\nimport * as math from './math.js'\nimport { cryptoRandomBuffer } from './isomorphic.js'\n\nexport const rand = Math.random\n\n/* istanbul ignore next */\nexport const uint32 = () => new Uint32Array(cryptoRandomBuffer(4))[0]\n\n/**\n * @template T\n * @param {Array<T>} arr\n * @return {T}\n */\nexport const oneOf = arr => arr[math.floor(rand() * arr.length)]\n\n// @ts-ignore\nconst uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11\nexport const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>\n (c ^ uint32() & 15 >> c / 4).toString(16)\n)\n"],"names":["cryptoRandomBuffer","math.floor"],"mappings":";;;;;;;AAYY,MAAC,IAAI,GAAG,IAAI,CAAC,OAAM;AAC/B;AACA;AACY,MAAC,MAAM,GAAG,MAAM,IAAI,WAAW,CAACA,gCAAkB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAC;AACrE;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,KAAK,GAAG,GAAG,IAAI,GAAG,CAACC,UAAU,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,EAAC;AAChE;AACA;AACA,MAAM,cAAc,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,KAAI;AAC7C,MAAC,MAAM,GAAG,MAAM,cAAc,CAAC,OAAO,CAAC,QAAQ,2BAA2B,CAAC;AACvF,EAAE,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC;AAC3C;;;;;;;"}
1
+ {"version":3,"file":"random.cjs","sources":["../random.js"],"sourcesContent":["\n/**\n * Isomorphic module for true random numbers / buffers / uuids.\n *\n * Attention: falls back to Math.random if the browser does not support crypto.\n *\n * @module random\n */\n\nimport * as math from './math.js'\nimport * as binary from './binary.js'\nimport { cryptoRandomBuffer } from './isomorphic.js'\n\nexport const rand = Math.random\n\nexport const uint32 = () => new Uint32Array(cryptoRandomBuffer(4))[0]\n\nexport const uint53 = () => {\n const arr = new Uint32Array(cryptoRandomBuffer(8))\n return (arr[0] & binary.BITS21) * (binary.BITS32 + 1) + (arr[1] >>> 0)\n}\n\n/**\n * @template T\n * @param {Array<T>} arr\n * @return {T}\n */\nexport const oneOf = arr => arr[math.floor(rand() * arr.length)]\n\n// @ts-ignore\nconst uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11\nexport const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>\n (c ^ uint32() & 15 >> c / 4).toString(16)\n)\n"],"names":["cryptoRandomBuffer","binary.BITS21","binary.BITS32","math.floor"],"mappings":";;;;;;;;AAaY,MAAC,IAAI,GAAG,IAAI,CAAC,OAAM;AAC/B;AACY,MAAC,MAAM,GAAG,MAAM,IAAI,WAAW,CAACA,gCAAkB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAC;AACrE;AACY,MAAC,MAAM,GAAG,MAAM;AAC5B,EAAE,MAAM,GAAG,GAAG,IAAI,WAAW,CAACA,gCAAkB,CAAC,CAAC,CAAC,EAAC;AACpD,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,GAAGC,aAAa,KAAKC,aAAa,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AACxE,EAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,KAAK,GAAG,GAAG,IAAI,GAAG,CAACC,UAAU,CAAC,IAAI,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,EAAC;AAChE;AACA;AACA,MAAM,cAAc,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,KAAI;AAC7C,MAAC,MAAM,GAAG,MAAM,cAAc,CAAC,OAAO,CAAC,QAAQ,2BAA2B,CAAC;AACvF,EAAE,CAAC,CAAC,GAAG,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC;AAC3C;;;;;;;;"}
package/dist/random.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export const rand: () => number;
2
2
  export function uint32(): number;
3
+ export function uint53(): number;
3
4
  export function oneOf<T>(arr: T[]): T;
4
5
  export function uuidv4(): any;
5
6
  //# sourceMappingURL=random.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../random.js"],"names":[],"mappings":"AAYA,gCAA+B;AAGxB,iCAA8D;AAO9D,sCAAyD;AAIzD,8BAEN"}
1
+ {"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../random.js"],"names":[],"mappings":"AAaA,gCAA+B;AAExB,iCAA8D;AAE9D,iCAGN;AAOM,sCAAyD;AAIzD,8BAEN"}
@@ -1,4 +1,5 @@
1
1
  export function testUint32(tc: t.TestCase): void;
2
+ export function testUint53(tc: t.TestCase): void;
2
3
  export function testUuidv4(tc: t.TestCase): void;
3
4
  export function testUuidv4Overlaps(tc: t.TestCase): void;
4
5
  import * as t from "./testing.js";
@@ -1 +1 @@
1
- {"version":3,"file":"random.test.d.ts","sourceRoot":"","sources":["../random.test.js"],"names":[],"mappings":"AASO,+BAFI,EAAE,QAAQ,QA0BpB;AAKM,+BAFI,EAAE,QAAQ,QAIpB;AAKM,uCAFI,EAAE,QAAQ,QAkBpB"}
1
+ {"version":3,"file":"random.test.d.ts","sourceRoot":"","sources":["../random.test.js"],"names":[],"mappings":"AASO,+BAFI,EAAE,QAAQ,QA0BpB;AAKM,+BAFI,EAAE,QAAQ,QAyBpB;AAKM,+BAFI,EAAE,QAAQ,QAIpB;AAKM,uCAFI,EAAE,QAAQ,QAkBpB"}
package/dist/test.cjs CHANGED
@@ -1548,15 +1548,6 @@ const simpleDiffStringWithCursor = (a, b, cursor) => {
1548
1548
  }
1549
1549
  };
1550
1550
 
1551
- /* istanbul ignore next */
1552
- const uint32$1 = () => new Uint32Array(isomorphic_js.cryptoRandomBuffer(4))[0];
1553
-
1554
- // @ts-ignore
1555
- const uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11;
1556
- const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>
1557
- (c ^ uint32$1() & 15 >> c / 4).toString(16)
1558
- );
1559
-
1560
1551
  /* eslint-env browser */
1561
1552
 
1562
1553
  /**
@@ -1717,6 +1708,19 @@ var binary$1 = /*#__PURE__*/Object.freeze({
1717
1708
  BITS32: BITS32
1718
1709
  });
1719
1710
 
1711
+ const uint32$1 = () => new Uint32Array(isomorphic_js.cryptoRandomBuffer(4))[0];
1712
+
1713
+ const uint53$1 = () => {
1714
+ const arr = new Uint32Array(isomorphic_js.cryptoRandomBuffer(8));
1715
+ return (arr[0] & BITS21) * (BITS32 + 1) + (arr[1] >>> 0)
1716
+ };
1717
+
1718
+ // @ts-ignore
1719
+ const uuidv4Template = [1e7] + -1e3 + -4e3 + -8e3 + -1e11;
1720
+ const uuidv4 = () => uuidv4Template.replace(/[018]/g, /** @param {number} c */ c =>
1721
+ (c ^ uint32$1() & 15 >> c / 4).toString(16)
1722
+ );
1723
+
1720
1724
  /**
1721
1725
  * @module prng
1722
1726
  */
@@ -2324,7 +2328,7 @@ const writeAny = (encoder, data) => {
2324
2328
  writeVarString(encoder, data);
2325
2329
  break
2326
2330
  case 'number':
2327
- if (isInteger(data) && data <= BITS31) {
2331
+ if (isInteger(data) && abs(data) <= BITS31) {
2328
2332
  // TYPE 125: INTEGER
2329
2333
  write(encoder, 125);
2330
2334
  writeVarInt(encoder, data);
@@ -2901,7 +2905,7 @@ const readVarUint = decoder => {
2901
2905
  return num >>> 0 // return unsigned number!
2902
2906
  }
2903
2907
  /* istanbul ignore if */
2904
- if (len > 35) {
2908
+ if (len > 53) {
2905
2909
  throw new Error('Integer out of range!')
2906
2910
  }
2907
2911
  }
@@ -2935,7 +2939,7 @@ const readVarInt = decoder => {
2935
2939
  return sign * (num >>> 0)
2936
2940
  }
2937
2941
  /* istanbul ignore if */
2938
- if (len > 41) {
2942
+ if (len > 53) {
2939
2943
  throw new Error('Integer out of range!')
2940
2944
  }
2941
2945
  }
@@ -3389,7 +3393,7 @@ const decodeAny = buf => readAny(createDecoder(buf));
3389
3393
  /**
3390
3394
  * Description of the function
3391
3395
  * @callback generatorNext
3392
- * @return {number} A 32bit integer
3396
+ * @return {number} A random float in the cange of [0,1)
3393
3397
  */
3394
3398
 
3395
3399
  /**
@@ -4580,8 +4584,8 @@ const testStringDecodingPerformance = () => {
4580
4584
  }
4581
4585
  });
4582
4586
  });
4587
+ assert(durationConcatElements < durationConcatElementsNative * 1.3, '1.3x faster. We expect that the native approach is slower. If this fails, our expectantion is not met in your javascript environment. Please report this issue.');
4583
4588
  assert(durationConcatElements < durationSingleElements, 'We expect that the second approach is faster. If this fails, our expectantion is not met in your javascript environment. Please report this issue.');
4584
- assert(durationConcatElements < durationConcatElementsNative * 1.3, 'We expect that the native approach is slower. If this fails, our expectantion is not met in your javascript environment. Please report this issue.');
4585
4589
  };
4586
4590
 
4587
4591
  /**
@@ -5062,6 +5066,18 @@ const testStringDecoder = tc => {
5062
5066
  }
5063
5067
  };
5064
5068
 
5069
+ /**
5070
+ * @param {t.TestCase} tc
5071
+ */
5072
+ const testLargeNumberAnyEncoding = tc => {
5073
+ const encoder = createEncoder();
5074
+ const num = -2.2062063918362897e+50;
5075
+ writeAny(encoder, num);
5076
+ const decoder = createDecoder(toUint8Array(encoder));
5077
+ const readNum = readAny(decoder);
5078
+ assert(readNum === num);
5079
+ };
5080
+
5065
5081
  var encoding = /*#__PURE__*/Object.freeze({
5066
5082
  __proto__: null,
5067
5083
  testGolangBinaryEncodingCompatibility: testGolangBinaryEncodingCompatibility,
@@ -5092,7 +5108,8 @@ var encoding = /*#__PURE__*/Object.freeze({
5092
5108
  testIntDiffRleEncoder: testIntDiffRleEncoder,
5093
5109
  testIntEncoders: testIntEncoders,
5094
5110
  testIntDiffEncoder: testIntDiffEncoder,
5095
- testStringDecoder: testStringDecoder
5111
+ testStringDecoder: testStringDecoder,
5112
+ testLargeNumberAnyEncoding: testLargeNumberAnyEncoding
5096
5113
  });
5097
5114
 
5098
5115
  /**
@@ -6040,6 +6057,34 @@ const testUint32 = tc => {
6040
6057
  assert(((smallest & BITS32) >>> 0) === smallest, 'Smallest number is 32 bits long.');
6041
6058
  };
6042
6059
 
6060
+ /**
6061
+ * @param {t.TestCase} tc
6062
+ */
6063
+ const testUint53 = tc => {
6064
+ const iterations = 10000;
6065
+ let largest = 0;
6066
+ let smallest = MAX_SAFE_INTEGER;
6067
+ let newNum = 0;
6068
+ let lenSum = 0;
6069
+ let ones = 0;
6070
+ for (let i = 0; i < iterations; i++) {
6071
+ newNum = uint53$1();
6072
+ lenSum += newNum.toString().length;
6073
+ ones += newNum.toString(2).split('').filter(x => x === '1').length;
6074
+ if (newNum > largest) {
6075
+ largest = newNum;
6076
+ }
6077
+ if (newNum < smallest) {
6078
+ smallest = newNum;
6079
+ }
6080
+ }
6081
+ info(`Largest number generated is ${largest}`);
6082
+ info(`Smallest number generated is ${smallest}`);
6083
+ info(`Average decimal length of number is ${lenSum / iterations}`);
6084
+ info(`Average number of 1s in number is ${ones / iterations} (expecting ~26.5)`);
6085
+ assert(largest > MAX_SAFE_INTEGER * 0.9);
6086
+ };
6087
+
6043
6088
  /**
6044
6089
  * @param {t.TestCase} tc
6045
6090
  */
@@ -6071,6 +6116,7 @@ const testUuidv4Overlaps = tc => {
6071
6116
  var random = /*#__PURE__*/Object.freeze({
6072
6117
  __proto__: null,
6073
6118
  testUint32: testUint32,
6119
+ testUint53: testUint53,
6074
6120
  testUuidv4: testUuidv4,
6075
6121
  testUuidv4Overlaps: testUuidv4Overlaps
6076
6122
  });