Implement md5

Hi,

I want to implement md5 into my Fuse project. I wanted to use a JavaScript Library (https://github.com/blueimp/JavaScript-MD5).

But I cant use it in my Fuse project, the file does not contain a module.export variable (or function whatever). And I cant use it.

Some Code:


MainView.ux:

<App Background="#eee">

    <!-- Includes -->
    <Font File="fonts/fontawesome-webfont.ttf" ux:Global="fa" />

    <ux:Include File="icons.ux" />
    <ux:Include File="Elements.ux" />

    <JavaScript File="scripts/md5.min.js" ux:Global="Hashlib" />   <!-- Yep I am Python developer -->
    <JavaScript File="scripts/main.js" />

    <!-- [...] -->


main.js

// Required Variables
var Observable = require("FuseJS/Observable");
var hashlib    = require("Hashlib");

// Ascii Tables
/ [...] /

function encode() {
    // Check if checkbox was checked
    if(checkboxChecked.value) {
        // key2 will be hashed with md5
        debug_log(hashlib.md5("test"));
    }

}


.unoproj:

{
  "RootNamespace":"",
  "Packages": [
      "Fuse",
      "FuseJS",
    "Fuse.BasicTheme"
  ],
  "Includes": [
    "*"
  ]

}


So and the error:

LOG: InternalError: Script error: Name: System.Exception
    Error message: Exception of type 'System.Exception' was thrown.
    File name: scripts/main.js
    Line number: 3
    Source line: var hashlib    = require("Hashlib");
    JS stack trace: [Uno code]
      at Fuse.Reactive.RootableScriptModule.FindRootTable () [0x00000] in <filename unknown>:0 
      at Fuse.Reactive.RootableScriptModule.EnsureRooted (Fuse.Scripting.Context context) [0x00000] in <filename unknown>:0 
      at Fuse.Reactive.RootableScriptModule.Evaluate (Fuse.Scripting.Context c, Fuse.Scripting.ModuleResult result) [0x00000] in <filename unknown>:0 
      at Fuse.Scripting.Module.Evaluate (Fuse.Scripting.Context c, System.String id) [0x00000] in <filename unknown>:0 
      at Fuse.Scripting.ScriptModule+RequireContext.Require (System.String id) [0x00000] in <filename unknown>:0 
      at Fuse.Scripting.ScriptModule+RequireContext.Require (System.Object[] args) [0x00000] in <filename unknown>:0 
      at Fuse.Scripting.V8.Marshaller+CallbackWrapper.Call (Fuse.Scripting.V8.Simple.UniqueValueVector args) [0x00000] in <filename unknown>:0 
    </usr/local/share/uno/Packages/Fuse.Scripting.V8/0.31.5/$.uno:135>
LOG: Error: JavaScript error in scripts/main.js line 3. Exception of type 'System.Exception' was thrown. in Fuse.Reactive.JavaScript</usr/local/share/uno/Packages/Fuse.Reactive/0.31.5/$.uno:1327>

How can I fix that? And how can I implement md5 into Fuse?

Hi!

Instead of using ux:Global, just add the JS file to your .unoproj as :Bundle and require() it.

Thank you :smiley:

That error is fixed, but I got some new… :confused: :smiley:

main.js:

// Required Variables
var Observable = require("FuseJS/Observable");
var hashlib    = require("scripts/md5.js");
/ [...] /
    if(checkboxChecked.value) {
        // key2 will be hashed with md5
        debug_log(hashlib.md5("test"));
    }


unoproj:

{
  "RootNamespace":"",
  "Packages": [
      "Fuse",
      "FuseJS",
    "Fuse.BasicTheme"
  ],
  "Includes": [
    "*",
    "scripts/md5.js:Bundle"
  ]

}


MainView.ux:

    <!-- Includes -->
    <Font File="fonts/fontawesome-webfont.ttf" ux:Global="fa" />

    <ux:Include File="icons.ux" />
    <ux:Include File="Elements.ux" />

    <JavaScript File="scripts/main.js" />


And error:

LOG: InternalError: Script error: Name: TypeError: hashlib.md5 is not a function
    Error message: Uncaught TypeError: hashlib.md5 is not a function
    File name: scripts/main.js
    Line number: 39
    Source line:         debug_log(hashlib.md5("test"));
    JS stack trace: TypeError: hashlib.md5 is not a function
        at encode (scripts/main.js:39:27)
    </usr/local/share/uno/Packages/Fuse.Scripting.V8/0.31.5/$.uno:135>
ERROR: 
    Name: TypeError: hashlib.md5 is not a function
    Error message: Uncaught TypeError: hashlib.md5 is not a function
    File name: scripts/main.js
    Line number: 39
    Source line:         debug_log(hashlib.md5("test"));
    JS stack trace: TypeError: hashlib.md5 is not a function
        at encode (scripts/main.js:39:27)

If I only use md5 its the same error…

Does this work?

var Observable = require("FuseJS/Observable");
require("scripts/md5.js");
/ [...] /
    if(checkboxChecked.value) {
        // key2 will be hashed with md5
        debug_log(md5("test"));
    }

No :confused:

But the md5.js file doesn’t contain module.exports…

Is that necessary?

It’s still the same error, md5 is not defined.

Ah… after reading the md5.js code it actually register itself on module.exports when it is available. So you only need to do this:

var md5 = require("scripts/md5.js");
console.log(md5("test"));

1 Like