Browse files

Fix the node request object to have an on() method and always process…

… callbacks asynchronously
  • Loading branch information...
1 parent cdf59a1 commit 3455ff857589358a66e36ccfac143509cecf58c3 @kriszyp kriszyp committed Aug 22, 2012
Showing with 21 additions and 17 deletions.
  1. +21 −17 jsgi/node.js
View
38 jsgi/node.js
@@ -6,28 +6,30 @@ exports.Node = function(nodeApp){
var endListeners = [];
var bodyDeferred;
var responseDeferred = defer();
- nodeApp({
+ var nodeRequest = {
headers: request.headers,
httpVersionMajor: request.version[0],
httpVersionMinor: request.version[1],
addListener: function(event, callback){
- if(event == "data"){
- when(request.body && request.body.forEach(function(data){
- callback(data);
- }), function(){
- endListeners.forEach(function(listener){
- listener();
+ process.nextTick(function(){
+ if(event == "data"){
+ when(request.body && request.body.forEach(function(data){
+ callback(data);
+ }), function(){
+ endListeners.forEach(function(listener){
+ listener();
+ });
+ endListeners = null;
});
- endListeners = null;
- });
- }
- if(event == "end"){
- if(endListeners){
- endListeners.push(callback);
- }else{
- callback();
}
- }
+ if(event == "end"){
+ if(endListeners){
+ endListeners.push(callback);
+ }else{
+ callback();
+ }
+ }
+ });
return this;
},
pause: function(){
@@ -36,7 +38,9 @@ exports.Node = function(nodeApp){
resume: function(){
}
- },
+ }
+ nodeRequest.on = nodeRequest.addListener;
+ nodeApp(nodeRequest,
{
writeHead: function(status, headers){
var write;

0 comments on commit 3455ff8

Please sign in to comment.