@procedures/client 0.15.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +60 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/adapters/http/server/handler.d.ts.map +1 -1
- package/dist/adapters/http/server/handler.js +5 -3
- package/dist/adapters/http/server/handler.js.map +1 -1
- package/dist/browser.d.ts +2 -1
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js.map +1 -1
- package/dist/client/errors/registry.d.ts.map +1 -1
- package/dist/client/errors/registry.js +12 -0
- package/dist/client/errors/registry.js.map +1 -1
- package/dist/client/middleware/rate-limit.d.ts +17 -0
- package/dist/client/middleware/rate-limit.d.ts.map +1 -1
- package/dist/client/middleware/rate-limit.js +94 -39
- package/dist/client/middleware/rate-limit.js.map +1 -1
- package/dist/events/index.d.ts +1 -0
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js.map +1 -1
- package/dist/events/publisher.d.ts +46 -1
- package/dist/events/publisher.d.ts.map +1 -1
- package/dist/events/publisher.js +95 -5
- package/dist/events/publisher.js.map +1 -1
- package/dist/events/recheck.d.ts +84 -0
- package/dist/events/recheck.d.ts.map +1 -0
- package/dist/events/recheck.js +135 -0
- package/dist/events/recheck.js.map +1 -0
- package/dist/protocol.d.ts +1 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +1 -0
- package/dist/protocol.js.map +1 -1
- package/dist/server/host-policy.d.ts +17 -0
- package/dist/server/host-policy.d.ts.map +1 -1
- package/dist/server/host-policy.js +7 -0
- package/dist/server/host-policy.js.map +1 -1
- package/dist/server/limits.d.ts +122 -0
- package/dist/server/limits.d.ts.map +1 -0
- package/dist/server/limits.js +153 -0
- package/dist/server/limits.js.map +1 -0
- package/dist/server/procedure-server.d.ts +31 -1
- package/dist/server/procedure-server.d.ts.map +1 -1
- package/dist/server/procedure-server.js +38 -2
- package/dist/server/procedure-server.js.map +1 -1
- package/dist/server/request-auth.d.ts +5 -1
- package/dist/server/request-auth.d.ts.map +1 -1
- package/dist/server/request-auth.js +19 -2
- package/dist/server/request-auth.js.map +1 -1
- package/package.json +3 -3
- package/src/adapters/http/server/handler.ts +5 -3
- package/src/browser.ts +4 -0
- package/src/client/errors/registry.ts +13 -0
- package/src/client/middleware/rate-limit.ts +116 -41
- package/src/events/index.ts +1 -0
- package/src/events/publisher.ts +116 -6
- package/src/events/recheck.ts +182 -0
- package/src/protocol.ts +3 -1
- package/src/server/host-policy.ts +22 -0
- package/src/server/limits.ts +254 -0
- package/src/server/procedure-server.ts +55 -3
- package/src/server/request-auth.ts +19 -2
package/src/events/publisher.ts
CHANGED
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
*
|
|
19
19
|
* - A value with rows (`withRows()`) reaches each subscriber through the output filter of its host
|
|
20
20
|
* (`ctx.admit`): each row passes the filter, and the subscriber gets only the admitted rows.
|
|
21
|
+
* - An open subscription gets the same checks again at a change of the rights (`rightsChanged()`,
|
|
22
|
+
* `recheck.rightsChanges`) and after `recheck.maxAge` (roadmap CLI-44). A refusal ends it.
|
|
21
23
|
*
|
|
22
24
|
* The delivery is at most once. The queue of a subscription holds at most `buffer` events. When
|
|
23
25
|
* it is full, the publisher drops the oldest event, and the next event tells the number of lost
|
|
@@ -36,6 +38,8 @@ import type { EventBus, Unsubscribe } from "./types.js";
|
|
|
36
38
|
import { getGlobalEventBus } from "./bus.js";
|
|
37
39
|
import { EVENTS_SUBSCRIBE_PATH, type EventItem, type SubscribeInput } from "./remote.js";
|
|
38
40
|
import { admittedValue, rowsOf } from "./rows.js";
|
|
41
|
+
import { realClock, type Clock } from "../clock.js";
|
|
42
|
+
import { SubscriptionRechecks, type RightsChange, type SubscriptionRecheck } from "./recheck.js";
|
|
39
43
|
|
|
40
44
|
/** The number of topics that one subscription can name. */
|
|
41
45
|
export const MAX_SUBSCRIPTION_TOPICS = 64;
|
|
@@ -73,7 +77,8 @@ export interface EventPublisherOptions {
|
|
|
73
77
|
allow?: ((topic: string, call: TopicCall) => boolean) | undefined;
|
|
74
78
|
/**
|
|
75
79
|
* The number of events that one subscription holds for a slow subscriber. When the queue is
|
|
76
|
-
* full, the publisher drops the oldest event.
|
|
80
|
+
* full, the publisher drops the oldest event. The events that wait for a re-check (roadmap
|
|
81
|
+
* CLI-44) are in the same queue, so a slow check holds at most this number of events too.
|
|
77
82
|
* @default 1024
|
|
78
83
|
*/
|
|
79
84
|
buffer?: number | undefined;
|
|
@@ -89,6 +94,23 @@ export interface EventPublisherOptions {
|
|
|
89
94
|
* @default the read of the collection of `collectionTopicTarget()`, with the repository of the call
|
|
90
95
|
*/
|
|
91
96
|
reads?: ((topic: string) => readonly string[]) | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* The clock of the re-check after `recheck.maxAge` (roadmap CLI-44). A test gives the
|
|
99
|
+
* `fakeClock` of `@mark1russell7/service/testing`. A `ProcedureServer` gives its own clock to the
|
|
100
|
+
* publisher that it makes from its option `events`.
|
|
101
|
+
* @default the clock of the platform
|
|
102
|
+
*/
|
|
103
|
+
clock?: Clock | undefined;
|
|
104
|
+
/**
|
|
105
|
+
* The re-check of the admission of each open subscription (roadmap CLI-44). A right can change
|
|
106
|
+
* after the start of a subscription. The publisher then examines the subscription again with the
|
|
107
|
+
* same checks: the topic rule, the reads, `allow` and the partition. A refusal ends it, for
|
|
108
|
+
* example with `FORBIDDEN`. `maxAge` starts a check after a time on the clock, and
|
|
109
|
+
* `rightsChanges` starts a check at each change from a source of the app. `rightsChanged()` also
|
|
110
|
+
* starts a check.
|
|
111
|
+
* @default no check after the start, other than at a call of `rightsChanged()`
|
|
112
|
+
*/
|
|
113
|
+
recheck?: SubscriptionRecheck | undefined;
|
|
92
114
|
}
|
|
93
115
|
|
|
94
116
|
/** A procedure that reads resources, for the admission of a topic under a capability. */
|
|
@@ -167,6 +189,10 @@ class TopicSubscription implements AsyncIterableIterator<EventItem> {
|
|
|
167
189
|
private closed = false;
|
|
168
190
|
private failure: unknown;
|
|
169
191
|
private readonly unsubscribes: Unsubscribe[];
|
|
192
|
+
/** The re-check of the admission that runs (roadmap CLI-44). The events wait for it. */
|
|
193
|
+
private recheck: Promise<void> | undefined;
|
|
194
|
+
/** The functions that run when the subscription ends */
|
|
195
|
+
private readonly endListeners: Array<() => void> = [];
|
|
170
196
|
|
|
171
197
|
constructor(
|
|
172
198
|
bus: EventBus,
|
|
@@ -201,7 +227,7 @@ class TopicSubscription implements AsyncIterableIterator<EventItem> {
|
|
|
201
227
|
if (this.closed) return;
|
|
202
228
|
const event = { type: "event" as const, topic, data, seq: ++this.seq };
|
|
203
229
|
const waiter = this.waiter;
|
|
204
|
-
if (waiter && this.queue.length === 0) {
|
|
230
|
+
if (waiter && this.queue.length === 0 && !this.recheck) {
|
|
205
231
|
this.waiter = undefined;
|
|
206
232
|
waiter.resolve(this.deliver(event));
|
|
207
233
|
return;
|
|
@@ -239,6 +265,8 @@ class TopicSubscription implements AsyncIterableIterator<EventItem> {
|
|
|
239
265
|
this.confirmation = undefined;
|
|
240
266
|
return Promise.resolve({ value: confirmation, done: false });
|
|
241
267
|
}
|
|
268
|
+
// The events wait for a re-check of the admission (roadmap CLI-44)
|
|
269
|
+
if (this.recheck) return this.recheck.then(() => this.next());
|
|
242
270
|
const event = this.queue.shift();
|
|
243
271
|
if (event) return this.deliver(event);
|
|
244
272
|
if (this.failure !== undefined) {
|
|
@@ -295,9 +323,51 @@ class TopicSubscription implements AsyncIterableIterator<EventItem> {
|
|
|
295
323
|
for (const unsubscribe of this.unsubscribes) unsubscribe();
|
|
296
324
|
this.signal?.removeEventListener("abort", this.onAbort);
|
|
297
325
|
this.onClose(this);
|
|
326
|
+
for (const listener of this.endListeners.splice(0)) listener();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* This function holds the events until the check ends (roadmap CLI-44). A check that passes gives
|
|
331
|
+
* the held events to the reader. A check that fails ends the subscription with its error, and the
|
|
332
|
+
* held events go. The promise ends when the check ends, and it does not reject.
|
|
333
|
+
*/
|
|
334
|
+
hold(check: Promise<void>): Promise<void> {
|
|
335
|
+
if (this.closed) return check.then(ignore, ignore);
|
|
336
|
+
const running: Promise<void> = check.then(
|
|
337
|
+
() => {
|
|
338
|
+
if (this.recheck === running) this.recheck = undefined;
|
|
339
|
+
this.flush();
|
|
340
|
+
},
|
|
341
|
+
(error: unknown) => {
|
|
342
|
+
if (this.recheck === running) this.recheck = undefined;
|
|
343
|
+
this.queue.length = 0;
|
|
344
|
+
this.fail(error);
|
|
345
|
+
},
|
|
346
|
+
);
|
|
347
|
+
this.recheck = running;
|
|
348
|
+
return running;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** This function runs the listener when the subscription ends, or at once after its end. */
|
|
352
|
+
onEnd(listener: () => void): void {
|
|
353
|
+
if (this.closed) listener();
|
|
354
|
+
else this.endListeners.push(listener);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** This function gives the first held event to a reader that waits, after a check passes. */
|
|
358
|
+
private flush(): void {
|
|
359
|
+
const waiter = this.waiter;
|
|
360
|
+
const event = this.queue[0];
|
|
361
|
+
if (!waiter || !event || this.recheck) return;
|
|
362
|
+
this.waiter = undefined;
|
|
363
|
+
this.queue.shift();
|
|
364
|
+
waiter.resolve(this.deliver(event));
|
|
298
365
|
}
|
|
299
366
|
}
|
|
300
367
|
|
|
368
|
+
/** This function does nothing: the end of a check of a closed subscription. */
|
|
369
|
+
function ignore(): void {}
|
|
370
|
+
|
|
301
371
|
/**
|
|
302
372
|
* The publisher of the remote events: it serves `events.subscribe` from a bus, under a topic
|
|
303
373
|
* rule. `ProcedureServer` makes one from its option `events`.
|
|
@@ -321,8 +391,11 @@ export class EventPublisher {
|
|
|
321
391
|
private readonly defaultReads: boolean;
|
|
322
392
|
private readonly buffer: number;
|
|
323
393
|
private readonly open = new Set<TopicSubscription>();
|
|
394
|
+
/** The re-checks of the admission of the open subscriptions (roadmap CLI-44) */
|
|
395
|
+
private readonly rechecks: SubscriptionRechecks;
|
|
324
396
|
|
|
325
397
|
constructor(options: EventPublisherOptions) {
|
|
398
|
+
this.rechecks = new SubscriptionRechecks(options.recheck ?? {}, options.clock ?? realClock);
|
|
326
399
|
this.bus = options.bus ?? getGlobalEventBus();
|
|
327
400
|
this.known = topicMatcher(options.topics);
|
|
328
401
|
this.allow = options.allow;
|
|
@@ -360,6 +433,18 @@ export class EventPublisher {
|
|
|
360
433
|
for (const subscription of [...this.open]) subscription.fail(stoppedError());
|
|
361
434
|
}
|
|
362
435
|
|
|
436
|
+
/**
|
|
437
|
+
* This function tells the publisher that rights changed (roadmap CLI-44). Each open subscription
|
|
438
|
+
* that the change touches gets the checks of its start again: the topic rule, the reads, `allow`
|
|
439
|
+
* and the partition. Its events wait for the check. A refusal ends the subscription with its
|
|
440
|
+
* error, for example `FORBIDDEN`. The promise ends when each check ends, and it does not reject.
|
|
441
|
+
*
|
|
442
|
+
* @param change - The subscriptions that the change touches. Without it, each open subscription gets a check.
|
|
443
|
+
*/
|
|
444
|
+
rightsChanged(change?: RightsChange): Promise<void> {
|
|
445
|
+
return this.rechecks.rightsChanged(change);
|
|
446
|
+
}
|
|
447
|
+
|
|
363
448
|
/**
|
|
364
449
|
* The topic of the bus for a topic of a subscriber (roadmap CLI-17). A topic of a partitioned
|
|
365
450
|
* collection names the partition that the subscriber asks for, as the input of a procedure does:
|
|
@@ -385,11 +470,10 @@ export class EventPublisher {
|
|
|
385
470
|
}
|
|
386
471
|
|
|
387
472
|
/**
|
|
388
|
-
* The
|
|
389
|
-
*
|
|
473
|
+
* The checks of the topics of a call: the topic rule, the reads of the capability and `allow`.
|
|
474
|
+
* A subscription passes them at its start and at each re-check (roadmap CLI-44).
|
|
390
475
|
*/
|
|
391
|
-
private
|
|
392
|
-
const unique = [...new Set(topics)];
|
|
476
|
+
private admitTopics(unique: readonly string[], ctx: ProcedureContext): void {
|
|
393
477
|
for (const topic of unique) {
|
|
394
478
|
if (!this.known(topic)) throw new TopicNotFoundError(`The host has no topic "${topic}"`);
|
|
395
479
|
}
|
|
@@ -408,10 +492,36 @@ export class EventPublisher {
|
|
|
408
492
|
}
|
|
409
493
|
}
|
|
410
494
|
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* The admission of an open subscription again (roadmap CLI-44): the checks of its start, and the
|
|
499
|
+
* partition of each topic. A refusal of the resolver throws its error. A topic whose partition
|
|
500
|
+
* changed throws `FORBIDDEN` (reason `partition-not-allowed`), because the listener has the old
|
|
501
|
+
* partition.
|
|
502
|
+
*/
|
|
503
|
+
private async readmit(routes: ReadonlyArray<readonly [string, string]>, ctx: ProcedureContext): Promise<void> {
|
|
504
|
+
this.admitTopics(routes.map(([topic]) => topic), ctx);
|
|
505
|
+
for (const [topic, busTopic] of routes) {
|
|
506
|
+
if ((await this.busTopic(topic, ctx)) !== busTopic) {
|
|
507
|
+
throw new InvocationError("FORBIDDEN", `The partition of the topic "${topic}" changed, so the subscription ends`, ctx.path, "partition-not-allowed");
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* The stream of one subscription. The publisher examines each topic before it adds a listener:
|
|
514
|
+
* the topic rule, the reads, `allow`, and the partition. A call with one bad topic makes no subscription.
|
|
515
|
+
*/
|
|
516
|
+
private async subscribe(topics: readonly string[], ctx: ProcedureContext): Promise<TopicSubscription> {
|
|
517
|
+
const unique = [...new Set(topics)];
|
|
518
|
+
this.admitTopics(unique, ctx);
|
|
411
519
|
const routes: Array<readonly [string, string]> = [];
|
|
412
520
|
for (const topic of unique) routes.push([topic, await this.busTopic(topic, ctx)]);
|
|
413
521
|
const subscription = new TopicSubscription(this.bus, routes, this.buffer, ctx.signal, (closed) => this.open.delete(closed), ctx.admit, ctx.invocation);
|
|
414
522
|
this.open.add(subscription);
|
|
523
|
+
// The same checks run again at a change of the rights and after the max age (roadmap CLI-44)
|
|
524
|
+
this.rechecks.watch(subscription, ctx.principal, () => this.readmit(routes, ctx));
|
|
415
525
|
return subscription.closeIfAborted();
|
|
416
526
|
}
|
|
417
527
|
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The re-check of the admission of the open subscriptions (roadmap CLI-44).
|
|
3
|
+
*
|
|
4
|
+
* At the start of a subscription, a publisher examines the rights of the subscriber. It examines
|
|
5
|
+
* the topic rule, the reads of the capability, `allow` and the partition. A right can change
|
|
6
|
+
* later, for example when the app removes a user from a group. Thus the publisher examines each
|
|
7
|
+
* open subscription again:
|
|
8
|
+
*
|
|
9
|
+
* - At a change of the rights. The host uses `rightsChanged()` of the publisher, or the source of
|
|
10
|
+
* the option `recheck.rightsChanges` gives the change to its listener.
|
|
11
|
+
* - After the time `recheck.maxAge` on the clock of the publisher, from the last check.
|
|
12
|
+
*
|
|
13
|
+
* The check is the same function as at the start. While it operates, the events of the
|
|
14
|
+
* subscription wait. A refusal ends the subscription with its error, for example `FORBIDDEN`, and
|
|
15
|
+
* the events that wait go. Without a call of `rightsChanged()` and without the option, nothing
|
|
16
|
+
* changes.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import type { Cancel, Clock } from "../clock.js";
|
|
20
|
+
import { toMs, type Duration } from "../duration.js";
|
|
21
|
+
import type { Principal } from "../server/types.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The subscriptions that a change of the rights touches: the id of a principal, or a test of the
|
|
25
|
+
* principal of a subscription. A subscription without a principal (a call from this process) has
|
|
26
|
+
* no id, so only a test or a change of each subscription touches it.
|
|
27
|
+
*/
|
|
28
|
+
export type RightsChange = string | ((principal: Principal | undefined) => boolean);
|
|
29
|
+
|
|
30
|
+
/** The options of the re-check of the open subscriptions of a publisher (roadmap CLI-44). */
|
|
31
|
+
export interface SubscriptionRecheck {
|
|
32
|
+
/**
|
|
33
|
+
* The longest time from one check of a subscription to the next check, for example `minutes(1)`.
|
|
34
|
+
* The time is on the clock of the publisher. Without it, only a change of the rights starts a
|
|
35
|
+
* check.
|
|
36
|
+
*/
|
|
37
|
+
readonly maxAge?: Duration | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* The source of the changes of the rights of the app. While the publisher has an open
|
|
40
|
+
* subscription, the source has one listener of the publisher. The source gives the function that
|
|
41
|
+
* removes the listener. Each call of the listener is a call of `rightsChanged()`: without a
|
|
42
|
+
* change, each subscription gets a check.
|
|
43
|
+
*/
|
|
44
|
+
readonly rightsChanges?: ((listener: (change?: RightsChange) => void) => () => void) | undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** A subscription that the re-checks can hold and watch. */
|
|
48
|
+
export interface RecheckedSubscription {
|
|
49
|
+
/**
|
|
50
|
+
* This function holds the events of the subscription until the check ends. A refusal ends the
|
|
51
|
+
* subscription with its error. The promise ends with the check, and it does not reject.
|
|
52
|
+
*/
|
|
53
|
+
hold(check: Promise<void>): Promise<void>;
|
|
54
|
+
/** This function runs the listener when the subscription ends. */
|
|
55
|
+
onEnd(listener: () => void): void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** One subscription that the re-checks watch. */
|
|
59
|
+
interface Watch {
|
|
60
|
+
readonly principal: Principal | undefined;
|
|
61
|
+
/** The timer of the max age, or undefined. */
|
|
62
|
+
timer: Cancel | undefined;
|
|
63
|
+
/** This function starts a check at once. The promise ends when the check ends. */
|
|
64
|
+
readonly recheck: () => Promise<void>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** True when the change touches a subscription of the principal. A test that throws touches it. */
|
|
68
|
+
function touches(change: RightsChange | undefined, principal: Principal | undefined): boolean {
|
|
69
|
+
if (change === undefined) return true;
|
|
70
|
+
if (typeof change === "string") return principal?.id === change;
|
|
71
|
+
try {
|
|
72
|
+
return change(principal);
|
|
73
|
+
} catch {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The promise of a check. A check that throws at once gives a rejected promise. */
|
|
79
|
+
function started(check: () => Promise<void>): Promise<void> {
|
|
80
|
+
return new Promise<void>((resolve) => resolve(check()));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The re-checks of the open subscriptions of one publisher (roadmap CLI-44). The publisher gives
|
|
85
|
+
* each new subscription to `watch()`, with the check of its admission.
|
|
86
|
+
*/
|
|
87
|
+
export class SubscriptionRechecks {
|
|
88
|
+
private readonly watches = new Set<Watch>();
|
|
89
|
+
private readonly maxAgeMs: number | undefined;
|
|
90
|
+
private readonly source: SubscriptionRecheck["rightsChanges"];
|
|
91
|
+
/** The function that removes the listener of the source, while a subscription is open */
|
|
92
|
+
private stopListening: (() => void) | undefined;
|
|
93
|
+
|
|
94
|
+
constructor(
|
|
95
|
+
options: SubscriptionRecheck,
|
|
96
|
+
private readonly clock: Clock,
|
|
97
|
+
) {
|
|
98
|
+
const maxAgeMs = options.maxAge === undefined ? undefined : toMs(options.maxAge, "recheck.maxAge");
|
|
99
|
+
if (maxAgeMs !== undefined && !(maxAgeMs > 0)) {
|
|
100
|
+
throw new TypeError(`recheck.maxAge must be more than 0 ms, got ${maxAgeMs} ms`);
|
|
101
|
+
}
|
|
102
|
+
this.maxAgeMs = maxAgeMs;
|
|
103
|
+
this.source = options.rightsChanges;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The number of the subscriptions that the re-checks watch. */
|
|
107
|
+
get size(): number {
|
|
108
|
+
return this.watches.size;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* This function watches a subscription until it ends. A change of the rights of its principal
|
|
113
|
+
* and its max age start the check. The check must throw the refusal.
|
|
114
|
+
*/
|
|
115
|
+
watch(subscription: RecheckedSubscription, principal: Principal | undefined, check: () => Promise<void>): void {
|
|
116
|
+
const watch: Watch = {
|
|
117
|
+
principal,
|
|
118
|
+
timer: undefined,
|
|
119
|
+
recheck: () => {
|
|
120
|
+
this.stopTimer(watch);
|
|
121
|
+
return subscription.hold(started(check)).then(() => {
|
|
122
|
+
// A check that passes gives the admission a new age
|
|
123
|
+
if (this.watches.has(watch)) this.startTimer(watch);
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
this.watches.add(watch);
|
|
128
|
+
subscription.onEnd(() => this.unwatch(watch));
|
|
129
|
+
if (!this.watches.has(watch)) return;
|
|
130
|
+
this.startTimer(watch);
|
|
131
|
+
if (this.source && !this.stopListening) {
|
|
132
|
+
try {
|
|
133
|
+
this.stopListening = this.source((change) => void this.rightsChanged(change));
|
|
134
|
+
} catch (error) {
|
|
135
|
+
// A source that cannot listen cannot tell a revocation: the subscription ends
|
|
136
|
+
void subscription.hold(Promise.reject(error));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* This function starts a check of each watched subscription that the change touches. The
|
|
143
|
+
* promise ends when each check ends.
|
|
144
|
+
*/
|
|
145
|
+
rightsChanged(change?: RightsChange): Promise<void> {
|
|
146
|
+
const checks: Array<Promise<void>> = [];
|
|
147
|
+
for (const watch of [...this.watches]) {
|
|
148
|
+
if (touches(change, watch.principal)) checks.push(watch.recheck());
|
|
149
|
+
}
|
|
150
|
+
return Promise.all(checks).then(() => undefined);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** This function stops the watch of a subscription that ended. */
|
|
154
|
+
private unwatch(watch: Watch): void {
|
|
155
|
+
this.stopTimer(watch);
|
|
156
|
+
if (!this.watches.delete(watch) || this.watches.size > 0) return;
|
|
157
|
+
// The last subscription ended: the publisher does not listen to the source
|
|
158
|
+
const stop = this.stopListening;
|
|
159
|
+
this.stopListening = undefined;
|
|
160
|
+
stop?.();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** This function starts the timer of the max age of a subscription. */
|
|
164
|
+
private startTimer(watch: Watch): void {
|
|
165
|
+
if (this.maxAgeMs === undefined) return;
|
|
166
|
+
this.stopTimer(watch);
|
|
167
|
+
const timer = this.clock.after(this.maxAgeMs, () => {
|
|
168
|
+
if (watch.timer === timer) watch.timer = undefined;
|
|
169
|
+
void watch.recheck();
|
|
170
|
+
});
|
|
171
|
+
// A check does not keep a Node process alive
|
|
172
|
+
timer.unref?.();
|
|
173
|
+
watch.timer = timer;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** This function stops the timer of the max age of a subscription. */
|
|
177
|
+
private stopTimer(watch: Watch): void {
|
|
178
|
+
const timer = watch.timer;
|
|
179
|
+
watch.timer = undefined;
|
|
180
|
+
timer?.();
|
|
181
|
+
}
|
|
182
|
+
}
|
package/src/protocol.ts
CHANGED
|
@@ -49,7 +49,8 @@ export type ProtocolErrorCode =
|
|
|
49
49
|
| "CONNECTION_NOT_FOUND"
|
|
50
50
|
| "TOPIC_NOT_FOUND"
|
|
51
51
|
| "TOPIC_CLEARED"
|
|
52
|
-
| "VERSION_CONFLICT"
|
|
52
|
+
| "VERSION_CONFLICT"
|
|
53
|
+
| "RATE_LIMITED";
|
|
53
54
|
|
|
54
55
|
/**
|
|
55
56
|
* The error codes of the protocol, on each face (HTTP, WebSocket, MCP, local). An error that a
|
|
@@ -82,6 +83,7 @@ export const ERROR_CODES: Readonly<Record<ProtocolErrorCode, ErrorCodeInfo>> = {
|
|
|
82
83
|
TOPIC_NOT_FOUND: { http: 404, retryable: false, meaning: "The topic rule of the host has no topic with this name" },
|
|
83
84
|
TOPIC_CLEARED: { http: 410, retryable: false, meaning: "The host cleared a topic of the subscription: clear() of its bus removed the listener" },
|
|
84
85
|
VERSION_CONFLICT: { http: 409, retryable: false, meaning: "The current item does not have the version that the write expects. The metadata key conflicts has the current items" },
|
|
86
|
+
RATE_LIMITED: { http: 429, retryable: true, meaning: "A limit of the host refuses the call. The metadata key retry-after gives the wait, and reason gives the cause" },
|
|
85
87
|
};
|
|
86
88
|
|
|
87
89
|
/** True when a code is an error code of the protocol. */
|
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
* call and what the host gives for it (CAPABILITIES-2026-10).
|
|
19
19
|
* - `events`: the remote events of the host (`documentation/EVENTS.md`). The server pins
|
|
20
20
|
* `events.subscribe` with this topic rule, and its procedures get the bus as `ctx.bus`.
|
|
21
|
+
* - `limits`, `limitStores`: the limits of the calls (CLI-40). Each server of the policy makes one
|
|
22
|
+
* keyed limiter for each limit, and refuses a call above a limit with `RATE_LIMITED`.
|
|
21
23
|
*/
|
|
22
24
|
|
|
23
25
|
import type { Capability, ClearanceOrder, ProcedurePath, ResourcesOf } from "../procedures/types.js";
|
|
@@ -30,6 +32,8 @@ import type { ProcedureRegistry } from "../procedures/registry.js";
|
|
|
30
32
|
import type { ProcedureServerOptions } from "./procedure-server.js";
|
|
31
33
|
import type { EventPublisherOptions } from "../events/publisher.js";
|
|
32
34
|
import { toMs, type Duration } from "../duration.js";
|
|
35
|
+
import type { LimiterStore } from "@mark1russell7/service/limiter";
|
|
36
|
+
import type { CallLimit } from "./limits.js";
|
|
33
37
|
|
|
34
38
|
/** The procedures of a host that are tools: namespaces (first path segments), less some keys. */
|
|
35
39
|
export interface ToolSurface {
|
|
@@ -102,6 +106,15 @@ export interface HostPolicyOptions {
|
|
|
102
106
|
* `ctx.bus`. Without this option, the host does not serve `events.subscribe`.
|
|
103
107
|
*/
|
|
104
108
|
events?: EventPublisherOptions | undefined;
|
|
109
|
+
/**
|
|
110
|
+
* The limits of the calls (CLI-40): each limit counts the calls of one key, for example the
|
|
111
|
+
* writes of each tenant. A refusal is `RATE_LIMITED` on each face. Without it: no limits.
|
|
112
|
+
* Each limit that covers a call takes one unit, in the sequence of the limits. A limit before a
|
|
113
|
+
* refusal keeps its unit: the server does not give it back.
|
|
114
|
+
*/
|
|
115
|
+
limits?: readonly CallLimit[] | undefined;
|
|
116
|
+
/** The stores that the replicas share, by the name of their dependency, for the limits with a shared store. */
|
|
117
|
+
limitStores?: Readonly<Record<string, LimiterStore>> | undefined;
|
|
105
118
|
}
|
|
106
119
|
|
|
107
120
|
/** What a host lets its callers do. */
|
|
@@ -120,6 +133,10 @@ export interface HostPolicy {
|
|
|
120
133
|
readonly admitItem: AdmitItem | undefined;
|
|
121
134
|
/** The remote events: each server of the policy makes its own publisher from these options. */
|
|
122
135
|
readonly events: EventPublisherOptions | undefined;
|
|
136
|
+
/** The limits of the calls (CLI-40). Each server of the policy makes its own keyed limiters from them. */
|
|
137
|
+
readonly limits?: readonly CallLimit[] | undefined;
|
|
138
|
+
/** The shared stores of the limits, by the name of their dependency. */
|
|
139
|
+
readonly limitStores?: Readonly<Record<string, LimiterStore>> | undefined;
|
|
123
140
|
}
|
|
124
141
|
|
|
125
142
|
/** The capability function of a policy: the capability of the options, narrowed by `readOnly`. */
|
|
@@ -167,6 +184,8 @@ export function hostPolicy(options: HostPolicyOptions = {}): HostPolicy {
|
|
|
167
184
|
resources: options.resources,
|
|
168
185
|
admitItem: options.admitItem,
|
|
169
186
|
events: options.events,
|
|
187
|
+
...(options.limits && options.limits.length > 0 ? { limits: options.limits } : {}),
|
|
188
|
+
...(options.limitStores ? { limitStores: options.limitStores } : {}),
|
|
170
189
|
};
|
|
171
190
|
}
|
|
172
191
|
|
|
@@ -188,5 +207,8 @@ export function procedureServerOptions(policy: HostPolicy, registry?: ProcedureR
|
|
|
188
207
|
...(policy.admitItem ? { admitItem: policy.admitItem } : {}),
|
|
189
208
|
// The remote events: the server makes its publisher and pins events.subscribe (EVENTS.md)
|
|
190
209
|
...(policy.events ? { events: policy.events } : {}),
|
|
210
|
+
// The limits of the calls: the server makes one keyed limiter for each one (CLI-40)
|
|
211
|
+
...(policy.limits ? { limits: policy.limits } : {}),
|
|
212
|
+
...(policy.limitStores ? { limitStores: policy.limitStores } : {}),
|
|
191
213
|
};
|
|
192
214
|
}
|