Skip to content

Commit

Permalink
Fixes to reading input
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Jan 12, 2010
1 parent b5e4dec commit 62989a2
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions lib/jsgi-node.js
Expand Up @@ -91,27 +91,28 @@ function Input( request ) {
});

this.read = function( length ) {
if ( !length ) {
if ( !requestCompletePromise._hasFired ) {
requestCompletePromise.wait();
}
}

else if ( length > inputLength ) {
waitingForLength = length;
inputPromise = new process.Promise();
inputPromise.wait();
waitingForLength = Infinity;
}

var chunk = inputBuffer.join("");
var keepInBuffer = length ? chunk.substring( length ) : "";

inputBuffer = [ keepInBuffer ];
inputLength = keepInBuffer.length;

return {
decodeToString: function() {
decodeToString: function( encoding ) {
request.setBodyEncoding( encoding );
if ( !length ) {
if ( !requestCompletePromise.hasFired ) {
requestCompletePromise.wait();
}
}

else if ( length > inputLength ) {
waitingForLength = length;
inputPromise = new process.Promise();
inputPromise.wait();
waitingForLength = Infinity;
}

var chunk = inputBuffer.join("");
var keepInBuffer = length ? chunk.substring( length ) : "";

inputBuffer = [ keepInBuffer ];
inputLength = keepInBuffer.length;

return length ? chunk.substring( 0, length ) : chunk;
}
};
Expand Down Expand Up @@ -149,8 +150,8 @@ function Response( response ) {
throw new Error("The body does not have a forEach function");
}

forEachResult = data.body.forEach( function( chunk ) {
response.sendBody( chunk );
forEachResult = data.body.forEach( function( chunk, encoding ) {
response.sendBody( chunk, encoding );
});

if ( !notDone && forEachResult && ( typeof forEachResult.then === "function" ) ) {
Expand Down

0 comments on commit 62989a2

Please sign in to comment.