@vitejs/devtools-rpc 0.0.0-alpha.1

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present, VoidZero Inc. and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,16 @@
1
+ import * as birpc0 from "birpc";
2
+ import { BirpcGroup, BirpcOptions, EventOptions } from "birpc";
3
+
4
+ //#region src/client.d.ts
5
+ declare function createRpcClient<ServerFunctions = Record<string, never>, ClientFunctions extends object = Record<string, never>>(functions: ClientFunctions, options: {
6
+ preset: BirpcOptions<ServerFunctions>;
7
+ rpcOptions?: Partial<BirpcOptions<ServerFunctions>>;
8
+ }): birpc0.BirpcReturn<ServerFunctions, ClientFunctions>;
9
+ //#endregion
10
+ //#region src/server.d.ts
11
+ declare function createRpcServer<ClientFunctions extends object = Record<string, never>, ServerFunctions extends object = Record<string, never>>(functions: ServerFunctions, options: {
12
+ preset: (rpc: BirpcGroup<ClientFunctions, ServerFunctions>) => void;
13
+ rpcOptions?: EventOptions<ClientFunctions>;
14
+ }): BirpcGroup<ClientFunctions, ServerFunctions>;
15
+ //#endregion
16
+ export { createRpcClient, createRpcServer };
package/dist/index.mjs ADDED
@@ -0,0 +1,22 @@
1
+ import { createBirpc, createBirpcGroup } from "birpc";
2
+
3
+ //#region src/client.ts
4
+ function createRpcClient(functions, options) {
5
+ const { preset, rpcOptions = {} } = options;
6
+ return createBirpc(functions, {
7
+ ...preset,
8
+ timeout: -1,
9
+ ...rpcOptions
10
+ });
11
+ }
12
+
13
+ //#endregion
14
+ //#region src/server.ts
15
+ function createRpcServer(functions, options) {
16
+ const rpc = createBirpcGroup(functions, [], options?.rpcOptions ?? {});
17
+ options?.preset(rpc);
18
+ return rpc;
19
+ }
20
+
21
+ //#endregion
22
+ export { createRpcClient, createRpcServer };
@@ -0,0 +1,13 @@
1
+ import { BirpcGroup, BirpcOptions, ChannelOptions } from "birpc";
2
+
3
+ //#region src/presets/index.d.ts
4
+ type RpcServerPresetReturnType = <ClientFunctions, ServerFunctions>(rpc: BirpcGroup<ClientFunctions, ServerFunctions>, options?: Pick<BirpcOptions<ClientFunctions>, 'serialize' | 'deserialize'>) => void;
5
+ type RpcServerPresetBasicType = (...args: any[]) => RpcServerPresetReturnType;
6
+ type RpcServerPreset<T extends RpcServerPresetBasicType> = (...args: Parameters<T>) => RpcServerPresetReturnType;
7
+ declare function defineRpcServerPreset<T extends RpcServerPresetBasicType>(preset: T): RpcServerPreset<T>;
8
+ type RpcClientPresetReturnType = Omit<ChannelOptions, 'bind'>;
9
+ type RpcClientPresetBasicType = (...args: any[]) => RpcClientPresetReturnType;
10
+ type RpcClientPreset<T extends RpcClientPresetBasicType> = (...args: Parameters<T>) => RpcClientPresetReturnType;
11
+ declare function defineRpcClientPreset<T extends RpcClientPresetBasicType>(preset: T): RpcClientPreset<T>;
12
+ //#endregion
13
+ export { RpcClientPreset, RpcClientPresetBasicType, RpcClientPresetReturnType, RpcServerPreset, RpcServerPresetBasicType, RpcServerPresetReturnType, defineRpcClientPreset, defineRpcServerPreset };
@@ -0,0 +1,2 @@
1
+ import { RpcClientPreset, RpcClientPresetBasicType, RpcClientPresetReturnType, RpcServerPreset, RpcServerPresetBasicType, RpcServerPresetReturnType, defineRpcClientPreset, defineRpcServerPreset } from "./index-CNN7vRfy.mjs";
2
+ export { RpcClientPreset, RpcClientPresetBasicType, RpcClientPresetReturnType, RpcServerPreset, RpcServerPresetBasicType, RpcServerPresetReturnType, defineRpcClientPreset, defineRpcServerPreset };
@@ -0,0 +1,3 @@
1
+ import { defineRpcClientPreset, defineRpcServerPreset } from "./presets-B1cCNE-L.mjs";
2
+
3
+ export { defineRpcClientPreset, defineRpcServerPreset };
@@ -0,0 +1,10 @@
1
+ //#region src/presets/index.ts
2
+ function defineRpcServerPreset(preset) {
3
+ return preset;
4
+ }
5
+ function defineRpcClientPreset(preset) {
6
+ return preset;
7
+ }
8
+
9
+ //#endregion
10
+ export { defineRpcClientPreset, defineRpcServerPreset };
@@ -0,0 +1,17 @@
1
+ import { RpcClientPreset } from "../index-CNN7vRfy.mjs";
2
+
3
+ //#region src/presets/ws/client.d.ts
4
+ interface WebSocketRpcClientOptions {
5
+ url: string;
6
+ onConnected?: (e: Event) => void;
7
+ onError?: (e: Error) => void;
8
+ onDisconnected?: (e: CloseEvent) => void;
9
+ }
10
+ declare const createWsRpcPreset: RpcClientPreset<(options: WebSocketRpcClientOptions) => {
11
+ on: (handler: (data: string) => void) => void;
12
+ post: (data: string) => void;
13
+ serialize: (obj: any) => string;
14
+ deserialize: (str: string) => unknown;
15
+ }>;
16
+ //#endregion
17
+ export { WebSocketRpcClientOptions, createWsRpcPreset };
@@ -0,0 +1,41 @@
1
+ import { defineRpcClientPreset } from "../presets-B1cCNE-L.mjs";
2
+ import { parse, stringify } from "structured-clone-es";
3
+
4
+ //#region src/presets/ws/client.ts
5
+ function NOOP() {}
6
+ const createWsRpcPreset = defineRpcClientPreset((options) => {
7
+ const ws = new WebSocket(options.url);
8
+ const { onConnected = NOOP, onError = NOOP, onDisconnected = NOOP } = options;
9
+ ws.addEventListener("open", (e) => {
10
+ onConnected(e);
11
+ });
12
+ ws.addEventListener("error", (e) => {
13
+ const _e = e instanceof Error ? e : new Error(e.type);
14
+ onError(_e);
15
+ });
16
+ ws.addEventListener("close", (e) => {
17
+ onDisconnected(e);
18
+ });
19
+ return {
20
+ on: (handler) => {
21
+ ws.addEventListener("message", (e) => {
22
+ handler(e.data);
23
+ });
24
+ },
25
+ post: (data) => {
26
+ if (ws.readyState === WebSocket.OPEN) ws.send(data);
27
+ else {
28
+ function handler() {
29
+ ws.send(data);
30
+ ws.removeEventListener("open", handler);
31
+ }
32
+ ws.addEventListener("open", handler);
33
+ }
34
+ },
35
+ serialize: stringify,
36
+ deserialize: parse
37
+ };
38
+ });
39
+
40
+ //#endregion
41
+ export { createWsRpcPreset };
@@ -0,0 +1,13 @@
1
+ import { RpcServerPreset } from "../index-CNN7vRfy.mjs";
2
+ import { BirpcGroup, BirpcOptions } from "birpc";
3
+ import { WebSocket } from "ws";
4
+
5
+ //#region src/presets/ws/server.d.ts
6
+ interface WebSocketRpcServerOptions {
7
+ port: number;
8
+ onConnected?: (ws: WebSocket) => void;
9
+ onDisconnected?: (ws: WebSocket) => void;
10
+ }
11
+ declare const createWsRpcPreset: RpcServerPreset<(options: WebSocketRpcServerOptions) => <ClientFunctions, ServerFunctions>(rpc: BirpcGroup<ClientFunctions, ServerFunctions>, options?: Pick<BirpcOptions<ClientFunctions>, "serialize" | "deserialize">) => void>;
12
+ //#endregion
13
+ export { WebSocketRpcServerOptions, createWsRpcPreset };