neon-bindings / neon

Rust bindings for writing safe and fast native Node.js modules.
https://www.neon-bindings.com/
Apache License 2.0
7.99k stars 283 forks source link

How to call into Node's libuv APIs (`uv.h`) #941

Open CMCDragonkai opened 1 year ago

CMCDragonkai commented 1 year ago

I'm coming from implementing native addons in NodeJS with C++ using the Node API.

I'm trying to integrate the rust library quiche into NodeJS.

The usage of quiche requires the ability to bind to addresses and open UDP sockets.

In quiche's examples, they show how to do this with mio inside Rust, or with libev in their C examples.

However we know, NodeJS has libuv and this is what is used to handle sockets. I believe I should be using libuv to ensure that my native addon is opening sockets in the same way that the rest of the NodeJS runtime is behaving.

If I'm trying to open and bind to a socket using Neon, how do I access the libuv API that the Node runtime would be bundled with?

I saw that Rust has a libuv crate, but using a such thing would result in simply statically compiling the libuv in the resulting node addon right?

What is the equivalent to:

#include <uv.h>

For Neon?

kjvalencik commented 1 year ago

Neon does not provide any direct hooks into libuv. At most, we would provide a pointer to uv_loop_s since that's all that Node API provides.

This could also be accessed without Neon's help.

Neon also provides high level APIs for calling back into the JavaScript VM from another thread. However, these are intended for general eventing.

bgfist commented 3 months ago

I wonder how node-gyp link those functions of libuv. After all, nodejs now only bundled with a binary executable "node", and accompanying header files, but without any dynamic library. If we know the underlying principles, we can figure out the linking options in Rust.