tiny-secp256k1 icon indicating copy to clipboard operation
tiny-secp256k1 copied to clipboard

`WebAssembly.Compile` is disallowed on the main thread, if the buffer size is larger than 4KB.

Open motorina0 opened this issue 3 years ago • 5 comments

Currently the WebAssembly part is loaded synchronously:

const mod = new WebAssembly.Module(binary);
const instance = new WebAssembly.Instance(mod, imports);

This results in the following error: 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.

However the async version of the methods cannot be run on the top-level file AFAIK. Any ideas how to fix this?

motorina0 avatar Feb 03 '22 18:02 motorina0

Can you provide detailed repro steps?

junderw avatar Feb 04 '22 00:02 junderw

I think this is a browser error... but that file is not used in browsers.

wasm_loader.browser.ts is for browsers.

Either:

  1. It's an error in whatever bundler you used.
  2. Maybe there's some weird edge case in NodeJS... in which case I need better repro steps.

junderw avatar Feb 06 '22 15:02 junderw

  1. It's an error in whatever bundler you used.

It is an Electron app that uses the lib, it might be the bundler. I will have a more in detail look. Thank you for the hint.

motorina0 avatar Feb 10 '22 09:02 motorina0

Facing the same issue.

I ended up creating a fork and updating the browser loader. src_ts /wasm_loader.browser.ts

// Suppress TS2792: Cannot find module './secp256k1.wasm'.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import wasm from "./secp256k1.wasm";

import * as rand from "./rand.js";
import * as validate_error from "./validate_error.js";
const imports = {
    "./rand.js": rand,
    "./validate_error.js": validate_error,
};
const mod = await WebAssembly.compile(Buffer.from(wasm.split(',')[1], 'base64'));
const instance = await WebAssembly.instantiate(mod, imports);

export default instance.exports;

pxr64 avatar Mar 29 '23 07:03 pxr64

hey @motorina0 , I know it's been long time since this issue was opened , just checking if you was able to solve this error

Taha-daboussi avatar Jun 23 '23 13:06 Taha-daboussi