summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xsrc/nodejs/unit-http/http_server.js17
-rw-r--r--src/nodejs/unit-http/unit.cpp73
2 files changed, 63 insertions, 27 deletions
diff --git a/src/nodejs/unit-http/http_server.js b/src/nodejs/unit-http/http_server.js
index 9b7b8403..8a536cde 100755
--- a/src/nodejs/unit-http/http_server.js
+++ b/src/nodejs/unit-http/http_server.js
@@ -342,7 +342,7 @@ Server.prototype.listen = function () {
this.unit.listen();
};
-Server.prototype.run_events = function (server, req, res) {
+Server.prototype.emit_events = function (server, req, res) {
req.server = server;
res.server = server;
req.res = res;
@@ -350,18 +350,11 @@ Server.prototype.run_events = function (server, req, res) {
server.buffer = server.unit._read(req.socket.req_pointer);
- /* Important!!! setImmediate starts the next iteration in Node.js loop. */
- setImmediate(function () {
- server.emit("request", req, res);
+ server.emit("request", req, res);
- Promise.resolve().then(() => {
- req.emit("finish");
- req.emit("end");
-
- if (res.finished) {
- unit_lib.unit_response_end(res);
- }
- });
+ process.nextTick(() => {
+ req.emit("finish");
+ req.emit("end");
});
};
diff --git a/src/nodejs/unit-http/unit.cpp b/src/nodejs/unit-http/unit.cpp
index be64a59b..b233359c 100644
--- a/src/nodejs/unit-http/unit.cpp
+++ b/src/nodejs/unit-http/unit.cpp
@@ -276,12 +276,13 @@ Unit::_read(napi_env env, napi_callback_info info)
void
Unit::request_handler(nxt_unit_request_info_t *req)
{
- Unit *obj;
- napi_value socket, request, response;
- napi_value global, server_obj;
- napi_value run_events, events_res;
- napi_status status;
- napi_value events_args[3];
+ Unit *obj;
+ napi_value socket, request, response, global, server_obj;
+ napi_value emit_events, events_res, async_name, resource_object;
+ napi_status status;
+ napi_async_context async_context;
+ napi_callback_scope async_scope;
+ napi_value events_args[3];
obj = reinterpret_cast<Unit *>(req->unit->data);
@@ -328,11 +329,11 @@ Unit::request_handler(nxt_unit_request_info_t *req)
return;
}
- status = napi_get_named_property(obj->env_, server_obj, "run_events",
- &run_events);
+ status = napi_get_named_property(obj->env_, server_obj, "emit_events",
+ &emit_events);
if (status != napi_ok) {
- napi_throw_error(obj->env_, NULL, "Failed to get"
- " 'run_events' function");
+ napi_throw_error(obj->env_, NULL, "Failed to get "
+ "'emit_events' function");
return;
}
@@ -340,15 +341,57 @@ Unit::request_handler(nxt_unit_request_info_t *req)
events_args[1] = request;
events_args[2] = response;
- status = napi_call_function(obj->env_, server_obj, run_events, 3,
- events_args, &events_res);
+ status = napi_create_string_utf8(obj->env_, "unit_request_handler",
+ sizeof("unit_request_handler") - 1,
+ &async_name);
+ if (status != napi_ok) {
+ napi_throw_error(obj->env_, NULL, "Failed to create utf-8 string");
+ return;
+ }
+
+ status = napi_async_init(obj->env_, NULL, async_name, &async_context);
+ if (status != napi_ok) {
+ napi_throw_error(obj->env_, NULL, "Failed to init async object");
+ return;
+ }
+
+ status = napi_create_object(obj->env_, &resource_object);
+ if (status != napi_ok) {
+ napi_throw_error(obj->env_, NULL, "Failed to create object for "
+ "callback scope");
+ return;
+ }
+
+ status = napi_open_callback_scope(obj->env_, resource_object, async_context,
+ &async_scope);
if (status != napi_ok) {
- napi_throw_error(obj->env_, NULL, "Failed to call"
- " 'run_events' function");
+ napi_throw_error(obj->env_, NULL, "Failed to open callback scope");
return;
}
- napi_close_handle_scope(obj->env_, scope);
+ status = napi_make_callback(obj->env_, async_context, server_obj,
+ emit_events, 3, events_args, &events_res);
+ if (status != napi_ok) {
+ napi_throw_error(obj->env_, NULL, "Failed to make callback");
+ return;
+ }
+
+ status = napi_close_callback_scope(obj->env_, async_scope);
+ if (status != napi_ok) {
+ napi_throw_error(obj->env_, NULL, "Failed to close callback scope");
+ return;
+ }
+
+ status = napi_async_destroy(obj->env_, async_context);
+ if (status != napi_ok) {
+ napi_throw_error(obj->env_, NULL, "Failed to destroy async object");
+ return;
+ }
+
+ status = napi_close_handle_scope(obj->env_, scope);
+ if (status != napi_ok) {
+ napi_throw_error(obj->env_, NULL, "Failed to close handle scope");
+ }
}