Skip to content

Commit

Permalink
Add wasm-bindgen version check to CI (#7983)
Browse files Browse the repository at this point in the history
  • Loading branch information
jprochazk committed Nov 4, 2024
1 parent 08593ea commit e002aad
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 70 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,42 @@ jobs:
mode: minimum
count: 1
labels: "📊 analytics, 🟦 blueprint, 🪳 bug, 🌊 C++ API, CLI, codegen/idl, 🧑‍💻 dev experience, dependencies, 📖 documentation, 💬 discussion, examples, exclude from changelog, 🪵 Log & send APIs, 📉 performance, 🐍 Python API, ⛃ re_datastore, 🔍 re_query, 📺 re_viewer, 🔺 re_renderer, 🚜 refactor, ⛴ release, 🦀 Rust API, 🔨 testing, ui, 🕸️ web"

wasm-bindgen-check:
name: Check wasm-bindgen version
if: ${{ github.event_name == 'pull_request' }}
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: prefix-dev/[email protected]
with:
pixi-version: v0.34.0

- name: Get current wasm-bindgen version
id: current-version
run: |
version=$(pixi run taplo get -f crates/viewer/re_viewer/Cargo.toml "target.*.dependencies.wasm-bindgen")
echo "current_version=$version" >> $GITHUB_OUTPUT
- name: Get previous wasm-bindgen version
id: previous-version
run: |
prev_ref=$(git rev-parse --abbrev-ref HEAD)
git checkout main
version=$(pixi run taplo get -f crates/viewer/re_viewer/Cargo.toml "target.*.dependencies.wasm-bindgen")
echo "previous_version=$version" >> $GITHUB_OUTPUT
git checkout $prev_ref
- name: Require label if versions changed
if: ${{ steps.current-version.outputs.current_version != steps.previous-version.outputs.previous_version }}
uses: mheap/github-action-required-labels@v3
with:
mode: exactly
count: 1
labels: "wasm-bindgen version update"
86 changes: 17 additions & 69 deletions rerun_js/web-viewer/build-wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ function buildWebViewer(mode) {
}
}

async function re_viewer_js(mode) {
function re_viewer_js() {
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 Down Expand Up @@ -89,9 +88,8 @@ return Object.assign(__wbg_init, { initSync, deinit }, __exports);
fs.writeFileSync(path.join(__dirname, "re_viewer.js"), code);
}

async function re_viewer_d_ts(mode) {
function re_viewer_d_ts() {
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 @@ -104,77 +102,27 @@ 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;
}
function main() {
const args = util.parseArgs({
options: {
mode: {
type: "string",
},
},
});
const mode = args.values.mode;

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);
if (!mode) {
throw new Error("Missing required argument: mode");
}
}

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",
},
},
});

let updateHashes = !!args.values["update-hashes"];
let hashes;
try {
hashes = JSON.parse(
fs.readFileSync(path.join(__dirname, "hashes.json"), "utf-8"),
);
} catch (e) {
hashes = {};
re_viewer_js();
re_viewer_d_ts();
}

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);
}
main();
} catch (e) {
console.error(e.message);
console.error(e);
}
1 change: 0 additions & 1 deletion rerun_js/web-viewer/hashes.json

This file was deleted.

0 comments on commit e002aad

Please sign in to comment.