Skip to content

Commit 4a828a7

Browse files
committed
process: internal/process/stdio.js cleanup / modernization
Avoid using deprecated getter syntax plus other miscellaneous updates.
1 parent 42ede93 commit 4a828a7

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

lib/internal/process/stdio.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,41 @@ exports.setup = setupStdio;
55
function setupStdio() {
66
var stdin, stdout, stderr;
77

8-
process.__defineGetter__('stdout', function() {
8+
function getStdout() {
99
if (stdout) return stdout;
1010
stdout = createWritableStdioStream(1);
1111
stdout.destroy = stdout.destroySoon = function(er) {
1212
er = er || new Error('process.stdout cannot be closed.');
1313
stdout.emit('error', er);
1414
};
1515
if (stdout.isTTY) {
16-
process.on('SIGWINCH', function() {
17-
stdout._refreshSize();
18-
});
16+
process.on('SIGWINCH', () => stdout._refreshSize());
1917
}
2018
return stdout;
21-
});
19+
}
2220

23-
process.__defineGetter__('stderr', function() {
21+
function getStderr() {
2422
if (stderr) return stderr;
2523
stderr = createWritableStdioStream(2);
2624
stderr.destroy = stderr.destroySoon = function(er) {
2725
er = er || new Error('process.stderr cannot be closed.');
2826
stderr.emit('error', er);
2927
};
3028
if (stderr.isTTY) {
31-
process.on('SIGWINCH', function() {
32-
stderr._refreshSize();
33-
});
29+
process.on('SIGWINCH', () => stderr._refreshSize());
3430
}
3531
return stderr;
36-
});
32+
}
3733

38-
process.__defineGetter__('stdin', function() {
34+
function getStdin() {
3935
if (stdin) return stdin;
4036

41-
var tty_wrap = process.binding('tty_wrap');
42-
var fd = 0;
37+
const tty_wrap = process.binding('tty_wrap');
38+
const fd = 0;
4339

4440
switch (tty_wrap.guessHandleType(fd)) {
4541
case 'TTY':
46-
var tty = require('tty');
42+
const tty = require('tty');
4743
stdin = new tty.ReadStream(fd, {
4844
highWaterMark: 0,
4945
readable: true,
@@ -52,13 +48,13 @@ function setupStdio() {
5248
break;
5349

5450
case 'FILE':
55-
var fs = require('fs');
51+
const fs = require('fs');
5652
stdin = new fs.ReadStream(null, { fd: fd, autoClose: false });
5753
break;
5854

5955
case 'PIPE':
6056
case 'TCP':
61-
var net = require('net');
57+
const net = require('net');
6258

6359
// It could be that process has been started with an IPC channel
6460
// sitting on fd=0, in such case the pipe for this fd is already
@@ -100,7 +96,7 @@ function setupStdio() {
10096

10197
// if the user calls stdin.pause(), then we need to stop reading
10298
// immediately, so that the process can close down.
103-
stdin.on('pause', function() {
99+
stdin.on('pause', () => {
104100
if (!stdin._handle)
105101
return;
106102
stdin._readableState.reading = false;
@@ -109,6 +105,24 @@ function setupStdio() {
109105
});
110106

111107
return stdin;
108+
}
109+
110+
Object.defineProperty(process, 'stdout', {
111+
configurable: true,
112+
enumerable: true,
113+
get: getStdout
114+
});
115+
116+
Object.defineProperty(process, 'stderr', {
117+
configurable: true,
118+
enumerable: true,
119+
get: getStderr
120+
});
121+
122+
Object.defineProperty(process, 'stdin', {
123+
configurable: true,
124+
enumerable: true,
125+
get: getStdin
112126
});
113127

114128
process.openStdin = function() {
@@ -119,26 +133,26 @@ function setupStdio() {
119133

120134
function createWritableStdioStream(fd) {
121135
var stream;
122-
var tty_wrap = process.binding('tty_wrap');
136+
const tty_wrap = process.binding('tty_wrap');
123137

124138
// Note stream._type is used for test-module-load-list.js
125139

126140
switch (tty_wrap.guessHandleType(fd)) {
127141
case 'TTY':
128-
var tty = require('tty');
142+
const tty = require('tty');
129143
stream = new tty.WriteStream(fd);
130144
stream._type = 'tty';
131145
break;
132146

133147
case 'FILE':
134-
var fs = require('fs');
148+
const fs = require('fs');
135149
stream = new fs.SyncWriteStream(fd, { autoClose: false });
136150
stream._type = 'fs';
137151
break;
138152

139153
case 'PIPE':
140154
case 'TCP':
141-
var net = require('net');
155+
const net = require('net');
142156
stream = new net.Socket({
143157
fd: fd,
144158
readable: false,

0 commit comments

Comments
 (0)