@procedures/client 0.18.1 → 0.20.0

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +25 -5
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/adapters/http/server/strategies.d.ts +17 -0
  5. package/dist/adapters/http/server/strategies.d.ts.map +1 -1
  6. package/dist/adapters/http/server/strategies.js.map +1 -1
  7. package/dist/adapters/http/server/types.d.ts +6 -3
  8. package/dist/adapters/http/server/types.d.ts.map +1 -1
  9. package/dist/adapters/port/transport.d.ts +10 -1
  10. package/dist/adapters/port/transport.d.ts.map +1 -1
  11. package/dist/adapters/port/transport.js +5 -1
  12. package/dist/adapters/port/transport.js.map +1 -1
  13. package/dist/adapters/websocket/client/transport.d.ts.map +1 -1
  14. package/dist/adapters/websocket/client/transport.js +8 -2
  15. package/dist/adapters/websocket/client/transport.js.map +1 -1
  16. package/dist/adapters/websocket/client/types.d.ts +3 -0
  17. package/dist/adapters/websocket/client/types.d.ts.map +1 -1
  18. package/dist/adapters/websocket/client/types.js.map +1 -1
  19. package/dist/browser.d.ts +7 -7
  20. package/dist/browser.d.ts.map +1 -1
  21. package/dist/browser.js +7 -7
  22. package/dist/browser.js.map +1 -1
  23. package/dist/index.d.ts +10 -12
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +9 -13
  26. package/dist/index.js.map +1 -1
  27. package/dist/node.d.ts +4 -5
  28. package/dist/node.d.ts.map +1 -1
  29. package/dist/node.js +4 -5
  30. package/dist/node.js.map +1 -1
  31. package/dist/server/browser-guard.d.ts +16 -4
  32. package/dist/server/browser-guard.d.ts.map +1 -1
  33. package/dist/server/browser-guard.js.map +1 -1
  34. package/dist/server/index.d.ts +3 -66
  35. package/dist/server/index.d.ts.map +1 -1
  36. package/dist/server/index.js +2 -22
  37. package/dist/server/index.js.map +1 -1
  38. package/dist/server/procedure-server.d.ts +13 -8
  39. package/dist/server/procedure-server.d.ts.map +1 -1
  40. package/dist/server/procedure-server.js +15 -3
  41. package/dist/server/procedure-server.js.map +1 -1
  42. package/dist/server/request-auth.d.ts +16 -1
  43. package/dist/server/request-auth.d.ts.map +1 -1
  44. package/dist/server/request-auth.js +18 -0
  45. package/dist/server/request-auth.js.map +1 -1
  46. package/dist/server/types.d.ts +22 -6
  47. package/dist/server/types.d.ts.map +1 -1
  48. package/dist/server/types.js +6 -0
  49. package/dist/server/types.js.map +1 -1
  50. package/package.json +2 -1
  51. package/src/adapters/http/server/strategies.ts +16 -0
  52. package/src/adapters/http/server/types.ts +6 -3
  53. package/src/adapters/port/transport.ts +15 -2
  54. package/src/adapters/websocket/client/transport.ts +10 -3
  55. package/src/adapters/websocket/client/types.ts +3 -0
  56. package/src/browser.ts +7 -6
  57. package/src/index.ts +11 -25
  58. package/src/node.ts +5 -6
  59. package/src/server/browser-guard.ts +19 -7
  60. package/src/server/index.ts +3 -90
  61. package/src/server/procedure-server.ts +23 -9
  62. package/src/server/request-auth.ts +20 -1
  63. package/src/server/types.ts +23 -6
@@ -22,8 +22,6 @@
22
22
  * Each refusal is the error `FORBIDDEN` of wire v1, with the status 403 and a reason (CLI-4).
23
23
  */
24
24
 
25
- import type { IncomingHttpHeaders } from "node:http";
26
-
27
25
  const LOOPBACK_NAMES = new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
28
26
 
29
27
  /** The options of the browser checks of a server transport. */
@@ -48,10 +46,24 @@ export interface BrowserGuardOptions {
48
46
  allowGet?: boolean | undefined;
49
47
  }
50
48
 
51
- /** The parts of an incoming request that the checks read. */
49
+ /**
50
+ * The headers of a request, by lower-case name. The type is structural, so the root names no Node
51
+ * module (CLI-61). `IncomingHttpHeaders` of `node:http` agrees with it.
52
+ */
53
+ export interface GuardedHeaders {
54
+ readonly [name: string]: string | string[] | undefined;
55
+ readonly host?: string | undefined;
56
+ readonly origin?: string | undefined;
57
+ readonly cookie?: string | undefined;
58
+ readonly "content-type"?: string | undefined;
59
+ readonly "content-length"?: string | undefined;
60
+ readonly "transfer-encoding"?: string | undefined;
61
+ }
62
+
63
+ /** The parts of an incoming request that the checks read. An `IncomingMessage` of `node:http` agrees with it. */
52
64
  export interface GuardedRequest {
53
65
  method?: string | undefined;
54
- headers: IncomingHttpHeaders;
66
+ headers: GuardedHeaders;
55
67
  socket?: { localAddress?: string | undefined } | undefined;
56
68
  }
57
69
 
@@ -121,7 +133,7 @@ function isJsonType(contentType: string | undefined): boolean {
121
133
  }
122
134
 
123
135
  /** True when the request has a body: a `Content-Length` above 0, or a chunked body. */
124
- function hasBody(headers: IncomingHttpHeaders): boolean {
136
+ function hasBody(headers: GuardedHeaders): boolean {
125
137
  const length = Number(headers["content-length"] ?? 0);
126
138
  return length > 0 || headers["transfer-encoding"] !== undefined;
127
139
  }
@@ -130,7 +142,7 @@ function hasBody(headers: IncomingHttpHeaders): boolean {
130
142
  * True when a request carries ambient credentials: a `Cookie` header, which a browser sends by
131
143
  * itself, also for a request that another site starts.
132
144
  */
133
- export function hasAmbientCredentials(headers: IncomingHttpHeaders): boolean {
145
+ export function hasAmbientCredentials(headers: GuardedHeaders): boolean {
134
146
  const cookie = headers.cookie;
135
147
  return typeof cookie === "string" && cookie.trim() !== "";
136
148
  }
@@ -143,7 +155,7 @@ export function hasAmbientCredentials(headers: IncomingHttpHeaders): boolean {
143
155
  *
144
156
  * @internal
145
157
  */
146
- export function needsNamedOrigin(mode: "always" | "cookies" | undefined, headers: IncomingHttpHeaders, cookiesNamed: boolean): boolean {
158
+ export function needsNamedOrigin(mode: "always" | "cookies" | undefined, headers: GuardedHeaders, cookiesNamed: boolean): boolean {
147
159
  return mode === "always" || (hasAmbientCredentials(headers) && (cookiesNamed || mode !== undefined));
148
160
  }
149
161
 
@@ -11,8 +11,8 @@
11
11
  * - Handler registry with pattern matching
12
12
  * - Graceful shutdown
13
13
  *
14
- * The server transports have the entry `@procedures/client/node` (CLI-6). This entry still
15
- * exports them as deprecated aliases. The next MINOR removes the aliases.
14
+ * The server transports have the entry `@procedures/client/node` (CLI-6). This entry imports no
15
+ * Node built-in, no `ws` and no `express`, and its declarations name none of them (CLI-61).
16
16
  *
17
17
  * @example
18
18
  * ```typescript
@@ -38,19 +38,6 @@
38
38
  * ```
39
39
  */
40
40
 
41
- import { HttpServerTransport as NodeHttpServerTransport } from "../adapters/http/server/transport.js";
42
- import type {
43
- HttpServerTransportOptions as NodeHttpServerTransportOptions,
44
- HttpUrlStrategy as NodeHttpUrlStrategy,
45
- } from "../adapters/http/server/types.js";
46
- import { WebSocketServerTransport as NodeWebSocketServerTransport } from "../adapters/websocket/server/transport.js";
47
- import type {
48
- WebSocketServerTransportOptions as NodeWebSocketServerTransportOptions,
49
- WebSocketAuthHandler as NodeWebSocketAuthHandler,
50
- WebSocketConnectionHandler as NodeWebSocketConnectionHandler,
51
- WebSocketMessage as NodeWebSocketMessage,
52
- } from "../adapters/websocket/server/types.js";
53
-
54
41
  export { Server } from "./server.js";
55
42
  export type { ServerOptions } from "./server.js";
56
43
 
@@ -77,78 +64,4 @@ export {
77
64
  } from "./types.js";
78
65
 
79
66
  export { defaultServerUrlStrategy, rpcServerUrlStrategy } from "../adapters/http/server/strategies.js";
80
-
81
- // ============================================================================
82
- // The server transports: deprecated here (CLI-6 step 1)
83
- // ============================================================================
84
- // Their entry is `@procedures/client/node`. This entry and the root (which re-exports these
85
- // names from this module) give them through deprecated aliases until the next MINOR, so an
86
- // editor marks each old import. Each alias is the same class or type as the export of ./node.
87
-
88
- /**
89
- * The HTTP server transport of Express, a shim over `procedureHandler`.
90
- *
91
- * @deprecated Import it from `@procedures/client/node`. The root and `@procedures/client/server` stop exporting it in the next MINOR (CLI-6).
92
- */
93
- export const HttpServerTransport: typeof NodeHttpServerTransport = NodeHttpServerTransport;
94
- /**
95
- * The HTTP server transport of Express, a shim over `procedureHandler`.
96
- *
97
- * @deprecated Import it from `@procedures/client/node`. The root and `@procedures/client/server` stop exporting it in the next MINOR (CLI-6).
98
- */
99
- export type HttpServerTransport = NodeHttpServerTransport;
100
-
101
- /**
102
- * The options of `HttpServerTransport`.
103
- *
104
- * @deprecated Import it from `@procedures/client/node`. The root and `@procedures/client/server` stop exporting it in the next MINOR (CLI-6).
105
- */
106
- export type HttpServerTransportOptions = NodeHttpServerTransportOptions;
107
-
108
- /**
109
- * The URL strategy of `HttpServerTransport`: it gives the method of an Express request.
110
- *
111
- * @deprecated Import it from `@procedures/client/node`. The root and `@procedures/client/server` stop exporting it in the next MINOR (CLI-6).
112
- */
113
- export type HttpUrlStrategy = NodeHttpUrlStrategy;
114
-
115
- /**
116
- * The WebSocket server transport, on the `ws` package.
117
- *
118
- * @deprecated Import it from `@procedures/client/node`. The root and `@procedures/client/server` stop exporting it in the next MINOR (CLI-6).
119
- */
120
- export const WebSocketServerTransport: typeof NodeWebSocketServerTransport = NodeWebSocketServerTransport;
121
- /**
122
- * The WebSocket server transport, on the `ws` package.
123
- *
124
- * @deprecated Import it from `@procedures/client/node`. The root and `@procedures/client/server` stop exporting it in the next MINOR (CLI-6).
125
- */
126
- export type WebSocketServerTransport = NodeWebSocketServerTransport;
127
-
128
- /**
129
- * The options of `WebSocketServerTransport`.
130
- *
131
- * @deprecated Import it from `@procedures/client/node`. The root and `@procedures/client/server` stop exporting it in the next MINOR (CLI-6).
132
- */
133
- export type WebSocketServerTransportOptions = NodeWebSocketServerTransportOptions;
134
-
135
- /**
136
- * The authentication handler of `WebSocketServerTransport`, for each upgrade request.
137
- *
138
- * @deprecated Import it from `@procedures/client/node`. The root and `@procedures/client/server` stop exporting it in the next MINOR (CLI-6).
139
- */
140
- export type WebSocketAuthHandler = NodeWebSocketAuthHandler;
141
-
142
- /**
143
- * The connection handler of `WebSocketServerTransport`, for each new connection.
144
- *
145
- * @deprecated Import it from `@procedures/client/node`. The root and `@procedures/client/server` stop exporting it in the next MINOR (CLI-6).
146
- */
147
- export type WebSocketConnectionHandler = NodeWebSocketConnectionHandler;
148
-
149
- /**
150
- * The RPC message of `WebSocketServerTransport`.
151
- *
152
- * @deprecated Import it from `@procedures/client/node`. The root and `@procedures/client/server` stop exporting it in the next MINOR (CLI-6).
153
- */
154
- export type WebSocketMessage<TReq = unknown> = NodeWebSocketMessage<TReq>;
67
+ export type { RoutedRequest } from "../adapters/http/server/strategies.js";
@@ -8,6 +8,7 @@
8
8
  import { Server, type ServerOptions } from "./server.js";
9
9
  import type { AmbientCredentials, Credentials, Method, Principal, PrincipalOf, RequestIdentity, ServerHandler, ServerRequest, ServerResponse } from "./types.js";
10
10
  import { methodToPath, pathToMethod } from "./method.js";
11
+ import { isLocalCredentials } from "./request-auth.js";
11
12
  import type {
12
13
  AnyProcedure,
13
14
  ProcedurePath,
@@ -135,10 +136,11 @@ export interface ProcedureServerOptions extends ServerOptions {
135
136
  idempotency?: IdempotencyOptions | false | undefined;
136
137
 
137
138
  /**
138
- * The token of the server (roadmap R18). Each request with credentials (a network request)
139
- * must present it as its bearer token, or the server answers `UNAUTHORIZED`. Before, only the
140
- * Express middleware of the peer examined the token, so a `ProcedureServer` with the HTTP or
141
- * WebSocket transport of the core and the token of a host policy accepted each request.
139
+ * The token of the server (roadmap R18). Each request must present it as its bearer token, or
140
+ * the server answers `UNAUTHORIZED`. Only a local request (`LOCAL_CREDENTIALS`) skips it. The
141
+ * server refuses a request without credentials (CLI-1). Before, only the Express middleware of
142
+ * the peer examined the token, so a `ProcedureServer` with the HTTP or WebSocket transport of the
143
+ * core and the token of a host policy accepted each request.
142
144
  */
143
145
  token?: string | undefined;
144
146
 
@@ -154,8 +156,9 @@ export interface ProcedureServerOptions extends ServerOptions {
154
156
  * header, the query) and gives a bearer token, a principal and a narrowing. It runs before the
155
157
  * token check: its bearer token replaces the bearer token of the credentials. Its principal
156
158
  * replaces the principal of the token check. `authenticate` runs after it and can read
157
- * `request.principal`. Undefined or an error refuses the request with `UNAUTHORIZED`. A request
158
- * without credentials (from this process) does not go through the hook.
159
+ * `request.principal`. Undefined or an error refuses the request with `UNAUTHORIZED`. A local
160
+ * request (`LOCAL_CREDENTIALS`) does not go through the hook. A server with a hook refuses a
161
+ * request without credentials, and the hook does not run for it (CLI-1).
159
162
  */
160
163
  principalOf?: PrincipalOf | undefined;
161
164
 
@@ -888,7 +891,9 @@ export class ProcedureServer extends Server implements RepositoryProvider, StopP
888
891
  /**
889
892
  * The token check and the principal of a request (roadmap R18). The request hook reads the
890
893
  * credentials of a network request first: a bearer token, a principal and a narrowing. A
891
- * network request (with credentials) must present the token. The authenticate function gives
894
+ * network request must present the token. Only a local request (`LOCAL_CREDENTIALS`) skips the
895
+ * hook and the token. A request without credentials is not local: a server with a token or a
896
+ * request hook refuses it, and the hook does not run (CLI-1). The authenticate function gives
892
897
  * the principal. The function gives an UNAUTHORIZED response, or undefined when the request
893
898
  * passes.
894
899
  */
@@ -898,7 +903,15 @@ export class ProcedureServer extends Server implements RepositoryProvider, StopP
898
903
  status: { type: "error", code: "UNAUTHORIZED", message, retryable: false },
899
904
  metadata: {},
900
905
  });
901
- const credentials = request.credentials;
906
+ // Only a local request skips the token and the request hook (CLI-1). A face that sets no
907
+ // credentials skips neither: the server refuses it, and the hook does not run. Before, such a
908
+ // request passed as a request of this process.
909
+ if (request.credentials === undefined && (this.token !== undefined || this.principalOf !== undefined)) {
910
+ return unauthorized("Unauthorized: this server authenticates each request, and the request has no credentials");
911
+ }
912
+ // A local face (this process, MCP over stdio, a message port) sets the local marker. The
913
+ // marker key is a symbol, so a request from the network cannot carry it (CLI-1).
914
+ const credentials = isLocalCredentials(request.credentials) ? undefined : request.credentials;
902
915
  let identity: RequestIdentity | undefined;
903
916
  if (this.principalOf && credentials !== undefined) {
904
917
  try {
@@ -975,7 +988,8 @@ export class ProcedureServer extends Server implements RepositoryProvider, StopP
975
988
  * This method tells if the server admits a connection with these credentials, before its first
976
989
  * call. It uses the path of each call: the request hook, the token check and the authenticate
977
990
  * function (R18, CLI-2a). A face of connections refuses the upgrade when it gives false (CLI-7).
978
- * A request hook that accepts a request without credentials (an empty identity) admits it.
991
+ * A request hook that accepts empty credentials (`{}`) with an empty identity admits the
992
+ * connection.
979
993
  */
980
994
  async admits(credentials: Credentials): Promise<boolean> {
981
995
  const request: ServerRequest<unknown> = { id: "connection", method: { service: "", operation: "" }, payload: undefined, metadata: {}, credentials };
@@ -6,7 +6,26 @@
6
6
  * This module imports no Node code: a server in a browser or a worker can use it too.
7
7
  */
8
8
 
9
- import type { Credentials } from "./types.js";
9
+ import { LOCAL_MARKER, type Credentials } from "./types.js";
10
+
11
+ /**
12
+ * The credentials of a local face (CLI-1): a call from this application, not from the network.
13
+ * The server applies no token and no request hook to a request with them.
14
+ *
15
+ * Only these faces set them:
16
+ * - A call in this process: a direct `handle()` of the code of the application.
17
+ * - MCP over stdio: the peer is the parent process that started this process.
18
+ * - A message port (`PortTransport`): only the holder of the other end of the port can send.
19
+ *
20
+ * A network face (HTTP, WebSocket, MCP over Streamable HTTP) never sets them. The marker key is a
21
+ * symbol, so no request from the network can carry it.
22
+ */
23
+ export const LOCAL_CREDENTIALS: Credentials = Object.freeze({ [LOCAL_MARKER]: true as const });
24
+
25
+ /** True when the credentials carry the local marker (CLI-1): the request comes from this application. */
26
+ export function isLocalCredentials(credentials: Credentials | undefined): boolean {
27
+ return credentials?.[LOCAL_MARKER] === true;
28
+ }
10
29
 
11
30
  /** The headers that carry a secret. They do not go into the metadata of a request. */
12
31
  const SECRET_HEADERS: ReadonlySet<string> = new Set(["authorization", "proxy-authorization", "cookie", "x-api-key"]);
@@ -48,11 +48,14 @@ export interface ServerRequest<TReq = unknown> {
48
48
  signal?: AbortSignal;
49
49
 
50
50
  /**
51
- * The credentials that a network transport read from the request (roadmap R18). They have the
52
- * bearer token of an `Authorization` header, or of the `token` query of a WebSocket connection.
53
- * They also have the cookies, the headers and the query of the request. They are not in the
54
- * metadata, so a handler and a nested call do not see them. A request without credentials comes
55
- * from this process (a direct `handle()`, MCP over stdio).
51
+ * The credentials of the request (roadmap R18). A network face reads them from the request: the
52
+ * bearer token of an `Authorization` header or of the `token` query of a WebSocket connection,
53
+ * the cookies, the headers and the query. A local face sets `LOCAL_CREDENTIALS` (CLI-1). They are
54
+ * not in the metadata, so a handler and a nested call do not see them.
55
+ *
56
+ * A server with a token or a request hook (`principalOf`) refuses a request without credentials
57
+ * with `UNAUTHORIZED`, and the hook does not run (CLI-1). Before, such a request skipped the
58
+ * token check and the hook.
56
59
  */
57
60
  credentials?: Credentials | undefined;
58
61
 
@@ -67,11 +70,25 @@ export interface ServerRequest<TReq = unknown> {
67
70
  narrowing?: CapabilityRequest | undefined;
68
71
  }
69
72
 
73
+ /**
74
+ * The key of the local marker of credentials (CLI-1). The key is a symbol. JSON, structured clone,
75
+ * a header, a cookie and a query cannot carry a symbol key. Thus a remote request cannot claim to
76
+ * be local. Use `LOCAL_CREDENTIALS` to set the marker.
77
+ */
78
+ export const LOCAL_MARKER: unique symbol = Symbol.for("@procedures/client.local-credentials");
79
+
70
80
  /**
71
81
  * The credentials of a request. A network transport sets each field that it can read. The request
72
- * hook of the server (`principalOf`) reads them.
82
+ * hook of the server (`principalOf`) reads them. A local face sets only the local marker
83
+ * (`LOCAL_CREDENTIALS`).
73
84
  */
74
85
  export interface Credentials {
86
+ /**
87
+ * The local marker (CLI-1): the request comes from this application, not from the network. The
88
+ * server applies no token and no request hook to a local request. Only a local face sets it: a
89
+ * call in this process, MCP over stdio, or a message port. A network face never sets it.
90
+ */
91
+ readonly [LOCAL_MARKER]?: true | undefined;
75
92
  /** The bearer token that the request presents. */
76
93
  readonly bearer?: string | undefined;
77
94
  /** The cookies of the request, by name (the `Cookie` header of an HTTP request or of an upgrade). */