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",
|
"name": "@mercuryworkshop/bare-mux",
|
||||||
"version": "2.1.4",
|
"version": "2.1.5",
|
||||||
"description": "",
|
"description": "",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -185,9 +185,6 @@ export class BareClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; ; i++) {
|
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>{
|
let resp = (await this.worker.sendMessage(<WorkerMessage>{
|
||||||
type: "fetch",
|
type: "fetch",
|
||||||
fetch: {
|
fetch: {
|
||||||
|
@ -204,7 +201,6 @@ export class BareClient {
|
||||||
status: resp.status,
|
status: resp.status,
|
||||||
statusText: resp.statusText,
|
statusText: resp.statusText,
|
||||||
}) as BareResponse;
|
}) as BareResponse;
|
||||||
responseobj.rawHeaders = resp.headers;
|
|
||||||
responseobj.rawResponse = new Response(resp.body);
|
responseobj.rawResponse = new Response(resp.body);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,4 +11,4 @@ export type * from './connection';
|
||||||
export type * from "./snapshot";
|
export type * from "./snapshot";
|
||||||
|
|
||||||
//@ts-expect-error this gets filled in
|
//@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 nativePostMessage = MessagePort.prototype.postMessage;
|
||||||
|
|
||||||
export const WebSocketFields = {
|
export const WebSocketFields = {
|
||||||
prototype: {
|
prototype: {
|
||||||
send: WebSocket.prototype.send,
|
send: WebSocket.prototype.send,
|
||||||
},
|
},
|
||||||
CLOSED: WebSocket.CLOSED,
|
CLOSED: WebSocket.CLOSED,
|
||||||
CLOSING: WebSocket.CLOSING,
|
CLOSING: WebSocket.CLOSING,
|
||||||
CONNECTING: WebSocket.CONNECTING,
|
CONNECTING: WebSocket.CONNECTING,
|
||||||
OPEN: WebSocket.OPEN,
|
OPEN: WebSocket.OPEN,
|
||||||
};
|
};
|
||||||
|
|
136
src/websocket.ts
136
src/websocket.ts
|
@ -3,85 +3,85 @@ import { WebSocketFields, nativePostMessage } from "./snapshot";
|
||||||
import { BareHeaders } from "./baretypes";
|
import { BareHeaders } from "./baretypes";
|
||||||
|
|
||||||
export class BareWebSocket extends EventTarget {
|
export class BareWebSocket extends EventTarget {
|
||||||
url: string;
|
url: string;
|
||||||
readyState: number = WebSocketFields.CONNECTING;
|
readyState: number = WebSocketFields.CONNECTING;
|
||||||
|
|
||||||
channel: MessageChannel;
|
channel: MessageChannel;
|
||||||
constructor(
|
constructor(
|
||||||
remote: string | URL,
|
remote: string | URL,
|
||||||
public protocols: string | string[] | undefined = [],
|
public protocols: string | string[] | undefined = [],
|
||||||
worker: WorkerConnection,
|
worker: WorkerConnection,
|
||||||
requestHeaders?: BareHeaders,
|
requestHeaders?: BareHeaders,
|
||||||
) {
|
) {
|
||||||
super();
|
super();
|
||||||
this.url = remote.toString();
|
this.url = remote.toString();
|
||||||
this.protocols = protocols;
|
this.protocols = protocols;
|
||||||
|
|
||||||
const onopen = (protocol: string) => {
|
const onopen = (protocol: string) => {
|
||||||
this.protocols = protocol;
|
this.protocols = protocol;
|
||||||
this.readyState = WebSocketFields.OPEN;
|
this.readyState = WebSocketFields.OPEN;
|
||||||
|
|
||||||
const event = new Event("open")
|
const event = new Event("open")
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onmessage = async (payload) => {
|
const onmessage = async (payload) => {
|
||||||
const event = new MessageEvent("message", { data: payload });
|
const event = new MessageEvent("message", { data: payload });
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onclose = (code: number, reason: string) => {
|
const onclose = (code: number, reason: string) => {
|
||||||
this.readyState = WebSocketFields.CLOSED;
|
this.readyState = WebSocketFields.CLOSED;
|
||||||
const event = new CloseEvent("close", { code, reason })
|
const event = new CloseEvent("close", { code, reason })
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onerror = () => {
|
const onerror = () => {
|
||||||
this.readyState = WebSocketFields.CLOSED;
|
this.readyState = WebSocketFields.CLOSED;
|
||||||
const event = new Event("error");
|
const event = new Event("error");
|
||||||
this.dispatchEvent(event);
|
this.dispatchEvent(event);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.channel = new MessageChannel();
|
this.channel = new MessageChannel();
|
||||||
|
|
||||||
this.channel.port1.onmessage = event => {
|
this.channel.port1.onmessage = event => {
|
||||||
if (event.data.type === "open") {
|
if (event.data.type === "open") {
|
||||||
onopen(event.data.args[0]);
|
onopen(event.data.args[0]);
|
||||||
} else if (event.data.type === "message") {
|
} else if (event.data.type === "message") {
|
||||||
onmessage(event.data.args[0]);
|
onmessage(event.data.args[0]);
|
||||||
} else if (event.data.type === "close") {
|
} else if (event.data.type === "close") {
|
||||||
onclose(event.data.args[0], event.data.args[1]);
|
onclose(event.data.args[0], event.data.args[1]);
|
||||||
} else if (event.data.type === "error") {
|
} else if (event.data.type === "error") {
|
||||||
onerror(/* event.data.args[0] */);
|
onerror(/* event.data.args[0] */);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
worker.sendMessage({
|
worker.sendMessage({
|
||||||
type: "websocket",
|
type: "websocket",
|
||||||
websocket: {
|
websocket: {
|
||||||
url: remote.toString(),
|
url: remote.toString(),
|
||||||
//@ts-expect-error
|
//@ts-expect-error
|
||||||
protocols: protocols,
|
protocols: protocols,
|
||||||
requestHeaders: requestHeaders,
|
requestHeaders: requestHeaders,
|
||||||
channel: this.channel.port2,
|
channel: this.channel.port2,
|
||||||
},
|
},
|
||||||
}, [this.channel.port2])
|
}, [this.channel.port2])
|
||||||
}
|
}
|
||||||
|
|
||||||
send(...args) {
|
send(...args) {
|
||||||
if (this.readyState === WebSocketFields.CONNECTING) {
|
if (this.readyState === WebSocketFields.CONNECTING) {
|
||||||
throw new DOMException(
|
throw new DOMException(
|
||||||
"Failed to execute 'send' on 'WebSocket': Still in CONNECTING state."
|
"Failed to execute 'send' on 'WebSocket': Still in CONNECTING state."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = args[0];
|
let data = args[0];
|
||||||
if (data.buffer) data = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
|
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) {
|
close(code, reason) {
|
||||||
nativePostMessage.call(this.channel.port1, { type: "close", closeCode: code, closeReason: 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
|
//@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