Remove extra event emitter object, also suppress res errors
This commit is contained in:
parent
6f46b52045
commit
27eafbbe2e
1 changed files with 27 additions and 25 deletions
52
index.js
52
index.js
|
@ -250,45 +250,44 @@ function createFastCGIHandler(options) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var stdoutPushEvent = new EventEmitter();
|
|
||||||
|
|
||||||
function stdoutPush(data) {
|
function stdoutPush(data) {
|
||||||
if(data === null) {
|
if(data === null) {
|
||||||
stdoutToEnd = true;
|
stdoutToEnd = true;
|
||||||
} else {
|
} else {
|
||||||
stdoutBuffer = Buffer.concat([stdoutBuffer, Buffer.from(data)]);
|
stdoutBuffer = Buffer.concat([stdoutBuffer, Buffer.from(data)]);
|
||||||
}
|
}
|
||||||
stdoutPushEvent.emit("ready");
|
var hpLength = hp.length;
|
||||||
|
for(var i = 0; i < hpLength; i++) {
|
||||||
|
var func = hp.shift();
|
||||||
|
if(func) func();
|
||||||
|
}
|
||||||
emulatedStdout.resume();
|
emulatedStdout.resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
function stdoutRead(n, callback) {
|
var zeroed = false;
|
||||||
if(stdoutBuffer.length == 0 && stdoutToEnd) {
|
var stdoutBuffer = Buffer.alloc(0);
|
||||||
callback(Buffer.alloc(0));
|
var stdoutToEnd = false;
|
||||||
} else if(n != 0) {
|
var hp = [];
|
||||||
var handler = function() {
|
var emulatedStdout = new stream.Readable({
|
||||||
if(n > stdoutBuffer.length && !stdoutToEnd) {
|
read: function (n) {
|
||||||
stdoutPushEvent.once("ready", handler);
|
var s = this;
|
||||||
fs.writeFileSync("/tmp/fai", "");
|
var handler = function () {
|
||||||
|
if (stdoutBuffer.length == 0) {
|
||||||
|
if (!stdoutToEnd) {
|
||||||
|
hp.push(handler);
|
||||||
|
s.pause();
|
||||||
|
} else {
|
||||||
|
s.push(null);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
var bytesToPush = Math.min(stdoutBuffer.length, n);
|
var bytesToPush = Math.min(stdoutBuffer.length, n);
|
||||||
var bufferToPush = stdoutBuffer.subarray(0, bytesToPush);
|
var bufferToPush = stdoutBuffer.subarray(0, bytesToPush);
|
||||||
stdoutBuffer = stdoutBuffer.subarray(bytesToPush);
|
stdoutBuffer = stdoutBuffer.subarray(bytesToPush);
|
||||||
callback(bufferToPush);
|
s.push(bufferToPush);
|
||||||
|
if (stdoutBuffer.length == 0 && !stdoutToEnd) s.pause();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
handler();
|
if (n != 0) handler();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var stdoutBuffer = Buffer.alloc(0);
|
|
||||||
var stdoutToEnd = false;
|
|
||||||
var emulatedStdout = new stream.Readable({
|
|
||||||
read: function (n) {
|
|
||||||
var s = this;
|
|
||||||
stdoutRead(n, function(buffer) {
|
|
||||||
s.push(buffer.length > 0 ? buffer : null);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
emulatedStdout.pause(); //Reduce backpressure
|
emulatedStdout.pause(); //Reduce backpressure
|
||||||
|
@ -480,6 +479,7 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
handler.stdout.pipe(res, {
|
handler.stdout.pipe(res, {
|
||||||
end: false
|
end: false
|
||||||
});
|
});
|
||||||
|
dataHandler = function () {}; //Prevent event listener memory leaks
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -512,10 +512,12 @@ Mod.prototype.callback = function (req, res, serverconsole, responseEnd, href, e
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
res.on("close", function() {
|
res.prependListener("close", function() {
|
||||||
if(handler.stdout) handler.stdout.unpipe(res); //Prevent server crashes with write after the end
|
if(handler.stdout) handler.stdout.unpipe(res); //Prevent server crashes with write after the end
|
||||||
});
|
});
|
||||||
|
|
||||||
|
res.on("error", function() {}); //Suppress response stream errors
|
||||||
|
|
||||||
function handlerConnection() {
|
function handlerConnection() {
|
||||||
handler.stdout.on("data", dataHandler);
|
handler.stdout.on("data", dataHandler);
|
||||||
handler.stderr.on("data", function (data) {
|
handler.stderr.on("data", function (data) {
|
||||||
|
|
Loading…
Reference in a new issue