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,
|
origin: origin,
|
||||||
protocols: protocols,
|
protocols: protocols,
|
||||||
requestHeaders: requestHeaders,
|
requestHeaders: requestHeaders,
|
||||||
|
channel: channel.port2,
|
||||||
},
|
},
|
||||||
websocketChannel: channel.port2,
|
|
||||||
}, [channel.port2])
|
}, [channel.port2])
|
||||||
|
|
||||||
// protocol is always an empty before connecting
|
// protocol is always an empty before connecting
|
||||||
|
@ -356,15 +356,15 @@ export class BareClient {
|
||||||
if ('host' in headers) headers.host = urlO.host;
|
if ('host' in headers) headers.host = urlO.host;
|
||||||
else headers.Host = urlO.host;
|
else headers.Host = urlO.host;
|
||||||
|
|
||||||
const message = Object.assign({
|
let resp = (await this.worker.sendMessage(<WorkerMessage>{
|
||||||
type: "fetch",
|
type: "fetch",
|
||||||
fetch: {
|
fetch: {
|
||||||
remote: urlO.toString(),
|
remote: urlO.toString(),
|
||||||
method: req.method,
|
method: req.method,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
|
body: body || undefined,
|
||||||
},
|
},
|
||||||
}, body ? { fetchBody: body } : {});
|
}, body ? [body] : [])).fetch;
|
||||||
let resp = (await this.worker.sendMessage(message as WorkerMessage, body ? [body] : [])).fetch;
|
|
||||||
|
|
||||||
let responseobj: BareResponse & Partial<BareResponseFetch> = new Response(
|
let responseobj: BareResponse & Partial<BareResponseFetch> = new Response(
|
||||||
statusEmpty.includes(resp.status) ? undefined : resp.body, {
|
statusEmpty.includes(resp.status) ? undefined : resp.body, {
|
||||||
|
|
|
@ -18,15 +18,15 @@ export type WorkerMessage = {
|
||||||
remote: string,
|
remote: string,
|
||||||
method: string,
|
method: string,
|
||||||
headers: BareHeaders,
|
headers: BareHeaders,
|
||||||
},
|
body: ReadableStream | undefined,
|
||||||
fetchBody?: ReadableStream,
|
}
|
||||||
websocket?: {
|
websocket?: {
|
||||||
url: string,
|
url: string,
|
||||||
origin: string,
|
origin: string,
|
||||||
protocols: string[],
|
protocols: string[],
|
||||||
requestHeaders: BareHeaders,
|
requestHeaders: BareHeaders,
|
||||||
|
channel: MessagePort,
|
||||||
},
|
},
|
||||||
websocketChannel?: MessagePort,
|
|
||||||
client?: string,
|
client?: string,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ export class WorkerConnection {
|
||||||
// create the SharedWorker and help other bare-mux clients get the workerPath
|
// create the SharedWorker and help other bare-mux clients get the workerPath
|
||||||
navigator.serviceWorker.addEventListener("message", event => {
|
navigator.serviceWorker.addEventListener("message", event => {
|
||||||
if (event.data.type === "getPort" && event.data.port) {
|
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]);
|
event.data.port.postMessage(worker.port, [worker.port]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -19,7 +19,7 @@ function handleConnection(port: MessagePort) {
|
||||||
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,
|
||||||
message.fetchBody,
|
message.fetch.body,
|
||||||
message.fetch.headers,
|
message.fetch.headers,
|
||||||
null
|
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) 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.websocketChannel.postMessage({ type: "open", args: [protocol] });
|
message.websocket.channel.postMessage({ type: "open", args: [protocol] });
|
||||||
};
|
};
|
||||||
const onclose = (code: number, reason: string) => {
|
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) => {
|
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) => {
|
const onmessage = (data: Blob | ArrayBuffer | string) => {
|
||||||
if (data instanceof ArrayBuffer) {
|
if (data instanceof ArrayBuffer) {
|
||||||
message.websocketChannel.postMessage({ type: "message", args: [data] }, [data]);
|
message.websocket.channel.postMessage({ type: "message", args: [data] }, [data]);
|
||||||
} else {
|
} else {
|
||||||
message.websocketChannel.postMessage({ type: "message", args: [data] });
|
message.websocket.channel.postMessage({ type: "message", args: [data] });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const [data, close] = currentTransport.connect(
|
const [data, close] = currentTransport.connect(
|
||||||
|
@ -62,7 +62,7 @@ function handleConnection(port: MessagePort) {
|
||||||
onclose,
|
onclose,
|
||||||
onerror,
|
onerror,
|
||||||
);
|
);
|
||||||
message.websocketChannel.onmessage = (event: MessageEvent) => {
|
message.websocket.channel.onmessage = (event: MessageEvent) => {
|
||||||
if (event.data.type === "data") {
|
if (event.data.type === "data") {
|
||||||
data(event.data.data);
|
data(event.data.data);
|
||||||
} else if (event.data.type === "close") {
|
} else if (event.data.type === "close") {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue