msb-file 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ "use strict";var Q=Object.create;var K=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var Z=Object.getOwnPropertyNames;var _=Object.getPrototypeOf,x=Object.prototype.hasOwnProperty;var ee=(r,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of Z(e))!x.call(r,t)&&t!==n&&K(r,t,{get:()=>e[t],enumerable:!(a=X(e,t))||a.enumerable});return r};var L=(r,e,n)=>(n=r!=null?Q(_(r)):{},ee(e||!r||!r.__esModule?K(n,"default",{value:r,enumerable:!0}):n,r));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("localforage"),re=require("js-md5");function U(r,e,n,a,t,u,i){try{var l=r[u](i),o=l.value}catch(h){return void n(h)}l.done?e(o):Promise.resolve(o).then(a,t)}function g(r){return function(){var e=this,n=arguments;return new Promise(function(a,t){var u=r.apply(e,n);function i(o){U(u,a,t,i,l,"next",o)}function l(o){U(u,a,t,i,l,"throw",o)}i(void 0)})}}function ne(r,e){if(!(r instanceof e))throw new TypeError("Cannot call a class as a function")}function W(r,e){for(var n=0;n<e.length;n++){var a=e[n];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(r,q(a.key),a)}}function te(r,e,n){return e&&W(r.prototype,e),n&&W(r,n),Object.defineProperty(r,"prototype",{writable:!1}),r}function m(r,e,n){return(e=q(e))in r?Object.defineProperty(r,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):r[e]=n,r}function c(){var r,e,n=typeof Symbol=="function"?Symbol:{},a=n.iterator||"@@iterator",t=n.toStringTag||"@@toStringTag";function u(p,y,P,j){var f=y&&y.prototype instanceof l?y:l,G=Object.create(f.prototype);return C(G,"_invoke",(function(F,Y,J){var E,d,w,B=0,T=J||[],z=!1,R={p:0,n:0,v:r,a:A,f:A.bind(r,4),d:function(S,k){return E=S,d=0,w=r,R.n=k,i}};function A(S,k){for(d=S,w=k,e=0;!z&&B&&!N&&e<T.length;e++){var N,b=T[e],$=R.p,I=b[2];S>3?(N=I===k)&&(w=b[(d=b[4])?5:(d=3,3)],b[4]=b[5]=r):b[0]<=$&&((N=S<2&&$<b[1])?(d=0,R.v=k,R.n=b[1]):$<I&&(N=S<3||b[0]>k||k>I)&&(b[4]=S,b[5]=k,R.n=I,d=0))}if(N||S>1)return i;throw z=!0,k}return function(S,k,N){if(B>1)throw TypeError("Generator is already running");for(z&&k===1&&A(k,N),d=k,w=N;(e=d<2?r:w)||!z;){E||(d?d<3?(d>1&&(R.n=-1),A(d,w)):R.n=w:R.v=w);try{if(B=2,E){if(d||(S="next"),e=E[S]){if(!(e=e.call(E,w)))throw TypeError("iterator result is not an object");if(!e.done)return e;w=e.value,d<2&&(d=0)}else d===1&&(e=E.return)&&e.call(E),d<2&&(w=TypeError("The iterator does not provide a '"+S+"' method"),d=1);E=r}else if((e=(z=R.n<0)?w:F.call(Y,R))!==i)break}catch(b){E=r,d=1,w=b}finally{B=1}}return{value:e,done:z}}})(p,P,j),!0),G}var i={};function l(){}function o(){}function h(){}e=Object.getPrototypeOf;var v=[][a]?e(e([][a]())):(C(e={},a,function(){return this}),e),s=h.prototype=l.prototype=Object.create(v);function D(p){return Object.setPrototypeOf?Object.setPrototypeOf(p,h):(p.__proto__=h,C(p,t,"GeneratorFunction")),p.prototype=Object.create(s),p}return o.prototype=h,C(s,"constructor",h),C(h,"constructor",o),o.displayName="GeneratorFunction",C(h,t,"GeneratorFunction"),C(s),C(s,t,"Generator"),C(s,a,function(){return this}),C(s,"toString",function(){return"[object Generator]"}),(c=function(){return{w:u,m:D}})()}function C(r,e,n,a){var t=Object.defineProperty;try{t({},"",{})}catch{t=0}C=function(u,i,l,o){function h(v,s){C(u,v,function(D){return this._invoke(v,s,D)})}i?t?t(u,i,{value:l,enumerable:!o,configurable:!o,writable:!o}):u[i]=l:(h("next",0),h("throw",1),h("return",2))},C(r,e,n,a)}function ae(r){if(r!=null){var e=r[typeof Symbol=="function"&&Symbol.iterator||"@@iterator"],n=0;if(e)return e.call(r);if(typeof r.next=="function")return r;if(!isNaN(r.length))return{next:function(){return r&&n>=r.length&&(r=void 0),{value:r&&r[n++],done:!r}}}}throw new TypeError(typeof r+" is not iterable")}function ie(r,e){if(typeof r!="object"||!r)return r;var n=r[Symbol.toPrimitive];if(n!==void 0){var a=n.call(r,e);if(typeof a!="object")return a;throw new TypeError("@@toPrimitive must return a primitive value.")}return(e==="string"?String:Number)(r)}function q(r){var e=ie(r,"string");return typeof e=="symbol"?e:e+""}var H=(function(r){return r.PENDING="pending",r.PAUSE="pause",r.DOWNLOADING="downloading",r.FINISHED="finished",r.ERROR="error",r})(H||{}),V=(function(r){return r.READY="ready",r.STATUS_UPDATE="status_update",r.PROGRESS_UPDATE="progress_update",r})(V||{}),M=(function(){function r(e){var n=this,a=e.url,t=e.fileName,u=e.chunkSize,i=u===void 0?10*1024*1024:u,l=e.onReady,o=e.onProgress,h=e.onStatusChange;ne(this,r),m(this,"url",void 0),m(this,"fileName",void 0),m(this,"isReady",!1),m(this,"abortController",void 0),m(this,"currentChunk",0),m(this,"chunkSize",void 0),m(this,"totalChunk",0),m(this,"totalSize",0),m(this,"status","pending"),m(this,"onReady",void 0),m(this,"onStatusChange",void 0),m(this,"onProgress",void 0),m(this,"getURLMd5",(function(){var v;return function(){return v||(v=re.md5(n.url))}})()),this.url=a,this.fileName=t,this.chunkSize=i,this.abortController=new AbortController,this.onReady=l,this.onProgress=o,this.onStatusChange=h,this.openDB()}return te(r,[{key:"changeStatus",value:function(n){var a,t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:null;this.status=n,(a=this.onStatusChange)===null||a===void 0||a.call(this,n,t)}},{key:"openDB",value:(function(){var e=g(c().m(function a(){var t,u;return c().w(function(i){for(;;)switch(i.p=i.n){case 0:return i.p=0,this.changeStatus("pending"),i.n=1,O.ready();case 1:return i.n=2,this.init();case 2:(t=this.onReady)===null||t===void 0||t.call(this),this.isReady=!0,i.n=4;break;case 3:i.p=3,u=i.v,this.changeStatus("error",u);case 4:return i.a(2)}},a,this,[[0,3]])}));function n(){return e.apply(this,arguments)}return n})()},{key:"getChunkKeys",value:(function(){var e=g(c().m(function a(){var t,u;return c().w(function(i){for(;;)switch(i.n){case 0:return t=this.getURLMd5(),i.n=1,O.keys();case 1:return u=i.v,i.a(2,u.filter(function(l){return l.startsWith("".concat(t,"_"))}))}},a,this)}));function n(){return e.apply(this,arguments)}return n})()},{key:"getCompletedCount",value:(function(){var e=g(c().m(function a(){var t;return c().w(function(u){for(;;)switch(u.n){case 0:return u.n=1,this.getChunkKeys();case 1:return t=u.v,u.a(2,t.length)}},a,this)}));function n(){return e.apply(this,arguments)}return n})()},{key:"getResumePosition",value:(function(){var e=g(c().m(function a(){var t,u,i,l;return c().w(function(o){for(;;)switch(o.n){case 0:return o.n=1,this.getChunkKeys();case 1:for(t=o.v,u=t.map(function(h){return parseInt(h.split("_")[1],10)}).filter(function(h){return!isNaN(h)}),i=0,l=new Set(u);l.has(i);)i++;return o.a(2,i)}},a,this)}));function n(){return e.apply(this,arguments)}return n})()},{key:"init",value:(function(){var e=g(c().m(function a(){var t;return c().w(function(u){for(;;)switch(u.n){case 0:return u.n=1,this.getFileSize();case 1:return t=u.v,this.totalSize=t,this.totalChunk=Math.ceil(this.totalSize/this.chunkSize),u.n=2,this.getResumePosition();case 2:this.currentChunk=u.v,this.updateProgress();case 3:return u.a(2)}},a,this)}));function n(){return e.apply(this,arguments)}return n})()},{key:"getFileSize",value:(function(){var e=g(c().m(function a(){var t,u,i,l,o,h,v;return c().w(function(s){for(;;)switch(s.p=s.n){case 0:return s.p=0,s.n=1,fetch(this.url,{method:"HEAD"});case 1:if(t=s.v,u=t.headers,i=u.get("content-length"),!i){s.n=2;break}return s.a(2,Number(i));case 2:s.n=4;break;case 3:s.p=3,s.v;case 4:return s.n=5,fetch(this.url,{method:"GET",headers:{Range:"bytes=0-0"}});case 5:if(l=s.v,o=l.headers,h=o.get("content-range"),!h){s.n=6;break}if(v=h.match(/\/(\d+)$/),!v){s.n=6;break}return s.a(2,Number(v[1]));case 6:throw new Error("无法获取文件大小");case 7:return s.a(2)}},a,this,[[0,3]])}));function n(){return e.apply(this,arguments)}return n})()},{key:"getCurrentChunkName",value:function(n){var a=this.getURLMd5();return"".concat(a,"_").concat(n)}},{key:"getChunkData",value:(function(){var e=g(c().m(function a(t,u){var i,l;return c().w(function(o){for(;;)switch(o.n){case 0:return o.n=1,fetch(this.url,{headers:{Range:"bytes=".concat(t,"-").concat(u)},signal:(i=this.abortController)===null||i===void 0?void 0:i.signal});case 1:return l=o.v,o.a(2,l.arrayBuffer())}},a,this)}));function n(a,t){return e.apply(this,arguments)}return n})()},{key:"abort",value:function(){var n;(n=this.abortController)===null||n===void 0||n.abort(),this.abortController=new AbortController}},{key:"updateProgress",value:(function(){var e=g(c().m(function a(){var t,u,i;return c().w(function(l){for(;;)switch(l.n){case 0:return l.n=1,this.getCompletedCount();case 1:u=l.v,i=(u/this.totalChunk*100).toFixed(2),(t=this.onProgress)===null||t===void 0||t.call(this,i);case 2:return l.a(2)}},a,this)}));function n(){return e.apply(this,arguments)}return n})()},{key:"resetState",value:function(){this.currentChunk=0,this.updateProgress(),this.changeStatus("pending")}},{key:"start",value:function(){this.isReady&&(this.changeStatus("downloading"),this.download())}},{key:"pause",value:function(){this.changeStatus("pause"),this.abort()}},{key:"resume",value:(function(){var e=g(c().m(function a(){return c().w(function(t){for(;;)switch(t.n){case 0:return t.n=1,this.getResumePosition();case 1:this.currentChunk=t.v,this.changeStatus("downloading"),this.download();case 2:return t.a(2)}},a,this)}));function n(){return e.apply(this,arguments)}return n})()},{key:"cancel",value:(function(){var e=g(c().m(function a(){return c().w(function(t){for(;;)switch(t.n){case 0:return this.abort(),t.n=1,this.remove();case 1:this.resetState();case 2:return t.a(2)}},a,this)}));function n(){return e.apply(this,arguments)}return n})()},{key:"uploadSingeChunk",value:(function(){var e=g(c().m(function a(t){var u,i,l,o;return c().w(function(h){for(;;)switch(h.n){case 0:return u=this.getCurrentChunkName(t),h.n=1,O.getItem(u);case 1:if(!h.v){h.n=2;break}return this.updateProgress(),h.a(2);case 2:return i=t*this.chunkSize,l=Math.min(i+this.chunkSize-1,this.totalSize-1),h.n=3,this.getChunkData(i,l);case 3:return o=h.v,h.n=4,O.setItem(u,new Blob([o]));case 4:this.updateProgress();case 5:return h.a(2)}},a,this)}));function n(a){return e.apply(this,arguments)}return n})()},{key:"download",value:(function(){var e=g(c().m(function a(){var t=this,u,i,l,o,h,v;return c().w(function(s){for(;;)switch(s.p=s.n){case 0:s.p=0,u=4,i=[],l=(function(){var D=g(c().m(function p(y){return c().w(function(P){for(;;)switch(P.n){case 0:return P.n=1,t.uploadSingeChunk(y);case 1:return P.a(2)}},p)}));return function(y){return D.apply(this,arguments)}})(),o=c().m(function D(){var p;return c().w(function(y){for(;;)switch(y.n){case 0:if(!(i.length>=u)){y.n=1;break}return y.n=1,Promise.race(i);case 1:p=l(t.currentChunk++).finally(function(){var P=i.indexOf(p);P>-1&&i.splice(P,1)}),i.push(p);case 2:return y.a(2)}},D)});case 1:if(!(this.currentChunk<this.totalChunk)){s.n=3;break}return s.d(ae(o()),2);case 2:s.n=1;break;case 3:return s.n=4,Promise.all(i);case 4:this.changeStatus("finished"),this.mergeAndDownload(),s.n=7;break;case 5:if(s.p=5,v=s.v,h=v,!(h&&h.name==="AbortError")){s.n=6;break}return s.a(2);case 6:this.changeStatus("error",v);case 7:return s.a(2)}},a,this,[[0,5]])}));function n(){return e.apply(this,arguments)}return n})()},{key:"remove",value:(function(){var e=g(c().m(function a(){var t;return c().w(function(u){for(;;)switch(u.n){case 0:return u.n=1,this.getChunkKeys();case 1:return t=u.v,u.n=2,Promise.all(t.map(function(i){return O.removeItem(i)}));case 2:return u.a(2)}},a,this)}));function n(){return e.apply(this,arguments)}return n})()},{key:"mergeAndDownload",value:(function(){var e=g(c().m(function a(){var t=this,u,i,l,o,h,v,s,D,p,y,P,j;return c().w(function(f){for(;;)switch(f.p=f.n){case 0:return f.n=1,this.getCompletedCount();case 1:if(u=f.v,!(u<this.totalChunk)){f.n=2;break}return this.changeStatus("error",new Error("下载不完整,请取消再重新下载")),f.a(2);case 2:return f.n=3,import("streamsaver");case 3:i=f.v,l=i.createWriteStream(this.fileName,{size:this.totalSize}),o=l.getWriter(),h=this.getURLMd5(),f.p=4,v=0;case 5:if(!(v<this.totalChunk)){f.n=13;break}return f.n=6,O.getItem("".concat(h,"_").concat(v));case 6:if(s=f.v,s){f.n=7;break}throw new Error("Chunk ".concat(v," missed!"));case 7:D=s.stream().getReader();case 8:return f.n=9,D.read();case 9:if(p=f.v,y=p.done,P=p.value,!y){f.n=10;break}return f.a(3,12);case 10:return f.n=11,o.write(P);case 11:f.n=8;break;case 12:v++,f.n=5;break;case 13:return f.n=14,o.close();case 14:setTimeout(g(c().m(function G(){return c().w(function(F){for(;;)switch(F.n){case 0:return F.n=1,t.remove();case 1:t.resetState();case 2:return F.a(2)}},G)})),2e3),f.n=16;break;case 15:f.p=15,j=f.v,this.changeStatus("error",j),o.abort&&o.abort(j);case 16:return f.a(2)}},a,this,[[4,15]])}));function n(){return e.apply(this,arguments)}return n})()},{key:"destroy",value:function(){this.abort()}}],[{key:"setMitmPath",value:function(n){import("streamsaver").then(function(a){a.mitm=n})}}])})();m(M,"EVENTS",V);m(M,"STATUS",H);exports.Download=M;
@@ -12,8 +12,8 @@ export declare class Download {
12
12
  status: STATUS;
13
13
  onReady: DownloadOptions['onReady'];
14
14
  onStatusChange: DownloadOptions['onStatusChange'];
15
- onProgressUpdate: DownloadOptions['onProgressUpdate'];
16
- constructor({ url, fileName, chunkSize, onReady, onProgressUpdate, onStatusChange, }: DownloadOptions);
15
+ onProgress: DownloadOptions['onProgress'];
16
+ constructor({ url, fileName, chunkSize, onReady, onProgress, onStatusChange, }: DownloadOptions);
17
17
  static setMitmPath(path: string): void;
18
18
  changeStatus(value: STATUS, error?: any): void;
19
19
  openDB(): Promise<void>;
@@ -39,15 +39,12 @@ export declare class Download {
39
39
  destroy(): void;
40
40
  }
41
41
 
42
- export declare type DownloadOptions = {
42
+ declare type DownloadOptions = {
43
43
  url: string;
44
44
  fileName: string;
45
45
  onReady?: () => void;
46
- onStatusChange?: (data: {
47
- status: STATUS;
48
- error: any;
49
- }) => void;
50
- onProgressUpdate?: (value: string) => void;
46
+ onStatusChange?: (status: STATUS, error: any) => void;
47
+ onProgress?: (value: string) => void;
51
48
  onError?: (error: any) => void;
52
49
  chunkSize?: number;
53
50
  };
@@ -58,7 +55,7 @@ declare enum EVENTS {
58
55
  PROGRESS_UPDATE = "progress_update"
59
56
  }
60
57
 
61
- export declare enum STATUS {
58
+ declare enum STATUS {
62
59
  PENDING = "pending",
63
60
  PAUSE = "pause",
64
61
  DOWNLOADING = "downloading",