Skip to content

Commit 16e0011

Browse files
ronagtargos
authored andcommitted
http: add missing stream-like properties to OutgoingMessage
PR-URL: #29018 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent e543d35 commit 16e0011

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

lib/_http_outgoing.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Object.setPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
112112
Object.setPrototypeOf(OutgoingMessage, Stream);
113113

114114
Object.defineProperty(OutgoingMessage.prototype, 'writableFinished', {
115-
get: function() {
115+
get() {
116116
return (
117117
this.finished &&
118118
this.outputSize === 0 &&
@@ -121,6 +121,24 @@ Object.defineProperty(OutgoingMessage.prototype, 'writableFinished', {
121121
}
122122
});
123123

124+
Object.defineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
125+
get() {
126+
return false;
127+
}
128+
});
129+
130+
Object.defineProperty(OutgoingMessage.prototype, 'writableLength', {
131+
get() {
132+
return this.outputSize + (this.socket ? this.socket.writableLength : 0);
133+
}
134+
});
135+
136+
Object.defineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
137+
get() {
138+
return this.socket ? this.socket.writableHighWaterMark : HIGH_WATER_MARK;
139+
}
140+
});
141+
124142
Object.defineProperty(OutgoingMessage.prototype, '_headers', {
125143
get: internalUtil.deprecate(function() {
126144
return this.getHeaders();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
const http = require('http');
6+
const OutgoingMessage = http.OutgoingMessage;
7+
8+
{
9+
const msg = new OutgoingMessage();
10+
assert.strictEqual(msg.writableObjectMode, false);
11+
}
12+
13+
{
14+
const msg = new OutgoingMessage();
15+
assert(msg.writableHighWaterMark > 0);
16+
}
17+
18+
{
19+
const server = http.createServer(common.mustCall(function(req, res) {
20+
const hwm = req.socket.writableHighWaterMark;
21+
assert.strictEqual(res.writableHighWaterMark, hwm);
22+
23+
assert.strictEqual(res.writableLength, 0);
24+
res.write('');
25+
const len = res.writableLength;
26+
res.write('asd');
27+
assert.strictEqual(res.writableLength, len + 8);
28+
res.end();
29+
res.on('finish', common.mustCall(() => {
30+
assert.strictEqual(res.writableLength, 0);
31+
server.close();
32+
}));
33+
}));
34+
35+
server.listen(0);
36+
37+
server.on('listening', common.mustCall(function() {
38+
const clientRequest = http.request({
39+
port: server.address().port,
40+
method: 'GET',
41+
path: '/'
42+
});
43+
clientRequest.end();
44+
}));
45+
}
46+
47+
{
48+
const msg = new OutgoingMessage();
49+
msg._implicitHeader = function() {};
50+
assert.strictEqual(msg.writableLength, 0);
51+
msg.write('asd');
52+
assert.strictEqual(msg.writableLength, 7);
53+
}

0 commit comments

Comments
 (0)