@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.
- package/dist/adapters/aws-lambda/handler.d.ts +17 -15
- package/dist/adapters/aws-lambda/handler.js +23 -23
- package/dist/adapters/aws-lambda/index.d.ts +4 -16
- package/dist/adapters/aws-lambda/index.js +2 -2
- package/dist/adapters/aws-lambda/rpc-handler.d.ts +9 -9
- package/dist/adapters/aws-lambda/rpc-handler.js +20 -17
- package/dist/adapters/crossws/handler.d.ts +44 -42
- package/dist/adapters/crossws/handler.js +53 -60
- package/dist/adapters/crossws/index.d.ts +3 -2
- package/dist/adapters/crossws/index.js +2 -2
- package/dist/adapters/crossws/rpc-handler.d.ts +10 -10
- package/dist/adapters/crossws/rpc-handler.js +17 -14
- package/dist/adapters/fastify/handler.d.ts +17 -15
- package/dist/adapters/fastify/handler.js +20 -20
- package/dist/adapters/fastify/index.d.ts +3 -2
- package/dist/adapters/fastify/index.js +2 -2
- package/dist/adapters/fastify/rpc-handler.d.ts +9 -9
- package/dist/adapters/fastify/rpc-handler.js +16 -13
- package/dist/adapters/fetch/handler.d.ts +19 -17
- package/dist/adapters/fetch/handler.js +22 -22
- package/dist/adapters/fetch/index.d.ts +4 -7
- package/dist/adapters/fetch/index.js +3 -7
- package/dist/adapters/fetch/rpc-handler.d.ts +9 -9
- package/dist/adapters/fetch/rpc-handler.js +18 -15
- package/dist/adapters/message-port/handler.d.ts +58 -60
- package/dist/adapters/message-port/handler.js +72 -86
- package/dist/adapters/message-port/index.d.ts +3 -2
- package/dist/adapters/message-port/index.js +2 -2
- package/dist/adapters/message-port/rpc-handler.d.ts +9 -9
- package/dist/adapters/message-port/rpc-handler.js +17 -14
- package/dist/adapters/node/handler.d.ts +17 -15
- package/dist/adapters/node/handler.js +20 -20
- package/dist/adapters/node/index.d.ts +4 -7
- package/dist/adapters/node/index.js +3 -7
- package/dist/adapters/node/rpc-handler.d.ts +9 -9
- package/dist/adapters/node/rpc-handler.js +17 -14
- package/dist/adapters/standard/codec.d.ts +13 -13
- package/dist/adapters/standard/handler.d.ts +49 -49
- package/dist/adapters/standard/handler.js +205 -205
- package/dist/adapters/standard/index.d.ts +7 -6
- package/dist/adapters/standard/index.js +4 -5
- package/dist/adapters/standard/plugin.d.ts +25 -31
- package/dist/adapters/standard/plugin.js +14 -16
- package/dist/adapters/standard/rpc-handler-codec.d.ts +39 -43
- package/dist/adapters/standard/rpc-handler-codec.js +50 -51
- package/dist/adapters/standard/rpc-matcher.d.ts +40 -40
- package/dist/adapters/standard/rpc-matcher.js +103 -149
- package/dist/adapters/standard/utils.d.ts +8 -8
- package/dist/adapters/standard/utils.js +7 -5
- package/dist/adapters/standard-peer/index.d.ts +2 -1
- package/dist/adapters/standard-peer/index.js +1 -1
- package/dist/adapters/standard-peer/utils.d.ts +6 -4
- package/dist/adapters/standard-peer/utils.js +12 -6
- package/dist/adapters/websocket/handler.d.ts +52 -50
- package/dist/adapters/websocket/handler.js +75 -85
- package/dist/adapters/websocket/index.d.ts +3 -2
- package/dist/adapters/websocket/index.js +2 -2
- package/dist/adapters/websocket/rpc-handler.d.ts +9 -9
- package/dist/adapters/websocket/rpc-handler.js +17 -14
- package/dist/builder-variants.d.ts +195 -195
- package/dist/builder.d.ts +103 -104
- package/dist/builder.js +198 -224
- package/dist/constants.d.ts +1 -1
- package/dist/constants.js +4 -2
- package/dist/context.d.ts +3 -3
- package/dist/extensions/callable.d.ts +11 -11
- package/dist/extensions/callable.js +6 -5
- package/dist/helpers/base64url.d.ts +1 -1
- package/dist/helpers/base64url.js +44 -49
- package/dist/helpers/cookie.d.ts +10 -11
- package/dist/helpers/cookie.js +56 -63
- package/dist/helpers/encryption.d.ts +1 -1
- package/dist/helpers/encryption.js +80 -70
- package/dist/helpers/index.d.ts +5 -4
- package/dist/helpers/index.js +4 -4
- package/dist/helpers/signing.d.ts +1 -1
- package/dist/helpers/signing.js +72 -82
- package/dist/implementer-procedure.d.ts +12 -13
- package/dist/implementer-procedure.js +37 -44
- package/dist/implementer-router.d.ts +30 -36
- package/dist/implementer-router.js +92 -111
- package/dist/implementer.d.ts +8 -8
- package/dist/implementer.js +28 -35
- package/dist/index.d.ts +22 -61
- package/dist/index.js +16 -48
- package/dist/lazy.d.ts +14 -14
- package/dist/lazy.js +19 -30
- package/dist/middleware-decorated.d.ts +8 -9
- package/dist/middleware-decorated.js +66 -70
- package/dist/middleware.d.ts +34 -34
- package/dist/plugins/batch.d.ts +75 -75
- package/dist/plugins/batch.js +345 -379
- package/dist/plugins/cors.d.ts +60 -64
- package/dist/plugins/cors.js +118 -111
- package/dist/plugins/get-method-csrf-protection.d.ts +8 -6
- package/dist/plugins/get-method-csrf-protection.js +47 -63
- package/dist/plugins/head-method.d.ts +11 -9
- package/dist/plugins/head-method.js +97 -94
- package/dist/plugins/index.d.ts +15 -39
- package/dist/plugins/index.js +14 -29
- package/dist/plugins/method-override.d.ts +30 -28
- package/dist/plugins/method-override.js +53 -49
- package/dist/plugins/request-compression.d.ts +11 -9
- package/dist/plugins/request-compression.js +52 -79
- package/dist/plugins/request-headers.d.ts +10 -8
- package/dist/plugins/request-headers.js +27 -29
- package/dist/plugins/request-limit.d.ts +21 -19
- package/dist/plugins/request-limit.js +60 -67
- package/dist/plugins/response-compression.d.ts +42 -39
- package/dist/plugins/response-compression.js +150 -202
- package/dist/plugins/response-headers.d.ts +15 -13
- package/dist/plugins/response-headers.js +43 -46
- package/dist/plugins/rethrow.d.ts +25 -23
- package/dist/plugins/rethrow.js +46 -58
- package/dist/plugins/signal-sync.d.ts +7 -5
- package/dist/plugins/signal-sync.js +71 -86
- package/dist/plugins/timeout.d.ts +29 -27
- package/dist/plugins/timeout.js +61 -58
- package/dist/procedure-client.d.ts +14 -15
- package/dist/procedure-client.js +149 -166
- package/dist/procedure-decorated.d.ts +9 -9
- package/dist/procedure-decorated.js +36 -44
- package/dist/procedure-utils.d.ts +10 -30
- package/dist/procedure-utils.js +39 -40
- package/dist/procedure.d.ts +60 -60
- package/dist/procedure.js +18 -24
- package/dist/router-client.d.ts +9 -11
- package/dist/router-client.js +33 -41
- package/dist/router-hidden.js +10 -12
- package/dist/router-utils.d.ts +13 -30
- package/dist/router-utils.js +146 -191
- package/dist/router.d.ts +12 -26
- package/package.json +10 -10
- package/dist/adapters/standard/codec.js +0 -1
- package/dist/builder-variants.js +0 -1
- package/dist/context.js +0 -1
- package/dist/middleware.js +0 -1
- package/dist/router-hidden.d.ts +0 -5
- package/dist/router.js +0 -1
package/dist/plugins/rethrow.js
CHANGED
|
@@ -1,59 +1,47 @@
|
|
|
1
|
-
import { toArray } from
|
|
1
|
+
import { toArray } from "@orpc/shared";
|
|
2
|
+
// src/plugins/rethrow.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
|
2
|
-
import
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
13
|
+
name: string;
|
|
14
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
15
|
+
}
|
|
@@ -1,91 +1,76 @@
|
|
|
1
|
-
import { AbortError,
|
|
1
|
+
import { AbortError, AsyncIteratorClass, anyAbortSignal, isAsyncIteratorObject, listenAbort, override, runWithSignal, toArray } from "@orpc/shared";
|
|
2
|
+
// src/plugins/signal-sync.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
|
2
|
-
import
|
|
3
|
-
import
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
+
}
|
package/dist/plugins/timeout.js
CHANGED
|
@@ -1,59 +1,62 @@
|
|
|
1
|
-
import { AbortError, anyAbortSignal, isAsyncIteratorObject, override, toArray, value, wrapAsyncIterator, wrapReadableStream
|
|
1
|
+
import { AbortError, anyAbortSignal, isAsyncIteratorObject, override, toArray, value, wrapAsyncIterator, wrapReadableStream } from "@orpc/shared";
|
|
2
|
+
// src/plugins/timeout.ts
|
|
2
3
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
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
|
-
|
|
13
|
-
|
|
11
|
+
path?: string[] | undefined;
|
|
12
|
+
interceptors?: ProcedureClientInterceptor<TInitialContext, TOutputSchema, TErrorMap>[] | undefined;
|
|
14
13
|
} & (object extends TInitialContext ? {
|
|
15
|
-
|
|
14
|
+
context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]> | undefined;
|
|
16
15
|
} : {
|
|
17
|
-
|
|
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>;
|