Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v14.x backport] http: don't throw on Uint8Arrays for http.ServerResponse#write #33490

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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 @@ -667,9 +668,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
return true;
}

if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
if (!fromEnd && typeof chunk !== 'string' && !(isUint8Array(chunk))) {
throw new ERR_INVALID_ARG_TYPE('first argument',
['string', 'Buffer'], chunk);
['string', 'Buffer', 'Uint8Array'], chunk);
ronag marked this conversation as resolved.
Show resolved Hide resolved
}

if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-http-outgoing-proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ assert.throws(() => {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The first argument must be of type string or an instance of ' +
'Buffer. Received undefined'
'Buffer or Uint8Array. Received undefined'
});

assert.throws(() => {
Expand All @@ -93,7 +93,7 @@ assert.throws(() => {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The first argument must be of type string or an instance of ' +
'Buffer. Received type number (1)'
'Buffer or Uint8Array. Received type number (1)'
codebytere marked this conversation as resolved.
Show resolved Hide resolved
});

// addTrailers()
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-http-outgoing-write-types.js
Original file line number Diff line number Diff line change
@@ -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 });
}));