websocket-ts 2.1.1 → 2.1.3

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 (113) hide show
  1. package/dist/cjs/src/backoff/backoff.d.ts +24 -0
  2. package/dist/cjs/src/backoff/backoff.d.ts.map +1 -0
  3. package/dist/cjs/src/backoff/backoff.js +3 -0
  4. package/dist/cjs/src/backoff/backoff.js.map +1 -0
  5. package/dist/cjs/src/backoff/constantbackoff.d.ts +18 -0
  6. package/dist/cjs/src/backoff/constantbackoff.d.ts.map +1 -0
  7. package/dist/cjs/src/backoff/constantbackoff.js +43 -0
  8. package/dist/cjs/src/backoff/constantbackoff.js.map +1 -0
  9. package/dist/cjs/src/backoff/exponentialbackoff.d.ts +48 -0
  10. package/dist/cjs/src/backoff/exponentialbackoff.d.ts.map +1 -0
  11. package/dist/cjs/src/backoff/exponentialbackoff.js +81 -0
  12. package/dist/cjs/src/backoff/exponentialbackoff.js.map +1 -0
  13. package/dist/cjs/src/backoff/linearbackoff.d.ts +51 -0
  14. package/dist/cjs/src/backoff/linearbackoff.d.ts.map +1 -0
  15. package/dist/cjs/src/backoff/linearbackoff.js +89 -0
  16. package/dist/cjs/src/backoff/linearbackoff.js.map +1 -0
  17. package/dist/cjs/src/index.d.ts +14 -0
  18. package/dist/cjs/src/index.d.ts.map +1 -0
  19. package/dist/cjs/src/index.js +20 -0
  20. package/dist/cjs/src/index.js.map +1 -0
  21. package/dist/cjs/src/queue/array_queue.d.ts +17 -0
  22. package/dist/cjs/src/queue/array_queue.d.ts.map +1 -0
  23. package/dist/cjs/src/queue/array_queue.js +36 -0
  24. package/dist/cjs/src/queue/array_queue.js.map +1 -0
  25. package/dist/cjs/src/queue/queue.d.ts +41 -0
  26. package/dist/cjs/src/queue/queue.d.ts.map +1 -0
  27. package/dist/cjs/src/queue/queue.js +3 -0
  28. package/dist/cjs/src/queue/queue.js.map +1 -0
  29. package/dist/cjs/src/queue/ring_queue.d.ts +20 -0
  30. package/dist/cjs/src/queue/ring_queue.d.ts.map +1 -0
  31. package/dist/cjs/src/queue/ring_queue.js +57 -0
  32. package/dist/cjs/src/queue/ring_queue.js.map +1 -0
  33. package/dist/cjs/src/websocket.d.ts +206 -0
  34. package/dist/cjs/src/websocket.d.ts.map +1 -0
  35. package/dist/cjs/src/websocket.js +434 -0
  36. package/dist/cjs/src/websocket.js.map +1 -0
  37. package/dist/cjs/src/websocket_buffer.d.ts +16 -0
  38. package/dist/cjs/src/websocket_buffer.d.ts.map +1 -0
  39. package/dist/cjs/src/websocket_buffer.js +3 -0
  40. package/dist/cjs/src/websocket_buffer.js.map +1 -0
  41. package/dist/cjs/src/websocket_builder.d.ts +147 -0
  42. package/dist/cjs/src/websocket_builder.d.ts.map +1 -0
  43. package/dist/cjs/src/websocket_builder.js +263 -0
  44. package/dist/cjs/src/websocket_builder.js.map +1 -0
  45. package/dist/cjs/src/websocket_event.d.ts +67 -0
  46. package/dist/cjs/src/websocket_event.d.ts.map +1 -0
  47. package/dist/cjs/src/websocket_event.js +22 -0
  48. package/dist/cjs/src/websocket_event.js.map +1 -0
  49. package/dist/cjs/src/websocket_options.d.ts +21 -0
  50. package/dist/cjs/src/websocket_options.d.ts.map +1 -0
  51. package/dist/cjs/src/websocket_options.js +3 -0
  52. package/dist/cjs/src/websocket_options.js.map +1 -0
  53. package/dist/cjs/src/websocket_retry_options.d.ts +20 -0
  54. package/dist/cjs/src/websocket_retry_options.d.ts.map +1 -0
  55. package/dist/cjs/src/websocket_retry_options.js +3 -0
  56. package/dist/cjs/src/websocket_retry_options.js.map +1 -0
  57. package/dist/esm/src/backoff/backoff.d.ts +24 -0
  58. package/dist/esm/src/backoff/backoff.d.ts.map +1 -0
  59. package/dist/esm/src/backoff/backoff.js +2 -0
  60. package/dist/esm/src/backoff/backoff.js.map +1 -0
  61. package/dist/esm/src/backoff/constantbackoff.d.ts +18 -0
  62. package/dist/esm/src/backoff/constantbackoff.d.ts.map +1 -0
  63. package/dist/esm/src/backoff/constantbackoff.js +30 -0
  64. package/dist/esm/src/backoff/constantbackoff.js.map +1 -0
  65. package/dist/esm/src/backoff/exponentialbackoff.d.ts +48 -0
  66. package/dist/esm/src/backoff/exponentialbackoff.d.ts.map +1 -0
  67. package/dist/esm/src/backoff/exponentialbackoff.js +68 -0
  68. package/dist/esm/src/backoff/exponentialbackoff.js.map +1 -0
  69. package/dist/esm/src/backoff/linearbackoff.d.ts +51 -0
  70. package/dist/esm/src/backoff/linearbackoff.d.ts.map +1 -0
  71. package/dist/esm/src/backoff/linearbackoff.js +76 -0
  72. package/dist/esm/src/backoff/linearbackoff.js.map +1 -0
  73. package/dist/esm/src/index.d.ts +14 -0
  74. package/dist/esm/src/index.d.ts.map +1 -0
  75. package/dist/esm/src/index.js +9 -0
  76. package/dist/esm/src/index.js.map +1 -0
  77. package/dist/esm/src/queue/array_queue.d.ts +17 -0
  78. package/dist/esm/src/queue/array_queue.d.ts.map +1 -0
  79. package/dist/esm/src/queue/array_queue.js +31 -0
  80. package/dist/esm/src/queue/array_queue.js.map +1 -0
  81. package/dist/esm/src/queue/queue.d.ts +41 -0
  82. package/dist/esm/src/queue/queue.d.ts.map +1 -0
  83. package/dist/esm/src/queue/queue.js +2 -0
  84. package/dist/esm/src/queue/queue.js.map +1 -0
  85. package/dist/esm/src/queue/ring_queue.d.ts +20 -0
  86. package/dist/esm/src/queue/ring_queue.d.ts.map +1 -0
  87. package/dist/esm/src/queue/ring_queue.js +52 -0
  88. package/dist/esm/src/queue/ring_queue.js.map +1 -0
  89. package/dist/esm/src/websocket.d.ts +206 -0
  90. package/dist/esm/src/websocket.d.ts.map +1 -0
  91. package/dist/esm/src/websocket.js +356 -0
  92. package/dist/esm/src/websocket.js.map +1 -0
  93. package/dist/esm/src/websocket_buffer.d.ts +16 -0
  94. package/dist/esm/src/websocket_buffer.d.ts.map +1 -0
  95. package/dist/esm/src/websocket_buffer.js +2 -0
  96. package/dist/esm/src/websocket_buffer.js.map +1 -0
  97. package/dist/esm/src/websocket_builder.d.ts +147 -0
  98. package/dist/esm/src/websocket_builder.d.ts.map +1 -0
  99. package/dist/esm/src/websocket_builder.js +213 -0
  100. package/dist/esm/src/websocket_builder.js.map +1 -0
  101. package/dist/esm/src/websocket_event.d.ts +67 -0
  102. package/dist/esm/src/websocket_event.d.ts.map +1 -0
  103. package/dist/esm/src/websocket_event.js +19 -0
  104. package/dist/esm/src/websocket_event.js.map +1 -0
  105. package/dist/esm/src/websocket_options.d.ts +21 -0
  106. package/dist/esm/src/websocket_options.d.ts.map +1 -0
  107. package/dist/esm/src/websocket_options.js +2 -0
  108. package/dist/esm/src/websocket_options.js.map +1 -0
  109. package/dist/esm/src/websocket_retry_options.d.ts +20 -0
  110. package/dist/esm/src/websocket_retry_options.d.ts.map +1 -0
  111. package/dist/esm/src/websocket_retry_options.js +2 -0
  112. package/dist/esm/src/websocket_retry_options.js.map +1 -0
  113. package/package.json +1 -1
@@ -0,0 +1,24 @@
1
+ /**
2
+ * A Backoff produces a series of numbers that are used to determine
3
+ * the delay between connection-retries. Values are expected to be in milliseconds.
4
+ */
5
+ export interface Backoff {
6
+ /**
7
+ * The number of retries. Starts at 0, increases by 1 for each call to next(). Resets to 0 when reset() is called.
8
+ */
9
+ readonly retries: number;
10
+ /**
11
+ * Current number in the series.
12
+ */
13
+ readonly current: number;
14
+ /**
15
+ * Advances the series to the next number and returns it.
16
+ * @return the next number in the series
17
+ */
18
+ next(): number;
19
+ /**
20
+ * Resets the series to its initial state.
21
+ */
22
+ reset(): void;
23
+ }
24
+ //# sourceMappingURL=backoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backoff.d.ts","sourceRoot":"","sources":["../../../../src/backoff/backoff.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAEzB;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IAEzB;;;OAGG;IACH,IAAI,IAAI,MAAM,CAAC;IAEf;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;CACf"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=backoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backoff.js","sourceRoot":"","sources":["../../../../src/backoff/backoff.ts"],"names":[],"mappings":""}
@@ -0,0 +1,18 @@
1
+ import { Backoff } from "./backoff";
2
+ /**
3
+ * ConstantBackoff always returns the same backoff-time.
4
+ */
5
+ export declare class ConstantBackoff implements Backoff {
6
+ private readonly backoff;
7
+ private _retries;
8
+ /**
9
+ * Creates a new ConstantBackoff.
10
+ * @param backoff the backoff-time to return
11
+ */
12
+ constructor(backoff: number);
13
+ get retries(): number;
14
+ get current(): number;
15
+ next(): number;
16
+ reset(): void;
17
+ }
18
+ //# sourceMappingURL=constantbackoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constantbackoff.d.ts","sourceRoot":"","sources":["../../../../src/backoff/constantbackoff.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC;;GAEG;AACH,qBAAa,eAAgB,YAAW,OAAO;IAC7C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAa;IAE7B;;;OAGG;gBACS,OAAO,EAAE,MAAM;IAQ3B,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,IAAI,MAAM;IAKd,KAAK,IAAI,IAAI;CAGd"}
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConstantBackoff = void 0;
4
+ /**
5
+ * ConstantBackoff always returns the same backoff-time.
6
+ */
7
+ var ConstantBackoff = /** @class */ (function () {
8
+ /**
9
+ * Creates a new ConstantBackoff.
10
+ * @param backoff the backoff-time to return
11
+ */
12
+ function ConstantBackoff(backoff) {
13
+ this._retries = 0;
14
+ if (!Number.isInteger(backoff) || backoff < 0) {
15
+ throw new Error("Backoff must be a positive integer");
16
+ }
17
+ this.backoff = backoff;
18
+ }
19
+ Object.defineProperty(ConstantBackoff.prototype, "retries", {
20
+ get: function () {
21
+ return this._retries;
22
+ },
23
+ enumerable: false,
24
+ configurable: true
25
+ });
26
+ Object.defineProperty(ConstantBackoff.prototype, "current", {
27
+ get: function () {
28
+ return this.backoff;
29
+ },
30
+ enumerable: false,
31
+ configurable: true
32
+ });
33
+ ConstantBackoff.prototype.next = function () {
34
+ this._retries++;
35
+ return this.backoff;
36
+ };
37
+ ConstantBackoff.prototype.reset = function () {
38
+ this._retries = 0;
39
+ };
40
+ return ConstantBackoff;
41
+ }());
42
+ exports.ConstantBackoff = ConstantBackoff;
43
+ //# sourceMappingURL=constantbackoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constantbackoff.js","sourceRoot":"","sources":["../../../../src/backoff/constantbackoff.ts"],"names":[],"mappings":";;;AAEA;;GAEG;AACH;IAIE;;;OAGG;IACH,yBAAY,OAAe;QANnB,aAAQ,GAAW,CAAC,CAAC;QAO3B,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,OAAO,GAAG,CAAC,EAAE;YAC7C,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;SACvD;QAED,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED,sBAAI,oCAAO;aAAX;YACE,OAAO,IAAI,CAAC,QAAQ,CAAC;QACvB,CAAC;;;OAAA;IAED,sBAAI,oCAAO;aAAX;YACE,OAAO,IAAI,CAAC,OAAO,CAAC;QACtB,CAAC;;;OAAA;IAED,8BAAI,GAAJ;QACE,IAAI,CAAC,QAAQ,EAAE,CAAC;QAChB,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,+BAAK,GAAL;QACE,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;IACpB,CAAC;IACH,sBAAC;AAAD,CAAC,AAhCD,IAgCC;AAhCY,0CAAe"}
@@ -0,0 +1,48 @@
1
+ import { Backoff } from "./backoff";
2
+ /**
3
+ * ExponentialBackoff increases the backoff-time exponentially.
4
+ * An optional maximum can be provided as an upper bound to the
5
+ * exponent and thus to the returned backoff.
6
+ *
7
+ * The series can be described as ('i' is the current step/retry):
8
+ * backoff = base * 2^i | without bound
9
+ * backoff = base * 2^min(i, expMax) | with bound
10
+ *
11
+ * Example:
12
+ *
13
+ * 1) Without bound:
14
+ * base = 1000, expMax = undefined
15
+ * backoff = 1000 * 2^0 = 1000 // first retry
16
+ * backoff = 1000 * 2^1 = 2000 // second retry
17
+ * backoff = 1000 * 2^2 = 4000 // ...doubles with every retry
18
+ * backoff = 1000 * 2^3 = 8000
19
+ * backoff = 1000 * 2^4 = 16000
20
+ * ... // and so on
21
+ *
22
+ * 2) With bound:
23
+ * base = 1000, expMax = 3
24
+ * backoff = 1000 * 2^0 = 1000 // first retry
25
+ * backoff = 1000 * 2^1 = 2000 // second retry
26
+ * backoff = 1000 * 2^2 = 4000 // third retry
27
+ * backoff = 1000 * 2^3 = 8000 // maximum reached, don't increase further
28
+ * backoff = 1000 * 2^3 = 8000
29
+ * backoff = 1000 * 2^3 = 8000
30
+ * ... // and so on
31
+ */
32
+ export declare class ExponentialBackoff implements Backoff {
33
+ private readonly base;
34
+ private readonly expMax?;
35
+ private i;
36
+ private _retries;
37
+ /**
38
+ * Creates a new ExponentialBackoff.
39
+ * @param base the base of the exponentiation
40
+ * @param expMax the maximum exponent, no bound if undefined
41
+ */
42
+ constructor(base: number, expMax?: number);
43
+ get retries(): number;
44
+ get current(): number;
45
+ next(): number;
46
+ reset(): void;
47
+ }
48
+ //# sourceMappingURL=exponentialbackoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exponentialbackoff.d.ts","sourceRoot":"","sources":["../../../../src/backoff/exponentialbackoff.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBAAa,kBAAmB,YAAW,OAAO;IAChD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAS;IACjC,OAAO,CAAC,CAAC,CAAS;IAClB,OAAO,CAAC,QAAQ,CAAa;IAE7B;;;;OAIG;gBACS,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM;IAazC,IAAI,OAAO,WAEV;IAED,IAAI,OAAO,IAAI,MAAM,CAEpB;IAED,IAAI,IAAI,MAAM;IASd,KAAK,IAAI,IAAI;CAId"}
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExponentialBackoff = void 0;
4
+ /**
5
+ * ExponentialBackoff increases the backoff-time exponentially.
6
+ * An optional maximum can be provided as an upper bound to the
7
+ * exponent and thus to the returned backoff.
8
+ *
9
+ * The series can be described as ('i' is the current step/retry):
10
+ * backoff = base * 2^i | without bound
11
+ * backoff = base * 2^min(i, expMax) | with bound
12
+ *
13
+ * Example:
14
+ *
15
+ * 1) Without bound:
16
+ * base = 1000, expMax = undefined
17
+ * backoff = 1000 * 2^0 = 1000 // first retry
18
+ * backoff = 1000 * 2^1 = 2000 // second retry
19
+ * backoff = 1000 * 2^2 = 4000 // ...doubles with every retry
20
+ * backoff = 1000 * 2^3 = 8000
21
+ * backoff = 1000 * 2^4 = 16000
22
+ * ... // and so on
23
+ *
24
+ * 2) With bound:
25
+ * base = 1000, expMax = 3
26
+ * backoff = 1000 * 2^0 = 1000 // first retry
27
+ * backoff = 1000 * 2^1 = 2000 // second retry
28
+ * backoff = 1000 * 2^2 = 4000 // third retry
29
+ * backoff = 1000 * 2^3 = 8000 // maximum reached, don't increase further
30
+ * backoff = 1000 * 2^3 = 8000
31
+ * backoff = 1000 * 2^3 = 8000
32
+ * ... // and so on
33
+ */
34
+ var ExponentialBackoff = /** @class */ (function () {
35
+ /**
36
+ * Creates a new ExponentialBackoff.
37
+ * @param base the base of the exponentiation
38
+ * @param expMax the maximum exponent, no bound if undefined
39
+ */
40
+ function ExponentialBackoff(base, expMax) {
41
+ this._retries = 0;
42
+ if (!Number.isInteger(base) || base < 0) {
43
+ throw new Error("Base must be a positive integer or zero");
44
+ }
45
+ if (expMax !== undefined && (!Number.isInteger(expMax) || expMax < 0)) {
46
+ throw new Error("ExpMax must be a undefined, a positive integer or zero");
47
+ }
48
+ this.base = base;
49
+ this.expMax = expMax;
50
+ this.i = 0;
51
+ }
52
+ Object.defineProperty(ExponentialBackoff.prototype, "retries", {
53
+ get: function () {
54
+ return this._retries;
55
+ },
56
+ enumerable: false,
57
+ configurable: true
58
+ });
59
+ Object.defineProperty(ExponentialBackoff.prototype, "current", {
60
+ get: function () {
61
+ return this.base * Math.pow(2, this.i);
62
+ },
63
+ enumerable: false,
64
+ configurable: true
65
+ });
66
+ ExponentialBackoff.prototype.next = function () {
67
+ this._retries++;
68
+ this.i =
69
+ this.expMax === undefined
70
+ ? this.i + 1
71
+ : Math.min(this.i + 1, this.expMax);
72
+ return this.current;
73
+ };
74
+ ExponentialBackoff.prototype.reset = function () {
75
+ this._retries = 0;
76
+ this.i = 0;
77
+ };
78
+ return ExponentialBackoff;
79
+ }());
80
+ exports.ExponentialBackoff = ExponentialBackoff;
81
+ //# sourceMappingURL=exponentialbackoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exponentialbackoff.js","sourceRoot":"","sources":["../../../../src/backoff/exponentialbackoff.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH;IAME;;;;OAIG;IACH,4BAAY,IAAY,EAAE,MAAe;QAPjC,aAAQ,GAAW,CAAC,CAAC;QAQ3B,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off returns a backoff-time that is incremented by a fixed amount
4
+ * with every step/retry. An optional maximum can be provided as an upper bound
5
+ * to the returned backoff.
6
+ *
7
+ * The series can be described as ('i' is the current step/retry):
8
+ * backoff = initial + increment * i | without bound
9
+ * backoff = initial + increment * min(i, max) | with bound
10
+ *
11
+ * Example:
12
+ *
13
+ * 1) Without bound:
14
+ * initial = 1000, increment = 1000
15
+ * backoff = 1000 + 1000 * 0 = 1000 // first retry
16
+ * backoff = 1000 + 1000 * 1 = 2000 // second retry
17
+ * backoff = 1000 + 1000 * 2 = 3000 // ...increases by 'increment' with every retry
18
+ * backoff = 1000 + 1000 * 3 = 4000
19
+ * backoff = 1000 + 1000 * 4 = 5000
20
+ * ... // and so on
21
+ *
22
+ * 2) With bound:
23
+ * initial = 1000, increment = 1000, max = 5000
24
+ * backoff = 1000 + 1000 * 0 = 1000 // first retry
25
+ * backoff = 1000 + 1000 * 1 = 2000 // second retry
26
+ * backoff = 1000 + 1000 * 2 = 3000 // third retry
27
+ * backoff = 1000 + 1000 * 3 = 4000 // fourth retry
28
+ * backoff = 1000 + 1000 * 4 = 5000 // maximum reached, don't increase further
29
+ * backoff = 1000 + 1000 * 4 = 5000
30
+ * backoff = 1000 + 1000 * 4 = 5000
31
+ * ... // and so on
32
+ */
33
+ export declare class LinearBackoff implements Backoff {
34
+ private readonly initial;
35
+ private readonly increment;
36
+ private readonly max?;
37
+ private i;
38
+ private _retries;
39
+ /**
40
+ * Creates a new LinearBackoff.
41
+ * @param initial the initial backoff-time in milliseconds
42
+ * @param increment the amount to increment the backoff-time with every step (in milliseconds)
43
+ * @param max the maximum backoff-time (in milliseconds), no bound if undefined
44
+ */
45
+ constructor(initial: number, increment: number, max?: number);
46
+ get retries(): number;
47
+ get current(): number;
48
+ next(): number;
49
+ reset(): void;
50
+ }
51
+ //# sourceMappingURL=linearbackoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"linearbackoff.d.ts","sourceRoot":"","sources":["../../../../src/backoff/linearbackoff.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBAAa,aAAc,YAAW,OAAO;IAC3C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAS;IAC9B,OAAO,CAAC,CAAC,CAAa;IACtB,OAAO,CAAC,QAAQ,CAAa;IAE7B;;;;;OAKG;gBACS,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM;IAqB5D,IAAI,OAAO,WAEV;IAED,IAAI,OAAO,IAAI,MAAM,CAIpB;IAED,IAAI,IAAI,MAAM;IAMd,KAAK,IAAI,IAAI;CAId"}
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LinearBackoff = void 0;
4
+ /**
5
+ * LinearBackoff returns a backoff-time that is incremented by a fixed amount
6
+ * with every step/retry. An optional maximum can be provided as an upper bound
7
+ * to the returned backoff.
8
+ *
9
+ * The series can be described as ('i' is the current step/retry):
10
+ * backoff = initial + increment * i | without bound
11
+ * backoff = initial + increment * min(i, max) | with bound
12
+ *
13
+ * Example:
14
+ *
15
+ * 1) Without bound:
16
+ * initial = 1000, increment = 1000
17
+ * backoff = 1000 + 1000 * 0 = 1000 // first retry
18
+ * backoff = 1000 + 1000 * 1 = 2000 // second retry
19
+ * backoff = 1000 + 1000 * 2 = 3000 // ...increases by 'increment' with every retry
20
+ * backoff = 1000 + 1000 * 3 = 4000
21
+ * backoff = 1000 + 1000 * 4 = 5000
22
+ * ... // and so on
23
+ *
24
+ * 2) With bound:
25
+ * initial = 1000, increment = 1000, max = 5000
26
+ * backoff = 1000 + 1000 * 0 = 1000 // first retry
27
+ * backoff = 1000 + 1000 * 1 = 2000 // second retry
28
+ * backoff = 1000 + 1000 * 2 = 3000 // third retry
29
+ * backoff = 1000 + 1000 * 3 = 4000 // fourth retry
30
+ * backoff = 1000 + 1000 * 4 = 5000 // maximum reached, don't increase further
31
+ * backoff = 1000 + 1000 * 4 = 5000
32
+ * backoff = 1000 + 1000 * 4 = 5000
33
+ * ... // and so on
34
+ */
35
+ var LinearBackoff = /** @class */ (function () {
36
+ /**
37
+ * Creates a new LinearBackoff.
38
+ * @param initial the initial backoff-time in milliseconds
39
+ * @param increment the amount to increment the backoff-time with every step (in milliseconds)
40
+ * @param max the maximum backoff-time (in milliseconds), no bound if undefined
41
+ */
42
+ function LinearBackoff(initial, increment, max) {
43
+ this.i = 0;
44
+ this._retries = 0;
45
+ if (initial < 0) {
46
+ throw new Error("Initial must be a positive number or zero");
47
+ }
48
+ if (increment < 0) {
49
+ throw new Error("Increment must be a positive number or zero");
50
+ }
51
+ if (max !== undefined && max < 0) {
52
+ throw new Error("Max must be undefined, a positive number or zero");
53
+ }
54
+ if (max !== undefined && max < initial) {
55
+ throw new Error("Max must be undefined or greater than or equal to initial");
56
+ }
57
+ this.initial = initial;
58
+ this.increment = increment;
59
+ this.max = max;
60
+ }
61
+ Object.defineProperty(LinearBackoff.prototype, "retries", {
62
+ get: function () {
63
+ return this._retries;
64
+ },
65
+ enumerable: false,
66
+ configurable: true
67
+ });
68
+ Object.defineProperty(LinearBackoff.prototype, "current", {
69
+ get: function () {
70
+ return this.max === undefined
71
+ ? this.initial + this.increment * this.i
72
+ : Math.min(this.initial + this.increment * this.i, this.max);
73
+ },
74
+ enumerable: false,
75
+ configurable: true
76
+ });
77
+ LinearBackoff.prototype.next = function () {
78
+ this._retries++;
79
+ this.i++;
80
+ return this.current;
81
+ };
82
+ LinearBackoff.prototype.reset = function () {
83
+ this._retries = 0;
84
+ this.i = 0;
85
+ };
86
+ return LinearBackoff;
87
+ }());
88
+ exports.LinearBackoff = LinearBackoff;
89
+ //# sourceMappingURL=linearbacko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AA;IAED,4BAAI,GAAJ;QACE,IAAI,CAAC,QAAQ,EAAE,CAAC;QAChB,IAAI,CAAC,CAAC,EAAE,CAAC;QACT,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,6BAAK,GAAL;QACE,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;QAClB,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IACb,CAAC;IACH,oBAAC;AAAD,CAAC,AAtDD,IAsDC;AAtDY,sCAAa"}
@@ -0,0 +1,14 @@
1
+ export { Backoff } from "./backoff/backoff";
2
+ export { ConstantBackoff } from "./backoff/constantbackoff";
3
+ export { ExponentialBackoff } from "./backoff/exponentialbackoff";
4
+ export { LinearBackoff } from "./backoff/linearbackoff";
5
+ export { Queue } from "./queue/queue";
6
+ export { ArrayQueue } from "./queue/array_queue";
7
+ export { RingQueue } from "./queue/ring_queue";
8
+ export { Websocket } from "./websocket";
9
+ export { WebsocketBuffer } from "./websocket_buffer";
10
+ export { WebsocketBuilder } from "./websocket_builder";
11
+ export { WebsocketEvent, RetryEventDetail, ReconnectEventDetail, WebsocketEventMap, WebsocketEventListener, WebsocketEventListenerParams, WebsocketEventListenerOptions, WebsocketEventListenerWithOptions, WebsocketEventListeners, } from "./websocket_event";
12
+ export { WebsocketOptions } from "./websocket_options";
13
+ export { WebsocketConnectionRetryOptions } from "./websocket_retry_options";
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,EACjB,sBAAsB,EACtB,4BAA4B,EAC5B,6BAA6B,EAC7B,iCAAiC,EACjC,uBAAuB,GACxB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC"}
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebsocketEvent = exports.WebsocketBuilder = exports.Websocket = exports.RingQueue = exports.ArrayQueue = exports.LinearBackoff = exports.ExponentialBackoff = exports.ConstantBackoff = void 0;
4
+ var constantbackoff_1 = require("./backoff/constantbackoff");
5
+ Object.defineProperty(exports, "ConstantBackoff", { enumerable: true, get: function () { return constantbackoff_1.ConstantBackoff; } });
6
+ var exponentialbackoff_1 = require("./backoff/exponentialbackoff");
7
+ Object.defineProperty(exports, "ExponentialBackoff", { enumerable: true, get: function () { return exponentialbackoff_1.ExponentialBackoff; } });
8
+ var linearbackoff_1 = require("./backoff/linearbackoff");
9
+ Object.defineProperty(exports, "LinearBackoff", { enumerable: true, get: function () { return linearbackoff_1.LinearBackoff; } });
10
+ var array_queue_1 = require("./queue/array_queue");
11
+ Object.defineProperty(exports, "ArrayQueue", { enumerable: true, get: function () { return array_queue_1.ArrayQueue; } });
12
+ var ring_queue_1 = require("./queue/ring_queue");
13
+ Object.defineProperty(exports, "RingQueue", { enumerable: true, get: function () { return ring_queue_1.RingQueue; } });
14
+ var websocket_1 = require("./websocket");
15
+ Object.defineProperty(exports, "Websocket", { enumerable: true, get: function () { return websocket_1.Websocket; } });
16
+ var websocket_builder_1 = require("./websocket_builder");
17
+ Object.defineProperty(exports, "WebsocketBuilder", { enumerable: true, get: function () { return websocket_builder_1.WebsocketBuilder; } });
18
+ var websocket_event_1 = require("./websocket_event");
19
+ Object.defineProperty(exports, "WebsocketEvent", { enumerable: true, get: function () { return websocket_event_1.WebsocketEvent; } });
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";;;AACA,6DAA4D;AAAnD,kHAAA,eAAe,OAAA;AACxB,mEAAkE;AAAzD,wHAAA,kBAAkB,OAAA;AAC3B,yDAAwD;AAA/C,8GAAA,aAAa,OAAA;AAEtB,mDAAiD;AAAxC,yGAAA,UAAU,OAAA;AACnB,iDAA+C;AAAtC,uGAAA,SAAS,OAAA;AAClB,yCAAwC;AAA/B,sGAAA,SAAS,OAAA;AAElB,yDAAuD;AAA9C,qHAAA,gBAAgB,OAAA;AACzB,qDAU2B;AATzB,iHAAA,cAAc,OAAA"}
@@ -0,0 +1,17 @@
1
+ import { Queue } from "./queue";
2
+ /**
3
+ * An array queue is a queue that has an unbounded capacity. Reading from an array queue
4
+ * will return the oldest element and effectively remove it from the queue.
5
+ */
6
+ export declare class ArrayQueue<E> implements Queue<E> {
7
+ private readonly elements;
8
+ constructor();
9
+ add(element: E): void;
10
+ clear(): void;
11
+ forEach(fn: (element: E) => unknown): void;
12
+ length(): number;
13
+ isEmpty(): boolean;
14
+ peek(): E | undefined;
15
+ read(): E | undefined;
16
+ }
17
+ //# sourceMappingURL=array_queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"array_queue.d.ts","sourceRoot":"","sources":["../../../../src/queue/array_queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC;;;GAGG;AACH,qBAAa,UAAU,CAAC,CAAC,CAAE,YAAW,KAAK,CAAC,CAAC,CAAC;IAC5C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAM;;IAM/B,GAAG,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI;IAIrB,KAAK;IAIL,OAAO,CAAC,EAAE,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,OAAO;IAInC,MAAM,IAAI,MAAM;IAIhB,OAAO,IAAI,OAAO;IAIlB,IAAI,IAAI,CAAC,GAAG,SAAS;IAIrB,IAAI,IAAI,CAAC,GAAG,SAAS;CAGtB"}
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ArrayQueue = void 0;
4
+ /**
5
+ * An array queue is a queue that has an unbounded capacity. Reading from an array queue
6
+ * will return the oldest element and effectively remove it from the queue.
7
+ */
8
+ var ArrayQueue = /** @class */ (function () {
9
+ function ArrayQueue() {
10
+ this.elements = [];
11
+ }
12
+ ArrayQueue.prototype.add = function (element) {
13
+ this.elements.push(element);
14
+ };
15
+ ArrayQueue.prototype.clear = function () {
16
+ this.elements.length = 0;
17
+ };
18
+ ArrayQueue.prototype.forEach = function (fn) {
19
+ this.elements.forEach(fn);
20
+ };
21
+ ArrayQueue.prototype.length = function () {
22
+ return this.elements.length;
23
+ };
24
+ ArrayQueue.prototype.isEmpty = function () {
25
+ return this.elements.length === 0;
26
+ };
27
+ ArrayQueue.prototype.peek = function () {
28
+ return this.elements[0];
29
+ };
30
+ ArrayQueue.prototype.read = function () {
31
+ return this.elements.shift();
32
+ };
33
+ return ArrayQueue;
34
+ }());
35
+ exports.ArrayQueue = ArrayQueue;
36
+ //# sourceMappingURL=array_queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"array_queue.js","sourceRoot":"","sources":["../../../../src/queue/array_queue.ts"],"names":[],"mappings":";;;AAEA;;;GAGG;AACH;IAGE;QACE,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;IACrB,CAAC;IAED,wBAAG,GAAH,UAAI,OAAU;QACZ,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;IAED,0BAAK,GAAL;QACE,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED,4BAAO,GAAP,UAAQ,EAA2B;QACjC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAC5B,CAAC;IAED,2BAAM,GAAN;QACE,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;IAC9B,CAAC;IAED,4BAAO,GAAP;QACE,OAAO,IAAI,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC,CAAC;IACpC,CAAC;IAED,yBAAI,GAAJ;QACE,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC1B,CAAC;IAED,yBAAI,GAAJ;QACE,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;IAC/B,CAAC;IACH,iBAAC;AAAD,CAAC,AAlCD,IAkCC;AAlCY,gCAAU"}
@@ -0,0 +1,41 @@
1
+ /**
2
+ * A queue holds elements until they are read. The order in which elements are
3
+ * read is determined by the implementation of the queue.
4
+ */
5
+ export interface Queue<E> {
6
+ /**
7
+ * Adds an element to the queue.
8
+ * @param element the element to add
9
+ */
10
+ add(element: E): void;
11
+ /**
12
+ * Clears the queue, removing all elements.
13
+ */
14
+ clear(): void;
15
+ /**
16
+ * Calls the given function for each element in the queue.
17
+ * @param fn the function to call
18
+ */
19
+ forEach(fn: (element: E) => unknown): void;
20
+ /**
21
+ * Number of elements in the queue.
22
+ * @return the number of elements in the queue
23
+ */
24
+ length(): number;
25
+ /**
26
+ * Whether the queue is empty.
27
+ * @return true if the queue is empty, false otherwise
28
+ */
29
+ isEmpty(): boolean;
30
+ /**
31
+ * Returns the next element in the queue without removing it.
32
+ * @return the next element in the queue, or undefined if the queue is empty
33
+ */
34
+ peek(): E | undefined;
35
+ /**
36
+ * Removes and returns the next element in the queue.
37
+ * @return the next element in the queue, or undefined if the queue is empty
38
+ */
39
+ read(): E | undefined;
40
+ }
41
+ //# sourceMappingURL=queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../../../../src/queue/queue.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,KAAK,CAAC,CAAC;IACtB;;;OAGG;IACH,GAAG,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI,CAAC;IAEtB;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;IAEd;;;OAGG;IACH,OAAO,CAAC,EAAE,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,OAAO,GAAG,IAAI,CAAC;IAE3C;;;OAGG;IACH,MAAM,IAAI,MAAM,CAAC;IAEjB;;;OAGG;IACH,OAAO,IAAI,OAAO,CAAC;IAEnB;;;OAGG;IACH,IAAI,IAAI,CAAC,GAAG,SAAS,CAAC;IAEtB;;;OAGG;IACH,IAAI,IAAI,CAAC,GAAG,SAAS,CAAC;CACvB"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../../src/queue/queue.ts"],"names":[],"mappings":""}
@@ -0,0 +1,20 @@
1
+ import { Queue } from "./queue";
2
+ /**
3
+ * A ring queue is a queue that has a fixed capacity. When the queue is full, the oldest element is
4
+ * removed to make room for the new element. Reading from a ring queue will return the oldest
5
+ * element and effectively remove it from the queue.
6
+ */
7
+ export declare class RingQueue<E> implements Queue<E> {
8
+ private readonly elements;
9
+ private head;
10
+ private tail;
11
+ constructor(capacity: number);
12
+ add(element: E): void;
13
+ clear(): void;
14
+ forEach(fn: (element: E) => unknown): void;
15
+ length(): number;
16
+ isEmpty(): boolean;
17
+ peek(): E | undefined;
18
+ read(): E | undefined;
19
+ }
20
+ //# sourceMappingURL=ring_queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ring_queue.d.ts","sourceRoot":"","sources":["../../../../src/queue/ring_queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC;;;;GAIG;AACH,qBAAa,SAAS,CAAC,CAAC,CAAE,YAAW,KAAK,CAAC,CAAC,CAAC;IAC3C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAM;IAC/B,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,IAAI,CAAS;gBAET,QAAQ,EAAE,MAAM;IAU5B,GAAG,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI;IAQrB,KAAK;IAKL,OAAO,CAAC,EAAE,EAAE,CAAC,OAAO,EAAE,CAAC,KAAK,OAAO;IAUnC,MAAM,IAAI,MAAM;IAQhB,OAAO,IAAI,OAAO;IAIlB,IAAI,IAAI,CAAC,GAAG,SAAS;IAIrB,IAAI,IAAI,CAAC,GAAG,SAAS;CAOtB"}
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RingQueue = void 0;
4
+ /**
5
+ * A ring queue is a queue that has a fixed capacity. When the queue is full, the oldest element is
6
+ * removed to make room for the new element. Reading from a ring queue will return the oldest
7
+ * element and effectively remove it from the queue.
8
+ */
9
+ var RingQueue = /** @class */ (function () {
10
+ function RingQueue(capacity) {
11
+ if (!Number.isInteger(capacity) || capacity <= 0) {
12
+ throw new Error("Capacity must be a positive integer");
13
+ }
14
+ this.elements = new Array(capacity + 1); // +1 to distinguish between full and empty
15
+ this.head = 0;
16
+ this.tail = 0;
17
+ }
18
+ RingQueue.prototype.add = function (element) {
19
+ this.elements[this.head] = element;
20
+ this.head = (this.head + 1) % this.elements.length;
21
+ if (this.head === this.tail) {
22
+ this.tail = (this.tail + 1) % this.elements.length;
23
+ }
24
+ };
25
+ RingQueue.prototype.clear = function () {
26
+ this.head = 0;
27
+ this.tail = 0;
28
+ };
29
+ RingQueue.prototype.forEach = function (fn) {
30
+ for (var i = this.tail; i !== this.head; i = (i + 1) % this.elements.length) {
31
+ fn(this.elements[i]);
32
+ }
33
+ };
34
+ RingQueue.prototype.length = function () {
35
+ return this.tail === this.head
36
+ ? 0
37
+ : this.tail < this.head
38
+ ? this.head - this.tail
39
+ : this.elements.length - this.tail + this.head;
40
+ };
41
+ RingQueue.prototype.isEmpty = function () {
42
+ return this.head === this.tail;
43
+ };
44
+ RingQueue.prototype.peek = function () {
45
+ return this.isEmpty() ? undefined : this.elements[this.tail];
46
+ };
47
+ RingQueue.prototype.read = function () {
48
+ var e = this.peek();
49
+ if (e !== undefined) {
50
+ this.tail = (this.tail + 1) % this.elements.length;
51
+ }
52
+ return e;
53
+ };
54
+ return RingQueue;
55
+ }());
56
+ exports.RingQueue = RingQueue;
57
+ //# sourceMappingURL=ring_queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ring_queue.js","sourceRoot":"","sources":["../../../../src/queue/ring_queue.ts"],"names":[],"mappings":";;;AAEA;;;;GAIG;AACH;IAKE,mBAAY,QAAgB;QAC1B,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,QAAQ,IAAI,CAAC,EAAE;YAChD,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;SACxD;QAED,IAAI,CAAC,QAAQ,GAAG,IAAI,KAAK,CAAI,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,2CAA2C;QACvF,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;QACd,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;IAChB,CAAC;IAED,uBAAG,GAAH,UAAI,OAAU;QACZ,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC;QACnC,IAAI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