Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs,net: standardize pending stream property #24067

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link

@sindresorhus sindresorhus Nov 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to name this .isPending. When you read .pending, it could either mean it contains a pending object or a boolean of whether it's pending. That's not clear from just reading the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.pending I think fits the style of other pre-existing properties like .connecting

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s also what HTTP/2 already uses for this purpose…

<!-- 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`.

### readStream.pending
addaleax marked this conversation as resolved.
Show resolved Hide resolved
<!-- 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()`
addaleax marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1168,6 +1179,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(ReadStream.prototype, 'pending', {
addaleax marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1823,7 +1823,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
8 changes: 7 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ function shutdownSocket(self, callback) {
// 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 @@ -494,6 +494,12 @@ Object.defineProperty(Socket.prototype, '_connecting', {
}
});

Object.defineProperty(Socket.prototype, 'pending', {
get: function() {
return !this._handle || this.connecting;
}
});
addaleax marked this conversation as resolved.
Show resolved Hide resolved


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(readStream.pending, true);
addaleax marked this conversation as resolved.
Show resolved Hide resolved
writeStream.on('ready', common.mustCall(() => {
assert.strictEqual(readStream.pending, false);
addaleax marked this conversation as resolved.
Show resolved Hide resolved
}));
3 changes: 3 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,7 @@ 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);
lpinca marked this conversation as resolved.
Show resolved Hide resolved
}));

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