Skip to content

Commit 7032e59

Browse files
ronagTrott
authored andcommitted
http: add response.writableFinished
response.writableFinished is true if all data has been flushed to the underlying system. PR-URL: #28681 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 462f438 commit 7032e59

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

doc/api/http.md

+9
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,15 @@ Returns `true` if the entire data was flushed successfully to the kernel
14661466
buffer. Returns `false` if all or part of the data was queued in user memory.
14671467
`'drain'` will be emitted when the buffer is free again.
14681468

1469+
### response.writableFinished
1470+
<!-- YAML
1471+
added: REPLACEME
1472+
-->
1473+
1474+
* {boolean}
1475+
1476+
Is `true` if all data has been flushed to the underlying system.
1477+
14691478
### response.writeContinue()
14701479
<!-- YAML
14711480
added: v0.3.0

lib/_http_outgoing.js

+9
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ function OutgoingMessage() {
109109
Object.setPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
110110
Object.setPrototypeOf(OutgoingMessage, Stream);
111111

112+
Object.defineProperty(OutgoingMessage.prototype, 'writableFinished', {
113+
get: function() {
114+
return (
115+
this.finished &&
116+
this.outputSize === 0 &&
117+
(!this.socket || this.socket.writableLength === 0)
118+
);
119+
}
120+
});
112121

113122
Object.defineProperty(OutgoingMessage.prototype, '_headers', {
114123
get: internalUtil.deprecate(function() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
6+
const server = http.createServer(common.mustCall(function(req, res) {
7+
assert.strictEqual(res.writableFinished, false);
8+
res
9+
.on('finish', common.mustCall(() => {
10+
assert.strictEqual(res.writableFinished, true);
11+
server.close();
12+
}))
13+
.end();
14+
}));
15+
16+
server.listen(0);
17+
18+
server.on('listening', common.mustCall(function() {
19+
const clientRequest = http.request({
20+
port: server.address().port,
21+
method: 'GET',
22+
path: '/'
23+
});
24+
25+
assert.strictEqual(clientRequest.writableFinished, false);
26+
clientRequest
27+
.on('finish', common.mustCall(() => {
28+
assert.strictEqual(clientRequest.writableFinished, true);
29+
}))
30+
.end();
31+
assert.strictEqual(clientRequest.writableFinished, false);
32+
}));

0 commit comments

Comments
 (0)