all files / lib/internal/process/ stdio.js

89.02% Statements 73/82
85.29% Branches 29/34
87.5% Functions 7/8
90.91% Lines 70/77
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176    1553×   1553× 1553×   1553× 29998× 1491× 1491×   1491×   1491×     1553× 32765× 1490× 1490×       1490×   1490×     1553× 134×   48× 48×   48×                           46×           46×           45×             46× 46×               48×         48× 46× 46× 46×         48× 73× 65× 65× 65×     48×     1553×           1553×           1553×           1553×       1553× 2981× 2981×       2981×       2613× 2613× 2613× 2613×       360× 360×         360× 360×               2981×   2981×   2981×    
'use strict';
 
exports.setup = setupStdio;
 
function setupStdio() {
  var stdin, stdout, stderr;
 
  function getStdout() {
    if (stdout) return stdout;
    stdout = createWritableStdioStream(1);
    stdout.destroy = stdout.destroySoon = function(er) {
      er = er || new Error('process.stdout cannot be closed.');
      stdout.emit('error', er);
    };
    if (stdout.isTTY) {
      process.on('SIGWINCH', () => stdout._refreshSize());
    }
    return stdout;
  }
 
  function getStderr() {
    if (stderr) return stderr;
    stderr = createWritableStdioStream(2);
    stderr.destroy = stderr.destroySoon = function(er) {
      er = er || new Error('process.stderr cannot be closed.');
      stderr.emit('error', er);
    };
    if (stderr.isTTY) {
      process.on('SIGWINCH', () => stderr._refreshSize());
    }
    return stderr;
  }
 
  function getStdin() {
    if (stdin) return stdin;
 
    const tty_wrap = process.binding('tty_wrap');
    const fd = 0;
 
    switch (tty_wrap.guessHandleType(fd)) {
      case 'TTY':
        const tty = require('tty');
        stdin = new tty.ReadStream(fd, {
          highWaterMark: 0,
          readable: true,
          writable: false
        });
        break;
 
      case 'FILE':
        const fs = require('fs');
        stdin = new fs.ReadStream(null, { fd: fd, autoClose: false });
        break;
 
      case 'PIPE':
      case 'TCP':
        const net = require('net');
 
        // It could be that process has been started with an IPC channel
        // sitting on fd=0, in such case the pipe for this fd is already
        // present and creating a new one will lead to the assertion failure
        // in libuv.
        if (process._channel && process._channel.fd === fd) {
          stdin = new net.Socket({
            handle: process._channel,
            readable: true,
            writable: false
          });
        } else {
          stdin = new net.Socket({
            fd: fd,
            readable: true,
            writable: false
          });
        }
        // Make sure the stdin can't be `.end()`-ed
        stdin._writableState.ended = true;
        break;
 
      default:
        // Probably an error on in uv_guess_handle()
        throw new Error('Implement me. Unknown stdin file type!');
    }
 
    // For supporting legacy API we put the FD here.
    stdin.fd = fd;
 
    // stdin starts out life in a paused state, but node doesn't
    // know yet.  Explicitly to readStop() it to put it in the
    // not-reading state.
    if (stdin._handle && stdin._handle.readStop) {
      stdin._handle.reading = false;
      stdin._readableState.reading = false;
      stdin._handle.readStop();
    }
 
    // if the user calls stdin.pause(), then we need to stop reading
    // immediately, so that the process can close down.
    stdin.on('pause', () => {
      if (!stdin._handle)
        return;
      stdin._readableState.reading = false;
      stdin._handle.reading = false;
      stdin._handle.readStop();
    });
 
    return stdin;
  }
 
  Object.defineProperty(process, 'stdout', {
    configurable: true,
    enumerable: true,
    get: getStdout
  });
 
  Object.defineProperty(process, 'stderr', {
    configurable: true,
    enumerable: true,
    get: getStderr
  });
 
  Object.defineProperty(process, 'stdin', {
    configurable: true,
    enumerable: true,
    get: getStdin
  });
 
  process.openStdin = function() {
    process.stdin.resume();
    return process.stdin;
  };
}
 
function createWritableStdioStream(fd) {
  var stream;
  const tty_wrap = process.binding('tty_wrap');
 
  // Note stream._type is used for test-module-load-list.js
 
  switch (tty_wrap.guessHandleType(fd)) {
    case 'TTY':
      const tty = require('tty');
      stream = new tty.WriteStream(fd);
      stream._type = 'tty';
      break;
 
    case 'FILE':
      const fs = require('fs');
      stream = new fs.SyncWriteStream(fd, { autoClose: false });
      stream._type = 'fs';
      break;
 
    case 'PIPE':
    case 'TCP':
      const net = require('net');
      stream = new net.Socket({
        fd: fd,
        readable: false,
        writable: true
      });
      stream._type = 'pipe';
      break;
 
    default:
      // Probably an error on in uv_guess_handle()
      throw new Error('Implement me. Unknown stream file type!');
  }
 
  // For supporting legacy API we put the FD here.
  stream.fd = fd;
 
  stream._isStdio = true;
 
  return stream;
}