Skip to content

Commit 8a8b43e

Browse files
sameer-codertargos
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 fdc51a1 commit 8a8b43e

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

20772077
self.fd = fd;
20782078
self.emit('open', fd);
2079+
self.emit('ready');
20792080
// start the flow of data.
20802081
self.read();
20812082
});
@@ -2234,6 +2235,7 @@ WriteStream.prototype.open = function() {
22342235

22352236
this.fd = fd;
22362237
this.emit('open', fd);
2238+
this.emit('ready');
22372239
});
22382240
};
22392241

lib/net.js

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

11601160
self.emit('connect');
1161+
self.emit('ready');
11611162

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