forked from QwikDev/partytown
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8847b49
commit 9a72c77
Showing
11 changed files
with
5,661 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* Partytown 0.10.2-dev1727590485751 - MIT builder.io */ | ||
Object.freeze((obj => { | ||
const properties = new Set; | ||
let currentObj = obj; | ||
do { | ||
Object.getOwnPropertyNames(currentObj).forEach((item => { | ||
"function" == typeof currentObj[item] && properties.add(item); | ||
})); | ||
} while ((currentObj = Object.getPrototypeOf(currentObj)) !== Object.prototype); | ||
return Array.from(properties); | ||
})([])); | ||
|
||
const resolves = new Map; | ||
|
||
const swMessageError = (accessReq, $error$) => ({ | ||
$msgId$: accessReq.$msgId$, | ||
$error$: $error$ | ||
}); | ||
|
||
const httpRequestFromWebWorker = req => new Promise((async resolve => { | ||
const accessReq = await req.clone().json(); | ||
const responseData = await (accessReq => new Promise((async resolve => { | ||
const clients = await self.clients.matchAll(); | ||
const client = ((clients, msgId) => { | ||
const tabId = msgId.split(".").pop(); | ||
let client = clients.find((a => a.url.endsWith(`?${tabId}`))); | ||
client || (client = [ ...clients ].sort(((a, b) => a.url > b.url ? -1 : a.url < b.url ? 1 : 0))[0]); | ||
return client; | ||
})([ ...clients ], accessReq.$msgId$); | ||
if (client) { | ||
const timeout = 12e4; | ||
const msgResolve = [ resolve, setTimeout((() => { | ||
resolves.delete(accessReq.$msgId$); | ||
resolve(swMessageError(accessReq, "Timeout")); | ||
}), timeout) ]; | ||
resolves.set(accessReq.$msgId$, msgResolve); | ||
client.postMessage(accessReq); | ||
} else { | ||
resolve(swMessageError(accessReq, "NoParty")); | ||
} | ||
})))(accessReq); | ||
resolve(response(JSON.stringify(responseData), "application/json")); | ||
})); | ||
|
||
const response = (body, contentType) => new Response(body, { | ||
headers: { | ||
"content-type": contentType || "text/html", | ||
"Cache-Control": "no-store" | ||
} | ||
}); | ||
|
||
self.oninstall = () => self.skipWaiting(); | ||
|
||
self.onactivate = () => self.clients.claim(); | ||
|
||
self.onmessage = ev => { | ||
const accessRsp = ev.data; | ||
const r = resolves.get(accessRsp.$msgId$); | ||
if (r) { | ||
resolves.delete(accessRsp.$msgId$); | ||
clearTimeout(r[1]); | ||
r[0](accessRsp); | ||
} | ||
}; | ||
|
||
self.onfetch = ev => { | ||
const req = ev.request; | ||
const url = new URL(req.url); | ||
const pathname = url.pathname; | ||
if (pathname.endsWith("sw.html")) { | ||
ev.respondWith(response('<!DOCTYPE html><html><head><meta charset="utf-8"><script src="./partytown-sandbox-sw.js?v=0.10.2-dev1727590485751"><\/script></head></html>')); | ||
} else { | ||
pathname.endsWith("proxytown") && ev.respondWith(httpRequestFromWebWorker(req)); | ||
} | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* Partytown 0.10.2-dev1727590485751 - MIT builder.io */ | ||
const defaultPartytownForwardPropertySettings = { | ||
preserveBehavior: false | ||
}; | ||
|
||
const resolvePartytownForwardProperty = propertyOrPropertyWithSettings => { | ||
if ("string" == typeof propertyOrPropertyWithSettings) { | ||
return [ propertyOrPropertyWithSettings, defaultPartytownForwardPropertySettings ]; | ||
} | ||
const [property, settings = defaultPartytownForwardPropertySettings] = propertyOrPropertyWithSettings; | ||
return [ property, { | ||
...defaultPartytownForwardPropertySettings, | ||
...settings | ||
} ]; | ||
}; | ||
|
||
const arrayMethods = Object.freeze((obj => { | ||
const properties = new Set; | ||
let currentObj = obj; | ||
do { | ||
Object.getOwnPropertyNames(currentObj).forEach((item => { | ||
"function" == typeof currentObj[item] && properties.add(item); | ||
})); | ||
} while ((currentObj = Object.getPrototypeOf(currentObj)) !== Object.prototype); | ||
return Array.from(properties); | ||
})([])); | ||
|
||
!function(win, doc, nav, top, useAtomics, config, libPath, timeout, scripts, sandbox, mainForwardFn = win, isReady) { | ||
function ready() { | ||
if (!isReady) { | ||
isReady = 1; | ||
libPath = (config.lib || "/~partytown/") + (false !== config.debug ? "debug/" : ""); | ||
if ("/" == libPath[0]) { | ||
scripts = doc.querySelectorAll('script[type="text/partytown"]'); | ||
if (top != win) { | ||
top.dispatchEvent(new CustomEvent("pt1", { | ||
detail: win | ||
})); | ||
} else { | ||
timeout = setTimeout(fallback, 999999999); | ||
doc.addEventListener("pt0", clearFallback); | ||
useAtomics ? loadSandbox(1) : nav.serviceWorker ? nav.serviceWorker.register(libPath + (config.swPath || "partytown-sw.js"), { | ||
scope: libPath | ||
}).then((function(swRegistration) { | ||
if (swRegistration.active) { | ||
loadSandbox(); | ||
} else if (swRegistration.installing) { | ||
swRegistration.installing.addEventListener("statechange", (function(ev) { | ||
"activated" == ev.target.state && loadSandbox(); | ||
})); | ||
} else { | ||
console.warn(swRegistration); | ||
} | ||
}), console.error) : fallback(); | ||
} | ||
} else { | ||
console.warn('Partytown config.lib url must start with "/"'); | ||
} | ||
} | ||
} | ||
function loadSandbox(isAtomics) { | ||
sandbox = doc.createElement(isAtomics ? "script" : "iframe"); | ||
win._pttab = Date.now(); | ||
if (!isAtomics) { | ||
sandbox.style.display = "block"; | ||
sandbox.style.width = "0"; | ||
sandbox.style.height = "0"; | ||
sandbox.style.border = "0"; | ||
sandbox.style.visibility = "hidden"; | ||
sandbox.setAttribute("aria-hidden", !0); | ||
} | ||
sandbox.src = libPath + "partytown-" + (isAtomics ? "atomics.js?v=0.10.2-dev1727590485751" : "sandbox-sw.html?" + win._pttab); | ||
doc.querySelector(config.sandboxParent || "body").appendChild(sandbox); | ||
} | ||
function fallback(i, script) { | ||
console.warn("Partytown script fallback"); | ||
clearFallback(); | ||
top == win && (config.forward || []).map((function(forwardProps) { | ||
const [property] = resolvePartytownForwardProperty(forwardProps); | ||
delete win[property.split(".")[0]]; | ||
})); | ||
for (i = 0; i < scripts.length; i++) { | ||
script = doc.createElement("script"); | ||
script.innerHTML = scripts[i].innerHTML; | ||
script.nonce = config.nonce; | ||
doc.head.appendChild(script); | ||
} | ||
sandbox && sandbox.parentNode.removeChild(sandbox); | ||
} | ||
function clearFallback() { | ||
clearTimeout(timeout); | ||
} | ||
config = win.partytown || {}; | ||
top == win && (config.forward || []).map((function(forwardProps) { | ||
const [property, {preserveBehavior: preserveBehavior}] = resolvePartytownForwardProperty(forwardProps); | ||
mainForwardFn = win; | ||
property.split(".").map((function(_, i, forwardPropsArr) { | ||
mainForwardFn = mainForwardFn[forwardPropsArr[i]] = i + 1 < forwardPropsArr.length ? mainForwardFn[forwardPropsArr[i]] || (propertyName => arrayMethods.includes(propertyName) ? [] : {})(forwardPropsArr[i + 1]) : (() => { | ||
let originalFunction = null; | ||
if (preserveBehavior) { | ||
const {methodOrProperty: methodOrProperty, thisObject: thisObject} = ((window, properties) => { | ||
let thisObject = window; | ||
for (let i = 0; i < properties.length - 1; i += 1) { | ||
thisObject = thisObject[properties[i]]; | ||
} | ||
return { | ||
thisObject: thisObject, | ||
methodOrProperty: properties.length > 0 ? thisObject[properties[properties.length - 1]] : void 0 | ||
}; | ||
})(win, forwardPropsArr); | ||
"function" == typeof methodOrProperty && (originalFunction = (...args) => methodOrProperty.apply(thisObject, ...args)); | ||
} | ||
return function() { | ||
let returnValue; | ||
originalFunction && (returnValue = originalFunction(arguments)); | ||
(win._ptf = win._ptf || []).push(forwardPropsArr, arguments); | ||
return returnValue; | ||
}; | ||
})(); | ||
})); | ||
})); | ||
if ("complete" == doc.readyState) { | ||
ready(); | ||
} else { | ||
win.addEventListener("DOMContentLoaded", ready); | ||
win.addEventListener("load", ready); | ||
} | ||
}(window, document, navigator, top, window.crossOriginIsolated); |
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.