2 lines
20 KiB
JavaScript
2 lines
20 KiB
JavaScript
var dbToCloud=function(t){"use strict";function e(t,e,n,r,o,i,c){try{var a=t[i](c),l=a.value}catch(t){return void n(t)}a.done?e(l):Promise.resolve(l).then(r,o)}function n(t){return function(){var n=this,r=arguments;return new Promise((function(o,i){var c=t.apply(n,r);function a(t){e(c,o,i,a,l,"next",t)}function l(t){e(c,o,i,a,l,"throw",t)}a(void 0)}))}}function r(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function o(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function i(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?o(n,!0).forEach((function(e){r(t,e,n[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):o(n).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}function c(t,e){if(null==t)return{};var n,r,o=function(t,e){if(null==t)return{};var n,r,o={},i=Object.keys(t);for(r=0;r<i.length;r++)n=i[r],e.indexOf(n)>=0||(o[n]=t[n]);return o}(t,e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);for(r=0;r<i.length;r++)n=i[r],e.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(t,n)&&(o[n]=t[n])}return o}function a(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if(!(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t)))return;var n=[],r=!0,o=!1,i=void 0;try{for(var c,a=t[Symbol.iterator]();!(r=(c=a.next()).done)&&(n.push(c.value),!e||n.length!==e);r=!0);}catch(t){o=!0,i=t}finally{try{r||null==a.return||a.return()}finally{if(o)throw i}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function l({maxActiveReader:t=1/0}={}){let e,n,r=0;const o={read:t=>i(t,!1),write:t=>i(t,!0),length:0};return o;function i(i,a){const l=function({fn:t,block:e=!1,prev:n,next:r,q:o=c(),q2:i=(t.length?c():null)}){return{fn:t,block:e,prev:n,next:r,q:o,q2:i}}({fn:i,block:a});return n?(n.next=l,l.prev=n,n=l,e||(e=n)):e=n=l,o.length++,function i(){const c=e;if(!c||c.block&&c.prev||c.prev&&c.prev.block||r>=t)return;c.block||r++;e=c.next;let a;try{a=c.fn(c.q2&&c.q2.resolve)}catch(t){return c.q.reject(t),void l()}c.q2&&c.q2.promise.then(u);if(a&&a.then){const t=a.then(c.q.resolve,c.q.reject);c.q2||t.then(l)}else if(c.q.resolve(a),!c.q2)return void l();i();function l(){u()}function u(t){c.prev&&(c.prev.next=c.next),c.next&&(c.next.prev=c.prev),n===c&&(n=c.prev),c.block||r--,o.length--,t&&t(),i()}}(),l.q.promise}function c(){const t={};return t.promise=new Promise((e,n)=>{t.resolve=e,t.reject=n}),t}}const u={};function s(t){return String.fromCharCode(parseInt(t.slice(1),16))}function p(t){return"%".concat("00".concat(t.charCodeAt(0).toString(16)).slice(-2))}Object.defineProperty(u,"__esModule",{value:!0}),u.encode=function(t){return btoa(encodeURIComponent(t).replace(/%[0-9A-F]{2}/g,s))},u.decode=function(t){return decodeURIComponent(Array.from(atob(t),p).join(""))};class f extends Error{constructor(t,e,n=e.message||"An error occured in db-to-cloud"){super(n),e.name&&(this.name=e.name),this.code=t,this.origin=e,Error.captureStackTrace&&Error.captureStackTrace(this,f)}}function h(t,e){const n=t.error;return"string"==typeof n?n.includes(e):n.error_summary&&n.error_summary.includes(e)}var d=Object.freeze({fsDrive:()=>{},github:function({owner:t,repo:e,getAccessToken:r,getOctokit:o=n((function*(){const t=yield Promise.resolve(e),e=t.Octokit||t.default&&t.default.Octokit||t,n=yield Promise.resolve(octokitPluginThrottling);return e.plugin(n.default||n)}))}){let i;const c=new Map,l={name:"github",init:function(){return h.apply(this,arguments)},get:y,put:m,post:function(t,e){return w.apply(this,arguments)},delete:function(t){return b.apply(this,arguments)},list:function(t){return d.apply(this,arguments)},shaCache:c};for(var s=0,p=Object.entries(l);s<p.length;s++){const t=a(p[s],2),e=t[0],r=t[1];"function"==typeof r&&(l[e]=function(){var t=n((function*(...t){try{return yield r(...t)}catch(t){if(404===t.status)throw new f("ENOENT",t);throw t}}));return function(){return t.apply(this,arguments)}}())}return l;function h(){return(h=n((function*(){const t=yield o();i=new t({auth:()=>n((function*(){return"token ".concat(yield r())}))(),throttle:{onAbuseLimit:(t,e)=>(console.warn("Abuse detected for request ".concat(e.method," ").concat(e.url)),!1),onRateLimit:(t,e)=>(console.warn("Request quota exhausted for request ".concat(e.method," ").concat(e.url)),!1)}})}))).apply(this,arguments)}function d(){return(d=n((function*(n){const r=yield i.repos.getContents({owner:t,repo:e,path:n}),o=[];var a=!0,l=!1,u=void 0;try{for(var s,p=r.data[Symbol.iterator]();!(a=(s=p.next()).done);a=!0){const t=s.value;o.push(t.name),c.set(t.path,t.sha)}}catch(t){l=!0,u=t}finally{try{a||null==p.return||p.return()}finally{if(l)throw u}}return o}))).apply(this,arguments)}function y(t){return v.apply(this,arguments)}function v(){return(v=n((function*(n){const r=yield i.repos.getContents({owner:t,repo:e,path:n});return c.set(r.data.path,r.data.sha),u.decode(r.data.content)}))).apply(this,arguments)}function m(t,e){return g.apply(this,arguments)}function g(){return(g=n((function*(n,r){const o={owner:t,repo:e,path:n,message:"",content:u.encode(r)};let a;c.has(n)&&(o.sha=c.get(n));try{a=yield i.repos.createOrUpdateFile(o)}catch(t){if(422===t.status&&!o.sha)return yield y(n),yield m(n,r);throw t}c.set(n,a.data.content.sha)}))).apply(this,arguments)}function w(){return(w=n((function*(n,r){let o;try{o=yield i.repos.createOrUpdateFile({owner:t,repo:e,path:n,message:"",content:u.encode(r)})}catch(t){if(422===t.status)throw new f("EEXIST",t);throw t}c.set(n,o.data.content.sha)}))).apply(this,arguments)}function b(){return(b=n((function*(n){c.has(n)||(yield y(n));try{yield i.repos.deleteFile({owner:t,repo:e,path:n,message:"",sha:c.get(n)})}catch(t){if(404===t.status)return;throw t}}))).apply(this,arguments)}},dropbox:function({getAccessToken:t,clientId:e,fetch:r=fetch,getDropbox:o=n((function*(){return Promise.resolve(i).then(t=>t.Dropbox||t.default.Dropbox)}))}){let i;return{name:"dropbox",init:function(){return c.apply(this,arguments)},get:function(t){return l.apply(this,arguments)},put:function(t,e){return u.apply(this,arguments)},post:function(t,e){return s.apply(this,arguments)},delete:function(t){return p.apply(this,arguments)},list:function(t){return a.apply(this,arguments)}};function c(){return(c=n((function*(){const n=yield o();(i=new n({fetch:r,clientId:e})).setAccessToken(yield t(i))}))).apply(this,arguments)}function a(){return(a=n((function*(t){const e=[];let n=yield i.filesListFolder({path:"/".concat(t)});var r=!0,o=!1,c=void 0;try{for(var a,l=n.entries[Symbol.iterator]();!(r=(a=l.next()).done);r=!0){const t=a.value;e.push(t.name)}}catch(t){o=!0,c=t}finally{try{r||null==l.return||l.return()}finally{if(o)throw c}}if(!n.has_more)return e;let u=n.cursor;for(;n.has_more;){u=(n=yield i.filesListFolderContinue({cursor:u})).cursor;var s=!0,p=!1,f=void 0;try{for(var h,d=n.entries[Symbol.iterator]();!(s=(h=d.next()).done);s=!0){const t=h.value;e.push(t.name)}}catch(t){p=!0,f=t}finally{try{s||null==d.return||d.return()}finally{if(p)throw f}}}return e}))).apply(this,arguments)}function l(){return(l=n((function*(t){let e;try{e=yield i.filesDownload({path:"/".concat(t)})}catch(t){if(h(t,"not_found"))throw new f("ENOENT",t);throw t}return e.fileBinary?e.fileBinary.toString():yield(n=e.fileBlob,new Promise((t,e)=>{const r=new FileReader;r.onload=()=>{t(r.result)},r.onerror=()=>{e(new Error("Failed to convert blob object to text"))},r.readAsText(n)}));var n}))).apply(this,arguments)}function u(){return(u=n((function*(t,e){yield i.filesUpload({contents:e,path:"/".concat(t),mode:"overwrite",autorename:!1})}))).apply(this,arguments)}function s(){return(s=n((function*(t,e){try{yield i.filesUpload({contents:e,path:"/".concat(t),mode:"add",autorename:!1})}catch(t){if(h(t,"conflict"))throw new f("EEXIST",t);throw t}}))).apply(this,arguments)}function p(){return(p=n((function*(t){try{yield i.filesDelete({path:"/".concat(t)})}catch(t){if(h(t,"not_found"))return;throw t}}))).apply(this,arguments)}},onedrive:function({getAccessToken:t,fetch:e=fetch}){return{name:"onedrive",get:function(t){return l.apply(this,arguments)},put:function(t,e){return u.apply(this,arguments)},post:function(t,e){return s.apply(this,arguments)},delete:function(t){return p.apply(this,arguments)},list:function(t){return a.apply(this,arguments)}};function r(t){return o.apply(this,arguments)}function o(){return(o=n((function*(n){let r=n.method,o=void 0===r?"GET":r,a=n.path,l=n.headers,u=n.format,s=void 0===u?"json":u,p=c(n,["method","path","headers","format"]);const h=yield e("https://graph.microsoft.com/v1.0/me/drive/special/approot".concat(a),i({method:o,headers:i({Authorization:"bearer ".concat(yield t())},l)},p));if(!h.ok){const t=(yield h.json()).error;throw new f(t.code,t)}if(s)return yield h[s]()}))).apply(this,arguments)}function a(){return(a=n((function*(t){return t&&(t=":/".concat(t,":")),(yield r({path:"".concat(t,"/children?select=name")})).value.map(t=>t.name)}))).apply(this,arguments)}function l(){return(l=n((function*(t){try{return yield r({path:":/".concat(t,":/content"),format:"text"})}catch(t){throw"itemNotFound"===t.code&&(t.code="ENOENT"),t}}))).apply(this,arguments)}function u(){return(u=n((function*(t,e){yield r({method:"PUT",path:":/".concat(t,":/content"),headers:{"Content-Type":"text/plain"},body:e})}))).apply(this,arguments)}function s(){return(s=n((function*(t,e){try{yield r({method:"PUT",path:":/".concat(t,":/content?@microsoft.graph.conflictBehavior=fail"),headers:{"Content-Type":"text/plain"},body:e})}catch(t){throw"nameAlreadyExists"===t.code&&(t.code="EEXIST"),t}}))).apply(this,arguments)}function p(){return(p=n((function*(t){try{yield r({method:"DELETE",path:":/".concat(t,":"),format:null})}catch(t){if("itemNotFound"===t.code)return;throw t}}))).apply(this,arguments)}},google:function({getAccessToken:t,fetch:e=fetch,FormData:r=FormData,Blob:o=Blob}){const a=new Map;let l;return{name:"google",get:function(t){return k.apply(this,arguments)},put:function(t,e){return O.apply(this,arguments)},post:x,delete:function(t){return C.apply(this,arguments)},list:function(t){return E.apply(this,arguments)},init:function(){return w.apply(this,arguments)},acquireLock:function(t){return p.apply(this,arguments)},releaseLock:function(){return h.apply(this,arguments)},fileMetaCache:a};function u(t,e){return s.apply(this,arguments)}function s(){return(s=n((function*(t,e){yield b({method:"DELETE",path:"https://www.googleapis.com/drive/v3/files/".concat(t,"/revisions/").concat(e),format:null})}))).apply(this,arguments)}function p(){return(p=n((function*(t){const e=a.get("lock.json"),n=(yield y(e.id,JSON.stringify({expire:Date.now()+60*t*1e3}))).headRevisionId,r=yield b({path:"https://www.googleapis.com/drive/v3/files/".concat(e.id,"/revisions?fields=revisions(id)")});for(let t=1;t<r.revisions.length;t++){const o=r.revisions[t].id;if(o===n)return void(l=n);if((yield b({path:"https://www.googleapis.com/drive/v3/files/".concat(e.id,"/revisions/").concat(o,"?alt=media")})).expire>Date.now())throw yield u(e.id,n),new f("EEXIST",new Error("failed to acquire lock"));yield u(e.id,o)}throw new Error("cannot find lock revision")}))).apply(this,arguments)}function h(){return(h=n((function*(){const t=a.get("lock.json");yield u(t.id,l),l=null}))).apply(this,arguments)}function d(){return(d=n((function*(t,e){t="https://www.googleapis.com/drive/v3/files?spaces=appDataFolder&fields=nextPageToken,files(id,name,headRevisionId)"+(t?"&"+t:"");let n=yield b({path:t});for(e(n);n.nextPageToken;)e(n=yield b({path:"".concat(t,"&pageToken=").concat(n.nextPageToken)}))}))).apply(this,arguments)}function y(t,e){return v.apply(this,arguments)}function v(){return(v=n((function*(t,e){return yield b({method:"PATCH",path:"https://www.googleapis.com/upload/drive/v3/files/".concat(t,"?uploadType=media&fields=headRevisionId"),headers:{"Content-Type":"text/plain"},body:e})}))).apply(this,arguments)}function m(t){return g.apply(this,arguments)}function g(){return(g=n((function*(t){t&&(t="q=".concat(encodeURIComponent(t))),yield function(t,e){return d.apply(this,arguments)}(t,t=>{var e=!0,n=!1,r=void 0;try{for(var o,i=t.files[Symbol.iterator]();!(e=(o=i.next()).done);e=!0){const t=o.value;a.set(t.name,t)}}catch(t){n=!0,r=t}finally{try{e||null==i.return||i.return()}finally{if(n)throw r}}})}))).apply(this,arguments)}function w(){return(w=n((function*(){yield m(),a.has("lock.json")||(yield x("lock.json","{}")),a.has("meta.json")||(yield x("meta.json","{}"))}))).apply(this,arguments)}function b(t){return j.apply(this,arguments)}function j(){return(j=n((function*(n){let r=n.method,o=void 0===r?"GET":r,a=n.path,l=n.headers,u=n.format,s=void 0===u?"json":u,p=c(n,["method","path","headers","format"]);const h=yield e(a,i({method:o,headers:i({Authorization:"Bearer ".concat(yield t())},l)},p));if(!h.ok){const t=(yield h.json()).error;throw new f(t.code,t)}if(s)return yield h[s]()}))).apply(this,arguments)}function E(){return(E=n((function*(t){return[...a.values()].filter(e=>e.name.startsWith(t+"/")).map(t=>t.name.split("/")[1])}))).apply(this,arguments)}function k(){return(k=n((function*(t){let e=a.get(t);if(!(e||(yield m("name = '".concat(t,"'")),e=a.get(t))))throw new f("ENOENT",new Error("metaCache doesn't contain ".concat(t)));try{return yield b({path:"https://www.googleapis.com/drive/v3/files/".concat(e.id,"?alt=media"),format:"text"})}catch(t){throw 404===t.code&&(t.code="ENOENT"),t}}))).apply(this,arguments)}function O(){return(O=n((function*(t,e){if(!a.has(t))return yield x(t,e);const n=a.get(t),r=yield y(n.id,e);n.headRevisionId=r.headRevisionId}))).apply(this,arguments)}function x(t,e){return T.apply(this,arguments)}function T(){return(T=n((function*(t,e){const n=new r,i={name:t,parents:["appDataFolder"]};n.append("metadata",new o([JSON.stringify(i)],{type:"application/json; charset=UTF-8"})),n.append("media",new o([e],{type:"text/plain"}));const c=yield b({method:"POST",path:"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,name,headRevisionId",body:n});a.set(c.name,c)}))).apply(this,arguments)}function C(){return(C=n((function*(t){const e=a.get(t);if(e)try{yield b({method:"DELETE",path:"https://www.googleapis.com/drive/v3/files/".concat(e.id),format:null})}catch(t){if(404===t.code)return;throw t}}))).apply(this,arguments)}}});function y(t){let e,n=0;return()=>(n&&clearTimeout(n),n=setTimeout(r),e||(e=function(){const t={};return t.promise=new Promise((e,n)=>{t.resolve=e,t.reject=n}),t}()),e.promise);function r(){Promise.resolve(t()).then(e.resolve,e.reject),n=0,e=null}}return t.dbToCloud=function({onGet:t,onPut:e,onDelete:r,onFirstSync:o,onWarn:i=console.error,compareRevision:c,getState:u,setState:s,lockExpire:p=60}){let f,h,d;const v=new Map,m=y(()=>s(f,h)),g=new Map,w=l();return{use:function(t){f=function(t){const e=Object.create(t);return e.get=function(){var e=n((function*(e){return JSON.parse(yield t.get(e))}));return function(t){return e.apply(this,arguments)}}(),e.put=function(){var e=n((function*(e,n){return yield t.put(e,JSON.stringify(n))}));return function(t,n){return e.apply(this,arguments)}}(),e.post=function(){var e=n((function*(e,n){return yield t.post(e,JSON.stringify(n))}));return function(t,n){return e.apply(this,arguments)}}(),e.acquireLock||(e.acquireLock=function(t){return r.apply(this,arguments)},e.releaseLock=function(){return o.apply(this,arguments)}),e.getMeta||(e.getMeta=function(){return i.apply(this,arguments)},e.putMeta=function(t){return c.apply(this,arguments)}),e.peekChanges||(e.peekChanges=function(t){return a.apply(this,arguments)}),e;function r(){return(r=n((function*(t){try{yield this.post("lock.json",{expire:Date.now()+60*t*1e3})}catch(t){if("EEXIST"===t.code){const t=yield this.get("lock.json");Date.now()>t.expire&&(yield this.delete("lock.json"))}throw t}}))).apply(this,arguments)}function o(){return(o=n((function*(){yield this.delete("lock.json")}))).apply(this,arguments)}function i(){return(i=n((function*(){try{return yield this.get("meta.json")}catch(t){if("ENOENT"===t.code)return{};throw t}}))).apply(this,arguments)}function c(){return(c=n((function*(t){yield this.put("meta.json",t)}))).apply(this,arguments)}function a(){return(a=n((function*(t){return(yield this.getMeta()).lastChange!==t.lastChange}))).apply(this,arguments)}}(t)},start:function(){return b.apply(this,arguments)},stop:function(){return j.apply(this,arguments)},put:function(t,e){if(!h||!h.enabled)return;h.queue.push({_id:t,_rev:e,action:"put"}),m()},delete:function(t,e){if(!h||!h.enabled)return;h.queue.push({_id:t,_rev:e,action:"delete"}),m()},syncNow:x,drive:()=>f};function b(){return(b=n((function*(){if(!f)throw new Error("cloud drive is undefined");f.init&&(yield f.init()),(h=(yield u(f))||{}).enabled=!0,h.queue||(h.queue=[]),null==h.lastChange&&(yield o()),yield x()}))).apply(this,arguments)}function j(){return(j=n((function*(){h.enabled=!1,yield w.write(n((function*(){f.uninit&&(yield f.uninit()),yield m()})))}))).apply(this,arguments)}function E(){return(E=n((function*(){if(!(d=yield f.getMeta()).lastChange||d.lastChange===h.lastChange)return;let t=[];if(h.lastChange){const e=Math.floor((d.lastChange-1)/100);let n=Math.floor(h.lastChange/100);for(;n<=e;){const e=yield f.get("changes/".concat(n,".json"));v.set(n,e),t=t.concat(e),n++}t=t.slice(h.lastChange%100)}else t=(yield f.list("docs")).map(t=>({action:"put",_id:t.slice(0,-5)}));const n=new Map;var o=!0,c=!1,l=void 0;try{for(var u,s=t[Symbol.iterator]();!(o=(u=s.next()).done);o=!0){const t=u.value;n.set(t._id,t)}}catch(t){c=!0,l=t}finally{try{o||null==s.return||s.return()}finally{if(c)throw l}}var p=!0,y=!1,w=void 0;try{for(var b,j=n[Symbol.iterator]();!(p=(b=j.next()).done);p=!0){const t=a(b.value,2),n=t[0],o=t[1];if("delete"===o.action)yield r(n,o._rev);else if("put"===o.action){let t;try{t=yield f.get("docs/".concat(n,".json"))}catch(t){if("ENOENT"===t.code){i("Cannot find ".concat(n,". Is it deleted without updating the history?"));continue}throw t}yield e(t)}o._rev&&g.set(n,o._rev)}}catch(t){y=!0,w=t}finally{try{p||null==j.return||j.return()}finally{if(y)throw w}}h.lastChange=d.lastChange,yield m()}))).apply(this,arguments)}function k(){return(k=n((function*(){if(!h.queue.length)return;const e=h.queue.slice(),n=new Map;var r=!0,o=!1,i=void 0;try{for(var l,u=e[Symbol.iterator]();!(r=(l=u.next()).done);r=!0){const t=l.value;n.set(t._id,t)}}catch(t){o=!0,i=t}finally{try{r||null==u.return||u.return()}finally{if(o)throw i}}const s=[];var p=!0,y=!1,w=void 0;try{for(var b,j=n.entries()[Symbol.iterator]();!(p=(b=j.next()).done);p=!0){const e=a(b.value,2),n=e[0],r=e[1],o=g.get(r._id);void 0!==o&&c(r._rev,o)<=0||("delete"===r.action?yield f.delete("docs/".concat(n,".json")):"put"===r.action&&(yield f.put("docs/".concat(n,".json"),yield t(n,r._rev))),g.set(n,r._rev),s.push(r))}}catch(t){y=!0,w=t}finally{try{p||null==j.return||j.return()}finally{if(y)throw w}}let E,k;if(d.lastChange){k=Math.floor(d.lastChange/100);const t=d.lastChange%100;E=(E=t?v.get(k)||(yield f.get("changes/".concat(k,".json"))):[]).slice(0,t).concat(s)}else k=0,E=s;for(let t=0;100*t<E.length;t++){const e=E.slice(100*t,100*(t+1));yield f.put("changes/".concat(k+t,".json"),e),v.set(k+t,e)}d.lastChange=(d.lastChange||0)+s.length,yield f.putMeta(d),h.queue=h.queue.slice(e.length),h.lastChange=d.lastChange,yield m()}))).apply(this,arguments)}function O(){return(O=n((function*(){yield f.acquireLock(p);try{yield function(){return E.apply(this,arguments)}(),yield function(){return k.apply(this,arguments)}()}finally{yield f.releaseLock()}}))).apply(this,arguments)}function x(){return T.apply(this,arguments)}function T(){return(T=n((function*(t=!0){if(!h.enabled)throw new Error("Cannot sync now, the sync is not enabled");yield w.write(n((function*(){if(!h.queue.length&&t&&d){if(!(yield f.peekChanges(d)))return}yield function(){return O.apply(this,arguments)}()})))}))).apply(this,arguments)}},t.drive=d,t}({});
|