Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ca83634

Browse filesBrowse 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 <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 2e77a10 commit ca83634
Copy full SHA for ca83634

File tree

Expand file treeCollapse file tree

3 files changed

+31
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+31
-5
lines changed
Open diff view settings
Collapse file

‎lib/_http_outgoing.js‎

Copy file name to clipboardExpand all lines: lib/_http_outgoing.js
+5-3Lines changed: 5 additions & 3 deletions
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')
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-http-outgoing-proto.js
+2-2Lines changed: 2 additions & 2 deletions
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()
Collapse file
+24Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.