feat: implement allowHalfOpen and end event for libp2p compatibility #224
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.
Hey! This update fixes the UnexpectedEOFError that was breaking libp2p connections.
What was happening
When using this library with js-libp2p, the connection would fail during the encryption handshake. After checking the logs, I found that libp2p expects sockets to support “half-open” mode (where reading can stop but writing can continue).
This library was closing the entire socket as soon as it got a FIN packet, which caused the error.
What I changed
I added support for allowHalfOpen and made the socket behave more like Node’s net module. -JavaScript
Added an allowHalfOpen option to Socket (defaults to false).
Added an event listener for when the read side closes.
Instead of destroying the socket, it now switches to a readOnly state.-Android
Updated TcpSocketClient to emit an onEnd() event when EOF is detected instead of calling destroy() immediately. iOS Implemented the missing onEnd delegate method. Now it sends an event to JS when the read side closes instead of disconnecting right away.
Testing-
Tested using @abuvanth’s reproduction repo (react-native-libp2p-tcp-test). Before this fix, the connection would fail instantly. After the changes, the handshake completes without issues.