@orpc/server 2.0.0-beta.47 → 2.0.0-beta.49

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 (178) hide show
  1. package/README.md +6 -7
  2. package/dist/adapters/aws-lambda/handler.d.ts +25 -0
  3. package/dist/adapters/aws-lambda/handler.js +24 -0
  4. package/dist/adapters/aws-lambda/index.d.ts +10 -45
  5. package/dist/adapters/aws-lambda/index.js +2 -0
  6. package/dist/adapters/aws-lambda/rpc-handler.d.ts +19 -0
  7. package/dist/adapters/aws-lambda/rpc-handler.js +18 -0
  8. package/dist/adapters/crossws/handler.d.ts +45 -0
  9. package/dist/adapters/crossws/handler.js +60 -0
  10. package/dist/adapters/crossws/index.d.ts +2 -63
  11. package/dist/adapters/crossws/index.js +2 -0
  12. package/dist/adapters/crossws/rpc-handler.d.ts +16 -0
  13. package/dist/adapters/crossws/rpc-handler.js +15 -0
  14. package/dist/adapters/fastify/handler.d.ts +21 -0
  15. package/dist/adapters/fastify/handler.js +20 -0
  16. package/dist/adapters/fastify/index.d.ts +2 -42
  17. package/dist/adapters/fastify/index.js +2 -0
  18. package/dist/adapters/fastify/rpc-handler.d.ts +15 -0
  19. package/dist/adapters/fastify/rpc-handler.js +14 -0
  20. package/dist/adapters/fetch/handler.d.ts +23 -0
  21. package/dist/adapters/fetch/handler.js +22 -0
  22. package/dist/adapters/fetch/index.d.ts +5 -45
  23. package/dist/adapters/fetch/index.js +7 -0
  24. package/dist/adapters/fetch/rpc-handler.d.ts +17 -0
  25. package/dist/adapters/fetch/rpc-handler.js +16 -0
  26. package/dist/adapters/message-port/{index.d.mts → handler.d.ts} +17 -30
  27. package/dist/adapters/message-port/handler.js +86 -0
  28. package/dist/adapters/message-port/index.d.ts +2 -77
  29. package/dist/adapters/message-port/index.js +2 -0
  30. package/dist/adapters/message-port/rpc-handler.d.ts +16 -0
  31. package/dist/adapters/message-port/rpc-handler.js +15 -0
  32. package/dist/adapters/node/handler.d.ts +21 -0
  33. package/dist/adapters/node/handler.js +20 -0
  34. package/dist/adapters/node/index.d.ts +5 -42
  35. package/dist/adapters/node/index.js +7 -0
  36. package/dist/adapters/node/rpc-handler.d.ts +16 -0
  37. package/dist/adapters/node/rpc-handler.js +15 -0
  38. package/dist/adapters/standard/codec.d.ts +16 -0
  39. package/dist/adapters/standard/codec.js +1 -0
  40. package/dist/adapters/standard/handler.d.ts +68 -0
  41. package/dist/adapters/standard/handler.js +205 -0
  42. package/dist/adapters/standard/index.d.ts +6 -95
  43. package/dist/adapters/standard/index.js +5 -0
  44. package/dist/adapters/standard/plugin.d.ts +32 -0
  45. package/dist/adapters/standard/plugin.js +16 -0
  46. package/dist/adapters/standard/rpc-handler-codec.d.ts +46 -0
  47. package/dist/adapters/standard/rpc-handler-codec.js +51 -0
  48. package/dist/adapters/standard/rpc-matcher.d.ts +53 -0
  49. package/dist/adapters/standard/rpc-matcher.js +152 -0
  50. package/dist/adapters/standard/utils.d.ts +10 -0
  51. package/dist/adapters/standard/utils.js +6 -0
  52. package/dist/adapters/standard-peer/index.d.ts +1 -11
  53. package/dist/adapters/standard-peer/index.js +1 -0
  54. package/dist/adapters/standard-peer/utils.d.ts +4 -0
  55. package/dist/adapters/standard-peer/utils.js +7 -0
  56. package/dist/adapters/websocket/{index.d.mts → handler.d.ts} +11 -29
  57. package/dist/adapters/websocket/handler.js +85 -0
  58. package/dist/adapters/websocket/index.d.ts +2 -77
  59. package/dist/adapters/websocket/index.js +2 -0
  60. package/dist/adapters/websocket/rpc-handler.d.ts +16 -0
  61. package/dist/adapters/websocket/rpc-handler.js +15 -0
  62. package/dist/builder-variants.d.ts +226 -0
  63. package/dist/builder-variants.js +1 -0
  64. package/dist/builder.d.ts +127 -0
  65. package/dist/builder.js +226 -0
  66. package/dist/constants.d.ts +2 -0
  67. package/dist/constants.js +2 -0
  68. package/dist/context.d.ts +6 -0
  69. package/dist/context.js +1 -0
  70. package/dist/extensions/callable.d.ts +6 -5
  71. package/dist/extensions/callable.js +7 -0
  72. package/dist/helpers/base64url.d.ts +31 -0
  73. package/dist/helpers/base64url.js +53 -0
  74. package/dist/helpers/cookie.d.ts +51 -0
  75. package/dist/helpers/cookie.js +69 -0
  76. package/dist/helpers/encryption.d.ts +28 -0
  77. package/dist/helpers/encryption.js +80 -0
  78. package/dist/helpers/index.d.ts +4 -167
  79. package/dist/helpers/index.js +4 -0
  80. package/dist/helpers/signing.d.ts +50 -0
  81. package/dist/helpers/signing.js +88 -0
  82. package/dist/implementer-procedure.d.ts +16 -0
  83. package/dist/implementer-procedure.js +44 -0
  84. package/dist/implementer-router.d.ts +40 -0
  85. package/dist/implementer-router.js +117 -0
  86. package/dist/implementer.d.ts +16 -0
  87. package/dist/implementer.js +37 -0
  88. package/dist/index.d.ts +45 -460
  89. package/dist/index.js +48 -0
  90. package/dist/lazy.d.ts +20 -0
  91. package/dist/lazy.js +31 -0
  92. package/dist/middleware-decorated.d.ts +10 -0
  93. package/dist/middleware-decorated.js +71 -0
  94. package/dist/middleware.d.ts +54 -0
  95. package/dist/middleware.js +1 -0
  96. package/dist/plugins/batch.d.ts +102 -0
  97. package/dist/plugins/batch.js +405 -0
  98. package/dist/plugins/cors.d.ts +73 -0
  99. package/dist/plugins/cors.js +112 -0
  100. package/dist/plugins/get-method-csrf-protection.d.ts +23 -0
  101. package/dist/plugins/get-method-csrf-protection.js +64 -0
  102. package/dist/plugins/head-method.d.ts +23 -0
  103. package/dist/plugins/head-method.js +104 -0
  104. package/dist/plugins/index.d.ts +29 -422
  105. package/dist/plugins/index.js +29 -0
  106. package/dist/plugins/method-override.d.ts +38 -0
  107. package/dist/plugins/method-override.js +50 -0
  108. package/dist/plugins/request-compression.d.ts +16 -0
  109. package/dist/plugins/request-compression.js +82 -0
  110. package/dist/plugins/request-headers.d.ts +22 -0
  111. package/dist/plugins/request-headers.js +30 -0
  112. package/dist/{shared/server.CgBF3Vj1.d.ts → plugins/request-limit.d.ts} +4 -8
  113. package/dist/plugins/request-limit.js +68 -0
  114. package/dist/plugins/response-compression.d.ts +59 -0
  115. package/dist/plugins/response-compression.js +209 -0
  116. package/dist/plugins/response-headers.d.ts +30 -0
  117. package/dist/plugins/response-headers.js +47 -0
  118. package/dist/plugins/rethrow.d.ts +33 -0
  119. package/dist/plugins/rethrow.js +59 -0
  120. package/dist/plugins/signal-sync.d.ts +13 -0
  121. package/dist/plugins/signal-sync.js +91 -0
  122. package/dist/plugins/timeout.d.ts +37 -0
  123. package/dist/plugins/timeout.js +59 -0
  124. package/dist/procedure-client.d.ts +19 -0
  125. package/dist/procedure-client.js +175 -0
  126. package/dist/procedure-decorated.d.ts +10 -0
  127. package/dist/procedure-decorated.js +44 -0
  128. package/dist/procedure-utils.d.ts +41 -0
  129. package/dist/procedure-utils.js +49 -0
  130. package/dist/procedure.d.ts +72 -0
  131. package/dist/procedure.js +24 -0
  132. package/dist/router-client.d.ts +23 -0
  133. package/dist/router-client.js +43 -0
  134. package/dist/router-hidden.d.ts +5 -0
  135. package/dist/router-hidden.js +14 -0
  136. package/dist/router-utils.d.ts +48 -0
  137. package/dist/router-utils.js +202 -0
  138. package/dist/{shared/server.Dz7j9BEh.d.ts → router.d.ts} +14 -15
  139. package/dist/router.js +1 -0
  140. package/package.json +84 -86
  141. package/dist/adapters/aws-lambda/index.d.mts +0 -51
  142. package/dist/adapters/aws-lambda/index.mjs +0 -39
  143. package/dist/adapters/crossws/index.d.mts +0 -63
  144. package/dist/adapters/crossws/index.mjs +0 -68
  145. package/dist/adapters/fastify/index.d.mts +0 -42
  146. package/dist/adapters/fastify/index.mjs +0 -39
  147. package/dist/adapters/fetch/index.d.mts +0 -47
  148. package/dist/adapters/fetch/index.mjs +0 -42
  149. package/dist/adapters/message-port/index.mjs +0 -87
  150. package/dist/adapters/node/index.d.mts +0 -44
  151. package/dist/adapters/node/index.mjs +0 -41
  152. package/dist/adapters/standard/index.d.mts +0 -95
  153. package/dist/adapters/standard/index.mjs +0 -8
  154. package/dist/adapters/standard-peer/index.d.mts +0 -11
  155. package/dist/adapters/standard-peer/index.mjs +0 -10
  156. package/dist/adapters/websocket/index.mjs +0 -87
  157. package/dist/extensions/callable.d.mts +0 -10
  158. package/dist/extensions/callable.mjs +0 -11
  159. package/dist/helpers/index.d.mts +0 -167
  160. package/dist/helpers/index.mjs +0 -200
  161. package/dist/index.d.mts +0 -476
  162. package/dist/index.mjs +0 -463
  163. package/dist/plugins/index.d.mts +0 -432
  164. package/dist/plugins/index.mjs +0 -965
  165. package/dist/shared/server.1ExWFMze.mjs +0 -69
  166. package/dist/shared/server.9B1jEm5a.d.mts +0 -105
  167. package/dist/shared/server.BB464y-6.mjs +0 -207
  168. package/dist/shared/server.BC5BIu02.d.mts +0 -73
  169. package/dist/shared/server.BSYGskId.d.ts +0 -14
  170. package/dist/shared/server.BvG3boBj.d.mts +0 -14
  171. package/dist/shared/server.CTV_mC2P.d.mts +0 -34
  172. package/dist/shared/server.CYbIpHbH.mjs +0 -239
  173. package/dist/shared/server.Chq5FpXf.mjs +0 -353
  174. package/dist/shared/server.DFPxP2v3.d.mts +0 -166
  175. package/dist/shared/server.DFPxP2v3.d.ts +0 -166
  176. package/dist/shared/server.DOg7ke6y.d.ts +0 -105
  177. package/dist/shared/server.W91HSRkE.mjs +0 -9
  178. package/dist/shared/server.sBQSgqK3.mjs +0 -41
@@ -0,0 +1,10 @@
1
+ import type { Promisable, Value } from '@orpc/shared';
2
+ import type { StandardLazyRequest } from '@standard-server/core';
3
+ import type { Context } from '../../context.ts';
4
+ import type { StandardHandlerHandleOptions } from './handler.ts';
5
+ export type FriendlyStandardHandlerHandleOptions<T extends Context> = Omit<StandardHandlerHandleOptions<T>, 'context'> & (object extends T ? {
6
+ context?: Value<Promisable<T>, [request: StandardLazyRequest]> | undefined;
7
+ } : {
8
+ context: Value<Promisable<T>, [request: StandardLazyRequest]>;
9
+ });
10
+ export declare function resolveFriendlyStandardHandlerHandleOptions<T extends Context>(options: FriendlyStandardHandlerHandleOptions<T>): StandardHandlerHandleOptions<T>;
@@ -0,0 +1,6 @@
1
+ export function resolveFriendlyStandardHandlerHandleOptions(options) {
2
+ return {
3
+ ...options,
4
+ context: options.context ?? {}, // Context only optional if all fields are optional
5
+ };
6
+ }
@@ -1,11 +1 @@
1
- import { StandardLazyRequest, StandardResponse } from '@standard-server/core';
2
- import { Context } from '../../index.js';
3
- import { StandardHandler } from '../standard/index.js';
4
- import { FriendlyStandardHandlerHandleOptions } from '../standard/index.js';
5
- import '@orpc/client';
6
- import '@orpc/contract';
7
- import '@orpc/shared';
8
-
9
- declare function createStandardPeerRequestHandler<T extends Context>(handler: StandardHandler<T>, options: FriendlyStandardHandlerHandleOptions<T>): (request: StandardLazyRequest) => Promise<StandardResponse>;
10
-
11
- export { createStandardPeerRequestHandler };
1
+ export * from './utils.ts';
@@ -0,0 +1 @@
1
+ export * from './utils.js';
@@ -0,0 +1,4 @@
1
+ import type { StandardLazyRequest, StandardResponse } from '@standard-server/core';
2
+ import type { Context } from '../../context.ts';
3
+ import type { FriendlyStandardHandlerHandleOptions, StandardHandler } from '../standard/index.ts';
4
+ export declare function createStandardPeerRequestHandler<T extends Context>(handler: StandardHandler<T>, options: FriendlyStandardHandlerHandleOptions<T>): (request: StandardLazyRequest) => Promise<StandardResponse>;
@@ -0,0 +1,7 @@
1
+ import { resolveFriendlyStandardHandlerHandleOptions } from '../standard/index.js';
2
+ export function createStandardPeerRequestHandler(handler, options) {
3
+ return async (request) => {
4
+ const { response } = await handler.handle(request, resolveFriendlyStandardHandlerHandleOptions(options));
5
+ return response ?? { status: 404, headers: {}, body: 'No procedure matched' };
6
+ };
7
+ }
@@ -1,22 +1,15 @@
1
- import { Arrayable, MaybeOptionalOptions } from '@orpc/shared';
2
- import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standard-server/peer';
3
- import { Context } from '../../index.mjs';
4
- import { StandardHandler, StandardHandlerOptions } from '../standard/index.mjs';
5
- import { FriendlyStandardHandlerHandleOptions } from '../standard/index.mjs';
6
- import { Router } from '../../index.mjs';
7
- import { RPCHandlerCodecOptions } from '../standard/index.mjs';
8
- import '@orpc/client';
9
- import '@orpc/contract';
10
- import '@standard-server/core';
11
-
1
+ import type { Arrayable, MaybeOptionalOptions } from '@orpc/shared';
2
+ import type { DecodePeerMessageOptions, EncodePeerMessageOptions } from '@standard-server/peer';
3
+ import type { Context } from '../../context.ts';
4
+ import type { FriendlyStandardHandlerHandleOptions, StandardHandler } from '../standard/index.ts';
12
5
  /**
13
6
  * Supports standard WebSocket instances, Bun ServerWebSocket, "ws" ServerWebSocket,
14
7
  * Cloudflare WebSocket Hibernation, and similar implementations.
15
8
  */
16
- type WebSocketLike = {
9
+ export type WebSocketLike = {
17
10
  send: (data: string | Uint8Array<ArrayBuffer>) => unknown;
18
11
  };
19
- interface WebSocketHandlerOptions<_T extends Context> {
12
+ export interface WebSocketHandlerOptions<_T extends Context> {
20
13
  /**
21
14
  * Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
22
15
  */
@@ -26,8 +19,11 @@ interface WebSocketHandlerOptions<_T extends Context> {
26
19
  */
27
20
  decodePeerMessage?: DecodePeerMessageOptions | undefined;
28
21
  }
29
- declare class WebSocketHandler<T extends Context> {
22
+ export declare class WebSocketHandler<T extends Context> {
30
23
  private readonly handler;
24
+ /**
25
+ * `null` marks a closed socket, so late messages never create a new peer.
26
+ */
31
27
  private readonly peers;
32
28
  private readonly encodePeerMessageOptions;
33
29
  private readonly decodePeerMessageOptions;
@@ -49,6 +45,7 @@ declare class WebSocketHandler<T extends Context> {
49
45
  }>;
50
46
  /**
51
47
  * Cleans up peer state for a closed WebSocket.
48
+ * Messages passed to `.message()` for this WebSocket afterwards are ignored.
52
49
  *
53
50
  * @param ws The same WebSocket instance passed to `.message()`.
54
51
  */
@@ -60,18 +57,3 @@ declare class WebSocketHandler<T extends Context> {
60
57
  */
61
58
  upgrade(ws: Pick<WebSocket, 'send' | 'addEventListener' | 'removeEventListener'>, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): void;
62
59
  }
63
-
64
- interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, RPCHandlerCodecOptions<T>, WebSocketHandlerOptions<T> {
65
- }
66
- /**
67
- * Serves an oRPC router over the RPC protocol on a standard WebSocket
68
- * connection (Deno, Bun, Cloudflare Durable Objects, and more).
69
- *
70
- * @see {@link https://orpc.dev/docs/adapters/websocket | WebSocket Adapters}
71
- */
72
- declare class RPCHandler<T extends Context> extends WebSocketHandler<T> {
73
- constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
74
- }
75
-
76
- export { RPCHandler, WebSocketHandler };
77
- export type { RPCHandlerOptions, WebSocketHandlerOptions, WebSocketLike };
@@ -0,0 +1,85 @@
1
+ import { loadBytes, resolveMaybeOptionalOptions, sequential, toStringOrBytes } from '@orpc/shared';
2
+ import { decodePeerMessage, encodePeerMessage, isClientPeerSendMessage, ServerPeer, } from '@standard-server/peer';
3
+ import { createStandardPeerRequestHandler } from '../standard-peer/index.js';
4
+ export class WebSocketHandler {
5
+ handler;
6
+ /**
7
+ * `null` marks a closed socket, so late messages never create a new peer.
8
+ */
9
+ peers = new WeakMap();
10
+ encodePeerMessageOptions;
11
+ decodePeerMessageOptions;
12
+ constructor(handler, options = {}) {
13
+ this.handler = handler;
14
+ this.encodePeerMessageOptions = options.encodePeerMessage;
15
+ this.decodePeerMessageOptions = options.decodePeerMessage;
16
+ }
17
+ /**
18
+ * Handles a single WebSocket message.
19
+ *
20
+ * Message order matters. Call this immediately after receiving a message,
21
+ * before any other async work, to preserve ordering.
22
+ *
23
+ * To avoid async Blob reads, configure `ws.binaryType` to return binary data
24
+ * directly (for example, `arraybuffer`).
25
+ *
26
+ * @param ws The WebSocket instance. Use the same instance for all messages.
27
+ * @param data The received message. Binary data should already be loaded (not a `Blob`).
28
+ */
29
+ async message(ws, data, ...rest) {
30
+ /**
31
+ * Message order is important: loading -> decode -> .message.
32
+ * This flow must stay synchronous, or we need to use `sequential` helper
33
+ */
34
+ const encoded = toStringOrBytes(data);
35
+ const result = decodePeerMessage(encoded, this.decodePeerMessageOptions);
36
+ let peer = this.peers.get(ws);
37
+ // Closed (e.g. a Blob finished loading after close): a new peer here would leak and never be aborted.
38
+ if (peer === null) {
39
+ return result;
40
+ }
41
+ if (!peer) {
42
+ this.peers.set(ws, (peer = new ServerPeer(async (message) => {
43
+ const encoded = await encodePeerMessage(message, this.encodePeerMessageOptions);
44
+ await ws.send(encoded);
45
+ })));
46
+ }
47
+ if (result.matched && isClientPeerSendMessage(result.message)) {
48
+ await peer.message(result.message, createStandardPeerRequestHandler(this.handler, resolveMaybeOptionalOptions(rest)));
49
+ }
50
+ return result;
51
+ }
52
+ /**
53
+ * Cleans up peer state for a closed WebSocket.
54
+ * Messages passed to `.message()` for this WebSocket afterwards are ignored.
55
+ *
56
+ * @param ws The same WebSocket instance passed to `.message()`.
57
+ */
58
+ async close(ws) {
59
+ const peer = this.peers.get(ws);
60
+ this.peers.set(ws, null);
61
+ await peer?.close();
62
+ }
63
+ /**
64
+ * Attaches message, close, and error event listeners to a WebSocket.
65
+ *
66
+ * Prefer this over calling `.message()` and `.close()` manually.
67
+ */
68
+ upgrade(ws, ...rest) {
69
+ /**
70
+ * Message order is important: loading -> decode -> .message.
71
+ * This flow must stay synchronous, or we need to use `sequential` helper
72
+ */
73
+ ws.addEventListener('message', sequential(async (event) => {
74
+ // For better compatibility avoid control or depend on websocket.binaryType
75
+ const data = event.data instanceof Blob ? await loadBytes(event.data) : event.data;
76
+ // Not awaited: `this.message` runs business logic that may be slow,
77
+ // and awaiting it would block decoding of subsequent messages.
78
+ void this.message(ws, data, ...rest);
79
+ }));
80
+ ws.addEventListener('close', () => this.close(ws));
81
+ // EventEmitter-based implementations like `ws` throw an unhandled `error` event,
82
+ // so a single malformed frame would crash the process. `close` always follows and handles cleanup.
83
+ ws.addEventListener('error', () => { });
84
+ }
85
+ }
@@ -1,77 +1,2 @@
1
- import { Arrayable, MaybeOptionalOptions } from '@orpc/shared';
2
- import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standard-server/peer';
3
- import { Context } from '../../index.js';
4
- import { StandardHandler, StandardHandlerOptions } from '../standard/index.js';
5
- import { FriendlyStandardHandlerHandleOptions } from '../standard/index.js';
6
- import { Router } from '../../index.js';
7
- import { RPCHandlerCodecOptions } from '../standard/index.js';
8
- import '@orpc/client';
9
- import '@orpc/contract';
10
- import '@standard-server/core';
11
-
12
- /**
13
- * Supports standard WebSocket instances, Bun ServerWebSocket, "ws" ServerWebSocket,
14
- * Cloudflare WebSocket Hibernation, and similar implementations.
15
- */
16
- type WebSocketLike = {
17
- send: (data: string | Uint8Array<ArrayBuffer>) => unknown;
18
- };
19
- interface WebSocketHandlerOptions<_T extends Context> {
20
- /**
21
- * Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
22
- */
23
- encodePeerMessage?: EncodePeerMessageOptions | undefined;
24
- /**
25
- * Options for decoding peer messages. such as `prefix` for distinguishing messages on the same channel..
26
- */
27
- decodePeerMessage?: DecodePeerMessageOptions | undefined;
28
- }
29
- declare class WebSocketHandler<T extends Context> {
30
- private readonly handler;
31
- private readonly peers;
32
- private readonly encodePeerMessageOptions;
33
- private readonly decodePeerMessageOptions;
34
- constructor(handler: StandardHandler<T>, options?: NoInfer<WebSocketHandlerOptions<T>>);
35
- /**
36
- * Handles a single WebSocket message.
37
- *
38
- * Message order matters. Call this immediately after receiving a message,
39
- * before any other async work, to preserve ordering.
40
- *
41
- * To avoid async Blob reads, configure `ws.binaryType` to return binary data
42
- * directly (for example, `arraybuffer`).
43
- *
44
- * @param ws The WebSocket instance. Use the same instance for all messages.
45
- * @param data The received message. Binary data should already be loaded (not a `Blob`).
46
- */
47
- message(ws: WebSocketLike, data: Arrayable<string | ArrayBuffer | Pick<Uint8Array<ArrayBuffer>, 'buffer' | 'byteOffset' | 'byteLength'>>, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): Promise<{
48
- matched: boolean;
49
- }>;
50
- /**
51
- * Cleans up peer state for a closed WebSocket.
52
- *
53
- * @param ws The same WebSocket instance passed to `.message()`.
54
- */
55
- close(ws: WebSocketLike): Promise<void>;
56
- /**
57
- * Attaches message, close, and error event listeners to a WebSocket.
58
- *
59
- * Prefer this over calling `.message()` and `.close()` manually.
60
- */
61
- upgrade(ws: Pick<WebSocket, 'send' | 'addEventListener' | 'removeEventListener'>, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): void;
62
- }
63
-
64
- interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, RPCHandlerCodecOptions<T>, WebSocketHandlerOptions<T> {
65
- }
66
- /**
67
- * Serves an oRPC router over the RPC protocol on a standard WebSocket
68
- * connection (Deno, Bun, Cloudflare Durable Objects, and more).
69
- *
70
- * @see {@link https://orpc.dev/docs/adapters/websocket | WebSocket Adapters}
71
- */
72
- declare class RPCHandler<T extends Context> extends WebSocketHandler<T> {
73
- constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
74
- }
75
-
76
- export { RPCHandler, WebSocketHandler };
77
- export type { RPCHandlerOptions, WebSocketHandlerOptions, WebSocketLike };
1
+ export * from './handler.ts';
2
+ export * from './rpc-handler.ts';
@@ -0,0 +1,2 @@
1
+ export * from './handler.js';
2
+ export * from './rpc-handler.js';
@@ -0,0 +1,16 @@
1
+ import type { Context } from '../../context.ts';
2
+ import type { Router } from '../../router.ts';
3
+ import type { RPCHandlerCodecOptions, StandardHandlerOptions } from '../standard/index.ts';
4
+ import type { WebSocketHandlerOptions } from './handler.ts';
5
+ import { WebSocketHandler } from './handler.ts';
6
+ export interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, RPCHandlerCodecOptions<T>, WebSocketHandlerOptions<T> {
7
+ }
8
+ /**
9
+ * Serves an oRPC router over the RPC protocol on a standard WebSocket
10
+ * connection (Deno, Bun, Cloudflare Durable Objects, and more).
11
+ *
12
+ * @see {@link https://orpc.dev/docs/adapters/websocket | WebSocket Adapters}
13
+ */
14
+ export declare class RPCHandler<T extends Context> extends WebSocketHandler<T> {
15
+ constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
16
+ }
@@ -0,0 +1,15 @@
1
+ import { RPCHandlerCodec, StandardHandler } from '../standard/index.js';
2
+ import { WebSocketHandler } from './handler.js';
3
+ /**
4
+ * Serves an oRPC router over the RPC protocol on a standard WebSocket
5
+ * connection (Deno, Bun, Cloudflare Durable Objects, and more).
6
+ *
7
+ * @see {@link https://orpc.dev/docs/adapters/websocket | WebSocket Adapters}
8
+ */
9
+ export class RPCHandler extends WebSocketHandler {
10
+ constructor(router, options = {}) {
11
+ const codec = new RPCHandlerCodec(router, options);
12
+ const handler = new StandardHandler(codec, options);
13
+ super(handler, options);
14
+ }
15
+ }
@@ -0,0 +1,226 @@
1
+ import type { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, InitialInputSchema, InitialOutputSchema, MergedErrorMap, MergedSchema, MetaPlugin, ORPCErrorConstructorMap, Schema } from '@orpc/contract';
2
+ import type { IntersectPick } from '@orpc/shared';
3
+ import type { BuilderDefinition } from './builder.ts';
4
+ import type { Context, MergedContext, MergedInitialContext } from './context.ts';
5
+ import type { Lazy } from './lazy.ts';
6
+ import type { DecoratedMiddleware } from './middleware-decorated.ts';
7
+ import type { Middleware } from './middleware.ts';
8
+ import type { DecoratedProcedure } from './procedure-decorated.ts';
9
+ import type { ProcedureHandler } from './procedure.ts';
10
+ import type { AugmentedRouterWithMiddlewares } from './router-utils.ts';
11
+ import type { Router } from './router.ts';
12
+ /**
13
+ * The builder variant returned after `.use` is called.
14
+ * `.$context` and `.$config` are no longer available once middleware is applied.
15
+ *
16
+ * @see {@link https://orpc.dev/docs/procedure | Procedure}
17
+ */
18
+ export interface BuilderWithMiddlewares<TInitialContext extends Context, TInjectedContext extends Context, TErrorMap extends ErrorMap> {
19
+ '~orpc': BuilderDefinition<InitialInputSchema, InitialOutputSchema, TErrorMap>;
20
+ /**
21
+ * Applies metadata plugins to procedures built from this builder.
22
+ *
23
+ * @see {@link https://orpc.dev/docs/metadata | Metadata}
24
+ */
25
+ 'meta'(...plugins: MetaPlugin<InitialInputSchema, InitialOutputSchema, TErrorMap>[]): BuilderWithMiddlewares<TInitialContext, TInjectedContext, TErrorMap>;
26
+ /**
27
+ * Defines typesafe errors that procedures built from this builder can throw
28
+ * via the `errors` utility in handlers and middleware.
29
+ *
30
+ * @see {@link https://orpc.dev/docs/error-handling#typesafe-errors | Error Handling - Typesafe Errors}
31
+ */
32
+ 'errors'<T extends ErrorMap>(errors: T): BuilderWithMiddlewares<TInitialContext, TInjectedContext, MergedErrorMap<TErrorMap, T>>;
33
+ /**
34
+ * Applies a middleware that runs before the handler of every procedure
35
+ * built from this builder.
36
+ *
37
+ * @see {@link https://orpc.dev/docs/middleware | Middleware}
38
+ */
39
+ 'use'<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $ErrorMap extends ErrorMap = TErrorMap>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, InferSchemaOutput<InitialInputSchema>, InferSchemaInput<InitialOutputSchema>, $ErrorMap>): BuilderWithMiddlewares<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, MergedErrorMap<$ErrorMap, TErrorMap>>;
40
+ /**
41
+ * Creates a standalone middleware that can be composed and applied to any
42
+ * compatible builder or procedure with `.use`.
43
+ *
44
+ * @see {@link https://orpc.dev/docs/middleware | Middleware}
45
+ */
46
+ 'middleware'<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $Input, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $Output = any>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, $Input, $Output, TErrorMap>): DecoratedMiddleware<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, $Input, $Output, TErrorMap>;
47
+ /**
48
+ * Defines the input schema used to validate and type the procedure input.
49
+ *
50
+ * @see {@link https://orpc.dev/docs/procedure#inputoutput-validation | Procedure - Input/Output Validation}
51
+ */
52
+ 'input'<T extends AnySchema>(schema: T): BuilderWithInput<TInitialContext, TInjectedContext, T, TErrorMap>;
53
+ /**
54
+ * Defines the output schema used to validate and type the procedure output.
55
+ *
56
+ * @see {@link https://orpc.dev/docs/procedure#inputoutput-validation | Procedure - Input/Output Validation}
57
+ */
58
+ 'output'<T extends AnySchema>(schema: T): BuilderWithOutput<TInitialContext, TInjectedContext, T, TErrorMap>;
59
+ /**
60
+ * Defines the function that implements the procedure and completes the
61
+ * chain, returning a callable procedure.
62
+ *
63
+ * @see {@link https://orpc.dev/docs/procedure | Procedure}
64
+ */
65
+ 'handler'<T>(handler: ProcedureHandler<MergedContext<TInitialContext, TInjectedContext>, InferSchemaOutput<InitialInputSchema>, T, ORPCErrorConstructorMap<TErrorMap>>): DecoratedProcedure<TInitialContext, TInjectedContext, InitialInputSchema, Schema<T>, TErrorMap>;
66
+ /**
67
+ * Applies the builder's middleware, errors, and metadata to every procedure
68
+ * in the given router.
69
+ *
70
+ * @see {@link https://orpc.dev/docs/router#extending-router | Router - Extending Router}
71
+ */
72
+ 'router'<T extends Router<MergedContext<TInitialContext, TInjectedContext>>>(router: T): AugmentedRouterWithMiddlewares<T, TInitialContext, TInjectedContext, TErrorMap>;
73
+ /**
74
+ * Like `.router`, but loads the router lazily on first access, which helps
75
+ * reduce startup time for large applications.
76
+ *
77
+ * @see {@link https://orpc.dev/docs/router#lazy-router | Router - Lazy Router}
78
+ */
79
+ 'lazy'<T extends Router<MergedContext<TInitialContext, TInjectedContext>>>(loader: () => Promise<{
80
+ default: T;
81
+ }>): Lazy<AugmentedRouterWithMiddlewares<T, TInitialContext, TInjectedContext, TErrorMap>>;
82
+ }
83
+ /**
84
+ * The builder variant returned after `.input` is called.
85
+ * Only procedure-level methods remain available once an input schema is defined.
86
+ *
87
+ * @see {@link https://orpc.dev/docs/procedure | Procedure}
88
+ */
89
+ export interface BuilderWithInput<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TErrorMap extends ErrorMap> {
90
+ '~orpc': BuilderDefinition<TInputSchema, InitialOutputSchema, TErrorMap>;
91
+ /**
92
+ * Applies metadata plugins to procedures built from this builder.
93
+ *
94
+ * @see {@link https://orpc.dev/docs/metadata | Metadata}
95
+ */
96
+ 'meta'(...plugins: MetaPlugin<TInputSchema, InitialOutputSchema, TErrorMap>[]): BuilderWithInput<TInitialContext, TInjectedContext, TInputSchema, TErrorMap>;
97
+ /**
98
+ * Defines typesafe errors that procedures built from this builder can throw
99
+ * via the `errors` utility in handlers and middleware.
100
+ *
101
+ * @see {@link https://orpc.dev/docs/error-handling#typesafe-errors | Error Handling - Typesafe Errors}
102
+ */
103
+ 'errors'<T extends ErrorMap>(errors: T): BuilderWithInput<TInitialContext, TInjectedContext, TInputSchema, MergedErrorMap<TErrorMap, T>>;
104
+ /**
105
+ * Applies a middleware that runs before the handler and can access the
106
+ * validated input.
107
+ *
108
+ * @see {@link https://orpc.dev/docs/middleware | Middleware}
109
+ */
110
+ 'use'<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $ErrorMap extends ErrorMap = TErrorMap>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, InferSchemaOutput<TInputSchema>, InferSchemaInput<InitialOutputSchema>, $ErrorMap>): BuilderWithInput<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, TInputSchema, MergedErrorMap<$ErrorMap, TErrorMap>>;
111
+ /**
112
+ * Adds an additional input schema, merged with the previously defined one.
113
+ *
114
+ * @see {@link https://orpc.dev/docs/procedure#multiple-schemas | Procedure - Multiple Schemas}
115
+ */
116
+ 'input'<T extends AnySchema>(schema: T): BuilderWithInput<TInitialContext, TInjectedContext, MergedSchema<T, TInputSchema>, TErrorMap>;
117
+ /**
118
+ * Defines the output schema used to validate and type the procedure output.
119
+ *
120
+ * @see {@link https://orpc.dev/docs/procedure#inputoutput-validation | Procedure - Input/Output Validation}
121
+ */
122
+ 'output'<T extends AnySchema>(schema: T): BuilderWithInputOutput<TInitialContext, TInjectedContext, TInputSchema, T, TErrorMap>;
123
+ /**
124
+ * Defines the function that implements the procedure and completes the
125
+ * chain, returning a callable procedure.
126
+ *
127
+ * @see {@link https://orpc.dev/docs/procedure | Procedure}
128
+ */
129
+ 'handler'<T>(handler: ProcedureHandler<MergedContext<TInitialContext, TInjectedContext>, InferSchemaOutput<TInputSchema>, T, ORPCErrorConstructorMap<TErrorMap>>): DecoratedProcedure<TInitialContext, TInjectedContext, TInputSchema, Schema<T>, TErrorMap>;
130
+ }
131
+ /**
132
+ * The builder variant returned after `.output` is called.
133
+ * Only procedure-level methods remain available once an output schema is defined.
134
+ *
135
+ * @see {@link https://orpc.dev/docs/procedure | Procedure}
136
+ */
137
+ export interface BuilderWithOutput<TInitialContext extends Context, TInjectedContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
138
+ '~orpc': BuilderDefinition<InitialInputSchema, TOutputSchema, TErrorMap>;
139
+ /**
140
+ * Applies metadata plugins to procedures built from this builder.
141
+ *
142
+ * @see {@link https://orpc.dev/docs/metadata | Metadata}
143
+ */
144
+ 'meta'(...plugins: MetaPlugin<InitialInputSchema, TOutputSchema, TErrorMap>[]): BuilderWithOutput<TInitialContext, TInjectedContext, TOutputSchema, TErrorMap>;
145
+ /**
146
+ * Defines typesafe errors that procedures built from this builder can throw
147
+ * via the `errors` utility in handlers and middleware.
148
+ *
149
+ * @see {@link https://orpc.dev/docs/error-handling#typesafe-errors | Error Handling - Typesafe Errors}
150
+ */
151
+ 'errors'<T extends ErrorMap>(errors: T): BuilderWithOutput<TInitialContext, TInjectedContext, TOutputSchema, MergedErrorMap<TErrorMap, T>>;
152
+ /**
153
+ * Applies a middleware that runs before the handler and can access the
154
+ * typed output.
155
+ *
156
+ * @see {@link https://orpc.dev/docs/middleware | Middleware}
157
+ */
158
+ 'use'<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $ErrorMap extends ErrorMap = TErrorMap>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, InferSchemaOutput<InitialInputSchema>, InferSchemaInput<TOutputSchema>, $ErrorMap>): BuilderWithOutput<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, TOutputSchema, MergedErrorMap<$ErrorMap, TErrorMap>>;
159
+ /**
160
+ * Defines the input schema used to validate and type the procedure input.
161
+ *
162
+ * @see {@link https://orpc.dev/docs/procedure#inputoutput-validation | Procedure - Input/Output Validation}
163
+ */
164
+ 'input'<T extends AnySchema>(schema: T): BuilderWithInputOutput<TInitialContext, TInjectedContext, T, TOutputSchema, TErrorMap>;
165
+ /**
166
+ * Adds an additional output schema, merged with the previously defined one.
167
+ *
168
+ * @see {@link https://orpc.dev/docs/procedure#multiple-schemas | Procedure - Multiple Schemas}
169
+ */
170
+ 'output'<T extends AnySchema>(schema: T): BuilderWithOutput<TInitialContext, TInjectedContext, MergedSchema<T, TOutputSchema>, TErrorMap>;
171
+ /**
172
+ * Defines the function that implements the procedure and completes the
173
+ * chain, returning a callable procedure.
174
+ *
175
+ * @see {@link https://orpc.dev/docs/procedure | Procedure}
176
+ */
177
+ 'handler'<T extends InferSchemaInput<TOutputSchema>>(handler: ProcedureHandler<MergedContext<TInitialContext, TInjectedContext>, InferSchemaOutput<InitialInputSchema>, T, ORPCErrorConstructorMap<TErrorMap>>): DecoratedProcedure<TInitialContext, TInjectedContext, InitialInputSchema, TOutputSchema, TErrorMap>;
178
+ }
179
+ /**
180
+ * The builder variant returned after both `.input` and `.output` are called.
181
+ * Only procedure-level methods remain available.
182
+ *
183
+ * @see {@link https://orpc.dev/docs/procedure | Procedure}
184
+ */
185
+ export interface BuilderWithInputOutput<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
186
+ '~orpc': BuilderDefinition<TInputSchema, TOutputSchema, TErrorMap>;
187
+ /**
188
+ * Applies metadata plugins to procedures built from this builder.
189
+ *
190
+ * @see {@link https://orpc.dev/docs/metadata | Metadata}
191
+ */
192
+ 'meta'(...plugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[]): BuilderWithInputOutput<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
193
+ /**
194
+ * Defines typesafe errors that procedures built from this builder can throw
195
+ * via the `errors` utility in handlers and middleware.
196
+ *
197
+ * @see {@link https://orpc.dev/docs/error-handling#typesafe-errors | Error Handling - Typesafe Errors}
198
+ */
199
+ 'errors'<T extends ErrorMap>(errors: T): BuilderWithInputOutput<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, T>>;
200
+ /**
201
+ * Applies a middleware that runs before the handler and can access the
202
+ * validated input and typed output.
203
+ *
204
+ * @see {@link https://orpc.dev/docs/middleware | Middleware}
205
+ */
206
+ 'use'<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $ErrorMap extends ErrorMap = TErrorMap>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>, $ErrorMap>): BuilderWithInputOutput<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, TInputSchema, TOutputSchema, MergedErrorMap<$ErrorMap, TErrorMap>>;
207
+ /**
208
+ * Adds an additional input schema, merged with the previously defined one.
209
+ *
210
+ * @see {@link https://orpc.dev/docs/procedure#multiple-schemas | Procedure - Multiple Schemas}
211
+ */
212
+ 'input'<T extends AnySchema>(schema: T): BuilderWithInputOutput<TInitialContext, TInjectedContext, MergedSchema<T, TInputSchema>, TOutputSchema, TErrorMap>;
213
+ /**
214
+ * Adds an additional output schema, merged with the previously defined one.
215
+ *
216
+ * @see {@link https://orpc.dev/docs/procedure#multiple-schemas | Procedure - Multiple Schemas}
217
+ */
218
+ 'output'<T extends AnySchema>(schema: T): BuilderWithInputOutput<TInitialContext, TInjectedContext, TInputSchema, MergedSchema<T, TOutputSchema>, TErrorMap>;
219
+ /**
220
+ * Defines the function that implements the procedure and completes the
221
+ * chain, returning a callable procedure.
222
+ *
223
+ * @see {@link https://orpc.dev/docs/procedure | Procedure}
224
+ */
225
+ 'handler'<T extends InferSchemaInput<TOutputSchema>>(handler: ProcedureHandler<MergedContext<TInitialContext, TInjectedContext>, InferSchemaOutput<TInputSchema>, T, ORPCErrorConstructorMap<TErrorMap>>): DecoratedProcedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
226
+ }
@@ -0,0 +1 @@
1
+ export {};