@orpc/server 2.0.0-beta.49 → 2.0.0-beta.50

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