@orpc/server 2.0.0-beta.11 → 2.0.0-beta.13
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 +1 -0
- package/dist/adapters/crossws/index.d.mts +7 -4
- package/dist/adapters/crossws/index.d.ts +7 -4
- package/dist/adapters/crossws/index.mjs +7 -4
- package/dist/adapters/message-port/index.d.mts +7 -9
- package/dist/adapters/message-port/index.d.ts +7 -9
- package/dist/adapters/message-port/index.mjs +25 -29
- package/dist/adapters/websocket/index.d.mts +14 -13
- package/dist/adapters/websocket/index.d.ts +14 -13
- package/dist/adapters/websocket/index.mjs +19 -15
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -50,6 +50,7 @@ You can read the documentation [here](https://orpc.dev).
|
|
|
50
50
|
- [@orpc/experimental-effect](https://www.npmjs.com/package/@orpc/experimental-effect): Integrate with [Effect](https://effect.website/).
|
|
51
51
|
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Implement your contract with [NestJS](https://nestjs.com/).
|
|
52
52
|
- [@orpc/bun](https://www.npmjs.com/package/@orpc/bun): Adapters for [Bun's Redis](https://bun.sh/).
|
|
53
|
+
- [@orpc/cloudflare](https://www.npmjs.com/package/@orpc/cloudflare): Adapters for [Cloudflare's RateLimit and Durable Objects](https://developers.cloudflare.com/workers/).
|
|
53
54
|
|
|
54
55
|
**Observability**
|
|
55
56
|
|
|
@@ -29,17 +29,20 @@ declare class experimental_CrosswsHandler<T extends Context> {
|
|
|
29
29
|
private readonly decodePeerMessageOptions;
|
|
30
30
|
constructor(handler: StandardHandler<T>, options?: NoInfer<experimental_CrosswsHandlerOptions<T>>);
|
|
31
31
|
/**
|
|
32
|
-
* Handles a single message
|
|
32
|
+
* Handles a single WebSocket message.
|
|
33
33
|
*
|
|
34
|
-
*
|
|
34
|
+
* Message order matters. Call this immediately after receiving a message,
|
|
35
|
+
* before any other async work, to preserve ordering.
|
|
36
|
+
*
|
|
37
|
+
* @param ws The crossws peer instance. Use the same instance for all messages.
|
|
35
38
|
*/
|
|
36
39
|
message(ws: CrosswsPeerLike, message: CrosswsMessageLike, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): Promise<{
|
|
37
40
|
matched: boolean;
|
|
38
41
|
}>;
|
|
39
42
|
/**
|
|
40
|
-
*
|
|
43
|
+
* Cleans up peer state for a closed WebSocket.
|
|
41
44
|
*
|
|
42
|
-
* @param ws The crossws
|
|
45
|
+
* @param ws The same crossws peer instance passed to `.message()`.
|
|
43
46
|
*/
|
|
44
47
|
close(ws: CrosswsPeerLike): Promise<void>;
|
|
45
48
|
}
|
|
@@ -29,17 +29,20 @@ declare class experimental_CrosswsHandler<T extends Context> {
|
|
|
29
29
|
private readonly decodePeerMessageOptions;
|
|
30
30
|
constructor(handler: StandardHandler<T>, options?: NoInfer<experimental_CrosswsHandlerOptions<T>>);
|
|
31
31
|
/**
|
|
32
|
-
* Handles a single message
|
|
32
|
+
* Handles a single WebSocket message.
|
|
33
33
|
*
|
|
34
|
-
*
|
|
34
|
+
* Message order matters. Call this immediately after receiving a message,
|
|
35
|
+
* before any other async work, to preserve ordering.
|
|
36
|
+
*
|
|
37
|
+
* @param ws The crossws peer instance. Use the same instance for all messages.
|
|
35
38
|
*/
|
|
36
39
|
message(ws: CrosswsPeerLike, message: CrosswsMessageLike, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): Promise<{
|
|
37
40
|
matched: boolean;
|
|
38
41
|
}>;
|
|
39
42
|
/**
|
|
40
|
-
*
|
|
43
|
+
* Cleans up peer state for a closed WebSocket.
|
|
41
44
|
*
|
|
42
|
-
* @param ws The crossws
|
|
45
|
+
* @param ws The same crossws peer instance passed to `.message()`.
|
|
43
46
|
*/
|
|
44
47
|
close(ws: CrosswsPeerLike): Promise<void>;
|
|
45
48
|
}
|
|
@@ -18,9 +18,12 @@ class experimental_CrosswsHandler {
|
|
|
18
18
|
encodePeerMessageOptions;
|
|
19
19
|
decodePeerMessageOptions;
|
|
20
20
|
/**
|
|
21
|
-
* Handles a single message
|
|
21
|
+
* Handles a single WebSocket message.
|
|
22
22
|
*
|
|
23
|
-
*
|
|
23
|
+
* Message order matters. Call this immediately after receiving a message,
|
|
24
|
+
* before any other async work, to preserve ordering.
|
|
25
|
+
*
|
|
26
|
+
* @param ws The crossws peer instance. Use the same instance for all messages.
|
|
24
27
|
*/
|
|
25
28
|
async message(ws, message, ...rest) {
|
|
26
29
|
let peer = this.peers.get(ws);
|
|
@@ -40,9 +43,9 @@ class experimental_CrosswsHandler {
|
|
|
40
43
|
return result;
|
|
41
44
|
}
|
|
42
45
|
/**
|
|
43
|
-
*
|
|
46
|
+
* Cleans up peer state for a closed WebSocket.
|
|
44
47
|
*
|
|
45
|
-
* @param ws The crossws
|
|
48
|
+
* @param ws The same crossws peer instance passed to `.message()`.
|
|
46
49
|
*/
|
|
47
50
|
async close(ws) {
|
|
48
51
|
const server = this.peers.get(ws);
|
|
@@ -41,25 +41,23 @@ declare class MessagePortHandler<T extends Context> {
|
|
|
41
41
|
private readonly decodePeerMessageOptions;
|
|
42
42
|
constructor(handler: StandardHandler<T>, options?: NoInfer<MessagePortHandlerOptions<T>>);
|
|
43
43
|
/**
|
|
44
|
-
* Attaches
|
|
44
|
+
* Attaches message and close listeners to a message port.
|
|
45
|
+
*
|
|
46
|
+
* Prefer this over calling `.message()` and `.close()` manually.
|
|
45
47
|
*/
|
|
46
48
|
upgrade(port: SupportedMessagePort, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): void;
|
|
47
49
|
/**
|
|
48
50
|
* Handles a single message received from a message port.
|
|
49
51
|
*
|
|
50
|
-
* @
|
|
51
|
-
*
|
|
52
|
-
* @param port The message port instance, require consistent instance across messages for proper peer management
|
|
52
|
+
* @param port The message port instance. Use the same instance for all messages.
|
|
53
53
|
*/
|
|
54
|
-
message(port: SupportedMessagePort, data:
|
|
54
|
+
message(port: SupportedMessagePort, data: unknown, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): Promise<{
|
|
55
55
|
matched: boolean;
|
|
56
56
|
}>;
|
|
57
57
|
/**
|
|
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.
|
|
58
|
+
* Cleans up peer state for a closed message port.
|
|
61
59
|
*
|
|
62
|
-
* @param port The message port instance to
|
|
60
|
+
* @param port The same message port instance passed to `.message()`.
|
|
63
61
|
*/
|
|
64
62
|
close(port: SupportedMessagePort): Promise<void>;
|
|
65
63
|
}
|
|
@@ -41,25 +41,23 @@ declare class MessagePortHandler<T extends Context> {
|
|
|
41
41
|
private readonly decodePeerMessageOptions;
|
|
42
42
|
constructor(handler: StandardHandler<T>, options?: NoInfer<MessagePortHandlerOptions<T>>);
|
|
43
43
|
/**
|
|
44
|
-
* Attaches
|
|
44
|
+
* Attaches message and close listeners to a message port.
|
|
45
|
+
*
|
|
46
|
+
* Prefer this over calling `.message()` and `.close()` manually.
|
|
45
47
|
*/
|
|
46
48
|
upgrade(port: SupportedMessagePort, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): void;
|
|
47
49
|
/**
|
|
48
50
|
* Handles a single message received from a message port.
|
|
49
51
|
*
|
|
50
|
-
* @
|
|
51
|
-
*
|
|
52
|
-
* @param port The message port instance, require consistent instance across messages for proper peer management
|
|
52
|
+
* @param port The message port instance. Use the same instance for all messages.
|
|
53
53
|
*/
|
|
54
|
-
message(port: SupportedMessagePort, data:
|
|
54
|
+
message(port: SupportedMessagePort, data: unknown, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): Promise<{
|
|
55
55
|
matched: boolean;
|
|
56
56
|
}>;
|
|
57
57
|
/**
|
|
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.
|
|
58
|
+
* Cleans up peer state for a closed message port.
|
|
61
59
|
*
|
|
62
|
-
* @param port The message port instance to
|
|
60
|
+
* @param port The same message port instance passed to `.message()`.
|
|
63
61
|
*/
|
|
64
62
|
close(port: SupportedMessagePort): Promise<void>;
|
|
65
63
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
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
5
|
import { R as RPCHandlerCodec, S as StandardHandler } from '../../shared/server.GDpX6Df8.mjs';
|
|
6
6
|
import '@orpc/client';
|
|
@@ -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,4 +1,4 @@
|
|
|
1
|
-
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
1
|
+
import { Arrayable, MaybeOptionalOptions } from '@orpc/shared';
|
|
2
2
|
import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standardserver/peer';
|
|
3
3
|
import { C as Context } from '../../shared/server.BL22TloH.mjs';
|
|
4
4
|
import { S as StandardHandler, a as StandardHandlerOptions } from '../../shared/server.DJIkub_t.mjs';
|
|
@@ -33,29 +33,30 @@ declare class WebSocketHandler<T extends Context> {
|
|
|
33
33
|
private readonly decodePeerMessageOptions;
|
|
34
34
|
constructor(handler: StandardHandler<T>, options?: NoInfer<WebSocketHandlerOptions<T>>);
|
|
35
35
|
/**
|
|
36
|
-
* Handles a single message
|
|
36
|
+
* Handles a single WebSocket message.
|
|
37
37
|
*
|
|
38
|
-
*
|
|
38
|
+
* Message order matters. Call this immediately after receiving a message,
|
|
39
|
+
* before any other async work, to preserve ordering.
|
|
39
40
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
41
|
+
* To avoid async Blob reads, configure `ws.binaryType` to return binary data
|
|
42
|
+
* directly (for example, `arraybuffer`).
|
|
43
|
+
*
|
|
44
|
+
* @param ws The WebSocket instance. Use the same instance for all messages.
|
|
45
|
+
* @param data The received message. Binary data should already be loaded (not a `Blob`).
|
|
42
46
|
*/
|
|
43
|
-
message(ws: WebSocketLike, data: string | ArrayBuffer |
|
|
47
|
+
message(ws: WebSocketLike, data: Arrayable<string | ArrayBuffer | Pick<Uint8Array<ArrayBuffer>, 'buffer' | 'byteOffset' | 'byteLength'>>, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): Promise<{
|
|
44
48
|
matched: boolean;
|
|
45
49
|
}>;
|
|
46
50
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @warning AVOID calling this method, if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
|
|
51
|
+
* Cleans up peer state for a closed WebSocket.
|
|
50
52
|
*
|
|
51
|
-
* @param ws The WebSocket instance to
|
|
53
|
+
* @param ws The same WebSocket instance passed to `.message()`.
|
|
52
54
|
*/
|
|
53
55
|
close(ws: WebSocketLike): Promise<void>;
|
|
54
56
|
/**
|
|
55
|
-
* Attaches
|
|
57
|
+
* Attaches message and close event listeners to a WebSocket.
|
|
56
58
|
*
|
|
57
|
-
*
|
|
58
|
-
* Requires a websocket-like object that supports `addEventListener` and `removeEventListener`.
|
|
59
|
+
* Prefer this over calling `.message()` and `.close()` manually.
|
|
59
60
|
*/
|
|
60
61
|
upgrade(ws: Pick<WebSocket, 'send' | 'addEventListener' | 'removeEventListener'>, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): void;
|
|
61
62
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
1
|
+
import { Arrayable, MaybeOptionalOptions } from '@orpc/shared';
|
|
2
2
|
import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standardserver/peer';
|
|
3
3
|
import { C as Context } from '../../shared/server.BL22TloH.js';
|
|
4
4
|
import { S as StandardHandler, a as StandardHandlerOptions } from '../../shared/server.DBANIheG.js';
|
|
@@ -33,29 +33,30 @@ declare class WebSocketHandler<T extends Context> {
|
|
|
33
33
|
private readonly decodePeerMessageOptions;
|
|
34
34
|
constructor(handler: StandardHandler<T>, options?: NoInfer<WebSocketHandlerOptions<T>>);
|
|
35
35
|
/**
|
|
36
|
-
* Handles a single message
|
|
36
|
+
* Handles a single WebSocket message.
|
|
37
37
|
*
|
|
38
|
-
*
|
|
38
|
+
* Message order matters. Call this immediately after receiving a message,
|
|
39
|
+
* before any other async work, to preserve ordering.
|
|
39
40
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
41
|
+
* To avoid async Blob reads, configure `ws.binaryType` to return binary data
|
|
42
|
+
* directly (for example, `arraybuffer`).
|
|
43
|
+
*
|
|
44
|
+
* @param ws The WebSocket instance. Use the same instance for all messages.
|
|
45
|
+
* @param data The received message. Binary data should already be loaded (not a `Blob`).
|
|
42
46
|
*/
|
|
43
|
-
message(ws: WebSocketLike, data: string | ArrayBuffer |
|
|
47
|
+
message(ws: WebSocketLike, data: Arrayable<string | ArrayBuffer | Pick<Uint8Array<ArrayBuffer>, 'buffer' | 'byteOffset' | 'byteLength'>>, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): Promise<{
|
|
44
48
|
matched: boolean;
|
|
45
49
|
}>;
|
|
46
50
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @warning AVOID calling this method, if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
|
|
51
|
+
* Cleans up peer state for a closed WebSocket.
|
|
50
52
|
*
|
|
51
|
-
* @param ws The WebSocket instance to
|
|
53
|
+
* @param ws The same WebSocket instance passed to `.message()`.
|
|
52
54
|
*/
|
|
53
55
|
close(ws: WebSocketLike): Promise<void>;
|
|
54
56
|
/**
|
|
55
|
-
* Attaches
|
|
57
|
+
* Attaches message and close event listeners to a WebSocket.
|
|
56
58
|
*
|
|
57
|
-
*
|
|
58
|
-
* Requires a websocket-like object that supports `addEventListener` and `removeEventListener`.
|
|
59
|
+
* Prefer this over calling `.message()` and `.close()` manually.
|
|
59
60
|
*/
|
|
60
61
|
upgrade(ws: Pick<WebSocket, 'send' | 'addEventListener' | 'removeEventListener'>, ...rest: MaybeOptionalOptions<StandardPeerRequestHandlerOptions<T>>): void;
|
|
61
62
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { toStringOrBytes, resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
1
|
+
import { toStringOrBytes, resolveMaybeOptionalOptions, sequential, loadBytes } from '@orpc/shared';
|
|
2
2
|
import { ServerPeer, encodePeerMessage, decodePeerMessage, isClientPeerSendMessage } from '@standardserver/peer';
|
|
3
3
|
import { c as createStandardPeerRequestHandler } from '../../shared/server.CX4vUnDk.mjs';
|
|
4
4
|
import { R as RPCHandlerCodec, S as StandardHandler } from '../../shared/server.GDpX6Df8.mjs';
|
|
@@ -18,12 +18,16 @@ class WebSocketHandler {
|
|
|
18
18
|
encodePeerMessageOptions;
|
|
19
19
|
decodePeerMessageOptions;
|
|
20
20
|
/**
|
|
21
|
-
* Handles a single message
|
|
21
|
+
* Handles a single WebSocket message.
|
|
22
22
|
*
|
|
23
|
-
*
|
|
23
|
+
* Message order matters. Call this immediately after receiving a message,
|
|
24
|
+
* before any other async work, to preserve ordering.
|
|
24
25
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
26
|
+
* To avoid async Blob reads, configure `ws.binaryType` to return binary data
|
|
27
|
+
* directly (for example, `arraybuffer`).
|
|
28
|
+
*
|
|
29
|
+
* @param ws The WebSocket instance. Use the same instance for all messages.
|
|
30
|
+
* @param data The received message. Binary data should already be loaded (not a `Blob`).
|
|
27
31
|
*/
|
|
28
32
|
async message(ws, data, ...rest) {
|
|
29
33
|
let peer = this.peers.get(ws);
|
|
@@ -33,7 +37,7 @@ class WebSocketHandler {
|
|
|
33
37
|
await ws.send(encoded2);
|
|
34
38
|
}));
|
|
35
39
|
}
|
|
36
|
-
const encoded =
|
|
40
|
+
const encoded = toStringOrBytes(data);
|
|
37
41
|
const result = decodePeerMessage(encoded, this.decodePeerMessageOptions);
|
|
38
42
|
if (result.matched && isClientPeerSendMessage(result.message)) {
|
|
39
43
|
await peer.message(
|
|
@@ -44,27 +48,27 @@ class WebSocketHandler {
|
|
|
44
48
|
return result;
|
|
45
49
|
}
|
|
46
50
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @warning AVOID calling this method, if `.upgrade()` is used, as `.upgrade()` already sets up necessary event listeners to call this method for incoming messages and manage peer lifecycle.
|
|
51
|
+
* Cleans up peer state for a closed WebSocket.
|
|
50
52
|
*
|
|
51
|
-
* @param ws The WebSocket instance to
|
|
53
|
+
* @param ws The same WebSocket instance passed to `.message()`.
|
|
52
54
|
*/
|
|
53
55
|
async close(ws) {
|
|
54
56
|
const peer = this.peers.get(ws);
|
|
55
57
|
if (peer) {
|
|
56
|
-
await peer.close();
|
|
57
58
|
this.peers.delete(ws);
|
|
59
|
+
await peer.close();
|
|
58
60
|
}
|
|
59
61
|
}
|
|
60
62
|
/**
|
|
61
|
-
* Attaches
|
|
63
|
+
* Attaches message and close event listeners to a WebSocket.
|
|
62
64
|
*
|
|
63
|
-
*
|
|
64
|
-
* Requires a websocket-like object that supports `addEventListener` and `removeEventListener`.
|
|
65
|
+
* Prefer this over calling `.message()` and `.close()` manually.
|
|
65
66
|
*/
|
|
66
67
|
upgrade(ws, ...rest) {
|
|
67
|
-
ws.addEventListener("message", (event) =>
|
|
68
|
+
ws.addEventListener("message", sequential(async (event) => {
|
|
69
|
+
const data = event.data instanceof Blob ? await loadBytes(event.data) : event.data;
|
|
70
|
+
this.message(ws, data, ...rest);
|
|
71
|
+
}));
|
|
68
72
|
ws.addEventListener("close", () => this.close(ws));
|
|
69
73
|
}
|
|
70
74
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.13",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.dev",
|
|
7
7
|
"repository": {
|
|
@@ -90,10 +90,10 @@
|
|
|
90
90
|
"@standardserver/node": "^0.0.25",
|
|
91
91
|
"@standardserver/peer": "^0.0.25",
|
|
92
92
|
"cookie": "^1.1.1",
|
|
93
|
-
"@orpc/client": "2.0.0-beta.
|
|
94
|
-
"@orpc/contract": "2.0.0-beta.
|
|
95
|
-
"@orpc/
|
|
96
|
-
"@orpc/
|
|
93
|
+
"@orpc/client": "2.0.0-beta.13",
|
|
94
|
+
"@orpc/contract": "2.0.0-beta.13",
|
|
95
|
+
"@orpc/interop": "2.0.0-beta.13",
|
|
96
|
+
"@orpc/shared": "2.0.0-beta.13"
|
|
97
97
|
},
|
|
98
98
|
"devDependencies": {
|
|
99
99
|
"crossws": "^0.4.6",
|