@restatedev/sessions 0.9.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +131 -73
  2. package/dist/addon.d.ts +9 -0
  3. package/dist/addon.d.ts.map +1 -0
  4. package/dist/addon.js +20 -0
  5. package/dist/addon.js.map +1 -0
  6. package/dist/database.d.ts +13 -0
  7. package/dist/database.d.ts.map +1 -1
  8. package/dist/database.js +44 -17
  9. package/dist/database.js.map +1 -1
  10. package/dist/driver.d.ts +186 -0
  11. package/dist/driver.d.ts.map +1 -0
  12. package/dist/driver.js +554 -0
  13. package/dist/driver.js.map +1 -0
  14. package/dist/endpoint.d.ts +13 -0
  15. package/dist/endpoint.d.ts.map +1 -0
  16. package/dist/endpoint.js +186 -0
  17. package/dist/endpoint.js.map +1 -0
  18. package/dist/handler.d.ts +35 -0
  19. package/dist/handler.d.ts.map +1 -0
  20. package/dist/handler.js +218 -0
  21. package/dist/handler.js.map +1 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +2 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/internal.d.ts +4 -2
  26. package/dist/internal.d.ts.map +1 -1
  27. package/dist/internal.js +7 -3
  28. package/dist/internal.js.map +1 -1
  29. package/dist/object.d.ts +15 -2
  30. package/dist/object.d.ts.map +1 -1
  31. package/dist/object.js +51 -12
  32. package/dist/object.js.map +1 -1
  33. package/dist/serve.d.ts.map +1 -1
  34. package/dist/serve.js +5 -0
  35. package/dist/serve.js.map +1 -1
  36. package/dist/session.d.ts +32 -2
  37. package/dist/session.d.ts.map +1 -1
  38. package/dist/session.js +59 -9
  39. package/dist/session.js.map +1 -1
  40. package/dist/vm.d.ts +151 -0
  41. package/dist/vm.d.ts.map +1 -0
  42. package/dist/vm.js +28 -0
  43. package/dist/vm.js.map +1 -0
  44. package/package.json +4 -4
  45. package/src/addon.ts +29 -0
  46. package/src/database.ts +48 -19
  47. package/src/driver.ts +657 -0
  48. package/src/endpoint.ts +220 -0
  49. package/src/handler.ts +272 -0
  50. package/src/index.ts +2 -1
  51. package/src/internal.ts +7 -4
  52. package/src/object.ts +75 -13
  53. package/src/serve.ts +5 -0
  54. package/src/session.ts +77 -8
  55. package/src/vm.ts +195 -0
  56. package/dist/selector.d.ts +0 -129
  57. package/dist/selector.d.ts.map +0 -1
  58. package/dist/selector.js +0 -251
  59. package/dist/selector.js.map +0 -1
  60. package/src/selector.ts +0 -323
@@ -0,0 +1,186 @@
1
+ import * as restate from '@restatedev/restate-sdk';
2
+ import { type Vm } from './vm.ts';
3
+ /**
4
+ * Restate shut the attempt down: it is over, and a new attempt runs the
5
+ * handler from the start.
6
+ */
7
+ export declare class AttemptShutDown extends Error {
8
+ constructor();
9
+ }
10
+ /** The attempt failed, and Restate retries it. */
11
+ export declare class AttemptFailed extends Error {
12
+ constructor(message: string);
13
+ }
14
+ /** What the attempt is of: the target it was invoked on, and how it handles cancellation. */
15
+ export interface AttemptOptions {
16
+ service: string;
17
+ handler: string;
18
+ /** A view: it reads beside the handlers, and writes nothing. */
19
+ shared?: boolean;
20
+ /**
21
+ * The handler stops its own work at a cancellation (`cancelled(ctx)`), so
22
+ * the attempt does not fail its awaits or cancel its calls for it.
23
+ */
24
+ explicitCancellation?: boolean;
25
+ }
26
+ /**
27
+ * Drives one attempt over its VM: the input pump, the tick, and the promises
28
+ * of the actions it issued.
29
+ */
30
+ export declare class AttemptDriver {
31
+ private readonly vm;
32
+ private readonly write;
33
+ private readonly options;
34
+ /** Set once the attempt is over, to what ended it. */
35
+ private ended;
36
+ private readonly waiting;
37
+ private readonly onEnd;
38
+ private queued;
39
+ /** The calls this attempt made, by the handle of their result. */
40
+ private readonly calls;
41
+ /** Issuing the cancellations of the calls, which the handler's end waits for. */
42
+ private cancelling;
43
+ private cancelledResolve;
44
+ /**
45
+ * A cancellation that failed nothing the handler awaits, carried to the
46
+ * next thing it does await: a result of Restate's ({@link promise}), or a
47
+ * call of its database ({@link takeCarriedCancellation}). A retry of a
48
+ * cancelled invocation knows it was cancelled before the handler starts,
49
+ * and its first call has to fail all the same, rather than run to
50
+ * completion.
51
+ *
52
+ * There is one, and it is here. The addon once carried a second for the
53
+ * database's waits, and the two could not agree on who had delivered: a
54
+ * handler saw one cancellation twice.
55
+ */
56
+ private carriedCancellation;
57
+ private readonly cancelledPromise;
58
+ private heartbeats;
59
+ private readonly aborter;
60
+ /**
61
+ * The invocation's input. Taking it from the VM copies the body across the
62
+ * addon, and the key, the body and the request each want it.
63
+ */
64
+ private theInput;
65
+ constructor(vm: Vm, write: (chunk: Uint8Array) => void, options: AttemptOptions);
66
+ /** A chunk of the request body. */
67
+ notifyInput(chunk: Uint8Array): void;
68
+ /** The end of the request body: while the handler runs, Restate shut the attempt down. */
69
+ notifyInputClosed(): void;
70
+ isReadyToExecute(): boolean;
71
+ /**
72
+ * Schedule a tick from outside: what the VM calls after the database's store
73
+ * acts on it from the runtime's thread, so that what it wrote is written and
74
+ * what it waits for is routed to it.
75
+ */
76
+ scheduleTick(): void;
77
+ /** Schedule a tick, once per turn of the event loop. */
78
+ private schedule;
79
+ /** Write what the VM has buffered. A closed attempt still flushes its last words. */
80
+ private flush;
81
+ /**
82
+ * Write the VM's output, settle the promises of the actions that completed,
83
+ * and notice the end of the attempt.
84
+ */
85
+ private tick;
86
+ /** The attempt ended: fail what still waits, and tell the listeners. */
87
+ private end;
88
+ /** Whether the attempt is over, and with what. */
89
+ get over(): Error | undefined;
90
+ /** Call `listener` when the attempt ends; the returned function unregisters it. */
91
+ whenEnded(listener: () => void): () => void;
92
+ /**
93
+ * Restate cancelled the invocation. A handler with `explicitCancellation`
94
+ * stops its own work and nothing else happens; otherwise the calls in flight
95
+ * are cancelled and every await of Restate fails, as a cancelled
96
+ * invocation's do.
97
+ */
98
+ private cancelled;
99
+ /**
100
+ * Whether a cancellation is owed to the handler's next await, which the
101
+ * caller then delivers: the handler's call of its database, before it
102
+ * issues anything, so that a call that fails with the cancellation did
103
+ * nothing.
104
+ */
105
+ takeCarriedCancellation(): boolean;
106
+ /** Issuing the cancellations of the calls, if a cancellation started them. */
107
+ get cancellingCalls(): Promise<void> | undefined;
108
+ /** Resolves once a cancellation arrived, in this attempt or an earlier one. */
109
+ cancellation(): Promise<void>;
110
+ isCancelled(): boolean;
111
+ /**
112
+ * Cancel the calls this attempt made that have not answered; returns their
113
+ * ids. A call whose invocation id has not arrived yet is waited for: it was
114
+ * made, so Restate will answer with its id, and leaving it uncancelled would
115
+ * leave an invocation running that nothing waits for any more.
116
+ */
117
+ cancelPreviousCalls(): Promise<string[]>;
118
+ /** Call the VM with `act`, then schedule a tick. The attempt's end throws. */
119
+ private act;
120
+ /** The promise the notification of `handle` settles. */
121
+ private promise;
122
+ /**
123
+ * A fire-and-forget action, which an attempt that ended simply drops. The
124
+ * database's store calls these from the addon, where a throw would reach no
125
+ * `catch` of ours and bring the process down; and a write of an attempt that
126
+ * is over cannot be sent anyway, since a new attempt writes it again.
127
+ */
128
+ private tell;
129
+ /** An action and the promise of its result, in one. */
130
+ private issue;
131
+ /** A call: the promise of its result, and of its invocation id. */
132
+ call(target: {
133
+ service: string;
134
+ handler: string;
135
+ key?: string;
136
+ input: Uint8Array;
137
+ idempotencyKey?: string;
138
+ headers?: {
139
+ key: string;
140
+ value: string;
141
+ }[];
142
+ name?: string;
143
+ }): {
144
+ result: Promise<Uint8Array | null>;
145
+ invocationId: Promise<string>;
146
+ };
147
+ /** A send: the promise of the invocation id it was given. */
148
+ send(target: {
149
+ service: string;
150
+ handler: string;
151
+ key?: string;
152
+ input: Uint8Array;
153
+ delay?: number;
154
+ idempotencyKey?: string;
155
+ headers?: {
156
+ key: string;
157
+ value: string;
158
+ }[];
159
+ name?: string;
160
+ }): Promise<string>;
161
+ /** The result of an invocation, once it ends. */
162
+ attach(invocationId: string, name?: string): Promise<Uint8Array | null>;
163
+ cancel(invocationId: string, name?: string): void;
164
+ startTrace(name: string, parent?: number): number;
165
+ endTrace(trace: number, output: Uint8Array): void;
166
+ /** Start the heartbeats of a running handler. */
167
+ startHeartbeats(everyMs?: number): void;
168
+ private stopHeartbeats;
169
+ /** The invocation's input, read from the VM once. */
170
+ private input;
171
+ /** The invocation's key, once the VM is ready to execute. */
172
+ get key(): string;
173
+ /** The handler's input. */
174
+ get body(): Uint8Array;
175
+ /** The invocation's request, as a handler's `ctx.request` has it. */
176
+ request(attemptHeaders?: ReadonlyMap<string, string | string[] | undefined>): restate.Request;
177
+ /** The handler ended with its output. */
178
+ endSuccess(output: Uint8Array): void;
179
+ /** The handler ended with a terminal failure. */
180
+ endFailure(error: restate.TerminalError): void;
181
+ /** The attempt failed, and Restate retries it. */
182
+ fail(message: string, stacktrace?: string, retryAfterMs?: number): void;
183
+ /** End the attempt with `act`, unless it ended already. */
184
+ private last;
185
+ }
186
+ //# sourceMappingURL=driver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"driver.d.ts","sourceRoot":"","sources":["../src/driver.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,OAAO,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAc,KAAK,EAAE,EAAqD,MAAM,SAAS,CAAC;AAgBjG;;;GAGG;AACH,qBAAa,eAAgB,SAAQ,KAAK;IACxC,cAGC;CACF;AAED,kDAAkD;AAClD,qBAAa,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAO,EAAE,MAAM,EAG1B;CACF;AAuED,6FAA6F;AAC7F,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;;OAGG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC;AAED;;;GAGG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAK;IACxB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA8B;IACpD,OAAO,CAAC,QA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