firebase
Version:
Firebase JavaScript library for web and Node.js
4 lines (2 loc) • 33.2 kB
JavaScript
import{_removeServiceInstance as e,getApp as t,_getProvider,_registerComponent as s,registerVersion as r,_isFirebaseServerApp as n,SDK_VERSION as i}from"https://www.gstatic.com/firebasejs/12.10.0/firebase-app.js";function isCloudWorkstation(e){try{return(e.startsWith("http://")||e.startsWith("https://")?new URL(e).hostname:e).endsWith(".cloudworkstations.dev")}catch{return!1}}const a={};let o=!1;function updateEmulatorBanner(e,t){if("undefined"==typeof window||"undefined"==typeof document||!isCloudWorkstation(window.location.host)||a[e]===t||a[e]||o)return;function prefixedId(e){return`__firebase__banner__${e}`}a[e]=t;const s="__firebase__banner",r=function getEmulatorSummary(){const e={prod:[],emulator:[]};for(const t of Object.keys(a))a[t]?e.emulator.push(t):e.prod.push(t);return e}().prod.length>0;function setupCloseBtn(){const e=document.createElement("span");return e.style.cursor="pointer",e.style.marginLeft="16px",e.style.fontSize="24px",e.innerHTML=" ×",e.onclick=()=>{o=!0,function tearDown(){const e=document.getElementById(s);e&&e.remove()}()},e}function setupDom(){const e=function getOrCreateEl(e){let t=document.getElementById(e),s=!1;return t||(t=document.createElement("div"),t.setAttribute("id",e),s=!0),{created:s,element:t}}(s),t=prefixedId("text"),n=document.getElementById(t)||document.createElement("span"),i=prefixedId("learnmore"),a=document.getElementById(i)||document.createElement("a"),o=prefixedId("preprendIcon"),c=document.getElementById(o)||document.createElementNS("http://www.w3.org/2000/svg","svg");if(e.created){const t=e.element;!function setupBannerStyles(e){e.style.display="flex",e.style.background="#7faaf0",e.style.position="fixed",e.style.bottom="5px",e.style.left="5px",e.style.padding=".5em",e.style.borderRadius="5px",e.style.alignItems="center"}(t),function setupLinkStyles(e,t){e.setAttribute("id",t),e.innerText="Learn more",e.href="https://firebase.google.com/docs/studio/preview-apps#preview-backend",e.setAttribute("target","__blank"),e.style.paddingLeft="5px",e.style.textDecoration="underline"}(a,i);const s=setupCloseBtn();!function setupIconStyles(e,t){e.setAttribute("width","24"),e.setAttribute("id",t),e.setAttribute("height","24"),e.setAttribute("viewBox","0 0 24 24"),e.setAttribute("fill","none"),e.style.marginLeft="-6px"}(c,o),t.append(c,n,a,s),document.body.appendChild(t)}r?(n.innerText="Preview backend disconnected.",c.innerHTML='<g clip-path="url(#clip0_6013_33858)">\n<path d="M4.8 17.6L12 5.6L19.2 17.6H4.8ZM6.91667 16.4H17.0833L12 7.93333L6.91667 16.4ZM12 15.6C12.1667 15.6 12.3056 15.5444 12.4167 15.4333C12.5389 15.3111 12.6 15.1667 12.6 15C12.6 14.8333 12.5389 14.6944 12.4167 14.5833C12.3056 14.4611 12.1667 14.4 12 14.4C11.8333 14.4 11.6889 14.4611 11.5667 14.5833C11.4556 14.6944 11.4 14.8333 11.4 15C11.4 15.1667 11.4556 15.3111 11.5667 15.4333C11.6889 15.5444 11.8333 15.6 12 15.6ZM11.4 13.6H12.6V10.4H11.4V13.6Z" fill="#212121"/>\n</g>\n<defs>\n<clipPath id="clip0_6013_33858">\n<rect width="24" height="24" fill="white"/>\n</clipPath>\n</defs>'):(c.innerHTML='<g clip-path="url(#clip0_6083_34804)">\n<path d="M11.4 15.2H12.6V11.2H11.4V15.2ZM12 10C12.1667 10 12.3056 9.94444 12.4167 9.83333C12.5389 9.71111 12.6 9.56667 12.6 9.4C12.6 9.23333 12.5389 9.09444 12.4167 8.98333C12.3056 8.86111 12.1667 8.8 12 8.8C11.8333 8.8 11.6889 8.86111 11.5667 8.98333C11.4556 9.09444 11.4 9.23333 11.4 9.4C11.4 9.56667 11.4556 9.71111 11.5667 9.83333C11.6889 9.94444 11.8333 10 12 10ZM12 18.4C11.1222 18.4 10.2944 18.2333 9.51667 17.9C8.73889 17.5667 8.05556 17.1111 7.46667 16.5333C6.88889 15.9444 6.43333 15.2611 6.1 14.4833C5.76667 13.7056 5.6 12.8778 5.6 12C5.6 11.1111 5.76667 10.2833 6.1 9.51667C6.43333 8.73889 6.88889 8.06111 7.46667 7.48333C8.05556 6.89444 8.73889 6.43333 9.51667 6.1C10.2944 5.76667 11.1222 5.6 12 5.6C12.8889 5.6 13.7167 5.76667 14.4833 6.1C15.2611 6.43333 15.9389 6.89444 16.5167 7.48333C17.1056 8.06111 17.5667 8.73889 17.9 9.51667C18.2333 10.2833 18.4 11.1111 18.4 12C18.4 12.8778 18.2333 13.7056 17.9 14.4833C17.5667 15.2611 17.1056 15.9444 16.5167 16.5333C15.9389 17.1111 15.2611 17.5667 14.4833 17.9C13.7167 18.2333 12.8889 18.4 12 18.4ZM12 17.2C13.4444 17.2 14.6722 16.6944 15.6833 15.6833C16.6944 14.6722 17.2 13.4444 17.2 12C17.2 10.5556 16.6944 9.32778 15.6833 8.31667C14.6722 7.30555 13.4444 6.8 12 6.8C10.5556 6.8 9.32778 7.30555 8.31667 8.31667C7.30556 9.32778 6.8 10.5556 6.8 12C6.8 13.4444 7.30556 14.6722 8.31667 15.6833C9.32778 16.6944 10.5556 17.2 12 17.2Z" fill="#212121"/>\n</g>\n<defs>\n<clipPath id="clip0_6083_34804">\n<rect width="24" height="24" fill="white"/>\n</clipPath>\n</defs>',n.innerText="Preview backend running in this workspace."),n.setAttribute("id",t)}"loading"===document.readyState?window.addEventListener("DOMContentLoaded",setupDom):setupDom()}class FirebaseError extends Error{constructor(e,t,s){super(t),this.code=e,this.customData=s,this.name="FirebaseError",Object.setPrototypeOf(this,FirebaseError.prototype),Error.captureStackTrace&&Error.captureStackTrace(this,ErrorFactory.prototype.create)}}class ErrorFactory{constructor(e,t,s){this.service=e,this.serviceName=t,this.errors=s}create(e,...t){const s=t[0]||{},r=`${this.service}/${e}`,n=this.errors[e],i=n?function replaceTemplate(e,t){return e.replace(c,((e,s)=>{const r=t[s];return null!=r?String(r):`<${s}?>`}))}(n,s):"Error",a=`${this.serviceName}: ${i} (${r}).`;return new FirebaseError(r,a,s)}}const c=/\{\$([^}]+)}/g;class Component{constructor(e,t,s){this.name=e,this.instanceFactory=t,this.type=s,this.multipleInstances=!1,this.serviceProps={},this.instantiationMode="LAZY",this.onInstanceCreated=null}setInstantiationMode(e){return this.instantiationMode=e,this}setMultipleInstances(e){return this.multipleInstances=e,this}setServiceProps(e){return this.serviceProps=e,this}setInstanceCreatedCallback(e){return this.onInstanceCreated=e,this}}var h;!function(e){e[e.DEBUG=0]="DEBUG",e[e.VERBOSE=1]="VERBOSE",e[e.INFO=2]="INFO",e[e.WARN=3]="WARN",e[e.ERROR=4]="ERROR",e[e.SILENT=5]="SILENT"}(h||(h={}));const l={debug:h.DEBUG,verbose:h.VERBOSE,info:h.INFO,warn:h.WARN,error:h.ERROR,silent:h.SILENT},u=h.INFO,d={[h.DEBUG]:"log",[h.VERBOSE]:"log",[h.INFO]:"info",[h.WARN]:"warn",[h.ERROR]:"error"},defaultLogHandler=(e,t,...s)=>{if(t<e.logLevel)return;const r=(new Date).toISOString(),n=d[t];if(!n)throw new Error(`Attempted to log a message with an invalid logType (value: ${t})`);console[n](`[${r}] ${e.name}:`,...s)};const p="@firebase/data-connect",f="0.4.0";let g="";const y={OTHER:"other",ALREADY_INITIALIZED:"already-initialized",NOT_INITIALIZED:"not-initialized",NOT_SUPPORTED:"not-supported",INVALID_ARGUMENT:"invalid-argument",PARTIAL_ERROR:"partial-error",UNAUTHORIZED:"unauthorized"};class DataConnectError extends FirebaseError{constructor(e,t){super(e,t),this.name="DataConnectError",Object.setPrototypeOf(this,DataConnectError.prototype)}toString(){return`${this.name}[code=${this.code}]: ${this.message}`}}class DataConnectOperationError extends DataConnectError{constructor(e,t){super(y.PARTIAL_ERROR,e),this.name="DataConnectOperationError",this.response=t}}class EntityDataObject{getServerValue(e){return this.serverValues[e]}constructor(e){this.globalID=e,this.serverValues={},this.referencedFrom=new Set}getServerValues(){return this.serverValues}toJSON(){return{globalID:this.globalID,map:this.serverValues,referencedFrom:Array.from(this.referencedFrom)}}static fromJSON(e){const t=new EntityDataObject(e.globalID);return t.serverValues=e.map,t.referencedFrom=new Set(e.referencedFrom),t}updateServerValue(e,t,s){return this.serverValues[e]=t,this.referencedFrom.add(s),Array.from(this.referencedFrom)}}class InMemoryCacheProvider{constructor(e){this._keyId=e,this.edos=new Map,this.resultTrees=new Map}async setResultTree(e,t){this.resultTrees.set(e,t)}async getResultTree(e){return this.resultTrees.get(e)}async updateEntityData(e){this.edos.set(e.globalID,e)}async getEntityData(e){return this.edos.has(e)||this.edos.set(e,new EntityDataObject(e)),this.edos.get(e)}close(){return Promise.resolve()}}const m="_id";class EntityNode{constructor(){this.scalars={},this.references={},this.objectLists={},this.entityDataKeys=new Set}async loadData(e,t,s,r,n){if(void 0!==t){if("object"!=typeof t||Array.isArray(t))throw new DataConnectError(y.INVALID_ARGUMENT,"EntityNode initialized with non-object value");if(null!==t){"object"==typeof t&&s&&s[m]&&"string"==typeof s[m]&&(this.globalId=s[m],this.entityData=await n.getEntityData(this.globalId));for(const i in t)if(t.hasOwnProperty(i))if("object"==typeof t[i])if(Array.isArray(t[i])){const a=s&&s[i],o=[],c=[];for(const[s,h]of t[i].entries())if("object"==typeof h)if(Array.isArray(h));else{const t=new EntityNode;await t.loadData(e,h,a&&a[s],r,n),o.push(t)}else c.push(h);if(c.length>0&&o.length>0)this.scalars[i]=t[i];else if(c.length>0)if(this.entityData){const t=this.entityData.updateServerValue(i,c,e);this.entityDataKeys.add(i),r.add(t)}else this.scalars[i]=c;else o.length>0?this.objectLists[i]=o:this.scalars[i]=[]}else{if(null===t[i]){this.scalars[i]=null;continue}const a=new EntityNode;await a.loadData(e,t[i],s&&s[i],r,n),this.references[i]=a}else if(this.entityData){const s=this.entityData.updateServerValue(i,t[i],e);this.entityDataKeys.add(i),r.add(s)}else this.scalars[i]=t[i];this.entityData&&await n.updateEntityData(this.entityData)}}}toJSON(e){const t={};if(e===C.hydrated){if(this.entityData)for(const e of this.entityDataKeys)t[e]=this.entityData.getServerValue(e);if(this.scalars&&Object.assign(t,this.scalars),this.references)for(const s in this.references)this.references.hasOwnProperty(s)&&(t[s]=this.references[s].toJSON(e));if(this.objectLists)for(const s in this.objectLists)this.objectLists.hasOwnProperty(s)&&(t[s]=this.objectLists[s].map((t=>t.toJSON(e))));return t}if(this.entityData&&(t[m]=this.entityData.globalID),t._entity_data_keys=Array.from(this.entityDataKeys),this.scalars&&(t._scalars=this.scalars),this.references){const s={};for(const t in this.references)this.references.hasOwnProperty(t)&&(s[t]=this.references[t].toJSON(e));t._references=s}if(this.objectLists){const s={};for(const t in this.objectLists)this.objectLists.hasOwnProperty(t)&&(s[t]=this.objectLists[t].map((t=>t.toJSON(e))));t._objectLists=s}return t}static fromJson(e){const t=new EntityNode;if(e.backingData&&(t.entityData=EntityDataObject.fromJSON(e.backingData)),t.globalId=e.globalID,t.scalars=e.scalars,e.references){const s={};for(const t in e.references)e.references.hasOwnProperty(t)&&(s[t]=EntityNode.fromJson(e.references[t]));t.references=s}if(e.objectLists){const s={};for(const t in e.objectLists)e.objectLists.hasOwnProperty(t)&&(s[t]=e.objectLists[t].map((e=>EntityNode.fromJson(e))));t.objectLists=s}return t}}var C;!function(e){e[e.hydrated=0]="hydrated",e[e.dehydrated=1]="dehydrated"}(C||(C={}));class ResultTree{static fromJson(e){return new ResultTree(EntityNode.fromJson(e.rootStub),e.maxAge,e.cachedAt,e.lastAccessed)}constructor(e,t=0,s,r){this.rootStub=e,this.maxAge=t,this.cachedAt=s,this._lastAccessed=r}isStale(){return Date.now()-new Date(this.cachedAt.getTime()).getTime()>1e3*this.maxAge}updateMaxAge(e){this.maxAge=e}updateAccessed(){this._lastAccessed=new Date}get lastAccessed(){return this._lastAccessed}getRootStub(){return this.rootStub}}class ImpactedQueryRefsAccumulator{constructor(e){this.queryId=e,this.impacted=new Set}add(e){e.filter((e=>e!==this.queryId)).forEach((e=>this.impacted.add(e)))}consumeEvents(){const e=Array.from(this.impacted);return this.impacted.clear(),e}}class ResultTreeProcessor{hydrateResults(e){return e.toJSON(C.hydrated)}async dehydrateResults(e,t,s,r){const n=new ImpactedQueryRefsAccumulator(r),i=new EntityNode;return await i.loadData(r,e,t,n,s),{entityNode:i,impacted:n.consumeEvents()}}}class DataConnectCache{constructor(e,t,s,r,n){this.authProvider=e,this.projectId=t,this.connectorConfig=s,this.host=r,this.cacheSettings=n,this.cacheProvider=null,this.uid=null,this.authProvider.addTokenChangeListener((async e=>{const t=this.authProvider.getAuth().getUid();if(this.uid!==t){this.cacheProvider?.close(),this.uid=t;const e=await this.getIdentifier(this.uid);this.cacheProvider=this.initializeNewProviders(e)}}))}async initialize(){if(!this.cacheProvider){const e=await this.getIdentifier(this.uid);this.cacheProvider=this.initializeNewProviders(e)}}async getIdentifier(e){const t=`memory-${this.projectId}-${this.connectorConfig.service}-${this.connectorConfig.connector}-${this.connectorConfig.location}-${e}-${this.host}`;return await async function generateSHA256Hash(e){const t=(new TextEncoder).encode(e),s=await crypto.subtle.digest("SHA-256",t);return Array.from(new Uint8Array(s)).map((e=>e.toString(16).padStart(2,"0"))).join("")}(t)}initializeNewProviders(e){return this.cacheSettings.cacheProvider.initialize(e)}async containsResultTree(e){await this.initialize();return void 0!==await this.cacheProvider.getResultTree(e)}async getResultTree(e){return await this.initialize(),this.cacheProvider.getResultTree(e)}async getResultJSON(e){await this.initialize();const t=new ResultTreeProcessor,s=this.cacheProvider,r=await s.getResultTree(e);if(!r)throw new DataConnectError(y.INVALID_ARGUMENT,`${e} not found in cache. Call "update()" first.`);return t.hydrateResults(r.getRootStub())}async update(e,t,s){await this.initialize();const r=new ResultTreeProcessor,n=this.cacheProvider,{entityNode:i,impacted:a}=await r.dehydrateResults(t,s,n,e),o=new Date;return await n.setResultTree(e,new ResultTree(i,t.maxAge||this.cacheSettings.maxAgeSeconds,o,o)),a}}class MemoryStub{constructor(){this.type="MEMORY"}initialize(e){return new InMemoryCacheProvider(e)}}class AppCheckTokenProvider{constructor(e,t){this.appCheckProvider=t,n(e)&&e.settings.appCheckToken&&(this.serverAppAppCheckToken=e.settings.appCheckToken),this.appCheck=t?.getImmediate({optional:!0}),this.appCheck||t?.get().then((e=>this.appCheck=e)).catch()}getToken(){return this.serverAppAppCheckToken?Promise.resolve({token:this.serverAppAppCheckToken}):this.appCheck?this.appCheck.getToken():new Promise(((e,t)=>{setTimeout((()=>{this.appCheck?this.getToken().then(e,t):e(null)}),0)}))}addTokenChangeListener(e){this.appCheckProvider?.get().then((t=>t.addTokenListener(e)))}}const b=new class Logger{constructor(e){this.name=e,this._logLevel=u,this._logHandler=defaultLogHandler,this._userLogHandler=null}get logLevel(){return this._logLevel}set logLevel(e){if(!(e in h))throw new TypeError(`Invalid value "${e}" assigned to \`logLevel\``);this._logLevel=e}setLogLevel(e){this._logLevel="string"==typeof e?l[e]:e}get logHandler(){return this._logHandler}set logHandler(e){if("function"!=typeof e)throw new TypeError("Value assigned to `logHandler` must be a function");this._logHandler=e}get userLogHandler(){return this._userLogHandler}set userLogHandler(e){this._userLogHandler=e}debug(...e){this._userLogHandler&&this._userLogHandler(this,h.DEBUG,...e),this._logHandler(this,h.DEBUG,...e)}log(...e){this._userLogHandler&&this._userLogHandler(this,h.VERBOSE,...e),this._logHandler(this,h.VERBOSE,...e)}info(...e){this._userLogHandler&&this._userLogHandler(this,h.INFO,...e),this._logHandler(this,h.INFO,...e)}warn(...e){this._userLogHandler&&this._userLogHandler(this,h.WARN,...e),this._logHandler(this,h.WARN,...e)}error(...e){this._userLogHandler&&this._userLogHandler(this,h.ERROR,...e),this._logHandler(this,h.ERROR,...e)}}("@firebase/data-connect");function setLogLevel(e){b.setLogLevel(e)}function logDebug(e){b.debug(`DataConnect (${g}): ${e}`)}function logError(e){b.error(`DataConnect (${g}): ${e}`)}class FirebaseAuthProvider{constructor(e,t,s){this._appName=e,this._options=t,this._authProvider=s,this._auth=s.getImmediate({optional:!0}),this._auth||s.onInit((e=>this._auth=e))}getAuth(){return this._auth}getToken(e){return this._auth?this._auth.getToken(e).catch((e=>e&&"auth/token-not-initialized"===e.code?(logDebug("Got auth/token-not-initialized error. Treating as null token."),null):(logError("Error received when attempting to retrieve token: "+JSON.stringify(e)),Promise.reject(e)))):new Promise(((t,s)=>{setTimeout((()=>{this._auth?this.getToken(e).then(t,s):t(null)}),0)}))}addTokenChangeListener(e){this._auth?.addAuthTokenListener(e)}removeTokenChangeListener(e){this._authProvider.get().then((t=>t.removeAuthTokenListener(e))).catch((e=>logError(e)))}}const _="query",E="mutation",v="SERVER",T="CACHE";function populatePath(e,t,s){let r=t;for(const t of e)"object"!=typeof r[t]&&(r[t]={}),r=r[t];if("entityId"in s&&s.entityId)r._id=s.entityId;else if("entityIds"in s){const e=s.entityIds;for(let t=0;t<e.length;t++){const s=e[t];void 0===r[t]&&(r[t]={}),r[t]._id=s}}}let w,R;function sortKeysForObj(e){return Object.keys(e).sort().reduce(((t,s)=>(t[s]=e[s],t)),{})}function getRefSerializer(e,t,s,r){return function toJSON(){return{data:t,refInfo:{name:e.name,variables:e.variables,connectorConfig:{projectId:e.dataConnect.app.options.projectId,...e.dataConnect.getSettings()}},fetchTime:r,source:s}}}!function setEncoder(e){w=e}((e=>JSON.stringify(sortKeysForObj(e)))),function setDecoder(e){R=e}((e=>sortKeysForObj(JSON.parse(e))));class QueryManager{async preferCacheResults(e,t=!1){let s;try{s=await this.fetchCacheResults(e,t)}catch(e){}return s||this.fetchServerResults(e)}constructor(e,t,s){this.transport=e,this.dc=t,this.cache=s,this.callbacks=new Map,this.subscriptionCache=new Map,this.queue=[]}async waitForQueuedWrites(){for(const e of this.queue)await e;this.queue=[]}updateSSR(e){this.queue.push(this.updateCache(e).then((async t=>this.publishCacheResultsToSubscribers(t,e.fetchTime))))}async updateCache(e,t){if(await this.waitForQueuedWrites(),this.cache){const s=function parseEntityIds(e){const t=e.extensions?.dataConnect,s=Object.assign(e);if(!t)return s;const r={};for(const e of t){const{path:t}=e;populatePath(t,r,e)}return r}(e),r=function getMaxAgeFromExtensions(e){if(!e)return;for(const t of e)if("maxAge"in t&&void 0!==t.maxAge&&null!==t.maxAge&&t.maxAge.endsWith("s"))return Number(t.maxAge.substring(0,t.maxAge.length-1))}(t);return void 0!==r&&(this.cache.cacheSettings.maxAgeSeconds=r),this.cache.update(w({name:e.ref.name,variables:e.ref.variables,refType:_}),e.data,s)}{const t=w({name:e.ref.name,variables:e.ref.variables,refType:_});return this.subscriptionCache.set(t,e),[t]}}addSubscription(e,t,s,r,n){const i=w({name:e.name,variables:e.variables,refType:_}),unsubscribe=()=>{if(this.callbacks.has(i)){const e=this.callbacks.get(i);this.callbacks.set(i,e.filter((e=>e!==a))),s?.()}},a={userCallback:t,errCallback:r,unsubscribe:unsubscribe};n&&this.updateSSR(n);return this.preferCacheResults(e,!0).then(void 0,(e=>{})),this.callbacks.has(i)||this.callbacks.set(i,[]),this.callbacks.get(i).push(a),unsubscribe}async fetchServerResults(e){await this.waitForQueuedWrites();const t=w({name:e.name,variables:e.variables,refType:_});try{const r=await this.transport.invokeQuery(e.name,e.variables),n=Date.now().toString(),i=r.extensions,a={...r,ref:e,source:v,fetchTime:n,data:r.data,extensions:(s=i,{dataConnect:s.dataConnect?.filter((e=>"entityId"in e||"entityIds"in e))}),toJSON:getRefSerializer(e,r.data,v,n)};let o=[];return o=await this.updateCache(a,i?.dataConnect),this.publishDataToSubscribers(t,a),this.cache?await this.publishCacheResultsToSubscribers(o,n):this.subscriptionCache.set(t,a),a}catch(e){throw this.publishErrorToSubscribers(t,e),e}var s}async fetchCacheResults(e,t=!1){let s;if(await this.waitForQueuedWrites(),s=this.cache?await this.getFromResultTreeCache(e,t):await this.getFromSubscriberCache(e),!s)throw new DataConnectError(y.OTHER,"No cache entry found for query: "+e.name);const r=Date.now().toString(),n={...s,ref:e,source:T,fetchTime:r,data:s.data,extensions:s.extensions,toJSON:getRefSerializer(e,s.data,T,r)};if(this.cache){const t=w({name:e.name,variables:e.variables,refType:_});await this.publishCacheResultsToSubscribers([t],r)}else{const t=w({name:e.name,variables:e.variables,refType:_});this.subscriptionCache.set(t,n),this.publishDataToSubscribers(t,n)}return n}publishErrorToSubscribers(e,t){this.callbacks.get(e)?.forEach((e=>{e.errCallback&&e.errCallback(t)}))}async getFromResultTreeCache(e,t=!1){const s=w({name:e.name,variables:e.variables,refType:_});if(!this.cache||!await this.cache.containsResultTree(s))return null;const r=await this.cache.getResultJSON(s),n=await this.cache.getResultTree(s);if(!t&&n.isStale())return null;const i={source:T,ref:e,data:r,toJSON:getRefSerializer(e,r,T,n.cachedAt.toString()),fetchTime:n.cachedAt.toString()};return(await this.cache.getResultTree(s)).updateAccessed(),i}async getFromSubscriberCache(e){const t=w({name:e.name,variables:e.variables,refType:_});if(!this.subscriptionCache.has(t))return;const s=this.subscriptionCache.get(t);return s.source=T,s.toJSON=getRefSerializer(s.ref,s.data,T,s.fetchTime),s}publishDataToSubscribers(e,t){if(!this.callbacks.has(e))return;this.callbacks.get(e).forEach((e=>{e.userCallback(t)}))}async publishCacheResultsToSubscribers(e,t){if(this.cache)for(const s of e){if(!this.callbacks.get(s))continue;const e=(await this.cache.getResultTree(s)).getRootStub().toJSON(C.hydrated),{name:r,variables:n}=R(s),i={dataConnect:this.dc,refType:_,name:r,variables:n};this.publishDataToSubscribers(s,{data:e,fetchTime:t,ref:i,source:T,toJSON:getRefSerializer(i,e,T,t)})}}enableEmulator(e,t){this.transport.useEmulator(e,t)}}const S={Base:"Base",Generated:"Generated",TanstackReactCore:"TanstackReactCore",GeneratedReact:"GeneratedReact",TanstackAngularCore:"TanstackAngularCore",GeneratedAngular:"GeneratedAngular"},k="firebasedataconnect.googleapis.com";function addToken(e,t){if(!t)return e;const s=new URL(e);return s.searchParams.append("key",t),s.toString()}let D=globalThis.fetch;function getGoogApiClientValue(e,t){let s="gl-js/ fire/"+g;return t!==S.Base&&t!==S.Generated?s+=" js/"+t.toLowerCase():(e||t===S.Generated)&&(s+=" js/gen"),s}async function dcFetch(e,t,{signal:s},r,n,i,a,o,c){if(!D)throw new DataConnectError(y.OTHER,"No Fetch Implementation detected!");const h={"Content-Type":"application/json","X-Goog-Api-Client":getGoogApiClientValue(a,o)};n&&(h["X-Firebase-Auth-Token"]=n),r&&(h["x-firebase-gmpid"]=r),i&&(h["X-Firebase-AppCheck"]=i);const l={body:JSON.stringify(t),method:"POST",headers:h,signal:s};let u,d;isCloudWorkstation(e)&&c&&(l.credentials="include");try{u=await D(e,l)}catch(e){throw new DataConnectError(y.OTHER,"Failed to fetch: "+JSON.stringify(e))}try{d=await u.json()}catch(e){throw new DataConnectError(y.OTHER,JSON.stringify(e))}const p=function getErrorMessage(e){if("message"in e&&e.message)return e.message;return JSON.stringify(e)}(d);if(u.status>=400){if(logError("Error while performing request: "+JSON.stringify(d)),401===u.status)throw new DataConnectError(y.UNAUTHORIZED,p);throw new DataConnectError(y.OTHER,p)}if(d.errors&&d.errors.length){const e=JSON.stringify(d.errors),t={errors:d.errors,data:d.data};throw new DataConnectOperationError("DataConnect error while performing request: "+e,t)}return d.extensions||(d.extensions={dataConnect:[]}),d}class RESTTransport{constructor(e,t,s,r,n,i,a=!1,o=S.Base){this.apiKey=t,this.appId=s,this.authProvider=r,this.appCheckProvider=n,this._isUsingGen=a,this._callerSdkType=o,this._host="",this._location="l",this._connectorName="",this._secure=!0,this._project="p",this._accessToken=null,this._appCheckToken=null,this._lastToken=null,this._isUsingEmulator=!1,this.invokeQuery=(e,t)=>{const s=new AbortController;return this.withRetry((()=>dcFetch(addToken(`${this.endpointUrl}:executeQuery`,this.apiKey),{name:`projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`,operationName:e,variables:t},s,this.appId,this._accessToken,this._appCheckToken,this._isUsingGen,this._callerSdkType,this._isUsingEmulator)))},this.invokeMutation=(e,t)=>{const s=new AbortController;return this.withRetry((()=>dcFetch(addToken(`${this.endpointUrl}:executeMutation`,this.apiKey),{name:`projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`,operationName:e,variables:t},s,this.appId,this._accessToken,this._appCheckToken,this._isUsingGen,this._callerSdkType,this._isUsingEmulator)))},i&&("number"==typeof i.port&&(this._port=i.port),void 0!==i.sslEnabled&&(this._secure=i.sslEnabled),this._host=i.host);const{location:c,projectId:h,connector:l,service:u}=e;if(c&&(this._location=c),h&&(this._project=h),this._serviceName=u,!l)throw new DataConnectError(y.INVALID_ARGUMENT,"Connector Name required!");this._connectorName=l,this.authProvider?.addTokenChangeListener((e=>{logDebug(`New Token Available: ${e}`),this._accessToken=e})),this.appCheckProvider?.addTokenChangeListener((e=>{const{token:t}=e;logDebug(`New App Check Token Available: ${t}`),this._appCheckToken=t}))}get endpointUrl(){return function urlBuilder(e,t){const{connector:s,location:r,projectId:n,service:i}=e,{host:a,sslEnabled:o,port:c}=t;let h=`${o?"https":"http"}://${a||k}`;if("number"==typeof c)h+=`:${c}`;else if(void 0!==c)throw logError("Port type is of an invalid type"),new DataConnectError(y.INVALID_ARGUMENT,"Incorrect type for port passed in!");return`${h}/v1/projects/${n}/locations/${r}/services/${i}/connectors/${s}`}({connector:this._connectorName,location:this._location,projectId:this._project,service:this._serviceName},{host:this._host,sslEnabled:this._secure,port:this._port})}useEmulator(e,t,s){this._host=e,this._isUsingEmulator=!0,"number"==typeof t&&(this._port=t),void 0!==s&&(this._secure=s)}onTokenChanged(e){this._accessToken=e}async getWithAuth(e=!1){let t=new Promise((e=>e(this._accessToken)));if(this.appCheckProvider){const e=await this.appCheckProvider.getToken();e&&(this._appCheckToken=e.token)}return t=this.authProvider?this.authProvider.getToken(e).then((e=>e?(this._accessToken=e.accessToken,this._accessToken):null)):new Promise((e=>e(""))),t}_setLastToken(e){this._lastToken=e}withRetry(e,t=!1){let s=!1;return this.getWithAuth(t).then((e=>(s=this._lastToken!==e,this._lastToken=e,e))).then(e).catch((r=>{if("code"in r&&r.code===y.UNAUTHORIZED&&!t&&s)return logDebug("Retrying due to unauthorized"),this.withRetry(e,!0);throw r}))}_setCallerSdkType(e){this._callerSdkType=e}}function mutationRef(e,t,s){e.setInitialized();return{dataConnect:e,name:t,refType:E,variables:s}}class MutationManager{constructor(e){this._transport=e,this._inflight=[]}executeMutation(e){const t=this._transport.invokeMutation(e.name,e.variables),s=t.then((t=>({...t,source:v,ref:e,fetchTime:Date.now().toLocaleString()})));this._inflight.push(t);const removePromise=()=>this._inflight=this._inflight.filter((e=>e!==t));return t.then(removePromise,removePromise),s}}function executeMutation(e){return e.dataConnect._mutationManager.executeMutation(e)}function parseOptions(e){const[t,s]=e.split("://"),r="https"===t,[n,i]=s.split(":");return{host:n,port:Number(i),sslEnabled:r}}class DataConnect{constructor(e,t,s,r){if(this.app=e,this.dataConnectOptions=t,this._authProvider=s,this._appCheckProvider=r,this.isEmulator=!1,this._initialized=!1,this._isUsingGeneratedSdk=!1,this._callerSdkType=S.Base,"undefined"!=typeof process&&process.env){const e=process.env.FIREBASE_DATA_CONNECT_EMULATOR_HOST;e&&(logDebug("Found custom host. Using emulator"),this.isEmulator=!0,this._transportOptions=parseOptions(e))}}getCache(){return this.cache}_useGeneratedSdk(){this._isUsingGeneratedSdk||(this._isUsingGeneratedSdk=!0)}_setCallerSdkType(e){this._callerSdkType=e,this._initialized&&this._transport._setCallerSdkType(e)}_delete(){return e(this.app,"data-connect",JSON.stringify(this.getSettings())),Promise.resolve()}getSettings(){const e=JSON.parse(JSON.stringify(this.dataConnectOptions));return delete e.projectId,e}setCacheSettings(e){this._cacheSettings=e}setInitialized(){if(this._initialized)return;void 0===this._transportClass&&(logDebug("transportClass not provided. Defaulting to RESTTransport."),this._transportClass=RESTTransport),this._authTokenProvider=new FirebaseAuthProvider(this.app.name,this.app.options,this._authProvider);const e={connector:this.dataConnectOptions.connector,service:this.dataConnectOptions.service,location:this.dataConnectOptions.location};this._cacheSettings&&(this.cache=new DataConnectCache(this._authTokenProvider,this.app.options.projectId,e,this._transportOptions?.host||k,this._cacheSettings)),this._appCheckProvider&&(this._appCheckTokenProvider=new AppCheckTokenProvider(this.app,this._appCheckProvider)),this._transport=new this._transportClass(this.dataConnectOptions,this.app.options.apiKey,this.app.options.appId,this._authTokenProvider,this._appCheckTokenProvider,void 0,this._isUsingGeneratedSdk,this._callerSdkType),this._transportOptions&&this._transport.useEmulator(this._transportOptions.host,this._transportOptions.port,this._transportOptions.sslEnabled),this._queryManager=new QueryManager(this._transport,this,this.cache),this._mutationManager=new MutationManager(this._transport),this._initialized=!0}enableEmulator(e){if(this._transportOptions&&this._initialized&&!areTransportOptionsEqual(this._transportOptions,e))throw logError("enableEmulator called after initialization"),new DataConnectError(y.ALREADY_INITIALIZED,"DataConnect instance already initialized!");this._transportOptions=e,this.isEmulator=!0}}function areTransportOptionsEqual(e,t){return e.host===t.host&&e.port===t.port&&e.sslEnabled===t.sslEnabled}function connectDataConnectEmulator(e,t,s,r=!1){isCloudWorkstation(t)&&(!async function pingServer(e){return(await fetch(e,{credentials:"include"})).ok}(`https://${t}${s?`:${s}`:""}`),updateEmulatorBanner("Data Connect",!0)),e.enableEmulator({host:t,port:s,sslEnabled:r})}function getDataConnect(e,s,r){let n,i,a;"location"in e?(i=e,n=t(),a=s):(n=e,i=s,a=r),n&&0!==Object.keys(n).length||(n=t());const o={...i,projectId:n.options.projectId},c=Object.fromEntries(Object.entries(o).sort()),h=_getProvider(n,"data-connect"),l=JSON.stringify(c);if(h.isInitialized(l)){const e=h.getImmediate({identifier:l}),t=h.getOptions(l);if(Object.keys(t).length>0)return logDebug("Re-using cached instance"),e}validateDCOptions(i),logDebug("Creating new DataConnect instance");const u=h.initialize({instanceIdentifier:l,options:Object.fromEntries(Object.entries({...c}).sort())});return a?.cacheSettings&&u.setCacheSettings(a.cacheSettings),u}function validateDCOptions(e){if(!e)throw new DataConnectError(y.INVALID_ARGUMENT,"DC Option Required");return["connector","location","service"].forEach((t=>{if(null===e[t]||void 0===e[t])throw new DataConnectError(y.INVALID_ARGUMENT,`${t} Required`)})),!0}function terminate(e){return e._delete()}const A={MEMORY:"MEMORY"};function makeMemoryCacheProvider(){return new MemoryStub}const I={PREFER_CACHE:"PREFER_CACHE",CACHE_ONLY:"CACHE_ONLY",SERVER_ONLY:"SERVER_ONLY"};function executeQuery(e,t){if(e.refType!==_)return Promise.reject(new DataConnectError(y.INVALID_ARGUMENT,"ExecuteQuery can only execute query operations"));const s=e.dataConnect._queryManager,r=t?.fetchPolicy??I.PREFER_CACHE;switch(r){case I.SERVER_ONLY:return s.fetchServerResults(e);case I.CACHE_ONLY:return s.fetchCacheResults(e,!0);case I.PREFER_CACHE:return s.preferCacheResults(e,!1);default:throw new DataConnectError(y.INVALID_ARGUMENT,`Invalid fetch policy: ${r}`)}}function queryRef(e,t,s,r){return e.setInitialized(),void 0!==r&&e._queryManager.updateSSR(r),{dataConnect:e,refType:_,name:t,variables:s}}function toQueryRef(e){const{refInfo:{name:t,variables:s,connectorConfig:r}}=e;return queryRef(getDataConnect(r),t,s)}function validateArgs(e,t,s,r){let n,i;if(t&&"enableEmulator"in t?(n=t,i=s):(n=getDataConnect(e),i=t),!n||!i&&r)throw new DataConnectError(y.INVALID_ARGUMENT,"Variables required.");return{dc:n,vars:i}}function subscribe(e,t,s,r){let n,i,a;if("refInfo"in e){const t=e,{data:s,source:r,fetchTime:a}=t;n=toQueryRef(t),i={data:s,source:r,fetchTime:a,ref:n,toJSON:getRefSerializer(n,s,r,a)}}else n=e;if("function"==typeof t?a=t:(a=t.onNext,s=t.onErr,r=t.onComplete),!a)throw new DataConnectError(y.INVALID_ARGUMENT,"Must provide onNext");return n.dataConnect._queryManager.addSubscription(n,a,r,s,i)}!function registerDataConnect(e){!function setSDKVersion(e){g=e}(i),s(new Component("data-connect",((e,{instanceIdentifier:t,options:s})=>{const r=e.getProvider("app").getImmediate(),n=e.getProvider("auth-internal"),i=e.getProvider("app-check-internal");let a=s;if(t&&(a={...JSON.parse(t),...a}),!r.options.projectId)throw new DataConnectError(y.INVALID_ARGUMENT,"Project ID must be provided. Did you pass in a proper projectId to initializeApp?");return new DataConnect(r,{...a,projectId:r.options.projectId},n,i)}),"PUBLIC").setMultipleInstances(!0)),r(p,f,e),r(p,f,"esm2020")}();export{S as CallerSdkTypeEnum,y as Code,DataConnect,DataConnectError,DataConnectOperationError,E as MUTATION_STR,MutationManager,_ as QUERY_STR,I as QueryFetchPolicy,T as SOURCE_CACHE,v as SOURCE_SERVER,A as StorageType,areTransportOptionsEqual,connectDataConnectEmulator,executeMutation,executeQuery,getDataConnect,makeMemoryCacheProvider,mutationRef,parseOptions,queryRef,setLogLevel,subscribe,terminate,toQueryRef,validateArgs,validateDCOptions};
//# sourceMappingURL=firebase-data-connect.js.map