dcodeIO / bcrypt.js

Optimized bcrypt in plain JavaScript with zero dependencies.
Other
3.47k stars 264 forks source link

Using bcrypt.js in the browser #84

Closed joananespina closed 5 years ago

joananespina commented 5 years ago

Hello, I am working on a webapp project that uses bcrypt.js. Right now I am using the Require.js route to load bcrypt.js:

require.config({ paths: { "bcrypt": "/path/to/bcrypt.js" } }); require(["bcrypt"], function(bcrypt) { ... });

But I want to try to load it in the browser without Require.js to lessen my project's pageload, because I am only using Require.js to load bcrypt.js. Right now the only documentation I see is to use var bcrypt = dcodeIO.bcrypt;, but dcodeIO is undefined when I load bcrypt.js using script tags?

joananespina commented 5 years ago

Just to clarify, adding bcrypt.js using script tag will make dcodeIO undefined.

So after reading a past issue I needed to use import to make it work, so what I did was:

import("/path/to/bcrypt.min.js").catch((error)=>{ // console.log(error); // This throws a });

This method worked and dcodeIO is now available, however, this also throws a TypeError: Cannot read property 'Promise' of undefined error.

Can anyone confirm that these are normal behaviors when using bcrypt.js on a browser?

joananespina commented 5 years ago

Just pointing this out. In my last message, the import() method only worked with Chrome, it failed on Firefox. I tried modifying the code to a simple encapsulated function, but I am afraid that it might cause some issues (although it looks like it wouldn't?), so just to be safe I am reverting back to using require.js.

Closing this unless someone can give some feedback?

besworks commented 2 years ago

I was able to get rid of this TypeError by using the unminified version and changing the value of global from this to window where it is injected into the loader function:

(function(global, factory) {

    /* AMD */ if (typeof define === 'function' && define["amd"])
        define([], factory);
    /* CommonJS */ else if (typeof require === 'function' && typeof module === "object" && module && module["exports"])
        module["exports"] = factory();
    /* Global */ else
        (global["dcodeIO"] = global["dcodeIO"] || {})["bcrypt"] = factory();

}(this, function() {

Look for that code block and make the change on the last line. Seems to work fine so far.