@milaboratories/pl-client 2.17.7 → 2.17.8

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 (274) hide show
  1. package/dist/_virtual/_rolldown/runtime.cjs +43 -0
  2. package/dist/_virtual/_rolldown/runtime.js +18 -0
  3. package/dist/core/PromiseTracker.cjs +33 -33
  4. package/dist/core/PromiseTracker.cjs.map +1 -1
  5. package/dist/core/PromiseTracker.d.ts +10 -7
  6. package/dist/core/PromiseTracker.js +33 -31
  7. package/dist/core/PromiseTracker.js.map +1 -1
  8. package/dist/core/StatefulPromise.cjs +60 -61
  9. package/dist/core/StatefulPromise.cjs.map +1 -1
  10. package/dist/core/StatefulPromise.js +60 -60
  11. package/dist/core/StatefulPromise.js.map +1 -1
  12. package/dist/core/abstract_stream.d.ts +18 -15
  13. package/dist/core/advisory_locks.cjs +42 -49
  14. package/dist/core/advisory_locks.cjs.map +1 -1
  15. package/dist/core/advisory_locks.js +42 -48
  16. package/dist/core/advisory_locks.js.map +1 -1
  17. package/dist/core/auth.cjs +10 -15
  18. package/dist/core/auth.cjs.map +1 -1
  19. package/dist/core/auth.d.ts +7 -3
  20. package/dist/core/auth.js +10 -13
  21. package/dist/core/auth.js.map +1 -1
  22. package/dist/core/cache.d.ts +11 -7
  23. package/dist/core/client.cjs +255 -306
  24. package/dist/core/client.cjs.map +1 -1
  25. package/dist/core/client.d.ts +72 -68
  26. package/dist/core/client.js +253 -285
  27. package/dist/core/client.js.map +1 -1
  28. package/dist/core/config.cjs +81 -99
  29. package/dist/core/config.cjs.map +1 -1
  30. package/dist/core/config.d.ts +93 -90
  31. package/dist/core/config.js +81 -98
  32. package/dist/core/config.js.map +1 -1
  33. package/dist/core/default_client.cjs +84 -125
  34. package/dist/core/default_client.cjs.map +1 -1
  35. package/dist/core/default_client.d.ts +9 -6
  36. package/dist/core/default_client.js +78 -103
  37. package/dist/core/default_client.js.map +1 -1
  38. package/dist/core/driver.cjs +12 -16
  39. package/dist/core/driver.cjs.map +1 -1
  40. package/dist/core/driver.d.ts +18 -14
  41. package/dist/core/driver.js +12 -15
  42. package/dist/core/driver.js.map +1 -1
  43. package/dist/core/error_resource.cjs +5 -4
  44. package/dist/core/error_resource.cjs.map +1 -1
  45. package/dist/core/error_resource.js +5 -3
  46. package/dist/core/error_resource.js.map +1 -1
  47. package/dist/core/errors.cjs +104 -140
  48. package/dist/core/errors.cjs.map +1 -1
  49. package/dist/core/errors.d.ts +34 -30
  50. package/dist/core/errors.js +102 -137
  51. package/dist/core/errors.js.map +1 -1
  52. package/dist/core/final.cjs +63 -89
  53. package/dist/core/final.cjs.map +1 -1
  54. package/dist/core/final.d.ts +8 -4
  55. package/dist/core/final.js +63 -87
  56. package/dist/core/final.js.map +1 -1
  57. package/dist/core/ll_client.cjs +416 -521
  58. package/dist/core/ll_client.cjs.map +1 -1
  59. package/dist/core/ll_client.d.ts +100 -97
  60. package/dist/core/ll_client.js +415 -519
  61. package/dist/core/ll_client.js.map +1 -1
  62. package/dist/core/ll_transaction.cjs +206 -240
  63. package/dist/core/ll_transaction.cjs.map +1 -1
  64. package/dist/core/ll_transaction.d.ts +50 -52
  65. package/dist/core/ll_transaction.js +205 -238
  66. package/dist/core/ll_transaction.js.map +1 -1
  67. package/dist/core/stat.cjs +64 -63
  68. package/dist/core/stat.cjs.map +1 -1
  69. package/dist/core/stat.d.ts +35 -36
  70. package/dist/core/stat.js +64 -62
  71. package/dist/core/stat.js.map +1 -1
  72. package/dist/core/transaction.cjs +613 -650
  73. package/dist/core/transaction.cjs.map +1 -1
  74. package/dist/core/transaction.d.ts +165 -162
  75. package/dist/core/transaction.js +612 -648
  76. package/dist/core/transaction.js.map +1 -1
  77. package/dist/core/type_conversion.cjs +62 -83
  78. package/dist/core/type_conversion.cjs.map +1 -1
  79. package/dist/core/type_conversion.js +61 -81
  80. package/dist/core/type_conversion.js.map +1 -1
  81. package/dist/core/types.cjs +56 -86
  82. package/dist/core/types.cjs.map +1 -1
  83. package/dist/core/types.d.ts +63 -62
  84. package/dist/core/types.js +54 -83
  85. package/dist/core/types.js.map +1 -1
  86. package/dist/core/unauth_client.cjs +35 -41
  87. package/dist/core/unauth_client.cjs.map +1 -1
  88. package/dist/core/unauth_client.d.ts +18 -14
  89. package/dist/core/unauth_client.js +34 -39
  90. package/dist/core/unauth_client.js.map +1 -1
  91. package/dist/core/websocket_stream.cjs +277 -349
  92. package/dist/core/websocket_stream.cjs.map +1 -1
  93. package/dist/core/websocket_stream.js +275 -347
  94. package/dist/core/websocket_stream.js.map +1 -1
  95. package/dist/core/wire.d.ts +21 -17
  96. package/dist/helpers/pl.cjs +71 -73
  97. package/dist/helpers/pl.cjs.map +1 -1
  98. package/dist/helpers/pl.d.ts +40 -41
  99. package/dist/helpers/pl.js +66 -46
  100. package/dist/helpers/pl.js.map +1 -1
  101. package/dist/helpers/poll.cjs +99 -134
  102. package/dist/helpers/poll.cjs.map +1 -1
  103. package/dist/helpers/poll.d.ts +37 -34
  104. package/dist/helpers/poll.js +97 -113
  105. package/dist/helpers/poll.js.map +1 -1
  106. package/dist/helpers/retry_strategy.cjs +82 -87
  107. package/dist/helpers/retry_strategy.cjs.map +1 -1
  108. package/dist/helpers/retry_strategy.js +83 -86
  109. package/dist/helpers/retry_strategy.js.map +1 -1
  110. package/dist/helpers/tx_helpers.cjs +21 -20
  111. package/dist/helpers/tx_helpers.cjs.map +1 -1
  112. package/dist/helpers/tx_helpers.d.ts +11 -7
  113. package/dist/helpers/tx_helpers.js +20 -18
  114. package/dist/helpers/tx_helpers.js.map +1 -1
  115. package/dist/index.cjs +117 -106
  116. package/dist/index.d.ts +17 -17
  117. package/dist/index.js +17 -19
  118. package/dist/proto-grpc/github.com/googleapis/googleapis/google/rpc/status.cjs +72 -66
  119. package/dist/proto-grpc/github.com/googleapis/googleapis/google/rpc/status.cjs.map +1 -1
  120. package/dist/proto-grpc/github.com/googleapis/googleapis/google/rpc/status.d.ts +35 -37
  121. package/dist/proto-grpc/github.com/googleapis/googleapis/google/rpc/status.js +71 -64
  122. package/dist/proto-grpc/github.com/googleapis/googleapis/google/rpc/status.js.map +1 -1
  123. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.cjs +12611 -12866
  124. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.cjs.map +1 -1
  125. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.client.cjs +226 -226
  126. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.client.cjs.map +1 -1
  127. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.client.d.ts +281 -330
  128. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.client.js +225 -224
  129. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.client.js.map +1 -1
  130. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts +2640 -4294
  131. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.js +12608 -12706
  132. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.js.map +1 -1
  133. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api_types.cjs +1230 -1089
  134. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api_types.cjs.map +1 -1
  135. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api_types.d.ts +393 -420
  136. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api_types.js +1228 -1083
  137. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api_types.js.map +1 -1
  138. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/base_types.cjs +142 -143
  139. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/base_types.cjs.map +1 -1
  140. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/base_types.d.ts +62 -64
  141. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/base_types.js +140 -141
  142. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/base_types.js.map +1 -1
  143. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/resource_types.cjs +572 -487
  144. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/resource_types.cjs.map +1 -1
  145. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/resource_types.d.ts +125 -228
  146. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/resource_types.js +572 -485
  147. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/resource_types.js.map +1 -1
  148. package/dist/proto-grpc/google/protobuf/any.cjs +131 -146
  149. package/dist/proto-grpc/google/protobuf/any.cjs.map +1 -1
  150. package/dist/proto-grpc/google/protobuf/any.d.ts +78 -84
  151. package/dist/proto-grpc/google/protobuf/any.js +130 -144
  152. package/dist/proto-grpc/google/protobuf/any.js.map +1 -1
  153. package/dist/proto-grpc/google/protobuf/duration.cjs +92 -100
  154. package/dist/proto-grpc/google/protobuf/duration.cjs.map +1 -1
  155. package/dist/proto-grpc/google/protobuf/duration.d.ts +38 -43
  156. package/dist/proto-grpc/google/protobuf/duration.js +91 -98
  157. package/dist/proto-grpc/google/protobuf/duration.js.map +1 -1
  158. package/dist/proto-grpc/google/protobuf/timestamp.cjs +117 -128
  159. package/dist/proto-grpc/google/protobuf/timestamp.cjs.map +1 -1
  160. package/dist/proto-grpc/google/protobuf/timestamp.d.ts +50 -55
  161. package/dist/proto-grpc/google/protobuf/timestamp.js +116 -126
  162. package/dist/proto-grpc/google/protobuf/timestamp.js.map +1 -1
  163. package/dist/proto-grpc/google/rpc/code.cjs +223 -238
  164. package/dist/proto-grpc/google/rpc/code.cjs.map +1 -1
  165. package/dist/proto-grpc/google/rpc/code.d.ts +209 -206
  166. package/dist/proto-grpc/google/rpc/code.js +221 -237
  167. package/dist/proto-grpc/google/rpc/code.js.map +1 -1
  168. package/dist/proto-rest/index.cjs +67 -66
  169. package/dist/proto-rest/index.cjs.map +1 -1
  170. package/dist/proto-rest/index.d.ts +24 -18
  171. package/dist/proto-rest/index.js +61 -65
  172. package/dist/proto-rest/index.js.map +1 -1
  173. package/dist/proto-rest/plapi.d.ts +1400 -1477
  174. package/dist/test/tcp-proxy.cjs +100 -126
  175. package/dist/test/tcp-proxy.cjs.map +1 -1
  176. package/dist/test/tcp-proxy.d.ts +17 -13
  177. package/dist/test/tcp-proxy.js +97 -104
  178. package/dist/test/tcp-proxy.js.map +1 -1
  179. package/dist/test/test_config.cjs +145 -194
  180. package/dist/test/test_config.cjs.map +1 -1
  181. package/dist/test/test_config.d.ts +34 -30
  182. package/dist/test/test_config.js +138 -166
  183. package/dist/test/test_config.js.map +1 -1
  184. package/dist/util/pl.cjs +4 -3
  185. package/dist/util/pl.cjs.map +1 -1
  186. package/dist/util/pl.js +4 -2
  187. package/dist/util/pl.js.map +1 -1
  188. package/dist/util/util.cjs +7 -10
  189. package/dist/util/util.cjs.map +1 -1
  190. package/dist/util/util.js +7 -9
  191. package/dist/util/util.js.map +1 -1
  192. package/package.json +8 -8
  193. package/src/core/transaction.ts +123 -133
  194. package/dist/__external/.pnpm/@rollup_plugin-typescript@12.3.0_rollup@4.52.4_tslib@2.8.1_typescript@5.6.3/__external/tslib/tslib.es6.cjs +0 -61
  195. package/dist/__external/.pnpm/@rollup_plugin-typescript@12.3.0_rollup@4.52.4_tslib@2.8.1_typescript@5.6.3/__external/tslib/tslib.es6.cjs.map +0 -1
  196. package/dist/__external/.pnpm/@rollup_plugin-typescript@12.3.0_rollup@4.52.4_tslib@2.8.1_typescript@5.6.3/__external/tslib/tslib.es6.js +0 -58
  197. package/dist/__external/.pnpm/@rollup_plugin-typescript@12.3.0_rollup@4.52.4_tslib@2.8.1_typescript@5.6.3/__external/tslib/tslib.es6.js.map +0 -1
  198. package/dist/core/PromiseTracker.d.ts.map +0 -1
  199. package/dist/core/StatefulPromise.d.ts +0 -39
  200. package/dist/core/StatefulPromise.d.ts.map +0 -1
  201. package/dist/core/abstract_stream.d.ts.map +0 -1
  202. package/dist/core/advisory_locks.d.ts +0 -10
  203. package/dist/core/advisory_locks.d.ts.map +0 -1
  204. package/dist/core/auth.d.ts.map +0 -1
  205. package/dist/core/cache.d.ts.map +0 -1
  206. package/dist/core/client.d.ts.map +0 -1
  207. package/dist/core/config.d.ts.map +0 -1
  208. package/dist/core/default_client.d.ts.map +0 -1
  209. package/dist/core/driver.d.ts.map +0 -1
  210. package/dist/core/error_resource.d.ts +0 -6
  211. package/dist/core/error_resource.d.ts.map +0 -1
  212. package/dist/core/errors.d.ts.map +0 -1
  213. package/dist/core/final.d.ts.map +0 -1
  214. package/dist/core/ll_client.d.ts.map +0 -1
  215. package/dist/core/ll_transaction.d.ts.map +0 -1
  216. package/dist/core/stat.d.ts.map +0 -1
  217. package/dist/core/transaction.d.ts.map +0 -1
  218. package/dist/core/type_conversion.d.ts +0 -8
  219. package/dist/core/type_conversion.d.ts.map +0 -1
  220. package/dist/core/types.d.ts.map +0 -1
  221. package/dist/core/unauth_client.d.ts.map +0 -1
  222. package/dist/core/websocket_stream.d.ts +0 -67
  223. package/dist/core/websocket_stream.d.ts.map +0 -1
  224. package/dist/core/wire.d.ts.map +0 -1
  225. package/dist/helpers/pl.d.ts.map +0 -1
  226. package/dist/helpers/poll.d.ts.map +0 -1
  227. package/dist/helpers/retry_strategy.d.ts +0 -24
  228. package/dist/helpers/retry_strategy.d.ts.map +0 -1
  229. package/dist/helpers/state_helpers.d.ts +0 -3
  230. package/dist/helpers/state_helpers.d.ts.map +0 -1
  231. package/dist/helpers/tx_helpers.d.ts.map +0 -1
  232. package/dist/index.cjs.map +0 -1
  233. package/dist/index.d.ts.map +0 -1
  234. package/dist/index.js.map +0 -1
  235. package/dist/proto-grpc/github.com/googleapis/googleapis/google/rpc/status.d.ts.map +0 -1
  236. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.client.d.ts.map +0 -1
  237. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts.map +0 -1
  238. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api_types.d.ts.map +0 -1
  239. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/base_types.d.ts.map +0 -1
  240. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/import.d.ts +0 -106
  241. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/import.d.ts.map +0 -1
  242. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/resource_types.d.ts.map +0 -1
  243. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/ws-test.d.ts +0 -73
  244. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/ws-test.d.ts.map +0 -1
  245. package/dist/proto-grpc/google/api/http.d.ts +0 -456
  246. package/dist/proto-grpc/google/api/http.d.ts.map +0 -1
  247. package/dist/proto-grpc/google/protobuf/any.d.ts.map +0 -1
  248. package/dist/proto-grpc/google/protobuf/descriptor.d.ts +0 -2340
  249. package/dist/proto-grpc/google/protobuf/descriptor.d.ts.map +0 -1
  250. package/dist/proto-grpc/google/protobuf/duration.d.ts.map +0 -1
  251. package/dist/proto-grpc/google/protobuf/empty.d.ts +0 -32
  252. package/dist/proto-grpc/google/protobuf/empty.d.ts.map +0 -1
  253. package/dist/proto-grpc/google/protobuf/struct.d.ts +0 -187
  254. package/dist/proto-grpc/google/protobuf/struct.d.ts.map +0 -1
  255. package/dist/proto-grpc/google/protobuf/timestamp.d.ts.map +0 -1
  256. package/dist/proto-grpc/google/protobuf/wrappers.d.ts +0 -308
  257. package/dist/proto-grpc/google/protobuf/wrappers.d.ts.map +0 -1
  258. package/dist/proto-grpc/google/rpc/code.d.ts.map +0 -1
  259. package/dist/proto-grpc/google/rpc/error_details.d.ts +0 -654
  260. package/dist/proto-grpc/google/rpc/error_details.d.ts.map +0 -1
  261. package/dist/proto-grpc/google/rpc/http.d.ts +0 -121
  262. package/dist/proto-grpc/google/rpc/http.d.ts.map +0 -1
  263. package/dist/proto-grpc/google/rpc/status.d.ts +0 -55
  264. package/dist/proto-grpc/google/rpc/status.d.ts.map +0 -1
  265. package/dist/proto-rest/index.d.ts.map +0 -1
  266. package/dist/proto-rest/plapi.d.ts.map +0 -1
  267. package/dist/test/tcp-proxy.d.ts.map +0 -1
  268. package/dist/test/test_config.d.ts.map +0 -1
  269. package/dist/util/branding.d.ts +0 -7
  270. package/dist/util/branding.d.ts.map +0 -1
  271. package/dist/util/pl.d.ts +0 -9
  272. package/dist/util/pl.d.ts.map +0 -1
  273. package/dist/util/util.d.ts +0 -2
  274. package/dist/util/util.d.ts.map +0 -1
@@ -1,123 +1,107 @@
1
- import { notEmpty, createRetryState, nextRetryStateOrError } from '@milaboratories/ts-helpers';
2
- import { isNullResourceId, isNotNullResourceId, resourceIdToString } from '../core/types.js';
3
- import * as tp from 'node:timers/promises';
1
+ import { isNotNullResourceId, isNullResourceId, resourceIdToString } from "../core/types.js";
2
+ import { createRetryState, nextRetryStateOrError, notEmpty } from "@milaboratories/ts-helpers";
3
+ import * as tp from "node:timers/promises";
4
4
 
5
+ //#region src/helpers/poll.ts
5
6
  /** This error tells state assertion mechanism that required state is not yet ready */
6
- class ContinuePolling extends Error {
7
- name = "ContinuePolling";
8
- }
7
+ var ContinuePolling = class extends Error {
8
+ name = "ContinuePolling";
9
+ };
9
10
  const DefaultPollFieldTraverseOps = {
10
- failOnError: true,
11
- onlyFinal: false,
11
+ failOnError: true,
12
+ onlyFinal: false
13
+ };
14
+ var PollResourceAccessor = class {
15
+ constructor(tx, data, path) {
16
+ this.tx = tx;
17
+ this.data = data;
18
+ this.path = path;
19
+ }
20
+ final() {
21
+ if (!this.data.final) throw new ContinuePolling();
22
+ return this;
23
+ }
24
+ async requireNoError() {
25
+ if (isNullResourceId(this.data.error)) return this;
26
+ await this.tx.throwError(this.data.error, this.path);
27
+ return this;
28
+ }
29
+ getFieldData(name, expectedType) {
30
+ const fieldData = this.data.fields.find((f) => f.name === name);
31
+ if (fieldData !== void 0) {
32
+ if (expectedType !== void 0 && fieldData.type !== expectedType) throw new Error(`Unexpected field type. Expected ${expectedType}, found ${fieldData.type}`);
33
+ return fieldData;
34
+ }
35
+ if ((expectedType === "Input" || expectedType === "Service") && this.data.inputsLocked || expectedType === "Output" && this.data.outputsLocked) throw new Error(`Field "${name}" not found. Expected type: ${expectedType}, state: ${this.data}`);
36
+ throw new ContinuePolling();
37
+ }
38
+ async get(name, ops = {}) {
39
+ const { expectedType, failOnError } = {
40
+ ...DefaultPollFieldTraverseOps,
41
+ ...ops
42
+ };
43
+ const path = [...this.path, name];
44
+ const fieldData = this.getFieldData(name, expectedType);
45
+ if (isNotNullResourceId(fieldData.error) && (failOnError || isNullResourceId(fieldData.value))) await this.tx.throwError(fieldData.error, path);
46
+ if (isNullResourceId(fieldData.value)) throw new ContinuePolling();
47
+ return await this.tx.get(fieldData.value, failOnError, path);
48
+ }
49
+ async getMulti(ops, ...names) {
50
+ return await Promise.all(names.map((name) => this.get(name, ops)));
51
+ }
52
+ async getMultiObj(ops, ...names) {
53
+ return Object.fromEntries(await Promise.all(names.map(async (name) => [name, await this.get(name, ops)])));
54
+ }
55
+ async getAllFinal(ops = {}) {
56
+ return await this.getMultiObj(ops, ...this.data.fields.filter((f) => f.valueIsFinal || isNotNullResourceId(f.error)).map((f) => f.name));
57
+ }
58
+ async getKValue(key) {
59
+ const value = await this.tx.tx.getKValueStringIfExists(this.data.id, key);
60
+ if (value === void 0) throw new ContinuePolling();
61
+ return value;
62
+ }
63
+ async getKValueObj(key) {
64
+ return JSON.parse(await this.getKValue(key));
65
+ }
66
+ };
67
+ var PollTxAccessor = class {
68
+ constructor(tx) {
69
+ this.tx = tx;
70
+ }
71
+ async get(rid, failOnError = true, path = []) {
72
+ const data = await this.tx.getResourceData(rid, true);
73
+ const accessor = new PollResourceAccessor(this, data, [...path, resourceIdToString(rid)]);
74
+ if (failOnError) await accessor.requireNoError();
75
+ return accessor;
76
+ }
77
+ async throwError(error, path = []) {
78
+ const errorRes = await this.get(error);
79
+ const errorText = Buffer.from(notEmpty(errorRes.data.data)).toString();
80
+ throw new Error(`${path.join(" -> ")} = ${errorText}`);
81
+ }
12
82
  };
13
- class PollResourceAccessor {
14
- tx;
15
- data;
16
- path;
17
- constructor(tx, data, path) {
18
- this.tx = tx;
19
- this.data = data;
20
- this.path = path;
21
- }
22
- final() {
23
- if (!this.data.final)
24
- throw new ContinuePolling();
25
- return this;
26
- }
27
- async requireNoError() {
28
- if (isNullResourceId(this.data.error))
29
- return this;
30
- await this.tx.throwError(this.data.error, this.path);
31
- // hmm... https://github.com/microsoft/TypeScript/issues/34955
32
- return this;
33
- }
34
- getFieldData(name, expectedType) {
35
- const fieldData = this.data.fields.find((f) => f.name === name);
36
- if (fieldData !== undefined) {
37
- if (expectedType !== undefined && fieldData.type !== expectedType)
38
- throw new Error(`Unexpected field type. Expected ${expectedType}, found ${fieldData.type}`);
39
- return fieldData;
40
- }
41
- if (((expectedType === "Input" || expectedType === "Service") && this.data.inputsLocked) ||
42
- (expectedType === "Output" && this.data.outputsLocked))
43
- throw new Error(
44
- // eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions
45
- `Field "${name}" not found. Expected type: ${expectedType}, state: ${this.data}`);
46
- throw new ContinuePolling();
47
- }
48
- async get(name, ops = {}) {
49
- const { expectedType, failOnError } = { ...DefaultPollFieldTraverseOps, ...ops };
50
- const path = [...this.path, name];
51
- const fieldData = this.getFieldData(name, expectedType);
52
- if (isNotNullResourceId(fieldData.error) && (failOnError || isNullResourceId(fieldData.value)))
53
- await this.tx.throwError(fieldData.error, path);
54
- if (isNullResourceId(fieldData.value))
55
- throw new ContinuePolling();
56
- return await this.tx.get(fieldData.value, failOnError, path);
57
- }
58
- async getMulti(ops, ...names) {
59
- return await Promise.all(names.map((name) => this.get(name, ops)));
60
- }
61
- async getMultiObj(ops, ...names) {
62
- return Object.fromEntries(await Promise.all(names.map(async (name) => [name, await this.get(name, ops)])));
63
- }
64
- async getAllFinal(ops = {}) {
65
- return await this.getMultiObj(ops, ...this.data.fields
66
- .filter((f) => f.valueIsFinal || isNotNullResourceId(f.error))
67
- .map((f) => f.name));
68
- }
69
- async getKValue(key) {
70
- const value = await this.tx.tx.getKValueStringIfExists(this.data.id, key);
71
- if (value === undefined)
72
- throw new ContinuePolling();
73
- return value;
74
- }
75
- async getKValueObj(key) {
76
- return JSON.parse(await this.getKValue(key));
77
- }
78
- }
79
- class PollTxAccessor {
80
- tx;
81
- constructor(tx) {
82
- this.tx = tx;
83
- }
84
- async get(rid, failOnError = true, path = []) {
85
- const data = await this.tx.getResourceData(rid, true);
86
- const accessor = new PollResourceAccessor(this, data, [...path, resourceIdToString(rid)]);
87
- if (failOnError)
88
- await accessor.requireNoError();
89
- return accessor;
90
- }
91
- async throwError(error, path = []) {
92
- const errorRes = await this.get(error);
93
- const errorText = Buffer.from(notEmpty(errorRes.data.data)).toString();
94
- throw new Error(`${path.join(" -> ")} = ${errorText}`);
95
- }
96
- }
97
83
  const DefaultPollingRetryOptions = {
98
- type: "linearBackoff",
99
- jitter: 0,
100
- maxAttempts: 100,
101
- backoffStep: 10,
102
- initialDelay: 10,
84
+ type: "linearBackoff",
85
+ jitter: 0,
86
+ maxAttempts: 100,
87
+ backoffStep: 10,
88
+ initialDelay: 10
103
89
  };
104
90
  async function poll(cl, cb, retryOptions = DefaultPollingRetryOptions, txName = "polling") {
105
- let retryState = createRetryState(retryOptions);
106
- while (true) {
107
- try {
108
- return await cl.withReadTx(txName, async (tx) => {
109
- return await cb(new PollTxAccessor(tx));
110
- });
111
- }
112
- catch (e) {
113
- // Rethrowing any error except the "not ready yet"
114
- if (!(e instanceof ContinuePolling))
115
- throw e;
116
- }
117
- await tp.setTimeout(retryState.nextDelay);
118
- retryState = nextRetryStateOrError(retryState);
119
- }
91
+ let retryState = createRetryState(retryOptions);
92
+ while (true) {
93
+ try {
94
+ return await cl.withReadTx(txName, async (tx) => {
95
+ return await cb(new PollTxAccessor(tx));
96
+ });
97
+ } catch (e) {
98
+ if (!(e instanceof ContinuePolling)) throw e;
99
+ }
100
+ await tp.setTimeout(retryState.nextDelay);
101
+ retryState = nextRetryStateOrError(retryState);
102
+ }
120
103
  }
121
104
 
105
+ //#endregion
122
106
  export { ContinuePolling, DefaultPollingRetryOptions, PollResourceAccessor, PollTxAccessor, poll };
123
- //# sourceMappingURL=poll.js.map
107
+ //# sourceMappingURL=poll.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"poll.js","sources":["../../src/helpers/poll.ts"],"sourcesContent":["import type { PlClient } from \"../core/client\";\nimport type { RetryOptions } from \"@milaboratories/ts-helpers\";\nimport { createRetryState, nextRetryStateOrError, notEmpty } from \"@milaboratories/ts-helpers\";\nimport type { FieldData, FieldType, ResourceData, ResourceId } from \"../core/types\";\nimport { isNotNullResourceId, isNullResourceId, resourceIdToString } from \"../core/types\";\nimport type { PlTransaction } from \"../core/transaction\";\nimport * as tp from \"node:timers/promises\";\n\n/** This error tells state assertion mechanism that required state is not yet ready */\nexport class ContinuePolling extends Error {\n name = \"ContinuePolling\";\n}\n\nexport type PollFieldTraverseOps = {\n expectedType?: FieldType;\n /** Fail if error present along with the value, if value not present,\n * but error do, exception will be thrown anyway. */\n failOnError: boolean;\n /** Traverse only if field report its value as final. */\n onlyFinal: boolean;\n};\n\nconst DefaultPollFieldTraverseOps: PollFieldTraverseOps = {\n failOnError: true,\n onlyFinal: false,\n};\n\nexport class PollResourceAccessor {\n constructor(\n public readonly tx: PollTxAccessor,\n public readonly data: ResourceData,\n public readonly path: string[],\n ) {}\n\n public final(): PollResourceAccessor {\n if (!this.data.final) throw new ContinuePolling();\n return this;\n }\n\n public async requireNoError(): Promise<PollResourceAccessor> {\n if (isNullResourceId(this.data.error)) return this;\n await this.tx.throwError(this.data.error, this.path);\n // hmm... https://github.com/microsoft/TypeScript/issues/34955\n return this;\n }\n\n public getFieldData(name: string, expectedType?: FieldType): FieldData {\n const fieldData = this.data.fields.find((f) => f.name === name);\n\n if (fieldData !== undefined) {\n if (expectedType !== undefined && fieldData.type !== expectedType)\n throw new Error(`Unexpected field type. Expected ${expectedType}, found ${fieldData.type}`);\n return fieldData;\n }\n\n if (\n ((expectedType === \"Input\" || expectedType === \"Service\") && this.data.inputsLocked) ||\n (expectedType === \"Output\" && this.data.outputsLocked)\n )\n throw new Error(\n // eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions\n `Field \"${name}\" not found. Expected type: ${expectedType}, state: ${this.data}`,\n );\n\n throw new ContinuePolling();\n }\n\n public async get(\n name: string,\n ops: Partial<PollFieldTraverseOps> = {},\n ): Promise<PollResourceAccessor> {\n const { expectedType, failOnError } = { ...DefaultPollFieldTraverseOps, ...ops };\n const path = [...this.path, name];\n\n const fieldData = this.getFieldData(name, expectedType);\n if (isNotNullResourceId(fieldData.error) && (failOnError || isNullResourceId(fieldData.value)))\n await this.tx.throwError(fieldData.error, path);\n\n if (isNullResourceId(fieldData.value)) throw new ContinuePolling();\n\n return await this.tx.get(fieldData.value, failOnError, path);\n }\n\n public async getMulti(\n ops: Partial<PollFieldTraverseOps>,\n ...names: string[]\n ): Promise<PollResourceAccessor[]> {\n return await Promise.all(names.map((name) => this.get(name, ops)));\n }\n\n public async getMultiObj<Key extends string>(\n ops: Partial<PollFieldTraverseOps>,\n ...names: Key[]\n ): Promise<Record<Key, PollResourceAccessor>> {\n return Object.fromEntries(\n await Promise.all(names.map(async (name) => [name, await this.get(name, ops)])),\n );\n }\n\n public async getAllFinal(\n ops: Partial<PollFieldTraverseOps> = {},\n ): Promise<Record<string, PollResourceAccessor>> {\n return await this.getMultiObj(\n ops,\n ...this.data.fields\n .filter((f) => f.valueIsFinal || isNotNullResourceId(f.error))\n .map((f) => f.name),\n );\n }\n\n public async getKValue(key: string): Promise<string> {\n const value = await this.tx.tx.getKValueStringIfExists(this.data.id, key);\n if (value === undefined) throw new ContinuePolling();\n return value;\n }\n\n public async getKValueObj<T>(key: string): Promise<T> {\n return JSON.parse(await this.getKValue(key)) as T;\n }\n}\n\nexport class PollTxAccessor {\n constructor(public readonly tx: PlTransaction) {}\n\n public async get(\n rid: ResourceId,\n failOnError: boolean = true,\n path: string[] = [],\n ): Promise<PollResourceAccessor> {\n const data = await this.tx.getResourceData(rid, true);\n const accessor = new PollResourceAccessor(this, data, [...path, resourceIdToString(rid)]);\n if (failOnError) await accessor.requireNoError();\n return accessor;\n }\n\n async throwError(error: ResourceId, path: string[] = []): Promise<never> {\n const errorRes = await this.get(error);\n const errorText = Buffer.from(notEmpty(errorRes.data.data)).toString();\n throw new Error(`${path.join(\" -> \")} = ${errorText}`);\n }\n}\n\nexport const DefaultPollingRetryOptions: RetryOptions = {\n type: \"linearBackoff\",\n jitter: 0,\n maxAttempts: 100,\n backoffStep: 10,\n initialDelay: 10,\n};\n\nexport async function poll<T>(\n cl: PlClient,\n cb: (tx: PollTxAccessor) => Promise<T>,\n retryOptions: RetryOptions = DefaultPollingRetryOptions,\n txName: string = \"polling\",\n): Promise<T> {\n let retryState = createRetryState(retryOptions);\n while (true) {\n try {\n return await cl.withReadTx(txName, async (tx) => {\n return await cb(new PollTxAccessor(tx));\n });\n } catch (e: any) {\n // Rethrowing any error except the \"not ready yet\"\n if (!(e instanceof ContinuePolling)) throw e;\n }\n await tp.setTimeout(retryState.nextDelay);\n retryState = nextRetryStateOrError(retryState);\n }\n}\n"],"names":[],"mappings":";;;;AAQA;AACM,MAAO,eAAgB,SAAQ,KAAK,CAAA;IACxC,IAAI,GAAG,iBAAiB;AACzB;AAWD,MAAM,2BAA2B,GAAyB;AACxD,IAAA,WAAW,EAAE,IAAI;AACjB,IAAA,SAAS,EAAE,KAAK;CACjB;MAEY,oBAAoB,CAAA;AAEb,IAAA,EAAA;AACA,IAAA,IAAA;AACA,IAAA,IAAA;AAHlB,IAAA,WAAA,CACkB,EAAkB,EAClB,IAAkB,EAClB,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;;;"}
1
+ {"version":3,"file":"poll.js","names":[],"sources":["../../src/helpers/poll.ts"],"sourcesContent":["import type { PlClient } from \"../core/client\";\nimport type { RetryOptions } from \"@milaboratories/ts-helpers\";\nimport { createRetryState, nextRetryStateOrError, notEmpty } from \"@milaboratories/ts-helpers\";\nimport type { FieldData, FieldType, ResourceData, ResourceId } from \"../core/types\";\nimport { isNotNullResourceId, isNullResourceId, resourceIdToString } from \"../core/types\";\nimport type { PlTransaction } from \"../core/transaction\";\nimport * as tp from \"node:timers/promises\";\n\n/** This error tells state assertion mechanism that required state is not yet ready */\nexport class ContinuePolling extends Error {\n name = \"ContinuePolling\";\n}\n\nexport type PollFieldTraverseOps = {\n expectedType?: FieldType;\n /** Fail if error present along with the value, if value not present,\n * but error do, exception will be thrown anyway. */\n failOnError: boolean;\n /** Traverse only if field report its value as final. */\n onlyFinal: boolean;\n};\n\nconst DefaultPollFieldTraverseOps: PollFieldTraverseOps = {\n failOnError: true,\n onlyFinal: false,\n};\n\nexport class PollResourceAccessor {\n constructor(\n public readonly tx: PollTxAccessor,\n public readonly data: ResourceData,\n public readonly path: string[],\n ) {}\n\n public final(): PollResourceAccessor {\n if (!this.data.final) throw new ContinuePolling();\n return this;\n }\n\n public async requireNoError(): Promise<PollResourceAccessor> {\n if (isNullResourceId(this.data.error)) return this;\n await this.tx.throwError(this.data.error, this.path);\n // hmm... https://github.com/microsoft/TypeScript/issues/34955\n return this;\n }\n\n public getFieldData(name: string, expectedType?: FieldType): FieldData {\n const fieldData = this.data.fields.find((f) => f.name === name);\n\n if (fieldData !== undefined) {\n if (expectedType !== undefined && fieldData.type !== expectedType)\n throw new Error(`Unexpected field type. Expected ${expectedType}, found ${fieldData.type}`);\n return fieldData;\n }\n\n if (\n ((expectedType === \"Input\" || expectedType === \"Service\") && this.data.inputsLocked) ||\n (expectedType === \"Output\" && this.data.outputsLocked)\n )\n throw new Error(\n // eslint-disable-next-line @typescript-eslint/no-base-to-string, @typescript-eslint/restrict-template-expressions\n `Field \"${name}\" not found. Expected type: ${expectedType}, state: ${this.data}`,\n );\n\n throw new ContinuePolling();\n }\n\n public async get(\n name: string,\n ops: Partial<PollFieldTraverseOps> = {},\n ): Promise<PollResourceAccessor> {\n const { expectedType, failOnError } = { ...DefaultPollFieldTraverseOps, ...ops };\n const path = [...this.path, name];\n\n const fieldData = this.getFieldData(name, expectedType);\n if (isNotNullResourceId(fieldData.error) && (failOnError || isNullResourceId(fieldData.value)))\n await this.tx.throwError(fieldData.error, path);\n\n if (isNullResourceId(fieldData.value)) throw new ContinuePolling();\n\n return await this.tx.get(fieldData.value, failOnError, path);\n }\n\n public async getMulti(\n ops: Partial<PollFieldTraverseOps>,\n ...names: string[]\n ): Promise<PollResourceAccessor[]> {\n return await Promise.all(names.map((name) => this.get(name, ops)));\n }\n\n public async getMultiObj<Key extends string>(\n ops: Partial<PollFieldTraverseOps>,\n ...names: Key[]\n ): Promise<Record<Key, PollResourceAccessor>> {\n return Object.fromEntries(\n await Promise.all(names.map(async (name) => [name, await this.get(name, ops)])),\n );\n }\n\n public async getAllFinal(\n ops: Partial<PollFieldTraverseOps> = {},\n ): Promise<Record<string, PollResourceAccessor>> {\n return await this.getMultiObj(\n ops,\n ...this.data.fields\n .filter((f) => f.valueIsFinal || isNotNullResourceId(f.error))\n .map((f) => f.name),\n );\n }\n\n public async getKValue(key: string): Promise<string> {\n const value = await this.tx.tx.getKValueStringIfExists(this.data.id, key);\n if (value === undefined) throw new ContinuePolling();\n return value;\n }\n\n public async getKValueObj<T>(key: string): Promise<T> {\n return JSON.parse(await this.getKValue(key)) as T;\n }\n}\n\nexport class PollTxAccessor {\n constructor(public readonly tx: PlTransaction) {}\n\n public async get(\n rid: ResourceId,\n failOnError: boolean = true,\n path: string[] = [],\n ): Promise<PollResourceAccessor> {\n const data = await this.tx.getResourceData(rid, true);\n const accessor = new PollResourceAccessor(this, data, [...path, resourceIdToString(rid)]);\n if (failOnError) await accessor.requireNoError();\n return accessor;\n }\n\n async throwError(error: ResourceId, path: string[] = []): Promise<never> {\n const errorRes = await this.get(error);\n const errorText = Buffer.from(notEmpty(errorRes.data.data)).toString();\n throw new Error(`${path.join(\" -> \")} = ${errorText}`);\n }\n}\n\nexport const DefaultPollingRetryOptions: RetryOptions = {\n type: \"linearBackoff\",\n jitter: 0,\n maxAttempts: 100,\n backoffStep: 10,\n initialDelay: 10,\n};\n\nexport async function poll<T>(\n cl: PlClient,\n cb: (tx: PollTxAccessor) => Promise<T>,\n retryOptions: RetryOptions = DefaultPollingRetryOptions,\n txName: string = \"polling\",\n): Promise<T> {\n let retryState = createRetryState(retryOptions);\n while (true) {\n try {\n return await cl.withReadTx(txName, async (tx) => {\n return await cb(new PollTxAccessor(tx));\n });\n } catch (e: any) {\n // Rethrowing any error except the \"not ready yet\"\n if (!(e instanceof ContinuePolling)) throw e;\n }\n await tp.setTimeout(retryState.nextDelay);\n retryState = nextRetryStateOrError(retryState);\n }\n}\n"],"mappings":";;;;;;AASA,IAAa,kBAAb,cAAqC,MAAM;CACzC,OAAO;;AAYT,MAAM,8BAAoD;CACxD,aAAa;CACb,WAAW;CACZ;AAED,IAAa,uBAAb,MAAkC;CAChC,YACE,AAAgB,IAChB,AAAgB,MAChB,AAAgB,MAChB;EAHgB;EACA;EACA;;CAGlB,AAAO,QAA8B;AACnC,MAAI,CAAC,KAAK,KAAK,MAAO,OAAM,IAAI,iBAAiB;AACjD,SAAO;;CAGT,MAAa,iBAAgD;AAC3D,MAAI,iBAAiB,KAAK,KAAK,MAAM,CAAE,QAAO;AAC9C,QAAM,KAAK,GAAG,WAAW,KAAK,KAAK,OAAO,KAAK,KAAK;AAEpD,SAAO;;CAGT,AAAO,aAAa,MAAc,cAAqC;EACrE,MAAM,YAAY,KAAK,KAAK,OAAO,MAAM,MAAM,EAAE,SAAS,KAAK;AAE/D,MAAI,cAAc,QAAW;AAC3B,OAAI,iBAAiB,UAAa,UAAU,SAAS,aACnD,OAAM,IAAI,MAAM,mCAAmC,aAAa,UAAU,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- maxDelay: 30000,
4
+ maxAttempts: 10,
5
+ initialDelay: 100,
6
+ maxDelay: 3e4
7
+ };
8
+ var RetryStrategy = class {
9
+ attempts = 0;
10
+ timer = null;
11
+ config;
12
+ callbacks;
13
+ backoff;
14
+ constructor(config, callbacks) {
15
+ this.config = {
16
+ ...DEFAULT_RETRY_CONFIG,
17
+ ...config
18
+ };
19
+ this.callbacks = callbacks;
20
+ this.backoff = new ExponentialBackoff({
21
+ initialDelay: this.config.initialDelay,
22
+ maxDelay: this.config.maxDelay,
23
+ factor: 2,
24
+ jitter: .1
25
+ });
26
+ }
27
+ schedule() {
28
+ if (this.timer) return;
29
+ if (this.hasExceededLimit()) {
30
+ this.notifyMaxAttemptsReached();
31
+ return;
32
+ }
33
+ this.timer = setTimeout(() => {
34
+ this.timer = null;
35
+ this.attempts++;
36
+ this.callbacks.onRetry();
37
+ }, this.backoff.delay());
38
+ }
39
+ cancel() {
40
+ if (this.timer) {
41
+ clearTimeout(this.timer);
42
+ this.timer = null;
43
+ }
44
+ }
45
+ reset() {
46
+ this.attempts = 0;
47
+ this.backoff.reset();
48
+ }
49
+ hasExceededLimit() {
50
+ return this.attempts >= this.config.maxAttempts;
51
+ }
52
+ notifyMaxAttemptsReached() {
53
+ const error = /* @__PURE__ */ new Error(`Max retry attempts (${this.config.maxAttempts}) reached`);
54
+ this.callbacks.onMaxAttemptsReached(error);
55
+ }
56
+ };
57
+ var ExponentialBackoff = class {
58
+ initialDelay;
59
+ maxDelay;
60
+ currentDelay;
61
+ factor;
62
+ jitter;
63
+ constructor(config) {
64
+ this.initialDelay = config.initialDelay;
65
+ this.maxDelay = config.maxDelay;
66
+ this.factor = config.factor;
67
+ this.jitter = config.jitter;
68
+ this.currentDelay = config.initialDelay;
69
+ }
70
+ delay() {
71
+ if (this.currentDelay >= this.maxDelay) return this.applyJitter(this.maxDelay);
72
+ this.currentDelay = this.currentDelay * this.factor;
73
+ if (this.currentDelay > this.maxDelay) this.currentDelay = this.maxDelay;
74
+ return this.applyJitter(this.currentDelay);
75
+ }
76
+ reset() {
77
+ this.currentDelay = this.initialDelay;
78
+ }
79
+ applyJitter(delay) {
80
+ if (delay === 0 || this.jitter === 0) return delay;
81
+ return delay * (1 - this.jitter / 2 + Math.random() * this.jitter);
82
+ }
7
83
  };
8
- class RetryStrategy {
9
- attempts = 0;
10
- timer = null;
11
- config;
12
- callbacks;
13
- backoff;
14
- constructor(config, callbacks) {
15
- this.config = { ...DEFAULT_RETRY_CONFIG, ...config };
16
- this.callbacks = callbacks;
17
- this.backoff = new ExponentialBackoff({
18
- initialDelay: this.config.initialDelay,
19
- maxDelay: this.config.maxDelay,
20
- factor: 2,
21
- jitter: 0.1,
22
- });
23
- }
24
- schedule() {
25
- if (this.timer)
26
- return;
27
- if (this.hasExceededLimit()) {
28
- this.notifyMaxAttemptsReached();
29
- return;
30
- }
31
- this.timer = setTimeout(() => {
32
- this.timer = null;
33
- this.attempts++;
34
- this.callbacks.onRetry();
35
- }, this.backoff.delay());
36
- }
37
- cancel() {
38
- if (this.timer) {
39
- clearTimeout(this.timer);
40
- this.timer = null;
41
- }
42
- }
43
- reset() {
44
- this.attempts = 0;
45
- this.backoff.reset();
46
- }
47
- hasExceededLimit() {
48
- return this.attempts >= this.config.maxAttempts;
49
- }
50
- notifyMaxAttemptsReached() {
51
- const error = new Error(`Max retry attempts (${this.config.maxAttempts}) reached`);
52
- this.callbacks.onMaxAttemptsReached(error);
53
- }
54
- }
55
- class ExponentialBackoff {
56
- initialDelay;
57
- maxDelay;
58
- currentDelay;
59
- factor;
60
- jitter;
61
- constructor(config) {
62
- this.initialDelay = config.initialDelay;
63
- this.maxDelay = config.maxDelay;
64
- this.factor = config.factor;
65
- this.jitter = config.jitter;
66
- this.currentDelay = config.initialDelay;
67
- }
68
- delay() {
69
- if (this.currentDelay >= this.maxDelay) {
70
- return this.applyJitter(this.maxDelay);
71
- }
72
- this.currentDelay = this.currentDelay * this.factor;
73
- if (this.currentDelay > this.maxDelay) {
74
- this.currentDelay = this.maxDelay;
75
- }
76
- return this.applyJitter(this.currentDelay);
77
- }
78
- reset() {
79
- this.currentDelay = this.initialDelay;
80
- }
81
- applyJitter(delay) {
82
- if (delay === 0 || this.jitter === 0) {
83
- return delay;
84
- }
85
- const delayFactor = 1 - this.jitter / 2 + Math.random() * this.jitter;
86
- return delay * delayFactor;
87
- }
88
- }
89
84
 
90
- exports.DEFAULT_RETRY_CONFIG = DEFAULT_RETRY_CONFIG;
85
+ //#endregion
91
86
  exports.RetryStrategy = RetryStrategy;
92
- //# sourceMappingURL=retry_strategy.cjs.map
87
+ //# sourceMappingURL=retry_strategy.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"retry_strategy.cjs","sources":["../../src/helpers/retry_strategy.ts"],"sourcesContent":["export interface RetryConfig {\n maxAttempts: number;\n initialDelay: number;\n maxDelay: number;\n}\n\nexport const DEFAULT_RETRY_CONFIG: RetryConfig = {\n maxAttempts: 10,\n initialDelay: 100,\n maxDelay: 30000,\n};\n\nexport interface RetryCallbacks {\n onRetry: () => void;\n onMaxAttemptsReached: (error: Error) => void;\n}\n\nexport class RetryStrategy {\n private attempts = 0;\n private timer: ReturnType<typeof setTimeout> | null = null;\n private readonly config: RetryConfig;\n private readonly callbacks: RetryCallbacks;\n private readonly backoff: ExponentialBackoff;\n\n constructor(config: Partial<RetryConfig>, callbacks: RetryCallbacks) {\n this.config = { ...DEFAULT_RETRY_CONFIG, ...config };\n this.callbacks = callbacks;\n this.backoff = new ExponentialBackoff({\n initialDelay: this.config.initialDelay,\n maxDelay: this.config.maxDelay,\n factor: 2,\n jitter: 0.1,\n });\n }\n\n schedule(): void {\n if (this.timer) return;\n if (this.hasExceededLimit()) {\n this.notifyMaxAttemptsReached();\n return;\n }\n\n this.timer = setTimeout(() => {\n this.timer = null;\n this.attempts++;\n this.callbacks.onRetry();\n }, this.backoff.delay());\n }\n\n cancel(): void {\n if (this.timer) {\n clearTimeout(this.timer);\n this.timer = null;\n }\n }\n\n reset(): void {\n this.attempts = 0;\n this.backoff.reset();\n }\n\n private hasExceededLimit(): boolean {\n return this.attempts >= this.config.maxAttempts;\n }\n\n private notifyMaxAttemptsReached(): void {\n const error = new Error(`Max retry attempts (${this.config.maxAttempts}) reached`);\n this.callbacks.onMaxAttemptsReached(error);\n }\n}\n\ninterface ExponentialBackoffConfig {\n initialDelay: number;\n maxDelay: number;\n factor: number;\n jitter: number;\n}\n\nclass ExponentialBackoff {\n private readonly initialDelay: number;\n private readonly maxDelay: number;\n\n private currentDelay: number;\n\n private readonly factor: number;\n private readonly jitter: number;\n\n constructor(config: ExponentialBackoffConfig) {\n this.initialDelay = config.initialDelay;\n this.maxDelay = config.maxDelay;\n this.factor = config.factor;\n this.jitter = config.jitter;\n this.currentDelay = config.initialDelay;\n }\n\n delay(): number {\n if (this.currentDelay >= this.maxDelay) {\n return this.applyJitter(this.maxDelay);\n }\n\n this.currentDelay = this.currentDelay * this.factor;\n\n if (this.currentDelay > this.maxDelay) {\n this.currentDelay = this.maxDelay;\n }\n\n return this.applyJitter(this.currentDelay);\n }\n\n reset(): void {\n this.currentDelay = this.initialDelay;\n }\n\n private applyJitter(delay: number): number {\n if (delay === 0 || this.jitter === 0) {\n return delay;\n }\n const delayFactor = 1 - this.jitter / 2 + Math.random() * this.jitter;\n return delay * delayFactor;\n }\n}\n"],"names":[],"mappings":";;AAMO,MAAM,oBAAoB,GAAgB;AAC/C,IAAA,WAAW,EAAE,EAAE;AACf,IAAA,YAAY,EAAE,GAAG;AACjB,IAAA,QAAQ,EAAE,KAAK;;MAQJ,aAAa,CAAA;IAChB,QAAQ,GAAG,CAAC;IACZ,KAAK,GAAyC,IAAI;AACzC,IAAA,MAAM;AACN,IAAA,SAAS;AACT,IAAA,OAAO;IAExB,WAAA,CAAY,MAA4B,EAAE,SAAyB,EAAA;QACjE,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,oBAAoB,EAAE,GAAG,MAAM,EAAE;AACpD,QAAA,IAAI,CAAC,SAAS,G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W;IAC5B;AACD;;;;;"}
1
+ {"version":3,"file":"retry_strategy.cjs","names":[],"sources":["../../src/helpers/retry_strategy.ts"],"sourcesContent":["export interface RetryConfig {\n maxAttempts: number;\n initialDelay: number;\n maxDelay: number;\n}\n\nexport const DEFAULT_RETRY_CONFIG: RetryConfig = {\n maxAttempts: 10,\n initialDelay: 100,\n maxDelay: 30000,\n};\n\nexport interface RetryCallbacks {\n onRetry: () => void;\n onMaxAttemptsReached: (error: Error) => void;\n}\n\nexport class RetryStrategy {\n private attempts = 0;\n private timer: ReturnType<typeof setTimeout> | null = null;\n private readonly config: RetryConfig;\n private readonly callbacks: RetryCallbacks;\n private readonly backoff: ExponentialBackoff;\n\n constructor(config: Partial<RetryConfig>, callbacks: RetryCallbacks) {\n this.config = { ...DEFAULT_RETRY_CONFIG, ...config };\n this.callbacks = callbacks;\n this.backoff = new ExponentialBackoff({\n initialDelay: this.config.initialDelay,\n maxDelay: this.config.maxDelay,\n factor: 2,\n jitter: 0.1,\n });\n }\n\n schedule(): void {\n if (this.timer) return;\n if (this.hasExceededLimit()) {\n this.notifyMaxAttemptsReached();\n return;\n }\n\n this.timer = setTimeout(() => {\n this.timer = null;\n this.attempts++;\n this.callbacks.onRetry();\n }, this.backoff.delay());\n }\n\n cancel(): void {\n if (this.timer) {\n clearTimeout(this.timer);\n this.timer = null;\n }\n }\n\n reset(): void {\n this.attempts = 0;\n this.backoff.reset();\n }\n\n private hasExceededLimit(): boolean {\n return this.attempts >= this.config.maxAttempts;\n }\n\n private notifyMaxAttemptsReached(): void {\n const error = new Error(`Max retry attempts (${this.config.maxAttempts}) reached`);\n this.callbacks.onMaxAttemptsReached(error);\n }\n}\n\ninterface ExponentialBackoffConfig {\n initialDelay: number;\n maxDelay: number;\n factor: number;\n jitter: number;\n}\n\nclass ExponentialBackoff {\n private readonly initialDelay: number;\n private readonly maxDelay: number;\n\n private currentDelay: number;\n\n private readonly factor: number;\n private readonly jitter: number;\n\n constructor(config: ExponentialBackoffConfig) {\n this.initialDelay = config.initialDelay;\n this.maxDelay = config.maxDelay;\n this.factor = config.factor;\n this.jitter = config.jitter;\n this.currentDelay = config.initialDelay;\n }\n\n delay(): number {\n if (this.currentDelay >= this.maxDelay) {\n return this.applyJitter(this.maxDelay);\n }\n\n this.currentDelay = this.currentDelay * this.factor;\n\n if (this.currentDelay > this.maxDelay) {\n this.currentDelay = this.maxDelay;\n }\n\n return this.applyJitter(this.currentDelay);\n }\n\n reset(): void {\n this.currentDelay = this.initialDelay;\n }\n\n private applyJitter(delay: number): number {\n if (delay === 0 || this.jitter === 0) {\n return delay;\n }\n const delayFactor = 1 - this.jitter / 2 + Math.random() * this.jitter;\n return delay * delayFactor;\n }\n}\n"],"mappings":";;AAMA,MAAa,uBAAoC;CAC/C,aAAa;CACb,cAAc;CACd,UAAU;CACX;AAOD,IAAa,gBAAb,MAA2B;CACzB,AAAQ,WAAW;CACnB,AAAQ,QAA8C;CACtD,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,QAA8B,WAA2B;AACnE,OAAK,SAAS;GAAE,GAAG;GAAsB,GAAG;GAAQ;AACpD,OAAK,YAAY;AACjB,OAAK,UAAU,IAAI,mBAAmB;GACpC,cAAc,KAAK,OAAO;GAC1B,UAAU,KAAK,OAAO;GACtB,QAAQ;GACR,QAAQ;GACT,CAAC;;CAGJ,WAAiB;AACf,MAAI,KAAK,MAAO;AAChB,MAAI,KAAK,kBAAkB,EAAE;AAC3B,QAAK,0BAA0B;AAC/B;;AAGF,OAAK,QAAQ,iBAAiB;AAC5B,QAAK,QAAQ;AACb,QAAK;AACL,QAAK,UAAU,SAAS;KACvB,KAAK,QAAQ,OAAO,CAAC;;CAG1B,SAAe;AACb,MAAI,KAAK,OAAO;AACd,gBAAa,KAAK,MAAM;AACxB,QAAK,QAAQ;;;CAIjB,QAAc;AACZ,OAAK,WAAW;AAChB,OAAK,QAAQ,OAAO;;CAGtB,AAAQ,mBAA4B;AAClC,SAAO,KAAK,YAAY,KAAK,OAAO;;CAGtC,AAAQ,2BAAiC;EACvC,MAAM,wBAAQ,IAAI,MAAM,uBAAuB,KAAK,OAAO,YAAY,WAAW;AAClF,OAAK,UAAU,qBAAqB,MAAM;;;AAW9C,IAAM,qBAAN,MAAyB;CACvB,AAAiB;CACjB,AAAiB;CAEjB,AAAQ;CAER,AAAiB;CACjB,AAAiB;CAEjB,YAAY,QAAkC;AAC5C,OAAK,eAAe,OAAO;AAC3B,OAAK,WAAW,OAAO;AACvB,OAAK,SAAS,OAAO;AACrB,OAAK,SAAS,OAAO;AACrB,OAAK,eAAe,OAAO;;CAG7B,QAAgB;AACd,MAAI,KAAK,gBAAgB,KAAK,SAC5B,QAAO,KAAK,YAAY,KAAK,SAAS;AAGxC,OAAK,eAAe,KAAK,eAAe,KAAK;AAE7C,MAAI,KAAK,eAAe,KAAK,SAC3B,MAAK,eAAe,KAAK;AAG3B,SAAO,KAAK,YAAY,KAAK,aAAa;;CAG5C,QAAc;AACZ,OAAK,eAAe,KAAK;;CAG3B,AAAQ,YAAY,OAAuB;AACzC,MAAI,UAAU,KAAK,KAAK,WAAW,EACjC,QAAO;AAGT,SAAO,SADa,IAAI,KAAK,SAAS,IAAI,KAAK,QAAQ,GAAG,KAAK"}
@@ -1,89 +1,86 @@
1
+ //#region src/helpers/retry_strategy.ts
1
2
  const DEFAULT_RETRY_CONFIG = {
2
- maxAttempts: 10,
3
- initialDelay: 100,
4
- maxDelay: 30000,
3
+ maxAttempts: 10,
4
+ initialDelay: 100,
5
+ maxDelay: 3e4
6
+ };
7
+ var RetryStrategy = class {
8
+ attempts = 0;
9
+ timer = null;
10
+ config;
11
+ callbacks;
12
+ backoff;
13
+ constructor(config, callbacks) {
14
+ this.config = {
15
+ ...DEFAULT_RETRY_CONFIG,
16
+ ...config
17
+ };
18
+ this.callbacks = callbacks;
19
+ this.backoff = new ExponentialBackoff({
20
+ initialDelay: this.config.initialDelay,
21
+ maxDelay: this.config.maxDelay,
22
+ factor: 2,
23
+ jitter: .1
24
+ });
25
+ }
26
+ schedule() {
27
+ if (this.timer) return;
28
+ if (this.hasExceededLimit()) {
29
+ this.notifyMaxAttemptsReached();
30
+ return;
31
+ }
32
+ this.timer = setTimeout(() => {
33
+ this.timer = null;
34
+ this.attempts++;
35
+ this.callbacks.onRetry();
36
+ }, this.backoff.delay());
37
+ }
38
+ cancel() {
39
+ if (this.timer) {
40
+ clearTimeout(this.timer);
41
+ this.timer = null;
42
+ }
43
+ }
44
+ reset() {
45
+ this.attempts = 0;
46
+ this.backoff.reset();
47
+ }
48
+ hasExceededLimit() {
49
+ return this.attempts >= this.config.maxAttempts;
50
+ }
51
+ notifyMaxAttemptsReached() {
52
+ const error = /* @__PURE__ */ new Error(`Max retry attempts (${this.config.maxAttempts}) reached`);
53
+ this.callbacks.onMaxAttemptsReached(error);
54
+ }
55
+ };
56
+ var ExponentialBackoff = class {
57
+ initialDelay;
58
+ maxDelay;
59
+ currentDelay;
60
+ factor;
61
+ jitter;
62
+ constructor(config) {
63
+ this.initialDelay = config.initialDelay;
64
+ this.maxDelay = config.maxDelay;
65
+ this.factor = config.factor;
66
+ this.jitter = config.jitter;
67
+ this.currentDelay = config.initialDelay;
68
+ }
69
+ delay() {
70
+ if (this.currentDelay >= this.maxDelay) return this.applyJitter(this.maxDelay);
71
+ this.currentDelay = this.currentDelay * this.factor;
72
+ if (this.currentDelay > this.maxDelay) this.currentDelay = this.maxDelay;
73
+ return this.applyJitter(this.currentDelay);
74
+ }
75
+ reset() {
76
+ this.currentDelay = this.initialDelay;
77
+ }
78
+ applyJitter(delay) {
79
+ if (delay === 0 || this.jitter === 0) return delay;
80
+ return delay * (1 - this.jitter / 2 + Math.random() * this.jitter);
81
+ }
5
82
  };
6
- class RetryStrategy {
7
- attempts = 0;
8
- timer = null;
9
- config;
10
- callbacks;
11
- backoff;
12
- constructor(config, callbacks) {
13
- this.config = { ...DEFAULT_RETRY_CONFIG, ...config };
14
- this.callbacks = callbacks;
15
- this.backoff = new ExponentialBackoff({
16
- initialDelay: this.config.initialDelay,
17
- maxDelay: this.config.maxDelay,
18
- factor: 2,
19
- jitter: 0.1,
20
- });
21
- }
22
- schedule() {
23
- if (this.timer)
24
- return;
25
- if (this.hasExceededLimit()) {
26
- this.notifyMaxAttemptsReached();
27
- return;
28
- }
29
- this.timer = setTimeout(() => {
30
- this.timer = null;
31
- this.attempts++;
32
- this.callbacks.onRetry();
33
- }, this.backoff.delay());
34
- }
35
- cancel() {
36
- if (this.timer) {
37
- clearTimeout(this.timer);
38
- this.timer = null;
39
- }
40
- }
41
- reset() {
42
- this.attempts = 0;
43
- this.backoff.reset();
44
- }
45
- hasExceededLimit() {
46
- return this.attempts >= this.config.maxAttempts;
47
- }
48
- notifyMaxAttemptsReached() {
49
- const error = new Error(`Max retry attempts (${this.config.maxAttempts}) reached`);
50
- this.callbacks.onMaxAttemptsReached(error);
51
- }
52
- }
53
- class ExponentialBackoff {
54
- initialDelay;
55
- maxDelay;
56
- currentDelay;
57
- factor;
58
- jitter;
59
- constructor(config) {
60
- this.initialDelay = config.initialDelay;
61
- this.maxDelay = config.maxDelay;
62
- this.factor = config.factor;
63
- this.jitter = config.jitter;
64
- this.currentDelay = config.initialDelay;
65
- }
66
- delay() {
67
- if (this.currentDelay >= this.maxDelay) {
68
- return this.applyJitter(this.maxDelay);
69
- }
70
- this.currentDelay = this.currentDelay * this.factor;
71
- if (this.currentDelay > this.maxDelay) {
72
- this.currentDelay = this.maxDelay;
73
- }
74
- return this.applyJitter(this.currentDelay);
75
- }
76
- reset() {
77
- this.currentDelay = this.initialDelay;
78
- }
79
- applyJitter(delay) {
80
- if (delay === 0 || this.jitter === 0) {
81
- return delay;
82
- }
83
- const delayFactor = 1 - this.jitter / 2 + Math.random() * this.jitter;
84
- return delay * delayFactor;
85
- }
86
- }
87
83
 
88
- export { DEFAULT_RETRY_CONFIG, RetryStrategy };
89
- //# sourceMappingURL=retry_strategy.js.map
84
+ //#endregion
85
+ export { RetryStrategy };
86
+ //# sourceMappingURL=retry_strategy.js.map