@orpc/client 1.14.9 → 1.14.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +52 -68
  2. package/dist/adapters/fetch/index.d.mts +54 -35
  3. package/dist/adapters/fetch/index.d.ts +54 -35
  4. package/dist/adapters/fetch/index.mjs +74 -27
  5. package/dist/adapters/message-port/index.d.mts +25 -30
  6. package/dist/adapters/message-port/index.d.ts +25 -30
  7. package/dist/adapters/message-port/index.mjs +39 -30
  8. package/dist/adapters/standard/index.d.mts +59 -9
  9. package/dist/adapters/standard/index.d.ts +59 -9
  10. package/dist/adapters/standard/index.mjs +5 -5
  11. package/dist/adapters/websocket/index.d.mts +113 -21
  12. package/dist/adapters/websocket/index.d.ts +113 -21
  13. package/dist/adapters/websocket/index.mjs +95 -37
  14. package/dist/index.d.mts +75 -184
  15. package/dist/index.d.ts +75 -184
  16. package/dist/index.mjs +74 -63
  17. package/dist/plugins/index.d.mts +191 -122
  18. package/dist/plugins/index.d.ts +191 -122
  19. package/dist/plugins/index.mjs +627 -281
  20. package/dist/shared/client.8f4DNmdE.d.mts +96 -0
  21. package/dist/shared/client.BBZBQID8.d.mts +167 -0
  22. package/dist/shared/client.BBZBQID8.d.ts +167 -0
  23. package/dist/shared/client.BMKYqpdy.d.ts +96 -0
  24. package/dist/shared/client.BRJnOJ0R.mjs +174 -0
  25. package/dist/shared/client.BdItY5DT.d.mts +111 -0
  26. package/dist/shared/client.BdItY5DT.d.ts +111 -0
  27. package/dist/shared/client.Dnfj8jnT.mjs +92 -0
  28. package/dist/shared/client.DqYwRDUO.mjs +343 -0
  29. package/package.json +7 -7
  30. package/dist/shared/client.2jUAqzYU.d.ts +0 -45
  31. package/dist/shared/client.B3pNRBih.d.ts +0 -91
  32. package/dist/shared/client.BFAVy68H.d.mts +0 -91
  33. package/dist/shared/client.BLtwTQUg.mjs +0 -40
  34. package/dist/shared/client.CFQL4ewg.mjs +0 -174
  35. package/dist/shared/client.CpCa3si8.d.mts +0 -45
  36. package/dist/shared/client.D2nvAALa.mjs +0 -404
  37. package/dist/shared/client.i2uoJbEp.d.mts +0 -83
  38. package/dist/shared/client.i2uoJbEp.d.ts +0 -83
@@ -1,174 +0,0 @@
1
- import { resolveMaybeOptionalOptions, getConstructor, isObject } from '@orpc/shared';
2
-
3
- const ORPC_CLIENT_PACKAGE_NAME = "@orpc/client";
4
- const ORPC_CLIENT_PACKAGE_VERSION = "1.14.9";
5
-
6
- const COMMON_ORPC_ERROR_DEFS = {
7
- BAD_REQUEST: {
8
- status: 400,
9
- message: "Bad Request"
10
- },
11
- UNAUTHORIZED: {
12
- status: 401,
13
- message: "Unauthorized"
14
- },
15
- FORBIDDEN: {
16
- status: 403,
17
- message: "Forbidden"
18
- },
19
- NOT_FOUND: {
20
- status: 404,
21
- message: "Not Found"
22
- },
23
- METHOD_NOT_SUPPORTED: {
24
- status: 405,
25
- message: "Method Not Supported"
26
- },
27
- NOT_ACCEPTABLE: {
28
- status: 406,
29
- message: "Not Acceptable"
30
- },
31
- TIMEOUT: {
32
- status: 408,
33
- message: "Request Timeout"
34
- },
35
- CONFLICT: {
36
- status: 409,
37
- message: "Conflict"
38
- },
39
- PRECONDITION_FAILED: {
40
- status: 412,
41
- message: "Precondition Failed"
42
- },
43
- PAYLOAD_TOO_LARGE: {
44
- status: 413,
45
- message: "Payload Too Large"
46
- },
47
- UNSUPPORTED_MEDIA_TYPE: {
48
- status: 415,
49
- message: "Unsupported Media Type"
50
- },
51
- UNPROCESSABLE_CONTENT: {
52
- status: 422,
53
- message: "Unprocessable Content"
54
- },
55
- TOO_MANY_REQUESTS: {
56
- status: 429,
57
- message: "Too Many Requests"
58
- },
59
- CLIENT_CLOSED_REQUEST: {
60
- status: 499,
61
- message: "Client Closed Request"
62
- },
63
- INTERNAL_SERVER_ERROR: {
64
- status: 500,
65
- message: "Internal Server Error"
66
- },
67
- NOT_IMPLEMENTED: {
68
- status: 501,
69
- message: "Not Implemented"
70
- },
71
- BAD_GATEWAY: {
72
- status: 502,
73
- message: "Bad Gateway"
74
- },
75
- SERVICE_UNAVAILABLE: {
76
- status: 503,
77
- message: "Service Unavailable"
78
- },
79
- GATEWAY_TIMEOUT: {
80
- status: 504,
81
- message: "Gateway Timeout"
82
- }
83
- };
84
- function fallbackORPCErrorStatus(code, status) {
85
- return status ?? COMMON_ORPC_ERROR_DEFS[code]?.status ?? 500;
86
- }
87
- function fallbackORPCErrorMessage(code, message) {
88
- return message || COMMON_ORPC_ERROR_DEFS[code]?.message || code;
89
- }
90
- let globalORPCErrorConstructors;
91
- class ORPCError extends Error {
92
- defined;
93
- code;
94
- status;
95
- data;
96
- static {
97
- const GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL = Symbol.for(`__${ORPC_CLIENT_PACKAGE_NAME}@${ORPC_CLIENT_PACKAGE_VERSION}/error/ORPC_ERROR_CONSTRUCTORS__`);
98
- void (globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL] ??= /* @__PURE__ */ new WeakSet());
99
- globalORPCErrorConstructors = globalThis[GLOBAL_ORPC_ERROR_CONSTRUCTORS_SYMBOL];
100
- globalORPCErrorConstructors.add(ORPCError);
101
- }
102
- constructor(code, ...rest) {
103
- const options = resolveMaybeOptionalOptions(rest);
104
- if (options.status !== void 0 && !isORPCErrorStatus(options.status)) {
105
- throw new Error("[ORPCError] Invalid error status code.");
106
- }
107
- const message = fallbackORPCErrorMessage(code, options.message);
108
- super(message, options);
109
- this.code = code;
110
- this.status = fallbackORPCErrorStatus(code, options.status);
111
- this.defined = options.defined ?? false;
112
- this.data = options.data;
113
- }
114
- toJSON() {
115
- return {
116
- defined: this.defined,
117
- code: this.code,
118
- status: this.status,
119
- message: this.message,
120
- data: this.data
121
- };
122
- }
123
- /**
124
- * Workaround for Next.js where different contexts use separate
125
- * dependency graphs, causing multiple ORPCError constructors existing and breaking
126
- * `instanceof` checks across contexts.
127
- *
128
- * This is particularly problematic with "Optimized SSR", where orpc-client
129
- * executes in one context but is invoked from another. When an error is thrown
130
- * in the execution context, `instanceof ORPCError` checks fail in the
131
- * invocation context due to separate class constructors.
132
- *
133
- * @todo Remove this and related code if Next.js resolves the multiple dependency graph issue.
134
- */
135
- static [Symbol.hasInstance](instance) {
136
- if (globalORPCErrorConstructors.has(this)) {
137
- const constructor = getConstructor(instance);
138
- if (constructor && globalORPCErrorConstructors.has(constructor)) {
139
- return true;
140
- }
141
- }
142
- return super[Symbol.hasInstance](instance);
143
- }
144
- }
145
- function isDefinedError(error) {
146
- return error instanceof ORPCError && error.defined;
147
- }
148
- function toORPCError(error) {
149
- return error instanceof ORPCError ? error : new ORPCError("INTERNAL_SERVER_ERROR", {
150
- message: "Internal server error",
151
- cause: error
152
- });
153
- }
154
- function isORPCErrorStatus(status) {
155
- return status < 200 || status >= 400;
156
- }
157
- function isORPCErrorJson(json) {
158
- if (!isObject(json)) {
159
- return false;
160
- }
161
- const validKeys = ["defined", "code", "status", "message", "data"];
162
- if (Object.keys(json).some((k) => !validKeys.includes(k))) {
163
- return false;
164
- }
165
- return "defined" in json && typeof json.defined === "boolean" && "code" in json && typeof json.code === "string" && "status" in json && typeof json.status === "number" && isORPCErrorStatus(json.status) && "message" in json && typeof json.message === "string";
166
- }
167
- function createORPCErrorFromJson(json, options = {}) {
168
- return new ORPCError(json.code, {
169
- ...options,
170
- ...json
171
- });
172
- }
173
-
174
- export { COMMON_ORPC_ERROR_DEFS as C, ORPC_CLIENT_PACKAGE_NAME as O, ORPC_CLIENT_PACKAGE_VERSION as a, fallbackORPCErrorMessage as b, ORPCError as c, isORPCErrorStatus as d, isORPCErrorJson as e, fallbackORPCErrorStatus as f, createORPCErrorFromJson as g, isDefinedError as i, toORPCError as t };
@@ -1,45 +0,0 @@
1
- import { Interceptor } from '@orpc/shared';
2
- import { StandardRequest, StandardLazyResponse } from '@orpc/standard-server';
3
- import { b as ClientContext, c as ClientOptions, C as ClientLink } from './client.i2uoJbEp.mjs';
4
-
5
- interface StandardLinkPlugin<T extends ClientContext> {
6
- order?: number;
7
- init?(options: StandardLinkOptions<T>): void;
8
- }
9
- declare class CompositeStandardLinkPlugin<T extends ClientContext, TPlugin extends StandardLinkPlugin<T>> implements StandardLinkPlugin<T> {
10
- protected readonly plugins: TPlugin[];
11
- constructor(plugins?: readonly TPlugin[]);
12
- init(options: StandardLinkOptions<T>): void;
13
- }
14
-
15
- interface StandardLinkCodec<T extends ClientContext> {
16
- encode(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<StandardRequest>;
17
- decode(response: StandardLazyResponse, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<unknown>;
18
- }
19
- interface StandardLinkClient<T extends ClientContext> {
20
- call(request: StandardRequest, options: ClientOptions<T>, path: readonly string[], input: unknown): Promise<StandardLazyResponse>;
21
- }
22
-
23
- interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
24
- path: readonly string[];
25
- input: unknown;
26
- }
27
- interface StandardLinkClientInterceptorOptions<T extends ClientContext> extends StandardLinkInterceptorOptions<T> {
28
- request: StandardRequest;
29
- }
30
- interface StandardLinkOptions<T extends ClientContext> {
31
- interceptors?: Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>[];
32
- clientInterceptors?: Interceptor<StandardLinkClientInterceptorOptions<T>, Promise<StandardLazyResponse>>[];
33
- plugins?: StandardLinkPlugin<T>[];
34
- }
35
- declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
36
- readonly codec: StandardLinkCodec<T>;
37
- readonly sender: StandardLinkClient<T>;
38
- private readonly interceptors;
39
- private readonly clientInterceptors;
40
- constructor(codec: StandardLinkCodec<T>, sender: StandardLinkClient<T>, options?: StandardLinkOptions<T>);
41
- call(path: readonly string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
42
- }
43
-
44
- export { CompositeStandardLinkPlugin as C, StandardLink as d };
45
- export type { StandardLinkClientInterceptorOptions as S, StandardLinkPlugin as a, StandardLinkOptions as b, StandardLinkInterceptorOptions as c, StandardLinkCodec as e, StandardLinkClient as f };
@@ -1,404 +0,0 @@
1
- import { toArray, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, intercept, getGlobalOtelConfig, isObject, value, stringifyJSON } from '@orpc/shared';
2
- import { mergeStandardHeaders, ErrorEvent } from '@orpc/standard-server';
3
- import { C as COMMON_ORPC_ERROR_DEFS, d as isORPCErrorStatus, e as isORPCErrorJson, g as createORPCErrorFromJson, c as ORPCError, t as toORPCError } from './client.CFQL4ewg.mjs';
4
- import { toStandardHeaders as toStandardHeaders$1 } from '@orpc/standard-server-fetch';
5
- import { m as mapEventIterator } from './client.BLtwTQUg.mjs';
6
-
7
- class CompositeStandardLinkPlugin {
8
- plugins;
9
- constructor(plugins = []) {
10
- this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
11
- }
12
- init(options) {
13
- for (const plugin of this.plugins) {
14
- plugin.init?.(options);
15
- }
16
- }
17
- }
18
-
19
- class StandardLink {
20
- constructor(codec, sender, options = {}) {
21
- this.codec = codec;
22
- this.sender = sender;
23
- const plugin = new CompositeStandardLinkPlugin(options.plugins);
24
- plugin.init(options);
25
- this.interceptors = toArray(options.interceptors);
26
- this.clientInterceptors = toArray(options.clientInterceptors);
27
- }
28
- interceptors;
29
- clientInterceptors;
30
- call(path, input, options) {
31
- return runWithSpan(
32
- { name: `${ORPC_NAME}.${path.join("/")}`, signal: options.signal },
33
- (span) => {
34
- span?.setAttribute("rpc.system", ORPC_NAME);
35
- span?.setAttribute("rpc.method", path.join("."));
36
- if (isAsyncIteratorObject(input)) {
37
- input = asyncIteratorWithSpan(
38
- { name: "consume_event_iterator_input", signal: options.signal },
39
- input
40
- );
41
- }
42
- return intercept(this.interceptors, { ...options, path, input }, async ({ path: path2, input: input2, ...options2 }) => {
43
- const otelConfig = getGlobalOtelConfig();
44
- let otelContext;
45
- const currentSpan = otelConfig?.trace.getActiveSpan() ?? span;
46
- if (currentSpan && otelConfig) {
47
- otelContext = otelConfig?.trace.setSpan(otelConfig.context.active(), currentSpan);
48
- }
49
- const request = await runWithSpan(
50
- { name: "encode_request", context: otelContext },
51
- () => this.codec.encode(path2, input2, options2)
52
- );
53
- const response = await intercept(
54
- this.clientInterceptors,
55
- { ...options2, input: input2, path: path2, request },
56
- ({ input: input3, path: path3, request: request2, ...options3 }) => {
57
- return runWithSpan(
58
- { name: "send_request", signal: options3.signal, context: otelContext },
59
- () => this.sender.call(request2, options3, path3, input3)
60
- );
61
- }
62
- );
63
- const output = await runWithSpan(
64
- { name: "decode_response", context: otelContext },
65
- () => this.codec.decode(response, options2, path2, input2)
66
- );
67
- if (isAsyncIteratorObject(output)) {
68
- return asyncIteratorWithSpan(
69
- { name: "consume_event_iterator_output", signal: options2.signal },
70
- output
71
- );
72
- }
73
- return output;
74
- });
75
- }
76
- );
77
- }
78
- }
79
-
80
- const STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES = {
81
- BIGINT: 0,
82
- DATE: 1,
83
- NAN: 2,
84
- UNDEFINED: 3,
85
- URL: 4,
86
- REGEXP: 5,
87
- SET: 6,
88
- MAP: 7
89
- };
90
- class StandardRPCJsonSerializer {
91
- customSerializers;
92
- constructor(options = {}) {
93
- this.customSerializers = options.customJsonSerializers ?? [];
94
- if (this.customSerializers.length !== new Set(this.customSerializers.map((custom) => custom.type)).size) {
95
- throw new Error("Custom serializer type must be unique.");
96
- }
97
- }
98
- serialize(data, segments = [], meta = [], maps = [], blobs = []) {
99
- for (const custom of this.customSerializers) {
100
- if (custom.condition(data)) {
101
- const result = this.serialize(custom.serialize(data), segments, meta, maps, blobs);
102
- meta.push([custom.type, ...segments]);
103
- return result;
104
- }
105
- }
106
- if (data instanceof Blob) {
107
- maps.push(segments);
108
- blobs.push(data);
109
- return [data, meta, maps, blobs];
110
- }
111
- if (typeof data === "bigint") {
112
- meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.BIGINT, ...segments]);
113
- return [data.toString(), meta, maps, blobs];
114
- }
115
- if (data instanceof Date) {
116
- meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.DATE, ...segments]);
117
- if (Number.isNaN(data.getTime())) {
118
- return [null, meta, maps, blobs];
119
- }
120
- return [data.toISOString(), meta, maps, blobs];
121
- }
122
- if (Number.isNaN(data)) {
123
- meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.NAN, ...segments]);
124
- return [null, meta, maps, blobs];
125
- }
126
- if (data instanceof URL) {
127
- meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.URL, ...segments]);
128
- return [data.toString(), meta, maps, blobs];
129
- }
130
- if (data instanceof RegExp) {
131
- meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.REGEXP, ...segments]);
132
- return [data.toString(), meta, maps, blobs];
133
- }
134
- if (data instanceof Set) {
135
- const result = this.serialize(Array.from(data), segments, meta, maps, blobs);
136
- meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.SET, ...segments]);
137
- return result;
138
- }
139
- if (data instanceof Map) {
140
- const result = this.serialize(Array.from(data.entries()), segments, meta, maps, blobs);
141
- meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.MAP, ...segments]);
142
- return result;
143
- }
144
- if (Array.isArray(data)) {
145
- const json = data.map((v, i) => {
146
- if (v === void 0) {
147
- meta.push([STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.UNDEFINED, ...segments, i]);
148
- return null;
149
- }
150
- return this.serialize(v, [...segments, i], meta, maps, blobs)[0];
151
- });
152
- return [json, meta, maps, blobs];
153
- }
154
- if (isObject(data)) {
155
- const json = {};
156
- for (const k in data) {
157
- if (k === "toJSON" && typeof data[k] === "function") {
158
- continue;
159
- }
160
- json[k] = this.serialize(data[k], [...segments, k], meta, maps, blobs)[0];
161
- }
162
- return [json, meta, maps, blobs];
163
- }
164
- return [data, meta, maps, blobs];
165
- }
166
- deserialize(json, meta, maps, getBlob) {
167
- const ref = { data: json };
168
- if (maps && getBlob) {
169
- maps.forEach((segments, i) => {
170
- let currentRef = ref;
171
- let preSegment = "data";
172
- segments.forEach((segment) => {
173
- currentRef = currentRef[preSegment];
174
- preSegment = segment;
175
- if (!Object.hasOwn(currentRef, preSegment)) {
176
- throw new Error(`Security error: accessing non-existent path during deserialization. Path segment: ${preSegment}`);
177
- }
178
- });
179
- currentRef[preSegment] = getBlob(i);
180
- });
181
- }
182
- for (const item of meta) {
183
- const type = item[0];
184
- let currentRef = ref;
185
- let preSegment = "data";
186
- for (let i = 1; i < item.length; i++) {
187
- currentRef = currentRef[preSegment];
188
- preSegment = item[i];
189
- if (!Object.hasOwn(currentRef, preSegment)) {
190
- throw new Error(`Security error: accessing non-existent path during deserialization. Path segment: ${preSegment}`);
191
- }
192
- }
193
- for (const custom of this.customSerializers) {
194
- if (custom.type === type) {
195
- currentRef[preSegment] = custom.deserialize(currentRef[preSegment]);
196
- break;
197
- }
198
- }
199
- switch (type) {
200
- case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.BIGINT:
201
- currentRef[preSegment] = BigInt(currentRef[preSegment]);
202
- break;
203
- case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.DATE:
204
- currentRef[preSegment] = new Date(currentRef[preSegment] ?? "Invalid Date");
205
- break;
206
- case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.NAN:
207
- currentRef[preSegment] = Number.NaN;
208
- break;
209
- case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.UNDEFINED:
210
- currentRef[preSegment] = void 0;
211
- break;
212
- case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.URL:
213
- currentRef[preSegment] = new URL(currentRef[preSegment]);
214
- break;
215
- case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.REGEXP: {
216
- const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
217
- currentRef[preSegment] = new RegExp(pattern, flags);
218
- break;
219
- }
220
- case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.SET:
221
- currentRef[preSegment] = new Set(currentRef[preSegment]);
222
- break;
223
- case STANDARD_RPC_JSON_SERIALIZER_BUILT_IN_TYPES.MAP:
224
- currentRef[preSegment] = new Map(currentRef[preSegment]);
225
- break;
226
- }
227
- }
228
- return ref.data;
229
- }
230
- }
231
-
232
- function toHttpPath(path) {
233
- return `/${path.map(encodeURIComponent).join("/")}`;
234
- }
235
- function toStandardHeaders(headers) {
236
- if (typeof headers.forEach === "function") {
237
- return toStandardHeaders$1(headers);
238
- }
239
- return headers;
240
- }
241
- function getMalformedResponseErrorCode(status) {
242
- return Object.entries(COMMON_ORPC_ERROR_DEFS).find(([, def]) => def.status === status)?.[0] ?? "MALFORMED_ORPC_ERROR_RESPONSE";
243
- }
244
-
245
- class StandardRPCLinkCodec {
246
- constructor(serializer, options) {
247
- this.serializer = serializer;
248
- this.baseUrl = options.url;
249
- this.maxUrlLength = options.maxUrlLength ?? 2083;
250
- this.fallbackMethod = options.fallbackMethod ?? "POST";
251
- this.expectedMethod = options.method ?? this.fallbackMethod;
252
- this.headers = options.headers ?? {};
253
- }
254
- baseUrl;
255
- maxUrlLength;
256
- fallbackMethod;
257
- expectedMethod;
258
- headers;
259
- async encode(path, input, options) {
260
- let headers = toStandardHeaders(await value(this.headers, options, path, input));
261
- if (options.lastEventId !== void 0) {
262
- headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
263
- }
264
- const expectedMethod = await value(this.expectedMethod, options, path, input);
265
- const baseUrl = await value(this.baseUrl, options, path, input);
266
- const url = new URL(baseUrl);
267
- url.pathname = `${url.pathname.replace(/\/$/, "")}${toHttpPath(path)}`;
268
- const serialized = this.serializer.serialize(input);
269
- if (expectedMethod === "GET" && !(serialized instanceof FormData) && !isAsyncIteratorObject(serialized)) {
270
- const maxUrlLength = await value(this.maxUrlLength, options, path, input);
271
- const getUrl = new URL(url);
272
- getUrl.searchParams.append("data", stringifyJSON(serialized));
273
- if (getUrl.toString().length <= maxUrlLength) {
274
- return {
275
- body: void 0,
276
- method: expectedMethod,
277
- headers,
278
- url: getUrl,
279
- signal: options.signal
280
- };
281
- }
282
- }
283
- return {
284
- url,
285
- method: expectedMethod === "GET" ? this.fallbackMethod : expectedMethod,
286
- headers,
287
- body: serialized,
288
- signal: options.signal
289
- };
290
- }
291
- async decode(response) {
292
- const isOk = !isORPCErrorStatus(response.status);
293
- const deserialized = await (async () => {
294
- let isBodyOk = false;
295
- try {
296
- const body = await response.body();
297
- isBodyOk = true;
298
- return this.serializer.deserialize(body);
299
- } catch (error) {
300
- if (!isBodyOk) {
301
- throw new Error("Cannot parse response body, please check the response body and content-type.", {
302
- cause: error
303
- });
304
- }
305
- throw new Error("Invalid RPC response format.", {
306
- cause: error
307
- });
308
- }
309
- })();
310
- if (!isOk) {
311
- if (isORPCErrorJson(deserialized)) {
312
- throw createORPCErrorFromJson(deserialized);
313
- }
314
- throw new ORPCError(getMalformedResponseErrorCode(response.status), {
315
- status: response.status,
316
- data: { ...response, body: deserialized }
317
- });
318
- }
319
- return deserialized;
320
- }
321
- }
322
-
323
- class StandardRPCSerializer {
324
- constructor(jsonSerializer) {
325
- this.jsonSerializer = jsonSerializer;
326
- }
327
- serialize(data) {
328
- if (isAsyncIteratorObject(data)) {
329
- return mapEventIterator(data, {
330
- value: async (value) => this.#serialize(value, false),
331
- error: async (e) => {
332
- return new ErrorEvent({
333
- data: this.#serialize(toORPCError(e).toJSON(), false),
334
- cause: e
335
- });
336
- }
337
- });
338
- }
339
- return this.#serialize(data, true);
340
- }
341
- #serialize(data, enableFormData) {
342
- const [json, meta_, maps, blobs] = this.jsonSerializer.serialize(data);
343
- const meta = meta_.length === 0 ? void 0 : meta_;
344
- if (!enableFormData || blobs.length === 0) {
345
- return {
346
- json,
347
- meta
348
- };
349
- }
350
- const form = new FormData();
351
- form.set("data", stringifyJSON({ json, meta, maps }));
352
- blobs.forEach((blob, i) => {
353
- form.set(i.toString(), blob);
354
- });
355
- return form;
356
- }
357
- deserialize(data) {
358
- if (isAsyncIteratorObject(data)) {
359
- return mapEventIterator(data, {
360
- value: async (value) => this.#deserialize(value),
361
- error: async (e) => {
362
- if (!(e instanceof ErrorEvent)) {
363
- return e;
364
- }
365
- const deserialized = this.#deserialize(e.data);
366
- if (isORPCErrorJson(deserialized)) {
367
- return createORPCErrorFromJson(deserialized, { cause: e });
368
- }
369
- return new ErrorEvent({
370
- data: deserialized,
371
- cause: e
372
- });
373
- }
374
- });
375
- }
376
- return this.#deserialize(data);
377
- }
378
- #deserialize(data) {
379
- if (data === void 0) {
380
- return void 0;
381
- }
382
- if (!(data instanceof FormData)) {
383
- return this.jsonSerializer.deserialize(data.json, data.meta ?? []);
384
- }
385
- const serialized = JSON.parse(data.get("data"));
386
- return this.jsonSerializer.deserialize(
387
- serialized.json,
388
- serialized.meta ?? [],
389
- serialized.maps,
390
- (i) => data.get(i.toString())
391
- );
392
- }
393
- }
394
-
395
- class StandardRPCLink extends StandardLink {
396
- constructor(linkClient, options) {
397
- const jsonSerializer = new StandardRPCJsonSerializer(options);
398
- const serializer = new StandardRPCSerializer(jsonSerializer);
399
- const linkCodec = new StandardRPCLinkCodec(serializer, options);
400
- super(linkCodec, linkClient, options);
401
- }
402
- }
403
-
404
- 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 };