deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

Browser support for `std/hash`

Open kjhughes opened this issue 4 years ago • 4 comments

I am trying to call createHash() from a bundled library loaded in a web browser, but the bundled JavaScript file has to compile WASM that the browser reports is too big for the main thread:

Uncaught RangeError: WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.compile, or compile on a worker thread. at issue.bundle.js:253

Description of demo files attached in WebAssembly.Compile_issue.zip:

  • issue.ts:
import { createHash } from "https://deno.land/[email protected]/hash/mod.ts";

const hash = createHash("sha256");
hash.update("Some data to be hashed");
console.log(hash.toString());
  • issue.bundle.js: Generated via deno bundle issue.ts issue.bundle.js. The line 253 of concern is
const wasmModule = new WebAssembly.Module(decode("AGFzbQEAAAAB   [...]
  • issue.html: Imports the JS bundle via <script type="module" src="issue.bundle.js"></script>

Serving and browsing to issue.html results in the above error regarding WebAssembly.Compile.

If I'm expecting too much of the bundler here, what's the simplest workaround to be able to call createHash() from a library loaded in a browser? Thank you.

kjhughes avatar Sep 02 '21 16:09 kjhughes

This isn't an error with the buffer, it is a challenge of how the WASM in std is created and loaded. The error is correct for the given browser:

Uncaught RangeError: WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.compile, or compile on a worker thread.

The buffer is larger than 4KB. In order to make it compatible with a browser, we would have to find another way to load the web assembly.

kitsonk avatar Sep 03 '21 00:09 kitsonk

I concur with your assessment and reclassification of the issue.

kjhughes avatar Sep 03 '21 04:09 kjhughes

I have the same problem.

rojvv avatar Oct 08 '21 18:10 rojvv

@kjhughes, std/hash is now deprecated in favour of std/crypto, which is web compatible. Try that and let us know how you go.

iuioiua avatar Aug 29 '22 05:08 iuioiua

This issue should probably be closed. std/hash has been removed from std.

lino-levan avatar Oct 27 '22 23:10 lino-levan

Thanks!

cjihrig avatar Oct 28 '22 00:10 cjihrig