@@ -2138,6 +2138,90 @@ Use `nc` to connect to a Unix domain socket server:
21382138nc -U /tmp/echo.sock
21392139```
21402140
2141+ ## ` net.createPipe() `
2142+
2143+ <!-- YAML
2144+ added: REPLACEME
2145+ -->
2146+
2147+ * Returns: {Object}
2148+ * ` readable ` {net.Socket} The readable end of the pipe.
2149+ * ` writable ` {net.Socket} The writable end of the pipe.
2150+
2151+ The ` net.createPipe() ` method creates an operating system pipe pair. The
2152+ returned ` readable ` and ` writable ` streams are owned by the current process and
2153+ may be passed to [ ` child_process.spawn() ` ] [ ] using the [ ` stdio ` ] [ ] option.
2154+
2155+ When a ` readable ` endpoint is passed as child stdin or as another child fd, the
2156+ child leases a readable handle. When a ` writable ` endpoint is passed as child
2157+ stdout, stderr, or another child fd, the child leases a writable handle. A
2158+ ` readable ` endpoint may not be passed as child stdout or stderr, and a
2159+ ` writable ` endpoint may not be passed as child stdin. An endpoint may be leased
2160+ to only one child process at a time. After the child process exits, endpoints
2161+ created by [ ` net.createPipe() ` ] [ ] are released from their lease and may be
2162+ passed to another [ ` child_process.spawn() ` ] [ ] call. Endpoints created by
2163+ [ ` net.createPipe() ` ] [ ] are not supported by synchronous child process APIs such
2164+ as [ ` child_process.spawnSync() ` ] [ ] .
2165+
2166+ A ` readable ` endpoint created by [ ` net.createPipe() ` ] [ ] must not be flowing
2167+ when it is passed to [ ` child_process.spawn() ` ] [ ] . The child process
2168+ [ ` 'close' ` event] [ child-process-close ] does not wait for such an endpoint to
2169+ close and does not resume it after the child process exits.
2170+
2171+ The current process is responsible for the endpoint streams. Use normal stream
2172+ idioms such as ` end() ` to finish writing and stream consumption to drain a
2173+ readable endpoint. Use ` resume() ` when an unread readable endpoint should be
2174+ drained without observing its data, and use ` destroy() ` when an endpoint is no
2175+ longer needed without being naturally ended or drained.
2176+
2177+ ``` cjs
2178+ const { spawn } = require (' node:child_process' );
2179+ const { createPipe } = require (' node:net' );
2180+ const { text } = require (' node:stream/consumers' );
2181+
2182+ const { readable , writable } = createPipe ();
2183+ const child = spawn (process .execPath , [' -e' , `
2184+ const fs = require('node:fs');
2185+ const buffer = Buffer.alloc(1);
2186+ const count = fs.readSync(0, buffer, 0, 1, null);
2187+ fs.writeSync(1, buffer.subarray(0, count));
2188+ ` ], {
2189+ stdio: [readable, ' pipe' , ' inherit' ],
2190+ });
2191+
2192+ const output = text (child .stdout );
2193+ writable .end (' abc' );
2194+
2195+ child .on (' close' , async () => {
2196+ console .log (await output); // Prints: a
2197+ console .log (await text (readable)); // Prints: bc
2198+ });
2199+ ```
2200+
2201+ ``` mjs
2202+ import { spawn } from ' node:child_process' ;
2203+ import { createPipe } from ' node:net' ;
2204+ import { text } from ' node:stream/consumers' ;
2205+
2206+ const { readable , writable } = createPipe ();
2207+ const child = spawn (process .execPath , [' -e' , `
2208+ const fs = require('node:fs');
2209+ const buffer = Buffer.alloc(1);
2210+ const count = fs.readSync(0, buffer, 0, 1, null);
2211+ fs.writeSync(1, buffer.subarray(0, count));
2212+ ` ], {
2213+ stdio: [readable, ' pipe' , ' inherit' ],
2214+ });
2215+
2216+ const output = text (child .stdout );
2217+ writable .end (' abc' );
2218+
2219+ child .on (' close' , async () => {
2220+ console .log (await output); // Prints: a
2221+ console .log (await text (readable)); // Prints: bc
2222+ });
2223+ ```
2224+
21412225## ` net.getDefaultAutoSelectFamily() `
21422226
21432227<!-- YAML
@@ -2264,6 +2348,8 @@ net.isIPv6('fhqwhgads'); // returns false
22642348[ `ERR_SOCKET_HANDLE_ADOPTED` ] : errors.md#err_socket_handle_adopted
22652349[ `EventEmitter` ] : events.md#class-eventemitter
22662350[ `child_process.fork()` ] : child_process.md#child_processforkmodulepath-args-options
2351+ [ `child_process.spawn()` ] : child_process.md#child_processspawncommand-args-options
2352+ [ `child_process.spawnSync()` ] : child_process.md#child_processspawnsynccommand-args-options
22672353[ `dns.lookup()` ] : dns.md#dnslookuphostname-options-callback
22682354[ `dns.lookup()` hints ] : dns.md#supported-getaddrinfo-flags
22692355[ `net.Server` ] : #class-netserver
@@ -2276,6 +2362,7 @@ net.isIPv6('fhqwhgads'); // returns false
22762362[ `net.createConnection(options)` ] : #netcreateconnectionoptions-connectlistener
22772363[ `net.createConnection(path)` ] : #netcreateconnectionpath-connectlistener
22782364[ `net.createConnection(port, host)` ] : #netcreateconnectionport-host-connectlistener
2365+ [ `net.createPipe()` ] : #netcreatepipe
22792366[ `net.createServer()` ] : #netcreateserveroptions-connectionlistener
22802367[ `net.getDefaultAutoSelectFamily()` ] : #netgetdefaultautoselectfamily
22812368[ `net.getDefaultAutoSelectFamilyAttemptTimeout()` ] : #netgetdefaultautoselectfamilyattempttimeout
@@ -2308,13 +2395,15 @@ net.isIPv6('fhqwhgads'); // returns false
23082395[ `socket.setTimeout()` ] : #socketsettimeouttimeout-callback
23092396[ `socket.setTimeout(timeout)` ] : #socketsettimeouttimeout-callback
23102397[ `stream.getDefaultHighWaterMark()` ] : stream.md#streamgetdefaulthighwatermarkobjectmode
2398+ [ `stdio` ] : child_process.md#optionsstdio
23112399[ `worker_threads` ] : worker_threads.md
23122400[ `writable.destroy()` ] : stream.md#writabledestroyerror
23132401[ `writable.destroyed` ] : stream.md#writabledestroyed
23142402[ `writable.end()` ] : stream.md#writableendchunk-encoding-callback
23152403[ `writable.writableLength` ] : stream.md#writablewritablelength
23162404[ dot-decimal notation ] : https://en.wikipedia.org/wiki/Dot-decimal_notation
23172405[ half-closed ] : https://tools.ietf.org/html/rfc1122
2406+ [ child-process-close ] : child_process.md#event-close
23182407[ stream_writable_write ] : stream.md#writablewritechunk-encoding-callback
23192408[ unspecified IPv4 address ] : https://en.wikipedia.org/wiki/0.0.0.0
23202409[ unspecified IPv6 address ] : https://en.wikipedia.org/wiki/IPv6_address#Unspecified_address
0 commit comments