From 8a8b43e1b1258aee4b5ff1f35ded99ab7b7916e7 Mon Sep 17 00:00:00 2001 From: Sameer Srivastava Date: Sat, 17 Mar 2018 16:24:46 +0530 Subject: [PATCH] fs,net: emit 'ready' for fs streams and sockets ... in addition to the event names they currently use. Currently, various internal streams have different events that indicate that the underlying resource has successfully been established. This commit adds ready event for fs and net sockets to standardize on emitting ready for all of these streams. PR-URL: https://github.com/nodejs/node/pull/19408 Fixes: https://github.com/nodejs/node/issues/19304 Reviewed-By: Anna Henningsen --- lib/fs.js | 2 ++ lib/net.js | 1 + test/parallel/test-fs-ready-event-stream.js | 13 ++++++++++++ .../test-net-socket-ready-without-cb.js | 20 +++++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 test/parallel/test-fs-ready-event-stream.js create mode 100644 test/parallel/test-net-socket-ready-without-cb.js diff --git a/lib/fs.js b/lib/fs.js index 231303ec42c620..0a5a5247607d1d 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2076,6 +2076,7 @@ ReadStream.prototype.open = function() { self.fd = fd; self.emit('open', fd); + self.emit('ready'); // start the flow of data. self.read(); }); @@ -2234,6 +2235,7 @@ WriteStream.prototype.open = function() { this.fd = fd; this.emit('open', fd); + this.emit('ready'); }); }; diff --git a/lib/net.js b/lib/net.js index 17a128a4f0c8db..9b22628ebc7e7c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1158,6 +1158,7 @@ function afterConnect(status, handle, req, readable, writable) { self._unrefTimer(); self.emit('connect'); + self.emit('ready'); // start the first read, or get an immediate EOF. // this doesn't actually consume any bytes, because len=0. diff --git a/test/parallel/test-fs-ready-event-stream.js b/test/parallel/test-fs-ready-event-stream.js new file mode 100644 index 00000000000000..71f7aa76e36c76 --- /dev/null +++ b/test/parallel/test-fs-ready-event-stream.js @@ -0,0 +1,13 @@ +'use strict'; +const common = require('../common'); +const fs = require('fs'); +const path = require('path'); +const tmpdir = require('../common/tmpdir'); + +const readStream = fs.createReadStream(__filename); +readStream.on('ready', common.mustCall(() => {}, 1)); + +const writeFile = path.join(tmpdir.path, 'write-fsreadyevent.txt'); +tmpdir.refresh(); +const writeStream = fs.createWriteStream(writeFile, { autoClose: true }); +writeStream.on('ready', common.mustCall(() => {}, 1)); diff --git a/test/parallel/test-net-socket-ready-without-cb.js b/test/parallel/test-net-socket-ready-without-cb.js new file mode 100644 index 00000000000000..1ad5db4760dc1e --- /dev/null +++ b/test/parallel/test-net-socket-ready-without-cb.js @@ -0,0 +1,20 @@ +'use strict'; +const common = require('../common'); + +// This test ensures that socket.connect can be called without callback +// which is optional. + +const net = require('net'); + +const server = net.createServer(common.mustCall(function(conn) { + conn.end(); + server.close(); +})).listen(0, common.mustCall(function() { + const client = new net.Socket(); + + client.on('ready', common.mustCall(function() { + client.end(); + })); + + client.connect(server.address()); +}));