Skip to content

Commit d08bd46

Browse files
committed
http: writableFinished
1 parent e12f48e commit d08bd46

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

1461+
### response.writableFinished
1462+
<!-- YAML
1463+
added: REPLACEME
1464+
-->
1465+
1466+
* {boolean}
1467+
1468+
Is `true` if all data has been flushed to the underlying system.
1469+
14611470
### response.writeContinue()
14621471
<!-- YAML
14631472
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)