@restatedev/restate-sdk 0.8.1 → 0.9.0
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/dist/{clients → src/clients}/workflow_client.d.ts +2 -2
- package/dist/src/clients/workflow_client.d.ts.map +1 -0
- package/dist/{clients → src/clients}/workflow_client.js +2 -2
- package/dist/src/clients/workflow_client.js.map +1 -0
- package/dist/{connection → src/connection}/buffered_connection.d.ts +1 -0
- package/dist/src/connection/buffered_connection.d.ts.map +1 -0
- package/dist/src/connection/buffered_connection.js.map +1 -0
- package/dist/src/connection/connection.d.ts.map +1 -0
- package/dist/src/connection/connection.js.map +1 -0
- package/dist/{connection → src/connection}/http_connection.d.ts +1 -1
- package/dist/src/connection/http_connection.d.ts.map +1 -0
- package/dist/{connection → src/connection}/http_connection.js +1 -1
- package/dist/src/connection/http_connection.js.map +1 -0
- package/dist/{connection → src/connection}/lambda_connection.d.ts +1 -0
- package/dist/src/connection/lambda_connection.d.ts.map +1 -0
- package/dist/{connection → src/connection}/lambda_connection.js +5 -4
- package/dist/src/connection/lambda_connection.js.map +1 -0
- package/dist/{context.d.ts → src/context.d.ts} +146 -224
- package/dist/src/context.d.ts.map +1 -0
- package/dist/{context.js → src/context.js} +6 -41
- package/dist/src/context.js.map +1 -0
- package/dist/src/context_impl.d.ts +62 -0
- package/dist/src/context_impl.d.ts.map +1 -0
- package/dist/{context_impl.js → src/context_impl.js} +201 -182
- package/dist/src/context_impl.js.map +1 -0
- package/dist/src/endpoint/endpoint_impl.d.ts +28 -0
- package/dist/src/endpoint/endpoint_impl.d.ts.map +1 -0
- package/dist/src/endpoint/endpoint_impl.js +164 -0
- package/dist/src/endpoint/endpoint_impl.js.map +1 -0
- package/dist/{endpoint → src/endpoint}/http2_handler.d.ts +1 -1
- package/dist/src/endpoint/http2_handler.d.ts.map +1 -0
- package/dist/src/endpoint/http2_handler.js +153 -0
- package/dist/src/endpoint/http2_handler.js.map +1 -0
- package/dist/{endpoint → src/endpoint}/lambda_handler.d.ts +2 -2
- package/dist/src/endpoint/lambda_handler.d.ts.map +1 -0
- package/dist/{endpoint → src/endpoint}/lambda_handler.js +66 -56
- package/dist/src/endpoint/lambda_handler.js.map +1 -0
- package/dist/src/endpoint/request_signing/basex.d.ts +10 -0
- package/dist/src/endpoint/request_signing/basex.d.ts.map +1 -0
- package/dist/src/endpoint/request_signing/basex.js +138 -0
- package/dist/src/endpoint/request_signing/basex.js.map +1 -0
- package/dist/src/endpoint/request_signing/ed25519.d.ts +17 -0
- package/dist/src/endpoint/request_signing/ed25519.d.ts.map +1 -0
- package/dist/src/endpoint/request_signing/ed25519.js +78 -0
- package/dist/src/endpoint/request_signing/ed25519.js.map +1 -0
- package/dist/src/endpoint/request_signing/v1.d.ts +9 -0
- package/dist/src/endpoint/request_signing/v1.d.ts.map +1 -0
- package/dist/src/endpoint/request_signing/v1.js +148 -0
- package/dist/src/endpoint/request_signing/v1.js.map +1 -0
- package/dist/src/endpoint/request_signing/validate.d.ts +18 -0
- package/dist/src/endpoint/request_signing/validate.d.ts.map +1 -0
- package/dist/src/endpoint/request_signing/validate.js +45 -0
- package/dist/src/endpoint/request_signing/validate.js.map +1 -0
- package/dist/src/endpoint.d.ts +102 -0
- package/dist/src/endpoint.d.ts.map +1 -0
- package/dist/{endpoint.js → src/endpoint.js} +1 -1
- package/dist/src/endpoint.js.map +1 -0
- package/dist/src/generated/proto/javascript_pb.d.ts +26 -0
- package/dist/src/generated/proto/javascript_pb.d.ts.map +1 -0
- package/dist/src/generated/proto/javascript_pb.js +52 -0
- package/dist/src/generated/proto/javascript_pb.js.map +1 -0
- package/dist/src/generated/proto/protocol_pb.d.ts +865 -0
- package/dist/src/generated/proto/protocol_pb.d.ts.map +1 -0
- package/dist/src/generated/proto/protocol_pb.js +1112 -0
- package/dist/src/generated/proto/protocol_pb.js.map +1 -0
- package/dist/src/generated/version.d.ts +2 -0
- package/dist/src/generated/version.d.ts.map +1 -0
- package/dist/src/generated/version.js +5 -0
- package/dist/src/generated/version.js.map +1 -0
- package/dist/{invocation.d.ts → src/invocation.d.ts} +14 -18
- package/dist/src/invocation.d.ts.map +1 -0
- package/dist/{invocation.js → src/invocation.js} +25 -24
- package/dist/src/invocation.js.map +1 -0
- package/dist/{io → src/io}/decoder.d.ts +3 -2
- package/dist/src/io/decoder.d.ts.map +1 -0
- package/dist/{io → src/io}/decoder.js +12 -10
- package/dist/src/io/decoder.js.map +1 -0
- package/dist/{io → src/io}/encoder.d.ts +1 -1
- package/dist/src/io/encoder.d.ts.map +1 -0
- package/dist/{io → src/io}/encoder.js +11 -21
- package/dist/src/io/encoder.js.map +1 -0
- package/dist/{journal.d.ts → src/journal.d.ts} +7 -7
- package/dist/src/journal.d.ts.map +1 -0
- package/dist/{journal.js → src/journal.js} +64 -46
- package/dist/src/journal.js.map +1 -0
- package/dist/{local_state_store.d.ts → src/local_state_store.d.ts} +2 -2
- package/dist/src/local_state_store.d.ts.map +1 -0
- package/dist/{local_state_store.js → src/local_state_store.js} +19 -17
- package/dist/src/local_state_store.js.map +1 -0
- package/dist/{logger.d.ts → src/logger.d.ts} +9 -1
- package/dist/src/logger.d.ts.map +1 -0
- package/dist/src/logger.js +158 -0
- package/dist/src/logger.js.map +1 -0
- package/dist/src/promise_combinator_tracker.d.ts.map +1 -0
- package/dist/src/promise_combinator_tracker.js.map +1 -0
- package/dist/src/public_api.d.ts +7 -0
- package/dist/src/public_api.d.ts.map +1 -0
- package/dist/{public_api.js → src/public_api.js} +4 -14
- package/dist/src/public_api.js.map +1 -0
- package/dist/{state_machine.d.ts → src/state_machine.d.ts} +7 -4
- package/dist/src/state_machine.d.ts.map +1 -0
- package/dist/{state_machine.js → src/state_machine.js} +32 -34
- package/dist/src/state_machine.js.map +1 -0
- package/dist/src/types/components.d.ts +65 -0
- package/dist/src/types/components.d.ts.map +1 -0
- package/dist/src/types/components.js +163 -0
- package/dist/src/types/components.js.map +1 -0
- package/dist/src/types/discovery.d.ts +40 -0
- package/dist/src/types/discovery.d.ts.map +1 -0
- package/dist/src/types/discovery.js +29 -0
- package/dist/src/types/discovery.js.map +1 -0
- package/dist/src/types/errors.d.ts +53 -0
- package/dist/src/types/errors.d.ts.map +1 -0
- package/dist/src/types/errors.js +129 -0
- package/dist/src/types/errors.js.map +1 -0
- package/dist/src/types/protocol.d.ts +33 -0
- package/dist/src/types/protocol.d.ts.map +1 -0
- package/dist/{types → src/types}/protocol.js +52 -52
- package/dist/src/types/protocol.js.map +1 -0
- package/dist/src/types/rpc.d.ts +50 -0
- package/dist/src/types/rpc.d.ts.map +1 -0
- package/dist/src/types/rpc.js +38 -0
- package/dist/src/types/rpc.js.map +1 -0
- package/dist/{types → src/types}/types.d.ts +0 -9
- package/dist/src/types/types.d.ts.map +1 -0
- package/dist/{types → src/types}/types.js +4 -21
- package/dist/src/types/types.js.map +1 -0
- package/dist/src/user_agent.d.ts +2 -0
- package/dist/src/user_agent.d.ts.map +1 -0
- package/dist/src/user_agent.js +16 -0
- package/dist/src/user_agent.js.map +1 -0
- package/dist/src/utils/message_logger.d.ts +7 -0
- package/dist/src/utils/message_logger.d.ts.map +1 -0
- package/dist/{utils → src/utils}/message_logger.js +17 -26
- package/dist/src/utils/message_logger.js.map +1 -0
- package/dist/src/utils/promises.d.ts.map +1 -0
- package/dist/src/utils/promises.js.map +1 -0
- package/dist/{utils → src/utils}/rand.d.ts +1 -0
- package/dist/src/utils/rand.d.ts.map +1 -0
- package/dist/{utils → src/utils}/rand.js +7 -6
- package/dist/src/utils/rand.js.map +1 -0
- package/dist/src/utils/serde.d.ts +3 -0
- package/dist/src/utils/serde.d.ts.map +1 -0
- package/dist/src/utils/serde.js +24 -0
- package/dist/src/utils/serde.js.map +1 -0
- package/dist/{utils → src/utils}/utils.d.ts +0 -1
- package/dist/src/utils/utils.d.ts.map +1 -0
- package/dist/{utils → src/utils}/utils.js +36 -43
- package/dist/src/utils/utils.js.map +1 -0
- package/dist/{workflows → src/workflows}/workflow.d.ts +6 -6
- package/dist/src/workflows/workflow.d.ts.map +1 -0
- package/dist/{workflows → src/workflows}/workflow.js +11 -7
- package/dist/src/workflows/workflow.js.map +1 -0
- package/dist/src/workflows/workflow_state_service.d.ts +35 -0
- package/dist/src/workflows/workflow_state_service.d.ts.map +1 -0
- package/dist/{workflows → src/workflows}/workflow_state_service.js +11 -11
- package/dist/src/workflows/workflow_state_service.js.map +1 -0
- package/dist/{workflows → src/workflows}/workflow_wrapper_service.d.ts +2 -2
- package/dist/src/workflows/workflow_wrapper_service.d.ts.map +1 -0
- package/dist/{workflows → src/workflows}/workflow_wrapper_service.js +66 -45
- package/dist/src/workflows/workflow_wrapper_service.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +13 -16
- package/LICENSE +0 -21
- package/README.md +0 -154
- package/dist/clients/workflow_client.d.ts.map +0 -1
- package/dist/clients/workflow_client.js.map +0 -1
- package/dist/connection/buffered_connection.d.ts.map +0 -1
- package/dist/connection/buffered_connection.js.map +0 -1
- package/dist/connection/connection.d.ts.map +0 -1
- package/dist/connection/connection.js.map +0 -1
- package/dist/connection/embedded_connection.d.ts +0 -20
- package/dist/connection/embedded_connection.d.ts.map +0 -1
- package/dist/connection/embedded_connection.js +0 -59
- package/dist/connection/embedded_connection.js.map +0 -1
- package/dist/connection/http_connection.d.ts.map +0 -1
- package/dist/connection/http_connection.js.map +0 -1
- package/dist/connection/lambda_connection.d.ts.map +0 -1
- package/dist/connection/lambda_connection.js.map +0 -1
- package/dist/context.d.ts.map +0 -1
- package/dist/context.js.map +0 -1
- package/dist/context_impl.d.ts +0 -65
- package/dist/context_impl.d.ts.map +0 -1
- package/dist/context_impl.js.map +0 -1
- package/dist/embedded/api.d.ts +0 -27
- package/dist/embedded/api.d.ts.map +0 -1
- package/dist/embedded/api.js +0 -35
- package/dist/embedded/api.js.map +0 -1
- package/dist/embedded/handler.d.ts +0 -4
- package/dist/embedded/handler.d.ts.map +0 -1
- package/dist/embedded/handler.js +0 -26
- package/dist/embedded/handler.js.map +0 -1
- package/dist/embedded/http2_remote.d.ts +0 -11
- package/dist/embedded/http2_remote.d.ts.map +0 -1
- package/dist/embedded/http2_remote.js +0 -91
- package/dist/embedded/http2_remote.js.map +0 -1
- package/dist/embedded/invocation.d.ts +0 -5
- package/dist/embedded/invocation.d.ts.map +0 -1
- package/dist/embedded/invocation.js +0 -94
- package/dist/embedded/invocation.js.map +0 -1
- package/dist/endpoint/endpoint_impl.d.ts +0 -35
- package/dist/endpoint/endpoint_impl.d.ts.map +0 -1
- package/dist/endpoint/endpoint_impl.js +0 -405
- package/dist/endpoint/endpoint_impl.js.map +0 -1
- package/dist/endpoint/http2_handler.d.ts.map +0 -1
- package/dist/endpoint/http2_handler.js +0 -119
- package/dist/endpoint/http2_handler.js.map +0 -1
- package/dist/endpoint/lambda_handler.d.ts.map +0 -1
- package/dist/endpoint/lambda_handler.js.map +0 -1
- package/dist/endpoint.d.ts +0 -161
- package/dist/endpoint.d.ts.map +0 -1
- package/dist/endpoint.js.map +0 -1
- package/dist/generated/dev/restate/events.d.ts +0 -96
- package/dist/generated/dev/restate/events.d.ts.map +0 -1
- package/dist/generated/dev/restate/events.js +0 -371
- package/dist/generated/dev/restate/events.js.map +0 -1
- package/dist/generated/dev/restate/ext.d.ts +0 -91
- package/dist/generated/dev/restate/ext.d.ts.map +0 -1
- package/dist/generated/dev/restate/ext.js +0 -215
- package/dist/generated/dev/restate/ext.js.map +0 -1
- package/dist/generated/google/protobuf/descriptor.d.ts +0 -1204
- package/dist/generated/google/protobuf/descriptor.d.ts.map +0 -1
- package/dist/generated/google/protobuf/descriptor.js +0 -6676
- package/dist/generated/google/protobuf/descriptor.js.map +0 -1
- package/dist/generated/google/protobuf/empty.d.ts +0 -86
- package/dist/generated/google/protobuf/empty.d.ts.map +0 -1
- package/dist/generated/google/protobuf/empty.js +0 -107
- package/dist/generated/google/protobuf/empty.js.map +0 -1
- package/dist/generated/google/protobuf/struct.d.ts +0 -175
- package/dist/generated/google/protobuf/struct.d.ts.map +0 -1
- package/dist/generated/google/protobuf/struct.js +0 -754
- package/dist/generated/google/protobuf/struct.js.map +0 -1
- package/dist/generated/proto/discovery.d.ts +0 -105
- package/dist/generated/proto/discovery.d.ts.map +0 -1
- package/dist/generated/proto/discovery.js +0 -364
- package/dist/generated/proto/discovery.js.map +0 -1
- package/dist/generated/proto/dynrpc.d.ts +0 -150
- package/dist/generated/proto/dynrpc.d.ts.map +0 -1
- package/dist/generated/proto/dynrpc.js +0 -668
- package/dist/generated/proto/dynrpc.js.map +0 -1
- package/dist/generated/proto/javascript.d.ts +0 -110
- package/dist/generated/proto/javascript.d.ts.map +0 -1
- package/dist/generated/proto/javascript.js +0 -416
- package/dist/generated/proto/javascript.js.map +0 -1
- package/dist/generated/proto/protocol.d.ts +0 -450
- package/dist/generated/proto/protocol.d.ts.map +0 -1
- package/dist/generated/proto/protocol.js +0 -2641
- package/dist/generated/proto/protocol.js.map +0 -1
- package/dist/generated/proto/services.d.ts +0 -307
- package/dist/generated/proto/services.d.ts.map +0 -1
- package/dist/generated/proto/services.js +0 -1535
- package/dist/generated/proto/services.js.map +0 -1
- package/dist/generated/proto/test.d.ts +0 -116
- package/dist/generated/proto/test.d.ts.map +0 -1
- package/dist/generated/proto/test.js +0 -321
- package/dist/generated/proto/test.js.map +0 -1
- package/dist/invocation.d.ts.map +0 -1
- package/dist/invocation.js.map +0 -1
- package/dist/io/decoder.d.ts.map +0 -1
- package/dist/io/decoder.js.map +0 -1
- package/dist/io/encoder.d.ts.map +0 -1
- package/dist/io/encoder.js.map +0 -1
- package/dist/journal.d.ts.map +0 -1
- package/dist/journal.js.map +0 -1
- package/dist/local_state_store.d.ts.map +0 -1
- package/dist/local_state_store.js.map +0 -1
- package/dist/logger.d.ts.map +0 -1
- package/dist/logger.js +0 -90
- package/dist/logger.js.map +0 -1
- package/dist/promise_combinator_tracker.d.ts.map +0 -1
- package/dist/promise_combinator_tracker.js.map +0 -1
- package/dist/public_api.d.ts +0 -10
- package/dist/public_api.d.ts.map +0 -1
- package/dist/public_api.js.map +0 -1
- package/dist/state_machine.d.ts.map +0 -1
- package/dist/state_machine.js.map +0 -1
- package/dist/types/errors.d.ts +0 -188
- package/dist/types/errors.d.ts.map +0 -1
- package/dist/types/errors.js +0 -273
- package/dist/types/errors.js.map +0 -1
- package/dist/types/grpc.d.ts +0 -90
- package/dist/types/grpc.d.ts.map +0 -1
- package/dist/types/grpc.js +0 -81
- package/dist/types/grpc.js.map +0 -1
- package/dist/types/protocol.d.ts +0 -30
- package/dist/types/protocol.d.ts.map +0 -1
- package/dist/types/protocol.js.map +0 -1
- package/dist/types/router.d.ts +0 -33
- package/dist/types/router.d.ts.map +0 -1
- package/dist/types/router.js +0 -36
- package/dist/types/router.js.map +0 -1
- package/dist/types/types.d.ts.map +0 -1
- package/dist/types/types.js.map +0 -1
- package/dist/utils/assumptions.d.ts +0 -8
- package/dist/utils/assumptions.d.ts.map +0 -1
- package/dist/utils/assumptions.js +0 -101
- package/dist/utils/assumptions.js.map +0 -1
- package/dist/utils/message_logger.d.ts +0 -28
- package/dist/utils/message_logger.d.ts.map +0 -1
- package/dist/utils/message_logger.js.map +0 -1
- package/dist/utils/promises.d.ts.map +0 -1
- package/dist/utils/promises.js.map +0 -1
- package/dist/utils/public_utils.d.ts +0 -58
- package/dist/utils/public_utils.d.ts.map +0 -1
- package/dist/utils/public_utils.js +0 -49
- package/dist/utils/public_utils.js.map +0 -1
- package/dist/utils/rand.d.ts.map +0 -1
- package/dist/utils/rand.js.map +0 -1
- package/dist/utils/utils.d.ts.map +0 -1
- package/dist/utils/utils.js.map +0 -1
- package/dist/workflows/workflow.d.ts.map +0 -1
- package/dist/workflows/workflow.js.map +0 -1
- package/dist/workflows/workflow_state_service.d.ts +0 -35
- package/dist/workflows/workflow_state_service.d.ts.map +0 -1
- package/dist/workflows/workflow_state_service.js.map +0 -1
- package/dist/workflows/workflow_wrapper_service.d.ts.map +0 -1
- package/dist/workflows/workflow_wrapper_service.js.map +0 -1
- package/src/clients/workflow_client.ts +0 -290
- package/src/connection/buffered_connection.ts +0 -47
- package/src/connection/connection.ts +0 -34
- package/src/connection/embedded_connection.ts +0 -62
- package/src/connection/http_connection.ts +0 -228
- package/src/connection/lambda_connection.ts +0 -69
- package/src/context.ts +0 -633
- package/src/context_impl.ts +0 -721
- package/src/embedded/api.ts +0 -57
- package/src/embedded/handler.ts +0 -36
- package/src/embedded/http2_remote.ts +0 -103
- package/src/embedded/invocation.ts +0 -126
- package/src/endpoint/endpoint_impl.ts +0 -623
- package/src/endpoint/http2_handler.ts +0 -151
- package/src/endpoint/lambda_handler.ts +0 -181
- package/src/endpoint.ts +0 -187
- package/src/generated/dev/restate/events.ts +0 -430
- package/src/generated/dev/restate/ext.ts +0 -238
- package/src/generated/google/protobuf/descriptor.ts +0 -7889
- package/src/generated/google/protobuf/empty.ts +0 -150
- package/src/generated/google/protobuf/struct.ts +0 -878
- package/src/generated/proto/discovery.ts +0 -423
- package/src/generated/proto/dynrpc.ts +0 -768
- package/src/generated/proto/javascript.ts +0 -488
- package/src/generated/proto/protocol.ts +0 -3091
- package/src/generated/proto/services.ts +0 -1834
- package/src/generated/proto/test.ts +0 -387
- package/src/invocation.ts +0 -212
- package/src/io/decoder.ts +0 -171
- package/src/io/encoder.ts +0 -72
- package/src/journal.ts +0 -537
- package/src/local_state_store.ts +0 -94
- package/src/logger.ts +0 -121
- package/src/promise_combinator_tracker.ts +0 -191
- package/src/public_api.ts +0 -53
- package/src/state_machine.ts +0 -635
- package/src/types/errors.ts +0 -297
- package/src/types/grpc.ts +0 -97
- package/src/types/protocol.ts +0 -201
- package/src/types/router.ts +0 -118
- package/src/types/types.ts +0 -160
- package/src/utils/assumptions.ts +0 -131
- package/src/utils/message_logger.ts +0 -112
- package/src/utils/promises.ts +0 -118
- package/src/utils/public_utils.ts +0 -91
- package/src/utils/rand.ts +0 -142
- package/src/utils/utils.ts +0 -178
- package/src/workflows/workflow.ts +0 -178
- package/src/workflows/workflow_state_service.ts +0 -299
- package/src/workflows/workflow_wrapper_service.ts +0 -314
- /package/dist/{connection → src/connection}/buffered_connection.js +0 -0
- /package/dist/{connection → src/connection}/connection.d.ts +0 -0
- /package/dist/{connection → src/connection}/connection.js +0 -0
- /package/dist/{promise_combinator_tracker.d.ts → src/promise_combinator_tracker.d.ts} +0 -0
- /package/dist/{promise_combinator_tracker.js → src/promise_combinator_tracker.js} +0 -0
- /package/dist/{utils → src/utils}/promises.d.ts +0 -0
- /package/dist/{utils → src/utils}/promises.js +0 -0
|
@@ -1,299 +0,0 @@
|
|
|
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 * as restate from "../public_api";
|
|
13
|
-
import { LifecycleStatus, WorkflowStartResult } from "./workflow";
|
|
14
|
-
|
|
15
|
-
const LIFECYCLE_STATUS_STATE_NAME = "status";
|
|
16
|
-
const RESULT_STATE_NAME = "result";
|
|
17
|
-
const RESULT_LISTENERS_NAME = "result_listeners";
|
|
18
|
-
const PROMISE_STATE_PREFIX = "prom_s_";
|
|
19
|
-
const USER_STATE_PREFIX = "state_";
|
|
20
|
-
const PROMISE_AWAKEABLE_PREFIX = "prom_l_";
|
|
21
|
-
|
|
22
|
-
export type ValueOrError<T> = {
|
|
23
|
-
value?: T;
|
|
24
|
-
error?: string;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const workflowStateService = restate.keyedRouter({
|
|
28
|
-
startWorkflow: async (
|
|
29
|
-
ctx: restate.KeyedContext
|
|
30
|
-
): Promise<WorkflowStartResult> => {
|
|
31
|
-
const status =
|
|
32
|
-
(await ctx.get<LifecycleStatus>(LIFECYCLE_STATUS_STATE_NAME)) ??
|
|
33
|
-
LifecycleStatus.NOT_STARTED;
|
|
34
|
-
|
|
35
|
-
if (status !== LifecycleStatus.NOT_STARTED) {
|
|
36
|
-
return status === LifecycleStatus.RUNNING
|
|
37
|
-
? WorkflowStartResult.ALREADY_STARTED
|
|
38
|
-
: WorkflowStartResult.ALREADY_FINISHED;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
ctx.set(LIFECYCLE_STATUS_STATE_NAME, LifecycleStatus.RUNNING);
|
|
42
|
-
return WorkflowStartResult.STARTED;
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
finishOrFailWorkflow: async <R>(
|
|
46
|
-
ctx: restate.KeyedContext,
|
|
47
|
-
_workflowId: string,
|
|
48
|
-
result: ValueOrError<R>
|
|
49
|
-
): Promise<void> => {
|
|
50
|
-
if (result.error === undefined && result.value === undefined) {
|
|
51
|
-
throw new restate.TerminalError("Result is undefined");
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const status =
|
|
55
|
-
(await ctx.get<LifecycleStatus>(LIFECYCLE_STATUS_STATE_NAME)) ??
|
|
56
|
-
LifecycleStatus.NOT_STARTED;
|
|
57
|
-
|
|
58
|
-
if (status !== LifecycleStatus.RUNNING) {
|
|
59
|
-
// not sure this can ever happen, but we put this here defensively
|
|
60
|
-
throw new restate.TerminalError("Unexpected state: " + status);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const newStatus = result.error
|
|
64
|
-
? LifecycleStatus.FAILED
|
|
65
|
-
: LifecycleStatus.FINISHED;
|
|
66
|
-
ctx.set(LIFECYCLE_STATUS_STATE_NAME, newStatus);
|
|
67
|
-
|
|
68
|
-
await completePromise(
|
|
69
|
-
ctx,
|
|
70
|
-
RESULT_STATE_NAME,
|
|
71
|
-
RESULT_LISTENERS_NAME,
|
|
72
|
-
result
|
|
73
|
-
);
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
getStatus: async (ctx: restate.KeyedContext): Promise<LifecycleStatus> => {
|
|
77
|
-
return (
|
|
78
|
-
(await ctx.get<LifecycleStatus>(LIFECYCLE_STATUS_STATE_NAME)) ??
|
|
79
|
-
LifecycleStatus.NOT_STARTED
|
|
80
|
-
);
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
completePromise: async <T>(
|
|
84
|
-
ctx: restate.KeyedContext,
|
|
85
|
-
_workflowId: string,
|
|
86
|
-
req: { promiseName: string; completion: ValueOrError<T> }
|
|
87
|
-
): Promise<void> => {
|
|
88
|
-
// we don't accept writes after the workflow is done
|
|
89
|
-
if (!(await checkIfRunning(ctx))) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
await completePromise(
|
|
94
|
-
ctx,
|
|
95
|
-
PROMISE_STATE_PREFIX + req.promiseName,
|
|
96
|
-
PROMISE_AWAKEABLE_PREFIX + req.promiseName,
|
|
97
|
-
req.completion
|
|
98
|
-
);
|
|
99
|
-
},
|
|
100
|
-
|
|
101
|
-
peekPromise: async <T>(
|
|
102
|
-
ctx: restate.KeyedContext,
|
|
103
|
-
_workflowId: string,
|
|
104
|
-
req: { promiseName: string }
|
|
105
|
-
): Promise<ValueOrError<T> | null> => {
|
|
106
|
-
return peekPromise(ctx, PROMISE_STATE_PREFIX + req.promiseName);
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
subscribePromise: async <T>(
|
|
110
|
-
ctx: restate.KeyedContext,
|
|
111
|
-
_workflowId: string,
|
|
112
|
-
req: { promiseName: string; awkId: string }
|
|
113
|
-
): Promise<ValueOrError<T> | null> => {
|
|
114
|
-
return subscribePromise(
|
|
115
|
-
ctx,
|
|
116
|
-
PROMISE_STATE_PREFIX + req.promiseName,
|
|
117
|
-
PROMISE_AWAKEABLE_PREFIX + req.promiseName,
|
|
118
|
-
req.awkId
|
|
119
|
-
);
|
|
120
|
-
},
|
|
121
|
-
|
|
122
|
-
getResult: async <R>(
|
|
123
|
-
ctx: restate.KeyedContext
|
|
124
|
-
): Promise<ValueOrError<R> | null> => {
|
|
125
|
-
return peekPromise(ctx, RESULT_STATE_NAME);
|
|
126
|
-
},
|
|
127
|
-
|
|
128
|
-
subscribeResult: async <T>(
|
|
129
|
-
ctx: restate.KeyedContext,
|
|
130
|
-
workflowId: string,
|
|
131
|
-
awkId: string
|
|
132
|
-
): Promise<ValueOrError<T> | null> => {
|
|
133
|
-
const status =
|
|
134
|
-
(await ctx.get(LIFECYCLE_STATUS_STATE_NAME)) ??
|
|
135
|
-
LifecycleStatus.NOT_STARTED;
|
|
136
|
-
if (status === LifecycleStatus.NOT_STARTED) {
|
|
137
|
-
throw new restate.TerminalError(
|
|
138
|
-
`Workflow with id '${workflowId}' does not exist.`
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
return subscribePromise(
|
|
142
|
-
ctx,
|
|
143
|
-
RESULT_STATE_NAME,
|
|
144
|
-
RESULT_LISTENERS_NAME,
|
|
145
|
-
awkId
|
|
146
|
-
);
|
|
147
|
-
},
|
|
148
|
-
|
|
149
|
-
getState: async <T>(
|
|
150
|
-
ctx: restate.KeyedContext,
|
|
151
|
-
_workflowId: string,
|
|
152
|
-
stateName: string
|
|
153
|
-
): Promise<T | null> => {
|
|
154
|
-
return ctx.get(USER_STATE_PREFIX + stateName);
|
|
155
|
-
},
|
|
156
|
-
|
|
157
|
-
setState: async <T>(
|
|
158
|
-
ctx: restate.KeyedContext,
|
|
159
|
-
_workflowId: string,
|
|
160
|
-
request: { stateName: string; value: T }
|
|
161
|
-
): Promise<void> => {
|
|
162
|
-
if (!request?.stateName) {
|
|
163
|
-
throw new restate.TerminalError("missing state name");
|
|
164
|
-
}
|
|
165
|
-
if (request.value === undefined || request.value === null) {
|
|
166
|
-
throw new restate.TerminalError("invalid state value: " + request.value);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// if the workflow isn't running (any more) we don't accept state updates
|
|
170
|
-
// shouldn't be possible anyways (because only workflow method has access to writable state)
|
|
171
|
-
// but we are defensive here against API errors
|
|
172
|
-
if (!(await checkIfRunning(ctx))) {
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const stateName = USER_STATE_PREFIX + request.stateName;
|
|
177
|
-
|
|
178
|
-
ctx.set(stateName, request.value);
|
|
179
|
-
},
|
|
180
|
-
|
|
181
|
-
clearState: async (
|
|
182
|
-
ctx: restate.KeyedContext,
|
|
183
|
-
_workflowId: string,
|
|
184
|
-
stateName: string
|
|
185
|
-
): Promise<void> => {
|
|
186
|
-
ctx.clear(USER_STATE_PREFIX + stateName);
|
|
187
|
-
},
|
|
188
|
-
|
|
189
|
-
stateKeys: async (ctx: restate.KeyedContext): Promise<Array<string>> => {
|
|
190
|
-
return (await ctx.stateKeys()).filter((name) =>
|
|
191
|
-
name.startsWith(USER_STATE_PREFIX)
|
|
192
|
-
);
|
|
193
|
-
},
|
|
194
|
-
|
|
195
|
-
clearAllState: async (ctx: restate.KeyedContext): Promise<void> => {
|
|
196
|
-
const stateNames = (await ctx.stateKeys()).filter((name) =>
|
|
197
|
-
name.startsWith(USER_STATE_PREFIX)
|
|
198
|
-
);
|
|
199
|
-
for (const stateName of stateNames) {
|
|
200
|
-
ctx.clear(stateName);
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
|
-
|
|
204
|
-
dispose: async (ctx: restate.KeyedContext): Promise<void> => {
|
|
205
|
-
ctx.clearAll();
|
|
206
|
-
},
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
export type api = typeof workflowStateService;
|
|
210
|
-
|
|
211
|
-
// ----------------------------------------------------------------------------
|
|
212
|
-
|
|
213
|
-
async function completePromise<T>(
|
|
214
|
-
ctx: restate.KeyedContext,
|
|
215
|
-
stateName: string,
|
|
216
|
-
awakeableStateName: string,
|
|
217
|
-
completion: ValueOrError<T>
|
|
218
|
-
): Promise<ValueOrError<T>> {
|
|
219
|
-
if (completion.value !== undefined && completion.error !== undefined) {
|
|
220
|
-
throw new restate.TerminalError(
|
|
221
|
-
"Completion can only be either with value or with error"
|
|
222
|
-
);
|
|
223
|
-
}
|
|
224
|
-
if (completion.value !== undefined && completion.value === null) {
|
|
225
|
-
throw new restate.TerminalError("promise cannot be completed with null");
|
|
226
|
-
}
|
|
227
|
-
if (completion.error !== undefined && completion.error === null) {
|
|
228
|
-
throw new restate.TerminalError("promise cannot be rejected with null");
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const currVal = await ctx.get<ValueOrError<T>>(stateName);
|
|
232
|
-
if (currVal !== null) {
|
|
233
|
-
// promise already completed
|
|
234
|
-
return currVal;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// first completor
|
|
238
|
-
// (a) set state
|
|
239
|
-
ctx.set(stateName, completion);
|
|
240
|
-
|
|
241
|
-
// (b) complete awaiting awakeables
|
|
242
|
-
const listeners = (await ctx.get<string[]>(awakeableStateName)) ?? [];
|
|
243
|
-
listeners.forEach((awkId: string) => {
|
|
244
|
-
if (completion.error !== undefined) {
|
|
245
|
-
ctx.rejectAwakeable(awkId, completion.error);
|
|
246
|
-
} else {
|
|
247
|
-
ctx.resolveAwakeable(awkId, completion.value);
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
ctx.clear(awakeableStateName);
|
|
251
|
-
|
|
252
|
-
return completion;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
async function subscribePromise<T>(
|
|
256
|
-
ctx: restate.KeyedContext,
|
|
257
|
-
stateName: string,
|
|
258
|
-
awakeableStateName: string,
|
|
259
|
-
awakeableId: string
|
|
260
|
-
): Promise<ValueOrError<T> | null> {
|
|
261
|
-
const currVal = await ctx.get<ValueOrError<T>>(stateName);
|
|
262
|
-
|
|
263
|
-
// case (a), we have a value already
|
|
264
|
-
if (currVal !== null) {
|
|
265
|
-
if (currVal.error !== undefined) {
|
|
266
|
-
ctx.rejectAwakeable(awakeableId, currVal.error);
|
|
267
|
-
} else {
|
|
268
|
-
ctx.resolveAwakeable(awakeableId, currVal.value);
|
|
269
|
-
}
|
|
270
|
-
return currVal;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// case (b), we remember the awk Id and get when we have a value
|
|
274
|
-
// but only if the workflow is still running
|
|
275
|
-
if (!(await checkIfRunning(ctx))) {
|
|
276
|
-
const response = {
|
|
277
|
-
error: "Promised will never resolve because workflow is not running",
|
|
278
|
-
};
|
|
279
|
-
ctx.rejectAwakeable(awakeableId, response.error);
|
|
280
|
-
return response;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
const listeners = (await ctx.get<string[]>(awakeableStateName)) ?? [];
|
|
284
|
-
listeners.push(awakeableId);
|
|
285
|
-
ctx.set(awakeableStateName, listeners);
|
|
286
|
-
return null;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
async function peekPromise<T>(
|
|
290
|
-
ctx: restate.KeyedContext,
|
|
291
|
-
stateName: string
|
|
292
|
-
): Promise<ValueOrError<T> | null> {
|
|
293
|
-
return ctx.get<ValueOrError<T>>(stateName);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
async function checkIfRunning(ctx: restate.KeyedContext): Promise<boolean> {
|
|
297
|
-
const status = await ctx.get<LifecycleStatus>(LIFECYCLE_STATUS_STATE_NAME);
|
|
298
|
-
return status === LifecycleStatus.RUNNING;
|
|
299
|
-
}
|
|
@@ -1,314 +0,0 @@
|
|
|
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 * as restate from "../public_api";
|
|
13
|
-
import * as wf from "./workflow";
|
|
14
|
-
import * as wss from "./workflow_state_service";
|
|
15
|
-
|
|
16
|
-
const DEFAULT_RETENTION_PERIOD = 7 * 24 * 60 * 60 * 1000; // 1 week
|
|
17
|
-
|
|
18
|
-
// ----------------------------------------------------------------------------
|
|
19
|
-
// Workflow Context Implementations
|
|
20
|
-
// ----------------------------------------------------------------------------
|
|
21
|
-
|
|
22
|
-
class SharedContextImpl implements wf.SharedWfContext {
|
|
23
|
-
constructor(
|
|
24
|
-
protected readonly ctx: restate.Context,
|
|
25
|
-
protected readonly wfId: string,
|
|
26
|
-
protected readonly stateServiceApi: restate.ServiceApi<wss.api>
|
|
27
|
-
) {}
|
|
28
|
-
|
|
29
|
-
workflowId(): string {
|
|
30
|
-
return this.wfId;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
get<T>(stateName: string): Promise<T | null> {
|
|
34
|
-
return this.ctx
|
|
35
|
-
.rpc(this.stateServiceApi)
|
|
36
|
-
.getState(this.wfId, stateName) as Promise<T | null>;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
promise<T = void>(name: string): wf.DurablePromise<T> {
|
|
40
|
-
// Create the awakeable to complete
|
|
41
|
-
const awk = this.ctx.awakeable<T>();
|
|
42
|
-
this.ctx.send(this.stateServiceApi).subscribePromise(this.wfId, {
|
|
43
|
-
promiseName: name,
|
|
44
|
-
awkId: awk.id,
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// Prepare implementation of DurablePromise
|
|
48
|
-
|
|
49
|
-
const peek = async (): Promise<T | null> => {
|
|
50
|
-
const result = await this.ctx
|
|
51
|
-
.rpc(this.stateServiceApi)
|
|
52
|
-
.peekPromise(this.wfId, { promiseName: name });
|
|
53
|
-
|
|
54
|
-
if (result === null) {
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
if (result.error !== undefined) {
|
|
58
|
-
return Promise.reject(new Error(result.error));
|
|
59
|
-
}
|
|
60
|
-
return Promise.resolve<T>(result.value as T);
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const resolve = (value: T) => {
|
|
64
|
-
const currentValue = value === undefined ? null : value;
|
|
65
|
-
|
|
66
|
-
this.ctx.send(this.stateServiceApi).completePromise(this.wfId, {
|
|
67
|
-
promiseName: name,
|
|
68
|
-
completion: { value: currentValue },
|
|
69
|
-
});
|
|
70
|
-
};
|
|
71
|
-
|
|
72
|
-
const reject = (errorMsg: string) => {
|
|
73
|
-
this.ctx.send(this.stateServiceApi).completePromise(this.wfId, {
|
|
74
|
-
promiseName: name,
|
|
75
|
-
completion: { error: errorMsg },
|
|
76
|
-
});
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
return Object.defineProperties(awk.promise, {
|
|
80
|
-
peek: {
|
|
81
|
-
value: peek.bind(this),
|
|
82
|
-
},
|
|
83
|
-
resolve: {
|
|
84
|
-
value: resolve.bind(this),
|
|
85
|
-
},
|
|
86
|
-
reject: {
|
|
87
|
-
value: reject.bind(this),
|
|
88
|
-
},
|
|
89
|
-
}) as wf.DurablePromise<T>;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
class ExclusiveContextImpl extends SharedContextImpl implements wf.WfContext {
|
|
94
|
-
public readonly id: Buffer;
|
|
95
|
-
public readonly serviceName: string;
|
|
96
|
-
public readonly rand: restate.Rand;
|
|
97
|
-
public readonly console: Console;
|
|
98
|
-
|
|
99
|
-
constructor(
|
|
100
|
-
ctx: restate.Context,
|
|
101
|
-
wfId: string,
|
|
102
|
-
stateServiceApi: restate.ServiceApi<wss.api>
|
|
103
|
-
) {
|
|
104
|
-
super(ctx, wfId, stateServiceApi);
|
|
105
|
-
this.id = ctx.id;
|
|
106
|
-
this.serviceName = ctx.serviceName;
|
|
107
|
-
this.rand = ctx.rand;
|
|
108
|
-
this.console = ctx.console;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
grpcChannel(): restate.RestateGrpcChannel {
|
|
112
|
-
return this.ctx.grpcChannel();
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
set<T>(stateName: string, value: T): void {
|
|
116
|
-
if (value === undefined || value === null) {
|
|
117
|
-
throw new restate.TerminalError("Cannot set state to null or undefined");
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
this.ctx
|
|
121
|
-
.send(this.stateServiceApi)
|
|
122
|
-
.setState(this.wfId, { stateName, value });
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
clear(stateName: string): void {
|
|
126
|
-
this.ctx.send(this.stateServiceApi).clearState(this.wfId, stateName);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
stateKeys(): Promise<Array<string>> {
|
|
130
|
-
return this.ctx.rpc(this.stateServiceApi).stateKeys(this.wfId);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
clearAll(): void {
|
|
134
|
-
this.ctx.send(this.stateServiceApi).clearAllState(this.wfId);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
sideEffect<T>(
|
|
138
|
-
fn: () => Promise<T>,
|
|
139
|
-
retryPolicy?: restate.RestateUtils.RetrySettings
|
|
140
|
-
): Promise<T> {
|
|
141
|
-
return this.ctx.sideEffect(fn, retryPolicy);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
awakeable<T>(): { id: string; promise: restate.CombineablePromise<T> } {
|
|
145
|
-
return this.ctx.awakeable();
|
|
146
|
-
}
|
|
147
|
-
resolveAwakeable<T>(id: string, payload: T): void {
|
|
148
|
-
this.ctx.resolveAwakeable(id, payload);
|
|
149
|
-
}
|
|
150
|
-
rejectAwakeable(id: string, reason: string): void {
|
|
151
|
-
this.ctx.rejectAwakeable(id, reason);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
sleep(millis: number): restate.CombineablePromise<void> {
|
|
155
|
-
return this.ctx.sleep(millis);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
rpc<M>(opts: restate.ServiceApi<M>): restate.Client<M> {
|
|
159
|
-
return this.ctx.rpc(opts);
|
|
160
|
-
}
|
|
161
|
-
send<M>(opts: restate.ServiceApi<M>): restate.SendClient<M> {
|
|
162
|
-
return this.ctx.send(opts);
|
|
163
|
-
}
|
|
164
|
-
sendDelayed<M>(
|
|
165
|
-
opts: restate.ServiceApi<M>,
|
|
166
|
-
delay: number
|
|
167
|
-
): restate.SendClient<M> {
|
|
168
|
-
return this.ctx.sendDelayed(opts, delay);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
// ----------------------------------------------------------------------------
|
|
173
|
-
// the service that wraps the workflow methods
|
|
174
|
-
// ----------------------------------------------------------------------------
|
|
175
|
-
|
|
176
|
-
export function createWrapperService<R, T, M>(
|
|
177
|
-
workflow: wf.Workflow<R, T, M>,
|
|
178
|
-
path: string,
|
|
179
|
-
stateServiceApi: restate.ServiceApi<wss.api>
|
|
180
|
-
) {
|
|
181
|
-
const wrapperService = {
|
|
182
|
-
submit: async (
|
|
183
|
-
ctx: restate.Context,
|
|
184
|
-
request: wf.WorkflowRequest<T>
|
|
185
|
-
): Promise<wf.WorkflowStartResult> => {
|
|
186
|
-
checkRequestAndWorkflowId(request);
|
|
187
|
-
|
|
188
|
-
const started = await ctx
|
|
189
|
-
.rpc(stateServiceApi)
|
|
190
|
-
.startWorkflow(request.workflowId);
|
|
191
|
-
if (started === wf.WorkflowStartResult.STARTED) {
|
|
192
|
-
ctx.send(wrapperServiceApi).run(request);
|
|
193
|
-
}
|
|
194
|
-
return started;
|
|
195
|
-
},
|
|
196
|
-
|
|
197
|
-
run: async (
|
|
198
|
-
ctx: restate.Context,
|
|
199
|
-
request: wf.WorkflowRequest<T>
|
|
200
|
-
): Promise<R> => {
|
|
201
|
-
checkRequestAndWorkflowId(request);
|
|
202
|
-
|
|
203
|
-
const wfCtx = new ExclusiveContextImpl(
|
|
204
|
-
ctx,
|
|
205
|
-
request.workflowId,
|
|
206
|
-
stateServiceApi
|
|
207
|
-
);
|
|
208
|
-
try {
|
|
209
|
-
const result = await workflow.run(wfCtx, request);
|
|
210
|
-
const resultValue = result !== undefined ? result : {};
|
|
211
|
-
await ctx
|
|
212
|
-
.rpc(stateServiceApi)
|
|
213
|
-
.finishOrFailWorkflow(request.workflowId, { value: resultValue });
|
|
214
|
-
return result;
|
|
215
|
-
} catch (err) {
|
|
216
|
-
const msg = stringifyError(err);
|
|
217
|
-
await ctx
|
|
218
|
-
.rpc(stateServiceApi)
|
|
219
|
-
.finishOrFailWorkflow(request.workflowId, { error: msg });
|
|
220
|
-
throw err;
|
|
221
|
-
} finally {
|
|
222
|
-
ctx
|
|
223
|
-
.sendDelayed(stateServiceApi, DEFAULT_RETENTION_PERIOD)
|
|
224
|
-
.dispose(request.workflowId);
|
|
225
|
-
}
|
|
226
|
-
},
|
|
227
|
-
|
|
228
|
-
waitForResult: async (
|
|
229
|
-
ctx: restate.Context,
|
|
230
|
-
request: wf.WorkflowRequest<unknown>
|
|
231
|
-
): Promise<R> => {
|
|
232
|
-
checkRequestAndWorkflowId(request);
|
|
233
|
-
|
|
234
|
-
const awakeable = ctx.awakeable<R>();
|
|
235
|
-
await ctx
|
|
236
|
-
.rpc(stateServiceApi)
|
|
237
|
-
.subscribeResult(request.workflowId, awakeable.id);
|
|
238
|
-
return awakeable.promise;
|
|
239
|
-
},
|
|
240
|
-
|
|
241
|
-
status: async (
|
|
242
|
-
ctx: restate.Context,
|
|
243
|
-
request: wf.WorkflowRequest<unknown>
|
|
244
|
-
): Promise<wf.LifecycleStatus> => {
|
|
245
|
-
checkRequestAndWorkflowId(request);
|
|
246
|
-
return ctx.rpc(stateServiceApi).getStatus(request.workflowId);
|
|
247
|
-
},
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
// add all the interaction methods to the wrapper service
|
|
251
|
-
for (const [route, handler] of Object.entries(workflow)) {
|
|
252
|
-
if (typeof handler !== "function" || route === "run") {
|
|
253
|
-
continue;
|
|
254
|
-
}
|
|
255
|
-
if (handler.length < 1 || handler.length > 2) {
|
|
256
|
-
throw new Error(
|
|
257
|
-
"Workflow function does not conform to correct signature: must have at least one argument (SharedWfContext) and at most a second argument (the request parameter)"
|
|
258
|
-
);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const wrappingHandler = async <OUT, IN>(
|
|
262
|
-
ctx: restate.Context,
|
|
263
|
-
request: wf.WorkflowRequest<IN>
|
|
264
|
-
): Promise<OUT> => {
|
|
265
|
-
checkRequestAndWorkflowId(request);
|
|
266
|
-
const wfCtx = new SharedContextImpl(
|
|
267
|
-
ctx,
|
|
268
|
-
request.workflowId,
|
|
269
|
-
stateServiceApi
|
|
270
|
-
);
|
|
271
|
-
|
|
272
|
-
// impl. note: we need the extra cast to 'unknown', because the 'run' method is
|
|
273
|
-
// otherwise incompatible with the cast. we exclude that method in the filter above,
|
|
274
|
-
// but the compiler doesn't recognize that.
|
|
275
|
-
return (
|
|
276
|
-
handler as unknown as (ctx: wf.SharedWfContext, req: IN) => Promise<OUT>
|
|
277
|
-
)(wfCtx, request);
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
281
|
-
(wrapperService as any)[route] = wrappingHandler;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const wrapperServiceRouter = restate.router(wrapperService);
|
|
285
|
-
const wrapperServiceApi: restate.ServiceApi<typeof wrapperServiceRouter> = {
|
|
286
|
-
path,
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
return wrapperServiceRouter;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function checkRequestAndWorkflowId(request: wf.WorkflowRequest<unknown>): void {
|
|
293
|
-
if (request === undefined) {
|
|
294
|
-
throw new restate.TerminalError("Request parameter is undefined");
|
|
295
|
-
}
|
|
296
|
-
if (request.workflowId === undefined) {
|
|
297
|
-
throw new restate.TerminalError("Request is missing property 'workflowId'");
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function stringifyError(error: unknown): string {
|
|
302
|
-
if (typeof error === "string") {
|
|
303
|
-
return error;
|
|
304
|
-
}
|
|
305
|
-
if (error instanceof Error) {
|
|
306
|
-
const e = error as Error;
|
|
307
|
-
return `${e.name}: ${e.message}\nStack: ${e.stack}`;
|
|
308
|
-
}
|
|
309
|
-
try {
|
|
310
|
-
return JSON.stringify(error);
|
|
311
|
-
} catch (err) {
|
|
312
|
-
return "(cause not stringify-able)";
|
|
313
|
-
}
|
|
314
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|