@orpc/client 0.0.0-next.05a8f88 → 0.0.0-next.05d8e79
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 +22 -22
- package/dist/adapters/fetch/index.d.mts +31 -11
- package/dist/adapters/fetch/index.d.ts +31 -11
- package/dist/adapters/fetch/index.mjs +26 -13
- package/dist/adapters/message-port/index.d.mts +80 -0
- package/dist/adapters/message-port/index.d.ts +80 -0
- package/dist/adapters/message-port/index.mjs +86 -0
- package/dist/adapters/standard/index.d.mts +6 -5
- package/dist/adapters/standard/index.d.ts +6 -5
- package/dist/adapters/standard/index.mjs +3 -2
- package/dist/adapters/websocket/index.d.mts +29 -0
- package/dist/adapters/websocket/index.d.ts +29 -0
- package/dist/adapters/websocket/index.mjs +46 -0
- package/dist/index.d.mts +104 -29
- package/dist/index.d.ts +104 -29
- package/dist/index.mjs +57 -11
- package/dist/plugins/index.d.mts +163 -25
- package/dist/plugins/index.d.ts +163 -25
- package/dist/plugins/index.mjs +332 -56
- package/dist/shared/{client.DhAxdT4W.mjs → client.ARLTZuXW.mjs} +90 -29
- package/dist/shared/client.BH1AYT_p.d.mts +83 -0
- package/dist/shared/client.BH1AYT_p.d.ts +83 -0
- package/dist/shared/{client.D-jrSuDb.d.ts → client.BxV-mzeR.d.ts} +13 -25
- package/dist/shared/{client.grRbC25r.d.ts → client.CPgZaUox.d.mts} +21 -15
- package/dist/shared/{client.Bt94sjrK.d.mts → client.D8lMmWVC.d.mts} +13 -25
- package/dist/shared/{client.5813Ufvs.d.mts → client.De8SW4Kw.d.ts} +21 -15
- package/dist/shared/client.fXmJjmJv.mjs +208 -0
- package/package.json +16 -5
- package/dist/shared/client.C0lT7w02.d.mts +0 -30
- package/dist/shared/client.C0lT7w02.d.ts +0 -30
- package/dist/shared/client.jKEwIsRd.mjs +0 -175
package/dist/plugins/index.mjs
CHANGED
|
@@ -1,5 +1,260 @@
|
|
|
1
|
-
import { value,
|
|
2
|
-
import {
|
|
1
|
+
import { isAsyncIteratorObject, defer, value, splitInHalf, toArray, stringifyJSON, overlayProxy, AsyncIteratorClass } from '@orpc/shared';
|
|
2
|
+
import { toBatchRequest, parseBatchResponse, toBatchAbortSignal } from '@orpc/standard-server/batch';
|
|
3
|
+
import { replicateStandardLazyResponse, getEventMeta } from '@orpc/standard-server';
|
|
4
|
+
|
|
5
|
+
class BatchLinkPlugin {
|
|
6
|
+
groups;
|
|
7
|
+
maxSize;
|
|
8
|
+
batchUrl;
|
|
9
|
+
maxUrlLength;
|
|
10
|
+
batchHeaders;
|
|
11
|
+
mapRequestItem;
|
|
12
|
+
exclude;
|
|
13
|
+
mode;
|
|
14
|
+
pending;
|
|
15
|
+
order = 5e6;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.groups = options.groups;
|
|
18
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
19
|
+
this.maxSize = options.maxSize ?? 10;
|
|
20
|
+
this.maxUrlLength = options.maxUrlLength ?? 2083;
|
|
21
|
+
this.mode = options.mode ?? "streaming";
|
|
22
|
+
this.batchUrl = options.url ?? (([options2]) => `${options2.request.url.origin}${options2.request.url.pathname}/__batch__`);
|
|
23
|
+
this.batchHeaders = options.headers ?? (([options2, ...rest]) => {
|
|
24
|
+
const headers = {};
|
|
25
|
+
for (const [key, value2] of Object.entries(options2.request.headers)) {
|
|
26
|
+
if (rest.every((item) => item.request.headers[key] === value2)) {
|
|
27
|
+
headers[key] = value2;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return headers;
|
|
31
|
+
});
|
|
32
|
+
this.mapRequestItem = options.mapRequestItem ?? (({ request, batchHeaders }) => {
|
|
33
|
+
const headers = {};
|
|
34
|
+
for (const [key, value2] of Object.entries(request.headers)) {
|
|
35
|
+
if (batchHeaders[key] !== value2) {
|
|
36
|
+
headers[key] = value2;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
method: request.method,
|
|
41
|
+
url: request.url,
|
|
42
|
+
headers,
|
|
43
|
+
body: request.body,
|
|
44
|
+
signal: request.signal
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
this.exclude = options.exclude ?? (() => false);
|
|
48
|
+
}
|
|
49
|
+
init(options) {
|
|
50
|
+
options.clientInterceptors ??= [];
|
|
51
|
+
options.clientInterceptors.push((options2) => {
|
|
52
|
+
if (options2.request.headers["x-orpc-batch"] !== "1") {
|
|
53
|
+
return options2.next();
|
|
54
|
+
}
|
|
55
|
+
return options2.next({
|
|
56
|
+
...options2,
|
|
57
|
+
request: {
|
|
58
|
+
...options2.request,
|
|
59
|
+
headers: {
|
|
60
|
+
...options2.request.headers,
|
|
61
|
+
"x-orpc-batch": void 0
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
options.clientInterceptors.push((options2) => {
|
|
67
|
+
if (this.exclude(options2) || options2.request.body instanceof Blob || options2.request.body instanceof FormData || isAsyncIteratorObject(options2.request.body) || options2.request.signal?.aborted) {
|
|
68
|
+
return options2.next();
|
|
69
|
+
}
|
|
70
|
+
const group = this.groups.find((group2) => group2.condition(options2));
|
|
71
|
+
if (!group) {
|
|
72
|
+
return options2.next();
|
|
73
|
+
}
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
this.#enqueueRequest(group, options2, resolve, reject);
|
|
76
|
+
defer(() => this.#processPendingBatches());
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
#enqueueRequest(group, options, resolve, reject) {
|
|
81
|
+
const items = this.pending.get(group);
|
|
82
|
+
if (items) {
|
|
83
|
+
items.push([options, resolve, reject]);
|
|
84
|
+
} else {
|
|
85
|
+
this.pending.set(group, [[options, resolve, reject]]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async #processPendingBatches() {
|
|
89
|
+
const pending = this.pending;
|
|
90
|
+
this.pending = /* @__PURE__ */ new Map();
|
|
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
|
+
const batchItems = groupItems;
|
|
103
|
+
if (batchItems.length === 1) {
|
|
104
|
+
batchItems[0][0].next().then(batchItems[0][1]).catch(batchItems[0][2]);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
const options = batchItems.map(([options2]) => options2);
|
|
109
|
+
const maxSize = await value(this.maxSize, options);
|
|
110
|
+
if (batchItems.length > maxSize) {
|
|
111
|
+
const [first, second] = splitInHalf(batchItems);
|
|
112
|
+
this.#executeBatch(method, group, first);
|
|
113
|
+
this.#executeBatch(method, group, second);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const batchUrl = new URL(await value(this.batchUrl, options));
|
|
117
|
+
const batchHeaders = await value(this.batchHeaders, options);
|
|
118
|
+
const mappedItems = batchItems.map(([options2]) => this.mapRequestItem({ ...options2, batchUrl, batchHeaders }));
|
|
119
|
+
const batchRequest = toBatchRequest({
|
|
120
|
+
method,
|
|
121
|
+
url: batchUrl,
|
|
122
|
+
headers: batchHeaders,
|
|
123
|
+
requests: mappedItems
|
|
124
|
+
});
|
|
125
|
+
const maxUrlLength = await value(this.maxUrlLength, options);
|
|
126
|
+
if (batchRequest.url.toString().length > maxUrlLength) {
|
|
127
|
+
const [first, second] = splitInHalf(batchItems);
|
|
128
|
+
this.#executeBatch(method, group, first);
|
|
129
|
+
this.#executeBatch(method, group, second);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const mode = value(this.mode, options);
|
|
133
|
+
try {
|
|
134
|
+
const lazyResponse = await options[0].next({
|
|
135
|
+
request: { ...batchRequest, headers: { ...batchRequest.headers, "x-orpc-batch": mode } },
|
|
136
|
+
signal: batchRequest.signal,
|
|
137
|
+
context: group.context,
|
|
138
|
+
input: group.input,
|
|
139
|
+
path: toArray(group.path)
|
|
140
|
+
});
|
|
141
|
+
const parsed = parseBatchResponse({ ...lazyResponse, body: await lazyResponse.body() });
|
|
142
|
+
for await (const item of parsed) {
|
|
143
|
+
batchItems[item.index]?.[1]({ ...item, body: () => Promise.resolve(item.body) });
|
|
144
|
+
}
|
|
145
|
+
} catch (err) {
|
|
146
|
+
if (batchRequest.signal?.aborted && batchRequest.signal.reason === err) {
|
|
147
|
+
for (const [{ signal }, , reject] of batchItems) {
|
|
148
|
+
if (signal?.aborted) {
|
|
149
|
+
reject(signal.reason);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
throw err;
|
|
154
|
+
}
|
|
155
|
+
throw new Error("Something went wrong make batch response not contains enough responses. This can be a bug please report it.");
|
|
156
|
+
} catch (error) {
|
|
157
|
+
for (const [, , reject] of batchItems) {
|
|
158
|
+
reject(error);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
class DedupeRequestsPlugin {
|
|
165
|
+
#groups;
|
|
166
|
+
#filter;
|
|
167
|
+
order = 4e6;
|
|
168
|
+
// make sure execute before batch plugin
|
|
169
|
+
#queue = /* @__PURE__ */ new Map();
|
|
170
|
+
constructor(options) {
|
|
171
|
+
this.#groups = options.groups;
|
|
172
|
+
this.#filter = options.filter ?? (({ request }) => request.method === "GET");
|
|
173
|
+
}
|
|
174
|
+
init(options) {
|
|
175
|
+
options.clientInterceptors ??= [];
|
|
176
|
+
options.clientInterceptors.push((options2) => {
|
|
177
|
+
if (options2.request.body instanceof Blob || options2.request.body instanceof FormData || options2.request.body instanceof URLSearchParams || isAsyncIteratorObject(options2.request.body) || !this.#filter(options2)) {
|
|
178
|
+
return options2.next();
|
|
179
|
+
}
|
|
180
|
+
const group = this.#groups.find((group2) => group2.condition(options2));
|
|
181
|
+
if (!group) {
|
|
182
|
+
return options2.next();
|
|
183
|
+
}
|
|
184
|
+
return new Promise((resolve, reject) => {
|
|
185
|
+
this.#enqueue(group, options2, resolve, reject);
|
|
186
|
+
defer(() => this.#dequeue());
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
#enqueue(group, options, resolve, reject) {
|
|
191
|
+
let queue = this.#queue.get(group);
|
|
192
|
+
if (!queue) {
|
|
193
|
+
this.#queue.set(group, queue = []);
|
|
194
|
+
}
|
|
195
|
+
const matched = queue.find((item) => {
|
|
196
|
+
const requestString1 = stringifyJSON({
|
|
197
|
+
body: item.options.request.body,
|
|
198
|
+
headers: item.options.request.headers,
|
|
199
|
+
method: item.options.request.method,
|
|
200
|
+
url: item.options.request.url
|
|
201
|
+
});
|
|
202
|
+
const requestString2 = stringifyJSON({
|
|
203
|
+
body: options.request.body,
|
|
204
|
+
headers: options.request.headers,
|
|
205
|
+
method: options.request.method,
|
|
206
|
+
url: options.request.url
|
|
207
|
+
});
|
|
208
|
+
return requestString1 === requestString2;
|
|
209
|
+
});
|
|
210
|
+
if (matched) {
|
|
211
|
+
matched.signals.push(options.request.signal);
|
|
212
|
+
matched.resolves.push(resolve);
|
|
213
|
+
matched.rejects.push(reject);
|
|
214
|
+
} else {
|
|
215
|
+
queue.push({
|
|
216
|
+
options,
|
|
217
|
+
signals: [options.request.signal],
|
|
218
|
+
resolves: [resolve],
|
|
219
|
+
rejects: [reject]
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
async #dequeue() {
|
|
224
|
+
const promises = [];
|
|
225
|
+
for (const [group, items] of this.#queue) {
|
|
226
|
+
for (const { options, signals, resolves, rejects } of items) {
|
|
227
|
+
promises.push(
|
|
228
|
+
this.#execute(group, options, signals, resolves, rejects)
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
this.#queue.clear();
|
|
233
|
+
await Promise.all(promises);
|
|
234
|
+
}
|
|
235
|
+
async #execute(group, options, signals, resolves, rejects) {
|
|
236
|
+
try {
|
|
237
|
+
const dedupedRequest = {
|
|
238
|
+
...options.request,
|
|
239
|
+
signal: toBatchAbortSignal(signals)
|
|
240
|
+
};
|
|
241
|
+
const response = await options.next({
|
|
242
|
+
...options,
|
|
243
|
+
request: dedupedRequest,
|
|
244
|
+
signal: dedupedRequest.signal,
|
|
245
|
+
context: group.context
|
|
246
|
+
});
|
|
247
|
+
const replicatedResponses = replicateStandardLazyResponse(response, resolves.length);
|
|
248
|
+
for (const resolve of resolves) {
|
|
249
|
+
resolve(replicatedResponses.shift());
|
|
250
|
+
}
|
|
251
|
+
} catch (error) {
|
|
252
|
+
for (const reject of rejects) {
|
|
253
|
+
reject(error);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
3
258
|
|
|
4
259
|
class ClientRetryPluginInvalidEventIteratorRetryResponse extends Error {
|
|
5
260
|
}
|
|
@@ -8,6 +263,7 @@ class ClientRetryPlugin {
|
|
|
8
263
|
defaultRetryDelay;
|
|
9
264
|
defaultShouldRetry;
|
|
10
265
|
defaultOnRetry;
|
|
266
|
+
order = 18e5;
|
|
11
267
|
constructor(options = {}) {
|
|
12
268
|
this.defaultRetry = options.default?.retry ?? 0;
|
|
13
269
|
this.defaultRetryDelay = options.default?.retryDelay ?? ((o) => o.lastEventRetry ?? 2e3);
|
|
@@ -18,75 +274,56 @@ class ClientRetryPlugin {
|
|
|
18
274
|
options.interceptors ??= [];
|
|
19
275
|
options.interceptors.push(async (interceptorOptions) => {
|
|
20
276
|
const maxAttempts = await value(
|
|
21
|
-
interceptorOptions.
|
|
22
|
-
interceptorOptions
|
|
23
|
-
interceptorOptions.path,
|
|
24
|
-
interceptorOptions.input
|
|
277
|
+
interceptorOptions.context.retry ?? this.defaultRetry,
|
|
278
|
+
interceptorOptions
|
|
25
279
|
);
|
|
26
|
-
const retryDelay = interceptorOptions.
|
|
27
|
-
const shouldRetry = interceptorOptions.
|
|
28
|
-
const onRetry = interceptorOptions.
|
|
280
|
+
const retryDelay = interceptorOptions.context.retryDelay ?? this.defaultRetryDelay;
|
|
281
|
+
const shouldRetry = interceptorOptions.context.shouldRetry ?? this.defaultShouldRetry;
|
|
282
|
+
const onRetry = interceptorOptions.context.onRetry ?? this.defaultOnRetry;
|
|
29
283
|
if (maxAttempts <= 0) {
|
|
30
284
|
return interceptorOptions.next();
|
|
31
285
|
}
|
|
32
|
-
let lastEventId = interceptorOptions.
|
|
286
|
+
let lastEventId = interceptorOptions.lastEventId;
|
|
33
287
|
let lastEventRetry;
|
|
34
|
-
let
|
|
288
|
+
let callback;
|
|
35
289
|
let attemptIndex = 0;
|
|
36
|
-
const next = async (
|
|
37
|
-
let
|
|
290
|
+
const next = async (initialError) => {
|
|
291
|
+
let currentError = initialError;
|
|
38
292
|
while (true) {
|
|
39
|
-
const
|
|
40
|
-
if (
|
|
293
|
+
const updatedInterceptorOptions = { ...interceptorOptions, lastEventId };
|
|
294
|
+
if (currentError) {
|
|
41
295
|
if (attemptIndex >= maxAttempts) {
|
|
42
|
-
throw
|
|
296
|
+
throw currentError.error;
|
|
43
297
|
}
|
|
44
298
|
const attemptOptions = {
|
|
299
|
+
...updatedInterceptorOptions,
|
|
45
300
|
attemptIndex,
|
|
46
|
-
error:
|
|
47
|
-
lastEventId,
|
|
301
|
+
error: currentError.error,
|
|
48
302
|
lastEventRetry
|
|
49
303
|
};
|
|
50
304
|
const shouldRetryBool = await value(
|
|
51
305
|
shouldRetry,
|
|
52
|
-
attemptOptions
|
|
53
|
-
newClientOptions,
|
|
54
|
-
interceptorOptions.path,
|
|
55
|
-
interceptorOptions.input
|
|
306
|
+
attemptOptions
|
|
56
307
|
);
|
|
57
308
|
if (!shouldRetryBool) {
|
|
58
|
-
throw
|
|
309
|
+
throw currentError.error;
|
|
59
310
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
newClientOptions,
|
|
63
|
-
interceptorOptions.path,
|
|
64
|
-
interceptorOptions.input
|
|
65
|
-
);
|
|
66
|
-
const retryDelayMs = await value(
|
|
67
|
-
retryDelay,
|
|
68
|
-
attemptOptions,
|
|
69
|
-
newClientOptions,
|
|
70
|
-
interceptorOptions.path,
|
|
71
|
-
interceptorOptions.input
|
|
72
|
-
);
|
|
311
|
+
callback = onRetry?.(attemptOptions);
|
|
312
|
+
const retryDelayMs = await value(retryDelay, attemptOptions);
|
|
73
313
|
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
|
|
74
314
|
attemptIndex++;
|
|
75
315
|
}
|
|
76
316
|
try {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
options: newClientOptions
|
|
80
|
-
});
|
|
81
|
-
return output2;
|
|
317
|
+
currentError = void 0;
|
|
318
|
+
return await interceptorOptions.next(updatedInterceptorOptions);
|
|
82
319
|
} catch (error) {
|
|
83
|
-
|
|
320
|
+
currentError = { error };
|
|
321
|
+
if (updatedInterceptorOptions.signal?.aborted) {
|
|
84
322
|
throw error;
|
|
85
323
|
}
|
|
86
|
-
current = { error };
|
|
87
324
|
} finally {
|
|
88
|
-
|
|
89
|
-
|
|
325
|
+
callback?.(!currentError);
|
|
326
|
+
callback = void 0;
|
|
90
327
|
}
|
|
91
328
|
}
|
|
92
329
|
};
|
|
@@ -94,19 +331,17 @@ class ClientRetryPlugin {
|
|
|
94
331
|
if (!isAsyncIteratorObject(output)) {
|
|
95
332
|
return output;
|
|
96
333
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
334
|
+
let current = output;
|
|
335
|
+
let isIteratorAborted = false;
|
|
336
|
+
return overlayProxy(() => current, new AsyncIteratorClass(
|
|
337
|
+
async () => {
|
|
100
338
|
while (true) {
|
|
101
339
|
try {
|
|
102
340
|
const item = await current.next();
|
|
103
341
|
const meta = getEventMeta(item.value);
|
|
104
342
|
lastEventId = meta?.id ?? lastEventId;
|
|
105
343
|
lastEventRetry = meta?.retry ?? lastEventRetry;
|
|
106
|
-
|
|
107
|
-
return item.value;
|
|
108
|
-
}
|
|
109
|
-
yield item.value;
|
|
344
|
+
return item;
|
|
110
345
|
} catch (error) {
|
|
111
346
|
const meta = getEventMeta(error);
|
|
112
347
|
lastEventId = meta?.id ?? lastEventId;
|
|
@@ -118,14 +353,55 @@ class ClientRetryPlugin {
|
|
|
118
353
|
);
|
|
119
354
|
}
|
|
120
355
|
current = maybeEventIterator;
|
|
356
|
+
if (isIteratorAborted) {
|
|
357
|
+
await current.return?.();
|
|
358
|
+
throw error;
|
|
359
|
+
}
|
|
121
360
|
}
|
|
122
361
|
}
|
|
123
|
-
}
|
|
124
|
-
|
|
362
|
+
},
|
|
363
|
+
async (reason) => {
|
|
364
|
+
isIteratorAborted = true;
|
|
365
|
+
if (reason !== "next") {
|
|
366
|
+
await current.return?.();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
));
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
class SimpleCsrfProtectionLinkPlugin {
|
|
375
|
+
headerName;
|
|
376
|
+
headerValue;
|
|
377
|
+
exclude;
|
|
378
|
+
constructor(options = {}) {
|
|
379
|
+
this.headerName = options.headerName ?? "x-csrf-token";
|
|
380
|
+
this.headerValue = options.headerValue ?? "orpc";
|
|
381
|
+
this.exclude = options.exclude ?? false;
|
|
382
|
+
}
|
|
383
|
+
order = 8e6;
|
|
384
|
+
init(options) {
|
|
385
|
+
options.clientInterceptors ??= [];
|
|
386
|
+
options.clientInterceptors.push(async (options2) => {
|
|
387
|
+
const excluded = await value(this.exclude, options2);
|
|
388
|
+
if (excluded) {
|
|
389
|
+
return options2.next();
|
|
390
|
+
}
|
|
391
|
+
const headerName = await value(this.headerName, options2);
|
|
392
|
+
const headerValue = await value(this.headerValue, options2);
|
|
393
|
+
return options2.next({
|
|
394
|
+
...options2,
|
|
395
|
+
request: {
|
|
396
|
+
...options2.request,
|
|
397
|
+
headers: {
|
|
398
|
+
...options2.request.headers,
|
|
399
|
+
[headerName]: headerValue
|
|
400
|
+
}
|
|
125
401
|
}
|
|
126
|
-
}
|
|
402
|
+
});
|
|
127
403
|
});
|
|
128
404
|
}
|
|
129
405
|
}
|
|
130
406
|
|
|
131
|
-
export { ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse };
|
|
407
|
+
export { BatchLinkPlugin, ClientRetryPlugin, ClientRetryPluginInvalidEventIteratorRetryResponse, DedupeRequestsPlugin, SimpleCsrfProtectionLinkPlugin };
|
|
@@ -1,36 +1,78 @@
|
|
|
1
|
-
import { toArray, intercept, isObject, value,
|
|
1
|
+
import { toArray, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, intercept, getGlobalOtelConfig, isObject, value, stringifyJSON } from '@orpc/shared';
|
|
2
2
|
import { mergeStandardHeaders, ErrorEvent } from '@orpc/standard-server';
|
|
3
|
-
import { C as COMMON_ORPC_ERROR_DEFS,
|
|
3
|
+
import { C as COMMON_ORPC_ERROR_DEFS, d as isORPCErrorStatus, e as isORPCErrorJson, g as createORPCErrorFromJson, c as ORPCError, m as mapEventIterator, t as toORPCError } from './client.fXmJjmJv.mjs';
|
|
4
|
+
import { toStandardHeaders as toStandardHeaders$1 } from '@orpc/standard-server-fetch';
|
|
4
5
|
|
|
5
|
-
class
|
|
6
|
+
class CompositeStandardLinkPlugin {
|
|
7
|
+
plugins;
|
|
8
|
+
constructor(plugins = []) {
|
|
9
|
+
this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
10
|
+
}
|
|
11
|
+
init(options) {
|
|
12
|
+
for (const plugin of this.plugins) {
|
|
13
|
+
plugin.init?.(options);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
6
16
|
}
|
|
17
|
+
|
|
7
18
|
class StandardLink {
|
|
8
19
|
constructor(codec, sender, options = {}) {
|
|
9
20
|
this.codec = codec;
|
|
10
21
|
this.sender = sender;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
22
|
+
const plugin = new CompositeStandardLinkPlugin(options.plugins);
|
|
23
|
+
plugin.init(options);
|
|
14
24
|
this.interceptors = toArray(options.interceptors);
|
|
15
25
|
this.clientInterceptors = toArray(options.clientInterceptors);
|
|
16
26
|
}
|
|
17
27
|
interceptors;
|
|
18
28
|
clientInterceptors;
|
|
19
29
|
call(path, input, options) {
|
|
20
|
-
return
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
return runWithSpan(
|
|
31
|
+
{ name: `${ORPC_NAME}.${path.join("/")}`, signal: options.signal },
|
|
32
|
+
(span) => {
|
|
33
|
+
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
34
|
+
span?.setAttribute("rpc.method", path.join("."));
|
|
35
|
+
if (isAsyncIteratorObject(input)) {
|
|
36
|
+
input = asyncIteratorWithSpan(
|
|
37
|
+
{ name: "consume_event_iterator_input", signal: options.signal },
|
|
38
|
+
input
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
return intercept(this.interceptors, { ...options, path, input }, async ({ path: path2, input: input2, ...options2 }) => {
|
|
42
|
+
const otelConfig = getGlobalOtelConfig();
|
|
43
|
+
let otelContext;
|
|
44
|
+
const currentSpan = otelConfig?.trace.getActiveSpan() ?? span;
|
|
45
|
+
if (currentSpan && otelConfig) {
|
|
46
|
+
otelContext = otelConfig?.trace.setSpan(otelConfig.context.active(), currentSpan);
|
|
47
|
+
}
|
|
48
|
+
const request = await runWithSpan(
|
|
49
|
+
{ name: "encode_request", context: otelContext },
|
|
50
|
+
() => this.codec.encode(path2, input2, options2)
|
|
51
|
+
);
|
|
52
|
+
const response = await intercept(
|
|
53
|
+
this.clientInterceptors,
|
|
54
|
+
{ ...options2, input: input2, path: path2, request },
|
|
55
|
+
({ input: input3, path: path3, request: request2, ...options3 }) => {
|
|
56
|
+
return runWithSpan(
|
|
57
|
+
{ name: "send_request", signal: options3.signal, context: otelContext },
|
|
58
|
+
() => this.sender.call(request2, options3, path3, input3)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
const output = await runWithSpan(
|
|
63
|
+
{ name: "decode_response", context: otelContext },
|
|
64
|
+
() => this.codec.decode(response, options2, path2, input2)
|
|
65
|
+
);
|
|
66
|
+
if (isAsyncIteratorObject(output)) {
|
|
67
|
+
return asyncIteratorWithSpan(
|
|
68
|
+
{ name: "consume_event_iterator_output", signal: options2.signal },
|
|
69
|
+
output
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
return output;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
31
75
|
);
|
|
32
|
-
const output = await this.codec.decode(response, options, path, input);
|
|
33
|
-
return output;
|
|
34
76
|
}
|
|
35
77
|
}
|
|
36
78
|
|
|
@@ -183,6 +225,12 @@ class StandardRPCJsonSerializer {
|
|
|
183
225
|
function toHttpPath(path) {
|
|
184
226
|
return `/${path.map(encodeURIComponent).join("/")}`;
|
|
185
227
|
}
|
|
228
|
+
function toStandardHeaders(headers) {
|
|
229
|
+
if (typeof headers.forEach === "function") {
|
|
230
|
+
return toStandardHeaders$1(headers);
|
|
231
|
+
}
|
|
232
|
+
return headers;
|
|
233
|
+
}
|
|
186
234
|
function getMalformedResponseErrorCode(status) {
|
|
187
235
|
return Object.entries(COMMON_ORPC_ERROR_DEFS).find(([, def]) => def.status === status)?.[0] ?? "MALFORMED_ORPC_ERROR_RESPONSE";
|
|
188
236
|
}
|
|
@@ -202,13 +250,14 @@ class StandardRPCLinkCodec {
|
|
|
202
250
|
expectedMethod;
|
|
203
251
|
headers;
|
|
204
252
|
async encode(path, input, options) {
|
|
205
|
-
|
|
206
|
-
let headers = await value(this.headers, options, path, input);
|
|
207
|
-
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
208
|
-
const url = new URL(`${baseUrl.toString().replace(/\/$/, "")}${toHttpPath(path)}`);
|
|
253
|
+
let headers = toStandardHeaders(await value(this.headers, options, path, input));
|
|
209
254
|
if (options.lastEventId !== void 0) {
|
|
210
255
|
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
211
256
|
}
|
|
257
|
+
const expectedMethod = await value(this.expectedMethod, options, path, input);
|
|
258
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
259
|
+
const url = new URL(baseUrl);
|
|
260
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${toHttpPath(path)}`;
|
|
212
261
|
const serialized = this.serializer.serialize(input);
|
|
213
262
|
if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
|
|
214
263
|
const maxUrlLength = await value(this.maxUrlLength, options, path, input);
|
|
@@ -252,12 +301,12 @@ class StandardRPCLinkCodec {
|
|
|
252
301
|
}
|
|
253
302
|
})();
|
|
254
303
|
if (!isOk) {
|
|
255
|
-
if (
|
|
256
|
-
throw
|
|
304
|
+
if (isORPCErrorJson(deserialized)) {
|
|
305
|
+
throw createORPCErrorFromJson(deserialized);
|
|
257
306
|
}
|
|
258
307
|
throw new ORPCError(getMalformedResponseErrorCode(response.status), {
|
|
259
308
|
status: response.status,
|
|
260
|
-
data: deserialized
|
|
309
|
+
data: { ...response, body: deserialized }
|
|
261
310
|
});
|
|
262
311
|
}
|
|
263
312
|
return deserialized;
|
|
@@ -307,8 +356,8 @@ class StandardRPCSerializer {
|
|
|
307
356
|
return e;
|
|
308
357
|
}
|
|
309
358
|
const deserialized = this.#deserialize(e.data);
|
|
310
|
-
if (
|
|
311
|
-
return
|
|
359
|
+
if (isORPCErrorJson(deserialized)) {
|
|
360
|
+
return createORPCErrorFromJson(deserialized, { cause: e });
|
|
312
361
|
}
|
|
313
362
|
return new ErrorEvent({
|
|
314
363
|
data: deserialized,
|
|
@@ -320,6 +369,9 @@ class StandardRPCSerializer {
|
|
|
320
369
|
return this.#deserialize(data);
|
|
321
370
|
}
|
|
322
371
|
#deserialize(data) {
|
|
372
|
+
if (data === void 0) {
|
|
373
|
+
return void 0;
|
|
374
|
+
}
|
|
323
375
|
if (!(data instanceof FormData)) {
|
|
324
376
|
return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
|
|
325
377
|
}
|
|
@@ -333,4 +385,13 @@ class StandardRPCSerializer {
|
|
|
333
385
|
}
|
|
334
386
|
}
|
|
335
387
|
|
|
336
|
-
|
|
388
|
+
class StandardRPCLink extends StandardLink {
|
|
389
|
+
constructor(linkClient, options) {
|
|
390
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
|
391
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
|
392
|
+
const linkCodec = new StandardRPCLinkCodec(serializer, options);
|
|
393
|
+
super(linkCodec, linkClient, options);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export { CompositeStandardLinkPlugin as C, StandardLink as S, STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES as a, StandardRPCJsonSerializer as b, StandardRPCLink as c, StandardRPCLinkCodec as d, StandardRPCSerializer as e, toStandardHeaders as f, getMalformedResponseErrorCode as g, toHttpPath as t };
|