File size: 4,087 Bytes
bee6636 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
import {
codecDecode,
codecEncode,
config,
loadCodecs,
setConfig,
} from "../shared/index";
import { ScramjetConfig, ScramjetInitConfig } from "../types";
import { ScramjetFrame } from "./frame";
export class ScramjetController {
private db: IDBDatabase;
constructor(config: Partial<ScramjetInitConfig>) {
// sane ish defaults
const defaultConfig: ScramjetInitConfig = {
prefix: "/scramjet/",
globals: {
wrapfn: "$scramjet$wrap",
wrapthisfn: "$scramjet$wrapthis",
trysetfn: "$scramjet$tryset",
importfn: "$scramjet$import",
rewritefn: "$scramjet$rewrite",
metafn: "$scramjet$meta",
setrealmfn: "$scramjet$setrealm",
pushsourcemapfn: "$scramjet$pushsourcemap",
},
files: {
wasm: "/scramjet.wasm.wasm",
all: "/scramjet.all.js",
sync: "/scramjet.sync.js",
},
flags: {
serviceworkers: false,
syncxhr: false,
naiiveRewriter: false,
strictRewrites: true,
rewriterLogs: false,
captureErrors: true,
cleanErrors: false,
scramitize: false,
sourcemaps: true,
},
siteFlags: {},
codec: {
encode: (url: string) => {
if (!url) return url;
return encodeURIComponent(url);
},
decode: (url: string) => {
if (!url) return url;
return decodeURIComponent(url);
},
},
};
const deepMerge = (target: any, source: any): any => {
for (const key in source) {
if (source[key] instanceof Object && key in target) {
Object.assign(source[key], deepMerge(target[key], source[key]));
}
}
return Object.assign(target || {}, source);
};
const newConfig = deepMerge(defaultConfig, config);
newConfig.codec.encode = newConfig.codec.encode.toString();
newConfig.codec.decode = newConfig.codec.decode.toString();
setConfig(newConfig as ScramjetConfig);
}
async init(): Promise<void> {
loadCodecs();
await this.openIDB();
navigator.serviceWorker.controller?.postMessage({
scramjet$type: "loadConfig",
config,
});
dbg.log("config loaded");
}
createFrame(frame?: HTMLIFrameElement): ScramjetFrame {
if (!frame) {
frame = document.createElement("iframe");
}
return new ScramjetFrame(this, frame);
}
encodeUrl(url: string | URL): string {
if (url instanceof URL) url = url.toString();
return config.prefix + codecEncode(url);
}
decodeUrl(url: string | URL) {
if (url instanceof URL) url = url.toString();
const prefixed = location.origin + config.prefix;
return codecDecode(url.slice(prefixed.length));
}
async openIDB(): Promise<IDBDatabase> {
const db = indexedDB.open("$scramjet", 1);
return new Promise<IDBDatabase>((resolve, reject) => {
db.onsuccess = async () => {
this.db = db.result;
await this.#saveConfig();
resolve(db.result);
};
db.onupgradeneeded = () => {
const res = db.result;
if (!res.objectStoreNames.contains("config")) {
res.createObjectStore("config");
}
if (!res.objectStoreNames.contains("cookies")) {
res.createObjectStore("cookies");
}
if (!res.objectStoreNames.contains("redirectTrackers")) {
res.createObjectStore("redirectTrackers");
}
if (!res.objectStoreNames.contains("referrerPolicies")) {
res.createObjectStore("referrerPolicies");
}
if (!res.objectStoreNames.contains("publicSuffixList")) {
res.createObjectStore("publicSuffixList");
}
};
db.onerror = () => reject(db.error);
});
}
async #saveConfig() {
if (!this.db) {
console.error("Store not ready!");
return;
}
const tx = this.db.transaction("config", "readwrite");
const store = tx.objectStore("config");
const req = store.put(config, "config");
return new Promise((resolve, reject) => {
req.onsuccess = resolve;
req.onerror = reject;
});
}
async modifyConfig(newconfig: ScramjetConfig) {
setConfig(Object.assign({}, config, newconfig));
loadCodecs();
await this.#saveConfig();
}
}
|