Created
February 4, 2025 23:42
-
-
Save forivall/7e9e1d77e772b6259135ce5b6c7e1279 to your computer and use it in GitHub Desktop.
This file contains 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
const port = process.argv[2]; | |
const host = process.argv[3]; | |
const delay = 1000; | |
const socket = new net.Socket(); | |
socket.on('close', () => { | |
setTimeout(() => { | |
socket.connect(port, host); | |
}, delay); | |
}); | |
socket.connect(port, 'localhost'); | |
const socketWithDisconnectionBuffer = bufferChunksUntilConnected(socket); | |
// and make use of socketWithDisconnectionBuffer! | |
function bufferChunksUntilConnected(socket: net.Socket) { | |
const passthrough = new stream.PassThrough(); | |
socket | |
.on('connect', () => { | |
passthrough.pipe(socket); | |
}) | |
.on('close', () => { | |
passthrough.unpipe(socket); | |
}); | |
return passthrough; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment