@orpc/server 2.0.0-beta.3 → 2.0.0-beta.30

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 (56) hide show
  1. package/README.md +71 -101
  2. package/dist/adapters/aws-lambda/index.d.mts +93 -0
  3. package/dist/adapters/aws-lambda/index.d.ts +93 -0
  4. package/dist/adapters/aws-lambda/index.mjs +66 -0
  5. package/dist/adapters/crossws/index.d.mts +17 -8
  6. package/dist/adapters/crossws/index.d.ts +17 -8
  7. package/dist/adapters/crossws/index.mjs +10 -7
  8. package/dist/adapters/fastify/index.d.mts +85 -0
  9. package/dist/adapters/fastify/index.d.ts +85 -0
  10. package/dist/adapters/fastify/index.mjs +66 -0
  11. package/dist/adapters/fetch/index.d.mts +16 -67
  12. package/dist/adapters/fetch/index.d.ts +16 -67
  13. package/dist/adapters/fetch/index.mjs +8 -144
  14. package/dist/adapters/message-port/index.d.mts +19 -16
  15. package/dist/adapters/message-port/index.d.ts +19 -16
  16. package/dist/adapters/message-port/index.mjs +28 -32
  17. package/dist/adapters/node/index.d.mts +15 -49
  18. package/dist/adapters/node/index.d.ts +15 -49
  19. package/dist/adapters/node/index.mjs +9 -120
  20. package/dist/adapters/standard/index.d.mts +6 -6
  21. package/dist/adapters/standard/index.d.ts +6 -6
  22. package/dist/adapters/standard/index.mjs +3 -3
  23. package/dist/adapters/standard-peer/index.d.mts +2 -2
  24. package/dist/adapters/standard-peer/index.d.ts +2 -2
  25. package/dist/adapters/websocket/index.d.mts +42 -26
  26. package/dist/adapters/websocket/index.d.ts +42 -26
  27. package/dist/adapters/websocket/index.mjs +33 -25
  28. package/dist/extensions/callable.d.mts +2 -2
  29. package/dist/extensions/callable.d.ts +2 -2
  30. package/dist/extensions/callable.mjs +2 -2
  31. package/dist/helpers/index.d.mts +23 -6
  32. package/dist/helpers/index.d.ts +23 -6
  33. package/dist/helpers/index.mjs +13 -5
  34. package/dist/index.d.mts +311 -16
  35. package/dist/index.d.ts +311 -16
  36. package/dist/index.mjs +126 -34
  37. package/dist/plugins/index.d.mts +236 -18
  38. package/dist/plugins/index.d.ts +236 -18
  39. package/dist/plugins/index.mjs +474 -15
  40. package/dist/shared/server.BhHrioCw.d.ts +34 -0
  41. package/dist/shared/{server.T9F3bzZx.d.ts → server.C2n16pp0.d.ts} +34 -9
  42. package/dist/shared/server.Cd4Z1hpV.mjs +69 -0
  43. package/dist/shared/{server.CrlKQucM.mjs → server.CkButhNT.mjs} +21 -38
  44. package/dist/shared/{server.B_U9y00a.d.mts → server.CkNnZ5F-.d.mts} +34 -9
  45. package/dist/shared/{server.BL22TloH.d.mts → server.CwrYlF72.d.mts} +31 -41
  46. package/dist/shared/{server.BL22TloH.d.ts → server.CwrYlF72.d.ts} +31 -41
  47. package/dist/shared/server.D0Ipbmdv.d.mts +34 -0
  48. package/dist/shared/{server.GDpX6Df8.mjs → server.D9VprDph.mjs} +82 -49
  49. package/dist/shared/{server.BwHnWUuN.mjs → server.Dh77P3ii.mjs} +17 -10
  50. package/dist/shared/{server.Pa0F03f_.d.ts → server.Dm0os-OP.d.mts} +24 -12
  51. package/dist/shared/{server.EOHJ3NJr.d.ts → server.DrN1Pj1-.d.ts} +3 -3
  52. package/dist/shared/{server.BsNNjG5J.d.mts → server.DtfwuV6U.d.ts} +24 -12
  53. package/dist/shared/{server.BB_Ik9Ph.d.mts → server.JbCIPL4P.d.mts} +3 -3
  54. package/dist/shared/{server.CjOb6ItT.mjs → server.eCTV8Vpp.mjs} +1 -1
  55. package/package.json +46 -12
  56. package/dist/shared/server.D_QauotT.mjs +0 -30
@@ -1,10 +1,19 @@
1
- import { Value, Promisable } from '@orpc/shared';
2
- import { StandardLazyRequest, StandardHeaders } from '@standardserver/core';
3
- import { C as Context } from '../shared/server.BL22TloH.js';
4
- import { e as StandardHandlerPlugin, f as StandardHandlerRoutingInterceptorOptions, a as StandardHandlerOptions } from '../shared/server.EOHJ3NJr.js';
1
+ import { Value, Promisable, ThrowableError } from '@orpc/shared';
2
+ import { StandardLazyRequest, StandardHeaders, StandardMethod } from '@standardserver/core';
3
+ import { Context } from '../index.js';
4
+ import { StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions, StandardHandlerOptions, StandardHandlerInterceptorOptions } from '../adapters/standard/index.js';
5
+ export { R as RequestLimitHandlerPlugin, a as RequestLimitHandlerPluginOptions } from '../shared/server.BhHrioCw.js';
5
6
  import '@orpc/client';
6
7
  import '@orpc/contract';
7
8
 
9
+ /**
10
+ * Content type for batch responses that use the length-prefixed binary framing
11
+ * (streaming mode, and buffered mode when any sub-response contains binary).
12
+ *
13
+ * Decoding is driven by the `standard-server` body hint, not this header,
14
+ * so it only serves to describe the payload to logs, proxies, and dev tools.
15
+ */
16
+ declare const BATCH_CONTENT_TYPE = "application/vnd.orpc.batch";
8
17
  interface BatchHandlerPluginOptions<T extends Context> {
9
18
  /**
10
19
  * The max size of the batch allowed.
@@ -30,7 +39,39 @@ interface BatchHandlerPluginOptions<T extends Context> {
30
39
  * @default {}
31
40
  */
32
41
  headers?: Value<Promisable<StandardHeaders>, [batchOptions: StandardHandlerRoutingInterceptorOptions<T>]>;
42
+ /**
43
+ * Keep-alive settings for streaming batch responses.
44
+ *
45
+ * When enabled, a zero-length length-prefixed frame is sent periodically while the
46
+ * stream is idle (no message sent for `interval` ms). Clients ignore these frames.
47
+ * Only applies to streaming mode.
48
+ *
49
+ * @default { enabled: true, interval: 15000 }
50
+ */
51
+ keepAlive?: undefined | {
52
+ /**
53
+ * If true, a keep-alive frame is sent periodically while the stream is idle.
54
+ *
55
+ * @default true
56
+ */
57
+ enabled: boolean;
58
+ /**
59
+ * Interval (in milliseconds) between keep-alive frames after the last message.
60
+ *
61
+ * @default 15000
62
+ */
63
+ interval?: number;
64
+ };
33
65
  }
66
+ /**
67
+ * Handles batch requests sent by the client Batch Link Plugin, splitting each
68
+ * batch into sub-requests and streaming their responses back together.
69
+ *
70
+ * @remarks
71
+ * **Note**: HTTP/2 and later already multiplex requests over a single connection, which often makes this plugin unnecessary.
72
+ *
73
+ * @see {@link https://orpc.dev/docs/plugins/batch | Batch Plugin}
74
+ */
34
75
  declare class BatchHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
35
76
  name: string;
36
77
  /**
@@ -42,6 +83,8 @@ declare class BatchHandlerPlugin<T extends Context> implements StandardHandlerPl
42
83
  private readonly mapSubrequest;
43
84
  private readonly successStatus;
44
85
  private readonly headers;
86
+ private readonly keepAliveEnabled;
87
+ private readonly keepAliveInterval;
45
88
  constructor(options?: BatchHandlerPluginOptions<T>);
46
89
  init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
47
90
  }
@@ -53,18 +96,18 @@ interface CORSHandlerPluginOptions<T extends Context> {
53
96
  *
54
97
  * @default (origin) => origin
55
98
  */
56
- origin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerRoutingInterceptorOptions<T>]>;
99
+ origin?: Value<string | readonly string[] | null | undefined, [origin: string | undefined, options: StandardHandlerRoutingInterceptorOptions<T>]>;
57
100
  /**
58
101
  * Configures the `Timing-Allow-Origin` header.
59
102
  * Can be a string, an array of allowed origins, or a function that returns the allowed origin(s).
60
103
  *
61
104
  * @default undefined
62
105
  */
63
- timingOrigin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerRoutingInterceptorOptions<T>]>;
106
+ timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string | undefined, options: StandardHandlerRoutingInterceptorOptions<T>]>;
64
107
  /**
65
108
  * Configures the `Access-Control-Allow-Methods` header for preflight requests.
66
109
  *
67
- * @default ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH']
110
+ * @default ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH', 'QUERY']
68
111
  */
69
112
  allowMethods?: readonly string[];
70
113
  /**
@@ -94,9 +137,10 @@ interface CORSHandlerPluginOptions<T extends Context> {
94
137
  exposeHeaders?: readonly string[];
95
138
  }
96
139
  /**
97
- * CORSHandlerPlugin is a plugin for oRPC that allows you to configure CORS for your API.
140
+ * Configures the [CORS Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
141
+ * for your API, including preflight requests.
98
142
  *
99
- * @see {@link https://orpc.dev/docs/plugins/cors CORS Plugin Docs}
143
+ * @see {@link https://orpc.dev/docs/plugins/cors | CORS Handler Plugin}
100
144
  */
101
145
  declare class CORSHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
102
146
  private readonly options;
@@ -112,17 +156,83 @@ declare class CORSHandlerPlugin<T extends Context> implements StandardHandlerPlu
112
156
  }
113
157
 
114
158
  /**
115
- * Adds basic Cross-Site Request Forgery (CSRF) protection to your oRPC application.
116
- * When a request includes cookies, it helps ensure the request originates from JavaScript
117
- * (for example, fetch/XHR) rather than from standard HTML forms or direct browser navigation.
159
+ * Adds Cross-Site Request Forgery (CSRF) protection that makes the safe `GET` method as
160
+ * secure as unsafe ones such as `POST`. It rejects `GET` requests arriving as top-level
161
+ * navigations initiated cross-site or from outside the browser, the only context where
162
+ * another site can make a browser attach `SameSite=Lax` cookies to a safe-method request.
163
+ *
164
+ * @remarks
165
+ * **Note**: Requests browsers send without `SameSite=Lax` cookies, such as cross-site `fetch`
166
+ * and `<img>`, pass through, so procedures stay reachable from other sites. This safeguard
167
+ * requires authentication cookies explicitly marked `SameSite=Lax` or `SameSite=Strict`,
168
+ * since browsers may attach other cookies to the requests that pass.
169
+ *
170
+ * @see {@link https://orpc.dev/docs/plugins/get-method-csrf-protection | GET Method CSRF Protection Plugin}
171
+ */
172
+ declare class GetMethodCsrfProtectionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
173
+ name: string;
174
+ /** Judge the real request, before batch splits it into client-authored sub-requests. */
175
+ after: string[];
176
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
177
+ private isAllowed;
178
+ }
179
+
180
+ interface MethodOverrideHandlerPluginOptions {
181
+ /**
182
+ * The query parameter carrying the override method.
183
+ *
184
+ * @default 'method'
185
+ */
186
+ param?: string;
187
+ /**
188
+ * The methods a POST request may be overridden to.
189
+ *
190
+ * GET and HEAD are excluded by default because they switch input decoding
191
+ * from the request body to the query string and widen the CSRF surface.
192
+ *
193
+ * @default ['PUT', 'PATCH', 'DELETE']
194
+ */
195
+ methods?: readonly StandardMethod[];
196
+ }
197
+ /**
198
+ * Overrides the HTTP method of a POST request based on a query parameter,
199
+ * so HTML forms (which only support GET and POST) can invoke procedures
200
+ * routed as PUT, PATCH, or DELETE.
201
+ *
202
+ * @see {@link https://orpc.dev/docs/plugins/method-override | Method Override Plugin}
203
+ */
204
+ declare class MethodOverrideHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
205
+ name: string;
206
+ /**
207
+ * Should override batch sub-request methods, not the original batch request.
208
+ */
209
+ before: string[];
210
+ private readonly param;
211
+ private readonly methods;
212
+ constructor(options?: MethodOverrideHandlerPluginOptions);
213
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
214
+ }
215
+
216
+ /**
217
+ * Decompresses incoming request bodies based on the Content-Encoding header,
218
+ * supporting gzip, deflate, and deflate-raw.
118
219
  *
119
- * @info This plugin is enabled by default for `RPCHandler` over HTTP.
220
+ * @see {@link https://orpc.dev/docs/plugins/request-compression | Request Compression Plugin}
120
221
  */
121
- declare class CSRFGuardHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
222
+ declare class RequestCompressionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
122
223
  name: string;
224
+ /**
225
+ * Should decompress the original batch request body instead of sub-requests.
226
+ */
227
+ after: string[];
123
228
  init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
124
229
  }
125
230
 
231
+ /**
232
+ * The context shape into which the Request Headers Plugin injects `reqHeaders`.
233
+ *
234
+ * @see {@link https://orpc.dev/docs/plugins/request-headers | Request Headers Plugin}
235
+ */
126
236
  interface RequestHeadersHandlerPluginContext {
127
237
  /**
128
238
  * Request headers as a Headers instance. This is injected by the Request Headers Plugin.
@@ -133,13 +243,55 @@ interface RequestHeadersHandlerPluginContext {
133
243
  * The Request Headers Plugin injects a `reqHeaders` instance into the context,
134
244
  * allowing access to request headers in oRPC.
135
245
  *
136
- * @see {@link https://orpc.dev/docs/plugins/request-headers Request Headers Plugin Docs}
246
+ * @see {@link https://orpc.dev/docs/plugins/request-headers | Request Headers Plugin}
137
247
  */
138
248
  declare class RequestHeadersHandlerPlugin<T extends RequestHeadersHandlerPluginContext> implements StandardHandlerPlugin<T> {
139
249
  name: string;
140
250
  init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
141
251
  }
142
252
 
253
+ interface ResponseCompressionHandlerPluginOptions<_T extends Context> {
254
+ /**
255
+ * The compression schemes to use for response compression.
256
+ * Schemes are prioritized by their order in this array and
257
+ * only applied if the client supports them (via Accept-Encoding).
258
+ *
259
+ * @default ['gzip', 'deflate']
260
+ */
261
+ encodings?: readonly ('gzip' | 'deflate' | 'deflate-raw')[];
262
+ /**
263
+ * The minimum response size in bytes required to trigger compression.
264
+ * Responses smaller than this threshold will not be compressed to avoid overhead.
265
+ * If the response size cannot be determined, compression will still be applied.
266
+ *
267
+ * @default 1024 (1KB)
268
+ */
269
+ threshold?: number;
270
+ }
271
+ /**
272
+ * Compresses response bodies based on the client's Accept-Encoding header.
273
+ * Works at the standard handler level, so it supports all adapters.
274
+ *
275
+ * @see {@link https://orpc.dev/docs/plugins/response-compression | Response Compression Plugin}
276
+ */
277
+ declare class ResponseCompressionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
278
+ name: string;
279
+ /**
280
+ * Compression should be done after batching, to compress the final response.
281
+ * Compression should also be done after response headers are set, to access final headers like Content-Type and Cache-Control.
282
+ */
283
+ after: string[];
284
+ private readonly encodings;
285
+ private readonly threshold;
286
+ constructor(options?: ResponseCompressionHandlerPluginOptions<T>);
287
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
288
+ }
289
+
290
+ /**
291
+ * The context shape into which the Response Headers Plugin injects `resHeaders`.
292
+ *
293
+ * @see {@link https://orpc.dev/docs/plugins/response-headers | Response Headers Plugin}
294
+ */
143
295
  interface ResponseHeadersHandlerPluginContext {
144
296
  /**
145
297
  * Response headers as a Headers instance. This is injected by the Response Headers Plugin.
@@ -151,7 +303,7 @@ interface ResponseHeadersHandlerPluginContext {
151
303
  * The Response Headers Plugin allows you to set response headers in oRPC.
152
304
  * It injects a resHeaders instance into the context, enabling you to modify response headers easily.
153
305
  *
154
- * @see {@link https://orpc.dev/docs/plugins/response-headers Response Headers Plugin Docs}
306
+ * @see {@link https://orpc.dev/docs/plugins/response-headers | Response Headers Plugin}
155
307
  */
156
308
  declare class ResponseHeadersHandlerPlugin<T extends ResponseHeadersHandlerPluginContext> implements StandardHandlerPlugin<T> {
157
309
  name: string;
@@ -162,5 +314,71 @@ declare class ResponseHeadersHandlerPlugin<T extends ResponseHeadersHandlerPlugi
162
314
  init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
163
315
  }
164
316
 
165
- export { BatchHandlerPlugin, CORSHandlerPlugin, CSRFGuardHandlerPlugin, RequestHeadersHandlerPlugin, ResponseHeadersHandlerPlugin };
166
- export type { BatchHandlerPluginOptions, CORSHandlerPluginOptions, RequestHeadersHandlerPluginContext, ResponseHeadersHandlerPluginContext };
317
+ interface RethrowHandlerPluginOptions<T extends Context> {
318
+ /**
319
+ * Decide which errors should be rethrown.
320
+ *
321
+ * @example
322
+ * ```ts
323
+ * const rethrowPlugin = new RethrowHandlerPlugin({
324
+ * filter: (error) => {
325
+ * // Rethrow all non-ORPCError errors
326
+ * return !(error instanceof ORPCError)
327
+ * }
328
+ * })
329
+ * ```
330
+ */
331
+ filter: (error: ThrowableError, options: StandardHandlerInterceptorOptions<T>) => boolean;
332
+ }
333
+ /**
334
+ * The plugin can bypass oRPC's built-in error handling
335
+ * and rethrow matching errors directly to your framework's error handling mechanism
336
+ * (e.g., NestJS exception filters, Express error middleware).
337
+ *
338
+ * @see {@link https://orpc.dev/docs/plugins/rethrow | Rethrow Handler Plugin}
339
+ */
340
+ declare class RethrowHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
341
+ name: string;
342
+ private readonly filter;
343
+ private readonly CONTEXT_SYMBOL;
344
+ constructor(options: RethrowHandlerPluginOptions<T>);
345
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
346
+ }
347
+
348
+ interface TimeoutHandlerPluginOptions<T extends Context> {
349
+ /**
350
+ * Timeout in milliseconds before the request signal is aborted.
351
+ * This only covers producing the response, use `streamingTimeout`
352
+ * to limit streaming response bodies.
353
+ * Use `null` or `undefined` to disable the timeout.
354
+ */
355
+ timeout: Value<number | null | undefined, [options: StandardHandlerInterceptorOptions<T>]>;
356
+ /**
357
+ * Timeout in milliseconds for the full duration of a streaming response body
358
+ * (async iterator object or readable stream), measured from when the response is produced.
359
+ * When exceeded, the request signal is aborted and the body ends
360
+ * once its producer honors the signal.
361
+ * Usually higher than `timeout`.
362
+ * Use `null` or `undefined` to disable the timeout.
363
+ *
364
+ * @default undefined (streaming responses run without limit)
365
+ */
366
+ streamingTimeout?: Value<number | null | undefined, [options: StandardHandlerInterceptorOptions<T>]>;
367
+ }
368
+ /**
369
+ * The Timeout Handler Plugin aborts the request signal with an `AbortError`
370
+ * when handling exceeds a configured timeout. It only aborts the signal,
371
+ * the procedure must honor it to stop early and produce the response.
372
+ *
373
+ * @see {@link https://orpc.dev/docs/plugins/timeout | Timeout Plugin}
374
+ */
375
+ declare class TimeoutHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
376
+ private readonly timeout;
377
+ private readonly streamingTimeout;
378
+ name: string;
379
+ constructor(options: NoInfer<TimeoutHandlerPluginOptions<T>>);
380
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
381
+ }
382
+
383
+ export { BATCH_CONTENT_TYPE, BatchHandlerPlugin, CORSHandlerPlugin, CORSHandlerPlugin as CORSPlugin, GetMethodCsrfProtectionHandlerPlugin, MethodOverrideHandlerPlugin, RequestCompressionHandlerPlugin, RequestHeadersHandlerPlugin, RequestHeadersHandlerPlugin as RequestHeadersPlugin, ResponseCompressionHandlerPlugin, ResponseHeadersHandlerPlugin, ResponseHeadersHandlerPlugin as ResponseHeadersPlugin, RethrowHandlerPlugin, TimeoutHandlerPlugin };
384
+ export type { BatchHandlerPluginOptions, CORSHandlerPluginOptions, MethodOverrideHandlerPluginOptions, RequestHeadersHandlerPluginContext, RequestHeadersHandlerPluginContext as RequestHeadersPluginContext, ResponseCompressionHandlerPluginOptions, ResponseHeadersHandlerPluginContext, ResponseHeadersHandlerPluginContext as ResponseHeadersPluginContext, RethrowHandlerPluginOptions, TimeoutHandlerPluginOptions };