@orpc/server 0.0.0-next.93e7a4c → 0.0.0-next.93fa264
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 +126 -0
- package/dist/adapters/aws-lambda/index.d.mts +45 -0
- package/dist/adapters/aws-lambda/index.d.ts +45 -0
- package/dist/adapters/aws-lambda/index.mjs +41 -0
- package/dist/adapters/bun-ws/index.d.mts +35 -0
- package/dist/adapters/bun-ws/index.d.ts +35 -0
- package/dist/adapters/bun-ws/index.mjs +46 -0
- package/dist/adapters/crossws/index.d.mts +30 -0
- package/dist/adapters/crossws/index.d.ts +30 -0
- package/dist/adapters/crossws/index.mjs +46 -0
- package/dist/adapters/fetch/index.d.mts +78 -0
- package/dist/adapters/fetch/index.d.ts +78 -0
- package/dist/adapters/fetch/index.mjs +110 -0
- package/dist/adapters/message-port/index.d.mts +28 -0
- package/dist/adapters/message-port/index.d.ts +28 -0
- package/dist/adapters/message-port/index.mjs +41 -0
- package/dist/adapters/node/index.d.mts +77 -0
- package/dist/adapters/node/index.d.ts +77 -0
- package/dist/adapters/node/index.mjs +95 -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 +7 -0
- package/dist/adapters/websocket/index.d.mts +51 -0
- package/dist/adapters/websocket/index.d.ts +51 -0
- package/dist/adapters/websocket/index.mjs +69 -0
- package/dist/adapters/ws/index.d.mts +28 -0
- package/dist/adapters/ws/index.d.ts +28 -0
- package/dist/adapters/ws/index.mjs +39 -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 +834 -0
- package/dist/index.d.ts +834 -0
- package/dist/index.mjs +488 -0
- package/dist/plugins/index.d.mts +156 -0
- package/dist/plugins/index.d.ts +156 -0
- package/dist/plugins/index.mjs +253 -0
- package/dist/shared/server.-ACo36I0.d.ts +74 -0
- package/dist/shared/server.BPAWobQg.d.ts +12 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.Bd52nNaH.d.mts +12 -0
- package/dist/shared/server.BliFSTnG.d.mts +10 -0
- package/dist/shared/server.CHvOkcM3.mjs +186 -0
- package/dist/shared/server.DD2C4ujN.d.mts +192 -0
- package/dist/shared/server.DD2C4ujN.d.ts +192 -0
- package/dist/shared/server.DG7Tamti.mjs +367 -0
- package/dist/shared/server.DZ5BIITo.mjs +9 -0
- package/dist/shared/server.Dq8xr7PQ.d.mts +74 -0
- package/dist/shared/server.IG2MjhrD.d.ts +10 -0
- package/package.json +82 -15
- package/dist/chunk-FN62GL22.js +0 -182
- package/dist/fetch.js +0 -286
- package/dist/index.js +0 -518
- package/dist/src/builder.d.ts +0 -35
- package/dist/src/fetch/composite-handler.d.ts +0 -8
- package/dist/src/fetch/index.d.ts +0 -6
- package/dist/src/fetch/orpc-handler.d.ts +0 -20
- package/dist/src/fetch/orpc-payload-codec.d.ts +0 -9
- package/dist/src/fetch/orpc-procedure-matcher.d.ts +0 -12
- package/dist/src/fetch/super-json.d.ts +0 -12
- package/dist/src/fetch/types.d.ts +0 -16
- package/dist/src/hidden.d.ts +0 -6
- package/dist/src/implementer-chainable.d.ts +0 -10
- package/dist/src/index.d.ts +0 -23
- package/dist/src/lazy-decorated.d.ts +0 -10
- package/dist/src/lazy-utils.d.ts +0 -4
- package/dist/src/lazy.d.ts +0 -18
- package/dist/src/middleware-decorated.d.ts +0 -8
- package/dist/src/middleware.d.ts +0 -23
- package/dist/src/procedure-builder.d.ts +0 -22
- package/dist/src/procedure-client.d.ts +0 -29
- package/dist/src/procedure-decorated.d.ts +0 -14
- package/dist/src/procedure-implementer.d.ts +0 -18
- package/dist/src/procedure.d.ts +0 -23
- package/dist/src/router-builder.d.ts +0 -29
- package/dist/src/router-client.d.ts +0 -25
- package/dist/src/router-implementer.d.ts +0 -21
- package/dist/src/router.d.ts +0 -16
- package/dist/src/types.d.ts +0 -12
- package/dist/src/utils.d.ts +0 -3
@@ -0,0 +1,156 @@
|
|
1
|
+
import { Value, Promisable } from '@orpc/shared';
|
2
|
+
import { StandardRequest, StandardHeaders } from '@orpc/standard-server';
|
3
|
+
import { BatchResponseBodyItem } from '@orpc/standard-server/batch';
|
4
|
+
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.-ACo36I0.js';
|
5
|
+
import { C as Context, F as ProcedureClientInterceptorOptions } from '../shared/server.DD2C4ujN.js';
|
6
|
+
import { Meta, ORPCError as ORPCError$1 } from '@orpc/contract';
|
7
|
+
import { ORPCError } from '@orpc/client';
|
8
|
+
|
9
|
+
interface BatchHandlerOptions<T extends Context> {
|
10
|
+
/**
|
11
|
+
* The max size of the batch allowed.
|
12
|
+
*
|
13
|
+
* @default 10
|
14
|
+
*/
|
15
|
+
maxSize?: Value<Promisable<number>, [StandardHandlerInterceptorOptions<T>]>;
|
16
|
+
/**
|
17
|
+
* Map the request before processing it.
|
18
|
+
*
|
19
|
+
* @default merged back batch request headers into the request
|
20
|
+
*/
|
21
|
+
mapRequestItem?(request: StandardRequest, batchOptions: StandardHandlerInterceptorOptions<T>): StandardRequest;
|
22
|
+
/**
|
23
|
+
* Success batch response status code.
|
24
|
+
*
|
25
|
+
* @default 207
|
26
|
+
*/
|
27
|
+
successStatus?: Value<Promisable<number>, [responses: Promise<BatchResponseBodyItem>[], batchOptions: StandardHandlerInterceptorOptions<T>]>;
|
28
|
+
/**
|
29
|
+
* success batch response headers.
|
30
|
+
*
|
31
|
+
* @default {}
|
32
|
+
*/
|
33
|
+
headers?: Value<Promisable<StandardHeaders>, [responses: Promise<BatchResponseBodyItem>[], batchOptions: StandardHandlerInterceptorOptions<T>]>;
|
34
|
+
}
|
35
|
+
/**
|
36
|
+
* The Batch Requests Plugin allows you to combine multiple requests and responses into a single batch,
|
37
|
+
* reducing the overhead of sending each one separately.
|
38
|
+
*
|
39
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/batch-requests Batch Requests Plugin Docs}
|
40
|
+
*/
|
41
|
+
declare class BatchHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
42
|
+
private readonly maxSize;
|
43
|
+
private readonly mapRequestItem;
|
44
|
+
private readonly successStatus;
|
45
|
+
private readonly headers;
|
46
|
+
order: number;
|
47
|
+
constructor(options?: BatchHandlerOptions<T>);
|
48
|
+
init(options: StandardHandlerOptions<T>): void;
|
49
|
+
}
|
50
|
+
|
51
|
+
interface CORSOptions<T extends Context> {
|
52
|
+
origin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
|
53
|
+
timingOrigin?: Value<Promisable<string | readonly string[] | null | undefined>, [origin: string, options: StandardHandlerInterceptorOptions<T>]>;
|
54
|
+
allowMethods?: readonly string[];
|
55
|
+
allowHeaders?: readonly string[];
|
56
|
+
maxAge?: number;
|
57
|
+
credentials?: boolean;
|
58
|
+
exposeHeaders?: readonly string[];
|
59
|
+
}
|
60
|
+
/**
|
61
|
+
* CORSPlugin is a plugin for oRPC that allows you to configure CORS for your API.
|
62
|
+
*
|
63
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/cors CORS Plugin Docs}
|
64
|
+
*/
|
65
|
+
declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
66
|
+
private readonly options;
|
67
|
+
order: number;
|
68
|
+
constructor(options?: CORSOptions<T>);
|
69
|
+
init(options: StandardHandlerOptions<T>): void;
|
70
|
+
}
|
71
|
+
|
72
|
+
interface ResponseHeadersPluginContext {
|
73
|
+
resHeaders?: Headers;
|
74
|
+
}
|
75
|
+
/**
|
76
|
+
* The Response Headers Plugin allows you to set response headers in oRPC.
|
77
|
+
* It injects a resHeaders instance into the context, enabling you to modify response headers easily.
|
78
|
+
*
|
79
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/response-headers Response Headers Plugin Docs}
|
80
|
+
*/
|
81
|
+
declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
82
|
+
init(options: StandardHandlerOptions<T>): void;
|
83
|
+
}
|
84
|
+
|
85
|
+
interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
86
|
+
/**
|
87
|
+
* The name of the header to check.
|
88
|
+
*
|
89
|
+
* @default 'x-csrf-token'
|
90
|
+
*/
|
91
|
+
headerName?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
|
92
|
+
/**
|
93
|
+
* The value of the header to check.
|
94
|
+
*
|
95
|
+
* @default 'orpc'
|
96
|
+
*
|
97
|
+
*/
|
98
|
+
headerValue?: Value<Promisable<string>, [options: StandardHandlerInterceptorOptions<T>]>;
|
99
|
+
/**
|
100
|
+
* Exclude a procedure from the plugin.
|
101
|
+
*
|
102
|
+
* @default false
|
103
|
+
*
|
104
|
+
*/
|
105
|
+
exclude?: Value<Promisable<boolean>, [options: ProcedureClientInterceptorOptions<T, Record<never, never>, Meta>]>;
|
106
|
+
/**
|
107
|
+
* The error thrown when the CSRF token is invalid.
|
108
|
+
*
|
109
|
+
* @default new ORPCError('CSRF_TOKEN_MISMATCH', {
|
110
|
+
* status: 403,
|
111
|
+
* message: 'Invalid CSRF token',
|
112
|
+
* })
|
113
|
+
*/
|
114
|
+
error?: InstanceType<typeof ORPCError>;
|
115
|
+
}
|
116
|
+
/**
|
117
|
+
* This plugin adds basic Cross-Site Request Forgery (CSRF) protection to your oRPC application.
|
118
|
+
* It helps ensure that requests to your procedures originate from JavaScript code,
|
119
|
+
* not from other sources like standard HTML forms or direct browser navigation.
|
120
|
+
*
|
121
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/simple-csrf-protection Simple CSRF Protection Plugin Docs}
|
122
|
+
*/
|
123
|
+
declare class SimpleCsrfProtectionHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
124
|
+
private readonly headerName;
|
125
|
+
private readonly headerValue;
|
126
|
+
private readonly exclude;
|
127
|
+
private readonly error;
|
128
|
+
constructor(options?: SimpleCsrfProtectionHandlerPluginOptions<T>);
|
129
|
+
order: number;
|
130
|
+
init(options: StandardHandlerOptions<T>): void;
|
131
|
+
}
|
132
|
+
|
133
|
+
interface StrictGetMethodPluginOptions {
|
134
|
+
/**
|
135
|
+
* The error thrown when a GET request is made to a procedure that doesn't allow GET.
|
136
|
+
*
|
137
|
+
* @default new ORPCError('METHOD_NOT_SUPPORTED')
|
138
|
+
*/
|
139
|
+
error?: InstanceType<typeof ORPCError$1>;
|
140
|
+
}
|
141
|
+
/**
|
142
|
+
* This plugin enhances security by ensuring only procedures explicitly marked to accept GET requests
|
143
|
+
* can be called using the HTTP GET method for RPC Protocol. This helps prevent certain types of
|
144
|
+
* Cross-Site Request Forgery (CSRF) attacks.
|
145
|
+
*
|
146
|
+
* @see {@link https://orpc.unnoq.com/docs/plugins/strict-get-method Strict Get Method Plugin Docs}
|
147
|
+
*/
|
148
|
+
declare class StrictGetMethodPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
149
|
+
private readonly error;
|
150
|
+
order: number;
|
151
|
+
constructor(options?: StrictGetMethodPluginOptions);
|
152
|
+
init(options: StandardHandlerOptions<T>): void;
|
153
|
+
}
|
154
|
+
|
155
|
+
export { BatchHandlerPlugin, CORSPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin, StrictGetMethodPlugin };
|
156
|
+
export type { BatchHandlerOptions, CORSOptions, ResponseHeadersPluginContext, SimpleCsrfProtectionHandlerPluginOptions, StrictGetMethodPluginOptions };
|
@@ -0,0 +1,253 @@
|
|
1
|
+
import { value, isAsyncIteratorObject } from '@orpc/shared';
|
2
|
+
import { flattenHeader } from '@orpc/standard-server';
|
3
|
+
import { parseBatchRequest, toBatchResponse } from '@orpc/standard-server/batch';
|
4
|
+
import { ORPCError } from '@orpc/client';
|
5
|
+
export { S as StrictGetMethodPlugin } from '../shared/server.BW-nUGgA.mjs';
|
6
|
+
import '@orpc/contract';
|
7
|
+
|
8
|
+
class BatchHandlerPlugin {
|
9
|
+
maxSize;
|
10
|
+
mapRequestItem;
|
11
|
+
successStatus;
|
12
|
+
headers;
|
13
|
+
order = 5e6;
|
14
|
+
constructor(options = {}) {
|
15
|
+
this.maxSize = options.maxSize ?? 10;
|
16
|
+
this.mapRequestItem = options.mapRequestItem ?? ((request, { request: batchRequest }) => ({
|
17
|
+
...request,
|
18
|
+
headers: {
|
19
|
+
...batchRequest.headers,
|
20
|
+
...request.headers
|
21
|
+
}
|
22
|
+
}));
|
23
|
+
this.successStatus = options.successStatus ?? 207;
|
24
|
+
this.headers = options.headers ?? {};
|
25
|
+
}
|
26
|
+
init(options) {
|
27
|
+
options.rootInterceptors ??= [];
|
28
|
+
options.rootInterceptors.unshift(async (options2) => {
|
29
|
+
const xHeader = flattenHeader(options2.request.headers["x-orpc-batch"]);
|
30
|
+
if (xHeader === void 0) {
|
31
|
+
return options2.next();
|
32
|
+
}
|
33
|
+
let isParsing = false;
|
34
|
+
try {
|
35
|
+
isParsing = true;
|
36
|
+
const parsed = parseBatchRequest({ ...options2.request, body: await options2.request.body() });
|
37
|
+
isParsing = false;
|
38
|
+
const maxSize = await value(this.maxSize, options2);
|
39
|
+
if (parsed.length > maxSize) {
|
40
|
+
return {
|
41
|
+
matched: true,
|
42
|
+
response: {
|
43
|
+
status: 413,
|
44
|
+
headers: {},
|
45
|
+
body: "Batch request size exceeds the maximum allowed size"
|
46
|
+
}
|
47
|
+
};
|
48
|
+
}
|
49
|
+
const responses = parsed.map(
|
50
|
+
(request, index) => {
|
51
|
+
const mapped = this.mapRequestItem(request, options2);
|
52
|
+
return options2.next({ ...options2, request: { ...mapped, body: () => Promise.resolve(mapped.body) } }).then(({ response: response2, matched }) => {
|
53
|
+
if (matched) {
|
54
|
+
if (response2.body instanceof Blob || response2.body instanceof FormData || isAsyncIteratorObject(response2.body)) {
|
55
|
+
return {
|
56
|
+
index,
|
57
|
+
status: 500,
|
58
|
+
headers: {},
|
59
|
+
body: "Batch responses do not support file/blob, or event-iterator. Please call this procedure separately outside of the batch request."
|
60
|
+
};
|
61
|
+
}
|
62
|
+
return { ...response2, index };
|
63
|
+
}
|
64
|
+
return { index, status: 404, headers: {}, body: "No procedure matched" };
|
65
|
+
}).catch(() => {
|
66
|
+
return { index, status: 500, headers: {}, body: "Internal server error" };
|
67
|
+
});
|
68
|
+
}
|
69
|
+
);
|
70
|
+
await Promise.race(responses);
|
71
|
+
const status = await value(this.successStatus, responses, options2);
|
72
|
+
const headers = await value(this.headers, responses, options2);
|
73
|
+
const response = await toBatchResponse({
|
74
|
+
status,
|
75
|
+
headers,
|
76
|
+
mode: xHeader === "buffered" ? "buffered" : "streaming",
|
77
|
+
body: async function* () {
|
78
|
+
const promises = [...responses];
|
79
|
+
while (true) {
|
80
|
+
const handling = promises.filter((p) => p !== void 0);
|
81
|
+
if (handling.length === 0) {
|
82
|
+
return;
|
83
|
+
}
|
84
|
+
const result = await Promise.race(handling);
|
85
|
+
promises[result.index] = void 0;
|
86
|
+
yield result;
|
87
|
+
}
|
88
|
+
}()
|
89
|
+
});
|
90
|
+
return {
|
91
|
+
matched: true,
|
92
|
+
response
|
93
|
+
};
|
94
|
+
} catch (cause) {
|
95
|
+
if (isParsing) {
|
96
|
+
return {
|
97
|
+
matched: true,
|
98
|
+
response: { status: 400, headers: {}, body: "Invalid batch request, this could be caused by a malformed request body or a missing header" }
|
99
|
+
};
|
100
|
+
}
|
101
|
+
throw cause;
|
102
|
+
}
|
103
|
+
});
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
class CORSPlugin {
|
108
|
+
options;
|
109
|
+
order = 9e6;
|
110
|
+
constructor(options = {}) {
|
111
|
+
const defaults = {
|
112
|
+
origin: (origin) => origin,
|
113
|
+
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
|
114
|
+
};
|
115
|
+
this.options = {
|
116
|
+
...defaults,
|
117
|
+
...options
|
118
|
+
};
|
119
|
+
}
|
120
|
+
init(options) {
|
121
|
+
options.rootInterceptors ??= [];
|
122
|
+
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
123
|
+
if (interceptorOptions.request.method === "OPTIONS") {
|
124
|
+
const resHeaders = {};
|
125
|
+
if (this.options.maxAge !== void 0) {
|
126
|
+
resHeaders["access-control-max-age"] = this.options.maxAge.toString();
|
127
|
+
}
|
128
|
+
if (this.options.allowMethods?.length) {
|
129
|
+
resHeaders["access-control-allow-methods"] = flattenHeader(this.options.allowMethods);
|
130
|
+
}
|
131
|
+
const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
|
132
|
+
if (typeof allowHeaders === "string" || allowHeaders?.length) {
|
133
|
+
resHeaders["access-control-allow-headers"] = flattenHeader(allowHeaders);
|
134
|
+
}
|
135
|
+
return {
|
136
|
+
matched: true,
|
137
|
+
response: {
|
138
|
+
status: 204,
|
139
|
+
headers: resHeaders,
|
140
|
+
body: void 0
|
141
|
+
}
|
142
|
+
};
|
143
|
+
}
|
144
|
+
return interceptorOptions.next();
|
145
|
+
});
|
146
|
+
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
147
|
+
const result = await interceptorOptions.next();
|
148
|
+
if (!result.matched) {
|
149
|
+
return result;
|
150
|
+
}
|
151
|
+
const origin = flattenHeader(interceptorOptions.request.headers.origin) ?? "";
|
152
|
+
const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
|
153
|
+
const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
|
154
|
+
if (allowedOriginArr.includes("*")) {
|
155
|
+
result.response.headers["access-control-allow-origin"] = "*";
|
156
|
+
} else {
|
157
|
+
if (allowedOriginArr.includes(origin)) {
|
158
|
+
result.response.headers["access-control-allow-origin"] = origin;
|
159
|
+
}
|
160
|
+
result.response.headers.vary = interceptorOptions.request.headers.vary ?? "origin";
|
161
|
+
}
|
162
|
+
const allowedTimingOrigin = await value(this.options.timingOrigin, origin, interceptorOptions);
|
163
|
+
const allowedTimingOriginArr = Array.isArray(allowedTimingOrigin) ? allowedTimingOrigin : [allowedTimingOrigin];
|
164
|
+
if (allowedTimingOriginArr.includes("*")) {
|
165
|
+
result.response.headers["timing-allow-origin"] = "*";
|
166
|
+
} else if (allowedTimingOriginArr.includes(origin)) {
|
167
|
+
result.response.headers["timing-allow-origin"] = origin;
|
168
|
+
}
|
169
|
+
if (this.options.credentials) {
|
170
|
+
result.response.headers["access-control-allow-credentials"] = "true";
|
171
|
+
}
|
172
|
+
if (this.options.exposeHeaders?.length) {
|
173
|
+
result.response.headers["access-control-expose-headers"] = flattenHeader(this.options.exposeHeaders);
|
174
|
+
}
|
175
|
+
return result;
|
176
|
+
});
|
177
|
+
}
|
178
|
+
}
|
179
|
+
|
180
|
+
class ResponseHeadersPlugin {
|
181
|
+
init(options) {
|
182
|
+
options.rootInterceptors ??= [];
|
183
|
+
options.rootInterceptors.push(async (interceptorOptions) => {
|
184
|
+
const resHeaders = interceptorOptions.context.resHeaders ?? new Headers();
|
185
|
+
const result = await interceptorOptions.next({
|
186
|
+
...interceptorOptions,
|
187
|
+
context: {
|
188
|
+
...interceptorOptions.context,
|
189
|
+
resHeaders
|
190
|
+
}
|
191
|
+
});
|
192
|
+
if (!result.matched) {
|
193
|
+
return result;
|
194
|
+
}
|
195
|
+
const responseHeaders = result.response.headers;
|
196
|
+
for (const [key, value] of resHeaders) {
|
197
|
+
if (Array.isArray(responseHeaders[key])) {
|
198
|
+
responseHeaders[key].push(value);
|
199
|
+
} else if (responseHeaders[key] !== void 0) {
|
200
|
+
responseHeaders[key] = [responseHeaders[key], value];
|
201
|
+
} else {
|
202
|
+
responseHeaders[key] = value;
|
203
|
+
}
|
204
|
+
}
|
205
|
+
return result;
|
206
|
+
});
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
const SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL = Symbol("SIMPLE_CSRF_PROTECTION_CONTEXT");
|
211
|
+
class SimpleCsrfProtectionHandlerPlugin {
|
212
|
+
headerName;
|
213
|
+
headerValue;
|
214
|
+
exclude;
|
215
|
+
error;
|
216
|
+
constructor(options = {}) {
|
217
|
+
this.headerName = options.headerName ?? "x-csrf-token";
|
218
|
+
this.headerValue = options.headerValue ?? "orpc";
|
219
|
+
this.exclude = options.exclude ?? false;
|
220
|
+
this.error = options.error ?? new ORPCError("CSRF_TOKEN_MISMATCH", {
|
221
|
+
status: 403,
|
222
|
+
message: "Invalid CSRF token"
|
223
|
+
});
|
224
|
+
}
|
225
|
+
order = 8e6;
|
226
|
+
init(options) {
|
227
|
+
options.rootInterceptors ??= [];
|
228
|
+
options.clientInterceptors ??= [];
|
229
|
+
options.rootInterceptors.unshift(async (options2) => {
|
230
|
+
const headerName = await value(this.headerName, options2);
|
231
|
+
const headerValue = await value(this.headerValue, options2);
|
232
|
+
return options2.next({
|
233
|
+
...options2,
|
234
|
+
context: {
|
235
|
+
...options2.context,
|
236
|
+
[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]: options2.request.headers[headerName] === headerValue
|
237
|
+
}
|
238
|
+
});
|
239
|
+
});
|
240
|
+
options.clientInterceptors.unshift(async (options2) => {
|
241
|
+
if (typeof options2.context[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL] !== "boolean") {
|
242
|
+
throw new TypeError("[SimpleCsrfProtectionHandlerPlugin] CSRF protection context has been corrupted or modified by another plugin or interceptor");
|
243
|
+
}
|
244
|
+
const excluded = await value(this.exclude, options2);
|
245
|
+
if (!excluded && !options2.context[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]) {
|
246
|
+
throw this.error;
|
247
|
+
}
|
248
|
+
return options2.next();
|
249
|
+
});
|
250
|
+
}
|
251
|
+
}
|
252
|
+
|
253
|
+
export { BatchHandlerPlugin, CORSPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin };
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import { HTTPPath, ORPCError } from '@orpc/client';
|
2
|
+
import { Meta } from '@orpc/contract';
|
3
|
+
import { Interceptor } from '@orpc/shared';
|
4
|
+
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
5
|
+
import { C as Context, R as Router, f as AnyRouter, h as AnyProcedure, F as ProcedureClientInterceptorOptions } from './server.DD2C4ujN.js';
|
6
|
+
|
7
|
+
interface StandardHandlerPlugin<T extends Context> {
|
8
|
+
order?: number;
|
9
|
+
init?(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
10
|
+
}
|
11
|
+
declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
|
12
|
+
protected readonly plugins: TPlugin[];
|
13
|
+
constructor(plugins?: readonly TPlugin[]);
|
14
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
15
|
+
}
|
16
|
+
|
17
|
+
type StandardParams = Record<string, string>;
|
18
|
+
type StandardMatchResult = {
|
19
|
+
path: readonly string[];
|
20
|
+
procedure: AnyProcedure;
|
21
|
+
params?: StandardParams;
|
22
|
+
} | undefined;
|
23
|
+
interface StandardMatcher {
|
24
|
+
init(router: AnyRouter): void;
|
25
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
26
|
+
}
|
27
|
+
interface StandardCodec {
|
28
|
+
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
29
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
30
|
+
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
31
|
+
}
|
32
|
+
|
33
|
+
interface StandardHandleOptions<T extends Context> {
|
34
|
+
prefix?: HTTPPath;
|
35
|
+
context: T;
|
36
|
+
}
|
37
|
+
type StandardHandleResult = {
|
38
|
+
matched: true;
|
39
|
+
response: StandardResponse;
|
40
|
+
} | {
|
41
|
+
matched: false;
|
42
|
+
response: undefined;
|
43
|
+
};
|
44
|
+
interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
|
45
|
+
request: StandardLazyRequest;
|
46
|
+
}
|
47
|
+
interface StandardHandlerOptions<TContext extends Context> {
|
48
|
+
plugins?: StandardHandlerPlugin<TContext>[];
|
49
|
+
/**
|
50
|
+
* Interceptors at the request level, helpful when you want catch errors
|
51
|
+
*/
|
52
|
+
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, Promise<StandardHandleResult>>[];
|
53
|
+
/**
|
54
|
+
* Interceptors at the root level, helpful when you want override the request/response
|
55
|
+
*/
|
56
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, Promise<StandardHandleResult>>[];
|
57
|
+
/**
|
58
|
+
*
|
59
|
+
* Interceptors for procedure client.
|
60
|
+
*/
|
61
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, Promise<unknown>>[];
|
62
|
+
}
|
63
|
+
declare class StandardHandler<T extends Context> {
|
64
|
+
private readonly matcher;
|
65
|
+
private readonly codec;
|
66
|
+
private readonly interceptors;
|
67
|
+
private readonly clientInterceptors;
|
68
|
+
private readonly rootInterceptors;
|
69
|
+
constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
|
70
|
+
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
71
|
+
}
|
72
|
+
|
73
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as i };
|
74
|
+
export type { StandardHandlerInterceptorOptions as S, StandardHandlerPlugin as a, StandardHandlerOptions as b, StandardCodec as c, StandardParams as d, StandardMatcher as e, StandardMatchResult as f, StandardHandleOptions as g, StandardHandleResult as h };
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
2
|
+
import { C as Context, R as Router } from './server.DD2C4ujN.js';
|
3
|
+
import { b as StandardHandlerOptions, i as StandardHandler } from './server.-ACo36I0.js';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
declare class StandardRPCHandler<T extends Context> extends StandardHandler<T> {
|
8
|
+
constructor(router: Router<any, T>, options?: StandardRPCHandlerOptions<T>);
|
9
|
+
}
|
10
|
+
|
11
|
+
export { StandardRPCHandler as a };
|
12
|
+
export type { StandardRPCHandlerOptions as S };
|
@@ -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,12 @@
|
|
1
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
2
|
+
import { C as Context, R as Router } from './server.DD2C4ujN.mjs';
|
3
|
+
import { b as StandardHandlerOptions, i as StandardHandler } from './server.Dq8xr7PQ.mjs';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
declare class StandardRPCHandler<T extends Context> extends StandardHandler<T> {
|
8
|
+
constructor(router: Router<any, T>, options?: StandardRPCHandlerOptions<T>);
|
9
|
+
}
|
10
|
+
|
11
|
+
export { StandardRPCHandler as a };
|
12
|
+
export type { StandardRPCHandlerOptions as S };
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { C as Context } from './server.DD2C4ujN.mjs';
|
2
|
+
import { g as StandardHandleOptions } from './server.Dq8xr7PQ.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
|
+
|
10
|
+
export type { FriendlyStandardHandleOptions as F };
|