jhs67 / usocket

node unix domain sockets
ISC License
12 stars 7 forks source link

Create USocket on existing fd? #12

Open joepie91 opened 2 months ago

joepie91 commented 2 months ago

First of all, thanks for the library :) This seems quite helpful for working around Node's lack of support for this (which somehow still persists).

So, I'm currently trying to use this library for a project of mine, a Wayland protocol implementation. Unfortunately Wayland requires supporting creating a socket from a pre-allocated fd rather than from a given path - and the API for this library only seems to support instantiating a socket from a path.

Is there any chance that you could add a function or option for instantiating a USocket from an existing fd?

jhs67 commented 2 months ago

This is undocumented, but looking at the code, you should be able to use an existing file descriptor like this:

const client = new usocket.USocket({ fd: 100 });

or

const client = new usocket.USocket();
client.connect({ fd: 100 });

Hopefully this works for you, otherwise please let me know.

joepie91 commented 2 months ago

That seems to work, thanks! I guess this is just a documentation bug then :)

I'll try using it for a while, and see if I run into any issues with it. So far it passes the initial test of "replace net.Socket with it and see if the code still runs", but I haven't gotten to the point of handling sent FDs yet.