Skip to content

Commit

Permalink
Properly handle asynchronous bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Jul 27, 2010
1 parent 86f7445 commit 4c11401
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
12 changes: 7 additions & 5 deletions lib/jsgi/media.js
Expand Up @@ -6,7 +6,7 @@
*/
exports.MediaConverter = MediaConverter;
var METHOD_HAS_BODY = require("./methods").METHOD_HAS_BODY,
onFile = require("../media").onFile,
mediaModule = require("../media"),
when = require("promised-io/promise").when;

function MediaConverter(mediaSelector, nextApp){
Expand All @@ -32,7 +32,7 @@ function MediaConverter(mediaSelector, nextApp){
request.body[parts[0].trim()] = parts[1].trim().replace(/"/g,'');
}
});
response = when(onFile(request.body), function(){
response = when(mediaModule.onFile(request.body), function(){
return nextApp(request);
}); // funnel errors to the response
}else{
Expand All @@ -53,13 +53,15 @@ function MediaConverter(mediaSelector, nextApp){
}
if(typeof body.callNextApp === "function"){
var response = body.callNextApp(nextApp);
}else{
request.body= body;
}
}

}
var response = response || nextApp(request);
var response = response || when(body, function(body){
request.body = body
return nextApp(request);
});


request.variedOn = ""; // let middleware add varied on headers to the request so we can efficiently add them all at once

Expand Down
6 changes: 4 additions & 2 deletions lib/media.js
Expand Up @@ -5,6 +5,7 @@
*/

var model = require("perstore/model"),
when = require("promised-io/promise").when,
Media = exports.Media = function(media){
Media.instances[media.mediaType] = media;
};
Expand Down Expand Up @@ -102,10 +103,11 @@ exports.getColumnsToExport = function(request, item){

exports.forEachableToString = function(input){
var strings = [];
input.forEach(function(block){
return when(input.forEach(function(block){
strings.push(block);
}), function(){
return strings.join("");
});
return strings.join("");
};

exports.onFile = function(file){
Expand Down
2 changes: 1 addition & 1 deletion lib/media/json.js
Expand Up @@ -13,7 +13,7 @@ Media({
},
serialize: StreamingSerializer(JSON.stringify),
deserialize: function(inputStream, request){
return JSONExt.parse(forEachableToString(inputStream));
return when(forEachableToString(inputStream), JSONExt.parse);
}
});

Expand Down
72 changes: 37 additions & 35 deletions lib/media/message/json.js
Expand Up @@ -17,44 +17,46 @@ Media({
return 0.75;
},
deserialize: function(body, request){
body = JSONExt.parse(forEachableToString(body));
body = when(forEachableToString(body), JSONExt.parse);
return {
callNextApp: function(nextApp){
if(!(body instanceof Array)){
body = [body];
}
var responses = [];
var clientConnection = getClientConnection(request);
body.forEach(function(message){
message.__proto__ = request;
if(!("to" in message)){
message.to = "";
}
var pathInfo = message.to.charAt(0) === '/' ? message.to : request.pathInfo.substring(0, request.pathInfo.lastIndexOf('/') + 1) + message.to;
while(lastPath !== pathInfo){
var lastPath = pathInfo;
pathInfo = pathInfo.replace(/\/[^\/]*\/\.\.\//,'/');
}
message.pathInfo = pathInfo;
var response = nextApp(message);
response.pathInfo = pathInfo;
if(response.body && typeof response.body.observe === "function"){
clientConnection.exportMore = true;
response.body.observe(function(message){
message.from = message.channel;
clientConnection.send(message);
});
}else{
responses.push(response);
}
});
return when(all(responses), function(responses){
return {
status: clientConnection.exportMore ? 202: 200,
headers: {},
messages: true,
body: responses
return when(body, function(body){
if(!(body instanceof Array)){
body = [body];
}
var responses = [];
var clientConnection = getClientConnection(request);
body.forEach(function(message){
message.__proto__ = request;
if(!("to" in message)){
message.to = "";
}
var pathInfo = message.to.charAt(0) === '/' ? message.to : request.pathInfo.substring(0, request.pathInfo.lastIndexOf('/') + 1) + message.to;
while(lastPath !== pathInfo){
var lastPath = pathInfo;
pathInfo = pathInfo.replace(/\/[^\/]*\/\.\.\//,'/');
}
message.pathInfo = pathInfo;
var response = nextApp(message);
response.pathInfo = pathInfo;
if(response.body && typeof response.body.observe === "function"){
clientConnection.exportMore = true;
response.body.observe(function(message){
message.from = message.channel;
clientConnection.send(message);
});
}else{
responses.push(response);
}
});
return when(all(responses), function(responses){
return {
status: clientConnection.exportMore ? 202: 200,
headers: {},
messages: true,
body: responses
}
});
});
}
};
Expand Down

0 comments on commit 4c11401

Please sign in to comment.