Skip to content

Commit ca83634

Browse files
rexagodBethGriggs
authored andcommitted
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: #33488 PR-URL: #33155 Fixes: #33379 Refs: #29829 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2e77a10 commit ca83634

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

lib/_http_outgoing.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const {
6060
hideStackFrames
6161
} = require('internal/errors');
6262
const { validateString } = require('internal/validators');
63+
const { isUint8Array } = require('internal/util/types');
6364

6465
const HIGH_WATER_MARK = getDefaultHighWaterMark();
6566
const { CRLF, debug } = common;
@@ -649,9 +650,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
649650
return true;
650651
}
651652

652-
if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
653+
if (!fromEnd && typeof chunk !== 'string' && !isUint8Array(chunk)) {
653654
throw new ERR_INVALID_ARG_TYPE('first argument',
654-
['string', 'Buffer'], chunk);
655+
['string', 'Buffer', 'Uint8Array'], chunk);
655656
}
656657

657658
if (!fromEnd && msg.connection && !msg.connection.writableCorked) {
@@ -742,7 +743,8 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
742743

743744
if (chunk) {
744745
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
745-
throw new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
746+
throw new ERR_INVALID_ARG_TYPE(
747+
'chunk', ['string', 'Buffer', 'Uint8Array'], chunk);
746748
}
747749
if (!this._header) {
748750
if (typeof chunk === 'string')

test/parallel/test-http-outgoing-proto.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ assert.throws(() => {
8080
code: 'ERR_INVALID_ARG_TYPE',
8181
name: 'TypeError',
8282
message: 'The first argument must be of type string or an instance of ' +
83-
'Buffer. Received undefined'
83+
'Buffer or Uint8Array. Received undefined'
8484
});
8585

8686
assert.throws(() => {
@@ -90,7 +90,7 @@ assert.throws(() => {
9090
code: 'ERR_INVALID_ARG_TYPE',
9191
name: 'TypeError',
9292
message: 'The first argument must be of type string or an instance of ' +
93-
'Buffer. Received type number (1)'
93+
'Buffer or Uint8Array. Received type number (1)'
9494
});
9595

9696
// addTrailers()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
6+
const httpServer = http.createServer(common.mustCall(function(req, res) {
7+
httpServer.close();
8+
assert.throws(() => {
9+
res.write(['Throws.']);
10+
}, {
11+
code: 'ERR_INVALID_ARG_TYPE'
12+
});
13+
// should not throw
14+
res.write('1a2b3c');
15+
// should not throw
16+
res.write(new Uint8Array(1024));
17+
// should not throw
18+
res.write(Buffer.from('1'.repeat(1024)));
19+
res.end();
20+
}));
21+
22+
httpServer.listen(0, common.mustCall(function() {
23+
http.get({ port: this.address().port });
24+
}));

0 commit comments

Comments
 (0)