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

Function createWritableStdioStream

lib/internal/bootstrap/switches/is_main_thread.js:50–112  ·  view source on GitHub ↗
(fd)

Source from the content-addressed store, hash-verified

48const { guessHandleType } = require('internal/util');
49
50function createWritableStdioStream(fd) {
51 let stream;
52 // Note stream._type is used for test-module-load-list.js
53 switch (guessHandleType(fd)) {
54 case 'TTY': {
55 const tty = require('tty');
56 stream = new tty.WriteStream(fd);
57 stream._type = 'tty';
58 break;
59 }
60
61 case 'FILE': {
62 const SyncWriteStream = require('internal/fs/sync_write_stream');
63 stream = new SyncWriteStream(fd, { autoClose: false });
64 stream._type = 'fs';
65 break;
66 }
67
68 case 'PIPE':
69 case 'TCP': {
70 const net = require('net');
71
72 // If fd is already being used for the IPC channel, libuv will return
73 // an error when trying to use it again. In that case, create the socket
74 // using the existing handle instead of the fd.
75 if (process.channel && process.channel.fd === fd) {
76 const { kChannelHandle } = require('internal/child_process');
77 stream = new net.Socket({
78 handle: process[kChannelHandle],
79 readable: false,
80 writable: true,
81 });
82 } else {
83 stream = new net.Socket({
84 fd,
85 readable: false,
86 writable: true,
87 });
88 }
89
90 stream._type = 'pipe';
91 break;
92 }
93
94 default: {
95 // Provide a dummy black-hole output for e.g. non-console
96 // Windows applications.
97 const { Writable } = require('stream');
98 stream = new Writable({
99 write(buf, enc, cb) {
100 cb();
101 },
102 });
103 }
104 }
105
106 // For supporting legacy API we put the FD here.
107 stream.fd = fd;

Callers 2

getStdoutFunction · 0.85
getStderrFunction · 0.85

Calls 2

guessHandleTypeFunction · 0.85
requireFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…