Skip to content

Commit

Permalink
Fix wasm-bindgen patch (#7970)
Browse files Browse the repository at this point in the history
### What

- Fixes the patch for `wasm-bindgen` output in `build-wasm.mjs`
- The names of some variables changed (e.g. `cachedUint8Memory0` became
`cachedUint8ArrayMemory0`)
- Added an auto-generated file which stores one hash per file from the
_unpatched_ output of `wasm-bindgen`
- If the hash changes, that means the output has changed and the build
will fail
- We currently build the JS package on every PR, so we'll find out then
and there
- Note that the viewer we upload to GCS is _not_ patched, only the JS
package (used in rerun.io/docs and notebooks) is patched.

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/7970?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/7970?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!
* [x] If have noted any breaking changes to the log API in
`CHANGELOG.md` and the migration guide

- [PR Build Summary](https://build.rerun.io/pr/7970)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
  • Loading branch information
jprochazk authored Nov 1, 2024
1 parent 5cf8451 commit 46ea1c0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 19 deletions.
91 changes: 73 additions & 18 deletions rerun_js/web-viewer/build-wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const exec = (cmd) => {
child_process.execSync(cmd, { cwd: __dirname, stdio: "inherit" });
};

function wasm(mode) {
function buildWebViewer(mode) {
switch (mode) {
case "debug": {
return exec(
Expand All @@ -31,13 +31,9 @@ function wasm(mode) {
}
}

child_process.execSync(
"cargo run -p re_dev_tools -- build-web-viewer --debug --target no-modules-base -o rerun_js/web-viewer",
{ cwd: __dirname, stdio: "inherit" },
);

function script() {
async function re_viewer_js(mode) {
let code = fs.readFileSync(path.join(__dirname, "re_viewer.js"), "utf-8");
await checkHash(mode, "re_viewer.js", code);

// this transforms the module, wrapping it in a default-exported function.
// calling the function produces a new "instance" of the module, because
Expand All @@ -64,11 +60,10 @@ ${code}
function deinit() {
__wbg_init.__wbindgen_wasm_module = null;
wasm = null;
cachedFloat32Memory0 = null;
cachedFloat64Memory0 = null;
cachedInt32Memory0 = null;
cachedUint32Memory0 = null;
cachedUint8Memory0 = null;
cachedFloat32ArrayMemory0 = null;
cachedInt32ArrayMemory0 = null;
cachedUint32ArrayMemory0 = null;
cachedUint8ArrayMemory0 = null;
}
return Object.assign(__wbg_init, { initSync, deinit }, __exports);
Expand All @@ -94,8 +89,9 @@ return Object.assign(__wbg_init, { initSync, deinit }, __exports);
fs.writeFileSync(path.join(__dirname, "re_viewer.js"), code);
}

function types() {
async function re_viewer_d_ts(mode) {
let code = fs.readFileSync(path.join(__dirname, "re_viewer.d.ts"), "utf-8");
await checkHash(mode, "re_viewer.d.ts", code);

// this transformation just re-exports WebHandle and adds a default export inside the `.d.ts` file

Expand All @@ -108,18 +104,77 @@ export default function(): wasm_bindgen;
fs.writeFileSync(path.join(__dirname, "re_viewer.d.ts"), code);
}

async function hash(data) {
const buffer = await crypto.subtle.digest("sha-256", data);
return Array.from(new Uint8Array(buffer))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}

async function checkHash(mode, id, data) {
const storedHash = hashes?.[mode]?.[id];
const computedHash = await hash(new TextEncoder().encode(data));

if (updateHashes) {
hashes[mode] ??= {};
hashes[mode][id] = computedHash;
return;
}

if (storedHash !== computedHash) {
console.error(`
==============================================================
Output of "${id}" changed.
Update the \`build-wasm.mjs\` script to handle the new output,
then run \`pixi run node build-wasm.mjs --update-hashes\`.
==============================================================
`);
process.exit(1);
}
}

async function run(mode) {
buildWebViewer(mode);
await re_viewer_js(mode);
await re_viewer_d_ts(mode);
}

const args = util.parseArgs({
options: {
mode: {
type: "string",
},
"update-hashes": {
type: "boolean",
},
},
});

if (!args.values.mode) {
throw new Error("Missing required argument: mode");
let updateHashes = !!args.values["update-hashes"];
let hashes;
try {
hashes = JSON.parse(
fs.readFileSync(path.join(__dirname, "hashes.json"), "utf-8"),
);
} catch (e) {
hashes = {};
}

wasm(args.values.mode);
script();
types();
try {
if (updateHashes) {
await run("release");
await run("debug");
fs.writeFileSync(
path.join(__dirname, "hashes.json"),
JSON.stringify(hashes),
);
} else {
if (!args.values.mode) {
throw new Error("Missing required argument: mode");
}

await run(args.values.mode);
}
} catch (e) {
console.error(e.message);
}
1 change: 1 addition & 0 deletions rerun_js/web-viewer/hashes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"release":{"re_viewer.js":"9548c50002f6c6844e50ceb154641a4ca547413a54eb3214b18909848776492f","re_viewer.d.ts":"d533906cbd59ac7b2f4a962a24daad55c5df854343414b8b6db08e2d956fdee3"},"debug":{"re_viewer.js":"74ec126e7deeb831c72c7a2ccffb98c3c63fb7268f27feffdfb2da286db65816","re_viewer.d.ts":"c642a7f1990d414d9953f0b215d8114418502b3b9f0240ca81b039766d5236b0"}}
2 changes: 1 addition & 1 deletion rerun_js/web-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@
"dts-buddy": "^0.3.0",
"typescript": "^5.2.2"
}
}
}

0 comments on commit 46ea1c0

Please sign in to comment.