@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
package/src/logger.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
13
|
+
/* eslint-disable no-console */
|
|
14
|
+
|
|
15
|
+
export class LoggerContext {
|
|
16
|
+
readonly fqMethodName: string;
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
readonly invocationId: string,
|
|
20
|
+
packageName: string,
|
|
21
|
+
serviceName: string,
|
|
22
|
+
methodName: string,
|
|
23
|
+
readonly additionalContext?: { [name: string]: string }
|
|
24
|
+
) {
|
|
25
|
+
this.fqMethodName = packageName
|
|
26
|
+
? `${packageName}.${serviceName}/${methodName}`
|
|
27
|
+
: `${serviceName}/${methodName}`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function formatLogPrefix(context?: LoggerContext): string {
|
|
32
|
+
if (context === undefined) {
|
|
33
|
+
return "[restate] ";
|
|
34
|
+
}
|
|
35
|
+
let prefix = `[restate] [${context.fqMethodName}][${context.invocationId}]`;
|
|
36
|
+
if (context.additionalContext !== undefined) {
|
|
37
|
+
for (const [k, v] of Object.entries(context.additionalContext)) {
|
|
38
|
+
prefix = prefix + `[${k}: ${v}]`;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return prefix;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function createRestateConsole(
|
|
45
|
+
context?: LoggerContext,
|
|
46
|
+
filter?: () => boolean
|
|
47
|
+
): Console {
|
|
48
|
+
const prefix = formatLogPrefix(context);
|
|
49
|
+
const restate_logger = Object.create(console);
|
|
50
|
+
|
|
51
|
+
const shouldLog: () => boolean = filter ?? (() => true);
|
|
52
|
+
|
|
53
|
+
restate_logger.log = (message?: any, ...optionalParams: any[]) => {
|
|
54
|
+
if (!shouldLog()) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
console.log(
|
|
58
|
+
prefix + `[${new Date().toISOString()}] LOG: ` + message,
|
|
59
|
+
...optionalParams
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
restate_logger.info = (message?: any, ...optionalParams: any[]) => {
|
|
64
|
+
if (!shouldLog()) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
console.info(
|
|
68
|
+
prefix + `[${new Date().toISOString()}] INFO: ` + message,
|
|
69
|
+
...optionalParams
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
restate_logger.warn = (message?: any, ...optionalParams: any[]) => {
|
|
74
|
+
if (!shouldLog()) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
console.warn(
|
|
78
|
+
prefix + `[${new Date().toISOString()}] WARN: ` + message,
|
|
79
|
+
...optionalParams
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
restate_logger.error = (message?: any, ...optionalParams: any[]) => {
|
|
84
|
+
if (!shouldLog()) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
console.error(
|
|
88
|
+
prefix + `[${new Date().toISOString()}] ERROR: ` + message,
|
|
89
|
+
...optionalParams
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
restate_logger.debug = (message?: any, ...optionalParams: any[]) => {
|
|
94
|
+
if (!shouldLog()) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
console.debug(
|
|
98
|
+
prefix + `[${new Date().toISOString()}] DEBUG: ` + message,
|
|
99
|
+
...optionalParams
|
|
100
|
+
);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
restate_logger.trace = (message?: any, ...optionalParams: any[]) => {
|
|
104
|
+
if (!shouldLog()) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
console.trace(
|
|
108
|
+
prefix + `[${new Date().toISOString()}] TRACE: ` + message,
|
|
109
|
+
...optionalParams
|
|
110
|
+
);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
return restate_logger;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* This is a simple console without contextual info.
|
|
118
|
+
*
|
|
119
|
+
* This should be used only in cases where no contextual info is available.
|
|
120
|
+
*/
|
|
121
|
+
export const rlog = createRestateConsole();
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CompletablePromise,
|
|
3
|
+
wrapDeeply,
|
|
4
|
+
WrappedPromise,
|
|
5
|
+
} from "./utils/promises";
|
|
6
|
+
|
|
7
|
+
export enum PromiseType {
|
|
8
|
+
JournalEntry,
|
|
9
|
+
// Combinator?,
|
|
10
|
+
// SideEffect?
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface PromiseId {
|
|
14
|
+
type: PromiseType;
|
|
15
|
+
id: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function newJournalEntryPromiseId(entryIndex: number): PromiseId {
|
|
19
|
+
return {
|
|
20
|
+
type: PromiseType.JournalEntry,
|
|
21
|
+
id: entryIndex,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Prepare a Promise combinator
|
|
27
|
+
*
|
|
28
|
+
* @param combinatorIndex the index of this combinator
|
|
29
|
+
* @param combinatorConstructor the function that creates the combinator promise, e.g. Promise.all/any/race/allSettled
|
|
30
|
+
* @param promises the promises given by the user, and the respective ids
|
|
31
|
+
* @param readReplayOrder the function to read the replay order
|
|
32
|
+
* @param onNewCompleted callback when a child entry is resolved
|
|
33
|
+
* @param onCombinatorResolved callback when the combinator is resolved
|
|
34
|
+
* @param onCombinatorReplayed callback when the combinator is replayed
|
|
35
|
+
*/
|
|
36
|
+
function preparePromiseCombinator(
|
|
37
|
+
combinatorIndex: number,
|
|
38
|
+
combinatorConstructor: (promises: PromiseLike<unknown>[]) => Promise<unknown>,
|
|
39
|
+
promises: Array<{ id: PromiseId; promise: Promise<unknown> }>,
|
|
40
|
+
readReplayOrder: (combinatorIndex: number) => PromiseId[] | undefined,
|
|
41
|
+
onNewCompleted: (combinatorIndex: number, promiseId: PromiseId) => void,
|
|
42
|
+
onCombinatorResolved: (combinatorIndex: number) => Promise<void>,
|
|
43
|
+
onCombinatorReplayed: (combinatorIndex: number) => void
|
|
44
|
+
): WrappedPromise<unknown> {
|
|
45
|
+
// Create the proxy promises and index them
|
|
46
|
+
const promisesWithProxyPromise = promises.map((v) => ({
|
|
47
|
+
id: v.id,
|
|
48
|
+
originalPromise: v.promise,
|
|
49
|
+
proxyPromise: new CompletablePromise<unknown>(),
|
|
50
|
+
}));
|
|
51
|
+
const promisesMap = new Map(
|
|
52
|
+
promisesWithProxyPromise.map((v) => [
|
|
53
|
+
// We need to define a key format for this map...
|
|
54
|
+
v.id.type.toString() + "-" + v.id.id.toString(),
|
|
55
|
+
{ originalPromise: v.originalPromise, proxyPromise: v.proxyPromise },
|
|
56
|
+
])
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
// Create the combinator using the proxy promises
|
|
60
|
+
const combinator = combinatorConstructor(
|
|
61
|
+
promisesWithProxyPromise.map((v) => v.proxyPromise.promise)
|
|
62
|
+
).finally(
|
|
63
|
+
async () =>
|
|
64
|
+
// Once the combinator is resolved, notify back.
|
|
65
|
+
await onCombinatorResolved(combinatorIndex)
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
return wrapDeeply(combinator, () => {
|
|
69
|
+
const replayOrder = readReplayOrder(combinatorIndex);
|
|
70
|
+
|
|
71
|
+
if (replayOrder === undefined) {
|
|
72
|
+
// We're in processing mode! We need to wire up original promises with proxy promises
|
|
73
|
+
for (const {
|
|
74
|
+
originalPromise,
|
|
75
|
+
proxyPromise,
|
|
76
|
+
id,
|
|
77
|
+
} of promisesWithProxyPromise) {
|
|
78
|
+
originalPromise
|
|
79
|
+
// This code works deterministically because the javascript runtime will enqueue
|
|
80
|
+
// the listeners of the proxy promise (which are mounted in Promise.all/any) in a single FIFO queue,
|
|
81
|
+
// so a subsequent resolve on another proxy promise can't overtake this one.
|
|
82
|
+
//
|
|
83
|
+
// Some resources:
|
|
84
|
+
// * https://stackoverflow.com/questions/38059284/why-does-javascript-promise-then-handler-run-after-other-code
|
|
85
|
+
// * https://262.ecma-international.org/6.0/#sec-jobs-and-job-queues
|
|
86
|
+
// * https://tr.javascript.info/microtask-queue
|
|
87
|
+
.then(
|
|
88
|
+
(v) => {
|
|
89
|
+
onNewCompleted(combinatorIndex, id);
|
|
90
|
+
proxyPromise.resolve(v);
|
|
91
|
+
},
|
|
92
|
+
(e) => {
|
|
93
|
+
onNewCompleted(combinatorIndex, id);
|
|
94
|
+
proxyPromise.reject(e);
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// We're in replay mode, Now follow the replayIndexes order.
|
|
102
|
+
onCombinatorReplayed(combinatorIndex);
|
|
103
|
+
for (const promiseId of replayOrder) {
|
|
104
|
+
// These are already completed, so once we set the then callback they will be immediately resolved.
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
106
|
+
const { originalPromise, proxyPromise } = promisesMap.get(
|
|
107
|
+
promiseId.type.toString() + "-" + promiseId.id.toString()
|
|
108
|
+
)!;
|
|
109
|
+
|
|
110
|
+
// Because this promise is already completed, promise.then will immediately enqueue in the promise microtask queue
|
|
111
|
+
// the handlers to execute.
|
|
112
|
+
// See the comment below for more details.
|
|
113
|
+
originalPromise.then(
|
|
114
|
+
(v) => proxyPromise.resolve(v),
|
|
115
|
+
(e) => proxyPromise.reject(e)
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* This class takes care of creating and managing deterministic promise combinators.
|
|
123
|
+
*
|
|
124
|
+
* It should be wired up to the journal/state machine methods to read and write entries.
|
|
125
|
+
*/
|
|
126
|
+
export class PromiseCombinatorTracker {
|
|
127
|
+
private nextCombinatorIndex = 0;
|
|
128
|
+
private pendingCombinators: Map<number, PromiseId[]> = new Map();
|
|
129
|
+
|
|
130
|
+
constructor(
|
|
131
|
+
private readonly readReplayOrder: (
|
|
132
|
+
combinatorIndex: number
|
|
133
|
+
) => PromiseId[] | undefined,
|
|
134
|
+
private readonly onWriteCombinatorOrder: (
|
|
135
|
+
combinatorIndex: number,
|
|
136
|
+
order: PromiseId[]
|
|
137
|
+
) => Promise<void>
|
|
138
|
+
) {}
|
|
139
|
+
|
|
140
|
+
public createCombinator(
|
|
141
|
+
combinatorConstructor: (
|
|
142
|
+
promises: PromiseLike<unknown>[]
|
|
143
|
+
) => Promise<unknown>,
|
|
144
|
+
promises: Array<{ id: PromiseId; promise: Promise<unknown> }>
|
|
145
|
+
): WrappedPromise<unknown> {
|
|
146
|
+
const combinatorIndex = this.nextCombinatorIndex;
|
|
147
|
+
this.nextCombinatorIndex++;
|
|
148
|
+
|
|
149
|
+
// Prepare combinator order
|
|
150
|
+
this.pendingCombinators.set(combinatorIndex, []);
|
|
151
|
+
|
|
152
|
+
return preparePromiseCombinator(
|
|
153
|
+
combinatorIndex,
|
|
154
|
+
combinatorConstructor,
|
|
155
|
+
promises,
|
|
156
|
+
this.readReplayOrder,
|
|
157
|
+
this.appendOrder.bind(this),
|
|
158
|
+
this.onCombinatorResolved.bind(this),
|
|
159
|
+
this.onCombinatorReplayed.bind(this)
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private appendOrder(idx: number, promiseId: PromiseId) {
|
|
164
|
+
const order = this.pendingCombinators.get(idx);
|
|
165
|
+
if (order === undefined) {
|
|
166
|
+
// The order was already published, nothing to do here.
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
order.push(promiseId);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private onCombinatorReplayed(idx: number) {
|
|
174
|
+
// This avoids republishing the order
|
|
175
|
+
this.pendingCombinators.delete(idx);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private async onCombinatorResolved(idx: number) {
|
|
179
|
+
const order = this.pendingCombinators.get(idx);
|
|
180
|
+
if (order === undefined) {
|
|
181
|
+
// It was already published
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// We don't need this list anymore.
|
|
186
|
+
this.pendingCombinators.delete(idx);
|
|
187
|
+
|
|
188
|
+
// Publish the combinator order
|
|
189
|
+
await this.onWriteCombinatorOrder(idx, order);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
export {
|
|
13
|
+
RestateContext,
|
|
14
|
+
Context,
|
|
15
|
+
KeyedContext,
|
|
16
|
+
useContext,
|
|
17
|
+
useKeyedContext,
|
|
18
|
+
ServiceApi,
|
|
19
|
+
CombineablePromise,
|
|
20
|
+
Rand,
|
|
21
|
+
RestateGrpcChannel,
|
|
22
|
+
} from "./context";
|
|
23
|
+
export {
|
|
24
|
+
router,
|
|
25
|
+
keyedRouter,
|
|
26
|
+
keyedEventHandler,
|
|
27
|
+
UnKeyedRouter,
|
|
28
|
+
KeyedRouter,
|
|
29
|
+
KeyedEventHandler,
|
|
30
|
+
Client,
|
|
31
|
+
SendClient,
|
|
32
|
+
} from "./types/router";
|
|
33
|
+
export {
|
|
34
|
+
endpoint,
|
|
35
|
+
ServiceBundle,
|
|
36
|
+
ServiceOpts,
|
|
37
|
+
RestateEndpoint,
|
|
38
|
+
} from "./endpoint";
|
|
39
|
+
export * as RestateUtils from "./utils/public_utils";
|
|
40
|
+
export {
|
|
41
|
+
ErrorCodes,
|
|
42
|
+
RestateError,
|
|
43
|
+
TerminalError,
|
|
44
|
+
TimeoutError,
|
|
45
|
+
} from "./types/errors";
|
|
46
|
+
export { Event } from "./types/types";
|
|
47
|
+
export {
|
|
48
|
+
RestateConnection,
|
|
49
|
+
connection,
|
|
50
|
+
RestateConnectionOptions,
|
|
51
|
+
} from "./embedded/api";
|
|
52
|
+
export * as workflow from "./workflows/workflow";
|
|
53
|
+
export * as clients from "./clients/workflow_client";
|