@mark1russell7/service 0.6.3 → 0.6.4
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 +2 -2
- package/build/index.js +2 -580
- package/build/index.js.map +1 -1
- package/build/requests-BIq9Tr4P.js +586 -0
- package/build/requests-BIq9Tr4P.js.map +1 -0
- package/build/testing.d.ts +54 -3
- package/build/testing.js +259 -12
- package/build/testing.js.map +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
import { n as operationProblems, t as mayReissue } from "./operation-DL5RxFds.js";
|
|
2
|
+
import { t as boundedMillisecondsOf } from "./knobs-D2ClI68I.js";
|
|
3
|
+
import { backoffProblems } from "./backoff.js";
|
|
4
|
+
import { quantity } from "@mark1russell7/quantity";
|
|
5
|
+
//#region src/routes.ts
|
|
6
|
+
const NAME = /^[A-Za-z_][A-Za-z0-9_]*/;
|
|
7
|
+
/** This function gives the problems of the syntax of a pattern: an empty pattern, a part without a name, or a name two times. */
|
|
8
|
+
const patternProblems = (owner, pattern) => {
|
|
9
|
+
const problems = [];
|
|
10
|
+
if (pattern.length === 0) problems.push(`${owner} has an empty route pattern`);
|
|
11
|
+
const names = /* @__PURE__ */ new Set();
|
|
12
|
+
for (let at = 0; at < pattern.length; at += 1) {
|
|
13
|
+
if (pattern[at] !== ":") continue;
|
|
14
|
+
const name = NAME.exec(pattern.slice(at + 1))?.[0];
|
|
15
|
+
if (name === void 0) {
|
|
16
|
+
problems.push(`${owner}'s pattern ${pattern} has a ":" without a name at ${at}`);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (names.has(name)) problems.push(`${owner}'s pattern ${pattern} names :${name} two times`);
|
|
20
|
+
names.add(name);
|
|
21
|
+
at += name.length;
|
|
22
|
+
}
|
|
23
|
+
return problems;
|
|
24
|
+
};
|
|
25
|
+
/** This function gives the problems of a pattern and of each of its operations, which `describe()` refuses. */
|
|
26
|
+
const routePatternProblems = (owner, declared) => {
|
|
27
|
+
const problems = patternProblems(owner, declared.pattern);
|
|
28
|
+
const names = Object.keys(declared.operations);
|
|
29
|
+
if (names.length === 0) problems.push(`${owner}'s pattern ${declared.pattern} declares no operation`);
|
|
30
|
+
for (const name of names) problems.push(...operationProblems(owner, `${declared.pattern} (${name})`, declared.operations[name]));
|
|
31
|
+
return problems;
|
|
32
|
+
};
|
|
33
|
+
/** This function compiles a pattern to a matcher. The match must cover the whole route. */
|
|
34
|
+
const compilePattern = (pattern) => {
|
|
35
|
+
const kinds = [];
|
|
36
|
+
let source = "^";
|
|
37
|
+
for (let at = 0; at < pattern.length; at += 1) {
|
|
38
|
+
const char = pattern[at];
|
|
39
|
+
if (char === "*") {
|
|
40
|
+
source += "([\\s\\S]*)";
|
|
41
|
+
kinds.push({ kind: "wildcard" });
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (char === ":") {
|
|
45
|
+
const name = NAME.exec(pattern.slice(at + 1))?.[0];
|
|
46
|
+
if (name !== void 0) {
|
|
47
|
+
source += "([^/]+)";
|
|
48
|
+
kinds.push({
|
|
49
|
+
kind: "param",
|
|
50
|
+
name
|
|
51
|
+
});
|
|
52
|
+
at += name.length;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
source += char.replace(/[.*+?^${}()|[\]\\/]/g, "\\$&");
|
|
57
|
+
}
|
|
58
|
+
const matcher = new RegExp(`${source}$`);
|
|
59
|
+
return {
|
|
60
|
+
pattern,
|
|
61
|
+
match(route) {
|
|
62
|
+
const found = matcher.exec(route);
|
|
63
|
+
if (found === null) return null;
|
|
64
|
+
const params = {};
|
|
65
|
+
const wildcards = [];
|
|
66
|
+
kinds.forEach((part, index) => {
|
|
67
|
+
const text = found[index + 1] ?? "";
|
|
68
|
+
if (part.kind === "param") params[part.name] = text;
|
|
69
|
+
else wildcards.push(text);
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
params,
|
|
73
|
+
wildcards
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/units.ts
|
|
80
|
+
/** This function gives the cause of an abort as text. */
|
|
81
|
+
const causeOf = (signal) => signal.reason instanceof Error ? signal.reason.message : String(signal.reason ?? "the unit got an abort");
|
|
82
|
+
/**
|
|
83
|
+
* This function does one unit of work, and gives its end. With a request, the unit goes through
|
|
84
|
+
* `life.request`, thus the service counts it, drains it and can cut it. A throw after the abort of
|
|
85
|
+
* the transport is a cut, not a failure (S6 A10). A throw after the bound is a failure.
|
|
86
|
+
*/
|
|
87
|
+
const runUnit = async (run) => {
|
|
88
|
+
const { clock, bound, abort, life, request, unit, work } = run;
|
|
89
|
+
const timeout = new AbortController();
|
|
90
|
+
const cancel = Number.isFinite(bound) ? clock.after(bound, () => timeout.abort(/* @__PURE__ */ new Error(`the unit did not end in its unit_processing_time of ${bound} ms`))) : () => {};
|
|
91
|
+
const call = async (tracker) => work(AbortSignal.any(tracker === void 0 ? [abort.signal, timeout.signal] : [
|
|
92
|
+
abort.signal,
|
|
93
|
+
timeout.signal,
|
|
94
|
+
tracker
|
|
95
|
+
]));
|
|
96
|
+
const thrown = (error) => {
|
|
97
|
+
if (abort.signal.aborted) return {
|
|
98
|
+
kind: "cut",
|
|
99
|
+
cause: causeOf(abort.signal),
|
|
100
|
+
retryable: false
|
|
101
|
+
};
|
|
102
|
+
if (timeout.signal.aborted) return {
|
|
103
|
+
kind: "failed",
|
|
104
|
+
error: timeout.signal.reason
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
kind: "failed",
|
|
108
|
+
error
|
|
109
|
+
};
|
|
110
|
+
};
|
|
111
|
+
try {
|
|
112
|
+
if (request === void 0) try {
|
|
113
|
+
return {
|
|
114
|
+
kind: "done",
|
|
115
|
+
value: await call()
|
|
116
|
+
};
|
|
117
|
+
} catch (error) {
|
|
118
|
+
return thrown(error);
|
|
119
|
+
}
|
|
120
|
+
let collected;
|
|
121
|
+
try {
|
|
122
|
+
const route = typeof request.route === "string" ? request.route : request.route(unit);
|
|
123
|
+
const detail = request.detail?.(unit);
|
|
124
|
+
collected = life.request(request.controller, route, (signal) => call(signal).then((value) => ({ value })), detail === void 0 ? {} : { detail }).collect();
|
|
125
|
+
} catch (error) {
|
|
126
|
+
return {
|
|
127
|
+
kind: "failed",
|
|
128
|
+
error
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
const { items, end } = await collected;
|
|
132
|
+
if (end.kind === "done") return {
|
|
133
|
+
kind: "done",
|
|
134
|
+
value: items[0]?.value
|
|
135
|
+
};
|
|
136
|
+
if (end.kind === "failed") return thrown(end.error);
|
|
137
|
+
if (end.kind === "refused") return {
|
|
138
|
+
kind: "refused",
|
|
139
|
+
cause: end.cause
|
|
140
|
+
};
|
|
141
|
+
return {
|
|
142
|
+
kind: "cut",
|
|
143
|
+
cause: end.cause,
|
|
144
|
+
retryable: end.retryable
|
|
145
|
+
};
|
|
146
|
+
} finally {
|
|
147
|
+
cancel();
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
/** This function makes a flight set on a clock. */
|
|
151
|
+
const flights = (clock) => {
|
|
152
|
+
const units = /* @__PURE__ */ new Set();
|
|
153
|
+
const idle = /* @__PURE__ */ new Set();
|
|
154
|
+
const lower = /* @__PURE__ */ new Set();
|
|
155
|
+
let aborted = null;
|
|
156
|
+
const settle = () => {
|
|
157
|
+
for (const done of [...lower]) done();
|
|
158
|
+
lower.clear();
|
|
159
|
+
if (units.size === 0) for (const done of [...idle]) done();
|
|
160
|
+
};
|
|
161
|
+
const abort = (reason) => {
|
|
162
|
+
aborted ??= { reason };
|
|
163
|
+
for (const unit of [...units]) if (!unit.signal.aborted) unit.abort(aborted.reason);
|
|
164
|
+
};
|
|
165
|
+
return {
|
|
166
|
+
get size() {
|
|
167
|
+
return units.size;
|
|
168
|
+
},
|
|
169
|
+
add(unit) {
|
|
170
|
+
units.add(unit);
|
|
171
|
+
if (aborted !== null && !unit.signal.aborted) unit.abort(aborted.reason);
|
|
172
|
+
return () => {
|
|
173
|
+
if (units.delete(unit)) settle();
|
|
174
|
+
};
|
|
175
|
+
},
|
|
176
|
+
abort,
|
|
177
|
+
clear() {
|
|
178
|
+
units.clear();
|
|
179
|
+
settle();
|
|
180
|
+
},
|
|
181
|
+
drain(deadline, signal) {
|
|
182
|
+
const cooperative = () => abort(signal.reason);
|
|
183
|
+
if (signal.aborted) cooperative();
|
|
184
|
+
else signal.addEventListener("abort", cooperative, { once: true });
|
|
185
|
+
if (units.size === 0 || deadline <= clock.now()) {
|
|
186
|
+
signal.removeEventListener("abort", cooperative);
|
|
187
|
+
return Promise.resolve();
|
|
188
|
+
}
|
|
189
|
+
return new Promise((resolve) => {
|
|
190
|
+
let cancel = () => {};
|
|
191
|
+
const done = () => {
|
|
192
|
+
idle.delete(done);
|
|
193
|
+
cancel();
|
|
194
|
+
signal.removeEventListener("abort", cooperative);
|
|
195
|
+
resolve();
|
|
196
|
+
};
|
|
197
|
+
idle.add(done);
|
|
198
|
+
cancel = clock.after(Math.max(0, deadline - clock.now()), done);
|
|
199
|
+
});
|
|
200
|
+
},
|
|
201
|
+
less() {
|
|
202
|
+
return new Promise((resolve) => lower.add(resolve));
|
|
203
|
+
},
|
|
204
|
+
release() {
|
|
205
|
+
for (const done of [...lower]) done();
|
|
206
|
+
lower.clear();
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
/** This function makes a sleeper on a clock. */
|
|
211
|
+
const sleeper = (clock) => {
|
|
212
|
+
let wakes = 0;
|
|
213
|
+
let stopped = false;
|
|
214
|
+
const sleeps = /* @__PURE__ */ new Set();
|
|
215
|
+
const backoffs = /* @__PURE__ */ new Set();
|
|
216
|
+
const wait = (ms, into) => stopped ? Promise.resolve() : new Promise((resolve) => {
|
|
217
|
+
let cancel = () => {};
|
|
218
|
+
const done = () => {
|
|
219
|
+
if (!into.delete(done)) return;
|
|
220
|
+
cancel();
|
|
221
|
+
resolve();
|
|
222
|
+
};
|
|
223
|
+
into.add(done);
|
|
224
|
+
if (Number.isFinite(ms)) cancel = clock.after(ms, done);
|
|
225
|
+
});
|
|
226
|
+
return {
|
|
227
|
+
get wakes() {
|
|
228
|
+
return wakes;
|
|
229
|
+
},
|
|
230
|
+
sleep(ms, since) {
|
|
231
|
+
if (since !== wakes) return Promise.resolve();
|
|
232
|
+
return wait(ms, sleeps);
|
|
233
|
+
},
|
|
234
|
+
backoff(ms) {
|
|
235
|
+
return wait(ms, backoffs);
|
|
236
|
+
},
|
|
237
|
+
wake() {
|
|
238
|
+
wakes += 1;
|
|
239
|
+
const ended = sleeps.size;
|
|
240
|
+
for (const done of [...sleeps]) done();
|
|
241
|
+
return ended;
|
|
242
|
+
},
|
|
243
|
+
stop() {
|
|
244
|
+
stopped = true;
|
|
245
|
+
for (const done of [...sleeps, ...backoffs]) done();
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
};
|
|
249
|
+
/** This function waits for a work until a bound on the clock, in milliseconds. */
|
|
250
|
+
const settleWithin = (clock, ms, work) => new Promise((resolve) => {
|
|
251
|
+
const cancel = Number.isFinite(ms) ? clock.after(ms, () => resolve({ kind: "bound" })) : () => {};
|
|
252
|
+
work.then(() => {
|
|
253
|
+
cancel();
|
|
254
|
+
resolve({ kind: "done" });
|
|
255
|
+
}, (error) => {
|
|
256
|
+
cancel();
|
|
257
|
+
resolve({
|
|
258
|
+
kind: "failed",
|
|
259
|
+
error
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
/**
|
|
264
|
+
* This function gives the value of a promise, or `stop` when the signal aborts first. Each wait adds
|
|
265
|
+
* its own listener, and removes it when the promise settles. A race against one pending promise
|
|
266
|
+
* keeps each settled value until that promise ends.
|
|
267
|
+
*/
|
|
268
|
+
const untilAbort = (promise, signal, stop) => {
|
|
269
|
+
if (signal.aborted) return Promise.race([promise, Promise.resolve(stop)]);
|
|
270
|
+
return new Promise((resolve, reject) => {
|
|
271
|
+
const onAbort = () => resolve(stop);
|
|
272
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
273
|
+
promise.then((value) => {
|
|
274
|
+
signal.removeEventListener("abort", onAbort);
|
|
275
|
+
resolve(value);
|
|
276
|
+
}, (error) => {
|
|
277
|
+
signal.removeEventListener("abort", onAbort);
|
|
278
|
+
reject(error);
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* This function gives the backoff of the knobs of a loop. The first delay is `retry_base`, and each
|
|
284
|
+
* delay is two times longer, up to `retry_cap`. Each delay has a jitter up to the base, as in the Redis client.
|
|
285
|
+
*/
|
|
286
|
+
const retryOf = (knobs, owner) => {
|
|
287
|
+
const base = boundedMillisecondsOf(knobs, "retry_base", owner);
|
|
288
|
+
const backoff = {
|
|
289
|
+
base,
|
|
290
|
+
factor: 2,
|
|
291
|
+
cap: boundedMillisecondsOf(knobs, "retry_cap", owner),
|
|
292
|
+
jitter: {
|
|
293
|
+
kind: "add",
|
|
294
|
+
amount: base
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
const problems = backoffProblems(backoff);
|
|
298
|
+
if (problems.length > 0) throw new Error(`${owner}: the retry_base and retry_cap give no backoff: ${problems.join(", ")}`);
|
|
299
|
+
return backoff;
|
|
300
|
+
};
|
|
301
|
+
/**
|
|
302
|
+
* This function gives the problems of a loop in a declaration: each phase that the loop needs and
|
|
303
|
+
* that the declaration does not give. A loop closes in `close transports`, and it ends its units in
|
|
304
|
+
* `drain`. `lint()` compares the bounds with the caps of the phases (`BOUND_RULES`, S6 A10).
|
|
305
|
+
*/
|
|
306
|
+
const boundProblems = (owner, description) => {
|
|
307
|
+
const has = (name) => description.shutdown.phases.some((each) => each.name === name);
|
|
308
|
+
return [...has("close transports") ? [] : [`${owner} needs the phase "close transports" to close`], ...has("drain") ? [] : [`${owner} needs the phase "drain" to end its units`]];
|
|
309
|
+
};
|
|
310
|
+
//#endregion
|
|
311
|
+
//#region src/requests.ts
|
|
312
|
+
const isAsyncIterable = (value) => value !== null && (typeof value === "object" || typeof value === "function") && typeof value[Symbol.asyncIterator] === "function";
|
|
313
|
+
const isPromiseLike = (value) => value !== null && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
|
|
314
|
+
/** This function gives a stream of events from a function that gives the next event. */
|
|
315
|
+
const streamOf = (next, stop) => {
|
|
316
|
+
let chain = Promise.resolve();
|
|
317
|
+
const iterator = {
|
|
318
|
+
next() {
|
|
319
|
+
const step = chain.then(next).then((event) => event === null ? {
|
|
320
|
+
value: void 0,
|
|
321
|
+
done: true
|
|
322
|
+
} : {
|
|
323
|
+
value: event,
|
|
324
|
+
done: false
|
|
325
|
+
});
|
|
326
|
+
chain = step.catch(() => {});
|
|
327
|
+
return step;
|
|
328
|
+
},
|
|
329
|
+
return() {
|
|
330
|
+
stop();
|
|
331
|
+
return Promise.resolve({
|
|
332
|
+
value: void 0,
|
|
333
|
+
done: true
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
return {
|
|
338
|
+
[Symbol.asyncIterator]: () => iterator,
|
|
339
|
+
async collect() {
|
|
340
|
+
const items = [];
|
|
341
|
+
for (;;) {
|
|
342
|
+
const step = await iterator.next();
|
|
343
|
+
if (step.done === true) throw new Error("the stream ended without its end");
|
|
344
|
+
if (step.value.kind === "item") items.push(step.value.value);
|
|
345
|
+
else return {
|
|
346
|
+
items,
|
|
347
|
+
end: step.value
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
};
|
|
353
|
+
/**
|
|
354
|
+
* This function makes a tracker. `refuses` gives the reason that the service refuses a new request
|
|
355
|
+
* for a controller at this time, or null when it admits it. `choices` gives the function of each
|
|
356
|
+
* route pattern, by controller and by pattern.
|
|
357
|
+
*/
|
|
358
|
+
const tracker = (controllers, clock, refuses, choices = /* @__PURE__ */ new Map()) => {
|
|
359
|
+
const byName = new Map(controllers.map((controller) => [controller.name, {
|
|
360
|
+
controller,
|
|
361
|
+
patterns: (controller.patterns ?? []).map((declared) => ({
|
|
362
|
+
declared,
|
|
363
|
+
compiled: compilePattern(declared.pattern)
|
|
364
|
+
}))
|
|
365
|
+
}]));
|
|
366
|
+
const flights = /* @__PURE__ */ new Set();
|
|
367
|
+
const idle = /* @__PURE__ */ new Set();
|
|
368
|
+
let intake = true;
|
|
369
|
+
/** This function gives the operation of a request: its exact route, else the first pattern that matches it. */
|
|
370
|
+
const operationOf = (controller, route, detail) => {
|
|
371
|
+
const held = byName.get(controller);
|
|
372
|
+
if (held === void 0) throw new Error(`there is no controller ${controller}`);
|
|
373
|
+
const exact = held.controller.routes[route];
|
|
374
|
+
if (exact !== void 0) return exact;
|
|
375
|
+
for (const { declared, compiled } of held.patterns) {
|
|
376
|
+
const found = compiled.match(route);
|
|
377
|
+
if (found === null) continue;
|
|
378
|
+
const names = Object.keys(declared.operations);
|
|
379
|
+
const choose = choices.get(controller)?.get(declared.pattern);
|
|
380
|
+
const name = choose === void 0 ? names[0] : choose({
|
|
381
|
+
controller,
|
|
382
|
+
route,
|
|
383
|
+
...found,
|
|
384
|
+
detail
|
|
385
|
+
});
|
|
386
|
+
const operation = declared.operations[name];
|
|
387
|
+
if (operation === void 0) throw new Error(`the pattern ${declared.pattern} of ${controller} declares no operation ${String(name)} for ${route}`);
|
|
388
|
+
return operation;
|
|
389
|
+
}
|
|
390
|
+
throw new Error(`controller ${controller} does not declare the route ${route}`);
|
|
391
|
+
};
|
|
392
|
+
const settle = (flight) => {
|
|
393
|
+
if (!flights.delete(flight)) return;
|
|
394
|
+
if (flights.size === 0) for (const done of [...idle]) done();
|
|
395
|
+
};
|
|
396
|
+
/** This function starts the work of an admitted request, and gives its stream. */
|
|
397
|
+
const open = (controller, operation, work) => {
|
|
398
|
+
const started = clock.now();
|
|
399
|
+
const abort = new AbortController();
|
|
400
|
+
let delivered = 0;
|
|
401
|
+
let end = null;
|
|
402
|
+
let ended = false;
|
|
403
|
+
let returned = false;
|
|
404
|
+
let source = null;
|
|
405
|
+
let unary = null;
|
|
406
|
+
const interruption = new AbortController();
|
|
407
|
+
const interrupt = () => interruption.abort();
|
|
408
|
+
const cutEnd = (cause) => {
|
|
409
|
+
const since = quantity(Math.max(0, clock.now() - started) / 1e3, "s");
|
|
410
|
+
const retryable = mayReissue(operation, since);
|
|
411
|
+
return {
|
|
412
|
+
kind: "cut",
|
|
413
|
+
status: delivered > 0 ? null : retryable ? 503 : 500,
|
|
414
|
+
retryable,
|
|
415
|
+
cause,
|
|
416
|
+
delivered
|
|
417
|
+
};
|
|
418
|
+
};
|
|
419
|
+
/** This function ends the work of the request: the end is known, and the request leaves the flights. */
|
|
420
|
+
const finish = (given) => {
|
|
421
|
+
if (end !== null) return;
|
|
422
|
+
end = given;
|
|
423
|
+
settle(flight);
|
|
424
|
+
interrupt();
|
|
425
|
+
};
|
|
426
|
+
/** This function ends the work after a throw: a failure after the shutdown aborted it is a cut. */
|
|
427
|
+
const failed = (error) => finish(abort.signal.aborted ? cutEnd("the shutdown aborted it, and it failed") : {
|
|
428
|
+
kind: "failed",
|
|
429
|
+
error,
|
|
430
|
+
delivered
|
|
431
|
+
});
|
|
432
|
+
const closeSource = () => {
|
|
433
|
+
if (source === null || returned) return;
|
|
434
|
+
returned = true;
|
|
435
|
+
try {
|
|
436
|
+
Promise.resolve(source.return?.()).catch(() => {});
|
|
437
|
+
} catch {}
|
|
438
|
+
};
|
|
439
|
+
const flight = {
|
|
440
|
+
controller,
|
|
441
|
+
cooperative: () => {
|
|
442
|
+
abort.abort(/* @__PURE__ */ new Error("the cooperative abort of the drain"));
|
|
443
|
+
closeSource();
|
|
444
|
+
},
|
|
445
|
+
cut: (cause) => {
|
|
446
|
+
abort.abort(new Error(cause));
|
|
447
|
+
finish(cutEnd(cause));
|
|
448
|
+
closeSource();
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
flights.add(flight);
|
|
452
|
+
/** This function takes the result of the work: a stream, or one value. */
|
|
453
|
+
const take = (result) => {
|
|
454
|
+
if (end !== null) return;
|
|
455
|
+
if (isAsyncIterable(result)) {
|
|
456
|
+
source = result[Symbol.asyncIterator]();
|
|
457
|
+
if (abort.signal.aborted) closeSource();
|
|
458
|
+
} else {
|
|
459
|
+
unary = { value: result };
|
|
460
|
+
settle(flight);
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
let ready;
|
|
464
|
+
try {
|
|
465
|
+
const result = work(abort.signal);
|
|
466
|
+
if (isAsyncIterable(result) || !isPromiseLike(result)) {
|
|
467
|
+
take(result);
|
|
468
|
+
ready = Promise.resolve();
|
|
469
|
+
} else ready = Promise.resolve(result).then(take, failed);
|
|
470
|
+
} catch (error) {
|
|
471
|
+
failed(error);
|
|
472
|
+
ready = Promise.resolve();
|
|
473
|
+
}
|
|
474
|
+
const next = async () => {
|
|
475
|
+
if (ended) return null;
|
|
476
|
+
if (end === null) await untilAbort(ready, interruption.signal, "interrupted");
|
|
477
|
+
if (end === null && unary !== null) {
|
|
478
|
+
const { value } = unary;
|
|
479
|
+
unary = null;
|
|
480
|
+
delivered = 1;
|
|
481
|
+
end = {
|
|
482
|
+
kind: "done",
|
|
483
|
+
delivered
|
|
484
|
+
};
|
|
485
|
+
return {
|
|
486
|
+
kind: "item",
|
|
487
|
+
value
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
if (end === null && source !== null) {
|
|
491
|
+
const pending = source.next();
|
|
492
|
+
let step;
|
|
493
|
+
try {
|
|
494
|
+
step = await untilAbort(Promise.resolve(pending), interruption.signal, "interrupted");
|
|
495
|
+
} catch (error) {
|
|
496
|
+
failed(error);
|
|
497
|
+
step = "interrupted";
|
|
498
|
+
}
|
|
499
|
+
if (step !== "interrupted") {
|
|
500
|
+
if (step.done !== true) {
|
|
501
|
+
delivered += 1;
|
|
502
|
+
return {
|
|
503
|
+
kind: "item",
|
|
504
|
+
value: step.value
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
finish(abort.signal.aborted ? cutEnd("the shutdown ended the stream") : {
|
|
508
|
+
kind: "done",
|
|
509
|
+
delivered
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
ended = true;
|
|
514
|
+
return end;
|
|
515
|
+
};
|
|
516
|
+
const stop = () => {
|
|
517
|
+
if (end === null) {
|
|
518
|
+
abort.abort(/* @__PURE__ */ new Error("the reader stopped the stream"));
|
|
519
|
+
finish({
|
|
520
|
+
kind: "done",
|
|
521
|
+
delivered
|
|
522
|
+
});
|
|
523
|
+
closeSource();
|
|
524
|
+
}
|
|
525
|
+
ended = true;
|
|
526
|
+
};
|
|
527
|
+
return streamOf(next, stop);
|
|
528
|
+
};
|
|
529
|
+
return {
|
|
530
|
+
request(controller, route, work, options = {}) {
|
|
531
|
+
const operation = operationOf(controller, route, options.detail);
|
|
532
|
+
const refusal = intake ? refuses(controller) : "the service stopped its intake";
|
|
533
|
+
if (refusal !== null) {
|
|
534
|
+
let given = false;
|
|
535
|
+
const refused = {
|
|
536
|
+
kind: "refused",
|
|
537
|
+
status: 503,
|
|
538
|
+
retryable: true,
|
|
539
|
+
cause: refusal,
|
|
540
|
+
delivered: 0
|
|
541
|
+
};
|
|
542
|
+
return streamOf(() => {
|
|
543
|
+
if (given) return Promise.resolve(null);
|
|
544
|
+
given = true;
|
|
545
|
+
return Promise.resolve(refused);
|
|
546
|
+
}, () => {
|
|
547
|
+
given = true;
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
return open(controller, operation, work);
|
|
551
|
+
},
|
|
552
|
+
inFlight(controller) {
|
|
553
|
+
let count = 0;
|
|
554
|
+
for (const flight of flights) if (controller === void 0 || flight.controller === controller) count += 1;
|
|
555
|
+
return count;
|
|
556
|
+
},
|
|
557
|
+
stopIntake() {
|
|
558
|
+
intake = false;
|
|
559
|
+
},
|
|
560
|
+
cooperativeAbort() {
|
|
561
|
+
for (const flight of [...flights]) flight.cooperative();
|
|
562
|
+
},
|
|
563
|
+
drain(deadline) {
|
|
564
|
+
if (flights.size === 0) return Promise.resolve();
|
|
565
|
+
return new Promise((resolve) => {
|
|
566
|
+
let finished = false;
|
|
567
|
+
const done = () => {
|
|
568
|
+
if (finished) return;
|
|
569
|
+
finished = true;
|
|
570
|
+
idle.delete(done);
|
|
571
|
+
cancel();
|
|
572
|
+
resolve();
|
|
573
|
+
};
|
|
574
|
+
idle.add(done);
|
|
575
|
+
const cancel = clock.after(Math.max(0, deadline - clock.now()), () => {
|
|
576
|
+
for (const flight of [...flights]) flight.cut("the drain reached its deadline");
|
|
577
|
+
done();
|
|
578
|
+
});
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
};
|
|
583
|
+
//#endregion
|
|
584
|
+
export { runUnit as a, untilAbort as c, retryOf as i, routePatternProblems as l, boundProblems as n, settleWithin as o, flights as r, sleeper as s, tracker as t };
|
|
585
|
+
|
|
586
|
+
//# sourceMappingURL=requests-BIq9Tr4P.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requests-BIq9Tr4P.js","names":[],"sources":["../src/routes.ts","../src/units.ts","../src/requests.ts"],"sourcesContent":["import type { Operation } from \"./operation.ts\";\nimport { operationProblems } from \"./operation.ts\";\n\n/**\n * A route pattern matches the routes of a controller that its exact routes do not name. A\n * registry that changes while the service operates, for example a registry of procedures, thus\n * needs no list of routes before the start. The syntax of a pattern:\n * - `*` matches each text, also an empty text and a text with `/`. The pattern `*` alone matches\n * each route.\n * - `:name` matches a text of one character or more without `/`, and gives it in `params.name`.\n * - Each other character matches itself.\n *\n * The declaration names the operations that a request of the pattern can be. Each operation\n * declares its idempotency, as a route does. A function picks the operation of each request.\n */\nexport interface RoutePattern {\n readonly pattern: string;\n /** These are the operations that a request of the pattern can be, by name. */\n readonly operations: Readonly<Record<string, Operation>>;\n}\n\n/** A route request is what the function of a pattern gets for one request. */\nexport interface RouteRequest {\n readonly controller: string;\n readonly route: string;\n /** These are the texts of the `:name` parts of the pattern, by name. */\n readonly params: Readonly<Record<string, string>>;\n /** These are the texts of the `*` parts of the pattern, in their sequence. */\n readonly wildcards: readonly string[];\n /** This value is the detail that the transport gives with the request, or undefined. */\n readonly detail: unknown;\n}\n\n/**\n * An operation choice gives the name of the operation of one request: one of the operations that\n * the pattern declares. Thus the function decides the idempotency of each request, and the\n * declaration keeps each window of a keyed operation as data.\n */\nexport type OperationOf = (request: RouteRequest) => string;\n\n/** A compiled pattern matches a route, and gives the parts of the route that it matched. */\nexport interface CompiledPattern {\n readonly pattern: string;\n match(route: string): { readonly params: Record<string, string>; readonly wildcards: string[] } | null;\n}\n\nconst NAME = /^[A-Za-z_][A-Za-z0-9_]*/;\n\n/** This function gives the problems of the syntax of a pattern: an empty pattern, a part without a name, or a name two times. */\nexport const patternProblems = (owner: string, pattern: string): string[] => {\n const problems: string[] = [];\n if (pattern.length === 0) problems.push(`${owner} has an empty route pattern`);\n const names = new Set<string>();\n for (let at = 0; at < pattern.length; at += 1) {\n if (pattern[at] !== \":\") continue;\n const name = NAME.exec(pattern.slice(at + 1))?.[0];\n if (name === undefined) {\n problems.push(`${owner}'s pattern ${pattern} has a \":\" without a name at ${at}`);\n continue;\n }\n if (names.has(name)) problems.push(`${owner}'s pattern ${pattern} names :${name} two times`);\n names.add(name);\n at += name.length;\n }\n return problems;\n};\n\n/** This function gives the problems of a pattern and of each of its operations, which `describe()` refuses. */\nexport const routePatternProblems = (owner: string, declared: RoutePattern): string[] => {\n const problems = patternProblems(owner, declared.pattern);\n const names = Object.keys(declared.operations);\n if (names.length === 0) problems.push(`${owner}'s pattern ${declared.pattern} declares no operation`);\n for (const name of names)\n problems.push(...operationProblems(owner, `${declared.pattern} (${name})`, declared.operations[name]!));\n return problems;\n};\n\n/** This function compiles a pattern to a matcher. The match must cover the whole route. */\nexport const compilePattern = (pattern: string): CompiledPattern => {\n const kinds: Array<{ kind: \"param\"; name: string } | { kind: \"wildcard\" }> = [];\n let source = \"^\";\n for (let at = 0; at < pattern.length; at += 1) {\n const char = pattern[at]!;\n if (char === \"*\") {\n source += \"([\\\\s\\\\S]*)\";\n kinds.push({ kind: \"wildcard\" });\n continue;\n }\n if (char === \":\") {\n const name = NAME.exec(pattern.slice(at + 1))?.[0];\n if (name !== undefined) {\n source += \"([^/]+)\";\n kinds.push({ kind: \"param\", name });\n at += name.length;\n continue;\n }\n }\n source += char.replace(/[.*+?^${}()|[\\]\\\\/]/g, \"\\\\$&\");\n }\n const matcher = new RegExp(`${source}$`);\n return {\n pattern,\n match(route) {\n const found = matcher.exec(route);\n if (found === null) return null;\n const params: Record<string, string> = {};\n const wildcards: string[] = [];\n kinds.forEach((part, index) => {\n const text = found[index + 1] ?? \"\";\n if (part.kind === \"param\") params[part.name] = text;\n else wildcards.push(text);\n });\n return { params, wildcards };\n },\n };\n};\n","// The units of work of a worker loop and of a queue drain: the count of the units in flight, the\n// drain to a deadline with the cooperative abort, the sleeps on the clock, the bound of each unit,\n// and the request tracking of the service for each unit.\n\nimport type { Backoff } from \"./backoff.ts\";\nimport { backoffProblems } from \"./backoff.ts\";\nimport type { Cancel, Clock } from \"./clock.ts\";\nimport type { ServiceDescription } from \"./description.ts\";\nimport type { KnobRecord } from \"./knobs.ts\";\nimport { boundedMillisecondsOf } from \"./knobs.ts\";\nimport type { Lifecycle } from \"./lifecycle.ts\";\nimport type { RequestEnd } from \"./requests.ts\";\n\n/**\n * A unit request names the route of each unit of work in the request tracking of the service. Then\n * the service admits, counts, drains and cuts each unit as it does each request (`life.request`).\n * The controller declares the route, thus its idempotency tells if a cut unit can go again.\n */\nexport interface UnitRequest<U> {\n /** This name is the controller that declares the route. */\n readonly controller: string;\n /** This name is the route of each unit, or a function that gives it from the unit. A route can match a route pattern. */\n readonly route: string | ((unit: U) => string);\n /** This function gives the detail of a unit, for the function of a route pattern. */\n readonly detail?: ((unit: U) => unknown) | undefined;\n}\n\n/**\n * A unit end tells how one unit of work ended:\n * - `done`: the work gave its value.\n * - `failed`: the work threw for a cause of its own, or after its bound aborted it.\n * - `refused`: the service did not admit the unit, thus the work did not start.\n * - `cut`: the shutdown stopped the unit. It is not a failure. `retryable` tells if the unit can go again.\n */\nexport type UnitEnd =\n | { readonly kind: \"done\"; readonly value: unknown }\n | { readonly kind: \"failed\"; readonly error: unknown }\n | { readonly kind: \"refused\"; readonly cause: string }\n | { readonly kind: \"cut\"; readonly cause: string; readonly retryable: boolean };\n\n/** This function gives the cause of an abort as text. */\nexport const causeOf = (signal: AbortSignal): string =>\n signal.reason instanceof Error ? signal.reason.message : String(signal.reason ?? \"the unit got an abort\");\n\n/** These are the parts of one unit of work. */\nexport interface UnitRun<U> {\n readonly clock: Clock;\n /** This time is the bound of the unit, in milliseconds, or infinity. At it, the signal of the work aborts. */\n readonly bound: number;\n /** This controller is the abort of the transport for the unit: the cooperative abort and the close. */\n readonly abort: AbortController;\n readonly life: Lifecycle;\n readonly request: UnitRequest<U> | undefined;\n readonly unit: U;\n /** This function starts the work, and gives its value or a promise. */\n readonly work: (signal: AbortSignal) => unknown;\n}\n\n/**\n * This function does one unit of work, and gives its end. With a request, the unit goes through\n * `life.request`, thus the service counts it, drains it and can cut it. A throw after the abort of\n * the transport is a cut, not a failure (S6 A10). A throw after the bound is a failure.\n */\nexport const runUnit = async <U>(run: UnitRun<U>): Promise<UnitEnd> => {\n const { clock, bound, abort, life, request, unit, work } = run;\n const timeout = new AbortController();\n const cancel: Cancel = Number.isFinite(bound)\n ? clock.after(bound, () => timeout.abort(new Error(`the unit did not end in its unit_processing_time of ${bound} ms`)))\n : () => {};\n const call = async (tracker?: AbortSignal): Promise<unknown> =>\n work(AbortSignal.any(tracker === undefined ? [abort.signal, timeout.signal] : [abort.signal, timeout.signal, tracker]));\n // the transport cannot read the idempotency of the route, thus a cut of its own does not go again\n const thrown = (error: unknown): UnitEnd => {\n if (abort.signal.aborted) return { kind: \"cut\", cause: causeOf(abort.signal), retryable: false };\n if (timeout.signal.aborted) return { kind: \"failed\", error: timeout.signal.reason };\n return { kind: \"failed\", error };\n };\n try {\n if (request === undefined) {\n try {\n return { kind: \"done\", value: await call() };\n } catch (error) {\n return thrown(error);\n }\n }\n let collected: Promise<{ readonly items: readonly unknown[]; readonly end: RequestEnd }>;\n try {\n const route = typeof request.route === \"string\" ? request.route : request.route(unit);\n const detail = request.detail?.(unit);\n // the value goes in a box: a work that gives an async iterable is one value here, not a stream\n const stream = life.request(request.controller, route, (signal) => call(signal).then((value) => ({ value })), detail === undefined ? {} : { detail });\n collected = stream.collect();\n } catch (error) {\n // an undeclared route, or a lifecycle with no service\n return { kind: \"failed\", error };\n }\n const { items, end } = await collected;\n if (end.kind === \"done\") return { kind: \"done\", value: (items[0] as { readonly value: unknown } | undefined)?.value };\n if (end.kind === \"failed\") return thrown(end.error);\n if (end.kind === \"refused\") return { kind: \"refused\", cause: end.cause };\n return { kind: \"cut\", cause: end.cause, retryable: end.retryable };\n } finally {\n cancel();\n }\n};\n\n/** A flight set counts the units in flight, from their events, and drains them to a deadline. */\nexport interface Flights {\n /** This count is the number of units in flight. */\n readonly size: number;\n /** This method adds a unit, and gives the function that removes it. A unit after the cooperative abort gets the abort at once. */\n add(unit: AbortController): () => void;\n /** This method aborts each unit in flight and each later unit: the cooperative abort, or the close. */\n abort(reason: unknown): void;\n /** This method removes each unit from the count: the close cuts them. */\n clear(): void;\n /** This method waits until no unit is in flight, or until the deadline. The signal is the cooperative abort. It does not throw. */\n drain(deadline: number, signal: AbortSignal): Promise<void>;\n /** This method gives a promise that ends when the count goes down, or at `release()`. */\n less(): Promise<void>;\n /** This method ends each promise of `less()`. */\n release(): void;\n}\n\n/** This function makes a flight set on a clock. */\nexport const flights = (clock: Clock): Flights => {\n const units = new Set<AbortController>();\n const idle = new Set<() => void>();\n const lower = new Set<() => void>();\n let aborted: { readonly reason: unknown } | null = null;\n const settle = (): void => {\n for (const done of [...lower]) done();\n lower.clear();\n if (units.size === 0) for (const done of [...idle]) done();\n };\n const abort = (reason: unknown): void => {\n aborted ??= { reason };\n for (const unit of [...units]) if (!unit.signal.aborted) unit.abort(aborted.reason);\n };\n return {\n get size() {\n return units.size;\n },\n add(unit) {\n units.add(unit);\n if (aborted !== null && !unit.signal.aborted) unit.abort(aborted.reason);\n return () => {\n if (units.delete(unit)) settle();\n };\n },\n abort,\n clear() {\n units.clear();\n settle();\n },\n drain(deadline, signal) {\n const cooperative = (): void => abort(signal.reason);\n if (signal.aborted) cooperative();\n else signal.addEventListener(\"abort\", cooperative, { once: true });\n // nothing in flight, or a deadline that passed: the drain ends at once, with no timer\n if (units.size === 0 || deadline <= clock.now()) {\n signal.removeEventListener(\"abort\", cooperative);\n return Promise.resolve();\n }\n return new Promise<void>((resolve) => {\n let cancel: Cancel = () => {};\n const done = (): void => {\n idle.delete(done);\n cancel();\n signal.removeEventListener(\"abort\", cooperative);\n resolve();\n };\n idle.add(done);\n cancel = clock.after(Math.max(0, deadline - clock.now()), done);\n });\n },\n less() {\n return new Promise<void>((resolve) => lower.add(resolve));\n },\n release() {\n for (const done of [...lower]) done();\n lower.clear();\n },\n };\n};\n\n/**\n * A sleeper gives the waits of a loop on the clock. `wake()` ends each wait that it can end, and\n * `stop()` ends each wait. A wait without a bound uses no timer.\n */\nexport interface Sleeper {\n /** This count goes up at each `wake()`. A loop reads it before a unit, and a wake during the unit then ends the next sleep at once. */\n readonly wakes: number;\n /** This method waits on the clock. A wake after `since` ends it, and with `since` past, it ends at once. */\n sleep(ms: number, since: number): Promise<void>;\n /** This method waits on the clock. A wake does not end it: it is a backoff after a failure. */\n backoff(ms: number): Promise<void>;\n /** This method ends each sleep, and gives the count of sleeps that it ended. */\n wake(): number;\n /** This method ends each wait, the backoffs too. Each later wait then ends at once. */\n stop(): void;\n}\n\n/** This function makes a sleeper on a clock. */\nexport const sleeper = (clock: Clock): Sleeper => {\n let wakes = 0;\n let stopped = false;\n const sleeps = new Set<() => void>();\n const backoffs = new Set<() => void>();\n const wait = (ms: number, into: Set<() => void>): Promise<void> =>\n stopped\n ? Promise.resolve()\n : new Promise<void>((resolve) => {\n let cancel: Cancel = () => {};\n const done = (): void => {\n if (!into.delete(done)) return;\n cancel();\n resolve();\n };\n into.add(done);\n if (Number.isFinite(ms)) cancel = clock.after(ms, done);\n });\n return {\n get wakes() {\n return wakes;\n },\n sleep(ms, since) {\n if (since !== wakes) return Promise.resolve();\n return wait(ms, sleeps);\n },\n backoff(ms) {\n return wait(ms, backoffs);\n },\n wake() {\n wakes += 1;\n const ended = sleeps.size;\n for (const done of [...sleeps]) done();\n return ended;\n },\n stop() {\n stopped = true;\n for (const done of [...sleeps, ...backoffs]) done();\n },\n };\n};\n\n/** A settled wait tells how a work ended in its bound: it ended, it threw, or the bound came first. */\nexport type Settled = { readonly kind: \"done\" } | { readonly kind: \"failed\"; readonly error: unknown } | { readonly kind: \"bound\" };\n\n/** This function waits for a work until a bound on the clock, in milliseconds. */\nexport const settleWithin = (clock: Clock, ms: number, work: Promise<unknown>): Promise<Settled> =>\n new Promise<Settled>((resolve) => {\n const cancel: Cancel = Number.isFinite(ms) ? clock.after(ms, () => resolve({ kind: \"bound\" })) : () => {};\n work.then(\n () => {\n cancel();\n resolve({ kind: \"done\" });\n },\n (error: unknown) => {\n cancel();\n resolve({ kind: \"failed\", error });\n },\n );\n });\n\n/**\n * This function gives the value of a promise, or `stop` when the signal aborts first. Each wait adds\n * its own listener, and removes it when the promise settles. A race against one pending promise\n * keeps each settled value until that promise ends.\n */\nexport const untilAbort = <T, S>(promise: Promise<T>, signal: AbortSignal, stop: S): Promise<T | S> => {\n // A promise that settled before the abort still comes first, as in a race.\n if (signal.aborted) return Promise.race([promise, Promise.resolve(stop)]);\n return new Promise<T | S>((resolve, reject) => {\n const onAbort = (): void => resolve(stop);\n signal.addEventListener(\"abort\", onAbort, { once: true });\n promise.then(\n (value) => {\n signal.removeEventListener(\"abort\", onAbort);\n resolve(value);\n },\n (error: unknown) => {\n signal.removeEventListener(\"abort\", onAbort);\n reject(error);\n },\n );\n });\n};\n\n/**\n * This function gives the backoff of the knobs of a loop. The first delay is `retry_base`, and each\n * delay is two times longer, up to `retry_cap`. Each delay has a jitter up to the base, as in the Redis client.\n */\nexport const retryOf = (knobs: KnobRecord, owner: string): Backoff => {\n const base = boundedMillisecondsOf(knobs, \"retry_base\", owner);\n const cap = boundedMillisecondsOf(knobs, \"retry_cap\", owner);\n const backoff: Backoff = { base, factor: 2, cap, jitter: { kind: \"add\", amount: base } };\n const problems = backoffProblems(backoff);\n if (problems.length > 0) throw new Error(`${owner}: the retry_base and retry_cap give no backoff: ${problems.join(\", \")}`);\n return backoff;\n};\n\n/**\n * This function gives the problems of a loop in a declaration: each phase that the loop needs and\n * that the declaration does not give. A loop closes in `close transports`, and it ends its units in\n * `drain`. `lint()` compares the bounds with the caps of the phases (`BOUND_RULES`, S6 A10).\n */\nexport const boundProblems = (owner: string, description: ServiceDescription): string[] => {\n const has = (name: string): boolean => description.shutdown.phases.some((each) => each.name === name);\n return [\n ...(has(\"close transports\") ? [] : [`${owner} needs the phase \"close transports\" to close`]),\n ...(has(\"drain\") ? [] : [`${owner} needs the phase \"drain\" to end its units`]),\n ];\n};\n","import { quantity } from \"@mark1russell7/quantity\";\nimport type { Clock } from \"./clock.ts\";\nimport type { Controller } from \"./description.ts\";\nimport type { Operation } from \"./operation.ts\";\nimport { mayReissue } from \"./operation.ts\";\nimport type { CompiledPattern, OperationOf } from \"./routes.ts\";\nimport { compilePattern } from \"./routes.ts\";\nimport { untilAbort } from \"./units.ts\";\n\n/**\n * A request end is the one final result of the stream of a request. Its count `delivered` is the\n * number of items that the stream gave before it. An end after the first item is in-band. The\n * transport sent its answer, thus it writes the end as the last line of the stream, not as a status.\n * - `done`: the work gave each of its items.\n * - `failed`: the work failed for a cause of its own. Before the first item, the adapter answers it\n * as it answers any error of the route.\n * - `refused`: the service did not admit the request. A refused request did not start, thus its\n * caller can send it again: 503, retryable.\n * - `cut`: the shutdown stopped the request. Before the first item, a route whose callers can send\n * it again answers 503, retryable, and each other route answers 500. After the first item, the\n * status is null, and the end is in-band.\n */\nexport type RequestEnd =\n | { readonly kind: \"done\"; readonly delivered: number }\n | { readonly kind: \"failed\"; readonly error: unknown; readonly delivered: number }\n | {\n readonly kind: \"refused\";\n readonly status: 503;\n readonly retryable: true;\n readonly cause: string;\n readonly delivered: 0;\n }\n | {\n readonly kind: \"cut\";\n readonly status: 503 | 500 | null;\n readonly retryable: boolean;\n readonly cause: string;\n readonly delivered: number;\n };\n\n/** A request event is one event of the stream of a request: an item, or the one end, which comes last. */\nexport type RequestEvent<T> = { readonly kind: \"item\"; readonly value: T } | RequestEnd;\n\n/**\n * A request stream is what a request comes to: its items, then one end. A unary work gives a stream\n * of one item. The stream gives an item only when its reader asks for the next event. Thus a slow\n * reader slows the work, and the request stays in flight until the work ends. When the reader\n * stops before the end, the work gets its abort and the `return()` of its iterator.\n */\nexport interface RequestStream<T> extends AsyncIterable<RequestEvent<T>> {\n /** This method reads the whole stream, and gives its items and its end. */\n collect(): Promise<{ readonly items: readonly T[]; readonly end: RequestEnd }>;\n}\n\n/**\n * A request work is what a request function gives: one value, or a stream of values. A work that\n * gives an async iterable is a stream, also through a promise. Each other value is a stream of one.\n */\nexport type RequestWork<T> = T | PromiseLike<T> | AsyncIterable<T> | PromiseLike<AsyncIterable<T>>;\n\n/** A request function does the work of one request. It receives a signal that the shutdown aborts. */\nexport type RequestFn<T> = (signal: AbortSignal) => RequestWork<T>;\n\n/** These options go with one request. */\nexport interface RequestOptions {\n /** This value goes to the function of a route pattern, for example the metadata of a procedure. */\n readonly detail?: unknown;\n}\n\n/** A tracker admits requests, counts the requests in flight, and ends what the shutdown cuts. */\nexport interface Tracker {\n request<T>(controller: string, route: string, work: RequestFn<T>, options?: RequestOptions): RequestStream<T>;\n inFlight(controller?: string): number;\n /** This method refuses each later request. */\n stopIntake(): void;\n /** This method aborts the signal of each request in flight, and ends each stream through `return()`: the cooperative abort of the drain. */\n cooperativeAbort(): void;\n /** This method waits until no request is in flight, or until `deadline`. Then it cuts each request that is left. */\n drain(deadline: number): Promise<void>;\n}\n\ninterface InFlight {\n readonly controller: string;\n /** This function aborts the work, and ends its iterator through `return()`. */\n readonly cooperative: () => void;\n /** This function ends the request with a cut, one time. */\n readonly cut: (cause: string) => void;\n}\n\nconst isAsyncIterable = (value: unknown): value is AsyncIterable<unknown> =>\n value !== null &&\n (typeof value === \"object\" || typeof value === \"function\") &&\n typeof (value as { [Symbol.asyncIterator]?: unknown })[Symbol.asyncIterator] === \"function\";\nconst isPromiseLike = (value: unknown): value is PromiseLike<unknown> =>\n value !== null &&\n (typeof value === \"object\" || typeof value === \"function\") &&\n typeof (value as { then?: unknown }).then === \"function\";\n\n/** This function gives a stream of events from a function that gives the next event. */\nconst streamOf = <T>(next: () => Promise<RequestEvent<T> | null>, stop: () => void): RequestStream<T> => {\n let chain: Promise<unknown> = Promise.resolve();\n const iterator: AsyncIterator<RequestEvent<T>> = {\n next() {\n // a reader that asks again before an answer gets the events in their sequence\n const step = chain.then(next).then((event): IteratorResult<RequestEvent<T>> =>\n event === null ? { value: undefined, done: true } : { value: event, done: false },\n );\n chain = step.catch(() => {});\n return step;\n },\n return() {\n stop();\n return Promise.resolve({ value: undefined, done: true });\n },\n };\n return {\n [Symbol.asyncIterator]: () => iterator,\n async collect() {\n const items: T[] = [];\n for (;;) {\n const step = await iterator.next();\n if (step.done === true) throw new Error(\"the stream ended without its end\");\n if (step.value.kind === \"item\") items.push(step.value.value);\n else return { items, end: step.value };\n }\n },\n };\n};\n\n/**\n * This function makes a tracker. `refuses` gives the reason that the service refuses a new request\n * for a controller at this time, or null when it admits it. `choices` gives the function of each\n * route pattern, by controller and by pattern.\n */\nexport const tracker = (\n controllers: readonly Controller[],\n clock: Clock,\n refuses: (controller: string) => string | null,\n choices: ReadonlyMap<string, ReadonlyMap<string, OperationOf>> = new Map(),\n): Tracker => {\n const byName = new Map(\n controllers.map((controller) => [\n controller.name,\n {\n controller,\n patterns: (controller.patterns ?? []).map((declared) => ({ declared, compiled: compilePattern(declared.pattern) as CompiledPattern })),\n },\n ]),\n );\n const flights = new Set<InFlight>();\n const idle = new Set<() => void>();\n let intake = true;\n\n /** This function gives the operation of a request: its exact route, else the first pattern that matches it. */\n const operationOf = (controller: string, route: string, detail: unknown): Operation => {\n const held = byName.get(controller);\n if (held === undefined) throw new Error(`there is no controller ${controller}`);\n const exact = held.controller.routes[route];\n if (exact !== undefined) return exact;\n for (const { declared, compiled } of held.patterns) {\n const found = compiled.match(route);\n if (found === null) continue;\n const names = Object.keys(declared.operations);\n const choose = choices.get(controller)?.get(declared.pattern);\n const name = choose === undefined ? names[0]! : choose({ controller, route, ...found, detail });\n const operation = declared.operations[name];\n if (operation === undefined)\n throw new Error(`the pattern ${declared.pattern} of ${controller} declares no operation ${String(name)} for ${route}`);\n return operation;\n }\n // a route that nobody declared has no idempotency: the runtime has no defaults\n throw new Error(`controller ${controller} does not declare the route ${route}`);\n };\n\n const settle = (flight: InFlight): void => {\n if (!flights.delete(flight)) return;\n if (flights.size === 0) for (const done of [...idle]) done();\n };\n\n /** This function starts the work of an admitted request, and gives its stream. */\n const open = <T>(controller: string, operation: Operation, work: RequestFn<T>): RequestStream<T> => {\n const started = clock.now();\n const abort = new AbortController();\n let delivered = 0;\n let end: RequestEnd | null = null;\n let ended = false;\n let returned = false;\n let source: AsyncIterator<T> | null = null;\n // a unary work keeps its value until the reader asks for it\n let unary: { readonly value: T } | null = null;\n // Each wait of the reader listens for the end with a listener of its own, thus a long stream keeps no item that went.\n const interruption = new AbortController();\n const interrupt = (): void => interruption.abort();\n\n const cutEnd = (cause: string): RequestEnd => {\n const since = quantity(Math.max(0, clock.now() - started) / 1000, \"s\");\n const retryable = mayReissue(operation, since);\n return { kind: \"cut\", status: delivered > 0 ? null : retryable ? 503 : 500, retryable, cause, delivered };\n };\n /** This function ends the work of the request: the end is known, and the request leaves the flights. */\n const finish = (given: RequestEnd): void => {\n if (end !== null) return;\n end = given;\n settle(flight);\n interrupt();\n };\n /** This function ends the work after a throw: a failure after the shutdown aborted it is a cut. */\n const failed = (error: unknown): void =>\n finish(abort.signal.aborted ? cutEnd(\"the shutdown aborted it, and it failed\") : { kind: \"failed\", error, delivered });\n const closeSource = (): void => {\n if (source === null || returned) return;\n returned = true;\n try {\n void Promise.resolve(source.return?.()).catch(() => {});\n } catch {\n // an iterator whose return() throws has ended\n }\n };\n const flight: InFlight = {\n controller,\n cooperative: () => {\n abort.abort(new Error(\"the cooperative abort of the drain\"));\n closeSource();\n },\n cut: (cause) => {\n abort.abort(new Error(cause));\n finish(cutEnd(cause));\n closeSource();\n },\n };\n flights.add(flight);\n\n /** This function takes the result of the work: a stream, or one value. */\n const take = (result: unknown): void => {\n if (end !== null) return;\n if (isAsyncIterable(result)) {\n source = (result as AsyncIterable<T>)[Symbol.asyncIterator]();\n // a stream that comes after the cooperative abort ends at once\n if (abort.signal.aborted) closeSource();\n } else {\n unary = { value: result as T };\n // the unary work has ended: its value waits for the reader\n settle(flight);\n }\n };\n let ready: Promise<void>;\n try {\n const result = work(abort.signal);\n if (isAsyncIterable(result) || !isPromiseLike(result)) {\n take(result);\n ready = Promise.resolve();\n } else ready = Promise.resolve(result).then(take, failed);\n } catch (error) {\n failed(error);\n ready = Promise.resolve();\n }\n\n const next = async (): Promise<RequestEvent<T> | null> => {\n if (ended) return null;\n if (end === null) await untilAbort(ready, interruption.signal, \"interrupted\");\n if (end === null && unary !== null) {\n const { value } = unary;\n unary = null;\n delivered = 1;\n end = { kind: \"done\", delivered };\n return { kind: \"item\", value };\n }\n if (end === null && source !== null) {\n const pending = source.next();\n let step: IteratorResult<T> | \"interrupted\";\n try {\n step = await untilAbort(Promise.resolve(pending), interruption.signal, \"interrupted\" as const);\n } catch (error) {\n failed(error);\n step = \"interrupted\";\n }\n if (step !== \"interrupted\") {\n if (step.done !== true) {\n delivered += 1;\n return { kind: \"item\", value: step.value };\n }\n // a stream that ends after the abort of the shutdown is a cut\n finish(abort.signal.aborted ? cutEnd(\"the shutdown ended the stream\") : { kind: \"done\", delivered });\n }\n }\n ended = true;\n return end!;\n };\n const stop = (): void => {\n // the reader stopped before the end: the work stops too\n if (end === null) {\n abort.abort(new Error(\"the reader stopped the stream\"));\n finish({ kind: \"done\", delivered });\n closeSource();\n }\n ended = true;\n };\n return streamOf(next, stop);\n };\n\n return {\n request<T>(controller: string, route: string, work: RequestFn<T>, options: RequestOptions = {}) {\n const operation = operationOf(controller, route, options.detail);\n const refusal = intake ? refuses(controller) : \"the service stopped its intake\";\n if (refusal !== null) {\n let given = false;\n const refused: RequestEnd = { kind: \"refused\", status: 503, retryable: true, cause: refusal, delivered: 0 };\n return streamOf<T>(\n () => {\n if (given) return Promise.resolve(null);\n given = true;\n return Promise.resolve(refused);\n },\n () => {\n given = true;\n },\n );\n }\n return open(controller, operation, work);\n },\n inFlight(controller) {\n let count = 0;\n for (const flight of flights) if (controller === undefined || flight.controller === controller) count += 1;\n return count;\n },\n stopIntake() {\n intake = false;\n },\n cooperativeAbort() {\n for (const flight of [...flights]) flight.cooperative();\n },\n drain(deadline) {\n if (flights.size === 0) return Promise.resolve();\n return new Promise<void>((resolve) => {\n let finished = false;\n const done = (): void => {\n if (finished) return;\n finished = true;\n idle.delete(done);\n cancel();\n resolve();\n };\n idle.add(done);\n const cancel = clock.after(Math.max(0, deadline - clock.now()), () => {\n for (const flight of [...flights]) flight.cut(\"the drain reached its deadline\");\n done();\n });\n });\n },\n };\n};\n"],"mappings":";;;;;AA8CA,MAAM,OAAO;;AAGb,MAAa,mBAAmB,OAAe,YAA8B;CAC3E,MAAM,WAAqB,CAAC;CAC5B,IAAI,QAAQ,WAAW,GAAG,SAAS,KAAK,GAAG,MAAM,4BAA4B;CAC7E,MAAM,wBAAQ,IAAI,IAAY;CAC9B,KAAK,IAAI,KAAK,GAAG,KAAK,QAAQ,QAAQ,MAAM,GAAG;EAC7C,IAAI,QAAQ,QAAQ,KAAK;EACzB,MAAM,OAAO,KAAK,KAAK,QAAQ,MAAM,KAAK,C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