MCPcopy Index your code
hub / github.com/nodejs/node / getStdin

Function getStdin

lib/internal/bootstrap/switches/is_main_thread.js:194–284  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

192}
193
194function getStdin() {
195 if (stdin) return stdin;
196 const fd = 0;
197
198 switch (guessHandleType(fd)) {
199 case 'TTY': {
200 const tty = require('tty');
201 stdin = new tty.ReadStream(fd);
202 break;
203 }
204
205 case 'FILE': {
206 const fs = require('fs');
207 stdin = new fs.ReadStream(null, { fd: fd, autoClose: false });
208 break;
209 }
210
211 case 'PIPE':
212 case 'TCP': {
213 const net = require('net');
214
215 // It could be that process has been started with an IPC channel
216 // sitting on fd=0, in such case the pipe for this fd is already
217 // present and creating a new one will lead to the assertion failure
218 // in libuv.
219 if (process.channel && process.channel.fd === fd) {
220 stdin = new net.Socket({
221 handle: process.channel,
222 readable: true,
223 writable: false,
224 manualStart: true,
225 });
226 } else {
227 stdin = new net.Socket({
228 fd: fd,
229 readable: true,
230 writable: false,
231 manualStart: true,
232 });
233 }
234 // Make sure the stdin can't be `.end()`-ed
235 stdin._writableState.ended = true;
236 break;
237 }
238
239 default: {
240 // Provide a dummy contentless input for e.g. non-console
241 // Windows applications.
242 const { Readable } = require('stream');
243 stdin = new Readable({ read() {} });
244 stdin.push(null);
245 }
246 }
247
248 // For supporting legacy API we put the FD here.
249 stdin.fd = fd;
250
251 // `stdin` starts out life in a paused state, but node doesn't

Callers

nothing calls this directly

Calls 7

guessHandleTypeFunction · 0.85
addCleanupFunction · 0.85
requireFunction · 0.50
pushMethod · 0.45
readStopMethod · 0.45
onMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected