@orpc/server 2.0.0-beta.48 → 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.
- package/README.md +4 -4
- package/dist/adapters/aws-lambda/handler.d.ts +25 -0
- package/dist/adapters/aws-lambda/handler.js +24 -0
- package/dist/adapters/aws-lambda/index.d.ts +16 -0
- package/dist/adapters/aws-lambda/index.js +2 -0
- package/dist/adapters/aws-lambda/rpc-handler.d.ts +19 -0
- package/dist/adapters/aws-lambda/rpc-handler.js +18 -0
- package/dist/adapters/crossws/handler.d.ts +45 -0
- package/dist/adapters/crossws/handler.js +60 -0
- package/dist/adapters/crossws/index.d.ts +2 -0
- package/dist/adapters/crossws/index.js +2 -0
- package/dist/adapters/crossws/rpc-handler.d.ts +16 -0
- package/dist/adapters/crossws/rpc-handler.js +15 -0
- package/dist/adapters/fastify/handler.d.ts +21 -0
- package/dist/adapters/fastify/handler.js +20 -0
- package/dist/adapters/fastify/index.d.ts +2 -0
- package/dist/adapters/fastify/index.js +2 -0
- package/dist/adapters/fastify/rpc-handler.d.ts +15 -0
- package/dist/adapters/fastify/rpc-handler.js +14 -0
- package/dist/adapters/fetch/handler.d.ts +23 -0
- package/dist/adapters/fetch/handler.js +22 -0
- package/dist/adapters/fetch/index.d.ts +7 -0
- package/dist/adapters/fetch/index.js +7 -0
- package/dist/adapters/fetch/rpc-handler.d.ts +17 -0
- package/dist/adapters/fetch/rpc-handler.js +16 -0
- package/dist/adapters/message-port/handler.d.ts +64 -0
- package/dist/adapters/message-port/handler.js +86 -0
- package/dist/adapters/message-port/index.d.ts +2 -0
- package/dist/adapters/message-port/index.js +2 -0
- package/dist/adapters/message-port/rpc-handler.d.ts +16 -0
- package/dist/adapters/message-port/rpc-handler.js +15 -0
- package/dist/adapters/node/handler.d.ts +21 -0
- package/dist/adapters/node/handler.js +20 -0
- package/dist/adapters/node/index.d.ts +7 -0
- package/dist/adapters/node/index.js +7 -0
- package/dist/adapters/node/rpc-handler.d.ts +16 -0
- package/dist/adapters/node/rpc-handler.js +15 -0
- package/dist/adapters/standard/codec.d.ts +16 -0
- package/dist/adapters/standard/handler.d.ts +68 -0
- package/dist/adapters/standard/handler.js +205 -0
- package/dist/adapters/standard/index.d.ts +6 -0
- package/dist/adapters/standard/index.js +5 -0
- package/dist/adapters/standard/plugin.d.ts +32 -0
- package/dist/adapters/standard/plugin.js +16 -0
- package/dist/adapters/standard/rpc-handler-codec.d.ts +46 -0
- package/dist/adapters/standard/rpc-handler-codec.js +51 -0
- package/dist/adapters/standard/rpc-matcher.d.ts +53 -0
- package/dist/adapters/standard/rpc-matcher.js +152 -0
- package/dist/adapters/standard/utils.d.ts +10 -0
- package/dist/adapters/standard/utils.js +6 -0
- package/dist/adapters/standard-peer/index.d.ts +1 -0
- package/dist/adapters/standard-peer/index.js +1 -0
- package/dist/adapters/standard-peer/{utils.d.mts → utils.d.ts} +3 -3
- package/dist/adapters/standard-peer/utils.js +7 -0
- package/dist/adapters/websocket/handler.d.ts +59 -0
- package/dist/adapters/websocket/handler.js +85 -0
- package/dist/adapters/websocket/index.d.ts +2 -0
- package/dist/adapters/websocket/index.js +2 -0
- package/dist/adapters/websocket/rpc-handler.d.ts +16 -0
- package/dist/adapters/websocket/rpc-handler.js +15 -0
- package/dist/builder-variants.d.ts +226 -0
- package/dist/builder.d.ts +127 -0
- package/dist/builder.js +226 -0
- package/dist/context.d.ts +6 -0
- package/dist/extensions/callable.d.ts +11 -0
- package/dist/extensions/callable.js +7 -0
- package/dist/helpers/base64url.d.ts +31 -0
- package/dist/helpers/base64url.js +53 -0
- package/dist/helpers/cookie.d.ts +51 -0
- package/dist/helpers/cookie.js +69 -0
- package/dist/helpers/encryption.d.ts +28 -0
- package/dist/helpers/encryption.js +80 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.js +4 -0
- package/dist/helpers/signing.d.ts +50 -0
- package/dist/helpers/signing.js +88 -0
- package/dist/implementer-procedure.d.ts +16 -0
- package/dist/implementer-procedure.js +44 -0
- package/dist/implementer-router.d.ts +40 -0
- package/dist/implementer-router.js +117 -0
- package/dist/implementer.d.ts +16 -0
- package/dist/implementer.js +37 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.js +48 -0
- package/dist/lazy.d.ts +20 -0
- package/dist/lazy.js +31 -0
- package/dist/middleware-decorated.d.ts +10 -0
- package/dist/middleware-decorated.js +71 -0
- package/dist/middleware.d.ts +54 -0
- package/dist/plugins/batch.d.ts +102 -0
- package/dist/plugins/batch.js +405 -0
- package/dist/plugins/cors.d.ts +73 -0
- package/dist/plugins/cors.js +112 -0
- package/dist/plugins/get-method-csrf-protection.d.ts +23 -0
- package/dist/plugins/get-method-csrf-protection.js +64 -0
- package/dist/plugins/head-method.d.ts +23 -0
- package/dist/plugins/head-method.js +104 -0
- package/dist/plugins/index.d.ts +39 -0
- package/dist/plugins/index.js +29 -0
- package/dist/plugins/method-override.d.ts +38 -0
- package/dist/plugins/method-override.js +50 -0
- package/dist/plugins/request-compression.d.ts +16 -0
- package/dist/plugins/request-compression.js +82 -0
- package/dist/plugins/request-headers.d.ts +22 -0
- package/dist/plugins/request-headers.js +30 -0
- package/dist/plugins/request-limit.d.ts +30 -0
- package/dist/plugins/request-limit.js +68 -0
- package/dist/plugins/response-compression.d.ts +59 -0
- package/dist/plugins/response-compression.js +209 -0
- package/dist/plugins/response-headers.d.ts +30 -0
- package/dist/plugins/response-headers.js +47 -0
- package/dist/plugins/rethrow.d.ts +33 -0
- package/dist/plugins/rethrow.js +59 -0
- package/dist/plugins/signal-sync.d.ts +13 -0
- package/dist/plugins/signal-sync.js +91 -0
- package/dist/plugins/timeout.d.ts +37 -0
- package/dist/plugins/timeout.js +59 -0
- package/dist/procedure-client.d.ts +19 -0
- package/dist/procedure-client.js +175 -0
- package/dist/procedure-decorated.d.ts +10 -0
- package/dist/procedure-decorated.js +44 -0
- package/dist/procedure-utils.d.ts +41 -0
- package/dist/procedure-utils.js +49 -0
- package/dist/procedure.d.ts +72 -0
- package/dist/procedure.js +24 -0
- package/dist/router-client.d.ts +23 -0
- package/dist/router-client.js +43 -0
- package/dist/{router-hidden.d.mts → router-hidden.d.ts} +3 -3
- package/dist/router-hidden.js +14 -0
- package/dist/router-utils.d.ts +48 -0
- package/dist/router-utils.js +202 -0
- package/dist/router.d.ts +72 -0
- package/package.json +84 -86
- package/dist/adapters/aws-lambda/handler.d.mts +0 -25
- package/dist/adapters/aws-lambda/handler.mjs +0 -21
- package/dist/adapters/aws-lambda/index.d.mts +0 -16
- package/dist/adapters/aws-lambda/index.mjs +0 -2
- package/dist/adapters/aws-lambda/rpc-handler.d.mts +0 -18
- package/dist/adapters/aws-lambda/rpc-handler.mjs +0 -10
- package/dist/adapters/crossws/handler.d.mts +0 -41
- package/dist/adapters/crossws/handler.mjs +0 -38
- package/dist/adapters/crossws/index.d.mts +0 -2
- package/dist/adapters/crossws/index.mjs +0 -2
- package/dist/adapters/crossws/rpc-handler.d.mts +0 -15
- package/dist/adapters/crossws/rpc-handler.mjs +0 -10
- package/dist/adapters/fastify/handler.d.mts +0 -21
- package/dist/adapters/fastify/handler.mjs +0 -20
- package/dist/adapters/fastify/index.d.mts +0 -2
- package/dist/adapters/fastify/index.mjs +0 -2
- package/dist/adapters/fastify/rpc-handler.d.mts +0 -14
- package/dist/adapters/fastify/rpc-handler.mjs +0 -10
- package/dist/adapters/fetch/handler.d.mts +0 -23
- package/dist/adapters/fetch/handler.mjs +0 -22
- package/dist/adapters/fetch/index.d.mts +0 -7
- package/dist/adapters/fetch/index.mjs +0 -5
- package/dist/adapters/fetch/rpc-handler.d.mts +0 -16
- package/dist/adapters/fetch/rpc-handler.mjs +0 -10
- package/dist/adapters/message-port/handler.d.mts +0 -57
- package/dist/adapters/message-port/handler.mjs +0 -62
- package/dist/adapters/message-port/index.d.mts +0 -2
- package/dist/adapters/message-port/index.mjs +0 -2
- package/dist/adapters/message-port/rpc-handler.d.mts +0 -15
- package/dist/adapters/message-port/rpc-handler.mjs +0 -10
- package/dist/adapters/node/handler.d.mts +0 -21
- package/dist/adapters/node/handler.mjs +0 -20
- package/dist/adapters/node/index.d.mts +0 -7
- package/dist/adapters/node/index.mjs +0 -5
- package/dist/adapters/node/rpc-handler.d.mts +0 -15
- package/dist/adapters/node/rpc-handler.mjs +0 -10
- package/dist/adapters/standard/codec.d.mts +0 -16
- package/dist/adapters/standard/handler.d.mts +0 -68
- package/dist/adapters/standard/handler.mjs +0 -185
- package/dist/adapters/standard/index.d.mts +0 -6
- package/dist/adapters/standard/index.mjs +0 -5
- package/dist/adapters/standard/plugin.d.mts +0 -32
- package/dist/adapters/standard/plugin.mjs +0 -16
- package/dist/adapters/standard/rpc-handler-codec.d.mts +0 -42
- package/dist/adapters/standard/rpc-handler-codec.mjs +0 -51
- package/dist/adapters/standard/rpc-matcher.d.mts +0 -53
- package/dist/adapters/standard/rpc-matcher.mjs +0 -140
- package/dist/adapters/standard/utils.d.mts +0 -10
- package/dist/adapters/standard/utils.mjs +0 -6
- package/dist/adapters/standard-peer/index.d.mts +0 -1
- package/dist/adapters/standard-peer/index.mjs +0 -1
- package/dist/adapters/standard-peer/utils.mjs +0 -11
- package/dist/adapters/websocket/handler.d.mts +0 -55
- package/dist/adapters/websocket/handler.mjs +0 -55
- package/dist/adapters/websocket/index.d.mts +0 -2
- package/dist/adapters/websocket/index.mjs +0 -2
- package/dist/adapters/websocket/rpc-handler.d.mts +0 -15
- package/dist/adapters/websocket/rpc-handler.mjs +0 -10
- package/dist/builder-variants.d.mts +0 -267
- package/dist/builder.d.mts +0 -141
- package/dist/builder.mjs +0 -145
- package/dist/context.d.mts +0 -13
- package/dist/extensions/callable.d.mts +0 -17
- package/dist/extensions/callable.mjs +0 -7
- package/dist/helpers/base64url.d.mts +0 -31
- package/dist/helpers/base64url.mjs +0 -25
- package/dist/helpers/cookie.d.mts +0 -50
- package/dist/helpers/cookie.mjs +0 -35
- package/dist/helpers/encryption.d.mts +0 -28
- package/dist/helpers/encryption.mjs +0 -62
- package/dist/helpers/index.d.mts +0 -4
- package/dist/helpers/index.mjs +0 -4
- package/dist/helpers/signing.d.mts +0 -50
- package/dist/helpers/signing.mjs +0 -43
- package/dist/implementer-procedure.d.mts +0 -36
- package/dist/implementer-procedure.mjs +0 -38
- package/dist/implementer-router.d.mts +0 -78
- package/dist/implementer-router.mjs +0 -77
- package/dist/implementer.d.mts +0 -22
- package/dist/implementer.mjs +0 -30
- package/dist/index.d.mts +0 -57
- package/dist/index.mjs +0 -30
- package/dist/lazy.d.mts +0 -20
- package/dist/lazy.mjs +0 -22
- package/dist/middleware-decorated.d.mts +0 -27
- package/dist/middleware-decorated.mjs +0 -65
- package/dist/middleware.d.mts +0 -70
- package/dist/plugins/batch.d.mts +0 -104
- package/dist/plugins/batch.mjs +0 -373
- package/dist/plugins/cors.d.mts +0 -67
- package/dist/plugins/cors.mjs +0 -134
- package/dist/plugins/get-method-csrf-protection.d.mts +0 -23
- package/dist/plugins/get-method-csrf-protection.mjs +0 -45
- package/dist/plugins/head-method.d.mts +0 -23
- package/dist/plugins/head-method.mjs +0 -95
- package/dist/plugins/index.d.mts +0 -39
- package/dist/plugins/index.mjs +0 -23
- package/dist/plugins/method-override.d.mts +0 -38
- package/dist/plugins/method-override.mjs +0 -50
- package/dist/plugins/request-compression.d.mts +0 -16
- package/dist/plugins/request-compression.mjs +0 -67
- package/dist/plugins/request-headers.d.mts +0 -22
- package/dist/plugins/request-headers.mjs +0 -21
- package/dist/plugins/request-limit.d.mts +0 -30
- package/dist/plugins/request-limit.mjs +0 -53
- package/dist/plugins/response-compression.d.mts +0 -59
- package/dist/plugins/response-compression.mjs +0 -172
- package/dist/plugins/response-headers.d.mts +0 -30
- package/dist/plugins/response-headers.mjs +0 -34
- package/dist/plugins/rethrow.d.mts +0 -33
- package/dist/plugins/rethrow.mjs +0 -46
- package/dist/plugins/signal-sync.d.mts +0 -13
- package/dist/plugins/signal-sync.mjs +0 -84
- package/dist/plugins/timeout.d.mts +0 -37
- package/dist/plugins/timeout.mjs +0 -56
- package/dist/procedure-client.d.mts +0 -41
- package/dist/procedure-client.mjs +0 -157
- package/dist/procedure-decorated.d.mts +0 -20
- package/dist/procedure-decorated.mjs +0 -41
- package/dist/procedure-utils.d.mts +0 -52
- package/dist/procedure-utils.mjs +0 -33
- package/dist/procedure.d.mts +0 -93
- package/dist/procedure.mjs +0 -20
- package/dist/router-client.d.mts +0 -27
- package/dist/router-client.mjs +0 -35
- package/dist/router-hidden.mjs +0 -12
- package/dist/router-utils.d.mts +0 -59
- package/dist/router-utils.mjs +0 -191
- package/dist/router.d.mts +0 -61
- /package/dist/adapters/standard/{codec.mjs → codec.js} +0 -0
- /package/dist/{builder-variants.mjs → builder-variants.js} +0 -0
- /package/dist/{constants.d.mts → constants.d.ts} +0 -0
- /package/dist/{constants.mjs → constants.js} +0 -0
- /package/dist/{context.mjs → context.js} +0 -0
- /package/dist/{middleware.mjs → middleware.js} +0 -0
- /package/dist/{router.mjs → router.js} +0 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin } from '../adapters/standard/index.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The context shape into which the Request Headers Plugin injects `reqHeaders`.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://orpc.dev/docs/plugins/request-headers | Request Headers Plugin}
|
|
6
|
+
*/
|
|
7
|
+
export interface RequestHeadersHandlerPluginContext {
|
|
8
|
+
/**
|
|
9
|
+
* Request headers as a Headers instance. This is injected by the Request Headers Plugin.
|
|
10
|
+
*/
|
|
11
|
+
reqHeaders?: Headers | undefined;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The Request Headers Plugin injects a `reqHeaders` instance into the context,
|
|
15
|
+
* allowing access to request headers in oRPC.
|
|
16
|
+
*
|
|
17
|
+
* @see {@link https://orpc.dev/docs/plugins/request-headers | Request Headers Plugin}
|
|
18
|
+
*/
|
|
19
|
+
export declare class RequestHeadersHandlerPlugin<T extends RequestHeadersHandlerPluginContext> implements StandardHandlerPlugin<T> {
|
|
20
|
+
name: string;
|
|
21
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { toArray } from '@orpc/shared';
|
|
2
|
+
import { toFetchHeaders } from '@standard-server/fetch';
|
|
3
|
+
/**
|
|
4
|
+
* The Request Headers Plugin injects a `reqHeaders` instance into the context,
|
|
5
|
+
* allowing access to request headers in oRPC.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://orpc.dev/docs/plugins/request-headers | Request Headers Plugin}
|
|
8
|
+
*/
|
|
9
|
+
export class RequestHeadersHandlerPlugin {
|
|
10
|
+
name = '~request-headers';
|
|
11
|
+
init(options) {
|
|
12
|
+
return {
|
|
13
|
+
...options,
|
|
14
|
+
interceptors: [
|
|
15
|
+
(interceptorOptions) => {
|
|
16
|
+
const reqHeaders = interceptorOptions.context.reqHeaders ??
|
|
17
|
+
toFetchHeaders(interceptorOptions.request.headers);
|
|
18
|
+
return interceptorOptions.next({
|
|
19
|
+
...interceptorOptions,
|
|
20
|
+
context: {
|
|
21
|
+
...interceptorOptions.context,
|
|
22
|
+
reqHeaders,
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
},
|
|
26
|
+
...toArray(options.interceptors),
|
|
27
|
+
],
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin } from '../adapters/standard/index.ts';
|
|
2
|
+
import type { Context } from '../context.ts';
|
|
3
|
+
export interface RequestLimitHandlerPluginOptions {
|
|
4
|
+
/**
|
|
5
|
+
* The maximum allowed request body size in bytes.
|
|
6
|
+
*/
|
|
7
|
+
maxBodySize: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Rejects requests whose body exceeds `maxBodySize`.
|
|
11
|
+
*
|
|
12
|
+
* When used with the request compression plugin, the limit applies to the
|
|
13
|
+
* decompressed payload rather than the compressed wire size.
|
|
14
|
+
*
|
|
15
|
+
* @see {@link https://orpc.dev/docs/plugins/request-limit | Request Limit Plugin}
|
|
16
|
+
*/
|
|
17
|
+
export declare class RequestLimitHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
18
|
+
name: string;
|
|
19
|
+
/**
|
|
20
|
+
* Should limit the original batch request body instead of sub-requests.
|
|
21
|
+
*/
|
|
22
|
+
after: string[];
|
|
23
|
+
/**
|
|
24
|
+
* Should limit the final body size instead of the compressed one.
|
|
25
|
+
*/
|
|
26
|
+
before: string[];
|
|
27
|
+
private readonly maxBodySize;
|
|
28
|
+
constructor(options: RequestLimitHandlerPluginOptions);
|
|
29
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ORPCError } from '@orpc/client';
|
|
2
|
+
import { toArray } from '@orpc/shared';
|
|
3
|
+
import { flattenStandardHeader } from '@standard-server/core';
|
|
4
|
+
import { toFetchHeaders, toStandardBody } from '@standard-server/fetch';
|
|
5
|
+
/**
|
|
6
|
+
* Rejects requests whose body exceeds `maxBodySize`.
|
|
7
|
+
*
|
|
8
|
+
* When used with the request compression plugin, the limit applies to the
|
|
9
|
+
* decompressed payload rather than the compressed wire size.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://orpc.dev/docs/plugins/request-limit | Request Limit Plugin}
|
|
12
|
+
*/
|
|
13
|
+
export class RequestLimitHandlerPlugin {
|
|
14
|
+
name = '~request-limit';
|
|
15
|
+
/**
|
|
16
|
+
* Should limit the original batch request body instead of sub-requests.
|
|
17
|
+
*/
|
|
18
|
+
after = ['~batch'];
|
|
19
|
+
/**
|
|
20
|
+
* Should limit the final body size instead of the compressed one.
|
|
21
|
+
*/
|
|
22
|
+
before = ['~request-compression'];
|
|
23
|
+
maxBodySize;
|
|
24
|
+
constructor(options) {
|
|
25
|
+
this.maxBodySize = options.maxBodySize;
|
|
26
|
+
}
|
|
27
|
+
init(options) {
|
|
28
|
+
const maxBodySize = this.maxBodySize;
|
|
29
|
+
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
30
|
+
return next({
|
|
31
|
+
...interceptorOptions,
|
|
32
|
+
request: {
|
|
33
|
+
...interceptorOptions.request,
|
|
34
|
+
async resolveBody(hint) {
|
|
35
|
+
const contentLength = Number(flattenStandardHeader(interceptorOptions.request.headers['content-length']));
|
|
36
|
+
if (Number.isFinite(contentLength) && contentLength > maxBodySize) {
|
|
37
|
+
throw new ORPCError('PAYLOAD_TOO_LARGE');
|
|
38
|
+
}
|
|
39
|
+
const stream = await interceptorOptions.request.resolveBody('octet-stream');
|
|
40
|
+
// adapter might not support hint (e.g. peer adapter)
|
|
41
|
+
if (!(stream instanceof ReadableStream)) {
|
|
42
|
+
return stream;
|
|
43
|
+
}
|
|
44
|
+
let currentBodySize = 0;
|
|
45
|
+
const limitedStream = stream.pipeThrough(new TransformStream({
|
|
46
|
+
transform(chunk, controller) {
|
|
47
|
+
currentBodySize += chunk.byteLength;
|
|
48
|
+
if (currentBodySize > maxBodySize) {
|
|
49
|
+
controller.error(new ORPCError('PAYLOAD_TOO_LARGE'));
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
controller.enqueue(chunk);
|
|
53
|
+
},
|
|
54
|
+
}));
|
|
55
|
+
const response = new Response(limitedStream, {
|
|
56
|
+
headers: toFetchHeaders(interceptorOptions.request.headers),
|
|
57
|
+
});
|
|
58
|
+
return toStandardBody(response, { hint });
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
...options,
|
|
65
|
+
routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { StandardHeaders, StandardResponse } from '@standard-server/core';
|
|
2
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin, StandardHandlerRoutingInterceptorOptions } from '../adapters/standard/index.ts';
|
|
3
|
+
import type { Context } from '../context.ts';
|
|
4
|
+
export interface ResponseCompressionHandlerPluginOptions<T extends Context> {
|
|
5
|
+
/**
|
|
6
|
+
* The compression schemes to use for response compression.
|
|
7
|
+
* Schemes are prioritized by their order in this array and
|
|
8
|
+
* only applied if the client supports them (via Accept-Encoding).
|
|
9
|
+
*
|
|
10
|
+
* @default ['gzip', 'deflate']
|
|
11
|
+
*/
|
|
12
|
+
encodings?: readonly ('gzip' | 'deflate' | 'deflate-raw')[] | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* The minimum response size in bytes required to trigger compression.
|
|
15
|
+
* Responses smaller than this threshold will not be compressed to avoid overhead.
|
|
16
|
+
* If the response size cannot be determined, compression will still be applied.
|
|
17
|
+
*
|
|
18
|
+
* @default 1024 (1KB)
|
|
19
|
+
*/
|
|
20
|
+
threshold?: number | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Determines whether a response with the given Content-Type should be compressed.
|
|
23
|
+
* Only consulted for binary transfers (streams, files, and file form-data parts).
|
|
24
|
+
* Also receives the routing interceptor options for per-request decisions.
|
|
25
|
+
*
|
|
26
|
+
* @default isCompressibleContentType (covers common text-based formats)
|
|
27
|
+
*/
|
|
28
|
+
isCompressibleContentType?: ((contentType: string | null | undefined, options: StandardHandlerRoutingInterceptorOptions<T>) => boolean) | undefined;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Compresses response bodies based on the client's Accept-Encoding header.
|
|
32
|
+
* Works at the standard handler level, so it supports all adapters.
|
|
33
|
+
*
|
|
34
|
+
* @see {@link https://orpc.dev/docs/plugins/response-compression | Response Compression Plugin}
|
|
35
|
+
*/
|
|
36
|
+
export declare class ResponseCompressionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
37
|
+
name: string;
|
|
38
|
+
/**
|
|
39
|
+
* Compression should be done after batching, to compress the final response.
|
|
40
|
+
* Compression should also be done after response headers are set, to access final headers like Content-Type and Cache-Control.
|
|
41
|
+
*/
|
|
42
|
+
after: string[];
|
|
43
|
+
private readonly encodings;
|
|
44
|
+
private readonly threshold;
|
|
45
|
+
private readonly isCompressibleContentType;
|
|
46
|
+
constructor(options?: ResponseCompressionHandlerPluginOptions<T>);
|
|
47
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Picks the first of `encodings` the client accepts, or `undefined` when the response
|
|
51
|
+
* must be sent as is because it is already encoded, partial, or forbids transforms.
|
|
52
|
+
* Shared by the response compression plugins so they agree on when to compress.
|
|
53
|
+
*/
|
|
54
|
+
export declare function negotiateResponseCompressionEncoding<TEncoding extends string>(response: StandardResponse, requestHeaders: StandardHeaders, encodings: readonly TEncoding[]): TEncoding | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* The headers of a response whose body is compressed with `encoding`.
|
|
57
|
+
* Shared by the response compression plugins so compressed responses carry the same headers.
|
|
58
|
+
*/
|
|
59
|
+
export declare function toCompressedHeaders(headers: StandardHeaders, encoding: string): StandardHeaders;
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { isAcceptableEncoding, isAsyncIteratorObject, isCompressibleContentType, isNoTransformCacheControl, parseAcceptEncodingQualities, toArray, varyByAcceptEncoding, } from '@orpc/shared';
|
|
2
|
+
import { flattenStandardHeader, generateContentDisposition } from '@standard-server/core';
|
|
3
|
+
import { toFetchBody } from '@standard-server/fetch';
|
|
4
|
+
// Rough UTF-8 estimate. Mostly ASCII text stays close to 1 byte/char;
|
|
5
|
+
// occasional multi-byte characters increase the average.
|
|
6
|
+
const AVG_BYTES_PER_CHAR = 1.2;
|
|
7
|
+
/**
|
|
8
|
+
* Compresses response bodies based on the client's Accept-Encoding header.
|
|
9
|
+
* Works at the standard handler level, so it supports all adapters.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://orpc.dev/docs/plugins/response-compression | Response Compression Plugin}
|
|
12
|
+
*/
|
|
13
|
+
export class ResponseCompressionHandlerPlugin {
|
|
14
|
+
name = '~response-compression';
|
|
15
|
+
/**
|
|
16
|
+
* Compression should be done after batching, to compress the final response.
|
|
17
|
+
* Compression should also be done after response headers are set, to access final headers like Content-Type and Cache-Control.
|
|
18
|
+
*/
|
|
19
|
+
after = ['~batch', '~response-headers'];
|
|
20
|
+
encodings;
|
|
21
|
+
threshold;
|
|
22
|
+
isCompressibleContentType;
|
|
23
|
+
constructor(options = {}) {
|
|
24
|
+
this.encodings = options.encodings ?? ['gzip', 'deflate'];
|
|
25
|
+
this.threshold = options.threshold ?? 1024;
|
|
26
|
+
this.isCompressibleContentType = options.isCompressibleContentType ?? isCompressibleContentType;
|
|
27
|
+
}
|
|
28
|
+
init(options) {
|
|
29
|
+
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
30
|
+
const result = await next();
|
|
31
|
+
if (!result.matched) {
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
const response = result.response;
|
|
35
|
+
const encoding = negotiateResponseCompressionEncoding(response, interceptorOptions.request.headers, this.encodings);
|
|
36
|
+
if (encoding === undefined) {
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
const body = response.body;
|
|
40
|
+
const headers = response.headers;
|
|
41
|
+
if (body instanceof ReadableStream) {
|
|
42
|
+
const contentLength = Number(flattenStandardHeader(headers['content-length']));
|
|
43
|
+
if ((!Number.isFinite(contentLength) || contentLength >= this.threshold) &&
|
|
44
|
+
this.isCompressibleContentType(flattenStandardHeader(headers['content-type']), interceptorOptions)) {
|
|
45
|
+
return {
|
|
46
|
+
...result,
|
|
47
|
+
response: {
|
|
48
|
+
...response,
|
|
49
|
+
body: body.pipeThrough(new CompressionStream(encoding)),
|
|
50
|
+
headers: toCompressedHeaders(headers, encoding),
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else if (body instanceof Blob) {
|
|
56
|
+
// Adapters only fall back to the blob type when no Content-Type header is set
|
|
57
|
+
const contentType = headers['content-type'] ?? body.type;
|
|
58
|
+
if ((!Number.isFinite(body.size) || body.size >= this.threshold) &&
|
|
59
|
+
this.isCompressibleContentType(flattenStandardHeader(contentType), interceptorOptions)) {
|
|
60
|
+
const contentDisposition = headers['content-disposition'] ??
|
|
61
|
+
generateContentDisposition(body instanceof File ? body.name : 'blob');
|
|
62
|
+
return {
|
|
63
|
+
...result,
|
|
64
|
+
response: {
|
|
65
|
+
...response,
|
|
66
|
+
body: body.stream().pipeThrough(new CompressionStream(encoding)),
|
|
67
|
+
headers: {
|
|
68
|
+
...toCompressedHeaders(headers, encoding),
|
|
69
|
+
'standard-server': headers['standard-server'] ?? 'file',
|
|
70
|
+
'content-type': contentType,
|
|
71
|
+
'content-disposition': contentDisposition,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else if (body instanceof FormData) {
|
|
78
|
+
const PART_OVERHEAD = 64; // approx bytes for boundary + Content-Disposition/Content-Type headers per part
|
|
79
|
+
let contentLength = 0;
|
|
80
|
+
for (const [key, value] of body) {
|
|
81
|
+
contentLength += PART_OVERHEAD + key.length;
|
|
82
|
+
if (value instanceof Blob) {
|
|
83
|
+
if (!Number.isFinite(value.size)) {
|
|
84
|
+
// Bun-s3 can use NaN for size
|
|
85
|
+
if (!this.isCompressibleContentType(value.type, interceptorOptions)) {
|
|
86
|
+
// Unknown non-compressible part size makes the estimate unreliable
|
|
87
|
+
contentLength = -Infinity;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
// Unknown size for compressible content - still apply compression
|
|
91
|
+
contentLength = Infinity;
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
contentLength += this.isCompressibleContentType(value.type, interceptorOptions)
|
|
95
|
+
? value.size
|
|
96
|
+
: -value.size;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
contentLength += value.length * AVG_BYTES_PER_CHAR;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (contentLength >= this.threshold) {
|
|
104
|
+
const res = new Response(body);
|
|
105
|
+
const compressedStream = res.body.pipeThrough(new CompressionStream(encoding));
|
|
106
|
+
return {
|
|
107
|
+
...result,
|
|
108
|
+
response: {
|
|
109
|
+
...response,
|
|
110
|
+
body: compressedStream,
|
|
111
|
+
headers: {
|
|
112
|
+
...toCompressedHeaders(headers, encoding),
|
|
113
|
+
'standard-server': [],
|
|
114
|
+
'content-type': res.headers.get('content-type'),
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else if (body instanceof URLSearchParams) {
|
|
121
|
+
const string = body.toString();
|
|
122
|
+
if (string.length * AVG_BYTES_PER_CHAR >= this.threshold) {
|
|
123
|
+
const [, bodyHeaders] = toFetchBody(body, headers);
|
|
124
|
+
return {
|
|
125
|
+
...result,
|
|
126
|
+
response: {
|
|
127
|
+
...response,
|
|
128
|
+
body: new Blob([string]).stream().pipeThrough(new CompressionStream(encoding)),
|
|
129
|
+
headers: {
|
|
130
|
+
...toCompressedHeaders(bodyHeaders, encoding),
|
|
131
|
+
// An empty hint rather than none, so the adapter does not hint the compressed stream as `octet-stream`
|
|
132
|
+
'standard-server': bodyHeaders['standard-server'] ?? [],
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
else if (body !== undefined && !isAsyncIteratorObject(body)) {
|
|
139
|
+
const [string, bodyHeaders] = toFetchBody(body, headers);
|
|
140
|
+
if (string.length * AVG_BYTES_PER_CHAR >= this.threshold) {
|
|
141
|
+
return {
|
|
142
|
+
...result,
|
|
143
|
+
response: {
|
|
144
|
+
...response,
|
|
145
|
+
body: new Blob([string]).stream().pipeThrough(new CompressionStream(encoding)),
|
|
146
|
+
headers: {
|
|
147
|
+
...toCompressedHeaders(bodyHeaders, encoding),
|
|
148
|
+
'standard-server': bodyHeaders['standard-server'] ?? [],
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
155
|
+
};
|
|
156
|
+
return {
|
|
157
|
+
...options,
|
|
158
|
+
routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)],
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Picks the first of `encodings` the client accepts, or `undefined` when the response
|
|
164
|
+
* must be sent as is because it is already encoded, partial, or forbids transforms.
|
|
165
|
+
* Shared by the response compression plugins so they agree on when to compress.
|
|
166
|
+
*/
|
|
167
|
+
export function negotiateResponseCompressionEncoding(response, requestHeaders, encodings) {
|
|
168
|
+
if (flattenStandardHeader(response.headers['content-encoding']) !== undefined) {
|
|
169
|
+
// already compressed, do not compress again
|
|
170
|
+
return undefined;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* A partial response body is a byte range of the identity representation, so compressing it
|
|
174
|
+
* would leave `Content-Range` describing offsets the client never receives.
|
|
175
|
+
*/
|
|
176
|
+
if (response.status === 206 ||
|
|
177
|
+
flattenStandardHeader(response.headers['content-range']) !== undefined) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
// Cache-Control: no-transform forbids intermediaries (and these plugins) from transforming the body
|
|
181
|
+
if (isNoTransformCacheControl(flattenStandardHeader(response.headers['cache-control']))) {
|
|
182
|
+
return undefined;
|
|
183
|
+
}
|
|
184
|
+
const acceptEncodings = parseAcceptEncodingQualities(flattenStandardHeader(requestHeaders['accept-encoding']));
|
|
185
|
+
return encodings.find((encoding) => isAcceptableEncoding(acceptEncodings, encoding));
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The headers of a response whose body is compressed with `encoding`.
|
|
189
|
+
* Shared by the response compression plugins so compressed responses carry the same headers.
|
|
190
|
+
*/
|
|
191
|
+
export function toCompressedHeaders(headers, encoding) {
|
|
192
|
+
return {
|
|
193
|
+
...headers,
|
|
194
|
+
'content-length': [],
|
|
195
|
+
'content-encoding': encoding,
|
|
196
|
+
'vary': varyByAcceptEncoding(flattenStandardHeader(headers.vary)),
|
|
197
|
+
/**
|
|
198
|
+
* A strong tag shared with the identity bytes would let `If-Range` splice them after compressed ones.
|
|
199
|
+
* A weak tag still revalidates through `If-None-Match`, and the compressed body serves no ranges.
|
|
200
|
+
*
|
|
201
|
+
* @see https://www.rfc-editor.org/rfc/rfc9110.html#name-etag
|
|
202
|
+
*/
|
|
203
|
+
'etag': weakenEtag(flattenStandardHeader(headers.etag)),
|
|
204
|
+
'accept-ranges': [],
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
function weakenEtag(etag) {
|
|
208
|
+
return etag === undefined || etag.startsWith('W/') ? etag : `W/${etag}`;
|
|
209
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin } from '../adapters/standard/index.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The context shape into which the Response Headers Plugin injects `resHeaders`.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link https://orpc.dev/docs/plugins/response-headers | Response Headers Plugin}
|
|
6
|
+
*/
|
|
7
|
+
export interface ResponseHeadersHandlerPluginContext {
|
|
8
|
+
/**
|
|
9
|
+
* Response headers as a Headers instance. This is injected by the Response Headers Plugin.
|
|
10
|
+
* When set before the response is sent, these headers will be included in the response. If not set, no additional headers will be added.
|
|
11
|
+
*/
|
|
12
|
+
resHeaders?: Headers | undefined;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The Response Headers Plugin allows you to set response headers in oRPC.
|
|
16
|
+
* It injects a resHeaders instance into the context, enabling you to modify response headers easily.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* **Warning**: In a `streaming` batch, only the cookies of the first procedure to respond reach the client.
|
|
20
|
+
*
|
|
21
|
+
* @see {@link https://orpc.dev/docs/plugins/response-headers | Response Headers Plugin}
|
|
22
|
+
*/
|
|
23
|
+
export declare class ResponseHeadersHandlerPlugin<T extends ResponseHeadersHandlerPluginContext> implements StandardHandlerPlugin<T> {
|
|
24
|
+
name: string;
|
|
25
|
+
/**
|
|
26
|
+
* Interceptors should run after batch interceptors so headers are applied to each sub-response.
|
|
27
|
+
*/
|
|
28
|
+
before: string[];
|
|
29
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { toArray } from '@orpc/shared';
|
|
2
|
+
import { mergeStandardHeaders } from '@standard-server/core';
|
|
3
|
+
import { toStandardHeaders } from '@standard-server/fetch';
|
|
4
|
+
/**
|
|
5
|
+
* The Response Headers Plugin allows you to set response headers in oRPC.
|
|
6
|
+
* It injects a resHeaders instance into the context, enabling you to modify response headers easily.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* **Warning**: In a `streaming` batch, only the cookies of the first procedure to respond reach the client.
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://orpc.dev/docs/plugins/response-headers | Response Headers Plugin}
|
|
12
|
+
*/
|
|
13
|
+
export class ResponseHeadersHandlerPlugin {
|
|
14
|
+
name = '~response-headers';
|
|
15
|
+
/**
|
|
16
|
+
* Interceptors should run after batch interceptors so headers are applied to each sub-response.
|
|
17
|
+
*/
|
|
18
|
+
before = ['~batch'];
|
|
19
|
+
init(options) {
|
|
20
|
+
return {
|
|
21
|
+
...options,
|
|
22
|
+
routingInterceptors: [
|
|
23
|
+
async (interceptorOptions) => {
|
|
24
|
+
const resHeaders = new Headers(interceptorOptions.context.resHeaders);
|
|
25
|
+
const result = await interceptorOptions.next({
|
|
26
|
+
...interceptorOptions,
|
|
27
|
+
context: {
|
|
28
|
+
...interceptorOptions.context,
|
|
29
|
+
resHeaders,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
if (!result.response) {
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
...result,
|
|
37
|
+
response: {
|
|
38
|
+
...result.response,
|
|
39
|
+
headers: mergeStandardHeaders(result.response.headers, toStandardHeaders(resHeaders)),
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
...toArray(options.routingInterceptors),
|
|
44
|
+
],
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ThrowableError } from '@orpc/shared';
|
|
2
|
+
import type { StandardHandlerInterceptorOptions, StandardHandlerOptions, StandardHandlerPlugin } from '../adapters/standard/index.ts';
|
|
3
|
+
import type { Context } from '../context.ts';
|
|
4
|
+
export interface RethrowHandlerPluginOptions<T extends Context> {
|
|
5
|
+
/**
|
|
6
|
+
* Decide which errors should be rethrown.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const rethrowPlugin = new RethrowHandlerPlugin({
|
|
11
|
+
* filter: (error) => {
|
|
12
|
+
* // Rethrow all non-ORPCError errors
|
|
13
|
+
* return !(error instanceof ORPCError)
|
|
14
|
+
* }
|
|
15
|
+
* })
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
filter: (error: ThrowableError, options: StandardHandlerInterceptorOptions<T>) => boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The plugin can bypass oRPC's built-in error handling
|
|
22
|
+
* and rethrow matching errors directly to your framework's error handling mechanism
|
|
23
|
+
* (e.g., NestJS exception filters, Express error middleware).
|
|
24
|
+
*
|
|
25
|
+
* @see {@link https://orpc.dev/docs/plugins/rethrow | Rethrow Handler Plugin}
|
|
26
|
+
*/
|
|
27
|
+
export declare class RethrowHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
28
|
+
name: string;
|
|
29
|
+
private readonly filter;
|
|
30
|
+
private readonly CONTEXT_SYMBOL;
|
|
31
|
+
constructor(options: RethrowHandlerPluginOptions<T>);
|
|
32
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { toArray } from '@orpc/shared';
|
|
2
|
+
/**
|
|
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
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin } from '../adapters/standard/index.ts';
|
|
2
|
+
import type { Context } from '../context.ts';
|
|
3
|
+
/**
|
|
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
|
+
export declare class SignalSyncHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
11
|
+
name: string;
|
|
12
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
13
|
+
}
|