http: don't throw on `Uint8Array`s for `http.ServerResponse#write`

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

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>
pull/33386/head
Pranshu Srivastava 2020-05-03 11:02:13 +05:30 committed by Michaël Zasso
parent b533fb3508
commit 2b50cd7a6d
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
2 changed files with 26 additions and 1 deletions

View File

@ -63,6 +63,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;
@ -674,7 +675,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
throw new ERR_STREAM_NULL_VALUES();
} else if (typeof chunk === 'string') {
len = Buffer.byteLength(chunk, encoding);
} else if (chunk instanceof Buffer) {
} else if (isUint8Array(chunk)) {
len = chunk.length;
} else {
throw new ERR_INVALID_ARG_TYPE(

View File

@ -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 });
}));