|
1 | | -import type { WebSocketRpcClientOptions } from '@vitejs/devtools-rpc/presets/ws/client' |
2 | | -import type { BirpcOptions, BirpcReturn } from 'birpc' |
3 | | -import type { ConnectionMeta, DevToolsDockEntry, DevToolsRpcClientFunctions, DevToolsRpcServerFunctions } from '../types' |
4 | | -import { createRpcClient } from '@vitejs/devtools-rpc' |
5 | | -import { createWsRpcPreset } from '@vitejs/devtools-rpc/presets/ws/client' |
6 | | - |
7 | | -function isNumeric(str: string | number | undefined) { |
8 | | - if (str == null) |
9 | | - return false |
10 | | - return `${+str}` === `${str}` |
11 | | -} |
12 | | - |
13 | | -export interface DevToolsRpcClientOptions { |
14 | | - connectionMeta?: ConnectionMeta |
15 | | - baseURL?: string[] |
16 | | - wsOptions?: Partial<WebSocketRpcClientOptions> |
17 | | - rpcOptions?: Partial<BirpcOptions<DevToolsRpcServerFunctions>> |
18 | | -} |
19 | | - |
20 | | -/** |
21 | | - * Context for client scripts running in dock entries |
22 | | - */ |
23 | | -export interface DockClientScriptContext { |
24 | | - /** |
25 | | - * The dock entry info of the current dock item |
26 | | - */ |
27 | | - dockEntry: DevToolsDockEntry |
28 | | - /** |
29 | | - * The current state of the dock |
30 | | - */ |
31 | | - dockState: 'active' | 'inactive' |
32 | | - /** |
33 | | - * Type of the client environment |
34 | | - * |
35 | | - * 'embedded' - running inside an embedded floating panel |
36 | | - * 'standalone' - running inside a standlone window (no user app) |
37 | | - */ |
38 | | - clientType: 'embedded' | 'standalone' |
39 | | - /** |
40 | | - * Function to hide the panel, if applicable |
41 | | - */ |
42 | | - hidePanel: () => void |
43 | | - /** |
44 | | - * The panel element to mount into, if applicable |
45 | | - */ |
46 | | - elPanel?: HTMLElement | null |
47 | | -} |
48 | | - |
49 | | -export async function getDevToolsRpcClient( |
50 | | - options: DevToolsRpcClientOptions = {}, |
51 | | -): Promise<{ |
52 | | - connectionMeta: ConnectionMeta |
53 | | - rpc: BirpcReturn<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions> |
54 | | -}> { |
55 | | - const { |
56 | | - baseURL = '/.devtools/', |
57 | | - rpcOptions = {}, |
58 | | - } = options |
59 | | - const urls = Array.isArray(baseURL) ? baseURL : [baseURL] |
60 | | - let connectionMeta: ConnectionMeta | undefined = options.connectionMeta |
61 | | - |
62 | | - if (!connectionMeta) { |
63 | | - const errors: Error[] = [] |
64 | | - for (const url of urls) { |
65 | | - try { |
66 | | - connectionMeta = await fetch(`${url}.vdt-connection.json`) |
67 | | - .then(r => r.json()) as ConnectionMeta |
68 | | - break |
69 | | - } |
70 | | - catch (e) { |
71 | | - errors.push(e as Error) |
72 | | - } |
73 | | - } |
74 | | - if (!connectionMeta) { |
75 | | - throw new Error(`Failed to get connection meta from ${urls.join(', ')}`, { |
76 | | - cause: errors, |
77 | | - }) |
78 | | - } |
79 | | - } |
80 | | - |
81 | | - const url = isNumeric(connectionMeta.websocket) |
82 | | - ? `${location.protocol.replace('http', 'ws')}//${location.hostname}:${connectionMeta.websocket}` |
83 | | - : connectionMeta.websocket as string |
84 | | - |
85 | | - const rpc = createRpcClient<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>({}, { |
86 | | - preset: createWsRpcPreset({ |
87 | | - url, |
88 | | - ...options.wsOptions, |
89 | | - }), |
90 | | - rpcOptions, |
91 | | - }) |
92 | | - |
93 | | - return { |
94 | | - connectionMeta, |
95 | | - rpc, |
96 | | - } |
97 | | -} |
| 1 | +export * from './client-script' |
| 2 | +export * from './rpc' |
0 commit comments