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