koa-tree-router
Advanced tools
Comparing version 0.3.0 to 0.4.0
{ | ||
"name": "koa-tree-router", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A high performance koa router", | ||
@@ -5,0 +5,0 @@ "main": "router.js", |
206
router.js
@@ -16,127 +16,103 @@ const compose = require("koa-compose"); | ||
function Router(opts = {}) { | ||
if (!(this instanceof Router)) { | ||
return new Router(opts); | ||
class Router { | ||
constructor(opts = {}) { | ||
if (!(this instanceof Router)) { | ||
return new Router(opts); | ||
} | ||
if (opts.prefix && opts.prefix[0] !== "/") { | ||
throw new Error("prefix must begin with '/' in path"); | ||
} | ||
this.trees = {}; | ||
this.opts = opts; | ||
} | ||
if (opts.prefix && opts.prefix[0] !== "/") { | ||
throw new Error("prefix must begin with '/' in path"); | ||
on(method, path, ...handle) { | ||
if (path[0] !== "/") { | ||
throw new Error("path must begin with '/' in path"); | ||
} | ||
if (!this.trees[method]) { | ||
this.trees[method] = new Node(); | ||
} | ||
if (this.opts.prefix) { | ||
path = this.opts.prefix + path; | ||
} | ||
this.trees[method].addRoute(path, handle); | ||
return this; | ||
} | ||
this.trees = {}; | ||
this.opts = opts; | ||
} | ||
Router.prototype.on = function(method, path, ...handle) { | ||
if (path[0] !== "/") { | ||
throw new Error("path must begin with '/' in path"); | ||
get(...arg) { | ||
return this.on("GET", ...arg); | ||
} | ||
if (!this.trees[method]) { | ||
this.trees[method] = new Node(); | ||
put(...arg) { | ||
return this.on("PUT", ...arg); | ||
} | ||
if (this.opts.prefix) { | ||
path = this.opts.prefix + path; | ||
post(...arg) { | ||
return this.on("POST", ...arg); | ||
} | ||
this.trees[method].addRoute(path, handle); | ||
return this; | ||
}; | ||
Router.prototype.get = function(...arg) { | ||
return this.on("GET", ...arg); | ||
}; | ||
Router.prototype.put = function(...arg) { | ||
return this.on("PUT", ...arg); | ||
}; | ||
Router.prototype.post = function(...arg) { | ||
return this.on("POST", ...arg); | ||
}; | ||
Router.prototype.delete = function(...arg) { | ||
return this.on("DELETE", ...arg); | ||
}; | ||
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); | ||
}); | ||
return this; | ||
}; | ||
Router.prototype.find = function(method, path) { | ||
const tree = this.trees[method]; | ||
if (tree) { | ||
return tree.search(path); | ||
delete(...arg) { | ||
return this.on("DELETE", ...arg); | ||
} | ||
return NOT_FOUND; | ||
}; | ||
Router.prototype.routes = Router.prototype.middleware = function() { | ||
const router = this; | ||
const handle = function(ctx, next) { | ||
const { handle, params } = router.find(ctx.method, ctx.path); | ||
if (!handle) { | ||
const handle405 = router.opts.onMethodNotAllowed; | ||
if (handle405) { | ||
const allowList = []; | ||
// Search for allowed methods | ||
for (let key in router.trees) { | ||
if (key === ctx.method) { | ||
continue; | ||
head(...arg) { | ||
return this.on("HEAD", ...arg); | ||
} | ||
patch(...arg) { | ||
return this.on("PATCH", ...arg); | ||
} | ||
options(...arg) { | ||
return this.on("OPTIONS", ...arg); | ||
} | ||
trace(...arg) { | ||
return this.on("TRACE", ...arg); | ||
} | ||
connect(...arg) { | ||
return this.on("CONNECT", ...arg); | ||
} | ||
all(...arg) { | ||
httpMethods.forEach(method => { | ||
this.on(method, ...arg); | ||
}); | ||
return this; | ||
} | ||
find(method, path) { | ||
const tree = this.trees[method]; | ||
if (tree) { | ||
return tree.search(path); | ||
} | ||
return NOT_FOUND; | ||
} | ||
routes() { | ||
const router = this; | ||
const handle = function(ctx, next) { | ||
const { handle, params } = router.find(ctx.method, ctx.path); | ||
if (!handle) { | ||
const handle405 = router.opts.onMethodNotAllowed; | ||
if (handle405) { | ||
const allowList = []; | ||
// Search for allowed methods | ||
for (let key in router.trees) { | ||
if (key === ctx.method) { | ||
continue; | ||
} | ||
const tree = router.trees[key]; | ||
if (tree.search(ctx.path).handle !== null) { | ||
allowList.push(key); | ||
} | ||
} | ||
const tree = router.trees[key]; | ||
if (tree.search(ctx.path).handle !== null) { | ||
allowList.push(key); | ||
} | ||
ctx.status = 405; | ||
ctx.set("Allow", allowList.join(", ")); | ||
return handle405(ctx, next); | ||
} | ||
ctx.status = 405; | ||
ctx.set("Allow", allowList.join(", ")); | ||
return handle405(ctx, next); | ||
return next(); | ||
} | ||
ctx.params = {}; | ||
params.forEach(({ key, value }) => { | ||
ctx.params[key] = value; | ||
}); | ||
return compose(handle)(ctx, next); | ||
}; | ||
return handle; | ||
} | ||
middleware() { | ||
return this.routes(); | ||
} | ||
} | ||
return next(); | ||
} | ||
ctx.params = {}; | ||
params.forEach(({ key, value }) => { | ||
ctx.params[key] = value; | ||
}); | ||
return compose(handle)(ctx, next); | ||
}; | ||
return handle; | ||
}; | ||
module.exports = Router; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
586
21492