@orpc/server 0.0.0-next.c59d67c → 0.0.0-next.c72b962
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 +131 -0
- package/dist/adapters/fetch/index.d.mts +58 -0
- package/dist/adapters/fetch/index.d.ts +58 -0
- package/dist/adapters/fetch/index.mjs +11 -0
- package/dist/adapters/hono/index.d.mts +22 -0
- package/dist/adapters/hono/index.d.ts +22 -0
- package/dist/adapters/hono/index.mjs +34 -0
- package/dist/adapters/next/index.d.mts +29 -0
- package/dist/adapters/next/index.d.ts +29 -0
- package/dist/adapters/next/index.mjs +31 -0
- package/dist/adapters/node/index.d.mts +57 -0
- package/dist/adapters/node/index.d.ts +57 -0
- package/dist/adapters/node/index.mjs +90 -0
- package/dist/adapters/standard/index.d.mts +26 -0
- package/dist/adapters/standard/index.d.ts +26 -0
- package/dist/adapters/standard/index.mjs +8 -0
- package/dist/index.d.mts +291 -0
- package/dist/index.d.ts +291 -0
- package/dist/index.mjs +363 -0
- package/dist/plugins/index.d.mts +124 -0
- package/dist/plugins/index.d.ts +124 -0
- package/dist/plugins/index.mjs +244 -0
- package/dist/shared/server.BVwwTHyO.mjs +9 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.Bm0UqHzd.mjs +103 -0
- package/dist/shared/server.C37gDhSZ.mjs +364 -0
- package/dist/shared/server.C8NkqxHo.d.ts +17 -0
- package/dist/shared/server.CGCwEAt_.d.mts +10 -0
- package/dist/shared/server.DCQgF_JR.d.mts +17 -0
- package/dist/shared/server.DFFT_EZo.d.ts +73 -0
- package/dist/shared/server.DFuJLDuo.mjs +190 -0
- package/dist/shared/server.DLt5njUb.d.mts +143 -0
- package/dist/shared/server.DLt5njUb.d.ts +143 -0
- package/dist/shared/server.DOYDVeMX.d.mts +73 -0
- package/dist/shared/server._2UufoXA.d.ts +10 -0
- package/package.json +41 -18
- package/dist/chunk-TDFYNRZV.js +0 -190
- package/dist/fetch.js +0 -106
- package/dist/index.js +0 -394
- package/dist/src/builder.d.ts +0 -49
- package/dist/src/fetch/handle.d.ts +0 -7
- package/dist/src/fetch/handler.d.ts +0 -3
- package/dist/src/fetch/index.d.ts +0 -4
- package/dist/src/fetch/types.d.ts +0 -35
- package/dist/src/index.d.ts +0 -15
- package/dist/src/middleware.d.ts +0 -26
- package/dist/src/procedure-builder.d.ts +0 -31
- package/dist/src/procedure-caller.d.ts +0 -19
- package/dist/src/procedure-implementer.d.ts +0 -18
- package/dist/src/procedure.d.ts +0 -29
- package/dist/src/router-builder.d.ts +0 -22
- package/dist/src/router-caller.d.ts +0 -22
- package/dist/src/router-implementer.d.ts +0 -20
- package/dist/src/router.d.ts +0 -20
- package/dist/src/types.d.ts +0 -8
- package/dist/src/utils.d.ts +0 -3
@@ -0,0 +1,244 @@
|
|
1
|
+
import { value } from '@orpc/shared';
|
2
|
+
import { parseBatchRequest, toBatchResponse } from '@orpc/standard-server/batch';
|
3
|
+
import { ORPCError } from '@orpc/client';
|
4
|
+
export { S as StrictGetMethodPlugin } from '../shared/server.BW-nUGgA.mjs';
|
5
|
+
import '@orpc/contract';
|
6
|
+
|
7
|
+
class BatchHandlerPlugin {
|
8
|
+
maxSize;
|
9
|
+
mapRequestItem;
|
10
|
+
successStatus;
|
11
|
+
headers;
|
12
|
+
order = 5e6;
|
13
|
+
constructor(options = {}) {
|
14
|
+
this.maxSize = options.maxSize ?? 10;
|
15
|
+
this.mapRequestItem = options.mapRequestItem ?? ((request, { request: batchRequest }) => ({
|
16
|
+
...request,
|
17
|
+
headers: {
|
18
|
+
...batchRequest.headers,
|
19
|
+
...request.headers
|
20
|
+
}
|
21
|
+
}));
|
22
|
+
this.successStatus = options.successStatus ?? 207;
|
23
|
+
this.headers = options.headers ?? {};
|
24
|
+
}
|
25
|
+
init(options) {
|
26
|
+
options.rootInterceptors ??= [];
|
27
|
+
options.rootInterceptors.unshift(async (options2) => {
|
28
|
+
if (options2.request.headers["x-orpc-batch"] !== "1") {
|
29
|
+
return options2.next();
|
30
|
+
}
|
31
|
+
let isParsing = false;
|
32
|
+
try {
|
33
|
+
isParsing = true;
|
34
|
+
const parsed = parseBatchRequest({ ...options2.request, body: await options2.request.body() });
|
35
|
+
isParsing = false;
|
36
|
+
const maxSize = await value(this.maxSize, options2);
|
37
|
+
if (parsed.length > maxSize) {
|
38
|
+
return {
|
39
|
+
matched: true,
|
40
|
+
response: {
|
41
|
+
status: 413,
|
42
|
+
headers: {},
|
43
|
+
body: "Batch request size exceeds the maximum allowed size"
|
44
|
+
}
|
45
|
+
};
|
46
|
+
}
|
47
|
+
const responses = parsed.map(
|
48
|
+
(request, index) => {
|
49
|
+
const mapped = this.mapRequestItem(request, options2);
|
50
|
+
return options2.next({ ...options2, request: { ...mapped, body: () => Promise.resolve(mapped.body) } }).then(({ response: response2, matched }) => {
|
51
|
+
if (matched) {
|
52
|
+
return { ...response2, index };
|
53
|
+
}
|
54
|
+
return { index, status: 404, headers: {}, body: "No procedure matched" };
|
55
|
+
}).catch(() => {
|
56
|
+
return { index, status: 500, headers: {}, body: "Internal server error" };
|
57
|
+
});
|
58
|
+
}
|
59
|
+
);
|
60
|
+
await Promise.race(responses);
|
61
|
+
const status = await value(this.successStatus, responses, options2);
|
62
|
+
const headers = await value(this.headers, responses, options2);
|
63
|
+
const response = toBatchResponse({
|
64
|
+
status,
|
65
|
+
headers,
|
66
|
+
body: async function* () {
|
67
|
+
const promises = [...responses];
|
68
|
+
while (true) {
|
69
|
+
const handling = promises.filter((p) => p !== void 0);
|
70
|
+
if (handling.length === 0) {
|
71
|
+
return;
|
72
|
+
}
|
73
|
+
const result = await Promise.race(handling);
|
74
|
+
promises[result.index] = void 0;
|
75
|
+
yield result;
|
76
|
+
}
|
77
|
+
}()
|
78
|
+
});
|
79
|
+
return {
|
80
|
+
matched: true,
|
81
|
+
response
|
82
|
+
};
|
83
|
+
} catch (cause) {
|
84
|
+
if (isParsing) {
|
85
|
+
return {
|
86
|
+
matched: true,
|
87
|
+
response: { status: 400, headers: {}, body: "Invalid batch request, this could be caused by a malformed request body or a missing header" }
|
88
|
+
};
|
89
|
+
}
|
90
|
+
throw cause;
|
91
|
+
}
|
92
|
+
});
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
class CORSPlugin {
|
97
|
+
options;
|
98
|
+
order = 9e6;
|
99
|
+
constructor(options = {}) {
|
100
|
+
const defaults = {
|
101
|
+
origin: (origin) => origin,
|
102
|
+
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
|
103
|
+
};
|
104
|
+
this.options = {
|
105
|
+
...defaults,
|
106
|
+
...options
|
107
|
+
};
|
108
|
+
}
|
109
|
+
init(options) {
|
110
|
+
options.rootInterceptors ??= [];
|
111
|
+
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
112
|
+
if (interceptorOptions.request.method === "OPTIONS") {
|
113
|
+
const resHeaders = {};
|
114
|
+
if (this.options.maxAge !== void 0) {
|
115
|
+
resHeaders["access-control-max-age"] = this.options.maxAge.toString();
|
116
|
+
}
|
117
|
+
if (this.options.allowMethods?.length) {
|
118
|
+
resHeaders["access-control-allow-methods"] = this.options.allowMethods.join(",");
|
119
|
+
}
|
120
|
+
const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
|
121
|
+
if (Array.isArray(allowHeaders) && allowHeaders.length) {
|
122
|
+
resHeaders["access-control-allow-headers"] = allowHeaders.join(",");
|
123
|
+
} else if (typeof allowHeaders === "string") {
|
124
|
+
resHeaders["access-control-allow-headers"] = allowHeaders;
|
125
|
+
}
|
126
|
+
return {
|
127
|
+
matched: true,
|
128
|
+
response: {
|
129
|
+
status: 204,
|
130
|
+
headers: resHeaders,
|
131
|
+
body: void 0
|
132
|
+
}
|
133
|
+
};
|
134
|
+
}
|
135
|
+
return interceptorOptions.next();
|
136
|
+
});
|
137
|
+
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
138
|
+
const result = await interceptorOptions.next();
|
139
|
+
if (!result.matched) {
|
140
|
+
return result;
|
141
|
+
}
|
142
|
+
const origin = Array.isArray(interceptorOptions.request.headers.origin) ? interceptorOptions.request.headers.origin.join(",") : interceptorOptions.request.headers.origin || "";
|
143
|
+
const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
|
144
|
+
const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
|
145
|
+
if (allowedOriginArr.includes("*")) {
|
146
|
+
result.response.headers["access-control-allow-origin"] = "*";
|
147
|
+
} else {
|
148
|
+
if (allowedOriginArr.includes(origin)) {
|
149
|
+
result.response.headers["access-control-allow-origin"] = origin;
|
150
|
+
}
|
151
|
+
result.response.headers.vary = interceptorOptions.request.headers.vary ?? "origin";
|
152
|
+
}
|
153
|
+
const allowedTimingOrigin = await value(this.options.timingOrigin, origin, interceptorOptions);
|
154
|
+
const allowedTimingOriginArr = Array.isArray(allowedTimingOrigin) ? allowedTimingOrigin : [allowedTimingOrigin];
|
155
|
+
if (allowedTimingOriginArr.includes("*")) {
|
156
|
+
result.response.headers["timing-allow-origin"] = "*";
|
157
|
+
} else if (allowedTimingOriginArr.includes(origin)) {
|
158
|
+
result.response.headers["timing-allow-origin"] = origin;
|
159
|
+
}
|
160
|
+
if (this.options.credentials) {
|
161
|
+
result.response.headers["access-control-allow-credentials"] = "true";
|
162
|
+
}
|
163
|
+
if (this.options.exposeHeaders?.length) {
|
164
|
+
result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders.join(",");
|
165
|
+
}
|
166
|
+
return result;
|
167
|
+
});
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
class ResponseHeadersPlugin {
|
172
|
+
init(options) {
|
173
|
+
options.rootInterceptors ??= [];
|
174
|
+
options.rootInterceptors.push(async (interceptorOptions) => {
|
175
|
+
const resHeaders = interceptorOptions.context.resHeaders ?? new Headers();
|
176
|
+
const result = await interceptorOptions.next({
|
177
|
+
...interceptorOptions,
|
178
|
+
context: {
|
179
|
+
...interceptorOptions.context,
|
180
|
+
resHeaders
|
181
|
+
}
|
182
|
+
});
|
183
|
+
if (!result.matched) {
|
184
|
+
return result;
|
185
|
+
}
|
186
|
+
const responseHeaders = result.response.headers;
|
187
|
+
for (const [key, value] of resHeaders) {
|
188
|
+
if (Array.isArray(responseHeaders[key])) {
|
189
|
+
responseHeaders[key].push(value);
|
190
|
+
} else if (responseHeaders[key] !== void 0) {
|
191
|
+
responseHeaders[key] = [responseHeaders[key], value];
|
192
|
+
} else {
|
193
|
+
responseHeaders[key] = value;
|
194
|
+
}
|
195
|
+
}
|
196
|
+
return result;
|
197
|
+
});
|
198
|
+
}
|
199
|
+
}
|
200
|
+
|
201
|
+
const SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL = Symbol("SIMPLE_CSRF_PROTECTION_CONTEXT");
|
202
|
+
class SimpleCsrfProtectionHandlerPlugin {
|
203
|
+
headerName;
|
204
|
+
headerValue;
|
205
|
+
exclude;
|
206
|
+
error;
|
207
|
+
constructor(options = {}) {
|
208
|
+
this.headerName = options.headerName ?? "x-csrf-token";
|
209
|
+
this.headerValue = options.headerValue ?? "orpc";
|
210
|
+
this.exclude = options.exclude ?? false;
|
211
|
+
this.error = options.error ?? new ORPCError("CSRF_TOKEN_MISMATCH", {
|
212
|
+
status: 403,
|
213
|
+
message: "Invalid CSRF token"
|
214
|
+
});
|
215
|
+
}
|
216
|
+
order = 8e6;
|
217
|
+
init(options) {
|
218
|
+
options.rootInterceptors ??= [];
|
219
|
+
options.clientInterceptors ??= [];
|
220
|
+
options.rootInterceptors.unshift(async (options2) => {
|
221
|
+
const headerName = await value(this.headerName, options2);
|
222
|
+
const headerValue = await value(this.headerValue, options2);
|
223
|
+
return options2.next({
|
224
|
+
...options2,
|
225
|
+
context: {
|
226
|
+
...options2.context,
|
227
|
+
[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]: options2.request.headers[headerName] === headerValue
|
228
|
+
}
|
229
|
+
});
|
230
|
+
});
|
231
|
+
options.clientInterceptors.unshift(async (options2) => {
|
232
|
+
if (typeof options2.context[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL] !== "boolean") {
|
233
|
+
throw new TypeError("[SimpleCsrfProtectionHandlerPlugin] CSRF protection context has been corrupted or modified by another plugin or interceptor");
|
234
|
+
}
|
235
|
+
const excluded = await value(this.exclude, options2);
|
236
|
+
if (!excluded && !options2.context[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]) {
|
237
|
+
throw this.error;
|
238
|
+
}
|
239
|
+
return options2.next();
|
240
|
+
});
|
241
|
+
}
|
242
|
+
}
|
243
|
+
|
244
|
+
export { BatchHandlerPlugin, CORSPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin };
|
@@ -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 };
|
@@ -0,0 +1,103 @@
|
|
1
|
+
import { ORPCError } from '@orpc/client';
|
2
|
+
import { toArray, intercept, resolveMaybeOptionalOptions } from '@orpc/shared';
|
3
|
+
import '@orpc/contract';
|
4
|
+
import { C as CompositeStandardHandlerPlugin, b as StandardRPCHandler } from './server.DFuJLDuo.mjs';
|
5
|
+
import '@orpc/client/standard';
|
6
|
+
import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
|
7
|
+
import { r as resolveFriendlyStandardHandleOptions } from './server.BVwwTHyO.mjs';
|
8
|
+
import '@orpc/standard-server/batch';
|
9
|
+
|
10
|
+
class BodyLimitPlugin {
|
11
|
+
maxBodySize;
|
12
|
+
constructor(options) {
|
13
|
+
this.maxBodySize = options.maxBodySize;
|
14
|
+
}
|
15
|
+
initRuntimeAdapter(options) {
|
16
|
+
options.adapterInterceptors ??= [];
|
17
|
+
options.adapterInterceptors.push(async (options2) => {
|
18
|
+
if (!options2.request.body) {
|
19
|
+
return options2.next();
|
20
|
+
}
|
21
|
+
let currentBodySize = 0;
|
22
|
+
const rawReader = options2.request.body.getReader();
|
23
|
+
const reader = new ReadableStream({
|
24
|
+
start: async (controller) => {
|
25
|
+
try {
|
26
|
+
if (Number(options2.request.headers.get("content-length")) > this.maxBodySize) {
|
27
|
+
controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
|
28
|
+
return;
|
29
|
+
}
|
30
|
+
while (true) {
|
31
|
+
const { done, value } = await rawReader.read();
|
32
|
+
if (done) {
|
33
|
+
break;
|
34
|
+
}
|
35
|
+
currentBodySize += value.length;
|
36
|
+
if (currentBodySize > this.maxBodySize) {
|
37
|
+
controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
|
38
|
+
break;
|
39
|
+
}
|
40
|
+
controller.enqueue(value);
|
41
|
+
}
|
42
|
+
} finally {
|
43
|
+
controller.close();
|
44
|
+
}
|
45
|
+
}
|
46
|
+
});
|
47
|
+
const requestInit = { body: reader, duplex: "half" };
|
48
|
+
return options2.next({
|
49
|
+
...options2,
|
50
|
+
request: new Request(options2.request, requestInit)
|
51
|
+
});
|
52
|
+
});
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
class CompositeFetchHandlerPlugin extends CompositeStandardHandlerPlugin {
|
57
|
+
initRuntimeAdapter(options) {
|
58
|
+
for (const plugin of this.plugins) {
|
59
|
+
plugin.initRuntimeAdapter?.(options);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
class FetchHandler {
|
65
|
+
constructor(standardHandler, options = {}) {
|
66
|
+
this.standardHandler = standardHandler;
|
67
|
+
const plugin = new CompositeFetchHandlerPlugin(options.plugins);
|
68
|
+
plugin.initRuntimeAdapter(options);
|
69
|
+
this.adapterInterceptors = toArray(options.adapterInterceptors);
|
70
|
+
this.toFetchResponseOptions = options;
|
71
|
+
}
|
72
|
+
toFetchResponseOptions;
|
73
|
+
adapterInterceptors;
|
74
|
+
async handle(request, ...rest) {
|
75
|
+
return intercept(
|
76
|
+
this.adapterInterceptors,
|
77
|
+
{
|
78
|
+
...resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest)),
|
79
|
+
request,
|
80
|
+
toFetchResponseOptions: this.toFetchResponseOptions
|
81
|
+
},
|
82
|
+
async ({ request: request2, toFetchResponseOptions, ...options }) => {
|
83
|
+
const standardRequest = toStandardLazyRequest(request2);
|
84
|
+
const result = await this.standardHandler.handle(standardRequest, options);
|
85
|
+
if (!result.matched) {
|
86
|
+
return result;
|
87
|
+
}
|
88
|
+
return {
|
89
|
+
matched: true,
|
90
|
+
response: toFetchResponse(result.response, toFetchResponseOptions)
|
91
|
+
};
|
92
|
+
}
|
93
|
+
);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
class RPCHandler extends FetchHandler {
|
98
|
+
constructor(router, options = {}) {
|
99
|
+
super(new StandardRPCHandler(router, options), options);
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
export { BodyLimitPlugin as B, CompositeFetchHandlerPlugin as C, FetchHandler as F, RPCHandler as R };
|