const http = require('http') const fsPromises = require('fs').promises const path = require('path') const crypto = require('crypto') const sleep = require('util').promisify(setTimeout) const argvIndexOfFile = process.argv.indexOf(__filename) let SLEEP_TIME = parseInt(process.argv[argvIndexOfFile + 1]) if (isNaN(SLEEP_TIME)) { SLEEP_TIME = 200 } let CACHE_MAX_AGE = parseInt(process.argv[argvIndexOfFile + 2]) if (isNaN(CACHE_MAX_AGE)) { CACHE_MAX_AGE = 0 } let PORT = parseInt(process.argv[argvIndexOfFile + 3]) if (isNaN(PORT)) { PORT = 8000 } function sha384(data) { const hash = crypto.createHash('sha384') hash.update(data) return hash.digest('base64') } async function requestListener(req, res) { await sleep(SLEEP_TIME) let headers = { 'Content-Type': 'text/html', } let pathString = req.url.substr(1) let page = parseInt(pathString) if (pathString == '') { page = 1 } let content = '' const jsContent = await fsPromises.readFile(path.resolve(__dirname, '../instantpage.js')) const jsHash = sha384(jsContent) if (pathString == 'instantpage.js') { headers['Content-Type'] = 'text/javascript' content += jsContent } else if (!isNaN(page)) { if (CACHE_MAX_AGE) { headers['Cache-Control'] = `max-age=${CACHE_MAX_AGE}` } content += await fsPromises.readFile(path.resolve(__dirname, 'header.html')) content += `