Hey, I’ve started to put together a experimental project over the last couple of days, effectively wrapping libuv using PureC. Imagine compiling from PureScript straight down to C and then hooking into libuv directly, side-stepping node.js entirely. It might seem insane at first to even attempt such a project, but so far I have not been disappointed. As it turns out most of the code in the node.js codebase I have seen so far is essentially concerned about integrating with JavaScript and making things usable in an OOP setting.
Example:
testUdp :: _ -> UV.Handler _ Unit
testUdp loop = do
recvH <- UV.udpNew loop
UV.udpBind (UV.ip4Addr "0.0.0.0" 1234) [ UV._UdpReuseAddr ] recvH
UV.udpRecvStart <@> recvH $ \(mBuf :: Maybe UV.Buffer) -> do
mS <- traverse UV.bufferToString mBuf
Console.log $ "udp: received: " <> show mS
sendH <- UV.udpNew loop
buf <- lift $ UV.bufferFromString "hello"
UV.udpSend [ buf ] (UV.ip4Addr "0.0.0.0" 1234)
(case _ of
Right _ ->
Console.log "udp: sent"
Left errCode ->
Console.log $ renderErrCode errCode
) sendH
The API is purposely low-level, and once complete could be wrapped up into something nice to use. Like a purescript-aff for PureC using libuvs async primitives.
The project lives here for now: https://github.com/pure-c/purec-uv. Don’t bother trying to compile and run it yet, though - I need to clean up a little first when I get around to it. Just wanted to post this here, gather feedback, and maybe find people interested in building this thing out.