Skip to content

Commit

Permalink
Support for JSONP, a bit of refactoring and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Jul 30, 2010
1 parent 6aaecb0 commit 7bce192
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
34 changes: 29 additions & 5 deletions geddy-core/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var http = require('http');
var sys = require('sys');
var fs = require('fs');

var fleegix = require('geddy-core/lib/fleegix');
var errors = require('geddy-core/lib/errors');
var session = require('geddy-core/lib/session');
var response = require('geddy-core/lib/response');
Expand All @@ -34,6 +33,12 @@ var App = function (initData) {
_queue = [];
_processing = false;

/**
* Initial handler for request/response pairs -- adds two event listners:
* 1. data listener, accumulates the request body
* 2. end listener, queues the request for processing
*
*/
this.handleReq = function (req, resp) {
// TODO: Wrap the request to avoid ad-hoc addition of body prop
// on actual request
Expand All @@ -47,33 +52,52 @@ var App = function (initData) {
});
};

/**
* Queues request/response pairs for processing, triggers processing
* if there are no outstanding requests still being processed
*/
this.queueReq = function (req, resp) {
_queue.push([req, resp]);
if (!_processing) {
this.processReq();
}
};

/**
* Pulls a request off the queue, processes it with the run function
*
*/
this.processReq = function () {
_processing = true;
var next = _queue.shift();
// If we still have request/response pairs to process, keep running
// them through
if (next) {
process.nextTick(function () {
_this.run.apply(_this, next);
});
}
// If we're done with all outstanding request/response pairs, flip off
//the flag hat tells the queueReq method is can process any new incoming
// requests
else {
_processing = false;
}
};

/**
* Called after responses end, either successfully, or with an error
* Grabs the next request/response pair to process
*/
this.nextReq = function () {
_processing = false;
this.processReq();
};

this.run = function (req, resp) {

// Set the 'currently processing req/resp vars to this pair -- these may
// be used in the case of unhandled errors to hand the user back a 500
geddy.request = req;
geddy.response = resp;

Expand All @@ -88,8 +112,8 @@ var App = function (initData) {
url = req.url;

// Get the QS params, so we can check to see if there's a method override
qs = fleegix.url.getQS(url);
qsParams = fleegix.url.qsToObject(qs);
qs = geddy.util.url.getQS(url);
qsParams = geddy.util.url.qsToObject(qs);

// The method may be overridden by the _method param
// TODO: Look for the x-http-method-override header
Expand All @@ -99,7 +123,7 @@ var App = function (initData) {
method = method.toUpperCase();

// The base path -- the router doesn't need to know about QS params
base = fleegix.url.getBase(url);
base = geddy.util.url.getBase(url);

// =====
// All the routing magic happens right here
Expand Down Expand Up @@ -194,7 +218,7 @@ var mergeParams = function (req, routeParams, qsParams) {
(req.headers['content-type'].indexOf('form-urlencoded') > -1)) {
// Deal with the retarded default encoding of spaces to plus-sign
var b = req.body.replace(/\+/g, '%20');
var bodyParams = fleegix.url.qsToObject(b, {arrayizeMulti: true});
var bodyParams = geddy.util.url.qsToObject(b, {arrayizeMulti: true});
p = geddy.util.meta.mixin(p, bodyParams);
}
return p;
Expand Down
9 changes: 6 additions & 3 deletions geddy-core/lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ Controller.prototype = new function () {
return toJson.call(content);
}
return JSON.stringify(content);
},
text: function (content) {
}
, js: function (content, params) {
return params.callback + '(' + JSON.stringify(content) + ');';
}
, text: function (content) {
if (typeof content.toString == 'function') {
return content.toString();
}
Expand Down Expand Up @@ -310,7 +313,7 @@ Controller.prototype = new function () {
this.renderTemplate(data);
}
else {
var c = this.formatters[format](data);
var c = this.formatters[format](data, this.params);
this.formatContentAndFinish(c);
}
};
Expand Down
1 change: 1 addition & 0 deletions geddy-core/lib/geddy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var geddy = new function () {
this.util.meta = require('geddy-util/lib/meta');
this.util.string = require('geddy-util/lib/string');
this.util.date = require('geddy-util/lib/date');
this.util.url = require('geddy-util/lib/url');
}();

if (typeof module != 'undefined') { module.exports = geddy; }
1 change: 1 addition & 0 deletions geddy-core/lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var response = new function () {
text: 'text/plain',
html: 'text/html',
json: 'application/json|text/json',
js: 'application/javascript|text/javascript',
xml: 'application/xml|text/xml'
};

Expand Down

0 comments on commit 7bce192

Please sign in to comment.