1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| const EventEmitter = require("events"); const Request = require("request"); const { getRead, getLength, getTotal, startNum } = require("../utils/index"); const fs = require("fs");
class Downloader extends EventEmitter { constructor() { super(); this.instance = null; this.request = this.request.bind(this); }
static init() { if (!this.instance) { this.instance = new this(); } return this.instance; }
}} options { pipe, hiden, time, size, readable, ...opts }
request(options) { const that = this; const { pipe, hiden, time, size, ...opts } = options; const start = startNum(time); let read = getRead(options); let response = 0; let total = 0; let speed = 0;
const res = Request(opts); const Interval = setInterval(() => { that.emit("progress", { completed: read, total, hiden, speed, time: { start }, status: { down: "正在下载。..", end: "完成、n" } }); speed = 0; }, 1000); read && res.setHeader("Range", `bytes=${read}-`);
return new Promise(function(resolve) { res.on("response", resp => { const length = resp.headers["content-length"]; response = getLength( read && length !== undefined ? read + (length - 0) : length, size ); }) .on("data", function(data) { speed += data.length; read += data.length; total = getTotal( size, response, read ); }) .on("end", () => { that.emit("progress", { completed: read, total: total, hiden, speed, time: { start }, status: { down: "正在下载。..", end: "完成、n" } }); clearInterval(Interval); resolve(); }); download(res, pipe, read); }); } }
function download(data, dir, append) { if (dir && dir.length) { const opts = append ? { flags: "a" } : undefined; data.pipe(fs.createWriteStream(dir || "./", opts)); } } module.exports = Downloader;
|