Skip to content

Commit 04734d2

Browse files
sameer-coderMylesBorins
authored andcommitted
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: #19408 Fixes: #19304 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 525b338 commit 04734d2

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

lib/fs.js

+2
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,7 @@ ReadStream.prototype.open = function() {
20042004

20052005
self.fd = fd;
20062006
self.emit('open', fd);
2007+
self.emit('ready');
20072008
// start the flow of data.
20082009
self.read();
20092010
});
@@ -2167,6 +2168,7 @@ WriteStream.prototype.open = function() {
21672168

21682169
this.fd = fd;
21692170
this.emit('open', fd);
2171+
this.emit('ready');
21702172
});
21712173
};
21722174

lib/net.js

+1
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ function afterConnect(status, handle, req, readable, writable) {
11751175
self._unrefTimer();
11761176

11771177
self.emit('connect');
1178+
self.emit('ready');
11781179

11791180
// start the first read, or get an immediate EOF.
11801181
// this doesn't actually consume any bytes, because len=0.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fs = require('fs');
4+
const path = require('path');
5+
const tmpdir = require('../common/tmpdir');
6+
7+
const readStream = fs.createReadStream(__filename);
8+
readStream.on('ready', common.mustCall(() => {}, 1));
9+
10+
const writeFile = path.join(tmpdir.path, 'write-fsreadyevent.txt');
11+
tmpdir.refresh();
12+
const writeStream = fs.createWriteStream(writeFile, { autoClose: true });
13+
writeStream.on('ready', common.mustCall(() => {}, 1));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// This test ensures that socket.connect can be called without callback
5+
// which is optional.
6+
7+
const net = require('net');
8+
9+
const server = net.createServer(common.mustCall(function(conn) {
10+
conn.end();
11+
server.close();
12+
})).listen(0, common.mustCall(function() {
13+
const client = new net.Socket();
14+
15+
client.on('ready', common.mustCall(function() {
16+
client.end();
17+
}));
18+
19+
client.connect(server.address());
20+
}));

0 commit comments

Comments
 (0)