@orpc/server 2.0.0-beta.3 → 2.0.0-beta.31
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 +71 -101
- package/dist/adapters/aws-lambda/index.d.mts +93 -0
- package/dist/adapters/aws-lambda/index.d.ts +93 -0
- package/dist/adapters/aws-lambda/index.mjs +66 -0
- package/dist/adapters/crossws/index.d.mts +17 -8
- package/dist/adapters/crossws/index.d.ts +17 -8
- package/dist/adapters/crossws/index.mjs +10 -7
- package/dist/adapters/fastify/index.d.mts +85 -0
- package/dist/adapters/fastify/index.d.ts +85 -0
- package/dist/adapters/fastify/index.mjs +66 -0
- package/dist/adapters/fetch/index.d.mts +16 -67
- package/dist/adapters/fetch/index.d.ts +16 -67
- package/dist/adapters/fetch/index.mjs +8 -144
- package/dist/adapters/message-port/index.d.mts +19 -16
- package/dist/adapters/message-port/index.d.ts +19 -16
- package/dist/adapters/message-port/index.mjs +28 -32
- package/dist/adapters/node/index.d.mts +15 -49
- package/dist/adapters/node/index.d.ts +15 -49
- package/dist/adapters/node/index.mjs +9 -120
- package/dist/adapters/standard/index.d.mts +6 -6
- package/dist/adapters/standard/index.d.ts +6 -6
- package/dist/adapters/standard/index.mjs +3 -3
- package/dist/adapters/standard-peer/index.d.mts +2 -2
- package/dist/adapters/standard-peer/index.d.ts +2 -2
- package/dist/adapters/websocket/index.d.mts +42 -26
- package/dist/adapters/websocket/index.d.ts +42 -26
- package/dist/adapters/websocket/index.mjs +33 -25
- package/dist/extensions/callable.d.mts +2 -2
- package/dist/extensions/callable.d.ts +2 -2
- package/dist/extensions/callable.mjs +2 -2
- package/dist/helpers/index.d.mts +23 -6
- package/dist/helpers/index.d.ts +23 -6
- package/dist/helpers/index.mjs +13 -5
- package/dist/index.d.mts +311 -16
- package/dist/index.d.ts +311 -16
- package/dist/index.mjs +126 -34
- package/dist/plugins/index.d.mts +241 -32
- package/dist/plugins/index.d.ts +241 -32
- package/dist/plugins/index.mjs +479 -15
- package/dist/shared/server.BhHrioCw.d.ts +34 -0
- package/dist/shared/{server.T9F3bzZx.d.ts → server.C2n16pp0.d.ts} +34 -9
- package/dist/shared/server.Cd4Z1hpV.mjs +69 -0
- package/dist/shared/{server.CrlKQucM.mjs → server.CkButhNT.mjs} +21 -38
- package/dist/shared/{server.B_U9y00a.d.mts → server.CkNnZ5F-.d.mts} +34 -9
- package/dist/shared/{server.BL22TloH.d.mts → server.CwrYlF72.d.mts} +31 -41
- package/dist/shared/{server.BL22TloH.d.ts → server.CwrYlF72.d.ts} +31 -41
- package/dist/shared/server.D0Ipbmdv.d.mts +34 -0
- package/dist/shared/{server.BwHnWUuN.mjs → server.Dh77P3ii.mjs} +17 -10
- package/dist/shared/{server.Pa0F03f_.d.ts → server.Dm0os-OP.d.mts} +24 -12
- package/dist/shared/{server.GDpX6Df8.mjs → server.DpUGXBi2.mjs} +84 -51
- package/dist/shared/{server.EOHJ3NJr.d.ts → server.DrN1Pj1-.d.ts} +3 -3
- package/dist/shared/{server.BsNNjG5J.d.mts → server.DtfwuV6U.d.ts} +24 -12
- package/dist/shared/{server.BB_Ik9Ph.d.mts → server.JbCIPL4P.d.mts} +3 -3
- package/dist/shared/{server.CjOb6ItT.mjs → server.eCTV8Vpp.mjs} +1 -1
- package/package.json +46 -12
- package/dist/shared/server.D_QauotT.mjs +0 -30
|
@@ -1,146 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
export { R as BodyLimitPlugin } from '../../shared/server.Cd4Z1hpV.mjs';
|
|
2
|
+
import { sortPlugins, intercept, resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
3
3
|
import { toStandardLazyRequest, toFetchResponse } from '@standardserver/fetch';
|
|
4
4
|
import { r as resolveFriendlyStandardHandlerHandleOptions } from '../../shared/server.W91HSRkE.mjs';
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
5
|
+
import { R as RPCHandlerCodec, S as StandardHandler } from '../../shared/server.DpUGXBi2.mjs';
|
|
6
|
+
import '@orpc/client';
|
|
7
7
|
import '@standardserver/core';
|
|
8
|
-
import '../../shared/server.
|
|
8
|
+
import '../../shared/server.CkButhNT.mjs';
|
|
9
9
|
import '@orpc/contract';
|
|
10
|
-
import '../../shared/server.
|
|
11
|
-
|
|
12
|
-
const ORDERED_SUPPORTED_ENCODINGS = ["gzip", "deflate"];
|
|
13
|
-
class BodyCompressionHandlerPlugin {
|
|
14
|
-
name = "~body-compression";
|
|
15
|
-
encodings;
|
|
16
|
-
threshold;
|
|
17
|
-
filter;
|
|
18
|
-
constructor(options = {}) {
|
|
19
|
-
this.encodings = options.encodings ?? ORDERED_SUPPORTED_ENCODINGS;
|
|
20
|
-
this.threshold = options.threshold ?? 1024;
|
|
21
|
-
this.filter = (request, response) => {
|
|
22
|
-
const hasContentDisposition = response.headers.has("content-disposition");
|
|
23
|
-
const contentType = response.headers.get("content-type");
|
|
24
|
-
if (!hasContentDisposition && contentType?.startsWith("text/event-stream")) {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
return options.filter ? options.filter(request, response) : isCompressibleContentType(contentType);
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
initFetchHandlerOptions(options) {
|
|
31
|
-
return {
|
|
32
|
-
...options,
|
|
33
|
-
fetchInterceptors: [
|
|
34
|
-
async (interceptorOptions) => {
|
|
35
|
-
const result = await interceptorOptions.next();
|
|
36
|
-
if (!result.matched) {
|
|
37
|
-
return result;
|
|
38
|
-
}
|
|
39
|
-
const response = result.response;
|
|
40
|
-
if (response.headers.has("content-encoding") || response.headers.has("transfer-encoding") || isNoTransformCacheControl(response.headers.get("cache-control"))) {
|
|
41
|
-
return result;
|
|
42
|
-
}
|
|
43
|
-
const contentLength = response.headers.get("content-length");
|
|
44
|
-
if (contentLength && Number(contentLength) < this.threshold) {
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
const acceptEncoding = interceptorOptions.request.headers.get("accept-encoding")?.split(",").map((enc) => enc.trim().split(";")[0]);
|
|
48
|
-
const encoding = this.encodings.find((enc) => acceptEncoding?.includes(enc));
|
|
49
|
-
if (!response.body || encoding === void 0) {
|
|
50
|
-
return result;
|
|
51
|
-
}
|
|
52
|
-
if (!this.filter(interceptorOptions.request, response)) {
|
|
53
|
-
return result;
|
|
54
|
-
}
|
|
55
|
-
const compressedBody = response.body.pipeThrough(new CompressionStream(encoding));
|
|
56
|
-
const compressedHeaders = new Headers(response.headers);
|
|
57
|
-
compressedHeaders.delete("content-length");
|
|
58
|
-
compressedHeaders.set("content-encoding", encoding);
|
|
59
|
-
return {
|
|
60
|
-
...result,
|
|
61
|
-
response: new Response(compressedBody, {
|
|
62
|
-
status: response.status,
|
|
63
|
-
statusText: response.statusText,
|
|
64
|
-
headers: compressedHeaders
|
|
65
|
-
})
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
...toArray(options.fetchInterceptors)
|
|
69
|
-
]
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/(?!event-stream(?:[;\s]|$))[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i;
|
|
74
|
-
function isCompressibleContentType(contentType) {
|
|
75
|
-
if (contentType === null) {
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
return COMPRESSIBLE_CONTENT_TYPE_REGEX.test(contentType);
|
|
79
|
-
}
|
|
80
|
-
const CACHE_CONTROL_NO_TRANSFORM_REGEX = /(?:^|,)\s*no-transform\s*(?:,|$)/i;
|
|
81
|
-
function isNoTransformCacheControl(cacheControl) {
|
|
82
|
-
if (cacheControl === null) {
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
return CACHE_CONTROL_NO_TRANSFORM_REGEX.test(cacheControl);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
class BodyLimitHandlerPlugin {
|
|
89
|
-
name = "~body-limit";
|
|
90
|
-
maxBodySize;
|
|
91
|
-
constructor(options) {
|
|
92
|
-
this.maxBodySize = options.maxBodySize;
|
|
93
|
-
}
|
|
94
|
-
initFetchHandlerOptions(options) {
|
|
95
|
-
return {
|
|
96
|
-
...options,
|
|
97
|
-
fetchInterceptors: [
|
|
98
|
-
async (interceptorOptions) => {
|
|
99
|
-
if (!interceptorOptions.request.body) {
|
|
100
|
-
return interceptorOptions.next();
|
|
101
|
-
}
|
|
102
|
-
let currentBodySize = 0;
|
|
103
|
-
const rawReader = interceptorOptions.request.body.getReader();
|
|
104
|
-
const body = new ReadableStream({
|
|
105
|
-
start: async (controller) => {
|
|
106
|
-
const reject = async (error) => {
|
|
107
|
-
controller.error(error);
|
|
108
|
-
await rawReader.cancel(error);
|
|
109
|
-
};
|
|
110
|
-
const contentLength = interceptorOptions.request.headers.get("content-length");
|
|
111
|
-
if (contentLength && Number(contentLength) > this.maxBodySize) {
|
|
112
|
-
await reject(new ORPCError("PAYLOAD_TOO_LARGE"));
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
while (true) {
|
|
116
|
-
const { done, value } = await rawReader.read();
|
|
117
|
-
if (done) {
|
|
118
|
-
controller.close();
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
currentBodySize += value.length;
|
|
122
|
-
if (currentBodySize > this.maxBodySize) {
|
|
123
|
-
await reject(new ORPCError("PAYLOAD_TOO_LARGE"));
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
controller.enqueue(value);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
const requestInit = { body, duplex: "half" };
|
|
131
|
-
return interceptorOptions.next({
|
|
132
|
-
...interceptorOptions,
|
|
133
|
-
request: new Request(interceptorOptions.request, requestInit)
|
|
134
|
-
});
|
|
135
|
-
},
|
|
136
|
-
...toArray(options.fetchInterceptors)
|
|
137
|
-
]
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
}
|
|
10
|
+
import '../../shared/server.Dh77P3ii.mjs';
|
|
141
11
|
|
|
142
12
|
class CompositeFetchHandlerPlugin {
|
|
143
|
-
name = "~composite/fetch
|
|
13
|
+
name = "~composite/fetch";
|
|
144
14
|
plugins;
|
|
145
15
|
constructor(plugins = []) {
|
|
146
16
|
this.plugins = sortPlugins(plugins);
|
|
@@ -189,16 +59,10 @@ class FetchHandler {
|
|
|
189
59
|
|
|
190
60
|
class RPCHandler extends FetchHandler {
|
|
191
61
|
constructor(router, options = {}) {
|
|
192
|
-
if (options.csrfGuardHandlerPlugin?.enabled !== false) {
|
|
193
|
-
options = {
|
|
194
|
-
...options,
|
|
195
|
-
plugins: [...toArray(options.plugins), new CSRFGuardHandlerPlugin()]
|
|
196
|
-
};
|
|
197
|
-
}
|
|
198
62
|
const codec = new RPCHandlerCodec(router, options);
|
|
199
63
|
const handler = new StandardHandler(codec, options);
|
|
200
64
|
super(handler, options);
|
|
201
65
|
}
|
|
202
66
|
}
|
|
203
67
|
|
|
204
|
-
export {
|
|
68
|
+
export { CompositeFetchHandlerPlugin, FetchHandler, RPCHandler };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { SupportedMessagePort } from '@orpc/client/message-port';
|
|
2
2
|
import { Value, Promisable, MaybeOptionalOptions } from '@orpc/shared';
|
|
3
3
|
import { ServerPeer, EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standardserver/peer';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { Context } from '../../index.mjs';
|
|
5
|
+
import { StandardHandler, StandardHandlerOptions } from '../standard/index.mjs';
|
|
6
6
|
import { StandardPeerRequestHandlerOptions } from '../standard-peer/index.mjs';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { Router } from '../../index.mjs';
|
|
8
|
+
import { RPCHandlerCodecOptions } from '../standard/index.mjs';
|
|
9
9
|
import '@orpc/client';
|
|
10
10
|
import '@orpc/contract';
|
|
11
11
|
import '@standardserver/core';
|
|
@@ -19,9 +19,8 @@ interface MessagePortHandlerOptions<_T extends Context> {
|
|
|
19
19
|
* such as transferring object ownership or supporting non-serializable objects like `OffscreenCanvas` or improving performance.
|
|
20
20
|
*
|
|
21
21
|
* @remarks
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @warning Ensure your message port implementation supports `transferable` objects before enabling this.
|
|
22
|
+
* **Note**: Returning `null` or `undefined` disables this feature.
|
|
23
|
+
* **Warning**: Ensure your message port implementation supports `transferable` objects before enabling this.
|
|
25
24
|
*/
|
|
26
25
|
experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedResponseMessage, port: SupportedMessagePort]>;
|
|
27
26
|
/**
|
|
@@ -41,31 +40,35 @@ declare class MessagePortHandler<T extends Context> {
|
|
|
41
40
|
private readonly decodePeerMessageOptions;
|
|
42
41
|
constructor(handler: StandardHandler<T>, options?: NoInfer<MessagePortHandlerOptions<T>>);
|
|
43
42
|
/**
|
|
44
|
-
* Attaches
|
|
43
|
+
* Attaches message and close listeners to a message port.
|
|
44
|
+
*
|
|
45
|
+
* Prefer this over calling `.message()` and `.close()` manually.
|
|
45
46
|
*/
|
|
46
47
|
upgrade(port: SupportedMessagePort, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): void;
|
|
47
48
|
/**
|
|
48
49
|
* Handles a single message received from a message port.
|
|
49
50
|
*
|
|
50
|
-
* @
|
|
51
|
-
*
|
|
52
|
-
* @param port The message port instance, require consistent instance across messages for proper peer management
|
|
51
|
+
* @param port The message port instance. Use the same instance for all messages.
|
|
53
52
|
*/
|
|
54
|
-
message(port: SupportedMessagePort, data:
|
|
53
|
+
message(port: SupportedMessagePort, data: unknown, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): Promise<{
|
|
55
54
|
matched: boolean;
|
|
56
55
|
}>;
|
|
57
56
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* @warning AVOID calling this method directly if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
|
|
57
|
+
* Cleans up peer state for a closed message port.
|
|
61
58
|
*
|
|
62
|
-
* @param port The message port instance to
|
|
59
|
+
* @param port The same message port instance passed to `.message()`.
|
|
63
60
|
*/
|
|
64
61
|
close(port: SupportedMessagePort): Promise<void>;
|
|
65
62
|
}
|
|
66
63
|
|
|
67
64
|
interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, RPCHandlerCodecOptions<T>, MessagePortHandlerOptions<T> {
|
|
68
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Serves an oRPC router over the RPC protocol on a message port,
|
|
68
|
+
* such as browser extensions, Electron, or worker threads.
|
|
69
|
+
*
|
|
70
|
+
* @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
|
|
71
|
+
*/
|
|
69
72
|
declare class RPCHandler<T extends Context> extends MessagePortHandler<T> {
|
|
70
73
|
constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
71
74
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { SupportedMessagePort } from '@orpc/client/message-port';
|
|
2
2
|
import { Value, Promisable, MaybeOptionalOptions } from '@orpc/shared';
|
|
3
3
|
import { ServerPeer, EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standardserver/peer';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
4
|
+
import { Context } from '../../index.js';
|
|
5
|
+
import { StandardHandler, StandardHandlerOptions } from '../standard/index.js';
|
|
6
6
|
import { StandardPeerRequestHandlerOptions } from '../standard-peer/index.js';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { Router } from '../../index.js';
|
|
8
|
+
import { RPCHandlerCodecOptions } from '../standard/index.js';
|
|
9
9
|
import '@orpc/client';
|
|
10
10
|
import '@orpc/contract';
|
|
11
11
|
import '@standardserver/core';
|
|
@@ -19,9 +19,8 @@ interface MessagePortHandlerOptions<_T extends Context> {
|
|
|
19
19
|
* such as transferring object ownership or supporting non-serializable objects like `OffscreenCanvas` or improving performance.
|
|
20
20
|
*
|
|
21
21
|
* @remarks
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* @warning Ensure your message port implementation supports `transferable` objects before enabling this.
|
|
22
|
+
* **Note**: Returning `null` or `undefined` disables this feature.
|
|
23
|
+
* **Warning**: Ensure your message port implementation supports `transferable` objects before enabling this.
|
|
25
24
|
*/
|
|
26
25
|
experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedResponseMessage, port: SupportedMessagePort]>;
|
|
27
26
|
/**
|
|
@@ -41,31 +40,35 @@ declare class MessagePortHandler<T extends Context> {
|
|
|
41
40
|
private readonly decodePeerMessageOptions;
|
|
42
41
|
constructor(handler: StandardHandler<T>, options?: NoInfer<MessagePortHandlerOptions<T>>);
|
|
43
42
|
/**
|
|
44
|
-
* Attaches
|
|
43
|
+
* Attaches message and close listeners to a message port.
|
|
44
|
+
*
|
|
45
|
+
* Prefer this over calling `.message()` and `.close()` manually.
|
|
45
46
|
*/
|
|
46
47
|
upgrade(port: SupportedMessagePort, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): void;
|
|
47
48
|
/**
|
|
48
49
|
* Handles a single message received from a message port.
|
|
49
50
|
*
|
|
50
|
-
* @
|
|
51
|
-
*
|
|
52
|
-
* @param port The message port instance, require consistent instance across messages for proper peer management
|
|
51
|
+
* @param port The message port instance. Use the same instance for all messages.
|
|
53
52
|
*/
|
|
54
|
-
message(port: SupportedMessagePort, data:
|
|
53
|
+
message(port: SupportedMessagePort, data: unknown, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): Promise<{
|
|
55
54
|
matched: boolean;
|
|
56
55
|
}>;
|
|
57
56
|
/**
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
* @warning AVOID calling this method directly if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
|
|
57
|
+
* Cleans up peer state for a closed message port.
|
|
61
58
|
*
|
|
62
|
-
* @param port The message port instance to
|
|
59
|
+
* @param port The same message port instance passed to `.message()`.
|
|
63
60
|
*/
|
|
64
61
|
close(port: SupportedMessagePort): Promise<void>;
|
|
65
62
|
}
|
|
66
63
|
|
|
67
64
|
interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, RPCHandlerCodecOptions<T>, MessagePortHandlerOptions<T> {
|
|
68
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Serves an oRPC router over the RPC protocol on a message port,
|
|
68
|
+
* such as browser extensions, Electron, or worker threads.
|
|
69
|
+
*
|
|
70
|
+
* @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
|
|
71
|
+
*/
|
|
69
72
|
declare class RPCHandler<T extends Context> extends MessagePortHandler<T> {
|
|
70
73
|
constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
71
74
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { onMessagePortMessage, onMessagePortClose, postMessagePortMessage } from '@orpc/client/message-port';
|
|
2
|
-
import { value,
|
|
3
|
-
import { ServerPeer, encodePeerMessage, decodePeerMessage, isClientPeerSendMessage } from '@standardserver/peer';
|
|
2
|
+
import { value, resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
3
|
+
import { ServerPeer, encodePeerMessage, decodePeerMessage, isClientPeerSendMessage, isPeerMessage } from '@standardserver/peer';
|
|
4
4
|
import { c as createStandardPeerRequestHandler } from '../../shared/server.CX4vUnDk.mjs';
|
|
5
|
-
import { R as RPCHandlerCodec, S as StandardHandler } from '../../shared/server.
|
|
5
|
+
import { R as RPCHandlerCodec, S as StandardHandler } from '../../shared/server.DpUGXBi2.mjs';
|
|
6
6
|
import '@orpc/client';
|
|
7
7
|
import '@standardserver/core';
|
|
8
|
-
import '../../shared/server.
|
|
8
|
+
import '../../shared/server.CkButhNT.mjs';
|
|
9
9
|
import '@orpc/contract';
|
|
10
|
-
import '../../shared/server.
|
|
10
|
+
import '../../shared/server.Dh77P3ii.mjs';
|
|
11
11
|
|
|
12
12
|
class MessagePortHandler {
|
|
13
13
|
constructor(handler, options = {}) {
|
|
@@ -21,7 +21,9 @@ class MessagePortHandler {
|
|
|
21
21
|
encodePeerMessageOptions;
|
|
22
22
|
decodePeerMessageOptions;
|
|
23
23
|
/**
|
|
24
|
-
* Attaches
|
|
24
|
+
* Attaches message and close listeners to a message port.
|
|
25
|
+
*
|
|
26
|
+
* Prefer this over calling `.message()` and `.close()` manually.
|
|
25
27
|
*/
|
|
26
28
|
upgrade(port, ...rest) {
|
|
27
29
|
onMessagePortMessage(port, (message) => this.message(port, message, ...rest));
|
|
@@ -30,51 +32,45 @@ class MessagePortHandler {
|
|
|
30
32
|
/**
|
|
31
33
|
* Handles a single message received from a message port.
|
|
32
34
|
*
|
|
33
|
-
* @
|
|
34
|
-
*
|
|
35
|
-
* @param port The message port instance, require consistent instance across messages for proper peer management
|
|
35
|
+
* @param port The message port instance. Use the same instance for all messages.
|
|
36
36
|
*/
|
|
37
37
|
async message(port, data, ...rest) {
|
|
38
38
|
let peer = this.peers.get(port);
|
|
39
39
|
if (!peer) {
|
|
40
|
-
this.peers.set(port, peer = new ServerPeer(async (
|
|
41
|
-
const transfer = await value(this.transfer,
|
|
40
|
+
this.peers.set(port, peer = new ServerPeer(async (message) => {
|
|
41
|
+
const transfer = await value(this.transfer, message, port);
|
|
42
42
|
if (transfer) {
|
|
43
|
-
postMessagePortMessage(port,
|
|
43
|
+
postMessagePortMessage(port, message, transfer);
|
|
44
44
|
} else {
|
|
45
|
-
postMessagePortMessage(port, await encodePeerMessage(
|
|
45
|
+
postMessagePortMessage(port, await encodePeerMessage(message, this.encodePeerMessageOptions));
|
|
46
46
|
}
|
|
47
47
|
}));
|
|
48
48
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
49
|
+
let peerMessage;
|
|
50
|
+
if (typeof data === "string" || data instanceof Uint8Array) {
|
|
51
|
+
const result = decodePeerMessage(data, this.decodePeerMessageOptions);
|
|
52
|
+
if (result.matched && isClientPeerSendMessage(result.message)) {
|
|
53
|
+
peerMessage = result.message;
|
|
54
|
+
}
|
|
55
|
+
} else if (isPeerMessage(data) && isClientPeerSendMessage(data)) {
|
|
56
|
+
peerMessage = data;
|
|
55
57
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (result.matched && isClientPeerSendMessage(result.message)) {
|
|
59
|
-
await peer.message(
|
|
60
|
-
result.message,
|
|
61
|
-
createStandardPeerRequestHandler(this.handler, resolveMaybeOptionalOptions(rest))
|
|
62
|
-
);
|
|
58
|
+
if (peerMessage === void 0) {
|
|
59
|
+
return { matched: false };
|
|
63
60
|
}
|
|
64
|
-
|
|
61
|
+
await peer.message(peerMessage, createStandardPeerRequestHandler(this.handler, resolveMaybeOptionalOptions(rest)));
|
|
62
|
+
return { matched: true };
|
|
65
63
|
}
|
|
66
64
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* @warning AVOID calling this method directly if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
|
|
65
|
+
* Cleans up peer state for a closed message port.
|
|
70
66
|
*
|
|
71
|
-
* @param port The message port instance to
|
|
67
|
+
* @param port The same message port instance passed to `.message()`.
|
|
72
68
|
*/
|
|
73
69
|
async close(port) {
|
|
74
70
|
const peer = this.peers.get(port);
|
|
75
71
|
if (peer) {
|
|
76
|
-
await peer.close();
|
|
77
72
|
this.peers.delete(port);
|
|
73
|
+
await peer.close();
|
|
78
74
|
}
|
|
79
75
|
}
|
|
80
76
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
import { C as Context } from '../../shared/server.BL22TloH.mjs';
|
|
1
|
+
export { R as BodyLimitPlugin } from '../../shared/server.D0Ipbmdv.mjs';
|
|
3
2
|
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
|
4
|
-
import {
|
|
3
|
+
import { SendStandardResponseOptions, NodeHttpRequest, NodeHttpResponse } from '@standardserver/node';
|
|
4
|
+
import { Context } from '../../index.mjs';
|
|
5
|
+
import { StandardHandlerPlugin, StandardHandlerHandleOptions, StandardHandler, StandardHandlerOptions } from '../standard/index.mjs';
|
|
5
6
|
import { FriendlyStandardHandlerHandleOptions } from '../standard/index.mjs';
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
import { R as RPCHandlerCodecOptions } from '../../shared/server.B_U9y00a.mjs';
|
|
7
|
+
import { Router } from '../../index.mjs';
|
|
8
|
+
import { RPCHandlerCodecOptions } from '../standard/index.mjs';
|
|
9
9
|
import '@orpc/client';
|
|
10
10
|
import '@orpc/contract';
|
|
11
11
|
import '@standardserver/core';
|
|
@@ -34,7 +34,7 @@ interface NodeHttpHandlerPlugin<T extends Context> extends StandardHandlerPlugin
|
|
|
34
34
|
initNodeHttpHandlerOptions?(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
35
35
|
}
|
|
36
36
|
declare class CompositeNodeHttpHandlerPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
37
|
-
readonly name = "~composite/node-http
|
|
37
|
+
readonly name = "~composite/node-http";
|
|
38
38
|
protected readonly plugins: NodeHttpHandlerPlugin<T>[];
|
|
39
39
|
constructor(plugins?: NodeHttpHandlerPlugin<T>[]);
|
|
40
40
|
initNodeHttpHandlerOptions(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
@@ -71,51 +71,17 @@ declare class NodeHttpHandler<T extends Context> {
|
|
|
71
71
|
handle(request: NodeHttpRequest, response: NodeHttpResponse, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): Promise<NodeHttpHandlerHandleResult>;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
interface BodyCompressionHandlerPluginOptions extends compression.CompressionOptions {
|
|
75
|
-
/**
|
|
76
|
-
* Override the default content-type filter used to determine which responses should be compressed.
|
|
77
|
-
*
|
|
78
|
-
* @warning Event stream responses are never compressed, regardless of this filter's return value.
|
|
79
|
-
* @default only responses with compressible content types are compressed.
|
|
80
|
-
*/
|
|
81
|
-
filter?: (request: NodeHttpRequest, response: NodeHttpResponse) => boolean;
|
|
82
|
-
}
|
|
83
|
-
declare class BodyCompressionHandlerPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
84
|
-
name: string;
|
|
85
|
-
private readonly compressionHandler;
|
|
86
|
-
constructor(options?: BodyCompressionHandlerPluginOptions);
|
|
87
|
-
initNodeHttpHandlerOptions(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
interface BodyLimitHandlerPluginOptions {
|
|
91
|
-
/**
|
|
92
|
-
* The maximum size of the body in bytes.
|
|
93
|
-
*/
|
|
94
|
-
maxBodySize: number;
|
|
95
|
-
}
|
|
96
|
-
declare class BodyLimitHandlerPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
97
|
-
name: string;
|
|
98
|
-
private readonly maxBodySize;
|
|
99
|
-
constructor(options: BodyLimitHandlerPluginOptions);
|
|
100
|
-
initNodeHttpHandlerOptions(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
74
|
interface RPCHandlerOptions<T extends Context> extends NodeHttpHandlerOptions<T>, Omit<StandardHandlerOptions<T>, 'plugins'>, RPCHandlerCodecOptions<T> {
|
|
104
|
-
/**
|
|
105
|
-
* Configuration for {@link CSRFGuardHandlerPlugin}, which is enabled by default for `RPCHandler` over HTTP.
|
|
106
|
-
*/
|
|
107
|
-
csrfGuardPlugin?: {
|
|
108
|
-
/**
|
|
109
|
-
* If `false`, this plugin is disabled.
|
|
110
|
-
*
|
|
111
|
-
* @default true
|
|
112
|
-
*/
|
|
113
|
-
enabled?: boolean;
|
|
114
|
-
};
|
|
115
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Serves an oRPC router over the RPC protocol using Node.js built-in
|
|
78
|
+
* HTTP request/response objects.
|
|
79
|
+
*
|
|
80
|
+
* @see {@link https://orpc.dev/docs/adapters/node-http | Node HTTP Adapter}
|
|
81
|
+
*/
|
|
116
82
|
declare class RPCHandler<T extends Context> extends NodeHttpHandler<T> {
|
|
117
83
|
constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
118
84
|
}
|
|
119
85
|
|
|
120
|
-
export {
|
|
121
|
-
export type {
|
|
86
|
+
export { CompositeNodeHttpHandlerPlugin, NodeHttpHandler, RPCHandler };
|
|
87
|
+
export type { NodeHttpHandlerHandleResult, NodeHttpHandlerNodeHttpInterceptor, NodeHttpHandlerNodeHttpInterceptorOptions, NodeHttpHandlerOptions, NodeHttpHandlerPlugin, RPCHandlerOptions };
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
import { C as Context } from '../../shared/server.BL22TloH.js';
|
|
1
|
+
export { R as BodyLimitPlugin } from '../../shared/server.BhHrioCw.js';
|
|
3
2
|
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
|
4
|
-
import {
|
|
3
|
+
import { SendStandardResponseOptions, NodeHttpRequest, NodeHttpResponse } from '@standardserver/node';
|
|
4
|
+
import { Context } from '../../index.js';
|
|
5
|
+
import { StandardHandlerPlugin, StandardHandlerHandleOptions, StandardHandler, StandardHandlerOptions } from '../standard/index.js';
|
|
5
6
|
import { FriendlyStandardHandlerHandleOptions } from '../standard/index.js';
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
import { R as RPCHandlerCodecOptions } from '../../shared/server.T9F3bzZx.js';
|
|
7
|
+
import { Router } from '../../index.js';
|
|
8
|
+
import { RPCHandlerCodecOptions } from '../standard/index.js';
|
|
9
9
|
import '@orpc/client';
|
|
10
10
|
import '@orpc/contract';
|
|
11
11
|
import '@standardserver/core';
|
|
@@ -34,7 +34,7 @@ interface NodeHttpHandlerPlugin<T extends Context> extends StandardHandlerPlugin
|
|
|
34
34
|
initNodeHttpHandlerOptions?(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
35
35
|
}
|
|
36
36
|
declare class CompositeNodeHttpHandlerPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
37
|
-
readonly name = "~composite/node-http
|
|
37
|
+
readonly name = "~composite/node-http";
|
|
38
38
|
protected readonly plugins: NodeHttpHandlerPlugin<T>[];
|
|
39
39
|
constructor(plugins?: NodeHttpHandlerPlugin<T>[]);
|
|
40
40
|
initNodeHttpHandlerOptions(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
@@ -71,51 +71,17 @@ declare class NodeHttpHandler<T extends Context> {
|
|
|
71
71
|
handle(request: NodeHttpRequest, response: NodeHttpResponse, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): Promise<NodeHttpHandlerHandleResult>;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
interface BodyCompressionHandlerPluginOptions extends compression.CompressionOptions {
|
|
75
|
-
/**
|
|
76
|
-
* Override the default content-type filter used to determine which responses should be compressed.
|
|
77
|
-
*
|
|
78
|
-
* @warning Event stream responses are never compressed, regardless of this filter's return value.
|
|
79
|
-
* @default only responses with compressible content types are compressed.
|
|
80
|
-
*/
|
|
81
|
-
filter?: (request: NodeHttpRequest, response: NodeHttpResponse) => boolean;
|
|
82
|
-
}
|
|
83
|
-
declare class BodyCompressionHandlerPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
84
|
-
name: string;
|
|
85
|
-
private readonly compressionHandler;
|
|
86
|
-
constructor(options?: BodyCompressionHandlerPluginOptions);
|
|
87
|
-
initNodeHttpHandlerOptions(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
interface BodyLimitHandlerPluginOptions {
|
|
91
|
-
/**
|
|
92
|
-
* The maximum size of the body in bytes.
|
|
93
|
-
*/
|
|
94
|
-
maxBodySize: number;
|
|
95
|
-
}
|
|
96
|
-
declare class BodyLimitHandlerPlugin<T extends Context> implements NodeHttpHandlerPlugin<T> {
|
|
97
|
-
name: string;
|
|
98
|
-
private readonly maxBodySize;
|
|
99
|
-
constructor(options: BodyLimitHandlerPluginOptions);
|
|
100
|
-
initNodeHttpHandlerOptions(options: NodeHttpHandlerOptions<T>): NodeHttpHandlerOptions<T>;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
74
|
interface RPCHandlerOptions<T extends Context> extends NodeHttpHandlerOptions<T>, Omit<StandardHandlerOptions<T>, 'plugins'>, RPCHandlerCodecOptions<T> {
|
|
104
|
-
/**
|
|
105
|
-
* Configuration for {@link CSRFGuardHandlerPlugin}, which is enabled by default for `RPCHandler` over HTTP.
|
|
106
|
-
*/
|
|
107
|
-
csrfGuardPlugin?: {
|
|
108
|
-
/**
|
|
109
|
-
* If `false`, this plugin is disabled.
|
|
110
|
-
*
|
|
111
|
-
* @default true
|
|
112
|
-
*/
|
|
113
|
-
enabled?: boolean;
|
|
114
|
-
};
|
|
115
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Serves an oRPC router over the RPC protocol using Node.js built-in
|
|
78
|
+
* HTTP request/response objects.
|
|
79
|
+
*
|
|
80
|
+
* @see {@link https://orpc.dev/docs/adapters/node-http | Node HTTP Adapter}
|
|
81
|
+
*/
|
|
116
82
|
declare class RPCHandler<T extends Context> extends NodeHttpHandler<T> {
|
|
117
83
|
constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
118
84
|
}
|
|
119
85
|
|
|
120
|
-
export {
|
|
121
|
-
export type {
|
|
86
|
+
export { CompositeNodeHttpHandlerPlugin, NodeHttpHandler, RPCHandler };
|
|
87
|
+
export type { NodeHttpHandlerHandleResult, NodeHttpHandlerNodeHttpInterceptor, NodeHttpHandlerNodeHttpInterceptorOptions, NodeHttpHandlerOptions, NodeHttpHandlerPlugin, RPCHandlerOptions };
|