mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 23:00:01 -04:00
websocket support
This commit is contained in:
parent
5355f49957
commit
e2b85c3d99
3 changed files with 298 additions and 19 deletions
|
@ -1,5 +1,5 @@
|
|||
import { BareTransport } from "./baretypes";
|
||||
import { WorkerMessage } from "./connection"
|
||||
import { WorkerMessage, WorkerResponse } from "./connection"
|
||||
|
||||
let currentTransport: BareTransport | null = null;
|
||||
|
||||
|
@ -11,7 +11,7 @@ function handleConnection(port: MessagePort) {
|
|||
const func = new Function(message.client);
|
||||
currentTransport = await func();
|
||||
console.log("set transport to ", currentTransport);
|
||||
port.postMessage({ type: "set" });
|
||||
port.postMessage(<WorkerResponse>{ type: "set" });
|
||||
} else if (message.type === "fetch") {
|
||||
try {
|
||||
if (!currentTransport) throw new Error("No BareTransport was set. Try creating a BareMuxConnection and calling set() on it.");
|
||||
|
@ -19,18 +19,59 @@ function handleConnection(port: MessagePort) {
|
|||
const resp = await currentTransport.request(
|
||||
new URL(message.fetch.remote),
|
||||
message.fetch.method,
|
||||
message.fetch.body,
|
||||
message.fetchBody,
|
||||
message.fetch.headers,
|
||||
null
|
||||
);
|
||||
|
||||
if (resp.body instanceof ReadableStream || resp.body instanceof ArrayBuffer) {
|
||||
port.postMessage({ type: "fetch", fetch: resp }, [resp.body]);
|
||||
port.postMessage(<WorkerResponse>{ type: "fetch", fetch: resp }, [resp.body]);
|
||||
} else {
|
||||
port.postMessage({ type: "fetch", fetch: resp });
|
||||
port.postMessage(<WorkerResponse>{ type: "fetch", fetch: resp });
|
||||
}
|
||||
} catch (err) {
|
||||
port.postMessage({ type: "error", error: err });
|
||||
port.postMessage(<WorkerResponse>{ type: "error", error: err });
|
||||
}
|
||||
} else if (message.type === "websocket") {
|
||||
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.websocketChannel.postMessage({ type: "open", args: [protocol] });
|
||||
};
|
||||
const onclose = (code: number, reason: string) => {
|
||||
message.websocketChannel.postMessage({ type: "close", args: [code, reason] });
|
||||
};
|
||||
const onerror = (error: string) => {
|
||||
message.websocketChannel.postMessage({ type: "error", args: [error] });
|
||||
};
|
||||
const onmessage = (data: Blob | ArrayBuffer | string) => {
|
||||
if (data instanceof ArrayBuffer) {
|
||||
message.websocketChannel.postMessage({ type: "message", args: [data] }, [data]);
|
||||
} else {
|
||||
message.websocketChannel.postMessage({ type: "message", args: [data] });
|
||||
}
|
||||
}
|
||||
const [data, close] = currentTransport.connect(
|
||||
new URL(message.websocket.url),
|
||||
message.websocket.origin,
|
||||
message.websocket.protocols,
|
||||
message.websocket.requestHeaders,
|
||||
onopen,
|
||||
onmessage,
|
||||
onclose,
|
||||
onerror,
|
||||
);
|
||||
message.websocketChannel.onmessage = (event: MessageEvent) => {
|
||||
if (event.data.type === "data") {
|
||||
data(event.data.data);
|
||||
} else if (event.data.type === "close") {
|
||||
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