Skip to content

net: add createPipe() and createSocketPair() - #65094

Open
kingces95 wants to merge 1 commit into
nodejs:mainfrom
kingces95:create-pipe
Open

net: add createPipe() and createSocketPair()#65094
kingces95 wants to merge 1 commit into
nodejs:mainfrom
kingces95:create-pipe

Conversation

@kingces95

@kingces95 kingces95 commented Aug 7, 2026

Copy link
Copy Markdown

Adds a node:pipe module with createPipe(), returning a readable
and writable endpoint owned by the parent process.

The endpoints may be passed to child_process.spawn() stdio. This lets
the parent lend a pipe endpoint to a child without turning the parent
stream itself into child-owned stdio. The parent can then reclaim unread
bytes or lend the same endpoint to a later child.

This is useful for bash-like partial consumption of long-lived streams.
For example, a parent can keep ownership of a Server Sent Event stream
while delegating bounded reads to external tools, then continue parsing
from the exact byte where the child stopped.

The central integration test (test-child-process-leased-pipe.js) writes abc,
lets child A read a, lets child B read b, and then verifies that the parent
can still read c.

Endpoints created by pipe.createPipe() are rejected by spawnSync(), and
an endpoint may only be leased to one child process at a time.

Tests cover node:pipe creation, documentation examples, child stdio
leasing, sequential lease-and-reclaim behavior, lease error cleanup,
spawnSync() rejection, and platform-consistent writable finish
behavior.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Aug 7, 2026
@kingces95
kingces95 marked this pull request as ready for review August 7, 2026 01:52
@kingces95 kingces95 changed the title net: add parent-owned pipe endpoints net: add createPipe() Aug 7, 2026
@marco-ippolito

Copy link
Copy Markdown
Member

I dont think we should create a new module

@jasnell

jasnell commented Aug 7, 2026

Copy link
Copy Markdown
Member

+1, there's no justification for a new top-level module.

@kingces95
kingces95 force-pushed the create-pipe branch 4 times, most recently from 83d8402 to db5d657 Compare August 7, 2026 20:33
@kingces95

kingces95 commented Aug 7, 2026

Copy link
Copy Markdown
Author

Yep. I'll move it to net.

@kingces95

kingces95 commented Aug 9, 2026

Copy link
Copy Markdown
Author

I also pushed a small stacked follow-up branch that explores the adjacent uv_socketpair() primitive as net.createSocketPair():

kingces95/node@create-pipe...create-socket-pair

My immediate use case is net.createPipe(); I’m offering this for completeness if reviewers think these APIs should be considered as a small family.

The idea is that net.createPipe() exposes directional OS-backed endpoints, while net.createSocketPair() exposes symmetric duplex OS-backed endpoints, similar in shape to stream.duplexPair() but with real handles.

The follow-up has child-process support for fd >= 3, where the duplex nature is explicit.

@kingces95

Copy link
Copy Markdown
Author

I folded the socket-pair follow-up into the main change.

The updated version keeps both APIs in node:net:

  • net.createPipe() returns directional parent-owned endpoints.
  • net.createSocketPair() returns a symmetric duplex pair.
  • Both can be leased to child processes through stdio, with createSocketPair() limited to fd >= 3.
  • The bash/SSE sketch was removed from the test suite.

This should make the PR easier to review as one coherent “parent-owned OS endpoints” feature.


The removed sketch is preserved here as a userland workbook/example of the style of shell-like stream composition this API is meant to enable:

https://gist.github.com/kingces95/1e024a1e987ef3956c7ae3513e468c64

@kingces95
kingces95 force-pushed the create-pipe branch 4 times, most recently from 3621fab to 3c6c2d0 Compare August 10, 2026 06:07
@kingces95
kingces95 marked this pull request as draft August 10, 2026 06:25
Signed-off-by: Chris King <kingces95@gmail.com>
@kingces95

kingces95 commented Aug 10, 2026

Copy link
Copy Markdown
Author

I landed on a single coherent node:net feature shape:

  • net.createPipe() exposes directional OS-backed endpoints: { readable, writable }.
  • net.createSocketPair() exposes symmetric duplex OS-backed endpoints: [left, right].
  • Pipe endpoints can be leased directly to child process stdio, because that maps cleanly to inherited pipe handles on both Unix and Windows.
  • Socket-pair endpoints are not accepted as stdio; instead, they use Node’s existing IPC handle-transfer path with subprocess.send(), which matches how socket-like handles already cross process boundaries.
  • The parent process owns the endpoint lifecycle, so docs now call out normal stream cleanup: end(), consumption, resume(), or destroy() as appropriate.

So the PR is no longer “just create pipe.” It is a parent-owned OS endpoint proposal: pipes for directional stdio leasing, socket pairs for duplex cross-process communication.

@kingces95
kingces95 marked this pull request as ready for review August 10, 2026 07:33
@kingces95 kingces95 changed the title net: add createPipe() net: add createPipe() and createSocketPair() Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants