Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added connect middleware back to percolator.

  • Loading branch information...
commit 1558015b1121567e508d681e55530756db73a43e 1 parent b930ce5
@cainus authored
View
117 lib/Percolator.js
@@ -14,9 +14,11 @@ var fs = require('fs');
var Server = require('oneone');
var hyperjsonBrowser = require('hyperjson-browser');
var urlgrey = require('urlgrey');
+var connect = require('connect');
Percolator = function(options){
+ this.connectApp = connect();
options = options || {};
this.options = options;
this.options.port = this.options.port || 3000;
@@ -49,24 +51,27 @@ Percolator = function(options){
this.server.onRequest(function(handler, context, cb){
var req = context.req;
var res = context.res;
- req.app = that.options;
- var router = that.server.router;
- req.router = router; // TODO is there any use-case for this? delete?
- req.uri = urlgrey(protocol + '://' + req.headers.host + req.url);
- ObjectHelper(req, res);
- res.status = that.statusman.createResponder(req, res);
- var oldEnd = res.end;
- res.end = function(data, encoding){
- oldEnd.apply(res, [data, encoding]);
- that.onResponseHandler(req, res, handler);
- };
- that.onRequestHandler(req, res, handler, function(){
- var accept = req.headers.accept || '*/*';
- if (!that.mediaTypes.isAcceptable(accept)){
- that.statusman.createResponder(req, res).notAcceptable();
- } else {
- that.defaultRequestHandler(req, res, handler, cb);
- }
+ that.connectApp(req, res, function(){
+ req.app = that.options;
+ var router = that.server.router;
+ req.router = router; // TODO is there any use-case for this? delete?
+ req.uri = urlgrey(protocol + '://' + req.headers.host + req.url);
+ ObjectHelper(req, res);
+ res.status = that.statusman.createResponder(req, res);
+ var oldEnd = res.end;
+ res.end = function(data, encoding){
+ oldEnd.apply(res, [data, encoding]);
+ that.onResponseHandler(req, res, handler);
+ };
+
+ that.onRequestHandler(req, res, handler, function(){
+ var accept = req.headers.accept || '*/*';
+ if (!that.mediaTypes.isAcceptable(accept)){
+ that.statusman.createResponder(req, res).notAcceptable();
+ } else {
+ that.defaultRequestHandler(req, res, handler, cb);
+ }
+ });
});
});
this._assignErrorHandlers();
@@ -84,49 +89,53 @@ Percolator = function(options){
Percolator.prototype = Object.create(EventEmitter.prototype);
+Percolator.prototype.connectMiddleware = function(middleware){
+ this.connectApp.use(middleware);
+};
+
Percolator.prototype.defaultRequestHandler = function(req, res, handler, cb){
var that = this;
req.pause();
- AuthenticateHelper(req, res, handler, function(){
- BasicAuthenticateHelper(req, res, handler, function(){
- fetchHelper(req, res, handler, function(){
- if (!!that.options.parseBody){
- req.resume();
- var ctxt = context(req, res);
- that.mediaTypes.connectMiddleware(ctxt)(req, res, function(err){
- req.body = ctxt.body;
- req.rawBody = ctxt.rawBody;
- if (!!err) {
- if (err.match(/^Parse Error:/)){
- that.statusman.createResponder(req, res).badRequest(err);
- return;
- }
- if (err === "Missing Content-Type"){
- that.statusman.createResponder(req, res).unsupportedMediaType("None provided.");
- return;
+ AuthenticateHelper(req, res, handler, function(){
+ BasicAuthenticateHelper(req, res, handler, function(){
+ fetchHelper(req, res, handler, function(){
+ if (!!that.options.parseBody){
+ req.resume();
+ var ctxt = context(req, res);
+ that.mediaTypes.connectMiddleware(ctxt)(req, res, function(err){
+ req.body = ctxt.body;
+ req.rawBody = ctxt.rawBody;
+ if (!!err) {
+ if (err.match(/^Parse Error:/)){
+ that.statusman.createResponder(req, res).badRequest(err);
+ return;
+ }
+ if (err === "Missing Content-Type"){
+ that.statusman.createResponder(req, res).unsupportedMediaType("None provided.");
+ return;
+ }
+ if (err === "Unregistered content-type."){
+ that.statusman.createResponder(req, res).unsupportedMediaType(req.headers['content-type']);
+ return;
+ }
+ console.log("post mediaTypes middleware error:");
+ console.log(err);
+ return cb(err);
+ } else {
+ cb(null, context(req, res));
}
- if (err === "Unregistered content-type."){
- that.statusman.createResponder(req, res).unsupportedMediaType(req.headers['content-type']);
- return;
- }
- console.log("post mediaTypes middleware error:");
- console.log(err);
- return cb(err);
- } else {
- cb(null, context(req, res));
- }
- });
- } else {
- req.onBody = onBodyHelper(req, res, handler, function(){
- req.onJson = onJsonHelper(req, res, handler, function(){
- req.resume();
- cb(null, context(req, res));
});
- });
- }
+ } else {
+ req.onBody = onBodyHelper(req, res, handler, function(){
+ req.onJson = onJsonHelper(req, res, handler, function(){
+ req.resume();
+ cb(null, context(req, res));
+ });
+ });
+ }
+ });
});
});
- });
};
View
92 package.json
@@ -1,41 +1,55 @@
{
- "name": "Percolator",
- "description" : "Percolator is a framework for quickly and easily building quality HTTP APIs in Node.js",
- "keywords" : ["api", "rest", "http", "resource", "hypermedia"],
- "version": "1.0.5",
- "bugs": {
- "url": "https://github.com/cainus/percolator/issues"
- },
- "scripts" : {
- "test" : "make test-coveralls"
- },
- "maintainers": [
- {
- "name": "Gregg Caines",
- "email": "gregg@caines.ca",
- "web": "http://caines.ca"
- }
- ],
- "dependencies": {
- "hyperjson-browser" : "0.0.5",
- "underscore" : "1.3.3",
- "reaper" : "0.2.3",
- "urlgrey" : "0.0.7",
- "oneone" : "0.4.5",
- "detour" : "0.16.6",
- "JSV" : "4.0.2"
- },
- "devDependencies" : {
- "mocha-lcov-reporter" : "0.0.1",
- "jscoverage" : "0.3.6",
- "coveralls": "2.0.5",
- "mockery" : "1.1.2",
- "hottap" : "1.0.0",
- "mocha" : "1.8.1",
- "should" : "1.1.0"
- },
- "engines" : {
- "node" : ">=0.8.14 < 0.9.0",
- "npm" : "1.1.65"
- }
+ "name": "Percolator",
+ "description": "Percolator is a framework for quickly and easily building quality HTTP APIs in Node.js",
+ "keywords": [
+ "api",
+ "rest",
+ "http",
+ "resource",
+ "hypermedia"
+ ],
+ "version": "1.0.5",
+ "bugs": {
+ "url": "https://github.com/cainus/percolator/issues"
+ },
+ "scripts": {
+ "test": "make test-coveralls"
+ },
+ "maintainers": [
+ "Gregg Caines <gregg@caines.ca> (http://caines.ca)"
+ ],
+ "dependencies": {
+ "hyperjson-browser": "0.0.7",
+ "underscore": "1.3.3",
+ "reaper": "0.2.3",
+ "urlgrey": "0.0.7",
+ "oneone": "0.4.5",
+ "detour": "0.16.6",
+ "JSV": "4.0.2",
+ "connect" : "2.7.4"
+ },
+ "devDependencies": {
+ "mocha-lcov-reporter": "0.0.1",
+ "jscoverage": "0.3.6",
+ "coveralls": "2.0.5",
+ "mockery": "1.1.2",
+ "hottap": "1.0.0",
+ "mocha": "1.8.1",
+ "should": "1.1.0"
+ },
+ "engines": {
+ "node": ">=0.8.14 < 0.9.0",
+ "npm": "1.1.65"
+ },
+ "main": "index.js",
+ "directories": {
+ "doc": "docs",
+ "test": "test"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/cainus/percolator.git"
+ },
+ "author": "Gregg Caines",
+ "license": "BSD"
}
View
1  test/test_fixtures/resources/index.js
@@ -9,6 +9,7 @@ exports.handler = {
.link('many', req.uri.child('many'))
.link('qstring', req.uri.child('qstring'))
.link('restricted', req.uri.child('restricted'))
+ .link('remote', req.uri.child('remote'))
.link('browser', req.uri.child('browser'))
.send();
}
View
10 test/test_fixtures/resources/remote.js
@@ -0,0 +1,10 @@
+exports.handler = {
+ GET : function(req, res){
+ res.object({})
+ .link('templatey',
+ '/intake/{app_id}?hub.challenge={challenge}',
+ {method : "GET"})
+ .link('google', 'http://google.com')
+ .send();
+ }
+};
View
5 test_server.js
@@ -43,6 +43,11 @@ var resourceDir = __dirname + '/test/test_fixtures/resources';
server.routeDirectory(resourceDir, app.resourcePath, function(err){
console.log("routed resources in " + resourceDir);
+ server.connectMiddleware(function(req, res, done){
+ console.log("hello from connect. URL: ", req.url);
+ done();
+ });
+
server.route('/inside',
{ GET : function(req, res){
res.end("muahahah!");
Please sign in to comment.
Something went wrong with that request. Please try again.