mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 06:20:02 -04:00
prettier
This commit is contained in:
parent
fa779017ad
commit
d6dd1d4ca3
20 changed files with 363 additions and 379 deletions
|
@ -25,6 +25,6 @@
|
|||
"no-unreachable": "warn",
|
||||
"no-undef": "off",
|
||||
"@typescript-eslint/no-explicit-any": "off",
|
||||
"@typescript-eslint/ban-ts-comment": "off",
|
||||
"@typescript-eslint/ban-ts-comment": "off"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
window.location.href = "http://example.com";
|
||||
|
||||
window.location.href = "http://example.com"
|
||||
console.log(top.window.aaa);
|
||||
consle.log(globalThis["win" + "dow"]);
|
||||
|
||||
globalThis.eval("..");
|
||||
|
||||
console.log(top.window.aaa)
|
||||
consle.log(globalThis["win" + "dow"])
|
||||
|
||||
globalThis.eval("..")
|
||||
|
||||
let ref = { b: this.top.window, c: globalThis["win" + "dow"] }
|
||||
let ref = { b: this.top.window, c: globalThis["win" + "dow"] };
|
||||
|
|
|
@ -51,5 +51,5 @@ export default defineConfig({
|
|||
// })
|
||||
],
|
||||
watch: true,
|
||||
target: "webworker"
|
||||
target: "webworker",
|
||||
});
|
||||
|
|
|
@ -50,7 +50,9 @@ for (const attr of attrs) {
|
|||
|
||||
if (["nonce", "integrity", "csp"].includes(attr)) {
|
||||
return;
|
||||
} else if (["src", "data", "href", "action", "formaction"].includes(attr)) {
|
||||
} else if (
|
||||
["src", "data", "href", "action", "formaction"].includes(attr)
|
||||
) {
|
||||
value = encodeUrl(value);
|
||||
} else if (attr === "srcdoc") {
|
||||
value = rewriteHtml(value);
|
||||
|
@ -88,7 +90,9 @@ Element.prototype.setAttribute = new Proxy(Element.prototype.setAttribute, {
|
|||
thisArg.__origattrs[argArray[0]] = argArray[1];
|
||||
if (["nonce", "integrity", "csp"].includes(argArray[0])) {
|
||||
return;
|
||||
} else if (["src", "data", "href", "action", "formaction"].includes(argArray[0])) {
|
||||
} else if (
|
||||
["src", "data", "href", "action", "formaction"].includes(argArray[0])
|
||||
) {
|
||||
argArray[1] = encodeUrl(argArray[1]);
|
||||
} else if (argArray[0] === "srcdoc") {
|
||||
argArray[1] = rewriteHtml(argArray[1]);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { encodeUrl } from "../shared/rewriters/url"
|
||||
import { encodeUrl } from "../shared/rewriters/url";
|
||||
|
||||
window.$sImport = function (url) {
|
||||
return (function() { }.constructor(`return import("${encodeUrl(url)}")`))();
|
||||
}
|
||||
return function () {}.constructor(`return import("${encodeUrl(url)}")`)();
|
||||
};
|
||||
|
|
|
@ -12,7 +12,7 @@ import "./storage.ts";
|
|||
import "./css.ts";
|
||||
import "./history.ts";
|
||||
import "./worker.ts";
|
||||
import "./beacon.ts"
|
||||
import "./beacon.ts";
|
||||
import "./origin.ts";
|
||||
import "./import.ts";
|
||||
|
||||
|
|
|
@ -11,9 +11,11 @@ function createLocation() {
|
|||
return loc;
|
||||
}
|
||||
|
||||
export const locationProxy = new Proxy({
|
||||
host: ""
|
||||
}, {
|
||||
export const locationProxy = new Proxy(
|
||||
{
|
||||
host: "",
|
||||
},
|
||||
{
|
||||
get(target, prop) {
|
||||
const loc = createLocation();
|
||||
|
||||
|
@ -31,4 +33,5 @@ export const locationProxy = new Proxy({
|
|||
|
||||
return true;
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { decodeUrl } from "../shared/rewriters/url";
|
||||
|
||||
|
||||
// const descriptor = Object.getOwnPropertyDescriptor(window, "origin");
|
||||
delete window.origin;
|
||||
|
||||
|
@ -13,15 +12,14 @@ Object.defineProperty(window, "origin", {
|
|||
},
|
||||
});
|
||||
|
||||
|
||||
Object.defineProperty(document, "URL", {
|
||||
get() {
|
||||
return decodeUrl(location.href);
|
||||
},
|
||||
set() {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
},
|
||||
});
|
||||
|
||||
Object.defineProperty(document, "baseURI", {
|
||||
get() {
|
||||
|
@ -29,8 +27,8 @@ Object.defineProperty(document, "baseURI", {
|
|||
},
|
||||
set() {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
},
|
||||
});
|
||||
|
||||
Object.defineProperty(document, "domain", {
|
||||
get() {
|
||||
|
@ -38,5 +36,5 @@ Object.defineProperty(document, "domain", {
|
|||
},
|
||||
set() {
|
||||
return false;
|
||||
}
|
||||
})
|
||||
},
|
||||
});
|
||||
|
|
|
@ -10,7 +10,7 @@ WebSocket = new Proxy(WebSocket, {
|
|||
target,
|
||||
{
|
||||
"User-Agent": navigator.userAgent,
|
||||
"Origin": new URL(decodeUrl(location.href)).origin,
|
||||
Origin: new URL(decodeUrl(location.href)).origin,
|
||||
},
|
||||
ArrayBuffer.prototype
|
||||
);
|
||||
|
|
|
@ -3,7 +3,11 @@ import { documentProxy, windowProxy } from "./window";
|
|||
|
||||
function scope(identifier: any) {
|
||||
// this will break iframe postmessage!
|
||||
if (identifier instanceof Window || identifier instanceof top.window.Window || identifier instanceof parent.window.Window) {
|
||||
if (
|
||||
identifier instanceof Window ||
|
||||
identifier instanceof top.window.Window ||
|
||||
identifier instanceof parent.window.Window
|
||||
) {
|
||||
return windowProxy;
|
||||
} else if (identifier instanceof Location) {
|
||||
return locationProxy;
|
||||
|
|
|
@ -12,7 +12,7 @@ export const windowProxy = new Proxy(window, {
|
|||
) {
|
||||
return windowProxy;
|
||||
} else if (propIsString && prop == "parent") {
|
||||
return window.parent
|
||||
return window.parent;
|
||||
} else if (propIsString && prop === "$scramjet") {
|
||||
return;
|
||||
} else if (propIsString && prop === "addEventListener") {
|
||||
|
@ -27,7 +27,6 @@ export const windowProxy = new Proxy(window, {
|
|||
|
||||
const value = Reflect.get(target, prop);
|
||||
|
||||
|
||||
// this is bad! i don't know what the right thing to do is
|
||||
if (typeof value === "function") {
|
||||
return new Proxy(value, {
|
||||
|
@ -55,7 +54,6 @@ export const windowProxy = new Proxy(window, {
|
|||
},
|
||||
});
|
||||
|
||||
|
||||
export const documentProxy = new Proxy(document, {
|
||||
get(target, prop) {
|
||||
const propIsString = typeof prop === "string";
|
||||
|
@ -78,7 +76,6 @@ export const documentProxy = new Proxy(document, {
|
|||
},
|
||||
set(target, prop, newValue) {
|
||||
if (typeof prop === "string" && prop === "location") {
|
||||
|
||||
//@ts-ignore
|
||||
location = new URL(encodeUrl(newValue));
|
||||
|
||||
|
@ -86,5 +83,5 @@ export const documentProxy = new Proxy(document, {
|
|||
}
|
||||
|
||||
return Reflect.set(target, prop, newValue);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
|
|
@ -19,9 +19,11 @@ import * as ESTree from "estree";
|
|||
import { initSync, rewrite_js } from "../../../rewriter/out/rewriter.js";
|
||||
import "../../../static/wasm.js";
|
||||
|
||||
initSync(new WebAssembly.Module(
|
||||
Uint8Array.from(atob(self.WASM), c => c.charCodeAt(0))
|
||||
))
|
||||
initSync(
|
||||
new WebAssembly.Module(
|
||||
Uint8Array.from(atob(self.WASM), (c) => c.charCodeAt(0))
|
||||
)
|
||||
);
|
||||
|
||||
global.rws = rewriteJs;
|
||||
export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
|
||||
|
@ -154,4 +156,3 @@ export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
|
|||
// return js;
|
||||
// }
|
||||
}
|
||||
|
||||
|
|
|
@ -9,4 +9,3 @@ export function rewriteWorkers(js: string, origin?: URL) {
|
|||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,45 +1,39 @@
|
|||
import { rewriteJs } from "../shared/rewriters/js";
|
||||
|
||||
|
||||
|
||||
// @ts-ignore
|
||||
onconnect = (e) => {
|
||||
const port = e.ports[0];
|
||||
|
||||
|
||||
console.log("thread: connected to port", port)
|
||||
console.log("thread: connected to port", port);
|
||||
port.postMessage("ready");
|
||||
|
||||
let syncToken = 0;
|
||||
port.onmessage = ({ data }) => {
|
||||
console.log("thread: received message", data)
|
||||
console.log("thread: received message", data);
|
||||
const [task, ...args] = data;
|
||||
let token = syncToken++;
|
||||
|
||||
try {
|
||||
let res = tasks[task](...args);
|
||||
console.log("thread: task", task, "completed with token", token)
|
||||
console.log("thread: task", task, "completed with token", token);
|
||||
port.postMessage({
|
||||
token,
|
||||
result: res
|
||||
})
|
||||
result: res,
|
||||
});
|
||||
} catch (e) {
|
||||
port.postMessage({
|
||||
token,
|
||||
error: e.message
|
||||
})
|
||||
error: e.message,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
port.postMessage("idle");
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
const tasks = {
|
||||
"rewriteJs": taskRewriteJs,
|
||||
}
|
||||
|
||||
rewriteJs: taskRewriteJs,
|
||||
};
|
||||
|
||||
function taskRewriteJs(js: ArrayBuffer, origin: string): string {
|
||||
return rewriteJs(js, new URL(origin));
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
export function errorTemplate(trace: string, fetchedURL: string) {
|
||||
// turn script into a data URI so we don"t have to escape any HTML values
|
||||
const script = `
|
||||
|
@ -43,7 +42,8 @@ export function errorTemplate(trace: string, fetchedURL: string) {
|
|||
<button id="reload">Reload</button>
|
||||
<hr />
|
||||
<p><i>Scramjet v<span id="version"></span></i></p>
|
||||
<script src="${"data:application/javascript," + encodeURIComponent(script)
|
||||
<script src="${
|
||||
"data:application/javascript," + encodeURIComponent(script)
|
||||
}"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -63,4 +63,3 @@ export function renderError(err: unknown, fetchedURL: string) {
|
|||
headers: headers,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -4,16 +4,14 @@ import { ParseResultType } from "parse-domain";
|
|||
import { ScramjetServiceWorker } from ".";
|
||||
import { renderError } from "./error";
|
||||
|
||||
export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEvent) {
|
||||
export async function swfetch(
|
||||
this: ScramjetServiceWorker,
|
||||
{ request }: FetchEvent
|
||||
) {
|
||||
const urlParam = new URLSearchParams(new URL(request.url).search);
|
||||
const { encodeUrl, decodeUrl } = self.$scramjet.shared.url;
|
||||
const {
|
||||
rewriteHeaders,
|
||||
rewriteHtml,
|
||||
rewriteJs,
|
||||
rewriteCss,
|
||||
rewriteWorkers,
|
||||
} = self.$scramjet.shared.rewrite;
|
||||
const { rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } =
|
||||
self.$scramjet.shared.rewrite;
|
||||
const { parseDomain } = self.$scramjet.shared.util;
|
||||
|
||||
if (urlParam.has("url")) {
|
||||
|
@ -50,7 +48,7 @@ export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEve
|
|||
|
||||
let [key, value] = cookieParsed.shift();
|
||||
if (!value) continue;
|
||||
value = value.replace("\"", "");
|
||||
value = value.replace('"', "");
|
||||
|
||||
const hostArg = cookieParsed.find((x) => x[0] === "Domain");
|
||||
cookieParsed = cookieParsed.filter((x) => x[0] !== "Domain");
|
||||
|
@ -70,8 +68,7 @@ export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEve
|
|||
|
||||
if (urlDomain.type === ParseResultType.Listed) {
|
||||
const { subDomains: domain, topLevelDomains } = urlDomain;
|
||||
if (!host.endsWith([domain, ...topLevelDomains].join(".")))
|
||||
continue;
|
||||
if (!host.endsWith([domain, ...topLevelDomains].join("."))) continue;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
@ -105,9 +102,7 @@ export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEve
|
|||
case "iframe":
|
||||
case "document":
|
||||
if (
|
||||
responseHeaders["content-type"]
|
||||
?.toString()
|
||||
?.startsWith("text/html")
|
||||
responseHeaders["content-type"]?.toString()?.startsWith("text/html")
|
||||
) {
|
||||
responseBody = rewriteHtml(await response.text(), url);
|
||||
} else {
|
||||
|
@ -139,9 +134,7 @@ export async function swfetch(this: ScramjetServiceWorker, { request }: FetchEve
|
|||
if (!/\s*?((inline|attachment);\s*?)filename=/i.test(header)) {
|
||||
// if filename= wasn"t specified then maybe the remote specified to download this as an attachment?
|
||||
// if it"s invalid then we can still possibly test for the attachment/inline type
|
||||
const type = /^\s*?attachment/i.test(header)
|
||||
? "attachment"
|
||||
: "inline";
|
||||
const type = /^\s*?attachment/i.test(header) ? "attachment" : "inline";
|
||||
|
||||
// set the filename
|
||||
const [filename] = new URL(response.finalURL).pathname
|
||||
|
|
|
@ -18,7 +18,6 @@ export class ScramjetServiceWorker {
|
|||
this.config = config;
|
||||
|
||||
this.threadpool = new ScramjetThreadpool();
|
||||
|
||||
}
|
||||
|
||||
route({ request }: FetchEvent) {
|
||||
|
@ -28,7 +27,6 @@ export class ScramjetServiceWorker {
|
|||
}
|
||||
|
||||
public fetch = swfetch;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
self.ScramjetServiceWorker = ScramjetServiceWorker;
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
|
||||
type Thread = {
|
||||
handle: MessagePort;
|
||||
ready: boolean;
|
||||
busy: boolean;
|
||||
syncToken: number;
|
||||
promises: Map<number, { resolve, reject }>;
|
||||
}
|
||||
promises: Map<number, { resolve; reject }>;
|
||||
};
|
||||
|
||||
export class ScramjetThreadpool {
|
||||
threads: Thread[] = [];
|
||||
|
@ -23,8 +22,8 @@ export class ScramjetThreadpool {
|
|||
ready: false,
|
||||
busy: false,
|
||||
syncToken: 0,
|
||||
promises: new Map()
|
||||
}
|
||||
promises: new Map(),
|
||||
};
|
||||
|
||||
this.threads.push(thread);
|
||||
|
||||
|
@ -47,14 +46,14 @@ export class ScramjetThreadpool {
|
|||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
thread.handle.start();
|
||||
}
|
||||
|
||||
pick(): Thread | undefined {
|
||||
const alive = this.threads.filter(t => t.ready);
|
||||
const idle = alive.filter(t => !t.busy);
|
||||
const alive = this.threads.filter((t) => t.ready);
|
||||
const idle = alive.filter((t) => !t.busy);
|
||||
|
||||
// no threads
|
||||
if (!alive.length) return;
|
||||
|
@ -71,21 +70,16 @@ export class ScramjetThreadpool {
|
|||
if (!thread) throw new Error("No threads available");
|
||||
thread.busy = true;
|
||||
|
||||
|
||||
let token = thread.syncToken++;
|
||||
|
||||
// console.log("runthread: dispatching task", task, "to thread", thread, "of token", token)
|
||||
return new Promise((resolve, reject) => {
|
||||
thread.promises.set(token, { resolve, reject });
|
||||
|
||||
thread.handle.postMessage([
|
||||
task,
|
||||
...args
|
||||
], transferrable);
|
||||
thread.handle.postMessage([task, ...args], transferrable);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async rewriteJs(js: ArrayBuffer, origin: string): Promise<string> {
|
||||
return await this.run("rewriteJs", [js, origin], [js]);
|
||||
}
|
||||
|
|
18
static/ui.js
18
static/ui.js
|
@ -1,18 +1,20 @@
|
|||
navigator.serviceWorker
|
||||
.register("./sw.js")
|
||||
.then((reg) => {
|
||||
navigator.serviceWorker.register("./sw.js").then((reg) => {
|
||||
reg.update();
|
||||
});
|
||||
|
||||
navigator.serviceWorker.ready.then((reg) => {
|
||||
for (let i = 0; i < 20; i++) {
|
||||
const thread = new SharedWorker($scramjet.config.thread, { name: "thread" + i });
|
||||
const thread = new SharedWorker($scramjet.config.thread, {
|
||||
name: "thread" + i,
|
||||
});
|
||||
|
||||
reg.active.postMessage({
|
||||
reg.active.postMessage(
|
||||
{
|
||||
scramjet$type: "add",
|
||||
handle: thread.port
|
||||
}, [thread.port]);
|
||||
|
||||
handle: thread.port,
|
||||
},
|
||||
[thread.port]
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue