All files / lib/internal/bootstrap/switches is_main_thread.js

93.33% Statements 252/270
98.03% Branches 50/51
58.82% Functions 10/17
93.33% Lines 252/270

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 2718x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 69x 69x 69x 69x 69x 69x 69x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3407x 3407x 3407x 3407x 3407x 26x 26x 26x 26x 26x 3407x 3407x 2405x 2405x 2405x 2405x 2405x 3407x 3407x 3407x 974x 974x 974x 974x 974x 974x             974x 974x 974x 974x 974x 974x 974x 974x 974x 974x 974x 3407x 3407x 2x 2x 2x 2x 2x     2x 2x 3407x 3407x 3407x 3407x 3407x 3407x 3407x 3407x 3407x 8x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 13x 12x 13x 13x 14x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 2x 8x 1x 1x 1x 8x 9793x 9793x 1214x 1214x 1214x 1214x 1214x 9792x 13x 13x 1214x 1214x         1214x 1214x 9793x 8x 42112x 42112x 41570x 41570x 41570x 41570x 41570x 42109x 13x 13x 42099x         42099x 42099x 42112x 8x 324x 324x 296x 296x 296x 324x 9x 9x 9x 9x 324x 324x 3x 3x 3x 3x 324x 324x 324x 151x 151x 151x 151x 151x 151x 151x 1x 1x 1x 1x 1x 1x 151x 150x 150x 150x 150x 150x 150x 150x 151x 151x 151x 151x 324x 324x 1x 1x 1x 1x 1x 1x 324x 324x 324x 324x 324x 324x 324x 324x 324x 159x 159x 159x 159x 164x 164x 164x 164x 164x 179x 164x 164x 164x 132x 132x 132x 86x 86x 86x 86x 132x 164x 164x     164x 164x 324x 8x 8x 8x 3x 3x 3x 8x  
'use strict';
 
const { ObjectDefineProperty } = primordials;
const rawMethods = internalBinding('process_methods');
 
// TODO(joyeecheung): deprecate and remove these underscore methods
process._debugProcess = rawMethods._debugProcess;
process._debugEnd = rawMethods._debugEnd;
 
// See the discussion in https://github.com/nodejs/node/issues/19009 and
// https://github.com/nodejs/node/pull/34010 for why these are no-ops.
// Five word summary: they were broken beyond repair.
process._startProfilerIdleNotifier = () => {};
process._stopProfilerIdleNotifier = () => {};
 
function defineStream(name, getter) {
  ObjectDefineProperty(process, name, {
    configurable: true,
    enumerable: true,
    get: getter
  });
}
 
defineStream('stdout', getStdout);
defineStream('stdin', getStdin);
defineStream('stderr', getStderr);
 
// Worker threads don't receive signals.
const {
  startListeningIfSignal,
  stopListeningIfSignal
} = require('internal/process/signal');
process.on('newListener', startListeningIfSignal);
process.on('removeListener', stopListeningIfSignal);
 
// ---- keep the attachment of the wrappers above so that it's easier to ----
// ----              compare the setups side-by-side                    -----
 
const { guessHandleType } = internalBinding('util');
 
function createWritableStdioStream(fd) {
  let stream;
  // Note stream._type is used for test-module-load-list.js
  switch (guessHandleType(fd)) {
    case 'TTY': {
      const tty = require('tty');
      stream = new tty.WriteStream(fd);
      stream._type = 'tty';
      break;
    }
 
    case 'FILE': {
      const SyncWriteStream = require('internal/fs/sync_write_stream');
      stream = new SyncWriteStream(fd, { autoClose: false });
      stream._type = 'fs';
      break;
    }
 
    case 'PIPE':
    case 'TCP': {
      const net = require('net');
 
      // If fd is already being used for the IPC channel, libuv will return
      // an error when trying to use it again. In that case, create the socket
      // using the existing handle instead of the fd.
      if (process.channel && process.channel.fd === fd) {
        const { kChannelHandle } = require('internal/child_process');
        stream = new net.Socket({
          handle: process[kChannelHandle],
          readable: false,
          writable: true
        });
      } else {
        stream = new net.Socket({
          fd,
          readable: false,
          writable: true
        });
      }
 
      stream._type = 'pipe';
      break;
    }
 
    default: {
      // Provide a dummy black-hole output for e.g. non-console
      // Windows applications.
      const { Writable } = require('stream');
      stream = new Writable({
        write(buf, enc, cb) {
          cb();
        }
      });
    }
  }
 
  // For supporting legacy API we put the FD here.
  stream.fd = fd;
 
  stream._isStdio = true;
 
  return stream;
}
 
function dummyDestroy(err, cb) {
  cb(err);
  this._undestroy();
 
  // We need to emit 'close' anyway so that the closing
  // of the stream is observable. We just make sure we
  // are not going to do it twice.
  // The 'close' event is needed so that finished and
  // pipeline work correctly.
  if (!this._writableState.emitClose) {
    process.nextTick(() => {
      this.emit('close');
    });
  }
}
 
let stdin;
let stdout;
let stderr;
 
let stdoutDestroy;
let stderrDestroy;
 
function refreshStdoutOnSigWinch() {
  stdout._refreshSize();
}
 
function refreshStderrOnSigWinch() {
  stderr._refreshSize();
}
 
function getStdout() {
  if (stdout) return stdout;
  stdout = createWritableStdioStream(1);
  stdout.destroySoon = stdout.destroy;
  // Override _destroy so that the fd is never actually closed.
  stdoutDestroy = stdout._destroy;
  stdout._destroy = dummyDestroy;
  if (stdout.isTTY) {
    process.on('SIGWINCH', refreshStdoutOnSigWinch);
  }
 
  internalBinding('mksnapshot').cleanups.push(function cleanupStdout() {
    stdout._destroy = stdoutDestroy;
    stdout.destroy();
    process.removeListener('SIGWINCH', refreshStdoutOnSigWinch);
    stdout = undefined;
  });
  return stdout;
}
 
function getStderr() {
  if (stderr) return stderr;
  stderr = createWritableStdioStream(2);
  stderr.destroySoon = stderr.destroy;
  stderrDestroy = stderr._destroy;
  // Override _destroy so that the fd is never actually closed.
  stderr._destroy = dummyDestroy;
  if (stderr.isTTY) {
    process.on('SIGWINCH', refreshStderrOnSigWinch);
  }
  internalBinding('mksnapshot').cleanups.push(function cleanupStderr() {
    stderr._destroy = stderrDestroy;
    stderr.destroy();
    process.removeListener('SIGWINCH', refreshStderrOnSigWinch);
    stderr = undefined;
  });
  return stderr;
}
 
function getStdin() {
  if (stdin) return stdin;
  const fd = 0;
 
  switch (guessHandleType(fd)) {
    case 'TTY': {
      const tty = require('tty');
      stdin = new tty.ReadStream(fd);
      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,
          manualStart: true
        });
      } else {
        stdin = new net.Socket({
          fd: fd,
          readable: true,
          writable: false,
          manualStart: true
        });
      }
      // Make sure the stdin can't be `.end()`-ed
      stdin._writableState.ended = true;
      break;
    }
 
    default: {
      // Provide a dummy contentless input for e.g. non-console
      // Windows applications.
      const { Readable } = require('stream');
      stdin = new Readable({ read() {} });
      stdin.push(null);
    }
  }
 
  // 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
  // once the stream implementation does so (one nextTick later),
  // so that the process can close down.
  stdin.on('pause', () => {
    process.nextTick(onpause);
  });
 
  function onpause() {
    if (!stdin._handle)
      return;
    if (stdin._handle.reading && !stdin.readableFlowing) {
      stdin._readableState.reading = false;
      stdin._handle.reading = false;
      stdin._handle.readStop();
    }
  }
 
  internalBinding('mksnapshot').cleanups.push(function cleanupStdin() {
    stdin.destroy();
    stdin = undefined;
  });
  return stdin;
}
 
// Used by internal tests.
rawMethods.resetStdioForTesting = function() {
  stdin = undefined;
  stdout = undefined;
  stderr = undefined;
};