@schematichq/schematic-js 1.2.16 → 1.2.17
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/README.md +48 -1
- package/dist/schematic.browser.js +2 -2
- package/dist/schematic.cjs.js +30 -53
- package/dist/schematic.d.ts +2 -6
- package/dist/schematic.esm.js +30 -53
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ pnpm add @schematichq/schematic-js
|
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
|
-
You can use Schematic to identify users; after this, your subsequent track events and flag checks will be associated with this user.
|
|
17
|
+
You can use Schematic to identify users; after this, your subsequent track events and flag checks will be associated with this user.
|
|
18
18
|
|
|
19
19
|
A number of these examples use `keys` to identify companies and users. Learn more about keys [here](https://docs.schematichq.com/developer_resources/key_management).
|
|
20
20
|
|
|
@@ -86,6 +86,53 @@ await schematic.checkFlag("some-flag-key");
|
|
|
86
86
|
schematic.cleanup();
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
+
## Fallback Behavior
|
|
90
|
+
|
|
91
|
+
The SDK includes built-in fallback behavior you can use to ensure your application continues to function even when unable to reach Schematic (e.g., during service disruptions or network issues).
|
|
92
|
+
|
|
93
|
+
### Flag Check Fallbacks
|
|
94
|
+
|
|
95
|
+
When `checkFlag` cannot reach Schematic, it uses fallback values in the following priority order:
|
|
96
|
+
|
|
97
|
+
1. Callsite fallback - fallback values can be provided directly in the `checkFlag` call
|
|
98
|
+
2. Initialization defaults - fallback values configured via `flagCheckDefaults` or `flagValueDefaults` options when initializing the SDK
|
|
99
|
+
3. Default value - Returns `false` if no fallback is configured
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
// Provide a fallback value at the callsite
|
|
103
|
+
const value = await schematic.checkFlag({
|
|
104
|
+
key: "feature-flag",
|
|
105
|
+
fallback: true // Used if API request fails
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Or configure defaults at initialization
|
|
109
|
+
const schematic = new Schematic("your-api-key", {
|
|
110
|
+
flagValueDefaults: {
|
|
111
|
+
"feature-flag": true, // Used if API request fails and no callsite fallback
|
|
112
|
+
},
|
|
113
|
+
flagCheckDefaults: {
|
|
114
|
+
"another-flag": {
|
|
115
|
+
flag: "another-flag",
|
|
116
|
+
value: true,
|
|
117
|
+
reason: "Default value",
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Event Queueing and Retry
|
|
124
|
+
|
|
125
|
+
When events (track, identify) cannot be sent due to network issues, they are automatically queued and retried:
|
|
126
|
+
|
|
127
|
+
- Events are queued in memory (up to 100 events by default, configurable via `maxEventQueueSize`)
|
|
128
|
+
- Failed events are retried with exponential backoff (up to 5 attempts by default, configurable via `maxEventRetries`)
|
|
129
|
+
- Events are automatically flushed when the network connection is restored
|
|
130
|
+
- Events queued when the page is hidden are sent when the page becomes visible
|
|
131
|
+
|
|
132
|
+
### WebSocket Fallback
|
|
133
|
+
|
|
134
|
+
In WebSocket mode, if the WebSocket connection fails, the SDK will provide the last known value or the configured fallback values as [outlined above](/#flag-check-fallbacks). The WebSocket will also automatically attempt to re-establish it's connection with Schematic using an exponential backoff.
|
|
135
|
+
|
|
89
136
|
## Troubleshooting
|
|
90
137
|
|
|
91
138
|
For debugging and development, Schematic supports two special modes:
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";(()=>{var re=Object.create;var Q=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var ae=Object.getPrototypeOf,oe=Object.prototype.hasOwnProperty;var ce=(s,e)=>()=>(e||s((e={exports:{}}).exports,e),e.exports);var le=(s,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of se(e))!oe.call(s,n)&&n!==t&&Q(s,n,{get:()=>e[n],enumerable:!(r=ie(e,n))||r.enumerable});return s};var ue=(s,e,t)=>(t=s!=null?re(ae(s)):{},le(e||!s||!s.__esModule?Q(t,"default",{value:s,enumerable:!0}):t,s));var G=ce(z=>{(function(s){var e=(function(t){var r=typeof globalThis<"u"&&globalThis||typeof s<"u"&&s||typeof global<"u"&&global||{},n={searchParams:"URLSearchParams"in r,iterable:"Symbol"in r&&"iterator"in Symbol,blob:"FileReader"in r&&"Blob"in r&&(function(){try{return new Blob,!0}catch{return!1}})(),formData:"FormData"in r,arrayBuffer:"ArrayBuffer"in r};function c(i){return i&&DataView.prototype.isPrototypeOf(i)}if(n.arrayBuffer)var o=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],l=ArrayBuffer.isView||function(i){return i&&o.indexOf(Object.prototype.toString.call(i))>-1};function
|
|
2
|
-
`)===0?g.substr(1,g.length):g}).forEach(function(g){var y=g.split(":"),f=y.shift().trim();if(f){var D=y.join(":").trim();try{a.append(f,D)}catch(A){console.warn("Response "+A.message)}}}),a}J.call(R.prototype);function k(i,a){if(!(this instanceof k))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');if(a||(a={}),this.type="default",this.status=a.status===void 0?200:a.status,this.status<200||this.status>599)throw new RangeError("Failed to construct 'Response': The status provided (0) is outside the range [200, 599].");this.ok=this.status>=200&&this.status<300,this.statusText=a.statusText===void 0?"":""+a.statusText,this.headers=new p(a.headers),this.url=a.url||"",this._initBody(i)}J.call(k.prototype),k.prototype.clone=function(){return new k(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new p(this.headers),url:this.url})},k.error=function(){var i=new k(null,{status:200,statusText:""});return i.ok=!1,i.status=0,i.type="error",i};var ne=[301,302,303,307,308];k.redirect=function(i,a){if(ne.indexOf(a)===-1)throw new RangeError("Invalid status code");return new k(null,{status:a,headers:{location:i}})},t.DOMException=r.DOMException;try{new t.DOMException}catch{t.DOMException=function(a,d){this.message=a,this.name=d;var g=Error(a);this.stack=g.stack},t.DOMException.prototype=Object.create(Error.prototype),t.DOMException.prototype.constructor=t.DOMException}function I(i,a){return new Promise(function(d,g){var y=new R(i,a);if(y.signal&&y.signal.aborted)return g(new t.DOMException("Aborted","AbortError"));var f=new XMLHttpRequest;function D(){f.abort()}f.onload=function(){var m={statusText:f.statusText,headers:te(f.getAllResponseHeaders()||"")};y.url.indexOf("file://")===0&&(f.status<200||f.status>599)?m.status=200:m.status=f.status,m.url="responseURL"in f?f.responseURL:m.headers.get("X-Request-URL");var S="response"in f?f.response:f.responseText;setTimeout(function(){d(new k(S,m))},0)},f.onerror=function(){setTimeout(function(){g(new TypeError("Network request failed"))},0)},f.ontimeout=function(){setTimeout(function(){g(new TypeError("Network request timed out"))},0)},f.onabort=function(){setTimeout(function(){g(new t.DOMException("Aborted","AbortError"))},0)};function A(m){try{return m===""&&r.location.href?r.location.href:m}catch{return m}}if(f.open(y.method,A(y.url),!0),y.credentials==="include"?f.withCredentials=!0:y.credentials==="omit"&&(f.withCredentials=!1),"responseType"in f&&(n.blob?f.responseType="blob":n.arrayBuffer&&(f.responseType="arraybuffer")),a&&typeof a.headers=="object"&&!(a.headers instanceof p||r.Headers&&a.headers instanceof r.Headers)){var q=[];Object.getOwnPropertyNames(a.headers).forEach(function(m){q.push(u(m)),f.setRequestHeader(m,h(a.headers[m]))}),y.headers.forEach(function(m,S){q.indexOf(S)===-1&&f.setRequestHeader(S,m)})}else y.headers.forEach(function(m,S){f.setRequestHeader(S,m)});y.signal&&(y.signal.addEventListener("abort",D),f.onreadystatechange=function(){f.readyState===4&&y.signal.removeEventListener("abort",D)}),f.send(typeof y._bodyInit>"u"?null:y._bodyInit)})}return I.polyfill=!0,r.fetch||(r.fetch=I,r.Headers=p,r.Request=R,r.Response=k),t.Headers=p,t.Request=R,t.Response=k,t.fetch=I,t})({})})(typeof self<"u"?self:z)});var b=[];for(let s=0;s<256;++s)b.push((s+256).toString(16).slice(1));function H(s,e=0){return(b[s[e+0]]+b[s[e+1]]+b[s[e+2]]+b[s[e+3]]+"-"+b[s[e+4]]+b[s[e+5]]+"-"+b[s[e+6]]+b[s[e+7]]+"-"+b[s[e+8]]+b[s[e+9]]+"-"+b[s[e+10]]+b[s[e+11]]+b[s[e+12]]+b[s[e+13]]+b[s[e+14]]+b[s[e+15]]).toLowerCase()}var P,de=new Uint8Array(16);function N(){if(!P){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");P=crypto.getRandomValues.bind(crypto)}return P(de)}var fe=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),B={randomUUID:fe};function he(s,e,t){s=s||{};let r=s.random??s.rng?.()??N();if(r.length<16)throw new Error("Random bytes length must be >= 16");if(r[6]=r[6]&15|64,r[8]=r[8]&63|128,e){if(t=t||0,t<0||t+16>e.length)throw new RangeError(`UUID byte range ${t}:${t+15} is out of buffer bounds`);for(let n=0;n<16;++n)e[t+n]=r[n];return e}return H(r)}function ge(s,e,t){return B.randomUUID&&!e&&!s?B.randomUUID():he(s,e,t)}var C=ge;var it=ue(G());function F(s){return pe(s,!1)}function pe(s,e){return s==null?s:{companyId:s.company_id==null?void 0:s.company_id,error:s.error==null?void 0:s.error,featureAllocation:s.feature_allocation==null?void 0:s.feature_allocation,featureUsage:s.feature_usage==null?void 0:s.feature_usage,featureUsageEvent:s.feature_usage_event==null?void 0:s.feature_usage_event,featureUsagePeriod:s.feature_usage_period==null?void 0:s.feature_usage_period,featureUsageResetAt:s.feature_usage_reset_at==null?void 0:new Date(s.feature_usage_reset_at),flag:s.flag,flagId:s.flag_id==null?void 0:s.flag_id,reason:s.reason,ruleId:s.rule_id==null?void 0:s.rule_id,ruleType:s.rule_type==null?void 0:s.rule_type,userId:s.user_id==null?void 0:s.user_id,value:s.value}}function L(s){return ye(s,!1)}function ye(s,e=!1){return s==null?s:{company_id:s.companyId,error:s.error,flag_id:s.flagId,flag_key:s.flagKey,reason:s.reason,req_company:s.reqCompany,req_user:s.reqUser,rule_id:s.ruleId,user_id:s.userId,value:s.value}}function _(s){return be(s,!1)}function be(s,e){return s==null?s:{data:F(s.data),params:s.params}}function X(s){return ve(s,!1)}function ve(s,e){return s==null?s:{flags:s.flags.map(F)}}function M(s){return ke(s,!1)}function ke(s,e){return s==null?s:{data:X(s.data),params:s.params}}var O=s=>{let{companyId:e,error:t,featureAllocation:r,featureUsage:n,featureUsageEvent:c,featureUsagePeriod:o,featureUsageResetAt:l,flag:u,flagId:h,reason:v,ruleId:p,ruleType:E,userId:x,value:T}=F(s);return{featureUsageExceeded:!T&&(E=="company_override_usage_exceeded"||E=="plan_entitlement_usage_exceeded"),companyId:e??void 0,error:t??void 0,featureAllocation:r??void 0,featureUsage:n??void 0,featureUsageEvent:c===null?void 0:c,featureUsagePeriod:o??void 0,featureUsageResetAt:l??void 0,flag:u,flagId:h??void 0,reason:v,ruleId:p??void 0,ruleType:E??void 0,userId:x??void 0,value:T}};function w(s){let e=Object.keys(s).reduce((t,r)=>{let c=Object.keys(s[r]||{}).sort().reduce((o,l)=>(o[l]=s[r][l],o),{});return t[r]=c,t},{});return JSON.stringify(e)}var $="1.2.16";var K="schematicId";var U=class{additionalHeaders={};apiKey;apiUrl="https://api.schematichq.com";conn=null;context={};debugEnabled=!1;offlineEnabled=!1;eventQueue;contextDependentEventQueue;eventUrl="https://c.schematichq.com";flagCheckListeners={};flagValueListeners={};isPending=!0;isPendingListeners=new Set;storage;useWebSocket=!1;checks={};featureUsageEventMap={};webSocketUrl="wss://api.schematichq.com";webSocketConnectionTimeout=1e4;webSocketReconnect=!0;webSocketMaxReconnectAttempts=7;webSocketMaxConnectionAttempts=3;webSocketInitialRetryDelay=1e3;webSocketMaxRetryDelay=3e4;wsReconnectAttempts=0;wsReconnectTimer=null;wsIntentionalDisconnect=!1;currentWebSocket=null;isConnecting=!1;maxEventQueueSize=100;maxEventRetries=5;eventRetryInitialDelay=1e3;eventRetryMaxDelay=3e4;retryTimer=null;flagValueDefaults={};flagCheckDefaults={};constructor(e,t){if(this.apiKey=e,this.eventQueue=[],this.contextDependentEventQueue=[],this.useWebSocket=t?.useWebSocket??!1,this.debugEnabled=t?.debug??!1,this.offlineEnabled=t?.offline??!1,typeof window<"u"&&typeof window.location<"u"){let r=new URLSearchParams(window.location.search),n=r.get("schematic_debug");n!==null&&(n===""||n==="true"||n==="1")&&(this.debugEnabled=!0);let c=r.get("schematic_offline");c!==null&&(c===""||c==="true"||c==="1")&&(this.offlineEnabled=!0,this.debugEnabled=!0)}if(this.offlineEnabled&&t?.debug!==!1&&(this.debugEnabled=!0),this.offlineEnabled&&this.setIsPending(!1),this.additionalHeaders={"X-Schematic-Client-Version":`schematic-js@${$}`,...t?.additionalHeaders??{}},t?.storage)this.storage=t.storage;else try{typeof localStorage<"u"&&(this.storage=localStorage)}catch{}t?.apiUrl!==void 0&&(this.apiUrl=t.apiUrl),t?.eventUrl!==void 0&&(this.eventUrl=t.eventUrl),t?.webSocketUrl!==void 0&&(this.webSocketUrl=t.webSocketUrl),t?.webSocketConnectionTimeout!==void 0&&(this.webSocketConnectionTimeout=t.webSocketConnectionTimeout),t?.webSocketReconnect!==void 0&&(this.webSocketReconnect=t.webSocketReconnect),t?.webSocketMaxReconnectAttempts!==void 0&&(this.webSocketMaxReconnectAttempts=t.webSocketMaxReconnectAttempts),t?.webSocketInitialRetryDelay!==void 0&&(this.webSocketInitialRetryDelay=t.webSocketInitialRetryDelay),t?.webSocketMaxRetryDelay!==void 0&&(this.webSocketMaxRetryDelay=t.webSocketMaxRetryDelay),t?.maxEventQueueSize!==void 0&&(this.maxEventQueueSize=t.maxEventQueueSize),t?.maxEventRetries!==void 0&&(this.maxEventRetries=t.maxEventRetries),t?.eventRetryInitialDelay!==void 0&&(this.eventRetryInitialDelay=t.eventRetryInitialDelay),t?.eventRetryMaxDelay!==void 0&&(this.eventRetryMaxDelay=t.eventRetryMaxDelay),t?.flagValueDefaults!==void 0&&(this.flagValueDefaults=t.flagValueDefaults),t?.flagCheckDefaults!==void 0&&(this.flagCheckDefaults=t.flagCheckDefaults),typeof window<"u"&&window?.addEventListener&&(window.addEventListener("beforeunload",()=>{this.flushEventQueue(),this.flushContextDependentEventQueue()}),this.useWebSocket&&(window.addEventListener("offline",()=>{this.debug("Browser went offline, closing WebSocket connection"),this.handleNetworkOffline()}),window.addEventListener("online",()=>{this.debug("Browser came online, attempting to reconnect WebSocket"),this.handleNetworkOnline()}))),this.offlineEnabled?this.debug("Initialized with offline mode enabled - no network requests will be made"):this.debugEnabled&&this.debug("Initialized with debug mode enabled")}resolveFallbackValue(e,t){return t!==void 0?t:e in this.flagCheckDefaults?this.flagCheckDefaults[e].value:e in this.flagValueDefaults?this.flagValueDefaults[e]:!1}resolveFallbackCheckFlagReturn(e,t,r="Fallback value used",n){if(t!==void 0)return{flag:e,value:t,reason:r,error:n};if(e in this.flagCheckDefaults){let c=this.flagCheckDefaults[e];return{...c,flag:e,reason:n!==void 0?r:c.reason,error:n}}return e in this.flagValueDefaults?{flag:e,value:this.flagValueDefaults[e],reason:r,error:n}:{flag:e,value:!1,reason:r,error:n}}async checkFlag(e){let{fallback:t,key:r}=e,n=e.context||this.context,c=w(n);if(this.debug(`checkFlag: ${r}`,{context:n,fallback:t}),this.isOffline()){let o=this.resolveFallbackCheckFlagReturn(r,t,"Offline mode - using initialization defaults");return this.debug(`checkFlag offline result: ${r}`,{value:o.value,offlineMode:!0}),o.value}if(!this.useWebSocket){let o=`${this.apiUrl}/flags/${r}/check`;return fetch(o,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:JSON.stringify(n)}).then(l=>{if(!l.ok)throw new Error("Network response was not ok");return l.json()}).then(l=>{let u=_(l);this.debug(`checkFlag result: ${r}`,u);let h=O(u.data);return typeof h.featureUsageEvent=="string"&&this.updateFeatureUsageEventMap(h),this.submitFlagCheckEvent(r,h,n),h.value}).catch(l=>{console.warn("There was a problem with the fetch operation:",l);let u=this.resolveFallbackCheckFlagReturn(r,t,"API request failed",l instanceof Error?l.message:String(l));return this.submitFlagCheckEvent(r,u,n),u.value})}try{let o=this.checks[c];if(this.conn!==null&&typeof o<"u"&&typeof o[r]<"u")return this.debug(`checkFlag cached result: ${r}`,o[r]),o[r].value;if(this.isOffline())return this.resolveFallbackValue(r,t);try{await this.setContext(n)}catch(v){return console.warn("WebSocket connection failed, falling back to REST:",v),this.fallbackToRest(r,n,t)}let u=(this.checks[c]??{})[r],h=u?.value??this.resolveFallbackValue(r,t);return this.debug(`checkFlag WebSocket result: ${r}`,typeof u<"u"?u:{value:h,fallbackUsed:!0}),typeof u<"u"&&this.submitFlagCheckEvent(r,u,n),h}catch(o){console.error("Unexpected error in checkFlag:",o);let l=this.resolveFallbackCheckFlagReturn(r,t,"Unexpected error in flag check",o instanceof Error?o.message:String(o));return this.submitFlagCheckEvent(r,l,n),l.value}}debug(e,...t){this.debugEnabled&&console.log(`[Schematic] ${e}`,...t)}createPersistentMessageHandler(e){return t=>{let r=JSON.parse(t.data);this.debug("WebSocket persistent message received:",r),w(e)in this.checks||(this.checks[w(e)]={}),(r.flags??[]).forEach(n=>{let c=O(n),o=w(e);this.checks[o]===void 0&&(this.checks[o]={}),this.checks[o][c.flag]=c,this.debug("WebSocket flag update:",{flag:c.flag,value:c.value,flagCheck:c}),typeof c.featureUsageEvent=="string"&&this.updateFeatureUsageEventMap(c),((this.flagCheckListeners[n.flag]?.size??0)>0||(this.flagValueListeners[n.flag]?.size??0)>0)&&this.submitFlagCheckEvent(c.flag,c,e),this.debug(`About to notify listeners for flag ${n.flag}`,{flag:n.flag,value:c.value}),this.notifyFlagCheckListeners(n.flag,c),this.notifyFlagValueListeners(n.flag,c.value),this.debug(`Finished notifying listeners for flag ${n.flag}`,{flag:n.flag,value:c.value})}),this.flushContextDependentEventQueue(),this.setIsPending(!1)}}isOffline(){return this.offlineEnabled}submitFlagCheckEvent(e,t,r){let n={flagKey:e,value:t.value,reason:t.reason,flagId:t.flagId,ruleId:t.ruleId,companyId:t.companyId,userId:t.userId,error:t.error,reqCompany:r.company,reqUser:r.user};return this.debug("submitting flag check event:",n),this.handleEvent("flag_check",L(n))}async fallbackToRest(e,t,r){if(this.isOffline()){let n=this.resolveFallbackValue(e,r);return this.debug(`fallbackToRest offline result: ${e}`,{value:n,offlineMode:!0}),n}try{let n=`${this.apiUrl}/flags/${e}/check`,c=await fetch(n,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:JSON.stringify(t)});if(!c.ok)throw new Error("Network response was not ok");let o=await c.json(),l=_(o);this.debug(`fallbackToRest result: ${e}`,l);let u=O(l.data);return typeof u.featureUsageEvent=="string"&&this.updateFeatureUsageEventMap(u),this.submitFlagCheckEvent(e,u,t),u.value}catch(n){console.warn("REST API call failed, using fallback value:",n);let c=this.resolveFallbackCheckFlagReturn(e,r,"API request failed (fallback)",n instanceof Error?n.message:String(n));return this.submitFlagCheckEvent(e,c,t),c.value}}checkFlags=async e=>{if(e=e||this.context,this.debug("checkFlags",{context:e}),this.isOffline())return this.debug("checkFlags offline result: returning empty object"),{};let t=`${this.apiUrl}/flags/check`,r=JSON.stringify(e);return fetch(t,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:r}).then(n=>{if(!n.ok)throw new Error("Network response was not ok");return n.json()}).then(n=>{let c=M(n);return this.debug("checkFlags result:",c),(c?.data?.flags??[]).reduce((o,l)=>(o[l.flag]=l.value,o),{})}).catch(n=>(console.warn("There was a problem with the fetch operation:",n),{}))};identify=e=>(this.debug("identify:",e),this.setContext({company:e.company?.keys,user:e.keys}).catch(t=>{console.warn("Error setting context:",t)}),this.handleEvent("identify",e));setContext=async e=>{if(this.isOffline()||!this.useWebSocket)return this.context=e,this.flushContextDependentEventQueue(),this.setIsPending(!1),Promise.resolve();try{if(this.setIsPending(!0),!this.conn){if(this.isConnecting){for(this.debug("Connection already in progress, waiting for it to complete");this.isConnecting&&this.conn===null;)await new Promise(r=>setTimeout(r,10));if(this.conn!==null){let r=await this.conn;await this.wsSendMessage(r,e);return}}this.wsReconnectTimer!==null&&(this.debug("Cancelling scheduled reconnection, connecting immediately"),clearTimeout(this.wsReconnectTimer),this.wsReconnectTimer=null),this.isConnecting=!0;try{this.conn=this.wsConnect();let r=await this.conn;this.isConnecting=!1,await this.wsSendMessage(r,e);return}catch(r){throw this.isConnecting=!1,r}}let t=await this.conn;await this.wsSendMessage(t,e)}catch(t){throw console.warn("Failed to establish WebSocket connection:",t),t}};track=e=>{let{company:t,user:r,event:n,traits:c,quantity:o=1}=e;if(!this.hasContext(t,r)){this.debug(`track: queuing event "${n}" until context is available`);let u={api_key:this.apiKey,body:{company:t,event:n,traits:c??{},user:r,quantity:o},sent_at:new Date().toISOString(),tracker_event_id:C(),tracker_user_id:this.getAnonymousId(),type:"track"};return this.contextDependentEventQueue.push(u),Promise.resolve()}let l={company:t??this.context.company,event:n,traits:c??{},user:r??this.context.user,quantity:o};return this.debug("track:",l),n in this.featureUsageEventMap&&this.optimisticallyUpdateFeatureUsage(n,o),this.handleEvent("track",l)};optimisticallyUpdateFeatureUsage=(e,t=1)=>{let r=this.featureUsageEventMap[e];r!=null&&(this.debug(`Optimistically updating feature usage for event: ${e}`,{quantity:t}),Object.entries(r).forEach(([n,c])=>{if(c===void 0)return;let o={...c};if(typeof o.featureUsage=="number"){if(o.featureUsage+=t,typeof o.featureAllocation=="number"){let u=o.featureUsageExceeded===!0,h=o.featureUsage>=o.featureAllocation;h!==u&&(o.featureUsageExceeded=h,h&&(o.value=!1),this.debug(`Usage limit status changed for flag: ${n}`,{was:u?"exceeded":"within limits",now:h?"exceeded":"within limits",featureUsage:o.featureUsage,featureAllocation:o.featureAllocation,value:o.value}))}this.featureUsageEventMap[e]!==void 0&&(this.featureUsageEventMap[e][n]=o);let l=w(this.context);this.checks[l]!==void 0&&this.checks[l]!==null&&(this.checks[l][n]=o),this.notifyFlagCheckListeners(n,o),this.notifyFlagValueListeners(n,o.value)}}))};hasContext=(e,t)=>{let r=e!=null&&Object.keys(e).length>0||t!=null&&Object.keys(t).length>0,n=this.context.company!==void 0&&this.context.company!==null&&Object.keys(this.context.company).length>0||this.context.user!==void 0&&this.context.user!==null&&Object.keys(this.context.user).length>0;return r||n};flushContextDependentEventQueue=()=>{for(this.debug(`flushing ${this.contextDependentEventQueue.length} context-dependent events`);this.contextDependentEventQueue.length>0;){let e=this.contextDependentEventQueue.shift();if(e)if(e.type==="track"&&typeof e.body=="object"&&e.body!==null){let t=e.body,r={...t,company:t.company??this.context.company,user:t.user??this.context.user},n={...e,body:r,sent_at:new Date().toISOString()};this.sendEvent(n)}else this.sendEvent(e)}};startRetryTimer=()=>{this.retryTimer===null&&(this.retryTimer=setInterval(()=>{this.flushEventQueue().catch(e=>{this.debug("Error in retry timer flush:",e)}),this.eventQueue.length===0&&this.stopRetryTimer()},5e3),this.debug("Started retry timer"))};stopRetryTimer=()=>{this.retryTimer!==null&&(clearInterval(this.retryTimer),this.retryTimer=null,this.debug("Stopped retry timer"))};flushEventQueue=async()=>{if(this.eventQueue.length===0)return;let e=Date.now(),t=[],r=[];for(let n of this.eventQueue)n.next_retry_at===void 0||n.next_retry_at<=e?t.push(n):r.push(n);if(t.length===0){this.debug(`No events ready for retry yet (${r.length} still in backoff)`);return}this.debug(`Flushing event queue: ${t.length} ready, ${r.length} waiting`),this.eventQueue=r;for(let n of t)try{await this.sendEvent(n),this.debug("Queued event sent successfully:",n.type)}catch(c){this.debug("Failed to send queued event:",c)}};getAnonymousId=()=>{if(!this.storage)return C();let e=this.storage.getItem(K);if(typeof e<"u")return e;let t=C();return this.storage.setItem(K,t),t};handleEvent=(e,t)=>{let r={api_key:this.apiKey,body:t,sent_at:new Date().toISOString(),tracker_event_id:C(),tracker_user_id:this.getAnonymousId(),type:e};return typeof document<"u"&&document?.hidden?this.storeEvent(r):this.sendEvent(r)};sendEvent=async e=>{let t=`${this.eventUrl}/e`,r=JSON.stringify(e);if(this.debug("sending event:",{url:t,event:e}),this.isOffline())return this.debug("event not sent (offline mode):",{event:e}),Promise.resolve();try{let n=await fetch(t,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8"},body:r});if(!n.ok)throw new Error(`HTTP ${n.status}: ${n.statusText}`);this.debug("event sent:",{status:n.status,statusText:n.statusText})}catch(n){let c=(e.retry_count??0)+1;if(c<=this.maxEventRetries){this.debug(`Event failed to send (attempt ${c}/${this.maxEventRetries}), queueing for retry:`,n);let o=this.eventRetryInitialDelay*Math.pow(2,c-1),l=Math.min(o,this.eventRetryMaxDelay),u=Date.now()+l,h={...e,retry_count:c,next_retry_at:u};this.eventQueue.length<this.maxEventQueueSize?(this.eventQueue.push(h),this.debug(`Event queued for retry in ${l}ms (${this.eventQueue.length}/${this.maxEventQueueSize})`)):(this.debug(`Event queue full (${this.maxEventQueueSize}), dropping oldest event`),this.eventQueue.shift(),this.eventQueue.push(h)),this.startRetryTimer()}else this.debug(`Event failed permanently after ${this.maxEventRetries} attempts, dropping:`,n)}return Promise.resolve()};storeEvent=e=>(this.eventQueue.push(e),Promise.resolve());forceReconnect=async()=>this.reconnect({force:!0});reconnectIfNeeded=async()=>this.reconnect({force:!1});reconnect=async e=>{let{force:t}=e,r=t?"forceReconnect":"reconnectIfNeeded";if(this.isOffline())return this.debug(`${r}: skipped (offline mode)`),Promise.resolve();if(!t&&this.conn!==null)try{if((await this.conn).readyState===WebSocket.OPEN)return this.debug(`${r}: connection is healthy, skipping`),Promise.resolve()}catch{}if(this.debug(`${r}: ${t?"forcing immediate reconnection":"reconnecting"}`),this.wsIntentionalDisconnect=!1,this.wsReconnectTimer!==null&&(this.debug(`${r}: cancelling pending reconnection timer`),clearTimeout(this.wsReconnectTimer),this.wsReconnectTimer=null),this.wsReconnectAttempts=0,this.conn!==null){this.debug(`${r}: closing existing connection`);try{let n=await this.conn;this.currentWebSocket===n&&(this.currentWebSocket=null),(n.readyState===WebSocket.OPEN||n.readyState===WebSocket.CONNECTING)&&n.close()}catch(n){this.debug(`${r}: error closing existing connection:`,n)}this.conn=null,this.isConnecting=!1}if(this.context.company!==void 0||this.context.user!==void 0){this.debug(`${r}: reconnecting with existing context`);try{this.isConnecting=!0,this.conn=this.wsConnect();let n=await this.conn;this.isConnecting=!1,await this.wsSendMessage(n,this.context,!0),this.debug(`${r}: reconnection successful`)}catch(n){this.isConnecting=!1,this.debug(`${r}: reconnection failed:`,n)}}else this.debug(`${r}: no context set, skipping reconnection`);return Promise.resolve()};cleanup=async()=>{if(this.isOffline())return this.debug("cleanup: skipped (offline mode)"),Promise.resolve();if(this.wsIntentionalDisconnect=!0,this.wsReconnectTimer!==null&&(clearTimeout(this.wsReconnectTimer),this.wsReconnectTimer=null),this.stopRetryTimer(),this.conn)try{let e=await this.conn;this.currentWebSocket===e&&(this.debug("Cleaning up current websocket tracking"),this.currentWebSocket=null),e.close()}catch(e){console.warn("Error during cleanup:",e)}finally{this.conn=null,this.currentWebSocket=null,this.isConnecting=!1}};calculateReconnectDelay=()=>{let e=this.webSocketInitialRetryDelay*Math.pow(2,this.wsReconnectAttempts),t=Math.min(e,this.webSocketMaxRetryDelay),r=Math.random()*t*.5,n=t+r;return this.debug(`Reconnect delay calculated: ${n.toFixed(0)}ms (attempt ${this.wsReconnectAttempts+1}/${this.webSocketMaxReconnectAttempts})`),n};handleNetworkOffline=async()=>{if(this.conn!==null){try{let e=await this.conn;(e.readyState===WebSocket.OPEN||e.readyState===WebSocket.CONNECTING)&&e.close()}catch(e){this.debug("Error closing connection on offline:",e)}this.conn=null}this.wsReconnectTimer!==null&&(clearTimeout(this.wsReconnectTimer),this.wsReconnectTimer=null)};handleNetworkOnline=()=>{this.debug("Network online, attempting reconnection and flushing queued events"),this.wsReconnectAttempts=0,this.wsReconnectTimer!==null&&(clearTimeout(this.wsReconnectTimer),this.wsReconnectTimer=null),this.flushEventQueue().catch(e=>{this.debug("Error flushing event queue on network online:",e)}),this.attemptReconnect()};attemptReconnect=()=>{if(this.wsReconnectAttempts>=this.webSocketMaxReconnectAttempts){this.debug(`Maximum reconnection attempts (${this.webSocketMaxReconnectAttempts}) reached, giving up`);return}if(this.wsReconnectTimer!==null){this.debug("Reconnection attempt already scheduled, ignoring duplicate request");return}let e=this.calculateReconnectDelay();this.debug(`Scheduling reconnection attempt ${this.wsReconnectAttempts+1}/${this.webSocketMaxReconnectAttempts} in ${e.toFixed(0)}ms`),this.wsReconnectTimer=setTimeout(async()=>{this.wsReconnectTimer=null,this.wsReconnectAttempts++,this.debug(`Attempting to reconnect (attempt ${this.wsReconnectAttempts}/${this.webSocketMaxReconnectAttempts})`);try{if(this.conn!==null){this.debug("Cleaning up existing connection before reconnection");try{let t=await this.conn;this.currentWebSocket===t&&(this.debug("Existing websocket is current, will be replaced"),this.currentWebSocket=null),(t.readyState===WebSocket.OPEN||t.readyState===WebSocket.CONNECTING)&&t.close()}catch(t){this.debug("Error cleaning up existing connection:",t)}this.conn=null,this.currentWebSocket=null,this.isConnecting=!1}this.isConnecting=!0;try{this.conn=this.wsConnect();let t=await this.conn;this.isConnecting=!1,this.debug("Reconnection context check:",{hasCompany:this.context.company!==void 0,hasUser:this.context.user!==void 0,context:this.context}),this.context.company!==void 0||this.context.user!==void 0?(this.debug("Reconnected, force re-sending context"),await this.wsSendMessage(t,this.context,!0)):(this.debug("No context to re-send after reconnection - websocket ready for new context"),this.debug("Setting up tracking for reconnected websocket (no context to send)"),this.currentWebSocket=t),this.flushEventQueue().catch(r=>{this.debug("Error flushing event queue after websocket reconnection:",r)}),this.debug("Reconnection successful")}catch(t){throw this.isConnecting=!1,t}}catch(t){this.debug("Reconnection attempt failed:",t)}},e)};wsConnect=async()=>{if(this.isOffline())throw this.debug("wsConnect: skipped (offline mode)"),new Error("WebSocket connection skipped in offline mode");let e=null,t=this.webSocketMaxConnectionAttempts;for(let r=0;r<t;r++)try{let n=await this.wsConnectOnce();return this.wsReconnectAttempts=0,n}catch(n){if(e=n instanceof Error?n:new Error(String(n)),!(e.message==="WebSocket connection timeout"))throw this.debug("WebSocket connection failed with non-timeout error, not retrying:",e.message),e;if(r<t-1){let o=this.webSocketInitialRetryDelay*Math.pow(2,r),l=Math.min(o,this.webSocketMaxRetryDelay),u=l*.2*Math.random(),h=l+u;this.debug(`WebSocket connection timeout (attempt ${r+1}/${t}), retrying in ${h.toFixed(0)}ms`),await new Promise(v=>setTimeout(v,h))}else this.debug(`WebSocket connection timeout (attempt ${r+1}/${t}), no more retries`)}throw e??new Error("WebSocket connection failed")};wsConnectOnce=()=>new Promise((e,t)=>{let r=`${this.webSocketUrl}/flags/bootstrap?apiKey=${this.apiKey}`;this.debug("connecting to WebSocket:",r);let n=new WebSocket(r),c=Math.random().toString(36).substring(7);this.debug(`Creating WebSocket connection ${c} to ${r}`);let o=null,l=!1;o=setTimeout(()=>{l||(l=!0,this.debug(`WebSocket connection timeout after ${this.webSocketConnectionTimeout}ms`),n.close(),t(new Error("WebSocket connection timeout")))},this.webSocketConnectionTimeout),n.onopen=()=>{l||(l=!0,o!==null&&clearTimeout(o),this.wsIntentionalDisconnect=!1,this.debug(`WebSocket connection ${c} opened successfully`),e(n))},n.onerror=u=>{l||(l=!0,o!==null&&clearTimeout(o),this.debug(`WebSocket connection ${c} error:`,u),t(u))},n.onclose=()=>{o!==null&&clearTimeout(o),this.debug(`WebSocket connection ${c} closed`),this.conn=null,this.currentWebSocket===n&&(this.currentWebSocket=null,this.isConnecting=!1),!l&&!this.wsIntentionalDisconnect&&this.webSocketReconnect&&this.attemptReconnect()}});wsSendMessage=(e,t,r=!1)=>this.isOffline()?(this.debug("wsSendMessage: skipped (offline mode)"),this.setIsPending(!1),Promise.resolve()):new Promise((n,c)=>{if(!r&&w(t)==w(this.context))return this.debug("WebSocket context unchanged, skipping update"),n(this.setIsPending(!1));this.debug(r?"WebSocket force sending context (reconnection):":"WebSocket context updated:",t),this.context=t;let o=()=>{let l=!1,u=this.createPersistentMessageHandler(t),h=E=>{u(E),l||(l=!0,n())};e.addEventListener("message",h),this.currentWebSocket=e;let v=this.additionalHeaders["X-Schematic-Client-Version"]??`schematic-js@${$}`,p={apiKey:this.apiKey,clientVersion:v,data:t};this.debug("WebSocket sending message:",p),e.send(JSON.stringify(p))};e.readyState===WebSocket.OPEN?(this.debug("WebSocket already open, sending message"),o()):e.readyState===WebSocket.CONNECTING?(this.debug("WebSocket connecting, waiting for open to send message"),e.addEventListener("open",o)):(this.debug("WebSocket is closed, cannot send message"),c("WebSocket is not open or connecting"))});getIsPending=()=>this.isPending;addIsPendingListener=e=>(this.isPendingListeners.add(e),()=>{this.isPendingListeners.delete(e)});setIsPending=e=>{this.isPending=e,this.isPendingListeners.forEach(t=>Ee(t,e))};getFlagCheck=e=>{let t=w(this.context),n=(this.checks[t]??{})[e];if(n!==void 0)return n;if(e in this.flagCheckDefaults||e in this.flagValueDefaults)return this.resolveFallbackCheckFlagReturn(e,void 0,"Default value used")};getFlagValue=e=>{let t=w(this.context),n=(this.checks[t]??{})[e];if(n?.value!==void 0)return n.value;if(e in this.flagCheckDefaults||e in this.flagValueDefaults)return this.resolveFallbackValue(e)};addFlagValueListener=(e,t)=>(e in this.flagValueListeners||(this.flagValueListeners[e]=new Set),this.flagValueListeners[e].add(t),()=>{this.flagValueListeners[e].delete(t)});addFlagCheckListener=(e,t)=>(e in this.flagCheckListeners||(this.flagCheckListeners[e]=new Set),this.flagCheckListeners[e].add(t),()=>{this.flagCheckListeners[e].delete(t)});notifyFlagCheckListeners=(e,t)=>{let r=this.flagCheckListeners?.[e]??[];r.size>0&&this.debug(`Notifying ${r.size} flag check listeners for ${e}`,t),typeof t.featureUsageEvent=="string"&&this.updateFeatureUsageEventMap(t),r.forEach(n=>we(n,t))};updateFeatureUsageEventMap=e=>{if(typeof e.featureUsageEvent!="string")return;let t=e.featureUsageEvent;(this.featureUsageEventMap[t]===void 0||this.featureUsageEventMap[t]===null)&&(this.featureUsageEventMap[t]={}),this.featureUsageEventMap[t]!==void 0&&(this.featureUsageEventMap[t][e.flag]=e),this.debug(`Updated featureUsageEventMap for event: ${t}, flag: ${e.flag}`,e)};notifyFlagValueListeners=(e,t)=>{let r=this.flagValueListeners?.[e]??[];r.size>0&&this.debug(`Notifying ${r.size} flag value listeners for ${e}`,{value:t}),r.forEach((n,c)=>{this.debug(`Calling listener ${c} for flag ${e}`,{flagKey:e,value:t}),Re(n,t),this.debug(`Listener ${c} for flag ${e} completed`,{flagKey:e,value:t})})}},Ee=(s,e)=>{s.length>0?s(e):s()},we=(s,e)=>{s.length>0?s(e):s()},Re=(s,e)=>{s.length>0?s(e):s()};window.Schematic=U;})();
|
|
1
|
+
"use strict";(()=>{var re=Object.create;var Q=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var ae=Object.getPrototypeOf,oe=Object.prototype.hasOwnProperty;var ce=(s,e)=>()=>(e||s((e={exports:{}}).exports,e),e.exports);var le=(s,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of se(e))!oe.call(s,n)&&n!==t&&Q(s,n,{get:()=>e[n],enumerable:!(r=ie(e,n))||r.enumerable});return s};var ue=(s,e,t)=>(t=s!=null?re(ae(s)):{},le(e||!s||!s.__esModule?Q(t,"default",{value:s,enumerable:!0}):t,s));var G=ce(z=>{(function(s){var e=(function(t){var r=typeof globalThis<"u"&&globalThis||typeof s<"u"&&s||typeof global<"u"&&global||{},n={searchParams:"URLSearchParams"in r,iterable:"Symbol"in r&&"iterator"in Symbol,blob:"FileReader"in r&&"Blob"in r&&(function(){try{return new Blob,!0}catch{return!1}})(),formData:"FormData"in r,arrayBuffer:"ArrayBuffer"in r};function c(i){return i&&DataView.prototype.isPrototypeOf(i)}if(n.arrayBuffer)var o=["[object Int8Array]","[object Uint8Array]","[object Uint8ClampedArray]","[object Int16Array]","[object Uint16Array]","[object Int32Array]","[object Uint32Array]","[object Float32Array]","[object Float64Array]"],l=ArrayBuffer.isView||function(i){return i&&o.indexOf(Object.prototype.toString.call(i))>-1};function f(i){if(typeof i!="string"&&(i=String(i)),/[^a-z0-9\-#$%&'*+.^_`|~!]/i.test(i)||i==="")throw new TypeError('Invalid character in header field name: "'+i+'"');return i.toLowerCase()}function h(i){return typeof i!="string"&&(i=String(i)),i}function v(i){var a={next:function(){var u=i.shift();return{done:u===void 0,value:u}}};return n.iterable&&(a[Symbol.iterator]=function(){return a}),a}function g(i){this.map={},i instanceof g?i.forEach(function(a,u){this.append(u,a)},this):Array.isArray(i)?i.forEach(function(a){if(a.length!=2)throw new TypeError("Headers constructor: expected name/value pair to be length 2, found"+a.length);this.append(a[0],a[1])},this):i&&Object.getOwnPropertyNames(i).forEach(function(a){this.append(a,i[a])},this)}g.prototype.append=function(i,a){i=f(i),a=h(a);var u=this.map[i];this.map[i]=u?u+", "+a:a},g.prototype.delete=function(i){delete this.map[f(i)]},g.prototype.get=function(i){return i=f(i),this.has(i)?this.map[i]:null},g.prototype.has=function(i){return this.map.hasOwnProperty(f(i))},g.prototype.set=function(i,a){this.map[f(i)]=h(a)},g.prototype.forEach=function(i,a){for(var u in this.map)this.map.hasOwnProperty(u)&&i.call(a,this.map[u],u,this)},g.prototype.keys=function(){var i=[];return this.forEach(function(a,u){i.push(u)}),v(i)},g.prototype.values=function(){var i=[];return this.forEach(function(a){i.push(a)}),v(i)},g.prototype.entries=function(){var i=[];return this.forEach(function(a,u){i.push([u,a])}),v(i)},n.iterable&&(g.prototype[Symbol.iterator]=g.prototype.entries);function k(i){if(!i._noBody){if(i.bodyUsed)return Promise.reject(new TypeError("Already read"));i.bodyUsed=!0}}function F(i){return new Promise(function(a,u){i.onload=function(){a(i.result)},i.onerror=function(){u(i.error)}})}function T(i){var a=new FileReader,u=F(a);return a.readAsArrayBuffer(i),u}function W(i){var a=new FileReader,u=F(a),p=/charset=([A-Za-z0-9_-]+)/.exec(i.type),y=p?p[1]:"utf-8";return a.readAsText(i,y),u}function Y(i){for(var a=new Uint8Array(i),u=new Array(a.length),p=0;p<a.length;p++)u[p]=String.fromCharCode(a[p]);return u.join("")}function V(i){if(i.slice)return i.slice(0);var a=new Uint8Array(i.byteLength);return a.set(new Uint8Array(i)),a.buffer}function J(){return this.bodyUsed=!1,this._initBody=function(i){this.bodyUsed=this.bodyUsed,this._bodyInit=i,i?typeof i=="string"?this._bodyText=i:n.blob&&Blob.prototype.isPrototypeOf(i)?this._bodyBlob=i:n.formData&&FormData.prototype.isPrototypeOf(i)?this._bodyFormData=i:n.searchParams&&URLSearchParams.prototype.isPrototypeOf(i)?this._bodyText=i.toString():n.arrayBuffer&&n.blob&&c(i)?(this._bodyArrayBuffer=V(i.buffer),this._bodyInit=new Blob([this._bodyArrayBuffer])):n.arrayBuffer&&(ArrayBuffer.prototype.isPrototypeOf(i)||l(i))?this._bodyArrayBuffer=V(i):this._bodyText=i=Object.prototype.toString.call(i):(this._noBody=!0,this._bodyText=""),this.headers.get("content-type")||(typeof i=="string"?this.headers.set("content-type","text/plain;charset=UTF-8"):this._bodyBlob&&this._bodyBlob.type?this.headers.set("content-type",this._bodyBlob.type):n.searchParams&&URLSearchParams.prototype.isPrototypeOf(i)&&this.headers.set("content-type","application/x-www-form-urlencoded;charset=UTF-8"))},n.blob&&(this.blob=function(){var i=k(this);if(i)return i;if(this._bodyBlob)return Promise.resolve(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(new Blob([this._bodyArrayBuffer]));if(this._bodyFormData)throw new Error("could not read FormData body as blob");return Promise.resolve(new Blob([this._bodyText]))}),this.arrayBuffer=function(){if(this._bodyArrayBuffer){var i=k(this);return i||(ArrayBuffer.isView(this._bodyArrayBuffer)?Promise.resolve(this._bodyArrayBuffer.buffer.slice(this._bodyArrayBuffer.byteOffset,this._bodyArrayBuffer.byteOffset+this._bodyArrayBuffer.byteLength)):Promise.resolve(this._bodyArrayBuffer))}else{if(n.blob)return this.blob().then(T);throw new Error("could not read as ArrayBuffer")}},this.text=function(){var i=k(this);if(i)return i;if(this._bodyBlob)return W(this._bodyBlob);if(this._bodyArrayBuffer)return Promise.resolve(Y(this._bodyArrayBuffer));if(this._bodyFormData)throw new Error("could not read FormData body as text");return Promise.resolve(this._bodyText)},n.formData&&(this.formData=function(){return this.text().then(ee)}),this.json=function(){return this.text().then(JSON.parse)},this}var Z=["CONNECT","DELETE","GET","HEAD","OPTIONS","PATCH","POST","PUT","TRACE"];function j(i){var a=i.toUpperCase();return Z.indexOf(a)>-1?a:i}function R(i,a){if(!(this instanceof R))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');a=a||{};var u=a.body;if(i instanceof R){if(i.bodyUsed)throw new TypeError("Already read");this.url=i.url,this.credentials=i.credentials,a.headers||(this.headers=new g(i.headers)),this.method=i.method,this.mode=i.mode,this.signal=i.signal,!u&&i._bodyInit!=null&&(u=i._bodyInit,i.bodyUsed=!0)}else this.url=String(i);if(this.credentials=a.credentials||this.credentials||"same-origin",(a.headers||!this.headers)&&(this.headers=new g(a.headers)),this.method=j(a.method||this.method||"GET"),this.mode=a.mode||this.mode||null,this.signal=a.signal||this.signal||(function(){if("AbortController"in r){var d=new AbortController;return d.signal}})(),this.referrer=null,(this.method==="GET"||this.method==="HEAD")&&u)throw new TypeError("Body not allowed for GET or HEAD requests");if(this._initBody(u),(this.method==="GET"||this.method==="HEAD")&&(a.cache==="no-store"||a.cache==="no-cache")){var p=/([?&])_=[^&]*/;if(p.test(this.url))this.url=this.url.replace(p,"$1_="+new Date().getTime());else{var y=/\?/;this.url+=(y.test(this.url)?"&":"?")+"_="+new Date().getTime()}}}R.prototype.clone=function(){return new R(this,{body:this._bodyInit})};function ee(i){var a=new FormData;return i.trim().split("&").forEach(function(u){if(u){var p=u.split("="),y=p.shift().replace(/\+/g," "),d=p.join("=").replace(/\+/g," ");a.append(decodeURIComponent(y),decodeURIComponent(d))}}),a}function te(i){var a=new g,u=i.replace(/\r?\n[\t ]+/g," ");return u.split("\r").map(function(p){return p.indexOf(`
|
|
2
|
+
`)===0?p.substr(1,p.length):p}).forEach(function(p){var y=p.split(":"),d=y.shift().trim();if(d){var D=y.join(":").trim();try{a.append(d,D)}catch(U){console.warn("Response "+U.message)}}}),a}J.call(R.prototype);function E(i,a){if(!(this instanceof E))throw new TypeError('Please use the "new" operator, this DOM object constructor cannot be called as a function.');if(a||(a={}),this.type="default",this.status=a.status===void 0?200:a.status,this.status<200||this.status>599)throw new RangeError("Failed to construct 'Response': The status provided (0) is outside the range [200, 599].");this.ok=this.status>=200&&this.status<300,this.statusText=a.statusText===void 0?"":""+a.statusText,this.headers=new g(a.headers),this.url=a.url||"",this._initBody(i)}J.call(E.prototype),E.prototype.clone=function(){return new E(this._bodyInit,{status:this.status,statusText:this.statusText,headers:new g(this.headers),url:this.url})},E.error=function(){var i=new E(null,{status:200,statusText:""});return i.ok=!1,i.status=0,i.type="error",i};var ne=[301,302,303,307,308];E.redirect=function(i,a){if(ne.indexOf(a)===-1)throw new RangeError("Invalid status code");return new E(null,{status:a,headers:{location:i}})},t.DOMException=r.DOMException;try{new t.DOMException}catch{t.DOMException=function(a,u){this.message=a,this.name=u;var p=Error(a);this.stack=p.stack},t.DOMException.prototype=Object.create(Error.prototype),t.DOMException.prototype.constructor=t.DOMException}function O(i,a){return new Promise(function(u,p){var y=new R(i,a);if(y.signal&&y.signal.aborted)return p(new t.DOMException("Aborted","AbortError"));var d=new XMLHttpRequest;function D(){d.abort()}d.onload=function(){var m={statusText:d.statusText,headers:te(d.getAllResponseHeaders()||"")};y.url.indexOf("file://")===0&&(d.status<200||d.status>599)?m.status=200:m.status=d.status,m.url="responseURL"in d?d.responseURL:m.headers.get("X-Request-URL");var S="response"in d?d.response:d.responseText;setTimeout(function(){u(new E(S,m))},0)},d.onerror=function(){setTimeout(function(){p(new TypeError("Network request failed"))},0)},d.ontimeout=function(){setTimeout(function(){p(new TypeError("Network request timed out"))},0)},d.onabort=function(){setTimeout(function(){p(new t.DOMException("Aborted","AbortError"))},0)};function U(m){try{return m===""&&r.location.href?r.location.href:m}catch{return m}}if(d.open(y.method,U(y.url),!0),y.credentials==="include"?d.withCredentials=!0:y.credentials==="omit"&&(d.withCredentials=!1),"responseType"in d&&(n.blob?d.responseType="blob":n.arrayBuffer&&(d.responseType="arraybuffer")),a&&typeof a.headers=="object"&&!(a.headers instanceof g||r.Headers&&a.headers instanceof r.Headers)){var q=[];Object.getOwnPropertyNames(a.headers).forEach(function(m){q.push(f(m)),d.setRequestHeader(m,h(a.headers[m]))}),y.headers.forEach(function(m,S){q.indexOf(S)===-1&&d.setRequestHeader(S,m)})}else y.headers.forEach(function(m,S){d.setRequestHeader(S,m)});y.signal&&(y.signal.addEventListener("abort",D),d.onreadystatechange=function(){d.readyState===4&&y.signal.removeEventListener("abort",D)}),d.send(typeof y._bodyInit>"u"?null:y._bodyInit)})}return O.polyfill=!0,r.fetch||(r.fetch=O,r.Headers=g,r.Request=R,r.Response=E),t.Headers=g,t.Request=R,t.Response=E,t.fetch=O,t})({})})(typeof self<"u"?self:z)});var b=[];for(let s=0;s<256;++s)b.push((s+256).toString(16).slice(1));function H(s,e=0){return(b[s[e+0]]+b[s[e+1]]+b[s[e+2]]+b[s[e+3]]+"-"+b[s[e+4]]+b[s[e+5]]+"-"+b[s[e+6]]+b[s[e+7]]+"-"+b[s[e+8]]+b[s[e+9]]+"-"+b[s[e+10]]+b[s[e+11]]+b[s[e+12]]+b[s[e+13]]+b[s[e+14]]+b[s[e+15]]).toLowerCase()}var I,de=new Uint8Array(16);function A(){if(!I){if(typeof crypto>"u"||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");I=crypto.getRandomValues.bind(crypto)}return I(de)}var fe=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),P={randomUUID:fe};function he(s,e,t){s=s||{};let r=s.random??s.rng?.()??A();if(r.length<16)throw new Error("Random bytes length must be >= 16");if(r[6]=r[6]&15|64,r[8]=r[8]&63|128,e){if(t=t||0,t<0||t+16>e.length)throw new RangeError(`UUID byte range ${t}:${t+15} is out of buffer bounds`);for(let n=0;n<16;++n)e[t+n]=r[n];return e}return H(r)}function ge(s,e,t){return P.randomUUID&&!e&&!s?P.randomUUID():he(s,e,t)}var C=ge;var it=ue(G());function x(s){return pe(s,!1)}function pe(s,e){return s==null?s:{companyId:s.company_id==null?void 0:s.company_id,error:s.error==null?void 0:s.error,featureAllocation:s.feature_allocation==null?void 0:s.feature_allocation,featureUsage:s.feature_usage==null?void 0:s.feature_usage,featureUsageEvent:s.feature_usage_event==null?void 0:s.feature_usage_event,featureUsagePeriod:s.feature_usage_period==null?void 0:s.feature_usage_period,featureUsageResetAt:s.feature_usage_reset_at==null?void 0:new Date(s.feature_usage_reset_at),flag:s.flag,flagId:s.flag_id==null?void 0:s.flag_id,reason:s.reason,ruleId:s.rule_id==null?void 0:s.rule_id,ruleType:s.rule_type==null?void 0:s.rule_type,userId:s.user_id==null?void 0:s.user_id,value:s.value}}function N(s){return ye(s,!1)}function ye(s,e=!1){return s==null?s:{company_id:s.companyId,error:s.error,flag_id:s.flagId,flag_key:s.flagKey,reason:s.reason,req_company:s.reqCompany,req_user:s.reqUser,rule_id:s.ruleId,user_id:s.userId,value:s.value}}function L(s){return be(s,!1)}function be(s,e){return s==null?s:{data:x(s.data),params:s.params}}function X(s){return ve(s,!1)}function ve(s,e){return s==null?s:{flags:s.flags.map(x)}}function B(s){return ke(s,!1)}function ke(s,e){return s==null?s:{data:X(s.data),params:s.params}}var M=s=>{let{companyId:e,error:t,featureAllocation:r,featureUsage:n,featureUsageEvent:c,featureUsagePeriod:o,featureUsageResetAt:l,flag:f,flagId:h,reason:v,ruleId:g,ruleType:k,userId:F,value:T}=x(s);return{featureUsageExceeded:!T&&(k=="company_override_usage_exceeded"||k=="plan_entitlement_usage_exceeded"),companyId:e??void 0,error:t??void 0,featureAllocation:r??void 0,featureUsage:n??void 0,featureUsageEvent:c===null?void 0:c,featureUsagePeriod:o??void 0,featureUsageResetAt:l??void 0,flag:f,flagId:h??void 0,reason:v,ruleId:g??void 0,ruleType:k??void 0,userId:F??void 0,value:T}};function w(s){let e=Object.keys(s).reduce((t,r)=>{let c=Object.keys(s[r]||{}).sort().reduce((o,l)=>(o[l]=s[r][l],o),{});return t[r]=c,t},{});return JSON.stringify(e)}var $="1.2.17";var K="schematicId";var _=class{additionalHeaders={};apiKey;apiUrl="https://api.schematichq.com";conn=null;context={};debugEnabled=!1;offlineEnabled=!1;eventQueue;contextDependentEventQueue;eventUrl="https://c.schematichq.com";flagCheckListeners={};flagValueListeners={};isPending=!0;isPendingListeners=new Set;storage;useWebSocket=!1;checks={};featureUsageEventMap={};webSocketUrl="wss://api.schematichq.com";webSocketConnectionTimeout=1e4;webSocketReconnect=!0;webSocketMaxReconnectAttempts=7;webSocketMaxConnectionAttempts=3;webSocketInitialRetryDelay=1e3;webSocketMaxRetryDelay=3e4;wsReconnectAttempts=0;wsReconnectTimer=null;wsIntentionalDisconnect=!1;currentWebSocket=null;isConnecting=!1;maxEventQueueSize=100;maxEventRetries=5;eventRetryInitialDelay=1e3;eventRetryMaxDelay=3e4;retryTimer=null;flagValueDefaults={};flagCheckDefaults={};constructor(e,t){if(this.apiKey=e,this.eventQueue=[],this.contextDependentEventQueue=[],this.useWebSocket=t?.useWebSocket??!1,this.debugEnabled=t?.debug??!1,this.offlineEnabled=t?.offline??!1,typeof window<"u"&&typeof window.location<"u"){let r=new URLSearchParams(window.location.search),n=r.get("schematic_debug");n!==null&&(n===""||n==="true"||n==="1")&&(this.debugEnabled=!0);let c=r.get("schematic_offline");c!==null&&(c===""||c==="true"||c==="1")&&(this.offlineEnabled=!0,this.debugEnabled=!0)}if(this.offlineEnabled&&t?.debug!==!1&&(this.debugEnabled=!0),this.offlineEnabled&&this.setIsPending(!1),this.additionalHeaders={"X-Schematic-Client-Version":`schematic-js@${$}`,...t?.additionalHeaders??{}},t?.storage)this.storage=t.storage;else try{typeof localStorage<"u"&&(this.storage=localStorage)}catch{}t?.apiUrl!==void 0&&(this.apiUrl=t.apiUrl),t?.eventUrl!==void 0&&(this.eventUrl=t.eventUrl),t?.webSocketUrl!==void 0&&(this.webSocketUrl=t.webSocketUrl),t?.webSocketConnectionTimeout!==void 0&&(this.webSocketConnectionTimeout=t.webSocketConnectionTimeout),t?.webSocketReconnect!==void 0&&(this.webSocketReconnect=t.webSocketReconnect),t?.webSocketMaxReconnectAttempts!==void 0&&(this.webSocketMaxReconnectAttempts=t.webSocketMaxReconnectAttempts),t?.webSocketInitialRetryDelay!==void 0&&(this.webSocketInitialRetryDelay=t.webSocketInitialRetryDelay),t?.webSocketMaxRetryDelay!==void 0&&(this.webSocketMaxRetryDelay=t.webSocketMaxRetryDelay),t?.maxEventQueueSize!==void 0&&(this.maxEventQueueSize=t.maxEventQueueSize),t?.maxEventRetries!==void 0&&(this.maxEventRetries=t.maxEventRetries),t?.eventRetryInitialDelay!==void 0&&(this.eventRetryInitialDelay=t.eventRetryInitialDelay),t?.eventRetryMaxDelay!==void 0&&(this.eventRetryMaxDelay=t.eventRetryMaxDelay),t?.flagValueDefaults!==void 0&&(this.flagValueDefaults=t.flagValueDefaults),t?.flagCheckDefaults!==void 0&&(this.flagCheckDefaults=t.flagCheckDefaults),typeof window<"u"&&window?.addEventListener&&(window.addEventListener("beforeunload",()=>{this.flushEventQueue(),this.flushContextDependentEventQueue()}),this.useWebSocket&&(window.addEventListener("offline",()=>{this.debug("Browser went offline, closing WebSocket connection"),this.handleNetworkOffline()}),window.addEventListener("online",()=>{this.debug("Browser came online, attempting to reconnect WebSocket"),this.handleNetworkOnline()}))),this.offlineEnabled?this.debug("Initialized with offline mode enabled - no network requests will be made"):this.debugEnabled&&this.debug("Initialized with debug mode enabled")}resolveFallbackValue(e,t){return t!==void 0?t:e in this.flagCheckDefaults?this.flagCheckDefaults[e].value:e in this.flagValueDefaults?this.flagValueDefaults[e]:!1}resolveFallbackCheckFlagReturn(e,t,r="Fallback value used",n){if(t!==void 0)return{flag:e,value:t,reason:r,error:n};if(e in this.flagCheckDefaults){let c=this.flagCheckDefaults[e];return{...c,flag:e,reason:n!==void 0?r:c.reason,error:n}}return e in this.flagValueDefaults?{flag:e,value:this.flagValueDefaults[e],reason:r,error:n}:{flag:e,value:!1,reason:r,error:n}}async checkFlag(e){let{fallback:t,key:r}=e,n=e.context||this.context,c=w(n);if(this.debug(`checkFlag: ${r}`,{context:n,fallback:t}),this.isOffline()){let o=this.resolveFallbackCheckFlagReturn(r,t,"Offline mode - using initialization defaults");return this.debug(`checkFlag offline result: ${r}`,{value:o.value,offlineMode:!0}),o.value}if(!this.useWebSocket){let o=`${this.apiUrl}/flags/${r}/check`;return fetch(o,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:JSON.stringify(n)}).then(l=>{if(!l.ok)throw new Error("Network response was not ok");return l.json()}).then(l=>{let f=L(l);this.debug(`checkFlag result: ${r}`,f);let h=M(f.data);return typeof h.featureUsageEvent=="string"&&this.updateFeatureUsageEventMap(h),this.submitFlagCheckEvent(r,h,n),h.value}).catch(l=>{console.warn("There was a problem with the fetch operation:",l);let f=this.resolveFallbackCheckFlagReturn(r,t,"API request failed",l instanceof Error?l.message:String(l));return this.submitFlagCheckEvent(r,f,n),f.value})}try{let o=this.checks[c];if(this.conn!==null&&typeof o<"u"&&typeof o[r]<"u")return this.debug(`checkFlag cached result: ${r}`,o[r]),o[r].value;if(this.isOffline())return this.resolveFallbackValue(r,t);try{await this.setContext(n)}catch(v){console.warn("WebSocket connection failed, using fallback value:",v);let g=this.resolveFallbackCheckFlagReturn(r,t,"WebSocket connection failed",v instanceof Error?v.message:String(v));return this.submitFlagCheckEvent(r,g,n),g.value}let f=(this.checks[c]??{})[r],h=f?.value??this.resolveFallbackValue(r,t);return this.debug(`checkFlag WebSocket result: ${r}`,typeof f<"u"?f:{value:h,fallbackUsed:!0}),typeof f<"u"&&this.submitFlagCheckEvent(r,f,n),h}catch(o){console.error("Unexpected error in checkFlag:",o);let l=this.resolveFallbackCheckFlagReturn(r,t,"Unexpected error in flag check",o instanceof Error?o.message:String(o));return this.submitFlagCheckEvent(r,l,n),l.value}}debug(e,...t){this.debugEnabled&&console.log(`[Schematic] ${e}`,...t)}createPersistentMessageHandler(e){return t=>{let r=JSON.parse(t.data);this.debug("WebSocket persistent message received:",r),w(e)in this.checks||(this.checks[w(e)]={}),(r.flags??[]).forEach(n=>{let c=M(n),o=w(e);this.checks[o]===void 0&&(this.checks[o]={}),this.checks[o][c.flag]=c,this.debug("WebSocket flag update:",{flag:c.flag,value:c.value,flagCheck:c}),typeof c.featureUsageEvent=="string"&&this.updateFeatureUsageEventMap(c),((this.flagCheckListeners[n.flag]?.size??0)>0||(this.flagValueListeners[n.flag]?.size??0)>0)&&this.submitFlagCheckEvent(c.flag,c,e),this.debug(`About to notify listeners for flag ${n.flag}`,{flag:n.flag,value:c.value}),this.notifyFlagCheckListeners(n.flag,c),this.notifyFlagValueListeners(n.flag,c.value),this.debug(`Finished notifying listeners for flag ${n.flag}`,{flag:n.flag,value:c.value})}),this.flushContextDependentEventQueue(),this.setIsPending(!1)}}isOffline(){return this.offlineEnabled}submitFlagCheckEvent(e,t,r){let n={flagKey:e,value:t.value,reason:t.reason,flagId:t.flagId,ruleId:t.ruleId,companyId:t.companyId,userId:t.userId,error:t.error,reqCompany:r.company,reqUser:r.user};return this.debug("submitting flag check event:",n),this.handleEvent("flag_check",N(n))}checkFlags=async e=>{if(e=e||this.context,this.debug("checkFlags",{context:e}),this.isOffline())return this.debug("checkFlags offline result: returning empty object"),{};let t=`${this.apiUrl}/flags/check`,r=JSON.stringify(e);return fetch(t,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8","X-Schematic-Api-Key":this.apiKey},body:r}).then(n=>{if(!n.ok)throw new Error("Network response was not ok");return n.json()}).then(n=>{let c=B(n);return this.debug("checkFlags result:",c),(c?.data?.flags??[]).reduce((o,l)=>(o[l.flag]=l.value,o),{})}).catch(n=>(console.warn("There was a problem with the fetch operation:",n),{}))};identify=e=>(this.debug("identify:",e),this.setContext({company:e.company?.keys,user:e.keys}).catch(t=>{console.warn("Error setting context:",t)}),this.handleEvent("identify",e));setContext=async e=>{if(this.isOffline()||!this.useWebSocket)return this.context=e,this.flushContextDependentEventQueue(),this.setIsPending(!1),Promise.resolve();try{if(this.setIsPending(!0),!this.conn){if(this.isConnecting){for(this.debug("Connection already in progress, waiting for it to complete");this.isConnecting&&this.conn===null;)await new Promise(r=>setTimeout(r,10));if(this.conn!==null){let r=await this.conn;await this.wsSendMessage(r,e);return}}this.wsReconnectTimer!==null&&(this.debug("Cancelling scheduled reconnection, connecting immediately"),clearTimeout(this.wsReconnectTimer),this.wsReconnectTimer=null),this.isConnecting=!0;try{this.conn=this.wsConnect();let r=await this.conn;this.isConnecting=!1,await this.wsSendMessage(r,e);return}catch(r){throw this.isConnecting=!1,r}}let t=await this.conn;await this.wsSendMessage(t,e)}catch(t){throw console.warn("Failed to establish WebSocket connection:",t),t}};track=e=>{let{company:t,user:r,event:n,traits:c,quantity:o=1}=e;if(!this.hasContext(t,r)){this.debug(`track: queuing event "${n}" until context is available`);let f={api_key:this.apiKey,body:{company:t,event:n,traits:c??{},user:r,quantity:o},sent_at:new Date().toISOString(),tracker_event_id:C(),tracker_user_id:this.getAnonymousId(),type:"track"};return this.contextDependentEventQueue.push(f),Promise.resolve()}let l={company:t??this.context.company,event:n,traits:c??{},user:r??this.context.user,quantity:o};return this.debug("track:",l),n in this.featureUsageEventMap&&this.optimisticallyUpdateFeatureUsage(n,o),this.handleEvent("track",l)};optimisticallyUpdateFeatureUsage=(e,t=1)=>{let r=this.featureUsageEventMap[e];r!=null&&(this.debug(`Optimistically updating feature usage for event: ${e}`,{quantity:t}),Object.entries(r).forEach(([n,c])=>{if(c===void 0)return;let o={...c};if(typeof o.featureUsage=="number"){if(o.featureUsage+=t,typeof o.featureAllocation=="number"){let f=o.featureUsageExceeded===!0,h=o.featureUsage>=o.featureAllocation;h!==f&&(o.featureUsageExceeded=h,h&&(o.value=!1),this.debug(`Usage limit status changed for flag: ${n}`,{was:f?"exceeded":"within limits",now:h?"exceeded":"within limits",featureUsage:o.featureUsage,featureAllocation:o.featureAllocation,value:o.value}))}this.featureUsageEventMap[e]!==void 0&&(this.featureUsageEventMap[e][n]=o);let l=w(this.context);this.checks[l]!==void 0&&this.checks[l]!==null&&(this.checks[l][n]=o),this.notifyFlagCheckListeners(n,o),this.notifyFlagValueListeners(n,o.value)}}))};hasContext=(e,t)=>{let r=e!=null&&Object.keys(e).length>0||t!=null&&Object.keys(t).length>0,n=this.context.company!==void 0&&this.context.company!==null&&Object.keys(this.context.company).length>0||this.context.user!==void 0&&this.context.user!==null&&Object.keys(this.context.user).length>0;return r||n};flushContextDependentEventQueue=()=>{for(this.debug(`flushing ${this.contextDependentEventQueue.length} context-dependent events`);this.contextDependentEventQueue.length>0;){let e=this.contextDependentEventQueue.shift();if(e)if(e.type==="track"&&typeof e.body=="object"&&e.body!==null){let t=e.body,r={...t,company:t.company??this.context.company,user:t.user??this.context.user},n={...e,body:r,sent_at:new Date().toISOString()};this.sendEvent(n)}else this.sendEvent(e)}};startRetryTimer=()=>{this.retryTimer===null&&(this.retryTimer=setInterval(()=>{this.flushEventQueue().catch(e=>{this.debug("Error in retry timer flush:",e)}),this.eventQueue.length===0&&this.stopRetryTimer()},5e3),this.debug("Started retry timer"))};stopRetryTimer=()=>{this.retryTimer!==null&&(clearInterval(this.retryTimer),this.retryTimer=null,this.debug("Stopped retry timer"))};flushEventQueue=async()=>{if(this.eventQueue.length===0)return;let e=Date.now(),t=[],r=[];for(let n of this.eventQueue)n.next_retry_at===void 0||n.next_retry_at<=e?t.push(n):r.push(n);if(t.length===0){this.debug(`No events ready for retry yet (${r.length} still in backoff)`);return}this.debug(`Flushing event queue: ${t.length} ready, ${r.length} waiting`),this.eventQueue=r;for(let n of t)try{await this.sendEvent(n),this.debug("Queued event sent successfully:",n.type)}catch(c){this.debug("Failed to send queued event:",c)}};getAnonymousId=()=>{if(!this.storage)return C();let e=this.storage.getItem(K);if(typeof e<"u")return e;let t=C();return this.storage.setItem(K,t),t};handleEvent=(e,t)=>{let r={api_key:this.apiKey,body:t,sent_at:new Date().toISOString(),tracker_event_id:C(),tracker_user_id:this.getAnonymousId(),type:e};return typeof document<"u"&&document?.hidden?this.storeEvent(r):this.sendEvent(r)};sendEvent=async e=>{let t=`${this.eventUrl}/e`,r=JSON.stringify(e);if(this.debug("sending event:",{url:t,event:e}),this.isOffline())return this.debug("event not sent (offline mode):",{event:e}),Promise.resolve();try{let n=await fetch(t,{method:"POST",headers:{...this.additionalHeaders??{},"Content-Type":"application/json;charset=UTF-8"},body:r});if(!n.ok)throw new Error(`HTTP ${n.status}: ${n.statusText}`);this.debug("event sent:",{status:n.status,statusText:n.statusText})}catch(n){let c=(e.retry_count??0)+1;if(c<=this.maxEventRetries){this.debug(`Event failed to send (attempt ${c}/${this.maxEventRetries}), queueing for retry:`,n);let o=this.eventRetryInitialDelay*Math.pow(2,c-1),l=Math.min(o,this.eventRetryMaxDelay),f=Date.now()+l,h={...e,retry_count:c,next_retry_at:f};this.eventQueue.length<this.maxEventQueueSize?(this.eventQueue.push(h),this.debug(`Event queued for retry in ${l}ms (${this.eventQueue.length}/${this.maxEventQueueSize})`)):(this.debug(`Event queue full (${this.maxEventQueueSize}), dropping oldest event`),this.eventQueue.shift(),this.eventQueue.push(h)),this.startRetryTimer()}else this.debug(`Event failed permanently after ${this.maxEventRetries} attempts, dropping:`,n)}return Promise.resolve()};storeEvent=e=>(this.eventQueue.push(e),Promise.resolve());forceReconnect=async()=>this.reconnect({force:!0});reconnectIfNeeded=async()=>this.reconnect({force:!1});reconnect=async e=>{let{force:t}=e,r=t?"forceReconnect":"reconnectIfNeeded";if(this.isOffline())return this.debug(`${r}: skipped (offline mode)`),Promise.resolve();if(!t&&this.conn!==null)try{if((await this.conn).readyState===WebSocket.OPEN)return this.debug(`${r}: connection is healthy, skipping`),Promise.resolve()}catch{}if(this.debug(`${r}: ${t?"forcing immediate reconnection":"reconnecting"}`),this.wsIntentionalDisconnect=!1,this.wsReconnectTimer!==null&&(this.debug(`${r}: cancelling pending reconnection timer`),clearTimeout(this.wsReconnectTimer),this.wsReconnectTimer=null),this.wsReconnectAttempts=0,this.conn!==null){this.debug(`${r}: closing existing connection`);try{let n=await this.conn;this.currentWebSocket===n&&(this.currentWebSocket=null),(n.readyState===WebSocket.OPEN||n.readyState===WebSocket.CONNECTING)&&n.close()}catch(n){this.debug(`${r}: error closing existing connection:`,n)}this.conn=null,this.isConnecting=!1}if(this.context.company!==void 0||this.context.user!==void 0){this.debug(`${r}: reconnecting with existing context`);try{this.isConnecting=!0,this.conn=this.wsConnect();let n=await this.conn;this.isConnecting=!1,await this.wsSendMessage(n,this.context,!0),this.debug(`${r}: reconnection successful`)}catch(n){this.isConnecting=!1,this.debug(`${r}: reconnection failed:`,n)}}else this.debug(`${r}: no context set, skipping reconnection`);return Promise.resolve()};cleanup=async()=>{if(this.isOffline())return this.debug("cleanup: skipped (offline mode)"),Promise.resolve();if(this.wsIntentionalDisconnect=!0,this.wsReconnectTimer!==null&&(clearTimeout(this.wsReconnectTimer),this.wsReconnectTimer=null),this.stopRetryTimer(),this.conn)try{let e=await this.conn;this.currentWebSocket===e&&(this.debug("Cleaning up current websocket tracking"),this.currentWebSocket=null),e.close()}catch(e){console.warn("Error during cleanup:",e)}finally{this.conn=null,this.currentWebSocket=null,this.isConnecting=!1}};calculateReconnectDelay=()=>{let e=this.webSocketInitialRetryDelay*Math.pow(2,this.wsReconnectAttempts),t=Math.min(e,this.webSocketMaxRetryDelay),r=Math.random()*t*.5,n=t+r;return this.debug(`Reconnect delay calculated: ${n.toFixed(0)}ms (attempt ${this.wsReconnectAttempts+1}/${this.webSocketMaxReconnectAttempts})`),n};handleNetworkOffline=async()=>{if(this.conn!==null){try{let e=await this.conn;(e.readyState===WebSocket.OPEN||e.readyState===WebSocket.CONNECTING)&&e.close()}catch(e){this.debug("Error closing connection on offline:",e)}this.conn=null}this.wsReconnectTimer!==null&&(clearTimeout(this.wsReconnectTimer),this.wsReconnectTimer=null)};handleNetworkOnline=()=>{this.debug("Network online, attempting reconnection and flushing queued events"),this.wsReconnectAttempts=0,this.wsReconnectTimer!==null&&(clearTimeout(this.wsReconnectTimer),this.wsReconnectTimer=null),this.flushEventQueue().catch(e=>{this.debug("Error flushing event queue on network online:",e)}),this.attemptReconnect()};attemptReconnect=()=>{if(this.wsReconnectAttempts>=this.webSocketMaxReconnectAttempts){this.debug(`Maximum reconnection attempts (${this.webSocketMaxReconnectAttempts}) reached, giving up`);return}if(this.wsReconnectTimer!==null){this.debug("Reconnection attempt already scheduled, ignoring duplicate request");return}let e=this.calculateReconnectDelay();this.debug(`Scheduling reconnection attempt ${this.wsReconnectAttempts+1}/${this.webSocketMaxReconnectAttempts} in ${e.toFixed(0)}ms`),this.wsReconnectTimer=setTimeout(async()=>{this.wsReconnectTimer=null,this.wsReconnectAttempts++,this.debug(`Attempting to reconnect (attempt ${this.wsReconnectAttempts}/${this.webSocketMaxReconnectAttempts})`);try{if(this.conn!==null){this.debug("Cleaning up existing connection before reconnection");try{let t=await this.conn;this.currentWebSocket===t&&(this.debug("Existing websocket is current, will be replaced"),this.currentWebSocket=null),(t.readyState===WebSocket.OPEN||t.readyState===WebSocket.CONNECTING)&&t.close()}catch(t){this.debug("Error cleaning up existing connection:",t)}this.conn=null,this.currentWebSocket=null,this.isConnecting=!1}this.isConnecting=!0;try{this.conn=this.wsConnect();let t=await this.conn;this.isConnecting=!1,this.debug("Reconnection context check:",{hasCompany:this.context.company!==void 0,hasUser:this.context.user!==void 0,context:this.context}),this.context.company!==void 0||this.context.user!==void 0?(this.debug("Reconnected, force re-sending context"),await this.wsSendMessage(t,this.context,!0)):(this.debug("No context to re-send after reconnection - websocket ready for new context"),this.debug("Setting up tracking for reconnected websocket (no context to send)"),this.currentWebSocket=t),this.flushEventQueue().catch(r=>{this.debug("Error flushing event queue after websocket reconnection:",r)}),this.debug("Reconnection successful")}catch(t){throw this.isConnecting=!1,t}}catch(t){this.debug("Reconnection attempt failed:",t)}},e)};wsConnect=async()=>{if(this.isOffline())throw this.debug("wsConnect: skipped (offline mode)"),new Error("WebSocket connection skipped in offline mode");let e=null,t=this.webSocketMaxConnectionAttempts;for(let r=0;r<t;r++)try{let n=await this.wsConnectOnce();return this.wsReconnectAttempts=0,n}catch(n){if(e=n instanceof Error?n:new Error(String(n)),!(e.message==="WebSocket connection timeout"))throw this.debug("WebSocket connection failed with non-timeout error, not retrying:",e.message),e;if(r<t-1){let o=this.webSocketInitialRetryDelay*Math.pow(2,r),l=Math.min(o,this.webSocketMaxRetryDelay),f=l*.2*Math.random(),h=l+f;this.debug(`WebSocket connection timeout (attempt ${r+1}/${t}), retrying in ${h.toFixed(0)}ms`),await new Promise(v=>setTimeout(v,h))}else this.debug(`WebSocket connection timeout (attempt ${r+1}/${t}), no more retries`)}throw e??new Error("WebSocket connection failed")};wsConnectOnce=()=>new Promise((e,t)=>{let r=`${this.webSocketUrl}/flags/bootstrap?apiKey=${this.apiKey}`;this.debug("connecting to WebSocket:",r);let n=new WebSocket(r),c=Math.random().toString(36).substring(7);this.debug(`Creating WebSocket connection ${c} to ${r}`);let o=null,l=!1;o=setTimeout(()=>{l||(l=!0,this.debug(`WebSocket connection timeout after ${this.webSocketConnectionTimeout}ms`),n.close(),t(new Error("WebSocket connection timeout")))},this.webSocketConnectionTimeout),n.onopen=()=>{l||(l=!0,o!==null&&clearTimeout(o),this.wsIntentionalDisconnect=!1,this.debug(`WebSocket connection ${c} opened successfully`),e(n))},n.onerror=f=>{if(l)return;l=!0,o!==null&&clearTimeout(o),this.debug(`WebSocket connection ${c} error:`,f);let h=new Error("WebSocket connection failed during handshake");t(h)},n.onclose=()=>{o!==null&&clearTimeout(o),this.debug(`WebSocket connection ${c} closed`),this.conn=null,this.currentWebSocket===n&&(this.currentWebSocket=null,this.isConnecting=!1),!l&&!this.wsIntentionalDisconnect&&this.webSocketReconnect&&this.attemptReconnect()}});wsSendMessage=(e,t,r=!1)=>this.isOffline()?(this.debug("wsSendMessage: skipped (offline mode)"),this.setIsPending(!1),Promise.resolve()):new Promise((n,c)=>{if(!r&&w(t)==w(this.context))return this.debug("WebSocket context unchanged, skipping update"),n(this.setIsPending(!1));this.debug(r?"WebSocket force sending context (reconnection):":"WebSocket context updated:",t),this.context=t;let o=()=>{let l=!1,f=this.createPersistentMessageHandler(t),h=k=>{f(k),l||(l=!0,n())};e.addEventListener("message",h),e.addEventListener("close",k=>{l||(l=!0,k.code===4001?c(new Error(`Authentication failed: ${k.reason!==""?k.reason:"Invalid API key"}`)):c(new Error("WebSocket connection closed unexpectedly")))}),this.currentWebSocket=e;let v=this.additionalHeaders["X-Schematic-Client-Version"]??`schematic-js@${$}`,g={apiKey:this.apiKey,clientVersion:v,data:t};this.debug("WebSocket sending message:",g),e.send(JSON.stringify(g))};e.readyState===WebSocket.OPEN?(this.debug("WebSocket already open, sending message"),o()):e.readyState===WebSocket.CONNECTING?(this.debug("WebSocket connecting, waiting for open to send message"),e.addEventListener("open",o)):(this.debug("WebSocket is closed, cannot send message"),c("WebSocket is not open or connecting"))});getIsPending=()=>this.isPending;addIsPendingListener=e=>(this.isPendingListeners.add(e),()=>{this.isPendingListeners.delete(e)});setIsPending=e=>{this.isPending=e,this.isPendingListeners.forEach(t=>Ee(t,e))};getFlagCheck=e=>{let t=w(this.context),n=(this.checks[t]??{})[e];if(n!==void 0)return n;if(e in this.flagCheckDefaults||e in this.flagValueDefaults)return this.resolveFallbackCheckFlagReturn(e,void 0,"Default value used")};getFlagValue=e=>{let t=w(this.context),n=(this.checks[t]??{})[e];if(n?.value!==void 0)return n.value;if(e in this.flagCheckDefaults||e in this.flagValueDefaults)return this.resolveFallbackValue(e)};addFlagValueListener=(e,t)=>(e in this.flagValueListeners||(this.flagValueListeners[e]=new Set),this.flagValueListeners[e].add(t),()=>{this.flagValueListeners[e].delete(t)});addFlagCheckListener=(e,t)=>(e in this.flagCheckListeners||(this.flagCheckListeners[e]=new Set),this.flagCheckListeners[e].add(t),()=>{this.flagCheckListeners[e].delete(t)});notifyFlagCheckListeners=(e,t)=>{let r=this.flagCheckListeners?.[e]??[];r.size>0&&this.debug(`Notifying ${r.size} flag check listeners for ${e}`,t),typeof t.featureUsageEvent=="string"&&this.updateFeatureUsageEventMap(t),r.forEach(n=>we(n,t))};updateFeatureUsageEventMap=e=>{if(typeof e.featureUsageEvent!="string")return;let t=e.featureUsageEvent;(this.featureUsageEventMap[t]===void 0||this.featureUsageEventMap[t]===null)&&(this.featureUsageEventMap[t]={}),this.featureUsageEventMap[t]!==void 0&&(this.featureUsageEventMap[t][e.flag]=e),this.debug(`Updated featureUsageEventMap for event: ${t}, flag: ${e.flag}`,e)};notifyFlagValueListeners=(e,t)=>{let r=this.flagValueListeners?.[e]??[];r.size>0&&this.debug(`Notifying ${r.size} flag value listeners for ${e}`,{value:t}),r.forEach((n,c)=>{this.debug(`Calling listener ${c} for flag ${e}`,{flagKey:e,value:t}),Re(n,t),this.debug(`Listener ${c} for flag ${e} completed`,{flagKey:e,value:t})})}},Ee=(s,e)=>{s.length>0?s(e):s()},we=(s,e)=>{s.length>0?s(e):s()},Re=(s,e)=>{s.length>0?s(e):s()};window.Schematic=_;})();
|
|
3
3
|
/* @preserve */
|
package/dist/schematic.cjs.js
CHANGED
|
@@ -800,7 +800,7 @@ function contextString(context) {
|
|
|
800
800
|
}
|
|
801
801
|
|
|
802
802
|
// src/version.ts
|
|
803
|
-
var version = "1.2.
|
|
803
|
+
var version = "1.2.17";
|
|
804
804
|
|
|
805
805
|
// src/index.ts
|
|
806
806
|
var anonymousIdKey = "schematicId";
|
|
@@ -1016,8 +1016,8 @@ var Schematic = class {
|
|
|
1016
1016
|
/**
|
|
1017
1017
|
* Get value for a single flag.
|
|
1018
1018
|
* In WebSocket mode, returns cached values if connection is active, otherwise establishes
|
|
1019
|
-
* new connection and then returns the
|
|
1020
|
-
* connection fails.
|
|
1019
|
+
* new connection and then returns the requested value. Falls back to preconfigured fallback
|
|
1020
|
+
* values if WebSocket connection fails.
|
|
1021
1021
|
* In REST mode, makes an API call for each check.
|
|
1022
1022
|
*/
|
|
1023
1023
|
async checkFlag(options) {
|
|
@@ -1086,10 +1086,17 @@ var Schematic = class {
|
|
|
1086
1086
|
await this.setContext(context);
|
|
1087
1087
|
} catch (error) {
|
|
1088
1088
|
console.warn(
|
|
1089
|
-
"WebSocket connection failed,
|
|
1089
|
+
"WebSocket connection failed, using fallback value:",
|
|
1090
1090
|
error
|
|
1091
1091
|
);
|
|
1092
|
-
|
|
1092
|
+
const errorResult = this.resolveFallbackCheckFlagReturn(
|
|
1093
|
+
key,
|
|
1094
|
+
fallback,
|
|
1095
|
+
"WebSocket connection failed",
|
|
1096
|
+
error instanceof Error ? error.message : String(error)
|
|
1097
|
+
);
|
|
1098
|
+
this.submitFlagCheckEvent(key, errorResult, context);
|
|
1099
|
+
return errorResult.value;
|
|
1093
1100
|
}
|
|
1094
1101
|
const contextVals = this.checks[contextStr] ?? {};
|
|
1095
1102
|
const flagCheck = contextVals[key];
|
|
@@ -1192,53 +1199,6 @@ var Schematic = class {
|
|
|
1192
1199
|
this.debug(`submitting flag check event:`, eventBody);
|
|
1193
1200
|
return this.handleEvent("flag_check", EventBodyFlagCheckToJSON(eventBody));
|
|
1194
1201
|
}
|
|
1195
|
-
/**
|
|
1196
|
-
* Helper method for falling back to REST API when WebSocket connection fails
|
|
1197
|
-
*/
|
|
1198
|
-
async fallbackToRest(key, context, fallback) {
|
|
1199
|
-
if (this.isOffline()) {
|
|
1200
|
-
const resolvedFallback = this.resolveFallbackValue(key, fallback);
|
|
1201
|
-
this.debug(`fallbackToRest offline result: ${key}`, {
|
|
1202
|
-
value: resolvedFallback,
|
|
1203
|
-
offlineMode: true
|
|
1204
|
-
});
|
|
1205
|
-
return resolvedFallback;
|
|
1206
|
-
}
|
|
1207
|
-
try {
|
|
1208
|
-
const requestUrl = `${this.apiUrl}/flags/${key}/check`;
|
|
1209
|
-
const response = await fetch(requestUrl, {
|
|
1210
|
-
method: "POST",
|
|
1211
|
-
headers: {
|
|
1212
|
-
...this.additionalHeaders ?? {},
|
|
1213
|
-
"Content-Type": "application/json;charset=UTF-8",
|
|
1214
|
-
"X-Schematic-Api-Key": this.apiKey
|
|
1215
|
-
},
|
|
1216
|
-
body: JSON.stringify(context)
|
|
1217
|
-
});
|
|
1218
|
-
if (!response.ok) {
|
|
1219
|
-
throw new Error("Network response was not ok");
|
|
1220
|
-
}
|
|
1221
|
-
const responseJson = await response.json();
|
|
1222
|
-
const data = CheckFlagResponseFromJSON(responseJson);
|
|
1223
|
-
this.debug(`fallbackToRest result: ${key}`, data);
|
|
1224
|
-
const result = CheckFlagReturnFromJSON(data.data);
|
|
1225
|
-
if (typeof result.featureUsageEvent === "string") {
|
|
1226
|
-
this.updateFeatureUsageEventMap(result);
|
|
1227
|
-
}
|
|
1228
|
-
this.submitFlagCheckEvent(key, result, context);
|
|
1229
|
-
return result.value;
|
|
1230
|
-
} catch (error) {
|
|
1231
|
-
console.warn("REST API call failed, using fallback value:", error);
|
|
1232
|
-
const errorResult = this.resolveFallbackCheckFlagReturn(
|
|
1233
|
-
key,
|
|
1234
|
-
fallback,
|
|
1235
|
-
"API request failed (fallback)",
|
|
1236
|
-
error instanceof Error ? error.message : String(error)
|
|
1237
|
-
);
|
|
1238
|
-
this.submitFlagCheckEvent(key, errorResult, context);
|
|
1239
|
-
return errorResult.value;
|
|
1240
|
-
}
|
|
1241
|
-
}
|
|
1242
1202
|
/**
|
|
1243
1203
|
* Make an API call to fetch all flag values for a given context.
|
|
1244
1204
|
* Recommended for use in REST mode only.
|
|
@@ -1992,7 +1952,10 @@ var Schematic = class {
|
|
|
1992
1952
|
clearTimeout(timeoutId);
|
|
1993
1953
|
}
|
|
1994
1954
|
this.debug(`WebSocket connection ${connectionId} error:`, error);
|
|
1995
|
-
|
|
1955
|
+
const wrappedError = new Error(
|
|
1956
|
+
"WebSocket connection failed during handshake"
|
|
1957
|
+
);
|
|
1958
|
+
reject(wrappedError);
|
|
1996
1959
|
};
|
|
1997
1960
|
webSocket.onclose = () => {
|
|
1998
1961
|
if (timeoutId !== null) {
|
|
@@ -2039,6 +2002,20 @@ var Schematic = class {
|
|
|
2039
2002
|
}
|
|
2040
2003
|
};
|
|
2041
2004
|
socket.addEventListener("message", messageHandler);
|
|
2005
|
+
socket.addEventListener("close", (event) => {
|
|
2006
|
+
if (!resolved) {
|
|
2007
|
+
resolved = true;
|
|
2008
|
+
if (event.code === 4001) {
|
|
2009
|
+
reject(
|
|
2010
|
+
new Error(
|
|
2011
|
+
`Authentication failed: ${event.reason !== "" ? event.reason : "Invalid API key"}`
|
|
2012
|
+
)
|
|
2013
|
+
);
|
|
2014
|
+
} else {
|
|
2015
|
+
reject(new Error("WebSocket connection closed unexpectedly"));
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
});
|
|
2042
2019
|
this.currentWebSocket = socket;
|
|
2043
2020
|
const clientVersion = this.additionalHeaders["X-Schematic-Client-Version"] ?? `schematic-js@${version}`;
|
|
2044
2021
|
const messagePayload = {
|
package/dist/schematic.d.ts
CHANGED
|
@@ -408,8 +408,8 @@ export declare class Schematic {
|
|
|
408
408
|
/**
|
|
409
409
|
* Get value for a single flag.
|
|
410
410
|
* In WebSocket mode, returns cached values if connection is active, otherwise establishes
|
|
411
|
-
* new connection and then returns the
|
|
412
|
-
* connection fails.
|
|
411
|
+
* new connection and then returns the requested value. Falls back to preconfigured fallback
|
|
412
|
+
* values if WebSocket connection fails.
|
|
413
413
|
* In REST mode, makes an API call for each check.
|
|
414
414
|
*/
|
|
415
415
|
checkFlag(options: CheckOptions): Promise<boolean>;
|
|
@@ -431,10 +431,6 @@ export declare class Schematic {
|
|
|
431
431
|
* Records data about a flag check for analytics
|
|
432
432
|
*/
|
|
433
433
|
private submitFlagCheckEvent;
|
|
434
|
-
/**
|
|
435
|
-
* Helper method for falling back to REST API when WebSocket connection fails
|
|
436
|
-
*/
|
|
437
|
-
private fallbackToRest;
|
|
438
434
|
/**
|
|
439
435
|
* Make an API call to fetch all flag values for a given context.
|
|
440
436
|
* Recommended for use in REST mode only.
|
package/dist/schematic.esm.js
CHANGED
|
@@ -781,7 +781,7 @@ function contextString(context) {
|
|
|
781
781
|
}
|
|
782
782
|
|
|
783
783
|
// src/version.ts
|
|
784
|
-
var version = "1.2.
|
|
784
|
+
var version = "1.2.17";
|
|
785
785
|
|
|
786
786
|
// src/index.ts
|
|
787
787
|
var anonymousIdKey = "schematicId";
|
|
@@ -997,8 +997,8 @@ var Schematic = class {
|
|
|
997
997
|
/**
|
|
998
998
|
* Get value for a single flag.
|
|
999
999
|
* In WebSocket mode, returns cached values if connection is active, otherwise establishes
|
|
1000
|
-
* new connection and then returns the
|
|
1001
|
-
* connection fails.
|
|
1000
|
+
* new connection and then returns the requested value. Falls back to preconfigured fallback
|
|
1001
|
+
* values if WebSocket connection fails.
|
|
1002
1002
|
* In REST mode, makes an API call for each check.
|
|
1003
1003
|
*/
|
|
1004
1004
|
async checkFlag(options) {
|
|
@@ -1067,10 +1067,17 @@ var Schematic = class {
|
|
|
1067
1067
|
await this.setContext(context);
|
|
1068
1068
|
} catch (error) {
|
|
1069
1069
|
console.warn(
|
|
1070
|
-
"WebSocket connection failed,
|
|
1070
|
+
"WebSocket connection failed, using fallback value:",
|
|
1071
1071
|
error
|
|
1072
1072
|
);
|
|
1073
|
-
|
|
1073
|
+
const errorResult = this.resolveFallbackCheckFlagReturn(
|
|
1074
|
+
key,
|
|
1075
|
+
fallback,
|
|
1076
|
+
"WebSocket connection failed",
|
|
1077
|
+
error instanceof Error ? error.message : String(error)
|
|
1078
|
+
);
|
|
1079
|
+
this.submitFlagCheckEvent(key, errorResult, context);
|
|
1080
|
+
return errorResult.value;
|
|
1074
1081
|
}
|
|
1075
1082
|
const contextVals = this.checks[contextStr] ?? {};
|
|
1076
1083
|
const flagCheck = contextVals[key];
|
|
@@ -1173,53 +1180,6 @@ var Schematic = class {
|
|
|
1173
1180
|
this.debug(`submitting flag check event:`, eventBody);
|
|
1174
1181
|
return this.handleEvent("flag_check", EventBodyFlagCheckToJSON(eventBody));
|
|
1175
1182
|
}
|
|
1176
|
-
/**
|
|
1177
|
-
* Helper method for falling back to REST API when WebSocket connection fails
|
|
1178
|
-
*/
|
|
1179
|
-
async fallbackToRest(key, context, fallback) {
|
|
1180
|
-
if (this.isOffline()) {
|
|
1181
|
-
const resolvedFallback = this.resolveFallbackValue(key, fallback);
|
|
1182
|
-
this.debug(`fallbackToRest offline result: ${key}`, {
|
|
1183
|
-
value: resolvedFallback,
|
|
1184
|
-
offlineMode: true
|
|
1185
|
-
});
|
|
1186
|
-
return resolvedFallback;
|
|
1187
|
-
}
|
|
1188
|
-
try {
|
|
1189
|
-
const requestUrl = `${this.apiUrl}/flags/${key}/check`;
|
|
1190
|
-
const response = await fetch(requestUrl, {
|
|
1191
|
-
method: "POST",
|
|
1192
|
-
headers: {
|
|
1193
|
-
...this.additionalHeaders ?? {},
|
|
1194
|
-
"Content-Type": "application/json;charset=UTF-8",
|
|
1195
|
-
"X-Schematic-Api-Key": this.apiKey
|
|
1196
|
-
},
|
|
1197
|
-
body: JSON.stringify(context)
|
|
1198
|
-
});
|
|
1199
|
-
if (!response.ok) {
|
|
1200
|
-
throw new Error("Network response was not ok");
|
|
1201
|
-
}
|
|
1202
|
-
const responseJson = await response.json();
|
|
1203
|
-
const data = CheckFlagResponseFromJSON(responseJson);
|
|
1204
|
-
this.debug(`fallbackToRest result: ${key}`, data);
|
|
1205
|
-
const result = CheckFlagReturnFromJSON(data.data);
|
|
1206
|
-
if (typeof result.featureUsageEvent === "string") {
|
|
1207
|
-
this.updateFeatureUsageEventMap(result);
|
|
1208
|
-
}
|
|
1209
|
-
this.submitFlagCheckEvent(key, result, context);
|
|
1210
|
-
return result.value;
|
|
1211
|
-
} catch (error) {
|
|
1212
|
-
console.warn("REST API call failed, using fallback value:", error);
|
|
1213
|
-
const errorResult = this.resolveFallbackCheckFlagReturn(
|
|
1214
|
-
key,
|
|
1215
|
-
fallback,
|
|
1216
|
-
"API request failed (fallback)",
|
|
1217
|
-
error instanceof Error ? error.message : String(error)
|
|
1218
|
-
);
|
|
1219
|
-
this.submitFlagCheckEvent(key, errorResult, context);
|
|
1220
|
-
return errorResult.value;
|
|
1221
|
-
}
|
|
1222
|
-
}
|
|
1223
1183
|
/**
|
|
1224
1184
|
* Make an API call to fetch all flag values for a given context.
|
|
1225
1185
|
* Recommended for use in REST mode only.
|
|
@@ -1973,7 +1933,10 @@ var Schematic = class {
|
|
|
1973
1933
|
clearTimeout(timeoutId);
|
|
1974
1934
|
}
|
|
1975
1935
|
this.debug(`WebSocket connection ${connectionId} error:`, error);
|
|
1976
|
-
|
|
1936
|
+
const wrappedError = new Error(
|
|
1937
|
+
"WebSocket connection failed during handshake"
|
|
1938
|
+
);
|
|
1939
|
+
reject(wrappedError);
|
|
1977
1940
|
};
|
|
1978
1941
|
webSocket.onclose = () => {
|
|
1979
1942
|
if (timeoutId !== null) {
|
|
@@ -2020,6 +1983,20 @@ var Schematic = class {
|
|
|
2020
1983
|
}
|
|
2021
1984
|
};
|
|
2022
1985
|
socket.addEventListener("message", messageHandler);
|
|
1986
|
+
socket.addEventListener("close", (event) => {
|
|
1987
|
+
if (!resolved) {
|
|
1988
|
+
resolved = true;
|
|
1989
|
+
if (event.code === 4001) {
|
|
1990
|
+
reject(
|
|
1991
|
+
new Error(
|
|
1992
|
+
`Authentication failed: ${event.reason !== "" ? event.reason : "Invalid API key"}`
|
|
1993
|
+
)
|
|
1994
|
+
);
|
|
1995
|
+
} else {
|
|
1996
|
+
reject(new Error("WebSocket connection closed unexpectedly"));
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1999
|
+
});
|
|
2023
2000
|
this.currentWebSocket = socket;
|
|
2024
2001
|
const clientVersion = this.additionalHeaders["X-Schematic-Client-Version"] ?? `schematic-js@${version}`;
|
|
2025
2002
|
const messagePayload = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schematichq/schematic-js",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.17",
|
|
4
4
|
"main": "dist/schematic.cjs.js",
|
|
5
5
|
"module": "dist/schematic.esm.js",
|
|
6
6
|
"types": "dist/schematic.d.ts",
|
|
@@ -37,21 +37,21 @@
|
|
|
37
37
|
"uuid": "^13.0.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@eslint/js": "^
|
|
41
|
-
"@microsoft/api-extractor": "^7.
|
|
42
|
-
"@openapitools/openapi-generator-cli": "^2.
|
|
43
|
-
"@vitest/browser": "^4.0.
|
|
44
|
-
"esbuild": "^0.27.
|
|
40
|
+
"@eslint/js": "^10.0.1",
|
|
41
|
+
"@microsoft/api-extractor": "^7.56.3",
|
|
42
|
+
"@openapitools/openapi-generator-cli": "^2.28.3",
|
|
43
|
+
"@vitest/browser": "^4.0.18",
|
|
44
|
+
"esbuild": "^0.27.3",
|
|
45
45
|
"eslint": "^9.39.2",
|
|
46
|
-
"globals": "^
|
|
47
|
-
"happy-dom": "^20.
|
|
46
|
+
"globals": "^17.3.0",
|
|
47
|
+
"happy-dom": "^20.6.1",
|
|
48
48
|
"husky": "^9.1.7",
|
|
49
|
-
"jsdom": "^
|
|
49
|
+
"jsdom": "^28.0.0",
|
|
50
50
|
"mock-socket": "^9.3.1",
|
|
51
|
-
"prettier": "^3.
|
|
51
|
+
"prettier": "^3.8.1",
|
|
52
52
|
"typescript": "^5.9.3",
|
|
53
|
-
"typescript-eslint": "^8.
|
|
54
|
-
"vitest": "^4.0.
|
|
53
|
+
"typescript-eslint": "^8.55.0",
|
|
54
|
+
"vitest": "^4.0.18"
|
|
55
55
|
},
|
|
56
56
|
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
|
|
57
57
|
}
|