I use javascript-obfuscator package, it works fine if I do operations with files, like:
javascript-obfuscator source.js
but I need to dynamically change js source content and get output on console in way like
javascript-obfuscator "var foo = 'bar'; alert(foo);"
Any suggestion how can I get rid from saving content to file and do it like in snippet above?
Not really considering why you'd need to do this, it looks like you'll need to use the programmatic API as described in the library's README.
Let's call this obfuscate.js:
var JavaScriptObfuscator = require('javascript-obfuscator');
var obfuscationResult = JavaScriptObfuscator.obfuscate(
process.argv[2],
{
compact: false,
controlFlowFlattening: true
}
);
console.log(obfuscationResult.getObfuscatedCode());
$ node obfuscate.js 'console.log(1)'
will then output (for example)
var _0x2b5a = ['log'];
(function (_0x630038, _0x2944a9) {
var _0x83df37 = function (_0x2ef1a5) {
while (--_0x2ef1a5) {
_0x630038['push'](_0x630038['shift']());
}
};
_0x83df37(++_0x2944a9);
}(_0x2b5a, 0xd7));
var _0x493b = function (_0x2b48eb, _0x33884a) {
_0x2b48eb = _0x2b48eb - 0x0;
var _0x41338b = _0x2b5a[_0x2b48eb];
return _0x41338b;
};
console[_0x493b('0x0')](0x1);
Related
In Node.JS, I'm trying to "reload" a file. I have the following code:
delete require.cache[require.resolve("./pathToFile/" + restartModule)]
with restartModule being the file name, but I'm not sure how I could add the file back using require() and define it as the variable restartModule. For example, if restartModule is myModule, how would I add myModule.js into the var called myModule? Or maybe there's an easier way to simply "reload" a file in the cache?
You could do something simple enough like this:
function reloadModule(moduleName){
delete require.cache[require.resolve(moduleName)]
console.log('reloadModule: Reloading ' + moduleName + "...");
return require(moduleName)
}
var restartModule= reloadModule('./restartModule.js');
You would have to call reloadModule every time you want to reload the source though. You could simplify by wrapping like:
var getRestartModule = function() {
return reloadModule('./restartModule.js');
}
getRestartModule().doStuff();
Or
var reloadModules = function() {
return {
restartModule = reloadModule('./restartModule.js');
};
}
var modules = reloadModules();
modules.restartModule.doStuff();
Or:
var reloadModules = function(moduleList) {
var result = {};
moduleList.forEach((module) => {
result[module.name] = reloadModule(module.path);
});
}
var modules = reloadModules([{name: 'restartModule', path: './restartModule.js'}]);
modules.restartModule.doStuff();
You could even put the module reload on a setInterval so modules would get loaded every N seconds.
Then there's always nodemon: https://nodemon.io/ this is useful in development, whenever a source file changes it will reload your server.
You just use it like node, e.g.
nodemon server.js
I have multiple nightwatch tests with setup and teardown in every single test. I am trying to unify it into globalModule.js in before after(path set in globals_path in nightwatch.json).
//globalModule.js
before:function(test, callback){
// do something with test object
}
//sampletest.js
before: function(test){
..
},
'testing':function(test){
....
}
My problem is test context is not available in globalsModule.js. How do i get it there? Can someone let me know?
Test contex not available now. As said beatfactor, it will available soon.
While it not available try use local before first file, but it hack.
Also you can export all your file into one object and export it into nightwatch, but then you can use local before just in time.
For example:
var tests = {};
var befores = [];
var fs =require('fs');
var requireDir = require('require-dir');
var dirs = fs.readdirSync('build');
//if you have dirs that should exclude
var usefull = dirs.filter(function(item){
return !(item=='data')
});
usefull.forEach(function(item){
var dirObj = requireDir('../build/' + item);
for(key in dirObj){
if(dirObj.hasOwnProperty(key))
for(testMethod in dirObj[key])
if(dirObj[key].hasOwnProperty(testMethod))
if(testMethod == 'before')
befores.push(dirObj[key][testMethod]);
else
tests[testMethod] = dirObj[key][testMethod];
}
});
tests.before = function(browser){
//some global before actions here
//...
befores.forEach(function(item){
item.call(tests,browser);
});
};
module.exports = tests;
For more information https://github.com/beatfactor/nightwatch/issues/388
SSCCE:
gulp.task('foo', [], function() {
var barFiles = getBarFiles();
var bazFiles = getBazFiles();
var barStream, bazStream;
if (barFiles && barFiles.length > 0) {
barStream = gulp.src(barFiles);
}
if (bazStream && bazStream.length > 0) {
bazStream = gulp.src(bazStream);
}
return eventStream
.merge(barStream, bazStream)
.pipe(g.concat('bar-and-baz'))
.pipe(gulp.dest('./foo-output/'));
});
getBarFiles() or getBazFiles() may return an empty array,
which is not allowed by gulp.src(): Error: Invalid glob argument,
hence the need to wrap the creation of the stream with an if condition.
So the question is, how do I create an empty stream,
so that it can be merged with the other empty on non-empty stream?
How about using:
return gulp.src('.');
For newer Gulp its possible you will need to add allowEmpty: true option as follows:
return gulp.src('.', {allowEmpty: true});
As I assume it should just pass current directory in the stream with no actions on it. Works for me.
A function for creating an empty stream for use in gulp (gleaned from vinyl-fs source) is this:
var through2 = require('through2');
function createEmptyStream() {
var pass = through2.obj();
process.nextTick(pass.end.bind(pass));
return pass;
}
If you make barFiles = createEmptyStream() in the example, it is functionally the same.
Current version of gulp (3.9.1) and vinyl-fs (0.3.0) on 2016-03-05 allows for an empty src array (from my testing). It uses something similar to the above to create an empty stream. Your example works (mostly as-is) in current versions:
var eventStream = require('event-stream');
var gulp = require('gulp');
var g = require('gulp-load-plugins')();
function getBarFiles() {
return [
];
}
function getBazFiles() {
return [
'baz.txt'
];
}
gulp.task('foo', [], function() {
var barFiles = getBarFiles();
var bazFiles = getBazFiles();
var barStream = gulp.src(barFiles);
var bazStream = gulp.src(bazFiles);
return eventStream
.merge(barStream, bazStream)
.pipe(g.concat('bar-and-baz.txt'))
.pipe(gulp.dest('./'));
});
gulp.task('default', [ 'foo' ]);
The file bar-and-baz.txt is a concatenation of the contents of all files in the globs returned from those two functions. An empty list of globs is accepted.
Maybe my answer is too simplistic, but I had exactly the same question as the OP, and I was able to solve it without importing any additional modules:
if (platform != 'android') {
// Android doesn't need anything here
return gulp.src([]); // empty stream
}
In other words, gulp.src() doesn't work, but gulp.src([]) seems to work fine. Or am I missing something?
EDIT 18 Mar 2020: Apparently, Gulp 4.0 now requires the allowEmpty flag for this to work:
if (platform != 'android') {
// Android doesn't need anything here
return gulp.src([], {allowEmpty: true}); // empty stream
}
See Artur Stępień's answer to this question, or this related SO question.
Another solution that met my needs quite well was to use gulp-tap:
var config = require('./config');
var gulp = require('gulp');
var tap = require('gulp-tap');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var noop = function() {};
gulp.task('mytask', function() {
return gulp.src('*.js')
.pipe(config.production ? uglify() : tap(noop))
.pipe(concat('code.min.js'))
.pipe(gulp.dest('dist'));
});
This allows you to easily create an empty stream which you can pass inside a .pipe() so you don't have to have an if statement outside.
Note: As I just figured out, you shouldn't store the tap(noop) into a variable and attempt to use it across multiple tasks as that may actually mix your streams and cause erratic behavior.
I use this:
https://github.com/dominictarr/event-stream
es.merge([]);
It works!
One solution:
gulp.task('foo', [], function() {
var barFiles = getBarFiles();
var bazFiles = getBazFiles();
var barStream, bazStream;
if (barFiles && barFiles.length > 0) {
barStream = gulp.src(barFiles);
}
if (bazStream && bazStream.length > 0) {
bazStream = gulp.src(bazStream);
}
var mergedStream;
if (barStream && bazStream) {
mergedStream = eventStream
.merge(barStream, bazStream);
}
else if (barStream || bazStream) {
mergedStream = barStream || bazStream;
}
if (mergedStream) {
return mergedStream
.pipe(g.concat('bar-and-baz'))
.pipe(gulp.dest('./foo-output/'));
}
});
This side-steps the the need to create an empty stream by testing each of the streams, and merging them only when both are present.
However, I would still like to know of a way to create an empty stream in gulp.
Given the following code, I have managed to write a test by making use of QUnit for the first part but was unable to test finder.doRoutefinding. How can I 'mock'the function finder.doRoutefinding? (Mockjax cannot be used here since no ajax calls are involved)
`finder.doSelectDestination = function(address)
{
finder.destination = address;
finder.doRoutefinding(
finder.departure,
finder.destination,
finder.whenRouteLoaded,
finder.showRoute);
}
test('Destination Selector',
function()
{
address="London";
finder.doSelectDestination(address);
equal(pathfinder.destination,address, "Succesful Destination Selection");
}
);
There are caveats, but you could simply replace the function with your mock:
var originalDoRoutefinding = finder.doRoutefinding;
finder.doRoutefinding = function() {
// Mock code here.
};
// Test code here.
finder.doRoutefinding = originalDoRoutefinding;
If that kind of thing works for you, you might consider using a library like Sinon.JS.
Our application currently shares messages between the Java and Javascript side. They are stored as resource bundles in the class path, and we have a custom controller that returns all the messages as Json. The client side code look like this:
// This calls the controller to get all the messages
var messages = MessageBundle();
var text = messages.get('my.message', 1);
This is great because we can mock "messages" in our unit tests.
I want to start using JAWR for this, since we already use it for other things. The problem is JAWR generates the following Javascript object:
var text = messages.my.message(1);
This means the code cannot be unit tested anymore unless the unit tests also define a global "messages" variable with the right nested objects. Is there a way around this? Any idea how to extend JAWR to make this unit-testable?
Currently my work around is:
function messages() {
var args = Array.prototype.slice.call(arguments);
var messageId = args.shift();
var messageFunc = window.messages;
messageId.split('.').forEach(function(part) {
messageFunc = messageFunc[part];
});
return messageFunc(args);
}
// Same syntax as the old one, but uses the JAWR object behind the scenes
// This function is easy to mock for a unit test
var text = messages('my.message', 1);
Thanks for any ideas!
Maybe next samples can help you.
1)
function messagesTester(funcPath,id) {
var args=funcPath.split('.'),root=window.messages;
for(var i=0;i<args.length;i++)root=root[args[i]];
return root(id);
// or if more that one parameter for *func*, then, for example:
// return root.apply(null,Array.prototype.slice(arguments,1));
}
var text = messagesTester('my.message',1);
2)
function messagesTester(funcPath) {
var args=funcPath.split('.'),root=window.messages;
for(var i=0;i<args.length;i++)root=root[args[i]];
return root;
}
// var text = messagesTester('my.message')( /*arguments list*/ );
var text = messagesTester('my.message')(1);