mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 14:50:03 -04:00
rename setTransport to setManualTransport
This commit is contained in:
parent
cad770e2d1
commit
fb8fe1d306
3 changed files with 18 additions and 4 deletions
|
@ -43,10 +43,12 @@ Here is an example of using bare-mux:
|
||||||
import { BareMuxConnection } from "@mercuryworkshop/bare-mux";
|
import { BareMuxConnection } from "@mercuryworkshop/bare-mux";
|
||||||
const conn = new BareMuxConnection("/bare-mux/worker.js");
|
const conn = new BareMuxConnection("/bare-mux/worker.js");
|
||||||
// Set Bare-Client transport
|
// Set Bare-Client transport
|
||||||
await conn.setTransport(`
|
await conn.setManualTransport(`
|
||||||
const exports = await import("/bare-mux/index.js");
|
const exports = await import("/bare-mux/index.js");
|
||||||
return new exports.BareClient("https://tomp.app");
|
return new exports.BareClient("https://tomp.app");
|
||||||
`);
|
`);
|
||||||
|
// If your transport is an ES module and exports the class as the default export
|
||||||
|
await conn.setTransport("/bare-mux/module.js", ["arg1", "ws://localhost:4000"]);
|
||||||
|
|
||||||
/// As a proxy developer
|
/// As a proxy developer
|
||||||
import { BareClient } from "@mercuryworkshop/bare-mux";
|
import { BareClient } from "@mercuryworkshop/bare-mux";
|
||||||
|
|
|
@ -107,10 +107,17 @@ export class BareMuxConnection {
|
||||||
this.worker = new WorkerConnection(workerPath);
|
this.worker = new WorkerConnection(workerPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
async setTransport(transport: string) {
|
async setTransport(path: string, options: any[]) {
|
||||||
|
await this.setManualTransport(`
|
||||||
|
const { default: BareTransport } = await import("${path}");
|
||||||
|
return new BareTransport(${options.map(x => JSON.stringify(x)).join(", ")});
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async setManualTransport(functionBody: string) {
|
||||||
await this.worker.sendMessage({
|
await this.worker.sendMessage({
|
||||||
type: "set",
|
type: "set",
|
||||||
client: transport,
|
client: functionBody,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,16 @@ function handleConnection(port: MessagePort) {
|
||||||
port.onmessage = async (event: MessageEvent) => {
|
port.onmessage = async (event: MessageEvent) => {
|
||||||
const port = event.data.port;
|
const port = event.data.port;
|
||||||
const message: WorkerMessage = event.data.message;
|
const message: WorkerMessage = event.data.message;
|
||||||
|
|
||||||
if (message.type === "set") {
|
if (message.type === "set") {
|
||||||
try {
|
try {
|
||||||
const AsyncFunction = (async function () {}).constructor;
|
const AsyncFunction = (async function () {}).constructor;
|
||||||
|
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
const func = new AsyncFunction(message.client);
|
const func = new AsyncFunction(message.client);
|
||||||
console.log(func);
|
|
||||||
currentTransport = await func();
|
currentTransport = await func();
|
||||||
console.log("set transport to ", currentTransport);
|
console.log("set transport to ", currentTransport);
|
||||||
|
|
||||||
port.postMessage(<WorkerResponse>{ type: "set" });
|
port.postMessage(<WorkerResponse>{ type: "set" });
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
port.postMessage(<WorkerResponse>{ type: "error", error: err });
|
port.postMessage(<WorkerResponse>{ type: "error", error: err });
|
||||||
|
@ -23,6 +25,7 @@ function handleConnection(port: MessagePort) {
|
||||||
try {
|
try {
|
||||||
if (!currentTransport) throw new Error("No BareTransport was set. Try creating a BareMuxConnection and calling set() on it.");
|
if (!currentTransport) throw new Error("No BareTransport was set. Try creating a BareMuxConnection and calling set() on it.");
|
||||||
if (!currentTransport.ready) await currentTransport.init();
|
if (!currentTransport.ready) await currentTransport.init();
|
||||||
|
|
||||||
const resp = await currentTransport.request(
|
const resp = await currentTransport.request(
|
||||||
new URL(message.fetch.remote),
|
new URL(message.fetch.remote),
|
||||||
message.fetch.method,
|
message.fetch.method,
|
||||||
|
@ -43,6 +46,7 @@ function handleConnection(port: MessagePort) {
|
||||||
try {
|
try {
|
||||||
if (!currentTransport) throw new Error("No BareTransport was set. Try creating a BareMuxConnection and calling set() on it.");
|
if (!currentTransport) throw new Error("No BareTransport was set. Try creating a BareMuxConnection and calling set() on it.");
|
||||||
if (!currentTransport.ready) await currentTransport.init();
|
if (!currentTransport.ready) await currentTransport.init();
|
||||||
|
|
||||||
const onopen = (protocol: string) => {
|
const onopen = (protocol: string) => {
|
||||||
message.websocket.channel.postMessage({ type: "open", args: [protocol] });
|
message.websocket.channel.postMessage({ type: "open", args: [protocol] });
|
||||||
};
|
};
|
||||||
|
@ -76,6 +80,7 @@ function handleConnection(port: MessagePort) {
|
||||||
close(event.data.closeCode, event.data.closeReason);
|
close(event.data.closeCode, event.data.closeReason);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
port.postMessage(<WorkerResponse>{ type: "websocket" });
|
port.postMessage(<WorkerResponse>{ type: "websocket" });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
port.postMessage(<WorkerResponse>{ type: "error", error: err });
|
port.postMessage(<WorkerResponse>{ type: "error", error: err });
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue