@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,144 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.LambdaHandler = void 0;
14
+ const logger_1 = require("../logger");
15
+ const discovery_1 = require("../generated/proto/discovery");
16
+ const lambda_connection_1 = require("../connection/lambda_connection");
17
+ const invocation_1 = require("../invocation");
18
+ const decoder_1 = require("../io/decoder");
19
+ const state_machine_1 = require("../state_machine");
20
+ const errors_1 = require("../types/errors");
21
+ const protocol_1 = require("../types/protocol");
22
+ class LambdaHandler {
23
+ endpoint;
24
+ discoveryResponse;
25
+ constructor(endpoint) {
26
+ this.endpoint = endpoint;
27
+ this.discoveryResponse = discovery_1.ServiceDiscoveryResponse.fromPartial({
28
+ ...this.endpoint.discovery,
29
+ protocolMode: discovery_1.ProtocolMode.REQUEST_RESPONSE,
30
+ });
31
+ }
32
+ // --------------------------------------------------------------------------
33
+ /**
34
+ * This is the main request handling method, effectively a typed variant of `create()`.
35
+ */
36
+ async handleRequest(event, context) {
37
+ let path;
38
+ if ("path" in event) {
39
+ // V1
40
+ path = event.path;
41
+ }
42
+ else {
43
+ // V2
44
+ path = event.rawPath;
45
+ }
46
+ const pathSegments = path.split("/");
47
+ // API Gateway can add a prefix to the path based on the name of the Lambda function and deployment stage
48
+ // (e.g. /default)
49
+ // So we only check the ending of the path on correctness.
50
+ // Logic:
51
+ // 1. Check whether there are at least three segments in the path and whether the third-last one is "invoke".
52
+ // If that is the case, treat it as an invocation.
53
+ // 2. See if the last one is "discover", answer with discovery.
54
+ // 3. Else report "invalid path".
55
+ if (pathSegments.length >= 3 &&
56
+ pathSegments[pathSegments.length - 3] === "invoke") {
57
+ const url = "/" + pathSegments.slice(-3).join("/");
58
+ return await this.handleInvoke(url, event, context);
59
+ }
60
+ else if (pathSegments[pathSegments.length - 1] === "discover") {
61
+ return this.handleDiscovery();
62
+ }
63
+ else {
64
+ const msg = `Invalid path: path doesn't end in /invoke/SvcName/MethodName and also not in /discover: ${path}`;
65
+ logger_1.rlog.trace(msg);
66
+ return this.toErrorResponse(500, msg);
67
+ }
68
+ }
69
+ async handleInvoke(url, event, context) {
70
+ try {
71
+ const method = this.endpoint.methodByUrl(url);
72
+ if (event.body == null) {
73
+ throw new Error("The incoming message body was null");
74
+ }
75
+ if (method === undefined) {
76
+ if (url.includes("?")) {
77
+ throw new Error(`Invalid path: path URL seems to include query parameters: ${url}`);
78
+ }
79
+ else {
80
+ const msg = `No service found for URL: ${url}`;
81
+ logger_1.rlog.error(msg);
82
+ return this.toErrorResponse(404, msg);
83
+ }
84
+ }
85
+ // build the previous journal from the events
86
+ let decodedEntries = (0, decoder_1.decodeLambdaBody)(event.body);
87
+ const journalBuilder = new invocation_1.InvocationBuilder(method);
88
+ decodedEntries.forEach((e) => journalBuilder.handleMessage(e));
89
+ const alreadyCompleted = decodedEntries.find((e) => e.messageType === protocol_1.OUTPUT_STREAM_ENTRY_MESSAGE_TYPE) !== undefined;
90
+ decodedEntries = null;
91
+ // set up and invoke the state machine
92
+ const connection = new lambda_connection_1.LambdaConnection(alreadyCompleted);
93
+ const invocation = journalBuilder.build();
94
+ const stateMachine = new state_machine_1.StateMachine(connection, invocation, discovery_1.ProtocolMode.REQUEST_RESPONSE, method.method.keyedContext, invocation.inferLoggerContext({
95
+ AWSRequestId: context.awsRequestId,
96
+ }));
97
+ await stateMachine.invoke();
98
+ const result = await connection.getResult();
99
+ return {
100
+ headers: {
101
+ "content-type": "application/restate",
102
+ },
103
+ statusCode: 200,
104
+ isBase64Encoded: true,
105
+ body: encodeResponse(result),
106
+ };
107
+ }
108
+ catch (e) {
109
+ const error = (0, errors_1.ensureError)(e);
110
+ logger_1.rlog.error(error.message);
111
+ logger_1.rlog.error(error.stack);
112
+ return this.toErrorResponse(500, error.message);
113
+ }
114
+ }
115
+ handleDiscovery() {
116
+ // return discovery information
117
+ logger_1.rlog.info("Answering discovery request. Announcing services: " +
118
+ JSON.stringify(this.discoveryResponse.services));
119
+ return {
120
+ headers: {
121
+ "content-type": "application/proto",
122
+ },
123
+ statusCode: 200,
124
+ isBase64Encoded: true,
125
+ body: encodeResponse(discovery_1.ServiceDiscoveryResponse.encode(this.discoveryResponse).finish()),
126
+ };
127
+ }
128
+ toErrorResponse(code, message) {
129
+ return {
130
+ headers: {
131
+ "content-type": "application/restate",
132
+ },
133
+ statusCode: code,
134
+ isBase64Encoded: true,
135
+ body: encodeResponse(Buffer.from(JSON.stringify({ message }))),
136
+ };
137
+ }
138
+ }
139
+ exports.LambdaHandler = LambdaHandler;
140
+ function encodeResponse(data) {
141
+ const buffer = data instanceof Buffer ? data : Buffer.from(data);
142
+ return buffer.toString("base64");
143
+ }
144
+ //# sourceMappingURL=lambda_handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lambda_handler.js","sourceRoot":"","sources":["../../src/endpoint/lambda_handler.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAEH,sCAAiC;AAQjC,4DAGsC;AAEtC,uEAAmE;AACnE,8CAAkD;AAClD,2CAAiD;AAEjD,oDAAgD;AAChD,4CAA8C;AAC9C,gDAAqE;AAErE,MAAa,aAAa;IAEK;IADZ,iBAAiB,CAA2B;IAC7D,YAA6B,QAAsB;QAAtB,aAAQ,GAAR,QAAQ,CAAc;QACjD,IAAI,CAAC,iBAAiB,GAAG,oCAAwB,CAAC,WAAW,CAAC;YAC5D,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS;YAC1B,YAAY,EAAE,wBAAY,CAAC,gBAAgB;SAC5C,CAAC,CAAC;IACL,CAAC;IAED,6EAA6E;IAE7E;;OAEG;IACH,KAAK,CAAC,aAAa,CACjB,KAAoD,EACpD,OAAgB;QAEhB,IAAI,IAAI,CAAC;QACT,IAAI,MAAM,IAAI,KAAK,EAAE;YACnB,KAAK;YACL,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;SACnB;aAAM;YACL,KAAK;YACL,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC;SACtB;QACD,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAErC,yGAAyG;QACzG,kBAAkB;QAClB,0DAA0D;QAC1D,SAAS;QACT,6GAA6G;QAC7G,kDAAkD;QAClD,+DAA+D;QAC/D,iCAAiC;QACjC,IACE,YAA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,KAAK,CAAC,KAAK,CAAC,CAAC;YACxB,OAAO,IAAI,CAAC,eAAe,CAAC,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;SACjD;IACH,CAAC;IAEO,eAAe;QACrB,+BAA+B;QAC/B,aAAI,CAAC,IAAI,CACP,oDAAoD;YAClD,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAClD,CAAC;QACF,OAAO;YACL,OAAO,EAAE;gBACP,cAAc,EAAE,mBAAmB;aACpC;YACD,UAAU,EAAE,GAAG;YACf,eAAe,EAAE,IAAI;YACrB,IAAI,EAAE,cAAc,CAClB,oCAAwB,CAAC,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,MAAM,EAAE,CACjE;SACF,CAAC;IACJ,CAAC;IAEO,eAAe,CAAC,IAAY,EAAE,OAAe;QACnD,OAAO;YACL,OAAO,EAAE;gBACP,cAAc,EAAE,qBAAqB;aACtC;YACD,UAAU,EAAE,IAAI;YAChB,eAAe,EAAE,IAAI;YACrB,IAAI,EAAE,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;SAC/D,CAAC;IACJ,CAAC;CACF;AA/ID,sCA+IC;AAED,SAAS,cAAc,CAAC,IAAgB;IACtC,MAAM,MAAM,GAAG,IAAI,YAAY,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjE,OAAO,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACnC,CAAC"}
@@ -0,0 +1,161 @@
1
+ /// <reference types="node" />
2
+ import { KeyedRouter, UnKeyedRouter } from "./types/router";
3
+ import { ProtoMetadata } from "./types/grpc";
4
+ import { Http2ServerRequest, Http2ServerResponse } from "http2";
5
+ /**
6
+ * Create a new {@link RestateEndpoint}.
7
+ */
8
+ export declare function endpoint(): RestateEndpoint;
9
+ /**
10
+ * The properties describing a gRPC service. Consisting of the name, the object holding the
11
+ * implementation, and the descriptor (metadata) describing the service and types.
12
+ */
13
+ export interface ServiceOpts {
14
+ descriptor: ProtoMetadata;
15
+ service: string;
16
+ instance: unknown;
17
+ }
18
+ /**
19
+ * Utility interface for a bundle of one or more services belonging together
20
+ * and being registered together.
21
+ */
22
+ export interface ServiceBundle {
23
+ /**
24
+ * Called to register the services at the endpoint.
25
+ */
26
+ registerServices(endpoint: RestateEndpoint): void;
27
+ }
28
+ /**
29
+ * RestateEndpoint encapsulates all the Restate services served by this endpoint.
30
+ *
31
+ * A RestateEndpoint can either be served either as HTTP2 server, using the methods {@link listen} or {@link http2Handler},
32
+ * or as Lambda, using the method {@link lambdaHandler}.
33
+ *
34
+ * @example
35
+ * A typical endpoint served as HTTP server would look like this:
36
+ * ```
37
+ * import * as restate from "@restatedev/restate-sdk";
38
+ *
39
+ * restate
40
+ * .endpoint()
41
+ * .bindService({
42
+ * service: "MyService",
43
+ * instance: new myService.MyServiceImpl(),
44
+ * descriptor: myService.protoMetadata,
45
+ * })
46
+ * .listen(8000);
47
+ * ```
48
+ * @example
49
+ * A typical endpoint served as AWS Lambda would look like this:
50
+ * ```
51
+ * import * as restate from "@restatedev/restate-sdk";
52
+ *
53
+ * export const handler = restate
54
+ * .endpoint()
55
+ * .bindService({
56
+ * service: "MyService",
57
+ * instance: new myService.MyServiceImpl(),
58
+ * descriptor: myService.protoMetadata,
59
+ * })
60
+ * .lambdaHandler();
61
+ * ```
62
+ */
63
+ export interface RestateEndpoint {
64
+ /**
65
+ * Adds a gRPC service to be served from this endpoint.
66
+ *
67
+ * The {@link ServiceOpts} passed here need to describe the following properties:
68
+ *
69
+ * - The 'service' name: the name of the gRPC service (as in the service definition proto file).
70
+ * - The service 'instance': the implementation of the service logic (must implement the generated
71
+ * gRPC service interface).
72
+ * - The gRPC/protobuf 'descriptor': The protoMetadata descriptor that describes the service, methods,
73
+ * and parameter types. It is usually found as the value 'protoMetadata' in the generated
74
+ * file '(service-name).ts'
75
+ *
76
+ * The descriptor is generated by the protobuf compiler and needed by Restate to reflectively discover
77
+ * the service details, understand payload serialization, perform HTTP/JSON-to-gRPC transcoding, or
78
+ * to proxy the service.
79
+ *
80
+ * If you define multiple services in the same '.proto' file, you may have only one descriptor that
81
+ * describes all services together. You can pass the same descriptor to multiple calls of '.bindService()'.
82
+ *
83
+ * If you don't find the gRPC/protobuf descriptor, make your you generated the gRPC/ProtoBuf code with
84
+ * the option to generate the descriptor. For example, using the 'ts-proto' plugin, make sure you pass
85
+ * the 'outputSchema=true' option. If you are using Restate's project templates, this should all be
86
+ * pre-configured for you.
87
+ *
88
+ * @example
89
+ * ```
90
+ * endpoint.bindService({
91
+ * service: "MyService",
92
+ * instance: new myService.MyServiceImpl(),
93
+ * descriptor: myService.protoMetadata
94
+ * })
95
+ * ```
96
+ *
97
+ * @param serviceOpts The options describing the service to be bound. See above for a detailed description.
98
+ * @returns An instance of this LambdaRestateServer
99
+ */
100
+ bindService(serviceOpts: ServiceOpts): RestateEndpoint;
101
+ /**
102
+ * Binds a new durable RPC service to the given path. This method is for regular (stateless)
103
+ * durably executed RPC services.
104
+ *
105
+ * The service will expose all properties of the router that are functions as follows:
106
+ * If the path is 'acme.myservice' and the router has '{ foo, bar }' as properties, the
107
+ * Restate will expose the RPC paths '/acme.myservice/foo' and '/acme.myservice/bar'.
108
+ */
109
+ bindRouter<M>(path: string, router: UnKeyedRouter<M>): RestateEndpoint;
110
+ /**
111
+ * Binds a new stateful keyed durable RPC service to the given path.
112
+ * This method is services where each invocation is bound to a key and that may maintain
113
+ * state per key.
114
+ *
115
+ * The service will expose all properties of the router that are functions as follows:
116
+ * If the path is 'acme.myservice' and the router has '{ foo, bar }' as properties, the
117
+ * Restate will expose the RPC paths '/acme.myservice/foo' and '/acme.myservice/bar'.
118
+ */
119
+ bindKeyedRouter<M>(path: string, router: KeyedRouter<M>): RestateEndpoint;
120
+ /**
121
+ * Adds one or more services to this endpoint. This will call the
122
+ * {@link ServiceBundle.registerServices} function to register all services at this endpoint.
123
+ */
124
+ bind(services: ServiceBundle): RestateEndpoint;
125
+ /**
126
+ * Creates the invocation handler function to be called by AWS Lambda.
127
+ *
128
+ * The returned type of this function is `(event: APIGatewayProxyEvent | APIGatewayProxyEventV2) => Promise<APIGatewayProxyResult | APIGatewayProxyResultV2>`.
129
+ * We use `any` types here to avoid a dependency on the `@types/aws-lambda` dependency for consumers of this API.
130
+ *
131
+ * @returns The invocation handler function for to be called by AWS Lambda.
132
+ */
133
+ lambdaHandler(): (event: any, ctx: any) => Promise<any>;
134
+ /**
135
+ * Serve this Restate Endpoint as HTTP2 server, listening to the given port.
136
+ *
137
+ * If the port is undefined, this method will use the port set in the `PORT`
138
+ * environment variable. If that variable is undefined as well, the method will
139
+ * default to port 9080.
140
+ *
141
+ * This method's result promise never completes.
142
+ *
143
+ * This method is a shorthand for:
144
+ *
145
+ * @example
146
+ * ```
147
+ * const httpServer = http2.createServer(endpoint.http2Handler());
148
+ * httpServer.listen(port);
149
+ * ```
150
+ *
151
+ * If you need to manually control the server lifecycle, we suggest to manually instantiate the http2 server and use {@link http2Handler}.
152
+ *
153
+ * @param port The port to listen at. May be undefined (see above).
154
+ */
155
+ listen(port?: number): Promise<void>;
156
+ /**
157
+ * Returns an http2 server handler. See {@link listen} for more details.
158
+ */
159
+ http2Handler(): (request: Http2ServerRequest, response: Http2ServerResponse) => void;
160
+ }
161
+ //# sourceMappingURL=endpoint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"endpoint.d.ts","sourceRoot":"","sources":["../src/endpoint.ts"],"names":[],"mappings":";AAWA,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,OAAO,CAAC;AAGhE;;GAEG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
+ *
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.endpoint = void 0;
14
+ const endpoint_impl_1 = require("./endpoint/endpoint_impl");
15
+ /**
16
+ * Create a new {@link RestateEndpoint}.
17
+ */
18
+ function endpoint() {
19
+ return new endpoint_impl_1.EndpointImpl();
20
+ }
21
+ exports.endpoint = endpoint;
22
+ //# sourceMappingURL=endpoint.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"endpoint.js","sourceRoot":"","sources":["../src/endpoint.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAKH,4DAAwD;AAExD;;GAEG;AACH,SAAgB,QAAQ;IACtB,OAAO,IAAI,4BAAY,EAAE,CAAC;AAC5B,CAAC;AAFD,4BAEC"}