diff --git a/package.json b/package.json index 4556edd..2cbcdb5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mercuryworkshop/bare-mux", - "version": "2.1.4", + "version": "2.1.5", "description": "", "type": "module", "scripts": { diff --git a/src/client.ts b/src/client.ts index 4714b96..788647e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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({ 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); diff --git a/src/index.ts b/src/index.ts index ffd085c..3bec628 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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})`); \ No newline at end of file +console.debug(`bare-mux: running v${self.BARE_MUX_VERSION} (build ${self.BARE_MUX_COMMITHASH})`); diff --git a/src/snapshot.ts b/src/snapshot.ts index a61ba10..6d0eb6b 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -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, }; diff --git a/src/websocket.ts b/src/websocket.ts index 05b8aaf..124bbde 100644 --- a/src/websocket.ts +++ b/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 }); + } } diff --git a/src/worker.ts b/src/worker.ts index 75d7a4d..394c19e 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -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})`); \ No newline at end of file +console.debug(`bare-mux: running v${self.BARE_MUX_VERSION} (build ${self.BARE_MUX_COMMITHASH})`);