Using Crypto-JS (commonjs) in titanium - javascript

I am not familiar with commonjs conception, so my strategy might be basically wrong..
I want to use CryptoJS.HMAC() and CryptoJS.SHA256 in titanium
I have downloaded CryptoJS v3.1.2.zip from here.
then copy all files under Resources/CryptJS/ .
then I add this last line in core.js
return C;
}(Math));
module.export = CryptoJS; //add this line
now in my app.js
var CryptoJS = require('./CryptoJS/components/core');
CryptoJS.HMAC(CryptoJS.SHA256, dateStamp, testKey, { asBytes: true});
however it shows
 
undefined is not a function error when I call CryptoJS.HMAC()
Can I have help?

You find a commonJS version of Crypto Js on npm: https://www.npmjs.com/package/browserify-cryptojs
Maybe you got more luck with that!

Try adding an s to export, so it becomes:
module.exports = CryptoJS; //add this line

Related

Reason for node module #kenjiuno/msgreader error: MsgReader is not a constructor

I'm unable to get even the first lines of the example code from the relatively popular #kenjiuno/msgreader for parsing Outlook .msg files to work. I've installed the module with npm successfully, and my code is:
const fs = require('fs')
const MsgReader = require('#kenjiuno/msgreader')
const msgFileBuffer = fs.readFileSync('./test-email.msg')
const testMsg = new MsgReader(msgFileBuffer)
But I get the error: "MsgReader is not a constructor".
A quick console log of MsgReader returns { default: [Function: MsgReader] }. I also tried doing it as a function (no 'new' keyword) which also produced an error.
The only difference between my code and the example code is that they use import (import MsgReader from '#kenjiuno/msgreader') whereas I've used require, but presumably that couldn't make a difference?
Any ideas anyone?
I ended up changing the require statement to add ["default"] which fixed the issue:
const MsgReader = require('#kenjiuno/msgreader')["default"]
I looked at the library code and made a guess based on the export statement using that 'default' syntax. Is this issue something to do with commonJS or something? If anyone can explain this to me that would be great!

get gulp version in gulpfile

Is there a way to detect what version of Gulp is running (available to utilize in a gulpfile)?
I've got two separate gulpfile's I'm using amongst different environments, some that require v3 and some v4. For easier version control, I would prefer it if I could combine those files and not have to deal with different file names in different environments to eliminate confusion between multiple developers. Obviously to accomplish this I would need the script to differentiate between versions.
Alternatively to #alireza.salemian's solution, you could try to run the command line version command in javascript:
Depending on your JavaScript backend, your code may vary slightly, but inspired by this post you could run it as below:
const execSync = require('child_process').execSync;
// import { execSync } from 'child_process'; // replace ^ if using ES modules
const output = execSync('gulp -v', { encoding: 'utf-8' }); // the default is 'buffer'
const str_pos = output.search('Local version') + 14;
const gulp_version = output.substring( str_pos, str_pos + 5 );
console.log( 'Gulp version: ' + gulp_version );
You can read package.json and find gulp version
const pkgJson = fs.readFileSync('./package.json', { encoding: 'utf8' });
const pkg = JSON.parse(pkgJson);
const gulpVersion = pkg['devDependencies']['gulp'];
It may not be the best solution, but you can quickly determine the gulp version.

Why special charactors got replaced when imported using 'require' in NodeJS?

I'm new to JavaScript and NodeJS. So please don't condemn me if this issue is obvious.
My outsourced file is a simple config file. Here is a short version of it.
config.js:
var config = {};
config.Web = {};
config.Web.Title = 'Title with öüöäéàè';
module.exports = config;
The config.js gets loaded into my app.js with that code:
var configread = require('./views/config/config');
All special-characters get replaced by a � as visible on console:
var WebsiteTitle = configread.Web.Title;
console.log(WebsiteTitle);
Strings defined in the app.js script itself doesn't have this behavior. So i think the problem must be in the way of loading my config.js into my app.
Does someone have a solution for this behavior?
After some over-the-night-research i got the answer by myself.
The config.js file was created with Windows-Notepad. From there it got the ANSI-Encoding. It seems like NodeJS can't handle special-characters when config.js isn't encoded in UTF-8. I had to place the code in an UTF-8 encoded config.js file to solve the problem.
Thanks for any prior help!

export node module to local error

i am a newby on node.js. i want to parse a xml file into json .so i am trying to use bulebutton library from https://github.com/blue-button/bluebutton.js .
first i have installed the module by command npm install bluebutton and its created a node_modules folder with bluebutton module.
now i created a test.js file with following code
var bb = require('bluebutton');
var myRecord = bb.BlueButton('./asd.xml');
console.log(myRecord);
but its gave me an error that bluebutton is not define .please help me to figureout this problem thanks
REVISED ANSWER
From bluebuttonjs.com/docs, the require statement you use would return the BlueButton object, so bb represents said object, and it would be called like so
var myRecord = bb('someFile.xml');
However you might also note that they use fs to read the file before passing it. http://www.bluebuttonjs.com/docs/#parsing-node
PREVIOUS ANSWER (for wrong module)
According to their npm docs, you need to do
var bb = require('blue-button');
https://www.npmjs.com/package/blue-button

How do you compile CoffeeScript in a Jakefile?

I would like to create a Jakefile which compiles some CoffeeScripts to install a NodeJS application.
How do you do that?
I tried with:
https://gist.github.com/1241827
but it's a weak approach, definitely not classy.
Any hints?
Approx snippet I have used:
var fs = require('fs')
var coffee = require('coffee-script')
// If you'd like to see compiled code..
// console.log(coffee.compile(fs.readFileSync('coffee.coffee')))
// ..otherwise
fs.writeFileSync('output.js', coffee.compile(fs.readFileSync('input.coffee')))
..assumes you have the coffee-script node module installed, of course.
Translated from this Cakefile of mine.

Categories