Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a connect adapter

Version Bump to 0.1.0
  • Loading branch information...
commit 42b1814727d079c303d0586fa9456395839350c5 1 parent ada51cc
@hassox hassox authored
View
49 README.rdoc
@@ -8,7 +8,7 @@ Simple HTTP routing in Javascript.
Here is an example of simple recognition run from Node.js.
- require('./lib/sherpa')
+ var Sherpa = require('./lib/sherpa')
var sherpa = new Sherpa.Router();
sherpa.add('/test').to('testRoute')
sherpa.recognize('/test')
@@ -64,7 +64,7 @@ And...
Would return
undefined
-
+
But if you change the extension to one supported by the <tt>matchesWith</tt> specified,
sherpa.recognize('/test2/testing.html')
@@ -83,8 +83,8 @@ Optional parts of the path are marked with <tt>(</tt> and <tt>)</tt>. For exampl
sherpa.add('/test(/:variable)').to('testing')
sherpa.recognize('/test/world')
-
-Would return
+
+Would return
{
"destination": "testing",
@@ -92,12 +92,12 @@ Would return
"variable": "world"
}
}
-
-And
+
+And
sherpa.add('/test(/:variable)').to('testing')
sherpa.recognize('/test')
-Would return only
+Would return only
{
"destination": "testing",
"params": { }
@@ -107,18 +107,18 @@ You can also specify other parts of the request to match on using <tt>conditions
sherpa.add('/test', {conditions: {method: 'GET'}}).to('testing')
sherpa.recognize('/test', {method: 'GET'})
-
+
{
"destination": "testing",
"params": {}
}
But
-
+
sherpa.recognize('/test', {method: 'POST'})
-
+
Will just return
-
+
undefined
You can also use regex here, such as:
@@ -131,7 +131,7 @@ Generating is easy too.
sherpa.add('/test2/:test2.:format').name('test')
sherpa.generate('test', {test2: 'variable', format: 'html'})
-
+
Would return
"/test2/variable.html"
@@ -144,12 +144,35 @@ Would return
"/test2/variable.html?page=1"
+== Connect
+
+Sherpa has an interface to allow it to work with the Connect framework.
+
+ var Sherpa = require('sherpa/connect'),
+ router = new Sherpa.Connect();
+
+In addition to the normal methods avaliable with the router, there are also a number of helpers defined.
+
+ GET, POST, PUT, DELETE, HEAD, OPTIONS, ANY
+
+ router.GET( "/some url", function(req,resp,next){ doSomething(); });
+ router.POST( "/some url", function(req,resp,next){ doSomething(); });
+ router.GET( "/some url", function(req,resp,next){ doSomething(); });
+ router.DELETE( "/some url", function(req,resp,next){ doSomething(); });
+ router.HEAD( "/some url", function(req,resp,next){ doSomething(); });
+ router.ANY( "/", function(req, resp, next){ pressTheAnyKey(); });
+
+To connect the router into the Connect middleware system:
+
+ var server = Connect.createServer();
+ server.use("/mount_point", router.connect())
+
== Node.js
To use this within Node.js, simply use the Node.js interface. Here is a simple example:
require('../lib/sherpa')
-
+
var sys = require("sys"), http = require("http");
http.createServer(new Sherpa.interfaces.NodeJs([
View
26 lib/sherpa.js
@@ -1,3 +1,5 @@
+var sys = require('sys');
+
RegExp.escape = function(text) {
if (!arguments.callee.sRE) {
var specials = [
@@ -24,7 +26,7 @@ Sherpa = {
var splitUri = uri.split(regex);
this.compiledUri = [];
-
+
for (var splitUriIdx = 0; splitUriIdx != splitUri.length; splitUriIdx++) {
if (splitUri[splitUriIdx].substring(0, 1) == ':') {
this.compiledUri.push("params['" + splitUri[splitUriIdx].substring(1) + "']");
@@ -32,11 +34,11 @@ Sherpa = {
this.compiledUri.push("'" + splitUri[splitUriIdx] + "'");
}
}
-
+
this.compiledUri = this.compiledUri.join('+');
-
+
this.groups = [];
-
+
for (var splitIndex = 0; splitIndex < splitUri.length; splitIndex++) {
var part = splitUri[splitIndex];
if (part == '/') {
@@ -53,10 +55,10 @@ Sherpa = {
this.variableNames = [];
var paths = [""];
var chars = uri.split('');
-
+
var startIndex = 0;
var endIndex = 1;
-
+
for (var charIndex in chars) {
var c = chars[charIndex];
if (c == '(') {
@@ -76,7 +78,7 @@ Sherpa = {
}
}
}
-
+
this.partial = false;
this.paths = [];
for (var pathsIdx = 0; pathsIdx != paths.length; pathsIdx++) {
@@ -170,7 +172,7 @@ Sherpa.Node.prototype = {
matchedParams.push(match[matchIdx]);
}
}
-
+
var newParams = params.concat(matchedParams);
matchedIndex = match.shift().length;
var resplitParts = wholePath.substring(matchedIndex).split('/');
@@ -179,7 +181,7 @@ Sherpa.Node.prototype = {
if (potentialMatch) return potentialMatch;
}
}
- }
+ }
if (this.lookup[parts[0]]) {
var potentialMatch = this.lookup[parts[0]].find(parts.slice(1, parts.size), request, params);
if (potentialMatch) return potentialMatch;
@@ -239,7 +241,7 @@ Sherpa.Node.prototype = {
if (currentNodes.length == 1 && currentNodes[0] === this) {
currentNodes = [this.addRequestNode()];
}
-
+
for (var currentNodeIndex = 0; currentNodeIndex != currentNodes.length; currentNodeIndex++) {
var currentNode = currentNodes[currentNodeIndex];
if (!currentNode.requestMethod) {
@@ -248,7 +250,7 @@ Sherpa.Node.prototype = {
var masterPosition = requestMethods.indexOf(method);
var currentPosition = requestMethods.indexOf(currentNode.requestMethod);
-
+
if (masterPosition == currentPosition) {
if (requestConditions[method].compile) {
currentNodes[currentNodeIndex] = currentNodes[currentNodeIndex].addLinear(requestConditions[method], 0);
@@ -357,7 +359,7 @@ Sherpa.Route.prototype = {
if (path) break;
}
}
-
+
if (path) {
path = encodeURI(path);
var query = '';
View
63 lib/sherpa/connect.js
@@ -0,0 +1,63 @@
+require('../sherpa');
+
+module.exports = Sherpa;
+
+var url = require('url');
+
+Sherpa.Connect = function () {
+ Sherpa.Router.call(this)
+};
+
+Sherpa.Connect.prototype = new Sherpa.Router();
+var proto = Sherpa.Connect.prototype;
+
+var sys = require('sys');
+function addMethodRoute(_method, path, opts, fn){
+ if (!fn){
+ fn = opts;
+ opts = undefined;
+ }
+
+ opts = opts || {};
+ opts.conditions = opts.conditions || {};
+ opts.conditions.method = _method;
+ return this.add(path, opts);
+}
+
+function addRouteMethod(_method){
+ proto[_meth] = function(path, opts){
+ return addMethodRoute.call(this, _method, path, opts);
+ }
+}
+
+var _methods = ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD'];
+for(i in _methods){
+ var _meth = _methods[i];
+ addRouteMethod(_meth);
+}
+
+proto.ANY = function(path, opts){
+ if (!fn){
+ fn = opts;
+ opts = undefined;
+ }
+
+ opts = opts || {};
+ return this.add(path);
+}
+
+// The connector provides the standard Connect interface
+proto.connect = function() {
+ var self = this
+ return function(httpRequest, httpResponse, next) {
+ var requestUrl = url.parse(httpRequest.url);
+ var response = self.recognize(requestUrl.pathname, httpRequest);
+ if (response) {
+ httpRequest.sherpaResponse = response;
+ response.destination(httpRequest, httpResponse, next);
+ } else {
+ next();
+ }
+ }
+}
+
View
2  lib/sherpa/interfaces.js
@@ -2,7 +2,7 @@ var sys = require('sys');
var sint = Sherpa.interfaces = Sherpa.interfaces || {};
var interfaces = {};
-["NodeJs", "Jsgi"].forEach(function(interface){
+["NodeJs", "Jsgi", "Connect"].forEach(function(interface){
sint.__defineGetter__(interface, function(){
return loadInterfaceFor(interface)
});
View
4 lib/sherpa/nodejs.js
@@ -12,8 +12,8 @@ Sherpa.NodeJs.prototype = {
listener: function() {
var router = new Sherpa.Router();
var notFound = function(req, res) {
- res.sendHeader(404, {});
- res.finish();
+ res.writeHead(404, {});
+ res.end();
}
for(var key in this.routes) {
View
14 package.json
@@ -1,8 +1,10 @@
{
- name : "sherpa",
- description : "Simple URI routing in Javascript",
- author : "Joshua Hull <joshbuddy@gmail.com>",
- version : "0.0.1",
- main : "lib/sherpa"
+ "name": "sherpa",
+ "description": "Simple URI routing in Javascript",
+ "author": "Joshua Hull <joshbuddy@gmail.com>",
+ "version": "0.1.0",
+ "directories": {
+ "lib" : "./lib/sherpa"
+ },
+ "main": "lib/sherpa"
}
-
Please sign in to comment.
Something went wrong with that request. Please try again.