hapipal / confidence

Dynamic, declarative configurations
Other
263 stars 43 forks source link

Make confidence isomorphic by leveraging browse crypto if needed #54

Closed tlvenn closed 8 years ago

tlvenn commented 8 years ago

Right now, the Id lib requires the crypto module, I wonder if we could not do the following so one can use confidence in client side app as well:

internals.getRandomBytes = (
    (typeof self !== 'undefined' && (self.crypto || self.msCrypto))
        ? function () { // Browsers
            const crypto = (self.crypto || self.msCrypto), QUOTA = 65536;
            return function (n) {
                let a = new Uint8Array(n);
                for (let i = 0; i < n; i += QUOTA) {
                    crypto.getRandomValues(a.subarray(i, i + Math.min(n - i, QUOTA)));
                }
                return a;
            };
        }
        : function () { // Node
            return require("crypto").randomBytes;
        }
)();
patrickkettner commented 8 years ago

i'd be open to it, but we would need to add proper browser testing suite in order for it to be comfortable in shipping it.

tlvenn commented 8 years ago

It's unfortunately a bit deeper that I though given Hoek has dependencies on nodejs as well. Also technically speaking the Crypto lib is actually not used by Confidence.Store but by the Id lib.

I wanted to use Confidence in a RN app where Crypto is not even available so I did end up forking Confluence, keeping only the store and embedding a lighter version of Hoek. I also converted the tests to Jest.

https://github.com/jumpn/confidence

It's little bit sad because as demonstrated the store itself could very well be isomorphic.

patrickkettner commented 8 years ago

Hoek is only used in a handful of lines, which could be replaced with lodash through either a webpack config or some other kind of compile time replacement.

I would be more than happy to review any PR adding this functionality, but I don't have the time to write it myself. As a result, closing this unless someone wants to take it on