Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added JSGI to Node adapter

  • Loading branch information...
commit 7631f250dff7f4e8009b3fd5591824b9817afffc 1 parent e4a1781
@kriszyp kriszyp authored
Showing with 65 additions and 0 deletions.
  1. +65 −0 lib/jsgi/node.js
View
65 lib/jsgi/node.js
@@ -0,0 +1,65 @@
+var when = require("../promise").when,
+ defer = require("../promise").defer;
+// takes a Node HTTP app and runs it on top of a JSGI stack
+exports.Node = function(nodeApp){
+ return function(request){
+ var endListeners = [];
+ var bodyDeferred;
+ var responseDeferred = defer();
+ nodeApp({
+ 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();
+ });
+ endListeners = null;
+ });
+ }
+ if(event == "end"){
+ if(endListeners){
+ endListeners.push(callback);
+ }else{
+ callback();
+ }
+ }
+ return this;
+ },
+ pause: function(){
+
+ },
+ resume: function(){
+
+ }
+ },
+ {
+ writeHead: function(status, headers){
+ var write;
+ bodyDeferred = defer();
+ responseDeferred.resolve({
+ status: status,
+ headers: headers,
+ body: {
+ forEach: function(callback){
+ write = callback;
+ return bodyDeferred.promise;
+ }
+ }
+ });
+ },
+ write: function(data){
+ write(data);
+ },
+ end: function(data){
+ write(data);
+ bodyDeferred.resolve();
+ }
+ });
+ return responseDeferred.promise;
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.