From a1dd396da856401a12c921d0cd2c4e97cb63f1b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Thu, 5 Dec 2024 22:02:50 +0900 Subject: [PATCH] fix: make handleInvoke interface compatible with invoke (#18876) --- packages/vite/src/node/server/hmr.ts | 12 +++++++++--- packages/vite/src/node/server/ws.ts | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 6b6b334202c579..b4993bd31c6b7e 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -157,7 +157,7 @@ export interface NormalizedHotChannel { off(event: string, listener: Function): void /** @internal */ setInvokeHandler(invokeHandlers: InvokeMethods | undefined): void - handleInvoke(payload: HotPayload): Promise<{ r: any } | { e: any }> + handleInvoke(payload: HotPayload): Promise<{ result: any } | { error: any }> /** * Start listening for messages */ @@ -301,7 +301,13 @@ export const normalizeHotChannel = ( } channel.on?.('vite:invoke', listenerForInvokeHandler) }, - handleInvoke, + handleInvoke: async (payload) => { + const data = await handleInvoke(payload) + if (data.e) { + return { error: data.e } + } + return { result: data.r } + }, send: (...args: any[]) => { let payload: HotPayload if (typeof args[0] === 'string') { @@ -1161,7 +1167,7 @@ export function createDeprecatedHotBroadcaster( send: ws.send, setInvokeHandler: ws.setInvokeHandler, handleInvoke: async () => ({ - e: { + error: { name: 'TransportError', message: 'handleInvoke not implemented', stack: new Error().stack, diff --git a/packages/vite/src/node/server/ws.ts b/packages/vite/src/node/server/ws.ts index 8b8081f43bfbb7..6e7ca804fbae72 100644 --- a/packages/vite/src/node/server/ws.ts +++ b/packages/vite/src/node/server/ws.ts @@ -105,7 +105,7 @@ export function createWebSocketServer( off: noop as any as WebSocketServer['off'], setInvokeHandler: noop, handleInvoke: async () => ({ - e: { + error: { name: 'TransportError', message: 'handleInvoke not implemented', stack: new Error().stack,