@restatedev/sessions 0.11.2 → 0.12.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.
- package/README.md +330 -178
- package/dist/addon.d.ts +1 -0
- package/dist/addon.d.ts.map +1 -1
- package/dist/addon.js.map +1 -1
- package/dist/calls.d.ts +28 -28
- package/dist/calls.d.ts.map +1 -1
- package/dist/calls.js +25 -25
- package/dist/calls.js.map +1 -1
- package/dist/database.d.ts +90 -166
- package/dist/database.d.ts.map +1 -1
- package/dist/database.js +137 -249
- package/dist/database.js.map +1 -1
- package/dist/driver.d.ts +129 -126
- package/dist/driver.d.ts.map +1 -1
- package/dist/driver.js +337 -276
- package/dist/driver.js.map +1 -1
- package/dist/endpoint.d.ts +47 -5
- package/dist/endpoint.d.ts.map +1 -1
- package/dist/endpoint.js +175 -60
- package/dist/endpoint.js.map +1 -1
- package/dist/handler.d.ts +20 -17
- package/dist/handler.d.ts.map +1 -1
- package/dist/handler.js +161 -127
- package/dist/handler.js.map +1 -1
- package/dist/host.d.ts +29 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +2 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -26
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +11 -11
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +20 -16
- package/dist/internal.js.map +1 -1
- package/dist/link.d.ts +1 -2
- package/dist/link.d.ts.map +1 -1
- package/dist/link.js +4 -5
- package/dist/link.js.map +1 -1
- package/dist/log.d.ts +14 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +21 -0
- package/dist/log.js.map +1 -0
- package/dist/object.d.ts +217 -142
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +195 -241
- package/dist/object.js.map +1 -1
- package/dist/platform.d.ts +0 -1
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +4 -6
- package/dist/platform.js.map +1 -1
- package/dist/schema.d.ts +14 -22
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +31 -69
- package/dist/schema.js.map +1 -1
- package/dist/serve.d.ts +11 -17
- package/dist/serve.d.ts.map +1 -1
- package/dist/serve.js +21 -39
- package/dist/serve.js.map +1 -1
- package/dist/session.d.ts +58 -240
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +206 -532
- package/dist/session.js.map +1 -1
- package/dist/stop.d.ts +59 -0
- package/dist/stop.d.ts.map +1 -0
- package/dist/stop.js +220 -0
- package/dist/stop.js.map +1 -0
- package/dist/vm.d.ts +108 -29
- package/dist/vm.d.ts.map +1 -1
- package/dist/vm.js +43 -10
- package/dist/vm.js.map +1 -1
- package/package.json +4 -4
- package/src/addon.ts +1 -0
- package/src/calls.ts +51 -47
- package/src/database.ts +195 -339
- package/src/driver.ts +379 -315
- package/src/endpoint.ts +217 -62
- package/src/handler.ts +183 -164
- package/src/host.ts +28 -0
- package/src/index.ts +6 -26
- package/src/internal.ts +28 -39
- package/src/link.ts +4 -5
- package/src/log.ts +42 -0
- package/src/object.ts +379 -335
- package/src/platform.ts +4 -6
- package/src/schema.ts +37 -103
- package/src/serve.ts +20 -41
- package/src/session.ts +236 -629
- package/src/stop.ts +225 -0
- package/src/vm.ts +151 -55
- package/dist/scope.d.ts +0 -38
- package/dist/scope.d.ts.map +0 -1
- package/dist/scope.js +0 -55
- package/dist/scope.js.map +0 -1
- package/src/scope.ts +0 -78
package/src/platform.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
// The platforms
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// (.github/workflows/packages.yml); `pnpm build` builds this machine's.
|
|
1
|
+
// The platforms the addon ships for. Each is an optional dependency package
|
|
2
|
+
// (npm/<platform>, `@restatedev/sessions-<platform>`), and a package manager
|
|
3
|
+
// installs only the one matching `os`, `cpu` and `libc`. CI builds them
|
|
4
|
+
// (.github/workflows/packages.yml); `pnpm build` builds the one for this machine.
|
|
6
5
|
|
|
7
|
-
/** The platforms built, as their packages name them. */
|
|
8
6
|
export const PLATFORMS = ['linux-x64-gnu', 'linux-arm64-gnu', 'darwin-arm64'] as const;
|
|
9
7
|
|
|
10
8
|
/** This machine's platform, as a package of the addon would name it. */
|
package/src/schema.ts
CHANGED
|
@@ -1,37 +1,21 @@
|
|
|
1
|
-
// The schema of a session object
|
|
2
|
-
// same steps, in order, applied once
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// key
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
// A step is a statement, or an async function of the transaction (to move
|
|
15
|
-
// data, say). Steps are never changed or removed once they ran: a change is
|
|
16
|
-
// a step of its own.
|
|
17
|
-
//
|
|
18
|
-
// A view applies the steps its key's database lacks in its own session,
|
|
19
|
-
// which stores nothing, then reads: a key never written reads as
|
|
20
|
-
// empty tables. This repository's libraries (@restatedev/agents) keep their tables
|
|
21
|
-
// in a schema of their own name, which `schema.ensure(ctx.db)` applies where
|
|
22
|
-
// they use them, in any object (`ensureSchemas(ctx.db, [...])` for several at
|
|
23
|
-
// once, in one commit): internal, from @restatedev/sessions/internal.
|
|
24
|
-
import type { Connection, DatabaseReader, DatabaseWriter, ConnectionTransaction } from './database.ts';
|
|
1
|
+
// The schema of the databases of a session object. Every key's database is
|
|
2
|
+
// made by the same steps, in order, each applied once. The record `schema`
|
|
3
|
+
// holds how many steps of each schema the database has, committed with the
|
|
4
|
+
// steps, so a step added later (a column, an index) runs on each key's
|
|
5
|
+
// database when that key is next opened. A view applies missing steps only in
|
|
6
|
+
// its session, which stores nothing, so a key never written reads as empty
|
|
7
|
+
// tables. A library can keep its tables in a named `Schema` and apply it where
|
|
8
|
+
// it uses it with `schema.ensure(ctx.db)`, or `ensureSchemas` for several in
|
|
9
|
+
// one commit (from @restatedev/sessions/internal). The pi integration of
|
|
10
|
+
// @restatedev/agents uses none: pi-durable's storage runs its own migrations,
|
|
11
|
+
// and the integration creates its one table in pi's commits.
|
|
12
|
+
import { TerminalError } from '@restatedev/restate-sdk';
|
|
13
|
+
import { Connection, type DatabaseReader, type DatabaseWriter, type ConnectionTransaction } from './database.ts';
|
|
25
14
|
import { Link } from './link.ts';
|
|
26
15
|
|
|
27
|
-
/**
|
|
28
|
-
* The database's record of its schemas: how many steps of each it has, by
|
|
29
|
-
* name. One record, so that `reset` clears all of them, a library's this
|
|
30
|
-
* process never loaded too.
|
|
31
|
-
*/
|
|
32
16
|
const RECORD = 'schema';
|
|
33
17
|
|
|
34
|
-
/** How many steps of each schema the database has, by name. */
|
|
18
|
+
/** How many steps of each schema the database has, by schema name. */
|
|
35
19
|
type Applied = Record<string, number>;
|
|
36
20
|
|
|
37
21
|
async function appliedIn(db: Connection | ConnectionTransaction): Promise<Applied> {
|
|
@@ -39,10 +23,13 @@ async function appliedIn(db: Connection | ConnectionTransaction): Promise<Applie
|
|
|
39
23
|
return record.length === 0 ? {} : (JSON.parse(new TextDecoder().decode(record)) as Applied);
|
|
40
24
|
}
|
|
41
25
|
|
|
42
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* A statement, or an async function of the transaction (to move data). A step
|
|
28
|
+
* never changes or goes away once it ran: a change is a new step.
|
|
29
|
+
*/
|
|
43
30
|
export type Step = string | ((tx: DatabaseWriter) => Promise<void>);
|
|
44
31
|
|
|
45
|
-
/** A part of a database
|
|
32
|
+
/** A named part of a database schema: the `steps` that make it, in order. */
|
|
46
33
|
export class Schema {
|
|
47
34
|
readonly name: string;
|
|
48
35
|
readonly steps: readonly Step[];
|
|
@@ -53,9 +40,8 @@ export class Schema {
|
|
|
53
40
|
}
|
|
54
41
|
|
|
55
42
|
/**
|
|
56
|
-
* Apply the steps `db` lacks
|
|
57
|
-
*
|
|
58
|
-
* handler's are its own, vanishing with its session.
|
|
43
|
+
* Apply the steps that `db` lacks, in any object. On `ctx.db` this runs
|
|
44
|
+
* once per attempt. In a view the steps are never stored.
|
|
59
45
|
*/
|
|
60
46
|
ensure(db: DatabaseReader | Connection): Promise<void> {
|
|
61
47
|
return ensureSchemas(db, [this]);
|
|
@@ -63,35 +49,37 @@ export class Schema {
|
|
|
63
49
|
}
|
|
64
50
|
|
|
65
51
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
* first handler, say) gets them in one commit rather than one each.
|
|
52
|
+
* {@link Schema.ensure} for several schemas in one transaction, so a database
|
|
53
|
+
* that lacks several (on the first handler of a key) gets them in one commit.
|
|
69
54
|
*/
|
|
70
55
|
export function ensureSchemas(db: DatabaseReader | Connection, schemas: readonly Schema[]): Promise<void> {
|
|
71
56
|
const ensure = ensureOf(db);
|
|
72
|
-
|
|
57
|
+
if (ensure) return ensure(schemas);
|
|
58
|
+
if (db instanceof Connection) return apply(db, schemas);
|
|
59
|
+
// A transaction `tx`, for example, cannot commit the steps itself.
|
|
60
|
+
return Promise.reject(
|
|
61
|
+
new TerminalError("Schema.ensure takes a handler's ctx.db (or its reader), or a database of asDatabase"),
|
|
62
|
+
);
|
|
73
63
|
}
|
|
74
64
|
|
|
75
|
-
/** How a database applies schemas:
|
|
65
|
+
/** How a database applies schemas: `ctx.db`, its reader, or a database from `asDatabase`. */
|
|
76
66
|
type Ensure = (schemas: readonly Schema[]) => Promise<void>;
|
|
77
67
|
|
|
78
|
-
/** A database's {@link Ensure}. */
|
|
79
68
|
const ENSURE = new Link<Ensure>('Schema.ensure');
|
|
80
69
|
|
|
81
|
-
/** `db`,
|
|
70
|
+
/** `db`, marked to apply schemas with `ensure`. */
|
|
82
71
|
export const ensuring = <D extends object>(db: D, ensure: Ensure): D => ENSURE.on(db, ensure);
|
|
83
72
|
|
|
84
|
-
/** How `db` applies schemas, if
|
|
73
|
+
/** How `db` applies schemas, if this package made it. */
|
|
85
74
|
export const ensureOf = (db: object): Ensure | undefined => ENSURE.of(db);
|
|
86
75
|
|
|
87
|
-
/** Apply the steps of `schemas` that `db` lacks, in one transaction,
|
|
76
|
+
/** Apply the steps of `schemas` that `db` lacks, in one transaction, and record them. */
|
|
88
77
|
export async function apply(db: Connection, schemas: readonly Schema[], { reader = false } = {}) {
|
|
89
78
|
if (behind(await appliedIn(db), schemas).length === 0) return;
|
|
90
|
-
//
|
|
91
|
-
// have applied them.
|
|
79
|
+
// Check again in the transaction, which holds the session: an earlier call may have applied them.
|
|
92
80
|
await db.transaction(async (tx) => {
|
|
93
81
|
if (!reader) return applyIn(tx, schemas);
|
|
94
|
-
// A reader
|
|
82
|
+
// A reader session is query-only, except while applying schemas.
|
|
95
83
|
await tx.execute('PRAGMA query_only = 0');
|
|
96
84
|
try {
|
|
97
85
|
await applyIn(tx, schemas);
|
|
@@ -101,8 +89,7 @@ export async function apply(db: Connection, schemas: readonly Schema[], { reader
|
|
|
101
89
|
});
|
|
102
90
|
}
|
|
103
91
|
|
|
104
|
-
|
|
105
|
-
export async function applyIn(tx: ConnectionTransaction, schemas: readonly Schema[]) {
|
|
92
|
+
async function applyIn(tx: ConnectionTransaction, schemas: readonly Schema[]) {
|
|
106
93
|
const applied = await appliedIn(tx);
|
|
107
94
|
const lacking = behind(applied, schemas);
|
|
108
95
|
if (lacking.length === 0) return;
|
|
@@ -115,59 +102,6 @@ export async function applyIn(tx: ConnectionTransaction, schemas: readonly Schem
|
|
|
115
102
|
tx.setRecord(RECORD, new TextEncoder().encode(JSON.stringify(applied)));
|
|
116
103
|
}
|
|
117
104
|
|
|
118
|
-
/** The schemas
|
|
105
|
+
/** The schemas with steps missing from `applied`. */
|
|
119
106
|
const behind = (applied: Applied, schemas: readonly Schema[]) =>
|
|
120
107
|
schemas.filter((schema) => (applied[schema.name] ?? 0) < schema.steps.length);
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Empty the database in the transaction `tx`: drop every table, view, index
|
|
124
|
-
* and trigger, and forget every schema, then apply `schemas` again. Its
|
|
125
|
-
* other records (the outbox) stay. It commits with `tx`: what `tx` writes
|
|
126
|
-
* after it are the new database's first rows.
|
|
127
|
-
*/
|
|
128
|
-
export async function dropAll(tx: ConnectionTransaction, schemas: readonly Schema[]) {
|
|
129
|
-
const made = await tx.query<{ type: string; name: string }>(
|
|
130
|
-
"SELECT type, name FROM sqlite_schema WHERE substr(name, 1, 7) != 'sqlite_'",
|
|
131
|
-
);
|
|
132
|
-
// A trigger or view may name a table; an index goes with its table.
|
|
133
|
-
for (const type of ['trigger', 'view']) {
|
|
134
|
-
for (const { name } of made.filter((m) => m.type === type)) await drop(tx, type, name);
|
|
135
|
-
}
|
|
136
|
-
for (const name of await childrenFirst(
|
|
137
|
-
tx,
|
|
138
|
-
made.filter((m) => m.type === 'table').map((m) => m.name),
|
|
139
|
-
)) {
|
|
140
|
-
await drop(tx, 'table', name);
|
|
141
|
-
}
|
|
142
|
-
tx.setRecord(RECORD, new Uint8Array());
|
|
143
|
-
await applyIn(tx, schemas);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function drop(tx: ConnectionTransaction, type: string, name: string) {
|
|
147
|
-
return tx.execute(`DROP ${type.toUpperCase()} IF EXISTS "${name.replaceAll('"', '""')}"`);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* `tables`, each after every table whose foreign keys name it: with foreign
|
|
152
|
-
* keys on, dropping a table deletes its rows, which a child's rows may not
|
|
153
|
-
* outlive. Tables in a cycle stay in their order.
|
|
154
|
-
*/
|
|
155
|
-
async function childrenFirst(tx: ConnectionTransaction, tables: string[]): Promise<string[]> {
|
|
156
|
-
const parents = new Map<string, Set<string>>();
|
|
157
|
-
for (const name of tables) {
|
|
158
|
-
const keys = await tx.query<{ table: string }>('SELECT "table" FROM pragma_foreign_key_list(?)', [name]);
|
|
159
|
-
// SQL names are case-insensitive.
|
|
160
|
-
const own = name.toLowerCase();
|
|
161
|
-
parents.set(name, new Set(keys.map((k) => k.table.toLowerCase()).filter((t) => t !== own)));
|
|
162
|
-
}
|
|
163
|
-
const ordered: string[] = [];
|
|
164
|
-
let left = tables;
|
|
165
|
-
while (left.length > 0) {
|
|
166
|
-
const named = new Set(left.flatMap((name) => [...parents.get(name)!]));
|
|
167
|
-
const unnamed = left.filter((name) => !named.has(name.toLowerCase()));
|
|
168
|
-
const next = unnamed.length > 0 ? unnamed : left;
|
|
169
|
-
ordered.push(...next);
|
|
170
|
-
left = left.filter((name) => !next.includes(name));
|
|
171
|
-
}
|
|
172
|
-
return ordered;
|
|
173
|
-
}
|
package/src/serve.ts
CHANGED
|
@@ -1,31 +1,15 @@
|
|
|
1
|
-
// `serve`: the services of a program,
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
// A session object's invocations run on the explicit recovery VM whichever of
|
|
5
|
-
// these serves them, and so does an endpoint built by hand: `endpoint.ts`
|
|
6
|
-
// wraps the SDK's endpoint class, which every transport builds its request
|
|
7
|
-
// handler from.
|
|
1
|
+
// `serve`, `connect` and `served`: the services of a program, each once, or
|
|
2
|
+
// those RESTATE_SERVE_ONLY names. Session objects run on the VM however they
|
|
3
|
+
// are served, also from an endpoint built by hand (see endpoint.ts).
|
|
8
4
|
import * as restate from '@restatedev/restate-sdk';
|
|
9
5
|
import { isRemote } from './calls.ts';
|
|
10
6
|
|
|
11
7
|
type Service = restate.ServeOptions['services'][number];
|
|
12
8
|
|
|
13
|
-
/** Where a definition keeps the services it needs served beside it: see {@link needing}. */
|
|
14
|
-
const NEEDS = Symbol.for('restate-sessions.needs');
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* `definition`, which needs `services` served beside it: {@link serve}
|
|
18
|
-
* serves them too. For a library whose objects call one of their own (an
|
|
19
|
-
* agent's companion, say).
|
|
20
|
-
*/
|
|
21
|
-
export const needing = <D extends object>(definition: D, services: () => Service[]): D =>
|
|
22
|
-
Object.defineProperty(definition, NEEDS, { value: services });
|
|
23
|
-
|
|
24
9
|
/**
|
|
25
|
-
* Serve `services` on PORT (default 9080),
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* beside it: one program (or image), deployed in parts.
|
|
10
|
+
* Serve `services` on PORT (default 9080), each once. RESTATE_SERVE_ONLY,
|
|
11
|
+
* names separated by commas (`triage,planner`), serves only those of them:
|
|
12
|
+
* one program (or image), deployed in parts.
|
|
29
13
|
*
|
|
30
14
|
* await restate.serve(notes, orders);
|
|
31
15
|
*/
|
|
@@ -33,9 +17,10 @@ export const serve = (...services: Service[]) =>
|
|
|
33
17
|
restate.serve({ services: served(services, process.env.RESTATE_SERVE_ONLY), port: Number(process.env.PORT ?? 9080) });
|
|
34
18
|
|
|
35
19
|
/**
|
|
36
|
-
* Connect out to Restate
|
|
37
|
-
*
|
|
38
|
-
*
|
|
20
|
+
* Connect out to a Restate server instead of listening: the SDK `connect`,
|
|
21
|
+
* with the services as {@link served} lists them (the SDK would bind a remote
|
|
22
|
+
* handle too). The server must accept deployments that connect out, and, as
|
|
23
|
+
* for every session object, serve the explicit recovery protocol.
|
|
39
24
|
*
|
|
40
25
|
* restate.connect({ services: [notes, orders], name: 'notes-v1' });
|
|
41
26
|
*
|
|
@@ -47,27 +32,26 @@ export const serve = (...services: Service[]) =>
|
|
|
47
32
|
export const connect = (options: ConnectOptions) =>
|
|
48
33
|
restate.connect({ ...options, services: served(options.services, process.env.RESTATE_SERVE_ONLY) });
|
|
49
34
|
|
|
50
|
-
/**
|
|
35
|
+
/** The SDK `connect` options, with `services` passed through {@link served}. */
|
|
51
36
|
type ConnectOptions = Omit<Parameters<typeof restate.connect>[0], 'services'> & { services: Service[] };
|
|
52
37
|
|
|
53
38
|
/**
|
|
54
|
-
* What {@link serve} serves of `services`: each with
|
|
55
|
-
*
|
|
56
|
-
*
|
|
39
|
+
* What {@link serve} serves of `services`: each once; with `names`
|
|
40
|
+
* (separated by commas), only the services it names. For an endpoint of
|
|
41
|
+
* your own:
|
|
57
42
|
*
|
|
58
43
|
* const endpoint = restate.endpoint();
|
|
59
44
|
* for (const service of restate.served([notes, orders])) endpoint.bind(service);
|
|
60
45
|
*/
|
|
61
46
|
export function served(services: Service[], names?: string): Service[] {
|
|
62
|
-
//
|
|
63
|
-
// service with no handlers, over the real one: the collision, from inside.
|
|
47
|
+
// Binding a remote handle would register a service with no handlers over the real one.
|
|
64
48
|
const handles = services.filter(isRemote).map((s) => s.name);
|
|
65
49
|
if (handles.length > 0) {
|
|
66
50
|
throw new Error(
|
|
67
51
|
`${handles.join(', ')}: a remote handle names what another deployment serves, so it cannot be served here`,
|
|
68
52
|
);
|
|
69
53
|
}
|
|
70
|
-
const all =
|
|
54
|
+
const all = once(services);
|
|
71
55
|
const wanted = (names ?? '')
|
|
72
56
|
.split(',')
|
|
73
57
|
.map((name) => name.trim())
|
|
@@ -78,17 +62,12 @@ export function served(services: Service[], names?: string): Service[] {
|
|
|
78
62
|
if (unknown.length > 0) {
|
|
79
63
|
throw new Error(`RESTATE_SERVE_ONLY names ${unknown.join(', ')}: the services are ${known.join(', ')}`);
|
|
80
64
|
}
|
|
81
|
-
return
|
|
65
|
+
return all.filter((s) => wanted.includes(s.name));
|
|
82
66
|
}
|
|
83
67
|
|
|
84
|
-
/**
|
|
85
|
-
function
|
|
68
|
+
/** The first service of each name. */
|
|
69
|
+
function once(services: Service[]): Service[] {
|
|
86
70
|
const all = new Map<string, Service>();
|
|
87
|
-
const
|
|
88
|
-
if (all.has(service.name)) return;
|
|
89
|
-
all.set(service.name, service);
|
|
90
|
-
for (const needed of (service as { [NEEDS]?: () => Service[] })[NEEDS]?.() ?? []) add(needed);
|
|
91
|
-
};
|
|
92
|
-
services.forEach(add);
|
|
71
|
+
for (const service of services) if (!all.has(service.name)) all.set(service.name, service);
|
|
93
72
|
return [...all.values()];
|
|
94
73
|
}
|