Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'javascript-obfuscator' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
///
import { JavaScriptObfuscator } from 'javascript-obfuscator';
let sourceCode1: string = JavaScriptObfuscator.obfuscate('var foo = 1;');
let sourceCode2: string = JavaScriptObfuscator.obfuscate('var foo = 1;', {
compact: true,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: true,
encodeUnicodeLiterals: false,
reservedNames: ['^foo$'],
rotateUnicodeArray: true,
selfDefending: true,
unicodeArray: true,
unicodeArrayThreshold: 0.8,
wrapUnicodeArrayCalls: true
});
files.forEach(file => {
if (path.extname(file) === '.js') {
let contents = fs.readFileSync(file, 'utf8');
console.log('Protecting ' + file);
// Change the settings here - https://github.com/javascript-obfuscator/javascript-obfuscator
let ret = javaScriptObfuscator.obfuscate(contents, {
compact: true
, controlFlowFlattening: false
, controlFlowFlatteningThreshold: 0.75
, deadCodeInjection: false
, deadCodeInjectionThreshold: 0.4
, debugProtection: false
, debugProtectionInterval: false
, disableConsoleOutput: false
, domainLock: []
, identifierNamesGenerator: 'hexadecimal'
, identifiersPrefix: ''
, inputFileName: ''
, log: false
, renameGlobals: false
, reservedNames: []
, reservedStrings: []
const {code, options} = body;
if (!options.sourceMap) {
delete options.sourceMapMode
}
// options.stringArrayEncoding come from the client as strings, but the
// obfuscator expects it to be a boolean or a string if 'base64'/'rc4'
if (['false', 'true'].indexOf(options.stringArrayEncoding) !== -1) {
options.stringArrayEncoding = options.stringArrayEncoding === 'true';
}
let response = {};
try {
const result = JavaScriptObfuscator.obfuscate(code, options);
response = {
code: result.getObfuscatedCode(),
sourceMap: result.getSourceMap(),
}
} catch (e) {
response = {
code: e.toString(),
sourceMap: '',
}
}
res.send(JSON.stringify(response));
});
}
});
}
// Wraps requires in conditional comments
let commentedSource = source.slice();
entries.sort((a, b) => b.end - a.end).forEach((n) => {
const before = commentedSource.slice(0, n.start);
const mid = commentedSource.slice(n.start, n.end);
const after = commentedSource.slice(n.end);
commentedSource = `${before}/* javascript-obfuscator:disable */${mid}/* javascript-obfuscator:enable */${after}`;
});
// Obfuscates commented source code
const options = loaderUtils.getOptions(this) || {};
const obfuscationResult = JavaScriptObfuscator.obfuscate(commentedSource, options);
const code = obfuscationResult.getObfuscatedCode();
return code;
};
private obfuscate(javascript: string): { obfuscatedSource: string, obfuscationSourceMap: string } {
const obfuscationResult = JavaScriptObfuscator.obfuscate(
javascript,
this.options
);
return {
obfuscatedSource: obfuscationResult.getObfuscatedCode(),
obfuscationSourceMap: obfuscationResult.getSourceMap()
}
}
}
return through2.obj(function (file, enc, cb) {
if (file.isNull()) return cb(null, file);
if (!file.isBuffer()) throw new PluginError('gulp-javascript-obfuscator', 'Only Buffers are supported!');
if (file.sourceMap) {
options.sourceMap = true;
options.inputFileName = file.relative;
options.sourceMapMode = 'separate';
}
try {
const obfuscationResult = JavaScriptObfuscator.obfuscate(String(file.contents), options);
file.contents = new Buffer(obfuscationResult.getObfuscatedCode());
if (options.sourceMap && options.sourceMapMode !== 'inline') {
if ( file.sourceMap ) {
const sourceMap = JSON.parse(obfuscationResult.getSourceMap());
sourceMap.file = file.sourceMap.file;
applySourceMap(file, sourceMap);
}
else {
this.push(new Vinyl({
cwd: file.cwd,
base: file.base,
path: file.path + '.map',
contents: new Buffer(obfuscationResult.getSourceMap())
}))
}
}
var sourceAndMap = asset.sourceAndMap();
inputSourceMap = sourceAndMap.map;
input = sourceAndMap.source;
}
else {
inputSourceMap = asset.map();
input = asset.source();
}
if (inputSourceMap) {
_this.options.sourceMap = true;
}
}
else {
input = asset.source();
}
var obfuscationResult = JavaScriptObfuscator.obfuscate(input, _this.options);
if (_this.options.sourceMap) {
var obfuscationSourceMap = obfuscationResult.getSourceMap(), transferredSourceMap = transferSourceMap({
fromSourceMap: obfuscationSourceMap,
toSourceMap: inputSourceMap
});
compilation.assets[file] = new SourceMapSource(obfuscationResult.toString(), file, JSON.parse(transferredSourceMap), asset.source(), inputSourceMap);
}
else {
compilation.assets[file] = new RawSource(obfuscationResult.toString());
}
});
});
if (device) {
message.params.url = new MemoryFile('js-framework.js', message.params.source).getUrl();
if (device.deviceInfo.logLevel) {
message.params.env.WXEnvironment.logLevel = device.deviceInfo.logLevel;
}
device.debuggerSession.postMessage(this, message);
}
else {
Logger.error('Fatal Error:native device unregistered before initJSRuntime!');
}
}
else if (method === 'callJS' && message.params.method === 'createInstance') {
if (device) {
let code = message.params.args[1];
if (message.params.args[2] && (message.params.args[2]['debuggable'] === 'false' || message.params.args[2]['debuggable'] === false)) {
const obfuscationResult = JavaScriptObfuscator.obfuscate(message.params.args[1], {
compact: true,
controlFlowFlattening: false,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: true,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: false,
stringArrayThreshold: 0.75,
unicodeEscapeSequence: true
});
code = obfuscationResult.getObfuscatedCode();
}
message.params.sourceUrl = new MemoryFile(message.params.args[2].bundleUrl || (Uuid() + '.js'), bundleWrapper(code)).getUrl();
device.debuggerSession.postMessage(this, message);
app.post('/api/weirdhta', (req, res) => {
const base64 = req.body.code;
let data = base64;
let buff = new Buffer(data, 'base64');
let text = buff.toString('ascii');
var obfuscationResult = JavaScriptObfuscator.obfuscate(
`
${text}
`,
{
compact: true,
controlFlowFlattening: true,
debugProtection: true,
log: false,
disableConsoleOutput: true,
rotateStringArray: true,
stringArray: true,
}
);
res.send(obfuscationResult.getObfuscatedCode());
});
if (device) {
message.params.url = new MemoryFile('js-framework.js', message.params.source).getUrl();
if (device.deviceInfo.logLevel) {
message.params.env.WXEnvironment.logLevel = device.deviceInfo.logLevel;
}
device.debuggerSession.postMessage(this, message);
}
else {
Logger.error('Fatal Error:native device unregistered before initJSRuntime!');
}
}
else if (method == 'callJS' && message.params.method == 'createInstance') {
if (device) {
let code = message.params.args[1];
if (message.params.args[2] && (message.params.args[2]['debuggable'] === 'false' || message.params.args[2]['debuggable'] === false)) {
var obfuscationResult = JavaScriptObfuscator.obfuscate(message.params.args[1], {
compact: true,
controlFlowFlattening: false,
debugProtection: false,
debugProtectionInterval: false,
disableConsoleOutput: true,
rotateStringArray: true,
selfDefending: true,
stringArray: true,
stringArrayEncoding: false,
stringArrayThreshold: 0.75,
unicodeEscapeSequence: true
});
code = obfuscationResult.getObfuscatedCode();
}
message.params.sourceUrl = new MemoryFile(message.params.args[2].bundleUrl || (Uuid() + '.js'), bundleWrapper(code)).getUrl();
device.debuggerSession.postMessage(this, message);