Copy .wasm artifacts and gate them with a hash manifest
Pin .wasm artifacts with a SHA-256 manifest, and fail the check when one byte changes.
Apache-2.0Command line
Package: @awacloud/tool-wasm-crypto
Run it
Command line Guide
Promote a package's dist/*.wasm into a committed directory and write
MANIFEST.sha256, then run the --check drift gate: it passes, and fails
after one byte changes. Offline, no toolchain.
Uses @awacloud/tool-wasm-crypto (Apache-2.0). This example's code is Apache-2.0.
When to use it
Built WebAssembly bytes live in a gitignored dist/; what ships is a
committed copy. The manifest records what was promoted and CI checks the
committed bytes against it, so a hand edit or a stale copy cannot slip
through. build and verify need the WASI SDK and are out of scope here.
Copy-paste steps
npm install --save-dev @awacloud/tool-wasm-cryptobunx wasm-crypto copy --pkg <package> --into <committed-dir>after a build, andbunx wasm-crypto copy --pkg <package> --into <committed-dir> --checkin CI.- Or copy
main.jsand runbun main.js: it builds a throwaway package in the OS temp directory and prints the three exit codes.
What it shows
run({ args: ['copy', '--pkg', pkg, '--into', dir, ...] })from@awacloud/tool-wasm-crypto, in process, returning the exit code.- The manifest format:
sha256sum-style rows, sorted by file name, LF-terminated, written to<pkg>/dist/MANIFEST.sha256. - Exit codes: 0 applied or no drift, 1 drift (one line per file on stderr)
or an unmatched
--target, 2 configuration error. - Running
copytwice without a rebuild changes nothing.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
// Run: bun main.js (Bun only: the tool ships TypeScript) — builds a throwaway package in the OS temp dir.
// Equivalent CLI: bunx wasm-crypto copy --pkg <pkg> --into <dir>;
// bunx wasm-crypto copy --pkg <pkg> --into <dir> --check
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { run } from '@awacloud/tool-wasm-crypto';
/** The smallest valid WebAssembly module (magic + version), plus a payload byte to tell files apart. */
const wasm = (tag) => new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, tag]);
/**
* A package whose `dist/` holds two built modules, and an empty destination.
* @param {string} root
* @returns {{ pkg: string, into: string }}
*/
export function scaffold(root) {
const pkg = join(root, 'pkg');
const into = join(root, 'committed');
mkdirSync(join(pkg, 'dist'), { recursive: true });
mkdirSync(into, { recursive: true });
writeFileSync(join(pkg, 'dist/alpha.scalar.wasm'), wasm(1));
writeFileSync(join(pkg, 'dist/beta.scalar.wasm'), wasm(2));
return { pkg, into };
}
/**
* `copy` in process: the same arguments as the CLI after the subcommand.
* @param {string} pkg
* @param {string} into
* @param {string[]} [extra] e.g. `['--check']`
* @returns {Promise<number>} 0 applied / no drift, 1 drift, 2 config error
*/
export function copy(pkg, into, extra = []) {
return run({ args: ['copy', '--pkg', pkg, '--into', into, ...extra] });
}
/** @param {string} pkg */
export const manifest = (pkg) => readFileSync(join(pkg, 'dist/MANIFEST.sha256'), 'utf8');
/**
* Flip one byte of a committed file: the gate must catch it.
* @param {string} into
* @param {string} name
*/
export function tamper(into, name) {
const bytes = new Uint8Array(readFileSync(join(into, name)));
bytes[bytes.length - 1] ^= 0xff;
writeFileSync(join(into, name), bytes);
}
/** @returns {Promise<string[]>} report lines */
export async function main() {
const root = mkdtempSync(join(tmpdir(), 'awa-example-wasm-crypto-'));
try {
const { pkg, into } = scaffold(root);
const applied = await copy(pkg, into);
const clean = await copy(pkg, into, ['--check']);
tamper(into, 'beta.scalar.wasm');
const drifted = await copy(pkg, into, ['--check']);
return [
`copy: exit ${applied}; manifest rows: ${manifest(pkg).trim().split('\n').length}`,
`copy --check: exit ${clean}`,
`copy --check after flipping a byte of beta.scalar.wasm: exit ${drifted}`,
];
} finally {
rmSync(root, { recursive: true, force: true });
}
}
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
for (const line of await main()) console.log(line);
}