@procedures/client 0.14.1 → 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 +93 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/adapters/http/client/transport.d.ts.map +1 -1
- package/dist/adapters/http/client/transport.js +4 -0
- package/dist/adapters/http/client/transport.js.map +1 -1
- package/dist/adapters/http/server/handler.d.ts.map +1 -1
- package/dist/adapters/http/server/handler.js +8 -3
- package/dist/adapters/http/server/handler.js.map +1 -1
- package/dist/adapters/local/client/transport.d.ts.map +1 -1
- package/dist/adapters/local/client/transport.js +5 -0
- package/dist/adapters/local/client/transport.js.map +1 -1
- package/dist/browser.d.ts +4 -3
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +2 -0
- package/dist/browser.js.map +1 -1
- package/dist/client/client.js +1 -1
- package/dist/client/client.js.map +1 -1
- package/dist/client/errors/registry.d.ts.map +1 -1
- package/dist/client/errors/registry.js +13 -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/client/types.d.ts +6 -1
- package/dist/client/types.d.ts.map +1 -1
- package/dist/client/types.js +7 -1
- package/dist/client/types.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/procedures/collection/index.d.ts +3 -1
- package/dist/procedures/collection/index.d.ts.map +1 -1
- package/dist/procedures/collection/index.js +2 -0
- package/dist/procedures/collection/index.js.map +1 -1
- package/dist/procedures/collection/precondition.d.ts +111 -0
- package/dist/procedures/collection/precondition.d.ts.map +1 -0
- package/dist/procedures/collection/precondition.js +153 -0
- package/dist/procedures/collection/precondition.js.map +1 -0
- package/dist/procedures/collection/procedures.d.ts.map +1 -1
- package/dist/procedures/collection/procedures.js +16 -3
- package/dist/procedures/collection/procedures.js.map +1 -1
- package/dist/procedures/collection/query.d.ts +2 -0
- package/dist/procedures/collection/query.d.ts.map +1 -1
- package/dist/procedures/collection/query.js +3 -0
- package/dist/procedures/collection/query.js.map +1 -1
- package/dist/procedures/collection/schemas.d.ts +16 -0
- package/dist/procedures/collection/schemas.d.ts.map +1 -1
- package/dist/procedures/collection/schemas.js +26 -5
- package/dist/procedures/collection/schemas.js.map +1 -1
- package/dist/procedures/collection/storage.d.ts +34 -6
- package/dist/procedures/collection/storage.d.ts.map +1 -1
- package/dist/procedures/collection/storage.js +3 -0
- package/dist/procedures/collection/storage.js.map +1 -1
- package/dist/procedures/index.d.ts +2 -2
- package/dist/procedures/index.d.ts.map +1 -1
- package/dist/procedures/index.js +1 -1
- package/dist/procedures/index.js.map +1 -1
- package/dist/protocol.d.ts +1 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +2 -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 +43 -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/client/transport.ts +3 -0
- package/src/adapters/http/server/handler.ts +8 -3
- package/src/adapters/local/client/transport.ts +4 -0
- package/src/browser.ts +20 -0
- package/src/client/client.ts +1 -1
- package/src/client/errors/registry.ts +14 -0
- package/src/client/middleware/rate-limit.ts +116 -41
- package/src/client/types.ts +8 -0
- package/src/events/index.ts +1 -0
- package/src/events/publisher.ts +116 -6
- package/src/events/recheck.ts +182 -0
- package/src/procedures/collection/index.ts +17 -0
- package/src/procedures/collection/precondition.ts +180 -0
- package/src/procedures/collection/procedures.ts +17 -4
- package/src/procedures/collection/query.ts +16 -0
- package/src/procedures/collection/schemas.ts +43 -6
- package/src/procedures/collection/storage.ts +36 -6
- package/src/procedures/index.ts +15 -0
- package/src/protocol.ts +5 -1
- package/src/server/host-policy.ts +22 -0
- package/src/server/limits.ts +254 -0
- package/src/server/procedure-server.ts +59 -3
- package/src/server/request-auth.ts +19 -2
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The limits of the calls of a server (CLI-40): one admission step for each face.
|
|
3
|
+
*
|
|
4
|
+
* A limit is data: a key over the call, the calls that it covers, a limit policy, a store and a
|
|
5
|
+
* bound on its keys. Each limit is a keyed limiter of `@mark1russell7/service/limiter` (SYS-28) on
|
|
6
|
+
* the clock of the server. The server takes one unit of each limit that covers a call, after the
|
|
7
|
+
* authentication and before the run. Thus the key can read the principal of the call. It takes the
|
|
8
|
+
* units in the sequence of the limits, and the first refusal stops the call. A limit before the
|
|
9
|
+
* refusal keeps its unit: the server does not give it back.
|
|
10
|
+
*
|
|
11
|
+
* A refusal is `RATE_LIMITED` (retryable), with the wait in the metadata key `retry-after` (in
|
|
12
|
+
* milliseconds) and the cause in `reason` (`limit`, `cap` or `store`). The HTTP face answers 429
|
|
13
|
+
* with `Retry-After` in whole seconds. The WebSocket face and the MCP face give the same code and
|
|
14
|
+
* the same wait. The work did not start, so a retry after the wait is safe.
|
|
15
|
+
*/
|
|
16
|
+
import { type KeyedLimiter, type LimitPolicy, type LimiterEviction, type LimiterStore, type LimiterStoreSpec, type Refused } from "@mark1russell7/service/limiter";
|
|
17
|
+
import type { Clock } from "../clock.js";
|
|
18
|
+
import { type Effects, type ProcedurePath } from "../procedures/types.js";
|
|
19
|
+
import type { Principal, ServerRequest, ServerResponse } from "./types.js";
|
|
20
|
+
/** The facts of a call that a limit reads. The server gives them after the authentication of the call. */
|
|
21
|
+
export interface LimitedCall {
|
|
22
|
+
/** The path of the procedure of the call. */
|
|
23
|
+
readonly path: ProcedurePath;
|
|
24
|
+
/** The route of the call: the key of its procedure, for example `collections.notes.set`. */
|
|
25
|
+
readonly route: string;
|
|
26
|
+
/** The effects that the procedure declares, or undefined when it declares none. */
|
|
27
|
+
readonly effects: Effects | undefined;
|
|
28
|
+
/** True when the procedure can change data: its effects do not say `writes: []`. A procedure without effects can change data. */
|
|
29
|
+
readonly writes: boolean;
|
|
30
|
+
/** The principal of the call: from the request hook, the token check or the authenticate function. Undefined for a call without one. */
|
|
31
|
+
readonly principal: Principal | undefined;
|
|
32
|
+
/** The request. Its payload and its metadata come from the caller, thus a key from them is a claim of the caller. */
|
|
33
|
+
readonly request: ServerRequest<unknown>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A part of the key of a limit, as data:
|
|
37
|
+
* - `principal`: the id of the principal of the call.
|
|
38
|
+
* - `route`: the key of the procedure of the call.
|
|
39
|
+
* - `{ claim }`: a claim of the principal, for example `{ claim: "tenant" }`. A string or a number.
|
|
40
|
+
*/
|
|
41
|
+
export type CallLimitKeyPart = "principal" | "route" | {
|
|
42
|
+
readonly claim: string;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* The key of a limit: one part, a list of parts, or a function of the call. A call whose key has
|
|
46
|
+
* no value (a part without a value, or a function that gives undefined) is outside the limit.
|
|
47
|
+
*/
|
|
48
|
+
export type CallLimitKey = CallLimitKeyPart | readonly CallLimitKeyPart[] | ((call: LimitedCall) => string | undefined);
|
|
49
|
+
/** The calls that a limit covers, as data. Each given field must agree. Without a field, the limit covers each call. */
|
|
50
|
+
export interface CallLimitCovers {
|
|
51
|
+
/** True: only the calls of a procedure that can change data. False: only the calls of a procedure that changes nothing. */
|
|
52
|
+
readonly writes?: boolean | undefined;
|
|
53
|
+
/** Only the calls of these procedures: an exact key (`notes.set`), a prefix (`collections.*`) or `*`. */
|
|
54
|
+
readonly routes?: readonly string[] | undefined;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A limit of the calls of a server (CLI-40): a keyed limiter of `@mark1russell7/service/limiter`
|
|
58
|
+
* with a key over the call. The policy, the store and the bound on the keys have no defaults, as
|
|
59
|
+
* in the spec of a keyed limiter (`LimiterSpec`).
|
|
60
|
+
*/
|
|
61
|
+
export interface CallLimit {
|
|
62
|
+
/** The name of the limiter. It is unique in the service: `svc.limiter()` declares the limiter with this name. */
|
|
63
|
+
readonly name: string;
|
|
64
|
+
/** The key of each call: who or what the limit counts. */
|
|
65
|
+
readonly key: CallLimitKey;
|
|
66
|
+
/**
|
|
67
|
+
* The text of the key in the declaration of the service (`LimiterSpec.key`), for example
|
|
68
|
+
* `tenant`. The default names the parts of the key, or `call` for a function.
|
|
69
|
+
*/
|
|
70
|
+
readonly keyName?: string | undefined;
|
|
71
|
+
/** The calls that the limit covers: the fields of `CallLimitCovers`, or a function of the call. Without it, each call. */
|
|
72
|
+
readonly covers?: CallLimitCovers | ((call: LimitedCall) => boolean) | undefined;
|
|
73
|
+
/** The policy: a token bucket or a sliding window. */
|
|
74
|
+
readonly policy: LimitPolicy;
|
|
75
|
+
/**
|
|
76
|
+
* The store of the counts. `{ kind: "local" }`: the memory of this replica. `{ kind: "shared",
|
|
77
|
+
* dependency, onStoreFailure }`: the store of `limitStores` with the name of the dependency.
|
|
78
|
+
*/
|
|
79
|
+
readonly store: LimiterStoreSpec;
|
|
80
|
+
/** The bound on the keys that the store holds. */
|
|
81
|
+
readonly evict: LimiterEviction;
|
|
82
|
+
}
|
|
83
|
+
/** The refusal of a call by one limit. */
|
|
84
|
+
export interface CallLimitRefusal {
|
|
85
|
+
/** The name of the limit that refused the call. */
|
|
86
|
+
readonly limit: string;
|
|
87
|
+
readonly refused: Refused;
|
|
88
|
+
}
|
|
89
|
+
/** The limits of one server, as runtime: one keyed limiter for each limit. */
|
|
90
|
+
export interface CallLimiter {
|
|
91
|
+
/** The keyed limiters, in the sequence of the limits. A host declares each one with `svc.limiter(name, spec)`. */
|
|
92
|
+
readonly limiters: readonly KeyedLimiter[];
|
|
93
|
+
/**
|
|
94
|
+
* This method takes one unit of each limit that covers the call, in the sequence of the limits.
|
|
95
|
+
* It gives the first refusal, or undefined. A limit before the refusal keeps its unit.
|
|
96
|
+
*/
|
|
97
|
+
admit(call: LimitedCall): Promise<CallLimitRefusal | undefined>;
|
|
98
|
+
}
|
|
99
|
+
/** The options of `callLimiter()`. */
|
|
100
|
+
export interface CallLimiterOptions {
|
|
101
|
+
/** The clock of the local store: give the clock of the server. */
|
|
102
|
+
readonly clock: Clock;
|
|
103
|
+
/** The stores that the replicas share, by the name of their dependency. */
|
|
104
|
+
readonly stores?: Readonly<Record<string, LimiterStore>> | undefined;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* This function makes the runtime of the limits of a server: one keyed limiter for each limit. The
|
|
108
|
+
* local limits share one memory store on the clock of the options. It refuses two limits with one
|
|
109
|
+
* name, a limit with a problem in its spec, and a shared store that `stores` does not have.
|
|
110
|
+
*/
|
|
111
|
+
export declare function callLimiter(limits: readonly CallLimit[], options: CallLimiterOptions): CallLimiter;
|
|
112
|
+
/** This function gives the facts of a call for the limits: its procedure, its effects, its principal and its request. */
|
|
113
|
+
export declare function limitedCall(procedure: {
|
|
114
|
+
readonly path: ProcedurePath;
|
|
115
|
+
readonly metadata?: unknown;
|
|
116
|
+
}, request: ServerRequest<unknown>): LimitedCall;
|
|
117
|
+
/**
|
|
118
|
+
* This function gives the response of a refused call: `RATE_LIMITED`, retryable, with the wait in
|
|
119
|
+
* `retry-after` (milliseconds) and the cause in `reason`. The failure of a store stays on the server.
|
|
120
|
+
*/
|
|
121
|
+
export declare function rateLimitedResponse(id: string, refusal: CallLimitRefusal): ServerResponse<unknown>;
|
|
122
|
+
//# sourceMappingURL=limits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limits.d.ts","sourceRoot":"","sources":["../../src/server/limits.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,EAIL,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,eAAe,EACpB,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,OAAO,EACb,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAa,KAAK,OAAO,EAAE,KAAK,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAErF,OAAO,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE3E,0GAA0G;AAC1G,MAAM,WAAW,WAAW;IAC1B,6CAA6C;IAC7C,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,4FAA4F;IAC5F,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,mFAAmF;IACnF,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,CAAC;IACtC,iIAAiI;IACjI,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,wIAAwI;IACxI,QAAQ,CAAC,SAAS,EAAE,SAAS,GAAG,SAAS,CAAC;IAC1C,qHAAqH;IACrH,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;CAC1C;AAED;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,OAAO,GAAG;IAAE,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H,KAAK,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAAC;CACjE;AAED,sCAAsC;AACtC,MAAM,WAAW,kBAAkB;IACjC,kEAAkE;IAClE,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,2EAA2E;IAC3E,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC,GAAG,SAAS,CAAC;CACtE;AAkED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,SAAS,SAAS,EAAE,EAAE,OAAO,EAAE,kBAAkB,GAAG,WAAW,CAyBlG;AAED,yHAAyH;AACzH,wBAAgB,WAAW,CAAC,SAAS,EAAE;IAAE,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,EAAE,OAAO,EAAE,aAAa,CAAC,OAAO,CAAC,GAAG,WAAW,CAUlJ;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,GAAG,cAAc,CAAC,OAAO,CAAC,CAclG"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The limits of the calls of a server (CLI-40): one admission step for each face.
|
|
3
|
+
*
|
|
4
|
+
* A limit is data: a key over the call, the calls that it covers, a limit policy, a store and a
|
|
5
|
+
* bound on its keys. Each limit is a keyed limiter of `@mark1russell7/service/limiter` (SYS-28) on
|
|
6
|
+
* the clock of the server. The server takes one unit of each limit that covers a call, after the
|
|
7
|
+
* authentication and before the run. Thus the key can read the principal of the call. It takes the
|
|
8
|
+
* units in the sequence of the limits, and the first refusal stops the call. A limit before the
|
|
9
|
+
* refusal keeps its unit: the server does not give it back.
|
|
10
|
+
*
|
|
11
|
+
* A refusal is `RATE_LIMITED` (retryable), with the wait in the metadata key `retry-after` (in
|
|
12
|
+
* milliseconds) and the cause in `reason` (`limit`, `cap` or `store`). The HTTP face answers 429
|
|
13
|
+
* with `Retry-After` in whole seconds. The WebSocket face and the MCP face give the same code and
|
|
14
|
+
* the same wait. The work did not start, so a retry after the wait is safe.
|
|
15
|
+
*/
|
|
16
|
+
import { valueIn } from "@mark1russell7/quantity";
|
|
17
|
+
import { keyedLimiter, memoryStore, retryAfterSeconds, } from "@mark1russell7/service/limiter";
|
|
18
|
+
import { effectsOf, isReadOnly } from "../procedures/capability.js";
|
|
19
|
+
import { pathToKey } from "../procedures/types.js";
|
|
20
|
+
import { exposePatterns } from "./host-policy.js";
|
|
21
|
+
/** The metadata key of the wait of a refusal, in milliseconds (the same key as `retry-after` of wire v1). */
|
|
22
|
+
const RETRY_AFTER = "retry-after";
|
|
23
|
+
/** This function tells if a key is a list of parts. */
|
|
24
|
+
function isPartList(key) {
|
|
25
|
+
return Array.isArray(key);
|
|
26
|
+
}
|
|
27
|
+
/** The value of one part of a key, or undefined. */
|
|
28
|
+
function partValue(part, call) {
|
|
29
|
+
if (part === "principal")
|
|
30
|
+
return call.principal?.id;
|
|
31
|
+
if (part === "route")
|
|
32
|
+
return call.route;
|
|
33
|
+
const value = call.principal?.claims?.[part.claim];
|
|
34
|
+
return typeof value === "string" ? value : typeof value === "number" && Number.isFinite(value) ? String(value) : undefined;
|
|
35
|
+
}
|
|
36
|
+
/** The text of one part of a key, for the declaration of the service. */
|
|
37
|
+
function partName(part) {
|
|
38
|
+
return typeof part === "string" ? part : part.claim;
|
|
39
|
+
}
|
|
40
|
+
/** This function gives the function of the key of a limit. A list of parts gives the JSON of the values. */
|
|
41
|
+
function keyFunction(key) {
|
|
42
|
+
if (typeof key === "function")
|
|
43
|
+
return key;
|
|
44
|
+
const parts = isPartList(key) ? key : [key];
|
|
45
|
+
if (parts.length === 0)
|
|
46
|
+
throw new Error("A key of a limit needs one part or more");
|
|
47
|
+
return (call) => {
|
|
48
|
+
const values = [];
|
|
49
|
+
for (const part of parts) {
|
|
50
|
+
const value = partValue(part, call);
|
|
51
|
+
if (value === undefined)
|
|
52
|
+
return undefined;
|
|
53
|
+
values.push(value);
|
|
54
|
+
}
|
|
55
|
+
return values.length === 1 ? values[0] : JSON.stringify(values);
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** The text of the key of a limit, for the declaration of the service. */
|
|
59
|
+
function keyNameOf(limit) {
|
|
60
|
+
if (limit.keyName !== undefined)
|
|
61
|
+
return limit.keyName;
|
|
62
|
+
if (typeof limit.key === "function")
|
|
63
|
+
return "call";
|
|
64
|
+
return (isPartList(limit.key) ? limit.key : [limit.key]).map(partName).join("+");
|
|
65
|
+
}
|
|
66
|
+
/** This function gives the function that tells if a limit covers a call. */
|
|
67
|
+
function coversFunction(covers) {
|
|
68
|
+
if (covers === undefined)
|
|
69
|
+
return () => true;
|
|
70
|
+
if (typeof covers === "function")
|
|
71
|
+
return covers;
|
|
72
|
+
const { writes, routes } = covers;
|
|
73
|
+
const routeRule = routes === undefined ? undefined : exposePatterns(routes);
|
|
74
|
+
return (call) => (writes === undefined || call.writes === writes) && (routeRule === undefined || routeRule(call.path));
|
|
75
|
+
}
|
|
76
|
+
/** This function gives the store of a limit: the local store, or the shared store of its dependency. */
|
|
77
|
+
function storeOf(limit, local, stores) {
|
|
78
|
+
const spec = limit.store;
|
|
79
|
+
if (spec === undefined || spec.kind !== "shared")
|
|
80
|
+
return local;
|
|
81
|
+
const store = Object.prototype.hasOwnProperty.call(stores, spec.dependency) ? stores[spec.dependency] : undefined;
|
|
82
|
+
if (store === undefined) {
|
|
83
|
+
throw new Error(`The limit ${limit.name} keeps its counts in the shared store ${spec.dependency}, and limitStores has no store with that name`);
|
|
84
|
+
}
|
|
85
|
+
return store;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* This function makes the runtime of the limits of a server: one keyed limiter for each limit. The
|
|
89
|
+
* local limits share one memory store on the clock of the options. It refuses two limits with one
|
|
90
|
+
* name, a limit with a problem in its spec, and a shared store that `stores` does not have.
|
|
91
|
+
*/
|
|
92
|
+
export function callLimiter(limits, options) {
|
|
93
|
+
const local = memoryStore(options.clock);
|
|
94
|
+
const stores = options.stores ?? {};
|
|
95
|
+
const names = new Set();
|
|
96
|
+
const entries = limits.map((limit) => {
|
|
97
|
+
if (names.has(limit.name))
|
|
98
|
+
throw new Error(`Two limits have the name ${limit.name}: the name of a limit is unique`);
|
|
99
|
+
names.add(limit.name);
|
|
100
|
+
const keyOf = keyFunction(limit.key);
|
|
101
|
+
const covers = coversFunction(limit.covers);
|
|
102
|
+
const limiter = keyedLimiter(limit.name, { key: keyNameOf(limit), policy: limit.policy, store: limit.store, evict: limit.evict }, storeOf(limit, local, stores));
|
|
103
|
+
return { limiter, keyOf, covers };
|
|
104
|
+
});
|
|
105
|
+
return {
|
|
106
|
+
limiters: entries.map((entry) => entry.limiter),
|
|
107
|
+
async admit(call) {
|
|
108
|
+
for (const { limiter, keyOf, covers } of entries) {
|
|
109
|
+
if (!covers(call))
|
|
110
|
+
continue;
|
|
111
|
+
const key = keyOf(call);
|
|
112
|
+
if (key === undefined)
|
|
113
|
+
continue;
|
|
114
|
+
const decision = await limiter.take(key);
|
|
115
|
+
if (!decision.allowed)
|
|
116
|
+
return { limit: limiter.name, refused: decision };
|
|
117
|
+
}
|
|
118
|
+
return undefined;
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** This function gives the facts of a call for the limits: its procedure, its effects, its principal and its request. */
|
|
123
|
+
export function limitedCall(procedure, request) {
|
|
124
|
+
const effects = effectsOf(procedure);
|
|
125
|
+
return {
|
|
126
|
+
path: procedure.path,
|
|
127
|
+
route: pathToKey(procedure.path),
|
|
128
|
+
effects,
|
|
129
|
+
writes: !isReadOnly(effects),
|
|
130
|
+
principal: request.principal,
|
|
131
|
+
request,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* This function gives the response of a refused call: `RATE_LIMITED`, retryable, with the wait in
|
|
136
|
+
* `retry-after` (milliseconds) and the cause in `reason`. The failure of a store stays on the server.
|
|
137
|
+
*/
|
|
138
|
+
export function rateLimitedResponse(id, refusal) {
|
|
139
|
+
const { limit, refused } = refusal;
|
|
140
|
+
const waitMs = valueIn(refused.retryAfter, "ms");
|
|
141
|
+
const seconds = retryAfterSeconds(refused.retryAfter);
|
|
142
|
+
return {
|
|
143
|
+
id,
|
|
144
|
+
status: {
|
|
145
|
+
type: "error",
|
|
146
|
+
code: "RATE_LIMITED",
|
|
147
|
+
message: `The limit ${limit} of the host refuses the call (${refused.cause}): retry after ${seconds} s`,
|
|
148
|
+
retryable: true,
|
|
149
|
+
},
|
|
150
|
+
metadata: { [RETRY_AFTER]: waitMs, reason: refused.cause },
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=limits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"limits.js","sourceRoot":"","sources":["../../src/server/limits.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EACL,YAAY,EACZ,WAAW,EACX,iBAAiB,GAOlB,MAAM,gCAAgC,CAAC;AAExC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AACpE,OAAO,EAAE,SAAS,EAAoC,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AA+FlD,6GAA6G;AAC7G,MAAM,WAAW,GAAG,aAAa,CAAC;AAElC,uDAAuD;AACvD,SAAS,UAAU,CAAC,GAAiB;IACnC,OAAO,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;AAC5B,CAAC;AAED,oDAAoD;AACpD,SAAS,SAAS,CAAC,IAAsB,EAAE,IAAiB;IAC1D,IAAI,IAAI,KAAK,WAAW;QAAE,OAA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,CAAC,SAAS;QAC5B,OAAO;KACR,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAAU,EAAE,OAAyB;IACvE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IACnC,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IACjD,MAAM,OAAO,GAAG,iBAAiB,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACtD,OAAO;QACL,EAAE;QACF,MAAM,EAAE;YACN,IAAI,EAAE,OAAO;YACb,IAAI,EAAE,cAAc;YACpB,OAAO,EAAE,aAAa,KAAK,kCAAkC,OAAO,CAAC,KAAK,kBAAkB,OAAO,IAAI;YACvG,SAAS,EAAE,IAAI;SAChB;QACD,QAAQ,EAAE,EAAE,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,KAAK,EAAE;KAC3D,CAAC;AACJ,CAAC"}
|
|
@@ -18,6 +18,8 @@ import { type Clock } from "../clock.js";
|
|
|
18
18
|
import { EventPublisher, type EventPublisherOptions } from "../events/publisher.js";
|
|
19
19
|
import type { CollectionProceduresOptions } from "../procedures/collection/procedures.js";
|
|
20
20
|
import type { ServerDebugReport } from "./types.js";
|
|
21
|
+
import type { KeyedLimiter, LimiterStore } from "@mark1russell7/service/limiter";
|
|
22
|
+
import { type CallLimit } from "./limits.js";
|
|
21
23
|
/**
|
|
22
24
|
* Storage configuration for a collection.
|
|
23
25
|
*/
|
|
@@ -134,7 +136,8 @@ export interface ProcedureServerOptions extends ServerOptions {
|
|
|
134
136
|
* The remote events of the server (`documentation/EVENTS.md`): a bus and its topic rule, or an
|
|
135
137
|
* `EventPublisher`. The server pins the procedure `events.subscribe`, outside its registry, so a
|
|
136
138
|
* peer can subscribe to the topics. The procedures of the server get this bus as `ctx.bus`.
|
|
137
|
-
* `stop()` ends each subscription first, with `UNAVAILABLE`.
|
|
139
|
+
* `stop()` ends each subscription first, with `UNAVAILABLE`. A publisher that the server makes
|
|
140
|
+
* from the options gets the clock of the server, unless the options give a clock (CLI-44).
|
|
138
141
|
*/
|
|
139
142
|
events?: EventPublisherOptions | EventPublisher | undefined;
|
|
140
143
|
/**
|
|
@@ -145,6 +148,19 @@ export interface ProcedureServerOptions extends ServerOptions {
|
|
|
145
148
|
* @default false
|
|
146
149
|
*/
|
|
147
150
|
debug?: boolean | ((report: ServerDebugReport) => void) | undefined;
|
|
151
|
+
/**
|
|
152
|
+
* The limits of the calls (CLI-40). Each limit is a keyed limiter of
|
|
153
|
+
* `@mark1russell7/service/limiter` on the clock of the server. The server takes one unit of each
|
|
154
|
+
* limit that covers a call, after the authentication and before the run, for each face. A
|
|
155
|
+
* refusal is `RATE_LIMITED` with the wait in `retry-after`: HTTP 429 with `Retry-After`. Without
|
|
156
|
+
* this option, the server limits nothing.
|
|
157
|
+
*
|
|
158
|
+
* The server takes the units in the sequence of the limits, and the first refusal stops the call.
|
|
159
|
+
* A limit before the refusal keeps its unit: the server does not give it back.
|
|
160
|
+
*/
|
|
161
|
+
limits?: readonly CallLimit[] | undefined;
|
|
162
|
+
/** The stores that the replicas share, by the name of their dependency, for each limit with a shared store. */
|
|
163
|
+
limitStores?: Readonly<Record<string, LimiterStore>> | undefined;
|
|
148
164
|
}
|
|
149
165
|
/** The options of `registerProcedure()`, `registerProcedures()` and `registerModule()` (CLI-47). */
|
|
150
166
|
export interface RegisterProcedureOptions {
|
|
@@ -306,6 +322,8 @@ export declare class ProcedureServer extends Server implements RepositoryProvide
|
|
|
306
322
|
readonly ambientCredentials: AmbientCredentials | undefined;
|
|
307
323
|
/** The receiver of the debug reports, or undefined when the option `debug` is off. */
|
|
308
324
|
private readonly debugReport;
|
|
325
|
+
/** The limits of the calls (CLI-40), or undefined without the option `limits`. */
|
|
326
|
+
private readonly callLimits;
|
|
309
327
|
constructor(options?: ProcedureServerOptions);
|
|
310
328
|
/**
|
|
311
329
|
* Get storage for a collection.
|
|
@@ -454,6 +472,12 @@ export declare class ProcedureServer extends Server implements RepositoryProvide
|
|
|
454
472
|
* passes.
|
|
455
473
|
*/
|
|
456
474
|
private admit;
|
|
475
|
+
/**
|
|
476
|
+
* The limits of a call (CLI-40): one unit of each limit that covers it. The function gives a
|
|
477
|
+
* `RATE_LIMITED` response, or undefined when the call passes. A key or a cover function that
|
|
478
|
+
* throws gives `INTERNAL_ERROR`, so a defect of a limit does not let a call in.
|
|
479
|
+
*/
|
|
480
|
+
private limit;
|
|
457
481
|
/** One call in flight ended: `drain()` can end when it was the last one. */
|
|
458
482
|
private callEnded;
|
|
459
483
|
/**
|
|
@@ -525,6 +549,12 @@ export declare class ProcedureServer extends Server implements RepositoryProvide
|
|
|
525
549
|
* idempotency of each request (decision D3, CLI-7).
|
|
526
550
|
*/
|
|
527
551
|
procedureOf(method: Method): AnyProcedure | undefined;
|
|
552
|
+
/**
|
|
553
|
+
* This method gives the keyed limiters of the option `limits`, in its sequence (CLI-40). A host
|
|
554
|
+
* declares each one on its service with `svc.limiter(limiter.name, limiter.spec)`, so
|
|
555
|
+
* `describe()` and `lint()` see it.
|
|
556
|
+
*/
|
|
557
|
+
limiters(): readonly KeyedLimiter[];
|
|
528
558
|
/**
|
|
529
559
|
* This function publishes an event on the bus of the server: the bus of the option `events`, or
|
|
530
560
|
* the global bus. Each subscription of the topic gets it (`documentation/EVENTS.md`).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"procedure-server.d.ts","sourceRoot":"","sources":["../../src/server/procedure-server.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,KAAK,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,EAAE,SAAS,EAAE,WAAW,EAAmB,aAAa,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjK,OAAO,KAAK,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H,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAgB1D;IAED,4FAA4F;IAC5F,QAAQ,IAAI,MAAM,CAEjB;IAED;;;;OAIG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAOrB;IAED;;;;;OAKG;IACY,IAAI,CAAC,OAAO,GAAE,WAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAa5D;CACF;AAMD;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,GAAE,sBAA2B,GACnC,eAAe,CAEjB"}
|
|
@@ -10,6 +10,7 @@ import { pathToKey } from "../procedures/types.js";
|
|
|
10
10
|
import { ProcedureRegistry, PROCEDURE_REGISTRY } from "../procedures/registry.js";
|
|
11
11
|
import { createCollectionProcedures } from "../procedures/collection/procedures.js";
|
|
12
12
|
import { InvocationError, invokeProcedure } from "../procedures/invoke.js";
|
|
13
|
+
import { VERSION_CONFLICTS_METADATA_KEY, versionConflictsOf } from "../procedures/collection/precondition.js";
|
|
13
14
|
import { TIMEOUT_METADATA_KEY, effectsOf, joinNarrowing, narrowCapability } from "../procedures/capability.js";
|
|
14
15
|
import { IdempotencyCache, idempotencyKeyOf, isRepeatable } from "../procedures/idempotency.js";
|
|
15
16
|
import { optionalMs } from "../duration.js";
|
|
@@ -18,6 +19,7 @@ import { realClock } from "../clock.js";
|
|
|
18
19
|
import { ERROR_CODES, errorCodeOf, isProtocolErrorCode } from "../protocol.js";
|
|
19
20
|
import { EventPublisher } from "../events/publisher.js";
|
|
20
21
|
import { getGlobalEventBus } from "../events/bus.js";
|
|
22
|
+
import { callLimiter, limitedCall, rateLimitedResponse } from "./limits.js";
|
|
21
23
|
/**
|
|
22
24
|
* The metadata key of a stream response whose procedure declares `stream: { unbounded: true }`.
|
|
23
25
|
* Its value is "unbounded".
|
|
@@ -108,6 +110,10 @@ export function errorResponse(id, error) {
|
|
|
108
110
|
const metadata = reason ? { reason } : {};
|
|
109
111
|
if (typeof retryAfterMs === "number" && Number.isFinite(retryAfterMs) && retryAfterMs >= 0)
|
|
110
112
|
metadata["retry-after"] = retryAfterMs;
|
|
113
|
+
// A VERSION_CONFLICT error tells the current item of each conflict (roadmap CLI-16)
|
|
114
|
+
const conflicts = versionConflictsOf(error);
|
|
115
|
+
if (conflicts !== undefined)
|
|
116
|
+
metadata[VERSION_CONFLICTS_METADATA_KEY] = conflicts;
|
|
111
117
|
const code = errorCodeOf(error);
|
|
112
118
|
return {
|
|
113
119
|
id,
|
|
@@ -206,6 +212,8 @@ export class ProcedureServer extends Server {
|
|
|
206
212
|
ambientCredentials;
|
|
207
213
|
/** The receiver of the debug reports, or undefined when the option `debug` is off. */
|
|
208
214
|
debugReport;
|
|
215
|
+
/** The limits of the calls (CLI-40), or undefined without the option `limits`. */
|
|
216
|
+
callLimits;
|
|
209
217
|
constructor(options = {}) {
|
|
210
218
|
super(options);
|
|
211
219
|
this.procedureRegistry = options.registry ?? PROCEDURE_REGISTRY;
|
|
@@ -231,9 +239,13 @@ export class ProcedureServer extends Server {
|
|
|
231
239
|
: options.debug === true
|
|
232
240
|
? (report) => console.debug(`[ProcedureServer] ${report.topic} of ${report.requestId}: ${report.message}`)
|
|
233
241
|
: undefined;
|
|
242
|
+
// The limits of the calls: one keyed limiter for each limit, on the clock of the server (CLI-40)
|
|
243
|
+
this.callLimits = options.limits && options.limits.length > 0 ? callLimiter(options.limits, { clock: this.clock, stores: options.limitStores }) : undefined;
|
|
234
244
|
// The remote events: the server pins events.subscribe, as a host pins host.health (EVENTS.md).
|
|
235
|
-
// The topic rule is its expose rule, so the pin is always on (CLI-47).
|
|
236
|
-
|
|
245
|
+
// The topic rule is its expose rule, so the pin is always on (CLI-47). The publisher that the
|
|
246
|
+
// server makes gets the clock of the server for its re-checks (CLI-44).
|
|
247
|
+
const events = options.events;
|
|
248
|
+
this.publisher = events instanceof EventPublisher ? events : events ? new EventPublisher({ ...events, clock: events.clock ?? this.clock }) : undefined;
|
|
237
249
|
if (this.publisher)
|
|
238
250
|
this.registerProcedure(this.publisher.procedure, { alwaysOn: true });
|
|
239
251
|
// Configure collections
|
|
@@ -520,6 +532,10 @@ export class ProcedureServer extends Server {
|
|
|
520
532
|
const refusal = await this.admit(request);
|
|
521
533
|
if (refusal)
|
|
522
534
|
return refusal;
|
|
535
|
+
// The limits come after the authentication, so a key can read the principal (CLI-40)
|
|
536
|
+
const limited = await this.limit(procedure, request);
|
|
537
|
+
if (limited)
|
|
538
|
+
return limited;
|
|
523
539
|
this.calls++;
|
|
524
540
|
let response;
|
|
525
541
|
try {
|
|
@@ -602,6 +618,23 @@ export class ProcedureServer extends Server {
|
|
|
602
618
|
}
|
|
603
619
|
return undefined;
|
|
604
620
|
}
|
|
621
|
+
/**
|
|
622
|
+
* The limits of a call (CLI-40): one unit of each limit that covers it. The function gives a
|
|
623
|
+
* `RATE_LIMITED` response, or undefined when the call passes. A key or a cover function that
|
|
624
|
+
* throws gives `INTERNAL_ERROR`, so a defect of a limit does not let a call in.
|
|
625
|
+
*/
|
|
626
|
+
async limit(procedure, request) {
|
|
627
|
+
if (!this.callLimits)
|
|
628
|
+
return undefined;
|
|
629
|
+
try {
|
|
630
|
+
const refused = await this.callLimits.admit(limitedCall(procedure, request));
|
|
631
|
+
return refused ? rateLimitedResponse(request.id, refused) : undefined;
|
|
632
|
+
}
|
|
633
|
+
catch (error) {
|
|
634
|
+
const message = `The limits of the server cannot admit the call: ${error instanceof Error ? error.message : String(error)}`;
|
|
635
|
+
return errorResponse(request.id, Object.assign(new Error(message), { code: "INTERNAL_ERROR" }));
|
|
636
|
+
}
|
|
637
|
+
}
|
|
605
638
|
/** One call in flight ended: `drain()` can end when it was the last one. */
|
|
606
639
|
callEnded() {
|
|
607
640
|
this.calls--;
|
|
@@ -832,6 +865,14 @@ export class ProcedureServer extends Server {
|
|
|
832
865
|
procedureOf(method) {
|
|
833
866
|
return this.resolveProcedure(method);
|
|
834
867
|
}
|
|
868
|
+
/**
|
|
869
|
+
* This method gives the keyed limiters of the option `limits`, in its sequence (CLI-40). A host
|
|
870
|
+
* declares each one on its service with `svc.limiter(limiter.name, limiter.spec)`, so
|
|
871
|
+
* `describe()` and `lint()` see it.
|
|
872
|
+
*/
|
|
873
|
+
limiters() {
|
|
874
|
+
return this.callLimits?.limiters ?? [];
|
|
875
|
+
}
|
|
835
876
|
/**
|
|
836
877
|
* This function publishes an event on the bus of the server: the bus of the option `events`, or
|
|
837
878
|
* the global bus. Each subscription of the topic gets it (`documentation/EVENTS.md`).
|