From 1443d623d4b5d59e4463e025b4125be9a5aa3436 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Fri, 17 Nov 2023 17:27:31 +0000 Subject: Node.js: ServerResponse.flushHeaders() implemented. This closes #1006 issue on GitHub. Reviewed-by: Andrew Clayton --- src/nodejs/unit-http/http_server.js | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/nodejs') diff --git a/src/nodejs/unit-http/http_server.js b/src/nodejs/unit-http/http_server.js index 0f00b47f..8eb13d7f 100644 --- a/src/nodejs/unit-http/http_server.js +++ b/src/nodejs/unit-http/http_server.js @@ -138,6 +138,10 @@ ServerResponse.prototype.removeHeader = function removeHeader(name) { } }; +ServerResponse.prototype.flushHeaders = function flushHeaders() { + this._sendHeaders(); +}; + ServerResponse.prototype._removeHeader = function _removeHeader(lc_name) { let entry = this.headers[lc_name]; let name_len = Buffer.byteLength(entry[0] + "", 'latin1'); -- cgit From a1e00b4e28d56365b4b5cc4aa44185c4b53f5c33 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Tue, 16 Jan 2024 15:37:07 +0000 Subject: White space formatting fixes Closes: --- src/nodejs/unit-http/websocket_connection.js | 8 ++++---- src/nodejs/unit-http/websocket_request.js | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'src/nodejs') diff --git a/src/nodejs/unit-http/websocket_connection.js b/src/nodejs/unit-http/websocket_connection.js index 4eccf6bf..c04075d7 100644 --- a/src/nodejs/unit-http/websocket_connection.js +++ b/src/nodejs/unit-http/websocket_connection.js @@ -36,11 +36,11 @@ var idCounter = 0; function WebSocketConnection(socket, extensions, protocol, maskOutgoingPackets, config) { this._debug = utils.BufferingLogger('websocket:connection', ++idCounter); this._debug('constructor'); - + if (this._debug.enabled) { instrumentSocketForDebugging(this, socket); } - + // Superclass Constructor EventEmitter.call(this); @@ -432,8 +432,8 @@ WebSocketConnection.prototype.processFrame = function(frame) { // logic to emit the ping frame: this is only done when a listener is known to exist // Expose a function allowing the user to override the default ping() behavior var cancelled = false; - var cancel = function() { - cancelled = true; + var cancel = function() { + cancelled = true; }; this.emit('ping', cancel, frame.binaryPayload); diff --git a/src/nodejs/unit-http/websocket_request.js b/src/nodejs/unit-http/websocket_request.js index d84e428b..450ab629 100644 --- a/src/nodejs/unit-http/websocket_request.js +++ b/src/nodejs/unit-http/websocket_request.js @@ -247,7 +247,7 @@ WebSocketRequest.prototype.parseCookies = function(str) { WebSocketRequest.prototype.accept = function(acceptedProtocol, allowedOrigin, cookies) { this._verifyResolution(); - + // TODO: Handle extensions var protocolFullCase; @@ -418,7 +418,7 @@ WebSocketRequest.prototype.accept = function(acceptedProtocol, allowedOrigin, co // if (negotiatedExtensions) { // response += 'Sec-WebSocket-Extensions: ' + negotiatedExtensions.join(', ') + '\r\n'; // } - + // Mark the request resolved now so that the user can't call accept or // reject a second time. this._resolved = true; @@ -447,12 +447,12 @@ WebSocketRequest.prototype.accept = function(acceptedProtocol, allowedOrigin, co WebSocketRequest.prototype.reject = function(status, reason, extraHeaders) { this._verifyResolution(); - + // Mark the request resolved now so that the user can't call accept or // reject a second time. this._resolved = true; this.emit('requestResolved', this); - + if (typeof(status) !== 'number') { status = 403; } -- cgit From 6452ca111c71188ab2813c763e6a0e86b48fbd56 Mon Sep 17 00:00:00 2001 From: Andrei Zeliankou Date: Thu, 25 Jan 2024 12:49:47 +0000 Subject: Node.js: fixed "httpVersion" variable format According to the Node.js documenation this variable should only include numbering scheme. Thanks to @dbit-xia. Closes: https://github.com/nginx/unit/issues/1085 --- src/nodejs/unit-http/unit.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/nodejs') diff --git a/src/nodejs/unit-http/unit.cpp b/src/nodejs/unit-http/unit.cpp index 7912d0ac..7d9395bb 100644 --- a/src/nodejs/unit-http/unit.cpp +++ b/src/nodejs/unit-http/unit.cpp @@ -581,6 +581,7 @@ Unit::get_server_object() void Unit::create_headers(nxt_unit_request_info_t *req, napi_value request) { + char *p; uint32_t i; napi_value headers, raw_headers; napi_status status; @@ -602,7 +603,12 @@ Unit::create_headers(nxt_unit_request_info_t *req, napi_value request) set_named_property(request, "headers", headers); set_named_property(request, "rawHeaders", raw_headers); - set_named_property(request, "httpVersion", r->version, r->version_length); + + // trim the "HTTP/" protocol prefix + p = (char *) nxt_unit_sptr_get(&r->version); + p += 5; + + set_named_property(request, "httpVersion", create_string_latin1(p, 3)); set_named_property(request, "method", r->method, r->method_length); set_named_property(request, "url", r->target, r->target_length); -- cgit From fbeb2065b180e2376088387ee150d3975dc08cd5 Mon Sep 17 00:00:00 2001 From: Gabor Javorszky Date: Wed, 14 Feb 2024 18:16:01 +0000 Subject: fix: Take options as well as requestListener (#1091) * Take options as well as requestListener Unit-http have not kept up with the signature of nodejs's http package development. Nodejs allows an optional `options` object to be passed to the `createServer` function, we didn't. This resulted in function signature errors when user code that did make use of the options arg tried to call unit's replaced function. This change changes the signature to be more in line with how nodejs does it discarding it and printing a message to stdout. * Add test file to start node application with options * Add changes to docs/changes.xml Closes: https://github.com/nginx/unit/issues/1043 --- src/nodejs/unit-http/http.js | 4 ++-- src/nodejs/unit-http/http_server.js | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'src/nodejs') diff --git a/src/nodejs/unit-http/http.js b/src/nodejs/unit-http/http.js index d298a35f..60b8004f 100644 --- a/src/nodejs/unit-http/http.js +++ b/src/nodejs/unit-http/http.js @@ -11,8 +11,8 @@ const { ServerResponse, } = require('./http_server'); -function createServer (requestHandler) { - return new Server(requestHandler); +function createServer (options, requestHandler) { + return new Server(options, requestHandler); } const http = require("http") diff --git a/src/nodejs/unit-http/http_server.js b/src/nodejs/unit-http/http_server.js index 8eb13d7f..b78f309a 100644 --- a/src/nodejs/unit-http/http_server.js +++ b/src/nodejs/unit-http/http_server.js @@ -5,6 +5,7 @@ 'use strict'; +const { stderr } = require('process'); const EventEmitter = require('events'); const http = require('http'); const util = require('util'); @@ -413,7 +414,14 @@ ServerRequest.prototype._read = function _read(n) { }; -function Server(requestListener) { +function Server(options, requestListener) { + if (typeof options === 'function') { + requestListener = options; + options = {}; + } else { + stderr.write("http.Server constructor was called with unsupported options, using default settings\n"); + } + EventEmitter.call(this); this.unit = new unit_lib.Unit(); -- cgit From 756feafd541160e97edfb6faf13e9942271629ce Mon Sep 17 00:00:00 2001 From: Dan Callahan Date: Mon, 19 Feb 2024 12:22:05 +0000 Subject: Node.js: Use console.warn instead of stderr.write Functionally identical, but marginally more idiomatic. Refines: fbeb2065b180e2376088387ee150d3975dc08cd5 --- src/nodejs/unit-http/http_server.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/nodejs') diff --git a/src/nodejs/unit-http/http_server.js b/src/nodejs/unit-http/http_server.js index b78f309a..4e1c190e 100644 --- a/src/nodejs/unit-http/http_server.js +++ b/src/nodejs/unit-http/http_server.js @@ -5,7 +5,6 @@ 'use strict'; -const { stderr } = require('process'); const EventEmitter = require('events'); const http = require('http'); const util = require('util'); @@ -419,7 +418,7 @@ function Server(options, requestListener) { requestListener = options; options = {}; } else { - stderr.write("http.Server constructor was called with unsupported options, using default settings\n"); + console.warn("http.Server constructor was called with unsupported options, using default settings"); } EventEmitter.call(this); -- cgit From d24ae5a9a4b1140695e027087e72dcfdeb484ec0 Mon Sep 17 00:00:00 2001 From: Gabor Javorszky Date: Thu, 7 Dec 2023 15:07:24 +0000 Subject: Add additional replace rules for node:* modules In that particular issue the compiled nuxt files end up importing the http module as node:http rather than http only. This bypasses unit's custom loader implementation which only check for the http or unit-http modules, and their websocket counterparts. This changeset adds replace sources for both the node:http and node:websocket import signatures. Closes: https://github.com/nginx/unit/issues/1013 Reviewed-by: Andrew Clayton --- src/nodejs/unit-http/loader.js | 2 ++ src/nodejs/unit-http/loader.mjs | 2 ++ 2 files changed, 4 insertions(+) (limited to 'src/nodejs') diff --git a/src/nodejs/unit-http/loader.js b/src/nodejs/unit-http/loader.js index e5aa3558..849df3d1 100644 --- a/src/nodejs/unit-http/loader.js +++ b/src/nodejs/unit-http/loader.js @@ -11,10 +11,12 @@ if (module.parent && module.parent.id === "internal/preload") { Module.prototype.require = function (id) { switch(id) { case "http": + case "node:http": case "unit-http": return http case "websocket": + case "node:websocket": case "unit-http/websocket": return websocket } diff --git a/src/nodejs/unit-http/loader.mjs b/src/nodejs/unit-http/loader.mjs index 83985b0f..01fa2920 100644 --- a/src/nodejs/unit-http/loader.mjs +++ b/src/nodejs/unit-http/loader.mjs @@ -2,6 +2,7 @@ export async function resolve(specifier, context, defaultResolver) { switch (specifier) { case "websocket": + case "node:websocket": return { url: new URL("./websocket.js", import.meta.url).href, format: "commonjs", @@ -9,6 +10,7 @@ export async function resolve(specifier, context, defaultResolver) { } case "http": + case "node:http": return { url: new URL("./http.js", import.meta.url).href, format: "commonjs", -- cgit