Skip to content

Commit feb6e1f

Browse files
rexagodcodebytere
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: #33490 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 b6ef6c8 commit feb6e1f

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

lib/_http_outgoing.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const {
6161
hideStackFrames
6262
} = require('internal/errors');
6363
const { validateString } = require('internal/validators');
64+
const { isUint8Array } = require('internal/util/types');
6465

6566
const HIGH_WATER_MARK = getDefaultHighWaterMark();
6667
const { CRLF, debug } = common;
@@ -669,9 +670,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
669670
return true;
670671
}
671672

672-
if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
673+
if (!fromEnd && typeof chunk !== 'string' && !(isUint8Array(chunk))) {
673674
throw new ERR_INVALID_ARG_TYPE('first argument',
674-
['string', 'Buffer'], chunk);
675+
['string', 'Buffer', 'Uint8Array'], chunk);
675676
}
676677

677678
if (!fromEnd && msg.socket && !msg.socket.writableCorked) {

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

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

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

9999
// 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)