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