
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Fastest HTTP Router
\this\is\:num([0-9]+)
\login\as\:role(admin|staff|user)
\this\is\*
, \this\is\wild*
\like\me\
and \like\me
.\like\me
and like\me
.\this\is\:age([0-9]+)
and \this\is\:name([a-zA-Z]+)
const http = require('http')
const router = require('anumargak')({
defaultRoute : defaultHandler,//it'll be called when no route matches. If it is not set the we'll set statusCode to 404
ignoreTrailingSlash: true,
ignoreLeadingSlash: true,
allowUnsafeRegex: false
})
router.on('GET', '/', (req, res, params) => {
//process the request response here
})
const server = http.createServer((req, res) => {
router.lookup(req, res)
})
server.listen(3000);
To register a route.
router.on("GET", "/this/is/static", handler);
router.on(["POST","PUT"], "/this/is/static", handler);
You can register dynamic url with multiple path paramters
router.on("GET", "/this/is/:dynamic", handler);
router.on("GET", "/this/is/:dynamic", handler);//it will error
router.on("GET", "/this/is/:dynamic/with/:pattern(\\d+)", handler);
//Eg: params = { dynamic : val, pattern: 123}
router.on("GET", "/this/is/:dynamic/with/:two:params", handler);// multiple parameters
router.on("GET", "/this/is/:dynamic/with/:two(\\d+):params", handler);//multiple parameters with pattern
router.on("GET", "/this/is/:dynamic/with/:two(\\d+)rest", handler);//single parameter
Anumargak handls enumerated URLs in static way. Because static URLs can be looked up faster than dynamic URLs.
router.on("GET", "/login/as/:role(admin|user|staff)", handler);
wild cards are helpful when a route handler wants to control all the underlying paths. Eg. a handler registered with /help*
may take care of all the help pages and static resources under the same path. You can check आलेख (Aalekh) for live example.
//this/is/juglee/and/
//this/is/juglee/and/wild
//this/is/juglee/and/wild/and/unknown
router.on("GET", "/this/is/:dynamic/and/*", handler);
//this/is/juglee/and/wild
//this/is/juglee/and/wildlife
//this/is/juglee/and/wild/and/unknown
router.on("GET", "/this/is/:dynamic/and/wild*", handler);
var router = Anumargak();
router.get("/this/is/:dynamic", () => 30);
router.head("/this/is/:dynamic", () => 30);
router.post("/this/is/:dynamic", () => 30);
router.put("/this/is/:dynamic", () => 30);
router.delete("/this/is/:dynamic", () => 30);
To remove a registered route. If no route is found no error will be thrown.
anumargak.off("GET", "/this/is/static");
anumargak.off("GET", "/this/is/:dynamic");
anumargak.off("GET", "/this/is/*/really/wild");
anumargak.off("GET", "/login/as/:role(admin|user|staff)"); //it'll delete all the versions
an enumerated URL can be deleted multi steps
anumargak.off("GET", "/login/as/:role(admin|user|staff)");
//or
anumargak.off("GET", "/login/as/admin");
anumargak.off("GET", "/login/as/user");
anumargak.off("GET", "/login/as/staff");
//or
anumargak.off("GET", "/login/as/:role(user|staff)");
anumargak.off("GET", "/login/as/admin");
version can be provided as an additional parmeter. Valid values are:
anumargak.off("GET", "/this/is/static", version);
Please note that, if you delete a route without specifying versions then all the versioned routes will also be deleted.
To find a registered route. It returns;
{
handler : function(){}, //registered function
params : {}, //path parameters
store : any // extra data provided at the time of registering the route
}
To find a registered route. It returns;
{
handler : function(){}, //registered function
store : any // extra data provided at the time of registering the route
}
quickFind(request)
quickFind()
is faster than find()
as it doesn't process path parameters.This method reads request object to fetch url, method, and accept-version
header to find matching route and then run the handler.
The handler should accept: request, response, and store. request._path.params is an object of path parameters.
Lookup method also save _path, _queryStr, and _hashStr in request object to save re-effort of spliting them. _path is an object with two properties: url, params.
save as above but raises events
You can always check how many routes are registered. If you delete some routes count will be decreased.
You can register the URLs which look similar but not exactly same.
const anumargak = require('anumargak')()
//this/is/my/75
anumargak.on("GET", "/this/is/my/:age([0-9]{2,3})", handler);
//this/is/my/amit
anumargak.on("GET", "/this/is/my/:name([a-zA-z]+)", handler);
anumargak.on("GET", "/login/as/:role(admin|user|staff)", handler);
anumargak.on("GET", "/login/as/:role(developer|tester|hacker)", handler);
Anumargak lets you add named expressions. You can use them at the time of registering the route.
router.addNamedExpression("num","\\d+");
router.addNamedExpression({
"name1" : "regx1",
"name2" : "regx2",
});
Example routes
/send/to/:phone(:phone:)
/authenticate/:token(:alphanum:)
Adding them make this router simple to use.
Same routes can be registerd with different versions. Lookup method reads accept-version
header to read the version or you can pass the version in find method directly.
router.on('GET', '/some/route', { version: '1.2.0' }, (req, res, params) => {
//..
})
router.get( '/some/route', { version: '1.2.3' }, (req, res, params) => {
//..
})
router.find('GET', '/some/route', "1.2.3");
router.lookup(req, res);
Note that
accept-version
header presents then versioned route handler will be returned.accept-version
header can be : "1.2.0", "1.2.x", "1.x", "*"You can register events.
router.on(eventName, fn);
Following events are supported;
These events will be called when you use lookupWithEvents()
.
Anumargak supports
router.on( "single/url", fn );
router.on( "single/url", [fn, fn] );
router.on( ["single/url", "other/url" ], fn );
router.on( ["single/url", "other/url" ], [fn, fn] );
Method | URL type | अनुमार्गक (Anumargak) v1.7.0 | Find My Way v1.15.1 |
---|---|---|---|
Find | static | 24369856.07 | 2614866.631 |
Find | dynamic | 2405576.122 | 1106656.051 |
Find | dynamic + query param | 1665114.806 | 1082533.894 |
Find | Enum | 23151436.16 | 1298019.289 |
Find | wildchar | 2591342.638 | 1630995.248 |
Find | versioned static | 1958139.854 | 263611.4248 |
Find | versioned Dynamic | 465584.9696 | 315857.0792 |
Look | static | 23614465.42 | 2201314.59 |
Look | dynamic | 2032592.029 | 940862.1238 |
Look | dynamic + query param | 1403264.652 | 923533.114 |
Look | Enum | 12662923.03 | 1066446.935 |
Look | wildchar | 2251611.449 | 1335382.238 |
Look | versioned static | 1313886.055 | 0 |
Look | versioned Dynamic | 392754.437 | 252312.1124 |
Note : Above benchmark has been taken on 16gb RAM ubuntu 17.10 machine with node v9.5.0 and npm v5.6.0
Muneem framework is already based on Anumargak. To use it with express js;
const app = require("express")();
const Anumargak = require("anumargak");
const router = new Anumargak();
app.use((req,res) => router.lookup(req, res));
router.on("GET", "/", (req, res) => {
//..
});
app.listen(3002);
Use it with restana
const anumargak = require('anumargak')
const service = require('restana')({
routerFactory: (options) => {
return anumargak(options)
}
})
service.get("/this/is/static", function(req, res){
res.send("Hello");
})
service.get("/this/is/:dynamic", function(req, res){
res.send("Hello");
})
service.start(3001).then((server) => {
console.log("server has been started on port 3001")
});
I initially used find-my-way npm package for मुनीम (Muneem) framework. But then I realized that lookup for static URLs is comparitively slower. Hence I develop this library. If you notice, I tried to keep the naming convention and syntaxes common wherever possible to reduce the time to switch from one library to another and to keep learning curve smaller.
FAQs
Amazing fast multipurpose simple to use web/ HTTP router
The npm package anumargak receives a total of 39 weekly downloads. As such, anumargak popularity was classified as not popular.
We found that anumargak 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
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.