
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
nodeannotations
Advanced tools
git clone git@github.com:anupam-git/nodeannotations-example.gitnpm installnode testParser.jsnodeannotationsnpm install --save nodeannotations
Create the annotations and place them in a folder, say [project root]/myannotations
myannotations/Path.js
const {Annotation} = require("nodeannotations");
class Path extends Annotation {
constructor() {
super("Path");
}
dir() { return this.dir; }
param() { return this.param; }
}
module.exports = Path;
myannotations/Request.js
const {Annotation} = require("nodeannotations");
class Request extends Annotation {
constructor() {
super("Request");
}
}
module.exports = Request;
annotatedFile.js
/**
* @Request("/controller/endpoint/param")
* @Path(dir="/home/user1", param="somevalue")
*/
function test() {
// Function Body
}
/**
* @Request("/controller1/endpoint1/param1")
*/
class Test {
constructor() {
/**
* @Request("/controller2/endpoint2/param2")
*/
let a;
/**
* @Path(dir="/home/user2", param="someothervalue")
*/
const b;
/**
* @Request("/controller3/endpoint3/param3")
*/
var c;
}
/**
* @Path(dir="/home", param="test123")
*/
testFunction(req) {
// Function Body
}
}
Parse the annotated file by calling parse function with arguments
filePath : Path of the Annotated File to be Parsed. In this case, it should be the absolute path to the annotatedFile.js file.annotationsPath : Path of the directory containing the Annotations. In this case, it should be the absolute path to the myannotations directory.const {AnnotationParser} = require("nodeannotations");
try {
let annotatedElements = AnnotationParser.parse(__dirname+"/annotatedFile.js", __dirname+"/myannotations/");
console.log("Example to Loop through all the annotated Elements :");
// Loop through all elements (Class, Method, Variable) that are annotated
annotatedElements.forEach((annotatedElement) => {
console.log("\t"+annotatedElement.getName()+" : "+annotatedElement.getType());
// Loop and Print All annotations of the current Element
annotatedElement.getAnnotations().forEach((annotation) => {
console.log("\t\t"+JSON.stringify(annotation));
});
console.log();
});
console.log("\n\nExample to Loop through the elements which are annotated with @Path() :");
// Loop through the elements which are annotated with @Request()
annotatedElements.filterBy("Request").forEach((annotatedElement) => {
console.log("\t"+annotatedElement.getName()+" : "+annotatedElement.getType());
// Loop and Print All annotations of the current Element
annotatedElement.getAnnotations().forEach((annotation) => {
console.log("\t\t"+JSON.stringify(annotation));
});
console.log();
});
console.log("\n\nExample to Loop through the elements which are annotated with @Path():");
// Loop through the elements which are annotated with @Path()
annotatedElements.filterBy("Path").forEach((annotatedElement) => {
console.log("\t"+annotatedElement.getName()+" : "+annotatedElement.getType());
// Loop and Print the "dir" value of the @Path annotation
console.log("\t\tdir: "+annotatedElement.getAnnotation("Path").dir);
console.log();
});
} catch (err) {
console.log(err);
}
Annotation.valueStores the value of the Annotation
/**
* @Annotation("someValue")
*/
function test() {
//Function Body
}
value will have the data someValue for the function test()
Annotation.[param]Stores the value of the parameter param of the Annotation
/**
* @Annotation(param1="someValue1", param2="someValue2")
*/
function test() {
//Function Body
}
param1 will have the data someValue1 for the function and param2 will have the data someValue2 for the function test()
Annotation.objectOfStores the type of the Annotation
/**
* @Annotation("someValue")
*/
function test() {
//Function Body
}
objectOf will have the data Annotation for the function test()
AnnotatedElement.getName()Returns the name of the Element
/**
* @Annotation("someValue")
*/
function test() {
//Function Body
}
getName() will return "test" for the function test()
AnnotatedElement.getType()Returns the Type of the Element (Variable, Method or Class)
/**
* @Annotation("someValue")
*/
function test() {
//Function Body
}
getType() will return "Method" for the function test()
AnnotatedElement.getAnnotation(objectOf)Returns the Annotation object which is an instance of objectOf
/**
* @Annotation("someValue")
*/
function test() {
//Function Body
}
getAnnotation("Annotation") will return {"value":"someValue","objectOf":"Annotation"} for the function test()
AnnotatedElement.getAnnotations()Returns all the annotations of an AnnotatedElement
/**
* @Annotation("someValue")
* @AnotherAnnotation("someMoreValue")
*/
function test() {
//Function Body
}
getAnnotations() will return [{"value":"someValue","objectOf":"Annotation"}, {"value":"someMoreValue","objectOf":"AnotherAnnotation"}] for the function test()
AnnotatedElement.TYPE_CLASSConstant value 0 representing the AnnotatedElement as a Class
AnnotatedElement.TYPE_METHODConstant value 1 representing the AnnotatedElement as a Method
AnnotatedElement.TYPE_VARIABLEConstant value 2 representing the AnnotatedElement as a Variable
AnnotationParser.parse(filePath, annotationsPath)Parses a file for Annotations and returns annotatedElements object.
filePath is the Absolute Path of File to be Parsed for Annotations.annotationsPath is the Absolute Path of Directory containing all the Annotation Classes.annotatedElementsannotatedElements is the result returned after parsing the file with path filePath. annotatedElements is the array of all the elements that are Annotated with the Annotations available at annotationsPath. The Array Object also has method filterBy(objectOf)
filterBy(objectOf)filterBy returns the array of Elements Annotated with objectOf type of Annotation
let annotatedElements = AnnotationParser.parse(__dirname+"/annotatedFile.js", __dirname+"/myannotations/");
annotatedElements.forEach((annotatedElement) => {
console.log("\t"+annotatedElement.getName()+" : "+annotatedElement.getType());
// Loop and Print All annotations of the current Element
annotatedElement.getAnnotations().forEach((annotation) => {
console.log("\t\t"+JSON.stringify(annotation));
});
console.log();
});
// Loop through the elements which are annotated with @Annotation()
annotatedElements.filterBy("Annotation").forEach((annotatedElement) => {
console.log("\t"+annotatedElement.getName()+" : "+annotatedElement.getType());
// Loop and Print All annotations of the current Element
annotatedElement.getAnnotations().forEach((annotation) => {
console.log("\t\t"+JSON.stringify(annotation));
});
console.log();
});
FAQs
Annotations Support for NodeJS
The npm package nodeannotations receives a total of 3 weekly downloads. As such, nodeannotations popularity was classified as not popular.
We found that nodeannotations 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
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.