diff options
author | skdltmxn <supershop@naver.com> | 2020-06-17 06:12:50 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-16 17:12:50 -0400 |
commit | b3c72d1e4554b5fd58cbf3ba2fbf56275446447b (patch) | |
tree | 613f42672a715b8fa9422a86af09abec66e4fcef /std/hash/_wasm/build.ts | |
parent | b8872cd303584b28c0d6250184e4a1205bf2ad9b (diff) |
feat(std/hash): reimplement all hashes in WASM (#6292)
Diffstat (limited to 'std/hash/_wasm/build.ts')
-rw-r--r-- | std/hash/_wasm/build.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/std/hash/_wasm/build.ts b/std/hash/_wasm/build.ts new file mode 100644 index 000000000..eaa6a21dd --- /dev/null +++ b/std/hash/_wasm/build.ts @@ -0,0 +1,48 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +import { encode as base64Encode } from "../../encoding/base64.ts"; + +// 1. build wasm +async function buildWasm(path: string): Promise<void> { + const cmd = [ + "wasm-pack", + "build", + "--target", + "web", + "--release", + "-d", + path, + ]; + const builder = Deno.run({ cmd }); + const status = await builder.status(); + + if (!status.success) { + console.error(`Failed to build wasm: ${status.code}`); + Deno.exit(1); + } +} + +// 2. encode wasm +async function encodeWasm(wasmPath: string): Promise<string> { + const wasm = await Deno.readFile(`${wasmPath}/deno_hash_bg.wasm`); + return base64Encode(wasm); +} + +// 3. generate script +async function generate(wasm: string, output: string): Promise<void> { + const initScript = await Deno.readTextFile(`${output}/deno_hash.js`); + const denoHashScript = + "/* eslint-disable */\n" + + "//deno-fmt-ignore-file\n" + + `import * as base64 from "../../encoding/base64.ts";` + + `export const source = base64.decode("${wasm}");` + + initScript; + + await Deno.writeFile("wasm.js", new TextEncoder().encode(denoHashScript)); +} + +const OUTPUT_DIR = "./out"; + +await buildWasm(OUTPUT_DIR); +const wasm = await encodeWasm(OUTPUT_DIR); +await generate(wasm, OUTPUT_DIR); |