@milaboratories/pl-client 2.17.7 → 2.17.9

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 (276) 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 +280 -349
  92. package/dist/core/websocket_stream.cjs.map +1 -1
  93. package/dist/core/websocket_stream.js +278 -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/errors.ts +1 -1
  194. package/src/core/transaction.ts +123 -133
  195. package/src/core/websocket_stream.ts +10 -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.cjs +0 -61
  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.cjs.map +0 -1
  198. 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
  199. 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
  200. package/dist/core/PromiseTracker.d.ts.map +0 -1
  201. package/dist/core/StatefulPromise.d.ts +0 -39
  202. package/dist/core/StatefulPromise.d.ts.map +0 -1
  203. package/dist/core/abstract_stream.d.ts.map +0 -1
  204. package/dist/core/advisory_locks.d.ts +0 -10
  205. package/dist/core/advisory_locks.d.ts.map +0 -1
  206. package/dist/core/auth.d.ts.map +0 -1
  207. package/dist/core/cache.d.ts.map +0 -1
  208. package/dist/core/client.d.ts.map +0 -1
  209. package/dist/core/config.d.ts.map +0 -1
  210. package/dist/core/default_client.d.ts.map +0 -1
  211. package/dist/core/driver.d.ts.map +0 -1
  212. package/dist/core/error_resource.d.ts +0 -6
  213. package/dist/core/error_resource.d.ts.map +0 -1
  214. package/dist/core/errors.d.ts.map +0 -1
  215. package/dist/core/final.d.ts.map +0 -1
  216. package/dist/core/ll_client.d.ts.map +0 -1
  217. package/dist/core/ll_transaction.d.ts.map +0 -1
  218. package/dist/core/stat.d.ts.map +0 -1
  219. package/dist/core/transaction.d.ts.map +0 -1
  220. package/dist/core/type_conversion.d.ts +0 -8
  221. package/dist/core/type_conversion.d.ts.map +0 -1
  222. package/dist/core/types.d.ts.map +0 -1
  223. package/dist/core/unauth_client.d.ts.map +0 -1
  224. package/dist/core/websocket_stream.d.ts +0 -67
  225. package/dist/core/websocket_stream.d.ts.map +0 -1
  226. package/dist/core/wire.d.ts.map +0 -1
  227. package/dist/helpers/pl.d.ts.map +0 -1
  228. package/dist/helpers/poll.d.ts.map +0 -1
  229. package/dist/helpers/retry_strategy.d.ts +0 -24
  230. package/dist/helpers/retry_strategy.d.ts.map +0 -1
  231. package/dist/helpers/state_helpers.d.ts +0 -3
  232. package/dist/helpers/state_helpers.d.ts.map +0 -1
  233. package/dist/helpers/tx_helpers.d.ts.map +0 -1
  234. package/dist/index.cjs.map +0 -1
  235. package/dist/index.d.ts.map +0 -1
  236. package/dist/index.js.map +0 -1
  237. package/dist/proto-grpc/github.com/googleapis/googleapis/google/rpc/status.d.ts.map +0 -1
  238. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.client.d.ts.map +0 -1
  239. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.d.ts.map +0 -1
  240. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api_types.d.ts.map +0 -1
  241. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/base_types.d.ts.map +0 -1
  242. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/import.d.ts +0 -106
  243. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/import.d.ts.map +0 -1
  244. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/resource_types.d.ts.map +0 -1
  245. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/ws-test.d.ts +0 -73
  246. package/dist/proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/ws-test.d.ts.map +0 -1
  247. package/dist/proto-grpc/google/api/http.d.ts +0 -456
  248. package/dist/proto-grpc/google/api/http.d.ts.map +0 -1
  249. package/dist/proto-grpc/google/protobuf/any.d.ts.map +0 -1
  250. package/dist/proto-grpc/google/protobuf/descriptor.d.ts +0 -2340
  251. package/dist/proto-grpc/google/protobuf/descriptor.d.ts.map +0 -1
  252. package/dist/proto-grpc/google/protobuf/duration.d.ts.map +0 -1
  253. package/dist/proto-grpc/google/protobuf/empty.d.ts +0 -32
  254. package/dist/proto-grpc/google/protobuf/empty.d.ts.map +0 -1
  255. package/dist/proto-grpc/google/protobuf/struct.d.ts +0 -187
  256. package/dist/proto-grpc/google/protobuf/struct.d.ts.map +0 -1
  257. package/dist/proto-grpc/google/protobuf/timestamp.d.ts.map +0 -1
  258. package/dist/proto-grpc/google/protobuf/wrappers.d.ts +0 -308
  259. package/dist/proto-grpc/google/protobuf/wrappers.d.ts.map +0 -1
  260. package/dist/proto-grpc/google/rpc/code.d.ts.map +0 -1
  261. package/dist/proto-grpc/google/rpc/error_details.d.ts +0 -654
  262. package/dist/proto-grpc/google/rpc/error_details.d.ts.map +0 -1
  263. package/dist/proto-grpc/google/rpc/http.d.ts +0 -121
  264. package/dist/proto-grpc/google/rpc/http.d.ts.map +0 -1
  265. package/dist/proto-grpc/google/rpc/status.d.ts +0 -55
  266. package/dist/proto-grpc/google/rpc/status.d.ts.map +0 -1
  267. package/dist/proto-rest/index.d.ts.map +0 -1
  268. package/dist/proto-rest/plapi.d.ts.map +0 -1
  269. package/dist/test/tcp-proxy.d.ts.map +0 -1
  270. package/dist/test/test_config.d.ts.map +0 -1
  271. package/dist/util/branding.d.ts +0 -7
  272. package/dist/util/branding.d.ts.map +0 -1
  273. package/dist/util/pl.d.ts +0 -9
  274. package/dist/util/pl.d.ts.map +0 -1
  275. package/dist/util/util.d.ts +0 -2
  276. package/dist/util/util.d.ts.map +0 -1
@@ -1,678 +1,641 @@
1
- 'use strict';
2
-
3
- var tslib_es6 = require('../__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');
4
- var types = require('./types.cjs');
5
- var api = require('../proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.cjs');
6
- var util = require('../util/util.cjs');
7
- var type_conversion = require('./type_conversion.cjs');
8
- var tsHelpers = require('@milaboratories/ts-helpers');
9
- var errors = require('./errors.cjs');
10
- var stat = require('./stat.cjs');
11
- var error_resource = require('./error_resource.cjs');
12
- var pl = require('../helpers/pl.cjs');
13
- var PromiseTracker = require('./PromiseTracker.cjs');
1
+ const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
2
+ const require_types = require('./types.cjs');
3
+ const require_api = require('../proto-grpc/github.com/milaboratory/pl/plapi/plapiproto/api.cjs');
4
+ const require_util = require('../util/util.cjs');
5
+ const require_errors = require('./errors.cjs');
6
+ const require_type_conversion = require('./type_conversion.cjs');
7
+ const require_stat = require('./stat.cjs');
8
+ const require_error_resource = require('./error_resource.cjs');
9
+ const require_PromiseTracker = require('./PromiseTracker.cjs');
10
+ const require_pl = require('../helpers/pl.cjs');
11
+ let _milaboratories_ts_helpers = require("@milaboratories/ts-helpers");
14
12
 
13
+ //#region src/core/transaction.ts
15
14
  function isField(ref) {
16
- return ref.hasOwnProperty("resourceId") && ref.hasOwnProperty("fieldName");
15
+ return ref.hasOwnProperty("resourceId") && ref.hasOwnProperty("fieldName");
17
16
  }
18
17
  function isResource(ref) {
19
- return (typeof ref === "bigint" || (ref.hasOwnProperty("globalId") && ref.hasOwnProperty("localId")));
18
+ return typeof ref === "bigint" || ref.hasOwnProperty("globalId") && ref.hasOwnProperty("localId");
20
19
  }
21
20
  function isResourceId(ref) {
22
- return typeof ref === "bigint" && !types.isLocalResourceId(ref) && !types.isNullResourceId(ref);
21
+ return typeof ref === "bigint" && !require_types.isLocalResourceId(ref) && !require_types.isNullResourceId(ref);
23
22
  }
24
23
  function isFieldRef(ref) {
25
- return isResourceRef(ref.resourceId);
24
+ return isResourceRef(ref.resourceId);
26
25
  }
27
26
  function isResourceRef(ref) {
28
- return ref.hasOwnProperty("globalId") && ref.hasOwnProperty("localId");
27
+ return ref.hasOwnProperty("globalId") && ref.hasOwnProperty("localId");
29
28
  }
30
29
  function toFieldId(ref) {
31
- if (isFieldRef(ref))
32
- return { resourceId: ref.resourceId.localId, fieldName: ref.fieldName };
33
- else
34
- return ref;
30
+ if (isFieldRef(ref)) return {
31
+ resourceId: ref.resourceId.localId,
32
+ fieldName: ref.fieldName
33
+ };
34
+ else return ref;
35
35
  }
36
36
  async function toGlobalFieldId(ref) {
37
- if (isFieldRef(ref))
38
- return { resourceId: await ref.resourceId.globalId, fieldName: ref.fieldName };
39
- else
40
- return ref;
37
+ if (isFieldRef(ref)) return {
38
+ resourceId: await ref.resourceId.globalId,
39
+ fieldName: ref.fieldName
40
+ };
41
+ else return ref;
41
42
  }
42
43
  function toResourceId(ref) {
43
- if (isResourceRef(ref))
44
- return ref.localId;
45
- else
46
- return ref;
44
+ if (isResourceRef(ref)) return ref.localId;
45
+ else return ref;
47
46
  }
48
47
  async function toGlobalResourceId(ref) {
49
- if (isResourceRef(ref))
50
- return await ref.globalId;
51
- else
52
- return ref;
48
+ if (isResourceRef(ref)) return await ref.globalId;
49
+ else return ref;
53
50
  }
54
51
  function field(resourceId, fieldName) {
55
- return { resourceId, fieldName };
52
+ return {
53
+ resourceId,
54
+ fieldName
55
+ };
56
56
  }
57
57
  /** If transaction commit failed due to write conflicts */
58
- class TxCommitConflict extends Error {
59
- name = "TxCommitConflict";
60
- }
58
+ var TxCommitConflict = class extends Error {
59
+ name = "TxCommitConflict";
60
+ };
61
61
  async function notFoundToUndefined(cb) {
62
- try {
63
- return await cb();
64
- }
65
- catch (e) {
66
- if (errors.isNotFoundError(e))
67
- return undefined;
68
- throw e;
69
- }
70
- }
71
- /**
72
- * Decorator that wraps the method's returned promise with this.track()
73
- * This ensures that the promise will be awaited before the transaction is completed.
74
- */
75
- function tracked(value, _context) {
76
- return function (...args) {
77
- return this.track(value.apply(this, args));
78
- };
62
+ try {
63
+ return await cb();
64
+ } catch (e) {
65
+ if (require_errors.isNotFoundError(e)) return void 0;
66
+ throw e;
67
+ }
79
68
  }
80
69
  /**
81
- * Each platform transaction has 3 stages:
82
- * - initialization (txOpen message -> txInfo response)
83
- * - communication (create resources, fields, references and so on)
84
- * - finalization (txCommit or txDiscard message)
85
- *
86
- * This class encapsulates finalization stage and provides ready-to-communication transaction object.
87
- * */
88
- let PlTransaction = (() => {
89
- let _instanceExtraInitializers = [];
90
- let _getResourceData_decorators;
91
- let _getResourceDataIfExists_decorators;
92
- let _getFieldIfExists_decorators;
93
- let _listKeyValues_decorators;
94
- let _listKeyValuesString_decorators;
95
- let _listKeyValuesIfResourceExists_decorators;
96
- let _listKeyValuesStringIfResourceExists_decorators;
97
- let _getKValue_decorators;
98
- let _getKValueString_decorators;
99
- let _getKValueJson_decorators;
100
- let _getKValueIfExists_decorators;
101
- let _getKValueStringIfExists_decorators;
102
- let _getKValueJsonIfExists_decorators;
103
- return class PlTransaction {
104
- static {
105
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
106
- _getResourceData_decorators = [tracked];
107
- _getResourceDataIfExists_decorators = [tracked];
108
- _getFieldIfExists_decorators = [tracked];
109
- _listKeyValues_decorators = [tracked];
110
- _listKeyValuesString_decorators = [tracked];
111
- _listKeyValuesIfResourceExists_decorators = [tracked];
112
- _listKeyValuesStringIfResourceExists_decorators = [tracked];
113
- _getKValue_decorators = [tracked];
114
- _getKValueString_decorators = [tracked];
115
- _getKValueJson_decorators = [tracked];
116
- _getKValueIfExists_decorators = [tracked];
117
- _getKValueStringIfExists_decorators = [tracked];
118
- _getKValueJsonIfExists_decorators = [tracked];
119
- tslib_es6.__esDecorate(this, null, _getResourceData_decorators, { kind: "method", name: "getResourceData", static: false, private: false, access: { has: obj => "getResourceData" in obj, get: obj => obj.getResourceData }, metadata: _metadata }, null, _instanceExtraInitializers);
120
- tslib_es6.__esDecorate(this, null, _getResourceDataIfExists_decorators, { kind: "method", name: "getResourceDataIfExists", static: false, private: false, access: { has: obj => "getResourceDataIfExists" in obj, get: obj => obj.getResourceDataIfExists }, metadata: _metadata }, null, _instanceExtraInitializers);
121
- tslib_es6.__esDecorate(this, null, _getFieldIfExists_decorators, { kind: "method", name: "getFieldIfExists", static: false, private: false, access: { has: obj => "getFieldIfExists" in obj, get: obj => obj.getFieldIfExists }, metadata: _metadata }, null, _instanceExtraInitializers);
122
- tslib_es6.__esDecorate(this, null, _listKeyValues_decorators, { kind: "method", name: "listKeyValues", static: false, private: false, access: { has: obj => "listKeyValues" in obj, get: obj => obj.listKeyValues }, metadata: _metadata }, null, _instanceExtraInitializers);
123
- tslib_es6.__esDecorate(this, null, _listKeyValuesString_decorators, { kind: "method", name: "listKeyValuesString", static: false, private: false, access: { has: obj => "listKeyValuesString" in obj, get: obj => obj.listKeyValuesString }, metadata: _metadata }, null, _instanceExtraInitializers);
124
- tslib_es6.__esDecorate(this, null, _listKeyValuesIfResourceExists_decorators, { kind: "method", name: "listKeyValuesIfResourceExists", static: false, private: false, access: { has: obj => "listKeyValuesIfResourceExists" in obj, get: obj => obj.listKeyValuesIfResourceExists }, metadata: _metadata }, null, _instanceExtraInitializers);
125
- tslib_es6.__esDecorate(this, null, _listKeyValuesStringIfResourceExists_decorators, { kind: "method", name: "listKeyValuesStringIfResourceExists", static: false, private: false, access: { has: obj => "listKeyValuesStringIfResourceExists" in obj, get: obj => obj.listKeyValuesStringIfResourceExists }, metadata: _metadata }, null, _instanceExtraInitializers);
126
- tslib_es6.__esDecorate(this, null, _getKValue_decorators, { kind: "method", name: "getKValue", static: false, private: false, access: { has: obj => "getKValue" in obj, get: obj => obj.getKValue }, metadata: _metadata }, null, _instanceExtraInitializers);
127
- tslib_es6.__esDecorate(this, null, _getKValueString_decorators, { kind: "method", name: "getKValueString", static: false, private: false, access: { has: obj => "getKValueString" in obj, get: obj => obj.getKValueString }, metadata: _metadata }, null, _instanceExtraInitializers);
128
- tslib_es6.__esDecorate(this, null, _getKValueJson_decorators, { kind: "method", name: "getKValueJson", static: false, private: false, access: { has: obj => "getKValueJson" in obj, get: obj => obj.getKValueJson }, metadata: _metadata }, null, _instanceExtraInitializers);
129
- tslib_es6.__esDecorate(this, null, _getKValueIfExists_decorators, { kind: "method", name: "getKValueIfExists", static: false, private: false, access: { has: obj => "getKValueIfExists" in obj, get: obj => obj.getKValueIfExists }, metadata: _metadata }, null, _instanceExtraInitializers);
130
- tslib_es6.__esDecorate(this, null, _getKValueStringIfExists_decorators, { kind: "method", name: "getKValueStringIfExists", static: false, private: false, access: { has: obj => "getKValueStringIfExists" in obj, get: obj => obj.getKValueStringIfExists }, metadata: _metadata }, null, _instanceExtraInitializers);
131
- tslib_es6.__esDecorate(this, null, _getKValueJsonIfExists_decorators, { kind: "method", name: "getKValueJsonIfExists", static: false, private: false, access: { has: obj => "getKValueJsonIfExists" in obj, get: obj => obj.getKValueJsonIfExists }, metadata: _metadata }, null, _instanceExtraInitializers);
132
- if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
133
- }
134
- ll = tslib_es6.__runInitializers(this, _instanceExtraInitializers);
135
- name;
136
- writable;
137
- _clientRoot;
138
- finalPredicate;
139
- sharedResourceDataCache;
140
- enableFormattedErrors;
141
- globalTxId;
142
- localTxId = PlTransaction.nextLocalTxId();
143
- /** Used in caching */
144
- txOpenTimestamp = Date.now();
145
- localResourceIdCounter = 0;
146
- /** Store logical tx open / closed state to prevent invalid sequence of requests.
147
- * True means output stream was completed.
148
- * Contract: there must be no async operations between setting this field to true and sending complete signal to stream. */
149
- _completed = false;
150
- globalTxIdWasAwaited = false;
151
- pending = new PromiseTracker.PromiseTracker();
152
- _startTime = Date.now();
153
- _stat = stat.initialTxStatWithoutTime();
154
- get stat() {
155
- return {
156
- ...this._stat,
157
- timeMs: Date.now() - this._startTime,
158
- };
159
- }
160
- constructor(ll, name, writable, _clientRoot, finalPredicate, sharedResourceDataCache, enableFormattedErrors = false) {
161
- this.ll = ll;
162
- this.name = name;
163
- this.writable = writable;
164
- this._clientRoot = _clientRoot;
165
- this.finalPredicate = finalPredicate;
166
- this.sharedResourceDataCache = sharedResourceDataCache;
167
- this.enableFormattedErrors = enableFormattedErrors;
168
- // initiating transaction
169
- this.globalTxId = this.sendSingleAndParse({
170
- oneofKind: "txOpen",
171
- txOpen: {
172
- name,
173
- enableFormattedErrors,
174
- writable: writable
175
- ? api.TxAPI_Open_Request_WritableTx.WRITABLE
176
- : api.TxAPI_Open_Request_WritableTx.NOT_WRITABLE,
177
- },
178
- }, (r) => tsHelpers.notEmpty(r.txOpen.tx?.id));
179
- void this.track(this.globalTxId);
180
- // To avoid floating promise
181
- this.globalTxId.catch((err) => {
182
- if (!this.globalTxIdWasAwaited) {
183
- console.warn(err);
184
- }
185
- });
186
- // Adding stats
187
- this._stat.txCount++;
188
- }
189
- /**
190
- * Collect all pending promises for the transaction finalization.
191
- */
192
- track(promiseOrCallback) {
193
- return this.pending.track(promiseOrCallback);
194
- }
195
- async drainAndAwaitPendingOps() {
196
- // awaiting these pending operations first, to catch any errors
197
- await this.pending.awaitAll();
198
- }
199
- sendSingleAndParse(r, parser) {
200
- return this.pending.track(async () => {
201
- const rawResponsePromise = this.ll.send(r, false);
202
- void this.pending.track(rawResponsePromise);
203
- await this.drainAndAwaitPendingOps();
204
- // awaiting our result, and parsing the response
205
- return parser(await rawResponsePromise);
206
- });
207
- }
208
- sendMultiAndParse(r, parser) {
209
- return this.pending.track(async () => {
210
- const rawResponsePromise = this.ll.send(r, true);
211
- void this.pending.track(rawResponsePromise);
212
- await this.drainAndAwaitPendingOps();
213
- // awaiting our result, and parsing the response
214
- return parser(await rawResponsePromise);
215
- });
216
- }
217
- async sendVoidSync(r) {
218
- await this.ll.send(r, false);
219
- }
220
- /** Requests sent with this method should never produce recoverable errors */
221
- sendVoidAsync(r) {
222
- void this.track(this.sendVoidSync(r));
223
- }
224
- checkTxOpen() {
225
- if (this._completed)
226
- throw new Error("Transaction already closed");
227
- }
228
- get completed() {
229
- return this._completed;
230
- }
231
- /** Commit & closes transaction. {@link TxCommitConflict} is thrown on
232
- * commit conflicts. */
233
- async commit() {
234
- this.checkTxOpen();
235
- // tx will accept no requests after this one
236
- this._completed = true;
237
- if (!this.writable) {
238
- // no need to explicitly commit or reject read-only tx
239
- const completeResult = this.track(this.ll.complete());
240
- await this.drainAndAwaitPendingOps();
241
- await completeResult;
242
- await this.ll.await();
243
- }
244
- else {
245
- const commitResponse = this.track(this.sendSingleAndParse({ oneofKind: "txCommit", txCommit: {} }, (r) => r.txCommit.success));
246
- // send closing frame right after commit to save some time on round-trips
247
- const completeResult = this.track(this.ll.complete());
248
- // now when we pushed all packets into the stream, we should wait for any
249
- // pending void operations from before, to catch any errors
250
- await this.drainAndAwaitPendingOps();
251
- if (!(await commitResponse))
252
- throw new TxCommitConflict();
253
- await completeResult;
254
- // await event-loop completion
255
- await this.ll.await();
256
- }
257
- }
258
- async discard() {
259
- this.checkTxOpen();
260
- // tx will accept no requests after this one
261
- this._completed = true;
262
- const discardResponse = this.sendVoidSync({ oneofKind: "txDiscard", txDiscard: {} });
263
- void this.track(discardResponse);
264
- // send closing frame right after commit to save some time on round-trips
265
- const completeResult = this.track(this.ll.complete());
266
- // now when we pushed all packets into the stream, we should wait for any
267
- // pending void operations from before, to catch any errors
268
- await this.drainAndAwaitPendingOps();
269
- await discardResponse;
270
- await completeResult;
271
- await this.ll.await();
272
- }
273
- //
274
- // Main tx methods
275
- //
276
- get clientRoot() {
277
- return types.ensureResourceIdNotNull(this._clientRoot);
278
- }
279
- //
280
- // Resources
281
- //
282
- createSingleton(name, type, errorIfExists = false) {
283
- const localId = this.nextLocalResourceId(false);
284
- const globalId = this.sendSingleAndParse({
285
- oneofKind: "resourceCreateSingleton",
286
- resourceCreateSingleton: {
287
- type,
288
- id: localId,
289
- data: Buffer.from(name),
290
- errorIfExists,
291
- },
292
- }, (r) => r.resourceCreateSingleton.resourceId);
293
- void this.track(globalId);
294
- return { globalId, localId };
295
- }
296
- getSingleton(name, loadFields = true) {
297
- return this.sendSingleAndParse({
298
- oneofKind: "resourceGetSingleton",
299
- resourceGetSingleton: {
300
- data: Buffer.from(name),
301
- loadFields,
302
- },
303
- }, (r) => type_conversion.protoToResource(tsHelpers.notEmpty(r.resourceGetSingleton.resource)));
304
- }
305
- createResource(root, req, parser) {
306
- const localId = this.nextLocalResourceId(root);
307
- const globalId = this.sendSingleAndParse(req(localId), (r) => parser(r));
308
- void this.track(globalId);
309
- return { globalId, localId };
310
- }
311
- createRoot(type) {
312
- this._stat.rootsCreated++;
313
- return this.createResource(true, (localId) => ({ oneofKind: "resourceCreateRoot", resourceCreateRoot: { type, id: localId } }), (r) => r.resourceCreateRoot.resourceId);
314
- }
315
- createStruct(type, data) {
316
- this._stat.structsCreated++;
317
- this._stat.structsCreatedDataBytes += data?.length ?? 0;
318
- return this.createResource(false, (localId) => ({
319
- oneofKind: "resourceCreateStruct",
320
- resourceCreateStruct: {
321
- type,
322
- id: localId,
323
- data: data === undefined ? undefined : typeof data === "string" ? Buffer.from(data) : data,
324
- },
325
- }), (r) => r.resourceCreateStruct.resourceId);
326
- }
327
- createEphemeral(type, data) {
328
- this._stat.ephemeralsCreated++;
329
- this._stat.ephemeralsCreatedDataBytes += data?.length ?? 0;
330
- return this.createResource(false, (localId) => ({
331
- oneofKind: "resourceCreateEphemeral",
332
- resourceCreateEphemeral: {
333
- type,
334
- id: localId,
335
- data: data === undefined ? undefined : typeof data === "string" ? Buffer.from(data) : data,
336
- },
337
- }), (r) => r.resourceCreateEphemeral.resourceId);
338
- }
339
- createValue(type, data, errorIfExists = false) {
340
- this._stat.valuesCreated++;
341
- this._stat.valuesCreatedDataBytes += data?.length ?? 0;
342
- return this.createResource(false, (localId) => ({
343
- oneofKind: "resourceCreateValue",
344
- resourceCreateValue: {
345
- type,
346
- id: localId,
347
- data: typeof data === "string" ? Buffer.from(data) : data,
348
- errorIfExists,
349
- },
350
- }), (r) => r.resourceCreateValue.resourceId);
351
- }
352
- createJsonValue(data) {
353
- const jsonData = tsHelpers.canonicalJsonBytes(data);
354
- return this.createValue(pl.JsonObject, jsonData, false);
355
- }
356
- createJsonGzValue(data, minSizeToGzip = 16_384) {
357
- const { data: jsonData, isGzipped } = tsHelpers.canonicalJsonGzBytes(data, minSizeToGzip);
358
- return this.createValue(isGzipped ? pl.JsonGzObject : pl.JsonObject, jsonData, false);
359
- }
360
- createError(message) {
361
- return this.createValue(error_resource.ErrorResourceType, JSON.stringify({ message }));
362
- }
363
- setResourceName(name, rId) {
364
- this.sendVoidAsync({
365
- oneofKind: "resourceNameSet",
366
- resourceNameSet: { resourceId: toResourceId(rId), name },
367
- });
368
- }
369
- deleteResourceName(name) {
370
- this.sendVoidAsync({ oneofKind: "resourceNameDelete", resourceNameDelete: { name } });
371
- }
372
- getResourceByName(name) {
373
- return this.sendSingleAndParse({ oneofKind: "resourceNameGet", resourceNameGet: { name } }, (r) => types.ensureResourceIdNotNull(r.resourceNameGet.resourceId));
374
- }
375
- checkResourceNameExists(name) {
376
- return this.sendSingleAndParse({ oneofKind: "resourceNameExists", resourceNameExists: { name } }, (r) => r.resourceNameExists.exists);
377
- }
378
- removeResource(rId) {
379
- this.sendVoidAsync({ oneofKind: "resourceRemove", resourceRemove: { id: rId } });
380
- }
381
- resourceExists(rId) {
382
- return this.sendSingleAndParse({ oneofKind: "resourceExists", resourceExists: { resourceId: rId } }, (r) => r.resourceExists.exists);
383
- }
384
- async getResourceData(rId, loadFields = true, ignoreCache = false) {
385
- if (!ignoreCache && !isResourceRef(rId) && !types.isLocalResourceId(rId)) {
386
- // checking if we can return result from cache
387
- const fromCache = this.sharedResourceDataCache.get(rId);
388
- if (fromCache && fromCache.cacheTxOpenTimestamp < this.txOpenTimestamp) {
389
- if (!loadFields) {
390
- this._stat.rGetDataCacheHits++;
391
- this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;
392
- return fromCache.basicData;
393
- }
394
- else if (fromCache.data) {
395
- this._stat.rGetDataCacheHits++;
396
- this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;
397
- this._stat.rGetDataCacheFields += fromCache.data.fields.length;
398
- return fromCache.data;
399
- }
400
- }
401
- }
402
- const result = await this.sendSingleAndParse({
403
- oneofKind: "resourceGet",
404
- resourceGet: { resourceId: toResourceId(rId), loadFields: loadFields },
405
- }, (r) => type_conversion.protoToResource(tsHelpers.notEmpty(r.resourceGet.resource)));
406
- this._stat.rGetDataNetRequests++;
407
- this._stat.rGetDataNetBytes += result.data?.length ?? 0;
408
- this._stat.rGetDataNetFields += result.fields.length;
409
- // we will cache only final resource data states
410
- // caching result even if we were ignore the cache
411
- if (!isResourceRef(rId) && !types.isLocalResourceId(rId) && this.finalPredicate(result)) {
412
- tsHelpers.deepFreeze(result);
413
- const fromCache = this.sharedResourceDataCache.get(rId);
414
- if (fromCache) {
415
- if (loadFields && !fromCache.data) {
416
- fromCache.data = result;
417
- // updating timestamp because we updated the record
418
- fromCache.cacheTxOpenTimestamp = this.txOpenTimestamp;
419
- }
420
- }
421
- else {
422
- const basicData = types.extractBasicResourceData(result);
423
- tsHelpers.deepFreeze(basicData);
424
- if (loadFields)
425
- this.sharedResourceDataCache.set(rId, {
426
- basicData,
427
- data: result,
428
- cacheTxOpenTimestamp: this.txOpenTimestamp,
429
- });
430
- else
431
- this.sharedResourceDataCache.set(rId, {
432
- basicData,
433
- data: undefined,
434
- cacheTxOpenTimestamp: this.txOpenTimestamp,
435
- });
436
- }
437
- }
438
- return result;
439
- }
440
- async getResourceDataIfExists(rId, loadFields = true) {
441
- // calling this method will ignore cache, because user intention is to detect resource absence
442
- // which cache will prevent
443
- const result = await notFoundToUndefined(async () => await this.getResourceData(rId, loadFields, true));
444
- // cleaning cache record if resource was removed from the db
445
- if (result === undefined && !isResourceRef(rId) && !types.isLocalResourceId(rId))
446
- this.sharedResourceDataCache.delete(rId);
447
- return result;
448
- }
449
- /**
450
- * Inform platform that resource will not get any new input fields.
451
- * This is required, when client creates resource without schema and wants
452
- * controller to start calculations.
453
- * Most controllers will not start calculations even when all inputs
454
- * have their values, if inputs list is not locked.
455
- */
456
- lockInputs(rId) {
457
- this._stat.inputsLocked++;
458
- this.sendVoidAsync({
459
- oneofKind: "resourceLockInputs",
460
- resourceLockInputs: { resourceId: toResourceId(rId) },
461
- });
462
- }
463
- /**
464
- * Inform platform that resource will not get any new output fields.
465
- * This is required for resource to pass deduplication.
466
- */
467
- lockOutputs(rId) {
468
- this._stat.outputsLocked++;
469
- this.sendVoidAsync({
470
- oneofKind: "resourceLockOutputs",
471
- resourceLockOutputs: { resourceId: toResourceId(rId) },
472
- });
473
- }
474
- lock(rID) {
475
- this.lockInputs(rID);
476
- this.lockOutputs(rID);
477
- }
478
- setResourceError(rId, ref) {
479
- this.sendVoidAsync({
480
- oneofKind: "resourceSetError",
481
- resourceSetError: { resourceId: toResourceId(rId), errorResourceId: toResourceId(ref) },
482
- });
483
- }
484
- //
485
- // Fields
486
- //
487
- createField(fId, fieldType, value) {
488
- this._stat.fieldsCreated++;
489
- this.sendVoidAsync({
490
- oneofKind: "fieldCreate",
491
- fieldCreate: { type: type_conversion.fieldTypeToProto(fieldType), id: toFieldId(fId) },
492
- });
493
- if (value !== undefined)
494
- this.setField(fId, value);
495
- }
496
- fieldExists(fId) {
497
- return this.sendSingleAndParse({
498
- oneofKind: "fieldExists",
499
- fieldExists: { field: toFieldId(fId) },
500
- }, (r) => r.fieldExists.exists);
501
- }
502
- setField(fId, ref) {
503
- this._stat.fieldsSet++;
504
- if (isResource(ref))
505
- this.sendVoidAsync({
506
- oneofKind: "fieldSet",
507
- fieldSet: {
508
- field: toFieldId(fId),
509
- value: {
510
- resourceId: toResourceId(ref),
511
- fieldName: "", // default value, read as undefined
512
- },
513
- },
514
- });
515
- else
516
- this.sendVoidAsync({
517
- oneofKind: "fieldSet",
518
- fieldSet: {
519
- field: toFieldId(fId),
520
- value: toFieldId(ref),
521
- },
522
- });
523
- }
524
- setFieldError(fId, ref) {
525
- this._stat.fieldsSet++;
526
- this.sendVoidAsync({
527
- oneofKind: "fieldSetError",
528
- fieldSetError: { field: toFieldId(fId), errResourceId: toResourceId(ref) },
529
- });
530
- }
531
- getField(fId) {
532
- this._stat.fieldsGet++;
533
- return this.sendSingleAndParse({ oneofKind: "fieldGet", fieldGet: { field: toFieldId(fId) } }, (r) => type_conversion.protoToField(tsHelpers.notEmpty(r.fieldGet.field)));
534
- }
535
- async getFieldIfExists(fId) {
536
- return notFoundToUndefined(async () => await this.getField(fId));
537
- }
538
- resetField(fId) {
539
- this.sendVoidAsync({ oneofKind: "fieldReset", fieldReset: { field: toFieldId(fId) } });
540
- }
541
- removeField(fId) {
542
- this.sendVoidAsync({ oneofKind: "fieldRemove", fieldRemove: { field: toFieldId(fId) } });
543
- }
544
- //
545
- // KV
546
- //
547
- async listKeyValues(rId) {
548
- const result = await this.sendMultiAndParse({
549
- oneofKind: "resourceKeyValueList",
550
- resourceKeyValueList: { resourceId: toResourceId(rId), startFrom: "", limit: 0 },
551
- }, (r) => r.map((e) => e.resourceKeyValueList.record));
552
- this._stat.kvListRequests++;
553
- this._stat.kvListEntries += result.length;
554
- for (const kv of result)
555
- this._stat.kvListBytes += kv.key.length + kv.value.length;
556
- return result;
557
- }
558
- async listKeyValuesString(rId) {
559
- return (await this.listKeyValues(rId)).map(({ key, value }) => ({
560
- key,
561
- value: Buffer.from(value).toString(),
562
- }));
563
- }
564
- async listKeyValuesIfResourceExists(rId) {
565
- return notFoundToUndefined(async () => await this.listKeyValues(rId));
566
- }
567
- async listKeyValuesStringIfResourceExists(rId) {
568
- return notFoundToUndefined(async () => await this.listKeyValuesString(rId));
569
- }
570
- setKValue(rId, key, value) {
571
- this._stat.kvSetRequests++;
572
- this._stat.kvSetBytes++;
573
- this.sendVoidAsync({
574
- oneofKind: "resourceKeyValueSet",
575
- resourceKeyValueSet: {
576
- resourceId: toResourceId(rId),
577
- key,
578
- value: util.toBytes(value),
579
- },
580
- });
581
- }
582
- deleteKValue(rId, key) {
583
- this.sendVoidAsync({
584
- oneofKind: "resourceKeyValueDelete",
585
- resourceKeyValueDelete: {
586
- resourceId: toResourceId(rId),
587
- key,
588
- },
589
- });
590
- }
591
- async getKValue(rId, key) {
592
- const result = await this.sendSingleAndParse({
593
- oneofKind: "resourceKeyValueGet",
594
- resourceKeyValueGet: { resourceId: toResourceId(rId), key },
595
- }, (r) => r.resourceKeyValueGet.value);
596
- this._stat.kvGetRequests++;
597
- this._stat.kvGetBytes += result.length;
598
- return result;
599
- }
600
- async getKValueString(rId, key) {
601
- return Buffer.from(await this.getKValue(rId, key)).toString();
602
- }
603
- async getKValueJson(rId, key) {
604
- return JSON.parse(await this.getKValueString(rId, key));
605
- }
606
- async getKValueIfExists(rId, key) {
607
- const result = await this.sendSingleAndParse({
608
- oneofKind: "resourceKeyValueGetIfExists",
609
- resourceKeyValueGetIfExists: { resourceId: toResourceId(rId), key },
610
- }, (r) => r.resourceKeyValueGetIfExists.exists ? r.resourceKeyValueGetIfExists.value : undefined);
611
- this._stat.kvGetRequests++;
612
- this._stat.kvGetBytes += result?.length ?? 0;
613
- return result;
614
- }
615
- async getKValueStringIfExists(rId, key) {
616
- const data = await this.getKValueIfExists(rId, key);
617
- return data === undefined ? undefined : Buffer.from(data).toString();
618
- }
619
- async getKValueJsonIfExists(rId, key) {
620
- const str = await this.getKValueString(rId, key);
621
- if (str === undefined)
622
- return undefined;
623
- return JSON.parse(str);
624
- }
625
- //
626
- // Cache
627
- //
628
- // TODO
629
- //
630
- // High level ops
631
- //
632
- /** Resolves existing or create first level resource from */
633
- getFutureFieldValue(rId, fieldName, fieldType) {
634
- const data = Buffer.from(JSON.stringify({ fieldName, fieldType }));
635
- const getFieldResource = this.createEphemeral({ name: "json/getField", version: "1" }, data);
636
- this.setField({ resourceId: getFieldResource, fieldName: "resource" }, rId);
637
- return { resourceId: getFieldResource, fieldName: "result" };
638
- }
639
- //
640
- // Technical
641
- //
642
- async getGlobalTxId() {
643
- this.globalTxIdWasAwaited = true;
644
- return await this.globalTxId;
645
- }
646
- /** Closes output event stream */
647
- async complete() {
648
- if (this._completed)
649
- return;
650
- this._completed = true;
651
- const completeResult = this.track(this.ll.complete());
652
- await this.drainAndAwaitPendingOps();
653
- await completeResult;
654
- }
655
- /** Await incoming message loop termination and throw
656
- * any leftover errors if it was unsuccessful */
657
- async await() {
658
- await this.ll.await();
659
- }
660
- //
661
- // Helpers
662
- //
663
- nextLocalResourceId(root) {
664
- return types.createLocalResourceId(root, ++this.localResourceIdCounter, this.localTxId);
665
- }
666
- static localTxIdCounter = 0;
667
- static nextLocalTxId() {
668
- PlTransaction.localTxIdCounter++;
669
- if (PlTransaction.localTxIdCounter === types.MaxTxId)
670
- PlTransaction.localTxIdCounter = 1;
671
- return PlTransaction.localTxIdCounter;
672
- }
673
- };
674
- })();
70
+ * Each platform transaction has 3 stages:
71
+ * - initialization (txOpen message -> txInfo response)
72
+ * - communication (create resources, fields, references and so on)
73
+ * - finalization (txCommit or txDiscard message)
74
+ *
75
+ * This class encapsulates finalization stage and provides ready-to-communication transaction object.
76
+ * */
77
+ var PlTransaction = class PlTransaction {
78
+ globalTxId;
79
+ localTxId = PlTransaction.nextLocalTxId();
80
+ /** Used in caching */
81
+ txOpenTimestamp = Date.now();
82
+ localResourceIdCounter = 0;
83
+ /** Store logical tx open / closed state to prevent invalid sequence of requests.
84
+ * True means output stream was completed.
85
+ * Contract: there must be no async operations between setting this field to true and sending complete signal to stream. */
86
+ _completed = false;
87
+ globalTxIdWasAwaited = false;
88
+ pending = new require_PromiseTracker.PromiseTracker();
89
+ _startTime = Date.now();
90
+ _stat = require_stat.initialTxStatWithoutTime();
91
+ get stat() {
92
+ return {
93
+ ...this._stat,
94
+ timeMs: Date.now() - this._startTime
95
+ };
96
+ }
97
+ constructor(ll, name, writable, _clientRoot, finalPredicate, sharedResourceDataCache, enableFormattedErrors = false) {
98
+ this.ll = ll;
99
+ this.name = name;
100
+ this.writable = writable;
101
+ this._clientRoot = _clientRoot;
102
+ this.finalPredicate = finalPredicate;
103
+ this.sharedResourceDataCache = sharedResourceDataCache;
104
+ this.enableFormattedErrors = enableFormattedErrors;
105
+ this.globalTxId = this.sendSingleAndParse({
106
+ oneofKind: "txOpen",
107
+ txOpen: {
108
+ name,
109
+ enableFormattedErrors,
110
+ writable: writable ? require_api.TxAPI_Open_Request_WritableTx.WRITABLE : require_api.TxAPI_Open_Request_WritableTx.NOT_WRITABLE
111
+ }
112
+ }, (r) => (0, _milaboratories_ts_helpers.notEmpty)(r.txOpen.tx?.id));
113
+ this.track(this.globalTxId);
114
+ this.globalTxId.catch((err) => {
115
+ if (!this.globalTxIdWasAwaited) console.warn(err);
116
+ });
117
+ this._stat.txCount++;
118
+ }
119
+ /**
120
+ * Collect all pending promises for the transaction finalization.
121
+ */
122
+ track(promiseOrCallback) {
123
+ return this.pending.track(promiseOrCallback);
124
+ }
125
+ async drainAndAwaitPendingOps() {
126
+ await this.pending.awaitAll();
127
+ }
128
+ sendSingleAndParse(r, parser) {
129
+ return this.pending.track(async () => {
130
+ const rawResponsePromise = this.ll.send(r, false);
131
+ this.pending.track(rawResponsePromise);
132
+ await this.drainAndAwaitPendingOps();
133
+ return parser(await rawResponsePromise);
134
+ });
135
+ }
136
+ sendMultiAndParse(r, parser) {
137
+ return this.pending.track(async () => {
138
+ const rawResponsePromise = this.ll.send(r, true);
139
+ this.pending.track(rawResponsePromise);
140
+ await this.drainAndAwaitPendingOps();
141
+ return parser(await rawResponsePromise);
142
+ });
143
+ }
144
+ async sendVoidSync(r) {
145
+ await this.ll.send(r, false);
146
+ }
147
+ /** Requests sent with this method should never produce recoverable errors */
148
+ sendVoidAsync(r) {
149
+ this.track(this.sendVoidSync(r));
150
+ }
151
+ checkTxOpen() {
152
+ if (this._completed) throw new Error("Transaction already closed");
153
+ }
154
+ get completed() {
155
+ return this._completed;
156
+ }
157
+ /** Commit & closes transaction. {@link TxCommitConflict} is thrown on
158
+ * commit conflicts. */
159
+ async commit() {
160
+ this.checkTxOpen();
161
+ this._completed = true;
162
+ if (!this.writable) {
163
+ const completeResult = this.track(this.ll.complete());
164
+ await this.drainAndAwaitPendingOps();
165
+ await completeResult;
166
+ await this.ll.await();
167
+ } else {
168
+ const commitResponse = this.track(this.sendSingleAndParse({
169
+ oneofKind: "txCommit",
170
+ txCommit: {}
171
+ }, (r) => r.txCommit.success));
172
+ const completeResult = this.track(this.ll.complete());
173
+ await this.drainAndAwaitPendingOps();
174
+ if (!await commitResponse) throw new TxCommitConflict();
175
+ await completeResult;
176
+ await this.ll.await();
177
+ }
178
+ }
179
+ async discard() {
180
+ this.checkTxOpen();
181
+ this._completed = true;
182
+ const discardResponse = this.sendVoidSync({
183
+ oneofKind: "txDiscard",
184
+ txDiscard: {}
185
+ });
186
+ this.track(discardResponse);
187
+ const completeResult = this.track(this.ll.complete());
188
+ await this.drainAndAwaitPendingOps();
189
+ await discardResponse;
190
+ await completeResult;
191
+ await this.ll.await();
192
+ }
193
+ get clientRoot() {
194
+ return require_types.ensureResourceIdNotNull(this._clientRoot);
195
+ }
196
+ createSingleton(name, type, errorIfExists = false) {
197
+ const localId = this.nextLocalResourceId(false);
198
+ const globalId = this.sendSingleAndParse({
199
+ oneofKind: "resourceCreateSingleton",
200
+ resourceCreateSingleton: {
201
+ type,
202
+ id: localId,
203
+ data: Buffer.from(name),
204
+ errorIfExists
205
+ }
206
+ }, (r) => r.resourceCreateSingleton.resourceId);
207
+ this.track(globalId);
208
+ return {
209
+ globalId,
210
+ localId
211
+ };
212
+ }
213
+ getSingleton(name, loadFields = true) {
214
+ return this.sendSingleAndParse({
215
+ oneofKind: "resourceGetSingleton",
216
+ resourceGetSingleton: {
217
+ data: Buffer.from(name),
218
+ loadFields
219
+ }
220
+ }, (r) => require_type_conversion.protoToResource((0, _milaboratories_ts_helpers.notEmpty)(r.resourceGetSingleton.resource)));
221
+ }
222
+ createResource(root, req, parser) {
223
+ const localId = this.nextLocalResourceId(root);
224
+ const globalId = this.sendSingleAndParse(req(localId), (r) => parser(r));
225
+ this.track(globalId);
226
+ return {
227
+ globalId,
228
+ localId
229
+ };
230
+ }
231
+ createRoot(type) {
232
+ this._stat.rootsCreated++;
233
+ return this.createResource(true, (localId) => ({
234
+ oneofKind: "resourceCreateRoot",
235
+ resourceCreateRoot: {
236
+ type,
237
+ id: localId
238
+ }
239
+ }), (r) => r.resourceCreateRoot.resourceId);
240
+ }
241
+ createStruct(type, data) {
242
+ this._stat.structsCreated++;
243
+ this._stat.structsCreatedDataBytes += data?.length ?? 0;
244
+ return this.createResource(false, (localId) => ({
245
+ oneofKind: "resourceCreateStruct",
246
+ resourceCreateStruct: {
247
+ type,
248
+ id: localId,
249
+ data: data === void 0 ? void 0 : typeof data === "string" ? Buffer.from(data) : data
250
+ }
251
+ }), (r) => r.resourceCreateStruct.resourceId);
252
+ }
253
+ createEphemeral(type, data) {
254
+ this._stat.ephemeralsCreated++;
255
+ this._stat.ephemeralsCreatedDataBytes += data?.length ?? 0;
256
+ return this.createResource(false, (localId) => ({
257
+ oneofKind: "resourceCreateEphemeral",
258
+ resourceCreateEphemeral: {
259
+ type,
260
+ id: localId,
261
+ data: data === void 0 ? void 0 : typeof data === "string" ? Buffer.from(data) : data
262
+ }
263
+ }), (r) => r.resourceCreateEphemeral.resourceId);
264
+ }
265
+ createValue(type, data, errorIfExists = false) {
266
+ this._stat.valuesCreated++;
267
+ this._stat.valuesCreatedDataBytes += data?.length ?? 0;
268
+ return this.createResource(false, (localId) => ({
269
+ oneofKind: "resourceCreateValue",
270
+ resourceCreateValue: {
271
+ type,
272
+ id: localId,
273
+ data: typeof data === "string" ? Buffer.from(data) : data,
274
+ errorIfExists
275
+ }
276
+ }), (r) => r.resourceCreateValue.resourceId);
277
+ }
278
+ createJsonValue(data) {
279
+ const jsonData = (0, _milaboratories_ts_helpers.canonicalJsonBytes)(data);
280
+ return this.createValue(require_pl.JsonObject, jsonData, false);
281
+ }
282
+ createJsonGzValue(data, minSizeToGzip = 16384) {
283
+ const { data: jsonData, isGzipped } = (0, _milaboratories_ts_helpers.canonicalJsonGzBytes)(data, minSizeToGzip);
284
+ return this.createValue(isGzipped ? require_pl.JsonGzObject : require_pl.JsonObject, jsonData, false);
285
+ }
286
+ createError(message) {
287
+ return this.createValue(require_error_resource.ErrorResourceType, JSON.stringify({ message }));
288
+ }
289
+ setResourceName(name, rId) {
290
+ this.sendVoidAsync({
291
+ oneofKind: "resourceNameSet",
292
+ resourceNameSet: {
293
+ resourceId: toResourceId(rId),
294
+ name
295
+ }
296
+ });
297
+ }
298
+ deleteResourceName(name) {
299
+ this.sendVoidAsync({
300
+ oneofKind: "resourceNameDelete",
301
+ resourceNameDelete: { name }
302
+ });
303
+ }
304
+ getResourceByName(name) {
305
+ return this.sendSingleAndParse({
306
+ oneofKind: "resourceNameGet",
307
+ resourceNameGet: { name }
308
+ }, (r) => require_types.ensureResourceIdNotNull(r.resourceNameGet.resourceId));
309
+ }
310
+ checkResourceNameExists(name) {
311
+ return this.sendSingleAndParse({
312
+ oneofKind: "resourceNameExists",
313
+ resourceNameExists: { name }
314
+ }, (r) => r.resourceNameExists.exists);
315
+ }
316
+ removeResource(rId) {
317
+ this.sendVoidAsync({
318
+ oneofKind: "resourceRemove",
319
+ resourceRemove: { id: rId }
320
+ });
321
+ }
322
+ resourceExists(rId) {
323
+ return this.sendSingleAndParse({
324
+ oneofKind: "resourceExists",
325
+ resourceExists: { resourceId: rId }
326
+ }, (r) => r.resourceExists.exists);
327
+ }
328
+ async getResourceData(rId, loadFields = true, ignoreCache = false) {
329
+ return this.track(async () => {
330
+ if (!ignoreCache && !isResourceRef(rId) && !require_types.isLocalResourceId(rId)) {
331
+ const fromCache = this.sharedResourceDataCache.get(rId);
332
+ if (fromCache && fromCache.cacheTxOpenTimestamp < this.txOpenTimestamp) {
333
+ if (!loadFields) {
334
+ this._stat.rGetDataCacheHits++;
335
+ this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;
336
+ return fromCache.basicData;
337
+ } else if (fromCache.data) {
338
+ this._stat.rGetDataCacheHits++;
339
+ this._stat.rGetDataCacheBytes += fromCache.basicData.data?.length ?? 0;
340
+ this._stat.rGetDataCacheFields += fromCache.data.fields.length;
341
+ return fromCache.data;
342
+ }
343
+ }
344
+ }
345
+ const result = await this.sendSingleAndParse({
346
+ oneofKind: "resourceGet",
347
+ resourceGet: {
348
+ resourceId: toResourceId(rId),
349
+ loadFields
350
+ }
351
+ }, (r) => require_type_conversion.protoToResource((0, _milaboratories_ts_helpers.notEmpty)(r.resourceGet.resource)));
352
+ this._stat.rGetDataNetRequests++;
353
+ this._stat.rGetDataNetBytes += result.data?.length ?? 0;
354
+ this._stat.rGetDataNetFields += result.fields.length;
355
+ if (!isResourceRef(rId) && !require_types.isLocalResourceId(rId) && this.finalPredicate(result)) {
356
+ (0, _milaboratories_ts_helpers.deepFreeze)(result);
357
+ const fromCache = this.sharedResourceDataCache.get(rId);
358
+ if (fromCache) {
359
+ if (loadFields && !fromCache.data) {
360
+ fromCache.data = result;
361
+ fromCache.cacheTxOpenTimestamp = this.txOpenTimestamp;
362
+ }
363
+ } else {
364
+ const basicData = require_types.extractBasicResourceData(result);
365
+ (0, _milaboratories_ts_helpers.deepFreeze)(basicData);
366
+ if (loadFields) this.sharedResourceDataCache.set(rId, {
367
+ basicData,
368
+ data: result,
369
+ cacheTxOpenTimestamp: this.txOpenTimestamp
370
+ });
371
+ else this.sharedResourceDataCache.set(rId, {
372
+ basicData,
373
+ data: void 0,
374
+ cacheTxOpenTimestamp: this.txOpenTimestamp
375
+ });
376
+ }
377
+ }
378
+ return result;
379
+ });
380
+ }
381
+ async getResourceDataIfExists(rId, loadFields = true) {
382
+ return this.track(async () => {
383
+ const result = await notFoundToUndefined(async () => await this.getResourceData(rId, loadFields, true));
384
+ if (result === void 0 && !isResourceRef(rId) && !require_types.isLocalResourceId(rId)) this.sharedResourceDataCache.delete(rId);
385
+ return result;
386
+ });
387
+ }
388
+ /**
389
+ * Inform platform that resource will not get any new input fields.
390
+ * This is required, when client creates resource without schema and wants
391
+ * controller to start calculations.
392
+ * Most controllers will not start calculations even when all inputs
393
+ * have their values, if inputs list is not locked.
394
+ */
395
+ lockInputs(rId) {
396
+ this._stat.inputsLocked++;
397
+ this.sendVoidAsync({
398
+ oneofKind: "resourceLockInputs",
399
+ resourceLockInputs: { resourceId: toResourceId(rId) }
400
+ });
401
+ }
402
+ /**
403
+ * Inform platform that resource will not get any new output fields.
404
+ * This is required for resource to pass deduplication.
405
+ */
406
+ lockOutputs(rId) {
407
+ this._stat.outputsLocked++;
408
+ this.sendVoidAsync({
409
+ oneofKind: "resourceLockOutputs",
410
+ resourceLockOutputs: { resourceId: toResourceId(rId) }
411
+ });
412
+ }
413
+ lock(rID) {
414
+ this.lockInputs(rID);
415
+ this.lockOutputs(rID);
416
+ }
417
+ setResourceError(rId, ref) {
418
+ this.sendVoidAsync({
419
+ oneofKind: "resourceSetError",
420
+ resourceSetError: {
421
+ resourceId: toResourceId(rId),
422
+ errorResourceId: toResourceId(ref)
423
+ }
424
+ });
425
+ }
426
+ createField(fId, fieldType, value) {
427
+ this._stat.fieldsCreated++;
428
+ this.sendVoidAsync({
429
+ oneofKind: "fieldCreate",
430
+ fieldCreate: {
431
+ type: require_type_conversion.fieldTypeToProto(fieldType),
432
+ id: toFieldId(fId)
433
+ }
434
+ });
435
+ if (value !== void 0) this.setField(fId, value);
436
+ }
437
+ fieldExists(fId) {
438
+ return this.sendSingleAndParse({
439
+ oneofKind: "fieldExists",
440
+ fieldExists: { field: toFieldId(fId) }
441
+ }, (r) => r.fieldExists.exists);
442
+ }
443
+ setField(fId, ref) {
444
+ this._stat.fieldsSet++;
445
+ if (isResource(ref)) this.sendVoidAsync({
446
+ oneofKind: "fieldSet",
447
+ fieldSet: {
448
+ field: toFieldId(fId),
449
+ value: {
450
+ resourceId: toResourceId(ref),
451
+ fieldName: ""
452
+ }
453
+ }
454
+ });
455
+ else this.sendVoidAsync({
456
+ oneofKind: "fieldSet",
457
+ fieldSet: {
458
+ field: toFieldId(fId),
459
+ value: toFieldId(ref)
460
+ }
461
+ });
462
+ }
463
+ setFieldError(fId, ref) {
464
+ this._stat.fieldsSet++;
465
+ this.sendVoidAsync({
466
+ oneofKind: "fieldSetError",
467
+ fieldSetError: {
468
+ field: toFieldId(fId),
469
+ errResourceId: toResourceId(ref)
470
+ }
471
+ });
472
+ }
473
+ getField(fId) {
474
+ this._stat.fieldsGet++;
475
+ return this.sendSingleAndParse({
476
+ oneofKind: "fieldGet",
477
+ fieldGet: { field: toFieldId(fId) }
478
+ }, (r) => require_type_conversion.protoToField((0, _milaboratories_ts_helpers.notEmpty)(r.fieldGet.field)));
479
+ }
480
+ async getFieldIfExists(fId) {
481
+ return this.track(notFoundToUndefined(async () => await this.getField(fId)));
482
+ }
483
+ resetField(fId) {
484
+ this.sendVoidAsync({
485
+ oneofKind: "fieldReset",
486
+ fieldReset: { field: toFieldId(fId) }
487
+ });
488
+ }
489
+ removeField(fId) {
490
+ this.sendVoidAsync({
491
+ oneofKind: "fieldRemove",
492
+ fieldRemove: { field: toFieldId(fId) }
493
+ });
494
+ }
495
+ async listKeyValues(rId) {
496
+ return this.track(async () => {
497
+ const result = await this.sendMultiAndParse({
498
+ oneofKind: "resourceKeyValueList",
499
+ resourceKeyValueList: {
500
+ resourceId: toResourceId(rId),
501
+ startFrom: "",
502
+ limit: 0
503
+ }
504
+ }, (r) => r.map((e) => e.resourceKeyValueList.record));
505
+ this._stat.kvListRequests++;
506
+ this._stat.kvListEntries += result.length;
507
+ for (const kv of result) this._stat.kvListBytes += kv.key.length + kv.value.length;
508
+ return result;
509
+ });
510
+ }
511
+ async listKeyValuesString(rId) {
512
+ return this.track(async () => (await this.listKeyValues(rId)).map(({ key, value }) => ({
513
+ key,
514
+ value: Buffer.from(value).toString()
515
+ })));
516
+ }
517
+ async listKeyValuesIfResourceExists(rId) {
518
+ return this.track(notFoundToUndefined(async () => await this.listKeyValues(rId)));
519
+ }
520
+ async listKeyValuesStringIfResourceExists(rId) {
521
+ return this.track(notFoundToUndefined(async () => await this.listKeyValuesString(rId)));
522
+ }
523
+ setKValue(rId, key, value) {
524
+ this._stat.kvSetRequests++;
525
+ this._stat.kvSetBytes++;
526
+ this.sendVoidAsync({
527
+ oneofKind: "resourceKeyValueSet",
528
+ resourceKeyValueSet: {
529
+ resourceId: toResourceId(rId),
530
+ key,
531
+ value: require_util.toBytes(value)
532
+ }
533
+ });
534
+ }
535
+ deleteKValue(rId, key) {
536
+ this.sendVoidAsync({
537
+ oneofKind: "resourceKeyValueDelete",
538
+ resourceKeyValueDelete: {
539
+ resourceId: toResourceId(rId),
540
+ key
541
+ }
542
+ });
543
+ }
544
+ async getKValue(rId, key) {
545
+ return this.track(async () => {
546
+ const result = await this.sendSingleAndParse({
547
+ oneofKind: "resourceKeyValueGet",
548
+ resourceKeyValueGet: {
549
+ resourceId: toResourceId(rId),
550
+ key
551
+ }
552
+ }, (r) => r.resourceKeyValueGet.value);
553
+ this._stat.kvGetRequests++;
554
+ this._stat.kvGetBytes += result.length;
555
+ return result;
556
+ });
557
+ }
558
+ async getKValueString(rId, key) {
559
+ return this.track(async () => Buffer.from(await this.getKValue(rId, key)).toString());
560
+ }
561
+ async getKValueJson(rId, key) {
562
+ return this.track(async () => JSON.parse(await this.getKValueString(rId, key)));
563
+ }
564
+ async getKValueIfExists(rId, key) {
565
+ return this.track(async () => {
566
+ const result = await this.sendSingleAndParse({
567
+ oneofKind: "resourceKeyValueGetIfExists",
568
+ resourceKeyValueGetIfExists: {
569
+ resourceId: toResourceId(rId),
570
+ key
571
+ }
572
+ }, (r) => r.resourceKeyValueGetIfExists.exists ? r.resourceKeyValueGetIfExists.value : void 0);
573
+ this._stat.kvGetRequests++;
574
+ this._stat.kvGetBytes += result?.length ?? 0;
575
+ return result;
576
+ });
577
+ }
578
+ async getKValueStringIfExists(rId, key) {
579
+ return this.track(async () => {
580
+ const data = await this.getKValueIfExists(rId, key);
581
+ return data === void 0 ? void 0 : Buffer.from(data).toString();
582
+ });
583
+ }
584
+ async getKValueJsonIfExists(rId, key) {
585
+ return this.track(async () => {
586
+ const str = await this.getKValueString(rId, key);
587
+ if (str === void 0) return void 0;
588
+ return JSON.parse(str);
589
+ });
590
+ }
591
+ /** Resolves existing or create first level resource from */
592
+ getFutureFieldValue(rId, fieldName, fieldType) {
593
+ const data = Buffer.from(JSON.stringify({
594
+ fieldName,
595
+ fieldType
596
+ }));
597
+ const getFieldResource = this.createEphemeral({
598
+ name: "json/getField",
599
+ version: "1"
600
+ }, data);
601
+ this.setField({
602
+ resourceId: getFieldResource,
603
+ fieldName: "resource"
604
+ }, rId);
605
+ return {
606
+ resourceId: getFieldResource,
607
+ fieldName: "result"
608
+ };
609
+ }
610
+ async getGlobalTxId() {
611
+ this.globalTxIdWasAwaited = true;
612
+ return await this.globalTxId;
613
+ }
614
+ /** Closes output event stream */
615
+ async complete() {
616
+ if (this._completed) return;
617
+ this._completed = true;
618
+ const completeResult = this.track(this.ll.complete());
619
+ await this.drainAndAwaitPendingOps();
620
+ await completeResult;
621
+ }
622
+ /** Await incoming message loop termination and throw
623
+ * any leftover errors if it was unsuccessful */
624
+ async await() {
625
+ await this.ll.await();
626
+ }
627
+ nextLocalResourceId(root) {
628
+ return require_types.createLocalResourceId(root, ++this.localResourceIdCounter, this.localTxId);
629
+ }
630
+ static localTxIdCounter = 0;
631
+ static nextLocalTxId() {
632
+ PlTransaction.localTxIdCounter++;
633
+ if (PlTransaction.localTxIdCounter === require_types.MaxTxId) PlTransaction.localTxIdCounter = 1;
634
+ return PlTransaction.localTxIdCounter;
635
+ }
636
+ };
675
637
 
638
+ //#endregion
676
639
  exports.PlTransaction = PlTransaction;
677
640
  exports.TxCommitConflict = TxCommitConflict;
678
641
  exports.field = field;
@@ -685,4 +648,4 @@ exports.toFieldId = toFieldId;
685
648
  exports.toGlobalFieldId = toGlobalFieldId;
686
649
  exports.toGlobalResourceId = toGlobalResourceId;
687
650
  exports.toResourceId = toResourceId;
688
- //# sourceMappingURL=transaction.cjs.map
651
+ //# sourceMappingURL=transaction.cjs.map