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";
|
||||
const conn = new BareMuxConnection("/bare-mux/worker.js");
|
||||
// Set Bare-Client transport
|
||||
await conn.setTransport(`
|
||||
await conn.setManualTransport(`
|
||||
const exports = await import("/bare-mux/index.js");
|
||||
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
|
||||
import { BareClient } from "@mercuryworkshop/bare-mux";
|
||||
|
|
|
@ -107,10 +107,17 @@ export class BareMuxConnection {
|
|||
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({
|
||||
type: "set",
|
||||
client: transport,
|
||||
client: functionBody,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,16 @@ function handleConnection(port: MessagePort) {
|
|||
port.onmessage = async (event: MessageEvent) => {
|
||||
const port = event.data.port;
|
||||
const message: WorkerMessage = event.data.message;
|
||||
|
||||
if (message.type === "set") {
|
||||
try {
|
||||
const AsyncFunction = (async function () {}).constructor;
|
||||
|
||||
// @ts-expect-error
|
||||
const func = new AsyncFunction(message.client);
|
||||
console.log(func);
|
||||
currentTransport = await func();
|
||||
console.log("set transport to ", currentTransport);
|
||||
|
||||
port.postMessage(<WorkerResponse>{ type: "set" });
|
||||
} catch(err) {
|
||||
port.postMessage(<WorkerResponse>{ type: "error", error: err });
|
||||
|
@ -23,6 +25,7 @@ function handleConnection(port: MessagePort) {
|
|||
try {
|
||||
if (!currentTransport) throw new Error("No BareTransport was set. Try creating a BareMuxConnection and calling set() on it.");
|
||||
if (!currentTransport.ready) await currentTransport.init();
|
||||
|
||||
const resp = await currentTransport.request(
|
||||
new URL(message.fetch.remote),
|
||||
message.fetch.method,
|
||||
|
@ -43,6 +46,7 @@ function handleConnection(port: MessagePort) {
|
|||
try {
|
||||
if (!currentTransport) throw new Error("No BareTransport was set. Try creating a BareMuxConnection and calling set() on it.");
|
||||
if (!currentTransport.ready) await currentTransport.init();
|
||||
|
||||
const onopen = (protocol: string) => {
|
||||
message.websocket.channel.postMessage({ type: "open", args: [protocol] });
|
||||
};
|
||||
|
@ -76,6 +80,7 @@ function handleConnection(port: MessagePort) {
|
|||
close(event.data.closeCode, event.data.closeReason);
|
||||
}
|
||||
}
|
||||
|
||||
port.postMessage(<WorkerResponse>{ type: "websocket" });
|
||||
} catch (err) {
|
||||
port.postMessage(<WorkerResponse>{ type: "error", error: err });
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue