Node.js: added async request execution.

This commit is contained in:
Alexander Borisov
2018-10-31 15:51:51 +03:00
parent 3b0afb1681
commit c838c3bd15
5 changed files with 181 additions and 98 deletions

View File

@@ -232,7 +232,6 @@ ServerResponse.prototype.write = function write(chunk, encoding, callback) {
ServerResponse.prototype.end = function end(chunk, encoding, callback) {
this._writeBody(chunk, encoding, callback);
unit_lib.unit_response_end(this)
this.finished = true;
@@ -290,10 +289,10 @@ function Server(requestListener) {
EventEmitter.call(this);
this.unit = new unit_lib.Unit();
this.unit.createServer();
this.unit.server = this;
this.unit.createServer();
this.socket = Socket;
this.request = ServerRequest;
this.response = ServerResponse;
@@ -318,10 +317,32 @@ Server.prototype.listen = function () {
this.unit.listen();
};
Server.prototype.run_events = function (server, req, res) {
/* Important!!! setImmediate starts the next iteration in Node.js loop. */
setImmediate(function () {
server.emit("request", req, res);
Promise.resolve().then(() => {
let buf = server.unit._read(req.socket.req_pointer);
if (buf.length != 0) {
req.emit("data", buf);
}
req.emit("end");
});
Promise.resolve().then(() => {
if (res.finished) {
unit_lib.unit_response_end(res);
}
});
});
};
function connectionListener(socket) {
}
module.exports = {
STATUS_CODES: http.STATUS_CODES,
Server,