kratos-core
Version:
kratos-core is an open-source, elegant and battery-included Minecraft Launcher API module written in TypeScript in order to be the based of KratosLauncher
1 lines • 4.55 kB
JavaScript
"use strict";var __awaiter=function(t,o,s,e){return new(s||(s=Promise))(function(i,r){function n(t){try{h(e.next(t))}catch(t){r(t)}}function a(t){try{h(e.throw(t))}catch(t){r(t)}}function h(t){var o;t.done?i(t.value):((o=t.value)instanceof s?o:new s(function(t){t(o)})).then(n,a)}h((e=e.apply(t,o||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.createAttemptDownload=exports.DownloadMatchingProcess=exports.DownloadMatchingObserver=exports.DownloadHashObservation=exports.createDownloadProcess=exports.DownloadProcess=exports.DownloadProgress=void 0;const path_1=require("path"),utils_1=require("./utils"),node_fetch_1=require("node-fetch"),fs_extra_1=require("fs-extra"),events_1=require("events"),crypto_1=require("crypto");class DownloadProgress extends events_1.EventEmitter{transferBytes(t){this.bytesTransferred+=t}constructor(t){super(),this.bytesTransferred=0,this.size=-1,this.size=t||-1}}exports.DownloadProgress=DownloadProgress;class DownloadProcess{startDownload(t){return new Promise((o,s)=>__awaiter(this,void 0,void 0,function*(){try{let e=(0,path_1.dirname)(this.info.destination);yield(0,fs_extra_1.ensureDir)(e);let i=(0,fs_extra_1.createWriteStream)(this.info.destination,{mode:493});i.on("error",t=>__awaiter(this,void 0,void 0,function*(){return s(t)}));let r=yield(0,node_fetch_1.default)(this.info.url,Object.assign({method:"get"},t));if(!r.ok){let t=Error("Unable to do HTTP Get with status "+r.status+"; statusText: "+r.statusText);return void 0!==this.options&&void 0!==this.options.progress&&this.options.progress.emit("error",t),s(t)}let n=r.body;n.pipe(i),n.on("error",t=>(void 0!==this.options.progress&&this.options.progress&&this.options.progress.emit("error",t),s(t))),n.on("data",t=>{void 0!==this.options&&this.options.progress&&(this.options.progress.transferBytes(t.length),this.options.progress.emit("progress",t)),void 0!==this.options&&void 0!==this.options.hashObservation&&this.options.hashObservation.update(t)}),n.on("close",()=>(void 0!==this.options&&void 0!==this.options.progress&&this.options.progress.emit("finish",this.info),o(this.info)))}catch(t){return void 0!==this.options&&void 0!==this.options.progress&&this.options.progress.emit("error",t),s(t)}}))}getOptions(){return this.options}constructor(t,o){if(this.info=t,this.options=o,void 0===t)throw Error("Invalid download process parameter");if(void 0===this.info.destination||void 0===this.info.url)throw Error("Invalid download info")}}function createDownloadProcess(t,o){return new DownloadProcess(t,o)}exports.DownloadProcess=DownloadProcess,exports.createDownloadProcess=createDownloadProcess;class DownloadHashObservation{update(t){this.hash.update(t)}digest(){return this.hash.digest()}getHash(){return this.hash}constructor(t){this.hash=(0,crypto_1.createHash)(t)}}exports.DownloadHashObservation=DownloadHashObservation;class DownloadMatchingObserver extends events_1.EventEmitter{}exports.DownloadMatchingObserver=DownloadMatchingObserver;class DownloadMatchingProcess{createSubprocess(){let t=new DownloadHashObservation(this.options&&this.options.algorithm||"sha1");return createDownloadProcess(this.info,{hashObservation:t,progress:this.options&&this.options.progress||void 0})}startDownload(t){return __awaiter(this,void 0,void 0,function*(){return new Promise((o,s)=>__awaiter(this,void 0,void 0,function*(){0===this.stack.length&&this.stack.push(this.createSubprocess());let e=this.options&&this.options.maxAttempt||3;for(;this.attempt<e;){let s=this.stack.pop();yield s.startDownload(t);let e=s.getOptions().hashObservation;if(e.digest().toString("hex")===this.hashValue)return void 0!==this.options&&void 0!==this.options.observer&&this.options.observer.emit("success",this.info,this),o(this.info);void 0!==this.options&&void 0!==this.options.observer&&this.options.observer.emit("retry",this.info,this.attempt,this),this.stack.push(this.createSubprocess()),this.attempt++}if(this.attempt===e)return void 0!==this.options&&void 0!==this.options.observer&&this.options.observer.emit("corrupted",this.info,this),s(Error(`Maximum attempt (${this.attempt} over ${this.options.maxAttempt}). The downloading file is broken or check your input hash`))}))})}getDownloadInfo(){return this.info}constructor(t,o,s){this.stack=[],utils_1.Preconditions.notNull(t),utils_1.Preconditions.notNull(o),this.info=t,this.hashValue=o,this.options=s,this.attempt=0}}exports.DownloadMatchingProcess=DownloadMatchingProcess,exports.createAttemptDownload=function(t,o,s){return new DownloadMatchingProcess(t,o,s)};