mirror of
https://github.com/MercuryWorkshop/bare-mux.git
synced 2025-05-14 14:50:03 -04:00
remove host header
This commit is contained in:
parent
4c62e7a692
commit
76bda04e7c
6 changed files with 78 additions and 82 deletions
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@mercuryworkshop/bare-mux",
|
||||
"version": "2.1.4",
|
||||
"version": "2.1.5",
|
||||
"description": "",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
|
|
@ -185,9 +185,6 @@ export class BareClient {
|
|||
}
|
||||
|
||||
for (let i = 0; ; i++) {
|
||||
if ('host' in headers) headers.host = urlO.host;
|
||||
else headers.Host = urlO.host;
|
||||
|
||||
let resp = (await this.worker.sendMessage(<WorkerMessage>{
|
||||
type: "fetch",
|
||||
fetch: {
|
||||
|
@ -204,7 +201,6 @@ export class BareClient {
|
|||
status: resp.status,
|
||||
statusText: resp.statusText,
|
||||
}) as BareResponse;
|
||||
responseobj.rawHeaders = resp.headers;
|
||||
responseobj.rawResponse = new Response(resp.body);
|
||||
|
||||
|
||||
|
|
|
@ -11,4 +11,4 @@ export type * from './connection';
|
|||
export type * from "./snapshot";
|
||||
|
||||
//@ts-expect-error this gets filled in
|
||||
console.debug(`bare-mux: running v${self.BARE_MUX_VERSION} (build ${self.BARE_MUX_COMMITHASH})`);
|
||||
console.debug(`bare-mux: running v${self.BARE_MUX_VERSION} (build ${self.BARE_MUX_COMMITHASH})`);
|
||||
|
|
|
@ -6,11 +6,11 @@ export const nativeServiceWorker = globalThis.navigator.serviceWorker;
|
|||
export const nativePostMessage = MessagePort.prototype.postMessage;
|
||||
|
||||
export const WebSocketFields = {
|
||||
prototype: {
|
||||
send: WebSocket.prototype.send,
|
||||
},
|
||||
CLOSED: WebSocket.CLOSED,
|
||||
CLOSING: WebSocket.CLOSING,
|
||||
CONNECTING: WebSocket.CONNECTING,
|
||||
OPEN: WebSocket.OPEN,
|
||||
prototype: {
|
||||
send: WebSocket.prototype.send,
|
||||
},
|
||||
CLOSED: WebSocket.CLOSED,
|
||||
CLOSING: WebSocket.CLOSING,
|
||||
CONNECTING: WebSocket.CONNECTING,
|
||||
OPEN: WebSocket.OPEN,
|
||||
};
|
||||
|
|
136
src/websocket.ts
136
src/websocket.ts
|
@ -3,85 +3,85 @@ import { WebSocketFields, nativePostMessage } from "./snapshot";
|
|||
import { BareHeaders } from "./baretypes";
|
||||
|
||||
export class BareWebSocket extends EventTarget {
|
||||
url: string;
|
||||
readyState: number = WebSocketFields.CONNECTING;
|
||||
url: string;
|
||||
readyState: number = WebSocketFields.CONNECTING;
|
||||
|
||||
channel: MessageChannel;
|
||||
constructor(
|
||||
remote: string | URL,
|
||||
public protocols: string | string[] | undefined = [],
|
||||
worker: WorkerConnection,
|
||||
requestHeaders?: BareHeaders,
|
||||
) {
|
||||
super();
|
||||
this.url = remote.toString();
|
||||
this.protocols = protocols;
|
||||
channel: MessageChannel;
|
||||
constructor(
|
||||
remote: string | URL,
|
||||
public protocols: string | string[] | undefined = [],
|
||||
worker: WorkerConnection,
|
||||
requestHeaders?: BareHeaders,
|
||||
) {
|
||||
super();
|
||||
this.url = remote.toString();
|
||||
this.protocols = protocols;
|
||||
|
||||
const onopen = (protocol: string) => {
|
||||
this.protocols = protocol;
|
||||
this.readyState = WebSocketFields.OPEN;
|
||||
const onopen = (protocol: string) => {
|
||||
this.protocols = protocol;
|
||||
this.readyState = WebSocketFields.OPEN;
|
||||
|
||||
const event = new Event("open")
|
||||
this.dispatchEvent(event);
|
||||
};
|
||||
const event = new Event("open")
|
||||
this.dispatchEvent(event);
|
||||
};
|
||||
|
||||
const onmessage = async (payload) => {
|
||||
const event = new MessageEvent("message", { data: payload });
|
||||
this.dispatchEvent(event);
|
||||
};
|
||||
const onmessage = async (payload) => {
|
||||
const event = new MessageEvent("message", { data: payload });
|
||||
this.dispatchEvent(event);
|
||||
};
|
||||
|
||||
const onclose = (code: number, reason: string) => {
|
||||
this.readyState = WebSocketFields.CLOSED;
|
||||
const event = new CloseEvent("close", { code, reason })
|
||||
this.dispatchEvent(event);
|
||||
};
|
||||
const onclose = (code: number, reason: string) => {
|
||||
this.readyState = WebSocketFields.CLOSED;
|
||||
const event = new CloseEvent("close", { code, reason })
|
||||
this.dispatchEvent(event);
|
||||
};
|
||||
|
||||
const onerror = () => {
|
||||
this.readyState = WebSocketFields.CLOSED;
|
||||
const event = new Event("error");
|
||||
this.dispatchEvent(event);
|
||||
};
|
||||
const onerror = () => {
|
||||
this.readyState = WebSocketFields.CLOSED;
|
||||
const event = new Event("error");
|
||||
this.dispatchEvent(event);
|
||||
};
|
||||
|
||||
this.channel = new MessageChannel();
|
||||
this.channel = new MessageChannel();
|
||||
|
||||
this.channel.port1.onmessage = event => {
|
||||
if (event.data.type === "open") {
|
||||
onopen(event.data.args[0]);
|
||||
} else if (event.data.type === "message") {
|
||||
onmessage(event.data.args[0]);
|
||||
} else if (event.data.type === "close") {
|
||||
onclose(event.data.args[0], event.data.args[1]);
|
||||
} else if (event.data.type === "error") {
|
||||
onerror(/* event.data.args[0] */);
|
||||
}
|
||||
}
|
||||
this.channel.port1.onmessage = event => {
|
||||
if (event.data.type === "open") {
|
||||
onopen(event.data.args[0]);
|
||||
} else if (event.data.type === "message") {
|
||||
onmessage(event.data.args[0]);
|
||||
} else if (event.data.type === "close") {
|
||||
onclose(event.data.args[0], event.data.args[1]);
|
||||
} else if (event.data.type === "error") {
|
||||
onerror(/* event.data.args[0] */);
|
||||
}
|
||||
}
|
||||
|
||||
worker.sendMessage({
|
||||
type: "websocket",
|
||||
websocket: {
|
||||
url: remote.toString(),
|
||||
//@ts-expect-error
|
||||
protocols: protocols,
|
||||
requestHeaders: requestHeaders,
|
||||
channel: this.channel.port2,
|
||||
},
|
||||
}, [this.channel.port2])
|
||||
}
|
||||
worker.sendMessage({
|
||||
type: "websocket",
|
||||
websocket: {
|
||||
url: remote.toString(),
|
||||
//@ts-expect-error
|
||||
protocols: protocols,
|
||||
requestHeaders: requestHeaders,
|
||||
channel: this.channel.port2,
|
||||
},
|
||||
}, [this.channel.port2])
|
||||
}
|
||||
|
||||
send(...args) {
|
||||
if (this.readyState === WebSocketFields.CONNECTING) {
|
||||
throw new DOMException(
|
||||
"Failed to execute 'send' on 'WebSocket': Still in CONNECTING state."
|
||||
);
|
||||
}
|
||||
send(...args) {
|
||||
if (this.readyState === WebSocketFields.CONNECTING) {
|
||||
throw new DOMException(
|
||||
"Failed to execute 'send' on 'WebSocket': Still in CONNECTING state."
|
||||
);
|
||||
}
|
||||
|
||||
let data = args[0];
|
||||
if (data.buffer) data = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
|
||||
let data = args[0];
|
||||
if (data.buffer) data = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
|
||||
|
||||
nativePostMessage.call(this.channel.port1, { type: "data", data: data }, data instanceof ArrayBuffer ? [data] : []);
|
||||
}
|
||||
nativePostMessage.call(this.channel.port1, { type: "data", data: data }, data instanceof ArrayBuffer ? [data] : []);
|
||||
}
|
||||
|
||||
close(code, reason) {
|
||||
nativePostMessage.call(this.channel.port1, { type: "close", closeCode: code, closeReason: reason });
|
||||
}
|
||||
close(code, reason) {
|
||||
nativePostMessage.call(this.channel.port1, { type: "close", closeCode: code, closeReason: reason });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -88,4 +88,4 @@ self.onconnect = (event: MessageEvent) => {
|
|||
}
|
||||
|
||||
//@ts-expect-error this gets filled in
|
||||
console.debug(`bare-mux: running v${self.BARE_MUX_VERSION} (build ${self.BARE_MUX_COMMITHASH})`);
|
||||
console.debug(`bare-mux: running v${self.BARE_MUX_VERSION} (build ${self.BARE_MUX_COMMITHASH})`);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue