diff options
Diffstat (limited to 'public/~partytown/debug/partytown-ww-atomics.js')
| -rw-r--r-- | public/~partytown/debug/partytown-ww-atomics.js | 1876 |
1 files changed, 0 insertions, 1876 deletions
diff --git a/public/~partytown/debug/partytown-ww-atomics.js b/public/~partytown/debug/partytown-ww-atomics.js deleted file mode 100644 index 8745785..0000000 --- a/public/~partytown/debug/partytown-ww-atomics.js +++ /dev/null @@ -1,1876 +0,0 @@ -/* Partytown 0.7.5 - MIT builder.io */ -(self => { - const WinIdKey = Symbol(); - const InstanceIdKey = Symbol(); - const InstanceDataKey = Symbol(); - const NamespaceKey = Symbol(); - const ApplyPathKey = Symbol(); - const InstanceStateKey = Symbol(); - const HookContinue = Symbol(); - const HookPrevent = Symbol(); - const webWorkerInstances = new Map; - const webWorkerRefsByRefId = {}; - const webWorkerRefIdsByRef = new WeakMap; - const postMessages = []; - const webWorkerCtx = {}; - const webWorkerlocalStorage = new Map; - const webWorkerSessionStorage = new Map; - const environments = {}; - const cachedDimensions = new Map; - const cachedStructure = new Map; - const commaSplit = str => str.split(","); - const partytownLibUrl = url => { - url = webWorkerCtx.$libPath$ + url; - if (new URL(url).origin != location.origin) { - throw "Invalid " + url; - } - return url; - }; - const getterDimensionPropNames = commaSplit("clientWidth,clientHeight,clientTop,clientLeft,innerWidth,innerHeight,offsetWidth,offsetHeight,offsetTop,offsetLeft,outerWidth,outerHeight,pageXOffset,pageYOffset,scrollWidth,scrollHeight,scrollTop,scrollLeft"); - const elementStructurePropNames = commaSplit("childElementCount,children,firstElementChild,lastElementChild,nextElementSibling,previousElementSibling"); - const structureChangingMethodNames = commaSplit("insertBefore,remove,removeChild,replaceChild"); - const dimensionChangingSetterNames = commaSplit("className,width,height,hidden,innerHTML,innerText,textContent"); - const dimensionChangingMethodNames = commaSplit("setAttribute,setAttributeNS,setProperty"); - const eventTargetMethods = commaSplit("addEventListener,dispatchEvent,removeEventListener"); - const nonBlockingMethods = eventTargetMethods.concat(dimensionChangingMethodNames, commaSplit("add,observe,remove,unobserve")); - const IS_TAG_REG = /^[A-Z_]([A-Z0-9-]*[A-Z0-9])?$/; - const noop = () => {}; - const len = obj => obj.length; - const getConstructorName = obj => { - var _a, _b, _c; - try { - const constructorName = null === (_a = null == obj ? void 0 : obj.constructor) || void 0 === _a ? void 0 : _a.name; - if (constructorName) { - return constructorName; - } - } catch (e) {} - try { - const zoneJsConstructorName = null === (_c = null === (_b = null == obj ? void 0 : obj.__zone_symbol__originalInstance) || void 0 === _b ? void 0 : _b.constructor) || void 0 === _c ? void 0 : _c.name; - if (zoneJsConstructorName) { - return zoneJsConstructorName; - } - } catch (e) {} - return ""; - }; - const EMPTY_ARRAY = []; - const randomId = () => Math.round(Math.random() * Number.MAX_SAFE_INTEGER).toString(36); - const SCRIPT_TYPE = "text/partytown"; - const defineProperty = (obj, memberName, descriptor) => Object.defineProperty(obj, memberName, { - ...descriptor, - configurable: true - }); - const defineConstructorName = (Cstr, value) => defineProperty(Cstr, "name", { - value: value - }); - const definePrototypeProperty = (Cstr, memberName, descriptor) => defineProperty(Cstr.prototype, memberName, descriptor); - const definePrototypePropertyDescriptor = (Cstr, propertyDescriptorMap) => Object.defineProperties(Cstr.prototype, propertyDescriptorMap); - const definePrototypeValue = (Cstr, memberName, value) => definePrototypeProperty(Cstr, memberName, { - value: value, - writable: true - }); - const hasInstanceStateValue = (instance, stateKey) => stateKey in instance[InstanceStateKey]; - const getInstanceStateValue = (instance, stateKey) => instance[InstanceStateKey][stateKey]; - const setInstanceStateValue = (instance, stateKey, stateValue) => instance[InstanceStateKey][stateKey] = stateValue; - const setWorkerRef = (ref, refId) => { - if (!(refId = webWorkerRefIdsByRef.get(ref))) { - webWorkerRefIdsByRef.set(ref, refId = randomId()); - webWorkerRefsByRefId[refId] = ref; - } - return refId; - }; - const getOrCreateNodeInstance = (winId, instanceId, nodeName, namespace, instance) => { - instance = webWorkerInstances.get(instanceId); - if (!instance && nodeName && environments[winId]) { - instance = environments[winId].$createNode$(nodeName, instanceId, namespace); - webWorkerInstances.set(instanceId, instance); - } - return instance; - }; - const definePrototypeNodeType = (Cstr, nodeType) => definePrototypeValue(Cstr, "nodeType", nodeType); - const cachedTreeProps = (Cstr, treeProps) => treeProps.map((propName => definePrototypeProperty(Cstr, propName, { - get() { - let cacheKey = getInstanceCacheKey(this, propName); - let result = cachedStructure.get(cacheKey); - if (!result) { - result = getter(this, [ propName ]); - cachedStructure.set(cacheKey, result); - } - return result; - } - }))); - const getInstanceCacheKey = (instance, memberName, args) => [ instance[WinIdKey], instance[InstanceIdKey], memberName, ...(args || EMPTY_ARRAY).map((arg => String(arg && arg[WinIdKey] ? arg[InstanceIdKey] : arg))) ].join("."); - const cachedProps = (Cstr, propNames) => commaSplit(propNames).map((propName => definePrototypeProperty(Cstr, propName, { - get() { - hasInstanceStateValue(this, propName) || setInstanceStateValue(this, propName, getter(this, [ propName ])); - return getInstanceStateValue(this, propName); - }, - set(val) { - getInstanceStateValue(this, propName) !== val && setter(this, [ propName ], val); - setInstanceStateValue(this, propName, val); - } - }))); - const cachedDimensionProps = Cstr => getterDimensionPropNames.map((propName => definePrototypeProperty(Cstr, propName, { - get() { - const dimension = cachedDimensions.get(getInstanceCacheKey(this, propName)); - if ("number" == typeof dimension) { - return dimension; - } - const groupedDimensions = getter(this, [ propName ], getterDimensionPropNames); - if (groupedDimensions && "object" == typeof groupedDimensions) { - Object.entries(groupedDimensions).map((([dimensionPropName, value]) => cachedDimensions.set(getInstanceCacheKey(this, dimensionPropName), value))); - return groupedDimensions[propName]; - } - return groupedDimensions; - } - }))); - const cachedDimensionMethods = (Cstr, dimensionMethodNames) => dimensionMethodNames.map((methodName => { - Cstr.prototype[methodName] = function(...args) { - let cacheKey = getInstanceCacheKey(this, methodName, args); - let dimensions = cachedDimensions.get(cacheKey); - if (!dimensions) { - dimensions = callMethod(this, [ methodName ], args); - cachedDimensions.set(cacheKey, dimensions); - } - return dimensions; - }; - })); - const serializeForMain = ($winId$, $instanceId$, value, added, type) => void 0 !== value && (type = typeof value) ? "string" === type || "boolean" === type || "number" === type || null == value ? [ 0, value ] : "function" === type ? [ 4, { - $winId$: $winId$, - $instanceId$: $instanceId$, - $refId$: setWorkerRef(value) - } ] : (added = added || new Set) && Array.isArray(value) ? added.has(value) ? [ 1, [] ] : added.add(value) && [ 1, value.map((v => serializeForMain($winId$, $instanceId$, v, added))) ] : "object" === type ? value[InstanceIdKey] ? [ 3, [ value[WinIdKey], value[InstanceIdKey] ] ] : value instanceof Event ? [ 5, serializeObjectForMain($winId$, $instanceId$, value, false, added) ] : supportsTrustedHTML && value instanceof TrustedHTML ? [ 0, value.toString() ] : value instanceof ArrayBuffer ? [ 8, value ] : ArrayBuffer.isView(value) ? [ 9, value.buffer, getConstructorName(value) ] : [ 2, serializeObjectForMain($winId$, $instanceId$, value, true, added) ] : void 0 : value; - const supportsTrustedHTML = "undefined" != typeof TrustedHTML; - const serializeObjectForMain = (winId, instanceId, obj, includeFunctions, added, serializedObj, propName, propValue) => { - serializedObj = {}; - if (!added.has(obj)) { - added.add(obj); - for (propName in obj) { - propValue = obj[propName]; - (includeFunctions || "function" != typeof propValue) && (serializedObj[propName] = serializeForMain(winId, instanceId, propValue, added)); - } - } - return serializedObj; - }; - const serializeInstanceForMain = (instance, value) => instance ? serializeForMain(instance[WinIdKey], instance[InstanceIdKey], value) : [ 0, value ]; - const deserializeFromMain = (winId, instanceId, applyPath, serializedValueTransfer, serializedType, serializedValue, obj, key) => { - if (serializedValueTransfer) { - serializedType = serializedValueTransfer[0]; - serializedValue = serializedValueTransfer[1]; - if (0 === serializedType || 11 === serializedType || 12 === serializedType) { - return serializedValue; - } - if (4 === serializedType) { - return deserializeRefFromMain(applyPath, serializedValue); - } - if (6 === serializedType) { - return winId && applyPath.length > 0 ? (...args) => callMethod(environments[winId].$window$, applyPath, args, 1) : noop; - } - if (3 === serializedType) { - return getOrCreateSerializedInstance(serializedValue); - } - if (7 === serializedType) { - return new NodeList(serializedValue.map(getOrCreateSerializedInstance)); - } - if (10 === serializedType) { - return new Attr(serializedValue); - } - if (1 === serializedType) { - return serializedValue.map((v => deserializeFromMain(winId, instanceId, applyPath, v))); - } - if (14 === serializedType) { - return new CustomError(serializedValue); - } - obj = {}; - for (key in serializedValue) { - obj[key] = deserializeFromMain(winId, instanceId, [ ...applyPath, key ], serializedValue[key]); - } - if (13 === serializedType) { - return new environments[winId].$window$.CSSStyleDeclaration(winId, instanceId, applyPath, obj); - } - if (5 === serializedType) { - if ("message" === obj.type && obj.origin) { - let postMessageKey = JSON.stringify(obj.data); - let postMessageData = postMessages.find((pm => pm.$data$ === postMessageKey)); - let env; - if (postMessageData) { - env = environments[postMessageData.$winId$]; - if (env) { - obj.source = env.$window$; - obj.origin = env.$location$.origin; - } - } - } - return new Proxy(new Event(obj.type, obj), { - get: (target, propName) => propName in obj ? obj[propName] : "function" == typeof target[String(propName)] ? noop : target[String(propName)] - }); - } - if (2 === serializedType) { - return obj; - } - } - }; - const getOrCreateSerializedInstance = ([winId, instanceId, nodeName]) => instanceId === winId && environments[winId] ? environments[winId].$window$ : getOrCreateNodeInstance(winId, instanceId, nodeName); - const deserializeRefFromMain = (applyPath, {$winId$: $winId$, $instanceId$: $instanceId$, $nodeName$: $nodeName$, $refId$: $refId$}) => { - webWorkerRefsByRefId[$refId$] || webWorkerRefIdsByRef.set(webWorkerRefsByRefId[$refId$] = function(...args) { - const instance = getOrCreateNodeInstance($winId$, $instanceId$, $nodeName$); - return callMethod(instance, applyPath, args); - }, $refId$); - return webWorkerRefsByRefId[$refId$]; - }; - class CustomError extends Error { - constructor(errorObject) { - super(errorObject.message); - this.name = errorObject.name; - this.message = errorObject.message; - this.stack = errorObject.stack; - } - } - class NodeList { - constructor(nodes) { - (this._ = nodes).map(((node, index) => this[index] = node)); - } - entries() { - return this._.entries(); - } - forEach(cb, thisArg) { - this._.map(cb, thisArg); - } - item(index) { - return this[index]; - } - keys() { - return this._.keys(); - } - get length() { - return len(this._); - } - values() { - return this._.values(); - } - [Symbol.iterator]() { - return this._[Symbol.iterator](); - } - } - const Attr = class { - constructor(serializedAttr) { - this.name = serializedAttr[0]; - this.value = serializedAttr[1]; - } - get nodeName() { - return this.name; - } - get nodeType() { - return 2; - } - }; - const warnCrossOrgin = (apiType, apiName, env) => console.warn(`Partytown unable to ${apiType} cross-origin ${apiName}: ` + env.$location$); - const logWorker = (msg, winId) => { - try { - const config = webWorkerCtx.$config$; - if (config.logStackTraces) { - const frames = (new Error).stack.split("\n"); - const i = frames.findIndex((f => f.includes("logWorker"))); - msg += "\n" + frames.slice(i + 1).join("\n"); - } - let prefix; - let color; - if (winId) { - prefix = `Worker (${normalizedWinId(winId)}) 🎉`; - color = winColor(winId); - } else { - prefix = self.name; - color = "#9844bf"; - } - if (webWorkerCtx.lastLog !== msg) { - webWorkerCtx.lastLog = msg; - console.debug.apply(console, [ `%c${prefix}`, `background: ${color}; color: white; padding: 2px 3px; border-radius: 2px; font-size: 0.8em;`, msg ]); - } - } catch (e) {} - }; - const winIds = []; - const normalizedWinId = winId => { - winIds.includes(winId) || winIds.push(winId); - return winIds.indexOf(winId) + 1; - }; - const winColor = winId => { - const colors = [ "#00309e", "#ea3655", "#eea727" ]; - const index = normalizedWinId(winId) - 1; - return colors[index] || colors[colors.length - 1]; - }; - const getTargetProp = (target, applyPath) => { - let n = ""; - if (target) { - const cstrName = getConstructorName(target); - if ("Window" === cstrName) { - n = ""; - } else if ("string" == typeof target[InstanceDataKey]) { - let nodeName = target[InstanceDataKey]; - n = "#text" === nodeName ? "textNode." : "#comment" === nodeName ? "commentNode." : "#document" === nodeName ? "document." : "html" === nodeName ? "doctype." : nodeName.toLowerCase() + "."; - } else { - n = "nodeType" in target && 2 === target.nodeType ? "attributes." : "CanvasRenderingContext2D" === cstrName ? "context2D." : "CanvasRenderingContextWebGL" === cstrName ? "contextWebGL." : "CSSStyleDeclaration" === cstrName ? "style." : "MutationObserver" === cstrName ? "mutationObserver." : "NamedNodeMap" === cstrName ? "namedNodeMap." : "ResizeObserver" === cstrName ? "resizeObserver." : cstrName.substring(0, 1).toLowerCase() + cstrName.substring(1) + "."; - } - target[ApplyPathKey] && target[ApplyPathKey].length && (n += [ ...target[ApplyPathKey] ].join(".") + "."); - } - if (applyPath.length > 1) { - const first = applyPath.slice(0, applyPath.length - 1); - const last = applyPath[applyPath.length - 1]; - if (!isNaN(last)) { - return n + `${first.join(".")}[${last}]`; - } - } - return n + applyPath.join("."); - }; - const getLogValue = (applyPath, v) => { - const type = typeof v; - if (void 0 === v) { - return "undefined"; - } - if ("boolean" === type || "number" === type || null == v) { - return JSON.stringify(v); - } - if ("string" === type) { - return applyPath.includes("cookie") ? JSON.stringify(v.slice(0, 10) + "...") : JSON.stringify(v.length > 50 ? v.slice(0, 40) + "..." : v); - } - if (Array.isArray(v)) { - return `[${v.map(getLogValue).join(", ")}]`; - } - if ("object" === type) { - const instanceId = v[InstanceIdKey]; - const cstrName = getConstructorName(v); - if ("string" == typeof instanceId) { - if ("Window" === cstrName) { - return "window"; - } - if ("string" == typeof v[InstanceDataKey]) { - if (1 === v.nodeType) { - return `<${v[InstanceDataKey].toLowerCase()}>`; - } - if (10 === v.nodeType) { - return `<!DOCTYPE ${v[InstanceDataKey]}>`; - } - if (v.nodeType <= 11) { - return v[InstanceDataKey]; - } - } - return "¯\\_(ツ)_/¯ instance obj"; - } - return v[Symbol.iterator] ? `[${Array.from(v).map((i => getLogValue(applyPath, i))).join(", ")}]` : "value" in v ? "string" == typeof v.value ? `"${v.value}"` : objToString(v.value) : objToString(v); - } - return (v => "object" == typeof v && v && v.then)(v) ? "Promise" : "function" === type ? `ƒ() ${v.name || ""}`.trim() : `¯\\_(ツ)_/¯ ${String(v)}`.trim(); - }; - const objToString = obj => { - const s = []; - for (let key in obj) { - const value = obj[key]; - const type = typeof value; - "string" === type ? s.push(`${key}: "${value}"`) : "function" === type ? s.push(`${key}: ƒ`) : Array.isArray(type) ? s.push(`${key}: [..]`) : "object" === type && value ? s.push(`${key}: {..}`) : s.push(`${key}: ${String(value)}`); - } - let str = s.join(", "); - str.length > 200 && (str = str.substring(0, 200) + ".."); - return `{ ${str} }`; - }; - const logDimensionCacheClearStyle = (target, propName) => { - (webWorkerCtx.$config$.logGetters || webWorkerCtx.$config$.logSetters) && logWorker(`Dimension cache cleared from style.${propName} setter`, target[WinIdKey]); - }; - const logDimensionCacheClearMethod = (target, methodName) => { - (webWorkerCtx.$config$.logGetters || webWorkerCtx.$config$.logCalls) && logWorker(`Dimension cache cleared from method call ${methodName}()`, target[WinIdKey]); - }; - const taskQueue = []; - const queue = (instance, $applyPath$, callType, $assignInstanceId$, $groupedGetters$, buffer) => { - if (instance[ApplyPathKey]) { - taskQueue.push({ - $winId$: instance[WinIdKey], - $instanceId$: instance[InstanceIdKey], - $applyPath$: [ ...instance[ApplyPathKey], ...$applyPath$ ], - $assignInstanceId$: $assignInstanceId$, - $groupedGetters$: $groupedGetters$ - }); - taskQueue[len(taskQueue) - 1].$debug$ = ((target, applyPath, callType) => { - let m = getTargetProp(target, applyPath); - 1 === callType ? m += " (blocking)" : 2 === callType ? m += " (non-blocking)" : 3 === callType && (m += " (non-blocking, no-side-effect)"); - return m.trim(); - })(instance, $applyPath$, callType); - buffer && 3 !== callType && console.error("buffer must be sent NonBlockingNoSideEffect"); - if (3 === callType) { - webWorkerCtx.$postMessage$([ 12, { - $msgId$: randomId(), - $tasks$: [ ...taskQueue ] - } ], buffer ? [ buffer instanceof ArrayBuffer ? buffer : buffer.buffer ] : void 0); - taskQueue.length = 0; - } else if (1 === callType) { - return sendToMain(true); - } - webWorkerCtx.$asyncMsgTimer$ = setTimeout(sendToMain, 20); - } - }; - const sendToMain = isBlocking => { - clearTimeout(webWorkerCtx.$asyncMsgTimer$); - if (len(taskQueue)) { - webWorkerCtx.$config$.logMainAccess && logWorker(`Main access, tasks sent: ${taskQueue.length}`); - const endTask = taskQueue[len(taskQueue) - 1]; - const accessReq = { - $msgId$: randomId(), - $tasks$: [ ...taskQueue ] - }; - taskQueue.length = 0; - if (isBlocking) { - const accessRsp = ((webWorkerCtx, accessReq) => { - const sharedDataBuffer = webWorkerCtx.$sharedDataBuffer$; - const sharedData = new Int32Array(sharedDataBuffer); - Atomics.store(sharedData, 0, 0); - webWorkerCtx.$postMessage$([ 11, accessReq ]); - Atomics.wait(sharedData, 0, 0); - let dataLength = Atomics.load(sharedData, 0); - let accessRespStr = ""; - let i = 0; - for (;i < dataLength; i++) { - accessRespStr += String.fromCharCode(sharedData[i + 1]); - } - return JSON.parse(accessRespStr); - })(webWorkerCtx, accessReq); - const isPromise = accessRsp.$isPromise$; - const rtnValue = deserializeFromMain(endTask.$winId$, endTask.$instanceId$, endTask.$applyPath$, accessRsp.$rtnValue$); - if (accessRsp.$error$) { - if (isPromise) { - return Promise.reject(accessRsp.$error$); - } - throw new Error(accessRsp.$error$); - } - return isPromise ? Promise.resolve(rtnValue) : rtnValue; - } - webWorkerCtx.$postMessage$([ 12, accessReq ]); - } - }; - const getter = (instance, applyPath, groupedGetters, rtnValue) => { - if (webWorkerCtx.$config$.get) { - rtnValue = webWorkerCtx.$config$.get(createHookOptions(instance, applyPath)); - if (rtnValue !== HookContinue) { - return rtnValue; - } - } - rtnValue = queue(instance, applyPath, 1, void 0, groupedGetters); - ((target, applyPath, rtnValue, restrictedToWorker = false, groupedGetters = false) => { - if (webWorkerCtx.$config$.logGetters) { - try { - const msg = `Get ${getTargetProp(target, applyPath)}, returned: ${getLogValue(applyPath, rtnValue)}${restrictedToWorker ? " (restricted to worker)" : ""}${groupedGetters ? " (grouped getter)" : ""}`; - msg.includes("Symbol(") || logWorker(msg, target[WinIdKey]); - } catch (e) {} - } - })(instance, applyPath, rtnValue, false, !!groupedGetters); - return rtnValue; - }; - const setter = (instance, applyPath, value, hookSetterValue) => { - if (webWorkerCtx.$config$.set) { - hookSetterValue = webWorkerCtx.$config$.set({ - value: value, - prevent: HookPrevent, - ...createHookOptions(instance, applyPath) - }); - if (hookSetterValue === HookPrevent) { - return; - } - hookSetterValue !== HookContinue && (value = hookSetterValue); - } - if (dimensionChangingSetterNames.some((s => applyPath.includes(s)))) { - cachedDimensions.clear(); - ((target, propName) => { - (webWorkerCtx.$config$.logGetters || webWorkerCtx.$config$.logSetters) && logWorker(`Dimension cache cleared from setter "${propName}"`, target[WinIdKey]); - })(instance, applyPath[applyPath.length - 1]); - } - applyPath = [ ...applyPath, serializeInstanceForMain(instance, value), 0 ]; - ((target, applyPath, value, restrictedToWorker = false) => { - if (webWorkerCtx.$config$.logSetters) { - try { - applyPath = applyPath.slice(0, applyPath.length - 2); - logWorker(`Set ${getTargetProp(target, applyPath)}, value: ${getLogValue(applyPath, value)}${restrictedToWorker ? " (restricted to worker)" : ""}`, target[WinIdKey]); - } catch (e) {} - } - })(instance, applyPath, value); - queue(instance, applyPath, 2); - }; - const callMethod = (instance, applyPath, args, callType, assignInstanceId, buffer, rtnValue, methodName) => { - if (webWorkerCtx.$config$.apply) { - rtnValue = webWorkerCtx.$config$.apply({ - args: args, - ...createHookOptions(instance, applyPath) - }); - if (rtnValue !== HookContinue) { - return rtnValue; - } - } - methodName = applyPath[len(applyPath) - 1]; - applyPath = [ ...applyPath, serializeInstanceForMain(instance, args) ]; - callType = callType || (nonBlockingMethods.includes(methodName) ? 2 : 1); - if ("setAttribute" === methodName && hasInstanceStateValue(instance, args[0])) { - setInstanceStateValue(instance, args[0], args[1]); - } else if (structureChangingMethodNames.includes(methodName)) { - cachedDimensions.clear(); - cachedStructure.clear(); - ((target, methodName) => { - (webWorkerCtx.$config$.logGetters || webWorkerCtx.$config$.logCalls) && logWorker(`Dimension and DOM structure cache cleared from method call ${methodName}()`, target[WinIdKey]); - })(instance, methodName); - } else if (dimensionChangingMethodNames.includes(methodName)) { - callType = 2; - cachedDimensions.clear(); - logDimensionCacheClearMethod(instance, methodName); - } - rtnValue = queue(instance, applyPath, callType, assignInstanceId, void 0, buffer); - ((target, applyPath, args, rtnValue) => { - if (webWorkerCtx.$config$.logCalls) { - try { - applyPath = applyPath.slice(0, applyPath.length - 1); - logWorker(`Call ${getTargetProp(target, applyPath)}(${args.map((v => getLogValue(applyPath, v))).join(", ")}), returned: ${getLogValue(applyPath, rtnValue)}`, target[WinIdKey]); - } catch (e) {} - } - })(instance, applyPath, args, rtnValue); - return rtnValue; - }; - const constructGlobal = (instance, cstrName, args) => { - ((target, cstrName, args) => { - if (webWorkerCtx.$config$.logCalls) { - try { - logWorker(`Construct new ${cstrName}(${args.map((v => getLogValue([], v))).join(", ")})`, target[WinIdKey]); - } catch (e) {} - } - })(instance, cstrName, args); - queue(instance, [ 1, cstrName, serializeInstanceForMain(instance, args) ], 1); - }; - const createHookOptions = (instance, applyPath) => ({ - name: applyPath.join("."), - continue: HookContinue, - nodeName: instance[InstanceDataKey], - constructor: getConstructorName(instance), - instance: instance, - window: environments[instance[WinIdKey]].$window$ - }); - const addStorageApi = (win, storageName, storages, isSameOrigin, env) => { - let getItems = items => { - items = storages.get(win.origin); - items || storages.set(win.origin, items = []); - return items; - }; - let getIndexByKey = key => getItems().findIndex((i => i[STORAGE_KEY] === key)); - let index; - let item; - let storage = { - getItem(key) { - index = getIndexByKey(key); - return index > -1 ? getItems()[index][STORAGE_VALUE] : null; - }, - setItem(key, value) { - index = getIndexByKey(key); - index > -1 ? getItems()[index][STORAGE_VALUE] = value : getItems().push([ key, value ]); - isSameOrigin ? callMethod(win, [ storageName, "setItem" ], [ key, value ], 2) : warnCrossOrgin("set", storageName, env); - }, - removeItem(key) { - index = getIndexByKey(key); - index > -1 && getItems().splice(index, 1); - isSameOrigin ? callMethod(win, [ storageName, "removeItem" ], [ key ], 2) : warnCrossOrgin("remove", storageName, env); - }, - key(index) { - item = getItems()[index]; - return item ? item[STORAGE_KEY] : null; - }, - clear() { - getItems().length = 0; - isSameOrigin ? callMethod(win, [ storageName, "clear" ], EMPTY_ARRAY, 2) : warnCrossOrgin("clear", storageName, env); - }, - get length() { - return getItems().length; - } - }; - win[storageName] = new Proxy(storage, { - get: (target, key) => Reflect.has(target, key) ? Reflect.get(target, key) : target.getItem(key), - set(target, key, value) { - target.setItem(key, value); - return true; - }, - has: (target, key) => !!Reflect.has(target, key) || "string" == typeof key && null !== target.getItem(key), - deleteProperty(target, key) { - target.removeItem(key); - return true; - } - }); - }; - const STORAGE_KEY = 0; - const STORAGE_VALUE = 1; - const createCSSStyleDeclarationCstr = (win, WorkerBase, cstrName) => { - win[cstrName] = defineConstructorName(class extends WorkerBase { - constructor(winId, instanceId, applyPath, styles) { - super(winId, instanceId, applyPath, styles || {}); - return new Proxy(this, { - get(target, propName) { - if (target[propName]) { - return target[propName]; - } - target[propName] || "string" != typeof propName || target[InstanceDataKey][propName] || (target[InstanceDataKey][propName] = getter(target, [ propName ])); - return target[InstanceDataKey][propName]; - }, - set(target, propName, propValue) { - target[InstanceDataKey][propName] = propValue; - setter(target, [ propName ], propValue); - logDimensionCacheClearStyle(target, propName); - cachedDimensions.clear(); - return true; - } - }); - } - setProperty(...args) { - this[InstanceDataKey][args[0]] = args[1]; - callMethod(this, [ "setProperty" ], args, 2); - logDimensionCacheClearStyle(this, args[0]); - cachedDimensions.clear(); - } - getPropertyValue(propName) { - return this[propName]; - } - removeProperty(propName) { - let value = this[InstanceDataKey][propName]; - callMethod(this, [ "removeProperty" ], [ propName ], 2); - logDimensionCacheClearStyle(this, propName); - cachedDimensions.clear(); - this[InstanceDataKey][propName] = void 0; - return value; - } - }, cstrName); - }; - const createCSSStyleSheetConstructor = (win, cssStyleSheetCstrName) => { - win[cssStyleSheetCstrName] = defineConstructorName(class { - constructor(ownerNode) { - this.ownerNode = ownerNode; - } - get cssRules() { - const ownerNode = this.ownerNode; - return new Proxy({}, { - get(target, propKey) { - const propName = String(propKey); - return "item" === propName ? index => getCssRule(ownerNode, index) : "length" === propName ? getCssRules(ownerNode).length : isNaN(propName) ? target[propKey] : getCssRule(ownerNode, propName); - } - }); - } - insertRule(ruleText, index) { - const cssRules = getCssRules(this.ownerNode); - index = void 0 === index ? 0 : index; - if (index >= 0 && index <= cssRules.length) { - callMethod(this.ownerNode, [ "sheet", "insertRule" ], [ ruleText, index ], 2); - cssRules.splice(index, 0, 0); - } - logDimensionCacheClearMethod(this.ownerNode, "insertRule"); - cachedDimensions.clear(); - return index; - } - deleteRule(index) { - callMethod(this.ownerNode, [ "sheet", "deleteRule" ], [ index ], 2); - getCssRules(this.ownerNode).splice(index, 1); - logDimensionCacheClearMethod(this.ownerNode, "deleteRule"); - cachedDimensions.clear(); - } - get type() { - return "text/css"; - } - }, cssStyleSheetCstrName); - const HTMLStyleDescriptorMap = { - sheet: { - get() { - return new win[cssStyleSheetCstrName](this); - } - } - }; - definePrototypePropertyDescriptor(win.HTMLStyleElement, HTMLStyleDescriptorMap); - }; - const getCssRules = (ownerNode, cssRules) => { - cssRules = getInstanceStateValue(ownerNode, 2); - if (!cssRules) { - cssRules = getter(ownerNode, [ "sheet", "cssRules" ]); - setInstanceStateValue(ownerNode, 2, cssRules); - } - return cssRules; - }; - const getCssRule = (ownerNode, index, cssRules) => { - cssRules = getCssRules(ownerNode); - 0 === cssRules[index] && (cssRules[index] = getter(ownerNode, [ "sheet", "cssRules", parseInt(index, 10) ])); - return cssRules[index]; - }; - const runScriptContent = (env, instanceId, scriptContent, winId, errorMsg) => { - try { - webWorkerCtx.$config$.logScriptExecution && logWorker(`Execute script: ${scriptContent.substring(0, 100).split("\n").map((l => l.trim())).join(" ").trim().substring(0, 60)}...`, winId); - env.$currentScriptId$ = instanceId; - run(env, scriptContent); - } catch (contentError) { - console.error(scriptContent, contentError); - errorMsg = String(contentError.stack || contentError); - } - env.$currentScriptId$ = ""; - return errorMsg; - }; - const run = (env, scriptContent, scriptUrl) => { - env.$runWindowLoadEvent$ = 1; - scriptContent = `with(this){${scriptContent.replace(/\bthis\b/g, "(thi$(this)?window:this)").replace(/\/\/# so/g, "//Xso")}\n;function thi$(t){return t===this}};${(webWorkerCtx.$config$.globalFns || []).filter((globalFnName => /[a-zA-Z_$][0-9a-zA-Z_$]*/.test(globalFnName))).map((g => `(typeof ${g}=='function'&&(this.${g}=${g}))`)).join(";")};` + (scriptUrl ? "\n//# sourceURL=" + scriptUrl : ""); - env.$isSameOrigin$ || (scriptContent = scriptContent.replace(/.postMessage\(/g, `.postMessage('${env.$winId$}',`)); - new Function(scriptContent).call(env.$window$); - env.$runWindowLoadEvent$ = 0; - }; - const runStateLoadHandlers = (instance, type, handlers) => { - handlers = getInstanceStateValue(instance, type); - handlers && setTimeout((() => handlers.map((cb => cb({ - type: type - }))))); - }; - const resolveToUrl = (env, url, type, baseLocation, resolvedUrl, configResolvedUrl) => { - baseLocation = env.$location$; - while (!baseLocation.host) { - env = environments[env.$parentWinId$]; - baseLocation = env.$location$; - if (env.$winId$ === env.$parentWinId$) { - break; - } - } - resolvedUrl = new URL(url || "", baseLocation); - if (type && webWorkerCtx.$config$.resolveUrl) { - configResolvedUrl = webWorkerCtx.$config$.resolveUrl(resolvedUrl, baseLocation, type); - if (configResolvedUrl) { - return configResolvedUrl; - } - } - return resolvedUrl; - }; - const resolveUrl = (env, url, type) => resolveToUrl(env, url, type) + ""; - const getPartytownScript = () => `<script src="${partytownLibUrl("partytown.js?v=0.7.5")}"><\/script>`; - const createImageConstructor = env => class HTMLImageElement { - constructor() { - this.s = ""; - this.l = []; - this.e = []; - this.style = {}; - } - get src() { - return this.s; - } - set src(src) { - webWorkerCtx.$config$.logImageRequests && logWorker(`Image() request: ${resolveUrl(env, src, "image")}`, env.$winId$); - this.s = src; - fetch(resolveUrl(env, src, "image"), { - mode: "no-cors", - credentials: "include", - keepalive: true - }).then((rsp => { - rsp.ok || 0 === rsp.status ? this.l.map((cb => cb({ - type: "load" - }))) : this.e.map((cb => cb({ - type: "error" - }))); - }), (() => this.e.forEach((cb => cb({ - type: "error" - }))))); - } - addEventListener(eventName, cb) { - "load" === eventName && this.l.push(cb); - "error" === eventName && this.e.push(cb); - } - get onload() { - return this.l[0]; - } - set onload(cb) { - this.l = [ cb ]; - } - get onerror() { - return this.e[0]; - } - set onerror(cb) { - this.e = [ cb ]; - } - }; - const HTMLSrcElementDescriptorMap = { - addEventListener: { - value(...args) { - const eventName = args[0]; - const callbacks = getInstanceStateValue(this, eventName) || []; - callbacks.push(args[1]); - setInstanceStateValue(this, eventName, callbacks); - } - }, - async: { - get: noop, - set: noop - }, - defer: { - get: noop, - set: noop - }, - onload: { - get() { - let callbacks = getInstanceStateValue(this, "load"); - return callbacks && callbacks[0] || null; - }, - set(cb) { - setInstanceStateValue(this, "load", cb ? [ cb ] : null); - } - }, - onerror: { - get() { - let callbacks = getInstanceStateValue(this, "error"); - return callbacks && callbacks[0] || null; - }, - set(cb) { - setInstanceStateValue(this, "error", cb ? [ cb ] : null); - } - }, - getAttribute: { - value(attrName) { - return "src" === attrName ? this.src : callMethod(this, [ "getAttribute" ], [ attrName ]); |
