Skip to content

Commit 06d0abe

Browse files
ronagtargos
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 <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent caee910 commit 06d0abe

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
@@ -1464,6 +1464,15 @@ Returns `true` if the entire data was flushed successfully to the kernel
14641464
buffer. Returns `false` if all or part of the data was queued in user memory.
14651465
`'drain'` will be emitted when the buffer is free again.
14661466

1467+
### response.writableFinished
1468+
<!-- YAML
1469+
added: REPLACEME
1470+
-->
1471+
1472+
* {boolean}
1473+
1474+
Is `true` if all data has been flushed to the underlying system.
1475+
14671476
### response.writeContinue()
14681477
<!-- YAML
14691478
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)