@orpc/client 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.
- package/dist/adapters/fetch/index.d.ts +3 -2
- package/dist/adapters/fetch/index.js +2 -2
- package/dist/adapters/fetch/rpc-link.d.ts +8 -8
- package/dist/adapters/fetch/rpc-link.js +16 -13
- package/dist/adapters/fetch/transport.d.ts +29 -31
- package/dist/adapters/fetch/transport.js +49 -63
- package/dist/adapters/message-port/index.d.ts +4 -3
- package/dist/adapters/message-port/index.js +3 -3
- package/dist/adapters/message-port/message-port.d.ts +13 -13
- package/dist/adapters/message-port/message-port.js +35 -53
- package/dist/adapters/message-port/rpc-link.d.ts +8 -8
- package/dist/adapters/message-port/rpc-link.js +17 -14
- package/dist/adapters/message-port/transport.d.ts +31 -34
- package/dist/adapters/message-port/transport.js +38 -52
- package/dist/adapters/standard/codec.d.ts +10 -10
- package/dist/adapters/standard/index.d.ts +6 -5
- package/dist/adapters/standard/index.js +2 -4
- package/dist/adapters/standard/link.d.ts +31 -31
- package/dist/adapters/standard/link.js +101 -85
- package/dist/adapters/standard/plugin.d.ts +25 -31
- package/dist/adapters/standard/plugin.js +14 -16
- package/dist/adapters/standard/rpc-link-codec.d.ts +53 -60
- package/dist/adapters/standard/rpc-link-codec.js +98 -102
- package/dist/adapters/standard/transport.d.ts +7 -7
- package/dist/adapters/websocket/index.d.ts +3 -2
- package/dist/adapters/websocket/index.js +2 -2
- package/dist/adapters/websocket/rpc-link.d.ts +8 -8
- package/dist/adapters/websocket/rpc-link.js +16 -13
- package/dist/adapters/websocket/transport.d.ts +101 -100
- package/dist/adapters/websocket/transport.js +102 -130
- package/dist/async-iterator-object.d.ts +2 -2
- package/dist/async-iterator-object.js +27 -28
- package/dist/client-safe.d.ts +4 -6
- package/dist/client-safe.js +29 -34
- package/dist/client.d.ts +24 -26
- package/dist/client.js +35 -37
- package/dist/consts.d.ts +1 -1
- package/dist/consts.js +18 -41
- package/dist/dynamic-link.d.ts +6 -6
- package/dist/dynamic-link.js +16 -16
- package/dist/error-utils.d.ts +3 -4
- package/dist/error-utils.js +47 -51
- package/dist/error.d.ts +79 -79
- package/dist/error.js +154 -166
- package/dist/index.d.ts +14 -53
- package/dist/index.js +12 -40
- package/dist/plugins/batch.d.ts +122 -155
- package/dist/plugins/batch.js +320 -391
- package/dist/plugins/cookie-jar.d.ts +64 -61
- package/dist/plugins/cookie-jar.js +208 -245
- package/dist/plugins/dedupe.d.ts +49 -53
- package/dist/plugins/dedupe.js +168 -211
- package/dist/plugins/index.d.ts +9 -28
- package/dist/plugins/index.js +8 -23
- package/dist/plugins/request-compression.d.ts +38 -35
- package/dist/plugins/request-compression.js +131 -177
- package/dist/plugins/response-compression.d.ts +21 -18
- package/dist/plugins/response-compression.js +64 -88
- package/dist/plugins/retry-after.d.ts +31 -38
- package/dist/plugins/retry-after.js +60 -85
- package/dist/plugins/retry.d.ts +55 -55
- package/dist/plugins/retry.js +118 -137
- package/dist/plugins/timeout.d.ts +19 -17
- package/dist/plugins/timeout.js +41 -36
- package/dist/rpc-json-serializer.d.ts +84 -84
- package/dist/rpc-json-serializer.js +338 -383
- package/dist/rpc-serializer.d.ts +20 -21
- package/dist/rpc-serializer.js +97 -125
- package/dist/types.d.ts +19 -31
- package/dist/utils.d.ts +18 -29
- package/dist/utils.js +63 -47
- package/package.json +5 -5
- package/dist/adapters/standard/codec.js +0 -1
- package/dist/adapters/standard/transport.js +0 -1
- package/dist/types.js +0 -1
package/dist/plugins/batch.js
CHANGED
|
@@ -1,401 +1,330 @@
|
|
|
1
|
-
import { captureAsyncContext, defer, isAsyncIteratorObject, loadBytes, once, promiseWithResolvers, safeEncodeURIComponent, splitInHalf, stringifyJSON, toArray, value
|
|
2
|
-
import { parseStandardUrl } from
|
|
3
|
-
import { ClientPeer, decodePeerMessage, isPeerMessage, isServerPeerSendMessage
|
|
1
|
+
import { captureAsyncContext, defer, isAsyncIteratorObject, loadBytes, once, promiseWithResolvers, safeEncodeURIComponent, splitInHalf, stringifyJSON, toArray, value } from "@orpc/shared";
|
|
2
|
+
import { parseStandardUrl } from "@standard-server/core";
|
|
3
|
+
import { ClientPeer, decodePeerMessage, isPeerMessage, isServerPeerSendMessage } from "@standard-server/peer";
|
|
4
|
+
// src/plugins/batch.ts
|
|
4
5
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
* Header that marks a request as a batch, set by the Batch Link Plugin and detected by the Batch Handler Plugin.
|
|
7
|
+
* Nothing between the client and server, such as a CORS policy or proxy, should block or strip it.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://orpc.dev/docs/plugins/batch#setup | Batch Plugin - Setup}
|
|
10
|
+
*/
|
|
11
|
+
const BATCH_HEADER = "orpc-batch";
|
|
11
12
|
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
* Default `maxSize` of both the Batch Link Plugin and the Batch Handler Plugin.
|
|
14
|
+
*
|
|
15
|
+
* @see {@link https://orpc.dev/docs/plugins/batch#max-size | Batch Plugin - Max Size}
|
|
16
|
+
*/
|
|
17
|
+
const DEFAULT_BATCH_MAX_SIZE = 10;
|
|
17
18
|
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
* instead of failing to parse it.
|
|
269
|
-
*/
|
|
270
|
-
if (batchResponse.status >= 400) {
|
|
271
|
-
const resolveBody = once(() => batchResponse.resolveBody());
|
|
272
|
-
const errorResponse = { ...batchResponse, resolveBody };
|
|
273
|
-
groupItems.forEach(([subOptions, resolve]) => {
|
|
274
|
-
resolve(this.mapSubresponse(errorResponse, batchResponse, subOptions));
|
|
275
|
-
});
|
|
276
|
-
suppressErrorFromCurrentBatch = true;
|
|
277
|
-
await peer.close();
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
const body = await batchResponse.resolveBody();
|
|
281
|
-
if (Array.isArray(body) &&
|
|
282
|
-
body.every((v) => isPeerMessage(v) && isServerPeerSendMessage(v))) {
|
|
283
|
-
for (const message of body) {
|
|
284
|
-
await peer.message(message);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
else if (body instanceof Blob) {
|
|
288
|
-
await decodeLengthPrefixedBlob(body, peer);
|
|
289
|
-
}
|
|
290
|
-
else if (body instanceof ReadableStream) {
|
|
291
|
-
await decodeLengthPrefixedStream(body, async (message) => {
|
|
292
|
-
await peer.message(message);
|
|
293
|
-
if (isLastServerMessage(message)) {
|
|
294
|
-
openRequestIds.delete(message.id);
|
|
295
|
-
cancelledRunningRequestIds.delete(message.id);
|
|
296
|
-
abortIfOnlyCancelledRemain();
|
|
297
|
-
}
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
else {
|
|
301
|
-
throw new TypeError('Invalid batch response format.');
|
|
302
|
-
}
|
|
303
|
-
await peer.close(new TypeError('Batch response is incomplete.'));
|
|
304
|
-
}
|
|
305
|
-
catch (error) {
|
|
306
|
-
await peer.close(error);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
catch (error) {
|
|
310
|
-
groupItems.forEach(([, , reject]) => reject(error));
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
}
|
|
19
|
+
* Combines multiple client requests into a single batch request
|
|
20
|
+
* and splits the batch response back into individual responses.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* **Note**: HTTP/2 and later already multiplex requests over a single connection, so this plugin is often less useful than it once was.
|
|
24
|
+
*
|
|
25
|
+
* @see {@link https://orpc.dev/docs/plugins/batch | Batch Plugin}
|
|
26
|
+
*/
|
|
27
|
+
var BatchLinkPlugin = class {
|
|
28
|
+
name = "~batch";
|
|
29
|
+
groups;
|
|
30
|
+
filter;
|
|
31
|
+
scope;
|
|
32
|
+
maxSize;
|
|
33
|
+
wait;
|
|
34
|
+
mode;
|
|
35
|
+
batchUrl;
|
|
36
|
+
maxUrlLength;
|
|
37
|
+
batchHeaders;
|
|
38
|
+
mapSubrequest;
|
|
39
|
+
mapSubresponse;
|
|
40
|
+
queue = /* @__PURE__ */ new Map();
|
|
41
|
+
constructor(options) {
|
|
42
|
+
this.groups = options.groups;
|
|
43
|
+
this.filter = options.filter ?? (() => true);
|
|
44
|
+
this.scope = options.scope ?? (() => void 0);
|
|
45
|
+
this.maxSize = options.maxSize ?? 10;
|
|
46
|
+
this.wait = options.wait ?? 0;
|
|
47
|
+
this.mode = options.mode ?? "buffered";
|
|
48
|
+
this.batchUrl = options.url ?? ((options) => {
|
|
49
|
+
const [pathname] = parseStandardUrl(options[0].request.url);
|
|
50
|
+
return `${pathname}/__batch__`;
|
|
51
|
+
});
|
|
52
|
+
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
|
53
|
+
this.batchHeaders = options.headers ?? (async (options) => {
|
|
54
|
+
const headersList = options.map((o) => o.request.headers);
|
|
55
|
+
const commonHeaders = {};
|
|
56
|
+
for (const headers of headersList) for (const key of Object.keys(headers)) {
|
|
57
|
+
const value = headers[key];
|
|
58
|
+
if (headersList.every((h) => h[key] === value)) commonHeaders[key] = value;
|
|
59
|
+
}
|
|
60
|
+
return commonHeaders;
|
|
61
|
+
});
|
|
62
|
+
this.mapSubrequest = options.mapSubrequest ?? (({ request }, { headers }) => {
|
|
63
|
+
const subHeaders = { ...request.headers };
|
|
64
|
+
for (const key of Object.keys(headers)) {
|
|
65
|
+
const value = headers[key];
|
|
66
|
+
if (subHeaders[key] === value) subHeaders[key] = void 0;
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
...request,
|
|
70
|
+
headers: subHeaders
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
this.mapSubresponse = options.mapSubresponse ?? ((subResponse, batchResponse) => {
|
|
74
|
+
return {
|
|
75
|
+
...subResponse,
|
|
76
|
+
headers: {
|
|
77
|
+
...batchResponse.headers,
|
|
78
|
+
...subResponse.headers
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
init(options) {
|
|
84
|
+
const transportInterceptor = async (interceptorOptions) => {
|
|
85
|
+
const { body, signal } = interceptorOptions.request;
|
|
86
|
+
if (body instanceof Blob || body instanceof FormData || body instanceof ReadableStream || isAsyncIteratorObject(body) || signal?.aborted || !value(this.filter, interceptorOptions)) return interceptorOptions.next();
|
|
87
|
+
const group = this.groups.find((group) => value(group.condition, interceptorOptions));
|
|
88
|
+
if (!group) return interceptorOptions.next();
|
|
89
|
+
const scope = this.scope(interceptorOptions);
|
|
90
|
+
return new Promise((resolve, reject) => {
|
|
91
|
+
if (!this.queue.size) defer(() => this.processPendingBatches(), this.wait);
|
|
92
|
+
let groups = this.queue.get(scope);
|
|
93
|
+
if (!groups) {
|
|
94
|
+
groups = /* @__PURE__ */ new Map();
|
|
95
|
+
this.queue.set(scope, groups);
|
|
96
|
+
}
|
|
97
|
+
let queue = groups.get(group);
|
|
98
|
+
if (!queue) {
|
|
99
|
+
queue = [];
|
|
100
|
+
groups.set(group, queue);
|
|
101
|
+
}
|
|
102
|
+
queue.push([
|
|
103
|
+
interceptorOptions,
|
|
104
|
+
resolve,
|
|
105
|
+
reject,
|
|
106
|
+
captureAsyncContext()
|
|
107
|
+
]);
|
|
108
|
+
});
|
|
109
|
+
};
|
|
110
|
+
return {
|
|
111
|
+
...options,
|
|
112
|
+
transportInterceptors: [...toArray(options.transportInterceptors), transportInterceptor]
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
processPendingBatches() {
|
|
116
|
+
const pending = [...this.queue.values()];
|
|
117
|
+
this.queue.clear();
|
|
118
|
+
for (const groups of pending) for (const [group, items] of groups) {
|
|
119
|
+
const getItems = items.filter(([options]) => options.request.method === "GET");
|
|
120
|
+
const queryItems = items.filter(([options]) => options.request.method === "QUERY");
|
|
121
|
+
const unsafeItems = items.filter(([options]) => options.request.method !== "GET" && options.request.method !== "QUERY");
|
|
122
|
+
this.executeBatchInOwnContext("GET", group, getItems);
|
|
123
|
+
this.executeBatchInOwnContext("QUERY", group, queryItems);
|
|
124
|
+
this.executeBatchInOwnContext("POST", group, unsafeItems);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Runs the batch in the async context of its first request instead of the timer's,
|
|
129
|
+
* so the transport and batch options see that caller's request state.
|
|
130
|
+
* `executeBatch` always runs in the context of its first request.
|
|
131
|
+
*/
|
|
132
|
+
async executeBatchInOwnContext(method, group, groupItems) {
|
|
133
|
+
await groupItems[0]?.[3](() => this.executeBatch(method, group, groupItems));
|
|
134
|
+
}
|
|
135
|
+
async executeBatch(method, group, groupItems) {
|
|
136
|
+
try {
|
|
137
|
+
if (groupItems.length === 1) {
|
|
138
|
+
const [options, resolve, reject] = groupItems[0];
|
|
139
|
+
options.next().then(resolve).catch(reject);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const subOptionsList = groupItems.map(([options]) => options);
|
|
143
|
+
const maxSize = await value(this.maxSize, subOptionsList);
|
|
144
|
+
if (groupItems.length > maxSize) {
|
|
145
|
+
const [first, second] = splitInHalf(groupItems);
|
|
146
|
+
await Promise.all([this.executeBatch(method, group, first), this.executeBatchInOwnContext(method, group, second)]);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const url = await value(this.batchUrl, subOptionsList);
|
|
150
|
+
const headers = await value(this.batchHeaders, subOptionsList);
|
|
151
|
+
const mode = await value(this.mode, subOptionsList);
|
|
152
|
+
let suppressErrorFromCurrentBatch = false;
|
|
153
|
+
const controller = new AbortController();
|
|
154
|
+
const pendingMessages = [];
|
|
155
|
+
const subrequests = groupItems.map(([subOptions]) => this.mapSubrequest(subOptions, {
|
|
156
|
+
url,
|
|
157
|
+
headers
|
|
158
|
+
}));
|
|
159
|
+
let batchResponse;
|
|
160
|
+
let markRequestSent;
|
|
161
|
+
const openRequestIds = /* @__PURE__ */ new Set();
|
|
162
|
+
const cancelledRunningRequestIds = /* @__PURE__ */ new Set();
|
|
163
|
+
let isBatchSent = false;
|
|
164
|
+
const abortIfOnlyCancelledRemain = () => {
|
|
165
|
+
if (openRequestIds.size === 0 && cancelledRunningRequestIds.size > 0) controller.abort();
|
|
166
|
+
};
|
|
167
|
+
const peer = new ClientPeer(async (message) => {
|
|
168
|
+
pendingMessages.push(message);
|
|
169
|
+
if (message.kind === "request") {
|
|
170
|
+
openRequestIds.add(message.id);
|
|
171
|
+
markRequestSent?.();
|
|
172
|
+
} else if (message.kind === "cancel" && openRequestIds.delete(message.id) && isBatchSent) {
|
|
173
|
+
cancelledRunningRequestIds.add(message.id);
|
|
174
|
+
abortIfOnlyCancelledRemain();
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
/**
|
|
178
|
+
* Subrequests go to the peer one at a time, so a request message always belongs to the current one.
|
|
179
|
+
* A subrequest aborted before the peer starts sending its request message sends nothing, not even a
|
|
180
|
+
* cancel, so settling also ends the wait for it.
|
|
181
|
+
*/
|
|
182
|
+
for (const [index, [subOptions, resolve, reject]] of groupItems.entries()) {
|
|
183
|
+
const sent = promiseWithResolvers();
|
|
184
|
+
markRequestSent = sent.resolve;
|
|
185
|
+
peer.request(subrequests[index]).then((subResponse) => resolve(this.mapSubresponse(subResponse, batchResponse, subOptions))).catch((error) => {
|
|
186
|
+
if (!suppressErrorFromCurrentBatch) reject(error);
|
|
187
|
+
}).then(sent.resolve);
|
|
188
|
+
await sent.promise;
|
|
189
|
+
}
|
|
190
|
+
if (openRequestIds.size === 0) return;
|
|
191
|
+
isBatchSent = true;
|
|
192
|
+
/**
|
|
193
|
+
* Leave out subrequests aborted before the batch is sent: the server counts their request and cancel
|
|
194
|
+
* against its batch size limit and would still run calls the client already rejected.
|
|
195
|
+
*/
|
|
196
|
+
const outgoingMessages = pendingMessages.filter((message) => openRequestIds.has(message.id));
|
|
197
|
+
try {
|
|
198
|
+
const request = {
|
|
199
|
+
url,
|
|
200
|
+
method,
|
|
201
|
+
headers: {
|
|
202
|
+
...headers,
|
|
203
|
+
[BATCH_HEADER]: mode
|
|
204
|
+
},
|
|
205
|
+
signal: controller.signal
|
|
206
|
+
};
|
|
207
|
+
if (method === "GET") {
|
|
208
|
+
const [pathname, search, hash] = parseStandardUrl(url);
|
|
209
|
+
const dataParam = `data=${safeEncodeURIComponent(stringifyJSON(outgoingMessages))}`;
|
|
210
|
+
const newUrl = search ? `${pathname}${search}&${dataParam}${hash ?? ""}` : `${pathname}?${dataParam}${hash ?? ""}`;
|
|
211
|
+
const maxUrlLength = await value(this.maxUrlLength, subOptionsList);
|
|
212
|
+
if (newUrl.length > maxUrlLength) {
|
|
213
|
+
const [first, second] = splitInHalf(groupItems);
|
|
214
|
+
suppressErrorFromCurrentBatch = true;
|
|
215
|
+
await Promise.all([
|
|
216
|
+
this.executeBatch(method, group, first),
|
|
217
|
+
this.executeBatchInOwnContext(method, group, second),
|
|
218
|
+
peer.close()
|
|
219
|
+
]);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
request.url = newUrl;
|
|
223
|
+
} else request.body = outgoingMessages;
|
|
224
|
+
batchResponse = await groupItems[0][0].next({
|
|
225
|
+
...subOptionsList[0],
|
|
226
|
+
context: value(group.context, subOptionsList),
|
|
227
|
+
path: value(group.path, subOptionsList) ?? [],
|
|
228
|
+
request,
|
|
229
|
+
signal: controller.signal
|
|
230
|
+
});
|
|
231
|
+
/**
|
|
232
|
+
* An error response is not a batch response, so forward it as-is to every subrequest
|
|
233
|
+
* instead of failing to parse it.
|
|
234
|
+
*/
|
|
235
|
+
if (batchResponse.status >= 400) {
|
|
236
|
+
const resolveBody = once(() => batchResponse.resolveBody());
|
|
237
|
+
const errorResponse = {
|
|
238
|
+
...batchResponse,
|
|
239
|
+
resolveBody
|
|
240
|
+
};
|
|
241
|
+
groupItems.forEach(([subOptions, resolve]) => {
|
|
242
|
+
resolve(this.mapSubresponse(errorResponse, batchResponse, subOptions));
|
|
243
|
+
});
|
|
244
|
+
suppressErrorFromCurrentBatch = true;
|
|
245
|
+
await peer.close();
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
const body = await batchResponse.resolveBody();
|
|
249
|
+
if (Array.isArray(body) && body.every((v) => isPeerMessage(v) && isServerPeerSendMessage(v))) for (const message of body) await peer.message(message);
|
|
250
|
+
else if (body instanceof Blob) await decodeLengthPrefixedBlob(body, peer);
|
|
251
|
+
else if (body instanceof ReadableStream) await decodeLengthPrefixedStream(body, async (message) => {
|
|
252
|
+
await peer.message(message);
|
|
253
|
+
if (isLastServerMessage(message)) {
|
|
254
|
+
openRequestIds.delete(message.id);
|
|
255
|
+
cancelledRunningRequestIds.delete(message.id);
|
|
256
|
+
abortIfOnlyCancelledRemain();
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
else throw new TypeError("Invalid batch response format.");
|
|
260
|
+
await peer.close(/* @__PURE__ */ new TypeError("Batch response is incomplete."));
|
|
261
|
+
} catch (error) {
|
|
262
|
+
await peer.close(error);
|
|
263
|
+
}
|
|
264
|
+
} catch (error) {
|
|
265
|
+
groupItems.forEach(([, , reject]) => reject(error));
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
};
|
|
314
269
|
/**
|
|
315
|
-
|
|
316
|
-
|
|
270
|
+
* Whether the server sends nothing more for the subrequest after this message.
|
|
271
|
+
*/
|
|
317
272
|
function isLastServerMessage(message) {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
case 'event-stream':
|
|
326
|
-
return message.json.event === 'close' || message.json.event === 'error';
|
|
327
|
-
case 'octet-stream':
|
|
328
|
-
return message.json.close === true;
|
|
329
|
-
case 'cancel':
|
|
330
|
-
return true;
|
|
331
|
-
default:
|
|
332
|
-
return false;
|
|
333
|
-
}
|
|
273
|
+
switch (message.kind) {
|
|
274
|
+
case "response": return message.json.body !== void 0 || message.binary !== void 0 || message.json.headers?.["content-type"] === void 0 && message.json.headers?.["standard-server"] === void 0;
|
|
275
|
+
case "event-stream": return message.json.event === "close" || message.json.event === "error";
|
|
276
|
+
case "octet-stream": return message.json.close === true;
|
|
277
|
+
case "cancel": return true;
|
|
278
|
+
default: return false;
|
|
279
|
+
}
|
|
334
280
|
}
|
|
335
281
|
async function decodeLengthPrefixedBlob(blob, peer) {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
offset += length;
|
|
350
|
-
const result = decodePeerMessage(messageBytes);
|
|
351
|
-
if (!result.matched || !isServerPeerSendMessage(result.message)) {
|
|
352
|
-
throw new TypeError('Invalid batch response: invalid message.');
|
|
353
|
-
}
|
|
354
|
-
await peer.message(result.message);
|
|
355
|
-
}
|
|
282
|
+
const buffer = await loadBytes(blob);
|
|
283
|
+
let offset = 0;
|
|
284
|
+
while (offset < buffer.length) {
|
|
285
|
+
if (offset + 4 > buffer.length) throw new TypeError("Invalid batch response: incomplete length header.");
|
|
286
|
+
const length = new DataView(buffer.buffer, buffer.byteOffset + offset, 4).getUint32(0, false);
|
|
287
|
+
offset += 4;
|
|
288
|
+
if (offset + length > buffer.length) throw new TypeError("Invalid batch response: incomplete message.");
|
|
289
|
+
const messageBytes = buffer.subarray(offset, offset + length);
|
|
290
|
+
offset += length;
|
|
291
|
+
const result = decodePeerMessage(messageBytes);
|
|
292
|
+
if (!result.matched || !isServerPeerSendMessage(result.message)) throw new TypeError("Invalid batch response: invalid message.");
|
|
293
|
+
await peer.message(result.message);
|
|
294
|
+
}
|
|
356
295
|
}
|
|
357
296
|
async function decodeLengthPrefixedStream(stream, receive) {
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
catch (error) {
|
|
394
|
-
// Free the response body so the server stops streaming to a reader that is gone
|
|
395
|
-
reader.cancel(error).catch(() => { });
|
|
396
|
-
throw error;
|
|
397
|
-
}
|
|
398
|
-
finally {
|
|
399
|
-
reader.releaseLock();
|
|
400
|
-
}
|
|
297
|
+
const reader = stream.getReader();
|
|
298
|
+
let buffer = /* @__PURE__ */ new Uint8Array(0);
|
|
299
|
+
try {
|
|
300
|
+
while (true) {
|
|
301
|
+
const { done, value: chunk } = await reader.read();
|
|
302
|
+
if (chunk) {
|
|
303
|
+
const newBuffer = new Uint8Array(buffer.length + chunk.length);
|
|
304
|
+
newBuffer.set(buffer);
|
|
305
|
+
newBuffer.set(chunk, buffer.length);
|
|
306
|
+
buffer = newBuffer;
|
|
307
|
+
}
|
|
308
|
+
while (buffer.length >= 4) {
|
|
309
|
+
const length = new DataView(buffer.buffer, buffer.byteOffset, 4).getUint32(0, false);
|
|
310
|
+
if (length === 0) {
|
|
311
|
+
buffer = buffer.subarray(4);
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (buffer.length < 4 + length) break;
|
|
315
|
+
const messageBytes = buffer.subarray(4, 4 + length);
|
|
316
|
+
buffer = buffer.subarray(4 + length);
|
|
317
|
+
const result = decodePeerMessage(messageBytes);
|
|
318
|
+
if (!result.matched || !isServerPeerSendMessage(result.message)) throw new TypeError("Invalid batch response: invalid message.");
|
|
319
|
+
await receive(result.message);
|
|
320
|
+
}
|
|
321
|
+
if (done) break;
|
|
322
|
+
}
|
|
323
|
+
} catch (error) {
|
|
324
|
+
reader.cancel(error).catch(() => {});
|
|
325
|
+
throw error;
|
|
326
|
+
} finally {
|
|
327
|
+
reader.releaseLock();
|
|
328
|
+
}
|
|
401
329
|
}
|
|
330
|
+
export { BATCH_HEADER, BatchLinkPlugin, DEFAULT_BATCH_MAX_SIZE };
|