Skip to content

Commit 2aa23cd

Browse files
addaleaxBridgeAR
authored andcommitted
fs,net: standardize pending stream property
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: #24067 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 36e4d0c commit 2aa23cd

File tree

7 files changed

+66
-4
lines changed

7 files changed

+66
-4
lines changed

doc/api/fs.md

+20
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,16 @@ argument to `fs.createReadStream()`. If `path` is passed as a string, then
488488
`readStream.path` will be a string. If `path` is passed as a `Buffer`, then
489489
`readStream.path` will be a `Buffer`.
490490

491+
### readStream.pending
492+
<!-- YAML
493+
added: REPLACEME
494+
-->
495+
496+
* {boolean}
497+
498+
This property is `true` if the underlying file has not been opened yet,
499+
i.e. before the `'ready'` event is emitted.
500+
491501
## Class: fs.Stats
492502
<!-- YAML
493503
added: v0.1.21
@@ -833,6 +843,16 @@ argument to [`fs.createWriteStream()`][]. If `path` is passed as a string, then
833843
`writeStream.path` will be a string. If `path` is passed as a `Buffer`, then
834844
`writeStream.path` will be a `Buffer`.
835845

846+
### writeStream.pending
847+
<!-- YAML
848+
added: REPLACEME
849+
-->
850+
851+
* {boolean}
852+
853+
This property is `true` if the underlying file has not been opened yet,
854+
i.e. before the `'ready'` event is emitted.
855+
836856
## fs.access(path[, mode], callback)
837857
<!-- YAML
838858
added: v0.11.15

doc/api/net.md

+12
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,17 @@ The numeric representation of the local port. For example, `80` or `21`.
724724
Pauses the reading of data. That is, [`'data'`][] events will not be emitted.
725725
Useful to throttle back an upload.
726726

727+
### socket.pending
728+
<!-- YAML
729+
added: REPLACEME
730+
-->
731+
732+
* {boolean}
733+
734+
This is `true` if the socket is not connected yet, either because `.connect()`
735+
has not yet been called or because it is still in the process of connecting
736+
(see [`socket.connecting`][]).
737+
727738
### socket.ref()
728739
<!-- YAML
729740
added: v0.9.1
@@ -1167,6 +1178,7 @@ Returns `true` if input is a version 6 IP address, otherwise returns `false`.
11671178
[`socket.connect(options)`]: #net_socket_connect_options_connectlistener
11681179
[`socket.connect(path)`]: #net_socket_connect_path_connectlistener
11691180
[`socket.connect(port, host)`]: #net_socket_connect_port_host_connectlistener
1181+
[`socket.connecting`]: #net_socket_connecting
11701182
[`socket.destroy()`]: #net_socket_destroy_exception
11711183
[`socket.end()`]: #net_socket_end_data_encoding_callback
11721184
[`socket.pause()`]: #net_socket_pause

lib/internal/fs/streams.js

+10
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ ReadStream.prototype.close = function(cb) {
228228
this.destroy(null, cb);
229229
};
230230

231+
Object.defineProperty(ReadStream.prototype, 'pending', {
232+
get() { return this.fd === null; },
233+
configurable: true
234+
});
235+
231236
function WriteStream(path, options) {
232237
if (!(this instanceof WriteStream))
233238
return new WriteStream(path, options);
@@ -394,6 +399,11 @@ WriteStream.prototype.close = function(cb) {
394399
// There is no shutdown() for files.
395400
WriteStream.prototype.destroySoon = WriteStream.prototype.end;
396401

402+
Object.defineProperty(WriteStream.prototype, 'pending', {
403+
get() { return this.fd === null; },
404+
configurable: true
405+
});
406+
397407
module.exports = {
398408
ReadStream,
399409
WriteStream

lib/internal/http2/core.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,7 @@ class Http2Stream extends Duplex {
18291829

18301830
_final(cb) {
18311831
const handle = this[kHandle];
1832-
if (this[kID] === undefined) {
1832+
if (this.pending) {
18331833
this.once('ready', () => this._final(cb));
18341834
} else if (handle !== undefined) {
18351835
debug(`Http2Stream ${this[kID]} [Http2Session ` +

lib/net.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
347347
// sent out to the other side.
348348
Socket.prototype._final = function(cb) {
349349
// If still connecting - defer handling `_final` until 'connect' will happen
350-
if (this.connecting) {
350+
if (this.pending) {
351351
debug('_final: not yet connected');
352352
return this.once('connect', () => this._final(cb));
353353
}
@@ -492,6 +492,13 @@ Object.defineProperty(Socket.prototype, '_connecting', {
492492
}
493493
});
494494

495+
Object.defineProperty(Socket.prototype, 'pending', {
496+
get() {
497+
return !this._handle || this.connecting;
498+
},
499+
configurable: true
500+
});
501+
495502

496503
Object.defineProperty(Socket.prototype, 'readyState', {
497504
get: function() {
+9-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
'use strict';
22
const common = require('../common');
3+
const assert = require('assert');
34
const fs = require('fs');
45
const path = require('path');
56
const tmpdir = require('../common/tmpdir');
67

78
const readStream = fs.createReadStream(__filename);
8-
readStream.on('ready', common.mustCall(() => {}, 1));
9+
assert.strictEqual(readStream.pending, true);
10+
readStream.on('ready', common.mustCall(() => {
11+
assert.strictEqual(readStream.pending, false);
12+
}));
913

1014
const writeFile = path.join(tmpdir.path, 'write-fsreadyevent.txt');
1115
tmpdir.refresh();
1216
const writeStream = fs.createWriteStream(writeFile, { autoClose: true });
13-
writeStream.on('ready', common.mustCall(() => {}, 1));
17+
assert.strictEqual(writeStream.pending, true);
18+
writeStream.on('ready', common.mustCall(() => {
19+
assert.strictEqual(writeStream.pending, false);
20+
}));

test/parallel/test-net-connect-buffer.js

+6
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ tcp.listen(0, common.mustCall(function() {
4444
const socket = net.Stream({ highWaterMark: 0 });
4545

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

50+
assert.strictEqual(socket.pending, true);
4951
assert.strictEqual(socket.connecting, true);
5052
assert.strictEqual(socket.readyState, 'opening');
5153

@@ -87,6 +89,10 @@ tcp.listen(0, common.mustCall(function() {
8789
console.error('write cb');
8890
assert.ok(connected);
8991
assert.strictEqual(socket.bytesWritten, Buffer.from(a + b).length);
92+
assert.strictEqual(socket.pending, false);
93+
}));
94+
socket.on('close', common.mustCall(() => {
95+
assert.strictEqual(socket.pending, true);
9096
}));
9197

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

0 commit comments

Comments
 (0)