Skip to content

Commit

Permalink
http: don't throw on Uint8Arrays for http.ServerResponse#write
Browse files Browse the repository at this point in the history
dont throw errors on Uint8Arrays and added test for all
valid types

Co-authored-by: Ruben Bridgewater <ruben@bridgewater.de>

fixup: remove dup

fixup: remove unused dec

fixup: add Uint8Array check in test

fixup: don't throw invalid arg error on `Uint8Array`s
  • Loading branch information
rexagod committed May 10, 2020
1 parent 339690c commit 6a4d9b2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/_http_outgoing.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
24 changes: 24 additions & 0 deletions 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 });
}));

0 comments on commit 6a4d9b2

Please sign in to comment.