@temporary-name/server 1.9.3-alpha.32780b2c7ad58cf8dcbc93a40f13494185223e6e → 1.9.3-alpha.3bfad2d4c9c25a8fb0e01d57b30d58f74bc8f953

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 (42) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +4 -6
  2. package/dist/adapters/aws-lambda/index.d.ts +4 -6
  3. package/dist/adapters/aws-lambda/index.mjs +4 -4
  4. package/dist/adapters/fetch/index.d.mts +8 -86
  5. package/dist/adapters/fetch/index.d.ts +8 -86
  6. package/dist/adapters/fetch/index.mjs +16 -155
  7. package/dist/adapters/node/index.d.mts +8 -63
  8. package/dist/adapters/node/index.d.ts +8 -63
  9. package/dist/adapters/node/index.mjs +14 -120
  10. package/dist/adapters/standard/index.d.mts +5 -7
  11. package/dist/adapters/standard/index.d.ts +5 -7
  12. package/dist/adapters/standard/index.mjs +4 -4
  13. package/dist/helpers/index.mjs +3 -29
  14. package/dist/index.d.mts +114 -189
  15. package/dist/index.d.ts +114 -189
  16. package/dist/index.mjs +144 -119
  17. package/dist/openapi/index.d.mts +11 -27
  18. package/dist/openapi/index.d.ts +11 -27
  19. package/dist/openapi/index.mjs +8 -75
  20. package/dist/shared/server.BYV-qTJt.d.ts +41 -0
  21. package/dist/shared/server.C1RJffw4.mjs +30 -0
  22. package/dist/shared/server.CQIFwyhc.mjs +40 -0
  23. package/dist/shared/server.CWb33y9B.d.mts +41 -0
  24. package/dist/shared/server.ChOv1yG3.mjs +319 -0
  25. package/dist/shared/server.DXzEGRE2.mjs +403 -0
  26. package/dist/shared/server.DgzAlPjF.mjs +160 -0
  27. package/dist/shared/server.DimTvmOQ.d.mts +373 -0
  28. package/dist/shared/server.DimTvmOQ.d.ts +373 -0
  29. package/dist/shared/server.YUvuxHty.mjs +48 -0
  30. package/package.json +10 -27
  31. package/dist/plugins/index.d.mts +0 -160
  32. package/dist/plugins/index.d.ts +0 -160
  33. package/dist/plugins/index.mjs +0 -288
  34. package/dist/shared/server.BRCH88Sb.d.mts +0 -56
  35. package/dist/shared/server.Bb9Wxubg.d.ts +0 -23
  36. package/dist/shared/server.BbfNcgas.d.mts +0 -23
  37. package/dist/shared/server.D24uJlXJ.d.ts +0 -56
  38. package/dist/shared/server.DlRH_uit.mjs +0 -409
  39. package/dist/shared/server.Kxw442A9.mjs +0 -247
  40. package/dist/shared/server.XpnxYU3D.d.mts +0 -247
  41. package/dist/shared/server.XpnxYU3D.d.ts +0 -247
  42. package/dist/shared/server.u-02Z0mj.mjs +0 -254
@@ -1,409 +0,0 @@
1
- import { isContractProcedure, mergePrefix, mergeErrorMap, enhanceRoute, validateORPCError, ValidationError } from '@temporary-name/contract';
2
- import { resolveMaybeOptionalOptions, ORPCError, toArray, value, runWithSpan, intercept, isAsyncIteratorObject, overlayProxy, asyncIteratorWithSpan } from '@temporary-name/shared';
3
- import { HibernationEventIterator, mapEventIterator } from '@temporary-name/standard-server';
4
- import { safeParseAsync } 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 Procedure {
31
- /**
32
- * This property holds the defined options.
33
- */
34
- "~orpc";
35
- constructor(def) {
36
- this["~orpc"] = def;
37
- }
38
- }
39
- function isProcedure(item) {
40
- if (item instanceof Procedure) {
41
- return true;
42
- }
43
- return isContractProcedure(item) && "middlewares" in item["~orpc"] && "handler" in item["~orpc"];
44
- }
45
-
46
- function mergeCurrentContext(context, other) {
47
- return { ...context, ...other };
48
- }
49
-
50
- function createORPCErrorConstructorMap(errors) {
51
- const proxy = new Proxy(errors, {
52
- get(target, code) {
53
- if (typeof code !== "string") {
54
- return Reflect.get(target, code);
55
- }
56
- const item = (...rest) => {
57
- const options = resolveMaybeOptionalOptions(rest);
58
- const config = errors[code];
59
- return new ORPCError(code, {
60
- defined: Boolean(config),
61
- status: config?.status,
62
- message: options.message ?? config?.message,
63
- data: options.data,
64
- cause: options.cause
65
- });
66
- };
67
- return item;
68
- }
69
- });
70
- return proxy;
71
- }
72
-
73
- const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
74
- function setHiddenRouterContract(router, contract) {
75
- return new Proxy(router, {
76
- get(target, key) {
77
- if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
78
- return contract;
79
- }
80
- return Reflect.get(target, key);
81
- }
82
- });
83
- }
84
- function getHiddenRouterContract(router) {
85
- return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
86
- }
87
-
88
- function getRouter(router, path) {
89
- let current = router;
90
- for (let i = 0; i < path.length; i++) {
91
- const segment = path[i];
92
- if (!current) {
93
- return void 0;
94
- }
95
- if (isProcedure(current)) {
96
- return void 0;
97
- }
98
- if (!isLazy(current)) {
99
- current = current[segment];
100
- continue;
101
- }
102
- const lazied = current;
103
- const rest = path.slice(i);
104
- return lazyInternal(async () => {
105
- const unwrapped = await unlazy(lazied);
106
- const next = getRouter(unwrapped.default, rest);
107
- return unlazy(next);
108
- }, getLazyMeta(lazied));
109
- }
110
- return current;
111
- }
112
- function createAccessibleLazyRouter(lazied) {
113
- const recursive = new Proxy(lazied, {
114
- get(target, key) {
115
- if (typeof key !== "string") {
116
- return Reflect.get(target, key);
117
- }
118
- const next = getRouter(lazied, [key]);
119
- return createAccessibleLazyRouter(next);
120
- }
121
- });
122
- return recursive;
123
- }
124
- function enhanceRouter(router, options) {
125
- if (isLazy(router)) {
126
- const laziedMeta = getLazyMeta(router);
127
- const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
128
- const enhanced2 = lazyInternal(
129
- async () => {
130
- const { default: unlaziedRouter } = await unlazy(router);
131
- const enhanced3 = enhanceRouter(unlaziedRouter, options);
132
- return unlazy(enhanced3);
133
- },
134
- {
135
- ...laziedMeta,
136
- prefix: enhancedPrefix
137
- }
138
- );
139
- const accessible = createAccessibleLazyRouter(enhanced2);
140
- return accessible;
141
- }
142
- if (isProcedure(router)) {
143
- const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
144
- dedupeLeading: options.dedupeLeadingMiddlewares
145
- });
146
- const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
147
- const enhanced2 = new Procedure({
148
- ...router["~orpc"],
149
- route: enhanceRoute(router["~orpc"].route, options),
150
- errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
151
- middlewares: newMiddlewares,
152
- inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
153
- outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
154
- });
155
- return enhanced2;
156
- }
157
- const enhanced = {};
158
- for (const key in router) {
159
- enhanced[key] = enhanceRouter(router[key], options);
160
- }
161
- return enhanced;
162
- }
163
- function traverseContractProcedures(options, callback, lazyOptions = []) {
164
- let currentRouter = options.router;
165
- const hiddenContract = getHiddenRouterContract(options.router);
166
- if (hiddenContract !== void 0) {
167
- currentRouter = hiddenContract;
168
- }
169
- if (isLazy(currentRouter)) {
170
- lazyOptions.push({
171
- router: currentRouter,
172
- path: options.path
173
- });
174
- } else if (isContractProcedure(currentRouter)) {
175
- callback({
176
- contract: currentRouter,
177
- path: options.path
178
- });
179
- } else {
180
- for (const key in currentRouter) {
181
- traverseContractProcedures(
182
- {
183
- router: currentRouter[key],
184
- path: [...options.path, key]
185
- },
186
- callback,
187
- lazyOptions
188
- );
189
- }
190
- }
191
- return lazyOptions;
192
- }
193
- async function resolveContractProcedures(options, callback) {
194
- const pending = [options];
195
- for (const options2 of pending) {
196
- const lazyOptions = traverseContractProcedures(options2, callback);
197
- for (const options3 of lazyOptions) {
198
- const { default: router } = await unlazy(options3.router);
199
- pending.push({
200
- router,
201
- path: options3.path
202
- });
203
- }
204
- }
205
- }
206
- async function unlazyRouter(router) {
207
- if (isProcedure(router)) {
208
- return router;
209
- }
210
- const unlazied = {};
211
- for (const key in router) {
212
- const item = router[key];
213
- const { default: unlaziedRouter } = await unlazy(item);
214
- unlazied[key] = await unlazyRouter(unlaziedRouter);
215
- }
216
- return unlazied;
217
- }
218
-
219
- const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
220
- function lazyInternal(loader, meta = {}) {
221
- return {
222
- [LAZY_SYMBOL]: {
223
- loader,
224
- meta
225
- }
226
- };
227
- }
228
- function lazy(prefix, loader) {
229
- return enhanceRouter(lazyInternal(loader), {
230
- middlewares: [],
231
- errorMap: {},
232
- dedupeLeadingMiddlewares: true,
233
- prefix
234
- });
235
- }
236
- function isLazy(item) {
237
- return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
238
- }
239
- function getLazyMeta(lazied) {
240
- return lazied[LAZY_SYMBOL].meta;
241
- }
242
- function unlazy(lazied) {
243
- return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
244
- }
245
-
246
- function middlewareOutputFn(output) {
247
- return { output, context: {} };
248
- }
249
-
250
- function createProcedureClient(lazyableProcedure, ...rest) {
251
- const options = resolveMaybeOptionalOptions(rest);
252
- return async (...[input, callerOptions]) => {
253
- const path = toArray(options.path);
254
- const { default: procedure } = await unlazy(lazyableProcedure);
255
- const clientContext = callerOptions?.context ?? {};
256
- const context = await value(options.context ?? {}, clientContext);
257
- const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
258
- const validateError = async (e) => {
259
- if (e instanceof ORPCError) {
260
- return await validateORPCError(procedure["~orpc"].errorMap, e);
261
- }
262
- return e;
263
- };
264
- try {
265
- const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
266
- span?.setAttribute("procedure.path", [...path]);
267
- return intercept(
268
- toArray(options.interceptors),
269
- {
270
- context,
271
- input,
272
- errors,
273
- path,
274
- procedure,
275
- signal: callerOptions?.signal,
276
- lastEventId: callerOptions?.lastEventId
277
- },
278
- (interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
279
- );
280
- });
281
- if (isAsyncIteratorObject(output)) {
282
- if (output instanceof HibernationEventIterator) {
283
- return output;
284
- }
285
- return overlayProxy(
286
- output,
287
- mapEventIterator(
288
- asyncIteratorWithSpan(
289
- { name: "consume_event_iterator_output", signal: callerOptions?.signal },
290
- output
291
- ),
292
- {
293
- value: (v) => v,
294
- error: (e) => validateError(e)
295
- }
296
- )
297
- );
298
- }
299
- return output;
300
- } catch (e) {
301
- throw await validateError(e);
302
- }
303
- };
304
- }
305
- async function validateInput(procedure, input) {
306
- const schemas = procedure["~orpc"].schemas;
307
- return runWithSpan({ name: "validate_input" }, async () => {
308
- const resultBody = await safeParseAsync(schemas.bodySchema, input.body);
309
- const resultPath = await safeParseAsync(schemas.pathSchema, input.path);
310
- const resultQuery = await safeParseAsync(schemas.querySchema, input.query);
311
- const issues = [];
312
- if (!resultBody.success) {
313
- issues.push(...resultBody.error.issues.map((i) => ({ ...i, path: ["body", ...i.path] })));
314
- }
315
- if (!resultPath.success) {
316
- issues.push(...resultPath.error.issues.map((i) => ({ ...i, path: ["path", ...i.path] })));
317
- }
318
- if (!resultQuery.success) {
319
- issues.push(...resultQuery.error.issues.map((i) => ({ ...i, path: ["query", ...i.path] })));
320
- }
321
- if (issues.length > 0) {
322
- throw new ORPCError("BAD_REQUEST", {
323
- message: "Input validation failed",
324
- data: {
325
- issues
326
- },
327
- cause: new ValidationError({
328
- message: "Input validation failed",
329
- issues,
330
- data: input
331
- })
332
- });
333
- }
334
- const results = {
335
- body: resultBody.data,
336
- path: resultPath.data,
337
- query: resultQuery.data
338
- };
339
- return results;
340
- });
341
- }
342
- async function validateOutput(procedure, output) {
343
- const schema = procedure["~orpc"].schemas.outputSchema;
344
- if (!schema) {
345
- return output;
346
- }
347
- return runWithSpan({ name: "validate_output" }, async () => {
348
- const result = await safeParseAsync(schema, output);
349
- if (!result.success) {
350
- throw new ORPCError("INTERNAL_SERVER_ERROR", {
351
- message: "Output validation failed",
352
- cause: new ValidationError({
353
- message: "Output validation failed",
354
- issues: result.error.issues,
355
- data: output
356
- })
357
- });
358
- }
359
- return result.data;
360
- });
361
- }
362
- async function executeProcedureInternal(procedure, options) {
363
- const middlewares = procedure["~orpc"].middlewares;
364
- const inputValidationIndex = Math.min(
365
- Math.max(0, procedure["~orpc"].inputValidationIndex),
366
- middlewares.length
367
- );
368
- const outputValidationIndex = Math.min(
369
- Math.max(0, procedure["~orpc"].outputValidationIndex),
370
- middlewares.length
371
- );
372
- const next = async (index, context, input) => {
373
- let currentInput = input;
374
- if (index === inputValidationIndex) {
375
- currentInput = await validateInput(procedure, currentInput);
376
- }
377
- const mid = middlewares[index];
378
- const output = mid ? await runWithSpan({ name: `middleware.${mid.name}`, signal: options.signal }, async (span) => {
379
- span?.setAttribute("middleware.index", index);
380
- span?.setAttribute("middleware.name", mid.name);
381
- const result = await mid(
382
- {
383
- ...options,
384
- context,
385
- next: async (...[nextOptions]) => {
386
- const nextContext = nextOptions?.context ?? {};
387
- return {
388
- output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
389
- context: nextContext
390
- };
391
- }
392
- },
393
- currentInput,
394
- middlewareOutputFn
395
- );
396
- return result.output;
397
- }) : await runWithSpan(
398
- { name: "handler", signal: options.signal },
399
- () => procedure["~orpc"].handler({ ...options, context, input: currentInput })
400
- );
401
- if (index === outputValidationIndex) {
402
- return await validateOutput(procedure, output);
403
- }
404
- return output;
405
- };
406
- return next(0, options.context, options.input);
407
- }
408
-
409
- export { LAZY_SYMBOL as L, Procedure as P, addMiddleware as a, isLazy as b, createProcedureClient as c, getRouter as d, enhanceRouter as e, createORPCErrorConstructorMap as f, getLazyMeta as g, lazy as h, isProcedure as i, middlewareOutputFn as j, isStartWithMiddlewares as k, lazyInternal as l, mergeCurrentContext as m, mergeMiddlewares as n, getHiddenRouterContract as o, createAccessibleLazyRouter as p, unlazyRouter as q, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };
@@ -1,247 +0,0 @@
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
- function standardizeHTTPPath(path) {
238
- return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
239
- }
240
- function getDynamicParams(path) {
241
- return path ? standardizeHTTPPath(path).match(/\/\{[^}]+\}/g)?.map((v) => ({
242
- raw: v,
243
- name: v.match(/\{\+?([^}]+)\}/)[1]
244
- })) : void 0;
245
- }
246
-
247
- export { standardizeHTTPPath as a, bracketNotationDeserialize as b, deserialize as d, getDynamicParams as g, jsonSerialize as j, serialize as s };