@restatedev/sessions 0.5.0 → 0.7.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 +24 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +3 -3
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +3 -3
- package/dist/internal.js.map +1 -1
- package/dist/object.d.ts +36 -2
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +57 -2
- package/dist/object.js.map +1 -1
- package/dist/schema.d.ts +6 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +10 -2
- package/dist/schema.js.map +1 -1
- package/dist/selector.d.ts +58 -15
- package/dist/selector.d.ts.map +1 -1
- package/dist/selector.js +121 -66
- package/dist/selector.js.map +1 -1
- package/dist/serve.d.ts +12 -5
- package/dist/serve.d.ts.map +1 -1
- package/dist/serve.js +30 -22
- package/dist/serve.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +3 -2
- package/src/internal.ts +4 -2
- package/src/object.ts +66 -4
- package/src/schema.ts +13 -3
- package/src/selector.ts +144 -74
- package/src/serve.ts +29 -21
package/src/selector.ts
CHANGED
|
@@ -33,6 +33,13 @@ export type DoProgressResult =
|
|
|
33
33
|
| 'CancelSignalReceived'
|
|
34
34
|
| { EphemeralNotificationReady: number };
|
|
35
35
|
|
|
36
|
+
/** The handle of the invocation's cancel signal: the VM's, and what `ctx.cancellation()` awaits. */
|
|
37
|
+
const CANCELLATION = 1;
|
|
38
|
+
|
|
39
|
+
/** Whether `future` waits for the invocation's cancellation, and nothing else. */
|
|
40
|
+
const cancellationAlone = (future: UnresolvedFuture): boolean =>
|
|
41
|
+
'Single' in future ? future.Single === CANCELLATION : Object.values(future)[0]!.every(cancellationAlone);
|
|
42
|
+
|
|
36
43
|
/** What the selector needs of a promise: the shape of InternalRestatePromise. */
|
|
37
44
|
export interface Selectable {
|
|
38
45
|
/** Takes the result if it is ready, and settles the promise with it. */
|
|
@@ -45,6 +52,12 @@ export interface Selectable {
|
|
|
45
52
|
|
|
46
53
|
/** What the selector needs of the invocation. */
|
|
47
54
|
export interface SelectorDriver {
|
|
55
|
+
/**
|
|
56
|
+
* Asks the VM for progress on `future`. With nothing ready, the VM tells
|
|
57
|
+
* Restate what the handler waits for, unless a run is executing; and once
|
|
58
|
+
* the input closed, it suspends the invocation on it (a throw), unless a
|
|
59
|
+
* run of the future is executing.
|
|
60
|
+
*/
|
|
48
61
|
doProgress(future: UnresolvedFuture): DoProgressResult;
|
|
49
62
|
/** Writes out what the VM has buffered. */
|
|
50
63
|
flushOutput(): Promise<void>;
|
|
@@ -52,36 +65,71 @@ export interface SelectorDriver {
|
|
|
52
65
|
awaitExternalProgress(): Promise<void>;
|
|
53
66
|
/** Signals external progress. */
|
|
54
67
|
wake(): void;
|
|
68
|
+
/**
|
|
69
|
+
* The handle of the attempt's working run, made at the first call: a run
|
|
70
|
+
* that the VM hands out like any other (`ExecuteRun`) and that never ends
|
|
71
|
+
* while the attempt lasts. It stands for the handler's work outside Restate.
|
|
72
|
+
*/
|
|
73
|
+
work(): number;
|
|
55
74
|
executeRun(handle: number): void;
|
|
56
|
-
completeEphemeral(completionId: number): Promise<void>;
|
|
57
75
|
/** The attempt cannot go on: suspension, or an error. */
|
|
58
76
|
onError(e: unknown): void;
|
|
59
77
|
}
|
|
60
78
|
|
|
79
|
+
/**
|
|
80
|
+
* The points of the loop where a test steps in: `pass`, as a pass starts,
|
|
81
|
+
* and `wait`, between a check that found nothing and the wait for progress.
|
|
82
|
+
*/
|
|
83
|
+
export type Failpoint = 'pass' | 'wait';
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* For tests: what every selector's loop awaits at each of its failpoints, to
|
|
87
|
+
* hold it there while something else happens (the input closes, a signal
|
|
88
|
+
* comes). At `wait`, it gets what the check asked for, and whether that was
|
|
89
|
+
* the invocation's cancellation alone (with the working run). Empty
|
|
90
|
+
* otherwise, and the loop then awaits nothing there.
|
|
91
|
+
*/
|
|
92
|
+
export const failpoints = new Map<Failpoint, (asked?: UnresolvedFuture, watching?: boolean) => unknown>();
|
|
93
|
+
|
|
61
94
|
/**
|
|
62
95
|
* One loop that drives every pending promise of an invocation, so that the
|
|
63
96
|
* invocation has a single await point however many promises user code awaits
|
|
64
97
|
* at once, with its own combinators or none.
|
|
65
98
|
*
|
|
66
|
-
* A promise is registered once and stays until it settles.
|
|
67
|
-
*
|
|
68
|
-
* pending promises to complete: so a suspension waits for any of
|
|
69
|
-
* pending run starts whichever promise is awaited first, and a
|
|
70
|
-
* reaches all of them. Registering wakes the loop, so a promise
|
|
71
|
-
* while it waits is part of the next
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
99
|
+
* A promise is registered once and stays until it settles. Each pass of the
|
|
100
|
+
* loop settles what is ready, then asks the VM for progress on the first of
|
|
101
|
+
* all the pending promises to complete: so a suspension waits for any of
|
|
102
|
+
* them, a pending run starts whichever promise is awaited first, and a
|
|
103
|
+
* cancellation reaches all of them. Registering wakes the loop, so a promise
|
|
104
|
+
* registered while it waits is part of the next pass. The loop runs while
|
|
105
|
+
* there is something to ask the VM for, and is the only one waiting for
|
|
106
|
+
* external progress.
|
|
107
|
+
*
|
|
108
|
+
* To ask is to say that the handler waits: the VM tells Restate what for,
|
|
109
|
+
* and suspends the invocation on it once the input closed (Restate's
|
|
110
|
+
* inactivity timeout). That holds for every promise but one. A handler with
|
|
111
|
+
* explicit cancellation awaits its cancellation (`cancelled(ctx)`) to stop
|
|
112
|
+
* work it does outside Restate, a model's request, say: it watches for it,
|
|
113
|
+
* and does not wait for it. Reported as awaited, the cancel signal would be
|
|
114
|
+
* all Restate believes the handler waits for; and suspended on it, the
|
|
115
|
+
* invocation would end its attempt in the middle of that work, with a
|
|
116
|
+
* cancellation as the only thing to resume it.
|
|
117
|
+
*
|
|
118
|
+
* So with only the cancellation pending, the loop asks for it together with
|
|
119
|
+
* the attempt's working run (`SelectorDriver.work`), which says what is true:
|
|
120
|
+
* the handler is at work on something of its own, as inside `ctx.run`. While
|
|
121
|
+
* a run executes, the VM reports no await, and does not suspend on a future
|
|
122
|
+
* the run is part of, whether the input is open or closed; and it still
|
|
123
|
+
* finds the cancel signal when it comes. See `awaited`.
|
|
76
124
|
*/
|
|
77
125
|
export class PromiseSelector {
|
|
78
126
|
private readonly pending = new Set<Selectable>();
|
|
79
|
-
// The completion ids of the ephemeral commands in flight.
|
|
80
|
-
private readonly ephemeral = new Set<number>();
|
|
81
127
|
private running = false;
|
|
82
128
|
private closed = false;
|
|
83
|
-
// Between
|
|
129
|
+
// Between a check that found nothing and the next external progress.
|
|
84
130
|
private waiting = false;
|
|
131
|
+
// Whether the last check asked for the cancellation alone, with the working run.
|
|
132
|
+
private watching = false;
|
|
85
133
|
|
|
86
134
|
private readonly driver: SelectorDriver;
|
|
87
135
|
|
|
@@ -92,17 +140,6 @@ export class PromiseSelector {
|
|
|
92
140
|
register(promise: Selectable): void {
|
|
93
141
|
if (this.closed || this.pending.has(promise)) return;
|
|
94
142
|
this.pending.add(promise);
|
|
95
|
-
this.start();
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** An ephemeral command's completion id, until its notification is ready. */
|
|
99
|
-
registerEphemeral(completionId: number): void {
|
|
100
|
-
if (this.closed) return;
|
|
101
|
-
this.ephemeral.add(completionId);
|
|
102
|
-
this.start();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
private start(): void {
|
|
106
143
|
if (this.running) {
|
|
107
144
|
this.wake();
|
|
108
145
|
} else {
|
|
@@ -116,13 +153,12 @@ export class PromiseSelector {
|
|
|
116
153
|
if (this.closed) return;
|
|
117
154
|
this.closed = true;
|
|
118
155
|
this.pending.clear();
|
|
119
|
-
this.ephemeral.clear();
|
|
120
156
|
if (this.running) this.wake();
|
|
121
157
|
}
|
|
122
158
|
|
|
123
159
|
// Only a waiting loop needs waking, once: any other picks up the change on
|
|
124
160
|
// its next pass. (Waking it otherwise would buffer a signal per promise, and
|
|
125
|
-
// as many needless passes.)
|
|
161
|
+
// as many needless passes, each of which tells Restate what is awaited.)
|
|
126
162
|
private wake(): void {
|
|
127
163
|
if (!this.waiting) return;
|
|
128
164
|
this.waiting = false;
|
|
@@ -131,43 +167,58 @@ export class PromiseSelector {
|
|
|
131
167
|
|
|
132
168
|
/** The promises still pending, for tests. */
|
|
133
169
|
get size(): number {
|
|
134
|
-
return this.pending.size
|
|
170
|
+
return this.pending.size;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* What to ask the VM for: the first of the pending promises to complete,
|
|
175
|
+
* or undefined with nothing pending. With only the invocation's
|
|
176
|
+
* cancellation pending, the handler watches and waits for nothing: the
|
|
177
|
+
* working run goes with it (see the class).
|
|
178
|
+
*/
|
|
179
|
+
private awaited(): UnresolvedFuture | undefined {
|
|
180
|
+
const futures: UnresolvedFuture[] = [];
|
|
181
|
+
let watching = true; // for the cancellation, and waiting for nothing
|
|
182
|
+
for (const promise of this.pending) {
|
|
183
|
+
const future = promise.unresolvedFuture();
|
|
184
|
+
if (future === null) {
|
|
185
|
+
this.pending.delete(promise);
|
|
186
|
+
} else {
|
|
187
|
+
futures.push(future);
|
|
188
|
+
watching &&= cancellationAlone(future);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
this.watching = watching && futures.length > 0;
|
|
192
|
+
if (futures.length === 0) return undefined;
|
|
193
|
+
if (watching) futures.push({ Single: this.driver.work() });
|
|
194
|
+
return futures.length === 1 ? futures[0] : { FirstCompleted: futures };
|
|
135
195
|
}
|
|
136
196
|
|
|
137
197
|
private async loop(): Promise<void> {
|
|
138
198
|
try {
|
|
139
199
|
await this.driver.flushOutput();
|
|
140
200
|
for (;;) {
|
|
201
|
+
if (failpoints.size > 0) await failpoints.get('pass')?.();
|
|
141
202
|
// Settle what is ready.
|
|
142
203
|
for (const promise of this.pending) {
|
|
143
204
|
await promise.tryComplete();
|
|
144
205
|
}
|
|
145
|
-
//
|
|
146
|
-
// that one check covers
|
|
147
|
-
// completed in a .finally, a tick late).
|
|
148
|
-
// registered later wakes the loop anyway.
|
|
206
|
+
// A turn of the event loop: what was settled runs, and registers
|
|
207
|
+
// what it awaits next, so that one check covers that too (a
|
|
208
|
+
// combinator also flags itself completed in a .finally, a tick late).
|
|
209
|
+
// An optimization: what is registered later wakes the loop anyway.
|
|
149
210
|
await setImmediate();
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
} else {
|
|
157
|
-
futures.push(future);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
if (futures.length === 0 && this.ephemeral.size === 0) return;
|
|
161
|
-
|
|
162
|
-
// With only ephemeral commands in flight, no future: the VM reports
|
|
163
|
-
// their notifications, or waits for input.
|
|
164
|
-
const result = this.driver.doProgress(futures.length === 1 ? futures[0]! : { FirstCompleted: futures });
|
|
211
|
+
// From here to the end of the pass, or to `waiting`, in one turn:
|
|
212
|
+
// what is registered after the check finds the loop gone, and starts
|
|
213
|
+
// another, or finds it waiting, and wakes it.
|
|
214
|
+
const future = this.awaited();
|
|
215
|
+
if (future === undefined) return;
|
|
216
|
+
const result = this.driver.doProgress(future);
|
|
165
217
|
if (result === 'AnyCompleted') {
|
|
166
218
|
continue;
|
|
167
219
|
} else if (result === 'WaitExternalProgress') {
|
|
168
|
-
// From here, in the same turn as the check: what is registered
|
|
169
|
-
// after it is not part of it.
|
|
170
220
|
this.waiting = true;
|
|
221
|
+
if (failpoints.size > 0) await failpoints.get('wait')?.(future, this.watching);
|
|
171
222
|
await this.driver.flushOutput();
|
|
172
223
|
await this.driver.awaitExternalProgress();
|
|
173
224
|
this.waiting = false;
|
|
@@ -175,21 +226,18 @@ export class PromiseSelector {
|
|
|
175
226
|
// Reaches every pending promise; what is awaited after it is not cancelled.
|
|
176
227
|
for (const promise of this.pending) promise.tryCancel();
|
|
177
228
|
this.pending.clear();
|
|
178
|
-
} else if ('
|
|
179
|
-
|
|
180
|
-
await this.driver.completeEphemeral(result.EphemeralNotificationReady);
|
|
181
|
-
} else {
|
|
229
|
+
} else if ('ExecuteRun' in result) {
|
|
230
|
+
// It starts in the turn the next pass takes before its check.
|
|
182
231
|
this.driver.executeRun(result.ExecuteRun);
|
|
183
|
-
|
|
184
|
-
|
|
232
|
+
} else {
|
|
233
|
+
// An ephemeral command's notification: a handler with SQL makes none.
|
|
234
|
+
throw new Error(`@restatedev/sessions: unexpected progress from the VM: ${JSON.stringify(result)}`);
|
|
185
235
|
}
|
|
186
236
|
}
|
|
187
237
|
} catch (e) {
|
|
188
238
|
this.close();
|
|
189
239
|
this.driver.onError(e);
|
|
190
240
|
} finally {
|
|
191
|
-
// Decided with nothing pending, in the same turn: a registration from
|
|
192
|
-
// here on starts a new loop.
|
|
193
241
|
this.running = false;
|
|
194
242
|
}
|
|
195
243
|
}
|
|
@@ -200,23 +248,19 @@ interface Executor {
|
|
|
200
248
|
doProgress(promise: Selectable): Promise<void>;
|
|
201
249
|
coreVm: {
|
|
202
250
|
do_progress(future: UnresolvedFuture): DoProgressResult;
|
|
203
|
-
|
|
251
|
+
sys_run(name: string): { handle: number };
|
|
252
|
+
propose_run_completion_success(handle: number, value: Uint8Array): void;
|
|
204
253
|
};
|
|
205
254
|
outputPump: { awaitNextProgress(): Promise<void> };
|
|
206
255
|
externalProgressChannel: { awaitNext(): Promise<void>; signal(): void };
|
|
207
|
-
runClosuresTracker: {
|
|
208
|
-
|
|
256
|
+
runClosuresTracker: {
|
|
257
|
+
executeRun(handle: number): void;
|
|
258
|
+
registerRunClosure(handle: number, run: () => Promise<unknown>): void;
|
|
259
|
+
};
|
|
209
260
|
errorCallback: (e: unknown) => void;
|
|
210
261
|
}
|
|
211
262
|
|
|
212
|
-
const FIELDS = [
|
|
213
|
-
'coreVm',
|
|
214
|
-
'outputPump',
|
|
215
|
-
'externalProgressChannel',
|
|
216
|
-
'runClosuresTracker',
|
|
217
|
-
'ephemeralRequestTracker',
|
|
218
|
-
'errorCallback',
|
|
219
|
-
] as const;
|
|
263
|
+
const FIELDS = ['coreVm', 'outputPump', 'externalProgressChannel', 'runClosuresTracker', 'errorCallback'] as const;
|
|
220
264
|
|
|
221
265
|
/**
|
|
222
266
|
* Drive every promise that the invocation of `ctx` (the SDK's context, not a
|
|
@@ -226,22 +270,38 @@ const FIELDS = [
|
|
|
226
270
|
*/
|
|
227
271
|
export function hookSelector(ctx: object): () => void {
|
|
228
272
|
const executor = (ctx as { promisesExecutor?: Executor }).promisesExecutor;
|
|
229
|
-
const missing = executor ? FIELDS.filter((field) => executor[field] === undefined) : ['promisesExecutor'];
|
|
230
|
-
if (missing.length
|
|
273
|
+
const missing: string[] = executor ? FIELDS.filter((field) => executor[field] === undefined) : ['promisesExecutor'];
|
|
274
|
+
if (missing.length === 0) {
|
|
275
|
+
const { coreVm, runClosuresTracker } = executor!;
|
|
276
|
+
for (const method of ['do_progress', 'sys_run', 'propose_run_completion_success'] as const) {
|
|
277
|
+
if (typeof coreVm[method] !== 'function') missing.push(`coreVm.${method}`);
|
|
278
|
+
}
|
|
279
|
+
if (typeof runClosuresTracker.registerRunClosure !== 'function')
|
|
280
|
+
missing.push('runClosuresTracker.registerRunClosure');
|
|
281
|
+
}
|
|
282
|
+
if (missing.length > 0 || executor === undefined) {
|
|
231
283
|
throw new Error(
|
|
232
284
|
`@restatedev/sessions drives a handler's promises through the SDK's internals, which this SDK version does not have ` +
|
|
233
|
-
`(${missing.join(', ')
|
|
285
|
+
`(${missing.join(', ')}): use the @restatedev/restate-sdk that @restatedev/sessions pins`,
|
|
234
286
|
);
|
|
235
287
|
}
|
|
236
|
-
const { coreVm, outputPump, externalProgressChannel, runClosuresTracker
|
|
288
|
+
const { coreVm, outputPump, externalProgressChannel, runClosuresTracker } = executor;
|
|
289
|
+
// The working run: journaled as a run named `work`, with no closure of its
|
|
290
|
+
// own to end it. It ends with the attempt.
|
|
291
|
+
let work: number | undefined;
|
|
237
292
|
const selector = new PromiseSelector({
|
|
238
293
|
doProgress: (future) => coreVm.do_progress(future),
|
|
239
294
|
flushOutput: () => outputPump.awaitNextProgress(),
|
|
240
295
|
awaitExternalProgress: () => externalProgressChannel.awaitNext(),
|
|
241
296
|
wake: () => externalProgressChannel.signal(),
|
|
297
|
+
work: () => {
|
|
298
|
+
if (work === undefined) {
|
|
299
|
+
work = coreVm.sys_run('work').handle;
|
|
300
|
+
runClosuresTracker.registerRunClosure(work, () => new Promise(() => {}));
|
|
301
|
+
}
|
|
302
|
+
return work;
|
|
303
|
+
},
|
|
242
304
|
executeRun: (handle) => runClosuresTracker.executeRun(handle),
|
|
243
|
-
completeEphemeral: (completionId) =>
|
|
244
|
-
ephemeralRequestTracker.complete(completionId, coreVm.take_ephemeral_notification(completionId)),
|
|
245
305
|
onError: (e) => executor.errorCallback(e),
|
|
246
306
|
});
|
|
247
307
|
// An own property, over the class's method: removing it gives the method back.
|
|
@@ -249,5 +309,15 @@ export function hookSelector(ctx: object): () => void {
|
|
|
249
309
|
return () => {
|
|
250
310
|
selector.close();
|
|
251
311
|
delete (executor as Partial<Executor>).doProgress;
|
|
312
|
+
// The work is over: so is its run, in the journal of an invocation that
|
|
313
|
+
// goes on to answer. (A VM that an error or a suspension closed refuses.)
|
|
314
|
+
if (work !== undefined) {
|
|
315
|
+
try {
|
|
316
|
+
coreVm.propose_run_completion_success(work, new Uint8Array());
|
|
317
|
+
} catch {
|
|
318
|
+
// The attempt ended: the next one, or nothing, ends the run.
|
|
319
|
+
}
|
|
320
|
+
work = undefined;
|
|
321
|
+
}
|
|
252
322
|
};
|
|
253
323
|
}
|
package/src/serve.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// `serve`: the services of a program, on PORT, with what they need beside
|
|
2
|
-
// them (a library's objects, such as @restatedev/agents'
|
|
2
|
+
// them (a library's objects, such as each @restatedev/agents agent's companion).
|
|
3
3
|
import * as restate from '@restatedev/restate-sdk';
|
|
4
4
|
|
|
5
5
|
type Service = restate.ServeOptions['services'][number];
|
|
@@ -9,8 +9,8 @@ const NEEDS = Symbol.for('restate-sessions.needs');
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* `definition`, which needs `services` served beside it: {@link serve}
|
|
12
|
-
* serves them too. For a library whose objects call one of
|
|
13
|
-
* agent's
|
|
12
|
+
* serves them too. For a library whose objects call one of their own (an
|
|
13
|
+
* agent's companion, say).
|
|
14
14
|
*/
|
|
15
15
|
export const needing = <D extends object>(definition: D, services: () => Service[]): D =>
|
|
16
16
|
Object.defineProperty(definition, NEEDS, { value: services });
|
|
@@ -18,37 +18,45 @@ export const needing = <D extends object>(definition: D, services: () => Service
|
|
|
18
18
|
/**
|
|
19
19
|
* Serve `services` on PORT (default 9080), with the services they need
|
|
20
20
|
* beside them, each once. RESTATE_SERVE_ONLY, names separated by commas
|
|
21
|
-
* (`triage,planner
|
|
22
|
-
* image), deployed in parts.
|
|
21
|
+
* (`triage,planner`), serves only those of them, each with what it needs
|
|
22
|
+
* beside it: one program (or image), deployed in parts.
|
|
23
23
|
*
|
|
24
24
|
* await restate.serve(notes, orders);
|
|
25
25
|
*/
|
|
26
26
|
export const serve = (...services: Service[]) =>
|
|
27
27
|
restate.serve({ services: served(services, process.env.RESTATE_SERVE_ONLY), port: Number(process.env.PORT ?? 9080) });
|
|
28
28
|
|
|
29
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* What {@link serve} serves of `services`: each with what it needs beside
|
|
31
|
+
* it, once; with `names` (separated by commas), only the services it names,
|
|
32
|
+
* each with what it needs beside it. For an endpoint of your own:
|
|
33
|
+
*
|
|
34
|
+
* const endpoint = restate.endpoint();
|
|
35
|
+
* for (const service of restate.served([notes, orders])) endpoint.bind(service);
|
|
36
|
+
*/
|
|
30
37
|
export function served(services: Service[], names?: string): Service[] {
|
|
31
|
-
const all =
|
|
32
|
-
const add = (service: Service) => {
|
|
33
|
-
if (all.has(service.name)) return;
|
|
34
|
-
all.set(service.name, service);
|
|
35
|
-
for (const needed of (service as { [NEEDS]?: () => Service[] })[NEEDS]?.() ?? []) add(needed);
|
|
36
|
-
};
|
|
37
|
-
services.forEach(add);
|
|
38
|
-
return only([...all.values()], names);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** The `services` that `names` (comma separated) names, or all of them without names; a name of none is an error. */
|
|
42
|
-
function only(services: Service[], names: string | undefined) {
|
|
38
|
+
const all = withNeeds(services);
|
|
43
39
|
const wanted = (names ?? '')
|
|
44
40
|
.split(',')
|
|
45
41
|
.map((name) => name.trim())
|
|
46
42
|
.filter(Boolean);
|
|
47
|
-
if (wanted.length === 0) return
|
|
48
|
-
const known =
|
|
43
|
+
if (wanted.length === 0) return all;
|
|
44
|
+
const known = all.map((s) => s.name);
|
|
49
45
|
const unknown = wanted.filter((name) => !known.includes(name));
|
|
50
46
|
if (unknown.length > 0) {
|
|
51
47
|
throw new Error(`RESTATE_SERVE_ONLY names ${unknown.join(', ')}: the services are ${known.join(', ')}`);
|
|
52
48
|
}
|
|
53
|
-
return
|
|
49
|
+
return withNeeds(all.filter((s) => wanted.includes(s.name)));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** `services`, each with what it needs beside it, once. */
|
|
53
|
+
function withNeeds(services: Service[]): Service[] {
|
|
54
|
+
const all = new Map<string, Service>();
|
|
55
|
+
const add = (service: Service) => {
|
|
56
|
+
if (all.has(service.name)) return;
|
|
57
|
+
all.set(service.name, service);
|
|
58
|
+
for (const needed of (service as { [NEEDS]?: () => Service[] })[NEEDS]?.() ?? []) add(needed);
|
|
59
|
+
};
|
|
60
|
+
services.forEach(add);
|
|
61
|
+
return [...all.values()];
|
|
54
62
|
}
|