@orpc/server 2.0.0-beta.15 → 2.0.0-beta.16
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 +1 -1
- package/dist/adapters/fetch/index.d.mts +3 -51
- package/dist/adapters/fetch/index.d.ts +3 -51
- package/dist/adapters/fetch/index.mjs +3 -133
- package/dist/adapters/node/index.d.mts +3 -33
- package/dist/adapters/node/index.d.ts +3 -33
- package/dist/adapters/node/index.mjs +3 -109
- package/dist/plugins/index.d.mts +77 -2
- package/dist/plugins/index.d.ts +77 -2
- package/dist/plugins/index.mjs +305 -5
- package/package.json +8 -9
package/README.md
CHANGED
|
@@ -175,7 +175,7 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
175
175
|
<a href="https://github.com/illarionvk?ref=orpc" target="_blank" rel="noopener" title="Illarion Koperski"><img src="https://avatars.githubusercontent.com/u/5012724?u=7cfa13652f7ac5fb3c56d880e3eb3fbe40c3ea34&v=4" width="32" height="32" alt="Illarion Koperski" /></a>
|
|
176
176
|
<a href="https://github.com/steelbrain?ref=orpc" target="_blank" rel="noopener" title="Anees Iqbal"><img src="https://avatars.githubusercontent.com/u/4278113?u=22b80b5399eed68ac76cd58b02961b0481f1db11&v=4" width="32" height="32" alt="Anees Iqbal" /></a>
|
|
177
177
|
<a href="https://github.com/Scrumplex?ref=orpc" target="_blank" rel="noopener" title="Sefa Eyeoglu"><img src="https://avatars.githubusercontent.com/u/11587657?u=ab503582165c0bbff0cca47ce31c9450bb1553c9&v=4" width="32" height="32" alt="Sefa Eyeoglu" /></a>
|
|
178
|
-
<a href="https://github.com/nattstack?ref=orpc" target="_blank" rel="noopener" title="
|
|
178
|
+
<a href="https://github.com/nattstack?ref=orpc" target="_blank" rel="noopener" title="NΛTT"><img src="https://avatars.githubusercontent.com/u/31426677?u=fa9dbb8b3e66eb0ea3c88db5dc07f31c8c5418fe&v=4" width="32" height="32" alt="NΛTT" /></a>
|
|
179
179
|
<a href="https://github.com/ChromeGG?ref=orpc" target="_blank" rel="noopener" title="Adam Tkaczyk"><img src="https://avatars.githubusercontent.com/u/39050595?u=a58ca6042a6950e94e6e92442db76ef584279bc0&v=4" width="32" height="32" alt="Adam Tkaczyk" /></a>
|
|
180
180
|
<a href="https://github.com/plancraft?ref=orpc" target="_blank" rel="noopener" title="plancraft"><img src="https://avatars.githubusercontent.com/u/46482287?v=4" width="32" height="32" alt="plancraft" /></a>
|
|
181
181
|
</p>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { C as Context } from '../../shared/server.BL22TloH.mjs';
|
|
2
1
|
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
|
3
2
|
import { ToFetchResponseOptions } from '@standardserver/fetch';
|
|
3
|
+
import { C as Context } from '../../shared/server.BL22TloH.mjs';
|
|
4
4
|
import { e as StandardHandlerPlugin, b as StandardHandlerHandleOptions, S as StandardHandler, a as StandardHandlerOptions } from '../../shared/server.DJIkub_t.mjs';
|
|
5
5
|
import { FriendlyStandardHandlerHandleOptions } from '../standard/index.mjs';
|
|
6
6
|
import { R as Router } from '../../shared/server.BsNNjG5J.mjs';
|
|
@@ -71,54 +71,6 @@ declare class FetchHandler<T extends Context> {
|
|
|
71
71
|
handle(request: Request, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): Promise<FetchHandlerHandleResult>;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
declare const ORDERED_SUPPORTED_ENCODINGS: readonly ["gzip", "deflate"];
|
|
75
|
-
interface BodyCompressionHandlerPluginOptions {
|
|
76
|
-
/**
|
|
77
|
-
* The compression schemes to use for response compression.
|
|
78
|
-
* Schemes are prioritized by their order in this array and
|
|
79
|
-
* only applied if the client supports them.
|
|
80
|
-
*
|
|
81
|
-
* @default ['gzip', 'deflate']
|
|
82
|
-
*/
|
|
83
|
-
encodings?: readonly (typeof ORDERED_SUPPORTED_ENCODINGS)[number][];
|
|
84
|
-
/**
|
|
85
|
-
* The minimum response size in bytes required to trigger compression.
|
|
86
|
-
* Responses smaller than this threshold will not be compressed to avoid overhead.
|
|
87
|
-
* If the response size cannot be determined, compression will still be applied.
|
|
88
|
-
*
|
|
89
|
-
* @default 1024 (1KB)
|
|
90
|
-
*/
|
|
91
|
-
threshold?: number;
|
|
92
|
-
/**
|
|
93
|
-
* Override the default content-type filter used to determine which responses should be compressed.
|
|
94
|
-
*
|
|
95
|
-
* @warning Event stream responses are never compressed, regardless of this filter's return value.
|
|
96
|
-
* @default only responses with compressible content types are compressed.
|
|
97
|
-
*/
|
|
98
|
-
filter?: (request: Request, response: Response) => boolean;
|
|
99
|
-
}
|
|
100
|
-
declare class BodyCompressionHandlerPlugin<T extends Context> implements FetchHandlerPlugin<T> {
|
|
101
|
-
name: string;
|
|
102
|
-
private readonly encodings;
|
|
103
|
-
private readonly threshold;
|
|
104
|
-
private readonly filter;
|
|
105
|
-
constructor(options?: BodyCompressionHandlerPluginOptions);
|
|
106
|
-
initFetchHandlerOptions(options: FetchHandlerOptions<T>): FetchHandlerOptions<T>;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
interface BodyLimitHandlerPluginOptions {
|
|
110
|
-
/**
|
|
111
|
-
* The maximum size of the body in bytes.
|
|
112
|
-
*/
|
|
113
|
-
maxBodySize: number;
|
|
114
|
-
}
|
|
115
|
-
declare class BodyLimitHandlerPlugin<T extends Context> implements FetchHandlerPlugin<T> {
|
|
116
|
-
name: string;
|
|
117
|
-
private readonly maxBodySize;
|
|
118
|
-
constructor(options: BodyLimitHandlerPluginOptions);
|
|
119
|
-
initFetchHandlerOptions(options: FetchHandlerOptions<T>): FetchHandlerOptions<T>;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
74
|
interface RPCHandlerOptions<T extends Context> extends FetchHandlerOptions<T>, Omit<StandardHandlerOptions<T>, 'plugins'>, RPCHandlerCodecOptions<T> {
|
|
123
75
|
/**
|
|
124
76
|
* Configuration for {@link CSRFGuardHandlerPlugin}, which is enabled by default for `RPCHandler` over HTTP.
|
|
@@ -136,5 +88,5 @@ declare class RPCHandler<T extends Context> extends FetchHandler<T> {
|
|
|
136
88
|
constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
137
89
|
}
|
|
138
90
|
|
|
139
|
-
export {
|
|
140
|
-
export type {
|
|
91
|
+
export { CompositeFetchHandlerPlugin, FetchHandler, RPCHandler };
|
|
92
|
+
export type { FetchHandlerFetchInterceptor, FetchHandlerFetchInterceptorOptions, FetchHandlerHandleResult, FetchHandlerOptions, FetchHandlerPlugin, RPCHandlerOptions };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { C as Context } from '../../shared/server.BL22TloH.js';
|
|
2
1
|
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
|
3
2
|
import { ToFetchResponseOptions } from '@standardserver/fetch';
|
|
3
|
+
import { C as Context } from '../../shared/server.BL22TloH.js';
|
|
4
4
|
import { e as StandardHandlerPlugin, b as StandardHandlerHandleOptions, S as StandardHandler, a as StandardHandlerOptions } from '../../shared/server.DBANIheG.js';
|
|
5
5
|
import { FriendlyStandardHandlerHandleOptions } from '../standard/index.js';
|
|
6
6
|
import { R as Router } from '../../shared/server.Pa0F03f_.js';
|
|
@@ -71,54 +71,6 @@ declare class FetchHandler<T extends Context> {
|
|
|
71
71
|
handle(request: Request, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): Promise<FetchHandlerHandleResult>;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
declare const ORDERED_SUPPORTED_ENCODINGS: readonly ["gzip", "deflate"];
|
|
75
|
-
interface BodyCompressionHandlerPluginOptions {
|
|
76
|
-
/**
|
|
77
|
-
* The compression schemes to use for response compression.
|
|
78
|
-
* Schemes are prioritized by their order in this array and
|
|
79
|
-
* only applied if the client supports them.
|
|
80
|
-
*
|
|
81
|
-
* @default ['gzip', 'deflate']
|
|
82
|
-
*/
|
|
83
|
-
encodings?: readonly (typeof ORDERED_SUPPORTED_ENCODINGS)[number][];
|
|
84
|
-
/**
|
|
85
|
-
* The minimum response size in bytes required to trigger compression.
|
|
86
|
-
* Responses smaller than this threshold will not be compressed to avoid overhead.
|
|
87
|
-
* If the response size cannot be determined, compression will still be applied.
|
|
88
|
-
*
|
|
89
|
-
* @default 1024 (1KB)
|
|
90
|
-
*/
|
|
91
|
-
threshold?: number;
|
|
92
|
-
/**
|
|
93
|
-
* Override the default content-type filter used to determine which responses should be compressed.
|
|
94
|
-
*
|
|
95
|
-
* @warning Event stream responses are never compressed, regardless of this filter's return value.
|
|
96
|
-
* @default only responses with compressible content types are compressed.
|
|
97
|
-
*/
|
|
98
|
-
filter?: (request: Request, response: Response) => boolean;
|
|
99
|
-
}
|
|
100
|
-
declare class BodyCompressionHandlerPlugin<T extends Context> implements FetchHandlerPlugin<T> {
|
|
101
|
-
name: string;
|
|
102
|
-
private readonly encodings;
|
|
103
|
-
private readonly threshold;
|
|
104
|
-
private readonly filter;
|
|
105
|
-
constructor(options?: BodyCompressionHandlerPluginOptions);
|
|
106
|
-
initFetchHandlerOptions(options: FetchHandlerOptions<T>): FetchHandlerOptions<T>;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
interface BodyLimitHandlerPluginOptions {
|
|
110
|
-
/**
|
|
111
|
-
* The maximum size of the body in bytes.
|
|
112
|
-
*/
|
|
113
|
-
maxBodySize: number;
|
|
114
|
-
}
|
|
115
|
-
declare class BodyLimitHandlerPlugin<T extends Context> implements FetchHandlerPlugin<T> {
|
|
116
|
-
name: string;
|
|
117
|
-
private readonly maxBodySize;
|
|
118
|
-
constructor(options: BodyLimitHandlerPluginOptions);
|
|
119
|
-
initFetchHandlerOptions(options: FetchHandlerOptions<T>): FetchHandlerOptions<T>;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
74
|
interface RPCHandlerOptions<T extends Context> extends FetchHandlerOptions<T>, Omit<StandardHandlerOptions<T>, 'plugins'>, RPCHandlerCodecOptions<T> {
|
|
123
75
|
/**
|
|
124
76
|
* Configuration for {@link CSRFGuardHandlerPlugin}, which is enabled by default for `RPCHandler` over HTTP.
|
|
@@ -136,5 +88,5 @@ declare class RPCHandler<T extends Context> extends FetchHandler<T> {
|
|
|
136
88
|
constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
137
89
|
}
|
|
138
90
|
|
|
139
|
-
export {
|
|
140
|
-
export type {
|
|
91
|
+
export { CompositeFetchHandlerPlugin, FetchHandler, RPCHandler };
|
|
92
|
+
export type { FetchHandlerFetchInterceptor, FetchHandlerFetchInterceptorOptions, FetchHandlerHandleResult, FetchHandlerOptions, FetchHandlerPlugin, RPCHandlerOptions };
|
|
@@ -1,144 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ORPCError } from '@orpc/client';
|
|
1
|
+
import { sortPlugins, intercept, resolveMaybeOptionalOptions, toArray } from '@orpc/shared';
|
|
3
2
|
import { toStandardLazyRequest, toFetchResponse } from '@standardserver/fetch';
|
|
4
3
|
import { r as resolveFriendlyStandardHandlerHandleOptions } from '../../shared/server.W91HSRkE.mjs';
|
|
5
4
|
import { C as CSRFGuardHandlerPlugin } from '../../shared/server.D_QauotT.mjs';
|
|
6
5
|
import { R as RPCHandlerCodec, S as StandardHandler } from '../../shared/server.DZEvIn7o.mjs';
|
|
6
|
+
import '@orpc/client';
|
|
7
7
|
import '@standardserver/core';
|
|
8
8
|
import '../../shared/server.BDeup7Ky.mjs';
|
|
9
9
|
import '@orpc/contract';
|
|
10
10
|
import '../../shared/server.C_osSBcd.mjs';
|
|
11
11
|
|
|
12
|
-
const ORDERED_SUPPORTED_ENCODINGS = ["gzip", "deflate"];
|
|
13
|
-
class BodyCompressionHandlerPlugin {
|
|
14
|
-
name = "~body-compression";
|
|
15
|
-
encodings;
|
|
16
|
-
threshold;
|
|
17
|
-
filter;
|
|
18
|
-
constructor(options = {}) {
|
|
19
|
-
this.encodings = options.encodings ?? ORDERED_SUPPORTED_ENCODINGS;
|
|
20
|
-
this.threshold = options.threshold ?? 1024;
|
|
21
|
-
this.filter = (request, response) => {
|
|
22
|
-
const hasContentDisposition = response.headers.has("content-disposition");
|
|
23
|
-
const contentType = response.headers.get("content-type");
|
|
24
|
-
if (!hasContentDisposition && contentType?.startsWith("text/event-stream")) {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
return options.filter ? options.filter(request, response) : isCompressibleContentType(contentType);
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
initFetchHandlerOptions(options) {
|
|
31
|
-
return {
|
|
32
|
-
...options,
|
|
33
|
-
fetchInterceptors: [
|
|
34
|
-
async (interceptorOptions) => {
|
|
35
|
-
const result = await interceptorOptions.next();
|
|
36
|
-
if (!result.matched) {
|
|
37
|
-
return result;
|
|
38
|
-
}
|
|
39
|
-
const response = result.response;
|
|
40
|
-
if (response.headers.has("content-encoding") || response.headers.has("transfer-encoding") || isNoTransformCacheControl(response.headers.get("cache-control"))) {
|
|
41
|
-
return result;
|
|
42
|
-
}
|
|
43
|
-
const contentLength = response.headers.get("content-length");
|
|
44
|
-
if (contentLength && Number(contentLength) < this.threshold) {
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
const acceptEncoding = interceptorOptions.request.headers.get("accept-encoding")?.split(",").map((enc) => enc.trim().split(";")[0]);
|
|
48
|
-
const encoding = this.encodings.find((enc) => acceptEncoding?.includes(enc));
|
|
49
|
-
if (!response.body || encoding === void 0) {
|
|
50
|
-
return result;
|
|
51
|
-
}
|
|
52
|
-
if (!this.filter(interceptorOptions.request, response)) {
|
|
53
|
-
return result;
|
|
54
|
-
}
|
|
55
|
-
const compressedBody = response.body.pipeThrough(new CompressionStream(encoding));
|
|
56
|
-
const compressedHeaders = new Headers(response.headers);
|
|
57
|
-
compressedHeaders.delete("content-length");
|
|
58
|
-
compressedHeaders.set("content-encoding", encoding);
|
|
59
|
-
return {
|
|
60
|
-
...result,
|
|
61
|
-
response: new Response(compressedBody, {
|
|
62
|
-
status: response.status,
|
|
63
|
-
statusText: response.statusText,
|
|
64
|
-
headers: compressedHeaders
|
|
65
|
-
})
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
...toArray(options.fetchInterceptors)
|
|
69
|
-
]
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/(?!event-stream(?:[;\s]|$))[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i;
|
|
74
|
-
function isCompressibleContentType(contentType) {
|
|
75
|
-
if (contentType === null) {
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
return COMPRESSIBLE_CONTENT_TYPE_REGEX.test(contentType);
|
|
79
|
-
}
|
|
80
|
-
const CACHE_CONTROL_NO_TRANSFORM_REGEX = /(?:^|,)\s*no-transform\s*(?:,|$)/i;
|
|
81
|
-
function isNoTransformCacheControl(cacheControl) {
|
|
82
|
-
if (cacheControl === null) {
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
return CACHE_CONTROL_NO_TRANSFORM_REGEX.test(cacheControl);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
class BodyLimitHandlerPlugin {
|
|
89
|
-
name = "~body-limit";
|
|
90
|
-
maxBodySize;
|
|
91
|
-
constructor(options) {
|
|
92
|
-
this.maxBodySize = options.maxBodySize;
|
|
93
|
-
}
|
|
94
|
-
initFetchHandlerOptions(options) {
|
|
95
|
-
return {
|
|
96
|
-
...options,
|
|
97
|
-
fetchInterceptors: [
|
|
98
|
-
async (interceptorOptions) => {
|
|
99
|
-
if (!interceptorOptions.request.body) {
|
|
100
|
-
return interceptorOptions.next();
|
|
101
|
-
}
|
|
102
|
-
let currentBodySize = 0;
|
|
103
|
-
const rawReader = interceptorOptions.request.body.getReader();
|
|
104
|
-
const body = new ReadableStream({
|
|
105
|
-
start: async (controller) => {
|
|
106
|
-
const reject = async (error) => {
|
|
107
|
-
controller.error(error);
|
|
108
|
-
await rawReader.cancel(error);
|
|
109
|
-
};
|
|
110
|
-
const contentLength = interceptorOptions.request.headers.get("content-length");
|
|
111
|
-
if (contentLength && Number(contentLength) > this.maxBodySize) {
|
|
112
|
-
await reject(new ORPCError("PAYLOAD_TOO_LARGE"));
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
while (true) {
|
|
116
|
-
const { done, value } = await rawReader.read();
|
|
117
|
-
if (done) {
|
|
118
|
-
controller.close();
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
currentBodySize += value.length;
|
|
122
|
-
if (currentBodySize > this.maxBodySize) {
|
|
123
|
-
await reject(new ORPCError("PAYLOAD_TOO_LARGE"));
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
controller.enqueue(value);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
const requestInit = { body, duplex: "half" };
|
|
131
|
-
return interceptorOptions.next({
|
|
132
|
-
...interceptorOptions,
|
|
133
|
-
request: new Request(interceptorOptions.request, requestInit)
|
|
134
|
-
});
|
|
135
|
-
},
|
|
136
|
-
...toArray(options.fetchInterceptors)
|
|
137
|
-
]
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
12
|
class CompositeFetchHandlerPlugin {
|
|
143
13
|
name = "~composite/fetch-handler";
|
|
144
14
|
plugins;
|
|
@@ -201,4 +71,4 @@ class RPCHandler extends FetchHandler {
|
|
|
201
71
|
}
|
|
202
72
|
}
|
|
203
73
|
|
|
204
|
-
export {
|
|
74
|
+
export { CompositeFetchHandlerPlugin, FetchHandler, RPCHandler };
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
|
1
2
|
import { SendStandardResponseOptions, NodeHttpRequest, NodeHttpResponse } from '@standardserver/node';
|
|
2
3
|
import { C as Context } from '../../shared/server.BL22TloH.mjs';
|
|
3
|
-
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
|
4
4
|
import { e as StandardHandlerPlugin, b as StandardHandlerHandleOptions, S as StandardHandler, a as StandardHandlerOptions } from '../../shared/server.DJIkub_t.mjs';
|
|
5
5
|
import { FriendlyStandardHandlerHandleOptions } from '../standard/index.mjs';
|
|
6
|
-
import compression from '@orpc/interop/compression';
|
|
7
6
|
import { R as Router } from '../../shared/server.BsNNjG5J.mjs';
|
|
8
7
|
import { R as RPCHandlerCodecOptions } from '../../shared/server.D8cib80w.mjs';
|
|
9
8
|
import '@orpc/client';
|
|
@@ -71,35 +70,6 @@ declare class NodeHttpHandler<T extends Context> {
|
|
|
71
70
|
handle(request: NodeHttpRequest, response: NodeHttpResponse, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): Promise<NodeHttpHandlerHandleResult>;
|
|
72
71
|
}
|
|
73
72
|
|
|
74
|
-
interface BodyCompressionHandlerPluginOptions extends compression.CompressionOptions {
|
|
75
|
-
/**
|
|
76
|
-
* Override the default content-type filter used to determine which responses should be compressed.
|
|
77
|
-
*
|
|
78
|
-
* @warning Event stream responses are never compressed, regardless of this filter's return value.
|
|
79
|
-
* @default only responses with compressible content types are compressed.
|
|
80
|
-
*/
|
|
81
|
-
filter?: (request: NodeHttpRequest, response: NodeHttpResponse) => boolean;
|
|
82
|
-
}
|
|
83
|
-
declare class BodyCompressionHandlerPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
84
|
-
name: string;
|
|
85
|
-
private readonly compressionHandler;
|
|
86
|
-
constructor(options?: BodyCompressionHandlerPluginOptions);
|
|
87
|
-
initNodeHttpHandlerOptions(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
interface BodyLimitHandlerPluginOptions {
|
|
91
|
-
/**
|
|
92
|
-
* The maximum size of the body in bytes.
|
|
93
|
-
*/
|
|
94
|
-
maxBodySize: number;
|
|
95
|
-
}
|
|
96
|
-
declare class BodyLimitHandlerPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
97
|
-
name: string;
|
|
98
|
-
private readonly maxBodySize;
|
|
99
|
-
constructor(options: BodyLimitHandlerPluginOptions);
|
|
100
|
-
initNodeHttpHandlerOptions(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
73
|
interface RPCHandlerOptions<T extends Context> extends NodeHttpHandlerOptions<T>, Omit<StandardHandlerOptions<T>, 'plugins'>, RPCHandlerCodecOptions<T> {
|
|
104
74
|
/**
|
|
105
75
|
* Configuration for {@link CSRFGuardHandlerPlugin}, which is enabled by default for `RPCHandler` over HTTP.
|
|
@@ -117,5 +87,5 @@ declare class RPCHandler<T extends Context> extends NodeHttpHandler<T> {
|
|
|
117
87
|
constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
118
88
|
}
|
|
119
89
|
|
|
120
|
-
export {
|
|
121
|
-
export type {
|
|
90
|
+
export { CompositeNodeHttpHandlerPlugin, NodeHttpHandler, RPCHandler };
|
|
91
|
+
export type { NodeHttpHandlerHandleResult, NodeHttpHandlerNodeHttpInterceptor, NodeHttpHandlerNodeHttpInterceptorOptions, NodeHttpHandlerOptions, NodeHttpHandlerPlugin, RPCHandlerOptions };
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
|
1
2
|
import { SendStandardResponseOptions, NodeHttpRequest, NodeHttpResponse } from '@standardserver/node';
|
|
2
3
|
import { C as Context } from '../../shared/server.BL22TloH.js';
|
|
3
|
-
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
|
4
4
|
import { e as StandardHandlerPlugin, b as StandardHandlerHandleOptions, S as StandardHandler, a as StandardHandlerOptions } from '../../shared/server.DBANIheG.js';
|
|
5
5
|
import { FriendlyStandardHandlerHandleOptions } from '../standard/index.js';
|
|
6
|
-
import compression from '@orpc/interop/compression';
|
|
7
6
|
import { R as Router } from '../../shared/server.Pa0F03f_.js';
|
|
8
7
|
import { R as RPCHandlerCodecOptions } from '../../shared/server.D0WUu0EH.js';
|
|
9
8
|
import '@orpc/client';
|
|
@@ -71,35 +70,6 @@ declare class NodeHttpHandler<T extends Context> {
|
|
|
71
70
|
handle(request: NodeHttpRequest, response: NodeHttpResponse, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): Promise<NodeHttpHandlerHandleResult>;
|
|
72
71
|
}
|
|
73
72
|
|
|
74
|
-
interface BodyCompressionHandlerPluginOptions extends compression.CompressionOptions {
|
|
75
|
-
/**
|
|
76
|
-
* Override the default content-type filter used to determine which responses should be compressed.
|
|
77
|
-
*
|
|
78
|
-
* @warning Event stream responses are never compressed, regardless of this filter's return value.
|
|
79
|
-
* @default only responses with compressible content types are compressed.
|
|
80
|
-
*/
|
|
81
|
-
filter?: (request: NodeHttpRequest, response: NodeHttpResponse) => boolean;
|
|
82
|
-
}
|
|
83
|
-
declare class BodyCompressionHandlerPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
84
|
-
name: string;
|
|
85
|
-
private readonly compressionHandler;
|
|
86
|
-
constructor(options?: BodyCompressionHandlerPluginOptions);
|
|
87
|
-
initNodeHttpHandlerOptions(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
interface BodyLimitHandlerPluginOptions {
|
|
91
|
-
/**
|
|
92
|
-
* The maximum size of the body in bytes.
|
|
93
|
-
*/
|
|
94
|
-
maxBodySize: number;
|
|
95
|
-
}
|
|
96
|
-
declare class BodyLimitHandlerPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
97
|
-
name: string;
|
|
98
|
-
private readonly maxBodySize;
|
|
99
|
-
constructor(options: BodyLimitHandlerPluginOptions);
|
|
100
|
-
initNodeHttpHandlerOptions(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
73
|
interface RPCHandlerOptions<T extends Context> extends NodeHttpHandlerOptions<T>, Omit<StandardHandlerOptions<T>, 'plugins'>, RPCHandlerCodecOptions<T> {
|
|
104
74
|
/**
|
|
105
75
|
* Configuration for {@link CSRFGuardHandlerPlugin}, which is enabled by default for `RPCHandler` over HTTP.
|
|
@@ -117,5 +87,5 @@ declare class RPCHandler<T extends Context> extends NodeHttpHandler<T> {
|
|
|
117
87
|
constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
118
88
|
}
|
|
119
89
|
|
|
120
|
-
export {
|
|
121
|
-
export type {
|
|
90
|
+
export { CompositeNodeHttpHandlerPlugin, NodeHttpHandler, RPCHandler };
|
|
91
|
+
export type { NodeHttpHandlerHandleResult, NodeHttpHandlerNodeHttpInterceptor, NodeHttpHandlerNodeHttpInterceptorOptions, NodeHttpHandlerOptions, NodeHttpHandlerPlugin, RPCHandlerOptions };
|
|
@@ -1,120 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { toArray, sortPlugins, intercept, resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
3
|
-
import { ORPCError } from '@orpc/client';
|
|
1
|
+
import { sortPlugins, intercept, resolveMaybeOptionalOptions, toArray } from '@orpc/shared';
|
|
4
2
|
import { toStandardLazyRequest, sendStandardResponse } from '@standardserver/node';
|
|
5
3
|
import { r as resolveFriendlyStandardHandlerHandleOptions } from '../../shared/server.W91HSRkE.mjs';
|
|
6
4
|
import { C as CSRFGuardHandlerPlugin } from '../../shared/server.D_QauotT.mjs';
|
|
7
5
|
import { R as RPCHandlerCodec, S as StandardHandler } from '../../shared/server.DZEvIn7o.mjs';
|
|
6
|
+
import '@orpc/client';
|
|
8
7
|
import '@standardserver/core';
|
|
9
8
|
import '../../shared/server.BDeup7Ky.mjs';
|
|
10
9
|
import '@orpc/contract';
|
|
11
10
|
import '../../shared/server.C_osSBcd.mjs';
|
|
12
11
|
|
|
13
|
-
class BodyCompressionHandlerPlugin {
|
|
14
|
-
name = "~body-compression";
|
|
15
|
-
compressionHandler;
|
|
16
|
-
constructor(options = {}) {
|
|
17
|
-
this.compressionHandler = compression({
|
|
18
|
-
...options,
|
|
19
|
-
filter: (request, response) => {
|
|
20
|
-
const hasContentDisposition = response.hasHeader("content-disposition");
|
|
21
|
-
const contentType = response.getHeader("content-type")?.toString();
|
|
22
|
-
if (!hasContentDisposition && contentType?.startsWith("text/event-stream")) {
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
return options.filter ? options.filter(request, response) : compression.filter(request, response);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
initNodeHttpHandlerOptions(options) {
|
|
30
|
-
return {
|
|
31
|
-
...options,
|
|
32
|
-
nodeHttpInterceptors: [
|
|
33
|
-
async (interceptorOptions) => {
|
|
34
|
-
let resolve;
|
|
35
|
-
let reject;
|
|
36
|
-
const promise = new Promise((res, rej) => {
|
|
37
|
-
resolve = res;
|
|
38
|
-
reject = rej;
|
|
39
|
-
});
|
|
40
|
-
const originalWrite = interceptorOptions.response.write;
|
|
41
|
-
const originalEnd = interceptorOptions.response.end;
|
|
42
|
-
const originalOn = interceptorOptions.response.on;
|
|
43
|
-
this.compressionHandler(
|
|
44
|
-
interceptorOptions.request,
|
|
45
|
-
interceptorOptions.response,
|
|
46
|
-
async (error) => {
|
|
47
|
-
if (error) {
|
|
48
|
-
reject(error);
|
|
49
|
-
} else {
|
|
50
|
-
try {
|
|
51
|
-
resolve(await interceptorOptions.next(interceptorOptions));
|
|
52
|
-
} catch (nextError) {
|
|
53
|
-
reject(nextError);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
);
|
|
58
|
-
try {
|
|
59
|
-
return await promise;
|
|
60
|
-
} finally {
|
|
61
|
-
interceptorOptions.response.write = originalWrite;
|
|
62
|
-
interceptorOptions.response.end = originalEnd;
|
|
63
|
-
interceptorOptions.response.on = originalOn;
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
...toArray(options.nodeHttpInterceptors)
|
|
67
|
-
]
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
class BodyLimitHandlerPlugin {
|
|
73
|
-
name = "~body-limit";
|
|
74
|
-
maxBodySize;
|
|
75
|
-
constructor(options) {
|
|
76
|
-
this.maxBodySize = options.maxBodySize;
|
|
77
|
-
}
|
|
78
|
-
initNodeHttpHandlerOptions(options) {
|
|
79
|
-
return {
|
|
80
|
-
...options,
|
|
81
|
-
nodeHttpInterceptors: [
|
|
82
|
-
async (interceptorOptions) => {
|
|
83
|
-
let isHeaderChecked = false;
|
|
84
|
-
const checkHeader = () => {
|
|
85
|
-
if (isHeaderChecked) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
isHeaderChecked = true;
|
|
89
|
-
const contentLength = interceptorOptions.request.headers["content-length"];
|
|
90
|
-
if (contentLength && Number(contentLength) > this.maxBodySize) {
|
|
91
|
-
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
const originalEmit = interceptorOptions.request.emit;
|
|
95
|
-
let currentBodySize = 0;
|
|
96
|
-
interceptorOptions.request.emit = (event, ...args) => {
|
|
97
|
-
if (event === "data") {
|
|
98
|
-
checkHeader();
|
|
99
|
-
currentBodySize += args[0]?.length ?? 0;
|
|
100
|
-
if (currentBodySize > this.maxBodySize) {
|
|
101
|
-
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return originalEmit.call(interceptorOptions.request, event, ...args);
|
|
105
|
-
};
|
|
106
|
-
try {
|
|
107
|
-
return await interceptorOptions.next(interceptorOptions);
|
|
108
|
-
} finally {
|
|
109
|
-
interceptorOptions.request.emit = originalEmit;
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
...toArray(options.nodeHttpInterceptors)
|
|
113
|
-
]
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
12
|
class CompositeNodeHttpHandlerPlugin {
|
|
119
13
|
name = "~composite/node-http-handler";
|
|
120
14
|
plugins;
|
|
@@ -176,4 +70,4 @@ class RPCHandler extends NodeHttpHandler {
|
|
|
176
70
|
}
|
|
177
71
|
}
|
|
178
72
|
|
|
179
|
-
export {
|
|
73
|
+
export { CompositeNodeHttpHandlerPlugin, NodeHttpHandler, RPCHandler };
|
package/dist/plugins/index.d.mts
CHANGED
|
@@ -123,6 +123,15 @@ declare class CSRFGuardHandlerPlugin<T extends Context> implements StandardHandl
|
|
|
123
123
|
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
declare class RequestCompressionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
127
|
+
name: string;
|
|
128
|
+
/**
|
|
129
|
+
* Should decompress the original batch request body instead of sub-requests.
|
|
130
|
+
*/
|
|
131
|
+
after: string[];
|
|
132
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
133
|
+
}
|
|
134
|
+
|
|
126
135
|
interface RequestHeadersHandlerPluginContext {
|
|
127
136
|
/**
|
|
128
137
|
* Request headers as a Headers instance. This is injected by the Request Headers Plugin.
|
|
@@ -140,6 +149,72 @@ declare class RequestHeadersHandlerPlugin<T extends RequestHeadersHandlerPluginC
|
|
|
140
149
|
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
141
150
|
}
|
|
142
151
|
|
|
152
|
+
interface RequestLimitHandlerPluginOptions {
|
|
153
|
+
/**
|
|
154
|
+
* The maximum allowed request body size in bytes.
|
|
155
|
+
*/
|
|
156
|
+
maxBodySize: number;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Rejects requests whose body exceeds `maxBodySize`.
|
|
160
|
+
*
|
|
161
|
+
* When used with the request compression plugin, the limit applies to the
|
|
162
|
+
* decompressed payload rather than the compressed wire size.
|
|
163
|
+
*
|
|
164
|
+
* @see {@link https://orpc.dev/docs/plugins/request-limit Request Limit Plugin Docs}
|
|
165
|
+
*/
|
|
166
|
+
declare class RequestLimitHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
167
|
+
name: string;
|
|
168
|
+
/**
|
|
169
|
+
* Should limit the original batch request body instead of sub-requests.
|
|
170
|
+
*/
|
|
171
|
+
after: string[];
|
|
172
|
+
/**
|
|
173
|
+
* Should limit the final body size instead of the compressed one.
|
|
174
|
+
*/
|
|
175
|
+
before: string[];
|
|
176
|
+
private readonly maxBodySize;
|
|
177
|
+
constructor(options: RequestLimitHandlerPluginOptions);
|
|
178
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
interface ResponseCompressionHandlerPluginOptions<_T extends Context> {
|
|
182
|
+
/**
|
|
183
|
+
* The compression schemes to use for response compression.
|
|
184
|
+
* Schemes are prioritized by their order in this array and
|
|
185
|
+
* only applied if the client supports them (via Accept-Encoding).
|
|
186
|
+
*
|
|
187
|
+
* @default ['gzip', 'deflate']
|
|
188
|
+
*/
|
|
189
|
+
encodings?: readonly ('gzip' | 'deflate' | 'deflate-raw')[];
|
|
190
|
+
/**
|
|
191
|
+
* The minimum response size in bytes required to trigger compression.
|
|
192
|
+
* Responses smaller than this threshold will not be compressed to avoid overhead.
|
|
193
|
+
* If the response size cannot be determined, compression will still be applied.
|
|
194
|
+
*
|
|
195
|
+
* @default 1024 (1KB)
|
|
196
|
+
*/
|
|
197
|
+
threshold?: number;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Compresses response bodies based on the client's Accept-Encoding header.
|
|
201
|
+
* Works at the standard handler level, so it supports all adapters.
|
|
202
|
+
*
|
|
203
|
+
* @see {@link https://orpc.dev/docs/plugins/response-compression Response Compression Plugin Docs}
|
|
204
|
+
*/
|
|
205
|
+
declare class ResponseCompressionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
206
|
+
name: string;
|
|
207
|
+
/**
|
|
208
|
+
* Compression should be done after batching, to compress the final response.
|
|
209
|
+
* Compression should also be done after response headers are set, to access final headers like Content-Type and Cache-Control.
|
|
210
|
+
*/
|
|
211
|
+
after: string[];
|
|
212
|
+
private readonly encodings;
|
|
213
|
+
private readonly threshold;
|
|
214
|
+
constructor(options?: ResponseCompressionHandlerPluginOptions<T>);
|
|
215
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
216
|
+
}
|
|
217
|
+
|
|
143
218
|
interface ResponseHeadersHandlerPluginContext {
|
|
144
219
|
/**
|
|
145
220
|
* Response headers as a Headers instance. This is injected by the Response Headers Plugin.
|
|
@@ -193,5 +268,5 @@ declare class RethrowHandlerPlugin<T extends Context> implements StandardHandler
|
|
|
193
268
|
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
194
269
|
}
|
|
195
270
|
|
|
196
|
-
export { BatchHandlerPlugin, CORSHandlerPlugin, CSRFGuardHandlerPlugin, RequestHeadersHandlerPlugin, ResponseHeadersHandlerPlugin, RethrowHandlerPlugin };
|
|
197
|
-
export type { BatchHandlerPluginOptions, CORSHandlerPluginOptions, RequestHeadersHandlerPluginContext, ResponseHeadersHandlerPluginContext, RethrowHandlerPluginOptions };
|
|
271
|
+
export { BatchHandlerPlugin, CORSHandlerPlugin, CSRFGuardHandlerPlugin, RequestCompressionHandlerPlugin, RequestHeadersHandlerPlugin, RequestLimitHandlerPlugin, ResponseCompressionHandlerPlugin, ResponseHeadersHandlerPlugin, RethrowHandlerPlugin };
|
|
272
|
+
export type { BatchHandlerPluginOptions, CORSHandlerPluginOptions, RequestHeadersHandlerPluginContext, RequestLimitHandlerPluginOptions, ResponseCompressionHandlerPluginOptions, ResponseHeadersHandlerPluginContext, RethrowHandlerPluginOptions };
|
package/dist/plugins/index.d.ts
CHANGED
|
@@ -123,6 +123,15 @@ declare class CSRFGuardHandlerPlugin<T extends Context> implements StandardHandl
|
|
|
123
123
|
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
declare class RequestCompressionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
127
|
+
name: string;
|
|
128
|
+
/**
|
|
129
|
+
* Should decompress the original batch request body instead of sub-requests.
|
|
130
|
+
*/
|
|
131
|
+
after: string[];
|
|
132
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
133
|
+
}
|
|
134
|
+
|
|
126
135
|
interface RequestHeadersHandlerPluginContext {
|
|
127
136
|
/**
|
|
128
137
|
* Request headers as a Headers instance. This is injected by the Request Headers Plugin.
|
|
@@ -140,6 +149,72 @@ declare class RequestHeadersHandlerPlugin<T extends RequestHeadersHandlerPluginC
|
|
|
140
149
|
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
141
150
|
}
|
|
142
151
|
|
|
152
|
+
interface RequestLimitHandlerPluginOptions {
|
|
153
|
+
/**
|
|
154
|
+
* The maximum allowed request body size in bytes.
|
|
155
|
+
*/
|
|
156
|
+
maxBodySize: number;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Rejects requests whose body exceeds `maxBodySize`.
|
|
160
|
+
*
|
|
161
|
+
* When used with the request compression plugin, the limit applies to the
|
|
162
|
+
* decompressed payload rather than the compressed wire size.
|
|
163
|
+
*
|
|
164
|
+
* @see {@link https://orpc.dev/docs/plugins/request-limit Request Limit Plugin Docs}
|
|
165
|
+
*/
|
|
166
|
+
declare class RequestLimitHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
167
|
+
name: string;
|
|
168
|
+
/**
|
|
169
|
+
* Should limit the original batch request body instead of sub-requests.
|
|
170
|
+
*/
|
|
171
|
+
after: string[];
|
|
172
|
+
/**
|
|
173
|
+
* Should limit the final body size instead of the compressed one.
|
|
174
|
+
*/
|
|
175
|
+
before: string[];
|
|
176
|
+
private readonly maxBodySize;
|
|
177
|
+
constructor(options: RequestLimitHandlerPluginOptions);
|
|
178
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
interface ResponseCompressionHandlerPluginOptions<_T extends Context> {
|
|
182
|
+
/**
|
|
183
|
+
* The compression schemes to use for response compression.
|
|
184
|
+
* Schemes are prioritized by their order in this array and
|
|
185
|
+
* only applied if the client supports them (via Accept-Encoding).
|
|
186
|
+
*
|
|
187
|
+
* @default ['gzip', 'deflate']
|
|
188
|
+
*/
|
|
189
|
+
encodings?: readonly ('gzip' | 'deflate' | 'deflate-raw')[];
|
|
190
|
+
/**
|
|
191
|
+
* The minimum response size in bytes required to trigger compression.
|
|
192
|
+
* Responses smaller than this threshold will not be compressed to avoid overhead.
|
|
193
|
+
* If the response size cannot be determined, compression will still be applied.
|
|
194
|
+
*
|
|
195
|
+
* @default 1024 (1KB)
|
|
196
|
+
*/
|
|
197
|
+
threshold?: number;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Compresses response bodies based on the client's Accept-Encoding header.
|
|
201
|
+
* Works at the standard handler level, so it supports all adapters.
|
|
202
|
+
*
|
|
203
|
+
* @see {@link https://orpc.dev/docs/plugins/response-compression Response Compression Plugin Docs}
|
|
204
|
+
*/
|
|
205
|
+
declare class ResponseCompressionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
206
|
+
name: string;
|
|
207
|
+
/**
|
|
208
|
+
* Compression should be done after batching, to compress the final response.
|
|
209
|
+
* Compression should also be done after response headers are set, to access final headers like Content-Type and Cache-Control.
|
|
210
|
+
*/
|
|
211
|
+
after: string[];
|
|
212
|
+
private readonly encodings;
|
|
213
|
+
private readonly threshold;
|
|
214
|
+
constructor(options?: ResponseCompressionHandlerPluginOptions<T>);
|
|
215
|
+
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
216
|
+
}
|
|
217
|
+
|
|
143
218
|
interface ResponseHeadersHandlerPluginContext {
|
|
144
219
|
/**
|
|
145
220
|
* Response headers as a Headers instance. This is injected by the Response Headers Plugin.
|
|
@@ -193,5 +268,5 @@ declare class RethrowHandlerPlugin<T extends Context> implements StandardHandler
|
|
|
193
268
|
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
194
269
|
}
|
|
195
270
|
|
|
196
|
-
export { BatchHandlerPlugin, CORSHandlerPlugin, CSRFGuardHandlerPlugin, RequestHeadersHandlerPlugin, ResponseHeadersHandlerPlugin, RethrowHandlerPlugin };
|
|
197
|
-
export type { BatchHandlerPluginOptions, CORSHandlerPluginOptions, RequestHeadersHandlerPluginContext, ResponseHeadersHandlerPluginContext, RethrowHandlerPluginOptions };
|
|
271
|
+
export { BatchHandlerPlugin, CORSHandlerPlugin, CSRFGuardHandlerPlugin, RequestCompressionHandlerPlugin, RequestHeadersHandlerPlugin, RequestLimitHandlerPlugin, ResponseCompressionHandlerPlugin, ResponseHeadersHandlerPlugin, RethrowHandlerPlugin };
|
|
272
|
+
export type { BatchHandlerPluginOptions, CORSHandlerPluginOptions, RequestHeadersHandlerPluginContext, RequestLimitHandlerPluginOptions, ResponseCompressionHandlerPluginOptions, ResponseHeadersHandlerPluginContext, RethrowHandlerPluginOptions };
|
package/dist/plugins/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { toArray, value } from '@orpc/shared';
|
|
2
|
-
import { flattenStandardHeader, parseStandardUrl, mergeStandardHeaders } from '@standardserver/core';
|
|
1
|
+
import { toArray, value, isCompressibleContentType, isAsyncIteratorObject, stringifyJSON } from '@orpc/shared';
|
|
2
|
+
import { flattenStandardHeader, parseStandardUrl, generateContentDisposition, mergeStandardHeaders } from '@standardserver/core';
|
|
3
3
|
import { isClientPeerSendMessage, ServerPeer, encodePeerMessage } from '@standardserver/peer';
|
|
4
4
|
export { C as CSRFGuardHandlerPlugin } from '../shared/server.D_QauotT.mjs';
|
|
5
|
-
import { toFetchHeaders, toStandardHeaders } from '@standardserver/fetch';
|
|
6
|
-
import '@orpc/client';
|
|
5
|
+
import { toFetchHeaders, toStandardBody, toStandardHeaders } from '@standardserver/fetch';
|
|
6
|
+
import { ORPCError } from '@orpc/client';
|
|
7
7
|
|
|
8
8
|
class BatchHandlerPlugin {
|
|
9
9
|
name = "~batch";
|
|
@@ -244,6 +244,69 @@ class CORSHandlerPlugin {
|
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
246
|
|
|
247
|
+
class RequestCompressionHandlerPlugin {
|
|
248
|
+
name = "~request-compression";
|
|
249
|
+
/**
|
|
250
|
+
* Should decompress the original batch request body instead of sub-requests.
|
|
251
|
+
*/
|
|
252
|
+
after = ["~batch"];
|
|
253
|
+
init(options) {
|
|
254
|
+
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
255
|
+
const encodings = parseContentEncodings(
|
|
256
|
+
flattenStandardHeader(interceptorOptions.request.headers["content-encoding"])
|
|
257
|
+
);
|
|
258
|
+
if (encodings.length === 0 || !encodings.every(isSupportedEncoding)) {
|
|
259
|
+
return next();
|
|
260
|
+
}
|
|
261
|
+
const decompressedHeaders = {
|
|
262
|
+
...interceptorOptions.request.headers,
|
|
263
|
+
"content-length": void 0,
|
|
264
|
+
"content-encoding": void 0
|
|
265
|
+
};
|
|
266
|
+
return next({
|
|
267
|
+
...interceptorOptions,
|
|
268
|
+
request: {
|
|
269
|
+
...interceptorOptions.request,
|
|
270
|
+
headers: decompressedHeaders,
|
|
271
|
+
async resolveBody(hint) {
|
|
272
|
+
const stream = await interceptorOptions.request.resolveBody("octet-stream");
|
|
273
|
+
if (!(stream instanceof ReadableStream)) {
|
|
274
|
+
return stream;
|
|
275
|
+
}
|
|
276
|
+
let decompressedStream = stream;
|
|
277
|
+
for (let i = encodings.length - 1; i >= 0; i--) {
|
|
278
|
+
decompressedStream = decompressedStream.pipeThrough(
|
|
279
|
+
new DecompressionStream(encodings[i])
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
const response = new Response(decompressedStream, {
|
|
283
|
+
headers: toFetchHeaders(decompressedHeaders)
|
|
284
|
+
});
|
|
285
|
+
return toStandardBody(response, { hint });
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
};
|
|
290
|
+
return {
|
|
291
|
+
...options,
|
|
292
|
+
routingInterceptors: [
|
|
293
|
+
routingInterceptor,
|
|
294
|
+
...toArray(options.routingInterceptors)
|
|
295
|
+
]
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
const SUPPORTED_ENCODINGS = ["gzip", "deflate", "deflate-raw"];
|
|
300
|
+
function isSupportedEncoding(encoding) {
|
|
301
|
+
return SUPPORTED_ENCODINGS.includes(encoding);
|
|
302
|
+
}
|
|
303
|
+
function parseContentEncodings(header) {
|
|
304
|
+
if (header === void 0) {
|
|
305
|
+
return [];
|
|
306
|
+
}
|
|
307
|
+
return header.split(",").map((part) => part.trim().toLowerCase());
|
|
308
|
+
}
|
|
309
|
+
|
|
247
310
|
class RequestHeadersHandlerPlugin {
|
|
248
311
|
name = "~request-headers";
|
|
249
312
|
init(options) {
|
|
@@ -266,6 +329,243 @@ class RequestHeadersHandlerPlugin {
|
|
|
266
329
|
}
|
|
267
330
|
}
|
|
268
331
|
|
|
332
|
+
class RequestLimitHandlerPlugin {
|
|
333
|
+
name = "~request-limit";
|
|
334
|
+
/**
|
|
335
|
+
* Should limit the original batch request body instead of sub-requests.
|
|
336
|
+
*/
|
|
337
|
+
after = ["~batch"];
|
|
338
|
+
/**
|
|
339
|
+
* Should limit the final body size instead of the compressed one.
|
|
340
|
+
*/
|
|
341
|
+
before = ["~request-compression"];
|
|
342
|
+
maxBodySize;
|
|
343
|
+
constructor(options) {
|
|
344
|
+
this.maxBodySize = options.maxBodySize;
|
|
345
|
+
}
|
|
346
|
+
init(options) {
|
|
347
|
+
const maxBodySize = this.maxBodySize;
|
|
348
|
+
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
349
|
+
return next({
|
|
350
|
+
...interceptorOptions,
|
|
351
|
+
request: {
|
|
352
|
+
...interceptorOptions.request,
|
|
353
|
+
async resolveBody(hint) {
|
|
354
|
+
const contentLength = Number(
|
|
355
|
+
flattenStandardHeader(interceptorOptions.request.headers["content-length"])
|
|
356
|
+
);
|
|
357
|
+
if (Number.isFinite(contentLength) && contentLength > maxBodySize) {
|
|
358
|
+
throw new ORPCError("PAYLOAD_TOO_LARGE");
|
|
359
|
+
}
|
|
360
|
+
const stream = await interceptorOptions.request.resolveBody("octet-stream");
|
|
361
|
+
if (!(stream instanceof ReadableStream)) {
|
|
362
|
+
return stream;
|
|
363
|
+
}
|
|
364
|
+
let currentBodySize = 0;
|
|
365
|
+
const limitedStream = stream.pipeThrough(
|
|
366
|
+
new TransformStream({
|
|
367
|
+
transform(chunk, controller) {
|
|
368
|
+
currentBodySize += chunk.byteLength;
|
|
369
|
+
if (currentBodySize > maxBodySize) {
|
|
370
|
+
controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
controller.enqueue(chunk);
|
|
374
|
+
}
|
|
375
|
+
})
|
|
376
|
+
);
|
|
377
|
+
const response = new Response(limitedStream, {
|
|
378
|
+
headers: toFetchHeaders(interceptorOptions.request.headers)
|
|
379
|
+
});
|
|
380
|
+
return toStandardBody(response, { hint });
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
};
|
|
385
|
+
return {
|
|
386
|
+
...options,
|
|
387
|
+
routingInterceptors: [
|
|
388
|
+
routingInterceptor,
|
|
389
|
+
...toArray(options.routingInterceptors)
|
|
390
|
+
]
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const AVG_BYTES_PER_CHAR = 1.2;
|
|
396
|
+
class ResponseCompressionHandlerPlugin {
|
|
397
|
+
name = "~response-compression";
|
|
398
|
+
/**
|
|
399
|
+
* Compression should be done after batching, to compress the final response.
|
|
400
|
+
* Compression should also be done after response headers are set, to access final headers like Content-Type and Cache-Control.
|
|
401
|
+
*/
|
|
402
|
+
after = ["~batch", "~response-headers"];
|
|
403
|
+
encodings;
|
|
404
|
+
threshold;
|
|
405
|
+
constructor(options = {}) {
|
|
406
|
+
this.encodings = options.encodings ?? ["gzip", "deflate"];
|
|
407
|
+
this.threshold = options.threshold ?? 1024;
|
|
408
|
+
}
|
|
409
|
+
init(options) {
|
|
410
|
+
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
411
|
+
const result = await next();
|
|
412
|
+
if (!result.matched) {
|
|
413
|
+
return result;
|
|
414
|
+
}
|
|
415
|
+
const response = result.response;
|
|
416
|
+
const contentEncoding = flattenStandardHeader(response.headers["content-encoding"])?.trim()?.toLowerCase();
|
|
417
|
+
if (contentEncoding !== void 0) {
|
|
418
|
+
return result;
|
|
419
|
+
}
|
|
420
|
+
if (isNoTransformCacheControl(flattenStandardHeader(response.headers["cache-control"]))) {
|
|
421
|
+
return result;
|
|
422
|
+
}
|
|
423
|
+
const acceptEncodings = parseAcceptEncodings(
|
|
424
|
+
flattenStandardHeader(interceptorOptions.request.headers["accept-encoding"])
|
|
425
|
+
);
|
|
426
|
+
const encoding = this.encodings.find((enc) => acceptEncodings.includes(enc));
|
|
427
|
+
if (encoding === void 0) {
|
|
428
|
+
return result;
|
|
429
|
+
}
|
|
430
|
+
const body = response.body;
|
|
431
|
+
const headers = response.headers;
|
|
432
|
+
if (body instanceof ReadableStream) {
|
|
433
|
+
const contentLength = Number(flattenStandardHeader(headers["content-length"]));
|
|
434
|
+
if ((!Number.isFinite(contentLength) || contentLength >= this.threshold) && isCompressibleContentType(flattenStandardHeader(headers["content-type"]))) {
|
|
435
|
+
return {
|
|
436
|
+
...result,
|
|
437
|
+
response: {
|
|
438
|
+
...response,
|
|
439
|
+
body: body.pipeThrough(new CompressionStream(encoding)),
|
|
440
|
+
headers: {
|
|
441
|
+
...headers,
|
|
442
|
+
"standard-server": "octet-stream",
|
|
443
|
+
"content-length": [],
|
|
444
|
+
"content-encoding": encoding
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
} else if (body instanceof Blob) {
|
|
450
|
+
if ((!Number.isFinite(body.size) || body.size >= this.threshold) && isCompressibleContentType(body.type)) {
|
|
451
|
+
const contentDisposition = headers["content-disposition"] ?? generateContentDisposition(
|
|
452
|
+
body instanceof File ? body.name : "blob"
|
|
453
|
+
);
|
|
454
|
+
return {
|
|
455
|
+
...result,
|
|
456
|
+
response: {
|
|
457
|
+
...response,
|
|
458
|
+
body: body.stream().pipeThrough(new CompressionStream(encoding)),
|
|
459
|
+
headers: {
|
|
460
|
+
...headers,
|
|
461
|
+
"standard-server": "file",
|
|
462
|
+
"content-type": body.type,
|
|
463
|
+
"content-length": [],
|
|
464
|
+
"content-disposition": contentDisposition,
|
|
465
|
+
"content-encoding": encoding
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
} else if (body instanceof FormData) {
|
|
471
|
+
const PART_OVERHEAD = 64;
|
|
472
|
+
let contentLength = 0;
|
|
473
|
+
for (const [key, value] of body) {
|
|
474
|
+
contentLength += PART_OVERHEAD + key.length;
|
|
475
|
+
if (value instanceof Blob) {
|
|
476
|
+
if (!Number.isFinite(value.size)) {
|
|
477
|
+
if (!isCompressibleContentType(value.type)) {
|
|
478
|
+
contentLength = -Infinity;
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
contentLength = Infinity;
|
|
482
|
+
} else {
|
|
483
|
+
contentLength += isCompressibleContentType(value.type) ? value.size : -value.size;
|
|
484
|
+
}
|
|
485
|
+
} else {
|
|
486
|
+
contentLength += value.length * AVG_BYTES_PER_CHAR;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
if (contentLength >= this.threshold) {
|
|
490
|
+
const res = new Response(body);
|
|
491
|
+
const compressedStream = res.body.pipeThrough(new CompressionStream(encoding));
|
|
492
|
+
return {
|
|
493
|
+
...result,
|
|
494
|
+
response: {
|
|
495
|
+
...response,
|
|
496
|
+
body: compressedStream,
|
|
497
|
+
headers: {
|
|
498
|
+
...headers,
|
|
499
|
+
"standard-server": [],
|
|
500
|
+
"content-type": res.headers.get("content-type"),
|
|
501
|
+
"content-length": [],
|
|
502
|
+
"content-encoding": encoding
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
} else if (body instanceof URLSearchParams) {
|
|
508
|
+
const string = body.toString();
|
|
509
|
+
if (string.length * AVG_BYTES_PER_CHAR >= this.threshold) {
|
|
510
|
+
return {
|
|
511
|
+
...result,
|
|
512
|
+
response: {
|
|
513
|
+
...response,
|
|
514
|
+
body: new Blob([string]).stream().pipeThrough(new CompressionStream(encoding)),
|
|
515
|
+
headers: {
|
|
516
|
+
...headers,
|
|
517
|
+
"standard-server": [],
|
|
518
|
+
"content-type": "application/x-www-form-urlencoded",
|
|
519
|
+
"content-length": [],
|
|
520
|
+
"content-encoding": encoding
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
} else if (body !== void 0 && !isAsyncIteratorObject(body)) {
|
|
526
|
+
const string = stringifyJSON(body);
|
|
527
|
+
if (string.length * AVG_BYTES_PER_CHAR >= this.threshold) {
|
|
528
|
+
return {
|
|
529
|
+
...result,
|
|
530
|
+
response: {
|
|
531
|
+
...response,
|
|
532
|
+
body: new Blob([string]).stream().pipeThrough(new CompressionStream(encoding)),
|
|
533
|
+
headers: {
|
|
534
|
+
...headers,
|
|
535
|
+
"standard-server": [],
|
|
536
|
+
"content-type": "application/json",
|
|
537
|
+
"content-length": [],
|
|
538
|
+
"content-encoding": encoding
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
return result;
|
|
545
|
+
};
|
|
546
|
+
return {
|
|
547
|
+
...options,
|
|
548
|
+
routingInterceptors: [
|
|
549
|
+
routingInterceptor,
|
|
550
|
+
...toArray(options.routingInterceptors)
|
|
551
|
+
]
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
function parseAcceptEncodings(header) {
|
|
556
|
+
if (header === void 0) {
|
|
557
|
+
return [];
|
|
558
|
+
}
|
|
559
|
+
return header.split(",").map((part) => part.trim().split(";")[0].trim().toLowerCase()).filter(Boolean);
|
|
560
|
+
}
|
|
561
|
+
const CACHE_CONTROL_NO_TRANSFORM_REGEX = /(?:^|,)\s*no-transform\s*(?:,|$)/i;
|
|
562
|
+
function isNoTransformCacheControl(cacheControl) {
|
|
563
|
+
if (cacheControl === void 0) {
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
return CACHE_CONTROL_NO_TRANSFORM_REGEX.test(cacheControl);
|
|
567
|
+
}
|
|
568
|
+
|
|
269
569
|
class ResponseHeadersHandlerPlugin {
|
|
270
570
|
name = "~response-headers";
|
|
271
571
|
/**
|
|
@@ -354,4 +654,4 @@ class RethrowHandlerPlugin {
|
|
|
354
654
|
}
|
|
355
655
|
}
|
|
356
656
|
|
|
357
|
-
export { BatchHandlerPlugin, CORSHandlerPlugin, RequestHeadersHandlerPlugin, ResponseHeadersHandlerPlugin, RethrowHandlerPlugin };
|
|
657
|
+
export { BatchHandlerPlugin, CORSHandlerPlugin, RequestCompressionHandlerPlugin, RequestHeadersHandlerPlugin, RequestLimitHandlerPlugin, ResponseCompressionHandlerPlugin, ResponseHeadersHandlerPlugin, RethrowHandlerPlugin };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.16",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.dev",
|
|
7
7
|
"repository": {
|
|
@@ -85,15 +85,14 @@
|
|
|
85
85
|
}
|
|
86
86
|
},
|
|
87
87
|
"dependencies": {
|
|
88
|
-
"@standardserver/core": "^0.0
|
|
89
|
-
"@standardserver/fetch": "^0.0
|
|
90
|
-
"@standardserver/node": "^0.0
|
|
91
|
-
"@standardserver/peer": "^0.0
|
|
88
|
+
"@standardserver/core": "^0.2.0",
|
|
89
|
+
"@standardserver/fetch": "^0.2.0",
|
|
90
|
+
"@standardserver/node": "^0.2.0",
|
|
91
|
+
"@standardserver/peer": "^0.2.0",
|
|
92
92
|
"cookie": "^2.0.1",
|
|
93
|
-
"@orpc/client": "2.0.0-beta.
|
|
94
|
-
"@orpc/
|
|
95
|
-
"@orpc/
|
|
96
|
-
"@orpc/shared": "2.0.0-beta.15"
|
|
93
|
+
"@orpc/client": "2.0.0-beta.16",
|
|
94
|
+
"@orpc/shared": "2.0.0-beta.16",
|
|
95
|
+
"@orpc/contract": "2.0.0-beta.16"
|
|
97
96
|
},
|
|
98
97
|
"devDependencies": {
|
|
99
98
|
"crossws": "^0.4.6",
|