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.
This commit is contained in:
@@ -243,8 +243,11 @@ ServerResponse.prototype._writeBody = function(chunk, encoding, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (chunk) {
|
if (chunk) {
|
||||||
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
|
if (typeof chunk !== 'string' && !(chunk instanceof Buffer ||
|
||||||
throw new TypeError('First argument must be a string or Buffer');
|
chunk instanceof Uint8Array)) {
|
||||||
|
throw new TypeError(
|
||||||
|
'First argument must be a string, Buffer, ' +
|
||||||
|
'or Uint8Array');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof chunk === 'string') {
|
if (typeof chunk === 'string') {
|
||||||
|
|||||||
4
test/node/write_array/app.js
Normal file
4
test/node/write_array/app.js
Normal file
@@ -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);
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
require('http').createServer(function (req, res) {
|
require('http').createServer(function (req, res) {
|
||||||
res.writeHead(200, {'Content-Type': 'text/plain'})
|
res.writeHead(200, {'Content-Type': 'text/plain'})
|
||||||
.end(new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]));
|
.end(Buffer.from('buffer', 'utf8'));
|
||||||
}).listen(7080);
|
}).listen(7080);
|
||||||
|
|||||||
@@ -149,6 +149,10 @@ def test_node_application_write_buffer():
|
|||||||
|
|
||||||
assert client.get()['body'] == 'buffer', '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):
|
def test_node_application_write_callback(temp_dir):
|
||||||
client.load('write_callback')
|
client.load('write_callback')
|
||||||
|
|||||||
Reference in New Issue
Block a user