Skip to content

Commit 2b50cd7

Browse files
rexagodtargos
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. PR-URL: #33155 Fixes: #33379 Refs: #29829 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Zeyu Yang <himself65@outlook.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b533fb3 commit 2b50cd7

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/_http_outgoing.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const {
6363
hideStackFrames
6464
} = require('internal/errors');
6565
const { validateString } = require('internal/validators');
66+
const { isUint8Array } = require('internal/util/types');
6667

6768
const HIGH_WATER_MARK = getDefaultHighWaterMark();
6869
const { CRLF, debug } = common;
@@ -674,7 +675,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
674675
throw new ERR_STREAM_NULL_VALUES();
675676
} else if (typeof chunk === 'string') {
676677
len = Buffer.byteLength(chunk, encoding);
677-
} else if (chunk instanceof Buffer) {
678+
} else if (isUint8Array(chunk)) {
678679
len = chunk.length;
679680
} else {
680681
throw new ERR_INVALID_ARG_TYPE(
Lines changed: 24 additions & 0 deletions
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)