lib0 0.2.49 → 0.2.52

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 (78) hide show
  1. package/.github/workflows/node.js.yml +1 -1
  2. package/README.md +23 -23
  3. package/broadcastchannel.d.ts +4 -4
  4. package/broadcastchannel.d.ts.map +1 -1
  5. package/broadcastchannel.js +7 -6
  6. package/decoding.d.ts +2 -0
  7. package/decoding.d.ts.map +1 -1
  8. package/decoding.js +39 -14
  9. package/dist/{broadcastchannel-1ac6c212.cjs → broadcastchannel-7655b66f.cjs} +9 -8
  10. package/dist/broadcastchannel-7655b66f.cjs.map +1 -0
  11. package/dist/broadcastchannel.cjs +2 -2
  12. package/dist/broadcastchannel.d.ts +4 -4
  13. package/dist/broadcastchannel.d.ts.map +1 -1
  14. package/dist/{buffer-c98f67d5.cjs → buffer-d3ba308b.cjs} +92 -28
  15. package/dist/buffer-d3ba308b.cjs.map +1 -0
  16. package/dist/buffer.cjs +1 -1
  17. package/dist/cache.cjs +3 -0
  18. package/dist/cache.cjs.map +1 -1
  19. package/dist/decoding.cjs +4 -2
  20. package/dist/decoding.cjs.map +1 -1
  21. package/dist/decoding.d.ts +2 -0
  22. package/dist/decoding.d.ts.map +1 -1
  23. package/dist/encoding.cjs +4 -1
  24. package/dist/encoding.cjs.map +1 -1
  25. package/dist/encoding.d.ts +3 -0
  26. package/dist/encoding.d.ts.map +1 -1
  27. package/dist/encoding.test.d.ts +6 -1
  28. package/dist/encoding.test.d.ts.map +1 -1
  29. package/dist/index.cjs +3 -3
  30. package/dist/list.cjs +52 -1
  31. package/dist/list.cjs.map +1 -1
  32. package/dist/list.d.ts +5 -0
  33. package/dist/list.d.ts.map +1 -1
  34. package/dist/list.test.d.ts.map +1 -1
  35. package/dist/number.test.d.ts +1 -0
  36. package/dist/number.test.d.ts.map +1 -1
  37. package/dist/{prng-1aa0a1bc.cjs → prng-76a2f4be.cjs} +3 -3
  38. package/dist/prng-76a2f4be.cjs.map +1 -0
  39. package/dist/prng.cjs +2 -2
  40. package/dist/prng.d.ts +1 -1
  41. package/dist/random.cjs +7 -1
  42. package/dist/random.cjs.map +1 -1
  43. package/dist/random.d.ts +1 -0
  44. package/dist/random.d.ts.map +1 -1
  45. package/dist/random.test.d.ts +1 -0
  46. package/dist/random.test.d.ts.map +1 -1
  47. package/dist/testing.cjs +5 -5
  48. package/dist/testing.cjs.map +1 -1
  49. package/encoding.d.ts +3 -0
  50. package/encoding.d.ts.map +1 -1
  51. package/encoding.js +46 -14
  52. package/encoding.test.d.ts +6 -1
  53. package/encoding.test.d.ts.map +1 -1
  54. package/list.d.ts +5 -0
  55. package/list.d.ts.map +1 -1
  56. package/list.js +46 -1
  57. package/list.test.d.ts.map +1 -1
  58. package/number.test.d.ts +1 -0
  59. package/number.test.d.ts.map +1 -1
  60. package/package.json +2 -2
  61. package/prng.d.ts +1 -1
  62. package/prng.js +1 -1
  63. package/random.d.ts +1 -0
  64. package/random.d.ts.map +1 -1
  65. package/random.js +6 -1
  66. package/random.test.d.ts +1 -0
  67. package/random.test.d.ts.map +1 -1
  68. package/testing.js +3 -3
  69. package/.circleci/config.yml +0 -7
  70. package/dist/broadcastchannel-1ac6c212.cjs.map +0 -1
  71. package/dist/buffer-c98f67d5.cjs.map +0 -1
  72. package/dist/prng-1aa0a1bc.cjs.map +0 -1
  73. package/dist/test.cjs +0 -7604
  74. package/dist/test.cjs.map +0 -1
  75. package/dist/test.d.ts +0 -2
  76. package/dist/test.d.ts.map +0 -1
  77. package/dist/test.js +0 -7631
  78. package/dist/test.js.map +0 -1
@@ -16,7 +16,7 @@ jobs:
16
16
 
17
17
  strategy:
18
18
  matrix:
19
- node-version: [12.x, 14.x]
19
+ node-version: [18.x]
20
20
 
21
21
  steps:
22
22
  - uses: actions/checkout@v2
package/README.md CHANGED
@@ -12,7 +12,7 @@ export const stringify = JSON.stringify
12
12
  export const parse = JSON.parse
13
13
 
14
14
  // index.js
15
- import * as json from 'lib0/json.js'
15
+ import * as json from 'lib0/json'
16
16
  export const f = (arg1, arg2) => json.stringify(arg1) + json.stringify(arg2)
17
17
 
18
18
  // compiled with rollup and uglifyjs:
@@ -177,11 +177,11 @@ broadcastchannel.publish('my events', 'Hello world!') // => A: 'Hello world!' fi
177
177
  broadcastchannel.publish('my events', 'hello from tab B') // => A: 'hello from tab B'
178
178
  </code></pre>
179
179
  <dl>
180
- <b><code>broadcastchannel.subscribe(room: string, f: function(any):any)</code></b><br>
180
+ <b><code>broadcastchannel.subscribe(room: string, f: function(any, any):any)</code></b><br>
181
181
  <dd><p>Subscribe to global <code>publish</code> events.</p></dd>
182
- <b><code>broadcastchannel.unsubscribe(room: string, f: function(any):any)</code></b><br>
182
+ <b><code>broadcastchannel.unsubscribe(room: string, f: function(any, any):any)</code></b><br>
183
183
  <dd><p>Unsubscribe from <code>publish</code> global events.</p></dd>
184
- <b><code>broadcastchannel.publish(room: string, data: any)</code></b><br>
184
+ <b><code>broadcastchannel.publish(room: string, data: any, origin: any)</code></b><br>
185
185
  <dd><p>Publish data to all subscribers (including subscribers on this tab)</p></dd>
186
186
  </dl>
187
187
  </details>
@@ -321,15 +321,7 @@ to the next byte and read it as unsigned integer.</p></dd>
321
321
  <dd><p>Look ahead and read varUint without incrementing position</p></dd>
322
322
  <b><code>decoding.peekVarInt(decoder: module:decoding.Decoder): number</code></b><br>
323
323
  <dd><p>Look ahead and read varUint without incrementing position</p></dd>
324
- <b><code>decoding.readVarString(decoder: module:decoding.Decoder): String</code></b><br>
325
- <dd><p>Read string of variable length</p>
326
- <ul>
327
- <li>varUint is used to store the length of the string</li>
328
- </ul>
329
- <p>Transforming utf8 to a string is pretty expensive. The code performs 10x better
330
- when String.fromCodePoint is fed with all characters as arguments.
331
- But most environments have a maximum number of arguments per functions.
332
- For effiency reasons we apply a maximum of 10000 characters at once.</p></dd>
324
+ <b><code>decoding.readVarString</code></b><br>
333
325
  <b><code>decoding.peekVarString(decoder: module:decoding.Decoder): string</code></b><br>
334
326
  <dd><p>Look ahead and read varString without incrementing position</p></dd>
335
327
  <b><code>decoding.readFromDataView(decoder: module:decoding.Decoder, len: number): DataView</code></b><br>
@@ -480,6 +472,9 @@ decoding.hasContent(decoder) // => false - all data is read
480
472
  <dd><p>The current length of the encoded data.</p></dd>
481
473
  <b><code>encoding.toUint8Array(encoder: module:encoding.Encoder): Uint8Array</code></b><br>
482
474
  <dd><p>Transform to Uint8Array.</p></dd>
475
+ <b><code>encoding.verifyLen(encoder: module:encoding.Encoder, len: number)</code></b><br>
476
+ <dd><p>Verify that it is possible to write <code>len</code> bytes wtihout checking. If
477
+ necessary, a new Buffer with the required length is attached.</p></dd>
483
478
  <b><code>encoding.write(encoder: module:encoding.Encoder, num: number)</code></b><br>
484
479
  <dd><p>Write one byte to the encoder.</p></dd>
485
480
  <b><code>encoding.set(encoder: module:encoding.Encoder, pos: number, num: number)</code></b><br>
@@ -501,16 +496,11 @@ Position must already be written (i.e. encoder.length &gt; pos)</p></dd>
501
496
  <b><code>encoding.setUint32(encoder: module:encoding.Encoder, pos: number, num: number)</code></b><br>
502
497
  <dd><p>Write two bytes as an unsigned integer at a specific location.</p></dd>
503
498
  <b><code>encoding.writeVarUint(encoder: module:encoding.Encoder, num: number)</code></b><br>
504
- <dd><p>Write a variable length unsigned integer.</p>
505
- <p>Encodes integers in the range from [0, 4294967295] / [0, 0xffffffff]. (max 32 bit unsigned integer)</p></dd>
499
+ <dd><p>Write a variable length unsigned integer. Max encodable integer is 2^53.</p></dd>
506
500
  <b><code>encoding.writeVarInt(encoder: module:encoding.Encoder, num: number)</code></b><br>
507
501
  <dd><p>Write a variable length integer.</p>
508
- <p>Encodes integers in the range from [-2147483648, -2147483647].</p>
509
- <p>We don't use zig-zag encoding because we want to keep the option open
510
- to use the same function for BigInt and 53bit integers (doubles).</p>
511
502
  <p>We use the 7th bit instead for signaling that this is a negative number.</p></dd>
512
- <b><code>encoding.writeVarString(encoder: module:encoding.Encoder, str: String)</code></b><br>
513
- <dd><p>Write a variable length string.</p></dd>
503
+ <b><code>encoding.writeVarString</code></b><br>
514
504
  <b><code>encoding.writeBinaryEncoder(encoder: module:encoding.Encoder, append: module:encoding.Encoder)</code></b><br>
515
505
  <dd><p>Write the content of another Encoder.</p></dd>
516
506
  <b><code>encoding.writeUint8Array(encoder: module:encoding.Encoder, uint8Array: Uint8Array)</code></b><br>
@@ -819,11 +809,16 @@ In practice, when decoding several million small strings, the GC will kick in mo
819
809
  <b><code>()</code></b><br>
820
810
  <b><code>(queue: module:list.List&lt;N&gt;)</code></b><br>
821
811
  <b><code>()</code></b><br>
822
- <b><code>ode(queue: module:list.List&lt;N&gt;, node: N)</code></b><br>
812
+ <b><code>(queue: module:list.List&lt;N&gt;, node: N)</code></b><br>
823
813
  <dd><p>Remove a single node from the queue. Only works with Queues that operate on Doubly-linked lists of nodes.</p></dd>
814
+ <b><code>()</code></b><br>
815
+ <b><code>ode</code></b><br>
824
816
  <b><code>ode()</code></b><br>
825
817
  <b><code>etween(queue: module:list.List&lt;N&gt;, left: N| null, right: N| null, node: N)</code></b><br>
826
818
  <b><code>etween()</code></b><br>
819
+ <b><code>(queue: module:list.List&lt;N&gt;, node: N, newNode: N)</code></b><br>
820
+ <dd><p>Remove a single node from the queue. Only works with Queues that operate on Doubly-linked lists of nodes.</p></dd>
821
+ <b><code>()</code></b><br>
827
822
  <b><code>(queue: module:list.List&lt;N&gt;, n: N)</code></b><br>
828
823
  <b><code>()</code></b><br>
829
824
  <b><code>nt(queue: module:list.List&lt;N&gt;, n: N)</code></b><br>
@@ -834,6 +829,10 @@ In practice, when decoding several million small strings, the GC will kick in mo
834
829
  <b><code>()</code></b><br>
835
830
  <b><code>(list: module:list.List&lt;N&gt;, f: function(N):M): Array&lt;M&gt;</code></b><br>
836
831
  <b><code>()</code></b><br>
832
+ <b><code>(list: module:list.List&lt;N&gt;)</code></b><br>
833
+ <b><code>()</code></b><br>
834
+ <b><code>(list: module:list.List&lt;N&gt;, f: function(N):M)</code></b><br>
835
+ <b><code>()</code></b><br>
837
836
  </dl>
838
837
  </details>
839
838
  <details><summary><b>[lib0/logging]</b> Isomorphic logging module with support for colors!</summary>
@@ -1098,6 +1097,7 @@ library has some insane environment where global Promise objects are overwritten
1098
1097
  <dl>
1099
1098
  <b><code>random.rand</code></b><br>
1100
1099
  <b><code>random.uint32</code></b><br>
1100
+ <b><code>random.uint53</code></b><br>
1101
1101
  <b><code>random.oneOf(arr: Array&lt;T&gt;): T</code></b><br>
1102
1102
  <b><code>random.uuidv4</code></b><br>
1103
1103
  </dl>
@@ -1170,8 +1170,8 @@ integrate this algorithm.</p>
1170
1170
  <pre>import * as testing from 'lib0/testing'</pre>
1171
1171
 
1172
1172
  <pre class="prettyprint source lang-js"><code>// test.js template for creating a test executable
1173
- import { runTests } from 'lib0/testing.js'
1174
- import * as log from 'lib0/logging.js'
1173
+ import { runTests } from 'lib0/testing'
1174
+ import * as log from 'lib0/logging'
1175
1175
  import * as mod1 from './mod1.test.js'
1176
1176
  import * as mod2 from './mod2.test.js'
1177
1177
  import { isBrowser, isNode } from 'lib0/environment.js'
@@ -1,8 +1,8 @@
1
- export function subscribe(room: string, f: (arg0: any) => any): Set<Function>;
2
- export function unsubscribe(room: string, f: (arg0: any) => any): boolean;
3
- export function publish(room: string, data: any): void;
1
+ export function subscribe(room: string, f: (arg0: any, arg1: any) => any): Set<(arg0: any, arg1: any) => any>;
2
+ export function unsubscribe(room: string, f: (arg0: any, arg1: any) => any): boolean;
3
+ export function publish(room: string, data: any, origin?: any): void;
4
4
  export type Channel = {
5
- subs: Set<Function>;
5
+ subs: Set<(arg0: any, arg1: any) => any>;
6
6
  bc: any;
7
7
  };
8
8
  //# sourceMappingURL=broadcastchannel.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"broadcastchannel.d.ts","sourceRoot":"","sources":["broadcastchannel.js"],"names":[],"mappings":"AAkFO,gCAHI,MAAM,YACG,GAAG,KAAE,GAAG,iBAEsC;AAS3D,kCAHI,MAAM,YACG,GAAG,KAAE,GAAG,WAE2C;AAShE,8BAHI,MAAM,QACN,GAAG,QAMb;;UA/Ea,aAAa;QACb,GAAG"}
1
+ {"version":3,"file":"broadcastchannel.d.ts","sourceRoot":"","sources":["broadcastchannel.js"],"names":[],"mappings":"AAkFO,gCAHI,MAAM,YACG,GAAG,QAAE,GAAG,KAAE,GAAG,cAvDN,GAAG,QAAE,GAAG,KAAE,GAAG,EAyD0B;AAS3D,kCAHI,MAAM,YACG,GAAG,QAAE,GAAG,KAAE,GAAG,WAEsC;AAUhE,8BAJI,MAAM,QACN,GAAG,WACH,GAAG,QAMb;;UAhFa,WAAa,GAAG,QAAE,GAAG,KAAE,GAAG,CAAC;QAC3B,GAAG"}
@@ -23,7 +23,7 @@ import * as storage from './storage.js'
23
23
 
24
24
  /**
25
25
  * @typedef {Object} Channel
26
- * @property {Set<Function>} Channel.subs
26
+ * @property {Set<function(any, any):any>} Channel.subs
27
27
  * @property {any} Channel.bc
28
28
  */
29
29
 
@@ -67,7 +67,7 @@ const getChannel = room =>
67
67
  /**
68
68
  * @param {{data:ArrayBuffer}} e
69
69
  */
70
- bc.onmessage = e => subs.forEach(sub => sub(e.data))
70
+ bc.onmessage = e => subs.forEach(sub => sub(e.data, 'broadcastchannel'))
71
71
  return {
72
72
  bc, subs
73
73
  }
@@ -78,7 +78,7 @@ const getChannel = room =>
78
78
  *
79
79
  * @function
80
80
  * @param {string} room
81
- * @param {function(any):any} f
81
+ * @param {function(any, any):any} f
82
82
  */
83
83
  export const subscribe = (room, f) => getChannel(room).subs.add(f)
84
84
 
@@ -87,7 +87,7 @@ export const subscribe = (room, f) => getChannel(room).subs.add(f)
87
87
  *
88
88
  * @function
89
89
  * @param {string} room
90
- * @param {function(any):any} f
90
+ * @param {function(any, any):any} f
91
91
  */
92
92
  export const unsubscribe = (room, f) => getChannel(room).subs.delete(f)
93
93
 
@@ -97,9 +97,10 @@ export const unsubscribe = (room, f) => getChannel(room).subs.delete(f)
97
97
  * @function
98
98
  * @param {string} room
99
99
  * @param {any} data
100
+ * @param {any} [origin]
100
101
  */
101
- export const publish = (room, data) => {
102
+ export const publish = (room, data, origin = null) => {
102
103
  const c = getChannel(room)
103
104
  c.bc.postMessage(data)
104
- c.subs.forEach(sub => sub(data))
105
+ c.subs.forEach(sub => sub(data, origin))
105
106
  }
package/decoding.d.ts CHANGED
@@ -37,6 +37,8 @@ export function readVarUint(decoder: Decoder): number;
37
37
  export function readVarInt(decoder: Decoder): number;
38
38
  export function peekVarUint(decoder: Decoder): number;
39
39
  export function peekVarInt(decoder: Decoder): number;
40
+ export function _readVarStringPolyfill(decoder: Decoder): string;
41
+ export function _readVarStringNative(decoder: Decoder): string;
40
42
  export function readVarString(decoder: Decoder): string;
41
43
  export function peekVarString(decoder: Decoder): string;
42
44
  export function readFromDataView(decoder: Decoder, len: number): DataView;
package/decoding.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"decoding.d.ts","sourceRoot":"","sources":["decoding.js"],"names":[],"mappings":"AAgCA;;GAEG;AACH;IACE;;OAEG;IACH,wBAFW,UAAU,EAepB;IAZC;;;;OAIG;IACH,KAFU,UAAU,CAEC;IACrB;;;;OAIG;IACH,KAFU,MAAM,CAEJ;CAEf;AAOM,0CAHI,UAAU,GACT,OAAO,CAE+C;AAO3D,oCAHI,OAAO,GACN,OAAO,CAEoD;AAWhE,+BAJI,OAAO,gCAEN,OAAO,CAMlB;AAaM,wCAJI,OAAO,OACP,MAAM,GACL,UAAU,CAMrB;AAYM,2CAHI,OAAO,GACN,UAAU,CAEmE;AAQlF,8CAHI,OAAO,GACN,UAAU,CAEkF;AAQjG,+BAHI,OAAO,GACN,MAAM,CAE2B;AAQtC,mCAHI,OAAO,GACN,MAAM,CAE4C;AASvD,oCAHI,OAAO,GACN,MAAM,CAQjB;AASM,oCAHI,OAAO,GACN,MAAM,CAUjB;AAUM,6CAHI,OAAO,GACN,MAAM,CAUjB;AAUM,mCAHI,OAAO,GACN,MAAM,CAE0C;AA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AO,UAEqE;AAKhF,sCAFI,OAAO,OAE4F;AAKvG,uCAFI,OAAO,OAE8F;AAyCzG,iCAFI,OAAO,OAEqE;AAEvF;;;;GAIG;AACH;IACE;;;OAGG;IACH,wBAHW,UAAU,iBACD,OAAO,KAAE,CAAC,EAc7B;IAVC;;OAEG;IACH,eAPkB,OAAO,KAAE,CAAC,CAOR;IACpB;;;OAGG;IACH,GAFU,CAAC,GAAC,IAAI,CAEH;IACb,cAAc;IAGhB,UAWC;CACF;AAED;IACE;;;OAGG;IACH,wBAHW,UAAU,SACV,MAAM,EAShB;IALC;;;OAGG;IACH,GAFU,MAAM,CAEF;IAGhB;;OAEG;IACH,QAFY,MAAM,CAKjB;CACF;AAED;IACE;;;OAGG;IACH,wBAHW,UAAU,SACV,MAAM,EAUhB;IANC;;;OAGG;IACH,GAFU,MAAM,CAEF;IACd,cAAc;IAGhB;;OAEG;IACH,QAFY,MAAM,CAajB;CACF;AAED;IAMI;;OAEG;IACH,GAFU,MAAM,CAEN;IACV,cAAc;IAGhB,eAaC;CACF;AAED;IAMI;;OAEG;IACH,GAFU,MAAM,CAEN;IACV,cAAc;IAGhB,eAaC;CACF;AAED;IAMI;;OAEG;IACH,GAFU,MAAM,CAEN;IACV,cAAc;IACd,aAAa;IAGf;;OAEG;IACH,QAFY,MAAM,CAgBjB;CACF;AAED;IACE;;OAEG;IACH,wBAFW,UAAU,EASpB;IANC,2BAAgD;IAChD,YAAsC;IACtC;;OAEG;IACH,MAFU,MAAM,CAEH;IAGf;;OAEG;IACH,QAFY,MAAM,CAOjB;CACF"}
package/decoding.js CHANGED
@@ -29,6 +29,8 @@
29
29
  import * as buffer from './buffer.js'
30
30
  import * as binary from './binary.js'
31
31
  import * as math from './math.js'
32
+ import * as number from './number.js'
33
+ import * as string from './string.js'
32
34
 
33
35
  /**
34
36
  * A Decoder handles the decoding of an Uint8Array.
@@ -234,16 +236,17 @@ export const peekUint32 = decoder => (
234
236
  */
235
237
  export const readVarUint = decoder => {
236
238
  let num = 0
237
- let len = 0
239
+ let mult = 1
238
240
  while (true) {
239
241
  const r = decoder.arr[decoder.pos++]
240
- num = num | ((r & binary.BITS7) << len)
241
- len += 7
242
+ // num = num | ((r & binary.BITS7) << len)
243
+ num = num + (r & binary.BITS7) * mult // shift $r << (7*#iterations) and add it to num
244
+ mult *= 128 // next iteration, shift 7 "more" to the left
242
245
  if (r < binary.BIT8) {
243
- return num >>> 0 // return unsigned number!
246
+ return num
244
247
  }
245
248
  /* istanbul ignore if */
246
- if (len > 53) {
249
+ if (num > number.MAX_SAFE_INTEGER) {
247
250
  throw new Error('Integer out of range!')
248
251
  }
249
252
  }
@@ -263,7 +266,7 @@ export const readVarUint = decoder => {
263
266
  export const readVarInt = decoder => {
264
267
  let r = decoder.arr[decoder.pos++]
265
268
  let num = r & binary.BITS6
266
- let len = 6
269
+ let mult = 64
267
270
  const sign = (r & binary.BIT7) > 0 ? -1 : 1
268
271
  if ((r & binary.BIT8) === 0) {
269
272
  // don't continue reading
@@ -271,13 +274,14 @@ export const readVarInt = decoder => {
271
274
  }
272
275
  while (true) {
273
276
  r = decoder.arr[decoder.pos++]
274
- num = num | ((r & binary.BITS7) << len)
275
- len += 7
277
+ // num = num | ((r & binary.BITS7) << len)
278
+ num = num + (r & binary.BITS7) * mult
279
+ mult *= 128
276
280
  if (r < binary.BIT8) {
277
- return sign * (num >>> 0)
281
+ return sign * num
278
282
  }
279
283
  /* istanbul ignore if */
280
- if (len > 53) {
284
+ if (num > number.MAX_SAFE_INTEGER) {
281
285
  throw new Error('Integer out of range!')
282
286
  }
283
287
  }
@@ -312,8 +316,8 @@ export const peekVarInt = decoder => {
312
316
  }
313
317
 
314
318
  /**
315
- * Read string of variable length
316
- * * varUint is used to store the length of the string
319
+ * We don't test this function anymore as we use native decoding/encoding by default now.
320
+ * Better not modify this anymore..
317
321
  *
318
322
  * Transforming utf8 to a string is pretty expensive. The code performs 10x better
319
323
  * when String.fromCodePoint is fed with all characters as arguments.
@@ -324,7 +328,8 @@ export const peekVarInt = decoder => {
324
328
  * @param {Decoder} decoder
325
329
  * @return {String} The read String.
326
330
  */
327
- export const readVarString = decoder => {
331
+ /* istanbul ignore next */
332
+ export const _readVarStringPolyfill = decoder => {
328
333
  let remainingLen = readVarUint(decoder)
329
334
  if (remainingLen === 0) {
330
335
  return ''
@@ -349,6 +354,26 @@ export const readVarString = decoder => {
349
354
  }
350
355
  }
351
356
 
357
+ /**
358
+ * @function
359
+ * @param {Decoder} decoder
360
+ * @return {String} The read String
361
+ */
362
+ export const _readVarStringNative = decoder =>
363
+ /** @type any */ (string.utf8TextDecoder).decode(readVarUint8Array(decoder))
364
+
365
+ /**
366
+ * Read string of variable length
367
+ * * varUint is used to store the length of the string
368
+ *
369
+ * @function
370
+ * @param {Decoder} decoder
371
+ * @return {String} The read String
372
+ *
373
+ */
374
+ /* istanbul ignore next */
375
+ export const readVarString = string.utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill
376
+
352
377
  /**
353
378
  * Look ahead and read varString without incrementing position
354
379
  *
@@ -608,7 +633,7 @@ export class IntDiffOptRleDecoder extends Decoder {
608
633
  const diff = readVarInt(this)
609
634
  // if the first bit is set, we read more data
610
635
  const hasCount = diff & 1
611
- this.diff = diff >> 1
636
+ this.diff = math.floor(diff / 2) // shift >> 1
612
637
  this.count = 1
613
638
  if (hasCount) {
614
639
  this.count = readVarUint(this) + 2
@@ -1,14 +1,14 @@
1
1
  'use strict';
2
2
 
3
3
  var map = require('./map-28a001c9.cjs');
4
- var encoding = require('./buffer-c98f67d5.cjs');
4
+ var encoding = require('./buffer-d3ba308b.cjs');
5
5
  var storage = require('./storage.cjs');
6
6
 
7
7
  /* eslint-env browser */
8
8
 
9
9
  /**
10
10
  * @typedef {Object} Channel
11
- * @property {Set<Function>} Channel.subs
11
+ * @property {Set<function(any, any):any>} Channel.subs
12
12
  * @property {any} Channel.bc
13
13
  */
14
14
 
@@ -52,7 +52,7 @@ const getChannel = room =>
52
52
  /**
53
53
  * @param {{data:ArrayBuffer}} e
54
54
  */
55
- bc.onmessage = e => subs.forEach(sub => sub(e.data));
55
+ bc.onmessage = e => subs.forEach(sub => sub(e.data, 'broadcastchannel'));
56
56
  return {
57
57
  bc, subs
58
58
  }
@@ -63,7 +63,7 @@ const getChannel = room =>
63
63
  *
64
64
  * @function
65
65
  * @param {string} room
66
- * @param {function(any):any} f
66
+ * @param {function(any, any):any} f
67
67
  */
68
68
  const subscribe = (room, f) => getChannel(room).subs.add(f);
69
69
 
@@ -72,7 +72,7 @@ const subscribe = (room, f) => getChannel(room).subs.add(f);
72
72
  *
73
73
  * @function
74
74
  * @param {string} room
75
- * @param {function(any):any} f
75
+ * @param {function(any, any):any} f
76
76
  */
77
77
  const unsubscribe = (room, f) => getChannel(room).subs.delete(f);
78
78
 
@@ -82,11 +82,12 @@ const unsubscribe = (room, f) => getChannel(room).subs.delete(f);
82
82
  * @function
83
83
  * @param {string} room
84
84
  * @param {any} data
85
+ * @param {any} [origin]
85
86
  */
86
- const publish = (room, data) => {
87
+ const publish = (room, data, origin = null) => {
87
88
  const c = getChannel(room);
88
89
  c.bc.postMessage(data);
89
- c.subs.forEach(sub => sub(data));
90
+ c.subs.forEach(sub => sub(data, origin));
90
91
  };
91
92
 
92
93
  var broadcastchannel = /*#__PURE__*/Object.freeze({
@@ -100,4 +101,4 @@ exports.broadcastchannel = broadcastchannel;
100
101
  exports.publish = publish;
101
102
  exports.subscribe = subscribe;
102
103
  exports.unsubscribe = unsubscribe;
103
- //# sourceMappingURL=broadcastchannel-1ac6c212.cjs.map
104
+ //# sourceMappingURL=broadcastchannel-7655b66f.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"broadcastchannel-7655b66f.cjs","sources":["../broadcastchannel.js"],"sourcesContent":["/* eslint-env browser */\n\n/**\n * Helpers for cross-tab communication using broadcastchannel with LocalStorage fallback.\n *\n * ```js\n * // In browser window A:\n * broadcastchannel.subscribe('my events', data => console.log(data))\n * broadcastchannel.publish('my events', 'Hello world!') // => A: 'Hello world!' fires synchronously in same tab\n *\n * // In browser window B:\n * broadcastchannel.publish('my events', 'hello from tab B') // => A: 'hello from tab B'\n * ```\n *\n * @module broadcastchannel\n */\n\n// @todo before next major: use Uint8Array instead as buffer object\n\nimport * as map from './map.js'\nimport * as buffer from './buffer.js'\nimport * as storage from './storage.js'\n\n/**\n * @typedef {Object} Channel\n * @property {Set<function(any, any):any>} Channel.subs\n * @property {any} Channel.bc\n */\n\n/**\n * @type {Map<string, Channel>}\n */\nconst channels = new Map()\n\nclass LocalStoragePolyfill {\n /**\n * @param {string} room\n */\n constructor (room) {\n this.room = room\n /**\n * @type {null|function({data:ArrayBuffer}):void}\n */\n this.onmessage = null\n storage.onChange(e => e.key === room && this.onmessage !== null && this.onmessage({ data: buffer.fromBase64(e.newValue || '') }))\n }\n\n /**\n * @param {ArrayBuffer} buf\n */\n postMessage (buf) {\n storage.varStorage.setItem(this.room, buffer.toBase64(buffer.createUint8ArrayFromArrayBuffer(buf)))\n }\n}\n\n// Use BroadcastChannel or Polyfill\nconst BC = typeof BroadcastChannel === 'undefined' ? LocalStoragePolyfill : BroadcastChannel\n\n/**\n * @param {string} room\n * @return {Channel}\n */\nconst getChannel = room =>\n map.setIfUndefined(channels, room, () => {\n const subs = new Set()\n const bc = new BC(room)\n /**\n * @param {{data:ArrayBuffer}} e\n */\n bc.onmessage = e => subs.forEach(sub => sub(e.data, 'broadcastchannel'))\n return {\n bc, subs\n }\n })\n\n/**\n * Subscribe to global `publish` events.\n *\n * @function\n * @param {string} room\n * @param {function(any, any):any} f\n */\nexport const subscribe = (room, f) => getChannel(room).subs.add(f)\n\n/**\n * Unsubscribe from `publish` global events.\n *\n * @function\n * @param {string} room\n * @param {function(any, any):any} f\n */\nexport const unsubscribe = (room, f) => getChannel(room).subs.delete(f)\n\n/**\n * Publish data to all subscribers (including subscribers on this tab)\n *\n * @function\n * @param {string} room\n * @param {any} data\n * @param {any} [origin]\n */\nexport const publish = (room, data, origin = null) => {\n const c = getChannel(room)\n c.bc.postMessage(data)\n c.subs.forEach(sub => sub(data, origin))\n}\n"],"names":["storage.onChange","buffer.fromBase64","storage.varStorage","buffer.toBase64","buffer.createUint8ArrayFromArrayBuffer","map.setIfUndefined"],"mappings":";;;;;;AAAA;AAsBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,QAAQ,GAAG,IAAI,GAAG,GAAE;AAC1B;AACA,MAAM,oBAAoB,CAAC;AAC3B;AACA;AACA;AACA,EAAE,WAAW,CAAC,CAAC,IAAI,EAAE;AACrB,IAAI,IAAI,CAAC,IAAI,GAAG,KAAI;AACpB;AACA;AACA;AACA,IAAI,IAAI,CAAC,SAAS,GAAG,KAAI;AACzB,IAAIA,gBAAgB,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,KAAK,IAAI,IAAI,IAAI,CAAC,SAAS,KAAK,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAEC,mBAAiB,CAAC,CAAC,CAAC,QAAQ,IAAI,EAAE,CAAC,EAAE,CAAC,EAAC;AACrI,GAAG;AACH;AACA;AACA;AACA;AACA,EAAE,WAAW,CAAC,CAAC,GAAG,EAAE;AACpB,IAAIC,kBAAkB,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,EAAEC,iBAAe,CAACC,wCAAsC,CAAC,GAAG,CAAC,CAAC,EAAC;AACvG,GAAG;AACH,CAAC;AACD;AACA;AACA,MAAM,EAAE,GAAG,OAAO,gBAAgB,KAAK,WAAW,GAAG,oBAAoB,GAAG,iBAAgB;AAC5F;AACA;AACA;AACA;AACA;AACA,MAAM,UAAU,GAAG,IAAI;AACvB,EAAEC,kBAAkB,CAAC,QAAQ,EAAE,IAAI,EAAE,MAAM;AAC3C,IAAI,MAAM,IAAI,GAAG,IAAI,GAAG,GAAE;AAC1B,IAAI,MAAM,EAAE,GAAG,IAAI,EAAE,CAAC,IAAI,EAAC;AAC3B;AACA;AACA;AACA,IAAI,EAAE,CAAC,SAAS,GAAG,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,kBAAkB,CAAC,EAAC;AAC5E,IAAI,OAAO;AACX,MAAM,EAAE,EAAE,IAAI;AACd,KAAK;AACL,GAAG,EAAC;AACJ;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,SAAS,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAC;AAClE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,WAAW,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,EAAC;AACvE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACY,MAAC,OAAO,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,KAAK;AACtD,EAAE,MAAM,CAAC,GAAG,UAAU,CAAC,IAAI,EAAC;AAC5B,EAAE,CAAC,CAAC,EAAE,CAAC,WAAW,CAAC,IAAI,EAAC;AACxB,EAAE,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,EAAC;AAC1C;;;;;;;;;;;;;;"}
@@ -3,9 +3,9 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  require('./map-28a001c9.cjs');
6
- require('./buffer-c98f67d5.cjs');
6
+ require('./buffer-d3ba308b.cjs');
7
7
  require('./storage.cjs');
8
- var broadcastchannel = require('./broadcastchannel-1ac6c212.cjs');
8
+ var broadcastchannel = require('./broadcastchannel-7655b66f.cjs');
9
9
  require('./string-ad04f734.cjs');
10
10
  require('./environment-60b83194.cjs');
11
11
  require('./conditions-fb475c70.cjs');
@@ -1,8 +1,8 @@
1
- export function subscribe(room: string, f: (arg0: any) => any): Set<Function>;
2
- export function unsubscribe(room: string, f: (arg0: any) => any): boolean;
3
- export function publish(room: string, data: any): void;
1
+ export function subscribe(room: string, f: (arg0: any, arg1: any) => any): Set<(arg0: any, arg1: any) => any>;
2
+ export function unsubscribe(room: string, f: (arg0: any, arg1: any) => any): boolean;
3
+ export function publish(room: string, data: any, origin?: any): void;
4
4
  export type Channel = {
5
- subs: Set<Function>;
5
+ subs: Set<(arg0: any, arg1: any) => any>;
6
6
  bc: any;
7
7
  };
8
8
  //# sourceMappingURL=broadcastchannel.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"broadcastchannel.d.ts","sourceRoot":"","sources":["../broadcastchannel.js"],"names":[],"mappings":"AAkFO,gCAHI,MAAM,YACG,GAAG,KAAE,GAAG,iBAEsC;AAS3D,kCAHI,MAAM,YACG,GAAG,KAAE,GAAG,WAE2C;AAShE,8BAHI,MAAM,QACN,GAAG,QAMb;;UA/Ea,aAAa;QACb,GAAG"}
1
+ {"version":3,"file":"broadcastchannel.d.ts","sourceRoot":"","sources":["../broadcastchannel.js"],"names":[],"mappings":"AAkFO,gCAHI,MAAM,YACG,GAAG,QAAE,GAAG,KAAE,GAAG,cAvDN,GAAG,QAAE,GAAG,KAAE,GAAG,EAyD0B;AAS3D,kCAHI,MAAM,YACG,GAAG,QAAE,GAAG,KAAE,GAAG,WAEsC;AAUhE,8BAJI,MAAM,QACN,GAAG,WACH,GAAG,QAMb;;UAhFa,WAAa,GAAG,QAAE,GAAG,KAAE,GAAG,CAAC;QAC3B,GAAG"}