Skip to content

Commit

Permalink
Fix broken notebook loading on firefox by compressing the encoded was…
Browse files Browse the repository at this point in the history
…m payload (#8426)

### Related
- Resolves: #8154

### What
It turns out firefox doesn't let you create a dataurl larger than 32MB.
Although our `.wasm` was under this threshold, the overhead of
base64-encoding pushed us over the threshold.

However, as we were encoding the raw, uncompressed .wasm, we still
actually have plenty of margin... we just need to jump through more
hoops to use it now. Using DecompressionStream like this seems to be
generally available across our target browsers so I think we should be
good?
  • Loading branch information
jleibs authored Dec 12, 2024
1 parent 931561e commit e1aea95
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions rerun_js/web-viewer/bundle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@
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");

const INLINE_MARKER = "/*<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 new Response(decompressedStream).arrayBuffer();
}

const inlined_js = js.replace("export default function", "return function");
Expand All @@ -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" } });
}
`;
Expand Down

0 comments on commit e1aea95

Please sign in to comment.