firebase 12.12.0 → 12.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/dist/esm/index.esm.js +1 -1
- package/app/dist/index.cjs.js +1 -1
- package/app/dist/index.mjs +1 -1
- package/compat/app/dist/esm/index.esm.js +1 -1
- package/compat/app/dist/index.cjs.js +1 -1
- package/compat/app/dist/index.mjs +1 -1
- package/compat/dist/esm/index.esm.js +2 -2
- package/compat/dist/index.node.cjs +2 -2
- package/compat/dist/index.rn.cjs.js +2 -2
- package/firebase-ai.js +1 -1
- package/firebase-ai.js.map +1 -1
- package/firebase-analytics.js +1 -1
- package/firebase-app-check.js +1 -1
- package/firebase-app-compat.js +2 -2
- package/firebase-app.js +4 -4
- package/firebase-auth-cordova.js +1 -1
- package/firebase-auth-web-extension.js +1 -1
- package/firebase-auth.js +1 -1
- package/firebase-compat.js +4 -4
- package/firebase-data-connect.js +1 -1
- package/firebase-database.js +1 -1
- package/firebase-firestore-compat.js +1 -1
- package/firebase-firestore-lite-pipelines.js +1 -1
- package/firebase-firestore-lite.js +1 -1
- package/firebase-firestore-pipelines.js +1 -1
- package/firebase-firestore.js +1 -1
- package/firebase-functions.js +1 -1
- package/firebase-installations.js +1 -1
- package/firebase-messaging-sw.js +1 -1
- package/firebase-messaging.js +1 -1
- package/firebase-performance-standalone-compat.js +3 -3
- package/firebase-performance.js +1 -1
- package/firebase-remote-config.js +1 -1
- package/firebase-storage.js +1 -1
- package/package.json +2 -2
package/firebase-data-connect.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
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.12.0/firebase-app.js";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(o,((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 o=/\{\$([^}]+)}/g;function isCloudWorkstation(e){try{return(e.startsWith("http://")||e.startsWith("https://")?new URL(e).hostname:e).endsWith(".cloudworkstations.dev")}catch{return!1}}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 a;!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"}(a||(a={}));const c={debug:a.DEBUG,verbose:a.VERBOSE,info:a.INFO,warn:a.WARN,error:a.ERROR,silent:a.SILENT},h=a.INFO,u={[a.DEBUG]:"log",[a.VERBOSE]:"log",[a.INFO]:"info",[a.WARN]:"warn",[a.ERROR]:"error"},defaultLogHandler=(e,t,...s)=>{if(t<e.logLevel)return;const r=(new Date).toISOString(),n=u[t];if(!n)throw new Error(`Attempted to log a message with an invalid logType (value: ${t})`);console[n](`[${r}] ${e.name}:`,...s)};const l="@firebase/data-connect",d="0.6.0";let p="";const g={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(g.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 b="_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(g.INVALID_ARGUMENT,"EntityNode initialized with non-object value");if(null!==t){"object"==typeof t&&s&&s[b]&&"string"==typeof s[b]&&(this.globalId=s[b],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 o=s&&s[i],a=[],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,o&&o[s],r,n),a.push(t)}else c.push(h);if(c.length>0&&a.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 a.length>0?this.objectLists[i]=a:this.scalars[i]=[]}else{if(null===t[i]){this.scalars[i]=null;continue}const o=new EntityNode;await o.loadData(e,t[i],s&&s[i],r,n),this.references[i]=o}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===f.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[b]=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 f;!function(e){e[e.hydrated=0]="hydrated",e[e.dehydrated=1]="dehydrated"}(f||(f={}));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(f.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(g.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:o}=await r.dehydrateResults(t,s,n,e),a=new Date;return await n.setResultTree(e,new ResultTree(i,t.maxAge||this.cacheSettings.maxAgeSeconds,a,a)),o}}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 m=new class Logger{constructor(e){this.name=e,this._logLevel=h,this._logHandler=defaultLogHandler,this._userLogHandler=null}get logLevel(){return this._logLevel}set logLevel(e){if(!(e in a))throw new TypeError(`Invalid value "${e}" assigned to \`logLevel\``);this._logLevel=e}setLogLevel(e){this._logLevel="string"==typeof e?c[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,a.DEBUG,...e),this._logHandler(this,a.DEBUG,...e)}log(...e){this._userLogHandler&&this._userLogHandler(this,a.VERBOSE,...e),this._logHandler(this,a.VERBOSE,...e)}info(...e){this._userLogHandler&&this._userLogHandler(this,a.INFO,...e),this._logHandler(this,a.INFO,...e)}warn(...e){this._userLogHandler&&this._userLogHandler(this,a.WARN,...e),this._logHandler(this,a.WARN,...e)}error(...e){this._userLogHandler&&this._userLogHandler(this,a.ERROR,...e),this._logHandler(this,a.ERROR,...e)}}("@firebase/data-connect");function setLogLevel(e){m.setLogLevel(e)}function logDebug(e){m.debug(`DataConnect (${p}): ${e}`)}function logError(e){m.error(`DataConnect (${p}): ${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 y="query",E="mutation",T="SERVER",C="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 v,S;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){v=e}((e=>JSON.stringify(sortKeysForObj(e)))),function setDecoder(e){S=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(v({name:e.ref.name,variables:e.ref.variables,refType:y}),e.data,s)}{const t=v({name:e.ref.name,variables:e.ref.variables,refType:y});return this.subscriptionCache.set(t,e),[t]}}addSubscription(e,t,s,r,n){const i=v({name:e.name,variables:e.variables,refType:y}),unsubscribe=()=>{if(this.callbacks.has(i)){const t=this.callbacks.get(i).filter((e=>e!==o));this.callbacks.set(i,t),0===t.length&&(this.callbacks.delete(i),this.transport.invokeUnsubscribe(e.name,e.variables)),s?.()}},o={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.get(i).push(o):(this.callbacks.set(i,[o]),this.transport.invokeSubscribe(this.makeSubscribeObserver(e),e.name,e.variables)),unsubscribe}async fetchServerResults(e){await this.waitForQueuedWrites();const t=v({name:e.name,variables:e.variables,refType:y});try{const s=await this.transport.invokeQuery(e.name,e.variables),r=Date.now().toString(),n=s.extensions,i={...s,ref:e,source:T,fetchTime:r,data:s.data,extensions:getDataConnectExtensionsWithoutMaxAge(n),toJSON:getRefSerializer(e,s.data,T,r)},o=await this.updateCache(i,n?.dataConnect);return this.publishDataToSubscribers(t,i),this.cache?await this.publishCacheResultsToSubscribers(o,r):this.subscriptionCache.set(t,i),i}catch(e){throw this.publishErrorToSubscribers(t,e),e}}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(g.OTHER,"No cache entry found for query: "+e.name);const r=Date.now().toString(),n={...s,ref:e,source:C,fetchTime:r,data:s.data,extensions:s.extensions,toJSON:getRefSerializer(e,s.data,C,r)};if(this.cache){const t=v({name:e.name,variables:e.variables,refType:y});await this.publishCacheResultsToSubscribers([t],r)}else{const t=v({name:e.name,variables:e.variables,refType:y});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=v({name:e.name,variables:e.variables,refType:y});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:C,ref:e,data:r,toJSON:getRefSerializer(e,r,C,n.cachedAt.toString()),fetchTime:n.cachedAt.toString()};return(await this.cache.getResultTree(s)).updateAccessed(),i}async getFromSubscriberCache(e){const t=v({name:e.name,variables:e.variables,refType:y});if(!this.subscriptionCache.has(t))return;const s=this.subscriptionCache.get(t);return s.source=C,s.toJSON=getRefSerializer(s.ref,s.data,C,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(f.hydrated),{name:r,variables:n}=S(s),i={dataConnect:this.dc,refType:y,name:r,variables:n};this.publishDataToSubscribers(s,{data:e,fetchTime:t,ref:i,source:C,toJSON:getRefSerializer(i,e,C,t)})}}enableEmulator(e,t){this.transport.useEmulator(e,t)}makeSubscribeObserver(e){const t=v({name:e.name,variables:e.variables,refType:y});return{onData:async s=>{await this.handleStreamNotification(t,s,e)},onDisconnect:(e,s)=>{this.handleStreamDisconnect(t,e,s)},onError:e=>{this.publishErrorToSubscribers(t,e)}}}async handleStreamNotification(e,t,s){if(t.errors&&t.errors.length>0){const s=JSON.stringify(t.errors.map((e=>e&&"object"==typeof e?{message:e.message,code:e.code}:e))),r={errors:t.errors,data:t.data},n=new DataConnectOperationError("DataConnect error received from subscribe notification: "+s,r);return void this.publishErrorToSubscribers(e,n)}const r=Date.now().toString(),n={ref:s,source:T,fetchTime:r,data:t.data,extensions:getDataConnectExtensionsWithoutMaxAge(t.extensions),toJSON:getRefSerializer(s,t.data,T,r)},i=await this.updateCache(n,t.extensions?.dataConnect);this.publishDataToSubscribers(e,n),this.cache&&await this.publishCacheResultsToSubscribers(i,r)}handleStreamDisconnect(e,t,s){const r=new DataConnectError(t,s);this.publishErrorToSubscribers(e,r);const n=this.callbacks.get(e);n&&[...n].forEach((e=>e.unsubscribe()))}}function getDataConnectExtensionsWithoutMaxAge(e){return{dataConnect:e.dataConnect?.filter((e=>"entityId"in e||"entityIds"in e))}}const R={Base:"Base",Generated:"Generated",TanstackReactCore:"TanstackReactCore",GeneratedReact:"GeneratedReact",TanstackAngularCore:"TanstackAngularCore",GeneratedAngular:"GeneratedAngular"};function getGoogApiClientValue$1(e,t){let s="gl-js/ fire/"+p;return t!==R.Base&&t!==R.Generated?s+=" js/"+t.toLowerCase():(e||t===R.Generated)&&(s+=" js/gen"),s}class AbstractDataConnectTransport{constructor(e,t,s,r,n,i,o=!1,a=R.Base){this.apiKey=t,this.appId=s,this.authProvider=r,this.appCheckProvider=n,this._isUsingGen=o,this._callerSdkType=a,this._host="",this._location="l",this._connectorName="",this._secure=!0,this._project="p",this._authToken=null,this._appCheckToken=null,this._lastToken=null,this._isUsingEmulator=!1,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:u,service:l}=e;if(c&&(this._location=c),h&&(this._project=h),this._serviceName=l,!u)throw new DataConnectError(g.INVALID_ARGUMENT,"Connector Name required!");this._connectorName=u,this._connectorResourcePath=`projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`,this.authProvider?.addTokenChangeListener((e=>{logDebug(`New Token Available: ${e}`),this.onAuthTokenChanged(e)})),this.appCheckProvider?.addTokenChangeListener((e=>{const{token:t}=e;logDebug(`New App Check Token Available: ${t}`),this._appCheckToken=t}))}useEmulator(e,t,s){this._host=e,this._isUsingEmulator=!0,"number"==typeof t&&(this._port=t),void 0!==s&&(this._secure=s)}async getWithAuth(e=!1){let t=new Promise((e=>e(this._authToken)));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._authToken=e.accessToken,this._authToken):null)):new Promise((e=>e(""))),t}async 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===g.UNAUTHORIZED&&!t&&s)return logDebug("Retrying due to unauthorized"),this.withRetry(e,!0);throw r}))}_setLastToken(e){this._lastToken=e}_setCallerSdkType(e){this._callerSdkType=e}}let k=globalThis.fetch;function getGoogApiClientValue(e,t){let s="gl-js/ fire/"+p;return t!==R.Base&&t!==R.Generated?s+=" js/"+t.toLowerCase():(e||t===R.Generated)&&(s+=" js/gen"),s}async function dcFetch(e,t,{signal:s},r,n,i,o,a,c){if(!k)throw new DataConnectError(g.OTHER,"No Fetch Implementation detected!");const h={"Content-Type":"application/json","X-Goog-Api-Client":getGoogApiClientValue(o,a)};n&&(h["X-Firebase-Auth-Token"]=n),r&&(h["x-firebase-gmpid"]=r),i&&(h["X-Firebase-AppCheck"]=i);const u={body:JSON.stringify(t),method:"POST",headers:h,signal:s};let l,d;isCloudWorkstation(e)&&c&&(u.credentials="include");try{l=await k(e,u)}catch(e){const t=e&&"object"==typeof e&&"message"in e?e.message:String(e);throw new DataConnectError(g.OTHER,"Failed to fetch: "+t)}try{d=await l.json()}catch(e){const t=e&&"object"==typeof e&&"message"in e?e.message:String(e);throw new DataConnectError(g.OTHER,"Failed to parse JSON response: "+t)}const p=function getErrorMessage(e){if("message"in e&&e.message)return e.message;return JSON.stringify(e)}(d);if(l.status>=400){if(logError("Error while performing request: "+JSON.stringify(d)),401===l.status)throw new DataConnectError(g.UNAUTHORIZED,p);throw new DataConnectError(g.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}const _="firebasedataconnect.googleapis.com";function addToken(e,t){if(!t)return e;const s=new URL(e);return s.searchParams.append("key",t),s.toString()}class RESTTransport extends AbstractDataConnectTransport{constructor(e,t,s,r,n,i,o=!1,a=R.Base){super(e,t,s,r,n,i,o,a),this.invokeQuery=(e,t)=>{const s=new AbortController;return this.withRetry((()=>dcFetch(addToken(`${this.endpointUrl}:executeQuery`,this.apiKey),{name:this._connectorResourcePath,operationName:e,variables:t},s,this.appId,this._authToken,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:this._connectorResourcePath,operationName:e,variables:t},s,this.appId,this._authToken,this._appCheckToken,this._isUsingGen,this._callerSdkType,this._isUsingEmulator)))}}get endpointUrl(){return function restUrlBuilder(e,t){const{connector:s,location:r,projectId:n,service:i}=e,{host:o,sslEnabled:a,port:c}=t;let h=`${a?"https":"http"}://${o||_}`;if("number"==typeof c)h+=`:${c}`;else if(void 0!==c)throw logError("Port type is of an invalid type"),new DataConnectError(g.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})}invokeSubscribe(e,t,s){throw new DataConnectError(g.NOT_SUPPORTED,"Subscriptions are not supported using REST!")}invokeUnsubscribe(e,t){throw new DataConnectError(g.NOT_SUPPORTED,"Unsubscriptions are not supported using REST!")}onAuthTokenChanged(e){this._authToken=e}}class AbstractDataConnectStreamTransport extends AbstractDataConnectTransport{constructor(){super(...arguments),this.pendingClose=!1,this.isUnableToConnect=!1,this.requestNumber=1,this.activeQueryExecuteRequests=new Map,this.activeMutationExecuteRequests=new Map,this.activeSubscribeRequests=new Map,this.executeRequestPromises=new Map,this.subscribeObservers=new Map,this.closeTimeout=null,this.closeTimeoutFinished=!1,this.hasWaitedForInitialAuth=!1,this.isFirstStreamMessage=!0,this.lastSentAuthToken=null}get isPendingClose(){return this.pendingClose}get hasActiveSubscriptions(){return this.activeSubscribeRequests.size>0}get hasActiveExecuteRequests(){return this.activeQueryExecuteRequests.size>0||this.activeMutationExecuteRequests.size>0}nextRequestId(){return(this.requestNumber++).toString()}trackQueryExecuteRequest(e,t,s){let r,n;const i={responsePromise:new Promise(((e,t)=>{r=e,n=t})),resolveFn:r,rejectFn:n};return this.activeQueryExecuteRequests.set(t,s),this.executeRequestPromises.set(e,i),i}trackMutationExecuteRequest(e,t,s){let r,n;const i={responsePromise:new Promise(((e,t)=>{r=e,n=t})),resolveFn:r,rejectFn:n},o=this.activeMutationExecuteRequests.get(t)||[];return o.push(s),this.activeMutationExecuteRequests.set(t,o),this.executeRequestPromises.set(e,i),i}trackSubscribeRequest(e,t,s,r){this.activeSubscribeRequests.set(t,s),this.subscribeObservers.set(e,r)}cleanupQueryExecuteRequest(e,t){this.activeQueryExecuteRequests.delete(t),this.executeRequestPromises.delete(e)}cleanupMutationExecuteRequest(e,t){const s=this.activeMutationExecuteRequests.get(t);if(s){const r=s.filter((t=>t.requestId!==e));r.length>0?this.activeMutationExecuteRequests.set(t,r):this.activeMutationExecuteRequests.delete(t)}this.executeRequestPromises.delete(e)}cleanupSubscribeRequest(e,t){this.activeSubscribeRequests.delete(t),this.subscribeObservers.delete(e)}get shouldIncludeAuth(){return this.isFirstStreamMessage||!!this._authToken&&this._authToken!==this.lastSentAuthToken}onConnectionReady(){this.isFirstStreamMessage=!0,this.lastSentAuthToken=null,this.hasWaitedForInitialAuth=!1}async attemptClose(){this.hasActiveSubscriptions||this.hasActiveExecuteRequests||(this.cancelClose(),await this.closeConnection(),this.onGracefulStreamClose?.())}prepareToCloseGracefully(){this.pendingClose||(this.pendingClose=!0,this.closeTimeoutFinished=!1,this.closeTimeout=setTimeout((()=>{this.closeTimeoutFinished=!0,this.attemptClose()}),6e4))}cancelClose(){this.closeTimeout&&clearTimeout(this.closeTimeout),this.pendingClose=!1,this.closeTimeoutFinished=!1}rejectAllActiveRequests(e,t){this.activeQueryExecuteRequests.clear(),this.activeMutationExecuteRequests.clear(),this.activeSubscribeRequests.clear();const s=new DataConnectError(e,t);for(const[e,{rejectFn:t}]of this.executeRequestPromises)this.executeRequestPromises.delete(e),t(s);for(const[s,r]of this.subscribeObservers)this.subscribeObservers.delete(s),r.onDisconnect(e,t)}onStreamClose(e,t){this.rejectAllActiveRequests(g.OTHER,`Stream disconnected with code ${e}: ${t}`)}prepareMessage(e){const t={...e},s={};return this.appId&&(s["x-firebase-gmpid"]=this.appId),s["X-Goog-Api-Client"]=getGoogApiClientValue$1(this._isUsingGen,this._callerSdkType),this.shouldIncludeAuth&&this._authToken&&(s["X-Firebase-Auth-Token"]=this._authToken,this.lastSentAuthToken=this._authToken),this.isFirstStreamMessage&&(this._appCheckToken&&(s["X-Firebase-App-Check"]=this._appCheckToken),t.name=this._connectorResourcePath),t.headers=s,this.isFirstStreamMessage=!1,t}async sendRequestMessage(e){if(!this.hasWaitedForInitialAuth&&this.authProvider&&(await this.getWithAuth(),this.hasWaitedForInitialAuth=!0),this.streamIsReady){const t=this.prepareMessage(e);return this.sendMessage(t)}return this.ensureConnection().then((()=>{const t=this.prepareMessage(e);return this.sendMessage(t)}))}getMapKey(e,t){const s=this.sortObjectKeys(t);return JSON.stringify({operationName:e,variables:s})}sortObjectKeys(e){if(null===e||"object"!=typeof e||Array.isArray(e))return e;const t={};return Object.keys(e).sort().forEach((s=>{t[s]=this.sortObjectKeys(e[s])})),t}invokeQuery(e,t){const s=this.nextRequestId(),r={operationName:e,variables:t},n=this.getMapKey(e,t),i={requestId:s,execute:r};let{responsePromise:o,rejectFn:a}=this.trackQueryExecuteRequest(s,n,i);return o=o.finally((()=>{this.cleanupQueryExecuteRequest(s,n),this.hasActiveSubscriptions||this.hasActiveExecuteRequests||!this.closeTimeoutFinished||this.attemptClose()})),this.sendRequestMessage(i).catch((e=>{a(e)})),o}invokeMutation(e,t){const s=this.nextRequestId(),r={operationName:e,variables:t},n=this.getMapKey(e,t),i={requestId:s,execute:r};let{responsePromise:o,rejectFn:a}=this.trackMutationExecuteRequest(s,n,i);return o=o.finally((()=>{this.cleanupMutationExecuteRequest(s,n),this.hasActiveSubscriptions||this.hasActiveExecuteRequests||!this.closeTimeoutFinished||this.attemptClose()})),this.sendRequestMessage(i).catch((e=>{a(e)})),o}invokeSubscribe(e,t,s){this.cancelClose();const r=this.nextRequestId(),n={operationName:t,variables:s},i=this.getMapKey(t,s),o={requestId:r,subscribe:n};this.trackSubscribeRequest(r,i,o,e),this.sendRequestMessage(o).catch((t=>{e.onError(t instanceof Error?t:new Error(String(t))),this.cleanupSubscribeRequest(r,i),this.hasActiveSubscriptions||this.prepareToCloseGracefully()}))}invokeUnsubscribe(e,t){const s=this.getMapKey(e,t),r=this.activeSubscribeRequests.get(s);if(!r)return;const n=r.requestId,i={requestId:n,cancel:{}};this.cleanupSubscribeRequest(n,s),this.sendRequestMessage(i).catch((e=>{logError(`Stream Transport failed to send unsubscribe message: ${e}`)})),this.hasActiveSubscriptions||this.prepareToCloseGracefully()}onAuthTokenChanged(e){const t=this._authToken;this._authToken=e;const s=this.authUid,r=this.authProvider?.getAuth()?.getUid();this.authUid=r;void 0===s||(t&&null===e||!s&&r||s&&r!==s)&&(this.rejectAllActiveRequests(g.UNAUTHORIZED,"Stream disconnected due to auth change."),this.attemptClose())}async handleResponse(e,t){if(this.executeRequestPromises.has(e)){const{resolveFn:s,rejectFn:r}=this.executeRequestPromises.get(e);if(t.errors&&t.errors.length){const e={errors:t.errors,data:t.data},s=JSON.stringify(t.errors);r(new DataConnectOperationError("DataConnect error while performing request: "+s,e))}else s(t)}else{if(!this.subscribeObservers.has(e))throw new DataConnectError(g.OTHER,`Stream response contained unrecognized requestId '${e}'`);{const s=this.subscribeObservers.get(e);await s.onData(t)}}}}let w=globalThis.WebSocket;class WebSocketTransport extends AbstractDataConnectStreamTransport{get endpointUrl(){return function websocketUrlBuilder(e,t){const{location:s}=e,{host:r,sslEnabled:n,port:i}=t;let o=`${n?"wss":"ws"}://${r||_}`;if("number"==typeof i)o+=`:${i}`;else if(void 0!==i)throw logError("Port type is of an invalid type"),new DataConnectError(g.INVALID_ARGUMENT,"Incorrect type for port passed in!");return`${o}/ws/google.firebase.dataconnect.v1.ConnectorStreamService/Connect/locations/${s}`}({connector:this._connectorName,location:this._location,projectId:this._project,service:this._serviceName},{host:this._host,sslEnabled:this._secure,port:this._port})}decodeBinaryResponse(e){return this.decoder||(this.decoder=new TextDecoder("utf-8")),this.decoder.decode(e)}get streamIsReady(){return this.connection?.readyState===WebSocket.OPEN}constructor(e,t,s,r,n,i,o=!1,a=R.Base){super(e,t,s,r,n,i,o,a),this.apiKey=t,this.appId=s,this.authProvider=r,this.appCheckProvider=n,this._isUsingGen=o,this._callerSdkType=a,this.decoder=void 0,this.connection=void 0,this.connectionAttempt=null}ensureConnection(){try{return this.streamIsReady?Promise.resolve():(this.connectionAttempt||(this.connectionAttempt=new Promise(((e,t)=>{if(!w)throw new DataConnectError(g.OTHER,"No WebSocket Implementation detected!");const s=new w(this.endpointUrl);this.connection=s,this.connection.binaryType="arraybuffer",s.onopen=()=>{this.isUnableToConnect=!1,this.onConnectionReady(),e()},s.onerror=e=>{this.connectionAttempt=null,this.isUnableToConnect=!0;const s=new DataConnectError(g.OTHER,"Error using WebSocket connection, closing WebSocket");this.handleError(s),t(s)},s.onmessage=e=>this.handleWebSocketMessage(e).catch((async e=>{this.handleError(e)})),s.onclose=e=>this.handleWebsocketDisconnect(e)}))),this.connectionAttempt)}catch(e){throw this.handleError(e),e}}openConnection(){return this.ensureConnection().catch((e=>{throw new DataConnectError(g.OTHER,`Failed to open connection: ${e}`)}))}closeConnection(e,t){if(!this.connection)return this.connectionAttempt=null,Promise.resolve();let s;try{if(t){const s=123,r=new TextEncoder;if(r.encode(t).length<=s)this.connection.close(e,t);else{const n=new Uint8Array(s),{read:i}=r.encodeInto(t,n),o=t.substring(0,i);this.connection.close(e,o)}}else this.connection.close(e)}catch(e){s=e}finally{this.connection=void 0,this.connectionAttempt=null}return s?Promise.reject(s):Promise.resolve()}handleWebsocketDisconnect(e){this.connection=void 0,this.connectionAttempt=null,this.onStreamClose(e.code,e.reason)}handleError(e){logError(`DataConnect WebSocket error, closing stream: ${e}`);let t=e?String(e):"Unknown Error";e instanceof DataConnectError&&(t=e.message),this.closeConnection(1e3,t)}sendMessage(e){return this.ensureConnection().then((()=>{try{return this.connection.send(JSON.stringify(e)),Promise.resolve()}catch(e){throw this.handleError(e),new DataConnectError(g.OTHER,`Failed to send message: ${String(e)}`)}}))}async handleWebSocketMessage(e){const t=this.parseWebSocketData(e.data),s=t.requestId,r={data:t.data,errors:t.errors,extensions:t.extensions||{dataConnect:[]}};await this.handleResponse(s,r)}parseWebSocketData(e){const t="string"==typeof e;let s,r;try{s=t?JSON.parse(e):JSON.parse(this.decodeBinaryResponse(e))}catch(e){throw new DataConnectError(g.OTHER,`Could not parse WebSocket message: ${e instanceof Error?e.message:String(e)}`)}if("object"!=typeof s||null===s)throw new DataConnectError(g.OTHER,"WebSocket message is not an object");if(t){if(!("result"in s))throw new DataConnectError(g.OTHER,"WebSocket message from emulator did not include result");if("object"!=typeof s.result||null===s.result)throw new DataConnectError(g.OTHER,"WebSocket message result is not an object");r=s.result}else r=s;if(!("requestId"in r))throw new DataConnectError(g.OTHER,"WebSocket message did not include requestId");return r}}class DataConnectTransportManager{constructor(e,t,s,r,n,i,o=!1,a){this.options=e,this.apiKey=t,this.appId=s,this.authProvider=r,this.appCheckProvider=n,this.transportOptions=i,this._isUsingGen=o,this._callerSdkType=a,this.isUsingEmulator=!1,this.restTransport=new RESTTransport(e,t,s,r,n,i,o,a)}initStreamTransport(){return this.streamTransport||(this.streamTransport=new WebSocketTransport(this.options,this.apiKey,this.appId,this.authProvider,this.appCheckProvider,this.transportOptions,this._isUsingGen,this._callerSdkType),this.isUsingEmulator&&this.transportOptions&&this.streamTransport.useEmulator(this.transportOptions.host,this.transportOptions.port,this.transportOptions.sslEnabled),this.streamTransport.onGracefulStreamClose=()=>{this.streamTransport=void 0}),this.streamTransport}executeShouldUseStream(){return!!this.streamTransport&&!this.streamTransport.isPendingClose&&this.streamTransport.streamIsReady&&this.streamTransport.hasActiveSubscriptions&&!this.streamTransport.isUnableToConnect}invokeQuery(e,t){return this.executeShouldUseStream()?this.streamTransport.invokeQuery(e,t).catch((s=>{if(this.executeShouldUseStream())throw s;return this.restTransport.invokeQuery(e,t)})):this.restTransport.invokeQuery(e,t)}invokeMutation(e,t){return this.executeShouldUseStream()?this.streamTransport.invokeMutation(e,t).catch((s=>{if(this.executeShouldUseStream())throw s;return this.restTransport.invokeMutation(e,t)})):this.restTransport.invokeMutation(e,t)}invokeSubscribe(e,t,s){const r=this.initStreamTransport();if(r.isUnableToConnect)throw new DataConnectError(g.OTHER,"Unable to connect streaming connection to server. Subscriptions are unavailable.");r.invokeSubscribe(e,t,s)}invokeUnsubscribe(e,t){this.streamTransport&&this.streamTransport.invokeUnsubscribe(e,t)}useEmulator(e,t,s){this.isUsingEmulator=!0,this.transportOptions={host:e,port:t,sslEnabled:s},this.restTransport.useEmulator(e,t,s),this.streamTransport&&this.streamTransport.useEmulator(e,t,s)}onAuthTokenChanged(e){this.restTransport.onAuthTokenChanged(e),this.streamTransport&&this.streamTransport.onAuthTokenChanged(e)}_setCallerSdkType(e){this._callerSdkType=e,this.restTransport._setCallerSdkType(e),this.streamTransport&&this.streamTransport._setCallerSdkType(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:T,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=R.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 DataConnectTransportManager."),this._transportClass=DataConnectTransportManager),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||_,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(g.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}`:""}`),e.enableEmulator({host:t,port:s,sslEnabled:r})}function getDataConnect(e,s,r){let n,i,o;"location"in e?(i=e,n=t(),o=s):(n=e,i=s,o=r),n&&0!==Object.keys(n).length||(n=t());const a={...i,projectId:n.options.projectId},c=Object.fromEntries(Object.entries(a).sort()),h=_getProvider(n,"data-connect"),u=JSON.stringify(c);if(h.isInitialized(u)){const e=h.getImmediate({identifier:u}),t=h.getOptions(u);if(Object.keys(t).length>0)return logDebug("Re-using cached instance"),e}validateDCOptions(i),logDebug("Creating new DataConnect instance");const l=h.initialize({instanceIdentifier:u,options:Object.fromEntries(Object.entries({...c}).sort())});return o?.cacheSettings&&l.setCacheSettings(o.cacheSettings),l}function validateDCOptions(e){if(!e)throw new DataConnectError(g.INVALID_ARGUMENT,"DC Option Required");return["connector","location","service"].forEach((t=>{if(null===e[t]||void 0===e[t])throw new DataConnectError(g.INVALID_ARGUMENT,`${t} Required`)})),!0}function terminate(e){return e._delete()}const A={MEMORY:"MEMORY"};function makeMemoryCacheProvider(){return new MemoryStub}const D={PREFER_CACHE:"PREFER_CACHE",CACHE_ONLY:"CACHE_ONLY",SERVER_ONLY:"SERVER_ONLY"};function executeQuery(e,t){if(e.refType!==y)return Promise.reject(new DataConnectError(g.INVALID_ARGUMENT,"ExecuteQuery can only execute query operations"));const s=e.dataConnect._queryManager,r=t?.fetchPolicy??D.PREFER_CACHE;switch(r){case D.SERVER_ONLY:return s.fetchServerResults(e);case D.CACHE_ONLY:return s.fetchCacheResults(e,!0);case D.PREFER_CACHE:return s.preferCacheResults(e,!1);default:throw new DataConnectError(g.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:y,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(g.INVALID_ARGUMENT,"Variables required.");return{dc:n,vars:i}}function validateArgsWithOptions(e,t,s,r,n,i){let o,a,c;if(t&&"enableEmulator"in t?(o=t,n?(a=s,c=r):(a=void 0,c=s)):(o=getDataConnect(e),n?(a=t,c=s):(a=void 0,c=t)),!o||!a&&i)throw new DataConnectError(g.INVALID_ARGUMENT,"Variables required.");return{dc:o,vars:a,options:c}}function subscribe(e,t,s,r){let n,i,o;if("refInfo"in e){const t=e,{data:s,source:r,fetchTime:o}=t;n=toQueryRef(t),i={data:s,source:r,fetchTime:o,ref:n,toJSON:getRefSerializer(n,s,r,o)}}else n=e;if("function"==typeof t?o=t:(o=t.onNext,s=t.onErr,r=t.onComplete),!o)throw new DataConnectError(g.INVALID_ARGUMENT,"Must provide onNext");return n.dataConnect._queryManager.addSubscription(n,o,r,s,i)}!function registerDataConnect(e){!function setSDKVersion(e){p=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 o=s;if(t&&(o={...JSON.parse(t),...o}),!r.options.projectId)throw new DataConnectError(g.INVALID_ARGUMENT,"Project ID must be provided. Did you pass in a proper projectId to initializeApp?");return new DataConnect(r,{...o,projectId:r.options.projectId},n,i)}),"PUBLIC").setMultipleInstances(!0)),r(l,d,e),r(l,d,"esm2020")}();export{AbstractDataConnectTransport,R as CallerSdkTypeEnum,g as Code,DataConnect,DataConnectError,DataConnectOperationError,E as MUTATION_STR,MutationManager,y as QUERY_STR,D as QueryFetchPolicy,C as SOURCE_CACHE,T as SOURCE_SERVER,A as StorageType,areTransportOptionsEqual,connectDataConnectEmulator,executeMutation,executeQuery,getDataConnect,getGoogApiClientValue$1 as getGoogApiClientValue,makeMemoryCacheProvider,mutationRef,parseOptions,queryRef,setLogLevel,subscribe,terminate,toQueryRef,validateArgs,validateArgsWithOptions,validateDCOptions};
|
|
1
|
+
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.12.1/firebase-app.js";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(o,((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 o=/\{\$([^}]+)}/g;function isCloudWorkstation(e){try{return(e.startsWith("http://")||e.startsWith("https://")?new URL(e).hostname:e).endsWith(".cloudworkstations.dev")}catch{return!1}}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 a;!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"}(a||(a={}));const c={debug:a.DEBUG,verbose:a.VERBOSE,info:a.INFO,warn:a.WARN,error:a.ERROR,silent:a.SILENT},h=a.INFO,u={[a.DEBUG]:"log",[a.VERBOSE]:"log",[a.INFO]:"info",[a.WARN]:"warn",[a.ERROR]:"error"},defaultLogHandler=(e,t,...s)=>{if(t<e.logLevel)return;const r=(new Date).toISOString(),n=u[t];if(!n)throw new Error(`Attempted to log a message with an invalid logType (value: ${t})`);console[n](`[${r}] ${e.name}:`,...s)};const l="@firebase/data-connect",d="0.6.0";let p="";const g={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(g.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 b="_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(g.INVALID_ARGUMENT,"EntityNode initialized with non-object value");if(null!==t){"object"==typeof t&&s&&s[b]&&"string"==typeof s[b]&&(this.globalId=s[b],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 o=s&&s[i],a=[],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,o&&o[s],r,n),a.push(t)}else c.push(h);if(c.length>0&&a.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 a.length>0?this.objectLists[i]=a:this.scalars[i]=[]}else{if(null===t[i]){this.scalars[i]=null;continue}const o=new EntityNode;await o.loadData(e,t[i],s&&s[i],r,n),this.references[i]=o}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===f.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[b]=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 f;!function(e){e[e.hydrated=0]="hydrated",e[e.dehydrated=1]="dehydrated"}(f||(f={}));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(f.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(g.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:o}=await r.dehydrateResults(t,s,n,e),a=new Date;return await n.setResultTree(e,new ResultTree(i,t.maxAge||this.cacheSettings.maxAgeSeconds,a,a)),o}}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 m=new class Logger{constructor(e){this.name=e,this._logLevel=h,this._logHandler=defaultLogHandler,this._userLogHandler=null}get logLevel(){return this._logLevel}set logLevel(e){if(!(e in a))throw new TypeError(`Invalid value "${e}" assigned to \`logLevel\``);this._logLevel=e}setLogLevel(e){this._logLevel="string"==typeof e?c[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,a.DEBUG,...e),this._logHandler(this,a.DEBUG,...e)}log(...e){this._userLogHandler&&this._userLogHandler(this,a.VERBOSE,...e),this._logHandler(this,a.VERBOSE,...e)}info(...e){this._userLogHandler&&this._userLogHandler(this,a.INFO,...e),this._logHandler(this,a.INFO,...e)}warn(...e){this._userLogHandler&&this._userLogHandler(this,a.WARN,...e),this._logHandler(this,a.WARN,...e)}error(...e){this._userLogHandler&&this._userLogHandler(this,a.ERROR,...e),this._logHandler(this,a.ERROR,...e)}}("@firebase/data-connect");function setLogLevel(e){m.setLogLevel(e)}function logDebug(e){m.debug(`DataConnect (${p}): ${e}`)}function logError(e){m.error(`DataConnect (${p}): ${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 y="query",E="mutation",T="SERVER",C="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 v,S;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){v=e}((e=>JSON.stringify(sortKeysForObj(e)))),function setDecoder(e){S=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(v({name:e.ref.name,variables:e.ref.variables,refType:y}),e.data,s)}{const t=v({name:e.ref.name,variables:e.ref.variables,refType:y});return this.subscriptionCache.set(t,e),[t]}}addSubscription(e,t,s,r,n){const i=v({name:e.name,variables:e.variables,refType:y}),unsubscribe=()=>{if(this.callbacks.has(i)){const t=this.callbacks.get(i).filter((e=>e!==o));this.callbacks.set(i,t),0===t.length&&(this.callbacks.delete(i),this.transport.invokeUnsubscribe(e.name,e.variables)),s?.()}},o={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.get(i).push(o):(this.callbacks.set(i,[o]),this.transport.invokeSubscribe(this.makeSubscribeObserver(e),e.name,e.variables)),unsubscribe}async fetchServerResults(e){await this.waitForQueuedWrites();const t=v({name:e.name,variables:e.variables,refType:y});try{const s=await this.transport.invokeQuery(e.name,e.variables),r=Date.now().toString(),n=s.extensions,i={...s,ref:e,source:T,fetchTime:r,data:s.data,extensions:getDataConnectExtensionsWithoutMaxAge(n),toJSON:getRefSerializer(e,s.data,T,r)},o=await this.updateCache(i,n?.dataConnect);return this.publishDataToSubscribers(t,i),this.cache?await this.publishCacheResultsToSubscribers(o,r):this.subscriptionCache.set(t,i),i}catch(e){throw this.publishErrorToSubscribers(t,e),e}}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(g.OTHER,"No cache entry found for query: "+e.name);const r=Date.now().toString(),n={...s,ref:e,source:C,fetchTime:r,data:s.data,extensions:s.extensions,toJSON:getRefSerializer(e,s.data,C,r)};if(this.cache){const t=v({name:e.name,variables:e.variables,refType:y});await this.publishCacheResultsToSubscribers([t],r)}else{const t=v({name:e.name,variables:e.variables,refType:y});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=v({name:e.name,variables:e.variables,refType:y});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:C,ref:e,data:r,toJSON:getRefSerializer(e,r,C,n.cachedAt.toString()),fetchTime:n.cachedAt.toString()};return(await this.cache.getResultTree(s)).updateAccessed(),i}async getFromSubscriberCache(e){const t=v({name:e.name,variables:e.variables,refType:y});if(!this.subscriptionCache.has(t))return;const s=this.subscriptionCache.get(t);return s.source=C,s.toJSON=getRefSerializer(s.ref,s.data,C,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(f.hydrated),{name:r,variables:n}=S(s),i={dataConnect:this.dc,refType:y,name:r,variables:n};this.publishDataToSubscribers(s,{data:e,fetchTime:t,ref:i,source:C,toJSON:getRefSerializer(i,e,C,t)})}}enableEmulator(e,t){this.transport.useEmulator(e,t)}makeSubscribeObserver(e){const t=v({name:e.name,variables:e.variables,refType:y});return{onData:async s=>{await this.handleStreamNotification(t,s,e)},onDisconnect:(e,s)=>{this.handleStreamDisconnect(t,e,s)},onError:e=>{this.publishErrorToSubscribers(t,e)}}}async handleStreamNotification(e,t,s){if(t.errors&&t.errors.length>0){const s=JSON.stringify(t.errors.map((e=>e&&"object"==typeof e?{message:e.message,code:e.code}:e))),r={errors:t.errors,data:t.data},n=new DataConnectOperationError("DataConnect error received from subscribe notification: "+s,r);return void this.publishErrorToSubscribers(e,n)}const r=Date.now().toString(),n={ref:s,source:T,fetchTime:r,data:t.data,extensions:getDataConnectExtensionsWithoutMaxAge(t.extensions),toJSON:getRefSerializer(s,t.data,T,r)},i=await this.updateCache(n,t.extensions?.dataConnect);this.publishDataToSubscribers(e,n),this.cache&&await this.publishCacheResultsToSubscribers(i,r)}handleStreamDisconnect(e,t,s){const r=new DataConnectError(t,s);this.publishErrorToSubscribers(e,r);const n=this.callbacks.get(e);n&&[...n].forEach((e=>e.unsubscribe()))}}function getDataConnectExtensionsWithoutMaxAge(e){return{dataConnect:e.dataConnect?.filter((e=>"entityId"in e||"entityIds"in e))}}const R={Base:"Base",Generated:"Generated",TanstackReactCore:"TanstackReactCore",GeneratedReact:"GeneratedReact",TanstackAngularCore:"TanstackAngularCore",GeneratedAngular:"GeneratedAngular"};function getGoogApiClientValue$1(e,t){let s="gl-js/ fire/"+p;return t!==R.Base&&t!==R.Generated?s+=" js/"+t.toLowerCase():(e||t===R.Generated)&&(s+=" js/gen"),s}class AbstractDataConnectTransport{constructor(e,t,s,r,n,i,o=!1,a=R.Base){this.apiKey=t,this.appId=s,this.authProvider=r,this.appCheckProvider=n,this._isUsingGen=o,this._callerSdkType=a,this._host="",this._location="l",this._connectorName="",this._secure=!0,this._project="p",this._authToken=null,this._appCheckToken=null,this._lastToken=null,this._isUsingEmulator=!1,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:u,service:l}=e;if(c&&(this._location=c),h&&(this._project=h),this._serviceName=l,!u)throw new DataConnectError(g.INVALID_ARGUMENT,"Connector Name required!");this._connectorName=u,this._connectorResourcePath=`projects/${this._project}/locations/${this._location}/services/${this._serviceName}/connectors/${this._connectorName}`,this.authProvider?.addTokenChangeListener((e=>{logDebug(`New Token Available: ${e}`),this.onAuthTokenChanged(e)})),this.appCheckProvider?.addTokenChangeListener((e=>{const{token:t}=e;logDebug(`New App Check Token Available: ${t}`),this._appCheckToken=t}))}useEmulator(e,t,s){this._host=e,this._isUsingEmulator=!0,"number"==typeof t&&(this._port=t),void 0!==s&&(this._secure=s)}async getWithAuth(e=!1){let t=new Promise((e=>e(this._authToken)));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._authToken=e.accessToken,this._authToken):null)):new Promise((e=>e(""))),t}async 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===g.UNAUTHORIZED&&!t&&s)return logDebug("Retrying due to unauthorized"),this.withRetry(e,!0);throw r}))}_setLastToken(e){this._lastToken=e}_setCallerSdkType(e){this._callerSdkType=e}}let k=globalThis.fetch;function getGoogApiClientValue(e,t){let s="gl-js/ fire/"+p;return t!==R.Base&&t!==R.Generated?s+=" js/"+t.toLowerCase():(e||t===R.Generated)&&(s+=" js/gen"),s}async function dcFetch(e,t,{signal:s},r,n,i,o,a,c){if(!k)throw new DataConnectError(g.OTHER,"No Fetch Implementation detected!");const h={"Content-Type":"application/json","X-Goog-Api-Client":getGoogApiClientValue(o,a)};n&&(h["X-Firebase-Auth-Token"]=n),r&&(h["x-firebase-gmpid"]=r),i&&(h["X-Firebase-AppCheck"]=i);const u={body:JSON.stringify(t),method:"POST",headers:h,signal:s};let l,d;isCloudWorkstation(e)&&c&&(u.credentials="include");try{l=await k(e,u)}catch(e){const t=e&&"object"==typeof e&&"message"in e?e.message:String(e);throw new DataConnectError(g.OTHER,"Failed to fetch: "+t)}try{d=await l.json()}catch(e){const t=e&&"object"==typeof e&&"message"in e?e.message:String(e);throw new DataConnectError(g.OTHER,"Failed to parse JSON response: "+t)}const p=function getErrorMessage(e){if("message"in e&&e.message)return e.message;return JSON.stringify(e)}(d);if(l.status>=400){if(logError("Error while performing request: "+JSON.stringify(d)),401===l.status)throw new DataConnectError(g.UNAUTHORIZED,p);throw new DataConnectError(g.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}const _="firebasedataconnect.googleapis.com";function addToken(e,t){if(!t)return e;const s=new URL(e);return s.searchParams.append("key",t),s.toString()}class RESTTransport extends AbstractDataConnectTransport{constructor(e,t,s,r,n,i,o=!1,a=R.Base){super(e,t,s,r,n,i,o,a),this.invokeQuery=(e,t)=>{const s=new AbortController;return this.withRetry((()=>dcFetch(addToken(`${this.endpointUrl}:executeQuery`,this.apiKey),{name:this._connectorResourcePath,operationName:e,variables:t},s,this.appId,this._authToken,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:this._connectorResourcePath,operationName:e,variables:t},s,this.appId,this._authToken,this._appCheckToken,this._isUsingGen,this._callerSdkType,this._isUsingEmulator)))}}get endpointUrl(){return function restUrlBuilder(e,t){const{connector:s,location:r,projectId:n,service:i}=e,{host:o,sslEnabled:a,port:c}=t;let h=`${a?"https":"http"}://${o||_}`;if("number"==typeof c)h+=`:${c}`;else if(void 0!==c)throw logError("Port type is of an invalid type"),new DataConnectError(g.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})}invokeSubscribe(e,t,s){throw new DataConnectError(g.NOT_SUPPORTED,"Subscriptions are not supported using REST!")}invokeUnsubscribe(e,t){throw new DataConnectError(g.NOT_SUPPORTED,"Unsubscriptions are not supported using REST!")}onAuthTokenChanged(e){this._authToken=e}}class AbstractDataConnectStreamTransport extends AbstractDataConnectTransport{constructor(){super(...arguments),this.pendingClose=!1,this.isUnableToConnect=!1,this.requestNumber=1,this.activeQueryExecuteRequests=new Map,this.activeMutationExecuteRequests=new Map,this.activeSubscribeRequests=new Map,this.executeRequestPromises=new Map,this.subscribeObservers=new Map,this.closeTimeout=null,this.closeTimeoutFinished=!1,this.hasWaitedForInitialAuth=!1,this.isFirstStreamMessage=!0,this.lastSentAuthToken=null}get isPendingClose(){return this.pendingClose}get hasActiveSubscriptions(){return this.activeSubscribeRequests.size>0}get hasActiveExecuteRequests(){return this.activeQueryExecuteRequests.size>0||this.activeMutationExecuteRequests.size>0}nextRequestId(){return(this.requestNumber++).toString()}trackQueryExecuteRequest(e,t,s){let r,n;const i={responsePromise:new Promise(((e,t)=>{r=e,n=t})),resolveFn:r,rejectFn:n};return this.activeQueryExecuteRequests.set(t,s),this.executeRequestPromises.set(e,i),i}trackMutationExecuteRequest(e,t,s){let r,n;const i={responsePromise:new Promise(((e,t)=>{r=e,n=t})),resolveFn:r,rejectFn:n},o=this.activeMutationExecuteRequests.get(t)||[];return o.push(s),this.activeMutationExecuteRequests.set(t,o),this.executeRequestPromises.set(e,i),i}trackSubscribeRequest(e,t,s,r){this.activeSubscribeRequests.set(t,s),this.subscribeObservers.set(e,r)}cleanupQueryExecuteRequest(e,t){this.activeQueryExecuteRequests.delete(t),this.executeRequestPromises.delete(e)}cleanupMutationExecuteRequest(e,t){const s=this.activeMutationExecuteRequests.get(t);if(s){const r=s.filter((t=>t.requestId!==e));r.length>0?this.activeMutationExecuteRequests.set(t,r):this.activeMutationExecuteRequests.delete(t)}this.executeRequestPromises.delete(e)}cleanupSubscribeRequest(e,t){this.activeSubscribeRequests.delete(t),this.subscribeObservers.delete(e)}get shouldIncludeAuth(){return this.isFirstStreamMessage||!!this._authToken&&this._authToken!==this.lastSentAuthToken}onConnectionReady(){this.isFirstStreamMessage=!0,this.lastSentAuthToken=null,this.hasWaitedForInitialAuth=!1}async attemptClose(){this.hasActiveSubscriptions||this.hasActiveExecuteRequests||(this.cancelClose(),await this.closeConnection(),this.onGracefulStreamClose?.())}prepareToCloseGracefully(){this.pendingClose||(this.pendingClose=!0,this.closeTimeoutFinished=!1,this.closeTimeout=setTimeout((()=>{this.closeTimeoutFinished=!0,this.attemptClose()}),6e4))}cancelClose(){this.closeTimeout&&clearTimeout(this.closeTimeout),this.pendingClose=!1,this.closeTimeoutFinished=!1}rejectAllActiveRequests(e,t){this.activeQueryExecuteRequests.clear(),this.activeMutationExecuteRequests.clear(),this.activeSubscribeRequests.clear();const s=new DataConnectError(e,t);for(const[e,{rejectFn:t}]of this.executeRequestPromises)this.executeRequestPromises.delete(e),t(s);for(const[s,r]of this.subscribeObservers)this.subscribeObservers.delete(s),r.onDisconnect(e,t)}onStreamClose(e,t){this.rejectAllActiveRequests(g.OTHER,`Stream disconnected with code ${e}: ${t}`)}prepareMessage(e){const t={...e},s={};return this.appId&&(s["x-firebase-gmpid"]=this.appId),s["X-Goog-Api-Client"]=getGoogApiClientValue$1(this._isUsingGen,this._callerSdkType),this.shouldIncludeAuth&&this._authToken&&(s["X-Firebase-Auth-Token"]=this._authToken,this.lastSentAuthToken=this._authToken),this.isFirstStreamMessage&&(this._appCheckToken&&(s["X-Firebase-App-Check"]=this._appCheckToken),t.name=this._connectorResourcePath),t.headers=s,this.isFirstStreamMessage=!1,t}async sendRequestMessage(e){if(!this.hasWaitedForInitialAuth&&this.authProvider&&(await this.getWithAuth(),this.hasWaitedForInitialAuth=!0),this.streamIsReady){const t=this.prepareMessage(e);return this.sendMessage(t)}return this.ensureConnection().then((()=>{const t=this.prepareMessage(e);return this.sendMessage(t)}))}getMapKey(e,t){const s=this.sortObjectKeys(t);return JSON.stringify({operationName:e,variables:s})}sortObjectKeys(e){if(null===e||"object"!=typeof e||Array.isArray(e))return e;const t={};return Object.keys(e).sort().forEach((s=>{t[s]=this.sortObjectKeys(e[s])})),t}invokeQuery(e,t){const s=this.nextRequestId(),r={operationName:e,variables:t},n=this.getMapKey(e,t),i={requestId:s,execute:r};let{responsePromise:o,rejectFn:a}=this.trackQueryExecuteRequest(s,n,i);return o=o.finally((()=>{this.cleanupQueryExecuteRequest(s,n),this.hasActiveSubscriptions||this.hasActiveExecuteRequests||!this.closeTimeoutFinished||this.attemptClose()})),this.sendRequestMessage(i).catch((e=>{a(e)})),o}invokeMutation(e,t){const s=this.nextRequestId(),r={operationName:e,variables:t},n=this.getMapKey(e,t),i={requestId:s,execute:r};let{responsePromise:o,rejectFn:a}=this.trackMutationExecuteRequest(s,n,i);return o=o.finally((()=>{this.cleanupMutationExecuteRequest(s,n),this.hasActiveSubscriptions||this.hasActiveExecuteRequests||!this.closeTimeoutFinished||this.attemptClose()})),this.sendRequestMessage(i).catch((e=>{a(e)})),o}invokeSubscribe(e,t,s){this.cancelClose();const r=this.nextRequestId(),n={operationName:t,variables:s},i=this.getMapKey(t,s),o={requestId:r,subscribe:n};this.trackSubscribeRequest(r,i,o,e),this.sendRequestMessage(o).catch((t=>{e.onError(t instanceof Error?t:new Error(String(t))),this.cleanupSubscribeRequest(r,i),this.hasActiveSubscriptions||this.prepareToCloseGracefully()}))}invokeUnsubscribe(e,t){const s=this.getMapKey(e,t),r=this.activeSubscribeRequests.get(s);if(!r)return;const n=r.requestId,i={requestId:n,cancel:{}};this.cleanupSubscribeRequest(n,s),this.sendRequestMessage(i).catch((e=>{logError(`Stream Transport failed to send unsubscribe message: ${e}`)})),this.hasActiveSubscriptions||this.prepareToCloseGracefully()}onAuthTokenChanged(e){const t=this._authToken;this._authToken=e;const s=this.authUid,r=this.authProvider?.getAuth()?.getUid();this.authUid=r;void 0===s||(t&&null===e||!s&&r||s&&r!==s)&&(this.rejectAllActiveRequests(g.UNAUTHORIZED,"Stream disconnected due to auth change."),this.attemptClose())}async handleResponse(e,t){if(this.executeRequestPromises.has(e)){const{resolveFn:s,rejectFn:r}=this.executeRequestPromises.get(e);if(t.errors&&t.errors.length){const e={errors:t.errors,data:t.data},s=JSON.stringify(t.errors);r(new DataConnectOperationError("DataConnect error while performing request: "+s,e))}else s(t)}else{if(!this.subscribeObservers.has(e))throw new DataConnectError(g.OTHER,`Stream response contained unrecognized requestId '${e}'`);{const s=this.subscribeObservers.get(e);await s.onData(t)}}}}let w=globalThis.WebSocket;class WebSocketTransport extends AbstractDataConnectStreamTransport{get endpointUrl(){return function websocketUrlBuilder(e,t){const{location:s}=e,{host:r,sslEnabled:n,port:i}=t;let o=`${n?"wss":"ws"}://${r||_}`;if("number"==typeof i)o+=`:${i}`;else if(void 0!==i)throw logError("Port type is of an invalid type"),new DataConnectError(g.INVALID_ARGUMENT,"Incorrect type for port passed in!");return`${o}/ws/google.firebase.dataconnect.v1.ConnectorStreamService/Connect/locations/${s}`}({connector:this._connectorName,location:this._location,projectId:this._project,service:this._serviceName},{host:this._host,sslEnabled:this._secure,port:this._port})}decodeBinaryResponse(e){return this.decoder||(this.decoder=new TextDecoder("utf-8")),this.decoder.decode(e)}get streamIsReady(){return this.connection?.readyState===WebSocket.OPEN}constructor(e,t,s,r,n,i,o=!1,a=R.Base){super(e,t,s,r,n,i,o,a),this.apiKey=t,this.appId=s,this.authProvider=r,this.appCheckProvider=n,this._isUsingGen=o,this._callerSdkType=a,this.decoder=void 0,this.connection=void 0,this.connectionAttempt=null}ensureConnection(){try{return this.streamIsReady?Promise.resolve():(this.connectionAttempt||(this.connectionAttempt=new Promise(((e,t)=>{if(!w)throw new DataConnectError(g.OTHER,"No WebSocket Implementation detected!");const s=new w(this.endpointUrl);this.connection=s,this.connection.binaryType="arraybuffer",s.onopen=()=>{this.isUnableToConnect=!1,this.onConnectionReady(),e()},s.onerror=e=>{this.connectionAttempt=null,this.isUnableToConnect=!0;const s=new DataConnectError(g.OTHER,"Error using WebSocket connection, closing WebSocket");this.handleError(s),t(s)},s.onmessage=e=>this.handleWebSocketMessage(e).catch((async e=>{this.handleError(e)})),s.onclose=e=>this.handleWebsocketDisconnect(e)}))),this.connectionAttempt)}catch(e){throw this.handleError(e),e}}openConnection(){return this.ensureConnection().catch((e=>{throw new DataConnectError(g.OTHER,`Failed to open connection: ${e}`)}))}closeConnection(e,t){if(!this.connection)return this.connectionAttempt=null,Promise.resolve();let s;try{if(t){const s=123,r=new TextEncoder;if(r.encode(t).length<=s)this.connection.close(e,t);else{const n=new Uint8Array(s),{read:i}=r.encodeInto(t,n),o=t.substring(0,i);this.connection.close(e,o)}}else this.connection.close(e)}catch(e){s=e}finally{this.connection=void 0,this.connectionAttempt=null}return s?Promise.reject(s):Promise.resolve()}handleWebsocketDisconnect(e){this.connection=void 0,this.connectionAttempt=null,this.onStreamClose(e.code,e.reason)}handleError(e){logError(`DataConnect WebSocket error, closing stream: ${e}`);let t=e?String(e):"Unknown Error";e instanceof DataConnectError&&(t=e.message),this.closeConnection(1e3,t)}sendMessage(e){return this.ensureConnection().then((()=>{try{return this.connection.send(JSON.stringify(e)),Promise.resolve()}catch(e){throw this.handleError(e),new DataConnectError(g.OTHER,`Failed to send message: ${String(e)}`)}}))}async handleWebSocketMessage(e){const t=this.parseWebSocketData(e.data),s=t.requestId,r={data:t.data,errors:t.errors,extensions:t.extensions||{dataConnect:[]}};await this.handleResponse(s,r)}parseWebSocketData(e){const t="string"==typeof e;let s,r;try{s=t?JSON.parse(e):JSON.parse(this.decodeBinaryResponse(e))}catch(e){throw new DataConnectError(g.OTHER,`Could not parse WebSocket message: ${e instanceof Error?e.message:String(e)}`)}if("object"!=typeof s||null===s)throw new DataConnectError(g.OTHER,"WebSocket message is not an object");if(t){if(!("result"in s))throw new DataConnectError(g.OTHER,"WebSocket message from emulator did not include result");if("object"!=typeof s.result||null===s.result)throw new DataConnectError(g.OTHER,"WebSocket message result is not an object");r=s.result}else r=s;if(!("requestId"in r))throw new DataConnectError(g.OTHER,"WebSocket message did not include requestId");return r}}class DataConnectTransportManager{constructor(e,t,s,r,n,i,o=!1,a){this.options=e,this.apiKey=t,this.appId=s,this.authProvider=r,this.appCheckProvider=n,this.transportOptions=i,this._isUsingGen=o,this._callerSdkType=a,this.isUsingEmulator=!1,this.restTransport=new RESTTransport(e,t,s,r,n,i,o,a)}initStreamTransport(){return this.streamTransport||(this.streamTransport=new WebSocketTransport(this.options,this.apiKey,this.appId,this.authProvider,this.appCheckProvider,this.transportOptions,this._isUsingGen,this._callerSdkType),this.isUsingEmulator&&this.transportOptions&&this.streamTransport.useEmulator(this.transportOptions.host,this.transportOptions.port,this.transportOptions.sslEnabled),this.streamTransport.onGracefulStreamClose=()=>{this.streamTransport=void 0}),this.streamTransport}executeShouldUseStream(){return!!this.streamTransport&&!this.streamTransport.isPendingClose&&this.streamTransport.streamIsReady&&this.streamTransport.hasActiveSubscriptions&&!this.streamTransport.isUnableToConnect}invokeQuery(e,t){return this.executeShouldUseStream()?this.streamTransport.invokeQuery(e,t).catch((s=>{if(this.executeShouldUseStream())throw s;return this.restTransport.invokeQuery(e,t)})):this.restTransport.invokeQuery(e,t)}invokeMutation(e,t){return this.executeShouldUseStream()?this.streamTransport.invokeMutation(e,t).catch((s=>{if(this.executeShouldUseStream())throw s;return this.restTransport.invokeMutation(e,t)})):this.restTransport.invokeMutation(e,t)}invokeSubscribe(e,t,s){const r=this.initStreamTransport();if(r.isUnableToConnect)throw new DataConnectError(g.OTHER,"Unable to connect streaming connection to server. Subscriptions are unavailable.");r.invokeSubscribe(e,t,s)}invokeUnsubscribe(e,t){this.streamTransport&&this.streamTransport.invokeUnsubscribe(e,t)}useEmulator(e,t,s){this.isUsingEmulator=!0,this.transportOptions={host:e,port:t,sslEnabled:s},this.restTransport.useEmulator(e,t,s),this.streamTransport&&this.streamTransport.useEmulator(e,t,s)}onAuthTokenChanged(e){this.restTransport.onAuthTokenChanged(e),this.streamTransport&&this.streamTransport.onAuthTokenChanged(e)}_setCallerSdkType(e){this._callerSdkType=e,this.restTransport._setCallerSdkType(e),this.streamTransport&&this.streamTransport._setCallerSdkType(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:T,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=R.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 DataConnectTransportManager."),this._transportClass=DataConnectTransportManager),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||_,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(g.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}`:""}`),e.enableEmulator({host:t,port:s,sslEnabled:r})}function getDataConnect(e,s,r){let n,i,o;"location"in e?(i=e,n=t(),o=s):(n=e,i=s,o=r),n&&0!==Object.keys(n).length||(n=t());const a={...i,projectId:n.options.projectId},c=Object.fromEntries(Object.entries(a).sort()),h=_getProvider(n,"data-connect"),u=JSON.stringify(c);if(h.isInitialized(u)){const e=h.getImmediate({identifier:u}),t=h.getOptions(u);if(Object.keys(t).length>0)return logDebug("Re-using cached instance"),e}validateDCOptions(i),logDebug("Creating new DataConnect instance");const l=h.initialize({instanceIdentifier:u,options:Object.fromEntries(Object.entries({...c}).sort())});return o?.cacheSettings&&l.setCacheSettings(o.cacheSettings),l}function validateDCOptions(e){if(!e)throw new DataConnectError(g.INVALID_ARGUMENT,"DC Option Required");return["connector","location","service"].forEach((t=>{if(null===e[t]||void 0===e[t])throw new DataConnectError(g.INVALID_ARGUMENT,`${t} Required`)})),!0}function terminate(e){return e._delete()}const A={MEMORY:"MEMORY"};function makeMemoryCacheProvider(){return new MemoryStub}const D={PREFER_CACHE:"PREFER_CACHE",CACHE_ONLY:"CACHE_ONLY",SERVER_ONLY:"SERVER_ONLY"};function executeQuery(e,t){if(e.refType!==y)return Promise.reject(new DataConnectError(g.INVALID_ARGUMENT,"ExecuteQuery can only execute query operations"));const s=e.dataConnect._queryManager,r=t?.fetchPolicy??D.PREFER_CACHE;switch(r){case D.SERVER_ONLY:return s.fetchServerResults(e);case D.CACHE_ONLY:return s.fetchCacheResults(e,!0);case D.PREFER_CACHE:return s.preferCacheResults(e,!1);default:throw new DataConnectError(g.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:y,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(g.INVALID_ARGUMENT,"Variables required.");return{dc:n,vars:i}}function validateArgsWithOptions(e,t,s,r,n,i){let o,a,c;if(t&&"enableEmulator"in t?(o=t,n?(a=s,c=r):(a=void 0,c=s)):(o=getDataConnect(e),n?(a=t,c=s):(a=void 0,c=t)),!o||!a&&i)throw new DataConnectError(g.INVALID_ARGUMENT,"Variables required.");return{dc:o,vars:a,options:c}}function subscribe(e,t,s,r){let n,i,o;if("refInfo"in e){const t=e,{data:s,source:r,fetchTime:o}=t;n=toQueryRef(t),i={data:s,source:r,fetchTime:o,ref:n,toJSON:getRefSerializer(n,s,r,o)}}else n=e;if("function"==typeof t?o=t:(o=t.onNext,s=t.onErr,r=t.onComplete),!o)throw new DataConnectError(g.INVALID_ARGUMENT,"Must provide onNext");return n.dataConnect._queryManager.addSubscription(n,o,r,s,i)}!function registerDataConnect(e){!function setSDKVersion(e){p=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 o=s;if(t&&(o={...JSON.parse(t),...o}),!r.options.projectId)throw new DataConnectError(g.INVALID_ARGUMENT,"Project ID must be provided. Did you pass in a proper projectId to initializeApp?");return new DataConnect(r,{...o,projectId:r.options.projectId},n,i)}),"PUBLIC").setMultipleInstances(!0)),r(l,d,e),r(l,d,"esm2020")}();export{AbstractDataConnectTransport,R as CallerSdkTypeEnum,g as Code,DataConnect,DataConnectError,DataConnectOperationError,E as MUTATION_STR,MutationManager,y as QUERY_STR,D as QueryFetchPolicy,C as SOURCE_CACHE,T as SOURCE_SERVER,A as StorageType,areTransportOptionsEqual,connectDataConnectEmulator,executeMutation,executeQuery,getDataConnect,getGoogApiClientValue$1 as getGoogApiClientValue,makeMemoryCacheProvider,mutationRef,parseOptions,queryRef,setLogLevel,subscribe,terminate,toQueryRef,validateArgs,validateArgsWithOptions,validateDCOptions};
|
|
2
2
|
|
|
3
3
|
//# sourceMappingURL=firebase-data-connect.js.map
|