Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use notFound callback, simpler code

  • Loading branch information...
commit 37d8b6bdac1786900e9520156f40ce3a1a9272bf 1 parent c8b6cdd
@joshbuddy authored
Showing with 162 additions and 161 deletions.
  1. +55 −60 lib/sherpa.coffee
  2. +96 −95 lib/sherpa.js
  3. +11 −5 lib/sherpa/nodejs.js
  4. +0 −1  spec/run.sh
View
115 lib/sherpa.coffee
@@ -19,15 +19,8 @@ root.Sherpa = class Sherpa
subparts.push part.slice(0, match[0].length)
part = part.slice(match[0].length, part.length)
subparts
- url: (name, params) ->
- @routes[name].url(params)
- add: (rawPath, opts) ->
- paths = [""]
- chars = rawPath.split('')
- startIndex = 0
- endIndex = 1
- matchesWith = opts?.matchesWith || {}
- routeName = opts?.name
+ generatePaths: (path) ->
+ [paths, chars, startIndex, endIndex] = [[''], path.split(''), 0, 1]
for charIndex in [0...chars.length]
c = chars[charIndex]
switch c
@@ -41,8 +34,57 @@ root.Sherpa = class Sherpa
startIndex -= endIndex - startIndex
else
paths[pathIndex] += c for pathIndex in [startIndex...endIndex]
-
- pathSet = for path in paths
+ paths
+ url: (name, params) ->
+ @routes[name].url(params)
+ addComplexPart: (subparts, compiledPath, matchesWith, variableNames) ->
+ [capturingIndicies, splittingIndicies, captures, spans] = [[], [], 0, false]
+ regexSubparts = for part in subparts
+ switch part[0]
+ when '\\' then escape(part[1].chr)
+ when ':', '*'
+ spans = true if part[0] == '*'
+ captures += 1
+ name = part.slice(1, part.length)
+ variableNames.push(name)
+ if part[0] == '*'
+ splittingIndicies.push(captures)
+ compiledPath.push "params['#{name}'].join('/')"
+ else
+ capturingIndicies.push(captures)
+ compiledPath.push "params['#{name}']"
+ if spans
+ if matchesWith[name]? then "((?:#{matchesWith[name].source}\\/?)+)" else '(.*?)'
+ else
+ "(#{(matchesWith[name]?.source || '[^/]*?')})"
+ else
+ compiledPath.push "'#{part}'"
+ escape(part)
+ regexp = new RegExp("#{regexSubparts.join('')}$")
+ if spans
+ new SpanningRegexMatcher(regexp, capturingIndicies, splittingIndicies)
+ else
+ new RegexMatcher(regexp, capturingIndicies, splittingIndicies)
+ addSimplePart: (subparts, compiledPath, matchesWith, variableNames) ->
+ part = subparts[0]
+ switch part[0]
+ when ':'
+ variableName = part.slice(1, part.length)
+ compiledPath.push "params['#{variableName}']"
+ variableNames.push(variableName)
+ if matchesWith[variableName]? then new SpanningRegexMatcher(matchesWith[variableName], [0], []) else new Variable()
+ when '*'
+ compiledPath.push "params['#{variableName}'].join('/')"
+ variableName = part.slice(1, part.length)
+ variableNames.push(variableName)
+ new Glob()
+ else
+ compiledPath.push "'#{part}'"
+ new Lookup(part)
+ add: (rawPath, opts) ->
+ matchesWith = opts?.matchesWith || {}
+ routeName = opts?.name
+ pathSet = for path in @generatePaths(rawPath)
node = @root
variableNames = []
parts = path.split('/')
@@ -54,55 +96,8 @@ root.Sherpa = class Sherpa
else
compiledPath.push "'/'"
subparts = @findSubparts(part)
- nextNode = if subparts.length == 1
- part = subparts[0]
- switch part[0]
- when ':'
- variableName = part.slice(1, part.length)
- compiledPath.push "params['#{variableName}']"
- variableNames.push(variableName)
- if matchesWith[variableName]? then new SpanningRegexMatcher(matchesWith[variableName], [0], []) else new Variable()
- when '*'
- compiledPath.push "params['#{variableName}'].join('/')"
- variableName = part.slice(1, part.length)
- variableNames.push(variableName)
- new Glob()
- else
- compiledPath.push "'#{part}'"
- new Lookup(part)
- else
- capturingIndicies = []
- splittingIndicies = []
- captures = 0
- spans = false
- regexSubparts = for part in subparts
- switch part[0]
- when '\\' then escape(part[1].chr)
- when ':', '*'
- if part[0] == '*'
- spans = true
- captures += 1
- name = part.slice(1, part.length)
- variableNames.push(name)
- if part[0] == '*'
- splittingIndicies.push(captures)
- compiledPath.push "params['#{name}'].join('/')"
- else
- capturingIndicies.push(captures)
- compiledPath.push "params['#{name}']"
- if spans
- if matchesWith[name]? then "((?:#{matchesWith[name].source}\\/?)+)" else '(.*?)'
- else
- "(#{(matchesWith[name]?.source || '[^/]*?')})"
- else
- compiledPath.push "'#{part}'"
- escape(part)
- regexp = new RegExp("#{regexSubparts.join('')}$")
- if spans
- new SpanningRegexMatcher(regexp, capturingIndicies, splittingIndicies)
- else
- new RegexMatcher(regexp, capturingIndicies, splittingIndicies)
- node = node.add(nextNode)
+ nextNodeFn = if subparts.length == 1 then @addSimplePart else @addComplexPart
+ node = node.add(nextNodeFn(subparts, compiledPath, matchesWith, variableNames))
if opts?.conditions?
node = node.add(new RequestMatcher(opts.conditions))
path = new Path(node, variableNames)
View
191 lib/sherpa.js
@@ -37,18 +37,10 @@
}
return subparts;
};
- Sherpa.prototype.url = function(name, params) {
- return this.routes[name].url(params);
- };
- Sherpa.prototype.add = function(rawPath, opts) {
- var c, captures, capturingIndicies, charIndex, chars, compiledPath, endIndex, escape, matchesWith, name, nextNode, node, part, parts, path, pathIndex, pathSet, paths, regexSubparts, regexp, route, routeName, spans, splittingIndicies, startIndex, subparts, variableName, variableNames, _ref;
- paths = [""];
- chars = rawPath.split('');
- startIndex = 0;
- endIndex = 1;
- matchesWith = (opts != null ? opts.matchesWith : void 0) || {};
- routeName = opts != null ? opts.name : void 0;
- for (charIndex = 0, _ref = chars.length; 0 <= _ref ? charIndex < _ref : charIndex > _ref; 0 <= _ref ? charIndex++ : charIndex--) {
+ Sherpa.prototype.generatePaths = function(path) {
+ var c, charIndex, chars, endIndex, pathIndex, paths, startIndex, _ref, _ref2;
+ _ref = [[''], path.split(''), 0, 1], paths = _ref[0], chars = _ref[1], startIndex = _ref[2], endIndex = _ref[3];
+ for (charIndex = 0, _ref2 = chars.length; 0 <= _ref2 ? charIndex < _ref2 : charIndex > _ref2; 0 <= _ref2 ? charIndex++ : charIndex--) {
c = chars[charIndex];
switch (c) {
case '(':
@@ -67,11 +59,98 @@
}
}
}
+ return paths;
+ };
+ Sherpa.prototype.url = function(name, params) {
+ return this.routes[name].url(params);
+ };
+ Sherpa.prototype.addComplexPart = function(subparts, compiledPath, matchesWith, variableNames) {
+ var captures, capturingIndicies, name, part, regexSubparts, regexp, spans, splittingIndicies, _ref;
+ _ref = [[], [], 0, false], capturingIndicies = _ref[0], splittingIndicies = _ref[1], captures = _ref[2], spans = _ref[3];
+ regexSubparts = (function() {
+ var _i, _len, _results;
+ _results = [];
+ for (_i = 0, _len = subparts.length; _i < _len; _i++) {
+ part = subparts[_i];
+ _results.push((function() {
+ var _ref2;
+ switch (part[0]) {
+ case '\\':
+ return escape(part[1].chr);
+ case ':':
+ case '*':
+ if (part[0] === '*') {
+ spans = true;
+ }
+ captures += 1;
+ name = part.slice(1, part.length);
+ variableNames.push(name);
+ if (part[0] === '*') {
+ splittingIndicies.push(captures);
+ compiledPath.push("params['" + name + "'].join('/')");
+ } else {
+ capturingIndicies.push(captures);
+ compiledPath.push("params['" + name + "']");
+ }
+ if (spans) {
+ if (matchesWith[name] != null) {
+ return "((?:" + matchesWith[name].source + "\\/?)+)";
+ } else {
+ return '(.*?)';
+ }
+ } else {
+ return "(" + (((_ref2 = matchesWith[name]) != null ? _ref2.source : void 0) || '[^/]*?') + ")";
+ }
+ break;
+ default:
+ compiledPath.push("'" + part + "'");
+ return escape(part);
+ }
+ })());
+ }
+ return _results;
+ })();
+ regexp = new RegExp("" + (regexSubparts.join('')) + "$");
+ if (spans) {
+ return new SpanningRegexMatcher(regexp, capturingIndicies, splittingIndicies);
+ } else {
+ return new RegexMatcher(regexp, capturingIndicies, splittingIndicies);
+ }
+ };
+ Sherpa.prototype.addSimplePart = function(subparts, compiledPath, matchesWith, variableNames) {
+ var part, variableName;
+ part = subparts[0];
+ switch (part[0]) {
+ case ':':
+ variableName = part.slice(1, part.length);
+ compiledPath.push("params['" + variableName + "']");
+ variableNames.push(variableName);
+ if (matchesWith[variableName] != null) {
+ return new SpanningRegexMatcher(matchesWith[variableName], [0], []);
+ } else {
+ return new Variable();
+ }
+ break;
+ case '*':
+ compiledPath.push("params['" + variableName + "'].join('/')");
+ variableName = part.slice(1, part.length);
+ variableNames.push(variableName);
+ return new Glob();
+ default:
+ compiledPath.push("'" + part + "'");
+ return new Lookup(part);
+ }
+ };
+ Sherpa.prototype.add = function(rawPath, opts) {
+ var compiledPath, escape, matchesWith, nextNodeFn, node, part, parts, path, pathSet, route, routeName, subparts, variableNames;
+ matchesWith = (opts != null ? opts.matchesWith : void 0) || {};
+ routeName = opts != null ? opts.name : void 0;
pathSet = (function() {
- var _i, _j, _len, _len2, _results;
+ var _i, _j, _len, _len2, _ref, _results;
+ _ref = this.generatePaths(rawPath);
_results = [];
- for (_i = 0, _len = paths.length; _i < _len; _i++) {
- path = paths[_i];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ path = _ref[_i];
node = this.root;
variableNames = [];
parts = path.split('/');
@@ -84,86 +163,8 @@
if (part === '') {} else {
compiledPath.push("'/'");
subparts = this.findSubparts(part);
- nextNode = (function() {
- if (subparts.length === 1) {
- part = subparts[0];
- switch (part[0]) {
- case ':':
- variableName = part.slice(1, part.length);
- compiledPath.push("params['" + variableName + "']");
- variableNames.push(variableName);
- if (matchesWith[variableName] != null) {
- return new SpanningRegexMatcher(matchesWith[variableName], [0], []);
- } else {
- return new Variable();
- }
- break;
- case '*':
- compiledPath.push("params['" + variableName + "'].join('/')");
- variableName = part.slice(1, part.length);
- variableNames.push(variableName);
- return new Glob();
- default:
- compiledPath.push("'" + part + "'");
- return new Lookup(part);
- }
- } else {
- capturingIndicies = [];
- splittingIndicies = [];
- captures = 0;
- spans = false;
- regexSubparts = (function() {
- var _k, _len3, _results2;
- _results2 = [];
- for (_k = 0, _len3 = subparts.length; _k < _len3; _k++) {
- part = subparts[_k];
- _results2.push((function() {
- var _ref2;
- switch (part[0]) {
- case '\\':
- return escape(part[1].chr);
- case ':':
- case '*':
- if (part[0] === '*') {
- spans = true;
- }
- captures += 1;
- name = part.slice(1, part.length);
- variableNames.push(name);
- if (part[0] === '*') {
- splittingIndicies.push(captures);
- compiledPath.push("params['" + name + "'].join('/')");
- } else {
- capturingIndicies.push(captures);
- compiledPath.push("params['" + name + "']");
- }
- if (spans) {
- if (matchesWith[name] != null) {
- return "((?:" + matchesWith[name].source + "\\/?)+)";
- } else {
- return '(.*?)';
- }
- } else {
- return "(" + (((_ref2 = matchesWith[name]) != null ? _ref2.source : void 0) || '[^/]*?') + ")";
- }
- break;
- default:
- compiledPath.push("'" + part + "'");
- return escape(part);
- }
- })());
- }
- return _results2;
- })();
- regexp = new RegExp("" + (regexSubparts.join('')) + "$");
- if (spans) {
- return new SpanningRegexMatcher(regexp, capturingIndicies, splittingIndicies);
- } else {
- return new RegexMatcher(regexp, capturingIndicies, splittingIndicies);
- }
- }
- })();
- node = node.add(nextNode);
+ nextNodeFn = subparts.length === 1 ? this.addSimplePart : this.addComplexPart;
+ node = node.add(nextNodeFn(subparts, compiledPath, matchesWith, variableNames));
}
}
if ((opts != null ? opts.conditions : void 0) != null) {
View
16 lib/sherpa/nodejs.js
@@ -11,21 +11,25 @@ Sherpa.NodeJs = function (routes) {
Sherpa.NodeJs.prototype = {
listener: function() {
var router = new Sherpa.Router();
- var notFound = function(req, res) {
+ router.callback = function(req, res) {
res.writeHead(404, {});
res.end();
}
for(var key in this.routes) {
if (this.routes[key][0] == 'not found') {
- notFound = this.routes[key][1];
+ router.callback = function() { this.routes[key][1](req, res) };
} else {
switch(this.routes[key].length) {
case 2:
- router.add(this.routes[key][0]).to(this.routes[key][1]);
+ router.add(this.routes[key][0]).to(function(req, params) {
+ this.routes[key][1](req, res, params)
+ });
break;
case 3:
- router.add(this.routes[key][0]).withOptions(this.routes[key][1]).to(this.routes[key][2]);
+ router.add(this.routes[key][0], this.routes[key][1]).to(function(req, params) {
+ this.routes[key][2](req, res, params)
+ });
break;
default:
throw("must be 2 or 3");
@@ -33,9 +37,11 @@ Sherpa.NodeJs.prototype = {
}
}
+ router.callback = notFound
+
return function(httpRequest, httpResponse) {
var requestUrl = url.parse(httpRequest.url)
- var response = router.recognize(requestUrl.pathname, httpRequest);
+ var response = router.match(httpRequest);
if (response) {
httpRequest.sherpaResponse = response;
response.destination(httpRequest, httpResponse);
View
1  spec/run.sh
@@ -1,6 +1,5 @@
#!/bin/sh
-#coffee spec/spec_generate2.js
coffee -c spec/spec_generate.coffee && node spec/spec_generate.js
coffee -c spec/spec_recognize.coffee && node spec/spec_recognize.js
Please sign in to comment.
Something went wrong with that request. Please try again.