mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 14:50:03 -04:00
fix some stuff
This commit is contained in:
parent
8ed59e901d
commit
508b265df2
3 changed files with 15 additions and 15 deletions
|
@ -254,8 +254,8 @@ export class BareClient {
|
|||
origin: origin,
|
||||
protocols: protocols,
|
||||
requestHeaders: requestHeaders,
|
||||
channel: channel.port2,
|
||||
},
|
||||
websocketChannel: channel.port2,
|
||||
}, [channel.port2])
|
||||
|
||||
// protocol is always an empty before connecting
|
||||
|
@ -356,15 +356,15 @@ export class BareClient {
|
|||
if ('host' in headers) headers.host = urlO.host;
|
||||
else headers.Host = urlO.host;
|
||||
|
||||
const message = Object.assign({
|
||||
let resp = (await this.worker.sendMessage(<WorkerMessage>{
|
||||
type: "fetch",
|
||||
fetch: {
|
||||
remote: urlO.toString(),
|
||||
method: req.method,
|
||||
headers: headers,
|
||||
body: body || undefined,
|
||||
},
|
||||
}, body ? { fetchBody: body } : {});
|
||||
let resp = (await this.worker.sendMessage(message as WorkerMessage, body ? [body] : [])).fetch;
|
||||
}, body ? [body] : [])).fetch;
|
||||
|
||||
let responseobj: BareResponse & Partial<BareResponseFetch> = new Response(
|
||||
statusEmpty.includes(resp.status) ? undefined : resp.body, {
|
||||
|
|
|
@ -18,15 +18,15 @@ export type WorkerMessage = {
|
|||
remote: string,
|
||||
method: string,
|
||||
headers: BareHeaders,
|
||||
},
|
||||
fetchBody?: ReadableStream,
|
||||
body: ReadableStream | undefined,
|
||||
}
|
||||
websocket?: {
|
||||
url: string,
|
||||
origin: string,
|
||||
protocols: string[],
|
||||
requestHeaders: BareHeaders,
|
||||
channel: MessagePort,
|
||||
},
|
||||
websocketChannel?: MessagePort,
|
||||
client?: string,
|
||||
};
|
||||
|
||||
|
@ -64,7 +64,7 @@ export class WorkerConnection {
|
|||
// create the SharedWorker and help other bare-mux clients get the workerPath
|
||||
navigator.serviceWorker.addEventListener("message", event => {
|
||||
if (event.data.type === "getPort" && event.data.port) {
|
||||
const worker = new SharedWorker(workerPath);
|
||||
const worker = new SharedWorker(workerPath, "bare-mux-worker");
|
||||
event.data.port.postMessage(worker.port, [worker.port]);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -19,7 +19,7 @@ function handleConnection(port: MessagePort) {
|
|||
const resp = await currentTransport.request(
|
||||
new URL(message.fetch.remote),
|
||||
message.fetch.method,
|
||||
message.fetchBody,
|
||||
message.fetch.body,
|
||||
message.fetch.headers,
|
||||
null
|
||||
);
|
||||
|
@ -37,19 +37,19 @@ function handleConnection(port: MessagePort) {
|
|||
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] });
|
||||
message.websocket.channel.postMessage({ type: "open", args: [protocol] });
|
||||
};
|
||||
const onclose = (code: number, reason: string) => {
|
||||
message.websocketChannel.postMessage({ type: "close", args: [code, reason] });
|
||||
message.websocket.channel.postMessage({ type: "close", args: [code, reason] });
|
||||
};
|
||||
const onerror = (error: string) => {
|
||||
message.websocketChannel.postMessage({ type: "error", args: [error] });
|
||||
message.websocket.channel.postMessage({ type: "error", args: [error] });
|
||||
};
|
||||
const onmessage = (data: Blob | ArrayBuffer | string) => {
|
||||
if (data instanceof ArrayBuffer) {
|
||||
message.websocketChannel.postMessage({ type: "message", args: [data] }, [data]);
|
||||
message.websocket.channel.postMessage({ type: "message", args: [data] }, [data]);
|
||||
} else {
|
||||
message.websocketChannel.postMessage({ type: "message", args: [data] });
|
||||
message.websocket.channel.postMessage({ type: "message", args: [data] });
|
||||
}
|
||||
}
|
||||
const [data, close] = currentTransport.connect(
|
||||
|
@ -62,7 +62,7 @@ function handleConnection(port: MessagePort) {
|
|||
onclose,
|
||||
onerror,
|
||||
);
|
||||
message.websocketChannel.onmessage = (event: MessageEvent) => {
|
||||
message.websocket.channel.onmessage = (event: MessageEvent) => {
|
||||
if (event.data.type === "data") {
|
||||
data(event.data.data);
|
||||
} else if (event.data.type === "close") {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue