antelle / argon2-browser

Argon2 library compiled for browser runtime
https://antelle.net/argon2-browser
MIT License
363 stars 78 forks source link

emcc -s MODULARIZE=1 generates invalid argon2.js #48

Closed glihm closed 3 years ago

glihm commented 3 years ago

@antelle ,

first of all thank you for this great repo.

I have cloned and build the repo without any problem with your version. No problem with plain Javascript. However, I am trying to load the wasm into Angular 10.

To do so, I have modified the build-wasm.sh adding to the -DCMAKE_EXE_LINKER_FLAGS= the emcc option -s MODULARIZE=1 to be able to load it easily from Typescript. Doing this, the generated argon2.js is no longer valid.

Can you try to add this option and compile to confirm that it's not due to my setup? Thank you for considering / your help.

antelle commented 3 years ago

Hi! What do you mean “load wasm into angular”? You should be able to load and use it as usual, no matter the framework. It mostly depends on bundler setup (webpack, etc...), not on the framework you’re using. Modularize will most likely break it, you’re right.

glihm commented 3 years ago

You're right, I apologize the bad vocabulary. :)

I have tried to compile once again removing -s MODULARIZE and adding -s ENVIRONMENT='web', and manually add the factory function from other js glue code that I have successfully used.

With this setup, the argon2.js is well formatted, but it contains a XMLHttpRequest that is trying to fetch automatically the wasm from localhost. I used to fetch manually the wasm file and then instantiating it. I am surely doing something wrong here.

Any help is welcome if you can achieve a build for a modularized version of argon2.js.

If for you this is not an issue, please feel free to close it and I'll continue to investigate as I do want to use argon2 for the front-end we have to deploy.

Thanks!

antelle commented 3 years ago

I just tried compiling it with:

-s MODULARIZE=1 -s EXPORT_NAME=createMyModule

With these lines disabled,

and then do this:

const wasmBinary = await fetch('dist/argon2.wasm').then(r => r.arrayBuffer());
const Module = await createMyModule({ wasmBinary });

It works for me. What happens for you if you do this?

glihm commented 3 years ago

It works great. 👍

I do apologize I didn't try to understand the perl lines first... Thank you for the update, and once again, a very nice and clean job you've done!

Best

antelle commented 3 years ago

These perl lines fix some stuff added by default by Emscripten. I already can't remember what it was then.