Skip to content

Commit

Permalink
add nodejs checks
Browse files Browse the repository at this point in the history
  • Loading branch information
au-re committed Nov 29, 2024
1 parent 216cc5a commit 8a0b342
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/host.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extractMethods, generateId, getOriginFromURL } from "./helpers";
import { extractMethods, generateId, getOriginFromURL, isNodeEnv } from "./helpers";
import { registerLocalMethods, registerRemoteMethods } from "./rpc";
import { actions, events, IConnection, IConnections, ISchema } from "./types";

Expand All @@ -25,14 +25,14 @@ function connect(guest: HTMLIFrameElement | Worker, schema: ISchema = {}): Promi
if (!guest) throw new Error("a target is required");

const guestIsWorker = (guest as Worker).onerror !== undefined && (guest as Worker).onmessage !== undefined;
const listeners = guestIsWorker ? guest : window;
const listeners = guestIsWorker || isNodeEnv() ? guest : window;

return new Promise((resolve) => {
const connectionID = generateId();

// on handshake request
function handleHandshake(event: any) {
if (!guestIsWorker && !isValidTarget(guest as HTMLIFrameElement, event)) return;
if (!guestIsWorker && !isNodeEnv() && !isValidTarget(guest as HTMLIFrameElement, event)) return;
if (event.data.action !== actions.HANDSHAKE_REQUEST) return;

// register local methods
Expand All @@ -41,7 +41,7 @@ function connect(guest: HTMLIFrameElement | Worker, schema: ISchema = {}): Promi
schema,
localMethods,
connectionID,
guestIsWorker ? (guest as Worker) : undefined
guestIsWorker || isNodeEnv() ? (guest as Worker) : undefined
);

// register remote methods
Expand All @@ -50,7 +50,7 @@ function connect(guest: HTMLIFrameElement | Worker, schema: ISchema = {}): Promi
event.data.methods,
connectionID,
event,
guestIsWorker ? (guest as Worker) : undefined
guestIsWorker || isNodeEnv() ? (guest as Worker) : undefined
);

const payload = {
Expand Down
14 changes: 9 additions & 5 deletions src/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateId, get, isWorker, set } from "./helpers";
import { generateId, get, isNodeEnv, isWorker, set } from "./helpers";
import { actions, events, IRPCRequestPayload, IRPCResolvePayload, ISchema } from "./types";

/**
Expand Down Expand Up @@ -105,12 +105,16 @@ export function createRPC(
connectionID: _connectionID,
};

if (guest) guest.addEventListener(events.MESSAGE, handleResponse);
else self.addEventListener(events.MESSAGE, handleResponse);
listeners.push(() => self.removeEventListener(events.MESSAGE, handleResponse));
if (guest || isNodeEnv()) {
guest?.addEventListener(events.MESSAGE, handleResponse);
listeners.push(() => guest?.removeEventListener(events.MESSAGE, handleResponse));
} else {
self.addEventListener(events.MESSAGE, handleResponse);
listeners.push(() => self.removeEventListener(events.MESSAGE, handleResponse));
}

if (guest) guest.postMessage(payload);
else if (isWorker()) (self as any).postMessage(payload);
else if (isWorker() || isNodeEnv()) (self as any).postMessage(payload);
else (event.source || event.target).postMessage(payload, event.origin);
});
};
Expand Down

0 comments on commit 8a0b342

Please sign in to comment.