@orpc/server 0.0.0-next.352022d → 0.0.0-next.35fabdb
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 +179 -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 -99
- package/dist/index.d.ts +633 -99
- package/dist/index.mjs +187 -37
- package/dist/plugins/index.d.mts +154 -16
- package/dist/plugins/index.d.ts +154 -16
- package/dist/plugins/index.mjs +198 -16
- package/dist/shared/{server.kPUGzsdw.mjs → server.7jWaIryJ.mjs} +136 -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.CM3tWr3C.d.mts +0 -75
- package/dist/shared/server.CXu_v2vM.mjs +0 -28
- package/dist/shared/server.DmW25ynm.d.ts +0 -75
- package/dist/shared/server.N2ttmv6r.mjs +0 -158
- package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import { ORPCError, toORPCError } from '@orpc/client';
|
|
2
|
-
import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
|
|
3
|
-
import { C as CompositePlugin } from './server.Q6ZmnTgO.mjs';
|
|
4
|
-
import { c as createProcedureClient, t as traverseContractProcedures, a as toHttpPath, i as isProcedure, u as unlazy, g as getRouter, b as createContractedProcedure } from './server.kPUGzsdw.mjs';
|
|
5
|
-
|
|
6
|
-
class StandardHandler {
|
|
7
|
-
constructor(router, matcher, codec, options) {
|
|
8
|
-
this.matcher = matcher;
|
|
9
|
-
this.codec = codec;
|
|
10
|
-
this.options = options;
|
|
11
|
-
this.plugin = new CompositePlugin(options.plugins);
|
|
12
|
-
this.plugin.init(this.options);
|
|
13
|
-
this.matcher.init(router);
|
|
14
|
-
}
|
|
15
|
-
plugin;
|
|
16
|
-
handle(request, ...[options]) {
|
|
17
|
-
return intercept(
|
|
18
|
-
this.options.rootInterceptors ?? [],
|
|
19
|
-
{
|
|
20
|
-
request,
|
|
21
|
-
...options,
|
|
22
|
-
context: options?.context ?? {}
|
|
23
|
-
// context is optional only when all fields are optional so we can safely force it to have a context
|
|
24
|
-
},
|
|
25
|
-
async (interceptorOptions) => {
|
|
26
|
-
let isDecoding = false;
|
|
27
|
-
try {
|
|
28
|
-
return await intercept(
|
|
29
|
-
this.options.interceptors ?? [],
|
|
30
|
-
interceptorOptions,
|
|
31
|
-
async (interceptorOptions2) => {
|
|
32
|
-
const method = interceptorOptions2.request.method;
|
|
33
|
-
const url = interceptorOptions2.request.url;
|
|
34
|
-
const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
|
|
35
|
-
const match = await this.matcher.match(method, pathname);
|
|
36
|
-
if (!match) {
|
|
37
|
-
return { matched: false, response: void 0 };
|
|
38
|
-
}
|
|
39
|
-
const client = createProcedureClient(match.procedure, {
|
|
40
|
-
context: interceptorOptions2.context,
|
|
41
|
-
path: match.path,
|
|
42
|
-
interceptors: this.options.clientInterceptors
|
|
43
|
-
});
|
|
44
|
-
isDecoding = true;
|
|
45
|
-
const input = await this.codec.decode(request, match.params, match.procedure);
|
|
46
|
-
isDecoding = false;
|
|
47
|
-
const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
|
|
48
|
-
const output = await client(input, { signal: request.signal, lastEventId });
|
|
49
|
-
const response = this.codec.encode(output, match.procedure);
|
|
50
|
-
return {
|
|
51
|
-
matched: true,
|
|
52
|
-
response
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
);
|
|
56
|
-
} catch (e) {
|
|
57
|
-
const error = isDecoding ? new ORPCError("BAD_REQUEST", {
|
|
58
|
-
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
|
59
|
-
cause: e
|
|
60
|
-
}) : toORPCError(e);
|
|
61
|
-
const response = this.codec.encodeError(error);
|
|
62
|
-
return {
|
|
63
|
-
matched: true,
|
|
64
|
-
response
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
class RPCCodec {
|
|
73
|
-
constructor(serializer) {
|
|
74
|
-
this.serializer = serializer;
|
|
75
|
-
}
|
|
76
|
-
async decode(request, _params, _procedure) {
|
|
77
|
-
const serialized = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("data").at(-1)) : await request.body();
|
|
78
|
-
return this.serializer.deserialize(serialized);
|
|
79
|
-
}
|
|
80
|
-
encode(output, _procedure) {
|
|
81
|
-
return {
|
|
82
|
-
status: 200,
|
|
83
|
-
headers: {},
|
|
84
|
-
body: this.serializer.serialize(output)
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
encodeError(error) {
|
|
88
|
-
return {
|
|
89
|
-
status: error.status,
|
|
90
|
-
headers: {},
|
|
91
|
-
body: this.serializer.serialize(error.toJSON())
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
class RPCMatcher {
|
|
97
|
-
tree = {};
|
|
98
|
-
pendingRouters = [];
|
|
99
|
-
init(router, path = []) {
|
|
100
|
-
const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
|
|
101
|
-
const httpPath = toHttpPath(path2);
|
|
102
|
-
if (isProcedure(contract)) {
|
|
103
|
-
this.tree[httpPath] = {
|
|
104
|
-
path: path2,
|
|
105
|
-
contract,
|
|
106
|
-
procedure: contract,
|
|
107
|
-
// this mean dev not used contract-first so we can used contract as procedure directly
|
|
108
|
-
router
|
|
109
|
-
};
|
|
110
|
-
} else {
|
|
111
|
-
this.tree[httpPath] = {
|
|
112
|
-
path: path2,
|
|
113
|
-
contract,
|
|
114
|
-
procedure: void 0,
|
|
115
|
-
router
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
|
120
|
-
...option,
|
|
121
|
-
httpPathPrefix: toHttpPath(option.path)
|
|
122
|
-
})));
|
|
123
|
-
}
|
|
124
|
-
async match(_method, pathname) {
|
|
125
|
-
if (this.pendingRouters.length) {
|
|
126
|
-
const newPendingRouters = [];
|
|
127
|
-
for (const pendingRouter of this.pendingRouters) {
|
|
128
|
-
if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
129
|
-
const { default: router } = await unlazy(pendingRouter.router);
|
|
130
|
-
this.init(router, pendingRouter.path);
|
|
131
|
-
} else {
|
|
132
|
-
newPendingRouters.push(pendingRouter);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
this.pendingRouters = newPendingRouters;
|
|
136
|
-
}
|
|
137
|
-
const match = this.tree[pathname];
|
|
138
|
-
if (!match) {
|
|
139
|
-
return void 0;
|
|
140
|
-
}
|
|
141
|
-
if (!match.procedure) {
|
|
142
|
-
const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
|
|
143
|
-
if (!isProcedure(maybeProcedure)) {
|
|
144
|
-
throw new Error(`
|
|
145
|
-
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
|
|
146
|
-
Ensure that the procedure is correctly defined and matches the expected contract.
|
|
147
|
-
`);
|
|
148
|
-
}
|
|
149
|
-
match.procedure = createContractedProcedure(maybeProcedure, match.contract);
|
|
150
|
-
}
|
|
151
|
-
return {
|
|
152
|
-
path: match.path,
|
|
153
|
-
procedure: match.procedure
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export { RPCCodec as R, StandardHandler as S, RPCMatcher as a };
|