@temporary-name/server 1.9.3-alpha.9f176ca3f9388d51e8dffa98a15fcf9f14f6a75e → 1.9.3-alpha.a253b67a3639148c12f13a47295e1922182adecd

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 (39) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +12 -7
  2. package/dist/adapters/aws-lambda/index.d.ts +12 -7
  3. package/dist/adapters/aws-lambda/index.mjs +12 -4
  4. package/dist/adapters/fetch/index.d.mts +12 -7
  5. package/dist/adapters/fetch/index.d.ts +12 -7
  6. package/dist/adapters/fetch/index.mjs +12 -11
  7. package/dist/adapters/node/index.d.mts +12 -7
  8. package/dist/adapters/node/index.d.ts +12 -7
  9. package/dist/adapters/node/index.mjs +12 -11
  10. package/dist/adapters/standard/index.d.mts +27 -13
  11. package/dist/adapters/standard/index.d.ts +27 -13
  12. package/dist/adapters/standard/index.mjs +8 -100
  13. package/dist/helpers/index.mjs +3 -29
  14. package/dist/index.d.mts +62 -583
  15. package/dist/index.d.ts +62 -583
  16. package/dist/index.mjs +212 -446
  17. package/dist/openapi/index.d.mts +220 -0
  18. package/dist/openapi/index.d.ts +220 -0
  19. package/dist/openapi/index.mjs +707 -0
  20. package/dist/plugins/index.d.mts +5 -83
  21. package/dist/plugins/index.d.ts +5 -83
  22. package/dist/plugins/index.mjs +17 -189
  23. package/dist/shared/server.BR0GBxlv.d.ts +23 -0
  24. package/dist/shared/server.C1RJffw4.mjs +30 -0
  25. package/dist/shared/server.C7HccVwN.d.mts +23 -0
  26. package/dist/shared/{server.Btxrgkj5.d.ts → server.CPThlZ_E.d.ts} +9 -27
  27. package/dist/shared/server.D-DR5Z00.mjs +362 -0
  28. package/dist/shared/server.JtIZ8YG7.mjs +237 -0
  29. package/dist/shared/server.oy0285uM.d.mts +252 -0
  30. package/dist/shared/server.oy0285uM.d.ts +252 -0
  31. package/dist/shared/server.rZDPWnA8.mjs +234 -0
  32. package/dist/shared/{server.Bo94xDTv.d.mts → server.y97Td78c.d.mts} +9 -27
  33. package/package.json +17 -22
  34. package/dist/shared/server.BEQrAa3A.mjs +0 -207
  35. package/dist/shared/server.C1YnHvvf.d.mts +0 -192
  36. package/dist/shared/server.C1YnHvvf.d.ts +0 -192
  37. package/dist/shared/server.D6K9uoPI.mjs +0 -35
  38. package/dist/shared/server.DZ5BIITo.mjs +0 -9
  39. package/dist/shared/server.X0YaZxSJ.mjs +0 -13
@@ -0,0 +1,362 @@
1
+ import { mergePrefix, enhanceRoute, ValidationError } from '@temporary-name/contract';
2
+ import { resolveMaybeOptionalOptions, toArray, value, runWithSpan, intercept, isAsyncIteratorObject, overlayProxy, asyncIteratorWithSpan, ORPCError } from '@temporary-name/shared';
3
+ import { HibernationEventIterator, mapEventIterator } from '@temporary-name/standard-server';
4
+ import { safeDecodeAsync, safeEncodeAsync } from '@temporary-name/zod';
5
+
6
+ function isStartWithMiddlewares(middlewares, compare) {
7
+ if (compare.length > middlewares.length) {
8
+ return false;
9
+ }
10
+ for (let i = 0; i < middlewares.length; i++) {
11
+ if (compare[i] === void 0) {
12
+ return true;
13
+ }
14
+ if (middlewares[i] !== compare[i]) {
15
+ return false;
16
+ }
17
+ }
18
+ return true;
19
+ }
20
+ function mergeMiddlewares(first, second, options) {
21
+ if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
22
+ return second;
23
+ }
24
+ return [...first, ...second];
25
+ }
26
+ function addMiddleware(middlewares, addition) {
27
+ return [...middlewares, addition];
28
+ }
29
+
30
+ class Contract {
31
+ /**
32
+ * This property holds the defined options.
33
+ */
34
+ "~orpc";
35
+ constructor(def) {
36
+ this["~orpc"] = def;
37
+ }
38
+ }
39
+ class Procedure extends Contract {
40
+ }
41
+ function isProcedure(item) {
42
+ return item instanceof Procedure || // This is so we'll return true for Proxy-wrapped Procedures e.g. as returned by `callable`
43
+ (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "route" in item["~orpc"] && "meta" in item["~orpc"] && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
44
+ }
45
+
46
+ function mergeCurrentContext(context, other) {
47
+ return { ...context, ...other };
48
+ }
49
+
50
+ function getRouter(router, path) {
51
+ let current = router;
52
+ for (let i = 0; i < path.length; i++) {
53
+ const segment = path[i];
54
+ if (!current) {
55
+ return void 0;
56
+ }
57
+ if (isProcedure(current)) {
58
+ return void 0;
59
+ }
60
+ if (!isLazy(current)) {
61
+ current = current[segment];
62
+ continue;
63
+ }
64
+ const lazied = current;
65
+ const rest = path.slice(i);
66
+ return lazyInternal(async () => {
67
+ const unwrapped = await unlazy(lazied);
68
+ const next = getRouter(unwrapped.default, rest);
69
+ return unlazy(next);
70
+ }, getLazyMeta(lazied));
71
+ }
72
+ return current;
73
+ }
74
+ function createAccessibleLazyRouter(lazied) {
75
+ const recursive = new Proxy(lazied, {
76
+ get(target, key) {
77
+ if (typeof key !== "string") {
78
+ return Reflect.get(target, key);
79
+ }
80
+ const next = getRouter(lazied, [key]);
81
+ return createAccessibleLazyRouter(next);
82
+ }
83
+ });
84
+ return recursive;
85
+ }
86
+ function enhanceRouter(router, options) {
87
+ if (isLazy(router)) {
88
+ const laziedMeta = getLazyMeta(router);
89
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
90
+ const enhanced2 = lazyInternal(
91
+ async () => {
92
+ const { default: unlaziedRouter } = await unlazy(router);
93
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
94
+ return unlazy(enhanced3);
95
+ },
96
+ {
97
+ ...laziedMeta,
98
+ prefix: enhancedPrefix
99
+ }
100
+ );
101
+ const accessible = createAccessibleLazyRouter(enhanced2);
102
+ return accessible;
103
+ }
104
+ if (isProcedure(router)) {
105
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
106
+ dedupeLeading: options.dedupeLeadingMiddlewares
107
+ });
108
+ const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
109
+ const enhanced2 = new Procedure({
110
+ ...router["~orpc"],
111
+ route: enhanceRoute(router["~orpc"].route, options),
112
+ middlewares: newMiddlewares,
113
+ inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
114
+ outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
115
+ });
116
+ return enhanced2;
117
+ }
118
+ const enhanced = {};
119
+ for (const key in router) {
120
+ enhanced[key] = enhanceRouter(router[key], options);
121
+ }
122
+ return enhanced;
123
+ }
124
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
125
+ let currentRouter = options.router;
126
+ if (isLazy(currentRouter)) {
127
+ lazyOptions.push({
128
+ router: currentRouter,
129
+ path: options.path
130
+ });
131
+ } else if (currentRouter instanceof Contract) {
132
+ callback({
133
+ contract: currentRouter,
134
+ path: options.path
135
+ });
136
+ } else if (typeof currentRouter === "string") {
137
+ throw new Error("Unexpected: got string instead of router");
138
+ } else {
139
+ for (const key in currentRouter) {
140
+ traverseContractProcedures(
141
+ {
142
+ router: currentRouter[key],
143
+ path: [...options.path, key]
144
+ },
145
+ callback,
146
+ lazyOptions
147
+ );
148
+ }
149
+ }
150
+ return lazyOptions;
151
+ }
152
+ async function resolveContractProcedures(options, callback) {
153
+ const pending = [options];
154
+ for (const options2 of pending) {
155
+ const lazyOptions = traverseContractProcedures(options2, callback);
156
+ for (const options3 of lazyOptions) {
157
+ const { default: router } = await unlazy(options3.router);
158
+ pending.push({
159
+ router,
160
+ path: options3.path
161
+ });
162
+ }
163
+ }
164
+ }
165
+ async function unlazyRouter(router) {
166
+ if (isProcedure(router)) {
167
+ return router;
168
+ }
169
+ const unlazied = {};
170
+ for (const key in router) {
171
+ const item = router[key];
172
+ const { default: unlaziedRouter } = await unlazy(item);
173
+ unlazied[key] = await unlazyRouter(unlaziedRouter);
174
+ }
175
+ return unlazied;
176
+ }
177
+
178
+ const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
179
+ function lazyInternal(loader, meta = {}) {
180
+ return {
181
+ [LAZY_SYMBOL]: {
182
+ loader,
183
+ meta
184
+ }
185
+ };
186
+ }
187
+ function lazy(prefix, loader) {
188
+ return enhanceRouter(lazyInternal(loader), {
189
+ middlewares: [],
190
+ dedupeLeadingMiddlewares: true,
191
+ prefix
192
+ });
193
+ }
194
+ function isLazy(item) {
195
+ return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
196
+ }
197
+ function getLazyMeta(lazied) {
198
+ return lazied[LAZY_SYMBOL].meta;
199
+ }
200
+ function unlazy(lazied) {
201
+ return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
202
+ }
203
+
204
+ function middlewareOutputFn(output) {
205
+ return { output, context: {} };
206
+ }
207
+
208
+ function createProcedureClient(lazyableProcedure, ...rest) {
209
+ const options = resolveMaybeOptionalOptions(rest);
210
+ return async (...[input, callerOptions]) => {
211
+ const path = toArray(options.path);
212
+ const { default: procedure } = await unlazy(lazyableProcedure);
213
+ const clientContext = callerOptions?.context ?? {};
214
+ const context = await value(options.context ?? {}, clientContext);
215
+ const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
216
+ span?.setAttribute("procedure.path", [...path]);
217
+ return intercept(
218
+ toArray(options.interceptors),
219
+ {
220
+ context,
221
+ input,
222
+ path,
223
+ procedure,
224
+ request: callerOptions?.request,
225
+ signal: callerOptions?.signal,
226
+ lastEventId: callerOptions?.lastEventId
227
+ },
228
+ (interceptorOptions) => {
229
+ const { input: input2, ...opts } = interceptorOptions;
230
+ return executeProcedureInternal(interceptorOptions.procedure, input2, opts);
231
+ }
232
+ );
233
+ });
234
+ if (isAsyncIteratorObject(output)) {
235
+ if (output instanceof HibernationEventIterator) {
236
+ return output;
237
+ }
238
+ return overlayProxy(
239
+ output,
240
+ mapEventIterator(
241
+ asyncIteratorWithSpan(
242
+ { name: "consume_event_iterator_output", signal: callerOptions?.signal },
243
+ output
244
+ ),
245
+ {
246
+ value: (v) => v,
247
+ error: async (e) => e
248
+ }
249
+ )
250
+ );
251
+ }
252
+ return output;
253
+ };
254
+ }
255
+ async function validateInput(procedure, input) {
256
+ const schemas = procedure["~orpc"].schemas;
257
+ return runWithSpan({ name: "validate_input" }, async () => {
258
+ const resultBody = await safeDecodeAsync(schemas.bodySchema, input.body, { parseType: "body" });
259
+ const resultPath = await safeDecodeAsync(schemas.pathSchema, input.path, { parseType: "path" });
260
+ const resultQuery = await safeDecodeAsync(schemas.querySchema, input.query, { parseType: "query" });
261
+ const issues = [];
262
+ if (!resultBody.success) {
263
+ issues.push(...resultBody.error.issues.map((i) => ({ ...i, path: ["body", ...i.path] })));
264
+ }
265
+ if (!resultPath.success) {
266
+ issues.push(...resultPath.error.issues.map((i) => ({ ...i, path: ["path", ...i.path] })));
267
+ }
268
+ if (!resultQuery.success) {
269
+ issues.push(...resultQuery.error.issues.map((i) => ({ ...i, path: ["query", ...i.path] })));
270
+ }
271
+ if (issues.length > 0) {
272
+ throw new ORPCError("BAD_REQUEST", {
273
+ message: "Input validation failed",
274
+ data: {
275
+ issues
276
+ },
277
+ cause: new ValidationError({
278
+ message: "Input validation failed",
279
+ issues,
280
+ data: input
281
+ })
282
+ });
283
+ }
284
+ const results = {
285
+ body: resultBody.data,
286
+ path: resultPath.data,
287
+ query: resultQuery.data
288
+ };
289
+ return results;
290
+ });
291
+ }
292
+ async function validateOutput(procedure, output) {
293
+ const schema = procedure["~orpc"].schemas.outputSchema;
294
+ if (!schema) {
295
+ return output;
296
+ }
297
+ return runWithSpan({ name: "validate_output" }, async () => {
298
+ const result = await safeEncodeAsync(schema, output, { parseType: "output" });
299
+ if (!result.success) {
300
+ throw new ORPCError("INTERNAL_SERVER_ERROR", {
301
+ message: "Output validation failed",
302
+ cause: new ValidationError({
303
+ message: "Output validation failed",
304
+ issues: result.error.issues,
305
+ data: output
306
+ })
307
+ });
308
+ }
309
+ return result.data;
310
+ });
311
+ }
312
+ async function executeProcedureInternal(procedure, input, options) {
313
+ const middlewares = procedure["~orpc"].middlewares;
314
+ const inputValidationIndex = Math.min(
315
+ Math.max(0, procedure["~orpc"].inputValidationIndex),
316
+ middlewares.length
317
+ );
318
+ const outputValidationIndex = Math.min(
319
+ Math.max(0, procedure["~orpc"].outputValidationIndex),
320
+ middlewares.length
321
+ );
322
+ const next = async (index, context, input2) => {
323
+ let currentInput = input2;
324
+ if (index === inputValidationIndex) {
325
+ currentInput = await validateInput(procedure, currentInput);
326
+ }
327
+ const mid = middlewares[index];
328
+ const output = mid ? await runWithSpan({ name: `middleware.${mid.name}`, signal: options.signal }, async (span) => {
329
+ span?.setAttribute("middleware.index", index);
330
+ span?.setAttribute("middleware.name", mid.name);
331
+ const result = await mid(
332
+ {
333
+ ...options,
334
+ context,
335
+ next: async (...[nextOptions]) => {
336
+ const nextContext = nextOptions?.context ?? {};
337
+ return {
338
+ output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
339
+ // NB: Pretty sure this isn't used (or meant to be used) at runtime, it's just there
340
+ // to get type inference in the builder (via the caller returning the output of next() in
341
+ // the middleware function)
342
+ context: nextContext
343
+ };
344
+ }
345
+ },
346
+ currentInput,
347
+ middlewareOutputFn
348
+ );
349
+ return result.output;
350
+ }) : await runWithSpan(
351
+ { name: "handler", signal: options.signal },
352
+ () => procedure["~orpc"].handler(currentInput, { ...options, context })
353
+ );
354
+ if (index === outputValidationIndex) {
355
+ return await validateOutput(procedure, output);
356
+ }
357
+ return output;
358
+ };
359
+ return next(0, options.context, input);
360
+ }
361
+
362
+ export { Contract as C, LAZY_SYMBOL as L, Procedure as P, addMiddleware as a, isLazy as b, createProcedureClient as c, getRouter as d, enhanceRouter as e, lazy as f, getLazyMeta as g, middlewareOutputFn as h, isProcedure as i, isStartWithMiddlewares as j, mergeMiddlewares as k, lazyInternal as l, mergeCurrentContext as m, createAccessibleLazyRouter as n, unlazyRouter as o, resolveContractProcedures as r, traverseContractProcedures as t, unlazy as u };
@@ -0,0 +1,237 @@
1
+ import { isObject, NullProtoObj, isAsyncIteratorObject, isORPCErrorJson, createORPCErrorFromJson, toORPCError } from '@temporary-name/shared';
2
+ import { mapEventIterator, ErrorEvent } from '@temporary-name/standard-server';
3
+
4
+ function bracketNotationSerialize(data, segments = [], result = []) {
5
+ if (Array.isArray(data)) {
6
+ data.forEach((item, i) => {
7
+ bracketNotationSerialize(item, [...segments, i], result);
8
+ });
9
+ } else if (isObject(data)) {
10
+ for (const key in data) {
11
+ bracketNotationSerialize(data[key], [...segments, key], result);
12
+ }
13
+ } else {
14
+ result.push([stringifyPath(segments), data]);
15
+ }
16
+ return result;
17
+ }
18
+ function bracketNotationDeserialize(serialized, { maxArrayIndex = 9999 } = {}) {
19
+ if (serialized.length === 0) {
20
+ return {};
21
+ }
22
+ const arrayPushStyles = /* @__PURE__ */ new WeakSet();
23
+ const ref = { value: [] };
24
+ for (const [path, value] of serialized) {
25
+ const segments = parsePath(path);
26
+ let currentRef = ref;
27
+ let nextSegment = "value";
28
+ segments.forEach((segment, i) => {
29
+ if (!Array.isArray(currentRef[nextSegment]) && !isObject(currentRef[nextSegment])) {
30
+ currentRef[nextSegment] = [];
31
+ }
32
+ if (i !== segments.length - 1) {
33
+ if (Array.isArray(currentRef[nextSegment]) && !isValidArrayIndex(segment, maxArrayIndex)) {
34
+ if (arrayPushStyles.has(currentRef[nextSegment])) {
35
+ arrayPushStyles.delete(currentRef[nextSegment]);
36
+ currentRef[nextSegment] = pushStyleArrayToObject(currentRef[nextSegment]);
37
+ } else {
38
+ currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
39
+ }
40
+ }
41
+ } else {
42
+ if (Array.isArray(currentRef[nextSegment])) {
43
+ if (segment === "") {
44
+ if (currentRef[nextSegment].length && !arrayPushStyles.has(currentRef[nextSegment])) {
45
+ currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
46
+ }
47
+ } else {
48
+ if (arrayPushStyles.has(currentRef[nextSegment])) {
49
+ arrayPushStyles.delete(currentRef[nextSegment]);
50
+ currentRef[nextSegment] = pushStyleArrayToObject(currentRef[nextSegment]);
51
+ } else if (!isValidArrayIndex(segment, maxArrayIndex)) {
52
+ currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
53
+ }
54
+ }
55
+ }
56
+ }
57
+ currentRef = currentRef[nextSegment];
58
+ nextSegment = segment;
59
+ });
60
+ if (Array.isArray(currentRef) && nextSegment === "") {
61
+ arrayPushStyles.add(currentRef);
62
+ currentRef.push(value);
63
+ } else if (nextSegment in currentRef) {
64
+ if (Array.isArray(currentRef[nextSegment])) {
65
+ currentRef[nextSegment].push(value);
66
+ } else {
67
+ currentRef[nextSegment] = [currentRef[nextSegment], value];
68
+ }
69
+ } else {
70
+ currentRef[nextSegment] = value;
71
+ }
72
+ }
73
+ return ref.value;
74
+ }
75
+ function stringifyPath(segments) {
76
+ return segments.map((segment) => {
77
+ return segment.toString().replace(/[\\[\]]/g, (match) => {
78
+ switch (match) {
79
+ case "\\":
80
+ return "\\\\";
81
+ case "[":
82
+ return "\\[";
83
+ case "]":
84
+ return "\\]";
85
+ /* v8 ignore next 2 */
86
+ default:
87
+ return match;
88
+ }
89
+ });
90
+ }).reduce((result, segment, i) => {
91
+ if (i === 0) {
92
+ return segment;
93
+ }
94
+ return `${result}[${segment}]`;
95
+ }, "");
96
+ }
97
+ function parsePath(path) {
98
+ const segments = [];
99
+ let inBrackets = false;
100
+ let currentSegment = "";
101
+ let backslashCount = 0;
102
+ for (let i = 0; i < path.length; i++) {
103
+ const char = path[i];
104
+ const nextChar = path[i + 1];
105
+ if (inBrackets && char === "]" && (nextChar === void 0 || nextChar === "[") && backslashCount % 2 === 0) {
106
+ if (nextChar === void 0) {
107
+ inBrackets = false;
108
+ }
109
+ segments.push(currentSegment);
110
+ currentSegment = "";
111
+ i++;
112
+ } else if (segments.length === 0 && char === "[" && backslashCount % 2 === 0) {
113
+ inBrackets = true;
114
+ segments.push(currentSegment);
115
+ currentSegment = "";
116
+ } else if (char === "\\") {
117
+ backslashCount++;
118
+ } else {
119
+ currentSegment += "\\".repeat(backslashCount / 2) + char;
120
+ backslashCount = 0;
121
+ }
122
+ }
123
+ return inBrackets || segments.length === 0 ? [path] : segments;
124
+ }
125
+ function isValidArrayIndex(value, maxIndex) {
126
+ return /^0$|^[1-9]\d*$/.test(value) && Number(value) <= maxIndex;
127
+ }
128
+ function arrayToObject(array) {
129
+ const obj = new NullProtoObj();
130
+ array.forEach((item, i) => {
131
+ obj[i] = item;
132
+ });
133
+ return obj;
134
+ }
135
+ function pushStyleArrayToObject(array) {
136
+ const obj = new NullProtoObj();
137
+ obj[""] = array.length === 1 ? array[0] : array;
138
+ return obj;
139
+ }
140
+
141
+ function jsonSerialize(data, hasBlobRef = { value: false }) {
142
+ if (data instanceof Blob) {
143
+ hasBlobRef.value = true;
144
+ return [data, hasBlobRef.value];
145
+ }
146
+ if (data instanceof Set) {
147
+ return jsonSerialize(Array.from(data), hasBlobRef);
148
+ }
149
+ if (data instanceof Map) {
150
+ return jsonSerialize(Array.from(data.entries()), hasBlobRef);
151
+ }
152
+ if (Array.isArray(data)) {
153
+ const json = data.map((v) => v === void 0 ? null : jsonSerialize(v, hasBlobRef)[0]);
154
+ return [json, hasBlobRef.value];
155
+ }
156
+ if (isObject(data)) {
157
+ const json = {};
158
+ for (const k in data) {
159
+ if (k === "toJSON" && typeof data[k] === "function") {
160
+ continue;
161
+ }
162
+ json[k] = jsonSerialize(data[k], hasBlobRef)[0];
163
+ }
164
+ return [json, hasBlobRef.value];
165
+ }
166
+ if (typeof data === "bigint" || data instanceof RegExp || data instanceof URL) {
167
+ return [data.toString(), hasBlobRef.value];
168
+ }
169
+ if (data instanceof Date) {
170
+ return [Number.isNaN(data.getTime()) ? null : data.toISOString(), hasBlobRef.value];
171
+ }
172
+ if (Number.isNaN(data)) {
173
+ return [null, hasBlobRef.value];
174
+ }
175
+ return [data, hasBlobRef.value];
176
+ }
177
+
178
+ function serialize(data, options = {}) {
179
+ if (isAsyncIteratorObject(data) && !options.outputFormat) {
180
+ return mapEventIterator(data, {
181
+ value: async (value) => _serialize(value, { outputFormat: "plain" }),
182
+ error: async (e) => {
183
+ return new ErrorEvent({
184
+ data: _serialize(toORPCError(e).toJSON(), { outputFormat: "plain" }),
185
+ cause: e
186
+ });
187
+ }
188
+ });
189
+ }
190
+ return _serialize(data, options);
191
+ }
192
+ function _serialize(data, options) {
193
+ const [json, hasBlob] = jsonSerialize(data);
194
+ if (options.outputFormat === "plain") {
195
+ return json;
196
+ }
197
+ if (options.outputFormat === "URLSearchParams") {
198
+ const params = new URLSearchParams();
199
+ for (const [path, value] of bracketNotationSerialize(json)) {
200
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
201
+ params.append(path, value.toString());
202
+ }
203
+ }
204
+ return params;
205
+ }
206
+ if (json instanceof Blob || json === void 0 || !hasBlob) {
207
+ return json;
208
+ }
209
+ const form = new FormData();
210
+ for (const [path, value] of bracketNotationSerialize(json)) {
211
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
212
+ form.append(path, value.toString());
213
+ } else if (value instanceof Blob) {
214
+ form.append(path, value);
215
+ }
216
+ }
217
+ return form;
218
+ }
219
+ function deserialize(data) {
220
+ if (data instanceof URLSearchParams || data instanceof FormData) {
221
+ return bracketNotationDeserialize(Array.from(data.entries()));
222
+ }
223
+ if (isAsyncIteratorObject(data)) {
224
+ return mapEventIterator(data, {
225
+ value: async (value) => value,
226
+ error: async (e) => {
227
+ if (e instanceof ErrorEvent && isORPCErrorJson(e.data)) {
228
+ return createORPCErrorFromJson(e.data, { cause: e });
229
+ }
230
+ return e;
231
+ }
232
+ });
233
+ }
234
+ return data;
235
+ }
236
+
237
+ export { bracketNotationDeserialize as b, deserialize as d, jsonSerialize as j, serialize as s };