import * as base64 from "../encoding/base64.ts";
const home = Deno.env.get("HOME");const root = new URL(".", import.meta.url).pathname;
if (new URL(import.meta.url).protocol === "file:") { Deno.chdir(root);} else { console.error("build.ts can only be run locally (from a file: URL)."); Deno.exit(1);}
if ( !((await Deno.run({ cmd: [ "cargo", "fmt", ], }).status()).success)) { console.error(`Failed to format the Rust code.`); Deno.exit(1);}
if ( !((await Deno.run({ cmd: [ "cargo", "build", "--release", ], env: { SOURCE_DATE_EPOCH: "1600000000", TZ: "UTC", LC_ALL: "C", RUSTFLAGS: `--remap-path-prefix=${root}=. --remap-path-prefix=${home}=~`, }, }).status()).success)) { console.error(`Failed to compile the Rust code to WASM.`); Deno.exit(1);}
const copyrightLine = `// Copyright 2018-${ new Date().getFullYear()} the Deno authors. All rights reserved. MIT license.`;
if ( !((await Deno.run({ cmd: [ "wasm-bindgen", "./target/wasm32-unknown-unknown/release/deno_std_wasm_crypto.wasm", "--target", "deno", "--weak-refs", "--out-dir", "./target/wasm32-bindgen-deno-js", ], }).status()).success)) { console.error(`Failed to generate JavaScript bindings from WASM.`); Deno.exit(1);}
const generatedWasm = await Deno.readFile( "./target/wasm32-bindgen-deno-js/deno_std_wasm_crypto_bg.wasm",);
const formattedWasmSize = generatedWasm.length.toString().padStart( Math.ceil(generatedWasm.length.toString().length / 3) * 3,).replace(/...\B/g, "$&_").trim();
const wasmIntegrity = `sha256-${ base64.encode(await crypto.subtle!.digest("SHA-256", generatedWasm))}`;
const wasmJs = `${copyrightLine}// This file is automatically @generated by _build.ts// It is not intended for manual editing.import * as base64 from "../encoding/base64.ts";
export const size = ${formattedWasmSize};export const name = "crypto.wasm";export const type = "application/wasm";export const hash = ${JSON.stringify(wasmIntegrity)};export const data = base64.decode("\\\n${ base64.encode(generatedWasm).replace(/.{78}/g, "$&\\\n")}\\\n");
export default data;`;
const generatedScript = await Deno.readTextFile( "./target/wasm32-bindgen-deno-js/deno_std_wasm_crypto.js",);const bindingsJs = `${copyrightLine}// This file is automatically @generated by _build.ts// It is not intended for manual editing.// deno-lint-ignore-fileimport wasmBytes from "./crypto.wasm.js";
${ generatedScript.replace( /^const wasm_url =.*?;\nlet wasmCode =.*?;\n.*?const wasmInstance =.*?;\n/sm, `const wasmModule = new WebAssembly.Module(wasmBytes);\n` + `const wasmInstance = new WebAssembly.Instance(wasmModule, imports);`, )}
// for testing/debuggingexport const _wasm = wasm;export const _wasmModule = wasmModule;export const _wasmInstance = wasmInstance;export const _wasmBytes = wasmBytes;`;
await Deno.writeTextFile("./crypto.wasm.js", wasmJs);await Deno.writeTextFile("./crypto.js", bindingsJs);
if ( !(await Deno.run({ cmd: [ "deno", "fmt", "./crypto.wasm.js", "./crypto.js", ], }).status()).success) { console.error( `Failed to format generated code.`, ); Deno.exit(1);}