From cf9e5a3418b95ef07e67dab3a1b15ba3c8b7b471 Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Sun, 14 Jul 2024 05:34:55 -0500 Subject: [PATCH] migrate to $scramjet object from __scramjet --- src/client/index.ts | 5 ++-- src/client/scope.ts | 2 +- src/codecs/index.ts | 15 ++++-------- src/scramjet.config.ts | 22 ++++------------- src/shared/index.ts | 21 ++++------------- src/shared/rewriters/html.ts | 4 ++-- src/shared/rewriters/js.ts | 2 +- src/shared/rewriters/url.ts | 6 ++--- src/shared/rewriters/worker.ts | 2 +- src/types.d.ts | 43 ++++++++++++++++++++++++++++++++++ src/worker/index.ts | 10 ++++---- static/index.html | 8 +++---- static/ui.js | 4 ++-- 13 files changed, 77 insertions(+), 67 deletions(-) create mode 100644 src/types.d.ts diff --git a/src/client/index.ts b/src/client/index.ts index f4823c9..10f3c3e 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -15,8 +15,7 @@ import "./scope.ts"; declare global { interface Window { - //@ts-ignore scope function cant be typed - __s: any; + $s: any; } } @@ -31,4 +30,4 @@ export const { rewriteWorkers, isScramjetFile, BareClient -} = self.__scramjet$bundle; \ No newline at end of file +} = self.$scramjet.bundle; \ No newline at end of file diff --git a/src/client/scope.ts b/src/client/scope.ts index a4abf47..847650d 100644 --- a/src/client/scope.ts +++ b/src/client/scope.ts @@ -12,4 +12,4 @@ function scope(identifier: any) { } // shorthand because this can get out of hand reall quickly -window.__s = scope; +window.$s = scope; diff --git a/src/codecs/index.ts b/src/codecs/index.ts index 2069396..5b64a77 100644 --- a/src/codecs/index.ts +++ b/src/codecs/index.ts @@ -67,17 +67,10 @@ const base64 = { } } -declare global { - interface Window { - __scramjet$codecs: { - none: Codec; - plain: Codec; - base64: Codec; - xor: Codec; - } - } +if (!self.$scramjet) { + //@ts-expect-error really dumb workaround + self.$scramjet = {} } - -self.__scramjet$codecs = { +self.$scramjet.codecs = { none, plain, base64, xor } \ No newline at end of file diff --git a/src/scramjet.config.ts b/src/scramjet.config.ts index 923ed86..d00051c 100644 --- a/src/scramjet.config.ts +++ b/src/scramjet.config.ts @@ -1,22 +1,10 @@ -import { Codec } from "./codecs"; - -declare global { - interface Window { - __scramjet$config: { - prefix: string; - codec: Codec - config: string; - shared: string; - worker: string; - client: string; - codecs: string; - } - } +if (!self.$scramjet) { + //@ts-expect-error really dumb workaround + self.$scramjet = {} } - -self.__scramjet$config = { +self.$scramjet.config = { prefix: "/scramjet/", - codec: self.__scramjet$codecs.plain, + codec: self.$scramjet.codecs.plain, config: "/scram/scramjet.config.js", shared: "/scram/scramjet.shared.js", worker: "/scram/scramjet.worker.js", diff --git a/src/shared/index.ts b/src/shared/index.ts index 30bfa92..9b92585 100644 --- a/src/shared/index.ts +++ b/src/shared/index.ts @@ -7,24 +7,11 @@ import { rewriteWorkers } from "./rewriters/worker"; import { isScramjetFile } from "./rewriters/html"; import { BareClient } from "@mercuryworkshop/bare-mux"; -declare global { - interface Window { - __scramjet$bundle: { - encodeUrl: typeof encodeUrl; - decodeUrl: typeof decodeUrl; - rewriteCss: typeof rewriteCss; - rewriteHtml: typeof rewriteHtml; - rewriteSrcset: typeof rewriteSrcset; - rewriteJs: typeof rewriteJs; - rewriteHeaders: typeof rewriteHeaders; - rewriteWorkers: typeof rewriteWorkers; - BareClient: typeof BareClient; - isScramjetFile: typeof isScramjetFile - } - } +if (!self.$scramjet) { + //@ts-expect-error really dumb workaround + self.$scramjet = {} } - -self.__scramjet$bundle = { +self.$scramjet.bundle = { encodeUrl, decodeUrl, rewriteCss, diff --git a/src/shared/rewriters/html.ts b/src/shared/rewriters/html.ts index fc52ac3..89acd92 100644 --- a/src/shared/rewriters/html.ts +++ b/src/shared/rewriters/html.ts @@ -9,7 +9,7 @@ import { rewriteJs } from "./js"; export function isScramjetFile(src: string) { let bool = false; ["codecs", "client", "shared", "worker", "config"].forEach((file) => { - if (src === self.__scramjet$config[file]) bool = true; + if (src === self.$scramjet.config[file]) bool = true; }); return bool; @@ -73,7 +73,7 @@ function traverseParsedHtml(node, origin?: URL) { const scramjetScripts = []; ["codecs", "config", "shared", "client"].forEach((script) => { scramjetScripts.push(new Element("script", { - src: self.__scramjet$config[script], + src: self.$scramjet.config[script], "data-scramjet": "" })); }); diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index fda1e55..6713837 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -63,7 +63,7 @@ export function rewriteJs(js: string, origin?: URL) { // js rweriting notrdone MemberExpression: (node: ESTree.MemberExpression) => { if (node.object.type === "Identifier" && identifierList.includes(node.object.name)) { - node.object.name = `__s(${node.object.name})`; + node.object.name = `$s(${node.object.name})`; } } }); diff --git a/src/shared/rewriters/url.ts b/src/shared/rewriters/url.ts index c7f8d11..c596ffd 100644 --- a/src/shared/rewriters/url.ts +++ b/src/shared/rewriters/url.ts @@ -13,7 +13,7 @@ function canParseUrl(url: string, origin?: URL) { // something is broken with this but i didn't debug it export function encodeUrl(url: string, origin?: URL) { if (!origin) { - origin = new URL(self.__scramjet$config.codec.decode(location.href.slice((location.origin + self.__scramjet$config.prefix).length))); + origin = new URL(self.$scramjet.config.codec.decode(location.href.slice((location.origin + self.$scramjet.config.prefix).length))); } if (url.startsWith("javascript:")) { @@ -21,7 +21,7 @@ export function encodeUrl(url: string, origin?: URL) { } else if (/^(#|mailto|about|data)/.test(url)) { return url; } else if (canParseUrl(url, origin)) { - return location.origin + self.__scramjet$config.prefix + self.__scramjet$config.codec.encode(new URL(url, origin).href); + return location.origin + self.$scramjet.config.prefix + self.$scramjet.config.codec.encode(new URL(url, origin).href); } } @@ -30,7 +30,7 @@ export function decodeUrl(url: string) { if (/^(#|about|data|mailto|javascript)/.test(url)) { return url; } else if (canParseUrl(url)) { - return self.__scramjet$config.codec.decode(url.slice((location.origin + self.__scramjet$config.prefix).length)) + return self.$scramjet.config.codec.decode(url.slice((location.origin + self.$scramjet.config.prefix).length)) } else { return url; } diff --git a/src/shared/rewriters/worker.ts b/src/shared/rewriters/worker.ts index d1d0148..27ea9ff 100644 --- a/src/shared/rewriters/worker.ts +++ b/src/shared/rewriters/worker.ts @@ -3,7 +3,7 @@ export function rewriteWorkers(js: string, origin?: URL) { let str = new String().toString() //@ts-expect-error ["codecs", "config", "shared", "client"].forEach((script) => { - str += `import "${self.__scramjet$config[script]}"\n` + str += `import "${self.$scramjet.config[script]}"\n` }) str += rewriteJs(js, origin); diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..b36d34d --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,43 @@ +import { encodeUrl, decodeUrl } from "./shared/rewriters/url"; +import { rewriteCss } from "./shared/rewriters/css"; +import { rewriteHtml, rewriteSrcset } from "./shared/rewriters/html"; +import { rewriteJs } from "./shared/rewriters/js"; +import { rewriteHeaders } from "./shared/rewriters/headers"; +import { rewriteWorkers } from "./shared/rewriters/worker"; +import { isScramjetFile } from "./shared/rewriters/html"; +import type { Codec } from "./codecs" +import { BareClient } from "@mercuryworkshop/bare-mux"; + +declare global { + interface Window { + $scramjet: { + bundle: { + encodeUrl: typeof encodeUrl, + decodeUrl: typeof decodeUrl, + rewriteCss: typeof rewriteCss, + rewriteHtml: typeof rewriteHtml, + rewriteSrcset: typeof rewriteSrcset, + rewriteJs: typeof rewriteJs, + rewriteHeaders: typeof rewriteHeaders, + rewriteWorkers: typeof rewriteWorkers, + BareClient: typeof BareClient, + isScramjetFile: typeof isScramjetFile, + } + config: { + prefix: string; + codec: Codec + config: string; + shared: string; + worker: string; + client: string; + codecs: string; + }, + codecs: { + none: Codec; + plain: Codec; + base64: Codec; + xor: Codec; + } + } + } +} \ No newline at end of file diff --git a/src/worker/index.ts b/src/worker/index.ts index cb88bfd..88a2264 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -7,10 +7,10 @@ declare global { } self.ScramjetServiceWorker = class ScramjetServiceWorker { - client: typeof self.__scramjet$bundle.BareClient.prototype; - config: typeof self.__scramjet$config; - constructor(config = self.__scramjet$config) { - this.client = new self.__scramjet$bundle.BareClient(); + client: typeof self.$scramjet.bundle.BareClient.prototype; + config: typeof self.$scramjet.config; + constructor(config = self.$scramjet.config) { + this.client = new self.$scramjet.bundle.BareClient(); if (!config.prefix) config.prefix = "/scramjet/"; this.config = config; } @@ -22,7 +22,7 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker { async fetch({ request }: FetchEvent) { const urlParam = new URLSearchParams(new URL(request.url).search); - const { encodeUrl, decodeUrl, rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = self.__scramjet$bundle; + const { encodeUrl, decodeUrl, rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = self.$scramjet.bundle; if (urlParam.has("url")) { return Response.redirect(encodeUrl(urlParam.get("url"), new URL(urlParam.get("url")))) diff --git a/static/index.html b/static/index.html index c083312..d211db0 100644 --- a/static/index.html +++ b/static/index.html @@ -4,8 +4,8 @@