mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 14:30:02 -04:00
site specific flags
This commit is contained in:
parent
0906dd78a9
commit
920bbd8d69
8 changed files with 34 additions and 11 deletions
|
@ -2,11 +2,14 @@ import { config, rewriteUrl } from "../../shared";
|
||||||
import { ScramjetClient } from "../client";
|
import { ScramjetClient } from "../client";
|
||||||
import { type MessageC2W } from "../../worker";
|
import { type MessageC2W } from "../../worker";
|
||||||
import { getOwnPropertyDescriptorHandler } from "../helpers";
|
import { getOwnPropertyDescriptorHandler } from "../helpers";
|
||||||
|
import { flagEnabled } from "../../scramjet";
|
||||||
|
|
||||||
// we need a late order because we're mangling with addEventListener at a higher level
|
// we need a late order because we're mangling with addEventListener at a higher level
|
||||||
export const order = 2;
|
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) {
|
export function disabled(_client: ScramjetClient, _self: Self) {
|
||||||
Reflect.deleteProperty(Navigator.prototype, "serviceWorker");
|
Reflect.deleteProperty(Navigator.prototype, "serviceWorker");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
import { flagEnabled } from "../../scramjet";
|
||||||
import { config } from "../../shared";
|
import { config } from "../../shared";
|
||||||
import { ScramjetClient } from "../client";
|
import { ScramjetClient } from "../client";
|
||||||
|
|
||||||
export const enabled = () => config.flags.captureErrors;
|
export const enabled = (client: ScramjetClient) =>
|
||||||
|
flagEnabled("captureErrors", client.url);
|
||||||
export function argdbg(arg, recurse = []) {
|
export function argdbg(arg, recurse = []) {
|
||||||
switch (typeof arg) {
|
switch (typeof arg) {
|
||||||
case "string":
|
case "string":
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
import { flagEnabled } from "../../scramjet";
|
||||||
import { config, unrewriteUrl } from "../../shared";
|
import { config, unrewriteUrl } from "../../shared";
|
||||||
import { ScramjetClient } from "../client";
|
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) {
|
export default function (client: ScramjetClient, _self: Self) {
|
||||||
// v8 only. all we need to do is clean the scramjet urls from stack traces
|
// v8 only. all we need to do is clean the scramjet urls from stack traces
|
||||||
const closure = (error, stack) => {
|
const closure = (error, stack) => {
|
||||||
|
@ -10,7 +12,7 @@ export default function (client: ScramjetClient, _self: Self) {
|
||||||
for (let i = 0; i < stack.length; i++) {
|
for (let i = 0; i < stack.length; i++) {
|
||||||
const url = stack[i].getFileName();
|
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
|
// strip stack frames including scramjet handlers from the trace
|
||||||
const lines = newstack.split("\n");
|
const lines = newstack.split("\n");
|
||||||
const line = lines.find((l) => l.includes(url));
|
const line = lines.find((l) => l.includes(url));
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { flagEnabled } from "../../../scramjet";
|
||||||
import { config, unrewriteUrl, rewriteUrl } from "../../../shared";
|
import { config, unrewriteUrl, rewriteUrl } from "../../../shared";
|
||||||
import { ScramjetClient } from "../../client";
|
import { ScramjetClient } from "../../client";
|
||||||
let nativeworker;
|
let nativeworker;
|
||||||
|
@ -35,8 +36,8 @@ export default function (client: ScramjetClient, self: Self) {
|
||||||
const args = ctx.this[ARGS];
|
const args = ctx.this[ARGS];
|
||||||
if (!args || args[2]) return;
|
if (!args || args[2]) return;
|
||||||
|
|
||||||
if (!self.$scramjet.config.flags.syncxhr) {
|
if (!flagEnabled("syncxhr", client.url)) {
|
||||||
console.warn("sync xhr disabled in flags");
|
console.warn("ignoring request - sync xhr disabled in flags");
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
|
import { flagEnabled } from "../../scramjet";
|
||||||
import { ScramjetClient } from "../client";
|
import { ScramjetClient } from "../client";
|
||||||
|
|
||||||
type Mapping = [string, number, number];
|
type Mapping = [string, number, number];
|
||||||
|
|
||||||
const sourcemaps: Record<string, Mapping[]> = {};
|
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) {
|
export default function (client: ScramjetClient, self: Self) {
|
||||||
// every script will push a sourcemap
|
// every script will push a sourcemap
|
||||||
|
|
|
@ -26,7 +26,7 @@ export class ScramjetController {
|
||||||
client: "/scramjet.client.js",
|
client: "/scramjet.client.js",
|
||||||
sync: "/scramjet.sync.js",
|
sync: "/scramjet.sync.js",
|
||||||
},
|
},
|
||||||
flags: {
|
defaultFlags: {
|
||||||
serviceworkers: false,
|
serviceworkers: false,
|
||||||
naiiveRewriter: false,
|
naiiveRewriter: false,
|
||||||
captureErrors: true,
|
captureErrors: true,
|
||||||
|
@ -35,6 +35,7 @@ export class ScramjetController {
|
||||||
scramitize: false,
|
scramitize: false,
|
||||||
sourcemaps: false,
|
sourcemaps: false,
|
||||||
},
|
},
|
||||||
|
siteFlags: {},
|
||||||
codec: {
|
codec: {
|
||||||
encode: `if (!url) return url;
|
encode: `if (!url) return url;
|
||||||
return encodeURIComponent(url);`,
|
return encodeURIComponent(url);`,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { ScramjetConfig } from "./types";
|
import { ScramjetConfig, ScramjetFlags } from "./types";
|
||||||
|
|
||||||
if (!("$scramjet" in self)) {
|
if (!("$scramjet" in self)) {
|
||||||
// @ts-expect-error ts stuff
|
// @ts-expect-error ts stuff
|
||||||
|
@ -24,3 +24,15 @@ export function loadCodecs() {
|
||||||
$scramjet.config.codec.decode
|
$scramjet.config.codec.decode
|
||||||
) as any;
|
) 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
4
src/types.d.ts
vendored
|
@ -52,8 +52,8 @@ interface ScramjetConfig {
|
||||||
client: string;
|
client: string;
|
||||||
sync: string;
|
sync: string;
|
||||||
};
|
};
|
||||||
flags: ScramjetFlags;
|
defaultFlags: ScramjetFlags;
|
||||||
siteflags: Record<string, ScramjetFlags>;
|
siteFlags: Record<string, Partial<ScramjetFlags>>;
|
||||||
codec: {
|
codec: {
|
||||||
encode: string;
|
encode: string;
|
||||||
decode: string;
|
decode: string;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue