@orpc/client 2.0.0-beta.47 → 2.0.0-beta.49
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 +6 -7
- package/dist/adapters/fetch/index.d.ts +2 -48
- package/dist/adapters/fetch/index.js +2 -0
- package/dist/adapters/fetch/rpc-link.d.ts +14 -0
- package/dist/adapters/fetch/rpc-link.js +14 -0
- package/dist/adapters/fetch/transport.d.ts +34 -0
- package/dist/adapters/fetch/transport.js +65 -0
- package/dist/adapters/message-port/index.d.ts +3 -80
- package/dist/adapters/message-port/index.js +3 -0
- package/dist/adapters/message-port/message-port.d.ts +29 -0
- package/dist/adapters/message-port/message-port.js +56 -0
- package/dist/adapters/message-port/rpc-link.d.ts +15 -0
- package/dist/adapters/message-port/rpc-link.js +15 -0
- package/dist/adapters/message-port/transport.d.ts +38 -0
- package/dist/adapters/message-port/transport.js +52 -0
- package/dist/adapters/standard/codec.d.ts +14 -0
- package/dist/adapters/standard/codec.js +1 -0
- package/dist/adapters/standard/index.d.ts +5 -60
- package/dist/adapters/standard/index.js +4 -0
- package/dist/adapters/standard/link.d.ts +40 -0
- package/dist/adapters/standard/link.js +85 -0
- package/dist/adapters/standard/plugin.d.ts +32 -0
- package/dist/adapters/standard/plugin.js +16 -0
- package/dist/adapters/standard/{index.d.mts → rpc-link-codec.d.ts} +17 -14
- package/dist/adapters/standard/rpc-link-codec.js +104 -0
- package/dist/adapters/standard/transport.d.ts +14 -0
- package/dist/adapters/standard/transport.js +1 -0
- package/dist/adapters/websocket/index.d.ts +2 -136
- package/dist/adapters/websocket/index.js +2 -0
- package/dist/adapters/websocket/rpc-link.d.ts +14 -0
- package/dist/adapters/websocket/rpc-link.js +14 -0
- package/dist/adapters/websocket/{index.d.mts → transport.d.ts} +32 -53
- package/dist/adapters/websocket/transport.js +133 -0
- package/dist/async-iterator-object.d.ts +2 -0
- package/dist/async-iterator-object.js +29 -0
- package/dist/client-safe.d.ts +18 -0
- package/dist/client-safe.js +36 -0
- package/dist/client.d.ts +40 -0
- package/dist/client.js +39 -0
- package/dist/consts.d.ts +10 -0
- package/dist/consts.js +43 -0
- package/dist/dynamic-link.d.ts +13 -0
- package/dist/dynamic-link.js +17 -0
- package/dist/error-utils.d.ts +21 -0
- package/dist/error-utils.js +58 -0
- package/dist/error.d.ts +132 -0
- package/dist/error.js +178 -0
- package/dist/index.d.ts +38 -129
- package/dist/index.js +40 -0
- package/dist/plugins/batch.d.ts +185 -0
- package/dist/plugins/batch.js +401 -0
- package/dist/plugins/cookie-jar.d.ts +82 -0
- package/dist/plugins/cookie-jar.js +268 -0
- package/dist/plugins/dedupe.d.ts +64 -0
- package/dist/plugins/dedupe.js +226 -0
- package/dist/plugins/index.d.ts +20 -412
- package/dist/plugins/index.js +23 -0
- package/dist/plugins/request-compression.d.ts +44 -0
- package/dist/plugins/request-compression.js +178 -0
- package/dist/plugins/response-compression.d.ts +28 -0
- package/dist/plugins/response-compression.js +91 -0
- package/dist/plugins/retry-after.d.ts +50 -0
- package/dist/plugins/retry-after.js +88 -0
- package/dist/plugins/retry.d.ts +84 -0
- package/dist/plugins/retry.js +140 -0
- package/dist/plugins/timeout.d.ts +25 -0
- package/dist/plugins/timeout.js +37 -0
- package/dist/{shared/client.DqIWeNhQ.d.mts → rpc-json-serializer.d.ts} +9 -41
- package/dist/rpc-json-serializer.js +392 -0
- package/dist/rpc-serializer.d.ts +32 -0
- package/dist/rpc-serializer.js +130 -0
- package/dist/types.d.ts +97 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +47 -0
- package/dist/utils.js +51 -0
- package/package.json +43 -45
- package/dist/adapters/fetch/index.d.mts +0 -48
- package/dist/adapters/fetch/index.mjs +0 -77
- package/dist/adapters/message-port/index.d.mts +0 -80
- package/dist/adapters/message-port/index.mjs +0 -101
- package/dist/adapters/standard/index.mjs +0 -6
- package/dist/adapters/websocket/index.mjs +0 -131
- package/dist/index.d.mts +0 -144
- package/dist/index.mjs +0 -147
- package/dist/plugins/index.d.mts +0 -420
- package/dist/plugins/index.mjs +0 -971
- 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.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
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
|
|
6
6
|
</a>
|
|
7
7
|
<a href="https://www.npmjs.com/package/@orpc/client">
|
|
8
|
-
<img alt="
|
|
8
|
+
<img alt="monthly downloads" src="https://img.shields.io/npm/dm/%40orpc%2Fclient?logo=npm" />
|
|
9
9
|
</a>
|
|
10
10
|
<a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
|
|
11
11
|
<img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
|
|
@@ -41,10 +41,11 @@ export const router = {
|
|
|
41
41
|
Then call them from a client that knows every input and output, without generating anything:
|
|
42
42
|
|
|
43
43
|
```ts
|
|
44
|
-
import type { RouterClient } from '@orpc/server'
|
|
45
|
-
import type { router } from './router'
|
|
46
44
|
import { createORPCClient } from '@orpc/client'
|
|
47
45
|
import { RPCLink } from '@orpc/client/fetch'
|
|
46
|
+
import type { RouterClient } from '@orpc/server'
|
|
47
|
+
|
|
48
|
+
import type { router } from './router'
|
|
48
49
|
|
|
49
50
|
const link = new RPCLink({ origin: 'http://localhost:3000', url: '/rpc' })
|
|
50
51
|
const client: RouterClient<typeof router> = createORPCClient(link)
|
|
@@ -191,15 +192,14 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
191
192
|
<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
193
|
<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
194
|
<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://
|
|
195
|
+
<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
196
|
</tr>
|
|
196
197
|
<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
198
|
<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
199
|
</tr>
|
|
200
200
|
</table>
|
|
201
201
|
|
|
202
|
-
With thanks to [
|
|
202
|
+
With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who helped get oRPC here.
|
|
203
203
|
|
|
204
204
|
## Open Source Programs
|
|
205
205
|
|
|
@@ -209,7 +209,6 @@ With thanks to [39 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
209
209
|
<tr>
|
|
210
210
|
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
|
|
211
211
|
<td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
|
|
212
|
-
<td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
|
|
213
212
|
</tr>
|
|
214
213
|
</table>
|
|
215
214
|
|
|
@@ -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.js';
|
|
6
|
-
import { RPCLinkCodecOptions } from '../standard/index.js';
|
|
7
|
-
import '../../shared/client.DqIWeNhQ.js';
|
|
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.ts';
|
|
2
|
+
export * from './transport.ts';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ClientContext } from '../../types.ts';
|
|
2
|
+
import type { RPCLinkCodecOptions, StandardLinkOptions } from '../standard/index.ts';
|
|
3
|
+
import { StandardLink } from '../standard/index.ts';
|
|
4
|
+
import type { FetchLinkTransportOptions } from './transport.ts';
|
|
5
|
+
export interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, FetchLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Client link that communicates with an RPC Handler over the Fetch API (HTTP).
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.dev/docs/adapters/fetch-api | Fetch API Adapter}
|
|
11
|
+
*/
|
|
12
|
+
export declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
13
|
+
constructor(options: RPCLinkOptions<T>);
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RPCLinkCodec, StandardLink } from '../standard/index.js';
|
|
2
|
+
import { FetchLinkTransport } from './transport.js';
|
|
3
|
+
/**
|
|
4
|
+
* Client link that communicates with an RPC Handler over the Fetch API (HTTP).
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://orpc.dev/docs/adapters/fetch-api | Fetch API Adapter}
|
|
7
|
+
*/
|
|
8
|
+
export class RPCLink extends StandardLink {
|
|
9
|
+
constructor(options) {
|
|
10
|
+
const codec = new RPCLinkCodec(options);
|
|
11
|
+
const transport = new FetchLinkTransport(options);
|
|
12
|
+
super(codec, transport, options);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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.ts';
|
|
5
|
+
import type { StandardLinkTransport } from '../standard/index.ts';
|
|
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>, [
|
|
14
|
+
options: ClientOptions<T>,
|
|
15
|
+
path: string[]
|
|
16
|
+
]> | 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
|
+
export 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
|
+
}
|
|
@@ -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
|
+
// TODO: Try `duplex: 'full'` when it is widely supported.
|
|
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
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
export class FetchLinkTransport {
|
|
22
|
+
origin;
|
|
23
|
+
fetch;
|
|
24
|
+
toFetchRequestOptions;
|
|
25
|
+
constructor(options) {
|
|
26
|
+
this.origin = options.origin;
|
|
27
|
+
// Resolve `globalThis.fetch` lazily so interception tools (msw, undici MockAgent, etc.)
|
|
28
|
+
// that patch it after this transport is constructed still take effect.
|
|
29
|
+
this.fetch = options.fetch ?? ((url, init) => globalThis.fetch(url, init));
|
|
30
|
+
this.toFetchRequestOptions = options.toFetchRequest;
|
|
31
|
+
}
|
|
32
|
+
async send(standardRequest, path, options) {
|
|
33
|
+
let origin = await value(this.origin, options, path);
|
|
34
|
+
if (origin?.endsWith('/')) {
|
|
35
|
+
origin = origin.slice(0, -1);
|
|
36
|
+
}
|
|
37
|
+
const url = `${origin ?? ''}${standardRequest.url}`;
|
|
38
|
+
const [body, standardHeaders] = toFetchBody(standardRequest.body, standardRequest.headers, this.toFetchRequestOptions);
|
|
39
|
+
const init = {
|
|
40
|
+
body,
|
|
41
|
+
headers: toFetchHeaders(standardHeaders),
|
|
42
|
+
method: standardRequest.method,
|
|
43
|
+
signal: options.signal,
|
|
44
|
+
};
|
|
45
|
+
if (body instanceof ReadableStream) {
|
|
46
|
+
const duplex = GET_SUPPORTED_DUPLEX_MODE();
|
|
47
|
+
if (duplex !== undefined) {
|
|
48
|
+
init.duplex = duplex;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Call without a receiver: browsers throw "Illegal invocation" when native `fetch` is called on a non-global `this`.
|
|
52
|
+
const fetch = this.fetch;
|
|
53
|
+
const response = await fetch(url, init, options, path);
|
|
54
|
+
// Opaque responses (status 0, no headers or body) would otherwise decode as a successful empty output.
|
|
55
|
+
// Browsers return them for redirects under `redirect: 'manual'` and for cross-origin `no-cors` requests.
|
|
56
|
+
if (response.type === 'opaqueredirect') {
|
|
57
|
+
throw new TypeError("Unexpected opaque redirect response: browsers hide redirects under `redirect: 'manual'`.");
|
|
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.js';
|
|
6
|
-
import { RPCLinkCodecOptions } from '../standard/index.js';
|
|
7
|
-
import '../../shared/client.DqIWeNhQ.js';
|
|
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.ts';
|
|
2
|
+
export * from './rpc-link.ts';
|
|
3
|
+
export * from './transport.ts';
|
|
@@ -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,56 @@
|
|
|
1
|
+
export function postMessagePortMessage(port, data, transfer) {
|
|
2
|
+
if (transfer) {
|
|
3
|
+
port.postMessage(data, transfer);
|
|
4
|
+
}
|
|
5
|
+
else {
|
|
6
|
+
port.postMessage(data);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function onMessagePortMessage(port, callback) {
|
|
10
|
+
/**
|
|
11
|
+
* MessagePort in node.js might have "on" method but it pass different parameters vs MessagePortMainLike
|
|
12
|
+
* So we need check "addEventListener" before "on"
|
|
13
|
+
*/
|
|
14
|
+
if ('addEventListener' in port) {
|
|
15
|
+
port.addEventListener('message', (event) => {
|
|
16
|
+
callback(event.data);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
else if ('on' in port) {
|
|
20
|
+
port.on('message', (event) => {
|
|
21
|
+
callback(event?.data);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
else if ('onMessage' in port) {
|
|
25
|
+
port.onMessage.addListener((data) => {
|
|
26
|
+
callback(data);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
throw new Error('Cannot find a addEventListener/on/onMessage method on the port');
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function onMessagePortClose(port, callback) {
|
|
34
|
+
/**
|
|
35
|
+
* MessagePort in node.js might have "on" method but it pass different parameters vs MessagePortMainLike
|
|
36
|
+
* So we need check "addEventListener" before "on"
|
|
37
|
+
*/
|
|
38
|
+
if ('addEventListener' in port) {
|
|
39
|
+
port.addEventListener('close', () => {
|
|
40
|
+
callback();
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
else if ('on' in port) {
|
|
44
|
+
port.on('close', () => {
|
|
45
|
+
callback();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
else if ('onDisconnect' in port) {
|
|
49
|
+
port.onDisconnect.addListener(() => {
|
|
50
|
+
callback();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
throw new Error('Cannot find a addEventListener/on/onDisconnect method on the port');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ClientContext } from '../../types.ts';
|
|
2
|
+
import type { RPCLinkCodecOptions, StandardLinkOptions } from '../standard/index.ts';
|
|
3
|
+
import { StandardLink } from '../standard/index.ts';
|
|
4
|
+
import type { MessagePortLinkTransportOptions } from './transport.ts';
|
|
5
|
+
export interface RPCLinkOptions<T extends ClientContext> extends StandardLinkOptions<T>, MessagePortLinkTransportOptions<T>, RPCLinkCodecOptions<T> {
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Client link that communicates with an RPC Handler over a Message Port
|
|
9
|
+
* (e.g. workers, iframes, browser extensions, Electron).
|
|
10
|
+
*
|
|
11
|
+
* @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
|
|
12
|
+
*/
|
|
13
|
+
export declare class RPCLink<T extends ClientContext> extends StandardLink<T> {
|
|
14
|
+
constructor(options: RPCLinkOptions<T>);
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RPCLinkCodec, StandardLink } from '../standard/index.js';
|
|
2
|
+
import { MessagePortLinkTransport } from './transport.js';
|
|
3
|
+
/**
|
|
4
|
+
* Client link that communicates with an RPC Handler over a Message Port
|
|
5
|
+
* (e.g. workers, iframes, browser extensions, Electron).
|
|
6
|
+
*
|
|
7
|
+
* @see {@link https://orpc.dev/docs/adapters/message-port | Message Port Adapter}
|
|
8
|
+
*/
|
|
9
|
+
export class RPCLink extends StandardLink {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
const codec = new RPCLinkCodec(options);
|
|
12
|
+
const transport = new MessagePortLinkTransport(options);
|
|
13
|
+
super(codec, transport, options);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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 { ClientPeer } from '@standard-server/peer';
|
|
5
|
+
import type { ClientContext, ClientOptions } from '../../types.ts';
|
|
6
|
+
import type { StandardLinkTransport } from '../standard/index.ts';
|
|
7
|
+
import type { SupportedMessagePort } from './message-port.ts';
|
|
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>, [
|
|
21
|
+
message: DecodedRequestMessage,
|
|
22
|
+
port: SupportedMessagePort
|
|
23
|
+
]> | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Options for encoding peer messages. such as `prefix` for distinguishing messages on the same channel..
|
|
26
|
+
*/
|
|
27
|
+
encodePeerMessage?: EncodePeerMessageOptions | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Options for decoding peer messages, such as `prefix` for distinguishing messages on the same channel.
|
|
30
|
+
*/
|
|
31
|
+
decodePeerMessage?: DecodePeerMessageOptions | undefined;
|
|
32
|
+
}
|
|
33
|
+
export declare class MessagePortLinkTransport<T extends ClientContext> implements StandardLinkTransport<T> {
|
|
34
|
+
private readonly peer;
|
|
35
|
+
constructor({ port, experimental_transfer, encodePeerMessage: encodePeerMessageOptions, decodePeerMessage: decodePeerMessageOptions, }: MessagePortLinkTransportOptions<T>);
|
|
36
|
+
send(standardRequest: StandardRequest, _path: string[], _options: ClientOptions<T>): Promise<StandardLazyResponse>;
|
|
37
|
+
}
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
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.js';
|
|
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
|
+
// `postMessage` silently discards data once the port is closed, so requests would never settle
|
|
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
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
postMessagePortMessage(port, await encodePeerMessage(message, encodePeerMessageOptions));
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
onMessagePortMessage(port, async (data) => {
|
|
22
|
+
let peerMessage;
|
|
23
|
+
if (typeof data === 'string' || data instanceof Uint8Array) {
|
|
24
|
+
// MessagePort receives the exact payload sent, and `encodePeerMessage` only returns string or Uint8Array.
|
|
25
|
+
const result = decodePeerMessage(data, decodePeerMessageOptions);
|
|
26
|
+
if (result.matched && isServerPeerSendMessage(result.message)) {
|
|
27
|
+
peerMessage = result.message;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else if (isPeerMessage(data) && isServerPeerSendMessage(data)) {
|
|
31
|
+
peerMessage = data;
|
|
32
|
+
}
|
|
33
|
+
if (peerMessage === undefined) {
|
|
34
|
+
return { matched: false };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Message order is important: loading -> decode -> .message.
|
|
38
|
+
* This flow must stay synchronous, or we need to use `sequential` helper
|
|
39
|
+
*/
|
|
40
|
+
await this.peer.message(peerMessage);
|
|
41
|
+
return { matched: true };
|
|
42
|
+
});
|
|
43
|
+
onMessagePortClose(port, async () => {
|
|
44
|
+
closeReason = new AbortError('MessagePort closed');
|
|
45
|
+
await this.peer.close(closeReason);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
async send(standardRequest, _path, _options) {
|
|
49
|
+
const standardResponse = await this.peer.request(standardRequest);
|
|
50
|
+
return standardResponse;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Promisable } from '@orpc/shared';
|
|
2
|
+
import type { StandardLazyResponse, StandardRequest } from '@standard-server/core';
|
|
3
|
+
import type { ClientContext, ClientOptions } from '../../types.ts';
|
|
4
|
+
export type StandardLinkCodecDecodedResponse = {
|
|
5
|
+
kind: 'output';
|
|
6
|
+
output: unknown;
|
|
7
|
+
} | {
|
|
8
|
+
kind: 'error';
|
|
9
|
+
error: Error;
|
|
10
|
+
};
|
|
11
|
+
export interface StandardLinkCodec<T extends ClientContext> {
|
|
12
|
+
encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promisable<StandardRequest>;
|
|
13
|
+
decodeResponse(response: StandardLazyResponse, path: string[], options: ClientOptions<T>): Promisable<StandardLinkCodecDecodedResponse>;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,60 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { RPCSerializer } from '../../index.js';
|
|
7
|
-
|
|
8
|
-
interface RPCLinkCodecOptions<T extends ClientContext> {
|
|
9
|
-
/**
|
|
10
|
-
* Base url for all requests (without origin). Should match with handler's prefix.
|
|
11
|
-
*
|
|
12
|
-
* @example '/rpc?base=1'
|
|
13
|
-
*
|
|
14
|
-
* @default '/'
|
|
15
|
-
*/
|
|
16
|
-
url?: Value<Promisable<StandardUrl>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
17
|
-
/**
|
|
18
|
-
* The maximum length of the URL.
|
|
19
|
-
*
|
|
20
|
-
* If the URL exceeds this length, the codec should use the `fallbackMethod` to send the request with the payload in the body instead of the URL.
|
|
21
|
-
*
|
|
22
|
-
* @default 2083
|
|
23
|
-
*/
|
|
24
|
-
maxUrlLength?: Value<Promisable<number>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
25
|
-
/**
|
|
26
|
-
* The method used to make the request.
|
|
27
|
-
*
|
|
28
|
-
* @default 'POST'
|
|
29
|
-
*/
|
|
30
|
-
method?: Value<Promisable<'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'QUERY'>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
31
|
-
/**
|
|
32
|
-
* The method to use when the payload cannot safely pass to the server with method return from method function.
|
|
33
|
-
* GET is not allowed, it's very dangerous.
|
|
34
|
-
*
|
|
35
|
-
* @default 'POST'
|
|
36
|
-
*/
|
|
37
|
-
fallbackMethod?: 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'QUERY' | undefined;
|
|
38
|
-
/**
|
|
39
|
-
* Inject headers to the request.
|
|
40
|
-
*/
|
|
41
|
-
headers?: Value<Promisable<StandardHeaders | Headers>, [options: ClientOptions<T>, path: string[], input: unknown]> | undefined;
|
|
42
|
-
/**
|
|
43
|
-
* Override the default RPC serializer.
|
|
44
|
-
*/
|
|
45
|
-
serializer?: Public<RPCSerializer> | undefined;
|
|
46
|
-
}
|
|
47
|
-
declare class RPCLinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
48
|
-
private readonly baseUrl;
|
|
49
|
-
private readonly maxUrlLength;
|
|
50
|
-
private readonly fallbackMethod;
|
|
51
|
-
private readonly expectedMethod;
|
|
52
|
-
private readonly headers;
|
|
53
|
-
private readonly serializer;
|
|
54
|
-
constructor(options: RPCLinkCodecOptions<T>);
|
|
55
|
-
encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promise<StandardRequest>;
|
|
56
|
-
decodeResponse(response: StandardLazyResponse): Promise<StandardLinkCodecDecodedResponse>;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export { RPCLinkCodec, StandardLinkCodec, StandardLinkCodecDecodedResponse };
|
|
60
|
-
export type { RPCLinkCodecOptions };
|
|
1
|
+
export * from './codec.ts';
|
|
2
|
+
export * from './link.ts';
|
|
3
|
+
export type { StandardLinkPlugin } from './plugin.ts';
|
|
4
|
+
export * from './rpc-link-codec.ts';
|
|
5
|
+
export * from './transport.ts';
|