Skip to content

Commit c396b2a

Browse files
ronagtargos
authored andcommitted
http: buffer writes even while no socket assigned
PR-URL: #29019 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 6bc4620 commit c396b2a

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

lib/_http_outgoing.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const { Object, ObjectPrototype } = primordials;
2525

26+
const { getDefaultHighWaterMark } = require('internal/streams/state');
2627
const assert = require('internal/assert');
2728
const Stream = require('stream');
2829
const internalUtil = require('internal/util');
@@ -51,6 +52,7 @@ const {
5152
} = require('internal/errors');
5253
const { validateString } = require('internal/validators');
5354

55+
const HIGH_WATER_MARK = getDefaultHighWaterMark();
5456
const { CRLF, debug } = common;
5557

5658
const kIsCorked = Symbol('isCorked');
@@ -277,7 +279,7 @@ function _writeRaw(data, encoding, callback) {
277279
this.outputData.push({ data, encoding, callback });
278280
this.outputSize += data.length;
279281
this._onPendingData(data.length);
280-
return false;
282+
return this.outputSize < HIGH_WATER_MARK;
281283
}
282284

283285

lib/internal/streams/state.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ function highWaterMarkFrom(options, isDuplex, duplexKey) {
99
isDuplex ? options[duplexKey] : null;
1010
}
1111

12+
function getDefaultHighWaterMark(objectMode) {
13+
return objectMode ? 16 : 16 * 1024;
14+
}
15+
1216
function getHighWaterMark(state, options, duplexKey, isDuplex) {
1317
const hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
1418
if (hwm != null) {
@@ -20,9 +24,10 @@ function getHighWaterMark(state, options, duplexKey, isDuplex) {
2024
}
2125

2226
// Default value
23-
return state.objectMode ? 16 : 16 * 1024;
27+
return getDefaultHighWaterMark(state.objectMode);
2428
}
2529

2630
module.exports = {
27-
getHighWaterMark
31+
getHighWaterMark,
32+
getDefaultHighWaterMark
2833
};
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
require('../common');
4+
const assert = require('assert');
5+
const { getDefaultHighWaterMark } = require('internal/streams/state');
6+
7+
const http = require('http');
8+
const OutgoingMessage = http.OutgoingMessage;
9+
10+
const msg = new OutgoingMessage();
11+
msg._implicitHeader = function() {};
12+
13+
// Writes should be buffered until highwatermark
14+
// even when no socket is assigned.
15+
16+
assert.strictEqual(msg.write('asd'), true);
17+
while (msg.write('asd'));
18+
const highwatermark = msg.writableHighWaterMark || getDefaultHighWaterMark();
19+
assert(msg.outputSize >= highwatermark);

0 commit comments

Comments
 (0)