@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,151 @@
1
+ /*
2
+ * Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3
+ *
4
+ * This file is part of the Restate SDK for Node.js/TypeScript,
5
+ * which is released under the MIT license.
6
+ *
7
+ * You can find a copy of the license in file LICENSE in the root
8
+ * directory of this repository or package, or at
9
+ * https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10
+ */
11
+
12
+ import stream from "stream";
13
+ import { pipeline, finished } from "stream/promises";
14
+ import http2, { Http2ServerRequest, Http2ServerResponse } from "http2";
15
+ import { parse as urlparse, Url } from "url";
16
+ import {
17
+ ProtocolMode,
18
+ ServiceDiscoveryResponse,
19
+ } from "../generated/proto/discovery";
20
+ import { EndpointImpl } from "./endpoint_impl";
21
+ import { RestateHttp2Connection } from "../connection/http_connection";
22
+ import { HostedGrpcServiceMethod } from "../types/grpc";
23
+ import { ensureError } from "../types/errors";
24
+ import { InvocationBuilder } from "../invocation";
25
+ import { StateMachine } from "../state_machine";
26
+ import { rlog } from "../logger";
27
+
28
+ export class Http2Handler {
29
+ private readonly discoveryResponse: ServiceDiscoveryResponse;
30
+ constructor(private readonly endpoint: EndpointImpl) {
31
+ this.discoveryResponse = ServiceDiscoveryResponse.fromPartial({
32
+ ...this.endpoint.discovery,
33
+ protocolMode: ProtocolMode.BIDI_STREAM,
34
+ });
35
+ }
36
+
37
+ acceptConnection(
38
+ request: Http2ServerRequest,
39
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
40
+ _response: Http2ServerResponse
41
+ ) {
42
+ const stream = request.stream;
43
+ const url: Url = urlparse(request.url ?? "/");
44
+
45
+ this.handleConnection(url, stream).catch((e) => {
46
+ const error = ensureError(e);
47
+ rlog.error(
48
+ "Error while handling connection: " + (error.stack ?? error.message)
49
+ );
50
+ stream.end();
51
+ stream.destroy();
52
+ });
53
+ }
54
+
55
+ private async handleConnection(
56
+ url: Url,
57
+ stream: http2.ServerHttp2Stream
58
+ ): Promise<void> {
59
+ const method = this.endpoint.methodByUrl(url.path);
60
+
61
+ if (method !== undefined) {
62
+ // valid connection, let's dispatch the invocation
63
+ stream.respond({
64
+ "content-type": "application/restate",
65
+ ":status": 200,
66
+ });
67
+
68
+ const restateStream = RestateHttp2Connection.from(stream);
69
+ await handleInvocation(method, restateStream);
70
+ return;
71
+ }
72
+
73
+ // no method under that name. might be a discovery request
74
+ if (url.path == "/discover") {
75
+ rlog.info(
76
+ "Answering discovery request. Announcing services: " +
77
+ JSON.stringify(this.discoveryResponse.services)
78
+ );
79
+ await respondDiscovery(this.discoveryResponse, stream);
80
+ return;
81
+ }
82
+
83
+ // no discovery, so unknown method: 404
84
+ rlog.error(`No service and function found for URL ${url.path}`);
85
+ await respondNotFound(stream);
86
+ }
87
+ }
88
+
89
+ async function respondDiscovery(
90
+ response: ServiceDiscoveryResponse,
91
+ http2Stream: http2.ServerHttp2Stream
92
+ ) {
93
+ const responseData = ServiceDiscoveryResponse.encode(response).finish();
94
+
95
+ http2Stream.respond({
96
+ ":status": 200,
97
+ "content-type": "application/proto",
98
+ });
99
+
100
+ await pipeline(stream.Readable.from(responseData), http2Stream, {
101
+ end: true,
102
+ });
103
+ }
104
+
105
+ async function respondNotFound(stream: http2.ServerHttp2Stream) {
106
+ stream.respond({
107
+ "content-type": "application/restate",
108
+ ":status": 404,
109
+ });
110
+ stream.end();
111
+ await finished(stream);
112
+ }
113
+
114
+ async function handleInvocation<I, O>(
115
+ func: HostedGrpcServiceMethod<I, O>,
116
+ connection: RestateHttp2Connection
117
+ ) {
118
+ // step 1: collect all journal events
119
+ const journalBuilder = new InvocationBuilder<I, O>(func);
120
+ connection.pipeToConsumer(journalBuilder);
121
+ try {
122
+ await journalBuilder.completion();
123
+ } finally {
124
+ // ensure GC friendliness, also in case of errors
125
+ connection.removeCurrentConsumer();
126
+ }
127
+
128
+ // step 2: create the state machine
129
+ const invocation = journalBuilder.build();
130
+ const stateMachine = new StateMachine<I, O>(
131
+ connection,
132
+ invocation,
133
+ ProtocolMode.BIDI_STREAM,
134
+ func.method.keyedContext,
135
+ invocation.inferLoggerContext()
136
+ );
137
+ connection.pipeToConsumer(stateMachine);
138
+
139
+ // step 3: invoke the function
140
+
141
+ // This call would propagate errors in the state machine logic, but not errors
142
+ // in the application function code. Ending a function with an error as well
143
+ // as failign an invocation and being retried are perfectly valid actions from the
144
+ // SDK's perspective.
145
+ try {
146
+ await stateMachine.invoke();
147
+ } finally {
148
+ // ensure GC friendliness, also in case of errors
149
+ connection.removeCurrentConsumer();
150
+ }
151
+ }
@@ -0,0 +1,181 @@
1
+ /*
2
+ * Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3
+ *
4
+ * This file is part of the Restate SDK for Node.js/TypeScript,
5
+ * which is released under the MIT license.
6
+ *
7
+ * You can find a copy of the license in file LICENSE in the root
8
+ * directory of this repository or package, or at
9
+ * https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10
+ */
11
+
12
+ import { rlog } from "../logger";
13
+ import {
14
+ APIGatewayProxyEvent,
15
+ APIGatewayProxyEventV2,
16
+ APIGatewayProxyResult,
17
+ APIGatewayProxyResultV2,
18
+ Context,
19
+ } from "aws-lambda";
20
+ import {
21
+ ProtocolMode,
22
+ ServiceDiscoveryResponse,
23
+ } from "../generated/proto/discovery";
24
+ import { EndpointImpl } from "./endpoint_impl";
25
+ import { LambdaConnection } from "../connection/lambda_connection";
26
+ import { InvocationBuilder } from "../invocation";
27
+ import { decodeLambdaBody } from "../io/decoder";
28
+ import { Message } from "../types/types";
29
+ import { StateMachine } from "../state_machine";
30
+ import { ensureError } from "../types/errors";
31
+ import { OUTPUT_STREAM_ENTRY_MESSAGE_TYPE } from "../types/protocol";
32
+
33
+ export class LambdaHandler {
34
+ private readonly discoveryResponse: ServiceDiscoveryResponse;
35
+ constructor(private readonly endpoint: EndpointImpl) {
36
+ this.discoveryResponse = ServiceDiscoveryResponse.fromPartial({
37
+ ...this.endpoint.discovery,
38
+ protocolMode: ProtocolMode.REQUEST_RESPONSE,
39
+ });
40
+ }
41
+
42
+ // --------------------------------------------------------------------------
43
+
44
+ /**
45
+ * This is the main request handling method, effectively a typed variant of `create()`.
46
+ */
47
+ async handleRequest(
48
+ event: APIGatewayProxyEvent | APIGatewayProxyEventV2,
49
+ context: Context
50
+ ): Promise<APIGatewayProxyResult | APIGatewayProxyResultV2> {
51
+ let path;
52
+ if ("path" in event) {
53
+ // V1
54
+ path = event.path;
55
+ } else {
56
+ // V2
57
+ path = event.rawPath;
58
+ }
59
+ const pathSegments = path.split("/");
60
+
61
+ // API Gateway can add a prefix to the path based on the name of the Lambda function and deployment stage
62
+ // (e.g. /default)
63
+ // So we only check the ending of the path on correctness.
64
+ // Logic:
65
+ // 1. Check whether there are at least three segments in the path and whether the third-last one is "invoke".
66
+ // If that is the case, treat it as an invocation.
67
+ // 2. See if the last one is "discover", answer with discovery.
68
+ // 3. Else report "invalid path".
69
+ if (
70
+ pathSegments.length >= 3 &&
71
+ pathSegments[pathSegments.length - 3] === "invoke"
72
+ ) {
73
+ const url = "/" + pathSegments.slice(-3).join("/");
74
+ return await this.handleInvoke(url, event, context);
75
+ } else if (pathSegments[pathSegments.length - 1] === "discover") {
76
+ return this.handleDiscovery();
77
+ } else {
78
+ const msg = `Invalid path: path doesn't end in /invoke/SvcName/MethodName and also not in /discover: ${path}`;
79
+ rlog.trace(msg);
80
+ return this.toErrorResponse(500, msg);
81
+ }
82
+ }
83
+
84
+ private async handleInvoke(
85
+ url: string,
86
+ event: APIGatewayProxyEvent | APIGatewayProxyEventV2,
87
+ context: Context
88
+ ): Promise<APIGatewayProxyResult | APIGatewayProxyResultV2> {
89
+ try {
90
+ const method = this.endpoint.methodByUrl(url);
91
+ if (event.body == null) {
92
+ throw new Error("The incoming message body was null");
93
+ }
94
+
95
+ if (method === undefined) {
96
+ if (url.includes("?")) {
97
+ throw new Error(
98
+ `Invalid path: path URL seems to include query parameters: ${url}`
99
+ );
100
+ } else {
101
+ const msg = `No service found for URL: ${url}`;
102
+ rlog.error(msg);
103
+ return this.toErrorResponse(404, msg);
104
+ }
105
+ }
106
+
107
+ // build the previous journal from the events
108
+ let decodedEntries: Message[] | null = decodeLambdaBody(event.body);
109
+ const journalBuilder = new InvocationBuilder(method);
110
+ decodedEntries.forEach((e: Message) => journalBuilder.handleMessage(e));
111
+ const alreadyCompleted =
112
+ decodedEntries.find(
113
+ (e: Message) => e.messageType === OUTPUT_STREAM_ENTRY_MESSAGE_TYPE
114
+ ) !== undefined;
115
+ decodedEntries = null;
116
+
117
+ // set up and invoke the state machine
118
+ const connection = new LambdaConnection(alreadyCompleted);
119
+ const invocation = journalBuilder.build();
120
+ const stateMachine = new StateMachine(
121
+ connection,
122
+ invocation,
123
+ ProtocolMode.REQUEST_RESPONSE,
124
+ method.method.keyedContext,
125
+ invocation.inferLoggerContext({
126
+ AWSRequestId: context.awsRequestId,
127
+ })
128
+ );
129
+ await stateMachine.invoke();
130
+ const result = await connection.getResult();
131
+
132
+ return {
133
+ headers: {
134
+ "content-type": "application/restate",
135
+ },
136
+ statusCode: 200,
137
+ isBase64Encoded: true,
138
+ body: encodeResponse(result),
139
+ };
140
+ } catch (e) {
141
+ const error = ensureError(e);
142
+ rlog.error(error.message);
143
+ rlog.error(error.stack);
144
+ return this.toErrorResponse(500, error.message);
145
+ }
146
+ }
147
+
148
+ private handleDiscovery(): APIGatewayProxyResult | APIGatewayProxyResultV2 {
149
+ // return discovery information
150
+ rlog.info(
151
+ "Answering discovery request. Announcing services: " +
152
+ JSON.stringify(this.discoveryResponse.services)
153
+ );
154
+ return {
155
+ headers: {
156
+ "content-type": "application/proto",
157
+ },
158
+ statusCode: 200,
159
+ isBase64Encoded: true,
160
+ body: encodeResponse(
161
+ ServiceDiscoveryResponse.encode(this.discoveryResponse).finish()
162
+ ),
163
+ };
164
+ }
165
+
166
+ private toErrorResponse(code: number, message: string) {
167
+ return {
168
+ headers: {
169
+ "content-type": "application/restate",
170
+ },
171
+ statusCode: code,
172
+ isBase64Encoded: true,
173
+ body: encodeResponse(Buffer.from(JSON.stringify({ message }))),
174
+ };
175
+ }
176
+ }
177
+
178
+ function encodeResponse(data: Uint8Array): string {
179
+ const buffer = data instanceof Buffer ? data : Buffer.from(data);
180
+ return buffer.toString("base64");
181
+ }
@@ -0,0 +1,187 @@
1
+ /*
2
+ * Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
3
+ *
4
+ * This file is part of the Restate SDK for Node.js/TypeScript,
5
+ * which is released under the MIT license.
6
+ *
7
+ * You can find a copy of the license in file LICENSE in the root
8
+ * directory of this repository or package, or at
9
+ * https://github.com/restatedev/sdk-typescript/blob/main/LICENSE
10
+ */
11
+
12
+ import { KeyedRouter, UnKeyedRouter } from "./types/router";
13
+ import { ProtoMetadata } from "./types/grpc";
14
+ import { Http2ServerRequest, Http2ServerResponse } from "http2";
15
+ import { EndpointImpl } from "./endpoint/endpoint_impl";
16
+
17
+ /**
18
+ * Create a new {@link RestateEndpoint}.
19
+ */
20
+ export function endpoint(): RestateEndpoint {
21
+ return new EndpointImpl();
22
+ }
23
+
24
+ /**
25
+ * The properties describing a gRPC service. Consisting of the name, the object holding the
26
+ * implementation, and the descriptor (metadata) describing the service and types.
27
+ */
28
+ export interface ServiceOpts {
29
+ descriptor: ProtoMetadata;
30
+ service: string;
31
+ instance: unknown;
32
+ }
33
+
34
+ /**
35
+ * Utility interface for a bundle of one or more services belonging together
36
+ * and being registered together.
37
+ */
38
+ export interface ServiceBundle {
39
+ /**
40
+ * Called to register the services at the endpoint.
41
+ */
42
+ registerServices(endpoint: RestateEndpoint): void;
43
+ }
44
+
45
+ /**
46
+ * RestateEndpoint encapsulates all the Restate services served by this endpoint.
47
+ *
48
+ * A RestateEndpoint can either be served either as HTTP2 server, using the methods {@link listen} or {@link http2Handler},
49
+ * or as Lambda, using the method {@link lambdaHandler}.
50
+ *
51
+ * @example
52
+ * A typical endpoint served as HTTP server would look like this:
53
+ * ```
54
+ * import * as restate from "@restatedev/restate-sdk";
55
+ *
56
+ * restate
57
+ * .endpoint()
58
+ * .bindService({
59
+ * service: "MyService",
60
+ * instance: new myService.MyServiceImpl(),
61
+ * descriptor: myService.protoMetadata,
62
+ * })
63
+ * .listen(8000);
64
+ * ```
65
+ * @example
66
+ * A typical endpoint served as AWS Lambda would look like this:
67
+ * ```
68
+ * import * as restate from "@restatedev/restate-sdk";
69
+ *
70
+ * export const handler = restate
71
+ * .endpoint()
72
+ * .bindService({
73
+ * service: "MyService",
74
+ * instance: new myService.MyServiceImpl(),
75
+ * descriptor: myService.protoMetadata,
76
+ * })
77
+ * .lambdaHandler();
78
+ * ```
79
+ */
80
+ export interface RestateEndpoint {
81
+ /**
82
+ * Adds a gRPC service to be served from this endpoint.
83
+ *
84
+ * The {@link ServiceOpts} passed here need to describe the following properties:
85
+ *
86
+ * - The 'service' name: the name of the gRPC service (as in the service definition proto file).
87
+ * - The service 'instance': the implementation of the service logic (must implement the generated
88
+ * gRPC service interface).
89
+ * - The gRPC/protobuf 'descriptor': The protoMetadata descriptor that describes the service, methods,
90
+ * and parameter types. It is usually found as the value 'protoMetadata' in the generated
91
+ * file '(service-name).ts'
92
+ *
93
+ * The descriptor is generated by the protobuf compiler and needed by Restate to reflectively discover
94
+ * the service details, understand payload serialization, perform HTTP/JSON-to-gRPC transcoding, or
95
+ * to proxy the service.
96
+ *
97
+ * If you define multiple services in the same '.proto' file, you may have only one descriptor that
98
+ * describes all services together. You can pass the same descriptor to multiple calls of '.bindService()'.
99
+ *
100
+ * If you don't find the gRPC/protobuf descriptor, make your you generated the gRPC/ProtoBuf code with
101
+ * the option to generate the descriptor. For example, using the 'ts-proto' plugin, make sure you pass
102
+ * the 'outputSchema=true' option. If you are using Restate's project templates, this should all be
103
+ * pre-configured for you.
104
+ *
105
+ * @example
106
+ * ```
107
+ * endpoint.bindService({
108
+ * service: "MyService",
109
+ * instance: new myService.MyServiceImpl(),
110
+ * descriptor: myService.protoMetadata
111
+ * })
112
+ * ```
113
+ *
114
+ * @param serviceOpts The options describing the service to be bound. See above for a detailed description.
115
+ * @returns An instance of this LambdaRestateServer
116
+ */
117
+ bindService(serviceOpts: ServiceOpts): RestateEndpoint;
118
+
119
+ /**
120
+ * Binds a new durable RPC service to the given path. This method is for regular (stateless)
121
+ * durably executed RPC services.
122
+ *
123
+ * The service will expose all properties of the router that are functions as follows:
124
+ * If the path is 'acme.myservice' and the router has '{ foo, bar }' as properties, the
125
+ * Restate will expose the RPC paths '/acme.myservice/foo' and '/acme.myservice/bar'.
126
+ */
127
+ bindRouter<M>(path: string, router: UnKeyedRouter<M>): RestateEndpoint;
128
+
129
+ /**
130
+ * Binds a new stateful keyed durable RPC service to the given path.
131
+ * This method is services where each invocation is bound to a key and that may maintain
132
+ * state per key.
133
+ *
134
+ * The service will expose all properties of the router that are functions as follows:
135
+ * If the path is 'acme.myservice' and the router has '{ foo, bar }' as properties, the
136
+ * Restate will expose the RPC paths '/acme.myservice/foo' and '/acme.myservice/bar'.
137
+ */
138
+ bindKeyedRouter<M>(path: string, router: KeyedRouter<M>): RestateEndpoint;
139
+
140
+ /**
141
+ * Adds one or more services to this endpoint. This will call the
142
+ * {@link ServiceBundle.registerServices} function to register all services at this endpoint.
143
+ */
144
+ bind(services: ServiceBundle): RestateEndpoint;
145
+
146
+ /**
147
+ * Creates the invocation handler function to be called by AWS Lambda.
148
+ *
149
+ * The returned type of this function is `(event: APIGatewayProxyEvent | APIGatewayProxyEventV2) => Promise<APIGatewayProxyResult | APIGatewayProxyResultV2>`.
150
+ * We use `any` types here to avoid a dependency on the `@types/aws-lambda` dependency for consumers of this API.
151
+ *
152
+ * @returns The invocation handler function for to be called by AWS Lambda.
153
+ */
154
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
155
+ lambdaHandler(): (event: any, ctx: any) => Promise<any>;
156
+
157
+ /**
158
+ * Serve this Restate Endpoint as HTTP2 server, listening to the given port.
159
+ *
160
+ * If the port is undefined, this method will use the port set in the `PORT`
161
+ * environment variable. If that variable is undefined as well, the method will
162
+ * default to port 9080.
163
+ *
164
+ * This method's result promise never completes.
165
+ *
166
+ * This method is a shorthand for:
167
+ *
168
+ * @example
169
+ * ```
170
+ * const httpServer = http2.createServer(endpoint.http2Handler());
171
+ * httpServer.listen(port);
172
+ * ```
173
+ *
174
+ * If you need to manually control the server lifecycle, we suggest to manually instantiate the http2 server and use {@link http2Handler}.
175
+ *
176
+ * @param port The port to listen at. May be undefined (see above).
177
+ */
178
+ listen(port?: number): Promise<void>;
179
+
180
+ /**
181
+ * Returns an http2 server handler. See {@link listen} for more details.
182
+ */
183
+ http2Handler(): (
184
+ request: Http2ServerRequest,
185
+ response: Http2ServerResponse
186
+ ) => void;
187
+ }