day-2.mjs

December 26, 2021 ยท View on GitHub

/////////////////////////////////////////////////////////////////////////////// // Day 2 - Cleaning up the mess // // #region problem // /////////////////////////////////////////////////////////////////////////////// // Write a class that performs a http.post() with a body when it is GC'd // // The fetch should be a JSON array of ID strings, it should not send the // // request until all IDs have been gathered // ///////////////////////////////////////////////////////////////////////////////

class LineWriter { // ... /**

  • @param {string} url URL to send the POST to / constructor(url) { // ... } /*
  • Add an id string to
  • @param {string} id
  • @returns {void} */ addID(id) { } }

/////////////////////////////////////////////////////////////////////////////// // #endregion problem // ///////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////// // Test - Leave code after this alone // // #region test // ///////////////////////////////////////////////////////////////////////////////

import assert from 'node:assert/strict'; import {once} from 'node:events'; import http from 'node:http';

// generates garbage to get GC to run // you can always watch via node --trace-gc setInterval(() => { [].concat(0); [].concat(0); [].concat(0); [].concat(0); }, 0);

const ids = [ String(Math.random()), String(Math.random()), String(Math.random()), String(Math.random()), ];

const server = http.createServer(async (req, res) => { console.error(req.method, req.url); if (req.method !== 'POST') { res.writeHead(400); res.end('invalid http method'); return; } let body = ''; req.setEncoding('utf8'); for await (const chunk of req) { body += chunk; } const postedIDs = JSON.parse(body); try { assert.strictEqual( JSON.stringify(postedIDs), JSON.stringify(ids), ); } catch (e) { res.writeHead(500); throw e; } res.end(); process.exit(0); }).listen(0, '127.0.0.1'); await once(server, 'listening');

let writer = new LineWriter(http://127.0.0.1:${server.address().port}/); for (const id of ids) { writer.addID(id); } // allow the writer to GC writer = null;

/////////////////////////////////////////////////////////////////////////////// // #endregion test // ///////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////// // Hints - Encoded in ROT13 encoded // // #region hints // /////////////////////////////////////////////////////////////////////////////// // // // Fvzcyr: SvanyvmngvbaErtvfgel yrgf lbh yvfgra sbe TP. // // // /////////////////////////////////////////////////////////////////////////////// // #endregion hints // ///////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////// // Use Cases // // #region uses // /////////////////////////////////////////////////////////////////////////////// // // // This kind of workflow can help with things like the SELECT n+1 problem // // where you don't know when something is actually done / can't require a // // .dispatch method be called // // // /////////////////////////////////////////////////////////////////////////////// // #endregion uses // ///////////////////////////////////////////////////////////////////////////////