@vitejs/devtools-rpc 0.0.0-alpha.21 → 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.
@@ -1,3 +1,14 @@
1
- import "../index.mjs";
2
- import { n as createWsRpcPreset, t as WebSocketRpcClientOptions } from "../../client-C3McYp9c.mjs";
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
+ authId?: string;
11
+ }
12
+ declare const createWsRpcPreset: RpcClientPreset<(options: WebSocketRpcClientOptions) => ChannelOptions>;
13
+ //#endregion
3
14
  export { WebSocketRpcClientOptions, createWsRpcPreset };
@@ -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,503 +1,21 @@
1
1
  import { RpcServerPreset } from "../index.mjs";
2
- import { BirpcGroup, BirpcOptions, BirpcReturn } from "birpc";
3
- import { RpcFunctionsCollector, RpcFunctionsCollectorBase } from "birpc-x";
2
+ import { BirpcGroup, BirpcOptions } from "birpc";
3
+ import { IncomingMessage } from "node:http";
4
4
  import { WebSocket } from "ws";
5
- import { ResolvedConfig, ViteDevServer } from "vite";
6
- import { Raw } from "vue";
7
- import { ChildProcess } from "node:child_process";
8
5
 
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
6
+ //#region src/presets/ws/server.d.ts
449
7
  interface DevToolsNodeRpcSessionMeta {
450
8
  id: number;
451
9
  ws?: WebSocket;
452
10
  clientAuthId?: string;
453
11
  isTrusted?: boolean;
454
12
  }
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
494
- //#region src/presets/ws/server.d.ts
495
13
  interface WebSocketRpcServerOptions {
496
14
  port: number;
497
15
  host?: string;
498
- onConnected?: (ws: WebSocket, meta: DevToolsNodeRpcSessionMeta) => void;
16
+ onConnected?: (ws: WebSocket, req: IncomingMessage, meta: DevToolsNodeRpcSessionMeta) => void;
499
17
  onDisconnected?: (ws: WebSocket, meta: DevToolsNodeRpcSessionMeta) => void;
500
18
  }
501
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>;
502
20
  //#endregion
503
- export { WebSocketRpcServerOptions, createWsRpcPreset };
21
+ export { DevToolsNodeRpcSessionMeta, WebSocketRpcServerOptions, createWsRpcPreset };
@@ -3557,9 +3557,9 @@ const createWsRpcPreset = defineRpcServerPreset((options) => {
3557
3557
  port,
3558
3558
  host
3559
3559
  });
3560
- return (rpc, options$1) => {
3560
+ return (rpcGroup, options$1) => {
3561
3561
  const { serialize = stringify, deserialize = parse } = options$1 ?? {};
3562
- wss.on("connection", (ws) => {
3562
+ wss.on("connection", (ws, req) => {
3563
3563
  const meta = {
3564
3564
  id: id++,
3565
3565
  ws
@@ -3577,17 +3577,17 @@ const createWsRpcPreset = defineRpcServerPreset((options) => {
3577
3577
  deserialize,
3578
3578
  meta
3579
3579
  };
3580
- rpc.updateChannels((channels) => {
3580
+ rpcGroup.updateChannels((channels) => {
3581
3581
  channels.push(channel);
3582
3582
  });
3583
3583
  ws.on("close", () => {
3584
- rpc.updateChannels((channels) => {
3584
+ rpcGroup.updateChannels((channels) => {
3585
3585
  const index = channels.indexOf(channel);
3586
3586
  if (index >= 0) channels.splice(index, 1);
3587
3587
  });
3588
3588
  onDisconnected(ws, meta);
3589
3589
  });
3590
- onConnected(ws, meta);
3590
+ onConnected(ws, req, meta);
3591
3591
  });
3592
3592
  };
3593
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.21",
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",
@@ -1,13 +0,0 @@
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 };