3 lines
24 KiB
JavaScript
3 lines
24 KiB
JavaScript
var dbToCloud=function(t){"use strict";function e(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(t);e&&(o=o.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,o)}return n}function n(t){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?e(Object(o),!0).forEach((function(e){i(t,e,o[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):e(Object(o)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(o,e))}))}return t}function o(t,e,n,o,r,i,c){try{var a=t[i](c),s=a.value}catch(t){return void n(t)}a.done?e(s):Promise.resolve(s).then(o,r)}function r(t){return function(){var e=this,n=arguments;return new Promise((function(r,i){var c=t.apply(e,n);function a(t){o(c,r,i,a,s,"next",t)}function s(t){o(c,r,i,a,s,"throw",t)}a(void 0)}))}}function i(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function c(t,e){if(null==t)return{};var n,o,r=function(t,e){if(null==t)return{};var n,o,r={},i=Object.keys(t);for(o=0;o<i.length;o++)n=i[o],e.indexOf(n)>=0||(r[n]=t[n]);return r}(t,e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);for(o=0;o<i.length;o++)n=i[o],e.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(t,n)&&(r[n]=t[n])}return r}function a(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null==n)return;var o,r,i=[],c=!0,a=!1;try{for(n=n.call(t);!(c=(o=n.next()).done)&&(i.push(o.value),!e||i.length!==e);c=!0);}catch(t){a=!0,r=t}finally{try{c||null==n.return||n.return()}finally{if(a)throw r}}return i}(t,e)||s(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function s(t,e){if(t){if("string"==typeof t)return l(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?l(t,e):void 0}}function l(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=new Array(e);n<e;n++)o[n]=t[n];return o}function u(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=s(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var o=0,r=function(){};return{s:r,n:function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}},e:function(t){throw t},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,c=!0,a=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return c=t.done,t},e:function(t){a=!0,i=t},f:function(){try{c||null==n.return||n.return()}finally{if(a)throw i}}}}function p({maxActiveReader:t=1/0}={}){let e,n,o=0;const r={read:t=>i(t,!1),write:t=>i(t,!0),length:0};return r;function i(t,o){const i=function({fn:t,block:e=!1,prev:n,next:o,q:r=c(),q2:i=(t.length?c():null)}){return{fn:t,block:e,prev:n,next:o,q:r,q2:i}}({fn:t,block:o});return n?(n.next=i,i.prev=n,n=i,e||(e=n)):e=n=i,r.length++,a(),i.q.promise}function c(){const t={};return t.promise=new Promise(((e,n)=>{t.resolve=e,t.reject=n})),t}function a(){const i=e;if(!i||i.block&&i.prev||i.prev&&i.prev.block||o>=t)return;let c;i.block||o++,e=i.next;try{c=i.fn(i.q2&&i.q2.resolve)}catch(t){return i.q.reject(t),void s()}if(i.q2&&i.q2.promise.then(l),c&&c.then){const t=c.then(i.q.resolve,i.q.reject);i.q2||t.then(s)}else if(i.q.resolve(c),!i.q2)return void s();function s(){l()}function l(t){i.prev&&(i.prev.next=i.next),i.next&&(i.next.prev=i.prev),n===i&&(n=i.prev),i.block||o--,r.length--,t&&t(),a()}a()}}class f extends Error{constructor(t){super("The database is locked. Will expire at ".concat(new Date(t).toLocaleString())),this.expire=t,this.name="LockError",Error.captureStackTrace&&Error.captureStackTrace(this,f)}}function h(t){let e,n=0;return()=>(n&&clearTimeout(n),n=setTimeout(o),e||(e=function(){const t={};return t.promise=new Promise(((e,n)=>{t.resolve=e,t.reject=n})),t}()),e.promise);function o(){Promise.resolve(t()).then(e.resolve,e.reject),n=0,e=null}}function d(t){return new Promise((e=>setTimeout(e,t)))}function y(t){return String.fromCharCode(parseInt(t.slice(1),16))}function g(t){return btoa(encodeURIComponent(t).replace(/%[0-9A-F]{2}/g,y))}function m(t){return"%".concat("00".concat(t.charCodeAt(0).toString(16)).slice(-2))}function v(t){return decodeURIComponent(Array.from(atob(t),m).join(""))}const w=["path","contentType","headers","format","raw"];class b extends Error{constructor(t,e,n=e&&e.status){super(t),this.code=n,this.origin=e,Error.captureStackTrace&&Error.captureStackTrace(this,b)}}function T({fetch:t,cooldown:e=0,getAccessToken:o,username:i,password:a}){const s=p(),l=i||a?"Basic ".concat(g("".concat(i,":").concat(a))):null;return t=>s.write(function(){var n=r((function*(n){try{return yield function(t){return u.apply(this,arguments)}(t)}finally{e&&t.method&&"GET"!==t.method?setTimeout(n,e):n()}}));return function(t){return n.apply(this,arguments)}}());function u(){return(u=r((function*(e){let r=e.path,i=e.contentType,a=e.headers,s=e.format,u=e.raw,p=void 0!==u&&u,f=c(e,w);const h={};for(o&&(h.Authorization="Bearer ".concat(yield o())),l&&(h.Authorization=l),i&&(h["Content-Type"]=i),Object.assign(h,a);;){const e=yield t(r,n({headers:h},f));if(!e.ok){const t=e.headers.get("Retry-After");if(t){const e=Number(t);if(e){yield d(1e3*e);continue}}const n=yield e.text();throw new b("failed to fetch [".concat(e.status,"]: ").concat(n),e)}if(p)return e;if(s)return yield e[s]();const o=e.headers.get("Content-Type");return/application\/json/.test(o)?yield e.json():yield e.text()}}))).apply(this,arguments)}}const E=["path","body"];function O(t){const e=t.replace(/[/\\][^/\\]+\/?$/,"");return e===t?".":e}const j=["path"];function k(t){return Array.isArray(t)?t:[t]}function x(t){const e=Array.prototype.filter.call(t.childNodes,(t=>1===t.nodeType));if(!e.length)return t.textContent;const n={};var o,r=u(e);try{for(r.s();!(o=r.n()).done;){const t=o.value,e=x(t);if(n[t.localName])if(Array.isArray(n[t.localName]))n[t.localName].push(e);else{const o=[n[t.localName]];o.push(e),n[t.localName]=o}else n[t.localName]=e}}catch(t){r.e(t)}finally{r.f()}return n}var C=Object.freeze({__proto__:null,fsDrive:()=>{},github:function({userAgent:t="db-to-cloud",owner:e,repo:n,getAccessToken:o,fetch:i=("undefined"!=typeof self?self:global).fetch}){const c=T({fetch:i,getAccessToken:o,cooldown:1e3}),a=new Map;return{name:"github",get:p,put:h,post:function(t,e){return h(t,e,!1)},delete:function(t){return y.apply(this,arguments)},list:function(t){return l.apply(this,arguments)},shaCache:a};function s(e){return e.headers||(e.headers={}),e.headers["User-Agent"]||(e.headers["User-Agent"]=t),e.headers.Accept||(e.headers.Accept="application/vnd.github.v3+json"),e.path="https://api.github.com".concat(e.path),c(e)}function l(){return(l=r((function*(t){const o=[];var r,i=u(yield s({path:"/repos/".concat(e,"/").concat(n,"/contents/").concat(t)}));try{for(i.s();!(r=i.n()).done;){const t=r.value;o.push(t.name),a.set(t.path,t.sha)}}catch(t){i.e(t)}finally{i.f()}return o}))).apply(this,arguments)}function p(t){return f.apply(this,arguments)}function f(){return(f=r((function*(t){const o=yield s({path:"/repos/".concat(e,"/").concat(n,"/contents/").concat(t)});return a.set(o.path,o.sha),v(o.content)}))).apply(this,arguments)}function h(t,e){return d.apply(this,arguments)}function d(){return(d=r((function*(t,o,r=!0){const i={message:"",content:g(o)};r&&a.has(t)&&(i.sha=a.get(t));const c={method:"PUT",path:"/repos/".concat(e,"/").concat(n,"/contents/").concat(t),contentType:"application/json",body:JSON.stringify(i)};let l,u=!1;for(;!l;){try{l=yield s(c)}catch(e){if(422!==e.code||!e.message.includes('\\"sha\\" wasn\'t supplied'))throw e;if(!r||u)throw e.code="EEXIST",e;yield p(t)}u=!0}a.set(t,l.content.sha)}))).apply(this,arguments)}function y(){return(y=r((function*(t){try{let o=a.get(t);o||(yield p(t),o=a.get(t)),yield s({method:"DELETE",path:"/repos/".concat(e,"/").concat(n,"/contents/").concat(t),body:JSON.stringify({message:"",sha:o})})}catch(t){if(404===t.code)return;throw t}}))).apply(this,arguments)}},dropbox:function({getAccessToken:t,fetch:e=("undefined"!=typeof self?self:global).fetch}){const o=T({fetch:e,getAccessToken:t});return{name:"dropbox",get:function(t){return l.apply(this,arguments)},put:p,post:function(t,e){return h.apply(this,arguments)},delete:function(t){return d.apply(this,arguments)},list:function(t){return a.apply(this,arguments)}};function i(t){let e=t.path,r=t.body,i=c(t,E);return o(n({method:"POST",path:"https://api.dropboxapi.com/2/".concat(e),contentType:"application/json",body:JSON.stringify(r)},i))}function a(){return(a=r((function*(t){const e=[];let n=yield i({path:"files/list_folder",body:{path:"/".concat(t)}});var o,r=u(n.entries);try{for(r.s();!(o=r.n()).done;){const t=o.value;e.push(t.name)}}catch(t){r.e(t)}finally{r.f()}if(!n.has_more)return e;for(;n.has_more;){n=yield i({path:"files/list_folder/continue",body:{cursor:n.cursor}});var c,a=u(n.entries);try{for(a.s();!(c=a.n()).done;){const t=c.value;e.push(t.name)}}catch(t){a.e(t)}finally{a.f()}}return e}))).apply(this,arguments)}function s(t){const e=new URLSearchParams;return e.set("arg",JSON.stringify(t)),e.toString()}function l(){return(l=r((function*(t){const e={path:"/".concat(t)};try{return yield o({path:"https://content.dropboxapi.com/2/files/download?".concat(s(e)),format:"text"})}catch(t){throw 409===t.code&&t.message.includes("not_found")&&(t.code="ENOENT"),t}}))).apply(this,arguments)}function p(t,e){return f.apply(this,arguments)}function f(){return(f=r((function*(t,e,n="overwrite"){const r={path:"/".concat(t),mode:n,autorename:!1,mute:!0};yield o({path:"https://content.dropboxapi.com/2/files/upload?".concat(s(r)),method:"POST",contentType:"application/octet-stream",body:e})}))).apply(this,arguments)}function h(){return(h=r((function*(t,e){try{return yield p(t,e,"add")}catch(t){throw 409===t.code&&t.message.includes("conflict")&&(t.code="EEXIST"),t}}))).apply(this,arguments)}function d(){return(d=r((function*(t){try{yield i({path:"files/delete_v2",body:{path:"/".concat(t)}})}catch(t){if(409===t.code&&t.message.includes("not_found"))return;throw t}}))).apply(this,arguments)}},onedrive:function({getAccessToken:t,fetch:e=("undefined"!=typeof self?self:global).fetch}){const n=T({fetch:e,getAccessToken:t});return{name:"onedrive",get:function(t){return a.apply(this,arguments)},put:function(t,e){return s.apply(this,arguments)},post:function(t,e){return l.apply(this,arguments)},delete:function(t){return u.apply(this,arguments)},list:function(t){return c.apply(this,arguments)}};function o(t){return i.apply(this,arguments)}function i(){return(i=r((function*(t){return t.path="https://graph.microsoft.com/v1.0/me/drive/special/approot".concat(t.path),yield n(t)}))).apply(this,arguments)}function c(){return(c=r((function*(t){t&&(t=":/".concat(t,":"));let e=yield o({path:"".concat(t,"/children?select=name")}),r=e.value.map((t=>t.name));for(;e["@odata.nextLink"];)e=yield n({path:e["@odata.nextLink"]}),r=r.concat(e.value.map((t=>t.name)));return r}))).apply(this,arguments)}function a(){return(a=r((function*(t){return yield o({path:":/".concat(t,":/content"),format:"text"})}))).apply(this,arguments)}function s(){return(s=r((function*(t,e){yield o({method:"PUT",path:":/".concat(t,":/content"),headers:{"Content-Type":"text/plain"},body:e})}))).apply(this,arguments)}function l(){return(l=r((function*(t,e){try{yield o({method:"PUT",path:":/".concat(t,":/content?@microsoft.graph.conflictBehavior=fail"),headers:{"Content-Type":"text/plain"},body:e})}catch(t){throw 409===t.code&&t.message.includes("nameAlreadyExists")&&(t.code="EEXIST"),t}}))).apply(this,arguments)}function u(){return(u=r((function*(t){try{yield o({method:"DELETE",path:":/".concat(t,":")})}catch(t){if(404===t.code)return;throw t}}))).apply(this,arguments)}},google:function({getAccessToken:t,fetch:e=("undefined"!=typeof self?self:global).fetch,FormData:n=("undefined"!=typeof self?self:global).FormData,Blob:o=("undefined"!=typeof self?self:global).Blob}){const i=T({fetch:e,getAccessToken:t}),c=new Map;let a;return{name:"google",get:function(t){return j.apply(this,arguments)},put:function(t,e){return k.apply(this,arguments)},post:x,delete:function(t){return S.apply(this,arguments)},list:function(t){return O.apply(this,arguments)},init:function(){return E.apply(this,arguments)},acquireLock:function(t){return p.apply(this,arguments)},releaseLock:function(){return h.apply(this,arguments)},fileMetaCache:c};function s(t,e){return l.apply(this,arguments)}function l(){return(l=r((function*(t,e){yield i({method:"DELETE",path:"https://www.googleapis.com/drive/v3/files/".concat(t,"/revisions/").concat(e)})}))).apply(this,arguments)}function p(){return(p=r((function*(t){const e=c.get("lock.json"),n=(yield g(e.id,JSON.stringify({expire:Date.now()+60*t*1e3}))).headRevisionId,o=yield i({path:"https://www.googleapis.com/drive/v3/files/".concat(e.id,"/revisions?fields=revisions(id)")});for(let t=1;t<o.revisions.length;t++){const r=o.revisions[t].id;if(r===n)return void(a=n);const c=JSON.parse(yield i({path:"https://www.googleapis.com/drive/v3/files/".concat(e.id,"/revisions/").concat(r,"?alt=media")}));if(c.expire>Date.now())throw yield s(e.id,n),new f(c.expire);yield s(e.id,r)}throw new Error("cannot find lock revision")}))).apply(this,arguments)}function h(){return(h=r((function*(){const t=c.get("lock.json");yield s(t.id,a),a=null}))).apply(this,arguments)}function d(t,e){return y.apply(this,arguments)}function y(){return(y=r((function*(t,e){t="https://www.googleapis.com/drive/v3/files?spaces=appDataFolder&fields=nextPageToken,files(id,name,headRevisionId)"+(t?"&"+t:"");let n=yield i({path:t});for(e(n);n.nextPageToken;)n=yield i({path:"".concat(t,"&pageToken=").concat(n.nextPageToken)}),e(n)}))).apply(this,arguments)}function g(t,e){return m.apply(this,arguments)}function m(){return(m=r((function*(t,e){return yield i({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 v(t){return w.apply(this,arguments)}function w(){return(w=r((function*(t){t&&(t="q=".concat(encodeURIComponent(t))),yield d(t,(t=>{var e,n=u(t.files);try{for(n.s();!(e=n.n()).done;){const t=e.value;c.set(t.name,t)}}catch(t){n.e(t)}finally{n.f()}}))}))).apply(this,arguments)}function E(){return(E=r((function*(){yield v(),c.has("lock.json")||(yield x("lock.json","{}")),c.has("meta.json")||(yield x("meta.json","{}"))}))).apply(this,arguments)}function O(){return(O=r((function*(t){return[...c.values()].filter((e=>e.name.startsWith(t+"/"))).map((t=>t.name.split("/")[1]))}))).apply(this,arguments)}function j(){return(j=r((function*(t){let e=c.get(t);if(!e&&(yield v("name = '".concat(t,"'")),e=c.get(t),!e))throw new b("metaCache doesn't contain ".concat(t),null,"ENOENT");try{return yield i({path:"https://www.googleapis.com/drive/v3/files/".concat(e.id,"?alt=media")})}catch(t){throw 404===t.code&&(t.code="ENOENT"),t}}))).apply(this,arguments)}function k(){return(k=r((function*(t,e){if(!c.has(t))return yield x(t,e);const n=c.get(t),o=yield g(n.id,e);n.headRevisionId=o.headRevisionId}))).apply(this,arguments)}function x(t,e){return C.apply(this,arguments)}function C(){return(C=r((function*(t,e){const r=new n,a={name:t,parents:["appDataFolder"]};r.append("metadata",new o([JSON.stringify(a)],{type:"application/json; charset=UTF-8"})),r.append("media",new o([e],{type:"text/plain"}));const s=yield i({method:"POST",path:"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,name,headRevisionId",body:r});c.set(s.name,s)}))).apply(this,arguments)}function S(){return(S=r((function*(t){const e=c.get(t);if(e)try{yield i({method:"DELETE",path:"https://www.googleapis.com/drive/v3/files/".concat(e.id)})}catch(t){if(404===t.code)return;throw t}}))).apply(this,arguments)}},webdav:function({username:t,password:e,url:o,fetch:i=("undefined"!=typeof self?self:global).fetch,DOMParser:a=("undefined"!=typeof self?self:global).DOMParser}){o.endsWith("/")||(o+="/");const s=T({fetch:i,username:t,password:e});return{name:"webdav",get:function(t){return h.apply(this,arguments)},put:function(t,e){return d.apply(this,arguments)},post:function(t,e){return m.apply(this,arguments)},delete:function(t){return v.apply(this,arguments)},list:function(t){return f.apply(this,arguments)}};function l(t){return p.apply(this,arguments)}function p(){return(p=r((function*(t){let e=t.path,r=c(t,j);const i=yield s(n({path:"".concat(o).concat(e)},r));if(r.format||"string"!=typeof i||!i)return i;const l=x((new a).parseFromString(i,"application/xml"));if(l.error)throw new Error("Failed requesting DAV at ".concat(o).concat(e,": ").concat(JSON.stringify(l.error)));if(l.multistatus){l.multistatus.response=k(l.multistatus.response);var p,f=u(l.multistatus.response);try{for(f.s();!(p=f.n()).done;){const t=p.value;if(t.error)throw new Error("Failed requesting DAV at ".concat(o).concat(e,": ").concat(t.href," ").concat(t.error))}}catch(t){f.e(t)}finally{f.f()}}return l}))).apply(this,arguments)}function f(){return(f=r((function*(t){t.endsWith("/")||(t+="/");const e=[];var n,r=u(k((yield l({method:"PROPFIND",path:t,contentType:"application/xml",body:'<?xml version="1.0" encoding="utf-8" ?> \n <propfind xmlns="DAV:">\n <allprop/>\n </propfind>',headers:{Depth:"1"}})).multistatus.response));try{for(r.s();!(n=r.n()).done;){const r=n.value;if(k(r.propstat).some((t=>t.prop.resourcetype&&void 0!==t.prop.resourcetype.collection)))continue;const i="".concat(o).concat(t),c=new URL(r.href,i).href.slice(i.length);e.push(c)}}catch(t){r.e(t)}finally{r.f()}return e}))).apply(this,arguments)}function h(){return(h=r((function*(t){return yield l({method:"GET",path:t,format:"text"})}))).apply(this,arguments)}function d(){return(d=r((function*(t,e){return yield y(O(t),(()=>l({method:"PUT",path:t,contentType:"application/octet-stream",body:e})))}))).apply(this,arguments)}function y(t,e){return g.apply(this,arguments)}function g(){return(g=r((function*(t,e){try{return yield e()}catch(e){if(409!==e.code&&404!==e.code||"."===t)throw e}return yield y(O(t),(()=>l({method:"MKCOL",path:t}))),yield e()}))).apply(this,arguments)}function m(){return(m=r((function*(t,e){try{return yield y(O(t),(()=>l({method:"PUT",path:t,body:e,contentType:"octet-stream",headers:{"If-None-Match":"*"}})))}catch(t){throw 412===t.code&&(t.code="EEXIST"),t}}))).apply(this,arguments)}function v(){return(v=r((function*(t){try{yield l({method:"DELETE",path:t})}catch(t){if(404===t.code)return;throw t}}))).apply(this,arguments)}}});return t.dbToCloud=function({onGet:t,onPut:e,onDelete:n,onFirstSync:o,onWarn:i=console.error,onProgress:c,compareRevision:s,getState:l,setState:y,lockExpire:g=60,retryMaxAttempts:m=5,retryExp:v=1.5,retryDelay:w=10}){let b,T,E;const O=new Map,j=h((()=>y(b,T))),k=new Map,x=p();return{use:function(t){b=function(t){const e=Object.create(t);return e.get=function(){var e=r((function*(e){return JSON.parse(yield t.get(e))}));return function(t){return e.apply(this,arguments)}}(),e.put=function(){var e=r((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=r((function*(e,n){return yield t.post(e,JSON.stringify(n))}));return function(t,n){return e.apply(this,arguments)}}(),e.isInit=!1,e.acquireLock||(e.acquireLock=function(t){return n.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 n(){return(n=r((function*(t){try{yield this.post("lock.json",{expire:Date.now()+60*t*1e3})}catch(t){if("EEXIST"!==t.code)throw t;const e=yield this.get("lock.json");if(Date.now()>e.expire)throw yield this.delete("lock.json"),new Error("Found expired lock, please try again");throw new f(e.expire)}}))).apply(this,arguments)}function o(){return(o=r((function*(){yield this.delete("lock.json")}))).apply(this,arguments)}function i(){return(i=r((function*(){try{return yield this.get("meta.json")}catch(t){if("ENOENT"===t.code||404===t.code)return{};throw t}}))).apply(this,arguments)}function c(){return(c=r((function*(t){yield this.put("meta.json",t)}))).apply(this,arguments)}function a(){return(a=r((function*(t){return(yield this.getMeta()).lastChange!==t.lastChange}))).apply(this,arguments)}}(t)},init:function(){return x.write(r((function*(){if(!T||!T.enabled){if(!b)throw new Error("cloud drive is undefined");T=(yield l(b))||{},T.enabled=!0,T.queue||(T.queue=[])}})))},uninit:function(){return x.write(r((function*(){T&&T.enabled&&(T=E=null,O.clear(),k.clear(),b.uninit&&b.isInit&&(yield b.uninit(),b.isInit=!1),yield j())})))},put:function(t,e){if(!T||!T.enabled)return;T.queue.push({_id:t,_rev:e,action:"put"}),j()},delete:function(t,e){if(!T||!T.enabled)return;T.queue.push({_id:t,_rev:e,action:"delete"}),j()},syncNow:function(t){return x.write(r((function*(){if(!T||!T.enabled)throw new Error("Cannot sync now, the sync is not enabled");b.init&&!b.isInit&&(yield b.init(),b.isInit=!0),null==T.lastChange&&(yield o()),yield function(){return q.apply(this,arguments)}(t)})))},drive:()=>b,isInit:()=>Boolean(T&&T.enabled)};function C(){return S.apply(this,arguments)}function S(){return(S=r((function*(){if(E=yield b.getMeta(),!E.lastChange||E.lastChange===T.lastChange)return;let t=[];if(T.lastChange){const e=Math.floor((E.lastChange-1)/100);let n=Math.floor(T.lastChange/100);for(;n<=e;){const e=yield b.get("changes/".concat(n,".json"));O.set(n,e),t=t.concat(e),n++}t=t.slice(T.lastChange%100)}else t=(yield b.list("docs")).map((t=>({action:"put",_id:t.slice(0,-5)})));const o=new Map;var r,s=u(t);try{for(s.s();!(r=s.n()).done;){const t=r.value;o.set(t._id,t)}}catch(t){s.e(t)}finally{s.f()}let l=0;var p,f=u(o);try{for(f.s();!(p=f.n()).done;){const t=a(p.value,2),r=t[0],s=t[1];let u,f;if(c&&c({phase:"pull",total:o.size,loaded:l,change:s}),"delete"===s.action)yield n(r,s._rev);else if("put"===s.action){try{var h=yield b.get("docs/".concat(r,".json"));u=h.doc,f=h._rev}catch(t){if("ENOENT"===t.code||404===t.code){i("Cannot find ".concat(r,". Is it deleted without updating the history?")),l++;continue}throw t}yield e(u)}const d=s._rev||f;d&&k.set(r,d),l++}}catch(t){f.e(t)}finally{f.f()}T.lastChange=E.lastChange,yield j()}))).apply(this,arguments)}function A(){return P.apply(this,arguments)}function P(){return(P=r((function*(){if(!T.queue.length)return;const e=T.queue.slice(),n=new Map;var o,r=u(e);try{for(r.s();!(o=r.n()).done;){const t=o.value;n.set(t._id,t)}}catch(t){r.e(t)}finally{r.f()}const i=[];var a,l=u(n.values());try{for(l.s();!(a=l.n()).done;){const t=a.value,e=k.get(t._id);void 0!==e&&s(t._rev,e)<=0||i.push(t)}}catch(t){l.e(t)}finally{l.f()}let p,f,h=0;for(var d=0,y=i;d<y.length;d++){const e=y[d];if(c&&c({phase:"push",loaded:h,total:i.length,change:e}),"delete"===e.action)yield b.delete("docs/".concat(e._id,".json"));else if("put"===e.action){const n=yield t(e._id,e._rev);yield b.put("docs/".concat(e._id,".json"),{doc:n,_rev:e._rev})}k.set(e._id,e._rev),h++}if(E.lastChange){f=Math.floor(E.lastChange/100);const t=E.lastChange%100;p=t?O.get(f)||(yield b.get("changes/".concat(f,".json"))):[],p=p.slice(0,t).concat(i)}else f=0,p=i;for(let t=0;100*t<p.length;t++){const e=p.slice(100*t,100*(t+1));yield b.put("changes/".concat(f+t,".json"),e),O.set(f+t,e)}E.lastChange=(E.lastChange||0)+i.length,yield b.putMeta(E),T.queue=T.queue.slice(e.length),T.lastChange=E.lastChange,yield j()}))).apply(this,arguments)}function _(){return N.apply(this,arguments)}function N(){return(N=r((function*(){let t,e=0,n=w;for(;;){try{yield b.acquireLock(g);break}catch(e){if("LockError"!==e.name)throw e;t=e}if(e++,e>=m)throw t;yield d(1e3*n),n*=v}try{yield C(),yield A()}finally{yield b.releaseLock()}}))).apply(this,arguments)}function q(){return(q=r((function*(t=!0){c&&c({phase:"start"});try{if(!T.queue.length&&t&&E){if(!(yield b.peekChanges(E)))return}yield _()}finally{c&&c({phase:"end"})}}))).apply(this,arguments)}},t.drive=C,Object.defineProperty(t,"__esModule",{value:!0}),t}({});
|
|
//# sourceMappingURL=db-to-cloud.min.js.map
|