From 9c3dd5052b69b5d39aef3581a59e9123ad81363c Mon Sep 17 00:00:00 2001 From: velzie Date: Sun, 25 Aug 2024 20:22:25 -0400 Subject: [PATCH] flags --- src/client/client.ts | 18 ++++++++++-------- src/client/dom/serviceworker.ts | 11 +++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/client/client.ts b/src/client/client.ts index 3d4f96a..2c61b5e 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -66,6 +66,8 @@ export class ScramjetClient { > = new Map(); constructor(public global: typeof globalThis) { + this.serviceWorker = this.global.navigator.serviceWorker; + if ("document" in self) { this.documentProxy = createDocumentProxy(this, global); } @@ -81,7 +83,6 @@ export class ScramjetClient { } hook() { - this.serviceWorker = this.global.navigator.serviceWorker; // @ts-ignore const context = import.meta.webpackContext(".", { recursive: true, @@ -109,7 +110,8 @@ export class ScramjetClient { }); for (const module of modules) { - module.default(this, this.global); + if (module.enabled()) module.default(this, this.global); + else module.disabled(this, this.global); } } @@ -141,7 +143,7 @@ export class ScramjetClient { const h: ProxyHandler = {}; if (handler.construct) { - h.construct = function ( + h.construct = function constructor: any, argArray: any[], newTarget: AnyFunction @@ -169,7 +171,7 @@ export class ScramjetClient { } if (handler.apply) { - h.apply = function (fn: any, thisArg: any, argArray: any[]) { + h.apply = functionfn: any, thisArg: any, argArray: any[]) { let returnValue: any = null; const ctx: ProxyCtx = { @@ -226,10 +228,10 @@ export class ScramjetClient { const ctx: TrapCtx = { this: null, - get: function () { + get: function) { return oldDescriptor && oldDescriptor.get.call(this.this); }, - set: function (v: T) { + set: functionv: T) { oldDescriptor && oldDescriptor.set.call(this.this, v); }, }; @@ -239,7 +241,7 @@ export class ScramjetClient { const desc: PropertyDescriptor = {}; if (descriptor.get) { - desc.get = function () { + desc.get = function) { ctx.this = this; return descriptor.get(ctx); @@ -249,7 +251,7 @@ export class ScramjetClient { } if (descriptor.set) { - desc.set = function (v: T) { + desc.set = functionv: T) { ctx.this = this; descriptor.set(ctx, v); diff --git a/src/client/dom/serviceworker.ts b/src/client/dom/serviceworker.ts index 10164d4..eb9c4fa 100644 --- a/src/client/dom/serviceworker.ts +++ b/src/client/dom/serviceworker.ts @@ -1,10 +1,20 @@ import { encodeUrl } from "../shared"; import { ScramjetClient } from "../client"; import { type MessageC2W } from "../../worker"; +import { getOwnPropertyDescriptorHandler } from "../helpers"; // we need a late order because we're mangling with addEventListener at a higher level export const order = 2; +export const enabled = () => self.$scramjet.config.flags.serviceworkers; +export function disabled(client: ScramjetClient, self: Self) { + client.Trap("navigator.serviceWorker", { + get() { + return undefined; + }, + }); +} + export default function (client: ScramjetClient, self: Self) { let registration; @@ -105,6 +115,7 @@ export default function (client: ScramjetClient, self: Self) { return Reflect.get(target, prop); }, + getOwnPropertyDescriptor: getOwnPropertyDescriptorHandler, } ); registration = fakeRegistration;