@orpc/server 2.0.0-beta.47 → 2.0.0-beta.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -7
- 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 +10 -45
- 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 -63
- 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 -42
- 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 +5 -45
- 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/{index.d.mts → handler.d.ts} +17 -30
- package/dist/adapters/message-port/handler.js +86 -0
- package/dist/adapters/message-port/index.d.ts +2 -77
- 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 +5 -42
- 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/codec.js +1 -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 -95
- 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 -11
- package/dist/adapters/standard-peer/index.js +1 -0
- package/dist/adapters/standard-peer/utils.d.ts +4 -0
- package/dist/adapters/standard-peer/utils.js +7 -0
- package/dist/adapters/websocket/{index.d.mts → handler.d.ts} +11 -29
- package/dist/adapters/websocket/handler.js +85 -0
- package/dist/adapters/websocket/index.d.ts +2 -77
- 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-variants.js +1 -0
- package/dist/builder.d.ts +127 -0
- package/dist/builder.js +226 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +2 -0
- package/dist/context.d.ts +6 -0
- package/dist/context.js +1 -0
- package/dist/extensions/callable.d.ts +6 -5
- 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 -167
- 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 +45 -460
- 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/middleware.js +1 -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 +29 -422
- 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/{shared/server.CgBF3Vj1.d.ts → plugins/request-limit.d.ts} +4 -8
- 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.ts +5 -0
- package/dist/router-hidden.js +14 -0
- package/dist/router-utils.d.ts +48 -0
- package/dist/router-utils.js +202 -0
- package/dist/{shared/server.Dz7j9BEh.d.ts → router.d.ts} +14 -15
- package/dist/router.js +1 -0
- package/package.json +84 -86
- package/dist/adapters/aws-lambda/index.d.mts +0 -51
- package/dist/adapters/aws-lambda/index.mjs +0 -39
- package/dist/adapters/crossws/index.d.mts +0 -63
- package/dist/adapters/crossws/index.mjs +0 -68
- package/dist/adapters/fastify/index.d.mts +0 -42
- package/dist/adapters/fastify/index.mjs +0 -39
- package/dist/adapters/fetch/index.d.mts +0 -47
- package/dist/adapters/fetch/index.mjs +0 -42
- package/dist/adapters/message-port/index.mjs +0 -87
- package/dist/adapters/node/index.d.mts +0 -44
- package/dist/adapters/node/index.mjs +0 -41
- package/dist/adapters/standard/index.d.mts +0 -95
- package/dist/adapters/standard/index.mjs +0 -8
- package/dist/adapters/standard-peer/index.d.mts +0 -11
- package/dist/adapters/standard-peer/index.mjs +0 -10
- package/dist/adapters/websocket/index.mjs +0 -87
- package/dist/extensions/callable.d.mts +0 -10
- package/dist/extensions/callable.mjs +0 -11
- package/dist/helpers/index.d.mts +0 -167
- package/dist/helpers/index.mjs +0 -200
- package/dist/index.d.mts +0 -476
- package/dist/index.mjs +0 -463
- package/dist/plugins/index.d.mts +0 -432
- package/dist/plugins/index.mjs +0 -965
- package/dist/shared/server.1ExWFMze.mjs +0 -69
- package/dist/shared/server.9B1jEm5a.d.mts +0 -105
- package/dist/shared/server.BB464y-6.mjs +0 -207
- package/dist/shared/server.BC5BIu02.d.mts +0 -73
- package/dist/shared/server.BSYGskId.d.ts +0 -14
- package/dist/shared/server.BvG3boBj.d.mts +0 -14
- package/dist/shared/server.CTV_mC2P.d.mts +0 -34
- package/dist/shared/server.CYbIpHbH.mjs +0 -239
- package/dist/shared/server.Chq5FpXf.mjs +0 -353
- package/dist/shared/server.DFPxP2v3.d.mts +0 -166
- package/dist/shared/server.DFPxP2v3.d.ts +0 -166
- package/dist/shared/server.DOg7ke6y.d.ts +0 -105
- package/dist/shared/server.W91HSRkE.mjs +0 -9
- package/dist/shared/server.sBQSgqK3.mjs +0 -41
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { toArray } from '@orpc/shared';
|
|
2
|
+
import { parseStandardUrl } from '@standard-server/core';
|
|
3
|
+
/**
|
|
4
|
+
* Overrides the HTTP method of a POST request based on a query parameter,
|
|
5
|
+
* so HTML forms (which only support GET and POST) can invoke procedures
|
|
6
|
+
* routed as PUT, PATCH, or DELETE.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://orpc.dev/docs/plugins/method-override | Method Override Plugin}
|
|
9
|
+
*/
|
|
10
|
+
export class MethodOverrideHandlerPlugin {
|
|
11
|
+
name = '~method-override';
|
|
12
|
+
/**
|
|
13
|
+
* Should override batch sub-request methods, not the original batch request.
|
|
14
|
+
*/
|
|
15
|
+
before = ['~batch'];
|
|
16
|
+
param;
|
|
17
|
+
methods;
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
this.param = options.param ?? 'method';
|
|
20
|
+
this.methods = new Set((options.methods ?? ['PUT', 'PATCH', 'DELETE']).map((method) => method.toUpperCase()));
|
|
21
|
+
}
|
|
22
|
+
init(options) {
|
|
23
|
+
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
24
|
+
const { request } = interceptorOptions;
|
|
25
|
+
if (request.method !== 'POST') {
|
|
26
|
+
return next();
|
|
27
|
+
}
|
|
28
|
+
const [pathname, search, hash] = parseStandardUrl(request.url);
|
|
29
|
+
const params = new URLSearchParams(search);
|
|
30
|
+
const raw = params.getAll(this.param).at(-1);
|
|
31
|
+
if (raw === undefined) {
|
|
32
|
+
return next();
|
|
33
|
+
}
|
|
34
|
+
const method = raw.toUpperCase();
|
|
35
|
+
if (!this.methods.has(method)) {
|
|
36
|
+
return next();
|
|
37
|
+
}
|
|
38
|
+
params.delete(this.param);
|
|
39
|
+
const url = `${pathname}${params.size ? `?${params}` : ''}${hash ?? ''}`;
|
|
40
|
+
return next({
|
|
41
|
+
...interceptorOptions,
|
|
42
|
+
request: { ...request, method, url },
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
return {
|
|
46
|
+
...options,
|
|
47
|
+
routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)],
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin } from '../adapters/standard/index.ts';
|
|
2
|
+
import type { Context } from '../context.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Decompresses incoming request bodies based on the Content-Encoding header,
|
|
5
|
+
* supporting gzip, deflate, and deflate-raw.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://orpc.dev/docs/plugins/request-compression | Request Compression Plugin}
|
|
8
|
+
*/
|
|
9
|
+
export declare class RequestCompressionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
10
|
+
name: string;
|
|
11
|
+
/**
|
|
12
|
+
* Should decompress the original batch request body instead of sub-requests.
|
|
13
|
+
*/
|
|
14
|
+
after: string[];
|
|
15
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
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
|
+
* Each coding adds a decompressor to the chain, so an unbounded list lets a tiny
|
|
7
|
+
* request cost unbounded CPU and memory. Same limit as undici and curl.
|
|
8
|
+
*/
|
|
9
|
+
const MAX_CONTENT_ENCODINGS = 5;
|
|
10
|
+
/**
|
|
11
|
+
* Decompresses incoming request bodies based on the Content-Encoding header,
|
|
12
|
+
* supporting gzip, deflate, and deflate-raw.
|
|
13
|
+
*
|
|
14
|
+
* @see {@link https://orpc.dev/docs/plugins/request-compression | Request Compression Plugin}
|
|
15
|
+
*/
|
|
16
|
+
export class RequestCompressionHandlerPlugin {
|
|
17
|
+
name = '~request-compression';
|
|
18
|
+
/**
|
|
19
|
+
* Should decompress the original batch request body instead of sub-requests.
|
|
20
|
+
*/
|
|
21
|
+
after = ['~batch'];
|
|
22
|
+
init(options) {
|
|
23
|
+
// oxlint-disable-next-line unicorn/consistent-function-scoping
|
|
24
|
+
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
25
|
+
const encodings = parseContentEncodings(flattenStandardHeader(interceptorOptions.request.headers['content-encoding']));
|
|
26
|
+
if (encodings.length === 0 || !encodings.every(isSupportedEncoding)) {
|
|
27
|
+
return next();
|
|
28
|
+
}
|
|
29
|
+
const decompressedHeaders = {
|
|
30
|
+
...interceptorOptions.request.headers,
|
|
31
|
+
'content-length': undefined,
|
|
32
|
+
'content-encoding': undefined,
|
|
33
|
+
};
|
|
34
|
+
return next({
|
|
35
|
+
...interceptorOptions,
|
|
36
|
+
request: {
|
|
37
|
+
...interceptorOptions.request,
|
|
38
|
+
headers: decompressedHeaders,
|
|
39
|
+
async resolveBody(hint) {
|
|
40
|
+
const stream = await interceptorOptions.request.resolveBody('octet-stream');
|
|
41
|
+
// adapter might not support hint (e.g peer adapter)
|
|
42
|
+
if (!(stream instanceof ReadableStream)) {
|
|
43
|
+
return stream;
|
|
44
|
+
}
|
|
45
|
+
if (encodings.length > MAX_CONTENT_ENCODINGS) {
|
|
46
|
+
throw new ORPCError('UNSUPPORTED_MEDIA_TYPE', {
|
|
47
|
+
message: 'Too many content encodings.',
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
let decompressedStream = stream;
|
|
51
|
+
for (let i = encodings.length - 1; i >= 0; i--) {
|
|
52
|
+
decompressedStream = decompressedStream.pipeThrough(new DecompressionStream(encodings[i]));
|
|
53
|
+
}
|
|
54
|
+
const response = new Response(decompressedStream, {
|
|
55
|
+
headers: toFetchHeaders(decompressedHeaders),
|
|
56
|
+
});
|
|
57
|
+
return toStandardBody(response, { hint });
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
...options,
|
|
64
|
+
routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const SUPPORTED_ENCODINGS = ['gzip', 'deflate', 'deflate-raw'];
|
|
69
|
+
function isSupportedEncoding(encoding) {
|
|
70
|
+
return SUPPORTED_ENCODINGS.includes(encoding);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Parse Content-Encoding into ordered codings (order applied).
|
|
74
|
+
*
|
|
75
|
+
* @see https://www.rfc-editor.org/rfc/rfc9110.html#name-content-encoding
|
|
76
|
+
*/
|
|
77
|
+
function parseContentEncodings(header) {
|
|
78
|
+
if (header === undefined) {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
return header.split(',').map((part) => part.trim().toLowerCase());
|
|
82
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
interface RequestLimitHandlerPluginOptions {
|
|
1
|
+
import type { StandardHandlerOptions, StandardHandlerPlugin } from '../adapters/standard/index.ts';
|
|
2
|
+
import type { Context } from '../context.ts';
|
|
3
|
+
export interface RequestLimitHandlerPluginOptions {
|
|
5
4
|
/**
|
|
6
5
|
* The maximum allowed request body size in bytes.
|
|
7
6
|
*/
|
|
@@ -15,7 +14,7 @@ interface RequestLimitHandlerPluginOptions {
|
|
|
15
14
|
*
|
|
16
15
|
* @see {@link https://orpc.dev/docs/plugins/request-limit | Request Limit Plugin}
|
|
17
16
|
*/
|
|
18
|
-
declare class RequestLimitHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
17
|
+
export declare class RequestLimitHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
19
18
|
name: string;
|
|
20
19
|
/**
|
|
21
20
|
* Should limit the original batch request body instead of sub-requests.
|
|
@@ -29,6 +28,3 @@ declare class RequestLimitHandlerPlugin<T extends Context> implements StandardHa
|
|
|
29
28
|
constructor(options: RequestLimitHandlerPluginOptions);
|
|
30
29
|
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
31
30
|
}
|
|
32
|
-
|
|
33
|
-
export { RequestLimitHandlerPlugin as R };
|
|
34
|
-
export type { RequestLimitHandlerPluginOptions as a };
|
|
@@ -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
|
+
}
|