From d75018678201402d03dd43508fa8ae9bed475b50 Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Tue, 16 Jul 2024 18:23:33 -0700 Subject: [PATCH] allow transferables while setting client --- src/client.ts | 15 +++++++++------ src/connection.ts | 5 ++++- src/worker.ts | 4 ++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/client.ts b/src/client.ts index fb7af99..fa7096a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -105,18 +105,21 @@ export class BareMuxConnection { return (await this.worker.sendMessage({ type: "get" })).name; } - async setTransport(path: string, options: any[]) { + async setTransport(path: string, options: any[], transferables?: Transferable[]) { await this.setManualTransport(` const { default: BareTransport } = await import("${path}"); - return [new BareTransport(${options.map(x => JSON.stringify(x)).join(", ")}), "${path}"]; - `); + return [BareTransport, "${path}"]; + `, options, transferables); } - async setManualTransport(functionBody: string) { + async setManualTransport(functionBody: string, options: any[], transferables?: Transferable[]) { await this.worker.sendMessage({ type: "set", - client: functionBody, - }); + client: { + function: functionBody, + args: options, + }, + }, transferables); } getInnerPort(): MessagePort | Promise { diff --git a/src/connection.ts b/src/connection.ts index b3fcc60..57053f7 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -17,7 +17,10 @@ export type WorkerMessage = { requestHeaders: BareHeaders, channel: MessagePort, }, - client?: string, + client?: { + function: string, + args: any[], + }, }; export type WorkerRequest = { diff --git a/src/worker.ts b/src/worker.ts index 566276f..d894fbf 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -25,9 +25,9 @@ function handleConnection(port: MessagePort) { const AsyncFunction = (async function() { }).constructor; // @ts-expect-error - const func = new AsyncFunction(message.client); + const func = new AsyncFunction(message.client.function); const [newTransport, name] = await func(); - currentTransport = newTransport; + currentTransport = new newTransport(...message.client.args); currentTransportName = name; console.log("set transport to ", currentTransport, name);