1 |
|
2 | (window => {
|
3 | const isPromise = v => "object" == typeof v && v && v.then;
|
4 | const noop = () => {};
|
5 | const len = obj => obj.length;
|
6 | const getConstructorName = obj => {
|
7 | var _a, _b, _c;
|
8 | try {
|
9 | const constructorName = null === (_a = null == obj ? void 0 : obj.constructor) || void 0 === _a ? void 0 : _a.name;
|
10 | if (constructorName) {
|
11 | return constructorName;
|
12 | }
|
13 | } catch (e) {}
|
14 | try {
|
15 | 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;
|
16 | if (zoneJsConstructorName) {
|
17 | return zoneJsConstructorName;
|
18 | }
|
19 | } catch (e) {}
|
20 | return "";
|
21 | };
|
22 | const startsWith = (str, val) => str.startsWith(val);
|
23 | const isValidMemberName = memberName => !(startsWith(memberName, "webkit") || startsWith(memberName, "toJSON") || startsWith(memberName, "constructor") || startsWith(memberName, "toString") || startsWith(memberName, "_"));
|
24 | const getNodeName = node => 11 === node.nodeType && node.host ? "#s" : node.nodeName;
|
25 | const randomId = () => Math.round(Math.random() * Number.MAX_SAFE_INTEGER).toString(36);
|
26 | const defineConstructorName = (Cstr, value) => ((obj, memberName, descriptor) => Object.defineProperty(obj, memberName, {
|
27 | ...descriptor,
|
28 | configurable: true
|
29 | }))(Cstr, "name", {
|
30 | value: value
|
31 | });
|
32 | const htmlConstructorTags = {
|
33 | Anchor: "a",
|
34 | DList: "dl",
|
35 | Image: "img",
|
36 | OList: "ol",
|
37 | Paragraph: "p",
|
38 | Quote: "q",
|
39 | TableCaption: "caption",
|
40 | TableCell: "td",
|
41 | TableCol: "colgroup",
|
42 | TableRow: "tr",
|
43 | TableSection: "tbody",
|
44 | UList: "ul"
|
45 | };
|
46 | const svgConstructorTags = {
|
47 | Graphics: "g",
|
48 | SVG: "svg"
|
49 | };
|
50 | const defaultPartytownForwardPropertySettings = {
|
51 | preserveBehavior: false
|
52 | };
|
53 | const arrayMethods = Object.freeze((obj => {
|
54 | const properties = new Set;
|
55 | let currentObj = obj;
|
56 | do {
|
57 | Object.getOwnPropertyNames(currentObj).forEach((item => {
|
58 | "function" == typeof currentObj[item] && properties.add(item);
|
59 | }));
|
60 | } while ((currentObj = Object.getPrototypeOf(currentObj)) !== Object.prototype);
|
61 | return Array.from(properties);
|
62 | })([]));
|
63 | const InstanceIdKey = Symbol();
|
64 | const CreatedKey = Symbol();
|
65 | const instances = new Map;
|
66 | const mainRefs = new Map;
|
67 | const winCtxs = {};
|
68 | const windowIds = new WeakMap;
|
69 | const getAndSetInstanceId = (instance, instanceId) => {
|
70 | if (instance) {
|
71 | if (instanceId = windowIds.get(instance)) {
|
72 | return instanceId;
|
73 | }
|
74 | (instanceId = instance[InstanceIdKey]) || setInstanceId(instance, instanceId = randomId());
|
75 | return instanceId;
|
76 | }
|
77 | };
|
78 | const getInstance = (winId, instanceId, win, doc, docId) => {
|
79 | if ((win = winCtxs[winId]) && win.$window$) {
|
80 | if (winId === instanceId) {
|
81 | return win.$window$;
|
82 | }
|
83 | doc = win.$window$.document;
|
84 | docId = instanceId.split(".").pop();
|
85 | if ("d" === docId) {
|
86 | return doc;
|
87 | }
|
88 | if ("e" === docId) {
|
89 | return doc.documentElement;
|
90 | }
|
91 | if ("h" === docId) {
|
92 | return doc.head;
|
93 | }
|
94 | if ("b" === docId) {
|
95 | return doc.body;
|
96 | }
|
97 | }
|
98 | return instances.get(instanceId);
|
99 | };
|
100 | const setInstanceId = (instance, instanceId, now) => {
|
101 | if (instance) {
|
102 | instances.set(instanceId, instance);
|
103 | instance[InstanceIdKey] = instanceId;
|
104 | instance[CreatedKey] = now = Date.now();
|
105 | if (now > lastCleanup + 5e3) {
|
106 | instances.forEach(((storedInstance, instanceId) => {
|
107 | storedInstance[CreatedKey] < lastCleanup && storedInstance.nodeType && !storedInstance.isConnected && instances.delete(instanceId);
|
108 | }));
|
109 | lastCleanup = now;
|
110 | }
|
111 | }
|
112 | };
|
113 | let lastCleanup = 0;
|
114 | const mainWindow = window.parent;
|
115 | const docImpl = document.implementation.createHTMLDocument();
|
116 | const config = mainWindow.partytown || {};
|
117 | const libPath = (config.lib || "/~partytown/") + "debug/";
|
118 | const logMain = msg => {
|
119 | console.debug.apply(console, [ "%cMain 🌎", "background: #717171; color: white; padding: 2px 3px; border-radius: 2px; font-size: 0.8em;", msg ]);
|
120 | };
|
121 | const winIds = [];
|
122 | const normalizedWinId = winId => {
|
123 | winIds.includes(winId) || winIds.push(winId);
|
124 | return winIds.indexOf(winId) + 1;
|
125 | };
|
126 | const defineCustomElement = (winId, worker, ceData) => {
|
127 | const Cstr = defineConstructorName(class extends winCtxs[winId].$window$.HTMLElement {}, ceData[0]);
|
128 | const ceCallbackMethods = "connectedCallback,disconnectedCallback,attributeChangedCallback,adoptedCallback".split(",");
|
129 | ceCallbackMethods.map((callbackMethodName => Cstr.prototype[callbackMethodName] = function(...args) {
|
130 | worker.postMessage([ 15, winId, getAndSetInstanceId(this), callbackMethodName, args ]);
|
131 | }));
|
132 | Cstr.observedAttributes = ceData[1];
|
133 | return Cstr;
|
134 | };
|
135 | const serializeForWorker = ($winId$, value, added, type, cstrName, prevInstanceId) => void 0 !== value && (type = typeof value) ? "string" === type || "number" === type || "boolean" === type || null == value ? [ 0, value ] : "function" === type ? [ 6 ] : (added = added || new Set) && Array.isArray(value) ? added.has(value) ? [ 1, [] ] : added.add(value) && [ 1, value.map((v => serializeForWorker($winId$, v, added))) ] : "object" === type ? serializedValueIsError(value) ? [ 14, {
|
136 | name: value.name,
|
137 | message: value.message,
|
138 | stack: value.stack
|
139 | } ] : "" === (cstrName = getConstructorName(value)) ? [ 2, {} ] : "Window" === cstrName ? [ 3, [ $winId$, $winId$ ] ] : "HTMLCollection" === cstrName || "NodeList" === cstrName ? [ 7, Array.from(value).map((v => serializeForWorker($winId$, v, added)[1])) ] : cstrName.endsWith("Event") ? [ 5, serializeObjectForWorker($winId$, value, added) ] : "CSSRuleList" === cstrName ? [ 12, Array.from(value).map(serializeCssRuleForWorker) ] : startsWith(cstrName, "CSS") && cstrName.endsWith("Rule") ? [ 11, serializeCssRuleForWorker(value) ] : "CSSStyleDeclaration" === cstrName ? [ 13, serializeObjectForWorker($winId$, value, added) ] : "Attr" === cstrName ? [ 10, [ value.name, value.value ] ] : value.nodeType ? [ 3, [ $winId$, getAndSetInstanceId(value), getNodeName(value), prevInstanceId ] ] : [ 2, serializeObjectForWorker($winId$, value, added, true, true) ] : void 0 : value;
|
140 | const serializeObjectForWorker = (winId, obj, added, includeFunctions, includeEmptyStrings, serializedObj, propName, propValue) => {
|
141 | serializedObj = {};
|
142 | if (!added.has(obj)) {
|
143 | added.add(obj);
|
144 | for (propName in obj) {
|
145 | if (isValidMemberName(propName)) {
|
146 | propValue = "path" === propName && getConstructorName(obj).endsWith("Event") ? obj.composedPath() : obj[propName];
|
147 | (includeFunctions || "function" != typeof propValue) && (includeEmptyStrings || "" !== propValue) && (serializedObj[propName] = serializeForWorker(winId, propValue, added));
|
148 | }
|
149 | }
|
150 | }
|
151 | return serializedObj;
|
152 | };
|
153 | const serializeCssRuleForWorker = cssRule => {
|
154 | let obj = {};
|
155 | let key;
|
156 | for (key in cssRule) {
|
157 | validCssRuleProps.includes(key) && (obj[key] = String(cssRule[key]));
|
158 | }
|
159 | return obj;
|
160 | };
|
161 | let ErrorObject = null;
|
162 | const serializedValueIsError = value => {
|
163 | var _a;
|
164 | ErrorObject = (null === (_a = window.top) || void 0 === _a ? void 0 : _a.Error) || ErrorObject;
|
165 | return value instanceof ErrorObject;
|
166 | };
|
167 | const deserializeFromWorker = (worker, serializedTransfer, serializedType, serializedValue) => {
|
168 | if (serializedTransfer) {
|
169 | serializedType = serializedTransfer[0];
|
170 | serializedValue = serializedTransfer[1];
|
171 | return 0 === serializedType ? serializedValue : 4 === serializedType ? deserializeRefFromWorker(worker, serializedValue) : 1 === serializedType ? serializedValue.map((v => deserializeFromWorker(worker, v))) : 3 === serializedType ? getInstance(serializedValue[0], serializedValue[1]) : 5 === serializedType ? constructEvent(deserializeObjectFromWorker(worker, serializedValue)) : 2 === serializedType ? deserializeObjectFromWorker(worker, serializedValue) : 8 === serializedType ? serializedValue : 9 === serializedType ? new window[serializedTransfer[2]](serializedValue) : void 0;
|
172 | }
|
173 | };
|
174 | const deserializeRefFromWorker = (worker, {$winId$: $winId$, $instanceId$: $instanceId$, $refId$: $refId$}, ref) => {
|
175 | ref = mainRefs.get($refId$);
|
176 | if (!ref) {
|
177 | ref = function(...args) {
|
178 | worker.postMessage([ 9, {
|
179 | $winId$: $winId$,
|
180 | $instanceId$: $instanceId$,
|
181 | $refId$: $refId$,
|
182 | $thisArg$: serializeForWorker($winId$, this),
|
183 | $args$: serializeForWorker($winId$, args)
|
184 | } ]);
|
185 | };
|
186 | mainRefs.set($refId$, ref);
|
187 | }
|
188 | return ref;
|
189 | };
|
190 | const constructEvent = eventProps => new ("detail" in eventProps ? CustomEvent : Event)(eventProps.type, eventProps);
|
191 | const deserializeObjectFromWorker = (worker, serializedValue, obj, key) => {
|
192 | obj = {};
|
193 | for (key in serializedValue) {
|
194 | obj[key] = deserializeFromWorker(worker, serializedValue[key]);
|
195 | }
|
196 | return obj;
|
197 | };
|
198 | const validCssRuleProps = "cssText,selectorText,href,media,namespaceURI,prefix,name,conditionText".split(",");
|
199 | const mainAccessHandler = async (worker, accessReq) => {
|
200 | let accessRsp = {
|
201 | $msgId$: accessReq.$msgId$
|
202 | };
|
203 | let totalTasks = len(accessReq.$tasks$);
|
204 | let i = 0;
|
205 | let task;
|
206 | let winId;
|
207 | let applyPath;
|
208 | let instance;
|
209 | let rtnValue;
|
210 | let isLast;
|
211 | for (;i < totalTasks; i++) {
|
212 | try {
|
213 | isLast = i === totalTasks - 1;
|
214 | task = accessReq.$tasks$[i];
|
215 | winId = task.$winId$;
|
216 | applyPath = task.$applyPath$;
|
217 | !winCtxs[winId] && winId.startsWith("f_") && await new Promise((resolve => {
|
218 | let check = 0;
|
219 | let callback = () => {
|
220 | winCtxs[winId] || check++ > 1e3 ? resolve() : requestAnimationFrame(callback);
|
221 | };
|
222 | callback();
|
223 | }));
|
224 | if (1 === applyPath[0] && applyPath[1] in winCtxs[winId].$window$) {
|
225 | setInstanceId(new winCtxs[winId].$window$[applyPath[1]](...deserializeFromWorker(worker, applyPath[2])), task.$instanceId$);
|
226 | } else {
|
227 | instance = getInstance(winId, task.$instanceId$);
|
228 | if (instance) {
|
229 | rtnValue = applyToInstance(worker, winId, instance, applyPath, isLast, task.$groupedGetters$);
|
230 | task.$assignInstanceId$ && ("string" == typeof task.$assignInstanceId$ ? setInstanceId(rtnValue, task.$assignInstanceId$) : winCtxs[task.$assignInstanceId$.$winId$] = {
|
231 | $winId$: task.$assignInstanceId$.$winId$,
|
232 | $window$: {
|
233 | document: rtnValue
|
234 | }
|
235 | });
|
236 | if (isPromise(rtnValue)) {
|
237 | rtnValue = await rtnValue;
|
238 | isLast && (accessRsp.$isPromise$ = true);
|
239 | }
|
240 | isLast && (accessRsp.$rtnValue$ = serializeForWorker(winId, rtnValue, void 0, void 0, void 0, task.$instanceId$));
|
241 | } else {
|
242 | accessRsp.$error$ = `Error finding instance "${task.$instanceId$}" on window ${normalizedWinId(winId)}`;
|
243 | console.error(accessRsp.$error$, task);
|
244 | }
|
245 | }
|
246 | } catch (e) {
|
247 | isLast ? accessRsp.$error$ = String(e.stack || e) : console.error(e);
|
248 | }
|
249 | }
|
250 | return accessRsp;
|
251 | };
|
252 | const applyToInstance = (worker, winId, instance, applyPath, isLast, groupedGetters) => {
|
253 | let i = 0;
|
254 | let l = len(applyPath);
|
255 | let next;
|
256 | let current;
|
257 | let previous;
|
258 | let args;
|
259 | let groupedRtnValues;
|
260 | for (;i < l; i++) {
|
261 | current = applyPath[i];
|
262 | next = applyPath[i + 1];
|
263 | previous = applyPath[i - 1];
|
264 | try {
|
265 | if (!Array.isArray(next)) {
|
266 | if ("string" == typeof current || "number" == typeof current) {
|
267 | if (i + 1 === l && groupedGetters) {
|
268 | groupedRtnValues = {};
|
269 | groupedGetters.map((propName => groupedRtnValues[propName] = instance[propName]));
|
270 | return groupedRtnValues;
|
271 | }
|
272 | instance = instance[current];
|
273 | } else {
|
274 | if (0 === next) {
|
275 | instance[previous] = deserializeFromWorker(worker, current);
|
276 | return;
|
277 | }
|
278 | if ("function" == typeof instance[previous]) {
|
279 | args = deserializeFromWorker(worker, current);
|
280 | "define" === previous && "CustomElementRegistry" === getConstructorName(instance) && (args[1] = defineCustomElement(winId, worker, args[1]));
|
281 | "insertRule" === previous && args[1] > len(instance.cssRules) && (args[1] = len(instance.cssRules));
|
282 | instance = instance[previous].apply(instance, args);
|
283 | if ("play" === previous) {
|
284 | return Promise.resolve();
|
285 | }
|
286 | }
|
287 | }
|
288 | }
|
289 | } catch (err) {
|
290 | if (isLast) {
|
291 | throw err;
|
292 | }
|
293 | console.debug("Non-blocking setter error:", err);
|
294 | }
|
295 | }
|
296 | return instance;
|
297 | };
|
298 | const mainForwardTrigger = (worker, $winId$, win) => {
|
299 | let queuedForwardCalls = win._ptf;
|
300 | let forwards = (win.partytown || {}).forward || [];
|
301 | let i;
|
302 | let mainForwardFn;
|
303 | let forwardCall = ($forward$, args) => worker.postMessage([ 10, {
|
304 | $winId$: $winId$,
|
305 | $forward$: $forward$,
|
306 | $args$: serializeForWorker($winId$, Array.from(args))
|
307 | } ]);
|
308 | win._ptf = void 0;
|
309 | forwards.map((forwardProps => {
|
310 | const [property, {preserveBehavior: preserveBehavior}] = (propertyOrPropertyWithSettings => {
|
311 | if ("string" == typeof propertyOrPropertyWithSettings) {
|
312 | return [ propertyOrPropertyWithSettings, defaultPartytownForwardPropertySettings ];
|
313 | }
|
314 | const [property, settings = defaultPartytownForwardPropertySettings] = propertyOrPropertyWithSettings;
|
315 | return [ property, {
|
316 | ...defaultPartytownForwardPropertySettings,
|
317 | ...settings
|
318 | } ];
|
319 | })(forwardProps);
|
320 | mainForwardFn = win;
|
321 | property.split(".").map(((_, i, arr) => {
|
322 | mainForwardFn = mainForwardFn[arr[i]] = i + 1 < len(arr) ? mainForwardFn[arr[i]] || (propertyName => arrayMethods.includes(propertyName) ? [] : {})(arr[i + 1]) : (() => {
|
323 | let originalFunction = null;
|
324 | if (preserveBehavior) {
|
325 | const {methodOrProperty: methodOrProperty, thisObject: thisObject} = ((window, properties) => {
|
326 | let thisObject = window;
|
327 | for (let i = 0; i < properties.length - 1; i += 1) {
|
328 | thisObject = thisObject[properties[i]];
|
329 | }
|
330 | return {
|
331 | thisObject: thisObject,
|
332 | methodOrProperty: properties.length > 0 ? thisObject[properties[properties.length - 1]] : void 0
|
333 | };
|
334 | })(win, arr);
|
335 | "function" == typeof methodOrProperty && (originalFunction = (...args) => methodOrProperty.apply(thisObject, ...args));
|
336 | }
|
337 | return (...args) => {
|
338 | let returnValue;
|
339 | originalFunction && (returnValue = originalFunction(args));
|
340 | forwardCall(arr, args);
|
341 | return returnValue;
|
342 | };
|
343 | })();
|
344 | }));
|
345 | }));
|
346 | if (queuedForwardCalls) {
|
347 | for (i = 0; i < len(queuedForwardCalls); i += 2) {
|
348 | forwardCall(queuedForwardCalls[i], queuedForwardCalls[i + 1]);
|
349 | }
|
350 | }
|
351 | };
|
352 | const readNextScript = (worker, winCtx) => {
|
353 | let $winId$ = winCtx.$winId$;
|
354 | let win = winCtx.$window$;
|
355 | let doc = win.document;
|
356 | let scriptSelector = 'script[type="text/partytown"]:not([data-ptid]):not([data-pterror])';
|
357 | let blockingScriptSelector = scriptSelector + ":not([async]):not([defer])";
|
358 | let scriptElm;
|
359 | let $instanceId$;
|
360 | let scriptData;
|
361 | if (doc && doc.body) {
|
362 | scriptElm = doc.querySelector(blockingScriptSelector);
|
363 | scriptElm || (scriptElm = doc.querySelector(scriptSelector));
|
364 | if (scriptElm) {
|
365 | scriptElm.dataset.ptid = $instanceId$ = getAndSetInstanceId(scriptElm, $winId$);
|
366 | scriptData = {
|
367 | $winId$: $winId$,
|
368 | $instanceId$: $instanceId$
|
369 | };
|
370 | if (scriptElm.src) {
|
371 | scriptData.$url$ = scriptElm.src;
|
372 | scriptData.$orgUrl$ = scriptElm.dataset.ptsrc || scriptElm.src;
|
373 | } else {
|
374 | scriptData.$content$ = scriptElm.innerHTML;
|
375 | }
|
376 | worker.postMessage([ 7, scriptData ]);
|
377 | } else {
|
378 | if (!winCtx.$isInitialized$) {
|
379 | winCtx.$isInitialized$ = 1;
|
380 | mainForwardTrigger(worker, $winId$, win);
|
381 | doc.dispatchEvent(new CustomEvent("pt0"));
|
382 | {
|
383 | const winType = win === win.top ? "top" : "iframe";
|
384 | logMain(`Executed ${winType} window ${normalizedWinId($winId$)} environment scripts in ${(performance.now() - winCtx.$startTime$).toFixed(1)}ms`);
|
385 | }
|
386 | }
|
387 | worker.postMessage([ 8, $winId$ ]);
|
388 | }
|
389 | } else {
|
390 | requestAnimationFrame((() => readNextScript(worker, winCtx)));
|
391 | }
|
392 | };
|
393 | const registerWindow = (worker, $winId$, $window$) => {
|
394 | if (!windowIds.has($window$)) {
|
395 | windowIds.set($window$, $winId$);
|
396 | const doc = $window$.document;
|
397 | const history = $window$.history;
|
398 | const $parentWinId$ = windowIds.get($window$.parent);
|
399 | let initialised = false;
|
400 | const onInitialisedQueue = [];
|
401 | const onInitialised = callback => {
|
402 | initialised ? callback() : onInitialisedQueue.push(callback);
|
403 | };
|
404 | const sendInitEnvData = () => {
|
405 | worker.postMessage([ 5, {
|
406 | $winId$: $winId$,
|
407 | $parentWinId$: $parentWinId$,
|
408 | $url$: doc.baseURI,
|
409 | $visibilityState$: doc.visibilityState
|
410 | } ]);
|
411 | setTimeout((() => {
|
412 | initialised = true;
|
413 | onInitialisedQueue.forEach((callback => {
|
414 | callback();
|
415 | }));
|
416 | }));
|
417 | };
|
418 | const pushState = history.pushState.bind(history);
|
419 | const replaceState = history.replaceState.bind(history);
|
420 | const onLocationChange = (type, state, newUrl, oldUrl) => () => {
|
421 | worker.postMessage([ 13, {
|
422 | $winId$: $winId$,
|
423 | type: type,
|
424 | state: state,
|
425 | url: doc.baseURI,
|
426 | newUrl: newUrl,
|
427 | oldUrl: oldUrl
|
428 | } ]);
|
429 | };
|
430 | history.pushState = (state, _, newUrl) => {
|
431 | pushState(state, _, newUrl);
|
432 | onInitialised(onLocationChange(0, state, null == newUrl ? void 0 : newUrl.toString()));
|
433 | };
|
434 | history.replaceState = (state, _, newUrl) => {
|
435 | replaceState(state, _, newUrl);
|
436 | onInitialised(onLocationChange(1, state, null == newUrl ? void 0 : newUrl.toString()));
|
437 | };
|
438 | $window$.addEventListener("popstate", (event => {
|
439 | onInitialised(onLocationChange(2, event.state));
|
440 | }));
|
441 | $window$.addEventListener("hashchange", (event => {
|
442 | onInitialised(onLocationChange(3, {}, event.newURL, event.oldURL));
|
443 | }));
|
444 | $window$.addEventListener("ptupdate", (() => {
|
445 | readNextScript(worker, winCtxs[$winId$]);
|
446 | }));
|
447 | doc.addEventListener("visibilitychange", (() => worker.postMessage([ 14, $winId$, doc.visibilityState ])));
|
448 | winCtxs[$winId$] = {
|
449 | $winId$: $winId$,
|
450 | $window$: $window$
|
451 | };
|
452 | winCtxs[$winId$].$startTime$ = performance.now();
|
453 | {
|
454 | const winType = $winId$ === $parentWinId$ ? "top" : "iframe";
|
455 | logMain(`Registered ${winType} window ${normalizedWinId($winId$)}`);
|
456 | }
|
457 | "complete" === doc.readyState ? sendInitEnvData() : $window$.addEventListener("load", sendInitEnvData);
|
458 | }
|
459 | };
|
460 | const onMessageFromWebWorker = (worker, msg, winCtx) => {
|
461 | if (4 === msg[0]) {
|
462 | registerWindow(worker, randomId(), mainWindow);
|
463 | } else {
|
464 | winCtx = winCtxs[msg[1]];
|
465 | winCtx && (7 === msg[0] ? requestAnimationFrame((() => readNextScript(worker, winCtx))) : 6 === msg[0] && ((worker, winCtx, instanceId, errorMsg, scriptElm) => {
|
466 | scriptElm = winCtx.$window$.document.querySelector(`[data-ptid="${instanceId}"]`);
|
467 | if (scriptElm) {
|
468 | errorMsg ? scriptElm.dataset.pterror = errorMsg : scriptElm.type += "-x";
|
469 | delete scriptElm.dataset.ptid;
|
470 | }
|
471 | readNextScript(worker, winCtx);
|
472 | })(worker, winCtx, msg[2], msg[3]));
|
473 | }
|
474 | };
|
475 | const readMainPlatform = () => {
|
476 | const elm = docImpl.createElement("i");
|
477 | const textNode = docImpl.createTextNode("");
|
478 | const comment = docImpl.createComment("");
|
479 | const frag = docImpl.createDocumentFragment();
|
480 | const shadowRoot = docImpl.createElement("p").attachShadow({
|
481 | mode: "open"
|
482 | });
|
483 | const intersectionObserver = getGlobalConstructor(mainWindow, "IntersectionObserver");
|
484 | const mutationObserver = getGlobalConstructor(mainWindow, "MutationObserver");
|
485 | const resizeObserver = getGlobalConstructor(mainWindow, "ResizeObserver");
|
486 | const perf = mainWindow.performance;
|
487 | const screen = mainWindow.screen;
|
488 | const impls = [ [ mainWindow.history ], [ perf ], [ perf.navigation ], [ perf.timing ], [ screen ], [ screen.orientation ], [ mainWindow.visualViewport ], [ intersectionObserver, 12 ], [ mutationObserver, 12 ], [ resizeObserver, 12 ], [ textNode ], [ comment ], [ frag ], [ shadowRoot ], [ elm ], [ elm.attributes ], [ elm.classList ], [ elm.dataset ], [ elm.style ], [ docImpl ], [ docImpl.doctype ] ];
|
489 | const initialInterfaces = [ readImplementation("Window", mainWindow), readImplementation("Node", textNode) ];
|
490 | const $config$ = function(config) {
|
491 | return JSON.stringify(config, ((key, value) => {
|
492 | if ("function" == typeof value) {
|
493 | value = String(value);
|
494 | value.startsWith(key + "(") && (value = "function " + value);
|
495 | }
|
496 | "loadScriptsOnMainThread" === key && (value = value.map((scriptUrl => Array.isArray(scriptUrl) ? scriptUrl : [ "string" == typeof scriptUrl ? "string" : "regexp", "string" == typeof scriptUrl ? scriptUrl : scriptUrl.source ])));
|
497 | return value;
|
498 | }));
|
499 | }(config);
|
500 | const initWebWorkerData = {
|
501 | $config$: $config$,
|
502 | $interfaces$: readImplementations(impls, initialInterfaces),
|
503 | $libPath$: new URL(libPath, mainWindow.location) + "",
|
504 | $origin$: origin,
|
505 | $tabId$: mainWindow._pttab
|
506 | };
|
507 | addGlobalConstructorUsingPrototype(initWebWorkerData.$interfaces$, mainWindow, "IntersectionObserverEntry");
|
508 | return initWebWorkerData;
|
509 | };
|
510 | const readMainInterfaces = () => {
|
511 | const elms = Object.getOwnPropertyNames(mainWindow).map((interfaceName => ((doc, interfaceName, r, tag) => {
|
512 | r = interfaceName.match(/^(HTML|SVG)(.+)Element$/);
|
513 | if (r) {
|
514 | tag = r[2];
|
515 | return "S" == interfaceName[0] ? doc.createElementNS("http://www.w3.org/2000/svg", svgConstructorTags[tag] || tag.slice(0, 2).toLowerCase() + tag.slice(2)) : doc.createElement(htmlConstructorTags[tag] || tag);
|
516 | }
|
517 | })(docImpl, interfaceName))).filter((elm => elm)).map((elm => [ elm ]));
|
518 | return readImplementations(elms, []);
|
519 | };
|
520 | const readImplementations = (impls, interfaces) => {
|
521 | const cstrs = new Set([ "Object" ]);
|
522 | const cstrImpls = impls.filter((implData => implData[0])).map((implData => {
|
523 | const impl = implData[0];
|
524 | const interfaceType = implData[1];
|
525 | const cstrName = getConstructorName(impl);
|
526 | const CstrPrototype = mainWindow[cstrName].prototype;
|
527 | return [ cstrName, CstrPrototype, impl, interfaceType ];
|
528 | }));
|
529 | cstrImpls.map((([cstrName, CstrPrototype, impl, intefaceType]) => readOwnImplementation(cstrs, interfaces, cstrName, CstrPrototype, impl, intefaceType)));
|
530 | return interfaces;
|
531 | };
|
532 | const readImplementation = (cstrName, impl, memberName) => {
|
533 | let interfaceMembers = [];
|
534 | let interfaceInfo = [ cstrName, "Object", interfaceMembers ];
|
535 | for (memberName in impl) {
|
536 | readImplementationMember(interfaceMembers, impl, memberName);
|
537 | }
|
538 | return interfaceInfo;
|
539 | };
|
540 | const readOwnImplementation = (cstrs, interfaces, cstrName, CstrPrototype, impl, interfaceType) => {
|
541 | if (!cstrs.has(cstrName)) {
|
542 | cstrs.add(cstrName);
|
543 | const SuperCstr = Object.getPrototypeOf(CstrPrototype);
|
544 | const superCstrName = getConstructorName(SuperCstr);
|
545 | const interfaceMembers = [];
|
546 | const propDescriptors = Object.getOwnPropertyDescriptors(CstrPrototype);
|
547 | readOwnImplementation(cstrs, interfaces, superCstrName, SuperCstr, impl, interfaceType);
|
548 | for (const memberName in propDescriptors) {
|
549 | readImplementationMember(interfaceMembers, impl, memberName);
|
550 | }
|
551 | interfaces.push([ cstrName, superCstrName, interfaceMembers, interfaceType, getNodeName(impl) ]);
|
552 | }
|
553 | };
|
554 | const readImplementationMember = (interfaceMembers, implementation, memberName, value, memberType, cstrName) => {
|
555 | try {
|
556 | if (isValidMemberName(memberName) && isNaN(memberName[0]) && "all" !== memberName) {
|
557 | value = implementation[memberName];
|
558 | memberType = typeof value;
|
559 | if ("function" === memberType) {
|
560 | (String(value).includes("[native") || Object.getPrototypeOf(implementation)[memberName]) && interfaceMembers.push([ memberName, 5 ]);
|
561 | } else if ("object" === memberType && null != value) {
|
562 | cstrName = getConstructorName(value);
|
563 | "Object" !== cstrName && "Function" !== cstrName && self[cstrName] && interfaceMembers.push([ memberName, value.nodeType || cstrName ]);
|
564 | } else {
|
565 | "symbol" !== memberType && (memberName.toUpperCase() === memberName ? interfaceMembers.push([ memberName, 6, value ]) : interfaceMembers.push([ memberName, 6 ]));
|
566 | }
|
567 | }
|
568 | } catch (e) {
|
569 | console.warn(e);
|
570 | }
|
571 | };
|
572 | const getGlobalConstructor = (mainWindow, cstrName) => void 0 !== mainWindow[cstrName] ? new mainWindow[cstrName](noop) : 0;
|
573 | const addGlobalConstructorUsingPrototype = ($interfaces$, mainWindow, cstrName) => {
|
574 | void 0 !== mainWindow[cstrName] && $interfaces$.push([ cstrName, "Object", Object.keys(mainWindow[cstrName].prototype).map((propName => [ propName, 6 ])), 12 ]);
|
575 | };
|
576 | let worker;
|
577 | (async receiveMessage => {
|
578 | const sharedDataBuffer = new SharedArrayBuffer(1073741824);
|
579 | const sharedData = new Int32Array(sharedDataBuffer);
|
580 | return (worker, msg) => {
|
581 | const msgType = msg[0];
|
582 | const accessReq = msg[1];
|
583 | if (0 === msgType) {
|
584 | const initData = readMainPlatform();
|
585 | initData.$sharedDataBuffer$ = sharedDataBuffer;
|
586 | worker.postMessage([ 1, initData ]);
|
587 | } else {
|
588 | 2 === msg[0] ? worker.postMessage([ 3, readMainInterfaces() ]) : 11 === msgType ? receiveMessage(accessReq, (accessRsp => {
|
589 | const stringifiedData = JSON.stringify(accessRsp);
|
590 | const stringifiedDataLength = stringifiedData.length;
|
591 | for (let i = 0; i < stringifiedDataLength; i++) {
|
592 | sharedData[i + 1] = stringifiedData.charCodeAt(i);
|
593 | }
|
594 | sharedData[0] = stringifiedDataLength;
|
595 | Atomics.notify(sharedData, 0);
|
596 | })) : onMessageFromWebWorker(worker, msg);
|
597 | }
|
598 | };
|
599 | })(((accessReq, responseCallback) => mainAccessHandler(worker, accessReq).then(responseCallback))).then((onMessageHandler => {
|
600 | if (onMessageHandler) {
|
601 | worker = new Worker(libPath + "partytown-ww-atomics.js?v=0.10.2", {
|
602 | name: "Partytown 🎉"
|
603 | });
|
604 | worker.onmessage = ev => {
|
605 | const msg = ev.data;
|
606 | 12 === msg[0] ? mainAccessHandler(worker, msg[1]) : onMessageHandler(worker, msg);
|
607 | };
|
608 | logMain("Created Partytown web worker (0.10.2)");
|
609 | worker.onerror = ev => console.error("Web Worker Error", ev);
|
610 | mainWindow.addEventListener("pt1", (ev => registerWindow(worker, getAndSetInstanceId(ev.detail.frameElement), ev.detail)));
|
611 | }
|
612 | }));
|
613 | })(window);
|