This commit is contained in:
velzie 2024-08-25 20:22:25 -04:00
parent 8d2ab5ac8b
commit 9c3dd5052b
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
2 changed files with 21 additions and 8 deletions

View file

@ -66,6 +66,8 @@ export class ScramjetClient {
> = new Map(); > = new Map();
constructor(public global: typeof globalThis) { constructor(public global: typeof globalThis) {
this.serviceWorker = this.global.navigator.serviceWorker;
if ("document" in self) { if ("document" in self) {
this.documentProxy = createDocumentProxy(this, global); this.documentProxy = createDocumentProxy(this, global);
} }
@ -81,7 +83,6 @@ export class ScramjetClient {
} }
hook() { hook() {
this.serviceWorker = this.global.navigator.serviceWorker;
// @ts-ignore // @ts-ignore
const context = import.meta.webpackContext(".", { const context = import.meta.webpackContext(".", {
recursive: true, recursive: true,
@ -109,7 +110,8 @@ export class ScramjetClient {
}); });
for (const module of modules) { 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<any> = {}; const h: ProxyHandler<any> = {};
if (handler.construct) { if (handler.construct) {
h.construct = function ( h.construct = function
constructor: any, constructor: any,
argArray: any[], argArray: any[],
newTarget: AnyFunction newTarget: AnyFunction
@ -169,7 +171,7 @@ export class ScramjetClient {
} }
if (handler.apply) { if (handler.apply) {
h.apply = function (fn: any, thisArg: any, argArray: any[]) { h.apply = functionfn: any, thisArg: any, argArray: any[]) {
let returnValue: any = null; let returnValue: any = null;
const ctx: ProxyCtx = { const ctx: ProxyCtx = {
@ -226,10 +228,10 @@ export class ScramjetClient {
const ctx: TrapCtx<T> = { const ctx: TrapCtx<T> = {
this: null, this: null,
get: function () { get: function) {
return oldDescriptor && oldDescriptor.get.call(this.this); return oldDescriptor && oldDescriptor.get.call(this.this);
}, },
set: function (v: T) { set: functionv: T) {
oldDescriptor && oldDescriptor.set.call(this.this, v); oldDescriptor && oldDescriptor.set.call(this.this, v);
}, },
}; };
@ -239,7 +241,7 @@ export class ScramjetClient {
const desc: PropertyDescriptor = {}; const desc: PropertyDescriptor = {};
if (descriptor.get) { if (descriptor.get) {
desc.get = function () { desc.get = function) {
ctx.this = this; ctx.this = this;
return descriptor.get(ctx); return descriptor.get(ctx);
@ -249,7 +251,7 @@ export class ScramjetClient {
} }
if (descriptor.set) { if (descriptor.set) {
desc.set = function (v: T) { desc.set = functionv: T) {
ctx.this = this; ctx.this = this;
descriptor.set(ctx, v); descriptor.set(ctx, v);

View file

@ -1,10 +1,20 @@
import { encodeUrl } from "../shared"; import { encodeUrl } from "../shared";
import { ScramjetClient } from "../client"; import { ScramjetClient } from "../client";
import { type MessageC2W } from "../../worker"; import { type MessageC2W } from "../../worker";
import { getOwnPropertyDescriptorHandler } from "../helpers";
// 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 = () => 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) { export default function (client: ScramjetClient, self: Self) {
let registration; let registration;
@ -105,6 +115,7 @@ export default function (client: ScramjetClient, self: Self) {
return Reflect.get(target, prop); return Reflect.get(target, prop);
}, },
getOwnPropertyDescriptor: getOwnPropertyDescriptorHandler,
} }
); );
registration = fakeRegistration; registration = fakeRegistration;