@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.
- package/LICENSE +1 -1
- package/README.md +29 -51
- package/dist/clients/workflow_client.d.ts +77 -0
- package/dist/clients/workflow_client.d.ts.map +1 -0
- package/dist/clients/workflow_client.js +172 -0
- package/dist/clients/workflow_client.js.map +1 -0
- package/dist/connection/buffered_connection.js +44 -0
- package/dist/connection/buffered_connection.js.map +1 -0
- package/dist/connection/connection.js +13 -0
- package/dist/connection/connection.js.map +1 -0
- package/dist/connection/embedded_connection.js +59 -0
- package/dist/connection/embedded_connection.js.map +1 -0
- package/dist/connection/http_connection.js +203 -0
- package/dist/connection/http_connection.js.map +1 -0
- package/dist/connection/lambda_connection.js +58 -0
- package/dist/connection/lambda_connection.js.map +1 -0
- package/dist/{restate_context.d.ts → context.d.ts} +239 -170
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +113 -0
- package/dist/context.js.map +1 -0
- package/dist/{restate_context_impl.d.ts → context_impl.d.ts} +26 -30
- package/dist/context_impl.d.ts.map +1 -0
- package/dist/context_impl.js +439 -0
- package/dist/context_impl.js.map +1 -0
- package/dist/embedded/api.d.ts +2 -2
- package/dist/embedded/api.d.ts.map +1 -1
- package/dist/embedded/api.js +35 -0
- package/dist/embedded/api.js.map +1 -0
- package/dist/embedded/handler.d.ts +2 -2
- package/dist/embedded/handler.d.ts.map +1 -1
- package/dist/embedded/handler.js +26 -0
- package/dist/embedded/handler.js.map +1 -0
- package/dist/embedded/http2_remote.js +91 -0
- package/dist/embedded/http2_remote.js.map +1 -0
- package/dist/embedded/invocation.d.ts.map +1 -1
- package/dist/embedded/invocation.js +94 -0
- package/dist/embedded/invocation.js.map +1 -0
- package/dist/endpoint/endpoint_impl.d.ts +35 -0
- package/dist/endpoint/endpoint_impl.d.ts.map +1 -0
- package/dist/endpoint/endpoint_impl.js +405 -0
- package/dist/endpoint/endpoint_impl.js.map +1 -0
- package/dist/endpoint/http2_handler.d.ts +11 -0
- package/dist/endpoint/http2_handler.d.ts.map +1 -0
- package/dist/endpoint/http2_handler.js +119 -0
- package/dist/endpoint/http2_handler.js.map +1 -0
- package/dist/endpoint/lambda_handler.d.ts +15 -0
- package/dist/endpoint/lambda_handler.d.ts.map +1 -0
- package/dist/endpoint/lambda_handler.js +144 -0
- package/dist/endpoint/lambda_handler.js.map +1 -0
- package/dist/endpoint.d.ts +161 -0
- package/dist/endpoint.d.ts.map +1 -0
- package/dist/endpoint.js +22 -0
- package/dist/endpoint.js.map +1 -0
- package/dist/generated/dev/restate/events.js +371 -0
- package/dist/generated/dev/restate/events.js.map +1 -0
- package/dist/generated/dev/restate/ext.js +215 -0
- package/dist/generated/dev/restate/ext.js.map +1 -0
- package/dist/generated/google/protobuf/descriptor.js +6676 -0
- package/dist/generated/google/protobuf/descriptor.js.map +1 -0
- package/dist/generated/google/protobuf/empty.js +107 -0
- package/dist/generated/google/protobuf/empty.js.map +1 -0
- package/dist/generated/google/protobuf/struct.js +754 -0
- package/dist/generated/google/protobuf/struct.js.map +1 -0
- package/dist/generated/proto/discovery.js +364 -0
- package/dist/generated/proto/discovery.js.map +1 -0
- package/dist/generated/proto/dynrpc.js +668 -0
- package/dist/generated/proto/dynrpc.js.map +1 -0
- package/dist/generated/proto/javascript.d.ts +13 -0
- package/dist/generated/proto/javascript.d.ts.map +1 -1
- package/dist/generated/proto/javascript.js +416 -0
- package/dist/generated/proto/javascript.js.map +1 -0
- package/dist/generated/proto/protocol.d.ts +43 -0
- package/dist/generated/proto/protocol.d.ts.map +1 -1
- package/dist/generated/proto/protocol.js +2641 -0
- package/dist/generated/proto/protocol.js.map +1 -0
- package/dist/generated/proto/services.js +1535 -0
- package/dist/generated/proto/services.js.map +1 -0
- package/dist/generated/proto/test.js +321 -0
- package/dist/generated/proto/test.js.map +1 -0
- package/dist/invocation.d.ts +4 -1
- package/dist/invocation.d.ts.map +1 -1
- package/dist/invocation.js +157 -0
- package/dist/invocation.js.map +1 -0
- package/dist/io/decoder.d.ts +1 -0
- package/dist/io/decoder.d.ts.map +1 -1
- package/dist/io/decoder.js +140 -0
- package/dist/io/decoder.js.map +1 -0
- package/dist/io/encoder.d.ts +1 -2
- package/dist/io/encoder.d.ts.map +1 -1
- package/dist/io/encoder.js +68 -0
- package/dist/io/encoder.js.map +1 -0
- package/dist/journal.d.ts +13 -4
- package/dist/journal.d.ts.map +1 -1
- package/dist/journal.js +405 -0
- package/dist/journal.js.map +1 -0
- package/dist/local_state_store.d.ts +5 -3
- package/dist/local_state_store.d.ts.map +1 -1
- package/dist/local_state_store.js +82 -0
- package/dist/local_state_store.js.map +1 -0
- package/dist/logger.d.ts +19 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +90 -0
- package/dist/logger.js.map +1 -0
- package/dist/promise_combinator_tracker.d.ts +29 -0
- package/dist/promise_combinator_tracker.d.ts.map +1 -0
- package/dist/promise_combinator_tracker.js +128 -0
- package/dist/promise_combinator_tracker.js.map +1 -0
- package/dist/public_api.d.ts +5 -5
- package/dist/public_api.d.ts.map +1 -1
- package/dist/public_api.js +60 -0
- package/dist/public_api.js.map +1 -0
- package/dist/state_machine.d.ts +19 -12
- package/dist/state_machine.d.ts.map +1 -1
- package/dist/state_machine.js +437 -0
- package/dist/state_machine.js.map +1 -0
- package/dist/types/errors.d.ts +12 -3
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +273 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/grpc.d.ts +6 -4
- package/dist/types/grpc.d.ts.map +1 -1
- package/dist/types/grpc.js +81 -0
- package/dist/types/grpc.js.map +1 -0
- package/dist/types/protocol.d.ts +9 -5
- package/dist/types/protocol.d.ts.map +1 -1
- package/dist/types/protocol.js +147 -0
- package/dist/types/protocol.js.map +1 -0
- package/dist/types/router.d.ts +8 -8
- package/dist/types/router.d.ts.map +1 -1
- package/dist/types/router.js +36 -0
- package/dist/types/router.js.map +1 -0
- package/dist/types/types.d.ts +1 -0
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/types.js +138 -0
- package/dist/types/types.js.map +1 -0
- package/dist/utils/{assumpsions.d.ts → assumptions.d.ts} +1 -1
- package/dist/utils/{assumpsions.d.ts.map → assumptions.d.ts.map} +1 -1
- package/dist/utils/assumptions.js +101 -0
- package/dist/utils/assumptions.js.map +1 -0
- package/dist/utils/message_logger.d.ts +28 -0
- package/dist/utils/message_logger.d.ts.map +1 -0
- package/dist/utils/message_logger.js +88 -0
- package/dist/utils/message_logger.js.map +1 -0
- package/dist/utils/promises.d.ts +15 -0
- package/dist/utils/promises.d.ts.map +1 -0
- package/dist/utils/promises.js +67 -0
- package/dist/utils/promises.js.map +1 -0
- package/dist/utils/public_utils.js +49 -0
- package/dist/utils/public_utils.js.map +1 -0
- package/dist/utils/rand.d.ts +1 -1
- package/dist/utils/rand.d.ts.map +1 -1
- package/dist/utils/rand.js +114 -0
- package/dist/utils/rand.js.map +1 -0
- package/dist/utils/utils.d.ts +1 -10
- package/dist/utils/utils.d.ts.map +1 -1
- package/dist/utils/utils.js +122 -0
- package/dist/utils/utils.js.map +1 -0
- package/dist/workflows/workflow.d.ts +101 -0
- package/dist/workflows/workflow.d.ts.map +1 -0
- package/dist/workflows/workflow.js +80 -0
- package/dist/workflows/workflow.js.map +1 -0
- package/dist/workflows/workflow_state_service.d.ts +35 -0
- package/dist/workflows/workflow_state_service.d.ts.map +1 -0
- package/dist/workflows/workflow_state_service.js +201 -0
- package/dist/workflows/workflow_state_service.js.map +1 -0
- package/dist/workflows/workflow_wrapper_service.d.ts +10 -0
- package/dist/workflows/workflow_wrapper_service.d.ts.map +1 -0
- package/dist/workflows/workflow_wrapper_service.js +264 -0
- package/dist/workflows/workflow_wrapper_service.js.map +1 -0
- package/package.json +38 -39
- package/src/clients/workflow_client.ts +290 -0
- package/src/connection/buffered_connection.ts +47 -0
- package/src/connection/connection.ts +34 -0
- package/src/connection/embedded_connection.ts +62 -0
- package/src/connection/http_connection.ts +228 -0
- package/src/connection/lambda_connection.ts +69 -0
- package/src/context.ts +633 -0
- package/src/context_impl.ts +721 -0
- package/src/embedded/api.ts +57 -0
- package/src/embedded/handler.ts +36 -0
- package/src/embedded/http2_remote.ts +103 -0
- package/src/embedded/invocation.ts +126 -0
- package/src/endpoint/endpoint_impl.ts +623 -0
- package/src/endpoint/http2_handler.ts +151 -0
- package/src/endpoint/lambda_handler.ts +181 -0
- package/src/endpoint.ts +187 -0
- package/src/generated/dev/restate/events.ts +430 -0
- package/src/generated/dev/restate/ext.ts +238 -0
- package/src/generated/google/protobuf/descriptor.ts +7889 -0
- package/src/generated/google/protobuf/empty.ts +150 -0
- package/src/generated/google/protobuf/struct.ts +878 -0
- package/src/generated/proto/discovery.ts +423 -0
- package/src/generated/proto/dynrpc.ts +768 -0
- package/src/generated/proto/javascript.ts +488 -0
- package/src/generated/proto/protocol.ts +3091 -0
- package/src/generated/proto/services.ts +1834 -0
- package/src/generated/proto/test.ts +387 -0
- package/src/invocation.ts +212 -0
- package/src/io/decoder.ts +171 -0
- package/src/io/encoder.ts +72 -0
- package/src/journal.ts +537 -0
- package/src/local_state_store.ts +94 -0
- package/src/logger.ts +121 -0
- package/src/promise_combinator_tracker.ts +191 -0
- package/src/public_api.ts +53 -0
- package/src/state_machine.ts +635 -0
- package/src/types/errors.ts +297 -0
- package/src/types/grpc.ts +97 -0
- package/src/types/protocol.ts +201 -0
- package/src/types/router.ts +118 -0
- package/src/types/types.ts +160 -0
- package/src/utils/assumptions.ts +131 -0
- package/src/utils/message_logger.ts +112 -0
- package/src/utils/promises.ts +118 -0
- package/src/utils/public_utils.ts +91 -0
- package/src/utils/rand.ts +142 -0
- package/src/utils/utils.ts +178 -0
- package/src/workflows/workflow.ts +178 -0
- package/src/workflows/workflow_state_service.ts +299 -0
- package/src/workflows/workflow_wrapper_service.ts +314 -0
- package/dist/cloudflare_bundle.js +0 -27387
- package/dist/restate_context.d.ts.map +0 -1
- package/dist/restate_context_impl.d.ts.map +0 -1
- package/dist/server/base_restate_server.d.ts +0 -32
- package/dist/server/base_restate_server.d.ts.map +0 -1
- package/dist/server/restate_lambda_handler.d.ts +0 -104
- package/dist/server/restate_lambda_handler.d.ts.map +0 -1
- package/dist/server/restate_server.d.ts +0 -97
- package/dist/server/restate_server.d.ts.map +0 -1
- package/dist/utils/logger.d.ts +0 -60
- package/dist/utils/logger.d.ts.map +0 -1
|
@@ -0,0 +1,203 @@
|
|
|
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.RestateHttp2Connection = void 0;
|
|
14
|
+
const decoder_1 = require("../io/decoder");
|
|
15
|
+
const logger_1 = require("../logger");
|
|
16
|
+
const promises_1 = require("stream/promises");
|
|
17
|
+
const buffered_connection_1 = require("./buffered_connection");
|
|
18
|
+
// utility promise, for cases where we want to save allocation of an extra promise
|
|
19
|
+
const RESOLVED = Promise.resolve();
|
|
20
|
+
/**
|
|
21
|
+
* A duplex stream with Restate Messages over HTTP2.
|
|
22
|
+
*
|
|
23
|
+
* This stream handles the following concerns:
|
|
24
|
+
*
|
|
25
|
+
* (1) encoding and decoding of messages from and from raw bytes
|
|
26
|
+
*
|
|
27
|
+
* (2) buffering the outgoing messages, because the call sites that produce (potentially) large
|
|
28
|
+
* messages might not await their transfer. Aside from the fact that we achieve better pipelining
|
|
29
|
+
* that way, we also simply cannot guarantee that users of the Restate SDK actually await the
|
|
30
|
+
* relevant async API methods.
|
|
31
|
+
*
|
|
32
|
+
* This stream essentially buffers messages and, upon flush, sends them asynchronously, as the
|
|
33
|
+
* stream has availability. Flush requests queue up, if new data gets flushed while the previous
|
|
34
|
+
* data is still being sent.
|
|
35
|
+
*
|
|
36
|
+
* (3) Input messages can be pipelined to a sequence of consumers. For example, first to a journal,
|
|
37
|
+
* and afterwards to the state machine.
|
|
38
|
+
*
|
|
39
|
+
* (4) Handling the relevant stream events for errors and consolidating them to one error handler, plus
|
|
40
|
+
* notifications for cleanly closed input (to trigger suspension).
|
|
41
|
+
*/
|
|
42
|
+
class RestateHttp2Connection {
|
|
43
|
+
rawStream;
|
|
44
|
+
/**
|
|
45
|
+
* create a RestateDuplex stream from an http2 (duplex) stream.
|
|
46
|
+
*/
|
|
47
|
+
static from(http2stream) {
|
|
48
|
+
return new RestateHttp2Connection(http2stream);
|
|
49
|
+
}
|
|
50
|
+
// --------------------------------------------------------------------------
|
|
51
|
+
// input as decoded messages
|
|
52
|
+
sdkInput;
|
|
53
|
+
// consumer handling
|
|
54
|
+
currentConsumer = null;
|
|
55
|
+
inputBuffer = [];
|
|
56
|
+
consumerError;
|
|
57
|
+
consumerInputClosed = false;
|
|
58
|
+
outputBuffer;
|
|
59
|
+
constructor(rawStream) {
|
|
60
|
+
this.rawStream = rawStream;
|
|
61
|
+
this.sdkInput = rawStream.pipe((0, decoder_1.streamDecoder)());
|
|
62
|
+
this.outputBuffer = new buffered_connection_1.BufferedConnection((buffer) => {
|
|
63
|
+
const hasMoreCapacity = rawStream.write(buffer);
|
|
64
|
+
if (hasMoreCapacity) {
|
|
65
|
+
return RESOLVED;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return new Promise((resolve) => rawStream.once("drain", resolve));
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
// remember and forward messages
|
|
72
|
+
this.sdkInput.on("data", (m) => {
|
|
73
|
+
// deliver message, if we have a consumer. otherwise buffer the message.
|
|
74
|
+
if (this.currentConsumer) {
|
|
75
|
+
if (this.currentConsumer.handleMessage(m)) {
|
|
76
|
+
this.removeCurrentConsumer();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
this.inputBuffer.push(m);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
// remember and forward close events
|
|
84
|
+
this.sdkInput.on("end", () => {
|
|
85
|
+
this.consumerInputClosed = true;
|
|
86
|
+
if (this.currentConsumer) {
|
|
87
|
+
this.currentConsumer.handleInputClosed();
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
// --------- error handling --------
|
|
91
|
+
// - a.k.a. node event wrangling...
|
|
92
|
+
// the error handler for all sorts of errors coming from streams
|
|
93
|
+
const errorHandler = (e) => {
|
|
94
|
+
// make sure we don't overwrite the initial error
|
|
95
|
+
if (this.consumerError !== undefined) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
this.consumerError = e;
|
|
99
|
+
if (this.currentConsumer) {
|
|
100
|
+
this.currentConsumer.handleStreamError(e);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
// those two event types should cover all types of connection losses
|
|
104
|
+
rawStream.on("aborted", () => {
|
|
105
|
+
logger_1.rlog.error("Connection to Restate was lost");
|
|
106
|
+
errorHandler(new Error("Connection to Restate was lost"));
|
|
107
|
+
});
|
|
108
|
+
// this is both the raw http2 stream and the output SDK->Restate
|
|
109
|
+
rawStream.on("error", (e) => {
|
|
110
|
+
logger_1.rlog.error("Error in http2 stream to Restate: " + (e.stack ?? e.message));
|
|
111
|
+
errorHandler(e);
|
|
112
|
+
});
|
|
113
|
+
// these events notify of errors in the decoding pipeline
|
|
114
|
+
this.sdkInput.on("error", (e) => {
|
|
115
|
+
logger_1.rlog.error("Error in input stream (Restate to Service): " + (e.stack ?? e.message));
|
|
116
|
+
errorHandler(e);
|
|
117
|
+
});
|
|
118
|
+
// see if streams get torn down before they end cleanly
|
|
119
|
+
this.sdkInput.on("close", () => {
|
|
120
|
+
if (!this.consumerInputClosed) {
|
|
121
|
+
errorHandler(new Error("stream was destroyed before end"));
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
// --------------------------------------------------------------------------
|
|
126
|
+
// input stream handling
|
|
127
|
+
// --------------------------------------------------------------------------
|
|
128
|
+
/**
|
|
129
|
+
* Pipes the messages from this connection to the given consumer. The consumer
|
|
130
|
+
* will also receive error and stream closing notifications.
|
|
131
|
+
*
|
|
132
|
+
* Once the 'handleMessage()' method returns 'true', the consumer is immediately removed.
|
|
133
|
+
* That way, consumers can consume a bounded amount of messages (like just the initial journal).
|
|
134
|
+
*
|
|
135
|
+
* There can only be one consumer at a time.
|
|
136
|
+
*/
|
|
137
|
+
pipeToConsumer(consumer) {
|
|
138
|
+
if (this.currentConsumer !== null) {
|
|
139
|
+
throw new Error("Already piping to a consumer");
|
|
140
|
+
}
|
|
141
|
+
this.currentConsumer = consumer;
|
|
142
|
+
// propagate pre-existing information
|
|
143
|
+
if (this.consumerError) {
|
|
144
|
+
consumer.handleStreamError(this.consumerError);
|
|
145
|
+
}
|
|
146
|
+
if (this.consumerInputClosed) {
|
|
147
|
+
consumer.handleInputClosed();
|
|
148
|
+
}
|
|
149
|
+
// pipe the buffered input messages, if we buffered some before the consumer was registered
|
|
150
|
+
const input = this.inputBuffer;
|
|
151
|
+
if (input.length > 0) {
|
|
152
|
+
let i = 0;
|
|
153
|
+
while (i < input.length) {
|
|
154
|
+
const done = consumer.handleMessage(input[i]);
|
|
155
|
+
i++;
|
|
156
|
+
if (done) {
|
|
157
|
+
this.removeCurrentConsumer();
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
this.inputBuffer = i === input.length ? [] : this.inputBuffer.slice(i);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Removes the current consumer, if there is one.
|
|
166
|
+
*/
|
|
167
|
+
removeCurrentConsumer() {
|
|
168
|
+
this.currentConsumer = null;
|
|
169
|
+
}
|
|
170
|
+
// --------------------------------------------------------------------------
|
|
171
|
+
// output stream handling
|
|
172
|
+
// --------------------------------------------------------------------------
|
|
173
|
+
/**
|
|
174
|
+
* Adds a message to the output stream.
|
|
175
|
+
*
|
|
176
|
+
* This always puts the message into the node stream, but will return a promise that is resolved once
|
|
177
|
+
* further messages can be written.
|
|
178
|
+
*
|
|
179
|
+
* The reasoning is that some, but not all Restate operations return promises and are typically
|
|
180
|
+
* awaited. For example, rpc, sleep, side-effect have promises and are awaited, while one-way-sends and
|
|
181
|
+
* state updates don't return promises.
|
|
182
|
+
*
|
|
183
|
+
* As a pragmatic solution, we always accept messages, but return a promise for when the output has
|
|
184
|
+
* capacity again, so that at least the operations that await results will respect backpressure.
|
|
185
|
+
*/
|
|
186
|
+
send(msg) {
|
|
187
|
+
return this.outputBuffer.send(msg);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Ends the stream, awaiting pending writes.
|
|
191
|
+
*/
|
|
192
|
+
async end() {
|
|
193
|
+
await this.outputBuffer.end();
|
|
194
|
+
this.rawStream.end();
|
|
195
|
+
const options = {
|
|
196
|
+
error: true,
|
|
197
|
+
cleanup: true,
|
|
198
|
+
};
|
|
199
|
+
await (0, promises_1.finished)(this.rawStream, options);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
exports.RestateHttp2Connection = RestateHttp2Connection;
|
|
203
|
+
//# sourceMappingURL=http_connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http_connection.js","sourceRoot":"","sources":["../../src/connection/http_connection.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAGH,2CAA8C;AAG9C,sCAAiC;AACjC,8CAA2C;AAC3C,+DAA2D;AAE3D,kFAAkF;AAClF,MAAM,QAAQ,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAa,sBAAsB;IAqBJ;IApB7B;;OA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;SACxE;IACH,CAAC;IAED;;OAEG;IACI,qBAAqB;QAC1B,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;IAC9B,CAAC;IAED,6EAA6E;IAC7E,0BAA0B;IAC1B,6EAA6E;IAE7E;;;;;;;;;;;;OAYG;IACI,IAAI,CAAC,GAAY;QACtB,OAAO,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACrC,CAAC;IAED;;OAEG;IACI,KAAK,CAAC,GAAG;QACd,MAAM,IAAI,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC;QAE9B,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC;QAErB,MAAM,OAAO,GAAG;YACd,KAAK,EAAE,IAAI;YACX,OAAO,EAAE,IAAI;SACd,CAAC;QAEF,MAAM,IAAA,mBAAQ,EAAC,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC1C,CAAC;CACF;AAvLD,wDAuLC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
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.LambdaConnection = void 0;
|
|
14
|
+
const encoder_1 = require("../io/encoder");
|
|
15
|
+
const protocol_1 = require("../types/protocol");
|
|
16
|
+
const logger_1 = require("../logger");
|
|
17
|
+
const RESOLVED = Promise.resolve();
|
|
18
|
+
class LambdaConnection {
|
|
19
|
+
suspendedOrCompleted;
|
|
20
|
+
// Empty buffer to store journal output messages
|
|
21
|
+
outputBuffer = Buffer.alloc(0);
|
|
22
|
+
// Callback to resolve the invocation promise of the Lambda handler when the response is ready
|
|
23
|
+
completionPromise;
|
|
24
|
+
resolveOnCompleted;
|
|
25
|
+
constructor(suspendedOrCompleted = false) {
|
|
26
|
+
this.suspendedOrCompleted = suspendedOrCompleted;
|
|
27
|
+
// Promise that signals when the invocation is over, to then flush the messages
|
|
28
|
+
this.completionPromise = new Promise((resolve) => {
|
|
29
|
+
this.resolveOnCompleted = resolve;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
// Send a message back to the runtime
|
|
33
|
+
send(msg) {
|
|
34
|
+
// Add the header and the body to buffer and add to the output buffer
|
|
35
|
+
const msgBuffer = (0, encoder_1.encodeMessage)(msg);
|
|
36
|
+
this.outputBuffer = Buffer.concat([this.outputBuffer, msgBuffer]);
|
|
37
|
+
// An output message, suspension message or error message is the end of a Lambda invocation
|
|
38
|
+
if (msg.messageType === protocol_1.OUTPUT_STREAM_ENTRY_MESSAGE_TYPE ||
|
|
39
|
+
msg.messageType === protocol_1.SUSPENSION_MESSAGE_TYPE ||
|
|
40
|
+
msg.messageType === protocol_1.ERROR_MESSAGE_TYPE) {
|
|
41
|
+
this.suspendedOrCompleted = true;
|
|
42
|
+
}
|
|
43
|
+
return RESOLVED;
|
|
44
|
+
}
|
|
45
|
+
getResult() {
|
|
46
|
+
return this.completionPromise;
|
|
47
|
+
}
|
|
48
|
+
end() {
|
|
49
|
+
if (this.suspendedOrCompleted) {
|
|
50
|
+
logger_1.rlog.debug("Flushing output buffer...");
|
|
51
|
+
this.resolveOnCompleted(this.outputBuffer);
|
|
52
|
+
}
|
|
53
|
+
this.outputBuffer = Buffer.alloc(0);
|
|
54
|
+
return RESOLVED;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.LambdaConnection = LambdaConnection;
|
|
58
|
+
//# sourceMappingURL=lambda_connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lamb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