Skip to content

Commit

Permalink
fs,net: standardize pending stream property
Browse files Browse the repository at this point in the history
Use the same property name as http2 does to indicate that
the stream is in the state before the `ready` event is emitted.

PR-URL: nodejs#24067
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
addaleax authored and kiyomizumia committed Nov 15, 2018
1 parent ced4cf7 commit 3cbc6cb
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
20 changes: 20 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,16 @@ argument to `fs.createReadStream()`. If `path` is passed as a string, then
`readStream.path` will be a string. If `path` is passed as a `Buffer`, then
`readStream.path` will be a `Buffer`.

### readStream.pending
<!-- YAML
added: REPLACEME
-->

* {boolean}

This property is `true` if the underlying file has not been opened yet,
i.e. before the `'ready'` event is emitted.

## Class: fs.Stats
<!-- YAML
added: v0.1.21
Expand Down Expand Up @@ -833,6 +843,16 @@ argument to [`fs.createWriteStream()`][]. If `path` is passed as a string, then
`writeStream.path` will be a string. If `path` is passed as a `Buffer`, then
`writeStream.path` will be a `Buffer`.

### writeStream.pending
<!-- YAML
added: REPLACEME
-->

* {boolean}

This property is `true` if the underlying file has not been opened yet,
i.e. before the `'ready'` event is emitted.

## fs.access(path[, mode], callback)
<!-- YAML
added: v0.11.15
Expand Down
12 changes: 12 additions & 0 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,17 @@ The numeric representation of the local port. For example, `80` or `21`.
Pauses the reading of data. That is, [`'data'`][] events will not be emitted.
Useful to throttle back an upload.

### socket.pending
<!-- YAML
added: REPLACEME
-->

* {boolean}

This is `true` if the socket is not connected yet, either because `.connect()`
has not yet been called or because it is still in the process of connecting
(see [`socket.connecting`][]).

### socket.ref()
<!-- YAML
added: v0.9.1
Expand Down Expand Up @@ -1167,6 +1178,7 @@ Returns `true` if input is a version 6 IP address, otherwise returns `false`.
[`socket.connect(options)`]: #net_socket_connect_options_connectlistener
[`socket.connect(path)`]: #net_socket_connect_path_connectlistener
[`socket.connect(port, host)`]: #net_socket_connect_port_host_connectlistener
[`socket.connecting`]: #net_socket_connecting
[`socket.destroy()`]: #net_socket_destroy_exception
[`socket.end()`]: #net_socket_end_data_encoding_callback
[`socket.pause()`]: #net_socket_pause
Expand Down
10 changes: 10 additions & 0 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ ReadStream.prototype.close = function(cb) {
this.destroy(null, cb);
};

Object.defineProperty(ReadStream.prototype, 'pending', {
get() { return this.fd === null; },
configurable: true
});

function WriteStream(path, options) {
if (!(this instanceof WriteStream))
return new WriteStream(path, options);
Expand Down Expand Up @@ -394,6 +399,11 @@ WriteStream.prototype.close = function(cb) {
// There is no shutdown() for files.
WriteStream.prototype.destroySoon = WriteStream.prototype.end;

Object.defineProperty(WriteStream.prototype, 'pending', {
get() { return this.fd === null; },
configurable: true
});

module.exports = {
ReadStream,
WriteStream
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1829,7 +1829,7 @@ class Http2Stream extends Duplex {

_final(cb) {
const handle = this[kHandle];
if (this[kID] === undefined) {
if (this.pending) {
this.once('ready', () => this._final(cb));
} else if (handle !== undefined) {
debug(`Http2Stream ${this[kID]} [Http2Session ` +
Expand Down
9 changes: 8 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
// sent out to the other side.
Socket.prototype._final = function(cb) {
// If still connecting - defer handling `_final` until 'connect' will happen
if (this.connecting) {
if (this.pending) {
debug('_final: not yet connected');
return this.once('connect', () => this._final(cb));
}
Expand Down Expand Up @@ -492,6 +492,13 @@ Object.defineProperty(Socket.prototype, '_connecting', {
}
});

Object.defineProperty(Socket.prototype, 'pending', {
get() {
return !this._handle || this.connecting;
},
configurable: true
});


Object.defineProperty(Socket.prototype, 'readyState', {
get: function() {
Expand Down
11 changes: 9 additions & 2 deletions test/parallel/test-fs-ready-event-stream.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');

const readStream = fs.createReadStream(__filename);
readStream.on('ready', common.mustCall(() => {}, 1));
assert.strictEqual(readStream.pending, true);
readStream.on('ready', common.mustCall(() => {
assert.strictEqual(readStream.pending, false);
}));

const writeFile = path.join(tmpdir.path, 'write-fsreadyevent.txt');
tmpdir.refresh();
const writeStream = fs.createWriteStream(writeFile, { autoClose: true });
writeStream.on('ready', common.mustCall(() => {}, 1));
assert.strictEqual(writeStream.pending, true);
writeStream.on('ready', common.mustCall(() => {
assert.strictEqual(writeStream.pending, false);
}));
6 changes: 6 additions & 0 deletions test/parallel/test-net-connect-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ tcp.listen(0, common.mustCall(function() {
const socket = net.Stream({ highWaterMark: 0 });

let connected = false;
assert.strictEqual(socket.pending, true);
socket.connect(this.address().port, common.mustCall(() => connected = true));

assert.strictEqual(socket.pending, true);
assert.strictEqual(socket.connecting, true);
assert.strictEqual(socket.readyState, 'opening');

Expand Down Expand Up @@ -87,6 +89,10 @@ tcp.listen(0, common.mustCall(function() {
console.error('write cb');
assert.ok(connected);
assert.strictEqual(socket.bytesWritten, Buffer.from(a + b).length);
assert.strictEqual(socket.pending, false);
}));
socket.on('close', common.mustCall(() => {
assert.strictEqual(socket.pending, true);
}));

assert.strictEqual(socket.bytesWritten, Buffer.from(a).length);
Expand Down

0 comments on commit 3cbc6cb

Please sign in to comment.