Permalink
Browse files

Revert "use notFound callback, simpler code"

This reverts commit 37d8b6b.
  • Loading branch information...
1 parent 37d8b6b commit 2ff46cfe92b46f39ef8ee4caf7de39c921f6b274 @joshbuddy committed Jul 6, 2011
Showing with 161 additions and 162 deletions.
  1. +60 −55 lib/sherpa.coffee
  2. +95 −96 lib/sherpa.js
  3. +5 −11 lib/sherpa/nodejs.js
  4. +1 −0 spec/run.sh
View
@@ -19,8 +19,15 @@ root.Sherpa = class Sherpa
subparts.push part.slice(0, match[0].length)
part = part.slice(match[0].length, part.length)
subparts
- generatePaths: (path) ->
- [paths, chars, startIndex, endIndex] = [[''], path.split(''), 0, 1]
+ url: (name, params) ->
+ @routes[name].url(params)
+ add: (rawPath, opts) ->
+ paths = [""]
+ chars = rawPath.split('')
+ startIndex = 0
+ endIndex = 1
+ matchesWith = opts?.matchesWith || {}
+ routeName = opts?.name
for charIndex in [0...chars.length]
c = chars[charIndex]
switch c
@@ -34,57 +41,8 @@ root.Sherpa = class Sherpa
startIndex -= endIndex - startIndex
else
paths[pathIndex] += c for pathIndex in [startIndex...endIndex]
- 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)
+
+ pathSet = for path in paths
node = @root
variableNames = []
parts = path.split('/')
@@ -96,8 +54,55 @@ root.Sherpa = class Sherpa
else
compiledPath.push "'/'"
subparts = @findSubparts(part)
- nextNodeFn = if subparts.length == 1 then @addSimplePart else @addComplexPart
- node = node.add(nextNodeFn(subparts, compiledPath, matchesWith, variableNames))
+ 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)
if opts?.conditions?
node = node.add(new RequestMatcher(opts.conditions))
path = new Path(node, variableNames)
View
Oops, something went wrong.
View
@@ -11,37 +11,31 @@ Sherpa.NodeJs = function (routes) {
Sherpa.NodeJs.prototype = {
listener: function() {
var router = new Sherpa.Router();
- router.callback = function(req, res) {
+ var notFound = function(req, res) {
res.writeHead(404, {});
res.end();
}
for(var key in this.routes) {
if (this.routes[key][0] == 'not found') {
- router.callback = function() { this.routes[key][1](req, res) };
+ notFound = this.routes[key][1];
} else {
switch(this.routes[key].length) {
case 2:
- router.add(this.routes[key][0]).to(function(req, params) {
- this.routes[key][1](req, res, params)
- });
+ router.add(this.routes[key][0]).to(this.routes[key][1]);
break;
case 3:
- router.add(this.routes[key][0], this.routes[key][1]).to(function(req, params) {
- this.routes[key][2](req, res, params)
- });
+ router.add(this.routes[key][0]).withOptions(this.routes[key][1]).to(this.routes[key][2]);
break;
default:
throw("must be 2 or 3");
}
}
}
- router.callback = notFound
-
return function(httpRequest, httpResponse) {
var requestUrl = url.parse(httpRequest.url)
- var response = router.match(httpRequest);
+ var response = router.recognize(requestUrl.pathname, httpRequest);
if (response) {
httpRequest.sherpaResponse = response;
response.destination(httpRequest, httpResponse);
View
@@ -1,5 +1,6 @@
#!/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

0 comments on commit 2ff46cf

Please sign in to comment.