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

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.
@@ -0,0 +1,13 @@
1
+ import { RpcClientPreset } from "./presets/index.mjs";
2
+ import { ChannelOptions } from "birpc";
3
+
4
+ //#region src/presets/ws/client.d.ts
5
+ interface WebSocketRpcClientOptions {
6
+ url: string;
7
+ onConnected?: (e: Event) => void;
8
+ onError?: (e: Error) => void;
9
+ onDisconnected?: (e: CloseEvent) => void;
10
+ }
11
+ declare const createWsRpcPreset: RpcClientPreset<(options: WebSocketRpcClientOptions) => ChannelOptions>;
12
+ //#endregion
13
+ export { createWsRpcPreset as n, WebSocketRpcClientOptions as t };
@@ -1,13 +1,3 @@
1
- import { RpcClientPreset } from "../index.mjs";
2
- import { ChannelOptions } from "birpc";
3
-
4
- //#region src/presets/ws/client.d.ts
5
- interface WebSocketRpcClientOptions {
6
- url: string;
7
- onConnected?: (e: Event) => void;
8
- onError?: (e: Error) => void;
9
- onDisconnected?: (e: CloseEvent) => void;
10
- }
11
- declare const createWsRpcPreset: RpcClientPreset<(options: WebSocketRpcClientOptions) => ChannelOptions>;
12
- //#endregion
1
+ import "../index.mjs";
2
+ import { n as createWsRpcPreset, t as WebSocketRpcClientOptions } from "../../client-C3McYp9c.mjs";
13
3
  export { WebSocketRpcClientOptions, createWsRpcPreset };
@@ -1,12 +1,502 @@
1
1
  import { RpcServerPreset } from "../index.mjs";
2
- import { BirpcGroup, BirpcOptions } from "birpc";
2
+ import { BirpcGroup, BirpcOptions, BirpcReturn } from "birpc";
3
+ import { RpcFunctionsCollector, RpcFunctionsCollectorBase } from "birpc-x";
3
4
  import { WebSocket } from "ws";
5
+ import { ResolvedConfig, ViteDevServer } from "vite";
6
+ import { Raw } from "vue";
7
+ import { ChildProcess } from "node:child_process";
4
8
 
9
+ //#region ../kit/src/types/events.d.ts
10
+ interface EventsMap {
11
+ [event: string]: any;
12
+ }
13
+ interface EventUnsubscribe {
14
+ (): void;
15
+ }
16
+ interface EventEmitter<Events extends EventsMap> {
17
+ /**
18
+ * Calls each of the listeners registered for a given event.
19
+ *
20
+ * ```js
21
+ * ee.emit('tick', tickType, tickDuration)
22
+ * ```
23
+ *
24
+ * @param event The event name.
25
+ * @param args The arguments for listeners.
26
+ */
27
+ emit: <K extends keyof Events>(event: K, ...args: Parameters<Events[K]>) => void;
28
+ /**
29
+ * Calls the listeners for a given event once and then removes the listener.
30
+ *
31
+ * @param event The event name.
32
+ * @param args The arguments for listeners.
33
+ */
34
+ emitOnce: <K extends keyof Events>(event: K, ...args: Parameters<Events[K]>) => void;
35
+ /**
36
+ * Event names in keys and arrays with listeners in values.
37
+ *
38
+ * @internal
39
+ */
40
+ _listeners: Partial<{ [E in keyof Events]: Events[E][] }>;
41
+ /**
42
+ * Add a listener for a given event.
43
+ *
44
+ * ```js
45
+ * const unbind = ee.on('tick', (tickType, tickDuration) => {
46
+ * count += 1
47
+ * })
48
+ *
49
+ * disable () {
50
+ * unbind()
51
+ * }
52
+ * ```
53
+ *
54
+ * @param event The event name.
55
+ * @param cb The listener function.
56
+ * @returns Unbind listener from event.
57
+ */
58
+ on: <K extends keyof Events>(event: K, cb: Events[K]) => EventUnsubscribe;
59
+ /**
60
+ * Add a listener for a given event once.
61
+ *
62
+ * ```js
63
+ * const unbind = ee.once('tick', (tickType, tickDuration) => {
64
+ * count += 1
65
+ * })
66
+ *
67
+ * disable () {
68
+ * unbind()
69
+ * }
70
+ * ```
71
+ *
72
+ * @param event The event name.
73
+ * @param cb The listener function.
74
+ * @returns Unbind listener from event.
75
+ */
76
+ once: <K extends keyof Events>(event: K, cb: Events[K]) => EventUnsubscribe;
77
+ }
78
+ //#endregion
79
+ //#region ../kit/src/types/docks.d.ts
80
+ interface DevToolsDockHost {
81
+ readonly views: Map<string, DevToolsDockUserEntry>;
82
+ readonly events: EventEmitter<{
83
+ 'dock:entry:updated': (entry: DevToolsDockUserEntry) => void;
84
+ }>;
85
+ register: <T extends DevToolsDockUserEntry>(entry: T, force?: boolean) => {
86
+ update: (patch: Partial<T>) => void;
87
+ };
88
+ update: (entry: DevToolsDockUserEntry) => void;
89
+ values: () => DevToolsDockUserEntry[];
90
+ }
91
+ type DevToolsDockEntryCategory = 'app' | 'framework' | 'web' | 'advanced' | 'default';
92
+ type DevToolsDockEntryIcon = string | {
93
+ light: string;
94
+ dark: string;
95
+ };
96
+ interface DevToolsDockEntryBase {
97
+ id: string;
98
+ title: string;
99
+ icon: DevToolsDockEntryIcon;
100
+ /**
101
+ * The default order of the entry in the dock.
102
+ * The higher the number the earlier it appears.
103
+ * @default 0
104
+ */
105
+ defaultOrder?: number;
106
+ /**
107
+ * The category of the entry
108
+ * @default 'default'
109
+ */
110
+ category?: DevToolsDockEntryCategory;
111
+ /**
112
+ * Whether the entry should be hidden from the user.
113
+ * @default false
114
+ */
115
+ isHidden?: boolean;
116
+ }
117
+ interface ClientScriptEntry {
118
+ /**
119
+ * The filepath or module name to import from
120
+ */
121
+ importFrom: string;
122
+ /**
123
+ * The name to import the module as
124
+ *
125
+ * @default 'default'
126
+ */
127
+ importName?: string;
128
+ }
129
+ interface DevToolsViewIframe extends DevToolsDockEntryBase {
130
+ type: 'iframe';
131
+ url: string;
132
+ /**
133
+ * The id of the iframe, if multiple tabs is assigned with the same id, the iframe will be shared.
134
+ *
135
+ * When not provided, it would be treated as a unique frame.
136
+ */
137
+ frameId?: string;
138
+ /**
139
+ * Optional client script to import into the iframe
140
+ */
141
+ clientScript?: ClientScriptEntry;
142
+ }
143
+ type DevToolsViewLauncherStatus = 'idle' | 'loading' | 'success' | 'error';
144
+ interface DevToolsViewLauncher extends DevToolsDockEntryBase {
145
+ type: 'launcher';
146
+ launcher: {
147
+ icon?: DevToolsDockEntryIcon;
148
+ title: string;
149
+ status?: DevToolsViewLauncherStatus;
150
+ error?: string;
151
+ description?: string;
152
+ buttonStart?: string;
153
+ buttonLoading?: string;
154
+ onLaunch: () => Promise<void>;
155
+ };
156
+ }
157
+ interface DevToolsViewAction extends DevToolsDockEntryBase {
158
+ type: 'action';
159
+ action: ClientScriptEntry;
160
+ }
161
+ interface DevToolsViewCustomRender extends DevToolsDockEntryBase {
162
+ type: 'custom-render';
163
+ renderer: ClientScriptEntry;
164
+ }
165
+ interface DevToolsViewBuiltin extends DevToolsDockEntryBase {
166
+ type: '~builtin';
167
+ id: '~terminals' | '~logs' | '~client-auth-notice';
168
+ }
169
+ type DevToolsDockUserEntry = DevToolsViewIframe | DevToolsViewAction | DevToolsViewCustomRender | DevToolsViewLauncher;
170
+ type DevToolsDockEntry = DevToolsDockUserEntry | DevToolsViewBuiltin;
171
+ //#endregion
172
+ //#region ../kit/src/types/rpc-augments.d.ts
173
+ /**
174
+ * To be extended
175
+ */
176
+ interface DevToolsRpcClientFunctions {}
177
+ /**
178
+ * To be extended
179
+ */
180
+ interface DevToolsRpcServerFunctions {}
181
+ //#endregion
182
+ //#region ../kit/src/client/rpc.d.ts
183
+ interface DevToolsRpcClient {
184
+ /**
185
+ * The events of the client
186
+ */
187
+ events: EventEmitter<RpcClientEvents>;
188
+ /**
189
+ * Whether the client is trusted
190
+ */
191
+ readonly isTrusted: boolean | null;
192
+ /**
193
+ * The connection meta
194
+ */
195
+ readonly connectionMeta: ConnectionMeta;
196
+ /**
197
+ * Return a promise that resolves when the client is trusted
198
+ *
199
+ * Rejects with an error if the timeout is reached
200
+ *
201
+ * @param timeout - The timeout in milliseconds, default to 60 seconds
202
+ */
203
+ ensureTrusted: (timeout?: number) => Promise<boolean>;
204
+ /**
205
+ * Request trust from the server
206
+ */
207
+ requestTrust: () => Promise<boolean>;
208
+ /**
209
+ * Call a RPC function on the server
210
+ */
211
+ call: BirpcReturn<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>['$call'];
212
+ /**
213
+ * Call a RPC event on the server, and does not expect a response
214
+ */
215
+ callEvent: BirpcReturn<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>['$callEvent'];
216
+ /**
217
+ * Call a RPC optional function on the server
218
+ */
219
+ callOptional: BirpcReturn<DevToolsRpcServerFunctions, DevToolsRpcClientFunctions>['$callOptional'];
220
+ /**
221
+ * The client RPC host
222
+ */
223
+ client: DevToolsClientRpcHost;
224
+ }
225
+ //#endregion
226
+ //#region ../kit/src/client/docks.d.ts
227
+ interface DockPanelStorage {
228
+ width: number;
229
+ height: number;
230
+ top: number;
231
+ left: number;
232
+ position: 'left' | 'right' | 'bottom' | 'top';
233
+ open: boolean;
234
+ inactiveTimeout: number;
235
+ }
236
+ interface DevToolsClientContext {
237
+ /**
238
+ * The RPC client to interact with the server
239
+ */
240
+ readonly rpc: DevToolsRpcClient;
241
+ }
242
+ interface DocksContext extends DevToolsClientContext {
243
+ /**
244
+ * Type of the client environment
245
+ *
246
+ * 'embedded' - running inside an embedded floating panel
247
+ * 'standalone' - running inside a standalone window (no user app)
248
+ */
249
+ readonly clientType: 'embedded' | 'standalone';
250
+ /**
251
+ * The panel context
252
+ */
253
+ readonly panel: DocksPanelContext;
254
+ /**
255
+ * The docks entries context
256
+ */
257
+ readonly docks: DocksEntriesContext;
258
+ }
259
+ type DevToolsClientRpcHost = RpcFunctionsCollector<DevToolsRpcClientFunctions, DevToolsClientContext>;
260
+ interface DocksPanelContext {
261
+ store: DockPanelStorage;
262
+ isDragging: boolean;
263
+ isResizing: boolean;
264
+ readonly isVertical: boolean;
265
+ }
266
+ interface DocksEntriesContext {
267
+ selectedId: string | null;
268
+ readonly selected: DevToolsDockEntry | null;
269
+ entries: DevToolsDockEntry[];
270
+ entryToStateMap: Map<string, DockEntryState>;
271
+ /**
272
+ * Get the state of a dock entry by its ID
273
+ */
274
+ getStateById: (id: string) => DockEntryState | undefined;
275
+ /**
276
+ * Switch to the selected dock entry, pass `null` to clear the selection
277
+ *
278
+ * @returns Whether the selection was changed successfully
279
+ */
280
+ switchEntry: (id?: string | null) => Promise<boolean>;
281
+ /**
282
+ * Toggle the selected dock entry
283
+ *
284
+ * @returns Whether the selection was changed successfully
285
+ */
286
+ toggleEntry: (id: string) => Promise<boolean>;
287
+ }
288
+ interface DockEntryState {
289
+ entryMeta: DevToolsDockEntry;
290
+ readonly isActive: boolean;
291
+ domElements: {
292
+ iframe?: HTMLIFrameElement | null;
293
+ panel?: HTMLDivElement | null;
294
+ };
295
+ events: Raw<EventEmitter<DockEntryStateEvents>>;
296
+ }
297
+ interface DockEntryStateEvents {
298
+ 'entry:activated': () => void;
299
+ 'entry:deactivated': () => void;
300
+ 'entry:updated': (newMeta: DevToolsDockUserEntry) => void;
301
+ 'dom:panel:mounted': (panel: HTMLDivElement) => void;
302
+ 'dom:iframe:mounted': (iframe: HTMLIFrameElement) => void;
303
+ }
304
+ interface RpcClientEvents {
305
+ 'rpc:is-trusted:updated': (isTrusted: boolean) => void;
306
+ }
307
+ //#endregion
308
+ //#region ../kit/src/client/client-script.d.ts
309
+ /**
310
+ * Context for client scripts running in dock entries
311
+ */
312
+ interface DockClientScriptContext extends DocksContext {
313
+ /**
314
+ * The state of the current dock entry
315
+ */
316
+ current: DockEntryState;
317
+ }
318
+ //#endregion
319
+ //#region ../kit/src/types/terminals.d.ts
320
+ interface DevToolsTerminalSessionStreamChunkEvent {
321
+ id: string;
322
+ chunks: string[];
323
+ ts: number;
324
+ }
325
+ interface DevToolsTerminalHost {
326
+ readonly sessions: Map<string, DevToolsTerminalSession>;
327
+ readonly events: EventEmitter<{
328
+ 'terminal:session:updated': (session: DevToolsTerminalSession) => void;
329
+ 'terminal:session:stream-chunk': (data: DevToolsTerminalSessionStreamChunkEvent) => void;
330
+ }>;
331
+ register: (session: DevToolsTerminalSession) => DevToolsTerminalSession;
332
+ update: (session: DevToolsTerminalSession) => void;
333
+ startChildProcess: (executeOptions: DevToolsChildProcessExecuteOptions, terminal: Omit<DevToolsTerminalSessionBase, 'status'>) => Promise<DevToolsChildProcessTerminalSession>;
334
+ }
335
+ type DevToolsTerminalStatus = 'running' | 'stopped' | 'error';
336
+ interface DevToolsTerminalSessionBase {
337
+ id: string;
338
+ title: string;
339
+ description?: string;
340
+ status: DevToolsTerminalStatus;
341
+ icon?: DevToolsDockEntryIcon;
342
+ }
343
+ interface DevToolsTerminalSession extends DevToolsTerminalSessionBase {
344
+ buffer?: string[];
345
+ stream?: ReadableStream<string>;
346
+ }
347
+ interface DevToolsChildProcessExecuteOptions {
348
+ command: string;
349
+ args: string[];
350
+ cwd?: string;
351
+ env?: Record<string, string>;
352
+ }
353
+ interface DevToolsChildProcessTerminalSession extends DevToolsTerminalSession {
354
+ type: 'child-process';
355
+ executeOptions: DevToolsChildProcessExecuteOptions;
356
+ getChildProcess: () => ChildProcess | undefined;
357
+ terminate: () => Promise<void>;
358
+ restart: () => Promise<void>;
359
+ }
360
+ //#endregion
361
+ //#region ../kit/src/types/views.d.ts
362
+ interface DevToolsViewHost {
363
+ /**
364
+ * @internal
365
+ */
366
+ buildStaticDirs: {
367
+ baseUrl: string;
368
+ distDir: string;
369
+ }[];
370
+ /**
371
+ * Helper to host static files
372
+ * - In `dev` mode, it will register middleware to `viteServer.middlewares` to host the static files
373
+ * - In `build` mode, it will copy the static files to the dist directory
374
+ */
375
+ hostStatic: (baseUrl: string, distDir: string) => void;
376
+ }
377
+ //#endregion
378
+ //#region ../kit/src/types/vite-plugin.d.ts
379
+ interface DevToolsCapabilities {
380
+ rpc?: boolean;
381
+ views?: boolean;
382
+ }
383
+ interface DevToolsPluginOptions {
384
+ capabilities?: {
385
+ dev?: DevToolsCapabilities | boolean;
386
+ build?: DevToolsCapabilities | boolean;
387
+ };
388
+ setup: (context: DevToolsNodeContext) => void | Promise<void>;
389
+ }
390
+ interface DevToolsNodeContext {
391
+ /**
392
+ * Workspace root directory of Vite DevTools
393
+ */
394
+ readonly workspaceRoot: string;
395
+ /**
396
+ * Current working directory of Vite DevTools
397
+ */
398
+ readonly cwd: string;
399
+ /**
400
+ * Current mode of Vite DevTools
401
+ * - 'dev' - when Vite DevTools is running in dev mode
402
+ * - 'build' - when Vite DevTools is running in build mode (no server)
403
+ */
404
+ readonly mode: 'dev' | 'build';
405
+ /**
406
+ * Resolved Vite configuration
407
+ */
408
+ readonly viteConfig: ResolvedConfig;
409
+ /**
410
+ * Vite dev server instance (only available in dev mode)
411
+ */
412
+ readonly viteServer?: ViteDevServer;
413
+ /**
414
+ * RPC functions host, for registering server-side RPC functions and calling client-side RPC functions
415
+ */
416
+ rpc: RpcFunctionsHost;
417
+ /**
418
+ * Docks host, for registering dock entries
419
+ */
420
+ docks: DevToolsDockHost;
421
+ /**
422
+ * Views host, for registering static views
423
+ */
424
+ views: DevToolsViewHost;
425
+ /**
426
+ * Utils for the node context
427
+ */
428
+ utils: DevToolsNodeUtils;
429
+ /**
430
+ * Terminals host, for registering terminal sessions and streaming terminal output
431
+ */
432
+ terminals: DevToolsTerminalHost;
433
+ }
434
+ interface DevToolsNodeUtils {
435
+ /**
436
+ * Create a simple client script from a function or stringified code
437
+ *
438
+ * @deprecated DO NOT USE. This is mostly for testing only. Please use a proper importable module instead.
439
+ * @experimental
440
+ */
441
+ createSimpleClientScript: (fn: string | ((ctx: DockClientScriptContext) => void)) => ClientScriptEntry;
442
+ }
443
+ interface ConnectionMeta {
444
+ backend: 'websocket' | 'static';
445
+ websocket?: number | string;
446
+ }
447
+ //#endregion
448
+ //#region ../kit/src/types/rpc.d.ts
449
+ interface DevToolsNodeRpcSessionMeta {
450
+ id: number;
451
+ ws?: WebSocket;
452
+ clientAuthId?: string;
453
+ isTrusted?: boolean;
454
+ }
455
+ interface DevToolsNodeRpcSession {
456
+ meta: DevToolsNodeRpcSessionMeta;
457
+ rpc: BirpcReturn<DevToolsRpcClientFunctions, DevToolsRpcServerFunctions, false>;
458
+ }
459
+ type RpcFunctionsHost = RpcFunctionsCollectorBase<DevToolsRpcServerFunctions, DevToolsNodeContext> & {
460
+ /**
461
+ * Broadcast a message to all connected clients
462
+ */
463
+ broadcast: <T extends keyof DevToolsRpcClientFunctions, Args extends Parameters<DevToolsRpcClientFunctions[T]>>(name: T, ...args: Args) => Promise<(Awaited<ReturnType<DevToolsRpcClientFunctions[T]>> | undefined)[]>;
464
+ /**
465
+ * Get the current RPC client
466
+ *
467
+ * Available in RPC functions to get the current RPC client
468
+ */
469
+ getCurrentRpcSession: () => DevToolsNodeRpcSession | undefined;
470
+ };
471
+ //#endregion
472
+ //#region ../kit/src/types/vite-augment.d.ts
473
+ declare module 'vite' {
474
+ interface Plugin {
475
+ devtools?: DevToolsPluginOptions;
476
+ }
477
+ interface UserConfig {
478
+ devtools?: ViteConfigDevtoolsOptions;
479
+ }
480
+ }
481
+ interface ViteConfigDevtoolsOptions {
482
+ /**
483
+ * Disable client authentication.
484
+ *
485
+ * Beware that if you disable client authentication,
486
+ * any browsers can connect to the devtools and access to your server and filesystem.
487
+ * (including other devices, if you open server `host` option to LAN or WAN)
488
+ *
489
+ * @default true
490
+ */
491
+ clientAuth?: boolean;
492
+ }
493
+ //#endregion
5
494
  //#region src/presets/ws/server.d.ts
6
495
  interface WebSocketRpcServerOptions {
7
496
  port: number;
8
- onConnected?: (ws: WebSocket) => void;
9
- onDisconnected?: (ws: WebSocket) => void;
497
+ host?: string;
498
+ onConnected?: (ws: WebSocket, meta: DevToolsNodeRpcSessionMeta) => void;
499
+ onDisconnected?: (ws: WebSocket, meta: DevToolsNodeRpcSessionMeta) => void;
10
500
  }
11
501
  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
502
  //#endregion
@@ -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 });
3555
+ const { port, host = "localhost", onConnected = NOOP, onDisconnected = NOOP } = options;
3556
+ const wss = new import_websocket_server.default({
3557
+ port,
3558
+ host
3559
+ });
3556
3560
  return (rpc, options$1) => {
3557
3561
  const { serialize = stringify, deserialize = parse } = options$1 ?? {};
3558
3562
  wss.on("connection", (ws) => {
3563
+ const meta = {
3564
+ id: id++,
3565
+ ws
3566
+ };
3559
3567
  const channel = {
3560
3568
  post: (data) => {
3561
3569
  ws.send(data);
@@ -3566,7 +3574,8 @@ const createWsRpcPreset = defineRpcServerPreset((options) => {
3566
3574
  });
3567
3575
  },
3568
3576
  serialize,
3569
- deserialize
3577
+ deserialize,
3578
+ meta
3570
3579
  };
3571
3580
  rpc.updateChannels((channels) => {
3572
3581
  channels.push(channel);
@@ -3576,9 +3585,9 @@ const createWsRpcPreset = defineRpcServerPreset((options) => {
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, 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.21",
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": {