migrate to $scramjet object from __scramjet

This commit is contained in:
Percs 2024-07-14 05:34:55 -05:00
parent 146c202ae2
commit cf9e5a3418
13 changed files with 77 additions and 67 deletions

View file

@ -15,8 +15,7 @@ import "./scope.ts";
declare global { declare global {
interface Window { interface Window {
//@ts-ignore scope function cant be typed $s: any;
__s: any;
} }
} }
@ -31,4 +30,4 @@ export const {
rewriteWorkers, rewriteWorkers,
isScramjetFile, isScramjetFile,
BareClient BareClient
} = self.__scramjet$bundle; } = self.$scramjet.bundle;

View file

@ -12,4 +12,4 @@ function scope(identifier: any) {
} }
// shorthand because this can get out of hand reall quickly // shorthand because this can get out of hand reall quickly
window.__s = scope; window.$s = scope;

View file

@ -67,17 +67,10 @@ const base64 = {
} }
} }
declare global { if (!self.$scramjet) {
interface Window { //@ts-expect-error really dumb workaround
__scramjet$codecs: { self.$scramjet = {}
none: Codec;
plain: Codec;
base64: Codec;
xor: Codec;
}
}
} }
self.$scramjet.codecs = {
self.__scramjet$codecs = {
none, plain, base64, xor none, plain, base64, xor
} }

View file

@ -1,22 +1,10 @@
import { Codec } from "./codecs"; if (!self.$scramjet) {
//@ts-expect-error really dumb workaround
declare global { self.$scramjet = {}
interface Window {
__scramjet$config: {
prefix: string;
codec: Codec
config: string;
shared: string;
worker: string;
client: string;
codecs: string;
}
}
} }
self.$scramjet.config = {
self.__scramjet$config = {
prefix: "/scramjet/", prefix: "/scramjet/",
codec: self.__scramjet$codecs.plain, codec: self.$scramjet.codecs.plain,
config: "/scram/scramjet.config.js", config: "/scram/scramjet.config.js",
shared: "/scram/scramjet.shared.js", shared: "/scram/scramjet.shared.js",
worker: "/scram/scramjet.worker.js", worker: "/scram/scramjet.worker.js",

View file

@ -7,24 +7,11 @@ import { rewriteWorkers } from "./rewriters/worker";
import { isScramjetFile } from "./rewriters/html"; import { isScramjetFile } from "./rewriters/html";
import { BareClient } from "@mercuryworkshop/bare-mux"; import { BareClient } from "@mercuryworkshop/bare-mux";
declare global { if (!self.$scramjet) {
interface Window { //@ts-expect-error really dumb workaround
__scramjet$bundle: { self.$scramjet = {}
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
}
}
} }
self.$scramjet.bundle = {
self.__scramjet$bundle = {
encodeUrl, encodeUrl,
decodeUrl, decodeUrl,
rewriteCss, rewriteCss,

View file

@ -9,7 +9,7 @@ import { rewriteJs } from "./js";
export function isScramjetFile(src: string) { export function isScramjetFile(src: string) {
let bool = false; let bool = false;
["codecs", "client", "shared", "worker", "config"].forEach((file) => { ["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; return bool;
@ -73,7 +73,7 @@ function traverseParsedHtml(node, origin?: URL) {
const scramjetScripts = []; const scramjetScripts = [];
["codecs", "config", "shared", "client"].forEach((script) => { ["codecs", "config", "shared", "client"].forEach((script) => {
scramjetScripts.push(new Element("script", { scramjetScripts.push(new Element("script", {
src: self.__scramjet$config[script], src: self.$scramjet.config[script],
"data-scramjet": "" "data-scramjet": ""
})); }));
}); });

View file

@ -63,7 +63,7 @@ export function rewriteJs(js: string, origin?: URL) {
// js rweriting notrdone // js rweriting notrdone
MemberExpression: (node: ESTree.MemberExpression) => { MemberExpression: (node: ESTree.MemberExpression) => {
if (node.object.type === "Identifier" && identifierList.includes(node.object.name)) { if (node.object.type === "Identifier" && identifierList.includes(node.object.name)) {
node.object.name = `__s(${node.object.name})`; node.object.name = `$s(${node.object.name})`;
} }
} }
}); });

View file

@ -13,7 +13,7 @@ function canParseUrl(url: string, origin?: URL) {
// something is broken with this but i didn't debug it // something is broken with this but i didn't debug it
export function encodeUrl(url: string, origin?: URL) { export function encodeUrl(url: string, origin?: URL) {
if (!origin) { 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:")) { if (url.startsWith("javascript:")) {
@ -21,7 +21,7 @@ export function encodeUrl(url: string, origin?: URL) {
} else if (/^(#|mailto|about|data)/.test(url)) { } else if (/^(#|mailto|about|data)/.test(url)) {
return url; return url;
} else if (canParseUrl(url, origin)) { } 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)) { if (/^(#|about|data|mailto|javascript)/.test(url)) {
return url; return url;
} else if (canParseUrl(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 { } else {
return url; return url;
} }

View file

@ -3,7 +3,7 @@ export function rewriteWorkers(js: string, origin?: URL) {
let str = new String().toString() let str = new String().toString()
//@ts-expect-error //@ts-expect-error
["codecs", "config", "shared", "client"].forEach((script) => { ["codecs", "config", "shared", "client"].forEach((script) => {
str += `import "${self.__scramjet$config[script]}"\n` str += `import "${self.$scramjet.config[script]}"\n`
}) })
str += rewriteJs(js, origin); str += rewriteJs(js, origin);

43
src/types.d.ts vendored Normal file
View file

@ -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;
}
}
}
}

View file

@ -7,10 +7,10 @@ declare global {
} }
self.ScramjetServiceWorker = class ScramjetServiceWorker { self.ScramjetServiceWorker = class ScramjetServiceWorker {
client: typeof self.__scramjet$bundle.BareClient.prototype; client: typeof self.$scramjet.bundle.BareClient.prototype;
config: typeof self.__scramjet$config; config: typeof self.$scramjet.config;
constructor(config = self.__scramjet$config) { constructor(config = self.$scramjet.config) {
this.client = new self.__scramjet$bundle.BareClient(); this.client = new self.$scramjet.bundle.BareClient();
if (!config.prefix) config.prefix = "/scramjet/"; if (!config.prefix) config.prefix = "/scramjet/";
this.config = config; this.config = config;
} }
@ -22,7 +22,7 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker {
async fetch({ request }: FetchEvent) { async fetch({ request }: FetchEvent) {
const urlParam = new URLSearchParams(new URL(request.url).search); 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")) { if (urlParam.has("url")) {
return Response.redirect(encodeUrl(urlParam.get("url"), new URL(urlParam.get("url")))) return Response.redirect(encodeUrl(urlParam.get("url"), new URL(urlParam.get("url"))))

View file

@ -4,8 +4,8 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title> <title>Document</title>
<link rel="prefetch" href="/scram/scramjet.worker.js" type="module"> <link rel="prefetch" href="/scram/scramjet.worker.js">
<link rel="prefetch" href="/scram/scramjet.shared.js" type="module"> <link rel="prefetch" href="/scram/scramjet.shared.js">
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&amp;family=Inter+Tight:ital,wght@0,100..900;1,100..900&amp;family=Inter:wght@100..900&amp;display=swap&amp;" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&amp;family=Inter+Tight:ital,wght@0,100..900;1,100..900&amp;family=Inter:wght@100..900&amp;display=swap&amp;" rel="stylesheet">
<style> <style>
body, html, #app { body, html, #app {
@ -22,8 +22,8 @@
<body> <body>
<script src="https://unpkg.com/dreamland"></script> <script src="https://unpkg.com/dreamland"></script>
<script src="/baremux/index.js" defer></script> <script src="/baremux/index.js" defer></script>
<script src="/scram/scramjet.codecs.js"></script> <script src="/scram/scramjet.codecs.js" defer></script>
<script src="/scram/scramjet.config.js"></script> <script src="/scram/scramjet.config.js" defer></script>
<script src="ui.js" defer></script> <script src="ui.js" defer></script>
</body> </body>
</html> </html>

View file

@ -1,5 +1,5 @@
navigator.serviceWorker.register("./sw.js", { navigator.serviceWorker.register("./sw.js", {
scope: __scramjet$config.prefix scope: $scramjet.config.prefix
}) })
const connection = new BareMux.BareMuxConnection("/baremux/worker.js") const connection = new BareMux.BareMuxConnection("/baremux/worker.js")
const flex = css`display: flex;`; const flex = css`display: flex;`;
@ -88,7 +88,7 @@ function App() {
<button on:click=${() => window.open(this.urlencoded)}>open in fullscreen</button> <button on:click=${() => window.open(this.urlencoded)}>open in fullscreen</button>
</div> </div>
</div> </div>
<input class="bar" bind:value=${use(store.url)} on:input=${(e) => (store.url = e.target.value)} on:keyup=${(e) => e.keyCode == 13 && console.log(this.urlencoded = __scramjet$config.prefix + __scramjet$config.codec.encode(e.target.value))}></input> <input class="bar" bind:value=${use(store.url)} on:input=${(e) => (store.url = e.target.value)} on:keyup=${(e) => e.keyCode == 13 && console.log(this.urlencoded = $scramjet.config.prefix + $scramjet.config.codec.encode(e.target.value))}></input>
<iframe src=${use(this.urlencoded)}></iframe> <iframe src=${use(this.urlencoded)}></iframe>
</div> </div>
` `