@procedures/client 0.8.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 +58 -0
- package/README.md +95 -8
- 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 +30 -46
- package/dist/adapters/http/server/transport.d.ts.map +1 -1
- package/dist/adapters/http/server/transport.js +48 -432
- 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/server/transport.d.ts +46 -1
- package/dist/adapters/websocket/server/transport.d.ts.map +1 -1
- package/dist/adapters/websocket/server/transport.js +152 -15
- 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 +12 -4
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +11 -2
- 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/events/publisher.d.ts +14 -0
- package/dist/events/publisher.d.ts.map +1 -1
- package/dist/events/publisher.js +42 -4
- package/dist/events/publisher.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/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/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 +111 -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 +50 -0
- package/dist/procedures/collection/schemas.d.ts.map +1 -1
- package/dist/procedures/collection/schemas.js +144 -6
- package/dist/procedures/collection/schemas.js.map +1 -1
- package/dist/procedures/collection/storage.d.ts +58 -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 +9 -2
- package/dist/procedures/index.d.ts.map +1 -1
- package/dist/procedures/index.js +5 -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 +13 -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 +42 -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/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 +38 -6
- package/dist/server/procedure-server.d.ts.map +1 -1
- package/dist/server/procedure-server.js +65 -13
- 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/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 +55 -478
- package/src/adapters/http/server/types.ts +15 -0
- package/src/adapters/websocket/server/transport.ts +166 -16
- 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 +79 -2
- package/src/client/client.ts +83 -1
- package/src/events/publisher.ts +48 -4
- package/src/events/rows.ts +48 -0
- package/src/index.ts +1 -1
- package/src/node.ts +21 -0
- package/src/procedures/capability.ts +21 -0
- package/src/procedures/collection/changes.ts +245 -0
- package/src/procedures/collection/index.ts +53 -1
- package/src/procedures/collection/procedures.ts +163 -7
- package/src/procedures/collection/query.ts +1009 -0
- package/src/procedures/collection/schemas.ts +196 -7
- package/src/procedures/collection/storage.ts +78 -6
- package/src/procedures/elicit.ts +320 -0
- package/src/procedures/hints.ts +91 -0
- package/src/procedures/index.ts +78 -2
- package/src/procedures/invoke.ts +29 -0
- package/src/procedures/progress.ts +25 -0
- package/src/procedures/registry.ts +4 -0
- package/src/procedures/types.ts +49 -0
- package/src/procedures/untrusted.ts +50 -0
- 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 +82 -14
- package/src/server/request-auth.ts +128 -6
- package/src/server/types.ts +82 -5
|
@@ -1,15 +1,36 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The credentials of a network request (roadmap R18). A server transport reads them into
|
|
3
|
-
* `ServerRequest.credentials
|
|
4
|
-
*
|
|
3
|
+
* `ServerRequest.credentials`. It leaves the secret headers out of the metadata, so a handler and a
|
|
4
|
+
* nested call do not see them. `ProcedureServer` examines them.
|
|
5
|
+
*
|
|
6
|
+
* This module imports no Node code: a server in a browser or a worker can use it too.
|
|
5
7
|
*/
|
|
6
8
|
/** The headers that carry a secret. They do not go into the metadata of a request. */
|
|
7
9
|
const SECRET_HEADERS = new Set(["authorization", "proxy-authorization", "cookie", "x-api-key"]);
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
+
/**
|
|
11
|
+
* True when a header carries a secret: a header of the default list (`Authorization`,
|
|
12
|
+
* `Proxy-Authorization`, `Cookie`, `X-Api-Key`), or a header of `more`. The names are lower case.
|
|
13
|
+
*/
|
|
14
|
+
export function isSecretHeader(name, more) {
|
|
15
|
+
const key = name.toLowerCase();
|
|
16
|
+
return SECRET_HEADERS.has(key) || (more?.has(key) ?? false);
|
|
17
|
+
}
|
|
18
|
+
/** This function gives a set of lower-case header names. */
|
|
19
|
+
export function headerNames(names) {
|
|
20
|
+
const set = new Set();
|
|
21
|
+
for (const name of names ?? [])
|
|
22
|
+
set.add(name.toLowerCase());
|
|
23
|
+
return set;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The headers without the secret ones, and without the headers that are not text. `secret` names
|
|
27
|
+
* more headers that carry a secret, for example the token header of an application.
|
|
28
|
+
*/
|
|
29
|
+
export function withoutSecretHeaders(headers, secret) {
|
|
30
|
+
const more = secret === undefined ? undefined : headerNames(secret);
|
|
10
31
|
const out = {};
|
|
11
32
|
for (const [key, value] of Object.entries(headers)) {
|
|
12
|
-
if (!
|
|
33
|
+
if (!isSecretHeader(key, more) && typeof value === "string")
|
|
13
34
|
out[key] = value;
|
|
14
35
|
}
|
|
15
36
|
return out;
|
|
@@ -29,4 +50,94 @@ export function credentialsOf(request) {
|
|
|
29
50
|
const query = new URL(request.url ?? "/", "http://localhost").searchParams.get("token");
|
|
30
51
|
return query === null ? {} : { bearer: query };
|
|
31
52
|
}
|
|
53
|
+
/** The text of a header value: a list of values becomes one text, with ", " between the values. */
|
|
54
|
+
function headerText(value) {
|
|
55
|
+
if (typeof value === "string")
|
|
56
|
+
return value;
|
|
57
|
+
if (Array.isArray(value))
|
|
58
|
+
return value.filter((item) => typeof item === "string").join(", ");
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
/** This function decodes a cookie value. A value that is not valid URI encoding stays as it is. */
|
|
62
|
+
function decodeCookieValue(value) {
|
|
63
|
+
const unquoted = value.length >= 2 && value.startsWith('"') && value.endsWith('"') ? value.slice(1, -1) : value;
|
|
64
|
+
try {
|
|
65
|
+
return decodeURIComponent(unquoted);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return unquoted;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The cookies of a `Cookie` header, by name. The function decodes each URI-encoded value when it
|
|
73
|
+
* can. When two cookies have the same name, the first one stays: a browser sends the
|
|
74
|
+
* cookie of the longest path first. A part without `=` is not a cookie.
|
|
75
|
+
*/
|
|
76
|
+
export function cookiesOf(header) {
|
|
77
|
+
const text = Array.isArray(header) ? header.filter((item) => typeof item === "string").join("; ") : header;
|
|
78
|
+
if (typeof text !== "string" || text === "")
|
|
79
|
+
return {};
|
|
80
|
+
const cookies = new Map();
|
|
81
|
+
for (const part of text.split(";")) {
|
|
82
|
+
const eq = part.indexOf("=");
|
|
83
|
+
if (eq <= 0)
|
|
84
|
+
continue;
|
|
85
|
+
const name = part.slice(0, eq).trim();
|
|
86
|
+
if (name === "" || cookies.has(name))
|
|
87
|
+
continue;
|
|
88
|
+
cookies.set(name, decodeCookieValue(part.slice(eq + 1).trim()));
|
|
89
|
+
}
|
|
90
|
+
// fromEntries defines own properties: a cookie named "__proto__" changes no prototype
|
|
91
|
+
return Object.fromEntries(cookies);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The parts of a request URL: its path and its query. The function does not resolve the URL
|
|
95
|
+
* against a host, so a path that starts with "//" stays a path.
|
|
96
|
+
*/
|
|
97
|
+
export function urlParts(url) {
|
|
98
|
+
let raw = url ?? "/";
|
|
99
|
+
// The absolute form of a request line (a request through a proxy): the path and the query only
|
|
100
|
+
if (!raw.startsWith("/") && /^[a-z][a-z0-9+.-]*:\/\//i.test(raw)) {
|
|
101
|
+
try {
|
|
102
|
+
const absolute = new URL(raw);
|
|
103
|
+
raw = `${absolute.pathname}${absolute.search}`;
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// Not a URL: the text stays a path
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const mark = raw.indexOf("?");
|
|
110
|
+
return mark === -1 ? { path: raw, query: new URLSearchParams() } : { path: raw.slice(0, mark), query: new URLSearchParams(raw.slice(mark + 1)) };
|
|
111
|
+
}
|
|
112
|
+
/** The first value of each query parameter, by name. */
|
|
113
|
+
export function queryRecord(query) {
|
|
114
|
+
const values = new Map();
|
|
115
|
+
for (const [key, value] of query)
|
|
116
|
+
if (!values.has(key))
|
|
117
|
+
values.set(key, value);
|
|
118
|
+
return Object.fromEntries(values);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The credentials of a network request (roadmap R18): the bearer token, the cookies, each header
|
|
122
|
+
* (the secret ones too) and the first value of each query parameter. The request hook of a server
|
|
123
|
+
* (`principalOf`) reads them. They are not in the metadata, so a handler does not see them.
|
|
124
|
+
*/
|
|
125
|
+
export function requestCredentials(request, options = {}) {
|
|
126
|
+
const headers = new Map();
|
|
127
|
+
for (const [key, value] of Object.entries(request.headers)) {
|
|
128
|
+
const text = headerText(value);
|
|
129
|
+
if (text !== undefined)
|
|
130
|
+
headers.set(key.toLowerCase(), text);
|
|
131
|
+
}
|
|
132
|
+
const { query } = urlParts(request.url);
|
|
133
|
+
const fromHeader = bearerOf(headers.get("authorization"));
|
|
134
|
+
const fromQuery = options.tokenQuery ? query.get(options.tokenQuery) ?? undefined : undefined;
|
|
135
|
+
const bearer = fromHeader ?? fromQuery;
|
|
136
|
+
return {
|
|
137
|
+
...(bearer !== undefined ? { bearer } : {}),
|
|
138
|
+
cookies: cookiesOf(headers.get("cookie")),
|
|
139
|
+
headers: Object.fromEntries(headers),
|
|
140
|
+
query: queryRecord(query),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
32
143
|
//# sourceMappingURL=request-auth.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-auth.js","sourceRoot":"","sources":["../../src/server/request-auth.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"request-auth.js","sourceRoot":"","sources":["../../src/server/request-auth.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,sFAAsF;AACtF,MAAM,cAAc,GAAwB,IAAI,GAAG,CAAC,CAAC,eAAe,EAAE,qBAAqB,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC;AAErH;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY,EAAE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SAAS;YAAE,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE,EAAE,IAAI,CAAC,CAAC;IAC/D,CAAC;IACD,MAAM,EAAE,KAAK,EAAE,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACxC,MAAM,UAAU,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC,CAAC;IAC1D,MAAM,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC;IAC9F,MAAM,MAAM,GAAG,UAAU,IAAI,SAAS,CAAC;IACvC,OAAO;QACL,GAAG,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3C,OAAO,EAAE,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACzC,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC;QACpC,KAAK,EAAE,WAAW,CAAC,KAAK,CAAC;KAC1B,CAAC;AACJ,CAAC"}
|
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,OAA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MAAM,EAAE,MAAM,EAOzB;CACF;AAED;;GAEG;AACH,qBAAa,WAAY,SAAQ,KAAK;aAGlB,IAAI,EAAE,MAAM;aACZ,SAAS,EAAE,OAAO;IAHpC,YACE,OAAO,EAAE,MAAM,EACC,IAAI,GAAE,MAAc,EACpB,SAAS,GAAE,OAAe,EAI3C;CACF"}
|
package/dist/server/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/server/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
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/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.8.
|
|
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
|
+
}
|