Skip to content

Commit c133fa9

Browse files
committed
Add -sNODERAWSOCKETS backend for real TCP & UDP on Node.js
This adds a new `-sNODERAWSOCKETS` setting that for supporting direct full sockets on Node.js via the `node:net` for TCP and `node:dgram` for UDP modules, without needing `ws`, an external proxy process, or pthreads. * Full support for both outgoing and incoming TCP, using the `node:net` APIs. * Full support for UDP using the `node:dgram` APIs * Support for threading, with tests * Support for IPV6 To support these embeddings without JSPI being mandatory requires using `process.binding('tcp_wrap')` and `process.binding('udp_wrap')` in Node.js, which are used here to support older versions of Node.js. Further, to avoid having to rely on private APIs in modern Node.js I actually implemented upstream Node.js PRs to make public API surface area available for the full embedding in the following: * nodejs/node#63838 * nodejs/node#63932 * nodejs/node#63951 * nodejs/node#63825 Then this PR conditionally checks these features and uses the public APIs as defined by them when it is able to, falling back to `tcp_wrap` and `udp_wrap` only when not supported. While Node.js with these features has not yet been released, as soon as all three have landed, we can rely on this surface area for modern 26+ versions of Node.js compat.
1 parent 4177187 commit c133fa9

29 files changed

Lines changed: 2399 additions & 45 deletions

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ See docs/process.md for more on how version tagging works.
2020

2121
6.0.2 (in development)
2222
----------------------
23+
- New `-sNODERAWSOCKETS` setting that backs the POSIX sockets API with real TCP
24+
(`node:net`) and UDP (`node:dgram`) sockets on Node.js, with no `ws`, proxy
25+
process, or pthreads required. Supports incoming and outgoing TCP, UDP, IPv6,
26+
and `-pthread` with `PROXY_TO_PTHREAD`. Uses the public node APIs where
27+
available, falling back to `tcp_wrap`/`udp_wrap` on older Node.js. (#27080)
2328

2429
6.0.1 - 06/22/26
2530
----------------

site/source/docs/tools_reference/settings_reference.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,35 @@ sockets calls from browser to native world.
588588

589589
Default value: false
590590

591+
.. _noderawsockets:
592+
593+
NODERAWSOCKETS
594+
==============
595+
596+
If enabled, the POSIX sockets API is backed by Node.js's ``node:net``
597+
module, giving real non-blocking outgoing TCP sockets with no WebSockets,
598+
proxy process or pthreads. This is the sockets counterpart to
599+
:ref:`NODERAWFS`: where :ref:`NODERAWFS` gives direct access to the host
600+
filesystem, this gives direct access to host sockets. It only works under
601+
node and is ignored elsewhere.
602+
603+
It supports full TCP (outgoing connect plus bind, listen and accept for
604+
servers) and UDP. TCP clients use the public ``node:net`` API when possible,
605+
falling back to the private ``tcp_wrap``/``udp_wrap`` handles on older
606+
Node.js.
607+
608+
It is event-driven. Socket readiness comes through the same
609+
``emscripten_set_socket_*_callback`` hooks the WebSocket backend uses, so it
610+
works with existing readiness reactors. It cannot be combined with the
611+
WebSocket emulation, :ref:`PROXY_POSIX_SOCKETS` or :ref:`SOCKET_WEBRTC`.
612+
613+
It works under -pthread with :ref:`PROXY_TO_PTHREAD`, where main() and every socket
614+
syscall run on a single worker alongside the node handles and their event
615+
loop. As with the WebSocket backend, sharing a socket across threads under a
616+
plain -pthread build (without PROXY_TO_PTHREAD) is not supported.
617+
618+
Default value: false
619+
591620
.. _websocket_subprotocol:
592621

593622
WEBSOCKET_SUBPROTOCOL

src/lib/libsigs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ sigs = {
271271
__syscall_rmdir__sig: 'ip',
272272
__syscall_sendmsg__sig: 'iipiiii',
273273
__syscall_sendto__sig: 'iippipi',
274+
__syscall_setsockopt__sig: 'iiiipii',
274275
__syscall_shutdown__sig: 'iiiiiii',
275276
__syscall_socket__sig: 'iiiiiii',
276277
__syscall_stat64__sig: 'ipp',

src/lib/libsockfs.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ addToLibrary({
88
$SOCKFS__postset: () => {
99
addAtInit('SOCKFS.root = FS.mount(SOCKFS, {}, null);');
1010
},
11-
$SOCKFS__deps: ['$FS'],
11+
$SOCKFS__deps: ['$FS',
12+
#if NODERAWSOCKETS
13+
'$nodeSockOps',
14+
#endif
15+
],
1216
$SOCKFS: {
1317
#if expectToReceiveOnModule('websocket')
1418
websocketArgs: {},
@@ -44,8 +48,12 @@ addToLibrary({
4448
return FS.createNode(null, '/', {{{ cDefs.S_IFDIR | 0o777 }}}, 0);
4549
},
4650
createSocket(family, type, protocol) {
47-
// Emscripten only supports AF_INET
48-
if (family != {{{ cDefs.AF_INET }}}) {
51+
if (family != {{{ cDefs.AF_INET }}}
52+
#if NODERAWSOCKETS
53+
// The node:net backend supports IPv6; other backends are IPv4 only.
54+
&& family != {{{ cDefs.AF_INET6 }}}
55+
#endif
56+
) {
4957
throw new FS.ErrnoError({{{ cDefs.EAFNOSUPPORT }}});
5058
}
5159
type &= ~{{{ cDefs.SOCK_CLOEXEC | cDefs.SOCK_NONBLOCK }}}; // Some applications may pass it; it makes no sense for a single process.
@@ -69,6 +77,8 @@ addToLibrary({
6977
pending: [],
7078
recv_queue: [],
7179
#if SOCKET_WEBRTC
80+
#elif NODERAWSOCKETS
81+
sock_ops: nodeSockOps
7282
#else
7383
sock_ops: SOCKFS.websocket_sock_ops
7484
#endif
@@ -726,7 +736,7 @@ addToLibrary({
726736

727737
return res;
728738
}
729-
}
739+
},
730740
},
731741

732742
/*

0 commit comments

Comments
 (0)