http: don't throw on Uint8Arrays for http.ServerResponse#write

Don't throw errors on Uint8Arrays and added test for all
valid types.

Backport-PR-URL: https://github.com/nodejs/node/pull/33488
PR-URL: https://github.com/nodejs/node/pull/33155
Fixes: https://github.com/nodejs/node/issues/33379
Refs: https://github.com/nodejs/node/issues/29829
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Pranshu Srivastava authored and Beth Griggs committed 2020-09-15 21:39:03 +01:00
1 parent 2e77a10d9c
commit ca836344fa
3 files changed
+31 -5

No files matched your search

+5 -3
View File
@@ -60,6 +60,7 @@ const {
hideStackFrames
} = require('internal/errors');
const { validateString } = require('internal/validators');
const { isUint8Array } = require('internal/util/types');
const HIGH_WATER_MARK = getDefaultHighWaterMark();
const { CRLF, debug } = common;
@@ -649,9 +650,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
return true;
}
if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
if (!fromEnd && typeof chunk !== 'string' && !isUint8Array(chunk)) {
throw new ERR_INVALID_ARG_TYPE('first argument',
['string', 'Buffer'], chunk);
['string', 'Buffer', 'Uint8Array'], chunk);
}
if (!fromEnd && msg.connection && !msg.connection.writableCorked) {
@@ -742,7 +743,8 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
if (chunk) {
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
throw new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
throw new ERR_INVALID_ARG_TYPE(
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
}
if (!this._header) {
if (typeof chunk === 'string')
+2 -2
View File
@@ -80,7 +80,7 @@ assert.throws(() => {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The first argument must be of type string or an instance of ' +
'Buffer. Received undefined'
'Buffer or Uint8Array. Received undefined'
});
assert.throws(() => {
@@ -90,7 +90,7 @@ assert.throws(() => {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The first argument must be of type string or an instance of ' +
'Buffer. Received type number (1)'
'Buffer or Uint8Array. Received type number (1)'
});
// addTrailers()
@@ -0,0 +1,24 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const httpServer = http.createServer(common.mustCall(function(req, res) {
httpServer.close();
assert.throws(() => {
res.write(['Throws.']);
}, {
code: 'ERR_INVALID_ARG_TYPE'
});
// should not throw
res.write('1a2b3c');
// should not throw
res.write(new Uint8Array(1024));
// should not throw
res.write(Buffer.from('1'.repeat(1024)));
res.end();
}));
httpServer.listen(0, common.mustCall(function() {
http.get({ port: this.address().port });
}));