Skip to content

Instantly share code, notes, and snippets.

@forivall
Created February 4, 2025 23:42
Show Gist options
  • Save forivall/7e9e1d77e772b6259135ce5b6c7e1279 to your computer and use it in GitHub Desktop.
Save forivall/7e9e1d77e772b6259135ce5b6c7e1279 to your computer and use it in GitHub Desktop.
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