Skip to content

Commit e87b87d

Browse files
authored
fix: Take options as well as requestListener (nginx#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: nginx#1043
1 parent a28a297 commit e87b87d

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

docs/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ NGINX Unit updated to 1.32.0.
5353
</para>
5454
</change>
5555

56+
<change type="bugfix">
57+
<para>
58+
http.createServer() now accepts "options" argument introduced in Node.js v9.6.0, v8.12.0.
59+
</para>
60+
</change>
61+
5662
<change type="feature">
5763
<para>
5864
conditional access logging.

src/nodejs/unit-http/http.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const {
1111
ServerResponse,
1212
} = require('./http_server');
1313

14-
function createServer (requestHandler) {
15-
return new Server(requestHandler);
14+
function createServer (options, requestHandler) {
15+
return new Server(options, requestHandler);
1616
}
1717

1818
const http = require("http")

src/nodejs/unit-http/http_server.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
'use strict';
77

8+
const { stderr } = require('process');
89
const EventEmitter = require('events');
910
const http = require('http');
1011
const util = require('util');
@@ -413,7 +414,14 @@ ServerRequest.prototype._read = function _read(n) {
413414
};
414415

415416

416-
function Server(requestListener) {
417+
function Server(options, requestListener) {
418+
if (typeof options === 'function') {
419+
requestListener = options;
420+
options = {};
421+
} else {
422+
stderr.write("http.Server constructor was called with unsupported options, using default settings\n");
423+
}
424+
417425
EventEmitter.call(this);
418426

419427
this.unit = new unit_lib.Unit();

test/node/options/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require('http').createServer({}, function (req, res) {
2+
res.writeHead(200, {'Content-Length': 12, 'Content-Type': 'text/plain'})
3+
.end('Hello World\n');
4+
}).listen(8080);

test/test_node_application.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def test_node_application_basic():
2121

2222
assert_basic_application()
2323

24+
def test_node_application_options(wait_for_record):
25+
client.load('options')
26+
27+
assert_basic_application()
28+
assert wait_for_record(r'constructor was called with unsupported') is not None
29+
2430

2531
def test_node_application_loader_unit_http():
2632
client.load('loader/unit_http')

0 commit comments

Comments
 (0)