@orpc/client 2.0.0-beta.4 → 2.0.0-beta.40
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 +97 -94
- package/dist/adapters/fetch/index.d.mts +16 -33
- package/dist/adapters/fetch/index.d.ts +16 -33
- package/dist/adapters/fetch/index.mjs +32 -30
- package/dist/adapters/message-port/index.d.mts +13 -8
- package/dist/adapters/message-port/index.d.ts +13 -8
- package/dist/adapters/message-port/index.mjs +22 -17
- package/dist/adapters/standard/index.d.mts +9 -10
- package/dist/adapters/standard/index.d.ts +9 -10
- package/dist/adapters/standard/index.mjs +5 -5
- package/dist/adapters/websocket/index.d.mts +21 -16
- package/dist/adapters/websocket/index.d.ts +21 -16
- package/dist/adapters/websocket/index.mjs +14 -14
- package/dist/index.d.mts +62 -61
- package/dist/index.d.ts +62 -61
- package/dist/index.mjs +47 -54
- package/dist/plugins/index.d.mts +134 -13
- package/dist/plugins/index.d.ts +134 -13
- package/dist/plugins/index.mjs +298 -27
- package/dist/shared/{client.Cby_-GGh.d.mts → client.BWkR2vPU.d.mts} +2 -2
- package/dist/shared/client.BYHnU_Z3.mjs +469 -0
- package/dist/shared/{client.CPF3hX6O.d.ts → client.BnpRXnLi.d.ts} +2 -2
- package/dist/shared/{client.8ug8I-zu.d.mts → client.BoXZCzuS.d.mts} +56 -16
- package/dist/shared/{client.8ug8I-zu.d.ts → client.BoXZCzuS.d.ts} +56 -16
- package/dist/shared/{client.Dnfj8jnT.mjs → client.Dtv1JPkU.mjs} +19 -15
- package/dist/shared/{client.DXhchJ84.mjs → client.VXsLhJtT.mjs} +25 -30
- package/dist/shared/{client.BdItY5DT.d.mts → client.n8K9dFhY.d.mts} +25 -4
- package/dist/shared/{client.BdItY5DT.d.ts → client.n8K9dFhY.d.ts} +25 -4
- package/package.json +20 -8
- package/dist/shared/client.DMXKFDyV.mjs +0 -343
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { toFetchBody, toFetchHeaders, toStandardLazyResponse } from '@
|
|
3
|
-
import { S as StandardLink, R as RPCLinkCodec } from '../../shared/client.
|
|
4
|
-
import '@
|
|
5
|
-
import '../../shared/client.
|
|
6
|
-
import '../../shared/client.
|
|
1
|
+
import { value, once } from '@orpc/shared';
|
|
2
|
+
import { toFetchBody, toFetchHeaders, toStandardLazyResponse } from '@standard-server/fetch';
|
|
3
|
+
import { S as StandardLink, R as RPCLinkCodec } from '../../shared/client.VXsLhJtT.mjs';
|
|
4
|
+
import '@standard-server/core';
|
|
5
|
+
import '../../shared/client.BYHnU_Z3.mjs';
|
|
6
|
+
import '../../shared/client.Dtv1JPkU.mjs';
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
const GET_SUPPORTED_DUPLEX_MODE = once(() => {
|
|
9
|
+
try {
|
|
10
|
+
let duplex;
|
|
11
|
+
void new Request(
|
|
12
|
+
"https://example.com",
|
|
13
|
+
{
|
|
14
|
+
method: "POST",
|
|
15
|
+
body: new ReadableStream(),
|
|
16
|
+
get duplex() {
|
|
17
|
+
duplex = "half";
|
|
18
|
+
return "half";
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
|
-
|
|
20
|
-
return
|
|
21
|
+
);
|
|
22
|
+
return duplex;
|
|
23
|
+
} catch {
|
|
24
|
+
return void 0;
|
|
21
25
|
}
|
|
22
|
-
}
|
|
23
|
-
|
|
26
|
+
});
|
|
24
27
|
class FetchLinkTransport {
|
|
25
28
|
origin;
|
|
26
29
|
fetch;
|
|
27
30
|
toFetchRequestOptions;
|
|
28
|
-
fetchInterceptors;
|
|
29
31
|
constructor(options) {
|
|
30
|
-
options = new CompositeFetchLinkTransportPlugin(options.plugins).initFetchLinkTransportOptions(options);
|
|
31
32
|
this.origin = options.origin;
|
|
32
|
-
this.fetch = options.fetch ?? globalThis.fetch
|
|
33
|
+
this.fetch = options.fetch ?? ((url, init) => globalThis.fetch(url, init));
|
|
33
34
|
this.toFetchRequestOptions = options.toFetchRequest;
|
|
34
|
-
this.fetchInterceptors = options.fetchInterceptors;
|
|
35
35
|
}
|
|
36
36
|
async send(standardRequest, path, options) {
|
|
37
37
|
let origin = await value(this.origin, options, path);
|
|
@@ -47,11 +47,13 @@ class FetchLinkTransport {
|
|
|
47
47
|
signal: options.signal,
|
|
48
48
|
redirect: "manual"
|
|
49
49
|
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
if (body instanceof ReadableStream) {
|
|
51
|
+
const duplex = GET_SUPPORTED_DUPLEX_MODE();
|
|
52
|
+
if (duplex !== void 0) {
|
|
53
|
+
init.duplex = duplex;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const response = await this.fetch(url, init, options, path);
|
|
55
57
|
const standardResponse = toStandardLazyResponse(response);
|
|
56
58
|
return standardResponse;
|
|
57
59
|
}
|
|
@@ -65,4 +67,4 @@ class RPCLink extends StandardLink {
|
|
|
65
67
|
}
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
export {
|
|
70
|
+
export { FetchLinkTransport, RPCLink };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ClientContext, ClientOptions } from '../../index.mjs';
|
|
2
2
|
import { Value, Promisable } from '@orpc/shared';
|
|
3
|
-
import { StandardRequest, StandardLazyResponse } from '@
|
|
4
|
-
import { ClientPeer, EncodePeerMessageOptions, DecodePeerMessageOptions } from '@
|
|
5
|
-
import {
|
|
3
|
+
import { StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
4
|
+
import { ClientPeer, EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standard-server/peer';
|
|
5
|
+
import { StandardLinkTransport, StandardLink, StandardLinkOptions } from '../standard/index.mjs';
|
|
6
6
|
import { RPCLinkCodecOptions } from '../standard/index.mjs';
|
|
7
|
-
import '../../shared/client.
|
|
7
|
+
import '../../shared/client.n8K9dFhY.mjs';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* The message port used by electron in main process
|
|
@@ -45,9 +45,8 @@ interface MessagePortLinkTransportOptions<_T extends ClientContext> {
|
|
|
45
45
|
* such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
|
|
46
46
|
*
|
|
47
47
|
* @remarks
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* @warning Make sure your message port supports `transfer` before using this feature.
|
|
48
|
+
* **Note**: Returning `null` or `undefined` disables this feature.
|
|
49
|
+
* **Warning**: Make sure your message port supports `transfer` before using this feature.
|
|
51
50
|
*/
|
|
52
51
|
experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedRequestMessage, port: SupportedMessagePort]>;
|
|
53
52
|
/**
|
|
@@ -67,6 +66,12 @@ declare class MessagePortLinkTransport<T extends ClientContext> implements Stand
|
|
|
67
66
|
|
|
68
67
|
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, MessagePortLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
69
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Client link that communicates with an RPC Handler over a Message Port
|
|
71
|
+
* (e.g. workers, iframes, browser extensions, Electron).
|
|
72
|
+
*
|
|
73
|
+
* @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
|
|
74
|
+
*/
|
|
70
75
|
declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
71
76
|
constructor(options: RPCLinkOptions<T>);
|
|
72
77
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ClientContext, ClientOptions } from '../../index.js';
|
|
2
2
|
import { Value, Promisable } from '@orpc/shared';
|
|
3
|
-
import { StandardRequest, StandardLazyResponse } from '@
|
|
4
|
-
import { ClientPeer, EncodePeerMessageOptions, DecodePeerMessageOptions } from '@
|
|
5
|
-
import {
|
|
3
|
+
import { StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
4
|
+
import { ClientPeer, EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standard-server/peer';
|
|
5
|
+
import { StandardLinkTransport, StandardLink, StandardLinkOptions } from '../standard/index.js';
|
|
6
6
|
import { RPCLinkCodecOptions } from '../standard/index.js';
|
|
7
|
-
import '../../shared/client.
|
|
7
|
+
import '../../shared/client.n8K9dFhY.js';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* The message port used by electron in main process
|
|
@@ -45,9 +45,8 @@ interface MessagePortLinkTransportOptions<_T extends ClientContext> {
|
|
|
45
45
|
* such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
|
|
46
46
|
*
|
|
47
47
|
* @remarks
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
* @warning Make sure your message port supports `transfer` before using this feature.
|
|
48
|
+
* **Note**: Returning `null` or `undefined` disables this feature.
|
|
49
|
+
* **Warning**: Make sure your message port supports `transfer` before using this feature.
|
|
51
50
|
*/
|
|
52
51
|
experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedRequestMessage, port: SupportedMessagePort]>;
|
|
53
52
|
/**
|
|
@@ -67,6 +66,12 @@ declare class MessagePortLinkTransport<T extends ClientContext> implements Stand
|
|
|
67
66
|
|
|
68
67
|
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, MessagePortLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
69
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Client link that communicates with an RPC Handler over a Message Port
|
|
71
|
+
* (e.g. workers, iframes, browser extensions, Electron).
|
|
72
|
+
*
|
|
73
|
+
* @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
|
|
74
|
+
*/
|
|
70
75
|
declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
71
76
|
constructor(options: RPCLinkOptions<T>);
|
|
72
77
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { value
|
|
2
|
-
import { ClientPeer, encodePeerMessage, decodePeerMessage, isServerPeerSendMessage } from '@
|
|
3
|
-
import { S as StandardLink, R as RPCLinkCodec } from '../../shared/client.
|
|
4
|
-
import '@
|
|
5
|
-
import '@
|
|
6
|
-
import '../../shared/client.
|
|
7
|
-
import '../../shared/client.
|
|
1
|
+
import { value } from '@orpc/shared';
|
|
2
|
+
import { ClientPeer, encodePeerMessage, decodePeerMessage, isServerPeerSendMessage, isPeerMessage } from '@standard-server/peer';
|
|
3
|
+
import { S as StandardLink, R as RPCLinkCodec } from '../../shared/client.VXsLhJtT.mjs';
|
|
4
|
+
import '@standard-server/core';
|
|
5
|
+
import '@standard-server/fetch';
|
|
6
|
+
import '../../shared/client.BYHnU_Z3.mjs';
|
|
7
|
+
import '../../shared/client.Dtv1JPkU.mjs';
|
|
8
8
|
|
|
9
9
|
function postMessagePortMessage(port, data, transfer) {
|
|
10
10
|
if (transfer) {
|
|
@@ -59,19 +59,24 @@ class MessagePortLinkTransport {
|
|
|
59
59
|
postMessagePortMessage(port, await encodePeerMessage(message, encodePeerMessageOptions));
|
|
60
60
|
}
|
|
61
61
|
});
|
|
62
|
-
onMessagePortMessage(port, async (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
onMessagePortMessage(port, async (data) => {
|
|
63
|
+
let peerMessage;
|
|
64
|
+
if (typeof data === "string" || data instanceof Uint8Array) {
|
|
65
|
+
const result = decodePeerMessage(data, decodePeerMessageOptions);
|
|
66
|
+
if (result.matched && isServerPeerSendMessage(result.message)) {
|
|
67
|
+
peerMessage = result.message;
|
|
68
|
+
}
|
|
69
|
+
} else if (isPeerMessage(data) && isServerPeerSendMessage(data)) {
|
|
70
|
+
peerMessage = data;
|
|
66
71
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (result.matched && isServerPeerSendMessage(result.message)) {
|
|
70
|
-
await this.peer.message(result.message);
|
|
72
|
+
if (peerMessage === void 0) {
|
|
73
|
+
return { matched: false };
|
|
71
74
|
}
|
|
75
|
+
await this.peer.message(peerMessage);
|
|
76
|
+
return { matched: true };
|
|
72
77
|
});
|
|
73
|
-
onMessagePortClose(port, () => {
|
|
74
|
-
this.peer.close();
|
|
78
|
+
onMessagePortClose(port, async () => {
|
|
79
|
+
await this.peer.close();
|
|
75
80
|
});
|
|
76
81
|
}
|
|
77
82
|
async send(standardRequest, _path, _options) {
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { f as StandardLinkCodec, g as StandardLinkCodecDecodedResponse } from '../../shared/client.
|
|
2
|
-
export { C as CompositeStandardLinkPlugin, a as StandardLink, h as StandardLinkInterceptor, e as StandardLinkInterceptorOptions, b as StandardLinkOptions, c as StandardLinkPlugin, S as StandardLinkTransport, i as StandardLinkTransportInterceptor, d as StandardLinkTransportInterceptorOptions } from '../../shared/client.
|
|
3
|
-
import { Value, Promisable } from '@orpc/shared';
|
|
4
|
-
import { StandardUrl, StandardHeaders, StandardRequest, StandardLazyResponse } from '@
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
import { e as RPCSerializer } from '../../shared/client.BdItY5DT.mjs';
|
|
1
|
+
import { f as StandardLinkCodec, g as StandardLinkCodecDecodedResponse } from '../../shared/client.BWkR2vPU.mjs';
|
|
2
|
+
export { C as CompositeStandardLinkPlugin, a as StandardLink, h as StandardLinkInterceptor, e as StandardLinkInterceptorOptions, b as StandardLinkOptions, c as StandardLinkPlugin, S as StandardLinkTransport, i as StandardLinkTransportInterceptor, d as StandardLinkTransportInterceptorOptions } from '../../shared/client.BWkR2vPU.mjs';
|
|
3
|
+
import { Value, Promisable, Public } from '@orpc/shared';
|
|
4
|
+
import { StandardUrl, StandardHeaders, StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
5
|
+
import { ClientContext, ClientOptions } from '../../index.mjs';
|
|
6
|
+
import { RPCSerializer } from '../../index.mjs';
|
|
8
7
|
|
|
9
8
|
interface RPCLinkCodecOptions<T extends ClientContext> {
|
|
10
9
|
/**
|
|
@@ -28,14 +27,14 @@ interface RPCLinkCodecOptions<T extends ClientContext> {
|
|
|
28
27
|
*
|
|
29
28
|
* @default 'POST'
|
|
30
29
|
*/
|
|
31
|
-
method?: Value<Promisable<'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'>, [options: ClientOptions<T>, path: string[], input: unknown]>;
|
|
30
|
+
method?: Value<Promisable<'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'QUERY'>, [options: ClientOptions<T>, path: string[], input: unknown]>;
|
|
32
31
|
/**
|
|
33
32
|
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
34
33
|
* GET is not allowed, it's very dangerous.
|
|
35
34
|
*
|
|
36
35
|
* @default 'POST'
|
|
37
36
|
*/
|
|
38
|
-
fallbackMethod?: 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
37
|
+
fallbackMethod?: 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'QUERY';
|
|
39
38
|
/**
|
|
40
39
|
* Inject headers to the request.
|
|
41
40
|
*/
|
|
@@ -43,7 +42,7 @@ interface RPCLinkCodecOptions<T extends ClientContext> {
|
|
|
43
42
|
/**
|
|
44
43
|
* Override the default RPC serializer.
|
|
45
44
|
*/
|
|
46
|
-
serializer?:
|
|
45
|
+
serializer?: Public<RPCSerializer>;
|
|
47
46
|
}
|
|
48
47
|
declare class RPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
49
48
|
private readonly baseUrl;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { f as StandardLinkCodec, g as StandardLinkCodecDecodedResponse } from '../../shared/client.
|
|
2
|
-
export { C as CompositeStandardLinkPlugin, a as StandardLink, h as StandardLinkInterceptor, e as StandardLinkInterceptorOptions, b as StandardLinkOptions, c as StandardLinkPlugin, S as StandardLinkTransport, i as StandardLinkTransportInterceptor, d as StandardLinkTransportInterceptorOptions } from '../../shared/client.
|
|
3
|
-
import { Value, Promisable } from '@orpc/shared';
|
|
4
|
-
import { StandardUrl, StandardHeaders, StandardRequest, StandardLazyResponse } from '@
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
import { e as RPCSerializer } from '../../shared/client.BdItY5DT.js';
|
|
1
|
+
import { f as StandardLinkCodec, g as StandardLinkCodecDecodedResponse } from '../../shared/client.BnpRXnLi.js';
|
|
2
|
+
export { C as CompositeStandardLinkPlugin, a as StandardLink, h as StandardLinkInterceptor, e as StandardLinkInterceptorOptions, b as StandardLinkOptions, c as StandardLinkPlugin, S as StandardLinkTransport, i as StandardLinkTransportInterceptor, d as StandardLinkTransportInterceptorOptions } from '../../shared/client.BnpRXnLi.js';
|
|
3
|
+
import { Value, Promisable, Public } from '@orpc/shared';
|
|
4
|
+
import { StandardUrl, StandardHeaders, StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
5
|
+
import { ClientContext, ClientOptions } from '../../index.js';
|
|
6
|
+
import { RPCSerializer } from '../../index.js';
|
|
8
7
|
|
|
9
8
|
interface RPCLinkCodecOptions<T extends ClientContext> {
|
|
10
9
|
/**
|
|
@@ -28,14 +27,14 @@ interface RPCLinkCodecOptions<T extends ClientContext> {
|
|
|
28
27
|
*
|
|
29
28
|
* @default 'POST'
|
|
30
29
|
*/
|
|
31
|
-
method?: Value<Promisable<'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'>, [options: ClientOptions<T>, path: string[], input: unknown]>;
|
|
30
|
+
method?: Value<Promisable<'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'QUERY'>, [options: ClientOptions<T>, path: string[], input: unknown]>;
|
|
32
31
|
/**
|
|
33
32
|
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
34
33
|
* GET is not allowed, it's very dangerous.
|
|
35
34
|
*
|
|
36
35
|
* @default 'POST'
|
|
37
36
|
*/
|
|
38
|
-
fallbackMethod?: 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
37
|
+
fallbackMethod?: 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'QUERY';
|
|
39
38
|
/**
|
|
40
39
|
* Inject headers to the request.
|
|
41
40
|
*/
|
|
@@ -43,7 +42,7 @@ interface RPCLinkCodecOptions<T extends ClientContext> {
|
|
|
43
42
|
/**
|
|
44
43
|
* Override the default RPC serializer.
|
|
45
44
|
*/
|
|
46
|
-
serializer?:
|
|
45
|
+
serializer?: Public<RPCSerializer>;
|
|
47
46
|
}
|
|
48
47
|
declare class RPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
49
48
|
private readonly baseUrl;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { C as CompositeStandardLinkPlugin, R as RPCLinkCodec, S as StandardLink } from '../../shared/client.
|
|
1
|
+
export { C as CompositeStandardLinkPlugin, R as RPCLinkCodec, S as StandardLink } from '../../shared/client.VXsLhJtT.mjs';
|
|
2
2
|
import '@orpc/shared';
|
|
3
|
-
import '@
|
|
4
|
-
import '@
|
|
5
|
-
import '../../shared/client.
|
|
6
|
-
import '../../shared/client.
|
|
3
|
+
import '@standard-server/core';
|
|
4
|
+
import '@standard-server/fetch';
|
|
5
|
+
import '../../shared/client.BYHnU_Z3.mjs';
|
|
6
|
+
import '../../shared/client.Dtv1JPkU.mjs';
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ClientContext, ClientOptions } from '../../index.mjs';
|
|
2
2
|
import { Promisable } from '@orpc/shared';
|
|
3
|
-
import { StandardRequest, StandardLazyResponse } from '@
|
|
4
|
-
import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@
|
|
5
|
-
import {
|
|
3
|
+
import { StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
4
|
+
import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standard-server/peer';
|
|
5
|
+
import { StandardLinkTransport, StandardLink, StandardLinkOptions } from '../standard/index.mjs';
|
|
6
6
|
import { RPCLinkCodecOptions } from '../standard/index.mjs';
|
|
7
|
-
import '../../shared/client.
|
|
7
|
+
import '../../shared/client.n8K9dFhY.mjs';
|
|
8
8
|
|
|
9
9
|
type WebSocketLike = Pick<WebSocket, 'addEventListener' | 'removeEventListener' | 'send' | 'readyState'>;
|
|
10
|
-
interface
|
|
10
|
+
interface WebSocketLinkTransportAttemptInfo {
|
|
11
11
|
/**
|
|
12
12
|
* Total number of connection attempts for this transport's lifetime.
|
|
13
13
|
* Starts at 1 on the first attempt, increments on every subsequent
|
|
@@ -21,7 +21,7 @@ interface WebsocketLinkTransportAttemptInfo {
|
|
|
21
21
|
*/
|
|
22
22
|
attempt: number;
|
|
23
23
|
}
|
|
24
|
-
interface
|
|
24
|
+
interface WebSocketLinkTransportReconnectOptions {
|
|
25
25
|
/**
|
|
26
26
|
* Whether to automatically reconnect when the connection is lost.
|
|
27
27
|
*
|
|
@@ -33,7 +33,7 @@ interface WebsocketLinkTransportReconnectOptions {
|
|
|
33
33
|
*
|
|
34
34
|
* @default info => info.attempt === 1 ? 0 : 2_000
|
|
35
35
|
*/
|
|
36
|
-
delay?: undefined | ((info:
|
|
36
|
+
delay?: undefined | ((info: WebSocketLinkTransportAttemptInfo) => number);
|
|
37
37
|
/**
|
|
38
38
|
* Maximum number of consecutive failed attempts before giving up.
|
|
39
39
|
* When exceeded, `getConnectedPeer` throws instead of retrying.
|
|
@@ -66,12 +66,12 @@ interface WebsocketLinkTransportReconnectOptions {
|
|
|
66
66
|
delay?: number;
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
|
-
interface
|
|
69
|
+
interface WebSocketLinkTransportOptions<_T extends ClientContext> {
|
|
70
70
|
/**
|
|
71
71
|
* Returns a WebSocket instance for peer communication.
|
|
72
72
|
* Can be async for lazy resolution.
|
|
73
73
|
*/
|
|
74
|
-
connect: (info:
|
|
74
|
+
connect: (info: WebSocketLinkTransportAttemptInfo) => Promisable<WebSocketLike>;
|
|
75
75
|
/**
|
|
76
76
|
* Whether to connect immediately on initialization, instead of waiting
|
|
77
77
|
* for the first call. Reduces latency for the first request.
|
|
@@ -84,7 +84,7 @@ interface WebsocketLinkTransportOptions<_T extends ClientContext> {
|
|
|
84
84
|
*
|
|
85
85
|
* @default { enabled: false }
|
|
86
86
|
*/
|
|
87
|
-
reconnect?: undefined |
|
|
87
|
+
reconnect?: undefined | WebSocketLinkTransportReconnectOptions;
|
|
88
88
|
/**
|
|
89
89
|
* Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
|
|
90
90
|
*/
|
|
@@ -94,7 +94,7 @@ interface WebsocketLinkTransportOptions<_T extends ClientContext> {
|
|
|
94
94
|
*/
|
|
95
95
|
decodePeerMessage?: DecodePeerMessageOptions | undefined;
|
|
96
96
|
}
|
|
97
|
-
declare class
|
|
97
|
+
declare class WebSocketLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
|
|
98
98
|
private readonly connect;
|
|
99
99
|
private readonly reconnectEnabled;
|
|
100
100
|
private readonly reconnectDelay;
|
|
@@ -103,7 +103,7 @@ declare class WebsocketLinkTransport<T extends ClientContext> implements Standar
|
|
|
103
103
|
private readonly reconnectOnCloseDelay;
|
|
104
104
|
private readonly encodePeerMessageOptions;
|
|
105
105
|
private readonly decodePeerMessageOptions;
|
|
106
|
-
constructor(options:
|
|
106
|
+
constructor(options: WebSocketLinkTransportOptions<T>);
|
|
107
107
|
send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
108
108
|
private totalAttempt;
|
|
109
109
|
private attempt;
|
|
@@ -111,11 +111,16 @@ declare class WebsocketLinkTransport<T extends ClientContext> implements Standar
|
|
|
111
111
|
private getConnectedPeer;
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>,
|
|
114
|
+
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, WebSocketLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
115
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Client link that communicates with an RPC Handler over a WebSocket connection.
|
|
118
|
+
*
|
|
119
|
+
* @see {@link https://orpc.dev/docs/adapters/websocket | WebSocket Adapters}
|
|
120
|
+
*/
|
|
116
121
|
declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
117
122
|
constructor(options: RPCLinkOptions<T>);
|
|
118
123
|
}
|
|
119
124
|
|
|
120
|
-
export { RPCLink,
|
|
121
|
-
export type { RPCLinkOptions, WebSocketLike,
|
|
125
|
+
export { RPCLink, WebSocketLinkTransport };
|
|
126
|
+
export type { RPCLinkOptions, WebSocketLike, WebSocketLinkTransportAttemptInfo, WebSocketLinkTransportOptions, WebSocketLinkTransportReconnectOptions };
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ClientContext, ClientOptions } from '../../index.js';
|
|
2
2
|
import { Promisable } from '@orpc/shared';
|
|
3
|
-
import { StandardRequest, StandardLazyResponse } from '@
|
|
4
|
-
import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@
|
|
5
|
-
import {
|
|
3
|
+
import { StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
4
|
+
import { EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standard-server/peer';
|
|
5
|
+
import { StandardLinkTransport, StandardLink, StandardLinkOptions } from '../standard/index.js';
|
|
6
6
|
import { RPCLinkCodecOptions } from '../standard/index.js';
|
|
7
|
-
import '../../shared/client.
|
|
7
|
+
import '../../shared/client.n8K9dFhY.js';
|
|
8
8
|
|
|
9
9
|
type WebSocketLike = Pick<WebSocket, 'addEventListener' | 'removeEventListener' | 'send' | 'readyState'>;
|
|
10
|
-
interface
|
|
10
|
+
interface WebSocketLinkTransportAttemptInfo {
|
|
11
11
|
/**
|
|
12
12
|
* Total number of connection attempts for this transport's lifetime.
|
|
13
13
|
* Starts at 1 on the first attempt, increments on every subsequent
|
|
@@ -21,7 +21,7 @@ interface WebsocketLinkTransportAttemptInfo {
|
|
|
21
21
|
*/
|
|
22
22
|
attempt: number;
|
|
23
23
|
}
|
|
24
|
-
interface
|
|
24
|
+
interface WebSocketLinkTransportReconnectOptions {
|
|
25
25
|
/**
|
|
26
26
|
* Whether to automatically reconnect when the connection is lost.
|
|
27
27
|
*
|
|
@@ -33,7 +33,7 @@ interface WebsocketLinkTransportReconnectOptions {
|
|
|
33
33
|
*
|
|
34
34
|
* @default info => info.attempt === 1 ? 0 : 2_000
|
|
35
35
|
*/
|
|
36
|
-
delay?: undefined | ((info:
|
|
36
|
+
delay?: undefined | ((info: WebSocketLinkTransportAttemptInfo) => number);
|
|
37
37
|
/**
|
|
38
38
|
* Maximum number of consecutive failed attempts before giving up.
|
|
39
39
|
* When exceeded, `getConnectedPeer` throws instead of retrying.
|
|
@@ -66,12 +66,12 @@ interface WebsocketLinkTransportReconnectOptions {
|
|
|
66
66
|
delay?: number;
|
|
67
67
|
};
|
|
68
68
|
}
|
|
69
|
-
interface
|
|
69
|
+
interface WebSocketLinkTransportOptions<_T extends ClientContext> {
|
|
70
70
|
/**
|
|
71
71
|
* Returns a WebSocket instance for peer communication.
|
|
72
72
|
* Can be async for lazy resolution.
|
|
73
73
|
*/
|
|
74
|
-
connect: (info:
|
|
74
|
+
connect: (info: WebSocketLinkTransportAttemptInfo) => Promisable<WebSocketLike>;
|
|
75
75
|
/**
|
|
76
76
|
* Whether to connect immediately on initialization, instead of waiting
|
|
77
77
|
* for the first call. Reduces latency for the first request.
|
|
@@ -84,7 +84,7 @@ interface WebsocketLinkTransportOptions<_T extends ClientContext> {
|
|
|
84
84
|
*
|
|
85
85
|
* @default { enabled: false }
|
|
86
86
|
*/
|
|
87
|
-
reconnect?: undefined |
|
|
87
|
+
reconnect?: undefined | WebSocketLinkTransportReconnectOptions;
|
|
88
88
|
/**
|
|
89
89
|
* Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
|
|
90
90
|
*/
|
|
@@ -94,7 +94,7 @@ interface WebsocketLinkTransportOptions<_T extends ClientContext> {
|
|
|
94
94
|
*/
|
|
95
95
|
decodePeerMessage?: DecodePeerMessageOptions | undefined;
|
|
96
96
|
}
|
|
97
|
-
declare class
|
|
97
|
+
declare class WebSocketLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
|
|
98
98
|
private readonly connect;
|
|
99
99
|
private readonly reconnectEnabled;
|
|
100
100
|
private readonly reconnectDelay;
|
|
@@ -103,7 +103,7 @@ declare class WebsocketLinkTransport<T extends ClientContext> implements Standar
|
|
|
103
103
|
private readonly reconnectOnCloseDelay;
|
|
104
104
|
private readonly encodePeerMessageOptions;
|
|
105
105
|
private readonly decodePeerMessageOptions;
|
|
106
|
-
constructor(options:
|
|
106
|
+
constructor(options: WebSocketLinkTransportOptions<T>);
|
|
107
107
|
send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
108
108
|
private totalAttempt;
|
|
109
109
|
private attempt;
|
|
@@ -111,11 +111,16 @@ declare class WebsocketLinkTransport<T extends ClientContext> implements Standar
|
|
|
111
111
|
private getConnectedPeer;
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
-
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>,
|
|
114
|
+
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, WebSocketLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
115
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Client link that communicates with an RPC Handler over a WebSocket connection.
|
|
118
|
+
*
|
|
119
|
+
* @see {@link https://orpc.dev/docs/adapters/websocket | WebSocket Adapters}
|
|
120
|
+
*/
|
|
116
121
|
declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
117
122
|
constructor(options: RPCLinkOptions<T>);
|
|
118
123
|
}
|
|
119
124
|
|
|
120
|
-
export { RPCLink,
|
|
121
|
-
export type { RPCLinkOptions, WebSocketLike,
|
|
125
|
+
export { RPCLink, WebSocketLinkTransport };
|
|
126
|
+
export type { RPCLinkOptions, WebSocketLike, WebSocketLinkTransportAttemptInfo, WebSocketLinkTransportOptions, WebSocketLinkTransportReconnectOptions };
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { runWithSignal, AbortError, sleep, promiseWithResolvers, toStringOrBytes } from '@orpc/shared';
|
|
2
|
-
import { ClientPeer, encodePeerMessage, decodePeerMessage, isServerPeerSendMessage } from '@
|
|
3
|
-
import { S as StandardLink, R as RPCLinkCodec } from '../../shared/client.
|
|
4
|
-
import '@
|
|
5
|
-
import '@
|
|
6
|
-
import '../../shared/client.
|
|
7
|
-
import '../../shared/client.
|
|
1
|
+
import { runWithSignal, AbortError, sleep, promiseWithResolvers, sequential, loadBytes, toStringOrBytes } from '@orpc/shared';
|
|
2
|
+
import { ClientPeer, encodePeerMessage, decodePeerMessage, isServerPeerSendMessage } from '@standard-server/peer';
|
|
3
|
+
import { S as StandardLink, R as RPCLinkCodec } from '../../shared/client.VXsLhJtT.mjs';
|
|
4
|
+
import '@standard-server/core';
|
|
5
|
+
import '@standard-server/fetch';
|
|
6
|
+
import '../../shared/client.BYHnU_Z3.mjs';
|
|
7
|
+
import '../../shared/client.Dtv1JPkU.mjs';
|
|
8
8
|
|
|
9
9
|
const WEBSOCKET_CONNECTING = 0;
|
|
10
10
|
const WEBSOCKET_OPEN = 1;
|
|
11
|
-
class
|
|
11
|
+
class WebSocketLinkTransport {
|
|
12
12
|
connect;
|
|
13
13
|
reconnectEnabled;
|
|
14
14
|
reconnectDelay;
|
|
@@ -71,13 +71,13 @@ class WebsocketLinkTransport {
|
|
|
71
71
|
connectingResolvers?.resolve();
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
|
-
websocket.addEventListener("message", async (event) => {
|
|
75
|
-
const message = await toStringOrBytes(event.data);
|
|
74
|
+
websocket.addEventListener("message", sequential(async (event) => {
|
|
75
|
+
const message = event.data instanceof Blob ? await loadBytes(event.data) : toStringOrBytes(event.data);
|
|
76
76
|
const result = decodePeerMessage(message, this.decodePeerMessageOptions);
|
|
77
77
|
if (result.matched && isServerPeerSendMessage(result.message)) {
|
|
78
|
-
|
|
78
|
+
peer.message(result.message);
|
|
79
79
|
}
|
|
80
|
-
});
|
|
80
|
+
}));
|
|
81
81
|
websocket.addEventListener("close", async (event) => {
|
|
82
82
|
connectingResolvers?.resolve();
|
|
83
83
|
if (this.reconnectOnCloseEnabled) {
|
|
@@ -102,9 +102,9 @@ class WebsocketLinkTransport {
|
|
|
102
102
|
class RPCLink extends StandardLink {
|
|
103
103
|
constructor(options) {
|
|
104
104
|
const codec = new RPCLinkCodec(options);
|
|
105
|
-
const transport = new
|
|
105
|
+
const transport = new WebSocketLinkTransport(options);
|
|
106
106
|
super(codec, transport, options);
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
export { RPCLink,
|
|
110
|
+
export { RPCLink, WebSocketLinkTransport };
|