@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 @@
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;IA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AC;IACvB,KAAK,IAAI,OAAO,CAAC;IAEjB,UAAU,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAAC;IACrC,UAAU,CAAC,OAAO,EAAE,SAAS,GAAG,IAAI,CAAC;IACrC,4EAA4E;IAC5E,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,eAAe,CAAC,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.9.0",
3
+ "version": "0.10.1",
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.9.0",
33
- "@restatedev/sessions-linux-arm64-gnu": "0.9.0",
34
- "@restatedev/sessions-linux-x64-gnu": "0.9.0"
32
+ "@restatedev/sessions-darwin-arm64": "0.10.1",
33
+ "@restatedev/sessions-linux-arm64-gnu": "0.10.1",
34
+ "@restatedev/sessions-linux-x64-gnu": "0.10.1"
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