fix imports

This commit is contained in:
Avad3 2024-07-14 11:42:23 -04:00
parent 82a95e71b0
commit 20caf32b5b
17 changed files with 46 additions and 40 deletions

View file

@ -6,6 +6,8 @@ import { fileURLToPath } from "url";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
export default defineConfig({
// change to production when needed
mode: "development",
entry: {
shared: join(__dirname, "src/shared/index.ts"),
worker: join(__dirname, "src/worker/index.ts"),
@ -35,7 +37,9 @@ export default defineConfig({
},
output: {
filename: "scramjet.[name].js",
path: join(__dirname, "dist")
path: join(__dirname, "dist"),
iife: true,
clean: true
},
plugins: [
// new RsdoctorRspackPlugin({

View file

@ -1,4 +1,4 @@
import { encodeUrl } from "./index";
import { encodeUrl } from "./shared";
navigator.sendBeacon = new Proxy(navigator.sendBeacon, {
apply(target, thisArg, argArray) {

View file

@ -1,4 +1,4 @@
import { rewriteCss } from "./index";
import { rewriteCss } from "./shared";
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"];

View file

@ -1,4 +1,4 @@
import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "./index";
import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "./shared";
const attrObject = {
"nonce": [HTMLElement],

View file

@ -1,5 +1,5 @@
import { encodeUrl } from "./index";
import { encodeUrl } from "./shared";
window.history.pushState = new Proxy(window.history.pushState, {
apply(target, thisArg, argArray) {

View file

@ -1,3 +1,4 @@
import "./scope.ts";
import "./window.ts";
import "./event.ts";
import "./native/eval.ts";
@ -11,23 +12,9 @@ import "./storage.ts";
import "./css.ts";
import "./history.ts"
import "./worker.ts";
import "./scope.ts";
declare global {
interface Window {
$s: any;
}
}
export const {
encodeUrl,
decodeUrl,
rewriteHeaders,
rewriteHtml,
rewriteSrcset,
rewriteJs,
rewriteCss,
rewriteWorkers,
isScramjetFile,
BareClient
} = self.$scramjet.bundle;

View file

@ -1,5 +1,5 @@
// @ts-nocheck
import { encodeUrl, decodeUrl } from "./index";
import { encodeUrl, decodeUrl } from "./shared";
function createLocation() {
const loc = new URL(decodeUrl(location.href));

View file

@ -1,4 +1,4 @@
import { rewriteJs } from "../index";
import { rewriteJs } from "../shared";
const FunctionProxy = new Proxy(Function, {
construct(target, argArray) {

View file

@ -1,6 +1,6 @@
// ts throws an error if you dont do window.fetch
import { encodeUrl, rewriteHeaders } from "../index";
import { encodeUrl, rewriteHeaders } from "../shared";
window.fetch = new Proxy(window.fetch, {
apply(target, thisArg, argArray) {

View file

@ -1,4 +1,4 @@
import { BareClient } from "../index";
import { BareClient } from "../shared";
const client = new BareClient();
WebSocket = new Proxy(WebSocket, {

View file

@ -1,4 +1,4 @@
import { encodeUrl, rewriteHeaders } from "../index";
import { encodeUrl, rewriteHeaders } from "../shared";
XMLHttpRequest.prototype.open = new Proxy(XMLHttpRequest.prototype.open, {
apply(target, thisArg, argArray) {

12
src/client/shared.ts Normal file
View file

@ -0,0 +1,12 @@
export const {
encodeUrl,
decodeUrl,
rewriteHeaders,
rewriteHtml,
rewriteSrcset,
rewriteJs,
rewriteCss,
rewriteWorkers,
isScramjetFile,
BareClient
} = self.$scramjet.shared;

View file

@ -1,4 +1,4 @@
import { rewriteHtml, rewriteJs, encodeUrl } from "./index";
import { rewriteHtml, rewriteJs, encodeUrl } from "./shared";
// @ts-expect-error
trustedTypes.createPolicy = new Proxy(trustedTypes.createPolicy, {

View file

@ -1,4 +1,4 @@
import { encodeUrl } from "./index";
import { encodeUrl } from "./shared";
Worker = new Proxy(Worker, {
construct(target, argArray) {
@ -20,12 +20,15 @@ Worklet.prototype.addModule = new Proxy(Worklet.prototype.addModule, {
},
});
window.importScripts = new Proxy(window.importScripts, {
apply(target, thisArg, argArray) {
for (const i in argArray) {
argArray[i] = encodeUrl(argArray[i]);
}
return Reflect.apply(target, thisArg, argArray);
},
});
// broken
// window.importScripts = new Proxy(window.importScripts, {
// apply(target, thisArg, argArray) {
// for (const i in argArray) {
// argArray[i] = encodeUrl(argArray[i]);
// }
// return Reflect.apply(target, thisArg, argArray);
// },
// });

View file

@ -11,7 +11,7 @@ if (!self.$scramjet) {
//@ts-expect-error really dumb workaround
self.$scramjet = {}
}
self.$scramjet.bundle = {
self.$scramjet.shared = {
encodeUrl,
decodeUrl,
rewriteCss,

2
src/types.d.ts vendored
View file

@ -11,7 +11,7 @@ import { BareClient } from "@mercuryworkshop/bare-mux";
declare global {
interface Window {
$scramjet: {
bundle: {
shared: {
encodeUrl: typeof encodeUrl,
decodeUrl: typeof decodeUrl,
rewriteCss: typeof rewriteCss,

View file

@ -7,10 +7,10 @@ declare global {
}
self.ScramjetServiceWorker = class ScramjetServiceWorker {
client: typeof self.$scramjet.bundle.BareClient.prototype;
client: typeof self.$scramjet.shared.BareClient.prototype;
config: typeof self.$scramjet.config;
constructor(config = self.$scramjet.config) {
this.client = new self.$scramjet.bundle.BareClient();
this.client = new self.$scramjet.shared.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.shared;
if (urlParam.has("url")) {
return Response.redirect(encodeUrl(urlParam.get("url"), new URL(urlParam.get("url"))))