Skip to content

Commit 1c81494

Browse files
sameer-coderaddaleax
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 84afd6e commit 1c81494

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
@@ -2051,6 +2051,7 @@ ReadStream.prototype.open = function() {
20512051

20522052
self.fd = fd;
20532053
self.emit('open', fd);
2054+
self.emit('ready');
20542055
// start the flow of data.
20552056
self.read();
20562057
});
@@ -2207,6 +2208,7 @@ WriteStream.prototype.open = function() {
22072208

22082209
this.fd = fd;
22092210
this.emit('open', fd);
2211+
this.emit('ready');
22102212
});
22112213
};
22122214

lib/net.js

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

11901190
self.emit('connect');
1191+
self.emit('ready');
11911192

11921193
// start the first read, or get an immediate EOF.
11931194
// 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)