@restatedev/sessions 0.8.0 → 0.10.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.
Files changed (62) hide show
  1. package/README.md +64 -31
  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 +549 -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 +1 -1
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +4 -2
  25. package/dist/index.js.map +1 -1
  26. package/dist/internal.d.ts +5 -3
  27. package/dist/internal.d.ts.map +1 -1
  28. package/dist/internal.js +8 -4
  29. package/dist/internal.js.map +1 -1
  30. package/dist/object.d.ts +15 -2
  31. package/dist/object.d.ts.map +1 -1
  32. package/dist/object.js +51 -12
  33. package/dist/object.js.map +1 -1
  34. package/dist/serve.d.ts +19 -26
  35. package/dist/serve.d.ts.map +1 -1
  36. package/dist/serve.js +21 -30
  37. package/dist/serve.js.map +1 -1
  38. package/dist/session.d.ts +32 -2
  39. package/dist/session.d.ts.map +1 -1
  40. package/dist/session.js +59 -9
  41. package/dist/session.js.map +1 -1
  42. package/dist/vm.d.ts +151 -0
  43. package/dist/vm.d.ts.map +1 -0
  44. package/dist/vm.js +28 -0
  45. package/dist/vm.js.map +1 -0
  46. package/package.json +4 -4
  47. package/src/addon.ts +29 -0
  48. package/src/database.ts +48 -19
  49. package/src/driver.ts +653 -0
  50. package/src/endpoint.ts +220 -0
  51. package/src/handler.ts +272 -0
  52. package/src/index.ts +4 -2
  53. package/src/internal.ts +8 -5
  54. package/src/object.ts +75 -13
  55. package/src/serve.ts +26 -39
  56. package/src/session.ts +77 -8
  57. package/src/vm.ts +195 -0
  58. package/dist/selector.d.ts +0 -129
  59. package/dist/selector.d.ts.map +0 -1
  60. package/dist/selector.js +0 -251
  61. package/dist/selector.js.map +0 -1
  62. package/src/selector.ts +0 -323
package/dist/vm.d.ts ADDED
@@ -0,0 +1,151 @@
1
+ /** A header of the request, or of a call. */
2
+ export interface VmHeader {
3
+ key: string;
4
+ value: string;
5
+ }
6
+ /** A terminal failure: what a handler throws, and what a failed action completes with. */
7
+ export interface VmFailure {
8
+ code: number;
9
+ message: string;
10
+ metadata: VmHeader[];
11
+ }
12
+ /** The response's head: written before the body. */
13
+ export interface VmResponseHead {
14
+ statusCode: number;
15
+ headers: VmHeader[];
16
+ }
17
+ /** The invocation's input, once the VM is ready to execute. */
18
+ export interface VmInput {
19
+ invocationId: string;
20
+ key: string;
21
+ headers: VmHeader[];
22
+ input: Buffer;
23
+ scope?: string;
24
+ limitKey?: string;
25
+ idempotencyKey?: string;
26
+ }
27
+ /** The handles of a call: its invocation id, and its result. */
28
+ export interface VmCallHandle {
29
+ invocationId: number;
30
+ result: number;
31
+ }
32
+ /** A ready result: `kind` says which field holds it. */
33
+ export type VmNotification = {
34
+ handle: number;
35
+ } & ({
36
+ kind: 'void';
37
+ } | {
38
+ kind: 'success';
39
+ bytes: Buffer;
40
+ } | {
41
+ kind: 'failure';
42
+ failure: VmFailure;
43
+ } | {
44
+ kind: 'stateKeys';
45
+ keys: string[];
46
+ } | {
47
+ kind: 'invocationId';
48
+ id: string;
49
+ });
50
+ /** Where the attempt is: `starting` until the input is in, then `running`, then `closed`. */
51
+ export type VmState = 'starting' | 'running' | 'closed';
52
+ /**
53
+ * The attempt's state machine. A command returns a handle, notified later
54
+ * through {@link Vm.takeNotification}; a fire-and-forget command with `ack`
55
+ * is notified once Restate stores it, and at once without.
56
+ */
57
+ export interface Vm {
58
+ /**
59
+ * What the VM calls, on this thread, after the addon acts on it from the
60
+ * runtime's — the database's store reading or writing the object's state.
61
+ * It is the driver's tick: it writes the output and settles what completed.
62
+ */
63
+ setScheduler(scheduler: () => void): void;
64
+ getResponseHead(): VmResponseHead;
65
+ notifyInput(buffer: Uint8Array): void;
66
+ /** The runtime closed the input stream. While the handler runs, this shuts the attempt down. */
67
+ notifyInputClosed(): void;
68
+ /** The bytes buffered so far for the response body. */
69
+ takeOutput(): Buffer;
70
+ isReadyToExecute(): boolean;
71
+ input(): VmInput;
72
+ /**
73
+ * The next ready result, in no particular order, or `null`. Throws once
74
+ * the attempt is closed: {@link isShutDown} tells a shutdown from a failure.
75
+ */
76
+ takeNotification(): VmNotification | null;
77
+ getState(key: string, name?: string): number;
78
+ getStateKeys(name?: string): number;
79
+ setState(key: string, buffer: Uint8Array, name?: string, ack?: boolean): number;
80
+ clearState(key: string, name?: string, ack?: boolean): number;
81
+ clearAllState(name?: string, ack?: boolean): number;
82
+ call(service: string, handler: string, buffer: Uint8Array, key?: string, idempotencyKey?: string, headers?: VmHeader[], name?: string): VmCallHandle;
83
+ /** Returns the handle of the invocation id. `delay` is in milliseconds. */
84
+ send(service: string, handler: string, buffer: Uint8Array, key?: string, delay?: number, idempotencyKey?: string, headers?: VmHeader[], name?: string): number;
85
+ attachInvocation(invocationId: string, name?: string): number;
86
+ cancelInvocation(invocationId: string, name?: string, ack?: boolean): number;
87
+ /** A trace, shown in Restate's UI as a run until it is ended. */
88
+ startTrace(name: string, parent?: number): number;
89
+ endTraceSuccess(trace: number, buffer: Uint8Array): void;
90
+ endTraceFailure(trace: number, failure: VmFailure): void;
91
+ /** Tells Restate the attempt makes progress, delaying its shutdown. */
92
+ heartbeat(): void;
93
+ /** Whether the invocation was cancelled, in this attempt or an earlier one. */
94
+ isCancelled(): boolean;
95
+ /**
96
+ * Fail what the database's store waits for, once the attempt is over and
97
+ * nothing will answer: its reads and acks live in the addon, so the driver
98
+ * cannot reject them itself.
99
+ */
100
+ endStateWaits(): void;
101
+ /**
102
+ * Let go of the scheduler. It holds the driver's tick, and so the driver and
103
+ * this VM: an attempt that kept it would never be freed.
104
+ */
105
+ clearScheduler(): void;
106
+ state(): VmState;
107
+ endSuccess(buffer: Uint8Array): void;
108
+ endFailure(failure: VmFailure): void;
109
+ /** Fails the attempt, for example with a retryable error of the handler. */
110
+ notifyError(message: string, stacktrace?: string, delayOverrideMs?: number): void;
111
+ }
112
+ interface VmConstructor {
113
+ /**
114
+ * `bidirectional` is false on a request/response transport (AWS Lambda,
115
+ * HTTP/1.1), which cannot serve such a handler: no response could reach
116
+ * it. The VM then fails the invocation at once, which pauses it.
117
+ */
118
+ new (headers: VmHeader[], bidirectional: boolean): Vm;
119
+ }
120
+ export declare const Vm: VmConstructor;
121
+ /**
122
+ * Restate's signature on a request, as the SDK verifies it before dispatch.
123
+ * This package runs a session object's invocation ahead of that handler, so it
124
+ * verifies the signature itself.
125
+ */
126
+ export interface Identity {
127
+ /**
128
+ * `null` if the headers carry a signature of `path` by one of the keys, and
129
+ * why not otherwise. It is `null` and not `undefined`: the addon answers
130
+ * `None`, which is how that crosses the boundary.
131
+ */
132
+ verify(path: string, headers: VmHeader[]): string | null;
133
+ }
134
+ interface IdentityConstructor {
135
+ /** The v1 public keys a deployment accepts (`publickeyv1_…`). */
136
+ new (keys: string[]): Identity;
137
+ }
138
+ export declare const Identity: IdentityConstructor;
139
+ /**
140
+ * What the database's store's waits fail with once the attempt is over, as
141
+ * the addon's errors carry it: the text crosses the boundary, the class does
142
+ * not (`native/src/vm.rs`).
143
+ */
144
+ export declare const STATE_WAIT_OVER = "restate-sessions: the attempt is over";
145
+ /**
146
+ * Whether `error` says the attempt was shut down (Restate closed the input),
147
+ * rather than failed. A new attempt runs the handler from the start.
148
+ */
149
+ export declare const isShutDown: (error: unknown) => boolean;
150
+ export {};
151
+ //# sourceMappingURL=vm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vm.d.ts","sourceRoot":"","sources":["../src/vm.ts"],"names":[],"mappings":"AAaA,6CAA6C;AAC7C,MAAM,WAAW,QAAQ;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACf;AAED,0FAA0F;AAC1F,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,QAAQ,EAAE,CAAC;CACtB;AAED,oDAAoD;AACpD,MAAM,WAAW,cAAc;IAC7B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,QAAQ,EAAE,CAAC;CACrB;AAED,+DAA+D;AAC/D,MAAM,WAAW,OAAO;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,QAAQ,EAAE,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,gEAAgE;AAChE,MAAM,WAAW,YAAY;IAC3B,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,wDAAwD;AACxD,MAAM,MAAM,cAAc,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,CAC9C;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAChB;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAClC;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,OAAO,EAAE,SAAS,CAAA;CAAE,GACvC;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,IAAI,EAAE,MAAM,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AAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACnF;AAED,UAAU,aAAa;IACrB;;;;OAIG;IACH,KAAK,OAAO,EAAE,QAAQ,EAAE,EAAE,aAAa,EAAE,OAAO,GAAG,EAAE,CAAC;CACvD;AAED,eAAO,MAAM,EAAE,eAAsC,CAAC;AAEtD;;;;GAIG;AACH,MAAM,WAAW,QAAQ;IACvB;;;;OAIG;IACH,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,MAAM,GAAG,IAAI,CAAC;CAC1D;AAED,UAAU,mBAAmB;IAC3B,iEAAiE;IACjE,KAAK,IAAI,EAAE,MAAM,EAAE,GAAG,QAAQ,CAAC;CAChC;AAED,eAAO,MAAM,QAAQ,qBAAwD,CAAC;AAE9E;;;;GAIG;AACH,eAAO,MAAM,eAAe,0CAA0C,CAAC;AAKvE;;;GAGG;AACH,eAAO,MAAM,UAAU,UAAW,OAAO,KAAG,OAAwE,CAAC"}
package/dist/vm.js ADDED
@@ -0,0 +1,28 @@
1
+ // The VM of a session object's attempt: shared-core's explicit recovery state
2
+ // machine, through the addon (`native/src/vm.rs`).
3
+ //
4
+ // It never replays a journal: every attempt runs the handler from the start
5
+ // and recovers from the state the earlier attempts stored. An action returns
6
+ // a handle, and its result comes back as a notification of that handle
7
+ // (`takeNotification`), which the driver settles a promise with. There is no
8
+ // `do_progress` and no single await point, so a handler awaits what it likes,
9
+ // with its own combinators.
10
+ //
11
+ // The VM is synchronous and does no I/O: every call only buffers output.
12
+ import { addon } from './addon.js';
13
+ export const Vm = addon.Vm;
14
+ export const Identity = addon.Identity;
15
+ /**
16
+ * What the database's store's waits fail with once the attempt is over, as
17
+ * the addon's errors carry it: the text crosses the boundary, the class does
18
+ * not (`native/src/vm.rs`).
19
+ */
20
+ export const STATE_WAIT_OVER = 'restate-sessions: the attempt is over';
21
+ /** The message of the error a closed attempt throws when Restate shut it down. */
22
+ const SHUT_DOWN = 'ATTEMPT_SHUT_DOWN';
23
+ /**
24
+ * Whether `error` says the attempt was shut down (Restate closed the input),
25
+ * rather than failed. A new attempt runs the handler from the start.
26
+ */
27
+ export const isShutDown = (error) => error instanceof Error && error.message.startsWith(SHUT_DOWN);
28
+ //# sourceMappingURL=vm.js.map
package/dist/vm.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vm.js","sourceRoot":"","sources":["../src/vm.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,mDAAmD;AACnD,EAAE;AACF,4EAA4E;AAC5E,6EAA6E;AAC7E,uEAAuE;AACvE,6EAA6E;AAC7E,8EAA8E;AAC9E,4BAA4B;AAC5B,EAAE;AACF,yEAAyE;AACzE,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAkJnC,MAAM,CAAC,MAAM,EAAE,GAAI,KAA+B,CAAC,EAAE,CAAC;AAqBtD,MAAM,CAAC,MAAM,QAAQ,GAAI,KAA2C,CAAC,QAAQ,CAAC;AAE9E;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,uCAAuC,CAAC;AAEvE,kFAAkF;AAClF,MAAM,SAAS,GAAG,mBAAmB,CAAC;AAEtC;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAc,EAAW,EAAE,CAAC,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@restatedev/sessions",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Session objects on Restate: virtual objects with a SQL database per key, kept in the object's state (ctx.db)",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -29,9 +29,9 @@
29
29
  "@restatedev/restate-sdk-core": "1.18.2"
30
30
  },
31
31
  "optionalDependencies": {
32
- "@restatedev/sessions-darwin-arm64": "0.8.0",
33
- "@restatedev/sessions-linux-arm64-gnu": "0.8.0",
34
- "@restatedev/sessions-linux-x64-gnu": "0.8.0"
32
+ "@restatedev/sessions-darwin-arm64": "0.10.0",
33
+ "@restatedev/sessions-linux-arm64-gnu": "0.10.0",
34
+ "@restatedev/sessions-linux-x64-gnu": "0.10.0"
35
35
  },
36
36
  "engines": {
37
37
  "node": ">=24"
package/src/addon.ts ADDED
@@ -0,0 +1,29 @@
1
+ // The addon (`native/`), loaded once: the database of a session
2
+ // (`database.ts`) and the VM of an attempt (`vm.ts`) both come from it.
3
+ import { createRequire } from 'node:module';
4
+ import { PLATFORMS, platform } from './platform.ts';
5
+
6
+ /** What the addon exports. Its types live with what uses them. */
7
+ interface Addon {
8
+ open(namespace: string, store: unknown, reader: boolean): Promise<unknown>;
9
+ Vm: unknown;
10
+ Identity: unknown;
11
+ }
12
+
13
+ /** The addon of this machine's platform, from its package (see platform.ts). */
14
+ function load(): Addon {
15
+ const machine = platform();
16
+ const name = `@restatedev/sessions-${machine}`;
17
+ try {
18
+ return createRequire(import.meta.url)(name) as Addon;
19
+ } catch (error) {
20
+ if ((error as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') throw error;
21
+ throw new Error(
22
+ `@restatedev/sessions found no addon for ${machine}, the package ${name}: it ships them for ` +
23
+ `${PLATFORMS.join(', ')}. In its repository, \`pnpm build\` builds this machine's.`,
24
+ { cause: error },
25
+ );
26
+ }
27
+ }
28
+
29
+ export const addon = load();
package/src/database.ts CHANGED
@@ -4,8 +4,9 @@
4
4
  // (`native/src/node.rs`); this module orders the calls and runs transactions.
5
5
  import { AsyncLocalStorage } from 'node:async_hooks';
6
6
  import { Buffer } from 'node:buffer';
7
- import { createRequire } from 'node:module';
8
- import { PLATFORMS, platform } from './platform.ts';
7
+ import { addon as loaded } from './addon.ts';
8
+ import { STATE_WAIT_OVER, type Vm } from './vm.ts';
9
+ import { AttemptShutDown } from './driver.ts';
9
10
 
10
11
  /** An SQL value. Integers come back as numbers, or as bigints beyond 2^53. */
11
12
  export type SqlValue = null | number | bigint | string | Uint8Array;
@@ -112,6 +113,7 @@ export interface Store {
112
113
  /** The addon (`native/src/node.rs`). */
113
114
  interface Addon {
114
115
  open(namespace: string, store: Store, reader: boolean): Promise<NativeSession>;
116
+ openOnVm(namespace: string, store: Store, vm: Vm, name: string | undefined, reader: boolean): Promise<NativeSession>;
115
117
  }
116
118
 
117
119
  /**
@@ -134,23 +136,7 @@ interface NativeSession {
134
136
  /** A value, or a promise of it: `await` takes either. */
135
137
  type Answer<T> = T | Promise<T>;
136
138
 
137
- const addon = load();
138
-
139
- /** The addon of this machine's platform, from its package (see platform.ts). */
140
- function load(): Addon {
141
- const machine = platform();
142
- const name = `@restatedev/sessions-${machine}`;
143
- try {
144
- return createRequire(import.meta.url)(name) as Addon;
145
- } catch (error) {
146
- if ((error as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') throw error;
147
- throw new Error(
148
- `@restatedev/sessions found no addon for ${machine}, the package ${name}: it ships them for ` +
149
- `${PLATFORMS.join(', ')}. In its repository, \`pnpm build\` builds this machine's.`,
150
- { cause: error },
151
- );
152
- }
153
- }
139
+ const addon = loaded as unknown as Addon;
154
140
 
155
141
  /**
156
142
  * Open the database of `namespace` over `store`. One process-wide page cache
@@ -166,6 +152,31 @@ export async function open(namespace: string, store: Store, { reader = false } =
166
152
  }
167
153
  }
168
154
 
155
+ /**
156
+ * Open the database of `namespace` over the attempt's VM: the state is read
157
+ * and written as the VM's own commands, in the addon, so none of it crosses
158
+ * into JavaScript. `store` is called at one point only — its `sync`, which the
159
+ * boundary uses to run what the committing transaction gave
160
+ * {@link ConnectionTransaction.withCommit} — so its `get`, `set` and `clear`
161
+ * are never reached. `name` names a database beside the object's own.
162
+ */
163
+ export async function openOnVm(
164
+ namespace: string,
165
+ store: Store,
166
+ vm: Vm,
167
+ { name, reader = false }: { name?: string; reader?: boolean } = {},
168
+ ): Promise<Connection> {
169
+ const boundary = new Boundary(store);
170
+ try {
171
+ return new Connection(await addon.openOnVm(namespace, boundary, vm, name, reader), boundary);
172
+ } catch (error) {
173
+ // The open reads the database's head. On this path that read waits in the
174
+ // addon, so its failure arrives as text and `boundary.failed` is never
175
+ // set: a cancellation here must still reach the handler as one.
176
+ throw stateWaitFailure(error) ?? boundary.failed?.error ?? error;
177
+ }
178
+ }
179
+
169
180
  /**
170
181
  * The store as the addon calls it. An error of the store's crosses the addon
171
182
  * as text, which ends the session: the first is kept here, and the calls that
@@ -275,6 +286,12 @@ abstract class Sql implements DatabaseWriter {
275
286
  */
276
287
  protected failure(error: unknown): unknown {
277
288
  if (error instanceof SqlError) return error;
289
+ // The state is read and written in the addon on the VM's path, so the
290
+ // end of the attempt fails those waits there and its text is all that
291
+ // crosses back. Give the handler the class the driver's own awaits fail
292
+ // with.
293
+ const stateWait = stateWaitFailure(error);
294
+ if (stateWait !== undefined) return stateWait;
278
295
  const failed = this.boundary.failed;
279
296
  if (failed !== undefined && !this.native.isUsable()) return failed.error;
280
297
  const repeats = (error as { code?: unknown }).code === REFUSED || this.native.isUsable();
@@ -286,6 +303,18 @@ abstract class Sql implements DatabaseWriter {
286
303
  // ended the session (its `Refused`), and of arguments it cannot convert.
287
304
  const REFUSED = 'InvalidArg';
288
305
 
306
+ /**
307
+ * The error of a call that failed because the attempt's state waits were
308
+ * failed in the addon, at the end of the attempt. `undefined` for anything
309
+ * else.
310
+ */
311
+ function stateWaitFailure(error: unknown): unknown {
312
+ const message = (error as { message?: unknown }).message;
313
+ if (typeof message !== 'string') return undefined;
314
+ if (message.includes(STATE_WAIT_OVER)) return new AttemptShutDown();
315
+ return undefined;
316
+ }
317
+
289
318
  /**
290
319
  * A session of a database: calls run one at a time, in call order, and a
291
320
  * transaction holds the session until it commits or rolls back. Calls made