@orpc/client 2.0.0-beta.5 → 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.
- package/README.md +137 -102
- package/dist/adapters/fetch/index.d.ts +3 -65
- package/dist/adapters/fetch/index.js +2 -0
- package/dist/adapters/fetch/rpc-link.d.ts +14 -0
- package/dist/adapters/fetch/rpc-link.js +17 -0
- package/dist/adapters/fetch/transport.d.ts +32 -0
- package/dist/adapters/fetch/transport.js +51 -0
- package/dist/adapters/message-port/index.d.ts +4 -75
- package/dist/adapters/message-port/index.js +3 -0
- package/dist/adapters/message-port/message-port.d.ts +29 -0
- package/dist/adapters/message-port/message-port.js +38 -0
- package/dist/adapters/message-port/rpc-link.d.ts +15 -0
- package/dist/adapters/message-port/rpc-link.js +18 -0
- package/dist/adapters/message-port/transport.d.ts +35 -0
- package/dist/adapters/message-port/transport.js +38 -0
- package/dist/adapters/standard/codec.d.ts +14 -0
- package/dist/adapters/standard/index.d.ts +6 -61
- package/dist/adapters/standard/index.js +2 -0
- package/dist/adapters/standard/link.d.ts +40 -0
- package/dist/adapters/standard/link.js +101 -0
- package/dist/adapters/standard/plugin.d.ts +26 -0
- package/dist/adapters/standard/plugin.js +14 -0
- package/dist/adapters/standard/rpc-link-codec.d.ts +56 -0
- package/dist/adapters/standard/rpc-link-codec.js +100 -0
- package/dist/adapters/standard/transport.d.ts +14 -0
- package/dist/adapters/websocket/index.d.ts +3 -121
- package/dist/adapters/websocket/index.js +2 -0
- package/dist/adapters/websocket/rpc-link.d.ts +14 -0
- package/dist/adapters/websocket/rpc-link.js +17 -0
- package/dist/adapters/websocket/transport.d.ts +116 -0
- package/dist/adapters/websocket/transport.js +105 -0
- package/dist/async-iterator-object.d.ts +2 -0
- package/dist/async-iterator-object.js +28 -0
- package/dist/client-safe.d.ts +16 -0
- package/dist/client-safe.js +31 -0
- package/dist/client.d.ts +38 -0
- package/dist/client.js +37 -0
- package/dist/consts.d.ts +10 -0
- package/dist/consts.js +20 -0
- package/dist/dynamic-link.d.ts +13 -0
- package/dist/dynamic-link.js +17 -0
- package/dist/error-utils.d.ts +20 -0
- package/dist/error-utils.js +54 -0
- package/dist/error.d.ts +132 -0
- package/dist/error.js +166 -0
- package/dist/index.d.ts +14 -155
- package/dist/index.js +12 -0
- package/dist/plugins/batch.d.ts +152 -0
- package/dist/plugins/batch.js +330 -0
- package/dist/plugins/cookie-jar.d.ts +85 -0
- package/dist/plugins/cookie-jar.js +231 -0
- package/dist/plugins/dedupe.d.ts +60 -0
- package/dist/plugins/dedupe.js +183 -0
- package/dist/plugins/index.d.ts +9 -242
- package/dist/plugins/index.js +8 -0
- package/dist/plugins/request-compression.d.ts +47 -0
- package/dist/plugins/request-compression.js +132 -0
- package/dist/plugins/response-compression.d.ts +31 -0
- package/dist/plugins/response-compression.js +67 -0
- package/dist/plugins/retry-after.d.ts +43 -0
- package/dist/plugins/retry-after.js +63 -0
- package/dist/plugins/retry.d.ts +84 -0
- package/dist/plugins/retry.js +121 -0
- package/dist/plugins/timeout.d.ts +27 -0
- package/dist/plugins/timeout.js +42 -0
- package/dist/rpc-json-serializer.d.ts +99 -0
- package/dist/rpc-json-serializer.js +347 -0
- package/dist/rpc-serializer.d.ts +31 -0
- package/dist/rpc-serializer.js +102 -0
- package/dist/types.d.ts +85 -0
- package/dist/utils.d.ts +36 -0
- package/dist/utils.js +67 -0
- package/package.json +47 -35
- package/dist/adapters/fetch/index.d.mts +0 -65
- package/dist/adapters/fetch/index.mjs +0 -68
- package/dist/adapters/message-port/index.d.mts +0 -75
- package/dist/adapters/message-port/index.mjs +0 -91
- package/dist/adapters/standard/index.d.mts +0 -61
- package/dist/adapters/standard/index.mjs +0 -6
- package/dist/adapters/websocket/index.d.mts +0 -121
- package/dist/adapters/websocket/index.mjs +0 -110
- package/dist/index.d.mts +0 -155
- package/dist/index.mjs +0 -154
- package/dist/plugins/index.d.mts +0 -242
- package/dist/plugins/index.mjs +0 -574
- package/dist/shared/client.8ug8I-zu.d.mts +0 -167
- package/dist/shared/client.8ug8I-zu.d.ts +0 -167
- package/dist/shared/client.BdItY5DT.d.mts +0 -111
- package/dist/shared/client.BdItY5DT.d.ts +0 -111
- package/dist/shared/client.CPF3hX6O.d.ts +0 -96
- package/dist/shared/client.Cby_-GGh.d.mts +0 -96
- package/dist/shared/client.DMXKFDyV.mjs +0 -343
- package/dist/shared/client.DXhchJ84.mjs +0 -174
- package/dist/shared/client.Dnfj8jnT.mjs +0 -92
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,242 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* Requests will be evaluated against each group's condition in order,
|
|
11
|
-
* and included in the first group whose condition returns true.
|
|
12
|
-
* If no group's condition returns true, the request will not be batched.
|
|
13
|
-
*/
|
|
14
|
-
condition: Value<boolean, [options: StandardLinkTransportInterceptorOptions<T>]>;
|
|
15
|
-
/**
|
|
16
|
-
* The client context applied to requests in this batch group for the remainder of the link chain.
|
|
17
|
-
*/
|
|
18
|
-
context: Value<T, [items: [StandardLinkTransportInterceptorOptions<T>, ...StandardLinkTransportInterceptorOptions<T>[]]]>;
|
|
19
|
-
/**
|
|
20
|
-
* The path segments applied to requests in this batch group for the remainder of the link chain.
|
|
21
|
-
*
|
|
22
|
-
* @default []
|
|
23
|
-
*/
|
|
24
|
-
path?: Value<string[], [items: [StandardLinkTransportInterceptorOptions<T>, ...StandardLinkTransportInterceptorOptions<T>[]]]>;
|
|
25
|
-
}
|
|
26
|
-
declare class BatchLinkPluginError extends TypeError {
|
|
27
|
-
}
|
|
28
|
-
interface BatchLinkPluginOptions<T extends ClientContext> {
|
|
29
|
-
groups: [BatchLinkPluginGroup<T>, ...BatchLinkPluginGroup<T>[]];
|
|
30
|
-
/**
|
|
31
|
-
* Filters requests to batch.
|
|
32
|
-
*
|
|
33
|
-
* @default () => true
|
|
34
|
-
*/
|
|
35
|
-
filter?: Value<boolean, [options: StandardLinkTransportInterceptorOptions<T>]>;
|
|
36
|
-
/**
|
|
37
|
-
* The maximum number of requests in the batch.
|
|
38
|
-
*
|
|
39
|
-
* @default 10
|
|
40
|
-
*/
|
|
41
|
-
maxSize?: Value<Promisable<number>, [subOptionsList: [StandardLinkTransportInterceptorOptions<T>, ...StandardLinkTransportInterceptorOptions<T>[]]]>;
|
|
42
|
-
/**
|
|
43
|
-
* The batch response mode.
|
|
44
|
-
*
|
|
45
|
-
* @default 'streaming'
|
|
46
|
-
*/
|
|
47
|
-
mode?: Value<BatchLinkPluginMode, [subOptionsList: [StandardLinkTransportInterceptorOptions<T>, ...StandardLinkTransportInterceptorOptions<T>[]]]>;
|
|
48
|
-
/**
|
|
49
|
-
* URL for the batch request.
|
|
50
|
-
*
|
|
51
|
-
* @default URL of the first subrequest in the batch + '/__batch__'
|
|
52
|
-
*/
|
|
53
|
-
url?: Value<Promisable<StandardUrl>, [subOptionsList: [StandardLinkTransportInterceptorOptions<T>, ...StandardLinkTransportInterceptorOptions<T>[]]]>;
|
|
54
|
-
/**
|
|
55
|
-
* The maximum length of the URL that runtime supports,
|
|
56
|
-
* if exceeded, the batch will be split into smaller batches and sent sequentially.
|
|
57
|
-
*
|
|
58
|
-
* This only applies to GET batch requests where the batch data is sent via URL query parameter.
|
|
59
|
-
*
|
|
60
|
-
* @default 2083
|
|
61
|
-
*/
|
|
62
|
-
maxUrlLength?: Value<Promisable<number>, [subOptionsList: [StandardLinkTransportInterceptorOptions<T>, ...StandardLinkTransportInterceptorOptions<T>[]]]>;
|
|
63
|
-
/**
|
|
64
|
-
* Headers used for the batch request.
|
|
65
|
-
*
|
|
66
|
-
* @default Common headers among all subrequests in the batch.
|
|
67
|
-
*/
|
|
68
|
-
headers?: Value<Promisable<StandardHeaders>, [subOptionsList: [StandardLinkTransportInterceptorOptions<T>, ...StandardLinkTransportInterceptorOptions<T>[]]]>;
|
|
69
|
-
/**
|
|
70
|
-
* Map each subrequest in the batch before it is sent.
|
|
71
|
-
*
|
|
72
|
-
* @default Removes headers that are duplicated with the batch headers
|
|
73
|
-
*/
|
|
74
|
-
mapSubrequest?: (subOptions: StandardLinkTransportInterceptorOptions<T>, partialBatchRequest: Pick<StandardRequest, 'url' | 'headers'>) => StandardRequest;
|
|
75
|
-
/**
|
|
76
|
-
* Maps each subresponse before returning the final response.
|
|
77
|
-
*
|
|
78
|
-
* @default Low-priority merges headers from the batch response into each subresponse.
|
|
79
|
-
*/
|
|
80
|
-
mapSubresponse?: (subResponse: StandardLazyResponse, batchResponse: StandardLazyResponse, subOptions: StandardLinkTransportInterceptorOptions<T>) => StandardLazyResponse;
|
|
81
|
-
}
|
|
82
|
-
declare class BatchLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
83
|
-
name: string;
|
|
84
|
-
private readonly groups;
|
|
85
|
-
private readonly filter;
|
|
86
|
-
private readonly maxSize;
|
|
87
|
-
private readonly mode;
|
|
88
|
-
private readonly batchUrl;
|
|
89
|
-
private readonly maxUrlLength;
|
|
90
|
-
private readonly batchHeaders;
|
|
91
|
-
private readonly mapSubrequest;
|
|
92
|
-
private readonly mapSubresponse;
|
|
93
|
-
private readonly queue;
|
|
94
|
-
constructor(options: NoInfer<BatchLinkPluginOptions<T>>);
|
|
95
|
-
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
96
|
-
private processPendingBatches;
|
|
97
|
-
private executeBatch;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
interface DedupeLinkPluginGroup<T extends ClientContext> {
|
|
101
|
-
condition: Value<boolean, [options: StandardLinkTransportInterceptorOptions<T>]>;
|
|
102
|
-
/**
|
|
103
|
-
* The context used for the rest of the request lifecycle.
|
|
104
|
-
*/
|
|
105
|
-
context: Value<T, [
|
|
106
|
-
items: [
|
|
107
|
-
StandardLinkTransportInterceptorOptions<T>,
|
|
108
|
-
StandardLinkTransportInterceptorOptions<T>,
|
|
109
|
-
...StandardLinkTransportInterceptorOptions<T>[]
|
|
110
|
-
]
|
|
111
|
-
]>;
|
|
112
|
-
}
|
|
113
|
-
interface DedupeLinkPluginOptions<T extends ClientContext> {
|
|
114
|
-
/**
|
|
115
|
-
* To enable deduplication, a request must match at least one defined group.
|
|
116
|
-
* Requests that fall into the same group are considered for deduplication together.
|
|
117
|
-
*/
|
|
118
|
-
groups: [DedupeLinkPluginGroup<T>, ...DedupeLinkPluginGroup<T>[]];
|
|
119
|
-
/**
|
|
120
|
-
* Filters requests to dedupe.
|
|
121
|
-
*
|
|
122
|
-
* @default ({ request }) => request.method === 'GET'
|
|
123
|
-
*/
|
|
124
|
-
filter?: Value<boolean, [options: StandardLinkTransportInterceptorOptions<T>]>;
|
|
125
|
-
}
|
|
126
|
-
declare class DedupeLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
127
|
-
name: string;
|
|
128
|
-
before: string[];
|
|
129
|
-
private readonly groups;
|
|
130
|
-
private readonly filter;
|
|
131
|
-
private readonly queue;
|
|
132
|
-
constructor(options: NoInfer<DedupeLinkPluginOptions<T>>);
|
|
133
|
-
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
134
|
-
private enqueue;
|
|
135
|
-
private processPendingRequests;
|
|
136
|
-
private execute;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
interface RetryLinkPluginAttemptOptions<T extends RetryLinkPluginContext> extends StandardLinkInterceptorOptions<T> {
|
|
140
|
-
/**
|
|
141
|
-
* Latest retry delay advertised by the server via event metadata.
|
|
142
|
-
*/
|
|
143
|
-
lastEventRetry: number | undefined;
|
|
144
|
-
/**
|
|
145
|
-
* Current retry attempt number, starting at 1.
|
|
146
|
-
*/
|
|
147
|
-
attempt: number;
|
|
148
|
-
/**
|
|
149
|
-
* Error that triggered this retry attempt.
|
|
150
|
-
*/
|
|
151
|
-
error: unknown;
|
|
152
|
-
}
|
|
153
|
-
interface RetryLinkPluginContext {
|
|
154
|
-
/**
|
|
155
|
-
* Maximum retry attempts before throwing.
|
|
156
|
-
* Use `Number.POSITIVE_INFINITY` for infinite retries (e.g. for event iterators).
|
|
157
|
-
*
|
|
158
|
-
* @default 0
|
|
159
|
-
*/
|
|
160
|
-
retry?: Value<Promisable<number>, [Omit<StandardLinkInterceptorOptions<RetryLinkPluginContext>, 'next'>]>;
|
|
161
|
-
/**
|
|
162
|
-
* Delay (in ms) before retrying.
|
|
163
|
-
*
|
|
164
|
-
* @info Why 2000ms? The EventSource spec suggests a default retry delay of 2 seconds if it doesn't specify
|
|
165
|
-
* @default (o) => o.lastEventRetry ?? 2000
|
|
166
|
-
*/
|
|
167
|
-
retryDelay?: Value<Promisable<number>, [RetryLinkPluginAttemptOptions<RetryLinkPluginContext>]>;
|
|
168
|
-
/**
|
|
169
|
-
* Determine whether to retry.
|
|
170
|
-
*
|
|
171
|
-
* @default true
|
|
172
|
-
*/
|
|
173
|
-
shouldRetry?: Value<Promisable<boolean>, [RetryLinkPluginAttemptOptions<RetryLinkPluginContext>]>;
|
|
174
|
-
/**
|
|
175
|
-
* Hook called before each retry. Can return a cleanup callback.
|
|
176
|
-
*/
|
|
177
|
-
onRetry?: (options: RetryLinkPluginAttemptOptions<RetryLinkPluginContext>) => void | ((isSuccess: boolean) => void);
|
|
178
|
-
}
|
|
179
|
-
interface RetryLinkPluginOptions<_T extends RetryLinkPluginContext> {
|
|
180
|
-
/**
|
|
181
|
-
* Default retry options. Can be overridden by individual calls via the context.
|
|
182
|
-
*/
|
|
183
|
-
default?: RetryLinkPluginContext | undefined;
|
|
184
|
-
}
|
|
185
|
-
declare class RetryLinkPluginInvalidEventIteratorRetryResponse extends Error {
|
|
186
|
-
}
|
|
187
|
-
declare class RetryLinkPlugin<T extends RetryLinkPluginContext & ClientContext> implements StandardLinkPlugin<T> {
|
|
188
|
-
private readonly defaultRetry;
|
|
189
|
-
private readonly defaultRetryDelay;
|
|
190
|
-
private readonly defaultShouldRetry;
|
|
191
|
-
private readonly defaultOnRetry;
|
|
192
|
-
name: string;
|
|
193
|
-
constructor(options?: RetryLinkPluginOptions<T>);
|
|
194
|
-
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
interface RetryAfterLinkPluginOptions<T extends ClientContext> {
|
|
198
|
-
/**
|
|
199
|
-
* Override condition to determine whether to retry or not.
|
|
200
|
-
*
|
|
201
|
-
* @default ((response) => response.status === 429 || response.status === 503)
|
|
202
|
-
*/
|
|
203
|
-
condition?: Value<boolean, [
|
|
204
|
-
response: StandardLazyResponse,
|
|
205
|
-
options: StandardLinkTransportInterceptorOptions<T>
|
|
206
|
-
]>;
|
|
207
|
-
/**
|
|
208
|
-
* Maximum attempts before giving up retries.
|
|
209
|
-
*
|
|
210
|
-
* @default 3
|
|
211
|
-
*/
|
|
212
|
-
maxAttempts?: Value<number, [
|
|
213
|
-
response: StandardLazyResponse,
|
|
214
|
-
options: StandardLinkTransportInterceptorOptions<T>
|
|
215
|
-
]>;
|
|
216
|
-
/**
|
|
217
|
-
* Maximum timeout in milliseconds to wait before giving up retries.
|
|
218
|
-
*
|
|
219
|
-
* @default 5 * 60 * 1000 (5 minutes)
|
|
220
|
-
*/
|
|
221
|
-
timeout?: Value<number, [
|
|
222
|
-
response: StandardLazyResponse,
|
|
223
|
-
options: StandardLinkTransportInterceptorOptions<T>
|
|
224
|
-
]>;
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* The Retry After Link Plugin automatically retries requests based on server `retry-after` header.
|
|
228
|
-
* This is particularly useful for handling rate limiting and temporary server unavailability.
|
|
229
|
-
*
|
|
230
|
-
* @see {@link https://orpc.dev/docs/plugins/retry-after Retry After Plugin Docs}
|
|
231
|
-
*/
|
|
232
|
-
declare class RetryAfterLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
233
|
-
private readonly condition;
|
|
234
|
-
private readonly maxAttempts;
|
|
235
|
-
private readonly timeout;
|
|
236
|
-
name: string;
|
|
237
|
-
constructor(options?: RetryAfterLinkPluginOptions<T>);
|
|
238
|
-
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
export { BatchLinkPlugin, BatchLinkPluginError, DedupeLinkPlugin, RetryAfterLinkPlugin, RetryLinkPlugin, RetryLinkPluginInvalidEventIteratorRetryResponse };
|
|
242
|
-
export type { BatchLinkPluginGroup, BatchLinkPluginMode, BatchLinkPluginOptions, DedupeLinkPluginGroup, DedupeLinkPluginOptions, RetryAfterLinkPluginOptions, RetryLinkPluginAttemptOptions, RetryLinkPluginContext, RetryLinkPluginOptions };
|
|
1
|
+
import { BATCH_HEADER, BatchLinkPlugin, BatchLinkPluginGroup, BatchLinkPluginMode, BatchLinkPluginOptions, DEFAULT_BATCH_MAX_SIZE } from "./batch.js";
|
|
2
|
+
import { CookieJar, CookieJarLinkPlugin, CookieJarLinkPluginOptions, StoredCookie } from "./cookie-jar.js";
|
|
3
|
+
import { DedupeLinkPlugin, DedupeLinkPluginGroup, DedupeLinkPluginOptions } from "./dedupe.js";
|
|
4
|
+
import { RequestCompressionLinkPlugin, RequestCompressionLinkPluginOptions } from "./request-compression.js";
|
|
5
|
+
import { ResponseCompressionLinkPlugin, ResponseCompressionLinkPluginOptions } from "./response-compression.js";
|
|
6
|
+
import { RetryLinkPlugin, RetryLinkPluginAttemptOptions, RetryLinkPluginContext, RetryLinkPluginOptions, isRetryableError } from "./retry.js";
|
|
7
|
+
import { RetryAfterLinkPlugin, RetryAfterLinkPluginOptions } from "./retry-after.js";
|
|
8
|
+
import { TimeoutLinkPlugin, TimeoutLinkPluginOptions } from "./timeout.js";
|
|
9
|
+
export { BATCH_HEADER, BatchLinkPlugin, BatchLinkPluginGroup, BatchLinkPluginMode, BatchLinkPluginOptions, RetryLinkPlugin as ClientRetryPlugin, type RetryLinkPluginContext as ClientRetryPluginContext, CookieJar, CookieJarLinkPlugin, CookieJarLinkPluginOptions, DEFAULT_BATCH_MAX_SIZE, DedupeLinkPlugin, DedupeLinkPluginGroup, DedupeLinkPluginOptions, DedupeLinkPlugin as DedupeRequestsPlugin, RequestCompressionLinkPlugin, RequestCompressionLinkPluginOptions, ResponseCompressionLinkPlugin, ResponseCompressionLinkPluginOptions, RetryAfterLinkPlugin, RetryAfterLinkPluginOptions, RetryAfterLinkPlugin as RetryAfterPlugin, RetryLinkPlugin, RetryLinkPluginAttemptOptions, RetryLinkPluginContext, RetryLinkPluginOptions, StoredCookie, TimeoutLinkPlugin, TimeoutLinkPluginOptions, isRetryableError };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { BATCH_HEADER, BatchLinkPlugin, DEFAULT_BATCH_MAX_SIZE } from "./batch.js";
|
|
2
|
+
export { CookieJar, CookieJarLinkPlugin } from "./cookie-jar.js";
|
|
3
|
+
export { DedupeLinkPlugin, DedupeLinkPlugin as DedupeRequestsPlugin } from "./dedupe.js";
|
|
4
|
+
export { RequestCompressionLinkPlugin } from "./request-compression.js";
|
|
5
|
+
export { ResponseCompressionLinkPlugin } from "./response-compression.js";
|
|
6
|
+
export { RetryLinkPlugin as ClientRetryPlugin, RetryLinkPlugin, isRetryableError } from "./retry.js";
|
|
7
|
+
export { RetryAfterLinkPlugin, RetryAfterLinkPlugin as RetryAfterPlugin } from "./retry-after.js";
|
|
8
|
+
export { TimeoutLinkPlugin } from "./timeout.js";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import "../adapters/standard/index.js";
|
|
2
|
+
import { StandardLinkPlugin } from "../adapters/standard/plugin.js";
|
|
3
|
+
import { StandardLinkOptions, StandardLinkTransportInterceptorOptions } from "../adapters/standard/link.js";
|
|
4
|
+
import { ClientContext } from "../types.js";
|
|
5
|
+
import { CompressionEncoding } from "@orpc/shared";
|
|
6
|
+
export interface RequestCompressionLinkPluginOptions<T extends ClientContext> {
|
|
7
|
+
/**
|
|
8
|
+
* The compression scheme to use for request compression.
|
|
9
|
+
*
|
|
10
|
+
* @default 'gzip'
|
|
11
|
+
*/
|
|
12
|
+
encoding?: CompressionEncoding | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* The minimum request size in bytes required to trigger compression.
|
|
15
|
+
* Requests smaller than this threshold will not be compressed to avoid overhead.
|
|
16
|
+
* If the request size cannot be determined, compression will still be applied.
|
|
17
|
+
*
|
|
18
|
+
* @default 1024 (1KB)
|
|
19
|
+
*/
|
|
20
|
+
threshold?: number | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Determines whether a request with the given Content-Type should be compressed.
|
|
23
|
+
* Only consulted for binary transfers (streams, files, and file form-data parts).
|
|
24
|
+
* Also receives the transport interceptor options for per-request decisions.
|
|
25
|
+
*
|
|
26
|
+
* @default isCompressibleContentType (covers common text-based formats)
|
|
27
|
+
*/
|
|
28
|
+
isCompressibleContentType?: ((contentType: string | null | undefined, options: StandardLinkTransportInterceptorOptions<T>) => boolean) | undefined;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Compresses request bodies before sending them to the server,
|
|
32
|
+
* reducing bandwidth usage for large payloads.
|
|
33
|
+
*
|
|
34
|
+
* @see {@link https://orpc.dev/docs/plugins/request-compression | Request Compression Plugin}
|
|
35
|
+
*/
|
|
36
|
+
export declare class RequestCompressionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
37
|
+
name: string;
|
|
38
|
+
/**
|
|
39
|
+
* Compression should be done after batching, to compress the final request
|
|
40
|
+
*/
|
|
41
|
+
after: string[];
|
|
42
|
+
private readonly encoding;
|
|
43
|
+
private readonly threshold;
|
|
44
|
+
private readonly isCompressibleContentType;
|
|
45
|
+
constructor(options?: RequestCompressionLinkPluginOptions<T>);
|
|
46
|
+
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
47
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { estimateCompressibleFormDataSize, estimateUtf8ByteLength, isAsyncIteratorObject, isCompressibleContentType, toArray } from "@orpc/shared";
|
|
2
|
+
import { flattenStandardHeader, generateContentDisposition } from "@standard-server/core";
|
|
3
|
+
import { toFetchBody } from "@standard-server/fetch";
|
|
4
|
+
// src/plugins/request-compression.ts
|
|
5
|
+
/**
|
|
6
|
+
* Compresses request bodies before sending them to the server,
|
|
7
|
+
* reducing bandwidth usage for large payloads.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://orpc.dev/docs/plugins/request-compression | Request Compression Plugin}
|
|
10
|
+
*/
|
|
11
|
+
var RequestCompressionLinkPlugin = class {
|
|
12
|
+
name = "~request-compression";
|
|
13
|
+
/**
|
|
14
|
+
* Compression should be done after batching, to compress the final request
|
|
15
|
+
*/
|
|
16
|
+
after = ["~batch"];
|
|
17
|
+
encoding;
|
|
18
|
+
threshold;
|
|
19
|
+
isCompressibleContentType;
|
|
20
|
+
constructor(options = {}) {
|
|
21
|
+
this.encoding = options.encoding ?? "gzip";
|
|
22
|
+
this.threshold = options.threshold ?? 1024;
|
|
23
|
+
this.isCompressibleContentType = options.isCompressibleContentType ?? isCompressibleContentType;
|
|
24
|
+
}
|
|
25
|
+
init(options) {
|
|
26
|
+
const transportInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
27
|
+
const request = interceptorOptions.request;
|
|
28
|
+
if (flattenStandardHeader(request.headers["content-encoding"])?.trim()?.toLowerCase() !== void 0) return next();
|
|
29
|
+
if (request.body instanceof ReadableStream) {
|
|
30
|
+
const contentLength = Number(flattenStandardHeader(request.headers["content-length"]));
|
|
31
|
+
if ((!Number.isFinite(contentLength) || contentLength >= this.threshold) && this.isCompressibleContentType(flattenStandardHeader(request.headers["content-type"]), interceptorOptions)) {
|
|
32
|
+
const compressedStream = request.body.pipeThrough(new CompressionStream(this.encoding));
|
|
33
|
+
return next({
|
|
34
|
+
...interceptorOptions,
|
|
35
|
+
request: {
|
|
36
|
+
...interceptorOptions.request,
|
|
37
|
+
body: compressedStream,
|
|
38
|
+
headers: {
|
|
39
|
+
...request.headers,
|
|
40
|
+
"content-length": [],
|
|
41
|
+
"content-encoding": this.encoding
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
} else if (request.body instanceof Blob) {
|
|
47
|
+
const contentType = request.headers["content-type"] ?? request.body.type;
|
|
48
|
+
if ((!Number.isFinite(request.body.size) || request.body.size >= this.threshold) && this.isCompressibleContentType(flattenStandardHeader(contentType), interceptorOptions)) {
|
|
49
|
+
const compressedStream = request.body.stream().pipeThrough(new CompressionStream(this.encoding));
|
|
50
|
+
const contentDisposition = request.headers["content-disposition"] ?? generateContentDisposition(request.body instanceof File ? request.body.name : "blob");
|
|
51
|
+
return next({
|
|
52
|
+
...interceptorOptions,
|
|
53
|
+
request: {
|
|
54
|
+
...interceptorOptions.request,
|
|
55
|
+
body: compressedStream,
|
|
56
|
+
headers: {
|
|
57
|
+
...request.headers,
|
|
58
|
+
"standard-server": request.headers["standard-server"] ?? "file",
|
|
59
|
+
"content-type": contentType,
|
|
60
|
+
"content-length": [],
|
|
61
|
+
"content-disposition": contentDisposition,
|
|
62
|
+
"content-encoding": this.encoding
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
} else if (request.body instanceof FormData) {
|
|
68
|
+
if (estimateCompressibleFormDataSize(request.body, (contentType) => this.isCompressibleContentType(contentType, interceptorOptions)) >= this.threshold) {
|
|
69
|
+
const res = new Response(request.body);
|
|
70
|
+
const compressedStream = res.body.pipeThrough(new CompressionStream(this.encoding));
|
|
71
|
+
return next({
|
|
72
|
+
...interceptorOptions,
|
|
73
|
+
request: {
|
|
74
|
+
...interceptorOptions.request,
|
|
75
|
+
body: compressedStream,
|
|
76
|
+
headers: {
|
|
77
|
+
...request.headers,
|
|
78
|
+
"standard-server": [],
|
|
79
|
+
"content-type": res.headers.get("content-type"),
|
|
80
|
+
"content-length": [],
|
|
81
|
+
"content-encoding": this.encoding
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
} else if (request.body instanceof URLSearchParams) {
|
|
87
|
+
const string = request.body.toString();
|
|
88
|
+
if (estimateUtf8ByteLength(string) >= this.threshold) {
|
|
89
|
+
const compressedStream = new Blob([string]).stream().pipeThrough(new CompressionStream(this.encoding));
|
|
90
|
+
const [, bodyHeaders] = toFetchBody(request.body, request.headers);
|
|
91
|
+
return next({
|
|
92
|
+
...interceptorOptions,
|
|
93
|
+
request: {
|
|
94
|
+
...interceptorOptions.request,
|
|
95
|
+
body: compressedStream,
|
|
96
|
+
headers: {
|
|
97
|
+
...bodyHeaders,
|
|
98
|
+
"standard-server": bodyHeaders["standard-server"] ?? [],
|
|
99
|
+
"content-length": [],
|
|
100
|
+
"content-encoding": this.encoding
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
} else if (request.body !== void 0 && !isAsyncIteratorObject(request.body)) {
|
|
106
|
+
const [string, bodyHeaders] = toFetchBody(request.body, request.headers);
|
|
107
|
+
if (estimateUtf8ByteLength(string) >= this.threshold) {
|
|
108
|
+
const compressedStream = new Blob([string]).stream().pipeThrough(new CompressionStream(this.encoding));
|
|
109
|
+
return next({
|
|
110
|
+
...interceptorOptions,
|
|
111
|
+
request: {
|
|
112
|
+
...interceptorOptions.request,
|
|
113
|
+
body: compressedStream,
|
|
114
|
+
headers: {
|
|
115
|
+
...bodyHeaders,
|
|
116
|
+
"standard-server": bodyHeaders["standard-server"] ?? [],
|
|
117
|
+
"content-length": [],
|
|
118
|
+
"content-encoding": this.encoding
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return next();
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
...options,
|
|
128
|
+
transportInterceptors: [...toArray(options.transportInterceptors), transportInterceptor]
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
export { RequestCompressionLinkPlugin };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import "../adapters/standard/index.js";
|
|
2
|
+
import { StandardLinkPlugin } from "../adapters/standard/plugin.js";
|
|
3
|
+
import { StandardLinkOptions } from "../adapters/standard/link.js";
|
|
4
|
+
import { ClientContext } from "../types.js";
|
|
5
|
+
import { CompressionEncoding } from "@orpc/shared";
|
|
6
|
+
export interface ResponseCompressionLinkPluginOptions<_T extends ClientContext> {
|
|
7
|
+
/**
|
|
8
|
+
* Compression schemes to advertise via Accept-Encoding, in preference order.
|
|
9
|
+
* Only schemes that can be decompressed by this plugin should be listed.
|
|
10
|
+
*
|
|
11
|
+
* @default ['gzip', 'deflate']
|
|
12
|
+
*/
|
|
13
|
+
encodings?: readonly CompressionEncoding[] | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Advertises Accept-Encoding on requests and decompresses response bodies
|
|
17
|
+
* based on the Content-Encoding header. Works at the standard link level,
|
|
18
|
+
* so it supports all adapters.
|
|
19
|
+
*
|
|
20
|
+
* @see {@link https://orpc.dev/docs/plugins/response-compression | Response Compression Plugin}
|
|
21
|
+
*/
|
|
22
|
+
export declare class ResponseCompressionLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
23
|
+
name: string;
|
|
24
|
+
/**
|
|
25
|
+
* Decompression should wrap the final batch response instead of sub-responses.
|
|
26
|
+
*/
|
|
27
|
+
after: string[];
|
|
28
|
+
private readonly encodings;
|
|
29
|
+
constructor(options?: ResponseCompressionLinkPluginOptions<T>);
|
|
30
|
+
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { isCompressionEncoding, parseContentEncodings, toArray } from "@orpc/shared";
|
|
2
|
+
import { flattenStandardHeader } from "@standard-server/core";
|
|
3
|
+
import { toFetchHeaders, toStandardBody } from "@standard-server/fetch";
|
|
4
|
+
// src/plugins/response-compression.ts
|
|
5
|
+
/**
|
|
6
|
+
* Each coding adds a decompressor to the chain, so an unbounded list lets a tiny
|
|
7
|
+
* response cost unbounded CPU and memory. Same limit as undici and curl.
|
|
8
|
+
*/
|
|
9
|
+
const MAX_CONTENT_ENCODINGS = 5;
|
|
10
|
+
/**
|
|
11
|
+
* Advertises Accept-Encoding on requests and decompresses response bodies
|
|
12
|
+
* based on the Content-Encoding header. Works at the standard link level,
|
|
13
|
+
* so it supports all adapters.
|
|
14
|
+
*
|
|
15
|
+
* @see {@link https://orpc.dev/docs/plugins/response-compression | Response Compression Plugin}
|
|
16
|
+
*/
|
|
17
|
+
var ResponseCompressionLinkPlugin = class {
|
|
18
|
+
name = "~response-compression";
|
|
19
|
+
/**
|
|
20
|
+
* Decompression should wrap the final batch response instead of sub-responses.
|
|
21
|
+
*/
|
|
22
|
+
after = ["~batch"];
|
|
23
|
+
encodings;
|
|
24
|
+
constructor(options = {}) {
|
|
25
|
+
this.encodings = options.encodings ?? ["gzip", "deflate"];
|
|
26
|
+
}
|
|
27
|
+
init(options) {
|
|
28
|
+
const acceptEncodingHeader = this.encodings.join(", ");
|
|
29
|
+
const transportInterceptor = async ({ next, ...interceptorOptions }) => {
|
|
30
|
+
const response = await next({
|
|
31
|
+
...interceptorOptions,
|
|
32
|
+
request: {
|
|
33
|
+
...interceptorOptions.request,
|
|
34
|
+
headers: {
|
|
35
|
+
...interceptorOptions.request.headers,
|
|
36
|
+
"accept-encoding": acceptEncodingHeader
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
const encodings = parseContentEncodings(flattenStandardHeader(response.headers["content-encoding"]));
|
|
41
|
+
if (encodings.length === 0 || !encodings.every(isCompressionEncoding)) return response;
|
|
42
|
+
const decompressedHeaders = {
|
|
43
|
+
...response.headers,
|
|
44
|
+
"content-length": void 0,
|
|
45
|
+
"content-encoding": void 0
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
...response,
|
|
49
|
+
headers: decompressedHeaders,
|
|
50
|
+
async resolveBody(hint) {
|
|
51
|
+
const stream = await response.resolveBody("octet-stream");
|
|
52
|
+
if (!(stream instanceof ReadableStream)) return stream;
|
|
53
|
+
if (encodings.length > MAX_CONTENT_ENCODINGS) throw new TypeError("Too many content encodings.");
|
|
54
|
+
let decompressedStream = stream;
|
|
55
|
+
for (let i = encodings.length - 1; i >= 0; i--) decompressedStream = decompressedStream.pipeThrough(new DecompressionStream(encodings[i]));
|
|
56
|
+
const fetchResponse = new Response(decompressedStream, { headers: toFetchHeaders(decompressedHeaders) });
|
|
57
|
+
return toStandardBody(fetchResponse, { hint });
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
...options,
|
|
63
|
+
transportInterceptors: [...toArray(options.transportInterceptors), transportInterceptor]
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
export { ResponseCompressionLinkPlugin };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import "../adapters/standard/index.js";
|
|
2
|
+
import { StandardLinkPlugin } from "../adapters/standard/plugin.js";
|
|
3
|
+
import { StandardLinkOptions, StandardLinkTransportInterceptorOptions } from "../adapters/standard/link.js";
|
|
4
|
+
import { ClientContext } from "../types.js";
|
|
5
|
+
import { Value } from "@orpc/shared";
|
|
6
|
+
import { StandardLazyResponse } from "@standard-server/core";
|
|
7
|
+
export interface RetryAfterLinkPluginOptions<T extends ClientContext> {
|
|
8
|
+
/**
|
|
9
|
+
* Override condition to determine whether to retry or not.
|
|
10
|
+
*
|
|
11
|
+
* @default ((response) => response.status === 429 || response.status === 503)
|
|
12
|
+
*/
|
|
13
|
+
condition?: Value<boolean, [response: StandardLazyResponse, options: StandardLinkTransportInterceptorOptions<T>]> | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Maximum attempts before giving up retries.
|
|
16
|
+
*
|
|
17
|
+
* @default 3
|
|
18
|
+
*/
|
|
19
|
+
maxAttempts?: Value<number, [response: StandardLazyResponse, options: StandardLinkTransportInterceptorOptions<T>]> | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Maximum timeout in milliseconds to wait before giving up retries.
|
|
22
|
+
*
|
|
23
|
+
* @default 5 * 60 * 1000 (5 minutes)
|
|
24
|
+
*/
|
|
25
|
+
timeout?: Value<number, [response: StandardLazyResponse, options: StandardLinkTransportInterceptorOptions<T>]> | undefined;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The Retry After Link Plugin automatically retries requests based on server `retry-after` header.
|
|
29
|
+
* This is particularly useful for handling rate limiting and temporary server unavailability.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* **Note**: Requests with a stream or async iterator body are never retried, since the body cannot be resent.
|
|
33
|
+
*
|
|
34
|
+
* @see {@link https://orpc.dev/docs/plugins/retry-after | Retry After Plugin}
|
|
35
|
+
*/
|
|
36
|
+
export declare class RetryAfterLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
37
|
+
private readonly condition;
|
|
38
|
+
private readonly maxAttempts;
|
|
39
|
+
private readonly timeout;
|
|
40
|
+
name: string;
|
|
41
|
+
constructor(options?: RetryAfterLinkPluginOptions<T>);
|
|
42
|
+
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
43
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { COMMON_ERROR_STATUS_MAP } from "../error.js";
|
|
2
|
+
import { isAsyncIteratorObject, sleep, toArray, value } from "@orpc/shared";
|
|
3
|
+
import { flattenStandardHeader } from "@standard-server/core";
|
|
4
|
+
// src/plugins/retry-after.ts
|
|
5
|
+
/**
|
|
6
|
+
* The Retry After Link Plugin automatically retries requests based on server `retry-after` header.
|
|
7
|
+
* This is particularly useful for handling rate limiting and temporary server unavailability.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* **Note**: Requests with a stream or async iterator body are never retried, since the body cannot be resent.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link https://orpc.dev/docs/plugins/retry-after | Retry After Plugin}
|
|
13
|
+
*/
|
|
14
|
+
var RetryAfterLinkPlugin = class {
|
|
15
|
+
condition;
|
|
16
|
+
maxAttempts;
|
|
17
|
+
timeout;
|
|
18
|
+
name = "~retry-after";
|
|
19
|
+
constructor(options = {}) {
|
|
20
|
+
this.condition = options.condition ?? ((response) => response.status === COMMON_ERROR_STATUS_MAP.TOO_MANY_REQUESTS || response.status === COMMON_ERROR_STATUS_MAP.SERVICE_UNAVAILABLE);
|
|
21
|
+
this.maxAttempts = options.maxAttempts ?? 3;
|
|
22
|
+
this.timeout = options.timeout ?? 3e5;
|
|
23
|
+
}
|
|
24
|
+
init(options) {
|
|
25
|
+
const interceptor = async (interceptorOptions) => {
|
|
26
|
+
const { body } = interceptorOptions.request;
|
|
27
|
+
/**
|
|
28
|
+
* A stream or iterator body is consumed by the first attempt and cannot be resent,
|
|
29
|
+
* so return its response as-is instead of retrying with an empty or locked body.
|
|
30
|
+
*/
|
|
31
|
+
if (body instanceof ReadableStream || isAsyncIteratorObject(body)) return interceptorOptions.next();
|
|
32
|
+
const startTime = Date.now();
|
|
33
|
+
let attemptCount = 0;
|
|
34
|
+
while (true) {
|
|
35
|
+
attemptCount++;
|
|
36
|
+
const response = await interceptorOptions.next();
|
|
37
|
+
if (!value(this.condition, response, interceptorOptions)) return response;
|
|
38
|
+
const retryAfterMs = parseRetryAfterHeader(flattenStandardHeader(response.headers["retry-after"]));
|
|
39
|
+
if (retryAfterMs === void 0) return response;
|
|
40
|
+
if (attemptCount >= value(this.maxAttempts, response, interceptorOptions)) return response;
|
|
41
|
+
const timeoutMs = value(this.timeout, response, interceptorOptions);
|
|
42
|
+
if (Date.now() - startTime + retryAfterMs > timeoutMs) return response;
|
|
43
|
+
try {
|
|
44
|
+
await sleep(retryAfterMs, { signal: interceptorOptions.signal });
|
|
45
|
+
} catch {}
|
|
46
|
+
if (interceptorOptions.signal?.aborted) return response;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
...options,
|
|
51
|
+
transportInterceptors: [interceptor, ...toArray(options.transportInterceptors)]
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
function parseRetryAfterHeader(value) {
|
|
56
|
+
value = value?.trim();
|
|
57
|
+
if (!value) return;
|
|
58
|
+
const seconds = Number(value);
|
|
59
|
+
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1e3);
|
|
60
|
+
const retryDate = Date.parse(value);
|
|
61
|
+
if (!Number.isNaN(retryDate)) return Math.max(0, retryDate - Date.now());
|
|
62
|
+
}
|
|
63
|
+
export { RetryAfterLinkPlugin };
|