New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

koa-tree-router

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-tree-router - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

4

package.json
{
"name": "koa-tree-router",
"version": "0.0.1",
"version": "0.0.2",
"description": "A high performance koa router",
"main": "index.js",
"main": "router.js",
"scripts": {

@@ -7,0 +7,0 @@ "test": "mocha"

@@ -7,3 +7,3 @@ # Koa tree router

- Fast. Up to 12 times faster than Koa-router.
- Fast. Up to 12 times faster than Koa-router. [Benchmark](benchmark/index.js)

@@ -23,2 +23,5 @@ - Express routing using `router.get`, `router.put`, `router.post`, etc.

```JS
const Koa = require("koa");
const Router = require("koa-tree-router");
const app = new Koa();

@@ -25,0 +28,0 @@ const router = new Router();

@@ -15,8 +15,9 @@ const compose = require("koa-compose");

function Router() {
function Router(opts = {}) {
if (!(this instanceof Router)) {
return new Router();
return new Router(opts);
}
this.trees = [];
this.trees = {};
this.opts = opts;
}

@@ -54,2 +55,28 @@

Router.prototype.head = function(...arg) {
return this.on("HEAD", ...arg);
};
Router.prototype.patch = function(...arg) {
return this.on("PATCH", ...arg);
};
Router.prototype.options = function(...arg) {
return this.on("OPTIONS", ...arg);
};
Router.prototype.trace = function(...arg) {
return this.on("TRACE", ...arg);
};
Router.prototype.connect = function(...arg) {
return this.on("CONNECT", ...arg);
};
Router.prototype.all = function(...arg) {
httpMethods.forEach(method => {
this.on(method, ...arg);
});
};
Router.prototype.find = function(method, path) {

@@ -68,4 +95,20 @@ const tree = this.trees[method];

const handle = function(ctx, next) {
const {handle, params} = router.find(ctx.method, ctx.path);
const { handle, params } = router.find(ctx.method, ctx.path);
if (!handle) {
const handle405 = router.opts.onMethodNotAllowed;
if (handle405) {
for (let key in router.trees) {
if (key === ctx.method) {
continue;
}
const tree = router.trees[key];
if (tree.search(ctx.path).handle !== null) {
ctx.status = 405;
return handle405(ctx, next);
}
}
}
return next();

@@ -75,4 +118,4 @@ }

ctx.params = {};
params.forEach(({key, value}) => {
ctx.params.key = value;
params.forEach(({ key, value }) => {
ctx.params[key] = value;
});

@@ -79,0 +122,0 @@

@@ -45,2 +45,25 @@ const Koa = require("koa");

});
it("support 405 method not allowed", done => {
const resBody = { msg: "not allowed" };
const app = new Koa();
const router = new Router({
onMethodNotAllowed(ctx) {
ctx.body = resBody;
}
});
router.get("/", function(ctx) {
ctx.body = "ok";
});
app.use(router.routes());
request(app.callback())
.post("/")
.expect(405)
.end(function(_, res) {
expect(res.body).toMatchObject(resBody);
done();
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc