Created
July 9, 2019 13:14
-
-
Save J-Cake/78ce059972595823243526e022e327e4 to your computer and use it in GitHub Desktop.
This script records progress made over download without using the `pipe` function and is incredibly memory efficient.
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 http = require('http'); | |
const fs = require('fs'); | |
const path = require('path'); | |
const fileLocation = path.join(__dirname, 'video.mp4'); | |
fs.writeFileSync(fileLocation, ''); | |
const file = fs.createWriteStream(fileLocation); | |
function printSize(bytes) { | |
let output = bytes; | |
let steps = 0; | |
units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; | |
while (output > 1024) { | |
output /= 1024; | |
steps++; | |
} | |
return parseFloat(output).toFixed(2) + " " + units[steps]; | |
} | |
const memoryUsage = [0]; | |
let progress = 0; | |
const req = http.request(process.argv[2], res => { | |
console.log(); | |
const size = Number(res.headers['content-length']); | |
res.on('data', data => { | |
file.write(data); | |
progress += Buffer.byteLength(data); | |
process.stdout.write("\r" + Math.floor((progress / size * 100)) + "%"); | |
memoryUsage.push(process.memoryUsage().heapUsed); | |
}); | |
file; | |
res.on('end', function() { | |
file.close(); | |
const avg = memoryUsage.reduce((a, i) => a + i) / memoryUsage.length; | |
const max = Math.max(...memoryUsage); | |
const min = Math.min(...memoryUsage); | |
console.log("\n Memory Usage Statistics for", printSize(size)); | |
console.log("Avg:", printSize(avg), "Max:", printSize(max), "Min:", printSize(min)); | |
}); | |
}); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment