@orpc/client 2.0.0-beta.5 → 2.0.0-beta.51
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 +139 -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 +17 -0
- package/dist/client-safe.js +31 -0
- package/dist/client.d.ts +39 -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 +141 -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 +91 -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.mjs
DELETED
|
@@ -1,574 +0,0 @@
|
|
|
1
|
-
import { toArray, value, splitInHalf, stringifyJSON, isAsyncIteratorObject, defer, loadBytes, allAbortSignal, replicateAsyncIterator, replicateReadableStream, override, AsyncIteratorClass, sleep } from '@orpc/shared';
|
|
2
|
-
import { parseStandardUrl, getEventMeta, flattenStandardHeader } from '@standardserver/core';
|
|
3
|
-
import { ClientPeer, isServerPeerSendMessage, decodePeerMessage } from '@standardserver/peer';
|
|
4
|
-
import { C as COMMON_ERROR_STATUS_MAP } from '../shared/client.Dnfj8jnT.mjs';
|
|
5
|
-
|
|
6
|
-
class BatchLinkPluginError extends TypeError {
|
|
7
|
-
}
|
|
8
|
-
class BatchLinkPlugin {
|
|
9
|
-
name = "~batch";
|
|
10
|
-
groups;
|
|
11
|
-
filter;
|
|
12
|
-
maxSize;
|
|
13
|
-
mode;
|
|
14
|
-
batchUrl;
|
|
15
|
-
maxUrlLength;
|
|
16
|
-
batchHeaders;
|
|
17
|
-
mapSubrequest;
|
|
18
|
-
mapSubresponse;
|
|
19
|
-
queue = /* @__PURE__ */ new Map();
|
|
20
|
-
constructor(options) {
|
|
21
|
-
this.groups = options.groups;
|
|
22
|
-
this.filter = options.filter ?? (() => true);
|
|
23
|
-
this.maxSize = options.maxSize ?? 10;
|
|
24
|
-
this.mode = options.mode ?? "streaming";
|
|
25
|
-
this.batchUrl = options.url ?? ((options2) => {
|
|
26
|
-
const [pathname] = parseStandardUrl(options2[0].request.url);
|
|
27
|
-
return `${pathname}/__batch__`;
|
|
28
|
-
});
|
|
29
|
-
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
|
30
|
-
this.batchHeaders = options.headers ?? (async (options2) => {
|
|
31
|
-
const headersList = options2.map((o) => o.request.headers);
|
|
32
|
-
const commonHeaders = {};
|
|
33
|
-
for (const headers of headersList) {
|
|
34
|
-
for (const [key, value2] of Object.entries(headers)) {
|
|
35
|
-
if (headersList.every((h) => h[key] === value2)) {
|
|
36
|
-
commonHeaders[key] = value2;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return commonHeaders;
|
|
41
|
-
});
|
|
42
|
-
this.mapSubrequest = options.mapSubrequest ?? (({ request }, { headers }) => {
|
|
43
|
-
const subHeaders = { ...request.headers };
|
|
44
|
-
for (const [key, value2] of Object.entries(headers)) {
|
|
45
|
-
if (subHeaders[key] === value2) {
|
|
46
|
-
subHeaders[key] = void 0;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return {
|
|
50
|
-
...request,
|
|
51
|
-
headers: subHeaders
|
|
52
|
-
};
|
|
53
|
-
});
|
|
54
|
-
this.mapSubresponse = (subResponse, batchResponse) => {
|
|
55
|
-
return {
|
|
56
|
-
...subResponse,
|
|
57
|
-
headers: {
|
|
58
|
-
...batchResponse.headers,
|
|
59
|
-
// low-priority
|
|
60
|
-
...subResponse.headers
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
init(options) {
|
|
66
|
-
const transportInterceptor = async (interceptorOptions) => {
|
|
67
|
-
if (interceptorOptions.request.body instanceof Blob || interceptorOptions.request.body instanceof ReadableStream || isAsyncIteratorObject(interceptorOptions.request.body) || interceptorOptions.request.signal?.aborted || !value(this.filter, interceptorOptions)) {
|
|
68
|
-
return interceptorOptions.next();
|
|
69
|
-
}
|
|
70
|
-
const group = this.groups.find((group2) => value(group2.condition, interceptorOptions));
|
|
71
|
-
if (!group) {
|
|
72
|
-
return interceptorOptions.next();
|
|
73
|
-
}
|
|
74
|
-
return new Promise((resolve, reject) => {
|
|
75
|
-
const queue = this.queue.get(group) ?? [];
|
|
76
|
-
if (!this.queue.has(group)) {
|
|
77
|
-
this.queue.set(group, queue);
|
|
78
|
-
}
|
|
79
|
-
queue.push([interceptorOptions, resolve, reject]);
|
|
80
|
-
defer(() => this.processPendingBatches());
|
|
81
|
-
});
|
|
82
|
-
};
|
|
83
|
-
return {
|
|
84
|
-
...options,
|
|
85
|
-
transportInterceptors: [...toArray(options.transportInterceptors), transportInterceptor]
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
async processPendingBatches() {
|
|
89
|
-
const pending = new Map(this.queue);
|
|
90
|
-
this.queue.clear();
|
|
91
|
-
for (const [group, items] of pending) {
|
|
92
|
-
const getItems = items.filter(([options]) => options.request.method === "GET");
|
|
93
|
-
const restItems = items.filter(([options]) => options.request.method !== "GET");
|
|
94
|
-
this.executeBatch("GET", group, getItems);
|
|
95
|
-
this.executeBatch("POST", group, restItems);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
async executeBatch(method, group, groupItems) {
|
|
99
|
-
if (!groupItems.length) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
if (groupItems.length === 1) {
|
|
103
|
-
const [options, resolve, reject] = groupItems[0];
|
|
104
|
-
options.next().then(resolve).catch(reject);
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
const subOptionsList = groupItems.map(([options]) => options);
|
|
108
|
-
const maxSize = await value(this.maxSize, subOptionsList);
|
|
109
|
-
if (groupItems.length > maxSize) {
|
|
110
|
-
const [first, second] = splitInHalf(groupItems);
|
|
111
|
-
await Promise.all([
|
|
112
|
-
this.executeBatch(method, group, first),
|
|
113
|
-
this.executeBatch(method, group, second)
|
|
114
|
-
]);
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
const url = await value(this.batchUrl, subOptionsList);
|
|
118
|
-
const headers = await value(this.batchHeaders, subOptionsList);
|
|
119
|
-
const mode = value(this.mode, subOptionsList);
|
|
120
|
-
let suppressErrorFromCurrentBatch = false;
|
|
121
|
-
const controller = new AbortController();
|
|
122
|
-
const pendingMessages = [];
|
|
123
|
-
let batchResponse;
|
|
124
|
-
const peer = new ClientPeer(async (message) => {
|
|
125
|
-
pendingMessages.push(message);
|
|
126
|
-
if (message.kind === "cancel" && pendingMessages.filter((m) => m.kind === "cancel").length === groupItems.length) {
|
|
127
|
-
controller.abort();
|
|
128
|
-
}
|
|
129
|
-
if (message.kind === "request" && pendingMessages.filter((m) => m.kind === "request").length === groupItems.length) {
|
|
130
|
-
(async () => {
|
|
131
|
-
try {
|
|
132
|
-
const request = {
|
|
133
|
-
url,
|
|
134
|
-
method,
|
|
135
|
-
headers: { ...headers, "orpc-batch": mode },
|
|
136
|
-
signal: controller.signal
|
|
137
|
-
};
|
|
138
|
-
if (method === "GET") {
|
|
139
|
-
const [pathname, search, hash] = parseStandardUrl(url);
|
|
140
|
-
const dataParam = `data=${encodeURIComponent(stringifyJSON(pendingMessages))}`;
|
|
141
|
-
const newUrl = search ? `${pathname}${search}&${dataParam}${hash ?? ""}` : `${pathname}?${dataParam}${hash ?? ""}`;
|
|
142
|
-
const maxUrlLength = await value(this.maxUrlLength, subOptionsList);
|
|
143
|
-
if (newUrl.length > maxUrlLength) {
|
|
144
|
-
const [first, second] = splitInHalf(groupItems);
|
|
145
|
-
suppressErrorFromCurrentBatch = true;
|
|
146
|
-
await Promise.all([
|
|
147
|
-
this.executeBatch(method, group, first),
|
|
148
|
-
this.executeBatch(method, group, second),
|
|
149
|
-
peer.close()
|
|
150
|
-
]);
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
request.url = newUrl;
|
|
154
|
-
} else {
|
|
155
|
-
request.body = pendingMessages;
|
|
156
|
-
}
|
|
157
|
-
batchResponse = await groupItems[0][0].next({
|
|
158
|
-
...subOptionsList[0],
|
|
159
|
-
context: value(group.context, subOptionsList),
|
|
160
|
-
path: value(group.path, subOptionsList) ?? [],
|
|
161
|
-
request,
|
|
162
|
-
signal: controller.signal
|
|
163
|
-
});
|
|
164
|
-
const body = await batchResponse.resolveBody();
|
|
165
|
-
if (Array.isArray(body) && body.every((v) => isServerPeerSendMessage(v))) {
|
|
166
|
-
for (const message2 of body) {
|
|
167
|
-
await peer.message(message2);
|
|
168
|
-
}
|
|
169
|
-
} else if (body instanceof Blob) {
|
|
170
|
-
await decodeLengthPrefixedBlob(body, peer);
|
|
171
|
-
} else if (body instanceof ReadableStream) {
|
|
172
|
-
await decodeLengthPrefixedStream(body, peer);
|
|
173
|
-
} else {
|
|
174
|
-
throw new BatchLinkPluginError("Invalid batch response format.");
|
|
175
|
-
}
|
|
176
|
-
await peer.close(new BatchLinkPluginError("Batch response is incomplete."));
|
|
177
|
-
} catch (error) {
|
|
178
|
-
await peer.close(error);
|
|
179
|
-
}
|
|
180
|
-
})();
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
groupItems.forEach(([subOptions, resolve, reject]) => {
|
|
184
|
-
peer.request(this.mapSubrequest(subOptions, { url, headers })).then((subResponse) => resolve(this.mapSubresponse(subResponse, batchResponse, subOptions))).catch((error) => {
|
|
185
|
-
if (!suppressErrorFromCurrentBatch) {
|
|
186
|
-
reject(error);
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
async function decodeLengthPrefixedBlob(blob, peer) {
|
|
193
|
-
const buffer = await loadBytes(blob);
|
|
194
|
-
let offset = 0;
|
|
195
|
-
while (offset < buffer.length) {
|
|
196
|
-
if (offset + 4 > buffer.length) {
|
|
197
|
-
throw new BatchLinkPluginError("Invalid batch response: incomplete length header.");
|
|
198
|
-
}
|
|
199
|
-
const view = new DataView(buffer.buffer, buffer.byteOffset + offset, 4);
|
|
200
|
-
const length = view.getUint32(0, false);
|
|
201
|
-
offset += 4;
|
|
202
|
-
if (offset + length > buffer.length) {
|
|
203
|
-
throw new BatchLinkPluginError("Invalid batch response: incomplete message.");
|
|
204
|
-
}
|
|
205
|
-
const messageBytes = buffer.subarray(offset, offset + length);
|
|
206
|
-
offset += length;
|
|
207
|
-
const result = decodePeerMessage(messageBytes);
|
|
208
|
-
if (!result.matched || !isServerPeerSendMessage(result.message)) {
|
|
209
|
-
throw new BatchLinkPluginError("Invalid batch response: invalid message.");
|
|
210
|
-
}
|
|
211
|
-
await peer.message(result.message);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
async function decodeLengthPrefixedStream(stream, peer) {
|
|
215
|
-
const reader = stream.getReader();
|
|
216
|
-
let buffer = new Uint8Array(0);
|
|
217
|
-
try {
|
|
218
|
-
while (true) {
|
|
219
|
-
const { done, value: chunk } = await reader.read();
|
|
220
|
-
if (chunk) {
|
|
221
|
-
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
222
|
-
newBuffer.set(buffer);
|
|
223
|
-
newBuffer.set(chunk, buffer.length);
|
|
224
|
-
buffer = newBuffer;
|
|
225
|
-
}
|
|
226
|
-
while (buffer.length >= 4) {
|
|
227
|
-
const view = new DataView(buffer.buffer, buffer.byteOffset, 4);
|
|
228
|
-
const length = view.getUint32(0, false);
|
|
229
|
-
if (buffer.length < 4 + length) {
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
const messageBytes = buffer.subarray(4, 4 + length);
|
|
233
|
-
buffer = buffer.subarray(4 + length);
|
|
234
|
-
const result = decodePeerMessage(messageBytes);
|
|
235
|
-
if (!result.matched || !isServerPeerSendMessage(result.message)) {
|
|
236
|
-
throw new BatchLinkPluginError("Invalid batch response: invalid message.");
|
|
237
|
-
}
|
|
238
|
-
await peer.message(result.message);
|
|
239
|
-
}
|
|
240
|
-
if (done) {
|
|
241
|
-
break;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
} finally {
|
|
245
|
-
reader.releaseLock();
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
class DedupeLinkPlugin {
|
|
250
|
-
name = "~dedupe";
|
|
251
|
-
before = ["~batch"];
|
|
252
|
-
groups;
|
|
253
|
-
filter;
|
|
254
|
-
queue = /* @__PURE__ */ new Map();
|
|
255
|
-
constructor(options) {
|
|
256
|
-
this.groups = options.groups;
|
|
257
|
-
this.filter = options.filter ?? (({ request }) => request.method === "GET");
|
|
258
|
-
}
|
|
259
|
-
init(options) {
|
|
260
|
-
const transportInterceptor = (interceptorOptions) => {
|
|
261
|
-
if (!canDedupeRequest(interceptorOptions.request) || !value(this.filter, interceptorOptions)) {
|
|
262
|
-
return interceptorOptions.next();
|
|
263
|
-
}
|
|
264
|
-
const group = this.groups.find((group2) => value(group2.condition, interceptorOptions));
|
|
265
|
-
if (!group) {
|
|
266
|
-
return interceptorOptions.next();
|
|
267
|
-
}
|
|
268
|
-
return new Promise((resolve, reject) => {
|
|
269
|
-
this.enqueue(group, interceptorOptions, resolve, reject);
|
|
270
|
-
defer(() => {
|
|
271
|
-
this.processPendingRequests();
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
};
|
|
275
|
-
return {
|
|
276
|
-
...options,
|
|
277
|
-
transportInterceptors: [...toArray(options.transportInterceptors), transportInterceptor]
|
|
278
|
-
};
|
|
279
|
-
}
|
|
280
|
-
enqueue(group, options, resolve, reject) {
|
|
281
|
-
let queue = this.queue.get(group);
|
|
282
|
-
if (!queue) {
|
|
283
|
-
queue = [];
|
|
284
|
-
this.queue.set(group, queue);
|
|
285
|
-
}
|
|
286
|
-
const requestKey = createRequestKey(options.path, options.request);
|
|
287
|
-
const matched = queue.find((item) => item.requestKey === requestKey);
|
|
288
|
-
if (matched) {
|
|
289
|
-
matched.matchedOptions.push(options);
|
|
290
|
-
matched.signals.push(options.request.signal);
|
|
291
|
-
matched.resolves.push(resolve);
|
|
292
|
-
matched.rejects.push(reject);
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
queue.push({
|
|
296
|
-
requestKey,
|
|
297
|
-
options,
|
|
298
|
-
matchedOptions: [options],
|
|
299
|
-
signals: [options.request.signal],
|
|
300
|
-
resolves: [resolve],
|
|
301
|
-
rejects: [reject]
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
async processPendingRequests() {
|
|
305
|
-
const pending = new Map(this.queue);
|
|
306
|
-
this.queue.clear();
|
|
307
|
-
const executions = [];
|
|
308
|
-
for (const [group, items] of pending) {
|
|
309
|
-
for (const item of items) {
|
|
310
|
-
executions.push(this.execute(group, item));
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
await Promise.all(executions);
|
|
314
|
-
}
|
|
315
|
-
async execute(group, item) {
|
|
316
|
-
try {
|
|
317
|
-
if (!shouldDedupe(item.matchedOptions)) {
|
|
318
|
-
const response2 = await item.options.next(item.options);
|
|
319
|
-
item.resolves[0]?.(response2);
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
const context = value(group.context, item.matchedOptions);
|
|
323
|
-
const request = {
|
|
324
|
-
...item.options.request,
|
|
325
|
-
signal: allAbortSignal(item.signals)
|
|
326
|
-
};
|
|
327
|
-
const response = await item.options.next({
|
|
328
|
-
...item.options,
|
|
329
|
-
request,
|
|
330
|
-
signal: request.signal,
|
|
331
|
-
context
|
|
332
|
-
});
|
|
333
|
-
const replicatedResponses = replicateLazyResponse(response, item.resolves.length);
|
|
334
|
-
for (const resolve of item.resolves) {
|
|
335
|
-
resolve(replicatedResponses.pop());
|
|
336
|
-
}
|
|
337
|
-
} catch (error) {
|
|
338
|
-
for (const reject of item.rejects) {
|
|
339
|
-
reject(error);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
function canDedupeRequest(request) {
|
|
345
|
-
return !(request.body instanceof Blob || request.body instanceof FormData || request.body instanceof URLSearchParams || request.body instanceof ReadableStream || isAsyncIteratorObject(request.body) || request.signal?.aborted);
|
|
346
|
-
}
|
|
347
|
-
function createRequestKey(path, request) {
|
|
348
|
-
return stringifyJSON({
|
|
349
|
-
path,
|
|
350
|
-
body: request.body,
|
|
351
|
-
headers: request.headers,
|
|
352
|
-
method: request.method,
|
|
353
|
-
url: request.url
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
function replicateLazyResponse(response, count) {
|
|
357
|
-
const replicated = [];
|
|
358
|
-
let bodyPromise;
|
|
359
|
-
let replicatedAsyncIterators;
|
|
360
|
-
let replicatedReadableStream;
|
|
361
|
-
for (let i = 0; i < count; i++) {
|
|
362
|
-
let resolvedBody;
|
|
363
|
-
replicated.push({
|
|
364
|
-
...response,
|
|
365
|
-
resolveBody: async (hint) => {
|
|
366
|
-
if (resolvedBody) {
|
|
367
|
-
return resolvedBody.body;
|
|
368
|
-
}
|
|
369
|
-
bodyPromise ??= response.resolveBody(hint);
|
|
370
|
-
const body = await bodyPromise;
|
|
371
|
-
if (isAsyncIteratorObject(body)) {
|
|
372
|
-
replicatedAsyncIterators ??= replicateAsyncIterator(body, count);
|
|
373
|
-
resolvedBody = { body: replicatedAsyncIterators.pop() };
|
|
374
|
-
} else if (body instanceof ReadableStream) {
|
|
375
|
-
replicatedReadableStream ??= replicateReadableStream(body, count);
|
|
376
|
-
resolvedBody = { body: replicatedReadableStream.pop() };
|
|
377
|
-
} else {
|
|
378
|
-
resolvedBody = { body };
|
|
379
|
-
}
|
|
380
|
-
return resolvedBody.body;
|
|
381
|
-
}
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
return replicated;
|
|
385
|
-
}
|
|
386
|
-
function shouldDedupe(items) {
|
|
387
|
-
return items.length >= 2;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
class RetryLinkPluginInvalidEventIteratorRetryResponse extends Error {
|
|
391
|
-
}
|
|
392
|
-
class RetryLinkPlugin {
|
|
393
|
-
defaultRetry;
|
|
394
|
-
defaultRetryDelay;
|
|
395
|
-
defaultShouldRetry;
|
|
396
|
-
defaultOnRetry;
|
|
397
|
-
name = "~retry";
|
|
398
|
-
constructor(options = {}) {
|
|
399
|
-
this.defaultRetry = options.default?.retry ?? 0;
|
|
400
|
-
this.defaultRetryDelay = options.default?.retryDelay ?? ((o) => o.lastEventRetry ?? 2e3);
|
|
401
|
-
this.defaultShouldRetry = options.default?.shouldRetry ?? true;
|
|
402
|
-
this.defaultOnRetry = options.default?.onRetry;
|
|
403
|
-
}
|
|
404
|
-
init(options) {
|
|
405
|
-
const interceptor = async (interceptorOptions) => {
|
|
406
|
-
const { next, ...callOptions } = interceptorOptions;
|
|
407
|
-
const maxAttempts = await value(
|
|
408
|
-
callOptions.context.retry ?? this.defaultRetry,
|
|
409
|
-
callOptions
|
|
410
|
-
);
|
|
411
|
-
const retryDelay = callOptions.context.retryDelay ?? this.defaultRetryDelay;
|
|
412
|
-
const shouldRetry = callOptions.context.shouldRetry ?? this.defaultShouldRetry;
|
|
413
|
-
const onRetry = callOptions.context.onRetry ?? this.defaultOnRetry;
|
|
414
|
-
if (maxAttempts <= 0) {
|
|
415
|
-
return next(callOptions);
|
|
416
|
-
}
|
|
417
|
-
let lastEventId = callOptions.lastEventId;
|
|
418
|
-
let lastEventRetry;
|
|
419
|
-
let callback;
|
|
420
|
-
let attempt = 1;
|
|
421
|
-
const callNext = async (initialError) => {
|
|
422
|
-
let currentError = initialError;
|
|
423
|
-
while (true) {
|
|
424
|
-
const updatedCallOptions = { ...callOptions, lastEventId };
|
|
425
|
-
if (currentError) {
|
|
426
|
-
if (attempt > maxAttempts) {
|
|
427
|
-
throw currentError.error;
|
|
428
|
-
}
|
|
429
|
-
const attemptOptions = {
|
|
430
|
-
...updatedCallOptions,
|
|
431
|
-
attempt,
|
|
432
|
-
error: currentError.error,
|
|
433
|
-
lastEventRetry
|
|
434
|
-
};
|
|
435
|
-
const shouldRetryBool = await value(
|
|
436
|
-
shouldRetry,
|
|
437
|
-
attemptOptions
|
|
438
|
-
);
|
|
439
|
-
if (!shouldRetryBool) {
|
|
440
|
-
throw currentError.error;
|
|
441
|
-
}
|
|
442
|
-
callback = onRetry?.(attemptOptions);
|
|
443
|
-
}
|
|
444
|
-
try {
|
|
445
|
-
if (currentError) {
|
|
446
|
-
const retryDelayMs = await value(retryDelay, {
|
|
447
|
-
...updatedCallOptions,
|
|
448
|
-
attempt,
|
|
449
|
-
error: currentError.error,
|
|
450
|
-
lastEventRetry
|
|
451
|
-
});
|
|
452
|
-
await sleep(retryDelayMs, { signal: updatedCallOptions.signal });
|
|
453
|
-
attempt++;
|
|
454
|
-
}
|
|
455
|
-
currentError = void 0;
|
|
456
|
-
return await next(updatedCallOptions);
|
|
457
|
-
} catch (error) {
|
|
458
|
-
currentError = { error };
|
|
459
|
-
if (updatedCallOptions.signal?.aborted) {
|
|
460
|
-
throw error;
|
|
461
|
-
}
|
|
462
|
-
} finally {
|
|
463
|
-
callback?.(!currentError);
|
|
464
|
-
callback = void 0;
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
const output = await callNext();
|
|
469
|
-
if (!isAsyncIteratorObject(output)) {
|
|
470
|
-
return output;
|
|
471
|
-
}
|
|
472
|
-
let current = output;
|
|
473
|
-
let isIteratorAborted = false;
|
|
474
|
-
return override(() => current, new AsyncIteratorClass(
|
|
475
|
-
async () => {
|
|
476
|
-
while (true) {
|
|
477
|
-
try {
|
|
478
|
-
const item = await current.next();
|
|
479
|
-
const meta = getEventMeta(item.value);
|
|
480
|
-
lastEventId = meta?.id ?? lastEventId;
|
|
481
|
-
lastEventRetry = meta?.retry ?? lastEventRetry;
|
|
482
|
-
return item;
|
|
483
|
-
} catch (error) {
|
|
484
|
-
const meta = getEventMeta(error);
|
|
485
|
-
lastEventId = meta?.id ?? lastEventId;
|
|
486
|
-
lastEventRetry = meta?.retry ?? lastEventRetry;
|
|
487
|
-
const maybeEventIterator = await callNext({ error });
|
|
488
|
-
if (!isAsyncIteratorObject(maybeEventIterator)) {
|
|
489
|
-
throw new RetryLinkPluginInvalidEventIteratorRetryResponse(
|
|
490
|
-
"RetryLinkPlugin: Expected an Event Iterator, got a non-Event Iterator"
|
|
491
|
-
);
|
|
492
|
-
}
|
|
493
|
-
current = maybeEventIterator;
|
|
494
|
-
if (isIteratorAborted) {
|
|
495
|
-
await current.return?.();
|
|
496
|
-
throw error;
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
},
|
|
501
|
-
async ({ kind }) => {
|
|
502
|
-
isIteratorAborted = true;
|
|
503
|
-
if (kind === "cancelled") {
|
|
504
|
-
await current.return?.();
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
));
|
|
508
|
-
};
|
|
509
|
-
return { ...options, interceptors: [interceptor, ...toArray(options.interceptors)] };
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
class RetryAfterLinkPlugin {
|
|
514
|
-
condition;
|
|
515
|
-
maxAttempts;
|
|
516
|
-
timeout;
|
|
517
|
-
name = "~retry-after";
|
|
518
|
-
constructor(options = {}) {
|
|
519
|
-
this.condition = options.condition ?? ((response) => response.status === COMMON_ERROR_STATUS_MAP.TOO_MANY_REQUESTS || response.status === COMMON_ERROR_STATUS_MAP.SERVICE_UNAVAILABLE);
|
|
520
|
-
this.maxAttempts = options.maxAttempts ?? 3;
|
|
521
|
-
this.timeout = options.timeout ?? 5 * 60 * 1e3;
|
|
522
|
-
}
|
|
523
|
-
init(options) {
|
|
524
|
-
const interceptor = async (interceptorOptions) => {
|
|
525
|
-
const startTime = Date.now();
|
|
526
|
-
let attemptCount = 0;
|
|
527
|
-
while (true) {
|
|
528
|
-
attemptCount++;
|
|
529
|
-
const response = await interceptorOptions.next();
|
|
530
|
-
if (!value(this.condition, response, interceptorOptions)) {
|
|
531
|
-
return response;
|
|
532
|
-
}
|
|
533
|
-
const retryAfterHeader = flattenStandardHeader(response.headers["retry-after"]);
|
|
534
|
-
const retryAfterMs = parseRetryAfterHeader(retryAfterHeader);
|
|
535
|
-
if (retryAfterMs === void 0) {
|
|
536
|
-
return response;
|
|
537
|
-
}
|
|
538
|
-
if (attemptCount >= value(this.maxAttempts, response, interceptorOptions)) {
|
|
539
|
-
return response;
|
|
540
|
-
}
|
|
541
|
-
const timeoutMs = value(this.timeout, response, interceptorOptions);
|
|
542
|
-
const elapsedTime = Date.now() - startTime;
|
|
543
|
-
if (elapsedTime + retryAfterMs > timeoutMs) {
|
|
544
|
-
return response;
|
|
545
|
-
}
|
|
546
|
-
try {
|
|
547
|
-
await sleep(retryAfterMs, { signal: interceptorOptions.signal });
|
|
548
|
-
} catch {
|
|
549
|
-
}
|
|
550
|
-
if (interceptorOptions.signal?.aborted) {
|
|
551
|
-
return response;
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
};
|
|
555
|
-
return { ...options, transportInterceptors: [interceptor, ...toArray(options.transportInterceptors)] };
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
function parseRetryAfterHeader(value2) {
|
|
559
|
-
value2 = value2?.trim();
|
|
560
|
-
if (!value2) {
|
|
561
|
-
return void 0;
|
|
562
|
-
}
|
|
563
|
-
const seconds = Number(value2);
|
|
564
|
-
if (Number.isFinite(seconds)) {
|
|
565
|
-
return Math.max(0, seconds * 1e3);
|
|
566
|
-
}
|
|
567
|
-
const retryDate = Date.parse(value2);
|
|
568
|
-
if (!Number.isNaN(retryDate)) {
|
|
569
|
-
return Math.max(0, retryDate - Date.now());
|
|
570
|
-
}
|
|
571
|
-
return void 0;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
export { BatchLinkPlugin, BatchLinkPluginError, DedupeLinkPlugin, RetryAfterLinkPlugin, RetryLinkPlugin, RetryLinkPluginInvalidEventIteratorRetryResponse };
|