antelle / argon2-browser

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

`ReferenceError: atob is not defined` In nodejs #65

Closed davidyuk closed 3 years ago

davidyuk commented 3 years ago

Thanks for solving the previous issue!

$ node
Welcome to Node.js v14.16.0.
Type ".help" for more information.
> const { hash } = require('argon2-browser/dist/argon2-bundled.min')
undefined
> hash({ pass: 'password', salt: 'somesalt' })
Promise { <pending> }
> (node:10078) UnhandledPromiseRejectionWarning: ReferenceError: atob is not defined
    at /.../argon2-browser/dist/argon2-bundled.min.js:1:8174
    at /.../argon2-browser/dist/argon2-bundled.min.js:1:8283
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)

argon2-browser version is 1.18.0

Seems that atob is not available in node, but it is used in https://github.com/antelle/argon2-browser/blob/63cda65cd2182e39a139aa074e2306332ddd4417/lib/argon2.js#L97

antelle commented 3 years ago

You can either upgrade your node.js to get atob working, or bundle a polyfill if you need to redistribute the library for older node.js.

davidyuk commented 3 years ago

According to https://nodejs.org/ node@14 is still LTS version, I think it should be supported by default. atob can be easily replaced with Buffer in node. I was going to sent a PR with something like:

    function decodeWasmBinary(base64) {
+        if (typeof Buffer === 'function') {
+            return new Uint8Array(Buffer.from(base64, 'base64'))
+        }
        const text = atob(base64);
        const binary = new Uint8Array(new ArrayBuffer(text.length));
        for (let i = 0; i < text.length; i++) {
            binary[i] = text.charCodeAt(i);
        }
        return binary;
    }

but can't check it because of #66

antelle commented 3 years ago

Added the fallback, thanks