Skip to content

Commit

Permalink
http: writableFinished
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jul 15, 2019
1 parent e12f48e commit d08bd46
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,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
Expand Down
9 changes: 9 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-http-outgoing-writableFinished.js
Original file line number Diff line number Diff line change
@@ -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);
}));

0 comments on commit d08bd46

Please sign in to comment.