worker rewriting

This commit is contained in:
velzie 2024-07-21 15:17:31 -04:00
parent 2598bee87b
commit f6c3c13d1e
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
22 changed files with 543 additions and 392 deletions

View file

@ -7,7 +7,9 @@ declare global {
$sImport: any;
}
}
type AnyFunction = (...args: any[]) => any;
//eslint-disable-next-line
type AnyFunction = Function;
type ProxyCtx = {
fn: AnyFunction;
@ -16,17 +18,43 @@ type ProxyCtx = {
newTarget: AnyFunction;
return: (r: any) => void;
};
type Proxy = {
construct?(ctx: ProxyCtx): any;
apply?(ctx: ProxyCtx): any;
};
type TrapCtx<T> = {
this: any;
get: () => T;
set: (v: T) => void;
};
type Trap<T> = {
writable?: boolean;
value?: any;
enumerable?: boolean;
configurable?: boolean;
get?: (ctx: TrapCtx<T>) => T;
set?: (ctx: TrapCtx<T>, v: T) => void;
};
class ScramjetClient {
Proxy(
target: any,
prop: string,
handler: {
construct?(ctx: ProxyCtx): any;
apply?(ctx: ProxyCtx): any;
Proxy(name: string | string[], handler: Proxy) {
if (Array.isArray(name)) {
for (const n of name) {
this.Proxy(n, handler);
}
return;
}
) {
const split = name.split(".");
const prop = split.pop();
const target = split.reduce((a, b) => a?.[b], self);
this.RawProxy(target, prop, handler);
}
RawProxy(target: any, prop: string, handler: Proxy) {
if (!target) return;
if (!prop) return;
if (!Reflect.has(target, prop)) return;
const value = Reflect.get(target, prop);
@ -36,14 +64,14 @@ class ScramjetClient {
if (handler.construct) {
h.construct = function (
target: any,
constructor: any,
argArray: any[],
newTarget: AnyFunction
) {
let returnValue: any = null;
const ctx: ProxyCtx = {
fn: target,
fn: constructor,
this: null,
args: argArray,
newTarget: newTarget,
@ -63,11 +91,11 @@ class ScramjetClient {
}
if (handler.apply) {
h.apply = function (target: any, thisArg: any, argArray: any[]) {
h.apply = function (fn: any, thisArg: any, argArray: any[]) {
let returnValue: any = null;
const ctx: ProxyCtx = {
fn: target,
fn,
this: thisArg,
args: argArray,
newTarget: null,
@ -88,36 +116,77 @@ class ScramjetClient {
target[prop] = new Proxy(value, h);
}
Trap<T>(name: string | string[], descriptor: Trap<T>) {
if (Array.isArray(name)) {
for (const n of name) {
this.Trap(n, descriptor);
}
return;
}
const split = name.split(".");
const prop = split.pop();
const target = split.reduce((a, b) => a?.[b], self);
this.RawTrap(target, prop, descriptor);
}
RawTrap<T>(target: any, prop: string, descriptor: Trap<T>) {
if (!target) return;
if (!prop) return;
if (!Reflect.has(target, prop)) return;
const oldDescriptor = Object.getOwnPropertyDescriptor(target, prop);
const ctx: TrapCtx<T> = {
this: null,
get: function () {
return oldDescriptor && oldDescriptor.get.call(this.this);
},
set: function (v: T) {
oldDescriptor && oldDescriptor.set.call(this.this, v);
},
};
delete target[prop];
const desc: PropertyDescriptor = {};
if (descriptor.get) {
desc.get = function () {
ctx.this = this;
return descriptor.get(ctx);
};
} else if (oldDescriptor?.get) {
desc.get = oldDescriptor.get;
}
if (descriptor.set) {
desc.set = function (v: T) {
ctx.this = this;
descriptor.set(ctx, v);
};
} else if (oldDescriptor?.set) {
desc.set = oldDescriptor.set;
}
if (descriptor.enumerable) desc.enumerable = descriptor.enumerable;
else if (oldDescriptor?.enumerable)
desc.enumerable = oldDescriptor.enumerable;
if (descriptor.configurable) desc.configurable = descriptor.configurable;
else if (oldDescriptor?.configurable)
desc.configurable = oldDescriptor.configurable;
Object.defineProperty(target, prop, desc);
}
get url(): URL {
return new URL(decodeUrl(location.href));
}
async init() {
function b64(buffer) {
let binary = "";
let bytes = new Uint8Array(buffer);
let len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
const arraybuffer = await (await fetch("/scramjet.png")).arrayBuffer();
console.log(
"%cb",
`
background-image: url(data:image/png;base64,${b64(arraybuffer)});
color: transparent;
padding-left: 200px;
padding-bottom: 100px;
background-size: contain;
background-position: center center;
background-repeat: no-repeat;
`
);
}
async init() {}
}
export const client = new ScramjetClient();