@temporary-name/server 1.9.3-alpha.37dfe6603933b8e445661fe453c4fbdb30ccb524 → 1.9.3-alpha.4225889dbc4c4adc76cdbabb804a30cd075c9d7a

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 (29) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +3 -3
  2. package/dist/adapters/aws-lambda/index.d.ts +3 -3
  3. package/dist/adapters/aws-lambda/index.mjs +4 -6
  4. package/dist/adapters/fetch/index.d.mts +3 -3
  5. package/dist/adapters/fetch/index.d.ts +3 -3
  6. package/dist/adapters/fetch/index.mjs +4 -6
  7. package/dist/adapters/node/index.d.mts +3 -3
  8. package/dist/adapters/node/index.d.ts +3 -3
  9. package/dist/adapters/node/index.mjs +4 -6
  10. package/dist/adapters/standard/index.d.mts +14 -30
  11. package/dist/adapters/standard/index.d.ts +14 -30
  12. package/dist/adapters/standard/index.mjs +4 -6
  13. package/dist/index.d.mts +41 -319
  14. package/dist/index.d.ts +41 -319
  15. package/dist/index.mjs +102 -352
  16. package/dist/openapi/index.mjs +27 -61
  17. package/dist/plugins/index.d.mts +2 -2
  18. package/dist/plugins/index.d.ts +2 -2
  19. package/dist/shared/{server.BeuTpcmO.d.mts → server.BYnDyuDL.d.mts} +2 -2
  20. package/dist/shared/{server.CQyYNJ1H.d.ts → server.BlJrjUA9.d.mts} +1 -2
  21. package/dist/shared/{server.C1fnTLq0.d.mts → server.C-tNYmY_.d.ts} +1 -2
  22. package/dist/shared/server.CjkiSCui.mjs +396 -0
  23. package/dist/shared/{server.DLsti1Pv.mjs → server.DdHBdcen.mjs} +58 -89
  24. package/dist/shared/{server.SLLuK6_v.d.ts → server.JI4dqTgD.d.ts} +2 -2
  25. package/dist/shared/{server.BEHw7Eyx.mjs → server.Kxw442A9.mjs} +1 -1
  26. package/dist/shared/{server.BKSOrA6h.d.mts → server.WsFQIubj.d.mts} +103 -60
  27. package/dist/shared/{server.BKSOrA6h.d.ts → server.WsFQIubj.d.ts} +103 -60
  28. package/package.json +10 -9
  29. package/dist/shared/server.BKh8I1Ny.mjs +0 -239
@@ -1,239 +0,0 @@
1
- import { 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 { AsyncLocalStorage } from 'node:async_hooks';
5
- import 'zod';
6
- import * as z4 from 'zod/v4/core';
7
-
8
- const gatingContext = new AsyncLocalStorage();
9
- function withoutGatedFields(data, schema, isGateEnabled) {
10
- const filtered = { ...data };
11
- const gatedFields = getGatedFields(schema);
12
- for (const [fieldName, gate] of gatedFields) {
13
- if (!isGateEnabled(gate)) {
14
- delete filtered[fieldName];
15
- }
16
- }
17
- return filtered;
18
- }
19
- function getGatedFields(schema) {
20
- if (!schema || schema["~standard"].vendor !== "zod") {
21
- return [];
22
- }
23
- const gatedFields = [];
24
- const zodDef = schema._zod.def;
25
- if (zodDef.type === "object") {
26
- const shape = zodDef.shape;
27
- for (const fieldName in shape) {
28
- const fieldSchema = shape[fieldName];
29
- const gate = z4.globalRegistry.get(fieldSchema)?.gate;
30
- if (gate) {
31
- gatedFields.push([fieldName, gate]);
32
- }
33
- }
34
- }
35
- return gatedFields;
36
- }
37
-
38
- const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
39
- function lazy(loader, meta = {}) {
40
- return {
41
- [LAZY_SYMBOL]: {
42
- loader,
43
- meta
44
- }
45
- };
46
- }
47
- function isLazy(item) {
48
- return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
49
- }
50
- function getLazyMeta(lazied) {
51
- return lazied[LAZY_SYMBOL].meta;
52
- }
53
- function unlazy(lazied) {
54
- return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
55
- }
56
-
57
- function mergeCurrentContext(context, other) {
58
- return { ...context, ...other };
59
- }
60
-
61
- function createORPCErrorConstructorMap(errors) {
62
- const proxy = new Proxy(errors, {
63
- get(target, code) {
64
- if (typeof code !== "string") {
65
- return Reflect.get(target, code);
66
- }
67
- const item = (...rest) => {
68
- const options = resolveMaybeOptionalOptions(rest);
69
- const config = errors[code];
70
- return new ORPCError(code, {
71
- defined: Boolean(config),
72
- status: config?.status,
73
- message: options.message ?? config?.message,
74
- data: options.data,
75
- cause: options.cause
76
- });
77
- };
78
- return item;
79
- }
80
- });
81
- return proxy;
82
- }
83
-
84
- function middlewareOutputFn(output) {
85
- return { output, context: {} };
86
- }
87
-
88
- function createProcedureClient(lazyableProcedure, ...rest) {
89
- const options = resolveMaybeOptionalOptions(rest);
90
- return async (...[input, callerOptions]) => {
91
- const path = toArray(options.path);
92
- const { default: procedure } = await unlazy(lazyableProcedure);
93
- const clientContext = callerOptions?.context ?? {};
94
- const context = await value(options.context ?? {}, clientContext);
95
- const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
96
- const validateError = async (e) => {
97
- if (e instanceof ORPCError) {
98
- return await validateORPCError(procedure["~orpc"].errorMap, e);
99
- }
100
- return e;
101
- };
102
- try {
103
- const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
104
- span?.setAttribute("procedure.path", [...path]);
105
- return intercept(
106
- toArray(options.interceptors),
107
- {
108
- context,
109
- input,
110
- // input only optional when it undefinable so we can safely cast it
111
- errors,
112
- path,
113
- procedure,
114
- signal: callerOptions?.signal,
115
- lastEventId: callerOptions?.lastEventId
116
- },
117
- (interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
118
- );
119
- });
120
- if (isAsyncIteratorObject(output)) {
121
- if (output instanceof HibernationEventIterator) {
122
- return output;
123
- }
124
- return overlayProxy(
125
- output,
126
- mapEventIterator(
127
- asyncIteratorWithSpan(
128
- { name: "consume_event_iterator_output", signal: callerOptions?.signal },
129
- output
130
- ),
131
- {
132
- value: (v) => v,
133
- error: (e) => validateError(e)
134
- }
135
- )
136
- );
137
- }
138
- return output;
139
- } catch (e) {
140
- throw await validateError(e);
141
- }
142
- };
143
- }
144
- async function validateInput(procedure, input) {
145
- const schema = procedure["~orpc"].inputSchema;
146
- if (!schema) {
147
- return input;
148
- }
149
- return runWithSpan({ name: "validate_input" }, async () => {
150
- const result = await schema["~standard"].validate(input);
151
- if (result.issues) {
152
- throw new ORPCError("BAD_REQUEST", {
153
- message: "Input validation failed",
154
- data: {
155
- issues: result.issues
156
- },
157
- cause: new ValidationError({
158
- message: "Input validation failed",
159
- issues: result.issues,
160
- data: input
161
- })
162
- });
163
- }
164
- return result.value;
165
- });
166
- }
167
- async function validateOutput(schema, output) {
168
- return runWithSpan({ name: "validate_output" }, async () => {
169
- const result = await schema["~standard"].validate(output);
170
- if (result.issues) {
171
- throw new ORPCError("INTERNAL_SERVER_ERROR", {
172
- message: "Output validation failed",
173
- cause: new ValidationError({
174
- message: "Output validation failed",
175
- issues: result.issues,
176
- data: output
177
- })
178
- });
179
- }
180
- return result.value;
181
- });
182
- }
183
- async function executeProcedureInternal(procedure, options) {
184
- const middlewares = procedure["~orpc"].middlewares;
185
- const inputValidationIndex = Math.min(
186
- Math.max(0, procedure["~orpc"].inputValidationIndex),
187
- middlewares.length
188
- );
189
- const outputValidationIndex = Math.min(
190
- Math.max(0, procedure["~orpc"].outputValidationIndex),
191
- middlewares.length
192
- );
193
- const next = async (index, context, input) => {
194
- let currentInput = input;
195
- if (index === inputValidationIndex) {
196
- currentInput = await validateInput(procedure, currentInput);
197
- }
198
- const mid = middlewares[index];
199
- const output = mid ? await runWithSpan({ name: `middleware.${mid.name}`, signal: options.signal }, async (span) => {
200
- span?.setAttribute("middleware.index", index);
201
- span?.setAttribute("middleware.name", mid.name);
202
- const result = await mid(
203
- {
204
- ...options,
205
- context,
206
- next: async (...[nextOptions]) => {
207
- const nextContext = nextOptions?.context ?? {};
208
- return {
209
- output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
210
- context: nextContext
211
- };
212
- }
213
- },
214
- currentInput,
215
- middlewareOutputFn
216
- );
217
- return result.output;
218
- }) : await runWithSpan(
219
- { name: "handler", signal: options.signal },
220
- () => procedure["~orpc"].handler({ ...options, context, input: currentInput })
221
- );
222
- if (index === outputValidationIndex) {
223
- const schema = procedure["~orpc"].outputSchema;
224
- if (!schema) {
225
- return output;
226
- }
227
- const validated = await validateOutput(schema, output);
228
- const isGateEnabled = gatingContext.getStore();
229
- if (!validated || !isGateEnabled) {
230
- return validated;
231
- }
232
- return withoutGatedFields(validated, schema, isGateEnabled);
233
- }
234
- return output;
235
- };
236
- return next(0, options.context, options.input);
237
- }
238
-
239
- export { LAZY_SYMBOL as L, gatingContext as a, createORPCErrorConstructorMap as b, createProcedureClient as c, middlewareOutputFn as d, getLazyMeta as g, isLazy as i, lazy as l, mergeCurrentContext as m, unlazy as u };