diff --git a/_locales/en/messages.json b/_locales/en/messages.json index c9fb1aa6..026c0b66 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1141,6 +1141,9 @@ "optionsSyncStatusDisconnected": { "message": "Disconnected" }, + "optionsSyncStatusRelogin": { + "message": "Session expired, please login again." + }, "paginationCurrent": { "message": "Current page", "description": "Tooltip for the current page index in search results" diff --git a/background/sync-manager.js b/background/sync-manager.js index 3fbb72e6..d37df6c5 100644 --- a/background/sync-manager.js +++ b/background/sync-manager.js @@ -63,19 +63,19 @@ const syncMan = (() => { return status; }, - async login(name = prefs.get('sync.enabled')) { + async login(name) { if (ready.then) await ready; + if (!name) name = prefs.get('sync.enabled'); + await tokenMan.revokeToken(name); try { await tokenMan.getToken(name, true); + status.login = true; } catch (err) { - if (/Authorization page could not be loaded/i.test(err.message)) { - // FIXME: Chrome always fails at the first login so we try again - await tokenMan.getToken(name); - } + status.login = false; throw err; + } finally { + emitStatusChange(); } - status.login = true; - emitStatusChange(); }, async put(...args) { @@ -88,29 +88,32 @@ const syncMan = (() => { async start(name, fromPref = false) { if (ready.then) await ready; if (!ctrl) await initController(); + if (currentDrive) return; currentDrive = getDrive(name); ctrl.use(currentDrive); + status.state = STATES.connecting; status.currentDriveName = currentDrive.name; - status.login = true; emitStatusChange(); - try { - if (!fromPref) { - await syncMan.login(name).catch(handle401Error); - } - await syncMan.syncNow(); - status.errorMessage = null; - lastError = null; - } catch (err) { - status.errorMessage = err.message; - lastError = err; - // FIXME: should we move this logic to options.js? - if (!fromPref) { + + if (fromPref) { + status.login = true; + } else { + try { + await syncMan.login(name); + } catch (err) { console.error(err); + status.errorMessage = err.message; + lastError = err; + emitStatusChange(); return syncMan.stop(); } } + + await ctrl.init(); + + await syncMan.syncNow(name); prefs.set('sync.enabled', name); status.state = STATES.connected; schedule(SYNC_INTERVAL); @@ -124,7 +127,7 @@ const syncMan = (() => { status.state = STATES.disconnecting; emitStatusChange(); try { - await ctrl.stop(); + await ctrl.uninit(); await tokenMan.revokeToken(currentDrive.name); await chromeLocal.remove(STORAGE_KEY + currentDrive.name); } catch (e) {} @@ -138,14 +141,21 @@ const syncMan = (() => { async syncNow() { if (ready.then) await ready; - if (!currentDrive) throw new Error('cannot sync when disconnected'); + if (!currentDrive || !status.login) { + console.warn('cannot sync when disconnected'); + return; + } try { - await (ctrl.isInit() ? ctrl.syncNow() : ctrl.start()).catch(handle401Error); + await ctrl.syncNow(); status.errorMessage = null; lastError = null; } catch (err) { status.errorMessage = err.message; lastError = err; + + if (isGrantError(err)) { + status.login = false; + } } emitStatusChange(); }, @@ -192,21 +202,6 @@ const syncMan = (() => { }); } - async function handle401Error(err) { - let authError = false; - if (err.code === 401) { - await tokenMan.revokeToken(currentDrive.name).catch(console.error); - authError = true; - } else if (/User interaction required|Requires user interaction/i.test(err.message)) { - authError = true; - } - if (authError) { - status.login = false; - emitStatusChange(); - } - return Promise.reject(err); - } - function emitStatusChange() { msg.broadcastExtension({method: 'syncStatusUpdate', status}); iconMan.overrideBadge(getErrorBadge()); @@ -223,11 +218,17 @@ const syncMan = (() => { } function getErrorBadge() { - if (status.state === STATES.connected && lastError && !isNetworkError(lastError)) { + if (status.state === STATES.connected && + (!status.login || lastError && !isNetworkError(lastError))) { return { text: 'x', color: '#F00', - title: isGrantError(lastError) ? 'syncErrorRelogin' : 'syncError', + title: !status.login ? 'syncErrorRelogin' : `${ + chrome.i18n.getMessage('syncError') + }\n---------------------\n${ + // splitting to limit each line length + lastError.message.replace(/.{60,}?\s(?=.{30,})/g, '$&\n') + }`, }; } } diff --git a/background/token-manager.js b/background/token-manager.js index 4c2e7f0c..4aac046a 100644 --- a/background/token-manager.js +++ b/background/token-manager.js @@ -75,13 +75,12 @@ const tokenMan = (() => { return obj[k.TOKEN]; } if (obj[k.REFRESH]) { - try { - return await refreshToken(name, k, obj); - } catch (err) { - if (err.code !== 401) throw err; - } + return refreshToken(name, k, obj); } } + if (!interactive) { + throw new Error(`Invalid token: ${name}`); + } return authUser(name, k, interactive); }, diff --git a/options/options.css b/options/options.css index 7c70e536..656bf4e3 100644 --- a/options/options.css +++ b/options/options.css @@ -398,12 +398,10 @@ html:not(.firefox):not(.opera) #updates { } .sync-status { + width: 0; /* together with flex-grow makes it reuse the current width */ flex-grow: 1; padding-right: 8px; box-sizing: border-box; - white-space: nowrap; - overflow: hidden; - text-overflow: ellipsis; } .sync-status::first-letter { text-transform: uppercase; diff --git a/options/options.js b/options/options.js index b68e6deb..107c02fc 100644 --- a/options/options.js +++ b/options/options.js @@ -147,7 +147,7 @@ document.onclick = e => { [elCloud, isDisconnected], [elStart, isDisconnected && elCloud.value !== 'none'], [elStop, isConnected && !status.syncing], - [elSyncNow, isConnected && !status.syncing], + [elSyncNow, isConnected && !status.syncing && status.login], ]) { el.disabled = !enable; } @@ -156,19 +156,22 @@ document.onclick = e => { } function getStatusText() { - let res; if (status.syncing) { const {phase, loaded, total} = status.progress || {}; - res = phase + return phase ? t(`optionsSyncStatus${capitalize(phase)}`, [loaded + 1, total], false) || `${phase} ${loaded} / ${total}` : t('optionsSyncStatusSyncing'); - } else { - const {state, errorMessage, STATES} = status; - res = (state === STATES.connected || state === STATES.disconnected) && errorMessage || - t(`optionsSyncStatus${capitalize(state)}`, null, false) || state; } - return res; + + const {state, errorMessage, STATES} = status; + if (errorMessage && (state === STATES.connected || state === STATES.disconnected)) { + return errorMessage; + } + if (state === STATES.connected && !status.login) { + return t('optionsSyncStatusRelogin'); + } + return t(`optionsSyncStatus${capitalize(state)}`, null, false) || state; } })(); diff --git a/package-lock.json b/package-lock.json index 9fce00ac..20bee63a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "GPL-3.0-only", "dependencies": { "codemirror": "5.59.2", - "db-to-cloud": "^0.4.5", + "db-to-cloud": "^0.6.0", "jsonlint": "^1.6.3", "less-bundle": "github:openstyles/less-bundle#v0.1.0", "lz-string-unsafe": "^1.4.4-fork-1", @@ -2772,12 +2772,15 @@ } }, "node_modules/db-to-cloud": { - "version": "0.4.5", - "resolved": "https://registry.npmjs.org/db-to-cloud/-/db-to-cloud-0.4.5.tgz", - "integrity": "sha512-3E5eYVIlZmX0ZRgSZ3WJF+lxs8eCFOJWruw8GLHbKDGK5tIZ13Bxsge+eFXbYBQUidzW7y3xuxD8MdpjDLY7eQ==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/db-to-cloud/-/db-to-cloud-0.6.0.tgz", + "integrity": "sha512-AbvxpU+fA3Fsdzu0OxL+cVPS9HwM6DzXFDg00WIQ3YeMkWs5saMXpiXMfISlkpBUwm5Cbr4W7cfhYszu38BzSw==", "dependencies": { "@eight04/read-write-lock": "^0.1.0", "universal-base64": "^2.1.0" + }, + "engines": { + "node": ">=8" } }, "node_modules/debounce": { @@ -14299,9 +14302,9 @@ } }, "db-to-cloud": { - "version": "0.4.5", - "resolved": "https://registry.npmjs.org/db-to-cloud/-/db-to-cloud-0.4.5.tgz", - "integrity": "sha512-3E5eYVIlZmX0ZRgSZ3WJF+lxs8eCFOJWruw8GLHbKDGK5tIZ13Bxsge+eFXbYBQUidzW7y3xuxD8MdpjDLY7eQ==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/db-to-cloud/-/db-to-cloud-0.6.0.tgz", + "integrity": "sha512-AbvxpU+fA3Fsdzu0OxL+cVPS9HwM6DzXFDg00WIQ3YeMkWs5saMXpiXMfISlkpBUwm5Cbr4W7cfhYszu38BzSw==", "requires": { "@eight04/read-write-lock": "^0.1.0", "universal-base64": "^2.1.0" diff --git a/package.json b/package.json index f7d0d447..43718d74 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "author": "Stylus Team", "dependencies": { "codemirror": "5.59.2", - "db-to-cloud": "^0.4.5", + "db-to-cloud": "^0.6.0", "jsonlint": "^1.6.3", "less-bundle": "github:openstyles/less-bundle#v0.1.0", "lz-string-unsafe": "^1.4.4-fork-1", diff --git a/vendor/db-to-cloud/README.md b/vendor/db-to-cloud/README.md index bab08836..fed55581 100644 --- a/vendor/db-to-cloud/README.md +++ b/vendor/db-to-cloud/README.md @@ -1,4 +1,4 @@ -## db-to-cloud v0.4.5 +## db-to-cloud v0.6.0 Following files are copied from npm (node_modules): diff --git a/vendor/db-to-cloud/db-to-cloud.min.js b/vendor/db-to-cloud/db-to-cloud.min.js index ca1f42bf..6d46dcfe 100644 --- a/vendor/db-to-cloud/db-to-cloud.min.js +++ b/vendor/db-to-cloud/db-to-cloud.min.js @@ -1,2 +1,2 @@ -var dbToCloud=function(t){"use strict";function e(t,e,n,o,r,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(o,r)}function n(t){return function(){var n=this,o=arguments;return new Promise((function(r,i){var c=t.apply(n,o);function a(t){e(c,r,i,a,l,"next",t)}function l(t){e(c,r,i,a,l,"throw",t)}a(void 0)}))}}function o(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function r(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 i(t){for(var e=1;e=0||(r[n]=t[n]);return r}(t,e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);for(o=0;o=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){if(!(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t)))return;var n=[],o=!0,r=!1,i=void 0;try{for(var c,a=t[Symbol.iterator]();!(o=(c=a.next()).done)&&(n.push(c.value),!e||n.length!==e);o=!0);}catch(t){r=!0,i=t}finally{try{o||null==a.return||a.return()}finally{if(r)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,o=0;const r={read:t=>i(t,!1),write:t=>i(t,!0),length:0};return r;function i(i,a){const l=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:i,block:a});return n?(n.next=l,l.prev=n,n=l,e||(e=n)):e=n=l,r.length++,function i(){const c=e;if(!c||c.block&&c.prev||c.prev&&c.prev.block||o>=t)return;c.block||o++;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(s);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(){s()}function s(t){c.prev&&(c.prev.next=c.next),c.next&&(c.next.prev=c.prev),n===c&&(n=c.prev),c.block||o--,r.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}}function s(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}}const u={};function p(t){return String.fromCharCode(parseInt(t.slice(1),16))}function f(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,p))},u.decode=function(t){return decodeURIComponent(Array.from(atob(t),f).join(""))};class d extends Error{constructor(t,e,n=e&&e.status){super(t),this.code=n,this.origin=e,Error.captureStackTrace&&Error.captureStackTrace(this,d)}}function h(t){return new Promise(e=>setTimeout(e,t))}function y({fetch:t,cooldown:e=0,getAccessToken:o}){const r=l();return t=>r.write(function(){var o=n((function*(n){try{return yield function(t){return a.apply(this,arguments)}(t)}finally{e&&t.method&&"GET"!==t.method?setTimeout(n,e):n()}}));return function(t){return o.apply(this,arguments)}}());function a(){return(a=n((function*(e){let n=e.path,r=e.contentType,a=e.headers,l=e.format,s=c(e,["path","contentType","headers","format"]);const u={Authorization:"Bearer ".concat(yield o())};for(r&&(u["Content-Type"]=r),Object.assign(u,a);;){const e=yield t(n,i({headers:u},s));if(!e.ok){const t=e.headers.get("Retry-After");if(t){const e=Number(t);if(e){yield h(1e3*e);continue}}const n=yield e.text();throw new d("failed to fetch [".concat(e.status,"]: ").concat(n),e)}if(l)return yield e[l]();const o=e.headers.get("Content-Type");return/application\/json/.test(o)?yield e.json():yield e.text()}}))).apply(this,arguments)}}var g=Object.freeze({fsDrive:()=>{},github:function({userAgent:t="db-to-cloud",owner:e,repo:o,getAccessToken:r,fetch:i=("undefined"!=typeof self?self:global).fetch}){const c=y({fetch:i,getAccessToken:r,cooldown:1e3}),a=new Map;return{name:"github",get:p,put:d,post:function(t,e){return d(t,e,!1)},delete:function(t){return g.apply(this,arguments)},list:function(t){return s.apply(this,arguments)},shaCache:a};function l(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 s(){return(s=n((function*(t){const n=yield l({path:"/repos/".concat(e,"/").concat(o,"/contents/").concat(t)}),r=[];var i=!0,c=!1,s=void 0;try{for(var u,p=n[Symbol.iterator]();!(i=(u=p.next()).done);i=!0){const t=u.value;r.push(t.name),a.set(t.path,t.sha)}}catch(t){c=!0,s=t}finally{try{i||null==p.return||p.return()}finally{if(c)throw s}}return r}))).apply(this,arguments)}function p(t){return f.apply(this,arguments)}function f(){return(f=n((function*(t){const n=yield l({path:"/repos/".concat(e,"/").concat(o,"/contents/").concat(t)});return a.set(n.path,n.sha),u.decode(n.content)}))).apply(this,arguments)}function d(t,e){return h.apply(this,arguments)}function h(){return(h=n((function*(t,n,r=!0){const i={message:"",content:u.encode(n)};r&&a.has(t)&&(i.sha=a.get(t));const c={method:"PUT",path:"/repos/".concat(e,"/").concat(o,"/contents/").concat(t),contentType:"application/json",body:JSON.stringify(i)};let s,f=!1;for(;!s;){try{s=yield l(c)}catch(e){if(422!==e.code||!e.message.includes('\\"sha\\" wasn\'t supplied'))throw e;if(!r||f)throw e.code="EEXIST",e;yield p(t)}f=!0}a.set(t,s.content.sha)}))).apply(this,arguments)}function g(){return(g=n((function*(t){try{let n=a.get(t);n||(yield p(t),n=a.get(t)),yield l({method:"DELETE",path:"/repos/".concat(e,"/").concat(o,"/contents/").concat(t),body:JSON.stringify({message:"",sha:n})})}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=y({fetch:e,getAccessToken:t});return{name:"dropbox",get:function(t){return s.apply(this,arguments)},put:u,post:function(t,e){return f.apply(this,arguments)},delete:function(t){return d.apply(this,arguments)},list:function(t){return a.apply(this,arguments)}};function r(t){let e=t.path,n=t.body,r=c(t,["path","body"]);return o(i({method:"POST",path:"https://api.dropboxapi.com/2/".concat(e),contentType:"application/json",body:JSON.stringify(n)},r))}function a(){return(a=n((function*(t){const e=[];let n=yield r({path:"files/list_folder",body:{path:"/".concat(t)}});var o=!0,i=!1,c=void 0;try{for(var a,l=n.entries[Symbol.iterator]();!(o=(a=l.next()).done);o=!0){const t=a.value;e.push(t.name)}}catch(t){i=!0,c=t}finally{try{o||null==l.return||l.return()}finally{if(i)throw c}}if(!n.has_more)return e;for(;n.has_more;){n=yield r({path:"files/list_folder/continue",body:{cursor:n.cursor}});var s=!0,u=!1,p=void 0;try{for(var f,d=n.entries[Symbol.iterator]();!(s=(f=d.next()).done);s=!0){const t=f.value;e.push(t.name)}}catch(t){u=!0,p=t}finally{try{s||null==d.return||d.return()}finally{if(u)throw p}}}return e}))).apply(this,arguments)}function l(t){const e=new URLSearchParams;return e.set("arg",JSON.stringify(t)),e.toString()}function s(){return(s=n((function*(t){const e={path:"/".concat(t)};try{return yield o({path:"https://content.dropboxapi.com/2/files/download?".concat(l(e)),format:"text"})}catch(t){throw 409===t.code&&t.message.includes("not_found")&&(t.code="ENOENT"),t}}))).apply(this,arguments)}function u(t,e){return p.apply(this,arguments)}function p(){return(p=n((function*(t,e,n="overwrite"){const r={path:"/".concat(t),mode:n,autorename:!1};yield o({path:"https://content.dropboxapi.com/2/files/upload?".concat(l(r)),method:"POST",contentType:"application/octet-stream",body:e})}))).apply(this,arguments)}function f(){return(f=n((function*(t,e){try{return yield u(t,e,"add")}catch(t){throw 409===t.code&&t.message.includes("conflict")&&(t.code="EEXIST"),t}}))).apply(this,arguments)}function d(){return(d=n((function*(t){try{yield r({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 o=y({fetch:e,getAccessToken:t});return{name:"onedrive",get:function(t){return a.apply(this,arguments)},put:function(t,e){return l.apply(this,arguments)},post:function(t,e){return s.apply(this,arguments)},delete:function(t){return u.apply(this,arguments)},list:function(t){return c.apply(this,arguments)}};function r(t){return i.apply(this,arguments)}function i(){return(i=n((function*(t){return t.path="https://graph.microsoft.com/v1.0/me/drive/special/approot".concat(t.path),yield o(t)}))).apply(this,arguments)}function c(){return(c=n((function*(t){t&&(t=":/".concat(t,":"));let e=yield r({path:"".concat(t,"/children?select=name")}),n=e.value.map(t=>t.name);for(;e["@odata.nextLink"];)e=yield o({path:e["@odata.nextLink"]}),n=n.concat(e.value.map(t=>t.name));return n}))).apply(this,arguments)}function a(){return(a=n((function*(t){return yield r({path:":/".concat(t,":/content"),format:"text"})}))).apply(this,arguments)}function l(){return(l=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 409===t.code&&t.message.includes("nameAlreadyExists")&&(t.code="EEXIST"),t}}))).apply(this,arguments)}function u(){return(u=n((function*(t){try{yield r({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:o=("undefined"!=typeof self?self:global).FormData,Blob:r=("undefined"!=typeof self?self:global).Blob}){const i=y({fetch:e,getAccessToken:t}),c=new Map;let a;return{name:"google",get:function(t){return T.apply(this,arguments)},put:function(t,e){return j.apply(this,arguments)},post:k,delete:function(t){return x.apply(this,arguments)},list:function(t){return b.apply(this,arguments)},init:function(){return w.apply(this,arguments)},acquireLock:function(t){return u.apply(this,arguments)},releaseLock:function(){return p.apply(this,arguments)},fileMetaCache:c};function l(t,e){return s.apply(this,arguments)}function s(){return(s=n((function*(t,e){yield i({method:"DELETE",path:"https://www.googleapis.com/drive/v3/files/".concat(t,"/revisions/").concat(e)})}))).apply(this,arguments)}function u(){return(u=n((function*(t){const e=c.get("lock.json"),n=(yield h(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;tDate.now())throw yield l(e.id,n),new d("failed to acquire lock",null,"EEXIST");yield l(e.id,r)}throw new Error("cannot find lock revision")}))).apply(this,arguments)}function p(){return(p=n((function*(){const t=c.get("lock.json");yield l(t.id,a),a=null}))).apply(this,arguments)}function f(){return(f=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 i({path:t});for(e(n);n.nextPageToken;)e(n=yield i({path:"".concat(t,"&pageToken=").concat(n.nextPageToken)}))}))).apply(this,arguments)}function h(t,e){return g.apply(this,arguments)}function g(){return(g=n((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 m.apply(this,arguments)}function m(){return(m=n((function*(t){t&&(t="q=".concat(encodeURIComponent(t))),yield function(t,e){return f.apply(this,arguments)}(t,t=>{var e=!0,n=!1,o=void 0;try{for(var r,i=t.files[Symbol.iterator]();!(e=(r=i.next()).done);e=!0){const t=r.value;c.set(t.name,t)}}catch(t){n=!0,o=t}finally{try{e||null==i.return||i.return()}finally{if(n)throw o}}})}))).apply(this,arguments)}function w(){return(w=n((function*(){yield v(),c.has("lock.json")||(yield k("lock.json","{}")),c.has("meta.json")||(yield k("meta.json","{}"))}))).apply(this,arguments)}function b(){return(b=n((function*(t){return[...c.values()].filter(e=>e.name.startsWith(t+"/")).map(t=>t.name.split("/")[1])}))).apply(this,arguments)}function T(){return(T=n((function*(t){let e=c.get(t);if(!(e||(yield v("name = '".concat(t,"'")),e=c.get(t))))throw new d("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 j(){return(j=n((function*(t,e){if(!c.has(t))return yield k(t,e);const n=c.get(t),o=yield h(n.id,e);n.headRevisionId=o.headRevisionId}))).apply(this,arguments)}function k(t,e){return O.apply(this,arguments)}function O(){return(O=n((function*(t,e){const n=new o,a={name:t,parents:["appDataFolder"]};n.append("metadata",new r([JSON.stringify(a)],{type:"application/json; charset=UTF-8"})),n.append("media",new r([e],{type:"text/plain"}));const l=yield i({method:"POST",path:"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,name,headRevisionId",body:n});c.set(l.name,l)}))).apply(this,arguments)}function x(){return(x=n((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)}}});return t.dbToCloud=function({onGet:t,onPut:e,onDelete:o,onFirstSync:r,onWarn:i=console.error,onProgress:c,compareRevision:u,getState:p,setState:f,lockExpire:d=60}){let h,y,g;const v=new Map,m=s(()=>f(h,y)),w=new Map,b=l();return{use:function(t){h=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 o.apply(this,arguments)},e.releaseLock=function(){return r.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 o(){return(o=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 r(){return(r=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||404===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.write(n((function*(){if(!y||!y.enabled){if(!h)throw new Error("cloud drive is undefined");h.init&&(yield h.init()),(y=(yield p(h))||{}).enabled=!0,y.queue||(y.queue=[]),null==y.lastChange&&(yield r()),yield O()}})))},stop:function(){return b.write(n((function*(){y&&y.enabled&&(y=g=null,v.clear(),w.clear(),h.uninit&&(yield h.uninit()),yield m())})))},put:function(t,e){if(!y||!y.enabled)return;y.queue.push({_id:t,_rev:e,action:"put"}),m()},delete:function(t,e){if(!y||!y.enabled)return;y.queue.push({_id:t,_rev:e,action:"delete"}),m()},syncNow:function(t){return b.write(n((function*(){if(!y||!y.enabled)throw new Error("Cannot sync now, the sync is not enabled");yield O(t)})))},drive:()=>h,isInit:()=>Boolean(y&&y.enabled)};function T(){return(T=n((function*(){if(!(g=yield h.getMeta()).lastChange||g.lastChange===y.lastChange)return;let t=[];if(y.lastChange){const e=Math.floor((g.lastChange-1)/100);let n=Math.floor(y.lastChange/100);for(;n<=e;){const e=yield h.get("changes/".concat(n,".json"));v.set(n,e),t=t.concat(e),n++}t=t.slice(y.lastChange%100)}else t=(yield h.list("docs")).map(t=>({action:"put",_id:t.slice(0,-5)}));const n=new Map;var r=!0,l=!1,s=void 0;try{for(var u,p=t[Symbol.iterator]();!(r=(u=p.next()).done);r=!0){const t=u.value;n.set(t._id,t)}}catch(t){l=!0,s=t}finally{try{r||null==p.return||p.return()}finally{if(l)throw s}}let f=0;var d=!0,b=!1,T=void 0;try{for(var j,k=n[Symbol.iterator]();!(d=(j=k.next()).done);d=!0){const t=a(j.value,2),r=t[0],l=t[1];let s,u;if(c&&c({phase:"pull",total:n.size,loaded:f,change:l}),"delete"===l.action)yield o(r,l._rev);else if("put"===l.action){try{var O=yield h.get("docs/".concat(r,".json"));s=O.doc,u=O._rev}catch(t){if("ENOENT"===t.code||404===t.code){i("Cannot find ".concat(r,". Is it deleted without updating the history?")),f++;continue}throw t}yield e(s)}const p=l._rev||u;p&&w.set(r,p),f++}}catch(t){b=!0,T=t}finally{try{d||null==k.return||k.return()}finally{if(b)throw T}}y.lastChange=g.lastChange,yield m()}))).apply(this,arguments)}function j(){return(j=n((function*(){if(!y.queue.length)return;const e=y.queue.slice(),n=new Map;var o=!0,r=!1,i=void 0;try{for(var a,l=e[Symbol.iterator]();!(o=(a=l.next()).done);o=!0){const t=a.value;n.set(t._id,t)}}catch(t){r=!0,i=t}finally{try{o||null==l.return||l.return()}finally{if(r)throw i}}const s=[];var p=!0,f=!1,d=void 0;try{for(var b,T=n.values()[Symbol.iterator]();!(p=(b=T.next()).done);p=!0){const t=b.value,e=w.get(t._id);void 0!==e&&u(t._rev,e)<=0||s.push(t)}}catch(t){f=!0,d=t}finally{try{p||null==T.return||T.return()}finally{if(f)throw d}}let j,k,O=0;for(var x=0,E=s;x=0||(r[n]=t[n]);return r}(t,e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);for(o=0;o=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){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var n=[],o=!0,r=!1,i=void 0;try{for(var c,a=t[Symbol.iterator]();!(o=(c=a.next()).done)&&(n.push(c.value),!e||n.length!==e);o=!0);}catch(t){r=!0,i=t}finally{try{o||null==a.return||a.return()}finally{if(r)throw i}}return n}(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=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=t[Symbol.iterator]()},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()}}function f(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}}const d={};function h(t){return String.fromCharCode(parseInt(t.slice(1),16))}function y(t){return"%".concat("00".concat(t.charCodeAt(0).toString(16)).slice(-2))}Object.defineProperty(d,"__esModule",{value:!0}),d.encode=function(t){return btoa(encodeURIComponent(t).replace(/%[0-9A-F]{2}/g,h))},d.decode=function(t){return decodeURIComponent(Array.from(atob(t),y).join(""))};class g extends Error{constructor(t,e,n=e&&e.status){super(t),this.code=n,this.origin=e,Error.captureStackTrace&&Error.captureStackTrace(this,g)}}function v(t){return new Promise((e=>setTimeout(e,t)))}function m({fetch:t,cooldown:e=0,getAccessToken:o}){const r=p();return t=>r.write(function(){var o=n((function*(n){try{return yield function(t){return a.apply(this,arguments)}(t)}finally{e&&t.method&&"GET"!==t.method?setTimeout(n,e):n()}}));return function(t){return o.apply(this,arguments)}}());function a(){return(a=n((function*(e){let n=e.path,r=e.contentType,a=e.headers,s=e.format,l=c(e,["path","contentType","headers","format"]);const u={Authorization:"Bearer ".concat(yield o())};for(r&&(u["Content-Type"]=r),Object.assign(u,a);;){const e=yield t(n,i({headers:u},l));if(!e.ok){const t=e.headers.get("Retry-After");if(t){const e=Number(t);if(e){yield v(1e3*e);continue}}const n=yield e.text();throw new g("failed to fetch [".concat(e.status,"]: ").concat(n),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)}}var b=Object.freeze({__proto__:null,fsDrive:()=>{},github:function({userAgent:t="db-to-cloud",owner:e,repo:o,getAccessToken:r,fetch:i=("undefined"!=typeof self?self:global).fetch}){const c=m({fetch:i,getAccessToken:r,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 g.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=n((function*(t){const n=[];var r,i=u(yield s({path:"/repos/".concat(e,"/").concat(o,"/contents/").concat(t)}));try{for(i.s();!(r=i.n()).done;){const t=r.value;n.push(t.name),a.set(t.path,t.sha)}}catch(t){i.e(t)}finally{i.f()}return n}))).apply(this,arguments)}function p(t){return f.apply(this,arguments)}function f(){return(f=n((function*(t){const n=yield s({path:"/repos/".concat(e,"/").concat(o,"/contents/").concat(t)});return a.set(n.path,n.sha),d.decode(n.content)}))).apply(this,arguments)}function h(t,e){return y.apply(this,arguments)}function y(){return(y=n((function*(t,n,r=!0){const i={message:"",content:d.encode(n)};r&&a.has(t)&&(i.sha=a.get(t));const c={method:"PUT",path:"/repos/".concat(e,"/").concat(o,"/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 g(){return(g=n((function*(t){try{let n=a.get(t);n||(yield p(t),n=a.get(t)),yield s({method:"DELETE",path:"/repos/".concat(e,"/").concat(o,"/contents/").concat(t),body:JSON.stringify({message:"",sha:n})})}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=m({fetch:e,getAccessToken:t});return{name:"dropbox",get:function(t){return l.apply(this,arguments)},put:p,post:function(t,e){return d.apply(this,arguments)},delete:function(t){return h.apply(this,arguments)},list:function(t){return a.apply(this,arguments)}};function r(t){let e=t.path,n=t.body,r=c(t,["path","body"]);return o(i({method:"POST",path:"https://api.dropboxapi.com/2/".concat(e),contentType:"application/json",body:JSON.stringify(n)},r))}function a(){return(a=n((function*(t){const e=[];let n=yield r({path:"files/list_folder",body:{path:"/".concat(t)}});var o,i=u(n.entries);try{for(i.s();!(o=i.n()).done;){const t=o.value;e.push(t.name)}}catch(t){i.e(t)}finally{i.f()}if(!n.has_more)return e;for(;n.has_more;){n=yield r({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=n((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=n((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 d(){return(d=n((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 h(){return(h=n((function*(t){try{yield r({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 o=m({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 r(t){return i.apply(this,arguments)}function i(){return(i=n((function*(t){return t.path="https://graph.microsoft.com/v1.0/me/drive/special/approot".concat(t.path),yield o(t)}))).apply(this,arguments)}function c(){return(c=n((function*(t){t&&(t=":/".concat(t,":"));let e=yield r({path:"".concat(t,"/children?select=name")}),n=e.value.map((t=>t.name));for(;e["@odata.nextLink"];)e=yield o({path:e["@odata.nextLink"]}),n=n.concat(e.value.map((t=>t.name)));return n}))).apply(this,arguments)}function a(){return(a=n((function*(t){return yield r({path:":/".concat(t,":/content"),format:"text"})}))).apply(this,arguments)}function s(){return(s=n((function*(t,e){yield r({method:"PUT",path:":/".concat(t,":/content"),headers:{"Content-Type":"text/plain"},body:e})}))).apply(this,arguments)}function l(){return(l=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 409===t.code&&t.message.includes("nameAlreadyExists")&&(t.code="EEXIST"),t}}))).apply(this,arguments)}function u(){return(u=n((function*(t){try{yield r({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:o=("undefined"!=typeof self?self:global).FormData,Blob:r=("undefined"!=typeof self?self:global).Blob}){const i=m({fetch:e,getAccessToken:t}),c=new Map;let a;return{name:"google",get:function(t){return O.apply(this,arguments)},put:function(t,e){return k.apply(this,arguments)},post:E,delete:function(t){return S.apply(this,arguments)},list:function(t){return j.apply(this,arguments)},init:function(){return T.apply(this,arguments)},acquireLock:function(t){return p.apply(this,arguments)},releaseLock:function(){return f.apply(this,arguments)},fileMetaCache:c};function s(t,e){return l.apply(this,arguments)}function l(){return(l=n((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=n((function*(t){const e=c.get("lock.json"),n=(yield y(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;tDate.now())throw yield s(e.id,n),new g("failed to acquire lock",null,"EEXIST");yield s(e.id,r)}throw new Error("cannot find lock revision")}))).apply(this,arguments)}function f(){return(f=n((function*(){const t=c.get("lock.json");yield s(t.id,a),a=null}))).apply(this,arguments)}function d(t,e){return h.apply(this,arguments)}function h(){return(h=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 i({path:t});for(e(n);n.nextPageToken;)n=yield i({path:"".concat(t,"&pageToken=").concat(n.nextPageToken)}),e(n)}))).apply(this,arguments)}function y(t,e){return v.apply(this,arguments)}function v(){return(v=n((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 b(t){return w.apply(this,arguments)}function w(){return(w=n((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 T(){return(T=n((function*(){yield b(),c.has("lock.json")||(yield E("lock.json","{}")),c.has("meta.json")||(yield E("meta.json","{}"))}))).apply(this,arguments)}function j(){return(j=n((function*(t){return[...c.values()].filter((e=>e.name.startsWith(t+"/"))).map((t=>t.name.split("/")[1]))}))).apply(this,arguments)}function O(){return(O=n((function*(t){let e=c.get(t);if(!e&&(yield b("name = '".concat(t,"'")),e=c.get(t),!e))throw new g("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=n((function*(t,e){if(!c.has(t))return yield E(t,e);const n=c.get(t),o=yield y(n.id,e);n.headRevisionId=o.headRevisionId}))).apply(this,arguments)}function E(t,e){return C.apply(this,arguments)}function C(){return(C=n((function*(t,e){const n=new o,a={name:t,parents:["appDataFolder"]};n.append("metadata",new r([JSON.stringify(a)],{type:"application/json; charset=UTF-8"})),n.append("media",new r([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:n});c.set(s.name,s)}))).apply(this,arguments)}function S(){return(S=n((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)}}});return t.dbToCloud=function({onGet:t,onPut:e,onDelete:o,onFirstSync:r,onWarn:i=console.error,onProgress:c,compareRevision:s,getState:l,setState:d,lockExpire:h=60}){let y,g,v;const m=new Map,b=f((()=>d(y,g))),w=new Map,T=p();return{use:function(t){y=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.isInit=!1,e.acquireLock||(e.acquireLock=function(t){return o.apply(this,arguments)},e.releaseLock=function(){return r.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 o(){return(o=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 r(){return(r=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||404===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)},init:function(){return T.write(n((function*(){if(!g||!g.enabled){if(!y)throw new Error("cloud drive is undefined");g=(yield l(y))||{},g.enabled=!0,g.queue||(g.queue=[])}})))},uninit:function(){return T.write(n((function*(){g&&g.enabled&&(g=v=null,m.clear(),w.clear(),y.uninit&&y.isInit&&(yield y.uninit(),y.isInit=!1),yield b())})))},put:function(t,e){if(!g||!g.enabled)return;g.queue.push({_id:t,_rev:e,action:"put"}),b()},delete:function(t,e){if(!g||!g.enabled)return;g.queue.push({_id:t,_rev:e,action:"delete"}),b()},syncNow:function(t){return T.write(n((function*(){if(!g||!g.enabled)throw new Error("Cannot sync now, the sync is not enabled");y.init&&!y.isInit&&(yield y.init(),y.isInit=!0),null==g.lastChange&&(yield r()),yield function(){return x.apply(this,arguments)}(t)})))},drive:()=>y,isInit:()=>Boolean(g&&g.enabled)};function j(){return O.apply(this,arguments)}function O(){return(O=n((function*(){if(v=yield y.getMeta(),!v.lastChange||v.lastChange===g.lastChange)return;let t=[];if(g.lastChange){const e=Math.floor((v.lastChange-1)/100);let n=Math.floor(g.lastChange/100);for(;n<=e;){const e=yield y.get("changes/".concat(n,".json"));m.set(n,e),t=t.concat(e),n++}t=t.slice(g.lastChange%100)}else t=(yield y.list("docs")).map((t=>({action:"put",_id:t.slice(0,-5)})));const n=new Map;var r,s=u(t);try{for(s.s();!(r=s.n()).done;){const t=r.value;n.set(t._id,t)}}catch(t){s.e(t)}finally{s.f()}let l=0;var p,f=u(n);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:n.size,loaded:l,change:s}),"delete"===s.action)yield o(r,s._rev);else if("put"===s.action){try{var d=yield y.get("docs/".concat(r,".json"));u=d.doc,f=d._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 h=s._rev||f;h&&w.set(r,h),l++}}catch(t){f.e(t)}finally{f.f()}g.lastChange=v.lastChange,yield b()}))).apply(this,arguments)}function k(){return E.apply(this,arguments)}function E(){return(E=n((function*(){if(!g.queue.length)return;const e=g.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=w.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,d=0;for(var h=0,T=i;h