@orpc/client 2.0.0-beta.47 → 2.0.0-beta.48
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 +2 -3
- package/dist/adapters/fetch/index.d.mts +2 -48
- package/dist/adapters/fetch/index.mjs +2 -77
- package/dist/adapters/fetch/rpc-link.d.mts +13 -0
- package/dist/adapters/fetch/rpc-link.mjs +10 -0
- package/dist/adapters/fetch/transport.d.mts +31 -0
- package/dist/adapters/fetch/transport.mjs +65 -0
- package/dist/adapters/message-port/index.d.mts +3 -80
- package/dist/adapters/message-port/index.mjs +3 -101
- package/dist/adapters/message-port/message-port.d.mts +29 -0
- package/dist/adapters/message-port/message-port.mjs +43 -0
- package/dist/adapters/message-port/rpc-link.d.mts +14 -0
- package/dist/adapters/message-port/rpc-link.mjs +10 -0
- package/dist/adapters/message-port/transport.d.mts +35 -0
- package/dist/adapters/message-port/transport.mjs +47 -0
- package/dist/adapters/standard/codec.d.mts +14 -0
- package/dist/adapters/standard/codec.mjs +1 -0
- package/dist/adapters/standard/index.d.mts +5 -60
- package/dist/adapters/standard/index.mjs +4 -6
- package/dist/adapters/standard/link.d.mts +40 -0
- package/dist/adapters/standard/link.mjs +79 -0
- package/dist/adapters/standard/plugin.d.mts +32 -0
- package/dist/adapters/standard/plugin.mjs +16 -0
- package/dist/adapters/standard/rpc-link-codec.d.mts +55 -0
- package/dist/adapters/standard/rpc-link-codec.mjs +104 -0
- package/dist/adapters/standard/transport.d.mts +14 -0
- package/dist/adapters/standard/transport.mjs +1 -0
- package/dist/adapters/websocket/index.d.mts +2 -136
- package/dist/adapters/websocket/index.mjs +2 -131
- package/dist/adapters/websocket/rpc-link.d.mts +13 -0
- package/dist/adapters/websocket/rpc-link.mjs +10 -0
- package/dist/adapters/websocket/transport.d.mts +119 -0
- package/dist/adapters/websocket/transport.mjs +123 -0
- package/dist/async-iterator-object.d.mts +7 -0
- package/dist/async-iterator-object.mjs +30 -0
- package/dist/client-safe.d.mts +16 -0
- package/dist/client-safe.mjs +23 -0
- package/dist/client.d.mts +51 -0
- package/dist/client.mjs +32 -0
- package/dist/consts.d.mts +10 -0
- package/dist/consts.mjs +10 -0
- package/dist/dynamic-link.d.mts +13 -0
- package/dist/dynamic-link.mjs +12 -0
- package/dist/error-utils.d.mts +24 -0
- package/dist/error-utils.mjs +44 -0
- package/dist/error.d.mts +138 -0
- package/dist/error.mjs +105 -0
- package/dist/index.d.mts +42 -137
- package/dist/index.mjs +18 -142
- package/dist/plugins/batch.d.mts +150 -0
- package/dist/plugins/batch.mjs +339 -0
- package/dist/plugins/cookie-jar.d.mts +82 -0
- package/dist/plugins/cookie-jar.mjs +229 -0
- package/dist/plugins/dedupe.d.mts +58 -0
- package/dist/plugins/dedupe.mjs +199 -0
- package/dist/plugins/index.d.mts +28 -420
- package/dist/plugins/index.mjs +17 -971
- package/dist/plugins/request-compression.d.mts +44 -0
- package/dist/plugins/request-compression.mjs +150 -0
- package/dist/plugins/response-compression.d.mts +28 -0
- package/dist/plugins/response-compression.mjs +78 -0
- package/dist/plugins/retry-after.d.mts +41 -0
- package/dist/plugins/retry-after.mjs +71 -0
- package/dist/plugins/retry.d.mts +82 -0
- package/dist/plugins/retry.mjs +127 -0
- package/dist/plugins/timeout.d.mts +25 -0
- package/dist/plugins/timeout.mjs +36 -0
- package/dist/rpc-json-serializer.d.mts +99 -0
- package/dist/rpc-json-serializer.mjs +365 -0
- package/dist/rpc-serializer.d.mts +32 -0
- package/dist/rpc-serializer.mjs +100 -0
- package/dist/types.d.mts +93 -0
- package/dist/types.mjs +1 -0
- package/dist/utils.d.mts +46 -0
- package/dist/utils.mjs +53 -0
- package/package.json +5 -5
- package/dist/adapters/fetch/index.d.ts +0 -48
- package/dist/adapters/message-port/index.d.ts +0 -80
- package/dist/adapters/standard/index.d.ts +0 -60
- package/dist/adapters/websocket/index.d.ts +0 -136
- package/dist/index.d.ts +0 -144
- package/dist/plugins/index.d.ts +0 -420
- package/dist/shared/client.BoHJeq15.d.mts +0 -233
- package/dist/shared/client.BoHJeq15.d.ts +0 -233
- package/dist/shared/client.CKQeJJ2U.mjs +0 -172
- package/dist/shared/client.DqIWeNhQ.d.mts +0 -131
- package/dist/shared/client.DqIWeNhQ.d.ts +0 -131
- package/dist/shared/client.JBJwBhR8.d.ts +0 -90
- package/dist/shared/client.LF04eOvL.mjs +0 -115
- package/dist/shared/client._wG5INmC.mjs +0 -505
- package/dist/shared/client.euYjjcFz.d.mts +0 -90
package/README.md
CHANGED
|
@@ -191,15 +191,14 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
191
191
|
<td align="center"><a href="https://kellychan.im/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
192
192
|
<td align="center"><a href="https://guyariely.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
|
|
193
193
|
<td align="center"><a href="https://paulsenon.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
|
|
194
|
-
<td align="center"><a href="https://
|
|
194
|
+
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
195
195
|
</tr>
|
|
196
196
|
<tr>
|
|
197
|
-
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
198
197
|
<td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
|
|
199
198
|
</tr>
|
|
200
199
|
</table>
|
|
201
200
|
|
|
202
|
-
With thanks to [
|
|
201
|
+
With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
|
|
203
202
|
|
|
204
203
|
## Open Source Programs
|
|
205
204
|
|
|
@@ -1,48 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { StandardRequest, StandardLazyResponse } from '@standard-server/core';
|
|
4
|
-
import { ToFetchBodyOptions } from '@standard-server/fetch';
|
|
5
|
-
import { StandardLinkTransport, StandardLink, StandardLinkOptions } from '../standard/index.mjs';
|
|
6
|
-
import { RPCLinkCodecOptions } from '../standard/index.mjs';
|
|
7
|
-
import '../../shared/client.DqIWeNhQ.mjs';
|
|
8
|
-
|
|
9
|
-
interface FetchLinkTransportOptions<T extends ClientContext> {
|
|
10
|
-
/**
|
|
11
|
-
* The origin to prepend to all request URLs, useful for CORS requests.
|
|
12
|
-
*
|
|
13
|
-
* @example 'https://api.example.com'
|
|
14
|
-
* @example 'http://localhost:3000'
|
|
15
|
-
*/
|
|
16
|
-
origin?: Value<Promisable<`https://${string}` | `http://${string}` | ({} & string) | undefined>, [options: ClientOptions<T>, path: string[]]> | undefined;
|
|
17
|
-
/**
|
|
18
|
-
* Options for how to convert the Standard Request to a Fetch Request, like event stream options, etc.
|
|
19
|
-
*/
|
|
20
|
-
toFetchRequest?: ToFetchBodyOptions | undefined;
|
|
21
|
-
/**
|
|
22
|
-
* Override the default fetch implementation.
|
|
23
|
-
*
|
|
24
|
-
* @default (url, init) => globalThis.fetch(url, init)
|
|
25
|
-
*/
|
|
26
|
-
fetch?(url: string, init: RequestInit, options: ClientOptions<T>, path: string[]): Promise<Response>;
|
|
27
|
-
}
|
|
28
|
-
declare class FetchLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
|
|
29
|
-
private readonly origin;
|
|
30
|
-
private readonly fetch;
|
|
31
|
-
private readonly toFetchRequestOptions;
|
|
32
|
-
constructor(options: FetchLinkTransportOptions<T>);
|
|
33
|
-
send(standardRequest: StandardRequest, path: string[], options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, FetchLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Client link that communicates with an RPC Handler over the Fetch API (HTTP).
|
|
40
|
-
*
|
|
41
|
-
* @see {@link https://orpc.dev/docs/adapters/fetch-api | Fetch API Adapter}
|
|
42
|
-
*/
|
|
43
|
-
declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
44
|
-
constructor(options: RPCLinkOptions<T>);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export { FetchLinkTransport, RPCLink };
|
|
48
|
-
export type { FetchLinkTransportOptions, RPCLinkOptions };
|
|
1
|
+
export * from "./rpc-link.mjs";
|
|
2
|
+
export * from "./transport.mjs";
|
|
@@ -1,77 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { S as StandardLink, R as RPCLinkCodec } from '../../shared/client.CKQeJJ2U.mjs';
|
|
4
|
-
import '@standard-server/core';
|
|
5
|
-
import '../../shared/client.LF04eOvL.mjs';
|
|
6
|
-
import '../../shared/client._wG5INmC.mjs';
|
|
7
|
-
|
|
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
|
-
}
|
|
20
|
-
}
|
|
21
|
-
);
|
|
22
|
-
return duplex;
|
|
23
|
-
} catch {
|
|
24
|
-
return void 0;
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
class FetchLinkTransport {
|
|
28
|
-
origin;
|
|
29
|
-
fetch;
|
|
30
|
-
toFetchRequestOptions;
|
|
31
|
-
constructor(options) {
|
|
32
|
-
this.origin = options.origin;
|
|
33
|
-
this.fetch = options.fetch ?? ((url, init) => globalThis.fetch(url, init));
|
|
34
|
-
this.toFetchRequestOptions = options.toFetchRequest;
|
|
35
|
-
}
|
|
36
|
-
async send(standardRequest, path, options) {
|
|
37
|
-
let origin = await value(this.origin, options, path);
|
|
38
|
-
if (origin?.endsWith("/")) {
|
|
39
|
-
origin = origin.slice(0, -1);
|
|
40
|
-
}
|
|
41
|
-
const url = `${origin ?? ""}${standardRequest.url}`;
|
|
42
|
-
const [body, standardHeaders] = toFetchBody(standardRequest.body, standardRequest.headers, this.toFetchRequestOptions);
|
|
43
|
-
const init = {
|
|
44
|
-
body,
|
|
45
|
-
headers: toFetchHeaders(standardHeaders),
|
|
46
|
-
method: standardRequest.method,
|
|
47
|
-
signal: options.signal,
|
|
48
|
-
redirect: "manual"
|
|
49
|
-
};
|
|
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 fetch = this.fetch;
|
|
57
|
-
const response = await fetch(url, init, options, path);
|
|
58
|
-
if (response.type === "opaqueredirect") {
|
|
59
|
-
throw new TypeError("Unexpected redirect response: redirects are not followed.");
|
|
60
|
-
}
|
|
61
|
-
if (response.type === "opaque") {
|
|
62
|
-
throw new TypeError("Unexpected opaque response: the response is unreadable, likely due to a no-cors request.");
|
|
63
|
-
}
|
|
64
|
-
const standardResponse = toStandardLazyResponse(response);
|
|
65
|
-
return standardResponse;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
class RPCLink extends StandardLink {
|
|
70
|
-
constructor(options) {
|
|
71
|
-
const codec = new RPCLinkCodec(options);
|
|
72
|
-
const transport = new FetchLinkTransport(options);
|
|
73
|
-
super(codec, transport, options);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export { FetchLinkTransport, RPCLink };
|
|
1
|
+
export * from "./rpc-link.mjs";
|
|
2
|
+
export * from "./transport.mjs";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ClientContext } from "../../types.mjs";
|
|
2
|
+
import type { RPCLinkCodecOptions, StandardLinkOptions } from "../standard/index.mjs";
|
|
3
|
+
import type { FetchLinkTransportOptions } from "./transport.mjs";
|
|
4
|
+
import { StandardLink } from "../standard/index.mjs";
|
|
5
|
+
export interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, FetchLinkTransportOptions<T>, RPCLinkCodecOptions<T> {}
|
|
6
|
+
/**
|
|
7
|
+
* Client link that communicates with an RPC Handler over the Fetch API (HTTP).
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://orpc.dev/docs/adapters/fetch-api | Fetch API Adapter}
|
|
10
|
+
*/
|
|
11
|
+
export declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
12
|
+
constructor(options: RPCLinkOptions<T>);
|
|
13
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RPCLinkCodec, StandardLink } from "../standard/index.mjs";
|
|
2
|
+
import { FetchLinkTransport } from "./transport.mjs";
|
|
3
|
+
|
|
4
|
+
export class RPCLink extends StandardLink {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
const codec = new RPCLinkCodec(options);
|
|
7
|
+
const transport = new FetchLinkTransport(options);
|
|
8
|
+
super(codec, transport, options);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Promisable, Value } from "@orpc/shared";
|
|
2
|
+
import type { StandardLazyResponse, StandardRequest } from "@standard-server/core";
|
|
3
|
+
import type { ToFetchBodyOptions } from "@standard-server/fetch";
|
|
4
|
+
import type { ClientContext, ClientOptions } from "../../types.mjs";
|
|
5
|
+
import type { StandardLinkTransport } from "../standard/index.mjs";
|
|
6
|
+
export interface FetchLinkTransportOptions<T extends ClientContext> {
|
|
7
|
+
/**
|
|
8
|
+
* The origin to prepend to all request URLs, useful for CORS requests.
|
|
9
|
+
*
|
|
10
|
+
* @example 'https://api.example.com'
|
|
11
|
+
* @example 'http://localhost:3000'
|
|
12
|
+
*/
|
|
13
|
+
origin?: Value<Promisable<`https://${string}` | `http://${string}` | {} & string | undefined>, [options: ClientOptions<T>, path: string[]]> | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Options for how to convert the Standard Request to a Fetch Request, like event stream options, etc.
|
|
16
|
+
*/
|
|
17
|
+
toFetchRequest?: ToFetchBodyOptions | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Override the default fetch implementation.
|
|
20
|
+
*
|
|
21
|
+
* @default (url, init) => globalThis.fetch(url, init)
|
|
22
|
+
*/
|
|
23
|
+
fetch?(url: string, init: RequestInit, options: ClientOptions<T>, path: string[]): Promise<Response>;
|
|
24
|
+
}
|
|
25
|
+
export declare class FetchLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
|
|
26
|
+
private readonly origin;
|
|
27
|
+
private readonly fetch;
|
|
28
|
+
private readonly toFetchRequestOptions;
|
|
29
|
+
constructor(options: FetchLinkTransportOptions<T>);
|
|
30
|
+
send(standardRequest: StandardRequest, path: string[], options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { once, value } from "@orpc/shared";
|
|
2
|
+
import { toFetchBody, toFetchHeaders, toStandardLazyResponse } from "@standard-server/fetch";
|
|
3
|
+
const GET_SUPPORTED_DUPLEX_MODE = /* @__PURE__ */ once(() => {
|
|
4
|
+
|
|
5
|
+
try {
|
|
6
|
+
let duplex;
|
|
7
|
+
void new Request("https://example.com", {
|
|
8
|
+
method: "POST",
|
|
9
|
+
body: new ReadableStream(),
|
|
10
|
+
get duplex() {
|
|
11
|
+
duplex = "half";
|
|
12
|
+
return "half";
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
return duplex;
|
|
16
|
+
} catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
export class FetchLinkTransport {
|
|
21
|
+
origin;
|
|
22
|
+
fetch;
|
|
23
|
+
toFetchRequestOptions;
|
|
24
|
+
constructor(options) {
|
|
25
|
+
this.origin = options.origin;
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
this.fetch = options.fetch ?? ((url, init) => globalThis.fetch(url, init));
|
|
29
|
+
this.toFetchRequestOptions = options.toFetchRequest;
|
|
30
|
+
}
|
|
31
|
+
async send(standardRequest, path, options) {
|
|
32
|
+
let origin = await value(this.origin, options, path);
|
|
33
|
+
if (origin?.endsWith("/")) {
|
|
34
|
+
origin = origin.slice(0, -1);
|
|
35
|
+
}
|
|
36
|
+
const url = `${origin ?? ""}${standardRequest.url}`;
|
|
37
|
+
const [body, standardHeaders] = toFetchBody(standardRequest.body, standardRequest.headers, this.toFetchRequestOptions);
|
|
38
|
+
const init = {
|
|
39
|
+
body,
|
|
40
|
+
headers: toFetchHeaders(standardHeaders),
|
|
41
|
+
method: standardRequest.method,
|
|
42
|
+
signal: options.signal,
|
|
43
|
+
redirect: "manual"
|
|
44
|
+
};
|
|
45
|
+
if (body instanceof ReadableStream) {
|
|
46
|
+
const duplex = GET_SUPPORTED_DUPLEX_MODE();
|
|
47
|
+
if (duplex !== undefined) {
|
|
48
|
+
init.duplex = duplex;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const fetch = this.fetch;
|
|
53
|
+
const response = await fetch(url, init, options, path);
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
if (response.type === "opaqueredirect") {
|
|
57
|
+
throw new TypeError("Unexpected redirect response: redirects are not followed.");
|
|
58
|
+
}
|
|
59
|
+
if (response.type === "opaque") {
|
|
60
|
+
throw new TypeError("Unexpected opaque response: the response is unreadable, likely due to a no-cors request.");
|
|
61
|
+
}
|
|
62
|
+
const standardResponse = toStandardLazyResponse(response);
|
|
63
|
+
return standardResponse;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -1,80 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { ClientPeer, EncodePeerMessageOptions, DecodePeerMessageOptions } from '@standard-server/peer';
|
|
5
|
-
import { StandardLinkTransport, StandardLink, StandardLinkOptions } from '../standard/index.mjs';
|
|
6
|
-
import { RPCLinkCodecOptions } from '../standard/index.mjs';
|
|
7
|
-
import '../../shared/client.DqIWeNhQ.mjs';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* The message port used by electron in main process
|
|
11
|
-
*/
|
|
12
|
-
interface MessagePortMainLike {
|
|
13
|
-
on: <T extends string>(event: T, callback: (event?: {
|
|
14
|
-
data: any;
|
|
15
|
-
}) => void) => void;
|
|
16
|
-
postMessage: (data: any, transfer?: any[]) => void;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* The message port used by browser extension
|
|
20
|
-
*/
|
|
21
|
-
interface BrowserPortLike {
|
|
22
|
-
onMessage: {
|
|
23
|
-
addListener: (callback: (data: any) => void) => void;
|
|
24
|
-
};
|
|
25
|
-
onDisconnect: {
|
|
26
|
-
addListener: (callback: () => void) => void;
|
|
27
|
-
};
|
|
28
|
-
postMessage: (data: any) => void;
|
|
29
|
-
}
|
|
30
|
-
type SupportedMessagePort = Pick<MessagePort, 'addEventListener' | 'postMessage'> | MessagePortMainLike | BrowserPortLike;
|
|
31
|
-
/**
|
|
32
|
-
* Message port can support [The structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
|
|
33
|
-
*/
|
|
34
|
-
type SupportedMessagePortData = any;
|
|
35
|
-
declare function postMessagePortMessage(port: SupportedMessagePort, data: SupportedMessagePortData, transfer?: any[]): void;
|
|
36
|
-
declare function onMessagePortMessage(port: SupportedMessagePort, callback: (data: SupportedMessagePortData) => void): void;
|
|
37
|
-
declare function onMessagePortClose(port: SupportedMessagePort, callback: () => void): void;
|
|
38
|
-
|
|
39
|
-
type DecodedRequestMessage = ConstructorParameters<typeof ClientPeer>[0] extends (message: infer TMessage) => unknown ? TMessage : never;
|
|
40
|
-
interface MessagePortLinkTransportOptions<_T extends ClientContext> {
|
|
41
|
-
port: SupportedMessagePort;
|
|
42
|
-
/**
|
|
43
|
-
* By default, oRPC serializes request/response messages to string/binary data before sending over message port.
|
|
44
|
-
* If needed, define this option to utilize full power of [MessagePort: postMessage() method](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/postMessage),
|
|
45
|
-
* such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
|
|
46
|
-
*
|
|
47
|
-
* @remarks
|
|
48
|
-
* **Note**: Returning `null` or `undefined` disables this feature.
|
|
49
|
-
* **Warning**: Make sure your message port supports `transfer` before using this feature.
|
|
50
|
-
*/
|
|
51
|
-
experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedRequestMessage, port: SupportedMessagePort]> | undefined;
|
|
52
|
-
/**
|
|
53
|
-
* Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
|
|
54
|
-
*/
|
|
55
|
-
encodePeerMessage?: EncodePeerMessageOptions | undefined;
|
|
56
|
-
/**
|
|
57
|
-
* Options for decoding peer messages, such as `prefix` for distinguishing messages on the same channel.
|
|
58
|
-
*/
|
|
59
|
-
decodePeerMessage?: DecodePeerMessageOptions | undefined;
|
|
60
|
-
}
|
|
61
|
-
declare class MessagePortLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
|
|
62
|
-
private readonly peer;
|
|
63
|
-
constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions }: MessagePortLinkTransportOptions<T>);
|
|
64
|
-
send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, MessagePortLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
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
|
-
*/
|
|
75
|
-
declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
76
|
-
constructor(options: RPCLinkOptions<T>);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export { MessagePortLinkTransport, RPCLink, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
|
|
80
|
-
export type { BrowserPortLike, MessagePortLinkTransportOptions, MessagePortMainLike, RPCLinkOptions, SupportedMessagePort, SupportedMessagePortData };
|
|
1
|
+
export * from "./message-port.mjs";
|
|
2
|
+
export * from "./rpc-link.mjs";
|
|
3
|
+
export * from "./transport.mjs";
|
|
@@ -1,101 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import '@standard-server/core';
|
|
5
|
-
import '@standard-server/fetch';
|
|
6
|
-
import '../../shared/client.LF04eOvL.mjs';
|
|
7
|
-
import '../../shared/client._wG5INmC.mjs';
|
|
8
|
-
|
|
9
|
-
function postMessagePortMessage(port, data, transfer) {
|
|
10
|
-
if (transfer) {
|
|
11
|
-
port.postMessage(data, transfer);
|
|
12
|
-
} else {
|
|
13
|
-
port.postMessage(data);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function onMessagePortMessage(port, callback) {
|
|
17
|
-
if ("addEventListener" in port) {
|
|
18
|
-
port.addEventListener("message", (event) => {
|
|
19
|
-
callback(event.data);
|
|
20
|
-
});
|
|
21
|
-
} else if ("on" in port) {
|
|
22
|
-
port.on("message", (event) => {
|
|
23
|
-
callback(event?.data);
|
|
24
|
-
});
|
|
25
|
-
} else if ("onMessage" in port) {
|
|
26
|
-
port.onMessage.addListener((data) => {
|
|
27
|
-
callback(data);
|
|
28
|
-
});
|
|
29
|
-
} else {
|
|
30
|
-
throw new Error("Cannot find a addEventListener/on/onMessage method on the port");
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function onMessagePortClose(port, callback) {
|
|
34
|
-
if ("addEventListener" in port) {
|
|
35
|
-
port.addEventListener("close", () => {
|
|
36
|
-
callback();
|
|
37
|
-
});
|
|
38
|
-
} else if ("on" in port) {
|
|
39
|
-
port.on("close", () => {
|
|
40
|
-
callback();
|
|
41
|
-
});
|
|
42
|
-
} else if ("onDisconnect" in port) {
|
|
43
|
-
port.onDisconnect.addListener(() => {
|
|
44
|
-
callback();
|
|
45
|
-
});
|
|
46
|
-
} else {
|
|
47
|
-
throw new Error("Cannot find a addEventListener/on/onDisconnect method on the port");
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
class MessagePortLinkTransport {
|
|
52
|
-
peer;
|
|
53
|
-
constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions }) {
|
|
54
|
-
let closeReason;
|
|
55
|
-
this.peer = new ClientPeer(async (message) => {
|
|
56
|
-
if (closeReason) {
|
|
57
|
-
throw closeReason;
|
|
58
|
-
}
|
|
59
|
-
const transfer = await value(experimental_transfer, message, port);
|
|
60
|
-
if (transfer) {
|
|
61
|
-
postMessagePortMessage(port, message, transfer);
|
|
62
|
-
} else {
|
|
63
|
-
postMessagePortMessage(port, await encodePeerMessage(message, encodePeerMessageOptions));
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
onMessagePortMessage(port, async (data) => {
|
|
67
|
-
let peerMessage;
|
|
68
|
-
if (typeof data === "string" || data instanceof Uint8Array) {
|
|
69
|
-
const result = decodePeerMessage(data, decodePeerMessageOptions);
|
|
70
|
-
if (result.matched && isServerPeerSendMessage(result.message)) {
|
|
71
|
-
peerMessage = result.message;
|
|
72
|
-
}
|
|
73
|
-
} else if (isPeerMessage(data) && isServerPeerSendMessage(data)) {
|
|
74
|
-
peerMessage = data;
|
|
75
|
-
}
|
|
76
|
-
if (peerMessage === void 0) {
|
|
77
|
-
return { matched: false };
|
|
78
|
-
}
|
|
79
|
-
await this.peer.message(peerMessage);
|
|
80
|
-
return { matched: true };
|
|
81
|
-
});
|
|
82
|
-
onMessagePortClose(port, async () => {
|
|
83
|
-
closeReason = new AbortError("MessagePort closed");
|
|
84
|
-
await this.peer.close(closeReason);
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
async send(standardRequest, _path, _options) {
|
|
88
|
-
const standardResponse = await this.peer.request(standardRequest);
|
|
89
|
-
return standardResponse;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
class RPCLink extends StandardLink {
|
|
94
|
-
constructor(options) {
|
|
95
|
-
const codec = new RPCLinkCodec(options);
|
|
96
|
-
const transport = new MessagePortLinkTransport(options);
|
|
97
|
-
super(codec, transport, options);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export { MessagePortLinkTransport, RPCLink, onMessagePortClose, onMessagePortMessage, postMessagePortMessage };
|
|
1
|
+
export * from "./message-port.mjs";
|
|
2
|
+
export * from "./rpc-link.mjs";
|
|
3
|
+
export * from "./transport.mjs";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The message port used by electron in main process
|
|
3
|
+
*/
|
|
4
|
+
export interface MessagePortMainLike {
|
|
5
|
+
on: <T extends string>(event: T, callback: (event?: {
|
|
6
|
+
data: any;
|
|
7
|
+
}) => void) => void;
|
|
8
|
+
postMessage: (data: any, transfer?: any[]) => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The message port used by browser extension
|
|
12
|
+
*/
|
|
13
|
+
export interface BrowserPortLike {
|
|
14
|
+
onMessage: {
|
|
15
|
+
addListener: (callback: (data: any) => void) => void;
|
|
16
|
+
};
|
|
17
|
+
onDisconnect: {
|
|
18
|
+
addListener: (callback: () => void) => void;
|
|
19
|
+
};
|
|
20
|
+
postMessage: (data: any) => void;
|
|
21
|
+
}
|
|
22
|
+
export type SupportedMessagePort = Pick<MessagePort, "addEventListener" | "postMessage"> | MessagePortMainLike | BrowserPortLike;
|
|
23
|
+
/**
|
|
24
|
+
* Message port can support [The structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
|
|
25
|
+
*/
|
|
26
|
+
export type SupportedMessagePortData = any;
|
|
27
|
+
export declare function postMessagePortMessage(port: SupportedMessagePort, data: SupportedMessagePortData, transfer?: any[]): void;
|
|
28
|
+
export declare function onMessagePortMessage(port: SupportedMessagePort, callback: (data: SupportedMessagePortData) => void): void;
|
|
29
|
+
export declare function onMessagePortClose(port: SupportedMessagePort, callback: () => void): void;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export function postMessagePortMessage(port, data, transfer) {
|
|
2
|
+
if (transfer) {
|
|
3
|
+
port.postMessage(data, transfer);
|
|
4
|
+
} else {
|
|
5
|
+
port.postMessage(data);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function onMessagePortMessage(port, callback) {
|
|
9
|
+
|
|
10
|
+
if ("addEventListener" in port) {
|
|
11
|
+
port.addEventListener("message", (event) => {
|
|
12
|
+
callback(event.data);
|
|
13
|
+
});
|
|
14
|
+
} else if ("on" in port) {
|
|
15
|
+
port.on("message", (event) => {
|
|
16
|
+
callback(event?.data);
|
|
17
|
+
});
|
|
18
|
+
} else if ("onMessage" in port) {
|
|
19
|
+
port.onMessage.addListener((data) => {
|
|
20
|
+
callback(data);
|
|
21
|
+
});
|
|
22
|
+
} else {
|
|
23
|
+
throw new Error("Cannot find a addEventListener/on/onMessage method on the port");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function onMessagePortClose(port, callback) {
|
|
27
|
+
|
|
28
|
+
if ("addEventListener" in port) {
|
|
29
|
+
port.addEventListener("close", () => {
|
|
30
|
+
callback();
|
|
31
|
+
});
|
|
32
|
+
} else if ("on" in port) {
|
|
33
|
+
port.on("close", () => {
|
|
34
|
+
callback();
|
|
35
|
+
});
|
|
36
|
+
} else if ("onDisconnect" in port) {
|
|
37
|
+
port.onDisconnect.addListener(() => {
|
|
38
|
+
callback();
|
|
39
|
+
});
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error("Cannot find a addEventListener/on/onDisconnect method on the port");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ClientContext } from "../../types.mjs";
|
|
2
|
+
import type { RPCLinkCodecOptions, StandardLinkOptions } from "../standard/index.mjs";
|
|
3
|
+
import type { MessagePortLinkTransportOptions } from "./transport.mjs";
|
|
4
|
+
import { StandardLink } from "../standard/index.mjs";
|
|
5
|
+
export interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, MessagePortLinkTransportOptions<T>, RPCLinkCodecOptions<T> {}
|
|
6
|
+
/**
|
|
7
|
+
* Client link that communicates with an RPC Handler over a Message Port
|
|
8
|
+
* (e.g. workers, iframes, browser extensions, Electron).
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
|
|
11
|
+
*/
|
|
12
|
+
export declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
13
|
+
constructor(options: RPCLinkOptions<T>);
|
|
14
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RPCLinkCodec, StandardLink } from "../standard/index.mjs";
|
|
2
|
+
import { MessagePortLinkTransport } from "./transport.mjs";
|
|
3
|
+
|
|
4
|
+
export class RPCLink extends StandardLink {
|
|
5
|
+
constructor(options) {
|
|
6
|
+
const codec = new RPCLinkCodec(options);
|
|
7
|
+
const transport = new MessagePortLinkTransport(options);
|
|
8
|
+
super(codec, transport, options);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Promisable, Value } from "@orpc/shared";
|
|
2
|
+
import type { StandardLazyResponse, StandardRequest } from "@standard-server/core";
|
|
3
|
+
import type { DecodePeerMessageOptions, EncodePeerMessageOptions } from "@standard-server/peer";
|
|
4
|
+
import type { ClientContext, ClientOptions } from "../../types.mjs";
|
|
5
|
+
import type { StandardLinkTransport } from "../standard/index.mjs";
|
|
6
|
+
import type { SupportedMessagePort } from "./message-port.mjs";
|
|
7
|
+
import { ClientPeer } from "@standard-server/peer";
|
|
8
|
+
type DecodedRequestMessage = ConstructorParameters<typeof ClientPeer>[0] extends (message: infer TMessage) => unknown ? TMessage : never;
|
|
9
|
+
export interface MessagePortLinkTransportOptions<_T extends ClientContext> {
|
|
10
|
+
port: SupportedMessagePort;
|
|
11
|
+
/**
|
|
12
|
+
* By default, oRPC serializes request/response messages to string/binary data before sending over message port.
|
|
13
|
+
* If needed, define this option to utilize full power of [MessagePort: postMessage() method](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort/postMessage),
|
|
14
|
+
* such as transferring ownership of objects to the other side or support unserializable objects like `OffscreenCanvas`.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* **Note**: Returning `null` or `undefined` disables this feature.
|
|
18
|
+
* **Warning**: Make sure your message port supports `transfer` before using this feature.
|
|
19
|
+
*/
|
|
20
|
+
experimental_transfer?: Value<Promisable<object[] | null | undefined>, [message: DecodedRequestMessage, port: SupportedMessagePort]> | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
|
|
23
|
+
*/
|
|
24
|
+
encodePeerMessage?: EncodePeerMessageOptions | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Options for decoding peer messages, such as `prefix` for distinguishing messages on the same channel.
|
|
27
|
+
*/
|
|
28
|
+
decodePeerMessage?: DecodePeerMessageOptions | undefined;
|
|
29
|
+
}
|
|
30
|
+
export declare class MessagePortLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
|
|
31
|
+
private readonly peer;
|
|
32
|
+
constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions }: MessagePortLinkTransportOptions<T>);
|
|
33
|
+
send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
34
|
+
}
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { AbortError, value } from "@orpc/shared";
|
|
2
|
+
import { ClientPeer, decodePeerMessage, encodePeerMessage, isPeerMessage, isServerPeerSendMessage } from "@standard-server/peer";
|
|
3
|
+
import { onMessagePortClose, onMessagePortMessage, postMessagePortMessage } from "./message-port.mjs";
|
|
4
|
+
export class MessagePortLinkTransport {
|
|
5
|
+
peer;
|
|
6
|
+
constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions }) {
|
|
7
|
+
let closeReason;
|
|
8
|
+
this.peer = new ClientPeer(async (message) => {
|
|
9
|
+
|
|
10
|
+
if (closeReason) {
|
|
11
|
+
throw closeReason;
|
|
12
|
+
}
|
|
13
|
+
const transfer = await value(experimental_transfer, message, port);
|
|
14
|
+
if (transfer) {
|
|
15
|
+
postMessagePortMessage(port, message, transfer);
|
|
16
|
+
} else {
|
|
17
|
+
postMessagePortMessage(port, await encodePeerMessage(message, encodePeerMessageOptions));
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
onMessagePortMessage(port, async (data) => {
|
|
21
|
+
let peerMessage;
|
|
22
|
+
if (typeof data === "string" || data instanceof Uint8Array) {
|
|
23
|
+
|
|
24
|
+
const result = decodePeerMessage(data, decodePeerMessageOptions);
|
|
25
|
+
if (result.matched && isServerPeerSendMessage(result.message)) {
|
|
26
|
+
peerMessage = result.message;
|
|
27
|
+
}
|
|
28
|
+
} else if (isPeerMessage(data) && isServerPeerSendMessage(data)) {
|
|
29
|
+
peerMessage = data;
|
|
30
|
+
}
|
|
31
|
+
if (peerMessage === undefined) {
|
|
32
|
+
return { matched: false };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
await this.peer.message(peerMessage);
|
|
36
|
+
return { matched: true };
|
|
37
|
+
});
|
|
38
|
+
onMessagePortClose(port, async () => {
|
|
39
|
+
closeReason = new AbortError("MessagePort closed");
|
|
40
|
+
await this.peer.close(closeReason);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async send(standardRequest, _path, _options) {
|
|
44
|
+
const standardResponse = await this.peer.request(standardRequest);
|
|
45
|
+
return standardResponse;
|
|
46
|
+
}
|
|
47
|
+
}
|