Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
node-recode
Advanced tools
Recode is a tiny [unified][unified] interface for translating javascript into [UNIST][unist] compliant jsAST format, performing transformations, and converting them back again!
Recode is a tiny unified interface for translating javascript into UNIST compliant jsAST format, performing transformations, and converting them back again!
This library is not production ready. Look forward to when the npm version no longer is tagged -experimental
Out of the box, recode transpiles javascript: javascript code is given, reformatted, and rewritten. Pass in some scripts, out come some scripts. Usually small programmatically irrelevant details are stripped (such as idiosyncratic spacing habits, or strange parenthesis placement). However, a lot more can be done by passing in plugins.
Recode will bridge the three largest tree transforming javascript plugin ecosystems: Babel, Acorn, and Unified.
NPM:
npm install node-recode
var recode = require('recode');
var fs = require('fs')
var exampleJS = fs.readFileSync('./example.js', 'utf8')
var map = require('unist-util-map');
var select = require('unist-util-select');
recode()
.use(function example () {
return function transformer (ast) {
var target;
select(ast, 'VariableDeclarator').forEach(function retrieveVariableName (node) {
if (node.type.match(/VariableDeclarator/)) {
//Grab where require is called, and where it requires 'recode'
if (select(node, 'Callee Identifier[name=require]').length > 0 &&
select(node, 'CallExpression Literal[value*=recode]').length > 0) {
//Retrieve the variable identifier set here.
var varNameNode = select(node, 'VariableIdentification Identifier')
//Save it for mischevious.
target = varNameNode[0].name
}
}
})
return map(ast, function (node) {
if (node.type.match('Identifier') && node.name === target) {
//Rename the recode variable everywhere it is mentioned in this program
node.name = "TheGreatestLibraryEver"
}
return node
});
}
})
.process(exampleJS, function (err, file) {
console.log(file.contents);
});
Yields:
var TheGreatestLibraryEver = require('recode');
var fs = require('fs');
var exampleJS = fs.readFileSync('./example.js', 'utf8');
var map = require('unist-util-map');
var select = require('unist-util-select');
TheGreatestLibraryEver().use(function example() {
return function transformer(ast) {
var target;
select(ast, 'VariableDeclarator').forEach(function retrieveVariableName(node) {
if (node.type.match(/VariableDeclarator/)) {
//Grab where require is called, and where it requires 'recode'
if (select(node, 'Callee Identifier[name=require]').length > 0 && select(node, 'CallExpression Literal[value*=recode]').length > 0) {
//Retrieve the variable identifier set here.
var varNameNode = select(node, 'VariableIdentification Identifier'); //Save it for mischevious.
//Save it for mischevious.
target = varNameNode[0].name;
}
}
});
return map(ast, function (node) {
if (node.type.match('Identifier') && node.name === target) {
//Rename the recode variable everywhere it is mentioned in this program
node.name = 'TheGreatestLibraryEver';
}
return node;
});
};
}).process(exampleJS, function (err, file) {
console.log(file.contents);
});
This library has only been made possible by the work of Titus Wormer. Everything about Recode has been modeled after his Rehype, Remark, and Retext libraries, even the name! The tests couldn't be possible if not for the espree repository itself.
FAQs
Recode is a tiny [unified][unified] interface for translating javascript into [UNIST][unist] compliant jsAST format, performing transformations, and converting them back again!
We found that node-recode demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.