remove host header

This commit is contained in:
Toshit Chawda 2024-10-20 18:07:16 -07:00
parent 4c62e7a692
commit 76bda04e7c
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
6 changed files with 78 additions and 82 deletions

View file

@ -1,6 +1,6 @@
{
"name": "@mercuryworkshop/bare-mux",
"version": "2.1.4",
"version": "2.1.5",
"description": "",
"type": "module",
"scripts": {

View file

@ -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);

View file

@ -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})`);

View file

@ -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,
};

View file

@ -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 });
}
}

View file

@ -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})`);