@restatedev/restate-sdk-gen 0.0.0 → 1.14.2
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 +21 -0
- package/README.md +115 -0
- package/dist/index.cjs +1129 -0
- package/dist/index.d.cts +269 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +269 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1078 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE
|
package/README.md
CHANGED
|
@@ -1 +1,116 @@
|
|
|
1
1
|
# @restatedev/restate-sdk-gen
|
|
2
|
+
|
|
3
|
+
A composable, generator-based DSL for [Restate](https://restate.dev/) workflows. Built around two user-visible concepts: **`Operation<T>`** (a lazy, one-shot description of work) and **`Future<T>`** (an eager, memoized handle to an eventual `T`).
|
|
4
|
+
|
|
5
|
+
For the rationale and internal architecture, see [`DESIGN.md`](./DESIGN.md). For user-facing patterns and a longer tour, see [`guide.md`](./guide.md).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @restatedev/restate-sdk @restatedev/restate-sdk-gen
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`@restatedev/restate-sdk` is a peer dependency — bring your own SDK version.
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import * as restate from "@restatedev/restate-sdk";
|
|
19
|
+
import { gen, execute, run, all } from "@restatedev/restate-sdk-gen";
|
|
20
|
+
|
|
21
|
+
const greeter = restate.service({
|
|
22
|
+
name: "greeter",
|
|
23
|
+
handlers: {
|
|
24
|
+
greet: async (ctx: restate.Context, name: string): Promise<string> =>
|
|
25
|
+
execute(
|
|
26
|
+
ctx,
|
|
27
|
+
gen(function* () {
|
|
28
|
+
const a = run(({ signal }) => fetchA(signal), { name: "a" });
|
|
29
|
+
const b = run(({ signal }) => fetchB(signal), { name: "b" });
|
|
30
|
+
const [aVal, bVal] = yield* all([a, b]);
|
|
31
|
+
return `${aVal}+${bVal} for ${name}`;
|
|
32
|
+
})
|
|
33
|
+
),
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
restate.endpoint().bind(greeter).listen();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`execute(ctx, op)` constructs a scheduler wired to the SDK and runs the
|
|
41
|
+
`Operation<T>`. The free-standing functions (`run`, `sleep`, `all`, …) inside
|
|
42
|
+
the generator body read the active scheduler from a synchronous current-fiber
|
|
43
|
+
slot — no `ops` parameter, no `AsyncLocalStorage`.
|
|
44
|
+
|
|
45
|
+
## Two tiers, one user-visible Future
|
|
46
|
+
|
|
47
|
+
- **`Operation<T>`** — lazy, one-shot. Constructed via `gen()` for user-authored
|
|
48
|
+
bodies, or by primitives that yield a marker the scheduler dispatches on.
|
|
49
|
+
`gen()` takes a *factory function* (`() => Generator<...>`), not a generator
|
|
50
|
+
instance — the type closes the reuse-after-exhausted trap.
|
|
51
|
+
- **`Future<T>`** — eager, memoized, reusable. Returned by `run`, `sleep`,
|
|
52
|
+
`awakeable`, etc. (journal-backed) and by `spawn` (routine-backed). Both
|
|
53
|
+
backings are indistinguishable to user code; combinators dispatch internally
|
|
54
|
+
to pick the cheapest implementation.
|
|
55
|
+
|
|
56
|
+
## Primitives
|
|
57
|
+
|
|
58
|
+
Imported directly from `@restatedev/restate-sdk-gen`:
|
|
59
|
+
|
|
60
|
+
- **`run(action, opts?)`** — journaled side effect. `action` is `(opts: { signal: AbortSignal }) => Promise<T>`. Pass `signal` into AbortSignal-aware APIs (`fetch(url, { signal })`) for cancellation hygiene. Journal-entry name comes from `opts.name` if given, otherwise from `action.name` (works for named functions and `const`-bound arrows). Retry policy via `opts.retry` (`{ maxAttempts, initialInterval, maxInterval, intervalFactor, maxDuration }`).
|
|
61
|
+
- **`sleep(duration)`** — journaled timer.
|
|
62
|
+
- **`awakeable<T>()`** — journaled awakeable; returns `{ id, promise: Future<T> }`.
|
|
63
|
+
- **`channel<T>()`** — single-shot in-memory `Channel<T>`.
|
|
64
|
+
- **`state<T>()` / `sharedState<T>()`** — typed key-value store.
|
|
65
|
+
- **`serviceClient` / `objectClient` / `workflowClient`** (+ `*SendClient`) — typed RPC into other Restate services.
|
|
66
|
+
- **`genericCall` / `genericSend`** — untyped RPC.
|
|
67
|
+
- **`cancel(invocationId)`** — cancel another invocation.
|
|
68
|
+
- **`workflowPromise(name)`** — workflow-bound durable promise.
|
|
69
|
+
|
|
70
|
+
## Combinators
|
|
71
|
+
|
|
72
|
+
- **`all(futures)`** — wait for every future, return their values in order. Heterogeneous-tuple typed (mirrors `Promise.all`).
|
|
73
|
+
- **`race(futures)`** — return the first to settle. Race losers continue running; their results are discarded.
|
|
74
|
+
- **`select({ tag1: future1, tag2: future2, ... })`** — Tokio/Go-style. Returns `{ tag, future }` of the winning branch; switch on `tag` and unwrap `future`.
|
|
75
|
+
- **`spawn(op)`** — register an `Operation` as a new routine; returns a `Future<T>` for its result.
|
|
76
|
+
|
|
77
|
+
Combinators have a fast path: when every input Future is journal-backed, they collapse to a single `RestatePromise.all/race`. Otherwise they fall back to a synthesized fiber. Same semantics either way.
|
|
78
|
+
|
|
79
|
+
## Cancellation
|
|
80
|
+
|
|
81
|
+
Invocation-level cancellation (from outside, via the SDK) is delivered as a `TerminalError` thrown by the next `yield*` boundary. Catch it to do cleanup; yield more journal work afterward and the next cancellation event is independent of the previous one — cancellation is **not sticky**.
|
|
82
|
+
|
|
83
|
+
Each `run` closure receives an `{ signal }` argument — an `AbortSignal` that aborts *before* the `TerminalError` fans out to parked routines. Plumb it into AbortSignal-aware APIs (`fetch(url, { signal })`) so in-flight syscalls cancel immediately instead of waiting for cancellation to surface at the next yield.
|
|
84
|
+
|
|
85
|
+
For routine-level "stop": use a `Channel<void>` plus `select({ work, stop: stop.receive })`. Per-routine cancellation primitives are deferred — see `DESIGN.md`.
|
|
86
|
+
|
|
87
|
+
## Repository layout
|
|
88
|
+
|
|
89
|
+
This package lives in the [`sdk-typescript`](https://github.com/restatedev/sdk-typescript) workspace. The library proper is in `src/`; auxiliary subdirectories live alongside but are not published:
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
packages/libs/restate-sdk-gen/
|
|
93
|
+
├── src/ # published library
|
|
94
|
+
├── test/ # vitest unit tests (227 tests / 22 files)
|
|
95
|
+
├── bench/ # vitest benchmarks
|
|
96
|
+
├── examples/tutorial/ # 6-tier tutorial; run with `pnpm start:tutorial`
|
|
97
|
+
├── e2e/ # testcontainers-based e2e; run with `pnpm test:e2e`
|
|
98
|
+
└── test-services/ # sdk-test-suite endpoint service
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Only `dist/` and `README.md` are published to npm.
|
|
102
|
+
|
|
103
|
+
## Development
|
|
104
|
+
|
|
105
|
+
From the workspace root:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
pnpm install
|
|
109
|
+
pnpm --filter @restatedev/restate-sdk-gen _test # unit tests
|
|
110
|
+
pnpm --filter @restatedev/restate-sdk-gen _build # build dist/
|
|
111
|
+
pnpm --filter @restatedev/restate-sdk-gen test:e2e # e2e (Docker required)
|
|
112
|
+
pnpm --filter @restatedev/restate-sdk-gen start:tutorial # boot the tutorial
|
|
113
|
+
pnpm --filter @restatedev/restate-sdk-gen bench # microbenchmarks
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
For the full architecture and design rationale, read [`DESIGN.md`](./DESIGN.md). For user-facing patterns, read [`guide.md`](./guide.md). For benchmark interpretation, read [`BENCHMARKS.md`](./BENCHMARKS.md).
|