@orpc/server 0.0.0-next.1d3318c → 0.0.0-next.1e104eb
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 +31 -21
- package/dist/adapters/aws-lambda/index.d.mts +46 -0
- package/dist/adapters/aws-lambda/index.d.ts +46 -0
- package/dist/adapters/aws-lambda/index.mjs +42 -0
- package/dist/adapters/bun-ws/index.d.mts +36 -0
- package/dist/adapters/bun-ws/index.d.ts +36 -0
- package/dist/adapters/bun-ws/index.mjs +47 -0
- package/dist/adapters/crossws/index.d.mts +33 -0
- package/dist/adapters/crossws/index.d.ts +33 -0
- package/dist/adapters/crossws/index.mjs +45 -0
- package/dist/adapters/fetch/index.d.mts +108 -10
- package/dist/adapters/fetch/index.d.ts +108 -10
- package/dist/adapters/fetch/index.mjs +178 -8
- package/dist/adapters/message-port/index.d.mts +31 -0
- package/dist/adapters/message-port/index.d.ts +31 -0
- package/dist/adapters/message-port/index.mjs +39 -0
- package/dist/adapters/node/index.d.mts +86 -21
- package/dist/adapters/node/index.d.ts +86 -21
- package/dist/adapters/node/index.mjs +144 -23
- package/dist/adapters/standard/index.d.mts +11 -15
- package/dist/adapters/standard/index.d.ts +11 -15
- package/dist/adapters/standard/index.mjs +5 -3
- package/dist/adapters/standard-peer/index.d.mts +18 -0
- package/dist/adapters/standard-peer/index.d.ts +18 -0
- package/dist/adapters/standard-peer/index.mjs +7 -0
- package/dist/adapters/websocket/index.d.mts +54 -0
- package/dist/adapters/websocket/index.d.ts +54 -0
- package/dist/adapters/websocket/index.mjs +67 -0
- package/dist/adapters/ws/index.d.mts +31 -0
- package/dist/adapters/ws/index.d.ts +31 -0
- package/dist/adapters/ws/index.mjs +37 -0
- package/dist/helpers/index.d.mts +134 -0
- package/dist/helpers/index.d.ts +134 -0
- package/dist/helpers/index.mjs +188 -0
- package/dist/hibernation/index.d.mts +44 -0
- package/dist/hibernation/index.d.ts +44 -0
- package/dist/hibernation/index.mjs +65 -0
- package/dist/index.d.mts +633 -95
- package/dist/index.d.ts +633 -95
- package/dist/index.mjs +187 -37
- package/dist/plugins/index.d.mts +157 -19
- package/dist/plugins/index.d.ts +157 -19
- package/dist/plugins/index.mjs +199 -17
- package/dist/shared/{server.BFBhsdJr.mjs → server.7jWaIryJ.mjs} +148 -77
- package/dist/shared/server.B7b2w3_i.d.ts +12 -0
- package/dist/shared/server.BEFBl-Cb.d.mts +12 -0
- package/dist/shared/server.BHZCyRuJ.mjs +219 -0
- package/dist/shared/server.BU4WI18A.d.mts +32 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.Bmh5xd4n.d.ts +74 -0
- package/dist/shared/{server.CPteJIPP.d.mts → server.CYNGeoCm.d.mts} +75 -24
- package/dist/shared/{server.CPteJIPP.d.ts → server.CYNGeoCm.d.ts} +75 -24
- package/dist/shared/server.D0H-iaY3.d.ts +32 -0
- package/dist/shared/server.DZ5BIITo.mjs +9 -0
- package/dist/shared/server.DhJj-1X9.d.mts +42 -0
- package/dist/shared/server.UVMTOWrk.mjs +26 -0
- package/dist/shared/server.gqRxT-yN.d.mts +74 -0
- package/dist/shared/server.jMTkVNIb.d.ts +42 -0
- package/package.json +71 -20
- package/dist/adapters/hono/index.d.mts +0 -19
- package/dist/adapters/hono/index.d.ts +0 -19
- package/dist/adapters/hono/index.mjs +0 -32
- package/dist/adapters/next/index.d.mts +0 -26
- package/dist/adapters/next/index.d.ts +0 -26
- package/dist/adapters/next/index.mjs +0 -29
- package/dist/shared/server.3cSam35R.mjs +0 -158
- package/dist/shared/server.BZRSVRDu.d.ts +0 -77
- package/dist/shared/server.CjB_m7jG.mjs +0 -28
- package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
- package/dist/shared/server.iM8li30u.d.mts +0 -77
@@ -1,6 +1,7 @@
|
|
1
1
|
import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, enhanceRoute } from '@orpc/contract';
|
2
|
-
import {
|
3
|
-
import {
|
2
|
+
import { resolveMaybeOptionalOptions, toArray, value, runWithSpan, intercept, isAsyncIteratorObject, asyncIteratorWithSpan } from '@orpc/shared';
|
3
|
+
import { ORPCError, fallbackORPCErrorStatus, mapEventIterator } from '@orpc/client';
|
4
|
+
import { HibernationEventIterator } from '@orpc/standard-server';
|
4
5
|
|
5
6
|
const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
|
6
7
|
function lazy(loader, meta = {}) {
|
@@ -21,25 +22,34 @@ function unlazy(lazied) {
|
|
21
22
|
return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
|
22
23
|
}
|
23
24
|
|
24
|
-
function
|
25
|
-
|
25
|
+
function isStartWithMiddlewares(middlewares, compare) {
|
26
|
+
if (compare.length > middlewares.length) {
|
27
|
+
return false;
|
28
|
+
}
|
26
29
|
for (let i = 0; i < middlewares.length; i++) {
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
+
if (compare[i] === void 0) {
|
31
|
+
return true;
|
32
|
+
}
|
33
|
+
if (middlewares[i] !== compare[i]) {
|
34
|
+
return false;
|
30
35
|
}
|
31
|
-
min = index + 1;
|
32
36
|
}
|
33
|
-
return
|
37
|
+
return true;
|
34
38
|
}
|
35
|
-
function mergeMiddlewares(first, second) {
|
36
|
-
|
39
|
+
function mergeMiddlewares(first, second, options) {
|
40
|
+
if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
|
41
|
+
return second;
|
42
|
+
}
|
43
|
+
return [...first, ...second];
|
37
44
|
}
|
38
45
|
function addMiddleware(middlewares, addition) {
|
39
46
|
return [...middlewares, addition];
|
40
47
|
}
|
41
48
|
|
42
49
|
class Procedure {
|
50
|
+
/**
|
51
|
+
* This property holds the defined options.
|
52
|
+
*/
|
43
53
|
"~orpc";
|
44
54
|
constructor(def) {
|
45
55
|
this["~orpc"] = def;
|
@@ -52,20 +62,25 @@ function isProcedure(item) {
|
|
52
62
|
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
53
63
|
}
|
54
64
|
|
65
|
+
function mergeCurrentContext(context, other) {
|
66
|
+
return { ...context, ...other };
|
67
|
+
}
|
68
|
+
|
55
69
|
function createORPCErrorConstructorMap(errors) {
|
56
70
|
const proxy = new Proxy(errors, {
|
57
71
|
get(target, code) {
|
58
72
|
if (typeof code !== "string") {
|
59
73
|
return Reflect.get(target, code);
|
60
74
|
}
|
61
|
-
const item = (...
|
75
|
+
const item = (...rest) => {
|
76
|
+
const options = resolveMaybeOptionalOptions(rest);
|
62
77
|
const config = errors[code];
|
63
78
|
return new ORPCError(code, {
|
64
79
|
defined: Boolean(config),
|
65
80
|
status: config?.status,
|
66
|
-
message: options
|
67
|
-
data: options
|
68
|
-
cause: options
|
81
|
+
message: options.message ?? config?.message,
|
82
|
+
data: options.data,
|
83
|
+
cause: options.cause
|
69
84
|
});
|
70
85
|
};
|
71
86
|
return item;
|
@@ -93,34 +108,59 @@ function middlewareOutputFn(output) {
|
|
93
108
|
return { output, context: {} };
|
94
109
|
}
|
95
110
|
|
96
|
-
function createProcedureClient(lazyableProcedure, ...
|
111
|
+
function createProcedureClient(lazyableProcedure, ...rest) {
|
112
|
+
const options = resolveMaybeOptionalOptions(rest);
|
97
113
|
return async (...[input, callerOptions]) => {
|
98
|
-
const path = options
|
114
|
+
const path = toArray(options.path);
|
99
115
|
const { default: procedure } = await unlazy(lazyableProcedure);
|
100
116
|
const clientContext = callerOptions?.context ?? {};
|
101
|
-
const context = await value(options
|
117
|
+
const context = await value(options.context ?? {}, clientContext);
|
102
118
|
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
119
|
+
const validateError = async (e) => {
|
120
|
+
if (e instanceof ORPCError) {
|
121
|
+
return await validateORPCError(procedure["~orpc"].errorMap, e);
|
122
|
+
}
|
123
|
+
return e;
|
124
|
+
};
|
103
125
|
try {
|
104
|
-
|
105
|
-
|
106
|
-
{
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
126
|
+
const output = await runWithSpan(
|
127
|
+
{ name: "call_procedure", signal: callerOptions?.signal },
|
128
|
+
(span) => {
|
129
|
+
span?.setAttribute("procedure.path", [...path]);
|
130
|
+
return intercept(
|
131
|
+
toArray(options.interceptors),
|
132
|
+
{
|
133
|
+
context,
|
134
|
+
input,
|
135
|
+
// input only optional when it undefinable so we can safely cast it
|
136
|
+
errors,
|
137
|
+
path,
|
138
|
+
procedure,
|
139
|
+
signal: callerOptions?.signal,
|
140
|
+
lastEventId: callerOptions?.lastEventId
|
141
|
+
},
|
142
|
+
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
143
|
+
);
|
144
|
+
}
|
117
145
|
);
|
118
|
-
|
119
|
-
|
120
|
-
|
146
|
+
if (isAsyncIteratorObject(output)) {
|
147
|
+
if (output instanceof HibernationEventIterator) {
|
148
|
+
return output;
|
149
|
+
}
|
150
|
+
return mapEventIterator(
|
151
|
+
asyncIteratorWithSpan(
|
152
|
+
{ name: "consume_event_iterator_output", signal: callerOptions?.signal },
|
153
|
+
output
|
154
|
+
),
|
155
|
+
{
|
156
|
+
value: (v) => v,
|
157
|
+
error: (e) => validateError(e)
|
158
|
+
}
|
159
|
+
);
|
121
160
|
}
|
122
|
-
|
123
|
-
|
161
|
+
return output;
|
162
|
+
} catch (e) {
|
163
|
+
throw await validateError(e);
|
124
164
|
}
|
125
165
|
};
|
126
166
|
}
|
@@ -129,58 +169,80 @@ async function validateInput(procedure, input) {
|
|
129
169
|
if (!schema) {
|
130
170
|
return input;
|
131
171
|
}
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
172
|
+
return runWithSpan(
|
173
|
+
{ name: "validate_input" },
|
174
|
+
async () => {
|
175
|
+
const result = await schema["~standard"].validate(input);
|
176
|
+
if (result.issues) {
|
177
|
+
throw new ORPCError("BAD_REQUEST", {
|
178
|
+
message: "Input validation failed",
|
179
|
+
data: {
|
180
|
+
issues: result.issues
|
181
|
+
},
|
182
|
+
cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
|
183
|
+
});
|
184
|
+
}
|
185
|
+
return result.value;
|
186
|
+
}
|
187
|
+
);
|
143
188
|
}
|
144
189
|
async function validateOutput(procedure, output) {
|
145
190
|
const schema = procedure["~orpc"].outputSchema;
|
146
191
|
if (!schema) {
|
147
192
|
return output;
|
148
193
|
}
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
194
|
+
return runWithSpan(
|
195
|
+
{ name: "validate_output" },
|
196
|
+
async () => {
|
197
|
+
const result = await schema["~standard"].validate(output);
|
198
|
+
if (result.issues) {
|
199
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
200
|
+
message: "Output validation failed",
|
201
|
+
cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
|
202
|
+
});
|
203
|
+
}
|
204
|
+
return result.value;
|
205
|
+
}
|
206
|
+
);
|
157
207
|
}
|
158
208
|
async function executeProcedureInternal(procedure, options) {
|
159
209
|
const middlewares = procedure["~orpc"].middlewares;
|
160
210
|
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
161
211
|
const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
|
162
|
-
|
163
|
-
|
164
|
-
let currentInput = options.input;
|
165
|
-
const next = async (...[nextOptions]) => {
|
166
|
-
const index = currentIndex;
|
167
|
-
currentIndex += 1;
|
168
|
-
currentContext = { ...currentContext, ...nextOptions?.context };
|
212
|
+
const next = async (index, context, input) => {
|
213
|
+
let currentInput = input;
|
169
214
|
if (index === inputValidationIndex) {
|
170
215
|
currentInput = await validateInput(procedure, currentInput);
|
171
216
|
}
|
172
217
|
const mid = middlewares[index];
|
173
|
-
const
|
218
|
+
const output = mid ? await runWithSpan(
|
219
|
+
{ name: `middleware.${mid.name}`, signal: options.signal },
|
220
|
+
async (span) => {
|
221
|
+
span?.setAttribute("middleware.index", index);
|
222
|
+
span?.setAttribute("middleware.name", mid.name);
|
223
|
+
const result = await mid({
|
224
|
+
...options,
|
225
|
+
context,
|
226
|
+
next: async (...[nextOptions]) => {
|
227
|
+
const nextContext = nextOptions?.context ?? {};
|
228
|
+
return {
|
229
|
+
output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
|
230
|
+
context: nextContext
|
231
|
+
};
|
232
|
+
}
|
233
|
+
}, currentInput, middlewareOutputFn);
|
234
|
+
return result.output;
|
235
|
+
}
|
236
|
+
) : await runWithSpan(
|
237
|
+
{ name: "handler", signal: options.signal },
|
238
|
+
() => procedure["~orpc"].handler({ ...options, context, input: currentInput })
|
239
|
+
);
|
174
240
|
if (index === outputValidationIndex) {
|
175
|
-
|
176
|
-
return {
|
177
|
-
...result,
|
178
|
-
output: validatedOutput
|
179
|
-
};
|
241
|
+
return await validateOutput(procedure, output);
|
180
242
|
}
|
181
|
-
return
|
243
|
+
return output;
|
182
244
|
};
|
183
|
-
return (
|
245
|
+
return next(0, options.context, options.input);
|
184
246
|
}
|
185
247
|
|
186
248
|
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
@@ -250,7 +312,7 @@ function enhanceRouter(router, options) {
|
|
250
312
|
return accessible;
|
251
313
|
}
|
252
314
|
if (isProcedure(router)) {
|
253
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
315
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
|
254
316
|
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
255
317
|
const enhanced2 = new Procedure({
|
256
318
|
...router["~orpc"],
|
@@ -311,6 +373,18 @@ async function resolveContractProcedures(options, callback) {
|
|
311
373
|
}
|
312
374
|
}
|
313
375
|
}
|
376
|
+
async function unlazyRouter(router) {
|
377
|
+
if (isProcedure(router)) {
|
378
|
+
return router;
|
379
|
+
}
|
380
|
+
const unlazied = {};
|
381
|
+
for (const key in router) {
|
382
|
+
const item = router[key];
|
383
|
+
const { default: unlaziedRouter } = await unlazy(item);
|
384
|
+
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
385
|
+
}
|
386
|
+
return unlazied;
|
387
|
+
}
|
314
388
|
|
315
389
|
function createAssertedLazyProcedure(lazied) {
|
316
390
|
const lazyProcedure = lazy(async () => {
|
@@ -335,11 +409,8 @@ function createContractedProcedure(procedure, contract) {
|
|
335
409
|
});
|
336
410
|
}
|
337
411
|
function call(procedure, input, ...rest) {
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
function toHttpPath(path) {
|
342
|
-
return `/${path.map(encodeURIComponent).join("/")}`;
|
412
|
+
const options = resolveMaybeOptionalOptions(rest);
|
413
|
+
return createProcedureClient(procedure, options)(input, options);
|
343
414
|
}
|
344
415
|
|
345
|
-
export { LAZY_SYMBOL as L, Procedure as P,
|
416
|
+
export { LAZY_SYMBOL as L, Procedure as P, createContractedProcedure as a, addMiddleware as b, createProcedureClient as c, isLazy as d, enhanceRouter as e, createAssertedLazyProcedure as f, getRouter as g, createORPCErrorConstructorMap as h, isProcedure as i, getLazyMeta as j, middlewareOutputFn as k, lazy as l, mergeCurrentContext as m, isStartWithMiddlewares as n, mergeMiddlewares as o, call as p, getHiddenRouterContract as q, createAccessibleLazyRouter as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, resolveContractProcedures as w, unlazyRouter as x };
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { C as Context } from './server.CYNGeoCm.js';
|
2
|
+
import { b as StandardHandleOptions } from './server.Bmh5xd4n.js';
|
3
|
+
|
4
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
5
|
+
context?: T;
|
6
|
+
} : {
|
7
|
+
context: T;
|
8
|
+
});
|
9
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
10
|
+
|
11
|
+
export { resolveFriendlyStandardHandleOptions as r };
|
12
|
+
export type { FriendlyStandardHandleOptions as F };
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { C as Context } from './server.CYNGeoCm.mjs';
|
2
|
+
import { b as StandardHandleOptions } from './server.gqRxT-yN.mjs';
|
3
|
+
|
4
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
5
|
+
context?: T;
|
6
|
+
} : {
|
7
|
+
context: T;
|
8
|
+
});
|
9
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
10
|
+
|
11
|
+
export { resolveFriendlyStandardHandleOptions as r };
|
12
|
+
export type { FriendlyStandardHandleOptions as F };
|
@@ -0,0 +1,219 @@
|
|
1
|
+
import { toHttpPath, StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
|
2
|
+
import { ORPCError, toORPCError } from '@orpc/client';
|
3
|
+
import { toArray, intercept, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, setSpanError, parseEmptyableJSON, NullProtoObj, value } from '@orpc/shared';
|
4
|
+
import { flattenHeader } from '@orpc/standard-server';
|
5
|
+
import { c as createProcedureClient, t as traverseContractProcedures, i as isProcedure, u as unlazy, g as getRouter, a as createContractedProcedure } from './server.7jWaIryJ.mjs';
|
6
|
+
|
7
|
+
class CompositeStandardHandlerPlugin {
|
8
|
+
plugins;
|
9
|
+
constructor(plugins = []) {
|
10
|
+
this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
11
|
+
}
|
12
|
+
init(options, router) {
|
13
|
+
for (const plugin of this.plugins) {
|
14
|
+
plugin.init?.(options, router);
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
18
|
+
|
19
|
+
class StandardHandler {
|
20
|
+
constructor(router, matcher, codec, options) {
|
21
|
+
this.matcher = matcher;
|
22
|
+
this.codec = codec;
|
23
|
+
const plugins = new CompositeStandardHandlerPlugin(options.plugins);
|
24
|
+
plugins.init(options, router);
|
25
|
+
this.interceptors = toArray(options.interceptors);
|
26
|
+
this.clientInterceptors = toArray(options.clientInterceptors);
|
27
|
+
this.rootInterceptors = toArray(options.rootInterceptors);
|
28
|
+
this.matcher.init(router);
|
29
|
+
}
|
30
|
+
interceptors;
|
31
|
+
clientInterceptors;
|
32
|
+
rootInterceptors;
|
33
|
+
async handle(request, options) {
|
34
|
+
const prefix = options.prefix?.replace(/\/$/, "") || void 0;
|
35
|
+
if (prefix && !request.url.pathname.startsWith(`${prefix}/`) && request.url.pathname !== prefix) {
|
36
|
+
return { matched: false, response: void 0 };
|
37
|
+
}
|
38
|
+
return intercept(
|
39
|
+
this.rootInterceptors,
|
40
|
+
{ ...options, request, prefix },
|
41
|
+
async (interceptorOptions) => {
|
42
|
+
return runWithSpan(
|
43
|
+
{ name: `${request.method} ${request.url.pathname}` },
|
44
|
+
async (span) => {
|
45
|
+
let step;
|
46
|
+
try {
|
47
|
+
return await intercept(
|
48
|
+
this.interceptors,
|
49
|
+
interceptorOptions,
|
50
|
+
async ({ request: request2, context, prefix: prefix2 }) => {
|
51
|
+
const method = request2.method;
|
52
|
+
const url = request2.url;
|
53
|
+
const pathname = prefix2 ? url.pathname.replace(prefix2, "") : url.pathname;
|
54
|
+
const match = await runWithSpan(
|
55
|
+
{ name: "find_procedure" },
|
56
|
+
() => this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`)
|
57
|
+
);
|
58
|
+
if (!match) {
|
59
|
+
return { matched: false, response: void 0 };
|
60
|
+
}
|
61
|
+
span?.updateName(`${ORPC_NAME}.${match.path.join("/")}`);
|
62
|
+
span?.setAttribute("rpc.system", ORPC_NAME);
|
63
|
+
span?.setAttribute("rpc.method", match.path.join("."));
|
64
|
+
step = "decode_input";
|
65
|
+
let input = await runWithSpan(
|
66
|
+
{ name: "decode_input" },
|
67
|
+
() => this.codec.decode(request2, match.params, match.procedure)
|
68
|
+
);
|
69
|
+
step = void 0;
|
70
|
+
if (isAsyncIteratorObject(input)) {
|
71
|
+
input = asyncIteratorWithSpan(
|
72
|
+
{ name: "consume_event_iterator_input", signal: request2.signal },
|
73
|
+
input
|
74
|
+
);
|
75
|
+
}
|
76
|
+
const client = createProcedureClient(match.procedure, {
|
77
|
+
context,
|
78
|
+
path: match.path,
|
79
|
+
interceptors: this.clientInterceptors
|
80
|
+
});
|
81
|
+
step = "call_procedure";
|
82
|
+
const output = await client(input, {
|
83
|
+
signal: request2.signal,
|
84
|
+
lastEventId: flattenHeader(request2.headers["last-event-id"])
|
85
|
+
});
|
86
|
+
step = void 0;
|
87
|
+
const response = this.codec.encode(output, match.procedure);
|
88
|
+
return {
|
89
|
+
matched: true,
|
90
|
+
response
|
91
|
+
};
|
92
|
+
}
|
93
|
+
);
|
94
|
+
} catch (e) {
|
95
|
+
if (step !== "call_procedure") {
|
96
|
+
setSpanError(span, e);
|
97
|
+
}
|
98
|
+
const error = step === "decode_input" && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
|
99
|
+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
100
|
+
cause: e
|
101
|
+
}) : toORPCError(e);
|
102
|
+
const response = this.codec.encodeError(error);
|
103
|
+
return {
|
104
|
+
matched: true,
|
105
|
+
response
|
106
|
+
};
|
107
|
+
}
|
108
|
+
}
|
109
|
+
);
|
110
|
+
}
|
111
|
+
);
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
class StandardRPCCodec {
|
116
|
+
constructor(serializer) {
|
117
|
+
this.serializer = serializer;
|
118
|
+
}
|
119
|
+
async decode(request, _params, _procedure) {
|
120
|
+
const serialized = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("data").at(-1)) : await request.body();
|
121
|
+
return this.serializer.deserialize(serialized);
|
122
|
+
}
|
123
|
+
encode(output, _procedure) {
|
124
|
+
return {
|
125
|
+
status: 200,
|
126
|
+
headers: {},
|
127
|
+
body: this.serializer.serialize(output)
|
128
|
+
};
|
129
|
+
}
|
130
|
+
encodeError(error) {
|
131
|
+
return {
|
132
|
+
status: error.status,
|
133
|
+
headers: {},
|
134
|
+
body: this.serializer.serialize(error.toJSON())
|
135
|
+
};
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
class StandardRPCMatcher {
|
140
|
+
filter;
|
141
|
+
tree = new NullProtoObj();
|
142
|
+
pendingRouters = [];
|
143
|
+
constructor(options = {}) {
|
144
|
+
this.filter = options.filter ?? true;
|
145
|
+
}
|
146
|
+
init(router, path = []) {
|
147
|
+
const laziedOptions = traverseContractProcedures({ router, path }, (traverseOptions) => {
|
148
|
+
if (!value(this.filter, traverseOptions)) {
|
149
|
+
return;
|
150
|
+
}
|
151
|
+
const { path: path2, contract } = traverseOptions;
|
152
|
+
const httpPath = toHttpPath(path2);
|
153
|
+
if (isProcedure(contract)) {
|
154
|
+
this.tree[httpPath] = {
|
155
|
+
path: path2,
|
156
|
+
contract,
|
157
|
+
procedure: contract,
|
158
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
159
|
+
router
|
160
|
+
};
|
161
|
+
} else {
|
162
|
+
this.tree[httpPath] = {
|
163
|
+
path: path2,
|
164
|
+
contract,
|
165
|
+
procedure: void 0,
|
166
|
+
router
|
167
|
+
};
|
168
|
+
}
|
169
|
+
});
|
170
|
+
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
171
|
+
...option,
|
172
|
+
httpPathPrefix: toHttpPath(option.path)
|
173
|
+
})));
|
174
|
+
}
|
175
|
+
async match(_method, pathname) {
|
176
|
+
if (this.pendingRouters.length) {
|
177
|
+
const newPendingRouters = [];
|
178
|
+
for (const pendingRouter of this.pendingRouters) {
|
179
|
+
if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
180
|
+
const { default: router } = await unlazy(pendingRouter.router);
|
181
|
+
this.init(router, pendingRouter.path);
|
182
|
+
} else {
|
183
|
+
newPendingRouters.push(pendingRouter);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
this.pendingRouters = newPendingRouters;
|
187
|
+
}
|
188
|
+
const match = this.tree[pathname];
|
189
|
+
if (!match) {
|
190
|
+
return void 0;
|
191
|
+
}
|
192
|
+
if (!match.procedure) {
|
193
|
+
const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
|
194
|
+
if (!isProcedure(maybeProcedure)) {
|
195
|
+
throw new Error(`
|
196
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
|
197
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
198
|
+
`);
|
199
|
+
}
|
200
|
+
match.procedure = createContractedProcedure(maybeProcedure, match.contract);
|
201
|
+
}
|
202
|
+
return {
|
203
|
+
path: match.path,
|
204
|
+
procedure: match.procedure
|
205
|
+
};
|
206
|
+
}
|
207
|
+
}
|
208
|
+
|
209
|
+
class StandardRPCHandler extends StandardHandler {
|
210
|
+
constructor(router, options = {}) {
|
211
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
212
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
213
|
+
const matcher = new StandardRPCMatcher(options);
|
214
|
+
const codec = new StandardRPCCodec(serializer);
|
215
|
+
super(router, matcher, codec, options);
|
216
|
+
}
|
217
|
+
}
|
218
|
+
|
219
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as S, StandardRPCCodec as a, StandardRPCHandler as b, StandardRPCMatcher as c };
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
2
|
+
import { b as AnyRouter, C as Context, R as Router } from './server.CYNGeoCm.mjs';
|
3
|
+
import { i as StandardMatcher, h as StandardMatchResult, e as StandardHandlerOptions, f as StandardHandler } from './server.gqRxT-yN.mjs';
|
4
|
+
import { HTTPPath } from '@orpc/client';
|
5
|
+
import { Value } from '@orpc/shared';
|
6
|
+
import { T as TraverseContractProcedureCallbackOptions } from './server.DhJj-1X9.mjs';
|
7
|
+
|
8
|
+
interface StandardRPCMatcherOptions {
|
9
|
+
/**
|
10
|
+
* Filter procedures. Return `false` to exclude a procedure from matching.
|
11
|
+
*
|
12
|
+
* @default true
|
13
|
+
*/
|
14
|
+
filter?: Value<boolean, [options: TraverseContractProcedureCallbackOptions]>;
|
15
|
+
}
|
16
|
+
declare class StandardRPCMatcher implements StandardMatcher {
|
17
|
+
private readonly filter;
|
18
|
+
private readonly tree;
|
19
|
+
private pendingRouters;
|
20
|
+
constructor(options?: StandardRPCMatcherOptions);
|
21
|
+
init(router: AnyRouter, path?: readonly string[]): void;
|
22
|
+
match(_method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
23
|
+
}
|
24
|
+
|
25
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions, StandardRPCMatcherOptions {
|
26
|
+
}
|
27
|
+
declare class StandardRPCHandler<T extends Context> extends StandardHandler<T> {
|
28
|
+
constructor(router: Router<any, T>, options?: StandardRPCHandlerOptions<T>);
|
29
|
+
}
|
30
|
+
|
31
|
+
export { StandardRPCHandler as a, StandardRPCMatcher as c };
|
32
|
+
export type { StandardRPCHandlerOptions as S, StandardRPCMatcherOptions as b };
|
@@ -0,0 +1,36 @@
|
|
1
|
+
import { ORPCError, fallbackContractConfig } from '@orpc/contract';
|
2
|
+
|
3
|
+
const STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL = Symbol("STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT");
|
4
|
+
class StrictGetMethodPlugin {
|
5
|
+
error;
|
6
|
+
order = 7e6;
|
7
|
+
constructor(options = {}) {
|
8
|
+
this.error = options.error ?? new ORPCError("METHOD_NOT_SUPPORTED");
|
9
|
+
}
|
10
|
+
init(options) {
|
11
|
+
options.rootInterceptors ??= [];
|
12
|
+
options.clientInterceptors ??= [];
|
13
|
+
options.rootInterceptors.unshift((options2) => {
|
14
|
+
const isGetMethod = options2.request.method === "GET";
|
15
|
+
return options2.next({
|
16
|
+
...options2,
|
17
|
+
context: {
|
18
|
+
...options2.context,
|
19
|
+
[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL]: isGetMethod
|
20
|
+
}
|
21
|
+
});
|
22
|
+
});
|
23
|
+
options.clientInterceptors.unshift((options2) => {
|
24
|
+
if (typeof options2.context[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL] !== "boolean") {
|
25
|
+
throw new TypeError("[StrictGetMethodPlugin] strict GET method context has been corrupted or modified by another plugin or interceptor");
|
26
|
+
}
|
27
|
+
const procedureMethod = fallbackContractConfig("defaultMethod", options2.procedure["~orpc"].route.method);
|
28
|
+
if (options2.context[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL] && procedureMethod !== "GET") {
|
29
|
+
throw this.error;
|
30
|
+
}
|
31
|
+
return options2.next();
|
32
|
+
});
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
export { StrictGetMethodPlugin as S };
|