From 6a4d9b25de637ef7cc9edc6585e8ba5e012042c1 Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sun, 3 May 2020 11:02:13 +0530 Subject: [PATCH] http: don't throw on `Uint8Array`s for `http.ServerResponse#write` dont throw errors on Uint8Arrays and added test for all valid types Co-authored-by: Ruben Bridgewater fixup: remove dup fixup: remove unused dec fixup: add Uint8Array check in test fixup: don't throw invalid arg error on `Uint8Array`s --- lib/_http_outgoing.js | 3 ++- .../test-http-outgoing-write-types.js | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-http-outgoing-write-types.js diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 33520610bc0eb2..213bb9b9fa5433 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -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; @@ -672,7 +673,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( diff --git a/test/parallel/test-http-outgoing-write-types.js b/test/parallel/test-http-outgoing-write-types.js new file mode 100644 index 00000000000000..6257b87eea8eb1 --- /dev/null +++ b/test/parallel/test-http-outgoing-write-types.js @@ -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 }); +}));