@restatedev/restate-sdk 0.7.3-worker → 0.8.1

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 (231) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +29 -51
  3. package/dist/clients/workflow_client.d.ts +77 -0
  4. package/dist/clients/workflow_client.d.ts.map +1 -0
  5. package/dist/clients/workflow_client.js +172 -0
  6. package/dist/clients/workflow_client.js.map +1 -0
  7. package/dist/connection/buffered_connection.js +44 -0
  8. package/dist/connection/buffered_connection.js.map +1 -0
  9. package/dist/connection/connection.js +13 -0
  10. package/dist/connection/connection.js.map +1 -0
  11. package/dist/connection/embedded_connection.js +59 -0
  12. package/dist/connection/embedded_connection.js.map +1 -0
  13. package/dist/connection/http_connection.js +203 -0
  14. package/dist/connection/http_connection.js.map +1 -0
  15. package/dist/connection/lambda_connection.js +58 -0
  16. package/dist/connection/lambda_connection.js.map +1 -0
  17. package/dist/{restate_context.d.ts → context.d.ts} +239 -170
  18. package/dist/context.d.ts.map +1 -0
  19. package/dist/context.js +113 -0
  20. package/dist/context.js.map +1 -0
  21. package/dist/{restate_context_impl.d.ts → context_impl.d.ts} +26 -30
  22. package/dist/context_impl.d.ts.map +1 -0
  23. package/dist/context_impl.js +439 -0
  24. package/dist/context_impl.js.map +1 -0
  25. package/dist/embedded/api.d.ts +2 -2
  26. package/dist/embedded/api.d.ts.map +1 -1
  27. package/dist/embedded/api.js +35 -0
  28. package/dist/embedded/api.js.map +1 -0
  29. package/dist/embedded/handler.d.ts +2 -2
  30. package/dist/embedded/handler.d.ts.map +1 -1
  31. package/dist/embedded/handler.js +26 -0
  32. package/dist/embedded/handler.js.map +1 -0
  33. package/dist/embedded/http2_remote.js +91 -0
  34. package/dist/embedded/http2_remote.js.map +1 -0
  35. package/dist/embedded/invocation.d.ts.map +1 -1
  36. package/dist/embedded/invocation.js +94 -0
  37. package/dist/embedded/invocation.js.map +1 -0
  38. package/dist/endpoint/endpoint_impl.d.ts +35 -0
  39. package/dist/endpoint/endpoint_impl.d.ts.map +1 -0
  40. package/dist/endpoint/endpoint_impl.js +405 -0
  41. package/dist/endpoint/endpoint_impl.js.map +1 -0
  42. package/dist/endpoint/http2_handler.d.ts +11 -0
  43. package/dist/endpoint/http2_handler.d.ts.map +1 -0
  44. package/dist/endpoint/http2_handler.js +119 -0
  45. package/dist/endpoint/http2_handler.js.map +1 -0
  46. package/dist/endpoint/lambda_handler.d.ts +15 -0
  47. package/dist/endpoint/lambda_handler.d.ts.map +1 -0
  48. package/dist/endpoint/lambda_handler.js +144 -0
  49. package/dist/endpoint/lambda_handler.js.map +1 -0
  50. package/dist/endpoint.d.ts +161 -0
  51. package/dist/endpoint.d.ts.map +1 -0
  52. package/dist/endpoint.js +22 -0
  53. package/dist/endpoint.js.map +1 -0
  54. package/dist/generated/dev/restate/events.js +371 -0
  55. package/dist/generated/dev/restate/events.js.map +1 -0
  56. package/dist/generated/dev/restate/ext.js +215 -0
  57. package/dist/generated/dev/restate/ext.js.map +1 -0
  58. package/dist/generated/google/protobuf/descriptor.js +6676 -0
  59. package/dist/generated/google/protobuf/descriptor.js.map +1 -0
  60. package/dist/generated/google/protobuf/empty.js +107 -0
  61. package/dist/generated/google/protobuf/empty.js.map +1 -0
  62. package/dist/generated/google/protobuf/struct.js +754 -0
  63. package/dist/generated/google/protobuf/struct.js.map +1 -0
  64. package/dist/generated/proto/discovery.js +364 -0
  65. package/dist/generated/proto/discovery.js.map +1 -0
  66. package/dist/generated/proto/dynrpc.js +668 -0
  67. package/dist/generated/proto/dynrpc.js.map +1 -0
  68. package/dist/generated/proto/javascript.d.ts +13 -0
  69. package/dist/generated/proto/javascript.d.ts.map +1 -1
  70. package/dist/generated/proto/javascript.js +416 -0
  71. package/dist/generated/proto/javascript.js.map +1 -0
  72. package/dist/generated/proto/protocol.d.ts +43 -0
  73. package/dist/generated/proto/protocol.d.ts.map +1 -1
  74. package/dist/generated/proto/protocol.js +2641 -0
  75. package/dist/generated/proto/protocol.js.map +1 -0
  76. package/dist/generated/proto/services.js +1535 -0
  77. package/dist/generated/proto/services.js.map +1 -0
  78. package/dist/generated/proto/test.js +321 -0
  79. package/dist/generated/proto/test.js.map +1 -0
  80. package/dist/invocation.d.ts +4 -1
  81. package/dist/invocation.d.ts.map +1 -1
  82. package/dist/invocation.js +157 -0
  83. package/dist/invocation.js.map +1 -0
  84. package/dist/io/decoder.d.ts +1 -0
  85. package/dist/io/decoder.d.ts.map +1 -1
  86. package/dist/io/decoder.js +140 -0
  87. package/dist/io/decoder.js.map +1 -0
  88. package/dist/io/encoder.d.ts +1 -2
  89. package/dist/io/encoder.d.ts.map +1 -1
  90. package/dist/io/encoder.js +68 -0
  91. package/dist/io/encoder.js.map +1 -0
  92. package/dist/journal.d.ts +13 -4
  93. package/dist/journal.d.ts.map +1 -1
  94. package/dist/journal.js +405 -0
  95. package/dist/journal.js.map +1 -0
  96. package/dist/local_state_store.d.ts +5 -3
  97. package/dist/local_state_store.d.ts.map +1 -1
  98. package/dist/local_state_store.js +82 -0
  99. package/dist/local_state_store.js.map +1 -0
  100. package/dist/logger.d.ts +19 -0
  101. package/dist/logger.d.ts.map +1 -0
  102. package/dist/logger.js +90 -0
  103. package/dist/logger.js.map +1 -0
  104. package/dist/promise_combinator_tracker.d.ts +29 -0
  105. package/dist/promise_combinator_tracker.d.ts.map +1 -0
  106. package/dist/promise_combinator_tracker.js +128 -0
  107. package/dist/promise_combinator_tracker.js.map +1 -0
  108. package/dist/public_api.d.ts +5 -5
  109. package/dist/public_api.d.ts.map +1 -1
  110. package/dist/public_api.js +60 -0
  111. package/dist/public_api.js.map +1 -0
  112. package/dist/state_machine.d.ts +19 -12
  113. package/dist/state_machine.d.ts.map +1 -1
  114. package/dist/state_machine.js +437 -0
  115. package/dist/state_machine.js.map +1 -0
  116. package/dist/types/errors.d.ts +12 -3
  117. package/dist/types/errors.d.ts.map +1 -1
  118. package/dist/types/errors.js +273 -0
  119. package/dist/types/errors.js.map +1 -0
  120. package/dist/types/grpc.d.ts +6 -4
  121. package/dist/types/grpc.d.ts.map +1 -1
  122. package/dist/types/grpc.js +81 -0
  123. package/dist/types/grpc.js.map +1 -0
  124. package/dist/types/protocol.d.ts +9 -5
  125. package/dist/types/protocol.d.ts.map +1 -1
  126. package/dist/types/protocol.js +147 -0
  127. package/dist/types/protocol.js.map +1 -0
  128. package/dist/types/router.d.ts +8 -8
  129. package/dist/types/router.d.ts.map +1 -1
  130. package/dist/types/router.js +36 -0
  131. package/dist/types/router.js.map +1 -0
  132. package/dist/types/types.d.ts +1 -0
  133. package/dist/types/types.d.ts.map +1 -1
  134. package/dist/types/types.js +138 -0
  135. package/dist/types/types.js.map +1 -0
  136. package/dist/utils/{assumpsions.d.ts → assumptions.d.ts} +1 -1
  137. package/dist/utils/{assumpsions.d.ts.map → assumptions.d.ts.map} +1 -1
  138. package/dist/utils/assumptions.js +101 -0
  139. package/dist/utils/assumptions.js.map +1 -0
  140. package/dist/utils/message_logger.d.ts +28 -0
  141. package/dist/utils/message_logger.d.ts.map +1 -0
  142. package/dist/utils/message_logger.js +88 -0
  143. package/dist/utils/message_logger.js.map +1 -0
  144. package/dist/utils/promises.d.ts +15 -0
  145. package/dist/utils/promises.d.ts.map +1 -0
  146. package/dist/utils/promises.js +67 -0
  147. package/dist/utils/promises.js.map +1 -0
  148. package/dist/utils/public_utils.js +49 -0
  149. package/dist/utils/public_utils.js.map +1 -0
  150. package/dist/utils/rand.d.ts +1 -1
  151. package/dist/utils/rand.d.ts.map +1 -1
  152. package/dist/utils/rand.js +114 -0
  153. package/dist/utils/rand.js.map +1 -0
  154. package/dist/utils/utils.d.ts +1 -10
  155. package/dist/utils/utils.d.ts.map +1 -1
  156. package/dist/utils/utils.js +122 -0
  157. package/dist/utils/utils.js.map +1 -0
  158. package/dist/workflows/workflow.d.ts +101 -0
  159. package/dist/workflows/workflow.d.ts.map +1 -0
  160. package/dist/workflows/workflow.js +80 -0
  161. package/dist/workflows/workflow.js.map +1 -0
  162. package/dist/workflows/workflow_state_service.d.ts +35 -0
  163. package/dist/workflows/workflow_state_service.d.ts.map +1 -0
  164. package/dist/workflows/workflow_state_service.js +201 -0
  165. package/dist/workflows/workflow_state_service.js.map +1 -0
  166. package/dist/workflows/workflow_wrapper_service.d.ts +10 -0
  167. package/dist/workflows/workflow_wrapper_service.d.ts.map +1 -0
  168. package/dist/workflows/workflow_wrapper_service.js +264 -0
  169. package/dist/workflows/workflow_wrapper_service.js.map +1 -0
  170. package/package.json +38 -39
  171. package/src/clients/workflow_client.ts +290 -0
  172. package/src/connection/buffered_connection.ts +47 -0
  173. package/src/connection/connection.ts +34 -0
  174. package/src/connection/embedded_connection.ts +62 -0
  175. package/src/connection/http_connection.ts +228 -0
  176. package/src/connection/lambda_connection.ts +69 -0
  177. package/src/context.ts +633 -0
  178. package/src/context_impl.ts +721 -0
  179. package/src/embedded/api.ts +57 -0
  180. package/src/embedded/handler.ts +36 -0
  181. package/src/embedded/http2_remote.ts +103 -0
  182. package/src/embedded/invocation.ts +126 -0
  183. package/src/endpoint/endpoint_impl.ts +623 -0
  184. package/src/endpoint/http2_handler.ts +151 -0
  185. package/src/endpoint/lambda_handler.ts +181 -0
  186. package/src/endpoint.ts +187 -0
  187. package/src/generated/dev/restate/events.ts +430 -0
  188. package/src/generated/dev/restate/ext.ts +238 -0
  189. package/src/generated/google/protobuf/descriptor.ts +7889 -0
  190. package/src/generated/google/protobuf/empty.ts +150 -0
  191. package/src/generated/google/protobuf/struct.ts +878 -0
  192. package/src/generated/proto/discovery.ts +423 -0
  193. package/src/generated/proto/dynrpc.ts +768 -0
  194. package/src/generated/proto/javascript.ts +488 -0
  195. package/src/generated/proto/protocol.ts +3091 -0
  196. package/src/generated/proto/services.ts +1834 -0
  197. package/src/generated/proto/test.ts +387 -0
  198. package/src/invocation.ts +212 -0
  199. package/src/io/decoder.ts +171 -0
  200. package/src/io/encoder.ts +72 -0
  201. package/src/journal.ts +537 -0
  202. package/src/local_state_store.ts +94 -0
  203. package/src/logger.ts +121 -0
  204. package/src/promise_combinator_tracker.ts +191 -0
  205. package/src/public_api.ts +53 -0
  206. package/src/state_machine.ts +635 -0
  207. package/src/types/errors.ts +297 -0
  208. package/src/types/grpc.ts +97 -0
  209. package/src/types/protocol.ts +201 -0
  210. package/src/types/router.ts +118 -0
  211. package/src/types/types.ts +160 -0
  212. package/src/utils/assumptions.ts +131 -0
  213. package/src/utils/message_logger.ts +112 -0
  214. package/src/utils/promises.ts +118 -0
  215. package/src/utils/public_utils.ts +91 -0
  216. package/src/utils/rand.ts +142 -0
  217. package/src/utils/utils.ts +178 -0
  218. package/src/workflows/workflow.ts +178 -0
  219. package/src/workflows/workflow_state_service.ts +299 -0
  220. package/src/workflows/workflow_wrapper_service.ts +314 -0
  221. package/dist/cloudflare_bundle.js +0 -27387
  222. package/dist/restate_context.d.ts.map +0 -1
  223. package/dist/restate_context_impl.d.ts.map +0 -1
  224. package/dist/server/base_restate_server.d.ts +0 -32
  225. package/dist/server/base_restate_server.d.ts.map +0 -1
  226. package/dist/server/restate_lambda_handler.d.ts +0 -104
  227. package/dist/server/restate_lambda_handler.d.ts.map +0 -1
  228. package/dist/server/restate_server.d.ts +0 -97
  229. package/dist/server/restate_server.d.ts.map +0 -1
  230. package/dist/utils/logger.d.ts +0 -60
  231. package/dist/utils/logger.d.ts.map +0 -1
@@ -0,0 +1,437 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
4
+ *
5
+ * This file is part of the Restate SDK for Node.js/TypeScript,
6
+ * which is released under the MIT license.
7
+ *
8
+ * You can find a copy of the license in file LICENSE in the root
9
+ * directory of this repository or package, or at
10
+ * https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
11
+ */
12
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ var desc = Object.getOwnPropertyDescriptor(m, k);
15
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
16
+ desc = { enumerable: true, get: function() { return m[k]; } };
17
+ }
18
+ Object.defineProperty(o, k2, desc);
19
+ }) : (function(o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ o[k2] = m[k];
22
+ }));
23
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
24
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
25
+ }) : function(o, v) {
26
+ o["default"] = v;
27
+ });
28
+ var __importStar = (this && this.__importStar) || function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.StateMachine = void 0;
37
+ const p = __importStar(require("./types/protocol"));
38
+ const context_impl_1 = require("./context_impl");
39
+ const discovery_1 = require("./generated/proto/discovery");
40
+ const types_1 = require("./types/types");
41
+ const message_logger_1 = require("./utils/message_logger");
42
+ const timers_1 = require("timers");
43
+ const protocol_1 = require("./types/protocol");
44
+ const journal_1 = require("./journal");
45
+ const errors_1 = require("./types/errors");
46
+ const logger_1 = require("./logger");
47
+ const promises_1 = require("./utils/promises");
48
+ const promise_combinator_tracker_1 = require("./promise_combinator_tracker");
49
+ class StateMachine {
50
+ connection;
51
+ invocation;
52
+ protocolMode;
53
+ suspensionMillis;
54
+ journal;
55
+ restateContext;
56
+ invocationComplete = new promises_1.CompletablePromise();
57
+ // when this flag is true, no more work will (and may) happen
58
+ // this is set to true in case of
59
+ // - a completed invocation
60
+ // - a suspension
61
+ // - an error in the state machine
62
+ stateMachineClosed = false;
63
+ localStateStore;
64
+ // Whether the input channel (runtime -> service) is closed
65
+ // If it is closed, then we suspend immediately upon the next suspension point
66
+ // If it is open, then we suspend later because we might still get completions
67
+ inputChannelClosed = false;
68
+ // Suspension timeout that gets set and cleared based on completion messages;
69
+ suspensionTimeout;
70
+ promiseCombinatorTracker;
71
+ console;
72
+ constructor(connection, invocation, protocolMode, keyedContext, loggerContext, suspensionMillis = 30_000) {
73
+ this.connection = connection;
74
+ this.invocation = invocation;
75
+ this.protocolMode = protocolMode;
76
+ this.suspensionMillis = suspensionMillis;
77
+ this.localStateStore = invocation.localStateStore;
78
+ this.console = (0, message_logger_1.createStateMachineConsole)(loggerContext);
79
+ this.restateContext = new context_impl_1.ContextImpl(this.invocation.id, this.invocation.method.service,
80
+ // The console exposed by RestateContext filters logs in replay, while the internal one is based on the ENV variables.
81
+ (0, logger_1.createRestateConsole)(loggerContext, () => !this.journal.isReplaying()), keyedContext, this);
82
+ this.journal = new journal_1.Journal(this.invocation);
83
+ this.promiseCombinatorTracker = new promise_combinator_tracker_1.PromiseCombinatorTracker(this.readCombinatorOrderEntry.bind(this), this.writeCombinatorOrderEntry.bind(this));
84
+ }
85
+ handleMessage(m) {
86
+ if (this.stateMachineClosed) {
87
+ // ignore this message
88
+ return false;
89
+ }
90
+ if (m.messageType === protocol_1.COMPLETION_MESSAGE_TYPE) {
91
+ this.console.debugJournalMessage("Received completion message from Restate, adding to journal.", m.messageType, m.message);
92
+ this.journal.handleRuntimeCompletionMessage(m.message);
93
+ }
94
+ else if (m.messageType === protocol_1.ENTRY_ACK_MESSAGE_TYPE) {
95
+ this.console.debugJournalMessage("Received entry ack message from Restate, adding to journal.", m.messageType, m.message);
96
+ this.journal.handleEntryAckMessage(m.message);
97
+ }
98
+ else {
99
+ throw errors_1.RetryableError.protocolViolation(`Received message of type ${m.messageType}. Can only accept completion or acks messages after replay has finished.`);
100
+ }
101
+ // Remove lingering suspension timeouts, if we are not waiting for completions anymore
102
+ if (this.suspensionTimeout !== undefined &&
103
+ this.journal.getCompletableIndices().length === 0) {
104
+ (0, timers_1.clearTimeout)(this.suspensionTimeout);
105
+ this.suspensionTimeout = undefined;
106
+ }
107
+ return false; // we are never complete
108
+ }
109
+ handleUserCodeMessage(messageType, message, completedFlag, protocolVersion, requiresAckFlag) {
110
+ // if the state machine is already closed, return a promise that never
111
+ // completes, so that the user code does not resume
112
+ if (this.stateMachineClosed) {
113
+ return promises_1.WRAPPED_PROMISE_PENDING;
114
+ }
115
+ const promise = this.journal.handleUserSideMessage(messageType, message);
116
+ const journalIndex = this.journal.getUserCodeJournalIndex();
117
+ // Only send the message to restate if we are not in replaying mode
118
+ if (this.journal.isProcessing()) {
119
+ this.console.debugJournalMessage("Adding message to journal and sending to Restate", messageType, message);
120
+ this.send(new types_1.Message(messageType, message, completedFlag, protocolVersion, requiresAckFlag));
121
+ }
122
+ else {
123
+ this.console.debugJournalMessage("Matched and replayed message from journal", messageType, message);
124
+ }
125
+ return (0, promises_1.wrapDeeply)(promise, () => {
126
+ if (!p.SUSPENSION_TRIGGERS.includes(messageType)) {
127
+ return;
128
+ }
129
+ if (this.journal.isUnResolved(journalIndex)) {
130
+ this.hitSuspensionPoint();
131
+ }
132
+ });
133
+ }
134
+ // -- Methods related to combinators to wire up promise combinator API with PromiseCombinatorTracker
135
+ createCombinator(combinatorConstructor, promises) {
136
+ if (this.stateMachineClosed) {
137
+ return promises_1.WRAPPED_PROMISE_PENDING;
138
+ }
139
+ // We don't need the promise wrapping here to schedule a suspension,
140
+ // because the combined promises will already have that, so once we call then() on them,
141
+ // if we have to suspend we will suspend.
142
+ return this.promiseCombinatorTracker.createCombinator(combinatorConstructor, promises);
143
+ }
144
+ readCombinatorOrderEntry(combinatorId) {
145
+ const wannabeCombinatorEntry = this.journal.readNextReplayEntry();
146
+ if (wannabeCombinatorEntry === undefined) {
147
+ // We're in processing mode
148
+ return undefined;
149
+ }
150
+ if (wannabeCombinatorEntry.messageType !== protocol_1.COMBINATOR_ENTRY_MESSAGE) {
151
+ throw errors_1.RetryableError.journalMismatch(this.journal.getUserCodeJournalIndex(), wannabeCombinatorEntry, {
152
+ messageType: protocol_1.COMBINATOR_ENTRY_MESSAGE,
153
+ message: {
154
+ combinatorId,
155
+ },
156
+ });
157
+ }
158
+ const combinatorMessage = wannabeCombinatorEntry.message;
159
+ if (combinatorMessage.combinatorId != combinatorId) {
160
+ throw errors_1.RetryableError.journalMismatch(this.journal.getUserCodeJournalIndex(), wannabeCombinatorEntry, {
161
+ messageType: protocol_1.COMBINATOR_ENTRY_MESSAGE,
162
+ message: {
163
+ combinatorId,
164
+ },
165
+ });
166
+ }
167
+ this.console.debugJournalMessage("Matched and replayed message from journal", protocol_1.COMBINATOR_ENTRY_MESSAGE, combinatorMessage);
168
+ return combinatorMessage.journalEntriesOrder.map((id) => ({
169
+ id,
170
+ type: promise_combinator_tracker_1.PromiseType.JournalEntry,
171
+ }));
172
+ }
173
+ async writeCombinatorOrderEntry(combinatorId, order) {
174
+ if (this.journal.isProcessing()) {
175
+ const combinatorMessage = {
176
+ combinatorId,
177
+ journalEntriesOrder: order.map((pid) => pid.id),
178
+ };
179
+ this.console.debugJournalMessage("Adding message to journal and sending to Restate", protocol_1.COMBINATOR_ENTRY_MESSAGE, combinatorMessage);
180
+ const ackPromise = this.journal.appendJournalEntry(protocol_1.COMBINATOR_ENTRY_MESSAGE, combinatorMessage);
181
+ this.send(new types_1.Message(protocol_1.COMBINATOR_ENTRY_MESSAGE, combinatorMessage, undefined, undefined, true));
182
+ this.hitSuspensionPoint();
183
+ await ackPromise;
184
+ }
185
+ }
186
+ /**
187
+ * Invokes the RPC function and returns a promise that completes when the state machine
188
+ * stops processing the invocation, meaning when:
189
+ * - The function completes with a result or an exception
190
+ * - The execution suspends
191
+ * - An error is raised in the state machine (network, API violation, ...)
192
+ *
193
+ * The returned promise resolves successfully for all the cases above, because the are (from
194
+ * the perspective of the state machine) expected outcomes in which it send out corresponding
195
+ * result messages and cleanly closed the connection.
196
+ *
197
+ * The returned promise is rejected when an unhandled error arises and the caller would be
198
+ * expected to ensure that resources are properly cleaned up.
199
+ */
200
+ invoke() {
201
+ // --------------------------------------------------------------------------------------------
202
+ // Implementation note:
203
+ //
204
+ // This method is not async, because we don't want to actually await anything
205
+ // in there. We cannot await the completion of the actual invocation, because for long-running
206
+ // code (that suspends), the function invocation never completes. Instead, the state machine
207
+ // triggers a suspension.
208
+ // We need to do a bit of promise chaining for the rpc function promise, and return a different
209
+ // promise that completes in also in suspension and error cases.
210
+ // --------------------------------------------------------------------------------------------
211
+ // it is unexpected for the state machine to be closed here, so we raise an error in
212
+ // that case, unlike in other places, where we simply ignore things
213
+ if (this.stateMachineClosed) {
214
+ return Promise.reject(new Error("state machine is already closed"));
215
+ }
216
+ if (this.journal.nextEntryWillBeReplayed()) {
217
+ this.console.debugInvokeMessage("Resuming (replaying) function.");
218
+ }
219
+ else {
220
+ this.console.debugInvokeMessage("Invoking function.");
221
+ }
222
+ let resultBytes;
223
+ switch (this.invocation.invocationValue.kind) {
224
+ case "value":
225
+ resultBytes = this.invocation.method.invoke(this.restateContext, this.invocation.invocationValue.value);
226
+ break;
227
+ case "failure":
228
+ resultBytes = Promise.reject((0, errors_1.failureToTerminalError)(this.invocation.invocationValue.failure));
229
+ break;
230
+ }
231
+ resultBytes
232
+ .then((bytes) => {
233
+ // invocation successfully returned with a result value
234
+ try {
235
+ // the state machine might be closed here in some cases like when there was an error (like
236
+ // API violation) or a suspension, but the function code still completed
237
+ if (this.stateMachineClosed) {
238
+ this.console.warn("Unexpected successful completion of the function after the state machine closed. " +
239
+ "This may indicate that: \n" +
240
+ "- the function code does not properly await some Restate calls " +
241
+ "and did not notice an error \n" +
242
+ "- the function code was delayed for longer than the suspension timeout \n" +
243
+ "- the function code contained a try-catch block around a side effect which throws retryable errors. " +
244
+ "This try-catch block should be placed inside the side effect.");
245
+ return;
246
+ }
247
+ const value = Buffer.from(bytes);
248
+ // handle the result value
249
+ const msg = new types_1.Message(protocol_1.OUTPUT_STREAM_ENTRY_MESSAGE_TYPE, protocol_1.OutputStreamEntryMessage.create({
250
+ value,
251
+ }));
252
+ this.journal.handleUserSideMessage(msg.messageType, msg.message);
253
+ if (!this.journal.outputMsgWasReplayed()) {
254
+ this.send(msg);
255
+ this.console.debugJournalMessage("Journaled and sent output message", msg.messageType, msg.message);
256
+ }
257
+ else {
258
+ this.console.debugJournalMessage("Replayed and matched output message from journal", msg.messageType, msg.message);
259
+ }
260
+ this.console.debugInvokeMessage("Function completed successfully.");
261
+ // Mark the end of the invocation
262
+ this.send(new types_1.Message(protocol_1.END_MESSAGE_TYPE, protocol_1.EndMessage.create()));
263
+ this.finish(value);
264
+ }
265
+ catch (e) {
266
+ this.unhandledError((0, errors_1.ensureError)(e));
267
+ }
268
+ })
269
+ .catch((e) => {
270
+ // because of how we try/catch in the promise handler above, this here exclusively handles
271
+ // errors coming from the rpc function
272
+ try {
273
+ // Sometimes the function code fails as a consequence of the state machine encountering
274
+ // an error before (possibly Restate closed the connection).
275
+ if (this.stateMachineClosed) {
276
+ return;
277
+ }
278
+ const error = (0, errors_1.ensureError)(e);
279
+ this.console.trace("Function completed with an error: " + error.message, e);
280
+ this.sendErrorAndFinish(error);
281
+ }
282
+ catch (ee) {
283
+ this.unhandledError((0, errors_1.ensureError)(ee));
284
+ }
285
+ });
286
+ // this promise here completes under any completion, including the cases where the
287
+ // rpc function does not end (error, suspension, ...)
288
+ return this.invocationComplete.promise;
289
+ }
290
+ async sendErrorAndFinish(e) {
291
+ if (e instanceof errors_1.TerminalError) {
292
+ this.sendTerminalError(e);
293
+ }
294
+ else {
295
+ this.sendRetryableError(e);
296
+ }
297
+ await this.finish();
298
+ }
299
+ sendRetryableError(e) {
300
+ const msg = new types_1.Message(protocol_1.ERROR_MESSAGE_TYPE, (0, errors_1.errorToErrorMessage)(e));
301
+ this.console.debugJournalMessage("Invocation ended with retryable error.", msg.messageType, msg.message);
302
+ this.send(msg);
303
+ }
304
+ sendTerminalError(e) {
305
+ const msg = new types_1.Message(protocol_1.OUTPUT_STREAM_ENTRY_MESSAGE_TYPE, protocol_1.OutputStreamEntryMessage.create({
306
+ failure: e.toFailure(),
307
+ }));
308
+ this.console.debugJournalMessage("Invocation ended with failure message.", msg.messageType, msg.message);
309
+ this.journal.handleUserSideMessage(msg.messageType, msg.message);
310
+ if (!this.journal.outputMsgWasReplayed()) {
311
+ this.send(msg);
312
+ }
313
+ // Mark the end of the invocation
314
+ this.send(new types_1.Message(protocol_1.END_MESSAGE_TYPE, protocol_1.EndMessage.create()));
315
+ }
316
+ send(message) {
317
+ this.connection.send(message).catch((err) => {
318
+ this.handleStreamError(err);
319
+ });
320
+ }
321
+ /**
322
+ * Closes the state machine, flushes all output, and resolves the invocation promise.
323
+ */
324
+ async finish(value) {
325
+ try {
326
+ this.stateMachineClosed = true;
327
+ this.journal.close();
328
+ this.clearSuspensionTimeout();
329
+ await this.connection.end();
330
+ this.invocationComplete.resolve(value);
331
+ }
332
+ catch (e) {
333
+ this.invocationComplete.reject((0, errors_1.ensureError)(e));
334
+ }
335
+ }
336
+ /**
337
+ * This function propagates errors up to the completion promise, to be handled
338
+ * on the connection layer.
339
+ */
340
+ unhandledError(e) {
341
+ this.invocationComplete.reject(e);
342
+ this.stateMachineClosed = true;
343
+ this.journal.close();
344
+ this.clearSuspensionTimeout();
345
+ }
346
+ /**
347
+ * This method is invoked when we hit a suspension point. A suspension point is everytime the user "await"s a Promise returned by RestateContext that might be completed at a later point in time by a CompletionMessage/AckMessage.
348
+ *
349
+ * Depending on the state of the read channel, and on the protocol mode, it might either immediately suspend, or schedule a suspension to happen at a later point in time.
350
+ */
351
+ hitSuspensionPoint() {
352
+ // If there was already a timeout set, we want to reset the time to postpone suspension as long as we make progress.
353
+ // So we first clear the old timeout, and then we set a new one.
354
+ this.clearSuspensionTimeout();
355
+ const delay = this.getSuspensionMillis();
356
+ this.console.debugJournalMessage("Scheduling suspension in " + delay + " ms");
357
+ if (delay >= 0) {
358
+ // Set a new suspension with a new timeout
359
+ // The suspension will only be sent if the timeout is not canceled due to a completion.
360
+ // In case the delay is 0 we still schedule a timeout in order to process the suspension on the next process tick,
361
+ // without interrupting the current work.
362
+ this.suspensionTimeout = setTimeout(() => {
363
+ this.suspend();
364
+ }, delay);
365
+ }
366
+ }
367
+ // Suspension timeouts:
368
+ // Lambda case: suspend immediately when control is back in the user code
369
+ // Bidi streaming case:
370
+ // - suspend after 1 seconds if input channel is still open (can still get completions)
371
+ // - suspend immediately if input channel is closed (cannot get completions)
372
+ getSuspensionMillis() {
373
+ return this.protocolMode === discovery_1.ProtocolMode.REQUEST_RESPONSE ||
374
+ this.inputChannelClosed
375
+ ? 0
376
+ : this.suspensionMillis;
377
+ }
378
+ async suspend() {
379
+ const indices = this.journal.getCompletableIndices();
380
+ // If the state is closed then we either already send a suspension
381
+ // or something else bad happened...
382
+ if (this.journal.isClosed() || indices.length === 0) {
383
+ return;
384
+ }
385
+ // There need to be journal entries to complete, otherwise this timeout should have been removed.
386
+ // A suspension message is the end of the invocation.
387
+ // Resolve the root call with the suspension message
388
+ // This will lead to a onCallSuccess call where this msg will be sent.
389
+ const msg = new types_1.Message(protocol_1.SUSPENSION_MESSAGE_TYPE, protocol_1.SuspensionMessage.create({
390
+ entryIndexes: indices,
391
+ }));
392
+ this.console.debugJournalMessage("Writing suspension message to journal.", msg.messageType, msg.message);
393
+ this.journal.handleUserSideMessage(msg.messageType, msg.message);
394
+ if (!this.journal.outputMsgWasReplayed()) {
395
+ this.send(msg);
396
+ }
397
+ this.console.debugInvokeMessage("Suspending function.");
398
+ await this.finish();
399
+ }
400
+ /**
401
+ * WARNING: make sure you use this at the right point in the code
402
+ * After the index has been incremented...
403
+ * This is error-prone... Would be good to have a better solution for this.
404
+ */
405
+ getUserCodeJournalIndex() {
406
+ return this.journal.getUserCodeJournalIndex();
407
+ }
408
+ handleInputClosed() {
409
+ if (this.journal.isClosed() ||
410
+ this.stateMachineClosed ||
411
+ this.inputChannelClosed) {
412
+ return;
413
+ }
414
+ this.inputChannelClosed = true;
415
+ this.console.debug("Restate closed connection to trigger suspension.");
416
+ // If there is a timeout planned, reset the timout to execute immediately when the work is done.
417
+ if (this.suspensionTimeout !== undefined) {
418
+ this.hitSuspensionPoint();
419
+ }
420
+ }
421
+ handleStreamError(e) {
422
+ this.console.info("Aborting function execution and closing state machine due to connection error: " +
423
+ e.message);
424
+ this.unhandledError(e);
425
+ }
426
+ nextEntryWillBeReplayed() {
427
+ return this.journal.nextEntryWillBeReplayed();
428
+ }
429
+ clearSuspensionTimeout() {
430
+ if (this.suspensionTimeout !== undefined) {
431
+ (0, timers_1.clearTimeout)(this.suspensionTimeout);
432
+ this.suspensionTimeout = undefined;
433
+ }
434
+ }
435
+ }
436
+ exports.StateMachine = StateMachine;
437
+ //# sourceMappingURL=state_machine.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state_machine.js","sourceRoot":"","sources":["../src/state_machine.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,oDAAsC;AACtC,iDAA6C;AAE7C,2DAA2D;AAC3D,yCAAwC;AACxC,2DAGgC;AAChC,mCAAsC;AACtC,+CAW0B;AAC1B,uCAAoC;AAEpC,2CAMwB;AAExB,qCAA+D;AAC/D,+CAK0B;AAC1B,6EAIsC;AAGtC,MAAa,YAAY;IA4BJ;IACA;IACA;IAGA;IAhCX,OAAO,CAAgB;IACvB,cAAc,CAAc;IAEnB,kBAAkB,GAAG,IAAI,6BAAkB,EAAiB,CAAC;IAE9E,6DAA6D;IAC7D,iCAAiC;IACjC,4BAA4B;IAC5B,kBAAkB;IAClB,mCAAmC;IAC3B,kBAAkB,GAAG,KAAK,CAAC;IAEnB,eAAe,CAAkB;IAEjD,2DAA2D;IAC3D,8EAA8E;IAC9E,8EAA8E;IACtE,kBAAkB,GAAG,KAAK,CAAC;IAEnC,6EAA6E;IACrE,iBAAiB,CAAkB;IAEnC,wBAAwB,CAA2B;IAE3D,OAAO,CAAsB;IAE7B,YACmB,UAAsB,EACtB,UAA4B,EAC5B,YAA0B,EAC3C,YAAqB,EACrB,aAA4B,EACX,mBAA2B,MAAM;QALjC,eAAU,GAAV,UAAU,CA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oBAAoB,EAAE,EAAE;YACxC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;SAChB;QAED,IAAI,CAAC,OAAO,CAAC,kBAAkB,CAAC,sBAAsB,CAAC,CAAC;QAExD,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;IACtB,CAAC;IAED;;;;OAIG;IACI,uBAAuB;QAC5B,OAAO,IAAI,CAAC,OAAO,CAAC,uBAAuB,EAAE,CAAC;IAChD,CAAC;IAEM,iBAAiB;QACtB,IACE,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE;YACvB,IAAI,CAAC,kBAAkB;YACvB,IAAI,CAAC,kBAAkB,EACvB;YACA,OAAO;SACR;QAED,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC;QAE/B,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,kDAAkD,CAAC,CAAC;QAEvE,gGAAgG;QAChG,IAAI,IAAI,CAAC,iBAAiB,KAAK,SAAS,EAAE;YACxC,IAAI,CAAC,kBAAkB,EAAE,CAAC;SAC3B;IACH,CAAC;IAEM,iBAAiB,CAAC,CAAQ;QAC/B,IAAI,CAAC,OAAO,CAAC,IAAI,CACf,iFAAiF;YAC/E,CAAC,CAAC,OAAO,CACZ,CAAC;QAEF,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IACzB,CAAC;IAEM,uBAAuB;QAC5B,OAAO,IAAI,CAAC,OAAO,CAAC,uBAAuB,EAAE,CAAC;IAChD,CAAC;IAEO,sBAAsB;QAC5B,IAAI,IAAI,CAAC,iBAAiB,KAAK,SAAS,EAAE;YACxC,IAAA,qBAAY,EAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;YACrC,IAAI,CAAC,iBAAiB,GAAG,SAAS,CAAC;SACpC;IACH,CAAC;CACF;AAjkBD,oCAikBC"}
@@ -1,7 +1,6 @@
1
1
  import { ErrorMessage, Failure } from "../generated/proto/protocol";
2
- import { Message } from "./types";
3
- import { JournalEntry } from "../journal";
4
2
  import { FailureWithTerminal } from "../generated/proto/javascript";
3
+ import * as p from "./protocol";
5
4
  export declare enum ErrorCodes {
6
5
  /**
7
6
  * Not an error; returned on success.
@@ -159,15 +158,25 @@ export declare class RestateError extends Error {
159
158
  export declare class TerminalError extends RestateError {
160
159
  constructor(message: string, options?: {
161
160
  errorCode?: number;
161
+ cause?: any;
162
162
  });
163
163
  }
164
+ export declare class TimeoutError extends TerminalError {
165
+ constructor();
166
+ }
164
167
  export declare class RetryableError extends RestateError {
165
168
  constructor(message: string, options?: {
166
169
  errorCode?: number;
167
170
  cause?: any;
168
171
  });
169
172
  static internal(message: string): RetryableError;
170
- static journalMismatch(journalIndex: number, replayMessage: Message, journalEntry: JournalEntry): RetryableError;
173
+ static journalMismatch(journalIndex: number, actualEntry: {
174
+ messageType: bigint;
175
+ message: p.ProtocolMessage | Uint8Array;
176
+ }, expectedEntry: {
177
+ messageType: bigint;
178
+ message: p.ProtocolMessage | Uint8Array;
179
+ }): RetryableError;
171
180
  static protocolViolation(message: string): RetryableError;
172
181
  static apiViolation(message: string): RetryableError;
173
182
  }
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/types/errors.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AAEpE,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AAEpE,oBAAY,UAAU;IACpB;;;OAGG;IACH,EAAE,IAAI;IACN;;;OAGG;IACH,SAAS,IAAI;IACb;;;;;;;OAOG;IACH,OAAO,IAAI;IACX;;;;;;OAMG;IACH,gBAAgB,IAAI;IACpB;;;;;;;OAOG;IACH,iBAAiB,IAAI;IACrB;;;;;;;;OAQG;IACH,SAAS,IAAI;IACb;;;;OAIG;IACH,cAAc,IAAI;I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