@orpc/client 2.0.0-beta.48 → 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 +4 -4
- package/dist/adapters/fetch/index.d.ts +2 -0
- 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 -0
- 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/index.d.ts +5 -0
- 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/rpc-link-codec.d.ts +63 -0
- package/dist/adapters/standard/rpc-link-codec.js +104 -0
- package/dist/adapters/standard/transport.d.ts +14 -0
- package/dist/adapters/websocket/index.d.ts +2 -0
- 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/transport.d.ts +115 -0
- 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 +53 -0
- 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 +28 -0
- 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/rpc-json-serializer.d.ts +99 -0
- 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/utils.d.ts +47 -0
- package/dist/utils.js +51 -0
- package/package.json +43 -45
- package/dist/adapters/fetch/index.d.mts +0 -2
- package/dist/adapters/fetch/index.mjs +0 -2
- package/dist/adapters/fetch/rpc-link.d.mts +0 -13
- package/dist/adapters/fetch/rpc-link.mjs +0 -10
- package/dist/adapters/fetch/transport.d.mts +0 -31
- package/dist/adapters/fetch/transport.mjs +0 -65
- package/dist/adapters/message-port/index.d.mts +0 -3
- package/dist/adapters/message-port/index.mjs +0 -3
- package/dist/adapters/message-port/message-port.d.mts +0 -29
- package/dist/adapters/message-port/message-port.mjs +0 -43
- package/dist/adapters/message-port/rpc-link.d.mts +0 -14
- package/dist/adapters/message-port/rpc-link.mjs +0 -10
- package/dist/adapters/message-port/transport.d.mts +0 -35
- package/dist/adapters/message-port/transport.mjs +0 -47
- package/dist/adapters/standard/codec.d.mts +0 -14
- package/dist/adapters/standard/index.d.mts +0 -5
- package/dist/adapters/standard/index.mjs +0 -4
- package/dist/adapters/standard/link.d.mts +0 -40
- package/dist/adapters/standard/link.mjs +0 -79
- package/dist/adapters/standard/plugin.d.mts +0 -32
- package/dist/adapters/standard/plugin.mjs +0 -16
- package/dist/adapters/standard/rpc-link-codec.d.mts +0 -55
- package/dist/adapters/standard/rpc-link-codec.mjs +0 -104
- package/dist/adapters/standard/transport.d.mts +0 -14
- package/dist/adapters/websocket/index.d.mts +0 -2
- package/dist/adapters/websocket/index.mjs +0 -2
- package/dist/adapters/websocket/rpc-link.d.mts +0 -13
- package/dist/adapters/websocket/rpc-link.mjs +0 -10
- package/dist/adapters/websocket/transport.d.mts +0 -119
- package/dist/adapters/websocket/transport.mjs +0 -123
- package/dist/async-iterator-object.d.mts +0 -7
- package/dist/async-iterator-object.mjs +0 -30
- package/dist/client-safe.d.mts +0 -16
- package/dist/client-safe.mjs +0 -23
- package/dist/client.d.mts +0 -51
- package/dist/client.mjs +0 -32
- package/dist/consts.d.mts +0 -10
- package/dist/consts.mjs +0 -10
- package/dist/dynamic-link.d.mts +0 -13
- package/dist/dynamic-link.mjs +0 -12
- package/dist/error-utils.d.mts +0 -24
- package/dist/error-utils.mjs +0 -44
- package/dist/error.d.mts +0 -138
- package/dist/error.mjs +0 -105
- package/dist/index.d.mts +0 -49
- package/dist/index.mjs +0 -23
- package/dist/plugins/batch.d.mts +0 -150
- package/dist/plugins/batch.mjs +0 -339
- package/dist/plugins/cookie-jar.d.mts +0 -82
- package/dist/plugins/cookie-jar.mjs +0 -229
- package/dist/plugins/dedupe.d.mts +0 -58
- package/dist/plugins/dedupe.mjs +0 -199
- package/dist/plugins/index.d.mts +0 -28
- package/dist/plugins/index.mjs +0 -17
- package/dist/plugins/request-compression.d.mts +0 -44
- package/dist/plugins/request-compression.mjs +0 -150
- package/dist/plugins/response-compression.d.mts +0 -28
- package/dist/plugins/response-compression.mjs +0 -78
- package/dist/plugins/retry-after.d.mts +0 -41
- package/dist/plugins/retry-after.mjs +0 -71
- package/dist/plugins/retry.d.mts +0 -82
- package/dist/plugins/retry.mjs +0 -127
- package/dist/plugins/timeout.d.mts +0 -25
- package/dist/plugins/timeout.mjs +0 -36
- package/dist/rpc-json-serializer.d.mts +0 -99
- package/dist/rpc-json-serializer.mjs +0 -365
- package/dist/rpc-serializer.d.mts +0 -32
- package/dist/rpc-serializer.mjs +0 -100
- package/dist/types.d.mts +0 -93
- package/dist/utils.d.mts +0 -46
- package/dist/utils.mjs +0 -53
- /package/dist/adapters/standard/{codec.mjs → codec.js} +0 -0
- /package/dist/adapters/standard/{transport.mjs → transport.js} +0 -0
- /package/dist/{types.mjs → types.js} +0 -0
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)
|
|
@@ -208,7 +209,6 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
208
209
|
<tr>
|
|
209
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>
|
|
210
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>
|
|
211
|
-
<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>
|
|
212
212
|
</tr>
|
|
213
213
|
</table>
|
|
214
214
|
|
|
@@ -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
|
+
}
|
|
@@ -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,40 @@
|
|
|
1
|
+
import type { Interceptor } from '@orpc/shared';
|
|
2
|
+
import type { StandardLazyResponse, StandardRequest } from '@standard-server/core';
|
|
3
|
+
import type { ClientContext, ClientLink, ClientOptions } from '../../types.ts';
|
|
4
|
+
import type { StandardLinkCodec } from './codec.ts';
|
|
5
|
+
import type { StandardLinkPlugin } from './plugin.ts';
|
|
6
|
+
import type { StandardLinkTransport } from './transport.ts';
|
|
7
|
+
export interface StandardLinkInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
8
|
+
path: string[];
|
|
9
|
+
input: unknown;
|
|
10
|
+
}
|
|
11
|
+
export type StandardLinkInterceptor<T extends ClientContext> = Interceptor<StandardLinkInterceptorOptions<T>, Promise<unknown>>;
|
|
12
|
+
export interface StandardLinkTransportInterceptorOptions<T extends ClientContext> extends ClientOptions<T> {
|
|
13
|
+
path: string[];
|
|
14
|
+
request: StandardRequest;
|
|
15
|
+
}
|
|
16
|
+
export type StandardLinkTransportInterceptor<T extends ClientContext> = Interceptor<StandardLinkTransportInterceptorOptions<T>, Promise<StandardLazyResponse>>;
|
|
17
|
+
export interface StandardLinkOptions<T extends ClientContext> {
|
|
18
|
+
/**
|
|
19
|
+
* Interceptors that execute around the entire call, including transport and codec.
|
|
20
|
+
* Useful for error handling, logging, metrics, ...
|
|
21
|
+
*/
|
|
22
|
+
interceptors?: StandardLinkInterceptor<T>[] | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Interceptors that execute around the transport layer, after encoding and before decoding.
|
|
25
|
+
* Useful for modifying the request or response, adding transport-level logging, ...
|
|
26
|
+
*/
|
|
27
|
+
transportInterceptors?: StandardLinkTransportInterceptor<T>[] | undefined;
|
|
28
|
+
plugins?: StandardLinkPlugin<T>[] | undefined;
|
|
29
|
+
}
|
|
30
|
+
export declare class StandardLink<T extends ClientContext> implements ClientLink<T> {
|
|
31
|
+
private readonly codec;
|
|
32
|
+
private readonly transport;
|
|
33
|
+
private readonly interceptors;
|
|
34
|
+
private readonly transportInterceptors;
|
|
35
|
+
constructor(codec: StandardLinkCodec<T>, transport: StandardLinkTransport<T>, options?: StandardLinkOptions<T>);
|
|
36
|
+
/**
|
|
37
|
+
* @throws ORPCError, MalformedResponseError, transport-level errors (network failures, timeouts, etc.)
|
|
38
|
+
*/
|
|
39
|
+
call(path: string[], input: unknown, options: ClientOptions<T>): Promise<unknown>;
|
|
40
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { getTracer, intercept, isAsyncIteratorObject, ORPC_NAME, override, runWithSpan, traceAsyncIterator, traceReadableStream, } from '@orpc/shared';
|
|
2
|
+
import { CompositeStandardLinkPlugin } from './plugin.js';
|
|
3
|
+
export class StandardLink {
|
|
4
|
+
codec;
|
|
5
|
+
transport;
|
|
6
|
+
interceptors;
|
|
7
|
+
transportInterceptors;
|
|
8
|
+
constructor(codec, transport, options = {}) {
|
|
9
|
+
this.codec = codec;
|
|
10
|
+
this.transport = transport;
|
|
11
|
+
options = new CompositeStandardLinkPlugin(options.plugins).init(options);
|
|
12
|
+
this.interceptors = options.interceptors;
|
|
13
|
+
this.transportInterceptors = options.transportInterceptors;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* @throws ORPCError, MalformedResponseError, transport-level errors (network failures, timeouts, etc.)
|
|
17
|
+
*/
|
|
18
|
+
call(path, input, options) {
|
|
19
|
+
return runWithSpan(`${ORPC_NAME}.${path.join('/')}`, (span) => {
|
|
20
|
+
/**
|
|
21
|
+
* [Semantic conventions for RPC spans](https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/)
|
|
22
|
+
*/
|
|
23
|
+
span?.setAttribute('rpc.system', ORPC_NAME);
|
|
24
|
+
span?.setAttribute('rpc.method', path.join('.'));
|
|
25
|
+
if (getTracer() && isAsyncIteratorObject(input)) {
|
|
26
|
+
/**
|
|
27
|
+
* @warning
|
|
28
|
+
* Remember use `override` for AsyncIteratorObject to remain other special properties
|
|
29
|
+
*/
|
|
30
|
+
input = override(input, traceAsyncIterator('consume_async_iterator_object_input', input));
|
|
31
|
+
}
|
|
32
|
+
else if (getTracer() && input instanceof ReadableStream) {
|
|
33
|
+
/**
|
|
34
|
+
* @warning
|
|
35
|
+
* Remember use `override` for ReadableStream to remain other special properties
|
|
36
|
+
*/
|
|
37
|
+
input = override(input, traceReadableStream('consume_octet_stream_input', input));
|
|
38
|
+
}
|
|
39
|
+
return intercept(this.interceptors, { ...options, path, input }, async ({ path, input, ...options }) => {
|
|
40
|
+
/**
|
|
41
|
+
* In browsers, the OpenTelemetry context manager may not work reliably with async functions,
|
|
42
|
+
* so we should manually pass the parent span here.
|
|
43
|
+
*/
|
|
44
|
+
const tracer = getTracer();
|
|
45
|
+
const activeSpan = tracer?.getActiveSpan() ?? span;
|
|
46
|
+
let request = await runWithSpan({ name: 'encode_input', parent: activeSpan }, () => this.codec.encodeInput(input, path, options));
|
|
47
|
+
if (activeSpan && tracer?.inject) {
|
|
48
|
+
const headers = { ...request.headers };
|
|
49
|
+
tracer.inject(activeSpan, headers);
|
|
50
|
+
request = { ...request, headers };
|
|
51
|
+
}
|
|
52
|
+
const response = await intercept(this.transportInterceptors, { ...options, path, request }, ({ path, request, ...options }) => {
|
|
53
|
+
/**
|
|
54
|
+
* In browsers, the OpenTelemetry context manager may not work reliably with async functions,
|
|
55
|
+
* so we should manually pass the parent span here.
|
|
56
|
+
*/
|
|
57
|
+
const activeTransportSpan = tracer?.getActiveSpan() ?? activeSpan;
|
|
58
|
+
return runWithSpan({ name: 'send_request', parent: activeTransportSpan }, () => this.transport.send(request, path, options));
|
|
59
|
+
});
|
|
60
|
+
const decodedResult = await runWithSpan({ name: 'decode_response', parent: activeSpan }, () => this.codec.decodeResponse(response, path, options));
|
|
61
|
+
if (decodedResult.kind === 'error') {
|
|
62
|
+
throw decodedResult.error;
|
|
63
|
+
}
|
|
64
|
+
const output = decodedResult.output;
|
|
65
|
+
if (getTracer() && isAsyncIteratorObject(output)) {
|
|
66
|
+
/**
|
|
67
|
+
* Do not pass the active span as parent here, as it is a lazy span.
|
|
68
|
+
*
|
|
69
|
+
* @warning
|
|
70
|
+
* Remember use `override` for AsyncIteratorObject to remain other special properties
|
|
71
|
+
*/
|
|
72
|
+
return override(output, traceAsyncIterator('consume_async_iterator_object_output', output));
|
|
73
|
+
}
|
|
74
|
+
else if (getTracer() && output instanceof ReadableStream) {
|
|
75
|
+
/**
|
|
76
|
+
* @warning
|
|
77
|
+
* Remember use `override` for ReadableStream to remain other special properties
|
|
78
|
+
*/
|
|
79
|
+
return override(output, traceReadableStream('consume_octet_stream_output', output));
|
|
80
|
+
}
|
|
81
|
+
return output;
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { OrderablePlugin } from '@orpc/shared';
|
|
2
|
+
import type { ClientContext } from '../../types.ts';
|
|
3
|
+
import type { StandardLinkOptions } from './link.ts';
|
|
4
|
+
export interface StandardLinkPlugin<T extends ClientContext> extends OrderablePlugin {
|
|
5
|
+
/**
|
|
6
|
+
* Initializes the plugin and returns new link options.
|
|
7
|
+
* Called once per plugin instance during composition.
|
|
8
|
+
*
|
|
9
|
+
* This method allows plugins to wrap, extend, or transform link options
|
|
10
|
+
* such as interceptors, or configuration.
|
|
11
|
+
*
|
|
12
|
+
* @param options - The current link options from previous plugins or base configuration
|
|
13
|
+
* @returns Transformed link options with plugin's modifications applied
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* init(options) {
|
|
18
|
+
* return {
|
|
19
|
+
* ...options,
|
|
20
|
+
* interceptors: [...(options.interceptors || []), myInterceptor]
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
init?(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
26
|
+
}
|
|
27
|
+
export declare class CompositeStandardLinkPlugin<T extends ClientContext> implements StandardLinkPlugin<T> {
|
|
28
|
+
name: string;
|
|
29
|
+
protected readonly plugins: StandardLinkPlugin<T>[];
|
|
30
|
+
constructor(plugins?: StandardLinkPlugin<T>[]);
|
|
31
|
+
init(options: StandardLinkOptions<T>): StandardLinkOptions<T>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { sortPlugins } from '@orpc/shared';
|
|
2
|
+
export class CompositeStandardLinkPlugin {
|
|
3
|
+
name = '~composite';
|
|
4
|
+
plugins;
|
|
5
|
+
constructor(plugins = []) {
|
|
6
|
+
this.plugins = sortPlugins(plugins);
|
|
7
|
+
}
|
|
8
|
+
init(options) {
|
|
9
|
+
for (const plugin of this.plugins) {
|
|
10
|
+
if (plugin.init) {
|
|
11
|
+
options = plugin.init(options);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return options;
|
|
15
|
+
}
|
|
16
|
+
}
|