@orpc/server 0.0.0-next.09ec5b4 → 0.0.0-next.0adc01c
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 +14 -1
- package/dist/adapters/fetch/index.d.mts +42 -11
- package/dist/adapters/fetch/index.d.ts +42 -11
- package/dist/adapters/fetch/index.mjs +7 -5
- package/dist/adapters/hono/index.d.mts +6 -4
- package/dist/adapters/hono/index.d.ts +6 -4
- package/dist/adapters/hono/index.mjs +7 -5
- package/dist/adapters/next/index.d.mts +6 -4
- package/dist/adapters/next/index.d.ts +6 -4
- package/dist/adapters/next/index.mjs +7 -5
- package/dist/adapters/node/index.d.mts +44 -22
- package/dist/adapters/node/index.d.ts +44 -22
- package/dist/adapters/node/index.mjs +82 -20
- package/dist/adapters/standard/index.d.mts +14 -17
- package/dist/adapters/standard/index.d.ts +14 -17
- package/dist/adapters/standard/index.mjs +6 -5
- package/dist/index.d.mts +160 -124
- package/dist/index.d.ts +160 -124
- package/dist/index.mjs +78 -48
- package/dist/plugins/index.d.mts +111 -18
- package/dist/plugins/index.d.ts +111 -18
- package/dist/plugins/index.mjs +148 -7
- 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.V6zT5iYQ.mjs → server.C37gDhSZ.mjs} +158 -173
- 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 +8 -8
- package/dist/shared/server.BBGuTxHE.mjs +0 -163
- package/dist/shared/server.B_cAGti1.d.mts +0 -9
- package/dist/shared/server.CUE4Aija.mjs +0 -24
- package/dist/shared/server.Cn9ybJtE.d.mts +0 -152
- package/dist/shared/server.Cn9ybJtE.d.ts +0 -152
- package/dist/shared/server.CynXWJja.d.ts +0 -9
- package/dist/shared/server.D86dtDX_.d.mts +0 -77
- package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
- package/dist/shared/server.dPmfqnQI.d.ts +0 -77
package/dist/plugins/index.mjs
CHANGED
@@ -1,9 +1,102 @@
|
|
1
|
-
export { C as CompositePlugin } from '../shared/server.Q6ZmnTgO.mjs';
|
2
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
|
+
}
|
3
95
|
|
4
96
|
class CORSPlugin {
|
5
97
|
options;
|
6
|
-
|
98
|
+
order = 9e6;
|
99
|
+
constructor(options = {}) {
|
7
100
|
const defaults = {
|
8
101
|
origin: (origin) => origin,
|
9
102
|
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
|
@@ -79,14 +172,19 @@ class ResponseHeadersPlugin {
|
|
79
172
|
init(options) {
|
80
173
|
options.rootInterceptors ??= [];
|
81
174
|
options.rootInterceptors.push(async (interceptorOptions) => {
|
82
|
-
const
|
83
|
-
|
84
|
-
|
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
|
+
});
|
85
183
|
if (!result.matched) {
|
86
184
|
return result;
|
87
185
|
}
|
88
186
|
const responseHeaders = result.response.headers;
|
89
|
-
for (const [key, value] of
|
187
|
+
for (const [key, value] of resHeaders) {
|
90
188
|
if (Array.isArray(responseHeaders[key])) {
|
91
189
|
responseHeaders[key].push(value);
|
92
190
|
} else if (responseHeaders[key] !== void 0) {
|
@@ -100,4 +198,47 @@ class ResponseHeadersPlugin {
|
|
100
198
|
}
|
101
199
|
}
|
102
200
|
|
103
|
-
|
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 };
|