site specific flags

This commit is contained in:
velzie 2024-10-13 14:43:51 -04:00
parent 0906dd78a9
commit 920bbd8d69
No known key found for this signature in database
GPG key ID: AA51AEFB0A1F3820
8 changed files with 34 additions and 11 deletions

View file

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

View file

@ -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":

View file

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

View file

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

View file

@ -1,10 +1,12 @@
import { flagEnabled } from "../../scramjet";
import { ScramjetClient } from "../client";
type Mapping = [string, number, number];
const sourcemaps: Record<string, Mapping[]> = {};
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

View file

@ -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);`,

View file

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

4
src/types.d.ts vendored
View file

@ -52,8 +52,8 @@ interface ScramjetConfig {
client: string;
sync: string;
};
flags: ScramjetFlags;
siteflags: Record<string, ScramjetFlags>;
defaultFlags: ScramjetFlags;
siteFlags: Record<string, Partial<ScramjetFlags>>;
codec: {
encode: string;
decode: string;