@orpc/server 2.0.0-beta.48 → 2.0.0-beta.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/adapters/aws-lambda/handler.d.ts +25 -0
- package/dist/adapters/aws-lambda/handler.js +24 -0
- package/dist/adapters/aws-lambda/index.d.ts +16 -0
- package/dist/adapters/aws-lambda/index.js +2 -0
- package/dist/adapters/aws-lambda/rpc-handler.d.ts +19 -0
- package/dist/adapters/aws-lambda/rpc-handler.js +18 -0
- package/dist/adapters/crossws/handler.d.ts +45 -0
- package/dist/adapters/crossws/handler.js +60 -0
- package/dist/adapters/crossws/index.d.ts +2 -0
- package/dist/adapters/crossws/index.js +2 -0
- package/dist/adapters/crossws/rpc-handler.d.ts +16 -0
- package/dist/adapters/crossws/rpc-handler.js +15 -0
- package/dist/adapters/fastify/handler.d.ts +21 -0
- package/dist/adapters/fastify/handler.js +20 -0
- package/dist/adapters/fastify/index.d.ts +2 -0
- package/dist/adapters/fastify/index.js +2 -0
- package/dist/adapters/fastify/rpc-handler.d.ts +15 -0
- package/dist/adapters/fastify/rpc-handler.js +14 -0
- package/dist/adapters/fetch/handler.d.ts +23 -0
- package/dist/adapters/fetch/handler.js +22 -0
- package/dist/adapters/fetch/index.d.ts +7 -0
- package/dist/adapters/fetch/index.js +7 -0
- package/dist/adapters/fetch/rpc-handler.d.ts +17 -0
- package/dist/adapters/fetch/rpc-handler.js +16 -0
- package/dist/adapters/message-port/handler.d.ts +64 -0
- package/dist/adapters/message-port/handler.js +86 -0
- package/dist/adapters/message-port/index.d.ts +2 -0
- package/dist/adapters/message-port/index.js +2 -0
- package/dist/adapters/message-port/rpc-handler.d.ts +16 -0
- package/dist/adapters/message-port/rpc-handler.js +15 -0
- package/dist/adapters/node/handler.d.ts +21 -0
- package/dist/adapters/node/handler.js +20 -0
- package/dist/adapters/node/index.d.ts +7 -0
- package/dist/adapters/node/index.js +7 -0
- package/dist/adapters/node/rpc-handler.d.ts +16 -0
- package/dist/adapters/node/rpc-handler.js +15 -0
- package/dist/adapters/standard/codec.d.ts +16 -0
- package/dist/adapters/standard/handler.d.ts +68 -0
- package/dist/adapters/standard/handler.js +205 -0
- package/dist/adapters/standard/index.d.ts +6 -0
- package/dist/adapters/standard/index.js +5 -0
- package/dist/adapters/standard/plugin.d.ts +32 -0
- package/dist/adapters/standard/plugin.js +16 -0
- package/dist/adapters/standard/rpc-handler-codec.d.ts +46 -0
- package/dist/adapters/standard/rpc-handler-codec.js +51 -0
- package/dist/adapters/standard/rpc-matcher.d.ts +53 -0
- package/dist/adapters/standard/rpc-matcher.js +152 -0
- package/dist/adapters/standard/utils.d.ts +10 -0
- package/dist/adapters/standard/utils.js +6 -0
- package/dist/adapters/standard-peer/index.d.ts +1 -0
- package/dist/adapters/standard-peer/index.js +1 -0
- package/dist/adapters/standard-peer/{utils.d.mts → utils.d.ts} +3 -3
- package/dist/adapters/standard-peer/utils.js +7 -0
- package/dist/adapters/websocket/handler.d.ts +59 -0
- package/dist/adapters/websocket/handler.js +85 -0
- package/dist/adapters/websocket/index.d.ts +2 -0
- package/dist/adapters/websocket/index.js +2 -0
- package/dist/adapters/websocket/rpc-handler.d.ts +16 -0
- package/dist/adapters/websocket/rpc-handler.js +15 -0
- package/dist/builder-variants.d.ts +226 -0
- package/dist/builder.d.ts +127 -0
- package/dist/builder.js +226 -0
- package/dist/context.d.ts +6 -0
- package/dist/extensions/callable.d.ts +11 -0
- package/dist/extensions/callable.js +7 -0
- package/dist/helpers/base64url.d.ts +31 -0
- package/dist/helpers/base64url.js +53 -0
- package/dist/helpers/cookie.d.ts +51 -0
- package/dist/helpers/cookie.js +69 -0
- package/dist/helpers/encryption.d.ts +28 -0
- package/dist/helpers/encryption.js +80 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.js +4 -0
- package/dist/helpers/signing.d.ts +50 -0
- package/dist/helpers/signing.js +88 -0
- package/dist/implementer-procedure.d.ts +16 -0
- package/dist/implementer-procedure.js +44 -0
- package/dist/implementer-router.d.ts +40 -0
- package/dist/implementer-router.js +117 -0
- package/dist/implementer.d.ts +16 -0
- package/dist/implementer.js +37 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.js +48 -0
- package/dist/lazy.d.ts +20 -0
- package/dist/lazy.js +31 -0
- package/dist/middleware-decorated.d.ts +10 -0
- package/dist/middleware-decorated.js +71 -0
- package/dist/middleware.d.ts +54 -0
- package/dist/plugins/batch.d.ts +102 -0
- package/dist/plugins/batch.js +405 -0
- package/dist/plugins/cors.d.ts +73 -0
- package/dist/plugins/cors.js +112 -0
- package/dist/plugins/get-method-csrf-protection.d.ts +23 -0
- package/dist/plugins/get-method-csrf-protection.js +64 -0
- package/dist/plugins/head-method.d.ts +23 -0
- package/dist/plugins/head-method.js +104 -0
- package/dist/plugins/index.d.ts +39 -0
- package/dist/plugins/index.js +29 -0
- package/dist/plugins/method-override.d.ts +38 -0
- package/dist/plugins/method-override.js +50 -0
- package/dist/plugins/request-compression.d.ts +16 -0
- package/dist/plugins/request-compression.js +82 -0
- package/dist/plugins/request-headers.d.ts +22 -0
- package/dist/plugins/request-headers.js +30 -0
- package/dist/plugins/request-limit.d.ts +30 -0
- package/dist/plugins/request-limit.js +68 -0
- package/dist/plugins/response-compression.d.ts +59 -0
- package/dist/plugins/response-compression.js +209 -0
- package/dist/plugins/response-headers.d.ts +30 -0
- package/dist/plugins/response-headers.js +47 -0
- package/dist/plugins/rethrow.d.ts +33 -0
- package/dist/plugins/rethrow.js +59 -0
- package/dist/plugins/signal-sync.d.ts +13 -0
- package/dist/plugins/signal-sync.js +91 -0
- package/dist/plugins/timeout.d.ts +37 -0
- package/dist/plugins/timeout.js +59 -0
- package/dist/procedure-client.d.ts +19 -0
- package/dist/procedure-client.js +175 -0
- package/dist/procedure-decorated.d.ts +10 -0
- package/dist/procedure-decorated.js +44 -0
- package/dist/procedure-utils.d.ts +41 -0
- package/dist/procedure-utils.js +49 -0
- package/dist/procedure.d.ts +72 -0
- package/dist/procedure.js +24 -0
- package/dist/router-client.d.ts +23 -0
- package/dist/router-client.js +43 -0
- package/dist/{router-hidden.d.mts → router-hidden.d.ts} +3 -3
- package/dist/router-hidden.js +14 -0
- package/dist/router-utils.d.ts +48 -0
- package/dist/router-utils.js +202 -0
- package/dist/router.d.ts +72 -0
- package/package.json +84 -86
- package/dist/adapters/aws-lambda/handler.d.mts +0 -25
- package/dist/adapters/aws-lambda/handler.mjs +0 -21
- package/dist/adapters/aws-lambda/index.d.mts +0 -16
- package/dist/adapters/aws-lambda/index.mjs +0 -2
- package/dist/adapters/aws-lambda/rpc-handler.d.mts +0 -18
- package/dist/adapters/aws-lambda/rpc-handler.mjs +0 -10
- package/dist/adapters/crossws/handler.d.mts +0 -41
- package/dist/adapters/crossws/handler.mjs +0 -38
- package/dist/adapters/crossws/index.d.mts +0 -2
- package/dist/adapters/crossws/index.mjs +0 -2
- package/dist/adapters/crossws/rpc-handler.d.mts +0 -15
- package/dist/adapters/crossws/rpc-handler.mjs +0 -10
- package/dist/adapters/fastify/handler.d.mts +0 -21
- package/dist/adapters/fastify/handler.mjs +0 -20
- package/dist/adapters/fastify/index.d.mts +0 -2
- package/dist/adapters/fastify/index.mjs +0 -2
- package/dist/adapters/fastify/rpc-handler.d.mts +0 -14
- package/dist/adapters/fastify/rpc-handler.mjs +0 -10
- package/dist/adapters/fetch/handler.d.mts +0 -23
- package/dist/adapters/fetch/handler.mjs +0 -22
- package/dist/adapters/fetch/index.d.mts +0 -7
- package/dist/adapters/fetch/index.mjs +0 -5
- package/dist/adapters/fetch/rpc-handler.d.mts +0 -16
- package/dist/adapters/fetch/rpc-handler.mjs +0 -10
- package/dist/adapters/message-port/handler.d.mts +0 -57
- package/dist/adapters/message-port/handler.mjs +0 -62
- package/dist/adapters/message-port/index.d.mts +0 -2
- package/dist/adapters/message-port/index.mjs +0 -2
- package/dist/adapters/message-port/rpc-handler.d.mts +0 -15
- package/dist/adapters/message-port/rpc-handler.mjs +0 -10
- package/dist/adapters/node/handler.d.mts +0 -21
- package/dist/adapters/node/handler.mjs +0 -20
- package/dist/adapters/node/index.d.mts +0 -7
- package/dist/adapters/node/index.mjs +0 -5
- package/dist/adapters/node/rpc-handler.d.mts +0 -15
- package/dist/adapters/node/rpc-handler.mjs +0 -10
- package/dist/adapters/standard/codec.d.mts +0 -16
- package/dist/adapters/standard/handler.d.mts +0 -68
- package/dist/adapters/standard/handler.mjs +0 -185
- package/dist/adapters/standard/index.d.mts +0 -6
- package/dist/adapters/standard/index.mjs +0 -5
- package/dist/adapters/standard/plugin.d.mts +0 -32
- package/dist/adapters/standard/plugin.mjs +0 -16
- package/dist/adapters/standard/rpc-handler-codec.d.mts +0 -42
- package/dist/adapters/standard/rpc-handler-codec.mjs +0 -51
- package/dist/adapters/standard/rpc-matcher.d.mts +0 -53
- package/dist/adapters/standard/rpc-matcher.mjs +0 -140
- package/dist/adapters/standard/utils.d.mts +0 -10
- package/dist/adapters/standard/utils.mjs +0 -6
- package/dist/adapters/standard-peer/index.d.mts +0 -1
- package/dist/adapters/standard-peer/index.mjs +0 -1
- package/dist/adapters/standard-peer/utils.mjs +0 -11
- package/dist/adapters/websocket/handler.d.mts +0 -55
- package/dist/adapters/websocket/handler.mjs +0 -55
- package/dist/adapters/websocket/index.d.mts +0 -2
- package/dist/adapters/websocket/index.mjs +0 -2
- package/dist/adapters/websocket/rpc-handler.d.mts +0 -15
- package/dist/adapters/websocket/rpc-handler.mjs +0 -10
- package/dist/builder-variants.d.mts +0 -267
- package/dist/builder.d.mts +0 -141
- package/dist/builder.mjs +0 -145
- package/dist/context.d.mts +0 -13
- package/dist/extensions/callable.d.mts +0 -17
- package/dist/extensions/callable.mjs +0 -7
- package/dist/helpers/base64url.d.mts +0 -31
- package/dist/helpers/base64url.mjs +0 -25
- package/dist/helpers/cookie.d.mts +0 -50
- package/dist/helpers/cookie.mjs +0 -35
- package/dist/helpers/encryption.d.mts +0 -28
- package/dist/helpers/encryption.mjs +0 -62
- package/dist/helpers/index.d.mts +0 -4
- package/dist/helpers/index.mjs +0 -4
- package/dist/helpers/signing.d.mts +0 -50
- package/dist/helpers/signing.mjs +0 -43
- package/dist/implementer-procedure.d.mts +0 -36
- package/dist/implementer-procedure.mjs +0 -38
- package/dist/implementer-router.d.mts +0 -78
- package/dist/implementer-router.mjs +0 -77
- package/dist/implementer.d.mts +0 -22
- package/dist/implementer.mjs +0 -30
- package/dist/index.d.mts +0 -57
- package/dist/index.mjs +0 -30
- package/dist/lazy.d.mts +0 -20
- package/dist/lazy.mjs +0 -22
- package/dist/middleware-decorated.d.mts +0 -27
- package/dist/middleware-decorated.mjs +0 -65
- package/dist/middleware.d.mts +0 -70
- package/dist/plugins/batch.d.mts +0 -104
- package/dist/plugins/batch.mjs +0 -373
- package/dist/plugins/cors.d.mts +0 -67
- package/dist/plugins/cors.mjs +0 -134
- package/dist/plugins/get-method-csrf-protection.d.mts +0 -23
- package/dist/plugins/get-method-csrf-protection.mjs +0 -45
- package/dist/plugins/head-method.d.mts +0 -23
- package/dist/plugins/head-method.mjs +0 -95
- package/dist/plugins/index.d.mts +0 -39
- package/dist/plugins/index.mjs +0 -23
- package/dist/plugins/method-override.d.mts +0 -38
- package/dist/plugins/method-override.mjs +0 -50
- package/dist/plugins/request-compression.d.mts +0 -16
- package/dist/plugins/request-compression.mjs +0 -67
- package/dist/plugins/request-headers.d.mts +0 -22
- package/dist/plugins/request-headers.mjs +0 -21
- package/dist/plugins/request-limit.d.mts +0 -30
- package/dist/plugins/request-limit.mjs +0 -53
- package/dist/plugins/response-compression.d.mts +0 -59
- package/dist/plugins/response-compression.mjs +0 -172
- package/dist/plugins/response-headers.d.mts +0 -30
- package/dist/plugins/response-headers.mjs +0 -34
- package/dist/plugins/rethrow.d.mts +0 -33
- package/dist/plugins/rethrow.mjs +0 -46
- package/dist/plugins/signal-sync.d.mts +0 -13
- package/dist/plugins/signal-sync.mjs +0 -84
- package/dist/plugins/timeout.d.mts +0 -37
- package/dist/plugins/timeout.mjs +0 -56
- package/dist/procedure-client.d.mts +0 -41
- package/dist/procedure-client.mjs +0 -157
- package/dist/procedure-decorated.d.mts +0 -20
- package/dist/procedure-decorated.mjs +0 -41
- package/dist/procedure-utils.d.mts +0 -52
- package/dist/procedure-utils.mjs +0 -33
- package/dist/procedure.d.mts +0 -93
- package/dist/procedure.mjs +0 -20
- package/dist/router-client.d.mts +0 -27
- package/dist/router-client.mjs +0 -35
- package/dist/router-hidden.mjs +0 -12
- package/dist/router-utils.d.mts +0 -59
- package/dist/router-utils.mjs +0 -191
- package/dist/router.d.mts +0 -61
- /package/dist/adapters/standard/{codec.mjs → codec.js} +0 -0
- /package/dist/{builder-variants.mjs → builder-variants.js} +0 -0
- /package/dist/{constants.d.mts → constants.d.ts} +0 -0
- /package/dist/{constants.mjs → constants.js} +0 -0
- /package/dist/{context.mjs → context.js} +0 -0
- /package/dist/{middleware.mjs → middleware.js} +0 -0
- /package/dist/{router.mjs → router.js} +0 -0
|
@@ -1,172 +0,0 @@
|
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
const AVG_BYTES_PER_CHAR = 1.2;
|
|
7
|
-
|
|
8
|
-
export class ResponseCompressionHandlerPlugin {
|
|
9
|
-
name = "~response-compression";
|
|
10
|
-
|
|
11
|
-
after = ["~batch", "~response-headers"];
|
|
12
|
-
encodings;
|
|
13
|
-
threshold;
|
|
14
|
-
isCompressibleContentType;
|
|
15
|
-
constructor(options = {}) {
|
|
16
|
-
this.encodings = options.encodings ?? ["gzip", "deflate"];
|
|
17
|
-
this.threshold = options.threshold ?? 1024;
|
|
18
|
-
this.isCompressibleContentType = options.isCompressibleContentType ?? isCompressibleContentType;
|
|
19
|
-
}
|
|
20
|
-
init(options) {
|
|
21
|
-
const routingInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
22
|
-
const result = await next();
|
|
23
|
-
if (!result.matched) {
|
|
24
|
-
return result;
|
|
25
|
-
}
|
|
26
|
-
const response = result.response;
|
|
27
|
-
const encoding = negotiateResponseCompressionEncoding(response, interceptorOptions.request.headers, this.encodings);
|
|
28
|
-
if (encoding === undefined) {
|
|
29
|
-
return result;
|
|
30
|
-
}
|
|
31
|
-
const body = response.body;
|
|
32
|
-
const headers = response.headers;
|
|
33
|
-
if (body instanceof ReadableStream) {
|
|
34
|
-
const contentLength = Number(flattenStandardHeader(headers["content-length"]));
|
|
35
|
-
if ((!Number.isFinite(contentLength) || contentLength >= this.threshold) && this.isCompressibleContentType(flattenStandardHeader(headers["content-type"]), interceptorOptions)) {
|
|
36
|
-
return {
|
|
37
|
-
...result,
|
|
38
|
-
response: {
|
|
39
|
-
...response,
|
|
40
|
-
body: body.pipeThrough(new CompressionStream(encoding)),
|
|
41
|
-
headers: toCompressedHeaders(headers, encoding)
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
} else if (body instanceof Blob) {
|
|
46
|
-
|
|
47
|
-
const contentType = headers["content-type"] ?? body.type;
|
|
48
|
-
if ((!Number.isFinite(body.size) || body.size >= this.threshold) && this.isCompressibleContentType(flattenStandardHeader(contentType), interceptorOptions)) {
|
|
49
|
-
const contentDisposition = headers["content-disposition"] ?? generateContentDisposition(body instanceof File ? body.name : "blob");
|
|
50
|
-
return {
|
|
51
|
-
...result,
|
|
52
|
-
response: {
|
|
53
|
-
...response,
|
|
54
|
-
body: body.stream().pipeThrough(new CompressionStream(encoding)),
|
|
55
|
-
headers: {
|
|
56
|
-
...toCompressedHeaders(headers, encoding),
|
|
57
|
-
"standard-server": headers["standard-server"] ?? "file",
|
|
58
|
-
"content-type": contentType,
|
|
59
|
-
"content-disposition": contentDisposition
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
} else if (body instanceof FormData) {
|
|
65
|
-
const PART_OVERHEAD = 64;
|
|
66
|
-
let contentLength = 0;
|
|
67
|
-
for (const [key, value] of body) {
|
|
68
|
-
contentLength += PART_OVERHEAD + key.length;
|
|
69
|
-
if (value instanceof Blob) {
|
|
70
|
-
if (!Number.isFinite(value.size)) {
|
|
71
|
-
if (!this.isCompressibleContentType(value.type, interceptorOptions)) {
|
|
72
|
-
|
|
73
|
-
contentLength = -Infinity;
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
contentLength = Infinity;
|
|
78
|
-
} else {
|
|
79
|
-
contentLength += this.isCompressibleContentType(value.type, interceptorOptions) ? value.size : -value.size;
|
|
80
|
-
}
|
|
81
|
-
} else {
|
|
82
|
-
contentLength += value.length * AVG_BYTES_PER_CHAR;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
if (contentLength >= this.threshold) {
|
|
86
|
-
const res = new Response(body);
|
|
87
|
-
const compressedStream = res.body.pipeThrough(new CompressionStream(encoding));
|
|
88
|
-
return {
|
|
89
|
-
...result,
|
|
90
|
-
response: {
|
|
91
|
-
...response,
|
|
92
|
-
body: compressedStream,
|
|
93
|
-
headers: {
|
|
94
|
-
...toCompressedHeaders(headers, encoding),
|
|
95
|
-
"standard-server": [],
|
|
96
|
-
"content-type": res.headers.get("content-type")
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
} else if (body instanceof URLSearchParams) {
|
|
102
|
-
const string = body.toString();
|
|
103
|
-
if (string.length * AVG_BYTES_PER_CHAR >= this.threshold) {
|
|
104
|
-
const [, bodyHeaders] = toFetchBody(body, headers);
|
|
105
|
-
return {
|
|
106
|
-
...result,
|
|
107
|
-
response: {
|
|
108
|
-
...response,
|
|
109
|
-
body: new Blob([string]).stream().pipeThrough(new CompressionStream(encoding)),
|
|
110
|
-
headers: {
|
|
111
|
-
...toCompressedHeaders(bodyHeaders, encoding),
|
|
112
|
-
|
|
113
|
-
"standard-server": bodyHeaders["standard-server"] ?? []
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
} else if (body !== undefined && !isAsyncIteratorObject(body)) {
|
|
119
|
-
const [string, bodyHeaders] = toFetchBody(body, headers);
|
|
120
|
-
if (string.length * AVG_BYTES_PER_CHAR >= this.threshold) {
|
|
121
|
-
return {
|
|
122
|
-
...result,
|
|
123
|
-
response: {
|
|
124
|
-
...response,
|
|
125
|
-
body: new Blob([string]).stream().pipeThrough(new CompressionStream(encoding)),
|
|
126
|
-
headers: {
|
|
127
|
-
...toCompressedHeaders(bodyHeaders, encoding),
|
|
128
|
-
"standard-server": bodyHeaders["standard-server"] ?? []
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return result;
|
|
135
|
-
};
|
|
136
|
-
return {
|
|
137
|
-
...options,
|
|
138
|
-
routingInterceptors: [routingInterceptor, ...toArray(options.routingInterceptors)]
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export function negotiateResponseCompressionEncoding(response, requestHeaders, encodings) {
|
|
144
|
-
if (flattenStandardHeader(response.headers["content-encoding"]) !== undefined) {
|
|
145
|
-
return undefined;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (response.status === 206 || flattenStandardHeader(response.headers["content-range"]) !== undefined) {
|
|
149
|
-
return undefined;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (isNoTransformCacheControl(flattenStandardHeader(response.headers["cache-control"]))) {
|
|
153
|
-
return undefined;
|
|
154
|
-
}
|
|
155
|
-
const acceptEncodings = parseAcceptEncodingQualities(flattenStandardHeader(requestHeaders["accept-encoding"]));
|
|
156
|
-
return encodings.find((encoding) => isAcceptableEncoding(acceptEncodings, encoding));
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export function toCompressedHeaders(headers, encoding) {
|
|
160
|
-
return {
|
|
161
|
-
...headers,
|
|
162
|
-
"content-length": [],
|
|
163
|
-
"content-encoding": encoding,
|
|
164
|
-
"vary": varyByAcceptEncoding(flattenStandardHeader(headers.vary)),
|
|
165
|
-
|
|
166
|
-
"etag": weakenEtag(flattenStandardHeader(headers.etag)),
|
|
167
|
-
"accept-ranges": []
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
function weakenEtag(etag) {
|
|
171
|
-
return etag === undefined || etag.startsWith("W/") ? etag : `W/${etag}`;
|
|
172
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { StandardHandlerOptions, StandardHandlerPlugin } from "../adapters/standard/index.mjs";
|
|
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
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { toArray } from "@orpc/shared";
|
|
2
|
-
import { mergeStandardHeaders } from "@standard-server/core";
|
|
3
|
-
import { toStandardHeaders } from "@standard-server/fetch";
|
|
4
|
-
|
|
5
|
-
export class ResponseHeadersHandlerPlugin {
|
|
6
|
-
name = "~response-headers";
|
|
7
|
-
|
|
8
|
-
before = ["~batch"];
|
|
9
|
-
init(options) {
|
|
10
|
-
return {
|
|
11
|
-
...options,
|
|
12
|
-
routingInterceptors: [async (interceptorOptions) => {
|
|
13
|
-
const resHeaders = new Headers(interceptorOptions.context.resHeaders);
|
|
14
|
-
const result = await interceptorOptions.next({
|
|
15
|
-
...interceptorOptions,
|
|
16
|
-
context: {
|
|
17
|
-
...interceptorOptions.context,
|
|
18
|
-
resHeaders
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
if (!result.response) {
|
|
22
|
-
return result;
|
|
23
|
-
}
|
|
24
|
-
return {
|
|
25
|
-
...result,
|
|
26
|
-
response: {
|
|
27
|
-
...result.response,
|
|
28
|
-
headers: mergeStandardHeaders(result.response.headers, toStandardHeaders(resHeaders))
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
}, ...toArray(options.routingInterceptors)]
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { ThrowableError } from "@orpc/shared";
|
|
2
|
-
import type { StandardHandlerInterceptorOptions, StandardHandlerOptions, StandardHandlerPlugin } from "../adapters/standard/index.mjs";
|
|
3
|
-
import type { Context } from "../context.mjs";
|
|
4
|
-
export interface RethrowHandlerPluginOptions<T extends Context> {
|
|
5
|
-
/**
|
|
6
|
-
* Decide which errors should be rethrown.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```ts
|
|
10
|
-
* const rethrowPlugin = new RethrowHandlerPlugin({
|
|
11
|
-
* filter: (error) => {
|
|
12
|
-
* // Rethrow all non-ORPCError errors
|
|
13
|
-
* return !(error instanceof ORPCError)
|
|
14
|
-
* }
|
|
15
|
-
* })
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
filter: (error: ThrowableError, options: StandardHandlerInterceptorOptions<T>) => boolean;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* The plugin can bypass oRPC's built-in error handling
|
|
22
|
-
* and rethrow matching errors directly to your framework's error handling mechanism
|
|
23
|
-
* (e.g., NestJS exception filters, Express error middleware).
|
|
24
|
-
*
|
|
25
|
-
* @see {@link https://orpc.dev/docs/plugins/rethrow | Rethrow Handler Plugin}
|
|
26
|
-
*/
|
|
27
|
-
export declare class RethrowHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
28
|
-
name: string;
|
|
29
|
-
private readonly filter;
|
|
30
|
-
private readonly CONTEXT_SYMBOL;
|
|
31
|
-
constructor(options: RethrowHandlerPluginOptions<T>);
|
|
32
|
-
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
33
|
-
}
|
package/dist/plugins/rethrow.mjs
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { toArray } from "@orpc/shared";
|
|
2
|
-
|
|
3
|
-
export class RethrowHandlerPlugin {
|
|
4
|
-
name = "~rethrow";
|
|
5
|
-
filter;
|
|
6
|
-
CONTEXT_SYMBOL = Symbol("ORPC_RETHROW_HANDLER_PLUGIN_CONTEXT");
|
|
7
|
-
constructor(options) {
|
|
8
|
-
this.filter = options.filter;
|
|
9
|
-
}
|
|
10
|
-
init(options) {
|
|
11
|
-
const routingInterceptor = async (options) => {
|
|
12
|
-
const pluginContext = {};
|
|
13
|
-
const result = await options.next({
|
|
14
|
-
...options,
|
|
15
|
-
context: {
|
|
16
|
-
...options.context,
|
|
17
|
-
[this.CONTEXT_SYMBOL]: pluginContext
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
if (pluginContext.error) {
|
|
21
|
-
throw pluginContext.error.value;
|
|
22
|
-
}
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
const interceptor = async (options) => {
|
|
26
|
-
const pluginContext = options.context[this.CONTEXT_SYMBOL];
|
|
27
|
-
if (!pluginContext) {
|
|
28
|
-
throw new TypeError("[RethrowHandlerPlugin] Rethrow handler context has been corrupted or modified by another plugin or interceptor");
|
|
29
|
-
}
|
|
30
|
-
try {
|
|
31
|
-
|
|
32
|
-
return await options.next();
|
|
33
|
-
} catch (error) {
|
|
34
|
-
if (this.filter(error, options)) {
|
|
35
|
-
pluginContext.error = { value: error };
|
|
36
|
-
}
|
|
37
|
-
throw error;
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
return {
|
|
41
|
-
...options,
|
|
42
|
-
routingInterceptors: [...toArray(options.routingInterceptors), routingInterceptor],
|
|
43
|
-
interceptors: [interceptor, ...toArray(options.interceptors)]
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { StandardHandlerOptions, StandardHandlerPlugin } from "../adapters/standard/index.mjs";
|
|
2
|
-
import type { Context } from "../context.mjs";
|
|
3
|
-
/**
|
|
4
|
-
* Keeps the request signal and a streaming output (async iterator object or readable stream) in sync:
|
|
5
|
-
* cancelling the output aborts the signal with an `AbortError`,
|
|
6
|
-
* and aborting the signal ends the output with the abort reason and cancels it.
|
|
7
|
-
*
|
|
8
|
-
* @see {@link https://orpc.dev/docs/plugins/signal-sync | Signal Sync Plugin}
|
|
9
|
-
*/
|
|
10
|
-
export declare class SignalSyncHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
11
|
-
name: string;
|
|
12
|
-
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
13
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { AbortError, anyAbortSignal, AsyncIteratorClass, isAsyncIteratorObject, override, runWithSignal, toArray } from "@orpc/shared";
|
|
2
|
-
|
|
3
|
-
export class SignalSyncHandlerPlugin {
|
|
4
|
-
name = "~signal-sync";
|
|
5
|
-
init(options) {
|
|
6
|
-
const clientInterceptor = async (interceptorOptions) => {
|
|
7
|
-
const controller = new AbortController();
|
|
8
|
-
const signal = anyAbortSignal([interceptorOptions.signal, controller.signal]);
|
|
9
|
-
const cancel = () => controller.abort(new AbortError("The response body was cancelled"));
|
|
10
|
-
const output = await interceptorOptions.next({
|
|
11
|
-
...interceptorOptions,
|
|
12
|
-
signal
|
|
13
|
-
});
|
|
14
|
-
if (isAsyncIteratorObject(output)) {
|
|
15
|
-
return override(output, syncAsyncIterator(output, signal, cancel));
|
|
16
|
-
}
|
|
17
|
-
if (output instanceof ReadableStream) {
|
|
18
|
-
return override(output, syncReadableStream(output, signal, cancel));
|
|
19
|
-
}
|
|
20
|
-
return output;
|
|
21
|
-
};
|
|
22
|
-
return {
|
|
23
|
-
...options,
|
|
24
|
-
clientInterceptors: [clientInterceptor, ...toArray(options.clientInterceptors)]
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
function listenAbort(signal, onAbort) {
|
|
29
|
-
if (signal.aborted) {
|
|
30
|
-
onAbort();
|
|
31
|
-
} else {
|
|
32
|
-
signal.addEventListener("abort", onAbort, { once: true });
|
|
33
|
-
}
|
|
34
|
-
return () => signal.removeEventListener("abort", onAbort);
|
|
35
|
-
}
|
|
36
|
-
function syncAsyncIterator(iterator, signal, cancel) {
|
|
37
|
-
let cancelling;
|
|
38
|
-
const unlisten = listenAbort(signal, () => {
|
|
39
|
-
cancelling = (async () => iterator.return?.())();
|
|
40
|
-
cancelling.catch(() => {});
|
|
41
|
-
});
|
|
42
|
-
return new AsyncIteratorClass(() => runWithSignal(signal, () => iterator.next()), async ({ kind }) => {
|
|
43
|
-
if (kind === "cancelled") {
|
|
44
|
-
cancel();
|
|
45
|
-
await cancelling;
|
|
46
|
-
}
|
|
47
|
-
unlisten();
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
function syncReadableStream(stream, signal, cancel) {
|
|
51
|
-
const reader = stream.getReader();
|
|
52
|
-
let cancelling;
|
|
53
|
-
let unlisten;
|
|
54
|
-
return new ReadableStream({
|
|
55
|
-
start(controller) {
|
|
56
|
-
unlisten = listenAbort(signal, () => {
|
|
57
|
-
controller.error(signal.reason);
|
|
58
|
-
cancelling = reader.cancel(signal.reason);
|
|
59
|
-
cancelling.catch(() => {});
|
|
60
|
-
});
|
|
61
|
-
},
|
|
62
|
-
async pull(controller) {
|
|
63
|
-
try {
|
|
64
|
-
const { done, value } = await reader.read();
|
|
65
|
-
if (signal.aborted) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
if (done) {
|
|
69
|
-
unlisten();
|
|
70
|
-
controller.close();
|
|
71
|
-
} else {
|
|
72
|
-
controller.enqueue(value);
|
|
73
|
-
}
|
|
74
|
-
} catch (error) {
|
|
75
|
-
unlisten();
|
|
76
|
-
throw error;
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
async cancel() {
|
|
80
|
-
cancel();
|
|
81
|
-
await cancelling;
|
|
82
|
-
}
|
|
83
|
-
}, { highWaterMark: 0 });
|
|
84
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { Value } from "@orpc/shared";
|
|
2
|
-
import type { StandardHandlerInterceptorOptions, StandardHandlerOptions, StandardHandlerPlugin } from "../adapters/standard/index.mjs";
|
|
3
|
-
import type { Context } from "../context.mjs";
|
|
4
|
-
export interface TimeoutHandlerPluginOptions<T extends Context> {
|
|
5
|
-
/**
|
|
6
|
-
* Timeout in milliseconds before the request signal is aborted.
|
|
7
|
-
* This only covers producing the response, use `streamingTimeout`
|
|
8
|
-
* to limit streaming response bodies.
|
|
9
|
-
* Use `null` or `undefined` to disable the timeout.
|
|
10
|
-
*/
|
|
11
|
-
timeout: Value<number | null | undefined, [options: StandardHandlerInterceptorOptions<T>]>;
|
|
12
|
-
/**
|
|
13
|
-
* Timeout in milliseconds for the full duration of a streaming response body
|
|
14
|
-
* (async iterator object or readable stream), measured from when the response is produced.
|
|
15
|
-
* When exceeded, the request signal is aborted and the body ends
|
|
16
|
-
* once its producer honors the signal.
|
|
17
|
-
* Usually higher than `timeout`.
|
|
18
|
-
* Use `null` or `undefined` to disable the timeout.
|
|
19
|
-
*
|
|
20
|
-
* @default undefined (streaming responses run without limit)
|
|
21
|
-
*/
|
|
22
|
-
streamingTimeout?: Value<number | null | undefined, [options: StandardHandlerInterceptorOptions<T>]> | undefined;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* The Timeout Handler Plugin aborts the request signal with an `AbortError`
|
|
26
|
-
* when handling exceeds a configured timeout. It only aborts the signal,
|
|
27
|
-
* the procedure must honor it to stop early and produce the response.
|
|
28
|
-
*
|
|
29
|
-
* @see {@link https://orpc.dev/docs/plugins/timeout | Timeout Plugin}
|
|
30
|
-
*/
|
|
31
|
-
export declare class TimeoutHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
32
|
-
private readonly timeout;
|
|
33
|
-
private readonly streamingTimeout;
|
|
34
|
-
name: string;
|
|
35
|
-
constructor(options: NoInfer<TimeoutHandlerPluginOptions<T>>);
|
|
36
|
-
init(options: StandardHandlerOptions<T>): StandardHandlerOptions<T>;
|
|
37
|
-
}
|
package/dist/plugins/timeout.mjs
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { AbortError, anyAbortSignal, isAsyncIteratorObject, override, toArray, value, wrapAsyncIterator, wrapReadableStream } from "@orpc/shared";
|
|
2
|
-
|
|
3
|
-
export class TimeoutHandlerPlugin {
|
|
4
|
-
timeout;
|
|
5
|
-
streamingTimeout;
|
|
6
|
-
name = "~timeout";
|
|
7
|
-
constructor(options) {
|
|
8
|
-
this.timeout = options.timeout;
|
|
9
|
-
this.streamingTimeout = options.streamingTimeout;
|
|
10
|
-
}
|
|
11
|
-
init(options) {
|
|
12
|
-
const interceptor = async (interceptorOptions) => {
|
|
13
|
-
const timeoutMs = value(this.timeout, interceptorOptions);
|
|
14
|
-
const streamingTimeoutMs = value(this.streamingTimeout, interceptorOptions);
|
|
15
|
-
const hasTimeout = timeoutMs !== null && timeoutMs !== undefined;
|
|
16
|
-
const hasStreamingTimeout = streamingTimeoutMs !== null && streamingTimeoutMs !== undefined;
|
|
17
|
-
if (!hasTimeout && !hasStreamingTimeout) {
|
|
18
|
-
return interceptorOptions.next();
|
|
19
|
-
}
|
|
20
|
-
const controller = new AbortController();
|
|
21
|
-
const abortWithTimeout = (ms) => {
|
|
22
|
-
controller.abort(new AbortError(`Request timed out after ${ms}ms`));
|
|
23
|
-
};
|
|
24
|
-
const timeoutId = hasTimeout ? setTimeout(abortWithTimeout, timeoutMs, timeoutMs) : undefined;
|
|
25
|
-
const signal = anyAbortSignal([interceptorOptions.request.signal, controller.signal]);
|
|
26
|
-
try {
|
|
27
|
-
const response = await interceptorOptions.next({
|
|
28
|
-
...interceptorOptions,
|
|
29
|
-
request: {
|
|
30
|
-
...interceptorOptions.request,
|
|
31
|
-
signal
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
if (hasStreamingTimeout) {
|
|
35
|
-
const body = response.body;
|
|
36
|
-
const isIterator = isAsyncIteratorObject(body);
|
|
37
|
-
if (isIterator || body instanceof ReadableStream) {
|
|
38
|
-
const streamingTimeoutId = setTimeout(abortWithTimeout, streamingTimeoutMs, streamingTimeoutMs);
|
|
39
|
-
const onFinish = () => clearTimeout(streamingTimeoutId);
|
|
40
|
-
return {
|
|
41
|
-
...response,
|
|
42
|
-
body: isIterator ? override(body, wrapAsyncIterator(body, { onFinish })) : override(body, wrapReadableStream(body, { onFinish }))
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return response;
|
|
47
|
-
} finally {
|
|
48
|
-
clearTimeout(timeoutId);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
return {
|
|
52
|
-
...options,
|
|
53
|
-
interceptors: [interceptor, ...toArray(options.interceptors)]
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { Client, ClientContext } from "@orpc/client";
|
|
2
|
-
import type { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, ORPCErrorConstructorMap, ORPCErrorFromErrorMap } from "@orpc/contract";
|
|
3
|
-
import type { Interceptor, MaybeOptionalOptions, Promisable, PromiseWithError, ThrowableError, Value } from "@orpc/shared";
|
|
4
|
-
import type { Context } from "./context.mjs";
|
|
5
|
-
import type { Lazyable } from "./lazy.mjs";
|
|
6
|
-
import type { Procedure, ProcedureHandlerOptions } from "./procedure.mjs";
|
|
7
|
-
export type ProcedureClient<
|
|
8
|
-
TClientContext extends ClientContext,
|
|
9
|
-
TInputSchema extends AnySchema,
|
|
10
|
-
TOutputSchema extends AnySchema,
|
|
11
|
-
TErrorMap extends ErrorMap
|
|
12
|
-
> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError>;
|
|
13
|
-
export interface ProcedureClientInterceptorOptions<
|
|
14
|
-
TInitialContext extends Context,
|
|
15
|
-
TErrorMap extends ErrorMap
|
|
16
|
-
> extends ProcedureHandlerOptions<TInitialContext, unknown, ORPCErrorConstructorMap<TErrorMap>> {}
|
|
17
|
-
export type ProcedureClientInterceptor<
|
|
18
|
-
TInitialContext extends Context,
|
|
19
|
-
TOutputSchema extends AnySchema,
|
|
20
|
-
TErrorMap extends ErrorMap
|
|
21
|
-
> = Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError>>;
|
|
22
|
-
export type ProcedureClientOptions<
|
|
23
|
-
TInitialContext extends Context,
|
|
24
|
-
TOutputSchema extends AnySchema,
|
|
25
|
-
TErrorMap extends ErrorMap,
|
|
26
|
-
TClientContext extends ClientContext
|
|
27
|
-
> = {
|
|
28
|
-
path?: string[] | undefined;
|
|
29
|
-
interceptors?: ProcedureClientInterceptor<TInitialContext, TOutputSchema, TErrorMap>[] | undefined;
|
|
30
|
-
} & (object extends TInitialContext ? {
|
|
31
|
-
context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]> | undefined;
|
|
32
|
-
} : {
|
|
33
|
-
context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
34
|
-
});
|
|
35
|
-
export declare function createProcedureClient<
|
|
36
|
-
TInitialContext extends Context,
|
|
37
|
-
TInputSchema extends AnySchema,
|
|
38
|
-
TOutputSchema extends AnySchema,
|
|
39
|
-
TErrorMap extends ErrorMap,
|
|
40
|
-
TClientContext extends ClientContext = object
|
|
41
|
-
>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap>>, ...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
|