@procedures/client 0.7.0 → 0.8.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 +107 -0
- package/README.md +115 -11
- package/dist/.tsbuildinfo +1 -1
- package/dist/adapters/http/client/transport.d.ts +4 -0
- package/dist/adapters/http/client/transport.d.ts.map +1 -1
- package/dist/adapters/http/client/transport.js +11 -1
- package/dist/adapters/http/client/transport.js.map +1 -1
- package/dist/adapters/http/client/types.d.ts +19 -0
- package/dist/adapters/http/client/types.d.ts.map +1 -1
- package/dist/adapters/http/server/cors.d.ts +64 -0
- package/dist/adapters/http/server/cors.d.ts.map +1 -0
- package/dist/adapters/http/server/cors.js +113 -0
- package/dist/adapters/http/server/cors.js.map +1 -0
- package/dist/adapters/http/server/handler.d.ts +136 -0
- package/dist/adapters/http/server/handler.d.ts.map +1 -0
- package/dist/adapters/http/server/handler.js +495 -0
- package/dist/adapters/http/server/handler.js.map +1 -0
- package/dist/adapters/http/server/strategies.d.ts +14 -5
- package/dist/adapters/http/server/strategies.d.ts.map +1 -1
- package/dist/adapters/http/server/strategies.js.map +1 -1
- package/dist/adapters/http/server/transport.d.ts +36 -46
- package/dist/adapters/http/server/transport.d.ts.map +1 -1
- package/dist/adapters/http/server/transport.js +58 -433
- package/dist/adapters/http/server/transport.js.map +1 -1
- package/dist/adapters/http/server/types.d.ts +13 -0
- package/dist/adapters/http/server/types.d.ts.map +1 -1
- package/dist/adapters/websocket/client/transport.d.ts +7 -3
- package/dist/adapters/websocket/client/transport.d.ts.map +1 -1
- package/dist/adapters/websocket/client/transport.js +9 -3
- package/dist/adapters/websocket/client/transport.js.map +1 -1
- package/dist/adapters/websocket/server/transport.d.ts +56 -1
- package/dist/adapters/websocket/server/transport.d.ts.map +1 -1
- package/dist/adapters/websocket/server/transport.js +174 -17
- package/dist/adapters/websocket/server/transport.js.map +1 -1
- package/dist/adapters/websocket/server/types.d.ts +10 -3
- package/dist/adapters/websocket/server/types.d.ts.map +1 -1
- package/dist/adapters/websocket/server/upgrade-router.d.ts +28 -0
- package/dist/adapters/websocket/server/upgrade-router.d.ts.map +1 -0
- package/dist/adapters/websocket/server/upgrade-router.js +62 -0
- package/dist/adapters/websocket/server/upgrade-router.js.map +1 -0
- package/dist/adapters/websocket/server/upgrade.d.ts +68 -0
- package/dist/adapters/websocket/server/upgrade.d.ts.map +1 -0
- package/dist/adapters/websocket/server/upgrade.js +49 -0
- package/dist/adapters/websocket/server/upgrade.js.map +1 -0
- package/dist/adapters/websocket/shared/rpc.d.ts +2 -0
- package/dist/adapters/websocket/shared/rpc.d.ts.map +1 -1
- package/dist/adapters/websocket/shared/rpc.js +8 -2
- package/dist/adapters/websocket/shared/rpc.js.map +1 -1
- package/dist/browser.d.ts +14 -6
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +12 -3
- package/dist/browser.js.map +1 -1
- package/dist/client/client.d.ts +43 -0
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +69 -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 +1 -0
- package/dist/client/errors/registry.js.map +1 -1
- package/dist/client/middleware/retry.d.ts +4 -0
- package/dist/client/middleware/retry.d.ts.map +1 -1
- package/dist/client/middleware/retry.js +86 -78
- package/dist/client/middleware/retry.js.map +1 -1
- package/dist/events/bus.d.ts +32 -14
- package/dist/events/bus.d.ts.map +1 -1
- package/dist/events/bus.js +119 -65
- package/dist/events/bus.js.map +1 -1
- package/dist/events/index.d.ts +3 -2
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js.map +1 -1
- package/dist/events/publisher.d.ts +14 -0
- package/dist/events/publisher.d.ts.map +1 -1
- package/dist/events/publisher.js +62 -10
- package/dist/events/publisher.js.map +1 -1
- package/dist/events/remote.d.ts +80 -18
- package/dist/events/remote.d.ts.map +1 -1
- package/dist/events/remote.js +448 -58
- package/dist/events/remote.js.map +1 -1
- package/dist/events/rows.d.ts +31 -0
- package/dist/events/rows.d.ts.map +1 -0
- package/dist/events/rows.js +39 -0
- package/dist/events/rows.js.map +1 -0
- package/dist/events/types.d.ts +13 -2
- package/dist/events/types.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +14 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +9 -0
- package/dist/node.js.map +1 -1
- package/dist/procedures/caller.d.ts +33 -0
- package/dist/procedures/caller.d.ts.map +1 -0
- package/dist/procedures/caller.js +27 -0
- package/dist/procedures/caller.js.map +1 -0
- package/dist/procedures/capability.d.ts +6 -0
- package/dist/procedures/capability.d.ts.map +1 -1
- package/dist/procedures/capability.js +25 -0
- package/dist/procedures/capability.js.map +1 -1
- package/dist/procedures/collection/changes.d.ts +94 -0
- package/dist/procedures/collection/changes.d.ts.map +1 -0
- package/dist/procedures/collection/changes.js +182 -0
- package/dist/procedures/collection/changes.js.map +1 -0
- package/dist/procedures/collection/index.d.ts +8 -3
- package/dist/procedures/collection/index.d.ts.map +1 -1
- package/dist/procedures/collection/index.js +6 -1
- package/dist/procedures/collection/index.js.map +1 -1
- package/dist/procedures/collection/procedures.d.ts +39 -2
- package/dist/procedures/collection/procedures.d.ts.map +1 -1
- package/dist/procedures/collection/procedures.js +131 -8
- package/dist/procedures/collection/procedures.js.map +1 -1
- package/dist/procedures/collection/query.d.ts +246 -0
- package/dist/procedures/collection/query.d.ts.map +1 -0
- package/dist/procedures/collection/query.js +854 -0
- package/dist/procedures/collection/query.js.map +1 -0
- package/dist/procedures/collection/schemas.d.ts +59 -0
- package/dist/procedures/collection/schemas.d.ts.map +1 -1
- package/dist/procedures/collection/schemas.js +172 -6
- package/dist/procedures/collection/schemas.js.map +1 -1
- package/dist/procedures/collection/storage.d.ts +64 -6
- package/dist/procedures/collection/storage.d.ts.map +1 -1
- package/dist/procedures/collection/storage.js +23 -0
- package/dist/procedures/collection/storage.js.map +1 -1
- package/dist/procedures/elicit.d.ts +190 -0
- package/dist/procedures/elicit.d.ts.map +1 -0
- package/dist/procedures/elicit.js +127 -0
- package/dist/procedures/elicit.js.map +1 -0
- package/dist/procedures/hints.d.ts +43 -0
- package/dist/procedures/hints.d.ts.map +1 -0
- package/dist/procedures/hints.js +74 -0
- package/dist/procedures/hints.js.map +1 -0
- package/dist/procedures/index.d.ts +12 -3
- package/dist/procedures/index.d.ts.map +1 -1
- package/dist/procedures/index.js +7 -1
- package/dist/procedures/index.js.map +1 -1
- package/dist/procedures/invoke.d.ts +20 -0
- package/dist/procedures/invoke.d.ts.map +1 -1
- package/dist/procedures/invoke.js +16 -0
- package/dist/procedures/invoke.js.map +1 -1
- package/dist/procedures/progress.d.ts +24 -0
- package/dist/procedures/progress.d.ts.map +1 -0
- package/dist/procedures/progress.js +11 -0
- package/dist/procedures/progress.js.map +1 -0
- package/dist/procedures/registry.d.ts.map +1 -1
- package/dist/procedures/registry.js +5 -0
- package/dist/procedures/registry.js.map +1 -1
- package/dist/procedures/types.d.ts +92 -0
- package/dist/procedures/types.d.ts.map +1 -1
- package/dist/procedures/types.js.map +1 -1
- package/dist/procedures/untrusted.d.ts +28 -0
- package/dist/procedures/untrusted.d.ts.map +1 -0
- package/dist/procedures/untrusted.js +32 -0
- package/dist/procedures/untrusted.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/browser-guard.d.ts +12 -2
- package/dist/server/browser-guard.d.ts.map +1 -1
- package/dist/server/browser-guard.js +43 -6
- package/dist/server/browser-guard.js.map +1 -1
- package/dist/server/host-policy.d.ts +10 -1
- package/dist/server/host-policy.d.ts.map +1 -1
- package/dist/server/host-policy.js +5 -0
- package/dist/server/host-policy.js.map +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/procedure-server.d.ts +75 -9
- package/dist/server/procedure-server.d.ts.map +1 -1
- package/dist/server/procedure-server.js +151 -17
- package/dist/server/procedure-server.js.map +1 -1
- package/dist/server/request-auth.d.ts +51 -4
- package/dist/server/request-auth.d.ts.map +1 -1
- package/dist/server/request-auth.js +116 -5
- package/dist/server/request-auth.js.map +1 -1
- package/dist/server/types.d.ts +73 -5
- package/dist/server/types.d.ts.map +1 -1
- package/dist/server/types.js.map +1 -1
- package/dist/timers.d.ts +5 -0
- package/dist/timers.d.ts.map +1 -1
- package/dist/timers.js +3 -1
- package/dist/timers.js.map +1 -1
- package/package.json +5 -1
- package/src/adapters/http/client/transport.ts +11 -1
- package/src/adapters/http/client/types.ts +22 -0
- package/src/adapters/http/server/cors.ts +144 -0
- package/src/adapters/http/server/handler.ts +624 -0
- package/src/adapters/http/server/strategies.ts +16 -6
- package/src/adapters/http/server/transport.ts +66 -479
- package/src/adapters/http/server/types.ts +15 -0
- package/src/adapters/websocket/client/transport.ts +9 -3
- package/src/adapters/websocket/server/transport.ts +189 -18
- package/src/adapters/websocket/server/types.ts +11 -3
- package/src/adapters/websocket/server/upgrade-router.ts +77 -0
- package/src/adapters/websocket/server/upgrade.ts +88 -0
- package/src/adapters/websocket/shared/rpc.ts +9 -2
- package/src/browser.ts +85 -2
- package/src/client/client.ts +83 -1
- package/src/client/errors/registry.ts +1 -0
- package/src/client/middleware/retry.ts +89 -78
- package/src/events/bus.ts +141 -73
- package/src/events/index.ts +3 -0
- package/src/events/publisher.ts +71 -10
- package/src/events/remote.ts +575 -73
- package/src/events/rows.ts +48 -0
- package/src/events/types.ts +15 -2
- package/src/index.ts +1 -1
- package/src/node.ts +21 -0
- package/src/procedures/caller.ts +45 -0
- package/src/procedures/capability.ts +21 -0
- package/src/procedures/collection/changes.ts +245 -0
- package/src/procedures/collection/index.ts +56 -1
- package/src/procedures/collection/procedures.ts +185 -7
- package/src/procedures/collection/query.ts +1009 -0
- package/src/procedures/collection/schemas.ts +233 -7
- package/src/procedures/collection/storage.ts +84 -6
- package/src/procedures/elicit.ts +320 -0
- package/src/procedures/hints.ts +91 -0
- package/src/procedures/index.ts +82 -2
- package/src/procedures/invoke.ts +31 -0
- package/src/procedures/progress.ts +25 -0
- package/src/procedures/registry.ts +4 -0
- package/src/procedures/types.ts +102 -0
- package/src/procedures/untrusted.ts +50 -0
- package/src/protocol.ts +3 -1
- package/src/server/browser-guard.ts +48 -6
- package/src/server/host-policy.ts +13 -1
- package/src/server/index.ts +3 -0
- package/src/server/procedure-server.ts +185 -20
- package/src/server/request-auth.ts +128 -6
- package/src/server/types.ts +82 -5
- package/src/timers.ts +8 -1
package/dist/server/types.d.ts
CHANGED
|
@@ -4,7 +4,10 @@
|
|
|
4
4
|
* Protocol-agnostic server types mirroring the client architecture.
|
|
5
5
|
* Enables handling HTTP, WebSocket, gRPC, etc. with unified RPC format.
|
|
6
6
|
*/
|
|
7
|
+
import type { Elicit } from "../procedures/elicit.js";
|
|
8
|
+
import type { ReportProgress } from "../procedures/progress.js";
|
|
7
9
|
import type { Metadata, Status, Method } from "../client/types.js";
|
|
10
|
+
import type { CapabilityRequest } from "../procedures/types.js";
|
|
8
11
|
export type { Method };
|
|
9
12
|
/**
|
|
10
13
|
* Server request in unified RPC format.
|
|
@@ -19,23 +22,74 @@ export interface ServerRequest<TReq = unknown> {
|
|
|
19
22
|
payload: TReq;
|
|
20
23
|
/** Request metadata (headers, query params, etc.) */
|
|
21
24
|
metadata: Metadata;
|
|
25
|
+
/**
|
|
26
|
+
* The function that asks the person at the caller, when the face can ask (MCP elicitation,
|
|
27
|
+
* CLI-12). The procedure gets it as `ctx.elicit`. It is not in the metadata, so a caller cannot
|
|
28
|
+
* send it.
|
|
29
|
+
*/
|
|
30
|
+
elicit?: Elicit | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* The function that tells the caller the progress of the call, when the caller asked for it
|
|
33
|
+
* (an MCP progress token). The procedure gets it as `ctx.progress`.
|
|
34
|
+
*/
|
|
35
|
+
progress?: ReportProgress | undefined;
|
|
22
36
|
/** Abort signal for cancellation */
|
|
23
37
|
signal?: AbortSignal;
|
|
24
38
|
/**
|
|
25
|
-
* The credentials that a network transport read from the request
|
|
26
|
-
* `Authorization` header, or of the `token` query of a WebSocket connection
|
|
27
|
-
*
|
|
28
|
-
*
|
|
39
|
+
* The credentials that a network transport read from the request (roadmap R18). They have the
|
|
40
|
+
* bearer token of an `Authorization` header, or of the `token` query of a WebSocket connection.
|
|
41
|
+
* They also have the cookies, the headers and the query of the request. They are not in the
|
|
42
|
+
* metadata, so a handler and a nested call do not see them. A request without credentials comes
|
|
43
|
+
* from this process (a direct `handle()`, MCP over stdio).
|
|
29
44
|
*/
|
|
30
45
|
credentials?: Credentials | undefined;
|
|
31
46
|
/** The principal of the request: who sends it. `ProcedureServer` sets it when it authenticates the request. */
|
|
32
47
|
principal?: Principal | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* A narrower capability that the request asks for. The request hook of the server
|
|
50
|
+
* (`principalOf`) can set it. The server narrows the capability of the call with it, so the
|
|
51
|
+
* capability does not become wider.
|
|
52
|
+
*/
|
|
53
|
+
narrowing?: CapabilityRequest | undefined;
|
|
33
54
|
}
|
|
34
|
-
/**
|
|
55
|
+
/**
|
|
56
|
+
* The credentials of a request. A network transport sets each field that it can read. The request
|
|
57
|
+
* hook of the server (`principalOf`) reads them.
|
|
58
|
+
*/
|
|
35
59
|
export interface Credentials {
|
|
36
60
|
/** The bearer token that the request presents. */
|
|
37
61
|
readonly bearer?: string | undefined;
|
|
62
|
+
/** The cookies of the request, by name (the `Cookie` header of an HTTP request or of an upgrade). */
|
|
63
|
+
readonly cookies?: Readonly<Record<string, string>> | undefined;
|
|
64
|
+
/** Each header of the request, the secret headers too, by lower-case name. */
|
|
65
|
+
readonly headers?: Readonly<Record<string, string>> | undefined;
|
|
66
|
+
/** The first value of each query parameter of the request URL, by name. */
|
|
67
|
+
readonly query?: Readonly<Record<string, string>> | undefined;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* What the request hook of a server (`principalOf`) gives for a network request. Each field is
|
|
71
|
+
* optional: an empty object accepts the request without a principal.
|
|
72
|
+
*/
|
|
73
|
+
export interface RequestIdentity {
|
|
74
|
+
/**
|
|
75
|
+
* The token to compare with the token of the server, for example the value of a cookie or of an
|
|
76
|
+
* application header. Without it, the server compares the bearer token of the credentials.
|
|
77
|
+
*/
|
|
78
|
+
readonly bearer?: string | undefined;
|
|
79
|
+
/** The principal of the request. It replaces the principal of the token check. */
|
|
80
|
+
readonly principal?: Principal | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* A narrower capability that the request asks for, for example a guest mode of a cookie. The
|
|
83
|
+
* server narrows the capability of the call with it. A request for more has no effect.
|
|
84
|
+
*/
|
|
85
|
+
readonly narrowing?: CapabilityRequest | undefined;
|
|
38
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* The request hook of a server: it reads the credentials of a network request (a cookie, a header,
|
|
89
|
+
* the query) and gives the identity of the request. Undefined, or an error, refuses the request
|
|
90
|
+
* with `UNAUTHORIZED`. The hook runs before the token check and before `authenticate`.
|
|
91
|
+
*/
|
|
92
|
+
export type PrincipalOf = (credentials: Credentials, request: ServerRequest<unknown>) => RequestIdentity | undefined | Promise<RequestIdentity | undefined>;
|
|
39
93
|
/** Who sends a request (roadmap R18). */
|
|
40
94
|
export interface Principal {
|
|
41
95
|
/** The id of the principal. With the token of a host: "token". */
|
|
@@ -53,7 +107,21 @@ export interface Principal {
|
|
|
53
107
|
*/
|
|
54
108
|
export interface RequestHandler {
|
|
55
109
|
handle(request: ServerRequest): Promise<ServerResponse>;
|
|
110
|
+
/**
|
|
111
|
+
* How the handler can authenticate a network request with ambient credentials: the cookies that
|
|
112
|
+
* a browser sends by itself, also for a request of another site (CSRF). A network face then
|
|
113
|
+
* trusts such a request only from a named origin. Absent: the handler reads no cookies.
|
|
114
|
+
*/
|
|
115
|
+
readonly ambientCredentials?: AmbientCredentials | undefined;
|
|
56
116
|
}
|
|
117
|
+
/**
|
|
118
|
+
* How a server can authenticate a request with ambient credentials (cookies):
|
|
119
|
+
* - `"always"`: each network request. The request hook `principalOf` reads the cookies, so a face
|
|
120
|
+
* trusts a request only from a named origin.
|
|
121
|
+
* - `"cookies"`: a request that carries a cookie. An `authenticate` function can read it, so a face
|
|
122
|
+
* trusts a request with a cookie only from a named origin.
|
|
123
|
+
*/
|
|
124
|
+
export type AmbientCredentials = "always" | "cookies";
|
|
57
125
|
/**
|
|
58
126
|
* Server response in unified RPC format.
|
|
59
127
|
* Converted by transport adapters to protocol-specific responses.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/server/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/server/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAGhE,YAAY,EAAE,MAAM,EAAE,CAAC;AAEvB;;;GAGG;AACH,MAAM,WAAW,aAAa,CAAC,IAAI,GAAG,OAAO;IAC3C,mDAAmD;IACnD,EAAE,EAAE,MAAM,CAAC;IAEX,+CAA+C;IAC/C,MAAM,EAAE,MAAM,CAAC;IAEf,sBAAsB;IACtB,OAAO,EAAE,IAAI,CAAC;IAEd,qDAAqD;IACrD,QAAQ,EAAE,QAAQ,CAAC;IAEnB;;;;OA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G;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/server/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAqOH;;GAEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC7C,YAAY,MAAc;QACxB,KAAK,CACH,wBAAwB,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,SAAS,GACxD,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,MAAM,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,EAC7C,EAAE,CACH,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IAGlB,IAAI;IACJ,SAAS;IAH3B,YACE,OAAe,EACC,IAAI,GAAW,KAAK,EACpB,SAAS,GAAY,KAAK;QAE1C,KAAK,CAAC,OAAO,CAAC,CAAC;oBAHC,IAAI;yBACJ,SAAS;QAGzB,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF"}
|
package/dist/timers.d.ts
CHANGED
|
@@ -57,6 +57,11 @@ export interface TimerDefaults {
|
|
|
57
57
|
readonly server: {
|
|
58
58
|
/** The time that `ProcedureServer.stop()` waits for the calls that run. */
|
|
59
59
|
readonly stopGraceMs: number;
|
|
60
|
+
/**
|
|
61
|
+
* The longest time that `ProcedureServer.useStorage` with `close` waits for the calls on the
|
|
62
|
+
* old storage, before it closes the old storage.
|
|
63
|
+
*/
|
|
64
|
+
readonly storageGraceMs: number;
|
|
60
65
|
};
|
|
61
66
|
}
|
|
62
67
|
/** The default times of the core. The middleware, the transports and the server read them. */
|
package/dist/timers.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timers.d.ts","sourceRoot":"","sources":["../src/timers.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,sDAAsD;AACtD,MAAM,WAAW,aAAa;IAC5B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QACxF,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;QACjC,QAAQ,CAAC,uBAAuB,EAAE,MAAM,CAAC;QACzC,QAAQ,CAAC,mBAAmB,EAAE,MAAM,CAAC;KACtC,CAAC;IACF,QAAQ,CAAC,eAAe,EAAE;QACxB,QAAQ,CAAC,mBAAmB,EAAE,MAAM,CAAC;QACrC,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;QACjC,0EAA0E;QAC1E,QAAQ,CAAC,uBAAuB,EAAE,MAAM,CAAC;KAC1C,CAAC;IACF,QAAQ,CAAC,UAAU,EAAE;QACnB,yFAAyF;QACzF,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;KAC9B,CAAC;IACF,QAAQ,CAAC,WAAW,EAAE;QACpB,kEAAkE;QAClE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;KACxB,CAAC;IACF,QAAQ,CAAC,MAAM,EAAE;QACf,2EAA2E;QAC3E,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;QAC7B;;;WAGG;QACH,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;KACjC,CAAC;CACH;AAED,8FAA8F;AAC9F,eAAO,MAAM,cAAc,EAAE,aA0B3B,CAAC"}
|
package/dist/timers.js
CHANGED
|
@@ -31,6 +31,8 @@ export const TIMER_DEFAULTS = Object.freeze({
|
|
|
31
31
|
// stream then stays open (WIRE-PROTOCOL.md, "The keep-alive of an NDJSON stream").
|
|
32
32
|
httpServer: Object.freeze({ keepAliveMs: 15_000 }),
|
|
33
33
|
idempotency: Object.freeze({ ttlMs: 10 * 60_000 }),
|
|
34
|
-
|
|
34
|
+
// 30 s: the request time of the WebSocket client. A call that runs longer timed out at its
|
|
35
|
+
// client already.
|
|
36
|
+
server: Object.freeze({ stopGraceMs: 0, storageGraceMs: 30_000 }),
|
|
35
37
|
});
|
|
36
38
|
//# sourceMappingURL=timers.js.map
|
package/dist/timers.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timers.js","sourceRoot":"","sources":["../src/timers.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;
|
|
1
|
+
{"version":3,"file":"timers.js","sourceRoot":"","sources":["../src/timers.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AA4DH,8FAA8F;AAC9F,MAAM,CAAC,MAAM,cAAc,GAAkB,MAAM,CAAC,MAAM,CAAC;IACzD,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,YAAY,EAAE,IAAI,EAAE,iBAAiB,EAAE,CAAC,EAAE,UAAU,EAAE,MAAM,CAAC,iBAAiB,EAAE,CAAC;IACvH,cAAc,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,eAAe,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,CAAC;IAClF,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,CAAC;IAChE,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;IAC9C,eAAe,EAAE,MAAM,CAAC,MAAM,CAAC;QAC7B,mBAAmB,EAAE,MAAM;QAC3B,gBAAgB,EAAE,MAAM;QACxB,8FAA8F;QAC9F,4FAA4F;QAC5F,mBAAmB,EAAE,MAAM;QAC3B,kBAAkB,EAAE,KAAK;QACzB,eAAe,EAAE,CAAC;QAClB,uBAAuB,EAAE,KAAK;QAC9B,mBAAmB,EAAE,MAAM;KAC5B,CAAC;IACF,+FAA+F;IAC/F,8FAA8F;IAC9F,eAAe,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,mBAAmB,EAAE,MAAM,EAAE,eAAe,EAAE,CAAC,EAAE,uBAAuB,EAAE,MAAM,EAAE,CAAC;IACpH,iGAAiG;IACjG,mFAAmF;IACnF,UAAU,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,MAAM,EAAE,CAAC;IAClD,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,EAAE,GAAG,MAAM,EAAE,CAAC;IAClD,2FAA2F;IAC3F,kBAAkB;IAClB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,cAAc,EAAE,MAAM,EAAE,CAAC;CAClE,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@procedures/client",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.8.1",
|
|
5
5
|
"description": "Procedures as data for TypeScript: define typed procedures once and call them in process, over HTTP, WebSocket, the CLI or MCP. Streaming, middleware, retries, caching and a JSON program format.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"rpc",
|
|
@@ -70,6 +70,10 @@
|
|
|
70
70
|
"./server": {
|
|
71
71
|
"types": "./dist/server/index.d.ts",
|
|
72
72
|
"import": "./dist/server/index.js"
|
|
73
|
+
},
|
|
74
|
+
"./query": {
|
|
75
|
+
"types": "./dist/procedures/collection/query.d.ts",
|
|
76
|
+
"import": "./dist/procedures/collection/query.js"
|
|
73
77
|
}
|
|
74
78
|
},
|
|
75
79
|
"files": [
|
|
@@ -83,6 +83,10 @@ export class HttpTransport implements Transport {
|
|
|
83
83
|
private readonly headerConverter: HeaderConverter;
|
|
84
84
|
private readonly defaultHeaders: Record<string, string>;
|
|
85
85
|
private readonly timeout: number | undefined;
|
|
86
|
+
/** The more options of each fetch call: the credentials mode and the others (CLI-18) */
|
|
87
|
+
private readonly requestInit: Omit<RequestInit, "method" | "headers" | "body" | "signal">;
|
|
88
|
+
/** The fetch function, or undefined for the global fetch at the time of each call */
|
|
89
|
+
private readonly fetchFn: typeof fetch | undefined;
|
|
86
90
|
|
|
87
91
|
constructor(options: HttpTransportOptions) {
|
|
88
92
|
this.baseUrl = options.baseUrl;
|
|
@@ -93,6 +97,11 @@ export class HttpTransport implements Transport {
|
|
|
93
97
|
options.headerConverter || createDefaultHeaderConverter();
|
|
94
98
|
this.defaultHeaders = options.defaultHeaders || {};
|
|
95
99
|
this.timeout = optionalMs(options.timeout, "timeout");
|
|
100
|
+
this.requestInit = {
|
|
101
|
+
...options.requestInit,
|
|
102
|
+
...(options.credentials !== undefined ? { credentials: options.credentials } : {}),
|
|
103
|
+
};
|
|
104
|
+
this.fetchFn = options.fetch;
|
|
96
105
|
}
|
|
97
106
|
|
|
98
107
|
/**
|
|
@@ -136,6 +145,7 @@ export class HttpTransport implements Transport {
|
|
|
136
145
|
|
|
137
146
|
// Build fetch options
|
|
138
147
|
const fetchOptions: RequestInit = {
|
|
148
|
+
...this.requestInit,
|
|
139
149
|
method: httpMethod,
|
|
140
150
|
headers: {
|
|
141
151
|
[HTTPHeaders.CONTENT_TYPE]: "application/json",
|
|
@@ -156,7 +166,7 @@ export class HttpTransport implements Transport {
|
|
|
156
166
|
}
|
|
157
167
|
|
|
158
168
|
// Execute HTTP request
|
|
159
|
-
const response = await fetch(url, fetchOptions);
|
|
169
|
+
const response = await (this.fetchFn ?? fetch)(url, fetchOptions);
|
|
160
170
|
const metadata = this.headerConverter.headersToMetadata(response.headers);
|
|
161
171
|
|
|
162
172
|
if (response.headers.get(HTTPHeaders.CONTENT_TYPE)?.includes(NDJSON) && response.body) {
|
|
@@ -31,4 +31,26 @@ export interface HttpTransportOptions {
|
|
|
31
31
|
|
|
32
32
|
/** The time to the first item of a response, for example `seconds(30)` */
|
|
33
33
|
timeout?: Duration;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The credentials mode of `fetch` (CLI-18). `"include"` sends the cookies of the server to
|
|
37
|
+
* another origin too. An application with a cookie session needs it for procedures on another
|
|
38
|
+
* origin. The server then must permit the origin with credentials (CORS). This option goes over
|
|
39
|
+
* `requestInit.credentials`.
|
|
40
|
+
* @default the default of `fetch`: "same-origin"
|
|
41
|
+
*/
|
|
42
|
+
credentials?: "include" | "same-origin" | "omit";
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* More options of each `fetch` call: `mode`, `cache`, `redirect`, `referrerPolicy`, `keepalive`,
|
|
46
|
+
* `priority` and the others. The transport sets `method`, `headers`, `body` and `signal` itself.
|
|
47
|
+
*/
|
|
48
|
+
requestInit?: Omit<RequestInit, "method" | "headers" | "body" | "signal">;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The `fetch` function of the transport, for example a `fetch` with a cookie jar in Node, or a
|
|
52
|
+
* test double.
|
|
53
|
+
* @default globalThis.fetch
|
|
54
|
+
*/
|
|
55
|
+
fetch?: typeof fetch;
|
|
34
56
|
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The CORS headers of a procedure server, without a framework. `procedureHandler` applies them to
|
|
3
|
+
* the requests on its path. The Express shim `HttpServerTransport` applies them to each request of
|
|
4
|
+
* its app, as before. This module imports only the types of `node:http`.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
8
|
+
import { HTTPHeaders, HTTPMethod } from "../shared/index.js";
|
|
9
|
+
import { PROTOCOL_HEADER } from "../../../protocol.js";
|
|
10
|
+
import { toMs, type Duration } from "../../../duration.js";
|
|
11
|
+
|
|
12
|
+
/** The CORS options of a procedure server. */
|
|
13
|
+
export interface CorsOptions {
|
|
14
|
+
/**
|
|
15
|
+
* The origins that can read the responses: `"*"`, one origin, a list, or a function that
|
|
16
|
+
* decides for each origin. A response names the origin of the request when the origin is
|
|
17
|
+
* permitted, with `Vary: Origin`.
|
|
18
|
+
* @default "*"
|
|
19
|
+
*/
|
|
20
|
+
readonly origin?: string | readonly string[] | ((origin: string) => boolean) | undefined;
|
|
21
|
+
/** The methods of `Access-Control-Allow-Methods`. @default GET, POST, PUT, DELETE, PATCH, OPTIONS */
|
|
22
|
+
readonly methods?: readonly string[] | undefined;
|
|
23
|
+
/** The request headers of `Access-Control-Allow-Headers`. @default the headers of `HttpTransport` */
|
|
24
|
+
readonly allowedHeaders?: readonly string[] | undefined;
|
|
25
|
+
/** The response headers that a page can read (`Access-Control-Expose-Headers`). @default none */
|
|
26
|
+
readonly exposedHeaders?: readonly string[] | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* True: a page can send cookies (`fetch` with `credentials: "include"`). The response then has
|
|
29
|
+
* `Access-Control-Allow-Credentials: true`. A browser refuses `"*"` with credentials, so give the
|
|
30
|
+
* origins.
|
|
31
|
+
*/
|
|
32
|
+
readonly credentials?: boolean | undefined;
|
|
33
|
+
/** The time that a browser keeps the answer to a preflight request (`Access-Control-Max-Age`). */
|
|
34
|
+
readonly maxAge?: Duration | undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The request headers that `HttpTransport` sends: the default `allowedHeaders`. */
|
|
38
|
+
export const DEFAULT_CORS_HEADERS: readonly string[] = Object.freeze([
|
|
39
|
+
"Content-Type",
|
|
40
|
+
"Accept",
|
|
41
|
+
HTTPHeaders.AUTHORIZATION,
|
|
42
|
+
HTTPHeaders.API_KEY,
|
|
43
|
+
HTTPHeaders.REQUEST_ID,
|
|
44
|
+
HTTPHeaders.TRACE_ID,
|
|
45
|
+
HTTPHeaders.SPAN_ID,
|
|
46
|
+
HTTPHeaders.PARENT_SPAN_ID,
|
|
47
|
+
"traceparent",
|
|
48
|
+
PROTOCOL_HEADER,
|
|
49
|
+
HTTPHeaders.METADATA,
|
|
50
|
+
"collection",
|
|
51
|
+
"collectionName",
|
|
52
|
+
"database",
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
/** The methods of the default `Access-Control-Allow-Methods`. */
|
|
56
|
+
export const DEFAULT_CORS_METHODS: readonly string[] = Object.freeze([
|
|
57
|
+
HTTPMethod.GET,
|
|
58
|
+
HTTPMethod.POST,
|
|
59
|
+
HTTPMethod.PUT,
|
|
60
|
+
HTTPMethod.DELETE,
|
|
61
|
+
HTTPMethod.PATCH,
|
|
62
|
+
HTTPMethod.OPTIONS,
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
/** The CORS options with their defaults, ready for each request. */
|
|
66
|
+
export interface CorsPolicy {
|
|
67
|
+
/** The permitted origin to send back for a request origin, or undefined for none. */
|
|
68
|
+
readonly allowOrigin: (requestOrigin: string | undefined) => string | undefined;
|
|
69
|
+
/** True when the permitted origin depends on the request: the response gets `Vary: Origin`. */
|
|
70
|
+
readonly varies: boolean;
|
|
71
|
+
readonly methods: string | undefined;
|
|
72
|
+
readonly allowedHeaders: string | undefined;
|
|
73
|
+
readonly exposedHeaders: string | undefined;
|
|
74
|
+
readonly credentials: boolean;
|
|
75
|
+
/** The value of `Access-Control-Max-Age` in seconds, or undefined. */
|
|
76
|
+
readonly maxAge: string | undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* This function makes the CORS policy of the options. `defaultCredentials` is the default of the
|
|
81
|
+
* `credentials` option.
|
|
82
|
+
*/
|
|
83
|
+
export function corsPolicy(options: CorsOptions, defaultCredentials: boolean): CorsPolicy {
|
|
84
|
+
const origin = options.origin ?? "*";
|
|
85
|
+
const allowOrigin = (requestOrigin: string | undefined): string | undefined => {
|
|
86
|
+
if (origin === "*") return "*";
|
|
87
|
+
if (requestOrigin === undefined) return undefined;
|
|
88
|
+
if (typeof origin === "function") return origin(requestOrigin) ? requestOrigin : undefined;
|
|
89
|
+
const list: readonly string[] = typeof origin === "string" ? [origin] : origin;
|
|
90
|
+
return list.includes(requestOrigin) ? requestOrigin : undefined;
|
|
91
|
+
};
|
|
92
|
+
const join = (list: readonly string[] | undefined): string | undefined => (list === undefined || list.length === 0 ? undefined : list.join(", "));
|
|
93
|
+
const maxAgeMs = options.maxAge === undefined ? undefined : toMs(options.maxAge, "cors.maxAge");
|
|
94
|
+
return {
|
|
95
|
+
allowOrigin,
|
|
96
|
+
varies: origin !== "*",
|
|
97
|
+
methods: join("methods" in options ? options.methods : DEFAULT_CORS_METHODS),
|
|
98
|
+
allowedHeaders: join("allowedHeaders" in options ? options.allowedHeaders : DEFAULT_CORS_HEADERS),
|
|
99
|
+
exposedHeaders: join(options.exposedHeaders),
|
|
100
|
+
credentials: options.credentials ?? defaultCredentials,
|
|
101
|
+
maxAge: maxAgeMs === undefined || maxAgeMs <= 0 ? undefined : String(Math.floor(maxAgeMs / 1000)),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** This function adds a value to the `Vary` header of a response, one time. */
|
|
106
|
+
function vary(res: ServerResponse, field: string): void {
|
|
107
|
+
const current = res.getHeader("Vary");
|
|
108
|
+
const text = Array.isArray(current) ? current.join(", ") : current === undefined ? "" : String(current);
|
|
109
|
+
const fields = text.split(",").map((part) => part.trim().toLowerCase());
|
|
110
|
+
if (fields.includes("*") || fields.includes(field.toLowerCase())) return;
|
|
111
|
+
res.setHeader("Vary", text.trim() === "" ? field : `${text}, ${field}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* This function sets the CORS headers of a response. It gives true for a preflight request
|
|
116
|
+
* (`OPTIONS`): the function then ends the response with 204.
|
|
117
|
+
*/
|
|
118
|
+
export function applyCors(req: IncomingMessage, res: ServerResponse, policy: CorsPolicy): boolean {
|
|
119
|
+
const requestOrigin = typeof req.headers.origin === "string" ? req.headers.origin : undefined;
|
|
120
|
+
const allowed = policy.allowOrigin(requestOrigin);
|
|
121
|
+
if (policy.varies) vary(res, "Origin");
|
|
122
|
+
if (allowed !== undefined) res.setHeader("Access-Control-Allow-Origin", allowed);
|
|
123
|
+
if (policy.methods !== undefined) res.setHeader("Access-Control-Allow-Methods", policy.methods);
|
|
124
|
+
if (policy.allowedHeaders !== undefined) res.setHeader("Access-Control-Allow-Headers", policy.allowedHeaders);
|
|
125
|
+
if (policy.exposedHeaders !== undefined) res.setHeader("Access-Control-Expose-Headers", policy.exposedHeaders);
|
|
126
|
+
if (policy.credentials) res.setHeader("Access-Control-Allow-Credentials", "true");
|
|
127
|
+
if (req.method !== "OPTIONS") return false;
|
|
128
|
+
if (policy.maxAge !== undefined) res.setHeader("Access-Control-Max-Age", policy.maxAge);
|
|
129
|
+
res.statusCode = 204;
|
|
130
|
+
res.end();
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The origins that the CORS options permit, for the browser checks: a page that CORS lets read the
|
|
136
|
+
* response can also send a call. Without CORS, no other origin can send a call.
|
|
137
|
+
*/
|
|
138
|
+
export function corsOrigins(cors: CorsOptions | undefined): readonly string[] | "*" | ((origin: string) => boolean) {
|
|
139
|
+
if (!cors) return [];
|
|
140
|
+
const origin = cors.origin ?? "*";
|
|
141
|
+
if (origin === "*") return "*";
|
|
142
|
+
if (typeof origin === "function") return origin;
|
|
143
|
+
return typeof origin === "string" ? [origin] : origin;
|
|
144
|
+
}
|