diff --git a/src/client/dom/serviceworker.ts b/src/client/dom/serviceworker.ts index 841a600..04ef2d1 100644 --- a/src/client/dom/serviceworker.ts +++ b/src/client/dom/serviceworker.ts @@ -2,11 +2,14 @@ import { config, rewriteUrl } from "../../shared"; import { ScramjetClient } from "../client"; import { type MessageC2W } from "../../worker"; import { getOwnPropertyDescriptorHandler } from "../helpers"; +import { flagEnabled } from "../../scramjet"; // we need a late order because we're mangling with addEventListener at a higher level export const order = 2; -export const enabled = () => config.flags.serviceworkers; +export const enabled = (client: ScramjetClient) => + flagEnabled("serviceworkers", client.url); + export function disabled(_client: ScramjetClient, _self: Self) { Reflect.deleteProperty(Navigator.prototype, "serviceWorker"); } diff --git a/src/client/shared/err.ts b/src/client/shared/err.ts index 7125fcc..75b5f09 100644 --- a/src/client/shared/err.ts +++ b/src/client/shared/err.ts @@ -1,7 +1,9 @@ +import { flagEnabled } from "../../scramjet"; import { config } from "../../shared"; import { ScramjetClient } from "../client"; -export const enabled = () => config.flags.captureErrors; +export const enabled = (client: ScramjetClient) => + flagEnabled("captureErrors", client.url); export function argdbg(arg, recurse = []) { switch (typeof arg) { case "string": diff --git a/src/client/shared/error.ts b/src/client/shared/error.ts index 3ae407a..8e79675 100644 --- a/src/client/shared/error.ts +++ b/src/client/shared/error.ts @@ -1,7 +1,9 @@ +import { flagEnabled } from "../../scramjet"; import { config, unrewriteUrl } from "../../shared"; import { ScramjetClient } from "../client"; -export const enabled = () => self.$scramjet.config.flags.cleanerrors; +export const enabled = (client: ScramjetClient) => + flagEnabled("cleanerrors", client.url); export default function (client: ScramjetClient, _self: Self) { // v8 only. all we need to do is clean the scramjet urls from stack traces const closure = (error, stack) => { @@ -10,7 +12,7 @@ export default function (client: ScramjetClient, _self: Self) { for (let i = 0; i < stack.length; i++) { const url = stack[i].getFileName(); - if (url.endsWith(config.client)) { + if (url.endsWith(config.files.client)) { // strip stack frames including scramjet handlers from the trace const lines = newstack.split("\n"); const line = lines.find((l) => l.includes(url)); diff --git a/src/client/shared/requests/xmlhttprequest.ts b/src/client/shared/requests/xmlhttprequest.ts index 6e24132..dd035e6 100644 --- a/src/client/shared/requests/xmlhttprequest.ts +++ b/src/client/shared/requests/xmlhttprequest.ts @@ -1,3 +1,4 @@ +import { flagEnabled } from "../../../scramjet"; import { config, unrewriteUrl, rewriteUrl } from "../../../shared"; import { ScramjetClient } from "../../client"; let nativeworker; @@ -35,8 +36,8 @@ export default function (client: ScramjetClient, self: Self) { const args = ctx.this[ARGS]; if (!args || args[2]) return; - if (!self.$scramjet.config.flags.syncxhr) { - console.warn("sync xhr disabled in flags"); + if (!flagEnabled("syncxhr", client.url)) { + console.warn("ignoring request - sync xhr disabled in flags"); return; } diff --git a/src/client/shared/sourcemaps.ts b/src/client/shared/sourcemaps.ts index fd5ed0a..80747dc 100644 --- a/src/client/shared/sourcemaps.ts +++ b/src/client/shared/sourcemaps.ts @@ -1,10 +1,12 @@ +import { flagEnabled } from "../../scramjet"; import { ScramjetClient } from "../client"; type Mapping = [string, number, number]; const sourcemaps: Record = {}; -export const enabled = () => self.$scramjet.config.flags.sourcemaps; +export const enabled = (client: ScramjetClient) => + flagEnabled("sourcemaps", client.url); export default function (client: ScramjetClient, self: Self) { // every script will push a sourcemap diff --git a/src/controller/index.ts b/src/controller/index.ts index 265e94b..9cdc4e3 100644 --- a/src/controller/index.ts +++ b/src/controller/index.ts @@ -26,7 +26,7 @@ export class ScramjetController { client: "/scramjet.client.js", sync: "/scramjet.sync.js", }, - flags: { + defaultFlags: { serviceworkers: false, naiiveRewriter: false, captureErrors: true, @@ -35,6 +35,7 @@ export class ScramjetController { scramitize: false, sourcemaps: false, }, + siteFlags: {}, codec: { encode: `if (!url) return url; return encodeURIComponent(url);`, diff --git a/src/scramjet.ts b/src/scramjet.ts index c05449f..e168734 100644 --- a/src/scramjet.ts +++ b/src/scramjet.ts @@ -1,4 +1,4 @@ -import { ScramjetConfig } from "./types"; +import { ScramjetConfig, ScramjetFlags } from "./types"; if (!("$scramjet" in self)) { // @ts-expect-error ts stuff @@ -24,3 +24,15 @@ export function loadCodecs() { $scramjet.config.codec.decode ) as any; } + +export function flagEnabled(flag: keyof ScramjetFlags, url: URL): boolean { + let value = $scramjet.config.defaultFlags[flag]; + for (const regex in $scramjet.config.siteFlags) { + const partialflags = $scramjet.config.siteFlags[regex]; + if (new RegExp(regex).test(url.href) && "flag" in partialflags) { + return partialflags[flag]; + } + } + + return value; +} diff --git a/src/types.d.ts b/src/types.d.ts index 611f387..6797819 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -52,8 +52,8 @@ interface ScramjetConfig { client: string; sync: string; }; - flags: ScramjetFlags; - siteflags: Record; + defaultFlags: ScramjetFlags; + siteFlags: Record>; codec: { encode: string; decode: string;