@vitejs/devtools-rpc 0.0.0-alpha.20 → 0.0.0-alpha.22

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.
@@ -7,6 +7,7 @@ interface WebSocketRpcClientOptions {
7
7
  onConnected?: (e: Event) => void;
8
8
  onError?: (e: Error) => void;
9
9
  onDisconnected?: (e: CloseEvent) => void;
10
+ authId?: string;
10
11
  }
11
12
  declare const createWsRpcPreset: RpcClientPreset<(options: WebSocketRpcClientOptions) => ChannelOptions>;
12
13
  //#endregion
@@ -4,7 +4,9 @@ import { parse, stringify } from "structured-clone-es";
4
4
  //#region src/presets/ws/client.ts
5
5
  function NOOP() {}
6
6
  const createWsRpcPreset = defineRpcClientPreset((options) => {
7
- const ws = new WebSocket(options.url);
7
+ let url = options.url;
8
+ if (options.authId) url = `${url}?vite_devtools_auth_id=${encodeURIComponent(options.authId)}`;
9
+ const ws = new WebSocket(url);
8
10
  const { onConnected = NOOP, onError = NOOP, onDisconnected = NOOP } = options;
9
11
  ws.addEventListener("open", (e) => {
10
12
  onConnected(e);
@@ -1,13 +1,21 @@
1
1
  import { RpcServerPreset } from "../index.mjs";
2
2
  import { BirpcGroup, BirpcOptions } from "birpc";
3
+ import { IncomingMessage } from "node:http";
3
4
  import { WebSocket } from "ws";
4
5
 
5
6
  //#region src/presets/ws/server.d.ts
7
+ interface DevToolsNodeRpcSessionMeta {
8
+ id: number;
9
+ ws?: WebSocket;
10
+ clientAuthId?: string;
11
+ isTrusted?: boolean;
12
+ }
6
13
  interface WebSocketRpcServerOptions {
7
14
  port: number;
8
- onConnected?: (ws: WebSocket) => void;
9
- onDisconnected?: (ws: WebSocket) => void;
15
+ host?: string;
16
+ onConnected?: (ws: WebSocket, req: IncomingMessage, meta: DevToolsNodeRpcSessionMeta) => void;
17
+ onDisconnected?: (ws: WebSocket, meta: DevToolsNodeRpcSessionMeta) => void;
10
18
  }
11
19
  declare const createWsRpcPreset: RpcServerPreset<(options: WebSocketRpcServerOptions) => <ClientFunctions extends object, ServerFunctions extends object>(rpc: BirpcGroup<ClientFunctions, ServerFunctions, false>, options?: Pick<BirpcOptions<ClientFunctions, ServerFunctions, false>, 'serialize' | 'deserialize'>) => void>;
12
20
  //#endregion
13
- export { WebSocketRpcServerOptions, createWsRpcPreset };
21
+ export { DevToolsNodeRpcSessionMeta, WebSocketRpcServerOptions, createWsRpcPreset };
@@ -3549,13 +3549,21 @@ var import_websocket_server = /* @__PURE__ */ __toESM(require_websocket_server()
3549
3549
 
3550
3550
  //#endregion
3551
3551
  //#region src/presets/ws/server.ts
3552
+ let id = 0;
3552
3553
  function NOOP() {}
3553
3554
  const createWsRpcPreset = defineRpcServerPreset((options) => {
3554
- const { port, onConnected = NOOP, onDisconnected = NOOP } = options;
3555
- const wss = new import_websocket_server.default({ port });
3556
- return (rpc, options$1) => {
3555
+ const { port, host = "localhost", onConnected = NOOP, onDisconnected = NOOP } = options;
3556
+ const wss = new import_websocket_server.default({
3557
+ port,
3558
+ host
3559
+ });
3560
+ return (rpcGroup, options$1) => {
3557
3561
  const { serialize = stringify, deserialize = parse } = options$1 ?? {};
3558
- wss.on("connection", (ws) => {
3562
+ wss.on("connection", (ws, req) => {
3563
+ const meta = {
3564
+ id: id++,
3565
+ ws
3566
+ };
3559
3567
  const channel = {
3560
3568
  post: (data) => {
3561
3569
  ws.send(data);
@@ -3566,19 +3574,20 @@ const createWsRpcPreset = defineRpcServerPreset((options) => {
3566
3574
  });
3567
3575
  },
3568
3576
  serialize,
3569
- deserialize
3577
+ deserialize,
3578
+ meta
3570
3579
  };
3571
- rpc.updateChannels((channels) => {
3580
+ rpcGroup.updateChannels((channels) => {
3572
3581
  channels.push(channel);
3573
3582
  });
3574
3583
  ws.on("close", () => {
3575
- rpc.updateChannels((channels) => {
3584
+ rpcGroup.updateChannels((channels) => {
3576
3585
  const index = channels.indexOf(channel);
3577
3586
  if (index >= 0) channels.splice(index, 1);
3578
3587
  });
3579
- onDisconnected(ws);
3588
+ onDisconnected(ws, meta);
3580
3589
  });
3581
- onConnected(ws);
3590
+ onConnected(ws, req, meta);
3582
3591
  });
3583
3592
  };
3584
3593
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitejs/devtools-rpc",
3
3
  "type": "module",
4
- "version": "0.0.0-alpha.20",
4
+ "version": "0.0.0-alpha.22",
5
5
  "description": "DevTools rpc for Vite (work in progress)",
6
6
  "author": "VoidZero Inc.",
7
7
  "license": "MIT",
@@ -41,7 +41,7 @@
41
41
  "structured-clone-es": "^1.0.0"
42
42
  },
43
43
  "devDependencies": {
44
- "tsdown": "^0.17.4",
44
+ "tsdown": "^0.18.2",
45
45
  "ws": "^8.18.3"
46
46
  },
47
47
  "scripts": {