From b4b0efeb60a25a333247928cc4406f9b6f4bef02 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 3 Nov 2018 19:53:06 +0100 Subject: [PATCH 1/3] 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. --- doc/api/fs.md | 20 ++++++++++++++++++++ doc/api/net.md | 12 ++++++++++++ lib/internal/fs/streams.js | 10 ++++++++++ lib/internal/http2/core.js | 2 +- lib/net.js | 8 +++++++- test/parallel/test-fs-ready-event-stream.js | 11 +++++++++-- test/parallel/test-net-connect-buffer.js | 3 +++ 7 files changed, 62 insertions(+), 4 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 9de798c6ff7ca2..38da782d3c1a0c 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -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 + + +* {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 + +* {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) + +* {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() diff --git a/doc/api/net.md b/doc/api/net.md index 6e824ec72a9717..90c3354e803e20 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -731,7 +731,7 @@ added: REPLACEME * {boolean} -This is true if the socket is not connected yet, either because `.connect()` +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`][]). diff --git a/lib/net.js b/lib/net.js index f26570eea4f48b..de798faa10ee9b 100644 --- a/lib/net.js +++ b/lib/net.js @@ -495,9 +495,10 @@ Object.defineProperty(Socket.prototype, '_connecting', { }); Object.defineProperty(Socket.prototype, 'pending', { - get: function() { + get() { return !this._handle || this.connecting; - } + }, + configurable: true }); From 653c298d354ae6aec205efe3d170ae30f9f684b8 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 3 Nov 2018 22:01:51 +0100 Subject: [PATCH 3/3] fixup! fs,net: standardize `pending` stream property --- lib/internal/fs/streams.js | 2 +- test/parallel/test-fs-ready-event-stream.js | 4 ++-- test/parallel/test-net-connect-buffer.js | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/internal/fs/streams.js b/lib/internal/fs/streams.js index de0ac9eb625480..8c9ed35f81c2f4 100644 --- a/lib/internal/fs/streams.js +++ b/lib/internal/fs/streams.js @@ -399,7 +399,7 @@ WriteStream.prototype.close = function(cb) { // There is no shutdown() for files. WriteStream.prototype.destroySoon = WriteStream.prototype.end; -Object.defineProperty(ReadStream.prototype, 'pending', { +Object.defineProperty(WriteStream.prototype, 'pending', { get() { return this.fd === null; }, configurable: true }); diff --git a/test/parallel/test-fs-ready-event-stream.js b/test/parallel/test-fs-ready-event-stream.js index 4913940891788b..f6b380cf26d42c 100644 --- a/test/parallel/test-fs-ready-event-stream.js +++ b/test/parallel/test-fs-ready-event-stream.js @@ -14,7 +14,7 @@ readStream.on('ready', common.mustCall(() => { const writeFile = path.join(tmpdir.path, 'write-fsreadyevent.txt'); tmpdir.refresh(); const writeStream = fs.createWriteStream(writeFile, { autoClose: true }); -assert.strictEqual(readStream.pending, true); +assert.strictEqual(writeStream.pending, true); writeStream.on('ready', common.mustCall(() => { - assert.strictEqual(readStream.pending, false); + assert.strictEqual(writeStream.pending, false); })); diff --git a/test/parallel/test-net-connect-buffer.js b/test/parallel/test-net-connect-buffer.js index 0425aad636e2b3..81dd8f0c569c71 100644 --- a/test/parallel/test-net-connect-buffer.js +++ b/test/parallel/test-net-connect-buffer.js @@ -91,6 +91,9 @@ tcp.listen(0, common.mustCall(function() { 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); assert.strictEqual(r, false);