@orpc/server 2.0.0-beta.49 → 2.0.0-beta.50
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/dist/adapters/aws-lambda/handler.d.ts +17 -15
- package/dist/adapters/aws-lambda/handler.js +23 -23
- package/dist/adapters/aws-lambda/index.d.ts +4 -16
- package/dist/adapters/aws-lambda/index.js +2 -2
- package/dist/adapters/aws-lambda/rpc-handler.d.ts +9 -9
- package/dist/adapters/aws-lambda/rpc-handler.js +20 -17
- package/dist/adapters/crossws/handler.d.ts +44 -42
- package/dist/adapters/crossws/handler.js +53 -60
- package/dist/adapters/crossws/index.d.ts +3 -2
- package/dist/adapters/crossws/index.js +2 -2
- package/dist/adapters/crossws/rpc-handler.d.ts +10 -10
- package/dist/adapters/crossws/rpc-handler.js +17 -14
- package/dist/adapters/fastify/handler.d.ts +17 -15
- package/dist/adapters/fastify/handler.js +20 -20
- package/dist/adapters/fastify/index.d.ts +3 -2
- package/dist/adapters/fastify/index.js +2 -2
- package/dist/adapters/fastify/rpc-handler.d.ts +9 -9
- package/dist/adapters/fastify/rpc-handler.js +16 -13
- package/dist/adapters/fetch/handler.d.ts +19 -17
- package/dist/adapters/fetch/handler.js +22 -22
- package/dist/adapters/fetch/index.d.ts +4 -7
- package/dist/adapters/fetch/index.js +3 -7
- package/dist/adapters/fetch/rpc-handler.d.ts +9 -9
- package/dist/adapters/fetch/rpc-handler.js +18 -15
- package/dist/adapters/message-port/handler.d.ts +58 -60
- package/dist/adapters/message-port/handler.js +72 -86
- package/dist/adapters/message-port/index.d.ts +3 -2
- package/dist/adapters/message-port/index.js +2 -2
- package/dist/adapters/message-port/rpc-handler.d.ts +9 -9
- package/dist/adapters/message-port/rpc-handler.js +17 -14
- package/dist/adapters/node/handler.d.ts +17 -15
- package/dist/adapters/node/handler.js +20 -20
- package/dist/adapters/node/index.d.ts +4 -7
- package/dist/adapters/node/index.js +3 -7
- package/dist/adapters/node/rpc-handler.d.ts +9 -9
- package/dist/adapters/node/rpc-handler.js +17 -14
- package/dist/adapters/standard/codec.d.ts +13 -13
- package/dist/adapters/standard/handler.d.ts +49 -49
- package/dist/adapters/standard/handler.js +205 -205
- package/dist/adapters/standard/index.d.ts +7 -6
- package/dist/adapters/standard/index.js +4 -5
- package/dist/adapters/standard/plugin.d.ts +25 -31
- package/dist/adapters/standard/plugin.js +14 -16
- package/dist/adapters/standard/rpc-handler-codec.d.ts +39 -43
- package/dist/adapters/standard/rpc-handler-codec.js +50 -51
- package/dist/adapters/standard/rpc-matcher.d.ts +40 -40
- package/dist/adapters/standard/rpc-matcher.js +103 -149
- package/dist/adapters/standard/utils.d.ts +8 -8
- package/dist/adapters/standard/utils.js +7 -5
- package/dist/adapters/standard-peer/index.d.ts +2 -1
- package/dist/adapters/standard-peer/index.js +1 -1
- package/dist/adapters/standard-peer/utils.d.ts +6 -4
- package/dist/adapters/standard-peer/utils.js +12 -6
- package/dist/adapters/websocket/handler.d.ts +52 -50
- package/dist/adapters/websocket/handler.js +75 -85
- package/dist/adapters/websocket/index.d.ts +3 -2
- package/dist/adapters/websocket/index.js +2 -2
- package/dist/adapters/websocket/rpc-handler.d.ts +9 -9
- package/dist/adapters/websocket/rpc-handler.js +17 -14
- package/dist/builder-variants.d.ts +195 -195
- package/dist/builder.d.ts +103 -104
- package/dist/builder.js +198 -224
- package/dist/constants.d.ts +1 -1
- package/dist/constants.js +4 -2
- package/dist/context.d.ts +3 -3
- package/dist/extensions/callable.d.ts +11 -11
- package/dist/extensions/callable.js +6 -5
- package/dist/helpers/base64url.d.ts +1 -1
- package/dist/helpers/base64url.js +44 -49
- package/dist/helpers/cookie.d.ts +10 -11
- package/dist/helpers/cookie.js +56 -63
- package/dist/helpers/encryption.d.ts +1 -1
- package/dist/helpers/encryption.js +80 -70
- package/dist/helpers/index.d.ts +5 -4
- package/dist/helpers/index.js +4 -4
- package/dist/helpers/signing.d.ts +1 -1
- package/dist/helpers/signing.js +72 -82
- package/dist/implementer-procedure.d.ts +12 -13
- package/dist/implementer-procedure.js +37 -44
- package/dist/implementer-router.d.ts +30 -36
- package/dist/implementer-router.js +92 -111
- package/dist/implementer.d.ts +8 -8
- package/dist/implementer.js +28 -35
- package/dist/index.d.ts +22 -61
- package/dist/index.js +16 -48
- package/dist/lazy.d.ts +14 -14
- package/dist/lazy.js +19 -30
- package/dist/middleware-decorated.d.ts +8 -9
- package/dist/middleware-decorated.js +66 -70
- package/dist/middleware.d.ts +34 -34
- package/dist/plugins/batch.d.ts +75 -75
- package/dist/plugins/batch.js +345 -379
- package/dist/plugins/cors.d.ts +60 -64
- package/dist/plugins/cors.js +118 -111
- package/dist/plugins/get-method-csrf-protection.d.ts +8 -6
- package/dist/plugins/get-method-csrf-protection.js +47 -63
- package/dist/plugins/head-method.d.ts +11 -9
- package/dist/plugins/head-method.js +97 -94
- package/dist/plugins/index.d.ts +15 -39
- package/dist/plugins/index.js +14 -29
- package/dist/plugins/method-override.d.ts +30 -28
- package/dist/plugins/method-override.js +53 -49
- package/dist/plugins/request-compression.d.ts +11 -9
- package/dist/plugins/request-compression.js +52 -79
- package/dist/plugins/request-headers.d.ts +10 -8
- package/dist/plugins/request-headers.js +27 -29
- package/dist/plugins/request-limit.d.ts +21 -19
- package/dist/plugins/request-limit.js +60 -67
- package/dist/plugins/response-compression.d.ts +42 -39
- package/dist/plugins/response-compression.js +150 -202
- package/dist/plugins/response-headers.d.ts +15 -13
- package/dist/plugins/response-headers.js +43 -46
- package/dist/plugins/rethrow.d.ts +25 -23
- package/dist/plugins/rethrow.js +46 -58
- package/dist/plugins/signal-sync.d.ts +7 -5
- package/dist/plugins/signal-sync.js +71 -86
- package/dist/plugins/timeout.d.ts +29 -27
- package/dist/plugins/timeout.js +61 -58
- package/dist/procedure-client.d.ts +14 -15
- package/dist/procedure-client.js +149 -166
- package/dist/procedure-decorated.d.ts +9 -9
- package/dist/procedure-decorated.js +36 -44
- package/dist/procedure-utils.d.ts +10 -30
- package/dist/procedure-utils.js +39 -40
- package/dist/procedure.d.ts +60 -60
- package/dist/procedure.js +18 -24
- package/dist/router-client.d.ts +9 -11
- package/dist/router-client.js +33 -41
- package/dist/router-hidden.js +10 -12
- package/dist/router-utils.d.ts +13 -30
- package/dist/router-utils.js +146 -191
- package/dist/router.d.ts +12 -26
- package/package.json +10 -10
- package/dist/adapters/standard/codec.js +0 -1
- package/dist/builder-variants.js +0 -1
- package/dist/context.js +0 -1
- package/dist/middleware.js +0 -1
- package/dist/router-hidden.d.ts +0 -5
- package/dist/router.js +0 -1
|
@@ -1,59 +1,61 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { Context } from "../../context.js";
|
|
2
|
+
import { StandardHandler } from "../standard/handler.js";
|
|
3
|
+
import { FriendlyStandardHandlerHandleOptions } from "../standard/utils.js";
|
|
4
|
+
import "../standard/index.js";
|
|
5
|
+
import { Arrayable, MaybeOptionalOptions } from "@orpc/shared";
|
|
6
|
+
import { DecodePeerMessageOptions, EncodePeerMessageOptions } from "@standard-server/peer";
|
|
5
7
|
/**
|
|
6
8
|
* Supports standard WebSocket instances, Bun ServerWebSocket, "ws" ServerWebSocket,
|
|
7
9
|
* Cloudflare WebSocket Hibernation, and similar implementations.
|
|
8
10
|
*/
|
|
9
11
|
export type WebSocketLike = {
|
|
10
|
-
|
|
12
|
+
send: (data: string | Uint8Array<ArrayBuffer>) => unknown;
|
|
11
13
|
};
|
|
12
14
|
export interface WebSocketHandlerOptions<_T extends Context> {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
|
|
17
|
+
*/
|
|
18
|
+
encodePeerMessage?: EncodePeerMessageOptions | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Options for decoding peer messages. such as `prefix` for distinguishing messages on the same channel..
|
|
21
|
+
*/
|
|
22
|
+
decodePeerMessage?: DecodePeerMessageOptions | undefined;
|
|
21
23
|
}
|
|
22
24
|
export declare class WebSocketHandler<T extends Context> {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
25
|
+
private readonly handler;
|
|
26
|
+
/**
|
|
27
|
+
* `null` marks a closed socket, so late messages never create a new peer.
|
|
28
|
+
*/
|
|
29
|
+
private readonly peers;
|
|
30
|
+
private readonly encodePeerMessageOptions;
|
|
31
|
+
private readonly decodePeerMessageOptions;
|
|
32
|
+
constructor(handler: StandardHandler<T>, options?: NoInfer<WebSocketHandlerOptions<T>>);
|
|
33
|
+
/**
|
|
34
|
+
* Handles a single WebSocket message.
|
|
35
|
+
*
|
|
36
|
+
* Message order matters. Call this immediately after receiving a message,
|
|
37
|
+
* before any other async work, to preserve ordering.
|
|
38
|
+
*
|
|
39
|
+
* To avoid async Blob reads, configure `ws.binaryType` to return binary data
|
|
40
|
+
* directly (for example, `arraybuffer`).
|
|
41
|
+
*
|
|
42
|
+
* @param ws The WebSocket instance. Use the same instance for all messages.
|
|
43
|
+
* @param data The received message. Binary data should already be loaded (not a `Blob`).
|
|
44
|
+
*/
|
|
45
|
+
message(ws: WebSocketLike, data: Arrayable<string | ArrayBuffer | Pick<Uint8Array<ArrayBuffer>, "buffer" | "byteOffset" | "byteLength">>, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): Promise<{
|
|
46
|
+
matched: boolean;
|
|
47
|
+
}>;
|
|
48
|
+
/**
|
|
49
|
+
* Cleans up peer state for a closed WebSocket.
|
|
50
|
+
* Messages passed to `.message()` for this WebSocket afterwards are ignored.
|
|
51
|
+
*
|
|
52
|
+
* @param ws The same WebSocket instance passed to `.message()`.
|
|
53
|
+
*/
|
|
54
|
+
close(ws: WebSocketLike): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Attaches message, close, and error event listeners to a WebSocket.
|
|
57
|
+
*
|
|
58
|
+
* Prefer this over calling `.message()` and `.close()` manually.
|
|
59
|
+
*/
|
|
60
|
+
upgrade(ws: Pick<WebSocket, "send" | "addEventListener" | "removeEventListener">, ...rest: MaybeOptionalOptions<FriendlyStandardHandlerHandleOptions<T>>): void;
|
|
61
|
+
}
|
|
@@ -1,85 +1,75 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
// Not awaited: `this.message` runs business logic that may be slow,
|
|
77
|
-
// and awaiting it would block decoding of subsequent messages.
|
|
78
|
-
void this.message(ws, data, ...rest);
|
|
79
|
-
}));
|
|
80
|
-
ws.addEventListener('close', () => this.close(ws));
|
|
81
|
-
// EventEmitter-based implementations like `ws` throw an unhandled `error` event,
|
|
82
|
-
// so a single malformed frame would crash the process. `close` always follows and handles cleanup.
|
|
83
|
-
ws.addEventListener('error', () => { });
|
|
84
|
-
}
|
|
85
|
-
}
|
|
1
|
+
import { createStandardPeerRequestHandler } from "../standard-peer/utils.js";
|
|
2
|
+
import { loadBytes, resolveMaybeOptionalOptions, sequential, toStringOrBytes } from "@orpc/shared";
|
|
3
|
+
import { ServerPeer, decodePeerMessage, encodePeerMessage, isClientPeerSendMessage } from "@standard-server/peer";
|
|
4
|
+
// src/adapters/websocket/handler.ts
|
|
5
|
+
var WebSocketHandler = class {
|
|
6
|
+
handler;
|
|
7
|
+
/**
|
|
8
|
+
* `null` marks a closed socket, so late messages never create a new peer.
|
|
9
|
+
*/
|
|
10
|
+
peers = /* @__PURE__ */ new WeakMap();
|
|
11
|
+
encodePeerMessageOptions;
|
|
12
|
+
decodePeerMessageOptions;
|
|
13
|
+
constructor(handler, options = {}) {
|
|
14
|
+
this.handler = handler;
|
|
15
|
+
this.encodePeerMessageOptions = options.encodePeerMessage;
|
|
16
|
+
this.decodePeerMessageOptions = options.decodePeerMessage;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Handles a single WebSocket message.
|
|
20
|
+
*
|
|
21
|
+
* Message order matters. Call this immediately after receiving a message,
|
|
22
|
+
* before any other async work, to preserve ordering.
|
|
23
|
+
*
|
|
24
|
+
* To avoid async Blob reads, configure `ws.binaryType` to return binary data
|
|
25
|
+
* directly (for example, `arraybuffer`).
|
|
26
|
+
*
|
|
27
|
+
* @param ws The WebSocket instance. Use the same instance for all messages.
|
|
28
|
+
* @param data The received message. Binary data should already be loaded (not a `Blob`).
|
|
29
|
+
*/
|
|
30
|
+
async message(ws, data, ...rest) {
|
|
31
|
+
/**
|
|
32
|
+
* Message order is important: loading -> decode -> .message.
|
|
33
|
+
* This flow must stay synchronous, or we need to use `sequential` helper
|
|
34
|
+
*/
|
|
35
|
+
const encoded = toStringOrBytes(data);
|
|
36
|
+
const result = decodePeerMessage(encoded, this.decodePeerMessageOptions);
|
|
37
|
+
let peer = this.peers.get(ws);
|
|
38
|
+
if (peer === null) return result;
|
|
39
|
+
if (!peer) this.peers.set(ws, peer = new ServerPeer(async (message) => {
|
|
40
|
+
const encoded = await encodePeerMessage(message, this.encodePeerMessageOptions);
|
|
41
|
+
await ws.send(encoded);
|
|
42
|
+
}));
|
|
43
|
+
if (result.matched && isClientPeerSendMessage(result.message)) await peer.message(result.message, createStandardPeerRequestHandler(this.handler, resolveMaybeOptionalOptions(rest)));
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Cleans up peer state for a closed WebSocket.
|
|
48
|
+
* Messages passed to `.message()` for this WebSocket afterwards are ignored.
|
|
49
|
+
*
|
|
50
|
+
* @param ws The same WebSocket instance passed to `.message()`.
|
|
51
|
+
*/
|
|
52
|
+
async close(ws) {
|
|
53
|
+
const peer = this.peers.get(ws);
|
|
54
|
+
this.peers.set(ws, null);
|
|
55
|
+
await peer?.close();
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Attaches message, close, and error event listeners to a WebSocket.
|
|
59
|
+
*
|
|
60
|
+
* Prefer this over calling `.message()` and `.close()` manually.
|
|
61
|
+
*/
|
|
62
|
+
upgrade(ws, ...rest) {
|
|
63
|
+
/**
|
|
64
|
+
* Message order is important: loading -> decode -> .message.
|
|
65
|
+
* This flow must stay synchronous, or we need to use `sequential` helper
|
|
66
|
+
*/
|
|
67
|
+
ws.addEventListener("message", sequential(async (event) => {
|
|
68
|
+
const data = event.data instanceof Blob ? await loadBytes(event.data) : event.data;
|
|
69
|
+
this.message(ws, data, ...rest);
|
|
70
|
+
}));
|
|
71
|
+
ws.addEventListener("close", () => this.close(ws));
|
|
72
|
+
ws.addEventListener("error", () => {});
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
export { WebSocketHandler };
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { WebSocketHandler, WebSocketHandlerOptions, WebSocketLike } from "./handler.js";
|
|
2
|
+
import { RPCHandler, RPCHandlerOptions } from "./rpc-handler.js";
|
|
3
|
+
export { RPCHandler, RPCHandlerOptions, WebSocketHandler, WebSocketHandlerOptions, WebSocketLike };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { WebSocketHandler } from "./handler.js";
|
|
2
|
+
export { RPCHandler } from "./rpc-handler.js";
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
}
|
|
1
|
+
import { Context } from "../../context.js";
|
|
2
|
+
import { StandardHandlerOptions } from "../standard/handler.js";
|
|
3
|
+
import { Router } from "../../router.js";
|
|
4
|
+
import { RPCHandlerCodecOptions } from "../standard/rpc-handler-codec.js";
|
|
5
|
+
import "../standard/index.js";
|
|
6
|
+
import { WebSocketHandler, WebSocketHandlerOptions } from "./handler.js";
|
|
7
|
+
export interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, RPCHandlerCodecOptions<T>, WebSocketHandlerOptions<T> {}
|
|
8
8
|
/**
|
|
9
9
|
* Serves an oRPC router over the RPC protocol on a standard WebSocket
|
|
10
10
|
* connection (Deno, Bun, Cloudflare Durable Objects, and more).
|
|
@@ -12,5 +12,5 @@ export interface RPCHandlerOptions<T extends Context> extends StandardHandlerOpt
|
|
|
12
12
|
* @see {@link https://orpc.dev/docs/adapters/websocket | WebSocket Adapters}
|
|
13
13
|
*/
|
|
14
14
|
export declare class RPCHandler<T extends Context> extends WebSocketHandler<T> {
|
|
15
|
-
|
|
16
|
-
}
|
|
15
|
+
constructor(router: Router<T>, options?: NoInfer<RPCHandlerOptions<T>>);
|
|
16
|
+
}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import { RPCHandlerCodec
|
|
2
|
-
import {
|
|
1
|
+
import { RPCHandlerCodec } from "../standard/rpc-handler-codec.js";
|
|
2
|
+
import { StandardHandler } from "../standard/handler.js";
|
|
3
|
+
import { WebSocketHandler } from "./handler.js";
|
|
4
|
+
// src/adapters/websocket/rpc-handler.ts
|
|
3
5
|
/**
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
6
|
+
* Serves an oRPC router over the RPC protocol on a standard WebSocket
|
|
7
|
+
* connection (Deno, Bun, Cloudflare Durable Objects, and more).
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://orpc.dev/docs/adapters/websocket | WebSocket Adapters}
|
|
10
|
+
*/
|
|
11
|
+
var RPCHandler = class extends WebSocketHandler {
|
|
12
|
+
constructor(router, options = {}) {
|
|
13
|
+
const codec = new RPCHandlerCodec(router, options);
|
|
14
|
+
const handler = new StandardHandler(codec, options);
|
|
15
|
+
super(handler, options);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
export { RPCHandler };
|