Skip to content

Truly independent web browser

2.4 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
    promiseTest(async () => {
        const server = httpTestServer();
        const tone = new Uint8Array(await (await fetch(server.getStaticURL("Assets/tone.mp3"))).arrayBuffer());
        const audioFrames = tone.subarray(226);
        const repetitions = Math.ceil((64 * 1024) / audioFrames.length);
        const media = new Uint8Array(audioFrames.length * repetitions);
        for (let i = 0; i < repetitions; i++)
            media.set(audioFrames, i * audioFrames.length);
        let binary = "";
        for (const byte of media)
            binary += String.fromCharCode(byte);
        const token = `gc-during-download-in-replaced-document-${crypto.randomUUID()}`;
        const mediaURL = new URL(await server.createEcho("GET", `/${token}.mp3`, {
            status: 200,
            headers: { "Content-Type": "audio/mpeg", "Content-Length": String(media.length), "Cache-Control": "no-store" },
            body: btoa(binary),
            body_encoding: "base64",
        }));
        mediaURL.search = `?chunks=16384&chunk_unblock_tokens=${token}`;
        const unblockURL = new URL(`/unblock/${token}`, mediaURL);
        unblockURL.hostname = uniqueLocalhostHostname("gc-during-download-in-replaced-document-unblock");

        const iframe = document.createElement("iframe");
        document.body.append(iframe);
        const navigate = html =>
            new Promise(resolve => {
                iframe.addEventListener("load", resolve, { once: true });
                iframe.srcdoc = html;
            });

        await navigate("<!DOCTYPE html>");
        const weakDocument = await (async () => {
            await navigate(`<!DOCTYPE html><audio preload="auto" src="${mediaURL.href}"></audio>`);
            const replacedDocument = iframe.contentDocument;
            const audio = replacedDocument.querySelector("audio");
            println(`networkState before navigating away: ${audio.networkState}`);
            replacedDocument.preserveWrapper = true;
            const weakDocument = new WeakRef(replacedDocument);
            await navigate("<!DOCTYPE html>");
            return weakDocument;
        })();

        await collectGarbageUntilCleared([weakDocument]);
        println(`replaced document collected: ${weakDocument.deref() === undefined}`);

        await fetch(unblockURL);
        iframe.remove();
    });
</script>