You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Wordit <wo...@gmail.com> on 2012/10/04 21:49:54 UTC

CommonJS syntax help

I'm using CommonJS, following Ryan's suggestion yesterday, so that
maps and update functions can access an SHA hashing function. I'm new
to this and since jsSHA needs to create an object I can't figure out
how to make it work.

jsSHA usage:

shaObj = new jsSHA(mytext, "ASCII");
hashedText = shaObj.getHash("SHA-256", "HEX");

I included the minified jsSHA code in views under lib:
"views": {
  "lib": {
        // this module is accessible from view functions
        "sha256": "export.hash = (function() ...


The map function I'm testing, which fails to deliver:

function (doc) {
var val = require('views/lib/sha256').hash;
var shaObj = new val.jsSHA("foobar", "ASCII");
var newhash = shaObj.getHash("SHA-256", "HEX");
emit(doc._id, newhash);
}

It emits nothing so I guess my syntax to access the jsSHA function is wrong?

Thanks for any help,

Marcus

Re: CommonJS syntax help

Posted by Jim Klo <ji...@sri.com>.
Looks like you assign to your own closure?

Typically what I do for libs that don't support CommonJS is :

(function(exports) {

// insert the lib code


// add exports

exports.jsSHA = jsSHA;

})(exports);


Then:

var shalib = require('lib/sha.js");
var mySHA = new shalib.jsSHA(...);

Sometimes I have to drop the closure and just assign the exports.

- Jim 

Sent from my iPhone

On Oct 5, 2012, at 9:56 AM, "Wordit" <wo...@gmail.com> wrote:

> On Fri, Oct 5, 2012 at 8:53 AM, Jim Klo <ji...@sri.com> wrote:
>> What does jsSHA look like?  Does it export something?  I think CommonJS uses exports vs export.
> 
> Oops, typo, googd catch. However, it changes nothing. I think the
> problem is related to how jsSHA needs to instantiate a new object
> first. From the docs:
> 
> "Instantiate a new jsSHA object with your string to be hashed and its
> format (HEX or TEXT) as the parameters. Then, call getHash with the
> desired hash variant (SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512)
> and output type (HEX or B64)
> ...
> var shaObj = new jsSHA("This is a Test", "TEXT");
> var hash = shaObj.getHash("SHA-512", "HEX");"
> 
> jsSHA is on Github at:
> 
> https://github.com/Caligatio/jsSHA
> 
> 
> Is it possible that CommonJS does not allow for this kind of module
> usage or is there a workaround?
> 
> Thanks,
> 
> Marcus

Re: CommonJS syntax help

Posted by Wordit <wo...@gmail.com>.
On Fri, Oct 5, 2012 at 8:53 AM, Jim Klo <ji...@sri.com> wrote:
> What does jsSHA look like?  Does it export something?  I think CommonJS uses exports vs export.

Oops, typo, googd catch. However, it changes nothing. I think the
problem is related to how jsSHA needs to instantiate a new object
first. From the docs:

"Instantiate a new jsSHA object with your string to be hashed and its
format (HEX or TEXT) as the parameters. Then, call getHash with the
desired hash variant (SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512)
and output type (HEX or B64)
...
var shaObj = new jsSHA("This is a Test", "TEXT");
var hash = shaObj.getHash("SHA-512", "HEX");"

jsSHA is on Github at:

https://github.com/Caligatio/jsSHA


Is it possible that CommonJS does not allow for this kind of module
usage or is there a workaround?

Thanks,

Marcus

Re: CommonJS syntax help

Posted by Jim Klo <ji...@sri.com>.
What does jsSHA look like?  Does it export something?  I think CommonJS uses exports vs export. 

FWIW, I did something similar in a kanso app [1].


- Jim

[1] https://github.com/jimklo/TheCollector/tree/master/dataservices/thecollector-resources

Sent from my iPad

On Oct 4, 2012, at 12:51 PM, "Wordit" <wo...@gmail.com> wrote:

> I'm using CommonJS, following Ryan's suggestion yesterday, so that
> maps and update functions can access an SHA hashing function. I'm new
> to this and since jsSHA needs to create an object I can't figure out
> how to make it work.
> 
> jsSHA usage:
> 
> shaObj = new jsSHA(mytext, "ASCII");
> hashedText = shaObj.getHash("SHA-256", "HEX");
> 
> I included the minified jsSHA code in views under lib:
> "views": {
>  "lib": {
>        // this module is accessible from view functions
>        "sha256": "export.hash = (function() ...
> 
> 
> The map function I'm testing, which fails to deliver:
> 
> function (doc) {
> var val = require('views/lib/sha256').hash;
> var shaObj = new val.jsSHA("foobar", "ASCII");
> var newhash = shaObj.getHash("SHA-256", "HEX");
> emit(doc._id, newhash);
> }
> 
> It emits nothing so I guess my syntax to access the jsSHA function is wrong?
> 
> Thanks for any help,
> 
> Marcus