From 1aaca2cc3696a00b1d183d4216d12fedd41b500c Mon Sep 17 00:00:00 2001 From: Jeremy Leibs Date: Wed, 11 Dec 2024 18:17:16 -0500 Subject: [PATCH 1/2] Compress the encoded wasm payload in the dataurl for jupyter notebooks --- rerun_js/web-viewer/bundle.mjs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/rerun_js/web-viewer/bundle.mjs b/rerun_js/web-viewer/bundle.mjs index c93feaf232b1..2c8347fee650 100644 --- a/rerun_js/web-viewer/bundle.mjs +++ b/rerun_js/web-viewer/bundle.mjs @@ -6,12 +6,13 @@ import { fileURLToPath } from "node:url"; import * as path from "node:path"; import * as fs from "node:fs"; +import * as zlib from "node:zlib"; import * as util from "node:util"; const __filename = path.resolve(fileURLToPath(import.meta.url)); const __dirname = path.dirname(__filename); -const wasm = fs.readFileSync(path.join(__dirname, "re_viewer_bg.wasm")); +const wasm = zlib.gzipSync(fs.readFileSync(path.join(__dirname, "re_viewer_bg.wasm"))); const js = fs.readFileSync(path.join(__dirname, "re_viewer.js"), "utf-8"); const index = fs.readFileSync(path.join(__dirname, "index.js"), "utf-8"); @@ -19,12 +20,17 @@ const INLINE_MARKER = "/**/"; /** @param {Buffer} buffer */ function buffer_to_data_url(buffer) { - return `data:application/wasm;base64,${buffer.toString("base64")}`; + return `data:application/octet-stream;gzip;base64,${buffer.toString("base64")}`; } -async function data_url_to_buffer(dataUrl) { - const response = await fetch(dataUrl); - return response.arrayBuffer(); +async function compressed_data_url_to_buffer(dataUrl) { + const response = await fetch(dataUrl); + const blob = await response.blob(); + + let ds = new DecompressionStream("gzip"); + let decompressedStream = blob.stream().pipeThrough(ds); + + return await new Response(decompressedStream).arrayBuffer(); } const inlined_js = js.replace("export default function", "return function"); @@ -35,9 +41,9 @@ async function fetch_viewer_js() { } async function fetch_viewer_wasm() { - ${data_url_to_buffer.toString()} + ${compressed_data_url_to_buffer.toString()} const dataUrl = ${JSON.stringify(buffer_to_data_url(wasm))}; - const buffer = await data_url_to_buffer(dataUrl); + const buffer = await compressed_data_url_to_buffer(dataUrl); return new Response(buffer, { "headers": { "Content-Type": "application/wasm" } }); } `; From ea53e9823284d63a574477e5104674896ebc531f Mon Sep 17 00:00:00 2001 From: Jeremy Leibs Date: Thu, 12 Dec 2024 10:35:31 -0500 Subject: [PATCH 2/2] Remove unnecessary await --- rerun_js/web-viewer/bundle.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rerun_js/web-viewer/bundle.mjs b/rerun_js/web-viewer/bundle.mjs index 2c8347fee650..ac79ec9611f8 100644 --- a/rerun_js/web-viewer/bundle.mjs +++ b/rerun_js/web-viewer/bundle.mjs @@ -30,7 +30,7 @@ async function compressed_data_url_to_buffer(dataUrl) { let ds = new DecompressionStream("gzip"); let decompressedStream = blob.stream().pipeThrough(ds); - return await new Response(decompressedStream).arrayBuffer(); + return new Response(decompressedStream).arrayBuffer(); } const inlined_js = js.replace("export default function", "return function");