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

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 (139) hide show
  1. package/dist/adapters/aws-lambda/handler.d.ts +17 -15
  2. package/dist/adapters/aws-lambda/handler.js +23 -23
  3. package/dist/adapters/aws-lambda/index.d.ts +4 -16
  4. package/dist/adapters/aws-lambda/index.js +2 -2
  5. package/dist/adapters/aws-lambda/rpc-handler.d.ts +9 -9
  6. package/dist/adapters/aws-lambda/rpc-handler.js +20 -17
  7. package/dist/adapters/crossws/handler.d.ts +44 -42
  8. package/dist/adapters/crossws/handler.js +53 -60
  9. package/dist/adapters/crossws/index.d.ts +3 -2
  10. package/dist/adapters/crossws/index.js +2 -2
  11. package/dist/adapters/crossws/rpc-handler.d.ts +10 -10
  12. package/dist/adapters/crossws/rpc-handler.js +17 -14
  13. package/dist/adapters/fastify/handler.d.ts +17 -15
  14. package/dist/adapters/fastify/handler.js +20 -20
  15. package/dist/adapters/fastify/index.d.ts +3 -2
  16. package/dist/adapters/fastify/index.js +2 -2
  17. package/dist/adapters/fastify/rpc-handler.d.ts +9 -9
  18. package/dist/adapters/fastify/rpc-handler.js +16 -13
  19. package/dist/adapters/fetch/handler.d.ts +19 -17
  20. package/dist/adapters/fetch/handler.js +22 -22
  21. package/dist/adapters/fetch/index.d.ts +4 -7
  22. package/dist/adapters/fetch/index.js +3 -7
  23. package/dist/adapters/fetch/rpc-handler.d.ts +9 -9
  24. package/dist/adapters/fetch/rpc-handler.js +18 -15
  25. package/dist/adapters/message-port/handler.d.ts +58 -60
  26. package/dist/adapters/message-port/handler.js +72 -86
  27. package/dist/adapters/message-port/index.d.ts +3 -2
  28. package/dist/adapters/message-port/index.js +2 -2
  29. package/dist/adapters/message-port/rpc-handler.d.ts +9 -9
  30. package/dist/adapters/message-port/rpc-handler.js +17 -14
  31. package/dist/adapters/node/handler.d.ts +17 -15
  32. package/dist/adapters/node/handler.js +20 -20
  33. package/dist/adapters/node/index.d.ts +4 -7
  34. package/dist/adapters/node/index.js +3 -7
  35. package/dist/adapters/node/rpc-handler.d.ts +9 -9
  36. package/dist/adapters/node/rpc-handler.js +17 -14
  37. package/dist/adapters/standard/codec.d.ts +13 -13
  38. package/dist/adapters/standard/handler.d.ts +49 -49
  39. package/dist/adapters/standard/handler.js +205 -205
  40. package/dist/adapters/standard/index.d.ts +7 -6
  41. package/dist/adapters/standard/index.js +4 -5
  42. package/dist/adapters/standard/plugin.d.ts +25 -31
  43. package/dist/adapters/standard/plugin.js +14 -16
  44. package/dist/adapters/standard/rpc-handler-codec.d.ts +39 -43
  45. package/dist/adapters/standard/rpc-handler-codec.js +50 -51
  46. package/dist/adapters/standard/rpc-matcher.d.ts +40 -40
  47. package/dist/adapters/standard/rpc-matcher.js +103 -149
  48. package/dist/adapters/standard/utils.d.ts +8 -8
  49. package/dist/adapters/standard/utils.js +7 -5
  50. package/dist/adapters/standard-peer/index.d.ts +2 -1
  51. package/dist/adapters/standard-peer/index.js +1 -1
  52. package/dist/adapters/standard-peer/utils.d.ts +6 -4
  53. package/dist/adapters/standard-peer/utils.js +12 -6
  54. package/dist/adapters/websocket/handler.d.ts +52 -50
  55. package/dist/adapters/websocket/handler.js +75 -85
  56. package/dist/adapters/websocket/index.d.ts +3 -2
  57. package/dist/adapters/websocket/index.js +2 -2
  58. package/dist/adapters/websocket/rpc-handler.d.ts +9 -9
  59. package/dist/adapters/websocket/rpc-handler.js +17 -14
  60. package/dist/builder-variants.d.ts +195 -195
  61. package/dist/builder.d.ts +103 -104
  62. package/dist/builder.js +198 -224
  63. package/dist/constants.d.ts +1 -1
  64. package/dist/constants.js +4 -2
  65. package/dist/context.d.ts +3 -3
  66. package/dist/extensions/callable.d.ts +11 -11
  67. package/dist/extensions/callable.js +6 -5
  68. package/dist/helpers/base64url.d.ts +1 -1
  69. package/dist/helpers/base64url.js +44 -49
  70. package/dist/helpers/cookie.d.ts +10 -11
  71. package/dist/helpers/cookie.js +56 -63
  72. package/dist/helpers/encryption.d.ts +1 -1
  73. package/dist/helpers/encryption.js +80 -70
  74. package/dist/helpers/index.d.ts +5 -4
  75. package/dist/helpers/index.js +4 -4
  76. package/dist/helpers/signing.d.ts +1 -1
  77. package/dist/helpers/signing.js +72 -82
  78. package/dist/implementer-procedure.d.ts +12 -13
  79. package/dist/implementer-procedure.js +37 -44
  80. package/dist/implementer-router.d.ts +30 -36
  81. package/dist/implementer-router.js +92 -111
  82. package/dist/implementer.d.ts +8 -8
  83. package/dist/implementer.js +28 -35
  84. package/dist/index.d.ts +22 -61
  85. package/dist/index.js +16 -48
  86. package/dist/lazy.d.ts +14 -14
  87. package/dist/lazy.js +19 -30
  88. package/dist/middleware-decorated.d.ts +8 -9
  89. package/dist/middleware-decorated.js +66 -70
  90. package/dist/middleware.d.ts +34 -34
  91. package/dist/plugins/batch.d.ts +75 -75
  92. package/dist/plugins/batch.js +345 -379
  93. package/dist/plugins/cors.d.ts +60 -64
  94. package/dist/plugins/cors.js +118 -111
  95. package/dist/plugins/get-method-csrf-protection.d.ts +8 -6
  96. package/dist/plugins/get-method-csrf-protection.js +47 -63
  97. package/dist/plugins/head-method.d.ts +11 -9
  98. package/dist/plugins/head-method.js +97 -94
  99. package/dist/plugins/index.d.ts +15 -39
  100. package/dist/plugins/index.js +14 -29
  101. package/dist/plugins/method-override.d.ts +30 -28
  102. package/dist/plugins/method-override.js +53 -49
  103. package/dist/plugins/request-compression.d.ts +11 -9
  104. package/dist/plugins/request-compression.js +52 -79
  105. package/dist/plugins/request-headers.d.ts +10 -8
  106. package/dist/plugins/request-headers.js +27 -29
  107. package/dist/plugins/request-limit.d.ts +21 -19
  108. package/dist/plugins/request-limit.js +60 -67
  109. package/dist/plugins/response-compression.d.ts +42 -39
  110. package/dist/plugins/response-compression.js +150 -202
  111. package/dist/plugins/response-headers.d.ts +15 -13
  112. package/dist/plugins/response-headers.js +43 -46
  113. package/dist/plugins/rethrow.d.ts +25 -23
  114. package/dist/plugins/rethrow.js +46 -58
  115. package/dist/plugins/signal-sync.d.ts +7 -5
  116. package/dist/plugins/signal-sync.js +71 -86
  117. package/dist/plugins/timeout.d.ts +29 -27
  118. package/dist/plugins/timeout.js +61 -58
  119. package/dist/procedure-client.d.ts +14 -15
  120. package/dist/procedure-client.js +149 -166
  121. package/dist/procedure-decorated.d.ts +9 -9
  122. package/dist/procedure-decorated.js +36 -44
  123. package/dist/procedure-utils.d.ts +10 -30
  124. package/dist/procedure-utils.js +39 -40
  125. package/dist/procedure.d.ts +60 -60
  126. package/dist/procedure.js +18 -24
  127. package/dist/router-client.d.ts +9 -11
  128. package/dist/router-client.js +33 -41
  129. package/dist/router-hidden.js +10 -12
  130. package/dist/router-utils.d.ts +13 -30
  131. package/dist/router-utils.js +146 -191
  132. package/dist/router.d.ts +12 -26
  133. package/package.json +10 -10
  134. package/dist/adapters/standard/codec.js +0 -1
  135. package/dist/builder-variants.js +0 -1
  136. package/dist/context.js +0 -1
  137. package/dist/middleware.js +0 -1
  138. package/dist/router-hidden.d.ts +0 -5
  139. package/dist/router.js +0 -1
@@ -1,59 +1,47 @@
1
- import { toArray } from '@orpc/shared';
1
+ import { toArray } from "@orpc/shared";
2
+ // src/plugins/rethrow.ts
2
3
  /**
3
- * The plugin can bypass oRPC's built-in error handling
4
- * and rethrow matching errors directly to your framework's error handling mechanism
5
- * (e.g., NestJS exception filters, Express error middleware).
6
- *
7
- * @see {@link https://orpc.dev/docs/plugins/rethrow | Rethrow Handler Plugin}
8
- */
9
- export class RethrowHandlerPlugin {
10
- name = '~rethrow';
11
- filter;
12
- CONTEXT_SYMBOL = Symbol('ORPC_RETHROW_HANDLER_PLUGIN_CONTEXT');
13
- constructor(options) {
14
- this.filter = options.filter;
15
- }
16
- init(options) {
17
- const routingInterceptor = async (options) => {
18
- const pluginContext = {};
19
- const result = await options.next({
20
- ...options,
21
- context: {
22
- ...options.context,
23
- [this.CONTEXT_SYMBOL]: pluginContext,
24
- },
25
- });
26
- if (pluginContext.error) {
27
- throw pluginContext.error.value;
28
- }
29
- return result;
30
- };
31
- const interceptor = async (options) => {
32
- const pluginContext = options.context[this.CONTEXT_SYMBOL];
33
- if (!pluginContext) {
34
- throw new TypeError('[RethrowHandlerPlugin] Rethrow handler context has been corrupted or modified by another plugin or interceptor');
35
- }
36
- try {
37
- // await is important here to catch both sync and async errors
38
- return await options.next();
39
- }
40
- catch (error) {
41
- if (this.filter(error, options)) {
42
- pluginContext.error = { value: error };
43
- }
44
- throw error;
45
- }
46
- };
47
- return {
48
- ...options,
49
- routingInterceptors: [
50
- ...toArray(options.routingInterceptors),
51
- routingInterceptor, // rethrow as early as possible and keep it close to `interceptor` to avoid a corrupted or modified context
52
- ],
53
- interceptors: [
54
- interceptor, // catch as many errors as possible
55
- ...toArray(options.interceptors),
56
- ],
57
- };
58
- }
59
- }
4
+ * The plugin can bypass oRPC's built-in error handling
5
+ * and rethrow matching errors directly to your framework's error handling mechanism
6
+ * (e.g., NestJS exception filters, Express error middleware).
7
+ *
8
+ * @see {@link https://orpc.dev/docs/plugins/rethrow | Rethrow Handler Plugin}
9
+ */
10
+ var RethrowHandlerPlugin = class {
11
+ name = "~rethrow";
12
+ filter;
13
+ CONTEXT_SYMBOL = Symbol("ORPC_RETHROW_HANDLER_PLUGIN_CONTEXT");
14
+ constructor(options) {
15
+ this.filter = options.filter;
16
+ }
17
+ init(options) {
18
+ const routingInterceptor = async (options) => {
19
+ const pluginContext = {};
20
+ const result = await options.next({
21
+ ...options,
22
+ context: {
23
+ ...options.context,
24
+ [this.CONTEXT_SYMBOL]: pluginContext
25
+ }
26
+ });
27
+ if (pluginContext.error) throw pluginContext.error.value;
28
+ return result;
29
+ };
30
+ const interceptor = async (options) => {
31
+ const pluginContext = options.context[this.CONTEXT_SYMBOL];
32
+ if (!pluginContext) throw new TypeError("[RethrowHandlerPlugin] Rethrow handler context has been corrupted or modified by another plugin or interceptor");
33
+ try {
34
+ return await options.next();
35
+ } catch (error) {
36
+ if (this.filter(error, options)) pluginContext.error = { value: error };
37
+ throw error;
38
+ }
39
+ };
40
+ return {
41
+ ...options,
42
+ routingInterceptors: [...toArray(options.routingInterceptors), routingInterceptor],
43
+ interceptors: [interceptor, ...toArray(options.interceptors)]
44
+ };
45
+ }
46
+ };
47
+ export { RethrowHandlerPlugin };
@@ -1,5 +1,7 @@
1
- import type { StandardHandlerOptions, StandardHandlerPlugin } from '../adapters/standard/index.ts';
2
- import type { Context } from '../context.ts';
1
+ import "../adapters/standard/index.js";
2
+ import { StandardHandlerPlugin } from "../adapters/standard/plugin.js";
3
+ import { StandardHandlerOptions } from "../adapters/standard/handler.js";
4
+ import { Context } from "../context.js";
3
5
  /**
4
6
  * Keeps the request signal and a streaming output (async iterator object or readable stream) in sync:
5
7
  * cancelling the output aborts the signal with an `AbortError`,
@@ -8,6 +10,6 @@ import type { Context } from '../context.ts';
8
10
  * @see {@link https://orpc.dev/docs/plugins/signal-sync | Signal Sync Plugin}
9
11
  */
10
12
  export declare class SignalSyncHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
11
- name: string;
12
- init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
13
- }
13
+ name: string;
14
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
15
+ }
@@ -1,91 +1,76 @@
1
- import { AbortError, anyAbortSignal, AsyncIteratorClass, isAsyncIteratorObject, override, runWithSignal, toArray, } from '@orpc/shared';
1
+ import { AbortError, AsyncIteratorClass, anyAbortSignal, isAsyncIteratorObject, listenAbort, override, runWithSignal, toArray } from "@orpc/shared";
2
+ // src/plugins/signal-sync.ts
2
3
  /**
3
- * Keeps the request signal and a streaming output (async iterator object or readable stream) in sync:
4
- * cancelling the output aborts the signal with an `AbortError`,
5
- * and aborting the signal ends the output with the abort reason and cancels it.
6
- *
7
- * @see {@link https://orpc.dev/docs/plugins/signal-sync | Signal Sync Plugin}
8
- */
9
- export class SignalSyncHandlerPlugin {
10
- name = '~signal-sync';
11
- init(options) {
12
- // oxlint-disable-next-line unicorn/consistent-function-scoping
13
- const clientInterceptor = async (interceptorOptions) => {
14
- const controller = new AbortController();
15
- const signal = anyAbortSignal([interceptorOptions.signal, controller.signal]);
16
- const cancel = () => controller.abort(new AbortError('The response body was cancelled'));
17
- const output = await interceptorOptions.next({ ...interceptorOptions, signal });
18
- if (isAsyncIteratorObject(output)) {
19
- return override(output, syncAsyncIterator(output, signal, cancel));
20
- }
21
- if (output instanceof ReadableStream) {
22
- return override(output, syncReadableStream(output, signal, cancel));
23
- }
24
- return output;
25
- };
26
- return {
27
- ...options,
28
- clientInterceptors: [clientInterceptor, ...toArray(options.clientInterceptors)],
29
- };
30
- }
31
- }
32
- function listenAbort(signal, onAbort) {
33
- if (signal.aborted) {
34
- onAbort();
35
- }
36
- else {
37
- signal.addEventListener('abort', onAbort, { once: true });
38
- }
39
- return () => signal.removeEventListener('abort', onAbort);
40
- }
4
+ * Keeps the request signal and a streaming output (async iterator object or readable stream) in sync:
5
+ * cancelling the output aborts the signal with an `AbortError`,
6
+ * and aborting the signal ends the output with the abort reason and cancels it.
7
+ *
8
+ * @see {@link https://orpc.dev/docs/plugins/signal-sync | Signal Sync Plugin}
9
+ */
10
+ var SignalSyncHandlerPlugin = class {
11
+ name = "~signal-sync";
12
+ init(options) {
13
+ const clientInterceptor = async (interceptorOptions) => {
14
+ const controller = new AbortController();
15
+ const signal = anyAbortSignal([interceptorOptions.signal, controller.signal]);
16
+ const cancel = () => controller.abort(new AbortError("The response body was cancelled"));
17
+ const output = await interceptorOptions.next({
18
+ ...interceptorOptions,
19
+ signal
20
+ });
21
+ if (isAsyncIteratorObject(output)) return override(output, syncAsyncIterator(output, signal, cancel));
22
+ if (output instanceof ReadableStream) return override(output, syncReadableStream(output, signal, cancel));
23
+ return output;
24
+ };
25
+ return {
26
+ ...options,
27
+ clientInterceptors: [clientInterceptor, ...toArray(options.clientInterceptors)]
28
+ };
29
+ }
30
+ };
41
31
  function syncAsyncIterator(iterator, signal, cancel) {
42
- let cancelling;
43
- const unlisten = listenAbort(signal, () => {
44
- cancelling = (async () => iterator.return?.())();
45
- cancelling.catch(() => { }); // only awaited when the consumer cancels
46
- });
47
- return new AsyncIteratorClass(() => runWithSignal(signal, () => iterator.next()), async ({ kind }) => {
48
- if (kind === 'cancelled') {
49
- cancel();
50
- await cancelling;
51
- }
52
- unlisten();
53
- });
32
+ let cancelling;
33
+ const unlisten = listenAbort(signal, () => {
34
+ cancelling = (async () => iterator.return?.())();
35
+ cancelling.catch(() => {});
36
+ });
37
+ return new AsyncIteratorClass(() => runWithSignal(signal, () => iterator.next()), async ({ kind }) => {
38
+ if (kind === "cancelled") {
39
+ cancel();
40
+ await cancelling;
41
+ }
42
+ unlisten();
43
+ });
54
44
  }
55
45
  function syncReadableStream(stream, signal, cancel) {
56
- const reader = stream.getReader();
57
- let cancelling;
58
- let unlisten;
59
- return new ReadableStream({
60
- start(controller) {
61
- unlisten = listenAbort(signal, () => {
62
- controller.error(signal.reason);
63
- cancelling = reader.cancel(signal.reason);
64
- cancelling.catch(() => { }); // only awaited when the consumer cancels
65
- });
66
- },
67
- async pull(controller) {
68
- try {
69
- const { done, value } = await reader.read();
70
- if (signal.aborted) {
71
- return;
72
- }
73
- if (done) {
74
- unlisten();
75
- controller.close();
76
- }
77
- else {
78
- controller.enqueue(value);
79
- }
80
- }
81
- catch (error) {
82
- unlisten();
83
- throw error;
84
- }
85
- },
86
- async cancel() {
87
- cancel();
88
- await cancelling;
89
- },
90
- }, { highWaterMark: 0 });
46
+ const reader = stream.getReader();
47
+ let cancelling;
48
+ let unlisten;
49
+ return new ReadableStream({
50
+ start(controller) {
51
+ unlisten = listenAbort(signal, (reason) => {
52
+ controller.error(reason);
53
+ cancelling = reader.cancel(reason);
54
+ cancelling.catch(() => {});
55
+ });
56
+ },
57
+ async pull(controller) {
58
+ try {
59
+ const { done, value } = await reader.read();
60
+ if (signal.aborted) return;
61
+ if (done) {
62
+ unlisten();
63
+ controller.close();
64
+ } else controller.enqueue(value);
65
+ } catch (error) {
66
+ unlisten();
67
+ throw error;
68
+ }
69
+ },
70
+ async cancel() {
71
+ cancel();
72
+ await cancelling;
73
+ }
74
+ }, { highWaterMark: 0 });
91
75
  }
76
+ export { SignalSyncHandlerPlugin };
@@ -1,25 +1,27 @@
1
- import type { Value } from '@orpc/shared';
2
- import type { StandardHandlerInterceptorOptions, StandardHandlerOptions, StandardHandlerPlugin } from '../adapters/standard/index.ts';
3
- import type { Context } from '../context.ts';
1
+ import "../adapters/standard/index.js";
2
+ import { StandardHandlerPlugin } from "../adapters/standard/plugin.js";
3
+ import { StandardHandlerInterceptorOptions, StandardHandlerOptions } from "../adapters/standard/handler.js";
4
+ import { Context } from "../context.js";
5
+ import { Value } from "@orpc/shared";
4
6
  export interface TimeoutHandlerPluginOptions<T extends Context> {
5
- /**
6
- * Timeout in milliseconds before the request signal is aborted.
7
- * This only covers producing the response, use `streamingTimeout`
8
- * to limit streaming response bodies.
9
- * Use `null` or `undefined` to disable the timeout.
10
- */
11
- timeout: Value<number | null | undefined, [options: StandardHandlerInterceptorOptions<T>]>;
12
- /**
13
- * Timeout in milliseconds for the full duration of a streaming response body
14
- * (async iterator object or readable stream), measured from when the response is produced.
15
- * When exceeded, the request signal is aborted and the body ends
16
- * once its producer honors the signal.
17
- * Usually higher than `timeout`.
18
- * Use `null` or `undefined` to disable the timeout.
19
- *
20
- * @default undefined (streaming responses run without limit)
21
- */
22
- streamingTimeout?: Value<number | null | undefined, [options: StandardHandlerInterceptorOptions<T>]> | undefined;
7
+ /**
8
+ * Timeout in milliseconds before the request signal is aborted.
9
+ * This only covers producing the response, use `streamingTimeout`
10
+ * to limit streaming response bodies.
11
+ * Use `null` or `undefined` to disable the timeout.
12
+ */
13
+ timeout: Value<number | null | undefined, [options: StandardHandlerInterceptorOptions<T>]>;
14
+ /**
15
+ * Timeout in milliseconds for the full duration of a streaming response body
16
+ * (async iterator object or readable stream), measured from when the response is produced.
17
+ * When exceeded, the request signal is aborted and the body ends
18
+ * once its producer honors the signal.
19
+ * Usually higher than `timeout`.
20
+ * Use `null` or `undefined` to disable the timeout.
21
+ *
22
+ * @default undefined (streaming responses run without limit)
23
+ */
24
+ streamingTimeout?: Value<number | null | undefined, [options: StandardHandlerInterceptorOptions<T>]> | undefined;
23
25
  }
24
26
  /**
25
27
  * The Timeout Handler Plugin aborts the request signal with an `AbortError`
@@ -29,9 +31,9 @@ export interface TimeoutHandlerPluginOptions<T extends Context> {
29
31
  * @see {@link https://orpc.dev/docs/plugins/timeout | Timeout Plugin}
30
32
  */
31
33
  export declare class TimeoutHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
32
- private readonly timeout;
33
- private readonly streamingTimeout;
34
- name: string;
35
- constructor(options: NoInfer<TimeoutHandlerPluginOptions<T>>);
36
- init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
37
- }
34
+ private readonly timeout;
35
+ private readonly streamingTimeout;
36
+ name: string;
37
+ constructor(options: NoInfer<TimeoutHandlerPluginOptions<T>>);
38
+ init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
39
+ }
@@ -1,59 +1,62 @@
1
- import { AbortError, anyAbortSignal, isAsyncIteratorObject, override, toArray, value, wrapAsyncIterator, wrapReadableStream, } from '@orpc/shared';
1
+ import { AbortError, anyAbortSignal, isAsyncIteratorObject, override, toArray, value, wrapAsyncIterator, wrapReadableStream } from "@orpc/shared";
2
+ // src/plugins/timeout.ts
2
3
  /**
3
- * The Timeout Handler Plugin aborts the request signal with an `AbortError`
4
- * when handling exceeds a configured timeout. It only aborts the signal,
5
- * the procedure must honor it to stop early and produce the response.
6
- *
7
- * @see {@link https://orpc.dev/docs/plugins/timeout | Timeout Plugin}
8
- */
9
- export class TimeoutHandlerPlugin {
10
- timeout;
11
- streamingTimeout;
12
- name = '~timeout';
13
- constructor(options) {
14
- this.timeout = options.timeout;
15
- this.streamingTimeout = options.streamingTimeout;
16
- }
17
- init(options) {
18
- const interceptor = async (interceptorOptions) => {
19
- const timeoutMs = value(this.timeout, interceptorOptions);
20
- const streamingTimeoutMs = value(this.streamingTimeout, interceptorOptions);
21
- const hasTimeout = timeoutMs !== null && timeoutMs !== undefined;
22
- const hasStreamingTimeout = streamingTimeoutMs !== null && streamingTimeoutMs !== undefined;
23
- if (!hasTimeout && !hasStreamingTimeout) {
24
- return interceptorOptions.next();
25
- }
26
- const controller = new AbortController();
27
- const abortWithTimeout = (ms) => {
28
- controller.abort(new AbortError(`Request timed out after ${ms}ms`));
29
- };
30
- const timeoutId = hasTimeout ? setTimeout(abortWithTimeout, timeoutMs, timeoutMs) : undefined;
31
- const signal = anyAbortSignal([interceptorOptions.request.signal, controller.signal]);
32
- try {
33
- const response = await interceptorOptions.next({
34
- ...interceptorOptions,
35
- request: { ...interceptorOptions.request, signal },
36
- });
37
- if (hasStreamingTimeout) {
38
- const body = response.body;
39
- const isIterator = isAsyncIteratorObject(body);
40
- if (isIterator || body instanceof ReadableStream) {
41
- const streamingTimeoutId = setTimeout(abortWithTimeout, streamingTimeoutMs, streamingTimeoutMs);
42
- const onFinish = () => clearTimeout(streamingTimeoutId);
43
- return {
44
- ...response,
45
- body: isIterator
46
- ? override(body, wrapAsyncIterator(body, { onFinish }))
47
- : override(body, wrapReadableStream(body, { onFinish })),
48
- };
49
- }
50
- }
51
- return response;
52
- }
53
- finally {
54
- clearTimeout(timeoutId);
55
- }
56
- };
57
- return { ...options, interceptors: [interceptor, ...toArray(options.interceptors)] };
58
- }
59
- }
4
+ * The Timeout Handler Plugin aborts the request signal with an `AbortError`
5
+ * when handling exceeds a configured timeout. It only aborts the signal,
6
+ * the procedure must honor it to stop early and produce the response.
7
+ *
8
+ * @see {@link https://orpc.dev/docs/plugins/timeout | Timeout Plugin}
9
+ */
10
+ var TimeoutHandlerPlugin = class {
11
+ timeout;
12
+ streamingTimeout;
13
+ name = "~timeout";
14
+ constructor(options) {
15
+ this.timeout = options.timeout;
16
+ this.streamingTimeout = options.streamingTimeout;
17
+ }
18
+ init(options) {
19
+ const interceptor = async (interceptorOptions) => {
20
+ const timeoutMs = value(this.timeout, interceptorOptions);
21
+ const streamingTimeoutMs = value(this.streamingTimeout, interceptorOptions);
22
+ const hasTimeout = timeoutMs !== null && timeoutMs !== void 0;
23
+ const hasStreamingTimeout = streamingTimeoutMs !== null && streamingTimeoutMs !== void 0;
24
+ if (!hasTimeout && !hasStreamingTimeout) return interceptorOptions.next();
25
+ const controller = new AbortController();
26
+ const abortWithTimeout = (ms) => {
27
+ controller.abort(new AbortError(`Request timed out after ${ms}ms`));
28
+ };
29
+ const timeoutId = hasTimeout ? setTimeout(abortWithTimeout, timeoutMs, timeoutMs) : void 0;
30
+ const signal = anyAbortSignal([interceptorOptions.request.signal, controller.signal]);
31
+ try {
32
+ const response = await interceptorOptions.next({
33
+ ...interceptorOptions,
34
+ request: {
35
+ ...interceptorOptions.request,
36
+ signal
37
+ }
38
+ });
39
+ if (hasStreamingTimeout) {
40
+ const body = response.body;
41
+ const isIterator = isAsyncIteratorObject(body);
42
+ if (isIterator || body instanceof ReadableStream) {
43
+ const streamingTimeoutId = setTimeout(abortWithTimeout, streamingTimeoutMs, streamingTimeoutMs);
44
+ const onFinish = () => clearTimeout(streamingTimeoutId);
45
+ return {
46
+ ...response,
47
+ body: isIterator ? override(body, wrapAsyncIterator(body, { onFinish })) : override(body, wrapReadableStream(body, { onFinish }))
48
+ };
49
+ }
50
+ }
51
+ return response;
52
+ } finally {
53
+ clearTimeout(timeoutId);
54
+ }
55
+ };
56
+ return {
57
+ ...options,
58
+ interceptors: [interceptor, ...toArray(options.interceptors)]
59
+ };
60
+ }
61
+ };
62
+ export { TimeoutHandlerPlugin };
@@ -1,19 +1,18 @@
1
- import type { Client, ClientContext } from '@orpc/client';
2
- import type { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, ORPCErrorConstructorMap, ORPCErrorFromErrorMap } from '@orpc/contract';
3
- import type { Interceptor, MaybeOptionalOptions, Promisable, PromiseWithError, ThrowableError, Value } from '@orpc/shared';
4
- import type { Context } from './context.ts';
5
- import type { Lazyable } from './lazy.ts';
6
- import type { Procedure, ProcedureHandlerOptions } from './procedure.ts';
7
- export type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError>;
8
- export interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap> extends ProcedureHandlerOptions<TInitialContext, unknown, ORPCErrorConstructorMap<TErrorMap>> {
9
- }
10
- export type ProcedureClientInterceptor<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError>>;
1
+ import { Context } from "./context.js";
2
+ import { Lazyable } from "./lazy.js";
3
+ import { Procedure, ProcedureHandlerOptions } from "./procedure.js";
4
+ import { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, ORPCErrorConstructorMap, ORPCErrorFromErrorMap } from "@orpc/contract";
5
+ import { Interceptor, MaybeOptionalOptions, Promisable, PromiseWithError, ThrowableError as ThrowableError$1, Value } from "@orpc/shared";
6
+ import { Client, ClientContext } from "@orpc/client";
7
+ export type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError$1>;
8
+ export interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap> extends ProcedureHandlerOptions<TInitialContext, unknown, ORPCErrorConstructorMap<TErrorMap>> {}
9
+ export type ProcedureClientInterceptor<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError$1>>;
11
10
  export type ProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TClientContext extends ClientContext> = {
12
- path?: string[] | undefined;
13
- interceptors?: ProcedureClientInterceptor<TInitialContext, TOutputSchema, TErrorMap>[] | undefined;
11
+ path?: string[] | undefined;
12
+ interceptors?: ProcedureClientInterceptor<TInitialContext, TOutputSchema, TErrorMap>[] | undefined;
14
13
  } & (object extends TInitialContext ? {
15
- context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]> | undefined;
14
+ context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]> | undefined;
16
15
  } : {
17
- context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
16
+ context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
18
17
  });
19
- export declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TClientContext extends ClientContext = object>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap>>, ...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
18
+ export declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TClientContext extends ClientContext = object>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap>>, ...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;