day-4.mjs
December 28, 2021 ยท View on GitHub
/////////////////////////////////////////////////////////////////////////////// // Day 4 - Silent connection, holy connection // // #region problem // /////////////////////////////////////////////////////////////////////////////// // Write a createConnection function that will create a connection to a // // server, but won't keep the process alive. // ///////////////////////////////////////////////////////////////////////////////
/**
- @param {{port: number, host: string}} netCreateConnectionOptions
- @returns {void} */ function createConnection(netCreateConnectionOptions) { // ... }
/////////////////////////////////////////////////////////////////////////////// // #endregion problem // ///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// // Test - Leave code after this alone // // #region test // ///////////////////////////////////////////////////////////////////////////////
import assert from 'node:assert'; import {once} from 'node:events'; import net from 'node:net';
const server = net.createServer((conn) => {
function fail(evt) {
return (...args) => {
assert.fail(connection saw "${evt}" event, should not be closed);
};
}
conn.on('close', fail('close'));
conn.on('error', fail('error'));
conn.on('timeout', fail('timeout'));
conn.on('end', fail('end'));
process.exitCode = 0;
conn.unref();
server.unref();
}).listen(0, '127.0.0.1');
await once(server, 'listening');
process.exitCode = 1; createConnection({ port: server.address().port, host: server.address().address, });
/////////////////////////////////////////////////////////////////////////////// // #endregion test // ///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// // Hints - Encoded in ROT13 encoded // // #region hints // /////////////////////////////////////////////////////////////////////////////// // // // Fvzcyr: gur grfg unq gb hfr gur zrgubq gung ceriragf ybat yvirq erfbheprf // // sebz xrrcvat gur cebprff nyvir. // // // /////////////////////////////////////////////////////////////////////////////// // #endregion hints // ///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// // Use Cases // // #region uses // /////////////////////////////////////////////////////////////////////////////// // // // Sometimes a server needs to be able to open a bi-directional streaming // // connection but it shouldn't prevent the process from gracefully exiting. // // // /////////////////////////////////////////////////////////////////////////////// // #endregion uses // ///////////////////////////////////////////////////////////////////////////////