@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,353 +1,281 @@
1
- 'use strict';
1
+ const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
2
+ const require_errors = require('./errors.cjs');
3
+ const require_retry_strategy = require('../helpers/retry_strategy.cjs');
4
+ let denque = require("denque");
5
+ denque = require_runtime.__toESM(denque);
6
+ let undici = require("undici");
2
7
 
3
- var undici = require('undici');
4
- var Denque = require('denque');
5
- var retry_strategy = require('../helpers/retry_strategy.cjs');
6
- var errors = require('./errors.cjs');
7
-
8
- var ConnectionState;
9
- (function (ConnectionState) {
10
- ConnectionState[ConnectionState["NEW"] = 0] = "NEW";
11
- ConnectionState[ConnectionState["CONNECTING"] = 1] = "CONNECTING";
12
- ConnectionState[ConnectionState["CONNECTED"] = 2] = "CONNECTED";
13
- ConnectionState[ConnectionState["CLOSING"] = 3] = "CLOSING";
14
- ConnectionState[ConnectionState["CLOSED"] = 4] = "CLOSED";
15
- })(ConnectionState || (ConnectionState = {}));
8
+ //#region src/core/websocket_stream.ts
9
+ var ConnectionState = /* @__PURE__ */ function(ConnectionState) {
10
+ ConnectionState[ConnectionState["NEW"] = 0] = "NEW";
11
+ ConnectionState[ConnectionState["CONNECTING"] = 1] = "CONNECTING";
12
+ ConnectionState[ConnectionState["CONNECTED"] = 2] = "CONNECTED";
13
+ ConnectionState[ConnectionState["CLOSING"] = 3] = "CLOSING";
14
+ ConnectionState[ConnectionState["CLOSED"] = 4] = "CLOSED";
15
+ return ConnectionState;
16
+ }(ConnectionState || {});
16
17
  /**
17
- * WebSocket-based bidirectional stream implementation for LLTransaction.
18
- * Implements BiDiStream interface which is compatible with DuplexStreamingCall.
19
- */
20
- class WebSocketBiDiStream {
21
- url;
22
- serializeClientMessage;
23
- parseServerMessage;
24
- options;
25
- // Connection
26
- ws = null;
27
- connectionState = ConnectionState.NEW;
28
- reconnection;
29
- // Send management
30
- sendQueue = new Denque();
31
- sendCompleted = false;
32
- onComplete;
33
- // Response management
34
- responseQueue = new Denque();
35
- responseResolvers = [];
36
- // Error tracking
37
- lastError;
38
- // === Public API ===
39
- requests = {
40
- send: async (message) => {
41
- return await this.enqueueSend(message);
42
- },
43
- complete: async () => {
44
- if (this.sendCompleted)
45
- return;
46
- await this.drainSendQueue(); // ensure we sent all already queued messages before closing the stream
47
- try {
48
- await this.onComplete(this); // custom onComplete may send additional messages
49
- }
50
- catch {
51
- // When 'complete' gets called concurrently with connection break or over a broken
52
- // transaction stream (server decided it should drop transaction), server would close
53
- // connection anyway on its end. We can safely ignore error here and just continue working.
54
- }
55
- this.sendCompleted = true;
56
- },
57
- };
58
- responses = {
59
- [Symbol.asyncIterator]: () => this.createResponseIterator(),
60
- };
61
- close() {
62
- this.reconnection.cancel();
63
- if (this.connectionState < ConnectionState.CONNECTED) {
64
- // Never reached CONNECTED state. ws.close() will never trigger 'close' event.
65
- this.ws?.close();
66
- this.onClose();
67
- return;
68
- }
69
- if (!this.progressConnectionState(ConnectionState.CLOSING))
70
- return;
71
- this.ws.close();
72
- }
73
- constructor(url, serializeClientMessage, parseServerMessage, options = {}) {
74
- this.url = url;
75
- this.serializeClientMessage = serializeClientMessage;
76
- this.parseServerMessage = parseServerMessage;
77
- this.options = options;
78
- this.onComplete = this.options.onComplete ?? ((stream) => stream.close());
79
- const retryConfig = this.options.retryConfig ?? {};
80
- this.reconnection = new retry_strategy.RetryStrategy(retryConfig, {
81
- onRetry: () => {
82
- void this.connect();
83
- },
84
- onMaxAttemptsReached: (error) => this.handleError(error),
85
- });
86
- if (this.options.abortSignal?.aborted) {
87
- this.progressConnectionState(ConnectionState.CLOSED);
88
- return;
89
- }
90
- this.options.abortSignal?.addEventListener("abort", () => this.close());
91
- this.connect();
92
- }
93
- // === Connection Lifecycle ===
94
- connect() {
95
- if (this.options.abortSignal?.aborted)
96
- return;
97
- // Prevent reconnecting after first successful connection.
98
- if (!this.progressConnectionState(ConnectionState.CONNECTING))
99
- return;
100
- try {
101
- this.ws = this.createWebSocket();
102
- this.ws.addEventListener("open", () => this.onOpen());
103
- this.ws.addEventListener("message", (event) => this.onMessage(event.data));
104
- this.ws.addEventListener("error", (error) => this.onError(error));
105
- this.ws.addEventListener("close", () => this.onClose());
106
- }
107
- catch (error) {
108
- this.lastError = this.toError(error);
109
- this.reconnection.schedule();
110
- }
111
- }
112
- createWebSocket() {
113
- const options = {};
114
- if (this.options.jwtToken)
115
- options.headers = { authorization: `Bearer ${this.options.jwtToken}` };
116
- if (this.options.dispatcher)
117
- options.dispatcher = this.options.dispatcher;
118
- const ws = new undici.WebSocket(this.url, options);
119
- ws.binaryType = "arraybuffer";
120
- return ws;
121
- }
122
- onOpen() {
123
- this.progressConnectionState(ConnectionState.CONNECTED);
124
- this.processSendQueue();
125
- }
126
- onMessage(data) {
127
- if (!(data instanceof ArrayBuffer)) {
128
- this.handleError(new Error(`Unexpected WS message format: ${typeof data}`));
129
- return;
130
- }
131
- try {
132
- const message = this.parseServerMessage(new Uint8Array(data));
133
- this.deliverResponse(message);
134
- }
135
- catch (error) {
136
- this.handleError(this.toError(error));
137
- }
138
- }
139
- onError(error) {
140
- if (this.connectionState < ConnectionState.CONNECTED) {
141
- // Try to connect several times until we succeed or run out of attempts.
142
- this.lastError = this.toError(error);
143
- this.reconnection.schedule();
144
- return;
145
- }
146
- this.handleError(this.toError(error));
147
- }
148
- onClose() {
149
- this.progressConnectionState(ConnectionState.CLOSED);
150
- // If abort signal was triggered, use that as the error source
151
- if (this.options.abortSignal?.aborted && !this.lastError) {
152
- const reason = this.options.abortSignal.reason;
153
- if (reason instanceof Error) {
154
- this.lastError = reason;
155
- }
156
- else if (reason !== undefined) {
157
- this.lastError = new Error(String(reason), { cause: reason });
158
- }
159
- else {
160
- this.lastError = this.createStreamClosedError();
161
- }
162
- }
163
- if (!this.lastError) {
164
- this.rejectAllSendOperations(this.createStreamClosedError());
165
- this.resolveAllPendingResponses(); // unblock active async iterator
166
- }
167
- else {
168
- this.rejectAllPendingOperations(this.lastError);
169
- }
170
- }
171
- // === Send Queue Management ===
172
- enqueueSend(message) {
173
- if (this.sendCompleted) {
174
- throw new Error("Cannot send: stream already completed");
175
- }
176
- if (this.options.abortSignal?.aborted) {
177
- throw new Error("Cannot send: stream aborted");
178
- }
179
- return new Promise((resolve, reject) => {
180
- this.sendQueue.push({ message, resolve, reject });
181
- this.processSendQueue();
182
- });
183
- }
184
- processSendQueue() {
185
- if (!this.canSendMessages())
186
- return;
187
- while (this.sendQueue.length > 0) {
188
- const queued = this.sendQueue.shift();
189
- this.sendQueuedMessage(queued);
190
- }
191
- }
192
- canSendMessages() {
193
- return this.connectionState === ConnectionState.CONNECTED;
194
- }
195
- sendQueuedMessage(queued) {
196
- try {
197
- const ws = this.ws;
198
- if (!ws) {
199
- throw new Error("WebSocket is not connected");
200
- }
201
- // Check if WebSocket is in a valid state for sending
202
- if (ws.readyState !== undici.WebSocket.OPEN) {
203
- throw new Error(`WebSocket is not open (readyState: ${ws.readyState})`);
204
- }
205
- const binary = this.serializeClientMessage(queued.message);
206
- ws.send(binary);
207
- queued.resolve();
208
- }
209
- catch (error) {
210
- queued.reject(this.toError(error));
211
- }
212
- }
213
- async drainSendQueue() {
214
- const POLL_INTERVAL_MS = 5;
215
- while (this.sendQueue.length > 0) {
216
- await this.waitForCondition(() => this.sendQueue.length === 0, POLL_INTERVAL_MS);
217
- }
218
- }
219
- waitForCondition(condition, intervalMs) {
220
- return new Promise((resolve, reject) => {
221
- if (this.options.abortSignal?.aborted) {
222
- return reject(this.toError(this.options.abortSignal.reason) ?? new Error("Stream aborted"));
223
- }
224
- let timeoutId;
225
- const onAbort = () => {
226
- clearTimeout(timeoutId);
227
- reject(this.toError(this.options.abortSignal?.reason) ?? new Error("Stream aborted"));
228
- };
229
- this.options.abortSignal?.addEventListener("abort", onAbort, { once: true });
230
- const check = () => {
231
- if (condition() || this.isStreamEnded()) {
232
- this.options.abortSignal?.removeEventListener("abort", onAbort);
233
- resolve();
234
- }
235
- else {
236
- timeoutId = setTimeout(check, intervalMs);
237
- }
238
- };
239
- check();
240
- });
241
- }
242
- // === Response Delivery ===
243
- deliverResponse(message) {
244
- if (this.responseResolvers.length > 0) {
245
- const resolver = this.responseResolvers.shift();
246
- resolver.resolve({ value: message, done: false });
247
- }
248
- else {
249
- this.responseQueue.push(message);
250
- }
251
- }
252
- async *createResponseIterator() {
253
- while (true) {
254
- const result = await this.nextResponse();
255
- if (result.done)
256
- break;
257
- yield result.value;
258
- }
259
- }
260
- nextResponse() {
261
- return new Promise((resolve, reject) => {
262
- // Fast path: message already available
263
- if (this.responseQueue.length > 0) {
264
- const message = this.responseQueue.shift();
265
- resolve({ value: message, done: false });
266
- return;
267
- }
268
- // Stream ended
269
- if (this.isStreamEnded()) {
270
- if (this.lastError) {
271
- reject(this.lastError);
272
- }
273
- else {
274
- resolve({ value: undefined, done: true });
275
- }
276
- return;
277
- }
278
- // Wait for next message
279
- this.responseResolvers.push({ resolve, reject });
280
- });
281
- }
282
- resolveAllPendingResponses() {
283
- while (this.responseResolvers.length > 0) {
284
- const resolver = this.responseResolvers.shift();
285
- resolver.resolve({ value: undefined, done: true });
286
- }
287
- }
288
- // === Error Handling ===
289
- handleError(error) {
290
- this.lastError = error;
291
- this.close();
292
- }
293
- rejectAllPendingOperations(error) {
294
- this.rejectAllSendOperations(error);
295
- this.rejectAllResponseResolvers(error);
296
- }
297
- rejectAllSendOperations(error) {
298
- while (this.sendQueue.length > 0) {
299
- const queued = this.sendQueue.shift();
300
- queued.reject(error);
301
- }
302
- }
303
- rejectAllResponseResolvers(error) {
304
- while (this.responseResolvers.length > 0) {
305
- const resolver = this.responseResolvers.shift();
306
- resolver.reject(error);
307
- }
308
- }
309
- createStreamClosedError() {
310
- if (this.options.abortSignal?.aborted) {
311
- const reason = this.options.abortSignal.reason;
312
- if (reason instanceof Error) {
313
- return reason;
314
- }
315
- return new Error("Stream aborted", { cause: reason });
316
- }
317
- return new Error("Stream closed");
318
- }
319
- // === Helpers ===
320
- isStreamEnded() {
321
- return (this.connectionState === ConnectionState.CLOSED || this.options.abortSignal?.aborted || false);
322
- }
323
- toError(error) {
324
- if (error instanceof Error)
325
- return error;
326
- if (error instanceof undici.ErrorEvent) {
327
- const err = error.error;
328
- // undici WebSocket throws TypeError with empty message on socket close
329
- // (e.g., when connection is lost or server disconnects)
330
- if (err instanceof TypeError && !err.message) {
331
- return new errors.DisconnectedError("WebSocket connection closed unexpectedly");
332
- }
333
- return err instanceof Error ? err : new Error("WebSocket error", { cause: error });
334
- }
335
- return new Error(String(error));
336
- }
337
- /**
338
- * Connection state progresses linearly from NEW to CLOSED and never goes back.
339
- * This internal contract dramatically simplifies the internal stream state management.
340
- *
341
- * If you ever feel the need to make this contract less strict, think twice.
342
- */
343
- progressConnectionState(newState) {
344
- if (newState < this.connectionState) {
345
- return false;
346
- }
347
- this.connectionState = newState;
348
- return true;
349
- }
350
- }
18
+ * WebSocket-based bidirectional stream implementation for LLTransaction.
19
+ * Implements BiDiStream interface which is compatible with DuplexStreamingCall.
20
+ */
21
+ var WebSocketBiDiStream = class {
22
+ ws = null;
23
+ connectionState = ConnectionState.NEW;
24
+ reconnection;
25
+ sendQueue = new denque.default();
26
+ sendCompleted = false;
27
+ onComplete;
28
+ responseQueue = new denque.default();
29
+ responseResolvers = [];
30
+ lastError;
31
+ requests = {
32
+ send: async (message) => {
33
+ return await this.enqueueSend(message);
34
+ },
35
+ complete: async () => {
36
+ if (this.sendCompleted) return;
37
+ await this.drainSendQueue();
38
+ try {
39
+ await this.onComplete(this);
40
+ } catch {}
41
+ this.sendCompleted = true;
42
+ }
43
+ };
44
+ responses = { [Symbol.asyncIterator]: () => this.createResponseIterator() };
45
+ close() {
46
+ this.reconnection.cancel();
47
+ if (this.connectionState < ConnectionState.CONNECTED) {
48
+ this.ws?.close();
49
+ this.onClose();
50
+ return;
51
+ }
52
+ if (!this.progressConnectionState(ConnectionState.CLOSING)) return;
53
+ this.ws.close();
54
+ }
55
+ constructor(url, serializeClientMessage, parseServerMessage, options = {}) {
56
+ this.url = url;
57
+ this.serializeClientMessage = serializeClientMessage;
58
+ this.parseServerMessage = parseServerMessage;
59
+ this.options = options;
60
+ this.onComplete = this.options.onComplete ?? ((stream) => stream.close());
61
+ this.reconnection = new require_retry_strategy.RetryStrategy(this.options.retryConfig ?? {}, {
62
+ onRetry: () => {
63
+ this.connect();
64
+ },
65
+ onMaxAttemptsReached: (error) => this.handleError(error)
66
+ });
67
+ if (this.options.abortSignal?.aborted) {
68
+ this.progressConnectionState(ConnectionState.CLOSED);
69
+ return;
70
+ }
71
+ this.options.abortSignal?.addEventListener("abort", () => this.close());
72
+ this.connect();
73
+ }
74
+ connect() {
75
+ if (this.options.abortSignal?.aborted) return;
76
+ if (!this.progressConnectionState(ConnectionState.CONNECTING)) return;
77
+ try {
78
+ this.ws = this.createWebSocket();
79
+ this.ws.addEventListener("open", () => this.onOpen());
80
+ this.ws.addEventListener("message", (event) => this.onMessage(event.data));
81
+ this.ws.addEventListener("error", (error) => this.onError(error));
82
+ this.ws.addEventListener("close", () => this.onClose());
83
+ } catch (error) {
84
+ this.lastError = this.toError(error);
85
+ this.reconnection.schedule();
86
+ }
87
+ }
88
+ createWebSocket() {
89
+ const options = {};
90
+ if (this.options.jwtToken) options.headers = { authorization: `Bearer ${this.options.jwtToken}` };
91
+ if (this.options.dispatcher) options.dispatcher = this.options.dispatcher;
92
+ const ws = new undici.WebSocket(this.url, options);
93
+ ws.binaryType = "arraybuffer";
94
+ return ws;
95
+ }
96
+ onOpen() {
97
+ this.progressConnectionState(ConnectionState.CONNECTED);
98
+ this.processSendQueue();
99
+ }
100
+ onMessage(data) {
101
+ if (!(data instanceof ArrayBuffer)) {
102
+ this.handleError(/* @__PURE__ */ new Error(`Unexpected WS message format: ${typeof data}`));
103
+ return;
104
+ }
105
+ try {
106
+ const message = this.parseServerMessage(new Uint8Array(data));
107
+ this.deliverResponse(message);
108
+ } catch (error) {
109
+ this.handleError(this.toError(error));
110
+ }
111
+ }
112
+ onError(error) {
113
+ if (this.connectionState < ConnectionState.CONNECTED) {
114
+ this.lastError = this.toError(error);
115
+ this.reconnection.schedule();
116
+ return;
117
+ }
118
+ this.handleError(this.toError(error));
119
+ }
120
+ onClose() {
121
+ this.progressConnectionState(ConnectionState.CLOSED);
122
+ if (this.options.abortSignal?.aborted && !this.lastError) {
123
+ const reason = this.options.abortSignal.reason;
124
+ if (reason instanceof Error) this.lastError = reason;
125
+ else if (reason !== void 0) this.lastError = new Error(String(reason), { cause: reason });
126
+ else this.lastError = this.createStreamClosedError();
127
+ }
128
+ if (!this.lastError) {
129
+ this.rejectAllSendOperations(this.createStreamClosedError());
130
+ this.resolveAllPendingResponses();
131
+ } else this.rejectAllPendingOperations(this.lastError);
132
+ }
133
+ enqueueSend(message) {
134
+ if (this.sendCompleted) throw new Error("Cannot send: stream already completed");
135
+ if (this.options.abortSignal?.aborted) throw new Error("Cannot send: stream aborted");
136
+ return new Promise((resolve, reject) => {
137
+ this.sendQueue.push({
138
+ message,
139
+ resolve,
140
+ reject
141
+ });
142
+ this.processSendQueue();
143
+ });
144
+ }
145
+ processSendQueue() {
146
+ if (!this.canSendMessages()) return;
147
+ while (this.sendQueue.length > 0) {
148
+ const queued = this.sendQueue.shift();
149
+ this.sendQueuedMessage(queued);
150
+ }
151
+ }
152
+ canSendMessages() {
153
+ return this.connectionState === ConnectionState.CONNECTED;
154
+ }
155
+ sendQueuedMessage(queued) {
156
+ try {
157
+ const ws = this.ws;
158
+ if (!ws) throw new Error("WebSocket is not connected");
159
+ if (ws.readyState !== undici.WebSocket.OPEN) throw new Error(`WebSocket is not open (readyState: ${ws.readyState})`);
160
+ const binary = this.serializeClientMessage(queued.message);
161
+ ws.send(binary);
162
+ queued.resolve();
163
+ } catch (error) {
164
+ queued.reject(this.toError(error));
165
+ }
166
+ }
167
+ async drainSendQueue() {
168
+ const POLL_INTERVAL_MS = 5;
169
+ while (this.sendQueue.length > 0) await this.waitForCondition(() => this.sendQueue.length === 0, POLL_INTERVAL_MS);
170
+ }
171
+ waitForCondition(condition, intervalMs) {
172
+ return new Promise((resolve, reject) => {
173
+ if (this.options.abortSignal?.aborted) return reject(this.toError(this.options.abortSignal.reason) ?? /* @__PURE__ */ new Error("Stream aborted"));
174
+ let timeoutId;
175
+ const onAbort = () => {
176
+ clearTimeout(timeoutId);
177
+ reject(this.toError(this.options.abortSignal?.reason) ?? /* @__PURE__ */ new Error("Stream aborted"));
178
+ };
179
+ this.options.abortSignal?.addEventListener("abort", onAbort, { once: true });
180
+ const check = () => {
181
+ if (condition() || this.isStreamEnded()) {
182
+ this.options.abortSignal?.removeEventListener("abort", onAbort);
183
+ resolve();
184
+ } else timeoutId = setTimeout(check, intervalMs);
185
+ };
186
+ check();
187
+ });
188
+ }
189
+ deliverResponse(message) {
190
+ if (this.responseResolvers.length > 0) this.responseResolvers.shift().resolve({
191
+ value: message,
192
+ done: false
193
+ });
194
+ else this.responseQueue.push(message);
195
+ }
196
+ async *createResponseIterator() {
197
+ while (true) {
198
+ const result = await this.nextResponse();
199
+ if (result.done) break;
200
+ yield result.value;
201
+ }
202
+ }
203
+ nextResponse() {
204
+ return new Promise((resolve, reject) => {
205
+ if (this.responseQueue.length > 0) {
206
+ resolve({
207
+ value: this.responseQueue.shift(),
208
+ done: false
209
+ });
210
+ return;
211
+ }
212
+ if (this.isStreamEnded()) {
213
+ if (this.lastError) reject(this.lastError);
214
+ else resolve({
215
+ value: void 0,
216
+ done: true
217
+ });
218
+ return;
219
+ }
220
+ this.responseResolvers.push({
221
+ resolve,
222
+ reject
223
+ });
224
+ });
225
+ }
226
+ resolveAllPendingResponses() {
227
+ while (this.responseResolvers.length > 0) this.responseResolvers.shift().resolve({
228
+ value: void 0,
229
+ done: true
230
+ });
231
+ }
232
+ handleError(error) {
233
+ this.lastError = error;
234
+ this.close();
235
+ }
236
+ rejectAllPendingOperations(error) {
237
+ this.rejectAllSendOperations(error);
238
+ this.rejectAllResponseResolvers(error);
239
+ }
240
+ rejectAllSendOperations(error) {
241
+ while (this.sendQueue.length > 0) this.sendQueue.shift().reject(error);
242
+ }
243
+ rejectAllResponseResolvers(error) {
244
+ while (this.responseResolvers.length > 0) this.responseResolvers.shift().reject(error);
245
+ }
246
+ createStreamClosedError() {
247
+ if (this.options.abortSignal?.aborted) {
248
+ const reason = this.options.abortSignal.reason;
249
+ if (reason instanceof Error) return reason;
250
+ return new Error("Stream aborted", { cause: reason });
251
+ }
252
+ return /* @__PURE__ */ new Error("Stream closed");
253
+ }
254
+ isStreamEnded() {
255
+ return this.connectionState === ConnectionState.CLOSED || this.options.abortSignal?.aborted || false;
256
+ }
257
+ toError(error) {
258
+ if (error instanceof Error) return error;
259
+ if (error instanceof undici.ErrorEvent) {
260
+ const err = error.error;
261
+ if (err instanceof TypeError && !err.message) return new require_errors.DisconnectedError("WebSocket connection closed unexpectedly");
262
+ return err instanceof Error ? err : new Error("WebSocket error", { cause: error });
263
+ }
264
+ return new Error(String(error));
265
+ }
266
+ /**
267
+ * Connection state progresses linearly from NEW to CLOSED and never goes back.
268
+ * This internal contract dramatically simplifies the internal stream state management.
269
+ *
270
+ * If you ever feel the need to make this contract less strict, think twice.
271
+ */
272
+ progressConnectionState(newState) {
273
+ if (newState < this.connectionState) return false;
274
+ this.connectionState = newState;
275
+ return true;
276
+ }
277
+ };
351
278
 
279
+ //#endregion
352
280
  exports.WebSocketBiDiStream = WebSocketBiDiStream;
353
- //# sourceMappingURL=websocket_stream.cjs.map
281
+ //# sourceMappingURL=websocket_stream.cjs.map