@restatedev/restate-sdk 1.3.2 → 1.4.0

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 (416) hide show
  1. package/README.md +4 -4
  2. package/dist/cjs/src/context.d.ts +35 -2
  3. package/dist/cjs/src/context.d.ts.map +1 -1
  4. package/dist/cjs/src/context.js +8 -8
  5. package/dist/cjs/src/context.js.map +1 -1
  6. package/dist/cjs/src/context_impl.d.ts +61 -17
  7. package/dist/cjs/src/context_impl.d.ts.map +1 -1
  8. package/dist/cjs/src/context_impl.js +529 -316
  9. package/dist/cjs/src/context_impl.js.map +1 -1
  10. package/dist/cjs/src/endpoint/endpoint_builder.d.ts +2 -3
  11. package/dist/cjs/src/endpoint/endpoint_builder.d.ts.map +1 -1
  12. package/dist/cjs/src/endpoint/endpoint_builder.js +4 -9
  13. package/dist/cjs/src/endpoint/endpoint_builder.js.map +1 -1
  14. package/dist/cjs/src/endpoint/fetch_endpoint.d.ts +1 -2
  15. package/dist/cjs/src/endpoint/fetch_endpoint.d.ts.map +1 -1
  16. package/dist/cjs/src/endpoint/fetch_endpoint.js.map +1 -1
  17. package/dist/cjs/src/endpoint/handlers/generic.d.ts +15 -0
  18. package/dist/cjs/src/endpoint/handlers/generic.d.ts.map +1 -1
  19. package/dist/cjs/src/endpoint/handlers/generic.js +185 -83
  20. package/dist/cjs/src/endpoint/handlers/generic.js.map +1 -1
  21. package/dist/cjs/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts +300 -0
  22. package/dist/cjs/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts.map +1 -0
  23. package/dist/cjs/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js +1646 -0
  24. package/dist/cjs/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js.map +1 -0
  25. package/dist/cjs/src/endpoint/lambda_endpoint.d.ts +1 -2
  26. package/dist/cjs/src/endpoint/lambda_endpoint.d.ts.map +1 -1
  27. package/dist/cjs/src/endpoint/lambda_endpoint.js.map +1 -1
  28. package/dist/cjs/src/endpoint/node_endpoint.d.ts +1 -2
  29. package/dist/cjs/src/endpoint/node_endpoint.d.ts.map +1 -1
  30. package/dist/cjs/src/endpoint/node_endpoint.js.map +1 -1
  31. package/dist/cjs/src/generated/version.d.ts +1 -1
  32. package/dist/cjs/src/generated/version.js +1 -1
  33. package/dist/cjs/src/logger.js +1 -1
  34. package/dist/cjs/src/logger.js.map +1 -1
  35. package/dist/cjs/src/types/components.js +2 -2
  36. package/dist/cjs/src/types/components.js.map +1 -1
  37. package/dist/cjs/src/types/errors.d.ts +1 -32
  38. package/dist/cjs/src/types/errors.d.ts.map +1 -1
  39. package/dist/cjs/src/types/errors.js +9 -76
  40. package/dist/cjs/src/types/errors.js.map +1 -1
  41. package/dist/cjs/src/types/rpc.js +9 -9
  42. package/dist/cjs/src/types/rpc.js.map +1 -1
  43. package/dist/cjs/src/user_agent.d.ts +1 -1
  44. package/dist/cjs/src/utils/completable_promise.d.ts +9 -0
  45. package/dist/cjs/src/utils/completable_promise.d.ts.map +1 -0
  46. package/dist/cjs/src/utils/completable_promise.js +34 -0
  47. package/dist/cjs/src/utils/completable_promise.js.map +1 -0
  48. package/dist/cjs/src/utils/rand.d.ts +1 -1
  49. package/dist/cjs/src/utils/rand.d.ts.map +1 -1
  50. package/dist/cjs/src/utils/rand.js +1 -1
  51. package/dist/cjs/src/utils/rand.js.map +1 -1
  52. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  53. package/dist/esm/src/context.d.ts +35 -2
  54. package/dist/esm/src/context.d.ts.map +1 -1
  55. package/dist/esm/src/context.js +8 -8
  56. package/dist/esm/src/context.js.map +1 -1
  57. package/dist/esm/src/context_impl.d.ts +61 -17
  58. package/dist/esm/src/context_impl.d.ts.map +1 -1
  59. package/dist/esm/src/context_impl.js +531 -318
  60. package/dist/esm/src/context_impl.js.map +1 -1
  61. package/dist/esm/src/endpoint/endpoint_builder.d.ts +2 -3
  62. package/dist/esm/src/endpoint/endpoint_builder.d.ts.map +1 -1
  63. package/dist/esm/src/endpoint/endpoint_builder.js +4 -9
  64. package/dist/esm/src/endpoint/endpoint_builder.js.map +1 -1
  65. package/dist/esm/src/endpoint/fetch_endpoint.d.ts +1 -2
  66. package/dist/esm/src/endpoint/fetch_endpoint.d.ts.map +1 -1
  67. package/dist/esm/src/endpoint/fetch_endpoint.js.map +1 -1
  68. package/dist/esm/src/endpoint/handlers/generic.d.ts +15 -0
  69. package/dist/esm/src/endpoint/handlers/generic.d.ts.map +1 -1
  70. package/dist/esm/src/endpoint/handlers/generic.js +162 -84
  71. package/dist/esm/src/endpoint/handlers/generic.js.map +1 -1
  72. package/dist/esm/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts +300 -0
  73. package/dist/esm/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts.map +1 -0
  74. package/dist/esm/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js +1636 -0
  75. package/dist/esm/src/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js.map +1 -0
  76. package/dist/esm/src/endpoint/lambda_endpoint.d.ts +1 -2
  77. package/dist/esm/src/endpoint/lambda_endpoint.d.ts.map +1 -1
  78. package/dist/esm/src/endpoint/lambda_endpoint.js.map +1 -1
  79. package/dist/esm/src/endpoint/node_endpoint.d.ts +1 -2
  80. package/dist/esm/src/endpoint/node_endpoint.d.ts.map +1 -1
  81. package/dist/esm/src/endpoint/node_endpoint.js.map +1 -1
  82. package/dist/esm/src/generated/version.d.ts +1 -1
  83. package/dist/esm/src/generated/version.js +1 -1
  84. package/dist/esm/src/logger.js +1 -1
  85. package/dist/esm/src/logger.js.map +1 -1
  86. package/dist/esm/src/types/components.js +2 -2
  87. package/dist/esm/src/types/components.js.map +1 -1
  88. package/dist/esm/src/types/errors.d.ts +1 -32
  89. package/dist/esm/src/types/errors.d.ts.map +1 -1
  90. package/dist/esm/src/types/errors.js +8 -70
  91. package/dist/esm/src/types/errors.js.map +1 -1
  92. package/dist/esm/src/types/rpc.js +9 -9
  93. package/dist/esm/src/types/rpc.js.map +1 -1
  94. package/dist/esm/src/user_agent.d.ts +1 -1
  95. package/dist/esm/src/utils/completable_promise.d.ts +9 -0
  96. package/dist/esm/src/utils/completable_promise.d.ts.map +1 -0
  97. package/dist/esm/src/utils/completable_promise.js +30 -0
  98. package/dist/esm/src/utils/completable_promise.js.map +1 -0
  99. package/dist/esm/src/utils/rand.d.ts +1 -1
  100. package/dist/esm/src/utils/rand.d.ts.map +1 -1
  101. package/dist/esm/src/utils/rand.js +1 -1
  102. package/dist/esm/src/utils/rand.js.map +1 -1
  103. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  104. package/package.json +2 -6
  105. package/dist/cjs/src/connection/connection.d.ts +0 -84
  106. package/dist/cjs/src/connection/connection.d.ts.map +0 -1
  107. package/dist/cjs/src/connection/connection.js +0 -181
  108. package/dist/cjs/src/connection/connection.js.map +0 -1
  109. package/dist/cjs/src/endpoint/request_signing/basex.d.ts +0 -10
  110. package/dist/cjs/src/endpoint/request_signing/basex.d.ts.map +0 -1
  111. package/dist/cjs/src/endpoint/request_signing/basex.js +0 -138
  112. package/dist/cjs/src/endpoint/request_signing/basex.js.map +0 -1
  113. package/dist/cjs/src/endpoint/request_signing/ed25519.d.ts +0 -17
  114. package/dist/cjs/src/endpoint/request_signing/ed25519.d.ts.map +0 -1
  115. package/dist/cjs/src/endpoint/request_signing/ed25519.js +0 -80
  116. package/dist/cjs/src/endpoint/request_signing/ed25519.js.map +0 -1
  117. package/dist/cjs/src/endpoint/request_signing/v1.d.ts +0 -10
  118. package/dist/cjs/src/endpoint/request_signing/v1.d.ts.map +0 -1
  119. package/dist/cjs/src/endpoint/request_signing/v1.js +0 -151
  120. package/dist/cjs/src/endpoint/request_signing/v1.js.map +0 -1
  121. package/dist/cjs/src/endpoint/request_signing/validate.d.ts +0 -19
  122. package/dist/cjs/src/endpoint/request_signing/validate.d.ts.map +0 -1
  123. package/dist/cjs/src/endpoint/request_signing/validate.js +0 -45
  124. package/dist/cjs/src/endpoint/request_signing/validate.js.map +0 -1
  125. package/dist/cjs/src/generated/proto/discovery_pb.d.ts +0 -18
  126. package/dist/cjs/src/generated/proto/discovery_pb.d.ts.map +0 -1
  127. package/dist/cjs/src/generated/proto/discovery_pb.js +0 -40
  128. package/dist/cjs/src/generated/proto/discovery_pb.js.map +0 -1
  129. package/dist/cjs/src/generated/proto/javascript_pb.d.ts +0 -26
  130. package/dist/cjs/src/generated/proto/javascript_pb.d.ts.map +0 -1
  131. package/dist/cjs/src/generated/proto/javascript_pb.js +0 -52
  132. package/dist/cjs/src/generated/proto/javascript_pb.js.map +0 -1
  133. package/dist/cjs/src/generated/proto/protocol_pb.d.ts +0 -1051
  134. package/dist/cjs/src/generated/proto/protocol_pb.d.ts.map +0 -1
  135. package/dist/cjs/src/generated/proto/protocol_pb.js +0 -1288
  136. package/dist/cjs/src/generated/proto/protocol_pb.js.map +0 -1
  137. package/dist/cjs/src/invocation.d.ts +0 -48
  138. package/dist/cjs/src/invocation.d.ts.map +0 -1
  139. package/dist/cjs/src/invocation.js +0 -158
  140. package/dist/cjs/src/invocation.js.map +0 -1
  141. package/dist/cjs/src/io/decoder.d.ts +0 -9
  142. package/dist/cjs/src/io/decoder.d.ts.map +0 -1
  143. package/dist/cjs/src/io/decoder.js +0 -137
  144. package/dist/cjs/src/io/decoder.js.map +0 -1
  145. package/dist/cjs/src/io/encoder.d.ts +0 -4
  146. package/dist/cjs/src/io/encoder.d.ts.map +0 -1
  147. package/dist/cjs/src/io/encoder.js +0 -41
  148. package/dist/cjs/src/io/encoder.js.map +0 -1
  149. package/dist/cjs/src/journal.d.ts +0 -52
  150. package/dist/cjs/src/journal.d.ts.map +0 -1
  151. package/dist/cjs/src/journal.js +0 -438
  152. package/dist/cjs/src/journal.js.map +0 -1
  153. package/dist/cjs/src/local_state_store.d.ts +0 -14
  154. package/dist/cjs/src/local_state_store.d.ts.map +0 -1
  155. package/dist/cjs/src/local_state_store.js +0 -83
  156. package/dist/cjs/src/local_state_store.js.map +0 -1
  157. package/dist/cjs/src/promise_combinator_tracker.d.ts +0 -29
  158. package/dist/cjs/src/promise_combinator_tracker.d.ts.map +0 -1
  159. package/dist/cjs/src/promise_combinator_tracker.js +0 -130
  160. package/dist/cjs/src/promise_combinator_tracker.js.map +0 -1
  161. package/dist/cjs/src/state_machine.d.ts +0 -85
  162. package/dist/cjs/src/state_machine.d.ts.map +0 -1
  163. package/dist/cjs/src/state_machine.js +0 -426
  164. package/dist/cjs/src/state_machine.js.map +0 -1
  165. package/dist/cjs/src/types/protocol.d.ts +0 -40
  166. package/dist/cjs/src/types/protocol.d.ts.map +0 -1
  167. package/dist/cjs/src/types/protocol.js +0 -237
  168. package/dist/cjs/src/types/protocol.js.map +0 -1
  169. package/dist/cjs/src/types/types.d.ts +0 -20
  170. package/dist/cjs/src/types/types.d.ts.map +0 -1
  171. package/dist/cjs/src/types/types.js +0 -110
  172. package/dist/cjs/src/types/types.js.map +0 -1
  173. package/dist/cjs/src/utils/message_logger.d.ts +0 -7
  174. package/dist/cjs/src/utils/message_logger.d.ts.map +0 -1
  175. package/dist/cjs/src/utils/message_logger.js +0 -79
  176. package/dist/cjs/src/utils/message_logger.js.map +0 -1
  177. package/dist/cjs/src/utils/promises.d.ts +0 -15
  178. package/dist/cjs/src/utils/promises.d.ts.map +0 -1
  179. package/dist/cjs/src/utils/promises.js +0 -68
  180. package/dist/cjs/src/utils/promises.js.map +0 -1
  181. package/dist/cjs/src/utils/utils.d.ts +0 -3
  182. package/dist/cjs/src/utils/utils.d.ts.map +0 -1
  183. package/dist/cjs/src/utils/utils.js +0 -105
  184. package/dist/cjs/src/utils/utils.js.map +0 -1
  185. package/dist/cjs/test/awakeable.test.d.ts +0 -2
  186. package/dist/cjs/test/awakeable.test.d.ts.map +0 -1
  187. package/dist/cjs/test/awakeable.test.js +0 -141
  188. package/dist/cjs/test/awakeable.test.js.map +0 -1
  189. package/dist/cjs/test/complete_awakeable.test.d.ts +0 -2
  190. package/dist/cjs/test/complete_awakeable.test.d.ts.map +0 -1
  191. package/dist/cjs/test/complete_awakeable.test.js +0 -123
  192. package/dist/cjs/test/complete_awakeable.test.js.map +0 -1
  193. package/dist/cjs/test/eager_state.test.d.ts +0 -2
  194. package/dist/cjs/test/eager_state.test.d.ts.map +0 -1
  195. package/dist/cjs/test/eager_state.test.js +0 -280
  196. package/dist/cjs/test/eager_state.test.js.map +0 -1
  197. package/dist/cjs/test/get_and_set_state.test.d.ts +0 -2
  198. package/dist/cjs/test/get_and_set_state.test.d.ts.map +0 -1
  199. package/dist/cjs/test/get_and_set_state.test.js +0 -233
  200. package/dist/cjs/test/get_and_set_state.test.js.map +0 -1
  201. package/dist/cjs/test/get_state.test.d.ts +0 -2
  202. package/dist/cjs/test/get_state.test.d.ts.map +0 -1
  203. package/dist/cjs/test/get_state.test.js +0 -328
  204. package/dist/cjs/test/get_state.test.js.map +0 -1
  205. package/dist/cjs/test/lambda.test.d.ts +0 -2
  206. package/dist/cjs/test/lambda.test.d.ts.map +0 -1
  207. package/dist/cjs/test/lambda.test.js +0 -258
  208. package/dist/cjs/test/lambda.test.js.map +0 -1
  209. package/dist/cjs/test/message_coders.test.d.ts +0 -2
  210. package/dist/cjs/test/message_coders.test.d.ts.map +0 -1
  211. package/dist/cjs/test/message_coders.test.js +0 -37
  212. package/dist/cjs/test/message_coders.test.js.map +0 -1
  213. package/dist/cjs/test/promise_combinator_tracker.test.d.ts +0 -2
  214. package/dist/cjs/test/promise_combinator_tracker.test.d.ts.map +0 -1
  215. package/dist/cjs/test/promise_combinator_tracker.test.js +0 -156
  216. package/dist/cjs/test/promise_combinator_tracker.test.js.map +0 -1
  217. package/dist/cjs/test/promise_combinators.test.d.ts +0 -2
  218. package/dist/cjs/test/promise_combinators.test.d.ts.map +0 -1
  219. package/dist/cjs/test/promise_combinators.test.js +0 -433
  220. package/dist/cjs/test/promise_combinators.test.js.map +0 -1
  221. package/dist/cjs/test/promises.test.d.ts +0 -2
  222. package/dist/cjs/test/promises.test.d.ts.map +0 -1
  223. package/dist/cjs/test/promises.test.js +0 -32
  224. package/dist/cjs/test/promises.test.js.map +0 -1
  225. package/dist/cjs/test/protocol_stream.test.d.ts +0 -2
  226. package/dist/cjs/test/protocol_stream.test.d.ts.map +0 -1
  227. package/dist/cjs/test/protocol_stream.test.js +0 -243
  228. package/dist/cjs/test/protocol_stream.test.js.map +0 -1
  229. package/dist/cjs/test/protoutils.d.ts +0 -44
  230. package/dist/cjs/test/protoutils.d.ts.map +0 -1
  231. package/dist/cjs/test/protoutils.js +0 -368
  232. package/dist/cjs/test/protoutils.js.map +0 -1
  233. package/dist/cjs/test/service_bind.test.d.ts +0 -2
  234. package/dist/cjs/test/service_bind.test.d.ts.map +0 -1
  235. package/dist/cjs/test/service_bind.test.js +0 -118
  236. package/dist/cjs/test/service_bind.test.js.map +0 -1
  237. package/dist/cjs/test/side_effect.test.d.ts +0 -2
  238. package/dist/cjs/test/side_effect.test.d.ts.map +0 -1
  239. package/dist/cjs/test/side_effect.test.js +0 -133
  240. package/dist/cjs/test/side_effect.test.js.map +0 -1
  241. package/dist/cjs/test/sleep.test.d.ts +0 -2
  242. package/dist/cjs/test/sleep.test.d.ts.map +0 -1
  243. package/dist/cjs/test/sleep.test.js +0 -224
  244. package/dist/cjs/test/sleep.test.js.map +0 -1
  245. package/dist/cjs/test/state_keys.test.d.ts +0 -2
  246. package/dist/cjs/test/state_keys.test.d.ts.map +0 -1
  247. package/dist/cjs/test/state_keys.test.js +0 -92
  248. package/dist/cjs/test/state_keys.test.js.map +0 -1
  249. package/dist/cjs/test/state_machine.test.d.ts +0 -2
  250. package/dist/cjs/test/state_machine.test.d.ts.map +0 -1
  251. package/dist/cjs/test/state_machine.test.js +0 -42
  252. package/dist/cjs/test/state_machine.test.js.map +0 -1
  253. package/dist/cjs/test/testdriver.d.ts +0 -39
  254. package/dist/cjs/test/testdriver.d.ts.map +0 -1
  255. package/dist/cjs/test/testdriver.js +0 -188
  256. package/dist/cjs/test/testdriver.js.map +0 -1
  257. package/dist/cjs/test/utils.test.d.ts +0 -2
  258. package/dist/cjs/test/utils.test.d.ts.map +0 -1
  259. package/dist/cjs/test/utils.test.js +0 -93
  260. package/dist/cjs/test/utils.test.js.map +0 -1
  261. package/dist/esm/src/connection/connection.d.ts +0 -84
  262. package/dist/esm/src/connection/connection.d.ts.map +0 -1
  263. package/dist/esm/src/connection/connection.js +0 -177
  264. package/dist/esm/src/connection/connection.js.map +0 -1
  265. package/dist/esm/src/endpoint/request_signing/basex.d.ts +0 -10
  266. package/dist/esm/src/endpoint/request_signing/basex.d.ts.map +0 -1
  267. package/dist/esm/src/endpoint/request_signing/basex.js +0 -136
  268. package/dist/esm/src/endpoint/request_signing/basex.js.map +0 -1
  269. package/dist/esm/src/endpoint/request_signing/ed25519.d.ts +0 -17
  270. package/dist/esm/src/endpoint/request_signing/ed25519.d.ts.map +0 -1
  271. package/dist/esm/src/endpoint/request_signing/ed25519.js +0 -52
  272. package/dist/esm/src/endpoint/request_signing/ed25519.js.map +0 -1
  273. package/dist/esm/src/endpoint/request_signing/v1.d.ts +0 -10
  274. package/dist/esm/src/endpoint/request_signing/v1.d.ts.map +0 -1
  275. package/dist/esm/src/endpoint/request_signing/v1.js +0 -143
  276. package/dist/esm/src/endpoint/request_signing/v1.js.map +0 -1
  277. package/dist/esm/src/endpoint/request_signing/validate.d.ts +0 -19
  278. package/dist/esm/src/endpoint/request_signing/validate.d.ts.map +0 -1
  279. package/dist/esm/src/endpoint/request_signing/validate.js +0 -40
  280. package/dist/esm/src/endpoint/request_signing/validate.js.map +0 -1
  281. package/dist/esm/src/generated/proto/discovery_pb.d.ts +0 -18
  282. package/dist/esm/src/generated/proto/discovery_pb.d.ts.map +0 -1
  283. package/dist/esm/src/generated/proto/discovery_pb.js +0 -37
  284. package/dist/esm/src/generated/proto/discovery_pb.js.map +0 -1
  285. package/dist/esm/src/generated/proto/javascript_pb.d.ts +0 -26
  286. package/dist/esm/src/generated/proto/javascript_pb.d.ts.map +0 -1
  287. package/dist/esm/src/generated/proto/javascript_pb.js +0 -48
  288. package/dist/esm/src/generated/proto/javascript_pb.js.map +0 -1
  289. package/dist/esm/src/generated/proto/protocol_pb.d.ts +0 -1051
  290. package/dist/esm/src/generated/proto/protocol_pb.d.ts.map +0 -1
  291. package/dist/esm/src/generated/proto/protocol_pb.js +0 -1258
  292. package/dist/esm/src/generated/proto/protocol_pb.js.map +0 -1
  293. package/dist/esm/src/invocation.d.ts +0 -48
  294. package/dist/esm/src/invocation.d.ts.map +0 -1
  295. package/dist/esm/src/invocation.js +0 -153
  296. package/dist/esm/src/invocation.js.map +0 -1
  297. package/dist/esm/src/io/decoder.d.ts +0 -9
  298. package/dist/esm/src/io/decoder.d.ts.map +0 -1
  299. package/dist/esm/src/io/decoder.js +0 -128
  300. package/dist/esm/src/io/decoder.js.map +0 -1
  301. package/dist/esm/src/io/encoder.d.ts +0 -4
  302. package/dist/esm/src/io/encoder.d.ts.map +0 -1
  303. package/dist/esm/src/io/encoder.js +0 -36
  304. package/dist/esm/src/io/encoder.js.map +0 -1
  305. package/dist/esm/src/journal.d.ts +0 -52
  306. package/dist/esm/src/journal.d.ts.map +0 -1
  307. package/dist/esm/src/journal.js +0 -410
  308. package/dist/esm/src/journal.js.map +0 -1
  309. package/dist/esm/src/local_state_store.d.ts +0 -14
  310. package/dist/esm/src/local_state_store.d.ts.map +0 -1
  311. package/dist/esm/src/local_state_store.js +0 -79
  312. package/dist/esm/src/local_state_store.js.map +0 -1
  313. package/dist/esm/src/promise_combinator_tracker.d.ts +0 -29
  314. package/dist/esm/src/promise_combinator_tracker.d.ts.map +0 -1
  315. package/dist/esm/src/promise_combinator_tracker.js +0 -125
  316. package/dist/esm/src/promise_combinator_tracker.js.map +0 -1
  317. package/dist/esm/src/state_machine.d.ts +0 -85
  318. package/dist/esm/src/state_machine.d.ts.map +0 -1
  319. package/dist/esm/src/state_machine.js +0 -399
  320. package/dist/esm/src/state_machine.js.map +0 -1
  321. package/dist/esm/src/types/protocol.d.ts +0 -40
  322. package/dist/esm/src/types/protocol.d.ts.map +0 -1
  323. package/dist/esm/src/types/protocol.js +0 -206
  324. package/dist/esm/src/types/protocol.js.map +0 -1
  325. package/dist/esm/src/types/types.d.ts +0 -20
  326. package/dist/esm/src/types/types.d.ts.map +0 -1
  327. package/dist/esm/src/types/types.js +0 -105
  328. package/dist/esm/src/types/types.js.map +0 -1
  329. package/dist/esm/src/utils/message_logger.d.ts +0 -7
  330. package/dist/esm/src/utils/message_logger.d.ts.map +0 -1
  331. package/dist/esm/src/utils/message_logger.js +0 -75
  332. package/dist/esm/src/utils/message_logger.js.map +0 -1
  333. package/dist/esm/src/utils/promises.d.ts +0 -15
  334. package/dist/esm/src/utils/promises.d.ts.map +0 -1
  335. package/dist/esm/src/utils/promises.js +0 -63
  336. package/dist/esm/src/utils/promises.js.map +0 -1
  337. package/dist/esm/src/utils/utils.d.ts +0 -3
  338. package/dist/esm/src/utils/utils.d.ts.map +0 -1
  339. package/dist/esm/src/utils/utils.js +0 -101
  340. package/dist/esm/src/utils/utils.js.map +0 -1
  341. package/dist/esm/test/awakeable.test.d.ts +0 -2
  342. package/dist/esm/test/awakeable.test.d.ts.map +0 -1
  343. package/dist/esm/test/awakeable.test.js +0 -139
  344. package/dist/esm/test/awakeable.test.js.map +0 -1
  345. package/dist/esm/test/complete_awakeable.test.d.ts +0 -2
  346. package/dist/esm/test/complete_awakeable.test.d.ts.map +0 -1
  347. package/dist/esm/test/complete_awakeable.test.js +0 -121
  348. package/dist/esm/test/complete_awakeable.test.js.map +0 -1
  349. package/dist/esm/test/eager_state.test.d.ts +0 -2
  350. package/dist/esm/test/eager_state.test.d.ts.map +0 -1
  351. package/dist/esm/test/eager_state.test.js +0 -278
  352. package/dist/esm/test/eager_state.test.js.map +0 -1
  353. package/dist/esm/test/get_and_set_state.test.d.ts +0 -2
  354. package/dist/esm/test/get_and_set_state.test.d.ts.map +0 -1
  355. package/dist/esm/test/get_and_set_state.test.js +0 -231
  356. package/dist/esm/test/get_and_set_state.test.js.map +0 -1
  357. package/dist/esm/test/get_state.test.d.ts +0 -2
  358. package/dist/esm/test/get_state.test.d.ts.map +0 -1
  359. package/dist/esm/test/get_state.test.js +0 -326
  360. package/dist/esm/test/get_state.test.js.map +0 -1
  361. package/dist/esm/test/lambda.test.d.ts +0 -2
  362. package/dist/esm/test/lambda.test.d.ts.map +0 -1
  363. package/dist/esm/test/lambda.test.js +0 -233
  364. package/dist/esm/test/lambda.test.js.map +0 -1
  365. package/dist/esm/test/message_coders.test.d.ts +0 -2
  366. package/dist/esm/test/message_coders.test.d.ts.map +0 -1
  367. package/dist/esm/test/message_coders.test.js +0 -35
  368. package/dist/esm/test/message_coders.test.js.map +0 -1
  369. package/dist/esm/test/promise_combinator_tracker.test.d.ts +0 -2
  370. package/dist/esm/test/promise_combinator_tracker.test.d.ts.map +0 -1
  371. package/dist/esm/test/promise_combinator_tracker.test.js +0 -154
  372. package/dist/esm/test/promise_combinator_tracker.test.js.map +0 -1
  373. package/dist/esm/test/promise_combinators.test.d.ts +0 -2
  374. package/dist/esm/test/promise_combinators.test.d.ts.map +0 -1
  375. package/dist/esm/test/promise_combinators.test.js +0 -431
  376. package/dist/esm/test/promise_combinators.test.js.map +0 -1
  377. package/dist/esm/test/promises.test.d.ts +0 -2
  378. package/dist/esm/test/promises.test.d.ts.map +0 -1
  379. package/dist/esm/test/promises.test.js +0 -30
  380. package/dist/esm/test/promises.test.js.map +0 -1
  381. package/dist/esm/test/protocol_stream.test.d.ts +0 -2
  382. package/dist/esm/test/protocol_stream.test.d.ts.map +0 -1
  383. package/dist/esm/test/protocol_stream.test.js +0 -218
  384. package/dist/esm/test/protocol_stream.test.js.map +0 -1
  385. package/dist/esm/test/protoutils.d.ts +0 -44
  386. package/dist/esm/test/protoutils.d.ts.map +0 -1
  387. package/dist/esm/test/protoutils.js +0 -335
  388. package/dist/esm/test/protoutils.js.map +0 -1
  389. package/dist/esm/test/service_bind.test.d.ts +0 -2
  390. package/dist/esm/test/service_bind.test.d.ts.map +0 -1
  391. package/dist/esm/test/service_bind.test.js +0 -93
  392. package/dist/esm/test/service_bind.test.js.map +0 -1
  393. package/dist/esm/test/side_effect.test.d.ts +0 -2
  394. package/dist/esm/test/side_effect.test.d.ts.map +0 -1
  395. package/dist/esm/test/side_effect.test.js +0 -131
  396. package/dist/esm/test/side_effect.test.js.map +0 -1
  397. package/dist/esm/test/sleep.test.d.ts +0 -2
  398. package/dist/esm/test/sleep.test.d.ts.map +0 -1
  399. package/dist/esm/test/sleep.test.js +0 -222
  400. package/dist/esm/test/sleep.test.js.map +0 -1
  401. package/dist/esm/test/state_keys.test.d.ts +0 -2
  402. package/dist/esm/test/state_keys.test.d.ts.map +0 -1
  403. package/dist/esm/test/state_keys.test.js +0 -90
  404. package/dist/esm/test/state_keys.test.js.map +0 -1
  405. package/dist/esm/test/state_machine.test.d.ts +0 -2
  406. package/dist/esm/test/state_machine.test.d.ts.map +0 -1
  407. package/dist/esm/test/state_machine.test.js +0 -40
  408. package/dist/esm/test/state_machine.test.js.map +0 -1
  409. package/dist/esm/test/testdriver.d.ts +0 -39
  410. package/dist/esm/test/testdriver.d.ts.map +0 -1
  411. package/dist/esm/test/testdriver.js +0 -182
  412. package/dist/esm/test/testdriver.js.map +0 -1
  413. package/dist/esm/test/utils.test.d.ts +0 -2
  414. package/dist/esm/test/utils.test.d.ts.map +0 -1
  415. package/dist/esm/test/utils.test.js +0 -91
  416. package/dist/esm/test/utils.test.js.map +0 -1
@@ -11,23 +11,18 @@
11
11
  */
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.ContextImpl = void 0;
14
- const protocol_pb_js_1 = require("./generated/proto/protocol_pb.js");
15
- const protocol_js_1 = require("./types/protocol.js");
16
14
  const errors_js_1 = require("./types/errors.js");
17
- const protobuf_1 = require("@bufbuild/protobuf");
18
15
  const rpc_js_1 = require("./types/rpc.js");
19
16
  const restate_sdk_core_1 = require("@restatedev/restate-sdk-core");
20
17
  const rand_js_1 = require("./utils/rand.js");
21
- const promise_combinator_tracker_js_1 = require("./promise_combinator_tracker.js");
22
- const node_buffer_1 = require("node:buffer");
23
18
  class ContextImpl {
19
+ coreVm;
20
+ input;
24
21
  console;
25
22
  handlerKind;
26
- keyedContextKey;
27
- stateMachine;
28
- // This is used to guard users against calling ctx.sideEffect without awaiting it.
29
- // See https://github.com/restatedev/sdk-typescript/issues/197 for more details.
30
- executingRun = false;
23
+ invocationEndPromise;
24
+ inputReader;
25
+ outputWriter;
31
26
  invocationRequest;
32
27
  rand;
33
28
  date = {
@@ -38,137 +33,122 @@ class ContextImpl {
38
33
  return this.run(() => new Date().toJSON());
39
34
  },
40
35
  };
41
- constructor(id, console, handlerKind, keyedContextKey, invocationValue, invocationHeaders, attemptHeaders, extraArgs,
42
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
- stateMachine) {
36
+ currentRead;
37
+ constructor(coreVm, input, console, handlerKind, attemptHeaders, extraArgs, invocationEndPromise, inputReader, outputWriter) {
38
+ this.coreVm = coreVm;
39
+ this.input = input;
44
40
  this.console = console;
45
41
  this.handlerKind = handlerKind;
46
- this.keyedContextKey = keyedContextKey;
47
- this.stateMachine = stateMachine;
42
+ this.invocationEndPromise = invocationEndPromise;
43
+ this.inputReader = inputReader;
44
+ this.outputWriter = outputWriter;
48
45
  this.invocationRequest = {
49
- id,
50
- headers: invocationHeaders,
51
- attemptHeaders,
52
- body: invocationValue,
46
+ id: input.invocation_id,
47
+ headers: input.headers.reduce((headers, { key, value }) => {
48
+ headers.set(key, value);
49
+ return headers;
50
+ }, new Map()),
51
+ attemptHeaders: Object.entries(attemptHeaders).reduce((headers, [key, value]) => {
52
+ if (value !== undefined) {
53
+ headers.set(key, value instanceof Array ? value[0] : value);
54
+ }
55
+ return headers;
56
+ }, new Map()),
57
+ body: input.input,
53
58
  extraArgs,
54
59
  };
55
- this.rand = new rand_js_1.RandImpl(id, this.checkState.bind(this));
56
- }
57
- promise(name, serde) {
58
- return new DurablePromiseImpl(this, name, serde);
60
+ this.rand = new rand_js_1.RandImpl(input.invocation_id, () => {
61
+ if (coreVm.is_inside_run()) {
62
+ throw new Error("Cannot generate random numbers within a run closure. Use the random object outside the run closure.");
63
+ }
64
+ });
59
65
  }
60
66
  get key() {
61
67
  switch (this.handlerKind) {
62
68
  case rpc_js_1.HandlerKind.EXCLUSIVE:
63
69
  case rpc_js_1.HandlerKind.SHARED:
64
70
  case rpc_js_1.HandlerKind.WORKFLOW: {
65
- if (this.keyedContextKey === undefined) {
66
- throw new errors_js_1.TerminalError("unexpected missing key");
67
- }
68
- return this.keyedContextKey;
71
+ return this.input.key;
69
72
  }
70
- case rpc_js_1.HandlerKind.SERVICE:
71
- throw new errors_js_1.TerminalError("unexpected missing key");
72
73
  default:
73
- throw new errors_js_1.TerminalError("unknown handler type");
74
+ throw new errors_js_1.TerminalError("this handler type doesn't support key()");
74
75
  }
75
76
  }
76
77
  request() {
77
78
  return this.invocationRequest;
78
79
  }
79
- // DON'T make this function async!!! see sideEffect comment for details.
80
80
  get(name, serde) {
81
- // Check if this is a valid action
82
- this.checkState("get state");
83
- // Create the message and let the state machine process it
84
- const msg = new protocol_pb_js_1.GetStateEntryMessage({
85
- key: new TextEncoder().encode(name),
86
- });
87
- const completed = this.stateMachine.localStateStore.tryCompleteGet(name, msg);
88
- const getState = async () => {
89
- const result = await this.stateMachine.handleUserCodeMessage(protocol_js_1.GET_STATE_ENTRY_MESSAGE_TYPE, msg, completed);
90
- // If the GetState message did not have a value or empty,
91
- // then we went to the runtime to get the value.
92
- // When we get the response, we set it in the localStateStore,
93
- // to answer subsequent requests
94
- if (!completed) {
95
- this.stateMachine.localStateStore.add(name, result);
96
- }
97
- if (!(result instanceof Uint8Array)) {
81
+ return this.processCompletableEntry((vm) => vm.sys_get_state(name), (asyncResultValue) => {
82
+ if (asyncResultValue === "Empty") {
83
+ // Empty
98
84
  return null;
99
85
  }
100
- return (serde ?? (0, rpc_js_1.defaultSerde)()).deserialize(result);
101
- };
102
- return getState();
86
+ else if ("Success" in asyncResultValue) {
87
+ return (serde ?? (0, rpc_js_1.defaultSerde)()).deserialize(asyncResultValue.Success);
88
+ }
89
+ else if ("Failure" in asyncResultValue) {
90
+ throw new errors_js_1.TerminalError(asyncResultValue.Failure.message, {
91
+ errorCode: asyncResultValue.Failure.code,
92
+ });
93
+ }
94
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
95
+ });
103
96
  }
104
- // DON'T make this function async!!! see sideEffect comment for details.
105
97
  stateKeys() {
106
- // Check if this is a valid action
107
- this.checkState("state keys");
108
- // Create the message and let the state machine process it
109
- const msg = new protocol_pb_js_1.GetStateKeysEntryMessage({});
110
- const completed = this.stateMachine.localStateStore.tryCompletedGetStateKeys(msg);
111
- const getStateKeys = async () => {
112
- const result = await this.stateMachine.handleUserCodeMessage(protocol_js_1.GET_STATE_KEYS_ENTRY_MESSAGE_TYPE, msg, completed);
113
- return result.keys.map((b) => new TextDecoder().decode(b));
114
- };
115
- return getStateKeys();
98
+ return this.processCompletableEntry((vm) => vm.sys_get_state_keys(), (asyncResultValue) => {
99
+ if (typeof asyncResultValue === "object" &&
100
+ "StateKeys" in asyncResultValue) {
101
+ return asyncResultValue.StateKeys;
102
+ }
103
+ else if (typeof asyncResultValue === "object" &&
104
+ "Failure" in asyncResultValue) {
105
+ throw new errors_js_1.TerminalError(asyncResultValue.Failure.message, {
106
+ errorCode: asyncResultValue.Failure.code,
107
+ });
108
+ }
109
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
110
+ });
116
111
  }
117
112
  set(name, value, serde) {
118
- this.checkState("set state");
119
- const bytes = (serde ?? (0, rpc_js_1.defaultSerde)()).serialize(value);
120
- const msg = this.stateMachine.localStateStore.set(name, bytes);
121
- this.stateMachine
122
- .handleUserCodeMessage(protocol_js_1.SET_STATE_ENTRY_MESSAGE_TYPE, msg)
123
- .catch((e) => this.stateMachine.handleDanglingPromiseError(e));
113
+ this.processNonCompletableEntry((vm) => vm.sys_set_state(name, (serde ?? (0, rpc_js_1.defaultSerde)()).serialize(value)));
124
114
  }
125
115
  clear(name) {
126
- this.checkState("clear state");
127
- const msg = this.stateMachine.localStateStore.clear(name);
128
- this.stateMachine
129
- .handleUserCodeMessage(protocol_js_1.CLEAR_STATE_ENTRY_MESSAGE_TYPE, msg)
130
- .catch((e) => this.stateMachine.handleDanglingPromiseError(e));
116
+ this.processNonCompletableEntry((vm) => vm.sys_clear_state(name));
131
117
  }
132
118
  clearAll() {
133
- this.checkState("clear all state");
134
- const msg = this.stateMachine.localStateStore.clearAll();
135
- this.stateMachine
136
- .handleUserCodeMessage(protocol_js_1.CLEAR_ALL_STATE_ENTRY_MESSAGE_TYPE, msg)
137
- .catch((e) => this.stateMachine.handleDanglingPromiseError(e));
119
+ this.processNonCompletableEntry((vm) => vm.sys_clear_all_state());
138
120
  }
139
121
  // --- Calls, background calls, etc
140
122
  //
141
123
  genericCall(call) {
142
124
  const requestSerde = call.inputSerde ?? restate_sdk_core_1.serde.binary;
143
125
  const responseSerde = call.outputSerde ?? restate_sdk_core_1.serde.binary;
144
- const parameter = requestSerde.serialize(call.parameter);
145
- const msg = new protocol_pb_js_1.CallEntryMessage({
146
- serviceName: call.service,
147
- handlerName: call.method,
148
- parameter,
149
- key: call.key,
126
+ return this.processCompletableEntry((vm) => {
127
+ const parameter = requestSerde.serialize(call.parameter);
128
+ return vm.sys_call(call.service, call.method, parameter, call.key);
129
+ }, (asyncResultValue) => {
130
+ if (typeof asyncResultValue === "object" &&
131
+ "Success" in asyncResultValue) {
132
+ return responseSerde.deserialize(asyncResultValue.Success);
133
+ }
134
+ else if (typeof asyncResultValue === "object" &&
135
+ "Failure" in asyncResultValue) {
136
+ throw new errors_js_1.TerminalError(asyncResultValue.Failure.message, {
137
+ errorCode: asyncResultValue.Failure.code,
138
+ });
139
+ }
140
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
150
141
  });
151
- const rawRequest = this.stateMachine.handleUserCodeMessage(protocol_js_1.INVOKE_ENTRY_MESSAGE_TYPE, msg);
152
- const decoded = rawRequest.transform((res) => responseSerde.deserialize(res));
153
- return this.markCombineablePromise(decoded);
154
142
  }
155
143
  genericSend(send) {
156
- const requestSerde = send.inputSerde ?? restate_sdk_core_1.serde.binary;
157
- const parameter = requestSerde.serialize(send.parameter);
158
- const actualDelay = send.delay || 0;
159
- const jsInvokeTime = actualDelay > 0 ? Date.now() + actualDelay : protobuf_1.protoInt64.zero;
160
- const invokeTime = protobuf_1.protoInt64.parse(jsInvokeTime);
161
- const msg = new protocol_pb_js_1.OneWayCallEntryMessage({
162
- serviceName: send.service,
163
- handlerName: send.method,
164
- parameter,
165
- invokeTime,
166
- key: send.key,
167
- });
168
- this.stateMachine
169
- .handleUserCodeMessage(protocol_js_1.BACKGROUND_INVOKE_ENTRY_MESSAGE_TYPE, msg)
170
- .catch((e) => {
171
- this.stateMachine.handleDanglingPromiseError(e);
144
+ this.processNonCompletableEntry((vm) => {
145
+ const requestSerde = send.inputSerde ?? restate_sdk_core_1.serde.binary;
146
+ const parameter = requestSerde.serialize(send.parameter);
147
+ let delay;
148
+ if (send.delay !== undefined) {
149
+ delay = BigInt(send.delay);
150
+ }
151
+ vm.sys_send(send.service, send.method, parameter, send.key, delay);
172
152
  });
173
153
  }
174
154
  serviceClient({ name }) {
@@ -194,225 +174,406 @@ class ContextImpl {
194
174
  // and not in the promise context. To understand the semantic difference, make this function async and run the
195
175
  // UnawaitedSideEffectShouldFailSubsequentContextCall test.
196
176
  run(nameOrAction, actionSecondParameter, options) {
197
- this.checkState("run");
198
177
  const { name, action } = unpack(nameOrAction, actionSecondParameter);
199
- this.executingRun = true;
200
178
  const serde = options?.serde ?? (0, rpc_js_1.defaultSerde)();
201
- const executeRun = async () => {
202
- // in replay mode, we directly return the value from the log
203
- if (this.stateMachine.nextEntryWillBeReplayed()) {
204
- const emptyMsg = new protocol_pb_js_1.RunEntryMessage({});
205
- return this.stateMachine
206
- .handleUserCodeMessage(protocol_js_1.SIDE_EFFECT_ENTRY_MESSAGE_TYPE, emptyMsg)
207
- .transform((result) => {
208
- if (!result || result instanceof protocol_pb_js_1.Empty) {
209
- return undefined;
210
- }
211
- return serde.deserialize(result);
212
- });
179
+ try {
180
+ const runEnterResult = this.coreVm.sys_run_enter(name || "");
181
+ // Check if the run was already executed
182
+ if (typeof runEnterResult === "object" &&
183
+ "ExecutedWithSuccess" in runEnterResult) {
184
+ return Promise.resolve(serde.deserialize(runEnterResult.ExecutedWithSuccess));
185
+ }
186
+ else if (typeof runEnterResult === "object" &&
187
+ "ExecutedWithFailure" in runEnterResult) {
188
+ return Promise.reject(new errors_js_1.TerminalError(runEnterResult.ExecutedWithFailure.message, {
189
+ errorCode: runEnterResult.ExecutedWithFailure.code,
190
+ }));
213
191
  }
214
- let sideEffectResult;
192
+ }
193
+ catch (e) {
194
+ this.handleInvocationEndError(e);
195
+ return pendingPromise();
196
+ }
197
+ // We wrap the rest of the execution in this closure to create a future
198
+ const doRun = async () => {
199
+ const startTime = Date.now();
200
+ let res;
201
+ let err;
215
202
  try {
216
- sideEffectResult = await action();
203
+ res = await action();
217
204
  }
218
205
  catch (e) {
219
- if (!(e instanceof errors_js_1.TerminalError)) {
220
- ///non terminal errors are retirable.
221
- // we do not commit the error itself into the journal, but rather let restate know about this
222
- // so that restate can retry this invocation later.
223
- // Before we can propagate this error to the user, we must let the state machine know that this attempt
224
- // is finished with an error, and it should not append anything else to the journal from now on.
225
- const error = (0, errors_js_1.ensureError)(e);
226
- const additionalContext = {
227
- relatedEntryName: name,
228
- relatedEntryType: protocol_js_1.SIDE_EFFECT_ENTRY_MESSAGE_TYPE,
229
- };
230
- await this.stateMachine.sendErrorAndFinish(error, additionalContext);
231
- throw e;
206
+ err = (0, errors_js_1.ensureError)(e);
207
+ }
208
+ const attemptDuration = Date.now() - startTime;
209
+ // Record the result/failure, get back the handle for the ack.
210
+ let handle;
211
+ try {
212
+ if (err !== undefined) {
213
+ if (err instanceof errors_js_1.TerminalError) {
214
+ // Record failure, go ahead
215
+ handle = this.coreVm.sys_run_exit_failure({
216
+ code: err.code,
217
+ message: err.message,
218
+ });
219
+ }
220
+ else {
221
+ if (options?.retryIntervalFactor === undefined &&
222
+ options?.initialRetryIntervalMillis === undefined &&
223
+ options?.maxRetryAttempts === undefined &&
224
+ options?.maxRetryDurationMillis === undefined &&
225
+ options?.maxRetryIntervalMillis === undefined) {
226
+ // If no retry option was set, simply throw the error.
227
+ // This will lead to the invoker applying its retry, without the SDK overriding it.
228
+ throw err;
229
+ }
230
+ handle = this.coreVm.sys_run_exit_failure_transient(err.message, err.cause?.toString(), BigInt(attemptDuration), {
231
+ factor: options?.retryIntervalFactor || 2.0,
232
+ initial_interval: options?.initialRetryIntervalMillis || 50,
233
+ max_attempts: options?.maxRetryAttempts,
234
+ max_duration: options?.maxRetryDurationMillis,
235
+ max_interval: options?.maxRetryIntervalMillis || 10 * 1000,
236
+ });
237
+ }
238
+ }
239
+ else {
240
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
241
+ // @ts-expect-error
242
+ handle = this.coreVm.sys_run_exit_success(serde.serialize(res));
232
243
  }
233
- // we commit a terminal error from the side effect to the journal, and re-throw it into
234
- // the function. that way, any catching by the user and reacting to it will be
235
- // deterministic on replay
236
- const error = (0, errors_js_1.ensureError)(e);
237
- const failure = (0, errors_js_1.errorToFailure)(error);
238
- const sideEffectMsg = new protocol_pb_js_1.RunEntryMessage({
239
- name,
240
- result: { case: "failure", value: failure },
241
- });
242
- // this may throw an error from the SDK/runtime/connection side, in case the
243
- // failure message cannot be committed to the journal. That error would then
244
- // be returned from this function (replace the original error)
245
- // that is acceptable, because in such a situation (failure to append to journal),
246
- // the state machine closes anyways and no further operations will succeed and the
247
- // the execution aborts
248
- await this.stateMachine.handleUserCodeMessage(protocol_js_1.SIDE_EFFECT_ENTRY_MESSAGE_TYPE, sideEffectMsg, false, true);
249
- throw e;
250
244
  }
251
- // we have this code outside the above try/catch block, to ensure that any error arising
252
- // from here is not incorrectly attributed to the side-effect
253
- const sideEffectMsg = sideEffectResult !== undefined
254
- ? new protocol_pb_js_1.RunEntryMessage({
255
- name,
256
- result: {
257
- case: "value",
258
- value: serde.serialize(sideEffectResult),
259
- },
260
- })
261
- : new protocol_pb_js_1.RunEntryMessage({
262
- name,
263
- });
264
- // if an error arises from committing the side effect result, then this error will
265
- // be thrown here (reject the returned promise) and the function will see that error,
266
- // even if the side-effect function completed correctly
267
- // that is acceptable, because in such a situation (failure to append to journal),
268
- // the state machine closes anyways and reports an execution failure, meaning no further
269
- // operations will succeed and the the execution will be retried.
270
- // If the side-effect result did in fact not make it to the journal, then the side-effect
271
- // re-executes, and if it made it to the journal after all (error happend inly during
272
- // ack-back), then retries will use the journaled result.
273
- // So all good in any case, due to the beauty of "the runtime log is the ground thruth" approach.
274
- await this.stateMachine.handleUserCodeMessage(protocol_js_1.SIDE_EFFECT_ENTRY_MESSAGE_TYPE, sideEffectMsg, false, true);
275
- return sideEffectResult;
245
+ catch (e) {
246
+ this.handleInvocationEndError(e);
247
+ return pendingPromise();
248
+ }
249
+ // Got the handle, wait for the result now (which we get once we get the ack)
250
+ return await this.pollAsyncResult(handle, (asyncResultValue) => {
251
+ if (typeof asyncResultValue === "object" &&
252
+ "Success" in asyncResultValue) {
253
+ return serde.deserialize(asyncResultValue.Success);
254
+ }
255
+ else if (typeof asyncResultValue === "object" &&
256
+ "Failure" in asyncResultValue) {
257
+ throw new errors_js_1.TerminalError(asyncResultValue.Failure.message, {
258
+ errorCode: asyncResultValue.Failure.code,
259
+ });
260
+ }
261
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
262
+ });
276
263
  };
277
- return executeRun().finally(() => {
278
- this.executingRun = false;
279
- });
264
+ return doRun();
280
265
  }
281
266
  sleep(millis) {
282
- this.checkState("sleep");
283
- return this.markCombineablePromise(this.sleepInternal(millis));
284
- }
285
- sleepInternal(millis) {
286
- return this.stateMachine.handleUserCodeMessage(protocol_js_1.SLEEP_ENTRY_MESSAGE_TYPE, new protocol_pb_js_1.SleepEntryMessage({
287
- wakeUpTime: protobuf_1.protoInt64.parse(Date.now() + millis),
288
- }));
289
- }
290
- // -- Awakeables
291
- awakeable(serde) {
292
- this.checkState("awakeable");
293
- const msg = new protocol_pb_js_1.AwakeableEntryMessage();
294
- const promise = this.stateMachine
295
- .handleUserCodeMessage(protocol_js_1.AWAKEABLE_ENTRY_MESSAGE_TYPE, msg)
296
- .transform((result) => {
297
- if (!(result instanceof Uint8Array)) {
298
- // This should either be a filled buffer or an empty buffer but never anything else.
299
- throw errors_js_1.RetryableError.internal("Awakeable was not resolved with a buffer payload");
300
- }
301
- if (!serde) {
302
- return (0, rpc_js_1.defaultSerde)().deserialize(result);
303
- }
304
- if (result.length == 0) {
267
+ return this.processCompletableEntry((vm) => vm.sys_sleep(BigInt(millis)), (asyncResultValue) => {
268
+ if (asyncResultValue === "Empty") {
269
+ // Empty
305
270
  return undefined;
306
271
  }
307
- return serde.deserialize(result);
272
+ else if ("Failure" in asyncResultValue) {
273
+ throw new errors_js_1.TerminalError(asyncResultValue.Failure.message, {
274
+ errorCode: asyncResultValue.Failure.code,
275
+ });
276
+ }
277
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
308
278
  });
309
- // This needs to be done after handling the message in the state machine
310
- // otherwise the index is not yet incremented.
311
- const encodedEntryIndex = node_buffer_1.Buffer.alloc(4 /* Size of u32 */);
312
- encodedEntryIndex.writeUInt32BE(this.stateMachine.getUserCodeJournalIndex());
279
+ }
280
+ // -- Awakeables
281
+ awakeable(serde) {
282
+ let awakeable;
283
+ try {
284
+ awakeable = this.coreVm.sys_awakeable();
285
+ }
286
+ catch (e) {
287
+ this.handleInvocationEndError(e);
288
+ return {
289
+ id: "invalid",
290
+ promise: new LazyContextPromise(0, this, () => pendingPromise()),
291
+ };
292
+ }
313
293
  return {
314
- id: protocol_js_1.AWAKEABLE_IDENTIFIER_PREFIX +
315
- node_buffer_1.Buffer.concat([this.request().id, encodedEntryIndex]).toString("base64url"),
316
- promise: this.markCombineablePromise(promise),
294
+ id: awakeable.id,
295
+ promise: new LazyContextPromise(awakeable.handle, this, () => this.pollAsyncResult(awakeable.handle, (asyncResultValue) => {
296
+ if (typeof asyncResultValue === "object" &&
297
+ "Success" in asyncResultValue) {
298
+ if (!serde) {
299
+ return (0, rpc_js_1.defaultSerde)().deserialize(asyncResultValue.Success);
300
+ }
301
+ if (asyncResultValue.Success.length === 0) {
302
+ return undefined;
303
+ }
304
+ return serde.deserialize(asyncResultValue.Success);
305
+ }
306
+ else if (typeof asyncResultValue === "object" &&
307
+ "Failure" in asyncResultValue) {
308
+ throw new errors_js_1.TerminalError(asyncResultValue.Failure.message, {
309
+ errorCode: asyncResultValue.Failure.code,
310
+ });
311
+ }
312
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
313
+ })),
317
314
  };
318
315
  }
319
316
  resolveAwakeable(id, payload, serde) {
320
- // We coerce undefined to null as null can be stringified by JSON.stringify
321
- let value;
322
- if (serde) {
323
- value =
324
- payload == undefined ? new Uint8Array() : serde.serialize(payload);
325
- }
326
- else {
327
- value =
328
- payload != undefined
329
- ? (0, rpc_js_1.defaultSerde)().serialize(payload)
330
- : (0, rpc_js_1.defaultSerde)().serialize(null);
331
- }
332
- this.checkState("resolveAwakeable");
333
- this.completeAwakeable(id, {
334
- result: {
335
- case: "value",
336
- value,
337
- },
317
+ this.processNonCompletableEntry((vm) => {
318
+ // We coerce undefined to null as null can be stringified by JSON.stringify
319
+ let value;
320
+ if (serde) {
321
+ value =
322
+ payload === undefined ? new Uint8Array() : serde.serialize(payload);
323
+ }
324
+ else {
325
+ value =
326
+ payload !== undefined
327
+ ? (0, rpc_js_1.defaultSerde)().serialize(payload)
328
+ : (0, rpc_js_1.defaultSerde)().serialize(null);
329
+ }
330
+ vm.sys_complete_awakeable_success(id, value);
338
331
  });
339
332
  }
340
333
  rejectAwakeable(id, reason) {
341
- this.checkState("rejectAwakeable");
342
- this.completeAwakeable(id, {
343
- result: {
344
- case: "failure",
345
- value: { code: errors_js_1.UNKNOWN_ERROR_CODE, message: reason },
346
- },
334
+ this.processNonCompletableEntry((vm) => {
335
+ vm.sys_complete_awakeable_failure(id, {
336
+ code: errors_js_1.UNKNOWN_ERROR_CODE,
337
+ message: reason,
338
+ });
347
339
  });
348
340
  }
349
- completeAwakeable(id, base) {
350
- base.id = id;
351
- this.stateMachine
352
- .handleUserCodeMessage(protocol_js_1.COMPLETE_AWAKEABLE_ENTRY_MESSAGE_TYPE, new protocol_pb_js_1.CompleteAwakeableEntryMessage(base))
353
- .catch((e) => this.stateMachine.handleDanglingPromiseError(e));
341
+ promise(name, serde) {
342
+ return new DurablePromiseImpl(this, name, serde);
354
343
  }
355
344
  // Used by static methods of CombineablePromise
356
- static createCombinator(combinatorConstructor, promises) {
345
+ static createCombinator(combinatorType, promises) {
346
+ // Extract context from first promise
357
347
  const self = extractContext(promises[0]);
358
348
  if (!self) {
359
- throw errors_js_1.RetryableError.internal("Not a combinable promise");
349
+ throw new Error("Not a combinable promise");
360
350
  }
361
- const outPromises = [];
351
+ // Collect first the promises downcasted to the internal promise type
352
+ const castedPromises = [];
362
353
  for (const promise of promises) {
363
354
  if (extractContext(promise) !== self) {
364
- throw errors_js_1.RetryableError.internal("You're mixing up CombineablePromises from different RestateContext. This is not supported.");
355
+ self.handleInvocationEndError(new Error("You're mixing up CombineablePromises from different RestateContext. This is not supported."));
356
+ return pendingPromise();
357
+ }
358
+ castedPromises.push(promise);
359
+ }
360
+ const handles = new Uint32Array(castedPromises.map((p) => p.asyncResultHandle));
361
+ // From now on, lazily executes on await
362
+ return new LazyPromise(async () => {
363
+ let combinatorResultHandle;
364
+ try {
365
+ // Take output
366
+ const nextOutput1 = self.coreVm.take_output();
367
+ if (nextOutput1 instanceof Uint8Array) {
368
+ await self.outputWriter.write(nextOutput1);
369
+ }
370
+ for (;;) {
371
+ switch (combinatorType) {
372
+ case "All":
373
+ combinatorResultHandle =
374
+ self.coreVm.sys_try_complete_all_combinator(handles);
375
+ break;
376
+ case "Any":
377
+ combinatorResultHandle =
378
+ self.coreVm.sys_try_complete_any_combinator(handles);
379
+ break;
380
+ case "AllSettled":
381
+ combinatorResultHandle =
382
+ self.coreVm.sys_try_complete_all_settled_combinator(handles);
383
+ break;
384
+ case "Race":
385
+ case "OrTimeout":
386
+ combinatorResultHandle =
387
+ self.coreVm.sys_try_complete_race_combinator(handles);
388
+ break;
389
+ }
390
+ // We got a result, we're done in this loop
391
+ if (combinatorResultHandle !== undefined) {
392
+ break;
393
+ }
394
+ // No result yet, await the next read
395
+ await self.awaitNextRead();
396
+ }
397
+ // We got a result, we need to take_output to write the combinator entry, then we need to poll the result
398
+ const nextOutput = self.coreVm.take_output();
399
+ if (nextOutput instanceof Uint8Array) {
400
+ await self.outputWriter.write(nextOutput);
401
+ }
402
+ }
403
+ catch (e) {
404
+ if (e instanceof errors_js_1.TerminalError) {
405
+ // All good, this is a recorded failure
406
+ throw e;
407
+ }
408
+ // Not good, this is a retryable error.
409
+ self.handleInvocationEndError(e);
410
+ return await pendingPromise();
365
411
  }
366
- const index = promise
367
- .journalIndex;
368
- outPromises.push({
369
- id: (0, promise_combinator_tracker_js_1.newJournalEntryPromiseId)(index),
370
- promise: promise,
412
+ const handlesResult = await self.pollAsyncResult(combinatorResultHandle, (asyncResultValue) => {
413
+ if (typeof asyncResultValue === "object" &&
414
+ "CombinatorResult" in asyncResultValue) {
415
+ return asyncResultValue.CombinatorResult;
416
+ }
417
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
371
418
  });
419
+ const promisesMap = new Map(castedPromises.map((p) => [p.asyncResultHandle, p]));
420
+ // Now all we need to do is to construct the final output based on the handles,
421
+ // this depends on combinators themselves.
422
+ switch (combinatorType) {
423
+ case "All":
424
+ return this.extractAllCombinatorResult(handlesResult, promisesMap);
425
+ case "Any":
426
+ return this.extractAnyCombinatorResult(handlesResult, promisesMap);
427
+ case "AllSettled":
428
+ return this.extractAllSettledCombinatorResult(handlesResult, promisesMap);
429
+ case "Race":
430
+ // Just one promise succeeded
431
+ return promisesMap.get(handlesResult[0]);
432
+ case "OrTimeout":
433
+ // The sleep promise is always the second one in the list.
434
+ if (handlesResult[0] === castedPromises[1].asyncResultHandle) {
435
+ return Promise.reject(new errors_js_1.TimeoutError());
436
+ }
437
+ else {
438
+ return promisesMap.get(handlesResult[0]);
439
+ }
440
+ }
441
+ });
442
+ }
443
+ static async extractAllCombinatorResult(handlesResult, promisesMap) {
444
+ // The result can either all values, or one error
445
+ const resultValues = [];
446
+ for (const handle of handlesResult) {
447
+ try {
448
+ resultValues.push(await promisesMap.get(handle));
449
+ }
450
+ catch (e) {
451
+ return Promise.reject(e);
452
+ }
453
+ }
454
+ return Promise.resolve(resultValues);
455
+ }
456
+ static async extractAnyCombinatorResult(handlesResult, promisesMap) {
457
+ // The result can either be one value, or a list of errors
458
+ const resultFailures = [];
459
+ for (const handle of handlesResult) {
460
+ try {
461
+ return Promise.resolve(await promisesMap.get(handle));
462
+ }
463
+ catch (e) {
464
+ resultFailures.push(e);
465
+ }
466
+ }
467
+ // Giving back the cause here is completely fine, because all these errors in Aggregate error are Terminal errors!
468
+ return Promise.reject(new errors_js_1.TerminalError("All input promises failed", {
469
+ cause: new AggregateError(resultFailures),
470
+ }));
471
+ }
472
+ static async extractAllSettledCombinatorResult(handlesResult, promisesMap) {
473
+ const resultValues = [];
474
+ for (const handle of handlesResult) {
475
+ try {
476
+ resultValues.push(await promisesMap.get(handle));
477
+ }
478
+ catch (e) {
479
+ resultValues.push(e);
480
+ }
372
481
  }
373
- return self.stateMachine.createCombinator(combinatorConstructor, outPromises);
482
+ return Promise.resolve(resultValues);
374
483
  }
375
484
  // -- Various private methods
376
- checkNotExecutingRun(callType) {
377
- if (this.executingRun) {
378
- throw new errors_js_1.TerminalError(`Invoked a RestateContext method (${callType}) while a run() is still executing.
379
- Make sure you await the ctx.run() call before using any other RestateContext method.`, { errorCode: errors_js_1.INTERNAL_ERROR_CODE });
485
+ processNonCompletableEntry(vmCall) {
486
+ try {
487
+ vmCall(this.coreVm);
488
+ }
489
+ catch (e) {
490
+ this.handleInvocationEndError(e);
380
491
  }
381
492
  }
382
- checkState(callType) {
383
- this.checkNotExecutingRun(callType);
493
+ processCompletableEntry(vmCall, transformer) {
494
+ let handle;
495
+ try {
496
+ handle = vmCall(this.coreVm);
497
+ }
498
+ catch (e) {
499
+ this.handleInvocationEndError(e);
500
+ return new LazyContextPromise(0, this, () => pendingPromise());
501
+ }
502
+ return new LazyContextPromise(handle, this, () => this.pollAsyncResult(handle, transformer));
503
+ }
504
+ async pollAsyncResult(handle, transformer) {
505
+ try {
506
+ // Take output
507
+ const nextOutput = this.coreVm.take_output();
508
+ if (nextOutput instanceof Uint8Array) {
509
+ await this.outputWriter.write(nextOutput);
510
+ }
511
+ // Now loop waiting for the async result
512
+ let asyncResult = this.coreVm.take_async_result(handle);
513
+ while (asyncResult === "NotReady") {
514
+ await this.awaitNextRead();
515
+ // Using notify_await_point immediately before take_async_result
516
+ // makes sure the state machine will try to suspend only now,
517
+ // in case there aren't other concurrent tasks trying to poll this async result.
518
+ this.coreVm.notify_await_point(handle);
519
+ asyncResult = this.coreVm.take_async_result(handle);
520
+ }
521
+ return transformer(asyncResult);
522
+ }
523
+ catch (e) {
524
+ if (e instanceof errors_js_1.TerminalError) {
525
+ // All good, this is a recorded failure
526
+ throw e;
527
+ }
528
+ // Not good, this is a retryable error.
529
+ this.handleInvocationEndError(e);
530
+ return await pendingPromise();
531
+ }
384
532
  }
385
- markCombineablePromise(p) {
386
- const journalIndex = this.stateMachine.getUserCodeJournalIndex();
387
- const orTimeout = (millis) => {
388
- const sleepPromise = this.sleepInternal(millis).transform(() => {
389
- throw new errors_js_1.TimeoutError();
533
+ // This function triggers a read on the input reader,
534
+ // and will notify the caller that a read was executed
535
+ // and the result was piped in the state machine.
536
+ awaitNextRead() {
537
+ if (this.currentRead === undefined) {
538
+ // Register a new read
539
+ this.currentRead = this.readNext().finally(() => {
540
+ this.currentRead = undefined;
390
541
  });
391
- const sleepPromiseIndex = this.stateMachine.getUserCodeJournalIndex();
392
- return this.stateMachine.createCombinator(Promise.race.bind(Promise), [
393
- {
394
- id: (0, promise_combinator_tracker_js_1.newJournalEntryPromiseId)(journalIndex),
395
- promise: p,
396
- },
397
- {
398
- id: (0, promise_combinator_tracker_js_1.newJournalEntryPromiseId)(sleepPromiseIndex),
399
- promise: sleepPromise,
400
- },
401
- ]);
402
- };
403
- defineProperty(p, RESTATE_CTX_SYMBOL, this);
404
- defineProperty(p, "journalIndex", journalIndex);
405
- defineProperty(p, "orTimeout", orTimeout.bind(this));
406
- return p;
542
+ }
543
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
544
+ return new Promise((resolve) => this.currentRead?.finally(resolve));
545
+ }
546
+ async readNext() {
547
+ // Take input, and notify it to the vm
548
+ let nextValue;
549
+ try {
550
+ nextValue = await this.inputReader.read();
551
+ }
552
+ catch (e) {
553
+ this.handleInvocationEndError(e);
554
+ return pendingPromise();
555
+ }
556
+ if (nextValue.value !== undefined) {
557
+ this.coreVm.notify_input(nextValue.value);
558
+ }
559
+ if (nextValue.done) {
560
+ this.coreVm.notify_input_closed();
561
+ }
562
+ }
563
+ handleInvocationEndError(e) {
564
+ const error = (0, errors_js_1.ensureError)(e);
565
+ if (!(error instanceof errors_js_1.RestateError) ||
566
+ error.code !== errors_js_1.SUSPENDED_ERROR_CODE) {
567
+ this.console.warn("Function completed with an error.\n", error);
568
+ }
569
+ this.coreVm.notify_error(error.message, error.stack);
570
+ // From now on, no progress will be made.
571
+ this.invocationEndPromise.resolve();
407
572
  }
408
573
  }
409
574
  exports.ContextImpl = ContextImpl;
410
- // wraps defineProperty such that it informs tsc of the correct type of its output
411
- function defineProperty(obj, prop, value) {
412
- Object.defineProperty(obj, prop, { value });
413
- }
414
575
  function unpack(a, b) {
415
- if (typeof a == "string") {
576
+ if (typeof a === "string") {
416
577
  if (typeof b !== "function") {
417
578
  throw new TypeError("");
418
579
  }
@@ -452,56 +613,108 @@ class DurablePromiseImpl {
452
613
  }
453
614
  [Symbol.toStringTag] = "DurablePromise";
454
615
  get() {
455
- const msg = new protocol_pb_js_1.GetPromiseEntryMessage({
456
- key: this.name,
457
- });
458
- return this.ctx.markCombineablePromise(this.ctx.stateMachine
459
- .handleUserCodeMessage(protocol_js_1.GET_PROMISE_MESSAGE_TYPE, msg)
460
- .transform((v) => {
461
- if (!v) {
462
- return undefined;
616
+ return this.ctx.processCompletableEntry((vm) => vm.sys_get_promise(this.name), (asyncResultValue) => {
617
+ if (typeof asyncResultValue === "object" &&
618
+ "Success" in asyncResultValue) {
619
+ return this.serde.deserialize(asyncResultValue.Success);
463
620
  }
464
- if (v instanceof protocol_pb_js_1.Empty) {
465
- return undefined;
621
+ else if (typeof asyncResultValue === "object" &&
622
+ "Failure" in asyncResultValue) {
623
+ throw new errors_js_1.TerminalError(asyncResultValue.Failure.message, {
624
+ errorCode: asyncResultValue.Failure.code,
625
+ });
466
626
  }
467
- return this.serde.deserialize(v);
468
- }));
627
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
628
+ });
469
629
  }
470
630
  peek() {
471
- const msg = new protocol_pb_js_1.PeekPromiseEntryMessage({
472
- key: this.name,
473
- });
474
- return this.ctx.stateMachine
475
- .handleUserCodeMessage(protocol_js_1.PEEK_PROMISE_MESSAGE_TYPE, msg)
476
- .transform((v) => {
477
- if (!v || v instanceof protocol_pb_js_1.Empty) {
631
+ return this.ctx.processCompletableEntry((vm) => vm.sys_peek_promise(this.name), (asyncResultValue) => {
632
+ if (asyncResultValue === "Empty") {
478
633
  return undefined;
479
634
  }
480
- return this.serde.deserialize(v);
635
+ else if (typeof asyncResultValue === "object" &&
636
+ "Success" in asyncResultValue) {
637
+ return this.serde.deserialize(asyncResultValue.Success);
638
+ }
639
+ else if (typeof asyncResultValue === "object" &&
640
+ "Failure" in asyncResultValue) {
641
+ throw new errors_js_1.TerminalError(asyncResultValue.Failure.message, {
642
+ errorCode: asyncResultValue.Failure.code,
643
+ });
644
+ }
645
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
481
646
  });
482
647
  }
483
648
  resolve(value) {
484
- const buffer = value != undefined ? this.serde.serialize(value) : new Uint8Array();
485
- const msg = new protocol_pb_js_1.CompletePromiseEntryMessage({
486
- key: this.name,
487
- completion: {
488
- case: "completionValue",
489
- value: buffer,
490
- },
649
+ return this.ctx.processCompletableEntry((vm) => vm.sys_complete_promise_success(this.name, this.serde.serialize(value)), (asyncResultValue) => {
650
+ if (asyncResultValue === "Empty") {
651
+ return undefined;
652
+ }
653
+ else if (typeof asyncResultValue === "object" &&
654
+ "Failure" in asyncResultValue) {
655
+ throw new errors_js_1.TerminalError(asyncResultValue.Failure.message, {
656
+ errorCode: asyncResultValue.Failure.code,
657
+ });
658
+ }
659
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
491
660
  });
492
- return this.ctx.stateMachine.handleUserCodeMessage(protocol_js_1.COMPLETE_PROMISE_MESSAGE_TYPE, msg);
493
661
  }
494
662
  reject(errorMsg) {
495
- const msg = new protocol_pb_js_1.CompletePromiseEntryMessage({
496
- key: this.name,
497
- completion: {
498
- case: "completionFailure",
499
- value: {
500
- message: errorMsg,
501
- },
502
- },
663
+ return this.ctx.processCompletableEntry((vm) => vm.sys_complete_promise_failure(this.name, {
664
+ code: errors_js_1.INTERNAL_ERROR_CODE,
665
+ message: errorMsg,
666
+ }), (asyncResultValue) => {
667
+ if (asyncResultValue === "Empty") {
668
+ return undefined;
669
+ }
670
+ else if (typeof asyncResultValue === "object" &&
671
+ "Failure" in asyncResultValue) {
672
+ throw new errors_js_1.TerminalError(asyncResultValue.Failure.message, {
673
+ errorCode: asyncResultValue.Failure.code,
674
+ });
675
+ }
676
+ throw new Error(`Unexpected variant in async result: ${JSON.stringify(asyncResultValue)}`);
503
677
  });
504
- return this.ctx.stateMachine.handleUserCodeMessage(protocol_js_1.COMPLETE_PROMISE_MESSAGE_TYPE, msg);
505
678
  }
506
679
  }
680
+ class LazyPromise {
681
+ executor;
682
+ _promise;
683
+ constructor(executor) {
684
+ this.executor = executor;
685
+ }
686
+ then(onfulfilled, onrejected) {
687
+ this._promise = this._promise || this.executor();
688
+ return this._promise.then(onfulfilled, onrejected);
689
+ }
690
+ catch(onrejected) {
691
+ this._promise = this._promise || this.executor();
692
+ return this._promise.catch(onrejected);
693
+ }
694
+ finally(onfinally) {
695
+ this._promise = this._promise || this.executor();
696
+ return this._promise.finally(onfinally);
697
+ }
698
+ [Symbol.toStringTag] = "LazyPromise";
699
+ }
700
+ class LazyContextPromise extends LazyPromise {
701
+ asyncResultHandle;
702
+ [RESTATE_CTX_SYMBOL];
703
+ constructor(asyncResultHandle, ctx, executor) {
704
+ super(executor);
705
+ this.asyncResultHandle = asyncResultHandle;
706
+ this[RESTATE_CTX_SYMBOL] = ctx;
707
+ }
708
+ orTimeout(millis) {
709
+ return ContextImpl.createCombinator("OrTimeout", [
710
+ this,
711
+ this[RESTATE_CTX_SYMBOL].sleep(millis),
712
+ ]);
713
+ }
714
+ }
715
+ // A promise that is never completed
716
+ function pendingPromise() {
717
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
718
+ return new Promise(() => { });
719
+ }
507
720
  //# sourceMappingURL=context_impl.js.map