Created
July 31, 2022 15:01
-
-
Save semlinker/49d5eb2de15f04ab9ef1edf10dc302f1 to your computer and use it in GitHub Desktop.
HTTP Transfer Large Files
This file contains hidden or 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 fs = require("fs"); | |
const http = require("http"); | |
const buffer = fs.readFileSync(__dirname + "/first-100-lines.txt"); | |
const lines = buffer.toString("utf-8").split("\n"); | |
const chunks = chunk(lines, 10); | |
function chunk(arr, len) { | |
let chunks = [], | |
i = 0, | |
n = arr.length; | |
while (i < n) { | |
chunks.push(arr.slice(i, (i += len))); | |
} | |
return chunks; | |
} | |
http | |
.createServer(async function (req, res) { | |
res.writeHead(200, { | |
"Content-Type": "text/plain;charset=utf-8", | |
"Transfer-Encoding": "chunked", | |
"Access-Control-Allow-Origin": "*", | |
}); | |
for (let index = 0; index < chunks.length; index++) { | |
setTimeout(() => { | |
let content = chunks[index].join("&"); | |
res.write(`${content.length.toString(16)}\r\n${content}\r\n`); | |
}, index * 1000); | |
} | |
setTimeout(() => { | |
res.end(); | |
}, chunks.length * 1000); | |
}) | |
.listen(3000, () => { | |
console.log("app starting at port 3000"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment