Format
This commit is contained in:
parent
df25d7882d
commit
bb26ff2e98
4 changed files with 689 additions and 661 deletions
|
|
@ -51,11 +51,12 @@ self.addEventListener("fetch", (event) => {
|
||||||
(async function () {
|
(async function () {
|
||||||
return await uv.fetch(event);
|
return await uv.fetch(event);
|
||||||
})()
|
})()
|
||||||
)}
|
);
|
||||||
else {
|
} else {
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
(async function () {
|
(async function () {
|
||||||
return await fetch(event.request);
|
return await fetch(event.request);
|
||||||
})()
|
})()
|
||||||
)}
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,14 @@
|
||||||
//Built from: https://github.com/motortruck1221/bare-as-module3 (commit: 36759f801e0009027878edecff156408b06404c6)
|
//Built from: https://github.com/motortruck1221/bare-as-module3 (commit: 36759f801e0009027878edecff156408b06404c6)
|
||||||
(function (global, factory) {
|
(function (global, factory) {
|
||||||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
typeof exports === "object" && typeof module !== "undefined"
|
||||||
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
? factory(exports)
|
||||||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.BareMod = {}));
|
: typeof define === "function" && define.amd
|
||||||
})(this, (function (exports) { 'use strict';
|
? define(["exports"], factory)
|
||||||
|
: ((global =
|
||||||
|
typeof globalThis !== "undefined" ? globalThis : global || self),
|
||||||
|
factory((global.BareMod = {})));
|
||||||
|
})(this, function (exports) {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
// The user likely has overwritten all networking functions after importing bare-client
|
// The user likely has overwritten all networking functions after importing bare-client
|
||||||
// It is our responsibility to make sure components of Bare-Client are using native networking functions
|
// It is our responsibility to make sure components of Bare-Client are using native networking functions
|
||||||
|
|
@ -12,12 +17,12 @@
|
||||||
const WebSocket = globalThis.WebSocket;
|
const WebSocket = globalThis.WebSocket;
|
||||||
const WebSocketFields = {
|
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
|
||||||
};
|
};
|
||||||
|
|
||||||
class BareError extends Error {
|
class BareError extends Error {
|
||||||
|
|
@ -254,7 +259,7 @@
|
||||||
* @returns MD5 string
|
* @returns MD5 string
|
||||||
*/
|
*/
|
||||||
function binl2rstr(input) {
|
function binl2rstr(input) {
|
||||||
let output = '';
|
let output = "";
|
||||||
const length32 = input.length * 32;
|
const length32 = input.length * 32;
|
||||||
for (let i = 0; i < length32; i += 8) {
|
for (let i = 0; i < length32; i += 8) {
|
||||||
output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff);
|
output += String.fromCharCode((input[i >> 5] >>> i % 32) & 0xff);
|
||||||
|
|
@ -317,8 +322,8 @@
|
||||||
* @returns Hex encoded string
|
* @returns Hex encoded string
|
||||||
*/
|
*/
|
||||||
function rstr2hex(input) {
|
function rstr2hex(input) {
|
||||||
const hexTab = '0123456789abcdef';
|
const hexTab = "0123456789abcdef";
|
||||||
let output = '';
|
let output = "";
|
||||||
for (let i = 0; i < input.length; i += 1) {
|
for (let i = 0; i < input.length; i += 1) {
|
||||||
const x = input.charCodeAt(i);
|
const x = input.charCodeAt(i);
|
||||||
output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f);
|
output += hexTab.charAt((x >>> 4) & 0x0f) + hexTab.charAt(x & 0x0f);
|
||||||
|
|
@ -404,10 +409,10 @@
|
||||||
*/
|
*/
|
||||||
function splitHeaders(headers) {
|
function splitHeaders(headers) {
|
||||||
const output = new Headers(headers);
|
const output = new Headers(headers);
|
||||||
if (headers.has('x-bare-headers')) {
|
if (headers.has("x-bare-headers")) {
|
||||||
const value = headers.get('x-bare-headers');
|
const value = headers.get("x-bare-headers");
|
||||||
if (value.length > MAX_HEADER_VALUE) {
|
if (value.length > MAX_HEADER_VALUE) {
|
||||||
output.delete('x-bare-headers');
|
output.delete("x-bare-headers");
|
||||||
let split = 0;
|
let split = 0;
|
||||||
for (let i = 0; i < value.length; i += MAX_HEADER_VALUE) {
|
for (let i = 0; i < value.length; i += MAX_HEADER_VALUE) {
|
||||||
const part = value.slice(i, i + MAX_HEADER_VALUE);
|
const part = value.slice(i, i + MAX_HEADER_VALUE);
|
||||||
|
|
@ -425,25 +430,25 @@
|
||||||
*/
|
*/
|
||||||
function joinHeaders(headers) {
|
function joinHeaders(headers) {
|
||||||
const output = new Headers(headers);
|
const output = new Headers(headers);
|
||||||
const prefix = 'x-bare-headers';
|
const prefix = "x-bare-headers";
|
||||||
if (headers.has(`${prefix}-0`)) {
|
if (headers.has(`${prefix}-0`)) {
|
||||||
const join = [];
|
const join = [];
|
||||||
for (const [header, value] of headers) {
|
for (const [header, value] of headers) {
|
||||||
if (!header.startsWith(prefix)) {
|
if (!header.startsWith(prefix)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!value.startsWith(';')) {
|
if (!value.startsWith(";")) {
|
||||||
throw new BareError(400, {
|
throw new BareError(400, {
|
||||||
code: 'INVALID_BARE_HEADER',
|
code: "INVALID_BARE_HEADER",
|
||||||
id: `request.headers.${header}`,
|
id: `request.headers.${header}`,
|
||||||
message: `Value didn't begin with semi-colon.`,
|
message: `Value didn't begin with semi-colon.`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const id = parseInt(header.slice(prefix.length + 1));
|
const id = parseInt(header.slice(prefix.length + 1));
|
||||||
join[id] = value.slice(1);
|
join[id] = value.slice(1);
|
||||||
output.delete(header);
|
output.delete(header);
|
||||||
}
|
}
|
||||||
output.set(prefix, join.join(''));
|
output.set(prefix, join.join(""));
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
@ -458,22 +463,30 @@
|
||||||
super(3, server);
|
super(3, server);
|
||||||
this.ws = new URL(this.base);
|
this.ws = new URL(this.base);
|
||||||
this.http = new URL(this.base);
|
this.http = new URL(this.base);
|
||||||
if (this.ws.protocol === 'https:') {
|
if (this.ws.protocol === "https:") {
|
||||||
this.ws.protocol = 'wss:';
|
this.ws.protocol = "wss:";
|
||||||
}
|
} else {
|
||||||
else {
|
this.ws.protocol = "ws:";
|
||||||
this.ws.protocol = 'ws:';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ready = true;
|
ready = true;
|
||||||
async init() {
|
async init() {
|
||||||
this.ready = true;
|
this.ready = true;
|
||||||
}
|
}
|
||||||
connect(url, origin, protocols, requestHeaders, onopen, onmessage, onclose, onerror) {
|
connect(
|
||||||
|
url,
|
||||||
|
origin,
|
||||||
|
protocols,
|
||||||
|
requestHeaders,
|
||||||
|
onopen,
|
||||||
|
onmessage,
|
||||||
|
onclose,
|
||||||
|
onerror
|
||||||
|
) {
|
||||||
const ws = new WebSocket(this.ws);
|
const ws = new WebSocket(this.ws);
|
||||||
const cleanup = () => {
|
const cleanup = () => {
|
||||||
ws.removeEventListener('close', closeListener);
|
ws.removeEventListener("close", closeListener);
|
||||||
ws.removeEventListener('message', messageListener);
|
ws.removeEventListener("message", messageListener);
|
||||||
};
|
};
|
||||||
const closeListener = () => {
|
const closeListener = () => {
|
||||||
cleanup();
|
cleanup();
|
||||||
|
|
@ -481,12 +494,14 @@
|
||||||
const messageListener = (event) => {
|
const messageListener = (event) => {
|
||||||
cleanup();
|
cleanup();
|
||||||
// ws.binaryType is irrelevant when sending text
|
// ws.binaryType is irrelevant when sending text
|
||||||
if (typeof event.data !== 'string')
|
if (typeof event.data !== "string")
|
||||||
throw new TypeError('the first websocket message was not a text frame');
|
throw new TypeError(
|
||||||
|
"the first websocket message was not a text frame"
|
||||||
|
);
|
||||||
const message = JSON.parse(event.data);
|
const message = JSON.parse(event.data);
|
||||||
// finally
|
// finally
|
||||||
if (message.type !== 'open')
|
if (message.type !== "open")
|
||||||
throw new TypeError('message was not of open type');
|
throw new TypeError("message was not of open type");
|
||||||
// onMeta({
|
// onMeta({
|
||||||
// protocol: message.protocol,
|
// protocol: message.protocol,
|
||||||
// setCookies: message.setCookies,
|
// setCookies: message.setCookies,
|
||||||
|
|
@ -497,35 +512,44 @@
|
||||||
onmessage(ev.data);
|
onmessage(ev.data);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
ws.addEventListener('close', closeListener);
|
ws.addEventListener("close", closeListener);
|
||||||
ws.addEventListener('message', messageListener);
|
ws.addEventListener("message", messageListener);
|
||||||
// CONNECTED TO THE BARE SERVER, NOT THE REMOTE
|
// CONNECTED TO THE BARE SERVER, NOT THE REMOTE
|
||||||
ws.addEventListener('open', (event) => {
|
ws.addEventListener(
|
||||||
|
"open",
|
||||||
|
(event) => {
|
||||||
// getRequestHeaders().then((headers:any) =>
|
// getRequestHeaders().then((headers:any) =>
|
||||||
WebSocketFields.prototype.send.call(ws, JSON.stringify({
|
WebSocketFields.prototype.send.call(
|
||||||
type: 'connect',
|
ws,
|
||||||
|
JSON.stringify({
|
||||||
|
type: "connect",
|
||||||
remote: url.toString(),
|
remote: url.toString(),
|
||||||
protocols,
|
protocols,
|
||||||
headers: requestHeaders,
|
headers: requestHeaders,
|
||||||
forwardHeaders: [],
|
forwardHeaders: []
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
// );
|
// );
|
||||||
},
|
},
|
||||||
// only block the open event once
|
// only block the open event once
|
||||||
{ once: true });
|
{ once: true }
|
||||||
|
);
|
||||||
return ws.send.bind(ws);
|
return ws.send.bind(ws);
|
||||||
}
|
}
|
||||||
async request(remote, method, body, headers, signal) {
|
async request(remote, method, body, headers, signal) {
|
||||||
const options = {
|
const options = {
|
||||||
credentials: 'omit',
|
credentials: "omit",
|
||||||
method: method,
|
method: method,
|
||||||
signal,
|
signal
|
||||||
};
|
};
|
||||||
if (body !== undefined) {
|
if (body !== undefined) {
|
||||||
options.body = body;
|
options.body = body;
|
||||||
}
|
}
|
||||||
options.headers = this.createBareHeaders(remote, headers);
|
options.headers = this.createBareHeaders(remote, headers);
|
||||||
const response = await fetch(this.http + '?cache=' + md5(remote.toString()), options);
|
const response = await fetch(
|
||||||
|
this.http + "?cache=" + md5(remote.toString()),
|
||||||
|
options
|
||||||
|
);
|
||||||
const readResponse = await this.readBareResponse(response);
|
const readResponse = await this.readBareResponse(response);
|
||||||
// const result: Response & Partial<BareResponse> = new Response(
|
// const result: Response & Partial<BareResponse> = new Response(
|
||||||
// statusEmpty.includes(readResponse.status!) ? undefined : response.body,
|
// statusEmpty.includes(readResponse.status!) ? undefined : response.body,
|
||||||
|
|
@ -542,7 +566,7 @@
|
||||||
body: response.body,
|
body: response.body,
|
||||||
headers: readResponse.headers,
|
headers: readResponse.headers,
|
||||||
status: readResponse.status,
|
status: readResponse.status,
|
||||||
statusText: readResponse.statusText,
|
statusText: readResponse.statusText
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
async readBareResponse(response) {
|
async readBareResponse(response) {
|
||||||
|
|
@ -551,29 +575,32 @@
|
||||||
}
|
}
|
||||||
const responseHeaders = joinHeaders(response.headers);
|
const responseHeaders = joinHeaders(response.headers);
|
||||||
const result = {};
|
const result = {};
|
||||||
const xBareStatus = responseHeaders.get('x-bare-status');
|
const xBareStatus = responseHeaders.get("x-bare-status");
|
||||||
if (xBareStatus !== null)
|
if (xBareStatus !== null) result.status = parseInt(xBareStatus);
|
||||||
result.status = parseInt(xBareStatus);
|
const xBareStatusText = responseHeaders.get("x-bare-status-text");
|
||||||
const xBareStatusText = responseHeaders.get('x-bare-status-text');
|
if (xBareStatusText !== null) result.statusText = xBareStatusText;
|
||||||
if (xBareStatusText !== null)
|
const xBareHeaders = responseHeaders.get("x-bare-headers");
|
||||||
result.statusText = xBareStatusText;
|
if (xBareHeaders !== null) result.headers = JSON.parse(xBareHeaders);
|
||||||
const xBareHeaders = responseHeaders.get('x-bare-headers');
|
|
||||||
if (xBareHeaders !== null)
|
|
||||||
result.headers = JSON.parse(xBareHeaders);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
createBareHeaders(remote, bareHeaders, forwardHeaders = [], passHeaders = [], passStatus = []) {
|
createBareHeaders(
|
||||||
|
remote,
|
||||||
|
bareHeaders,
|
||||||
|
forwardHeaders = [],
|
||||||
|
passHeaders = [],
|
||||||
|
passStatus = []
|
||||||
|
) {
|
||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
headers.set('x-bare-url', remote.toString());
|
headers.set("x-bare-url", remote.toString());
|
||||||
headers.set('x-bare-headers', JSON.stringify(bareHeaders));
|
headers.set("x-bare-headers", JSON.stringify(bareHeaders));
|
||||||
for (const header of forwardHeaders) {
|
for (const header of forwardHeaders) {
|
||||||
headers.append('x-bare-forward-headers', header);
|
headers.append("x-bare-forward-headers", header);
|
||||||
}
|
}
|
||||||
for (const header of passHeaders) {
|
for (const header of passHeaders) {
|
||||||
headers.append('x-bare-pass-headers', header);
|
headers.append("x-bare-pass-headers", header);
|
||||||
}
|
}
|
||||||
for (const status of passStatus) {
|
for (const status of passStatus) {
|
||||||
headers.append('x-bare-pass-status', status.toString());
|
headers.append("x-bare-pass-status", status.toString());
|
||||||
}
|
}
|
||||||
splitHeaders(headers);
|
splitHeaders(headers);
|
||||||
return headers;
|
return headers;
|
||||||
|
|
@ -581,6 +608,5 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.BareClient = ClientV3;
|
exports.BareClient = ClientV3;
|
||||||
|
});
|
||||||
}));
|
|
||||||
//# sourceMappingURL=bare.cjs.map
|
//# sourceMappingURL=bare.cjs.map
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,8 @@ function changeTransport(transport: string, wispUrl: string) {
|
||||||
case "bare":
|
case "bare":
|
||||||
localStorage.setItem("transport", "bare");
|
localStorage.setItem("transport", "bare");
|
||||||
console.log("Setting transport to Bare");
|
console.log("Setting transport to Bare");
|
||||||
const bare = localStorage.getItem("bare") || window.location.origin + "/bare/";
|
const bare =
|
||||||
|
localStorage.getItem("bare") || window.location.origin + "/bare/";
|
||||||
console.log("Bare URL: " + bare);
|
console.log("Bare URL: " + bare);
|
||||||
SetTransport("BareMod.BareClient", bare);
|
SetTransport("BareMod.BareClient", bare);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue