From 3a4bb9af082464477b4665ec3447a701bd966970 Mon Sep 17 00:00:00 2001 From: Aurelien Franky Date: Sat, 30 Nov 2024 02:04:04 +0100 Subject: [PATCH 1/2] default to empty object --- src/rpc.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rpc.ts b/src/rpc.ts index 14e32fe..ae35696 100644 --- a/src/rpc.ts +++ b/src/rpc.ts @@ -39,7 +39,7 @@ export function registerLocalMethods( // run function and return the results to the remote try { const result = await get(schema, methodName)(...args); - payload.result = JSON.parse(JSON.stringify(result)); + payload.result = JSON.parse(JSON.stringify(result || {})); } catch (error) { payload.action = actions.RPC_REJECT; payload.error = JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error))); From 2108e62767581b07dec7001cdee132ef5a4c9126 Mon Sep 17 00:00:00 2001 From: Aurelien Franky Date: Sat, 30 Nov 2024 02:15:54 +0100 Subject: [PATCH 2/2] fix rpc --- src/rpc.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/rpc.ts b/src/rpc.ts index ae35696..98bbc2f 100644 --- a/src/rpc.ts +++ b/src/rpc.ts @@ -1,4 +1,4 @@ -import { generateId, get, isNodeEnv, isWorker, set } from "./helpers"; +import { generateId, get, isNodeEnv, isWorker, set, addEventListener, removeEventListener, getEventData } from "./helpers"; import { actions, events, IRPCRequestPayload, IRPCResolvePayload, ISchema } from "./types"; /** @@ -20,7 +20,8 @@ export function registerLocalMethods( methods.forEach((methodName) => { // handle a remote calling a local method async function handleCall(event: any) { - const { action, callID, connectionID, callName, args = [] } = event.data as IRPCRequestPayload; + const eventData = getEventData(event); + const { action, callID, connectionID, callName, args = [] } = eventData as IRPCRequestPayload; if (action !== actions.RPC_REQUEST) return; if (!callID || !callName) return; @@ -51,10 +52,13 @@ export function registerLocalMethods( } // subscribe to the call event - if (guest) guest.addEventListener(events.MESSAGE, handleCall); - else self.addEventListener(events.MESSAGE, handleCall); - - listeners.push(() => self.removeEventListener(events.MESSAGE, handleCall)); + if (guest) { + addEventListener(guest, events.MESSAGE, handleCall); + listeners.push(() => removeEventListener(guest, events.MESSAGE, handleCall)); + } else { + addEventListener(self, events.MESSAGE, handleCall); + listeners.push(() => removeEventListener(self, events.MESSAGE, handleCall)); + } }); return () => listeners.forEach((unregister) => unregister()); @@ -85,7 +89,8 @@ export function createRPC( // on RPC response function handleResponse(event: any) { - const { callID, connectionID, callName, result, error, action } = event.data as IRPCResolvePayload; + const eventData = getEventData(event); + const { callID, connectionID, callName, result, error, action } = eventData as IRPCResolvePayload; if (!callID || !callName) return; if (callName !== _callName) return; @@ -105,12 +110,12 @@ export function createRPC( connectionID: _connectionID, }; - if (guest || isNodeEnv()) { - guest?.addEventListener(events.MESSAGE, handleResponse); - listeners.push(() => guest?.removeEventListener(events.MESSAGE, handleResponse)); + if (guest) { + addEventListener(guest, events.MESSAGE, handleResponse); + listeners.push(() => removeEventListener(guest, events.MESSAGE, handleResponse)); } else { - self.addEventListener(events.MESSAGE, handleResponse); - listeners.push(() => self.removeEventListener(events.MESSAGE, handleResponse)); + addEventListener(self, events.MESSAGE, handleResponse); + listeners.push(() => removeEventListener(self, events.MESSAGE, handleResponse)); } if (guest) guest.postMessage(payload);