diff --git a/rollup.config.js b/rollup.config.js index fb615a0..a325e19 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,5 +1,5 @@ import typescript from "rollup-plugin-typescript2"; -import { nodeResolve } from '@rollup/plugin-node-resolve'; +import { nodeResolve } from "@rollup/plugin-node-resolve"; import { join } from "node:path"; import fs from "node:fs" import { fileURLToPath } from "node:url"; @@ -27,8 +27,8 @@ export default { output: { entryFileNames: "scramjet.[name].js", dir: "./dist", - format: "esm", + // format: "amd", sourcemap: true, - compact: production, - }, + compact: production + } }; diff --git a/server.js b/server.js index 38a778c..92370d4 100644 --- a/server.js +++ b/server.js @@ -4,10 +4,8 @@ import { createServer } from "http"; import Fastify from "fastify"; import fastifyStatic from "@fastify/static"; import { join } from "node:path"; -import fs from "node:fs" import { spawn } from "node:child_process" import { fileURLToPath } from "node:url"; -import { loadConfigFile } from "rollup/loadConfigFile" //transports import { baremuxPath } from "@mercuryworkshop/bare-mux/node" @@ -71,7 +69,7 @@ fastify.listen({ port: process.env.PORT || 1337 }); -const watch = spawn("rollup", ["-c", "-w"], { detached: true }); +const watch = spawn("pnpm", ["rollup", "-c", "-w"], { detached: true, cwd: process.cwd() }); watch.stdout.on("data", (data) => { console.log(`${data}`); @@ -79,4 +77,4 @@ watch.stdout.on("data", (data) => { watch.stderr.on("data", (data) => { console.log(`${data}`); -}); \ No newline at end of file +}); diff --git a/src/client/beacon.ts b/src/client/beacon.ts index f8d7b60..e48d209 100644 --- a/src/client/beacon.ts +++ b/src/client/beacon.ts @@ -1,4 +1,4 @@ -import { encodeUrl } from "../shared"; +import { encodeUrl } from "./index"; navigator.sendBeacon = new Proxy(navigator.sendBeacon, { apply(target, thisArg, argArray) { diff --git a/src/client/css.ts b/src/client/css.ts index cb95cef..d507706 100644 --- a/src/client/css.ts +++ b/src/client/css.ts @@ -1,4 +1,4 @@ -import { rewriteCss } from "../shared"; +import { rewriteCss } from "./index"; const cssProperties = ["background", "background-image", "mask", "mask-image", "list-style", "list-style-image", "border-image", "border-image-source", "cursor"]; const jsProperties = ["background", "backgroundImage", "mask", "maskImage", "listStyle", "listStyleImage", "borderImage", "borderImageSource", "cursor"]; diff --git a/src/client/element.ts b/src/client/element.ts index 7f06a9d..2c400f8 100644 --- a/src/client/element.ts +++ b/src/client/element.ts @@ -1,4 +1,4 @@ -import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "../shared"; +import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "./index"; const attrObject = { "nonce": [HTMLElement], diff --git a/src/client/history.ts b/src/client/history.ts index 240e476..6ec6e96 100644 --- a/src/client/history.ts +++ b/src/client/history.ts @@ -1,5 +1,5 @@ -import { encodeUrl } from "../shared"; +import { encodeUrl } from "./index"; window.history.pushState = new Proxy(window.history.pushState, { apply(target, thisArg, argArray) { diff --git a/src/client/index.ts b/src/client/index.ts index 309579a..f4823c9 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -20,19 +20,15 @@ declare global { } } -const scripts = document.querySelectorAll("script:not([data-scramjet])"); - -for (const script of scripts) { - const clone = document.createElement("script"); - - for (const attr of Array.from(script.attributes)) { - clone.setAttribute(attr.name, attr.value); - } - - if (script.innerHTML !== "") { - clone.innerHTML = script.innerHTML; - } - - script.insertAdjacentElement("afterend", clone); - script.remove(); -} +export const { + encodeUrl, + decodeUrl, + rewriteHeaders, + rewriteHtml, + rewriteSrcset, + rewriteJs, + rewriteCss, + rewriteWorkers, + isScramjetFile, + BareClient +} = self.__scramjet$bundle; \ No newline at end of file diff --git a/src/client/location.ts b/src/client/location.ts index 15a6158..257cacd 100644 --- a/src/client/location.ts +++ b/src/client/location.ts @@ -1,18 +1,26 @@ // @ts-nocheck -import { encodeUrl, decodeUrl } from "../shared"; +import { encodeUrl, decodeUrl } from "./index"; -const loc = new URL(decodeUrl(location.href)); -loc.assign = (url: string) => location.assign(encodeUrl(url)); -loc.reload = () => location.reload(); -loc.replace = (url: string) => location.replace(encodeUrl(url)); -loc.toString = () => loc.href; +function createLocation() { + const loc = new URL(decodeUrl(location.href)); + loc.assign = (url: string) => location.assign(encodeUrl(url)); + loc.reload = () => location.reload(); + loc.replace = (url: string) => location.replace(encodeUrl(url)); + loc.toString = () => loc.href; + + return loc; +} export const locationProxy = new Proxy(window.location, { get(target, prop) { + const loc = createLocation(); + return loc[prop]; }, set(obj, prop, value) { + const loc = createLocation(); + if (prop === "href") { location.href = encodeUrl(value); } else { diff --git a/src/client/native/eval.ts b/src/client/native/eval.ts index 988de77..574f2c6 100644 --- a/src/client/native/eval.ts +++ b/src/client/native/eval.ts @@ -1,4 +1,4 @@ -import { rewriteJs } from "../../shared"; +import { rewriteJs } from "../index"; const FunctionProxy = new Proxy(Function, { construct(target, argArray) { diff --git a/src/client/requests/fetch.ts b/src/client/requests/fetch.ts index a9244c3..70131d6 100644 --- a/src/client/requests/fetch.ts +++ b/src/client/requests/fetch.ts @@ -1,6 +1,6 @@ // ts throws an error if you dont do window.fetch -import { encodeUrl, rewriteHeaders } from "../../shared"; +import { encodeUrl, rewriteHeaders } from "../index"; window.fetch = new Proxy(window.fetch, { apply(target, thisArg, argArray) { diff --git a/src/client/requests/websocket.ts b/src/client/requests/websocket.ts index 0e3caff..5a19937 100644 --- a/src/client/requests/websocket.ts +++ b/src/client/requests/websocket.ts @@ -1,5 +1,6 @@ -import { BareClient } from "@mercuryworkshop/bare-mux" -const client = new BareClient() +import { BareClient } from "../index"; +const client = new BareClient(); + WebSocket = new Proxy(WebSocket, { construct(target, args) { return client.createWebSocket( @@ -12,4 +13,4 @@ WebSocket = new Proxy(WebSocket, { ArrayBuffer.prototype ) } -}); \ No newline at end of file +}) \ No newline at end of file diff --git a/src/client/requests/xmlhttprequest.ts b/src/client/requests/xmlhttprequest.ts index 490df69..ea42a49 100644 --- a/src/client/requests/xmlhttprequest.ts +++ b/src/client/requests/xmlhttprequest.ts @@ -1,4 +1,4 @@ -import { encodeUrl, rewriteHeaders } from "../../shared"; +import { encodeUrl, rewriteHeaders } from "../index"; XMLHttpRequest.prototype.open = new Proxy(XMLHttpRequest.prototype.open, { apply(target, thisArg, argArray) { diff --git a/src/client/trustedTypes.ts b/src/client/trustedTypes.ts index 654b43b..d767c0d 100644 --- a/src/client/trustedTypes.ts +++ b/src/client/trustedTypes.ts @@ -1,4 +1,4 @@ -import { rewriteHtml, rewriteJs, encodeUrl } from "../shared"; +import { rewriteHtml, rewriteJs, encodeUrl } from "./index"; // @ts-expect-error trustedTypes.createPolicy = new Proxy(trustedTypes.createPolicy, { diff --git a/src/client/worker.ts b/src/client/worker.ts index 79efb29..cd3c465 100644 --- a/src/client/worker.ts +++ b/src/client/worker.ts @@ -1,4 +1,4 @@ -import { encodeUrl } from "../shared"; +import { encodeUrl } from "./index"; Worker = new Proxy(Worker, { construct(target, argArray) { diff --git a/src/shared/index.ts b/src/shared/index.ts index 6b0bb76..30bfa92 100644 --- a/src/shared/index.ts +++ b/src/shared/index.ts @@ -1,16 +1,38 @@ -export { encodeUrl, decodeUrl } from "./rewriters/url"; -export { rewriteCss } from "./rewriters/css"; -export { rewriteHtml, rewriteSrcset } from "./rewriters/html"; -export { rewriteJs } from "./rewriters/js"; -export { rewriteHeaders } from "./rewriters/headers"; -export { rewriteWorkers } from "./rewriters/worker" -export { BareClient } from "@mercuryworkshop/bare-mux" +import { encodeUrl, decodeUrl } from "./rewriters/url"; +import { rewriteCss } from "./rewriters/css"; +import { rewriteHtml, rewriteSrcset } from "./rewriters/html"; +import { rewriteJs } from "./rewriters/js"; +import { rewriteHeaders } from "./rewriters/headers"; +import { rewriteWorkers } from "./rewriters/worker"; +import { isScramjetFile } from "./rewriters/html"; +import { BareClient } from "@mercuryworkshop/bare-mux"; -export function isScramjetFile(src: string) { - let bool = false; - ["codecs", "client", "shared", "worker", "config"].forEach((file) => { - if (src === self.__scramjet$config[file]) bool = true; - }); +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 + } + } +} - return bool; +self.__scramjet$bundle = { + encodeUrl, + decodeUrl, + rewriteCss, + rewriteHtml, + rewriteSrcset, + rewriteJs, + rewriteHeaders, + rewriteWorkers, + isScramjetFile, + BareClient } \ No newline at end of file diff --git a/src/shared/rewriters/html.ts b/src/shared/rewriters/html.ts index 72a7686..fc52ac3 100644 --- a/src/shared/rewriters/html.ts +++ b/src/shared/rewriters/html.ts @@ -4,8 +4,16 @@ import { hasAttrib } from "domutils"; import render from "dom-serializer"; import { encodeUrl } from "./url"; import { rewriteCss } from "./css"; -// import { rewriteJs } from "./js"; -import { isScramjetFile } from ".."; +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; + }); + + return bool; +} export function rewriteHtml(html: string, origin?: URL) { const handler = new DomHandler((err, dom) => dom); @@ -50,7 +58,7 @@ function traverseParsedHtml(node, origin?: URL) { if (hasAttrib(node, "style")) node.attribs.style = rewriteCss(node.attribs.style, origin); if (node.name === "style" && node.children[0] !== undefined) node.children[0].data = rewriteCss(node.children[0].data, origin); - // if (node.name === "script" && /(application|text)\/javascript|importmap|undefined/.test(node.attribs.type) && node.children[0] !== undefined) node.children[0].data = rewriteJs(node.children[0].data, origin); + if (node.name === "script" && /(application|text)\/javascript|importmap|undefined/.test(node.attribs.type) && node.children[0] !== undefined) node.children[0].data = rewriteJs(node.children[0].data, origin); if (node.name === "meta" && hasAttrib(node, "http-equiv")) { if (node.attribs["http-equiv"] === "content-security-policy") { node = {}; @@ -66,7 +74,6 @@ function traverseParsedHtml(node, origin?: URL) { ["codecs", "config", "shared", "client"].forEach((script) => { scramjetScripts.push(new Element("script", { src: self.__scramjet$config[script], - type: "module", "data-scramjet": "" })); }); diff --git a/src/worker/index.ts b/src/worker/index.ts index 4ca9499..cb88bfd 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -1,6 +1,4 @@ -import { BareClient } from "@mercuryworkshop/bare-mux"; import { BareResponseFetch } from "@mercuryworkshop/bare-mux"; -import { encodeUrl, decodeUrl, rewriteCss, rewriteHeaders, rewriteHtml, rewriteJs, rewriteWorkers } from "../shared"; declare global { interface Window { @@ -8,11 +6,11 @@ declare global { } } -export default class ScramjetServiceWorker { - client: typeof BareClient.prototype; +self.ScramjetServiceWorker = class ScramjetServiceWorker { + client: typeof self.__scramjet$bundle.BareClient.prototype; config: typeof self.__scramjet$config; constructor(config = self.__scramjet$config) { - this.client = new BareClient(); + this.client = new self.__scramjet$bundle.BareClient(); if (!config.prefix) config.prefix = "/scramjet/"; this.config = config; } @@ -24,6 +22,7 @@ export default 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; 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 c796492..c083312 100644 --- a/static/index.html +++ b/static/index.html @@ -22,8 +22,8 @@ - - + + \ No newline at end of file diff --git a/static/sw.js b/static/sw.js index 90b13e9..86749d0 100644 --- a/static/sw.js +++ b/static/sw.js @@ -1,6 +1,9 @@ -import ScramjetServiceWorker from "/scram/scramjet.worker.js"; -import "/scram/scramjet.codecs.js"; -import "/scram/scramjet.config.js"; +importScripts( + "/scram/scramjet.codecs.js", + "/scram/scramjet.config.js", + "/scram/scramjet.shared.js", + "/scram/scramjet.worker.js" +); const scramjet = new ScramjetServiceWorker(); diff --git a/static/ui.js b/static/ui.js index d7d0746..4b25f34 100644 --- a/static/ui.js +++ b/static/ui.js @@ -1,6 +1,5 @@ navigator.serviceWorker.register("./sw.js", { - scope: __scramjet$config.prefix, - type: "module" + scope: __scramjet$config.prefix }) const connection = new BareMux.BareMuxConnection("/baremux/worker.js") const flex = css`display: flex;`;