diff options
author | Andrei Zeliankou <zelenkov@nginx.com> | 2023-09-26 12:49:39 +0100 |
---|---|---|
committer | Andrei Zeliankou <zelenkov@nginx.com> | 2023-09-26 12:49:39 +0100 |
commit | e0c26757740fa7974af6e6592e35b5f2e00339fe (patch) | |
tree | 78651c9a5c94ced2a57c12f03ffa97ffe6e73ed4 | |
parent | 0f630c3f604e76de370529907b51ad29dfdc4ecb (diff) | |
download | unit-e0c26757740fa7974af6e6592e35b5f2e00339fe.tar.gz unit-e0c26757740fa7974af6e6592e35b5f2e00339fe.tar.bz2 |
Node.js: response body chunk can now be a Uint8Array.
Starting from Node.js 15.0.0 the chunk parameter of the response.write()
can be a Uint8Array.
This closes #870 issue on GitHub.
-rw-r--r-- | src/nodejs/unit-http/http_server.js | 7 | ||||
-rw-r--r-- | test/node/write_array/app.js | 4 | ||||
-rw-r--r-- | test/node/write_buffer/app.js | 2 | ||||
-rw-r--r-- | test/test_node_application.py | 4 |
4 files changed, 14 insertions, 3 deletions
diff --git a/src/nodejs/unit-http/http_server.js b/src/nodejs/unit-http/http_server.js index 89964ec3..11651ed7 100644 --- a/src/nodejs/unit-http/http_server.js +++ b/src/nodejs/unit-http/http_server.js @@ -243,8 +243,11 @@ ServerResponse.prototype._writeBody = function(chunk, encoding, callback) { } if (chunk) { - if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) { - throw new TypeError('First argument must be a string or Buffer'); + if (typeof chunk !== 'string' && !(chunk instanceof Buffer || + chunk instanceof Uint8Array)) { + throw new TypeError( + 'First argument must be a string, Buffer, ' + + 'or Uint8Array'); } if (typeof chunk === 'string') { diff --git a/test/node/write_array/app.js b/test/node/write_array/app.js new file mode 100644 index 00000000..b7abb3fc --- /dev/null +++ b/test/node/write_array/app.js @@ -0,0 +1,4 @@ +require('http').createServer(function (req, res) { + res.writeHead(200, {'Content-Length': 5, 'Content-Type': 'text/plain'}) + .end(new Uint8Array(Buffer.from('array', 'utf8'))); +}).listen(7080); diff --git a/test/node/write_buffer/app.js b/test/node/write_buffer/app.js index 506e8613..72e9c600 100644 --- a/test/node/write_buffer/app.js +++ b/test/node/write_buffer/app.js @@ -1,5 +1,5 @@ require('http').createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}) - .end(new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])); + .end(Buffer.from('buffer', 'utf8')); }).listen(7080); diff --git a/test/test_node_application.py b/test/test_node_application.py index e4226535..ab8aa8f8 100644 --- a/test/test_node_application.py +++ b/test/test_node_application.py @@ -149,6 +149,10 @@ def test_node_application_write_buffer(): assert client.get()['body'] == 'buffer', 'write buffer' +def test_node_application_write_array(): + client.load('write_array') + + assert client.get()['body'] == 'array', 'write array' def test_node_application_write_callback(temp_dir): client.load('write_callback') |