trek-router
Advanced tools
Comparing version 0.0.8 to 0.0.9
'use strict'; | ||
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }; | ||
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
/*! | ||
@@ -18,11 +12,9 @@ * router | ||
var _methods = require('methods'); | ||
const METHODS = ['CONNECT', 'DELETE', 'GET', 'HEAD', 'OPTIONS', 'PATCH', 'POST', 'PUT', 'TRACE']; | ||
var _methods2 = _interopRequireWildcard(_methods); | ||
const SNODE = 0; // static route | ||
const PNODE = 1; // param route | ||
const ANODE = 2; // all star route | ||
var SNODE = 0; // static route | ||
var PNODE = 1; // param route | ||
var ANODE = 2; // all star route | ||
var Node = (function () { | ||
let Node = (function () { | ||
function Node(prefix, has, handler, edges) { | ||
@@ -39,18 +31,13 @@ _classCallCheck(this, Node); | ||
_createClass(Node, [{ | ||
key: 'findEdge', | ||
value: function findEdge(c) { | ||
var i = 0; | ||
var l = this.edges.length; | ||
var e = void 0; | ||
Node.prototype.findEdge = function findEdge(c) { | ||
let i = 0; | ||
let l = this.edges.length; | ||
let e = void 0; | ||
for (; i < l; i++) { | ||
e = this.edges[i]; | ||
if (e.label.charCodeAt() === c.charCodeAt()) { | ||
return e; | ||
} | ||
} | ||
return null; | ||
for (; i < l; i++) { | ||
e = this.edges[i]; | ||
if (e.label.charCodeAt() === c.charCodeAt()) return e; | ||
} | ||
}]); | ||
return null; | ||
}; | ||
@@ -60,3 +47,3 @@ return Node; | ||
var Router = (function () { | ||
let Router = (function () { | ||
function Router() { | ||
@@ -68,3 +55,3 @@ var _this = this; | ||
this.trees = Object.create(null); | ||
_methods2['default'].forEach(function (m) { | ||
METHODS.forEach(function (m) { | ||
_this.trees[m.toUpperCase()] = new Node('', null, null, []); | ||
@@ -74,148 +61,143 @@ }); | ||
_createClass(Router, [{ | ||
key: 'add', | ||
value: function add(method, path, handler) { | ||
var i = 0, | ||
l = path.length; | ||
for (; i < l; i++) { | ||
// `:` | ||
if (path.charCodeAt(i) === 58) { | ||
this.insert(method, path.substring(0, i), null, PNODE); | ||
// `/` | ||
for (; i < l && path.charCodeAt(i) !== 47; i++) {} | ||
if (i === l) { | ||
this.insert(method, path.substring(0, i), handler, SNODE); | ||
return; | ||
} | ||
this.insert(method, path.substring(0, i), null, SNODE); | ||
// `*` | ||
} else if (path.charCodeAt(i) === 42) { | ||
this.insert(method, path.substring(0, i), handler, ANODE); | ||
Router.prototype.add = function add(method, path, handler) { | ||
var i = 0, | ||
l = path.length; | ||
for (; i < l; i++) { | ||
// `:` | ||
if (path.charCodeAt(i) === 58) { | ||
this.insert(method, path.substring(0, i), null, PNODE); | ||
// `/` | ||
for (; i < l && path.charCodeAt(i) !== 47; i++) {} | ||
if (i === l) { | ||
this.insert(method, path.substring(0, i), handler, SNODE); | ||
return; | ||
} | ||
this.insert(method, path.substring(0, i), null, SNODE); | ||
// `*` | ||
} else if (path.charCodeAt(i) === 42) { | ||
this.insert(method, path.substring(0, i), handler, ANODE); | ||
} | ||
this.insert(method, path, handler, SNODE); | ||
} | ||
}, { | ||
key: 'insert', | ||
value: function insert(method, path, handler, has) { | ||
var cn = this.trees[method]; // Current node as root | ||
var search = path; | ||
this.insert(method, path, handler, SNODE); | ||
}; | ||
while (true) { | ||
var sl = search.length; | ||
var pl = cn.prefix.length; | ||
var l = lcp(search, cn.prefix); | ||
Router.prototype.insert = function insert(method, path, handler, has) { | ||
let cn = this.trees[method]; // Current node as root | ||
let search = path; | ||
if (l === 0) { | ||
// At root node | ||
cn.label = search[0]; | ||
cn.prefix = search; | ||
cn.has = has; | ||
if (handler) { | ||
cn.handler = handler; | ||
} | ||
return; | ||
} else if (l < pl) { | ||
// Split the node | ||
var n = new Node(cn.prefix.substring(l), cn.has, cn.handler, cn.edges); | ||
cn.edges = [n]; // Add to parent | ||
while (true) { | ||
let sl = search.length; | ||
let pl = cn.prefix.length; | ||
let l = lcp(search, cn.prefix); | ||
// Reset parent node | ||
cn.label = cn.prefix[0]; | ||
cn.prefix = cn.prefix.substring(0, l); | ||
cn.has = SNODE; | ||
cn.handler = null; | ||
if (l === 0) { | ||
// At root node | ||
cn.label = search[0]; | ||
cn.prefix = search; | ||
cn.has = has; | ||
if (handler) { | ||
cn.handler = handler; | ||
} | ||
return; | ||
} else if (l < pl) { | ||
// Split the node | ||
let n = new Node(cn.prefix.substring(l), cn.has, cn.handler, cn.edges); | ||
cn.edges = [n]; // Add to parent | ||
if (l === sl) { | ||
// At parent node | ||
cn.handler = handler; | ||
} else { | ||
// Need to fork a node | ||
var _n = new Node(search.substring(l), has, handler, null); | ||
cn.edges.push(_n); | ||
} | ||
break; | ||
} else if (l < sl) { | ||
search = search.substring(l); | ||
var e = cn.findEdge(search[0]); | ||
if (e) { | ||
cn = e; | ||
} else { | ||
var n = new Node(search, has, handler, []); | ||
cn.edges.push(n); | ||
break; | ||
} | ||
// Reset parent node | ||
cn.label = cn.prefix[0]; | ||
cn.prefix = cn.prefix.substring(0, l); | ||
cn.has = SNODE; | ||
cn.handler = null; | ||
if (l === sl) { | ||
// At parent node | ||
cn.handler = handler; | ||
} else { | ||
// Node already exists | ||
if (handler) { | ||
cn.handler = handler; | ||
} | ||
// Need to fork a node | ||
let n = new Node(search.substring(l), has, handler, null); | ||
cn.edges.push(n); | ||
} | ||
break; | ||
} else if (l < sl) { | ||
search = search.substring(l); | ||
let e = cn.findEdge(search[0]); | ||
if (e) { | ||
cn = e; | ||
} else { | ||
let n = new Node(search, has, handler, []); | ||
cn.edges.push(n); | ||
break; | ||
} | ||
} else { | ||
// Node already exists | ||
if (handler) { | ||
cn.handler = handler; | ||
} | ||
break; | ||
} | ||
} | ||
}, { | ||
key: 'find', | ||
value: function find(method, path) { | ||
var cn = this.trees[method]; // Current node as root | ||
var search = path; | ||
var n = 0; // Param count | ||
var result = [null]; | ||
var params = []; | ||
}; | ||
while (true) { | ||
if (search === '' || search === cn.prefix) { | ||
// Found | ||
result[0] = cn.handler; | ||
result[1] = params; | ||
return result; | ||
} | ||
Router.prototype.find = function find(method, path) { | ||
let cn = this.trees[method]; // Current node as root | ||
let search = path; | ||
let n = 0; // Param count | ||
let result = [null]; | ||
let params = []; | ||
var pl = cn.prefix.length; | ||
var l = lcp(search, cn.prefix); | ||
while (true) { | ||
if (search === '' || search === cn.prefix) { | ||
// Found | ||
result[0] = cn.handler; | ||
result[1] = params; | ||
return result; | ||
} | ||
if (l === pl) { | ||
search = search.substring(l); | ||
switch (cn.has) { | ||
case PNODE: | ||
cn = cn.edges[0]; | ||
var i = 0; | ||
l = search.length; | ||
// `/` | ||
for (; i < l && search.charCodeAt(i) !== 47; i++) {} | ||
let pl = cn.prefix.length; | ||
let l = lcp(search, cn.prefix); | ||
params[n] = { | ||
name: cn.prefix.substring(1), | ||
value: search.substring(0, i) | ||
}; | ||
n++; | ||
if (l === pl) { | ||
search = search.substring(l); | ||
switch (cn.has) { | ||
case PNODE: | ||
cn = cn.edges[0]; | ||
var i = 0; | ||
l = search.length; | ||
// `/` | ||
for (; i < l && search.charCodeAt(i) !== 47; i++) {} | ||
search = search.substring(i); | ||
params[n] = { | ||
name: cn.prefix.substring(1), | ||
value: search.substring(0, i) | ||
}; | ||
n++; | ||
if (i === l) { | ||
// All params read | ||
continue; | ||
} | ||
break; | ||
case ANODE: | ||
params[n] = { | ||
name: cn.prefix.substring(1), | ||
value: search.substring(0, i) | ||
}; | ||
search = ''; // End search | ||
search = search.substring(i); | ||
if (i === l) { | ||
// All params read | ||
continue; | ||
} | ||
} | ||
break; | ||
case ANODE: | ||
params[n] = { | ||
name: cn.prefix.substring(1), | ||
value: search.substring(0, i) | ||
}; | ||
search = ''; // End search | ||
continue; | ||
} | ||
var e = cn.findEdge(search[0]); | ||
if (!e) { | ||
// Not found | ||
return result; | ||
} | ||
cn = e; | ||
continue; | ||
let e = cn.findEdge(search[0]); | ||
if (!e) { | ||
// Not found | ||
return result; | ||
} | ||
return result; | ||
cn = e; | ||
continue; | ||
} | ||
return result; | ||
} | ||
}]); | ||
}; | ||
@@ -228,4 +210,4 @@ return Router; | ||
var i = 0; | ||
var max = a.length; | ||
var l = b.length; | ||
let max = a.length; | ||
let l = b.length; | ||
if (l < max) { | ||
@@ -232,0 +214,0 @@ max = l; |
{ | ||
"name": "trek-router", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "A fast HTTP router router", | ||
@@ -40,5 +40,3 @@ "repository": "trekjs/router", | ||
}, | ||
"dependencies": { | ||
"methods": "^1.1.1" | ||
} | ||
"dependencies": {} | ||
} |
0
9162
186
- Removedmethods@^1.1.1
- Removedmethods@1.1.2(transitive)