diff options
author | Robert Nagy <ronagy@icloud.com> | 2019-07-14 18:13:08 +0200 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-07-16 10:26:08 -0700 |
commit | 7032e59bb550adcb448b2d24877921e7f97e7399 (patch) | |
tree | 0d01f95a35123a7ad9d90f9740e55fc6aa258bbf | |
parent | 462f43824f6af577bde27da76d9f33365eddcfe7 (diff) | |
download | android-node-v8-7032e59bb550adcb448b2d24877921e7f97e7399.tar.gz android-node-v8-7032e59bb550adcb448b2d24877921e7f97e7399.tar.bz2 android-node-v8-7032e59bb550adcb448b2d24877921e7f97e7399.zip |
http: add response.writableFinished
response.writableFinished is true if all data has been flushed to the
underlying system.
PR-URL: https://github.com/nodejs/node/pull/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>
-rw-r--r-- | doc/api/http.md | 9 | ||||
-rw-r--r-- | lib/_http_outgoing.js | 9 | ||||
-rw-r--r-- | test/parallel/test-http-outgoing-writableFinished.js | 32 |
3 files changed, 50 insertions, 0 deletions
diff --git a/doc/api/http.md b/doc/api/http.md index 02c5859dc2..5bdcc66ac5 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1466,6 +1466,15 @@ Returns `true` if the entire data was flushed successfully to the kernel buffer. Returns `false` if all or part of the data was queued in user memory. `'drain'` will be emitted when the buffer is free again. +### response.writableFinished +<!-- YAML +added: REPLACEME +--> + +* {boolean} + +Is `true` if all data has been flushed to the underlying system. + ### response.writeContinue() <!-- YAML added: v0.3.0 diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index a4a2b3ab14..ef7c8c6577 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -109,6 +109,15 @@ function OutgoingMessage() { Object.setPrototypeOf(OutgoingMessage.prototype, Stream.prototype); Object.setPrototypeOf(OutgoingMessage, Stream); +Object.defineProperty(OutgoingMessage.prototype, 'writableFinished', { + get: function() { + return ( + this.finished && + this.outputSize === 0 && + (!this.socket || this.socket.writableLength === 0) + ); + } +}); Object.defineProperty(OutgoingMessage.prototype, '_headers', { get: internalUtil.deprecate(function() { diff --git a/test/parallel/test-http-outgoing-writableFinished.js b/test/parallel/test-http-outgoing-writableFinished.js new file mode 100644 index 0000000000..6f84d91e71 --- /dev/null +++ b/test/parallel/test-http-outgoing-writableFinished.js @@ -0,0 +1,32 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const http = require('http'); + +const server = http.createServer(common.mustCall(function(req, res) { + assert.strictEqual(res.writableFinished, false); + res + .on('finish', common.mustCall(() => { + assert.strictEqual(res.writableFinished, true); + server.close(); + })) + .end(); +})); + +server.listen(0); + +server.on('listening', common.mustCall(function() { + const clientRequest = http.request({ + port: server.address().port, + method: 'GET', + path: '/' + }); + + assert.strictEqual(clientRequest.writableFinished, false); + clientRequest + .on('finish', common.mustCall(() => { + assert.strictEqual(clientRequest.writableFinished, true); + })) + .end(); + assert.strictEqual(clientRequest.writableFinished, false); +})); |