Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

firebase-installations.js 12KB

123
  1. import{registerVersion as t,_getProvider as e,getApp as n,_registerComponent as r}from"https://www.gstatic.com/firebasejs/9.16.0/firebase-app.js";class a extends Error{constructor(t,e,n){super(e),this.code=t,this.customData=n,this.name="FirebaseError",Object.setPrototypeOf(this,a.prototype),Error.captureStackTrace&&Error.captureStackTrace(this,i.prototype.create)}}class i{constructor(t,e,n){this.service=t,this.serviceName=e,this.errors=n}create(t,...e){const n=e[0]||{},r=`${this.service}/${t}`,i=this.errors[t],s=i?function(t,e){return t.replace(o,((t,n)=>{const r=e[n];return null!=r?String(r):`<${n}?>`}))}(i,n):"Error",c=`${this.serviceName}: ${s} (${r}).`;return new a(r,c,n)}}const o=/\{\$([^}]+)}/g;class s{constructor(t,e,n){this.name=t,this.instanceFactory=e,this.type=n,this.multipleInstances=!1,this.serviceProps={},this.instantiationMode="LAZY",this.onInstanceCreated=null}setInstantiationMode(t){return this.instantiationMode=t,this}setMultipleInstances(t){return this.multipleInstances=t,this}setServiceProps(t){return this.serviceProps=t,this}setInstanceCreatedCallback(t){return this.onInstanceCreated=t,this}}let c,u;const f=new WeakMap,p=new WeakMap,d=new WeakMap,l=new WeakMap,g=new WeakMap;let w={get(t,e,n){if(t instanceof IDBTransaction){if("done"===e)return p.get(t);if("objectStoreNames"===e)return t.objectStoreNames||d.get(t);if("store"===e)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return y(t[e])},set:(t,e,n)=>(t[e]=n,!0),has:(t,e)=>t instanceof IDBTransaction&&("done"===e||"store"===e)||e in t};function h(t){return t!==IDBDatabase.prototype.transaction||"objectStoreNames"in IDBTransaction.prototype?(u||(u=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(t)?function(...e){return t.apply(v(this),e),y(f.get(this))}:function(...e){return y(t.apply(v(this),e))}:function(e,...n){const r=t.call(v(this),e,...n);return d.set(r,e.sort?e.sort():[e]),y(r)}}function m(t){return"function"==typeof t?h(t):(t instanceof IDBTransaction&&function(t){if(p.has(t))return;const e=new Promise(((e,n)=>{const r=()=>{t.removeEventListener("complete",a),t.removeEventListener("error",i),t.removeEventListener("abort",i)},a=()=>{e(),r()},i=()=>{n(t.error||new DOMException("AbortError","AbortError")),r()};t.addEventListener("complete",a),t.addEventListener("error",i),t.addEventListener("abort",i)}));p.set(t,e)}(t),e=t,(c||(c=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction])).some((t=>e instanceof t))?new Proxy(t,w):t);var e}function y(t){if(t instanceof IDBRequest)return function(t){const e=new Promise(((e,n)=>{const r=()=>{t.removeEventListener("success",a),t.removeEventListener("error",i)},a=()=>{e(y(t.result)),r()},i=()=>{n(t.error),r()};t.addEventListener("success",a),t.addEventListener("error",i)}));return e.then((e=>{e instanceof IDBCursor&&f.set(e,t)})).catch((()=>{})),g.set(e,t),e}(t);if(l.has(t))return l.get(t);const e=m(t);return e!==t&&(l.set(t,e),g.set(e,t)),e}const v=t=>g.get(t);const I=["get","getKey","getAll","getAllKeys","count"],b=["put","add","delete","clear"],S=new Map;function C(t,e){if(!(t instanceof IDBDatabase)||e in t||"string"!=typeof e)return;if(S.get(e))return S.get(e);const n=e.replace(/FromIndex$/,""),r=e!==n,a=b.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!a&&!I.includes(n))return;const i=async function(t,...e){const i=this.transaction(t,a?"readwrite":"readonly");let o=i.store;return r&&(o=o.index(e.shift())),(await Promise.all([o[n](...e),a&&i.done]))[0]};return S.set(e,i),i}w=(t=>({...t,get:(e,n,r)=>C(e,n)||t.get(e,n,r),has:(e,n)=>!!C(e,n)||t.has(e,n)}))(w);const k="@firebase/installations",T=new i("installations","Installations",{"missing-app-config-values":'Missing App configuration value: "{$valueName}"',"not-registered":"Firebase Installation is not registered.","installation-not-found":"Firebase Installation not found.","request-failed":'{$requestName} request failed with error "{$serverCode} {$serverStatus}: {$serverMessage}"',"app-offline":"Could not process request. Application offline.","delete-pending-registration":"Can't delete installation while there is a pending registration request."});function D(t){return t instanceof a&&t.code.includes("request-failed")}function E({projectId:t}){return`https://firebaseinstallations.googleapis.com/v1/projects/${t}/installations`}function j(t){return{token:t.token,requestStatus:2,expiresIn:(e=t.expiresIn,Number(e.replace("s","000"))),creationTime:Date.now()};var e}async function P(t,e){const n=(await e.json()).error;return T.create("request-failed",{requestName:t,serverCode:n.code,serverMessage:n.message,serverStatus:n.status})}function B({apiKey:t}){return new Headers({"Content-Type":"application/json",Accept:"application/json","x-goog-api-key":t})}function $(t,{refreshToken:e}){const n=B(t);return n.append("Authorization",function(t){return`FIS_v2 ${t}`}(e)),n}async function q(t){const e=await t();return e.status>=500&&e.status<600?t():e}function L(t){return new Promise((e=>{setTimeout(e,t)}))}const O=/^[cdef][\w-]{21}$/;function N(){try{const t=new Uint8Array(17);(self.crypto||self.msCrypto).getRandomValues(t),t[0]=112+t[0]%16;const e=function(t){return(e=t,btoa(String.fromCharCode(...e)).replace(/\+/g,"-").replace(/\//g,"_")).substr(0,22);var e}(t);return O.test(e)?e:""}catch(t){return""}}function M(t){return`${t.appName}!${t.appId}`}const x=new Map;function A(t,e){const n=M(t);F(n,e),function(t,e){const n=V();n&&n.postMessage({key:t,fid:e});H()}(n,e)}function F(t,e){const n=x.get(t);if(n)for(const t of n)t(e)}let K=null;function V(){return!K&&"BroadcastChannel"in self&&(K=new BroadcastChannel("[Firebase] FID Change"),K.onmessage=t=>{F(t.data.key,t.data.fid)}),K}function H(){0===x.size&&K&&(K.close(),K=null)}const W="firebase-installations-store";let _=null;function z(){return _||(_=function(t,e,{blocked:n,upgrade:r,blocking:a,terminated:i}={}){const o=indexedDB.open(t,e),s=y(o);return r&&o.addEventListener("upgradeneeded",(t=>{r(y(o.result),t.oldVersion,t.newVersion,y(o.transaction))})),n&&o.addEventListener("blocked",(()=>n())),s.then((t=>{i&&t.addEventListener("close",(()=>i())),a&&t.addEventListener("versionchange",(()=>a()))})).catch((()=>{})),s}("firebase-installations-database",1,{upgrade:(t,e)=>{if(0===e)t.createObjectStore(W)}})),_}async function R(t,e){const n=M(t),r=(await z()).transaction(W,"readwrite"),a=r.objectStore(W),i=await a.get(n);return await a.put(e,n),await r.done,i&&i.fid===e.fid||A(t,e.fid),e}async function J(t){const e=M(t),n=(await z()).transaction(W,"readwrite");await n.objectStore(W).delete(e),await n.done}async function U(t,e){const n=M(t),r=(await z()).transaction(W,"readwrite"),a=r.objectStore(W),i=await a.get(n),o=e(i);return void 0===o?await a.delete(n):await a.put(o,n),await r.done,!o||i&&i.fid===o.fid||A(t,o.fid),o}async function G(t){let e;const n=await U(t.appConfig,(n=>{const r=function(t){return Q(t||{fid:N(),registrationStatus:0})}(n),a=function(t,e){if(0===e.registrationStatus){if(!navigator.onLine){return{installationEntry:e,registrationPromise:Promise.reject(T.create("app-offline"))}}const n={fid:e.fid,registrationStatus:1,registrationTime:Date.now()},r=async function(t,e){try{const n=await async function({appConfig:t,heartbeatServiceProvider:e},{fid:n}){const r=E(t),a=B(t),i=e.getImmediate({optional:!0});if(i){const t=await i.getHeartbeatsHeader();t&&a.append("x-firebase-client",t)}const o={fid:n,authVersion:"FIS_v2",appId:t.appId,sdkVersion:"w:0.6.1"},s={method:"POST",headers:a,body:JSON.stringify(o)},c=await q((()=>fetch(r,s)));if(c.ok){const t=await c.json();return{fid:t.fid||n,registrationStatus:2,refreshToken:t.refreshToken,authToken:j(t.authToken)}}throw await P("Create Installation",c)}(t,e);return R(t.appConfig,n)}catch(n){throw D(n)&&409===n.customData.serverCode?await J(t.appConfig):await R(t.appConfig,{fid:e.fid,registrationStatus:0}),n}}(t,n);return{installationEntry:n,registrationPromise:r}}return 1===e.registrationStatus?{installationEntry:e,registrationPromise:Y(t)}:{installationEntry:e}}(t,r);return e=a.registrationPromise,a.installationEntry}));return""===n.fid?{installationEntry:await e}:{installationEntry:n,registrationPromise:e}}async function Y(t){let e=await Z(t.appConfig);for(;1===e.registrationStatus;)await L(100),e=await Z(t.appConfig);if(0===e.registrationStatus){const{installationEntry:e,registrationPromise:n}=await G(t);return n||e}return e}function Z(t){return U(t,(t=>{if(!t)throw T.create("installation-not-found");return Q(t)}))}function Q(t){return 1===(e=t).registrationStatus&&e.registrationTime+1e4<Date.now()?{fid:t.fid,registrationStatus:0}:t;var e}async function X({appConfig:t,heartbeatServiceProvider:e},n){const r=function(t,{fid:e}){return`${E(t)}/${e}/authTokens:generate`}(t,n),a=$(t,n),i=e.getImmediate({optional:!0});if(i){const t=await i.getHeartbeatsHeader();t&&a.append("x-firebase-client",t)}const o={installation:{sdkVersion:"w:0.6.1",appId:t.appId}},s={method:"POST",headers:a,body:JSON.stringify(o)},c=await q((()=>fetch(r,s)));if(c.ok){return j(await c.json())}throw await P("Generate Auth Token",c)}async function tt(t,e=!1){let n;const r=await U(t.appConfig,(r=>{if(!nt(r))throw T.create("not-registered");const a=r.authToken;if(!e&&function(t){return 2===t.requestStatus&&!function(t){const e=Date.now();return e<t.creationTime||t.creationTime+t.expiresIn<e+36e5}(t)}(a))return r;if(1===a.requestStatus)return n=async function(t,e){let n=await et(t.appConfig);for(;1===n.authToken.requestStatus;)await L(100),n=await et(t.appConfig);const r=n.authToken;return 0===r.requestStatus?tt(t,e):r}(t,e),r;{if(!navigator.onLine)throw T.create("app-offline");const e=function(t){const e={requestStatus:1,requestTime:Date.now()};return Object.assign(Object.assign({},t),{authToken:e})}(r);return n=async function(t,e){try{const n=await X(t,e),r=Object.assign(Object.assign({},e),{authToken:n});return await R(t.appConfig,r),n}catch(n){if(!D(n)||401!==n.customData.serverCode&&404!==n.customData.serverCode){const n=Object.assign(Object.assign({},e),{authToken:{requestStatus:0}});await R(t.appConfig,n)}else await J(t.appConfig);throw n}}(t,e),e}}));return n?await n:r.authToken}function et(t){return U(t,(t=>{if(!nt(t))throw T.create("not-registered");const e=t.authToken;return 1===(n=e).requestStatus&&n.requestTime+1e4<Date.now()?Object.assign(Object.assign({},t),{authToken:{requestStatus:0}}):t;var n}))}function nt(t){return void 0!==t&&2===t.registrationStatus}async function rt(t){const e=t,{installationEntry:n,registrationPromise:r}=await G(e);return r?r.catch(console.error):tt(e).catch(console.error),n.fid}async function at(t,e=!1){const n=t;await async function(t){const{registrationPromise:e}=await G(t);e&&await e}(n);return(await tt(n,e)).token}async function it(t,e){const n=function(t,{fid:e}){return`${E(t)}/${e}`}(t,e),r={method:"DELETE",headers:$(t,e)},a=await q((()=>fetch(n,r)));if(!a.ok)throw await P("Delete Installation",a)}async function ot(t){const{appConfig:e}=t,n=await U(e,(t=>{if(!t||0!==t.registrationStatus)return t}));if(n){if(1===n.registrationStatus)throw T.create("delete-pending-registration");if(2===n.registrationStatus){if(!navigator.onLine)throw T.create("app-offline");await it(e,n),await J(e)}}}function st(t,e){const{appConfig:n}=t;return function(t,e){V();const n=M(t);let r=x.get(n);r||(r=new Set,x.set(n,r)),r.add(e)}(n,e),()=>{!function(t,e){const n=M(t),r=x.get(n);r&&(r.delete(e),0===r.size&&x.delete(n),H())}(n,e)}}function ct(t=n()){return e(t,"installations").getImmediate()}function ut(t){return T.create("missing-app-config-values",{valueName:t})}const ft=t=>{const n=t.getProvider("app").getImmediate(),r=e(n,"installations").getImmediate();return{getId:()=>rt(r),getToken:t=>at(r,t)}};r(new s("installations",(t=>{const n=t.getProvider("app").getImmediate(),r=function(t){if(!t||!t.options)throw ut("App Configuration");if(!t.name)throw ut("App Name");const e=["projectId","apiKey","appId"];for(const n of e)if(!t.options[n])throw ut(n);return{appName:t.name,projectId:t.options.projectId,apiKey:t.options.apiKey,appId:t.options.appId}}(n);return{app:n,appConfig:r,heartbeatServiceProvider:e(n,"heartbeat"),_delete:()=>Promise.resolve()}}),"PUBLIC")),r(new s("installations-internal",ft,"PRIVATE")),t(k,"0.6.1"),t(k,"0.6.1","esm2017");export{ot as deleteInstallations,rt as getId,ct as getInstallations,at as getToken,st as onIdChange};
  2. //# sourceMappingURL=firebase-installations.js.map