From db2c795b5beec9dcb595307864bcc0b275797e1e Mon Sep 17 00:00:00 2001 From: Aurelien Franky Date: Sat, 30 Nov 2024 01:54:41 +0100 Subject: [PATCH] fix various issues --- docs/typings.d.ts | 11 --------- src/guest.ts | 15 ++++++------ src/helpers.ts | 56 +++++++++++++++++++++++++++++++++++++++++++-- src/host.ts | 58 +++++++++++++++++++++++++++++++---------------- 4 files changed, 100 insertions(+), 40 deletions(-) delete mode 100644 docs/typings.d.ts diff --git a/docs/typings.d.ts b/docs/typings.d.ts deleted file mode 100644 index 4240ce8..0000000 --- a/docs/typings.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -declare module "*.html?raw" { - const content: any; - export default content; -} - -declare module "*?worker" { - const WorkerFactory: { - new (): Worker; - }; - export default WorkerFactory; -} diff --git a/src/guest.ts b/src/guest.ts index 32ca711..b8372cc 100644 --- a/src/guest.ts +++ b/src/guest.ts @@ -1,4 +1,4 @@ -import { extractMethods, isWorker } from "./helpers"; +import { extractMethods, getEventData, isWorker } from "./helpers"; import { registerLocalMethods, registerRemoteMethods } from "./rpc"; import { actions, EventHandlers, events, IConnection, ISchema } from "./types"; @@ -8,16 +8,17 @@ function connect(schema: ISchema = {}, eventHandlers?: EventHandlers): Promise { +function connect(guest: HTMLIFrameElement | Worker | NodeWorker, schema: ISchema = {}): Promise { if (!guest) throw new Error("a target is required"); - const guestIsWorker = (guest as Worker).onerror !== undefined && (guest as Worker).onmessage !== undefined; + const guestIsWorker = isNodeWorker(guest) || ((guest as Worker).onerror !== undefined && (guest as Worker).onmessage !== undefined); const listeners = guestIsWorker || isNodeEnv() ? guest : window; return new Promise((resolve) => { @@ -32,8 +44,11 @@ function connect(guest: HTMLIFrameElement | Worker, schema: ISchema = {}): Promi // on handshake request function handleHandshake(event: any) { - if (!guestIsWorker && !isNodeEnv() && !isValidTarget(guest as HTMLIFrameElement, event)) return; - if (event.data.action !== actions.HANDSHAKE_REQUEST) return; + + if (!guestIsWorker && !isNodeEnv() && !isValidTarget(guest, event)) return; + + const eventData = getEventData(event); + if (eventData?.action !== actions.HANDSHAKE_REQUEST) return; // register local methods const localMethods = extractMethods(schema); @@ -46,8 +61,8 @@ function connect(guest: HTMLIFrameElement | Worker, schema: ISchema = {}): Promi // register remote methods const { remote, unregisterRemote } = registerRemoteMethods( - event.data.schema, - event.data.methods, + eventData.schema, + eventData.methods, connectionID, event, guestIsWorker || isNodeEnv() ? (guest as Worker) : undefined @@ -66,10 +81,12 @@ function connect(guest: HTMLIFrameElement | Worker, schema: ISchema = {}): Promi // close the connection and all listeners when called const close = () => { - listeners.removeEventListener(events.MESSAGE, handleHandshake); + removeEventListener(listeners, events.MESSAGE, handleHandshake); unregisterRemote(); unregisterLocal(); - if (guestIsWorker) (guest as Worker).terminate(); + if (guestIsWorker) { + (guest as Worker).terminate(); + } }; const connection: IConnection = { remote, close }; @@ -77,15 +94,16 @@ function connect(guest: HTMLIFrameElement | Worker, schema: ISchema = {}): Promi } // subscribe to HANDSHAKE MESSAGES - listeners.addEventListener(events.MESSAGE, handleHandshake); + addEventListener(listeners, events.MESSAGE, handleHandshake); // on handshake reply function handleHandshakeReply(event: any) { - if (event.data.action !== actions.HANDSHAKE_REPLY) return; - return resolve(connections[event.data.connectionID]); + const eventData = getEventData(event); + if (eventData?.action !== actions.HANDSHAKE_REPLY) return; + return resolve(connections[eventData.connectionID]); } - listeners.addEventListener(events.MESSAGE, handleHandshakeReply); + addEventListener(listeners, events.MESSAGE, handleHandshakeReply); }); }