Repository navigation
Conversation
hs.socket.create() handles Unix domain stream sockets through Network.framework, which
can't do datagram sockets, and hs.socket.createUDP() only speaks IP. Programs such as
Karabiner-Elements' send_user_command send to a Unix domain datagram socket, so there was
no way to receive them.
HSUnixDatagramSocket binds to, connects to and sends to socket file paths, and delivers
each datagram whole, with the sender's {path} (or null for an unbound sender). bind()
replaces a stale socket file but refuses one another socket is still bound to, and close()
removes the file it created. Its send and receive buffers are raised above macOS's
2048 byte default, so it can exchange datagrams up to 64 KB.
The CFSocket endpoint, receive mode and Unix socket path helpers are moved out of
HSUDPSocket and HSSocket into DatagramEndpoint.swift, so the UDP and Unix datagram
sockets share them.
Fixes #394
Changelog-Added: `hs.socket.createUnixDatagram()`, for sending and receiving Unix domain datagrams, e.g. from Karabiner-Elements' `send_user_command`
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
[Medium impact] The PR appears safe to merge. SummaryAdds
Reviews (2) · Last reviewed commit: "Address review: don't remove live socket..." · Reviewed by Greptile |
…grams A Unix socket file is now only treated as stale when connecting to it is refused (ECONNREFUSED/ENOENT). Previously a probe of the wrong type (EPROTOTYPE, e.g. a datagram probe of a stream listener) counted as stale, so bind() could unlink another program's live socket file. A datagram larger than the receive limit is now discarded and reported as an error, rather than delivered truncated as if it were whole. Also fix the receiveOne() example, which never bound the socket. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
Author
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #394.
hs.socket.create()supports Unix domain stream sockets via Network.framework, which can't do datagram sockets, andhs.socket.createUDP()is IP-only. So nothing could receive from tools like Karabiner-Elements'send_user_command, which sends JSON to a Unix domain datagram socket.What's new
hs.socket.createUnixDatagram()returns anHSUnixDatagramSocket:bind(path),connect(path),send(text, path?),sendBytes(bytes, path?),receive(),receiveOne(),pause(),close(),destroy(), plusisBound,isConnected,isReceiving,localPath,peerPath.data,connected,error). Each datagram arrives whole as aUint8Array, and the address argument is{path}, ornullif the sender isn't bound (as with Karabiner).bind()replaces a stale socket file but refuses a path another socket is still bound to.close()removes the file it created, but only if it's still the same file.net.local.dgram.maxdgram), which the docs mention.errorevents.Refactor
The CFSocket endpoint, the receive mode, and the stale-socket and path helpers moved out of
HSUDPSocket/HSSocketintoDatagramEndpoint.swift, so both datagram types use the same code. UDP behaviour is unchanged.Testing
HSSocketTestsin an hs2vm VM: 46/46 pass, including 9 new tests. They cover a round trip with bound and unbound senders, a datagram sent from a raw socket with default buffers (what Karabiner does), a 60 KB datagram, connect +receiveOne()+ replies, stale vs in-use paths, error reporting, both GC tests, and the leak test after shutdown.setActivepin is removed.npm run docs:generate/docs:testpass.🤖 Generated with Claude Code