@restatedev/restate-sdk 0.0.0-SNAPSHOT-20231025205407

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 (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +157 -0
  3. package/dist/connection/connection.d.ts +19 -0
  4. package/dist/connection/connection.d.ts.map +1 -0
  5. package/dist/connection/connection.js +13 -0
  6. package/dist/connection/connection.js.map +1 -0
  7. package/dist/connection/embedded_connection.d.ts +22 -0
  8. package/dist/connection/embedded_connection.d.ts.map +1 -0
  9. package/dist/connection/embedded_connection.js +78 -0
  10. package/dist/connection/embedded_connection.js.map +1 -0
  11. package/dist/connection/http_connection.d.ts +73 -0
  12. package/dist/connection/http_connection.d.ts.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.d.ts +14 -0
  16. package/dist/connection/lambda_connection.d.ts.map +1 -0
  17. package/dist/connection/lambda_connection.js +56 -0
  18. package/dist/connection/lambda_connection.js.map +1 -0
  19. package/dist/embedded/api.d.ts +18 -0
  20. package/dist/embedded/api.d.ts.map +1 -0
  21. package/dist/embedded/api.js +37 -0
  22. package/dist/embedded/api.js.map +1 -0
  23. package/dist/embedded/handler.d.ts +4 -0
  24. package/dist/embedded/handler.d.ts.map +1 -0
  25. package/dist/embedded/handler.js +28 -0
  26. package/dist/embedded/handler.js.map +1 -0
  27. package/dist/embedded/http2_remote.d.ts +10 -0
  28. package/dist/embedded/http2_remote.d.ts.map +1 -0
  29. package/dist/embedded/http2_remote.js +85 -0
  30. package/dist/embedded/http2_remote.js.map +1 -0
  31. package/dist/embedded/invocation.d.ts +4 -0
  32. package/dist/embedded/invocation.d.ts.map +1 -0
  33. package/dist/embedded/invocation.js +90 -0
  34. package/dist/embedded/invocation.js.map +1 -0
  35. package/dist/generated/dev/restate/events.d.ts +96 -0
  36. package/dist/generated/dev/restate/events.d.ts.map +1 -0
  37. package/dist/generated/dev/restate/events.js +371 -0
  38. package/dist/generated/dev/restate/events.js.map +1 -0
  39. package/dist/generated/dev/restate/ext.d.ts +88 -0
  40. package/dist/generated/dev/restate/ext.d.ts.map +1 -0
  41. package/dist/generated/dev/restate/ext.js +212 -0
  42. package/dist/generated/dev/restate/ext.js.map +1 -0
  43. package/dist/generated/google/protobuf/descriptor.d.ts +1204 -0
  44. package/dist/generated/google/protobuf/descriptor.d.ts.map +1 -0
  45. package/dist/generated/google/protobuf/descriptor.js +6676 -0
  46. package/dist/generated/google/protobuf/descriptor.js.map +1 -0
  47. package/dist/generated/google/protobuf/empty.d.ts +86 -0
  48. package/dist/generated/google/protobuf/empty.d.ts.map +1 -0
  49. package/dist/generated/google/protobuf/empty.js +107 -0
  50. package/dist/generated/google/protobuf/empty.js.map +1 -0
  51. package/dist/generated/google/protobuf/struct.d.ts +175 -0
  52. package/dist/generated/google/protobuf/struct.d.ts.map +1 -0
  53. package/dist/generated/google/protobuf/struct.js +754 -0
  54. package/dist/generated/google/protobuf/struct.js.map +1 -0
  55. package/dist/generated/proto/discovery.d.ts +105 -0
  56. package/dist/generated/proto/discovery.d.ts.map +1 -0
  57. package/dist/generated/proto/discovery.js +364 -0
  58. package/dist/generated/proto/discovery.js.map +1 -0
  59. package/dist/generated/proto/dynrpc.d.ts +150 -0
  60. package/dist/generated/proto/dynrpc.d.ts.map +1 -0
  61. package/dist/generated/proto/dynrpc.js +668 -0
  62. package/dist/generated/proto/dynrpc.js.map +1 -0
  63. package/dist/generated/proto/javascript.d.ts +97 -0
  64. package/dist/generated/proto/javascript.d.ts.map +1 -0
  65. package/dist/generated/proto/javascript.js +297 -0
  66. package/dist/generated/proto/javascript.js.map +1 -0
  67. package/dist/generated/proto/protocol.d.ts +371 -0
  68. package/dist/generated/proto/protocol.d.ts.map +1 -0
  69. package/dist/generated/proto/protocol.js +2179 -0
  70. package/dist/generated/proto/protocol.js.map +1 -0
  71. package/dist/generated/proto/services.d.ts +307 -0
  72. package/dist/generated/proto/services.d.ts.map +1 -0
  73. package/dist/generated/proto/services.js +1535 -0
  74. package/dist/generated/proto/services.js.map +1 -0
  75. package/dist/generated/proto/test.d.ts +116 -0
  76. package/dist/generated/proto/test.d.ts.map +1 -0
  77. package/dist/generated/proto/test.js +321 -0
  78. package/dist/generated/proto/test.js.map +1 -0
  79. package/dist/invocation.d.ts +39 -0
  80. package/dist/invocation.d.ts.map +1 -0
  81. package/dist/invocation.js +138 -0
  82. package/dist/invocation.js.map +1 -0
  83. package/dist/io/decoder.d.ts +8 -0
  84. package/dist/io/decoder.d.ts.map +1 -0
  85. package/dist/io/decoder.js +133 -0
  86. package/dist/io/decoder.js.map +1 -0
  87. package/dist/io/encoder.d.ts +7 -0
  88. package/dist/io/encoder.d.ts.map +1 -0
  89. package/dist/io/encoder.js +57 -0
  90. package/dist/io/encoder.js.map +1 -0
  91. package/dist/journal.d.ts +42 -0
  92. package/dist/journal.d.ts.map +1 -0
  93. package/dist/journal.js +368 -0
  94. package/dist/journal.js.map +1 -0
  95. package/dist/local_state_store.d.ts +13 -0
  96. package/dist/local_state_store.d.ts.map +1 -0
  97. package/dist/local_state_store.js +72 -0
  98. package/dist/local_state_store.js.map +1 -0
  99. package/dist/public_api.d.ts +10 -0
  100. package/dist/public_api.d.ts.map +1 -0
  101. package/dist/public_api.js +59 -0
  102. package/dist/public_api.js.map +1 -0
  103. package/dist/restate_context.d.ts +405 -0
  104. package/dist/restate_context.d.ts.map +1 -0
  105. package/dist/restate_context.js +46 -0
  106. package/dist/restate_context.js.map +1 -0
  107. package/dist/restate_context_impl.d.ts +54 -0
  108. package/dist/restate_context_impl.d.ts.map +1 -0
  109. package/dist/restate_context_impl.js +364 -0
  110. package/dist/restate_context_impl.js.map +1 -0
  111. package/dist/server/base_restate_server.d.ts +30 -0
  112. package/dist/server/base_restate_server.d.ts.map +1 -0
  113. package/dist/server/base_restate_server.js +360 -0
  114. package/dist/server/base_restate_server.js.map +1 -0
  115. package/dist/server/restate_lambda_handler.d.ts +104 -0
  116. package/dist/server/restate_lambda_handler.d.ts.map +1 -0
  117. package/dist/server/restate_lambda_handler.js +245 -0
  118. package/dist/server/restate_lambda_handler.js.map +1 -0
  119. package/dist/server/restate_server.d.ts +91 -0
  120. package/dist/server/restate_server.d.ts.map +1 -0
  121. package/dist/server/restate_server.js +230 -0
  122. package/dist/server/restate_server.js.map +1 -0
  123. package/dist/state_machine.d.ts +75 -0
  124. package/dist/state_machine.d.ts.map +1 -0
  125. package/dist/state_machine.js +380 -0
  126. package/dist/state_machine.js.map +1 -0
  127. package/dist/types/errors.d.ts +40 -0
  128. package/dist/types/errors.d.ts.map +1 -0
  129. package/dist/types/errors.js +121 -0
  130. package/dist/types/errors.js.map +1 -0
  131. package/dist/types/grpc.d.ts +88 -0
  132. package/dist/types/grpc.d.ts.map +1 -0
  133. package/dist/types/grpc.js +68 -0
  134. package/dist/types/grpc.js.map +1 -0
  135. package/dist/types/protocol.d.ts +24 -0
  136. package/dist/types/protocol.d.ts.map +1 -0
  137. package/dist/types/protocol.js +115 -0
  138. package/dist/types/protocol.js.map +1 -0
  139. package/dist/types/router.d.ts +33 -0
  140. package/dist/types/router.d.ts.map +1 -0
  141. package/dist/types/router.js +36 -0
  142. package/dist/types/router.js.map +1 -0
  143. package/dist/types/types.d.ts +30 -0
  144. package/dist/types/types.d.ts.map +1 -0
  145. package/dist/types/types.js +125 -0
  146. package/dist/types/types.js.map +1 -0
  147. package/dist/utils/assumpsions.d.ts +8 -0
  148. package/dist/utils/assumpsions.d.ts.map +1 -0
  149. package/dist/utils/assumpsions.js +101 -0
  150. package/dist/utils/assumpsions.js.map +1 -0
  151. package/dist/utils/logger.d.ts +60 -0
  152. package/dist/utils/logger.d.ts.map +1 -0
  153. package/dist/utils/logger.js +102 -0
  154. package/dist/utils/logger.js.map +1 -0
  155. package/dist/utils/public_utils.d.ts +58 -0
  156. package/dist/utils/public_utils.d.ts.map +1 -0
  157. package/dist/utils/public_utils.js +49 -0
  158. package/dist/utils/public_utils.js.map +1 -0
  159. package/dist/utils/utils.d.ts +14 -0
  160. package/dist/utils/utils.d.ts.map +1 -0
  161. package/dist/utils/utils.js +119 -0
  162. package/dist/utils/utils.js.map +1 -0
  163. package/package.json +75 -0
@@ -0,0 +1,405 @@
1
+ /// <reference types="node" />
2
+ import "./utils/logger";
3
+ import { RetrySettings } from "./utils/public_utils";
4
+ import { Client, SendClient } from "./types/router";
5
+ /**
6
+ * Base Restate context, which contains all operations that are the same in the gRPC-based API
7
+ * as in the dynamic rpc-handler-based API.
8
+ *
9
+ * Those operations include state access/updates, side-effects, awakeables, or sleeps.
10
+ */
11
+ export interface RestateBaseContext {
12
+ /**
13
+ * The unique id that identifies the current function invocation. This id is guaranteed to be
14
+ * unique across invocations, but constant across reties and suspensions.
15
+ */
16
+ id: Buffer;
17
+ /**
18
+ * Name of the service.
19
+ */
20
+ serviceName: string;
21
+ /**
22
+ * Get/retrieve state from the Restate runtime.
23
+ * Note that state objects are serialized with `Buffer.from(JSON.stringify(theObject))`
24
+ * and deserialized with `JSON.parse(value.toString()) as T`.
25
+ *
26
+ * @param name key of the state to retrieve
27
+ * @returns a Promise that is resolved with the value of the state key
28
+ *
29
+ * @example
30
+ * const ctx = restate.useContext(this);
31
+ * const state = await ctx.get<string>("STATE");
32
+ */
33
+ get<T>(name: string): Promise<T | null>;
34
+ /**
35
+ * Set/store state in the Restate runtime.
36
+ * Note that state objects are serialized with `Buffer.from(JSON.stringify(theObject))`
37
+ * and deserialized with `JSON.parse(value.toString()) as T`.
38
+ *
39
+ * @param name key of the state to set
40
+ * @param value value to set
41
+ *
42
+ * @example
43
+ * const ctx = restate.useContext(this);
44
+ * const state = ctx.set("STATE", "Hello");
45
+ */
46
+ set<T>(name: string, value: T): void;
47
+ /**
48
+ * Clear/delete state in the Restate runtime.
49
+ * @param name key of the state to delete
50
+ *
51
+ * @example
52
+ * const ctx = restate.useContext(this);
53
+ * const state = ctx.clear("STATE");
54
+ */
55
+ clear(name: string): void;
56
+ /**
57
+ * Execute a side effect and store the result in Restate. The side effect will thus not
58
+ * be re-executed during a later replay, but take the durable result from Restate.
59
+ *
60
+ * Side effects let you capture potentially non-deterministic computation and interaction
61
+ * with external systems in a safe way.
62
+ *
63
+ * Failure semantics of side effects are:
64
+ * - If a side effect executed and persisted before, the result (value or Error) will be
65
+ * taken from the Restate journal.
66
+ * - There is a small window where a side effect may be re-executed twice, if a failure
67
+ * occurred between execution and persisting the result.
68
+ * - No second side effect will be executed while a previous side effect's result is not
69
+ * yet durable. That way, side effects that build on top of each other can assume
70
+ * deterministic results from previous effects, and at most one side effect will be
71
+ * re-executed on replay (the latest, if the failure happened in the small windows
72
+ * described above).
73
+ *
74
+ * This function takes an optional retry policy, that determines what happens if the
75
+ * side effect throws an error. The default retry policy retries infinitely, with exponential
76
+ * backoff and uses suspending sleep for the wait times between retries.
77
+ *
78
+ * @example
79
+ * const ctx = restate.useContext(this);
80
+ * const result = await ctx.sideEffect(async () => someExternalAction() )
81
+ *
82
+ * @example
83
+ * const paymentAction = async () => {
84
+ * const result = await paymentClient.call(txId, methodIdentifier, amount);
85
+ * if (result.error) {
86
+ * throw result.error;
87
+ * } else {
88
+ * return result.payment_accepted;
89
+ * }
90
+ * }
91
+ * const paymentAccepted: boolean =
92
+ * await ctx.sideEffect(paymentAction, { maxRetries: 10});
93
+ *
94
+ * @param fn The function to run as a side effect.
95
+ * @param retryPolicy The optional policy describing how retries happen.
96
+ */
97
+ sideEffect<T>(fn: () => Promise<T>, retryPolicy?: RetrySettings): Promise<T>;
98
+ /**
99
+ * Register an awakeable and pause the processing until the awakeable ID (and optional payload) have been returned to the service
100
+ * (via ctx.completeAwakeable(...)). The SDK deserializes the payload with `JSON.parse(result.toString()) as T`.
101
+ * @returns
102
+ * - id: the string ID that has to be used to complete the awakaeble by some external service
103
+ * - promise: the Promise that needs to be awaited and that is resolved with the payload that was supplied by the service which completed the awakeable
104
+ *
105
+ * @example
106
+ * const ctx = restate.useContext(this);
107
+ * const awakeable = ctx.awakeable<string>();
108
+ *
109
+ * // send the awakeable ID to some external service that will wake this one back up
110
+ * // The ID can be retrieved by:
111
+ * const id = awakeable.id;
112
+ *
113
+ * // ... send to external service ...
114
+ *
115
+ * // Wait for the external service to wake this service back up
116
+ * const result = await awakeable.promise;
117
+ */
118
+ awakeable<T>(): {
119
+ id: string;
120
+ promise: Promise<T>;
121
+ };
122
+ /**
123
+ * Resolve an awakeable of another service.
124
+ * @param id the string ID of the awakeable.
125
+ * This is supplied by the service that needs to be woken up.
126
+ * @param payload the payload to pass to the service that is woken up.
127
+ * The SDK serializes the payload with `Buffer.from(JSON.stringify(payload))`
128
+ * and deserializes it in the receiving service with `JSON.parse(result.toString()) as T`.
129
+ *
130
+ * @example
131
+ * const ctx = restate.useContext(this);
132
+ * // The sleeping service should have sent the awakeableIdentifier string to this service.
133
+ * ctx.resolveAwakeable(awakeableIdentifier, "hello");
134
+ */
135
+ resolveAwakeable<T>(id: string, payload: T): void;
136
+ /**
137
+ * Reject an awakeable of another service. When rejecting, the service waiting on this awakeable will be woken up with a terminal error with the provided reason.
138
+ * @param id the string ID of the awakeable.
139
+ * This is supplied by the service that needs to be woken up.
140
+ * @param reason the reason of the rejection.
141
+ *
142
+ * @example
143
+ * const ctx = restate.useContext(this);
144
+ * // The sleeping service should have sent the awakeableIdentifier string to this service.
145
+ * ctx.rejectAwakeable(awakeableIdentifier, "super bad error");
146
+ */
147
+ rejectAwakeable(id: string, reason: string): void;
148
+ /**
149
+ * Sleep until a timeout has passed.
150
+ * @param millis duration of the sleep in millis.
151
+ * This is a lower-bound.
152
+ *
153
+ * @example
154
+ * const ctx = restate.useContext(this);
155
+ * await ctx.sleep(1000);
156
+ */
157
+ sleep(millis: number): Promise<void>;
158
+ }
159
+ /**
160
+ * The context that gives access to all Restate-backed operations, for example
161
+ * - sending reliable messages / rpc through Restate
162
+ * - access/update state (for keyed services)
163
+ * - side effects
164
+ * - sleeps and delayed calls
165
+ * - awakeables
166
+ * - ...
167
+ *
168
+ * This context is for use in **gRPC service** implementations.
169
+ * For the rpc-handler API, use the {@link RpcContext} instead.
170
+ */
171
+ export interface RestateGrpcContext extends RestateBaseContext {
172
+ /**
173
+ * Unidirectional call to other Restate services ( = in background / async / not waiting on response).
174
+ * To do this, wrap the call via the proto-ts client with oneWayCall, as shown in the example.
175
+ *
176
+ * NOTE: this returns a Promise because we override the gRPC clients provided by proto-ts.
177
+ * So we are required to return a Promise.
178
+ *
179
+ * @param call Invoke another service by using the generated proto-ts client.
180
+ * @example
181
+ * const ctx = restate.useContext(this);
182
+ * const client = new GreeterClientImpl(ctx);
183
+ * await ctx.oneWayCall(() =>
184
+ * client.greet(Request.create({ name: "Peter" }))
185
+ * )
186
+ */
187
+ oneWayCall(call: () => Promise<any>): Promise<void>;
188
+ /**
189
+ * Delayed unidirectional call to other Restate services ( = in background / async / not waiting on response).
190
+ * To do this, wrap the call via the proto-ts client with delayedCall, as shown in the example.
191
+ * Add the delay in millis as the second parameter.
192
+ *
193
+ * NOTE: this returns a Promise because we override the gRPC clients provided by proto-ts.
194
+ * So we are required to return a Promise.
195
+ *
196
+ * @param call Invoke another service by using the generated proto-ts client.
197
+ * @param delayMillis millisecond delay duration to delay the execution of the call
198
+ * @example
199
+ * const ctx = restate.useContext(this);
200
+ * const client = new GreeterClientImpl(ctx);
201
+ * await ctx.delayedCall(() =>
202
+ * client.greet(Request.create({ name: "Peter" })),
203
+ * 5000
204
+ * )
205
+ */
206
+ delayedCall(call: () => Promise<any>, delayMillis?: number): Promise<void>;
207
+ /**
208
+ * Call another Restate service and await the response.
209
+ *
210
+ * This function is not recommended to be called directly. Instead, use the generated gRPC client
211
+ * that was generated based on the Protobuf service definitions (which internally use this method):
212
+ *
213
+ * @example
214
+ * ```
215
+ * const ctx = restate.useContext(this);
216
+ * const client = new GreeterClientImpl(ctx);
217
+ * client.greet(Request.create({ name: "Peter" }))
218
+ * ```
219
+ *
220
+ * @param service name of the service to call
221
+ * @param method name of the method to call
222
+ * @param data payload as Uint8Array
223
+ * @returns a Promise that is resolved with the response of the called service
224
+ */
225
+ request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
226
+ }
227
+ /**
228
+ * For compatibility, we make the support 'RestateContext' as the type for the Grpc-based API context.
229
+ */
230
+ export type RestateContext = RestateGrpcContext;
231
+ /**
232
+ * Returns the RestateContext which is the entrypoint for all interaction with Restate.
233
+ * Use this from within a method to retrieve the RestateContext.
234
+ * The context is bounded to a single invocation.
235
+ *
236
+ * @example
237
+ * const ctx = restate.useContext(this);
238
+ *
239
+ */
240
+ export declare function useContext<T>(instance: T): RestateGrpcContext;
241
+ export declare function setContext<T>(instance: T, context: RestateGrpcContext): T;
242
+ /**
243
+ * ServiceApi captures the type and parameters to make RPC calls and send messages to
244
+ * a set of RPC handlers in a router.
245
+ *
246
+ * @example
247
+ * **Service Side:**
248
+ * ```ts
249
+ * const router = restate.router({
250
+ * someAction: async(ctx: restate.RpcContext, req: string) => { ... },
251
+ * anotherAction: async(ctx: restate.RpcContext, count: number) => { ... }
252
+ * });
253
+ *
254
+ * export const myApi: restate.ServiceApi<typeof router> = { path : "myservice" };
255
+ *
256
+ * restate.createServer().bindRouter("myservice", router).listen(9080);
257
+ * ```
258
+ * **Client side:**
259
+ * ```ts
260
+ * ctx.rpc(myApi).someAction("hello!");
261
+ * ```
262
+ */
263
+ export type ServiceApi<_M = unknown> = {
264
+ path: string;
265
+ };
266
+ /**
267
+ * The context that gives access to all Restate-backed operations, for example
268
+ * - sending reliable messages / RPC through Restate
269
+ * - access/update state (for keyed services)
270
+ * - side effects
271
+ * - sleeps and delayed calls
272
+ * - awakeables
273
+ * - ...
274
+ *
275
+ * This context is for use with the **rpc-handler API**.
276
+ * For gRPC-based API, use the {@link RestateContext} instead.
277
+ */
278
+ export interface RpcContext extends RestateBaseContext {
279
+ /**
280
+ * Makes a type-safe request/response RPC to the specified target service.
281
+ *
282
+ * The RPC goes through Restate and is guaranteed to be reliably delivered. The RPC is also
283
+ * journaled for durable execution and will thus not be duplicated when the handler is re-invoked
284
+ * for retries or after suspending.
285
+ *
286
+ * This call will return the result produced by the target handler, or the Error, if the target
287
+ * handler finishes with a Terminal Error.
288
+ *
289
+ * This call is a suspension point: The handler might suspend while awaiting the response and
290
+ * resume once the response is available.
291
+ *
292
+ * @example
293
+ * *Service Side:*
294
+ * ```ts
295
+ * const router = restate.router({
296
+ * someAction: async(ctx: restate.RpcContext, req: string) => { ... },
297
+ * anotherAction: async(ctx: restate.RpcContext, count: number) => { ... }
298
+ * });
299
+ *
300
+ * // option 1: export only the type signature of the router
301
+ * export type myApiType = typeof router;
302
+ *
303
+ * // option 2: export the API definition with type and name (path)
304
+ * export const myApi: restate.ServiceApi<typeof router> = { path : "myservice" };
305
+ *
306
+ * restate.createServer().bindRouter("myservice", router).listen(9080);
307
+ * ```
308
+ * **Client side:**
309
+ * ```ts
310
+ * // option 1: use only types and supply service name separately
311
+ * const result1 = await ctx.rpc<myApiType>({path: "myservice"}).someAction("hello!");
312
+ *
313
+ * // option 2: use full API spec
314
+ * const result2 = await ctx.rpc(myApi).anotherAction(1337);
315
+ * ```
316
+ */
317
+ rpc<M>(opts: ServiceApi<M>): Client<M>;
318
+ /**
319
+ * Makes a type-safe one-way RPC to the specified target service. This method effectively behaves
320
+ * like enqueuing the message in a message queue.
321
+ *
322
+ * The message goes through Restate and is guaranteed to be reliably delivered. The RPC is also
323
+ * journaled for durable execution and will thus not be duplicated when the handler is re-invoked
324
+ * for retries or after suspending.
325
+ *
326
+ * This call will return immediately; the message sending happens asynchronously in the background.
327
+ * Despite that, the message is guaranteed to be sent, because the completion of the invocation that
328
+ * triggers the send (calls this function) happens logically after the sending. That means that any
329
+ * failure where the message does not reach Restate also cannot complete this invocation, and will
330
+ * hence recover this handler and (through the durable execution) recover the message to be sent.
331
+ *
332
+ * @example
333
+ * *Service Side:*
334
+ * ```ts
335
+ * const router = restate.router({
336
+ * someAction: async(ctx: restate.RpcContext, req: string) => { ... },
337
+ * anotherAction: async(ctx: restate.RpcContext, count: number) => { ... }
338
+ * });
339
+ *
340
+ * // option 1: export only the type signature of the router
341
+ * export type myApiType = typeof router;
342
+ *
343
+ * // option 2: export the API definition with type and name (path)
344
+ * export const myApi: restate.ServiceApi<typeof router> = { path : "myservice" };
345
+ *
346
+ * restate.createServer().bindRouter("myservice", router).listen(9080);
347
+ * ```
348
+ * **Client side:**
349
+ * ```ts
350
+ * // option 1: use only types and supply service name separately
351
+ * ctx.send<myApiType>({path: "myservice"}).someAction("hello!");
352
+ *
353
+ * // option 2: use full API spec
354
+ * ctx.send(myApi).anotherAction(1337);
355
+ * ```
356
+ */
357
+ send<M>(opts: ServiceApi<M>): SendClient<M>;
358
+ /**
359
+ * Makes a type-safe one-way RPC to the specified target service, after a delay specified by the
360
+ * milliseconds' argument.
361
+ * This method is like stetting up a fault-tolerant cron job that enqueues the message in a
362
+ * message queue.
363
+ * The handler calling this function does not have to stay active for the delay time.
364
+ *
365
+ * Both the delay timer and the message are durably stored in Restate and guaranteed to be reliably
366
+ * delivered. The delivery happens no earlier than specified through the delay, but may happen
367
+ * later, if the target service is down, or backpressuring the system.
368
+ *
369
+ * The delay message is journaled for durable execution and will thus not be duplicated when the
370
+ * handler is re-invoked for retries or after suspending.
371
+ *
372
+ * This call will return immediately; the message sending happens asynchronously in the background.
373
+ * Despite that, the message is guaranteed to be sent, because the completion of the invocation that
374
+ * triggers the send (calls this function) happens logically after the sending. That means that any
375
+ * failure where the message does not reach Restate also cannot complete this invocation, and will
376
+ * hence recover this handler and (through the durable execution) recover the message to be sent.
377
+ *
378
+ * @example
379
+ * *Service Side:*
380
+ * ```ts
381
+ * const router = restate.router({
382
+ * someAction: async(ctx: restate.RpcContext, req: string) => { ... },
383
+ * anotherAction: async(ctx: restate.RpcContext, count: number) => { ... }
384
+ * });
385
+ *
386
+ * // option 1: export only the type signature of the router
387
+ * export type myApiType = typeof router;
388
+ *
389
+ * // option 2: export the API definition with type and name (path)
390
+ * export const myApi: restate.ServiceApi<typeof router> = { path : "myservice" };
391
+ *
392
+ * restate.createServer().bindRouter("myservice", router).listen(9080);
393
+ * ```
394
+ * **Client side:**
395
+ * ```ts
396
+ * // option 1: use only types and supply service name separately
397
+ * ctx.sendDelayed<myApiType>({path: "myservice"}, 60_000).someAction("hello!");
398
+ *
399
+ * // option 2: use full API spec
400
+ * ctx.sendDelayed(myApi, 60_000).anotherAction(1337);
401
+ * ```
402
+ */
403
+ sendDelayed<M>(opts: ServiceApi<M>, delay: number): SendClient<M>;
404
+ }
405
+ //# sourceMappingURL=restate_context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"restate_context.d.ts","sourceRoot":"","sources":["../src/restate_context.ts"],"names":[],"mappings":";AAYA,OAAO,gBAAgB,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEpD;;;;;GAKG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,EAAE,EAAE,MAAM,CAAC;IAEX;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;;;;;;;;;;OAWG;IACH,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;IAExC;;;;;;;;;;;OAWG;IACH,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI,CAAC;IAErC;;;;;;;OAOG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAwCG;IACH,UAAU,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAE7E;;;;;;;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;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,UAAU,CAAC,EAAE,GAAG,OAAO,IAAI;IACrC,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,UAAW,SAAQ,kBAAkB;IACpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqCG;IACH,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAEvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAsCG;IACH,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IAE5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA4CG;IACH,WAAW,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;CACnE"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2023 - 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
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.setContext = exports.useContext = void 0;
14
+ // Use our prefixed logger instead of default console logging
15
+ require("./utils/logger");
16
+ /**
17
+ * Returns the RestateContext which is the entrypoint for all interaction with Restate.
18
+ * Use this from within a method to retrieve the RestateContext.
19
+ * The context is bounded to a single invocation.
20
+ *
21
+ * @example
22
+ * const ctx = restate.useContext(this);
23
+ *
24
+ */
25
+ function useContext(instance) {
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ const wrapper = instance;
28
+ if (wrapper.$$restate === undefined || wrapper.$$restate === null) {
29
+ throw new Error(`not running within a Restate call.`);
30
+ }
31
+ return wrapper.$$restate;
32
+ }
33
+ exports.useContext = useContext;
34
+ function setContext(instance, context) {
35
+ // creates a *new*, per call object that shares all the properties that @instance has
36
+ // except '$$restate' which is a unique, per call pointer to a restate context.
37
+ //
38
+ // The following line create a new object, that its prototype is @instance.
39
+ // and that object has a $$restate property.
40
+ const wrapper = Object.create(instance, {
41
+ $$restate: { value: context },
42
+ });
43
+ return wrapper;
44
+ }
45
+ exports.setContext = setContext;
46
+ //# sourceMappingURL=restate_context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"restate_context.js","sourceRoot":"","sources":["../src/restate_context.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAEH,6DAA6D;AAC7D,0BAAwB;AA2PxB;;;;;;;;GAQG;AACH,SAAgB,UAAU,CAAI,QAAW;IACvC,8DAA8D;IAC9D,MAAM,OAAO,GAAG,QAAe,CAAC;IAChC,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,IAAI,OAAO,CAAC,SAAS,KAAK,IAAI,EAAE;QACjE,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;KACvD;IACD,OAAO,OAAO,CAAC,SAAS,CAAC;AAC3B,CAAC;AAPD,gCAOC;AAED,SAAgB,UAAU,CAAI,QAAW,EAAE,OAA2B;IACpE,qFAAqF;IACrF,+EAA+E;IAC/E,EAAE;IACF,2EAA2E;IAC3E,4CAA4C;IAC5C,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,QAAkB,EAAE;QAChD,SAAS,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE;KAC9B,CAAC,CAAC;IACH,OAAO,OAAY,CAAC;AACtB,CAAC;AAVD,gCAUC"}
@@ -0,0 +1,54 @@
1
+ /// <reference types="node" />
2
+ import { RestateGrpcContext, RpcContext, ServiceApi } from "./restate_context";
3
+ import { StateMachine } from "./state_machine";
4
+ import { RetrySettings } from "./utils/public_utils";
5
+ import { Client, SendClient } from "./types/router";
6
+ export declare class RestateGrpcContextImpl implements RestateGrpcContext {
7
+ readonly id: Buffer;
8
+ readonly serviceName: string;
9
+ private readonly stateMachine;
10
+ private static callContext;
11
+ constructor(id: Buffer, serviceName: string, stateMachine: StateMachine<any, any>);
12
+ get<T>(name: string): Promise<T | null>;
13
+ set<T>(name: string, value: T): void;
14
+ clear(name: string): void;
15
+ request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
16
+ private invoke;
17
+ private invokeOneWay;
18
+ oneWayCall(call: () => Promise<any>): Promise<void>;
19
+ delayedCall(call: () => Promise<any>, delayMillis?: number): Promise<void>;
20
+ sideEffect<T>(fn: () => Promise<T>, retryPolicy?: RetrySettings): Promise<T>;
21
+ sleep(millis: number): Promise<void>;
22
+ awakeable<T>(): {
23
+ id: string;
24
+ promise: Promise<T>;
25
+ };
26
+ resolveAwakeable<T>(id: string, payload: T): void;
27
+ rejectAwakeable(id: string, reason: string): void;
28
+ private completeAwakeable;
29
+ private isInSideEffect;
30
+ private isInOneWayCall;
31
+ private getOneWayCallDelay;
32
+ private checkState;
33
+ }
34
+ export declare class RpcContextImpl implements RpcContext {
35
+ private readonly ctx;
36
+ readonly id: Buffer;
37
+ readonly serviceName: string;
38
+ constructor(ctx: RestateGrpcContext, id?: Buffer, serviceName?: string);
39
+ rpc<M>({ path }: ServiceApi): Client<M>;
40
+ send<M>(options: ServiceApi): SendClient<M>;
41
+ sendDelayed<M>({ path }: ServiceApi, delayMillis: number): SendClient<M>;
42
+ get<T>(name: string): Promise<T | null>;
43
+ set<T>(name: string, value: T): void;
44
+ clear(name: string): void;
45
+ sideEffect<T>(fn: () => Promise<T>): Promise<T>;
46
+ awakeable<T>(): {
47
+ id: string;
48
+ promise: Promise<T>;
49
+ };
50
+ resolveAwakeable<T>(id: string, payload: T): void;
51
+ rejectAwakeable(id: string, reason: string): void;
52
+ sleep(millis: number): Promise<void>;
53
+ }
54
+ //# sourceMappingURL=restate_context_impl.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"restate_context_impl.d.ts","sourceRoot":"","sources":["../src/restate_context_impl.ts"],"names":[],"mappings":";AAWA,OAAO,EAAE,kBAAkB,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAgC/C,OAAO,EAIL,aAAa,EACd,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAepD,qBAAa,sBAAuB,YAAW,kBAAkB;aAU7C,EAAE,EAAE,MAAM;aACV,WAAW,EAAE,MAAM;IAEnC,OAAO,CAAC,QAAQ,CAAC,YAAY;IAN/B,OAAO,CAAC,MAAM,CAAC,WAAW,CAAwC;gBAGhD,EAAE,EAAE,MAAM,EACV,WAAW,EAAE,MAAM,EAElB,YAAY,EAAE,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC;IAG1C,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IA0B7C,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,IAAI;IAMpC,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAUzB,OAAO,CACZ,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,UAAU,GACf,OAAO,CAAC,UAAU,CAAC;YAQR,MAAM;YAmBN,YAAY;IAqBb,UAAU,CAErB,IAAI,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,GACvB,OAAO,CAAC,IAAI,CAAC;IASH,WAAW,CAEtB,IAAI,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,EACxB,WAAW,CAAC,E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