@tinkerlandai/indiana-sdk 0.10.2 → 0.10.3
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/CHANGELOG.md +6 -0
- package/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/dist/{chunk-CWHJKWOD.js → chunk-CJRIKZQD.js} +1 -1
- package/dist/chunk-E3JBY72K.js +1 -0
- package/dist/{chunk-LCLCG2I2.js → chunk-GWHDPLPE.js} +1 -1
- package/dist/{chunk-FYGXGMRB.js → chunk-KI27THNK.js} +1 -1
- package/dist/{chunk-XO42OH3M.js → chunk-Q2K6SWD2.js} +1 -1
- package/dist/{chunk-VZVUZWP2.js → chunk-U77SWE5J.js} +1 -1
- package/dist/{chunk-GHJ65ILJ.js → chunk-V6XM3KIC.js} +1 -1
- package/dist/{chunk-V3RAOCVQ.js → chunk-VAANAWSQ.js} +1 -1
- package/dist/{chunk-Q64VNSPV.js → chunk-WLFZPIQ4.js} +2 -2
- package/dist/{content-QM75T7JO.js → content-Y4ENI5PK.js} +1 -1
- package/dist/funnel-runtime-host-I66G3KQD.js +1 -0
- package/dist/index.js +1 -1
- package/dist/native-shell.js +1 -1
- package/dist/openrouter.js +1 -1
- package/dist/page.js +1 -1
- package/dist/{paywall-527RPFAX.js → paywall-SOTWV22V.js} +1 -1
- package/dist/react-native.js +1 -1
- package/dist/server.js +1 -1
- package/dist/visual.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-JPV7F5XI.js +0 -1
- package/dist/funnel-runtime-host-BJZUVJAI.js +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
import{f as _,j as F}from"./chunk-X6BCUINI.js";import{b as q}from"./chunk-SSLZKIIU.js";import{L as U}from"./chunk-
|
|
1
|
+
import{f as _,j as F}from"./chunk-X6BCUINI.js";import{b as q}from"./chunk-SSLZKIIU.js";import{L as U}from"./chunk-WLFZPIQ4.js";import{Y as P,ca as M,ja as z,ka as N,qa as d}from"./chunk-ACF2FBUB.js";import{a as v}from"./chunk-DSPLGTQI.js";import"./chunk-G56XGVMS.js";import"./chunk-V6XM3KIC.js";import{a as O}from"./chunk-7DXL3LHT.js";import"./chunk-3PZ4Y6UD.js";import{z as p}from"zod";var V=new WeakMap;function Q(r){let t={string:p.string(),number:p.number().finite(),boolean:p.boolean(),strings:p.array(p.string()),json:O}[r.type];return r.nullable?t.nullable():t}async function ne(r,t){return W(r,t,!1)}async function re(r,t){return W(r,t,!0)}async function W(r,t,w){t.signal?.throwIfAborted();let T=U.parse(t.content),o=T.document;if(V.has(r))throw new d("CONFLICT","Content is already mounted in this container");let f=p.record(p.string(),O).parse(t.inputs??{});if(Object.keys(f).length>40)throw new d("INVALID_REQUEST","Too many content inputs");for(let[e,s]of Object.entries(f))z.parse({[e]:s});if(Object.keys(f).some(e=>!Object.hasOwn(o.contract.inputs,e)))throw new d("INVALID_REQUEST","Undeclared content input");for(let[e,s]of Object.entries(o.contract.inputs))if(Object.hasOwn(f,e))Q(s).parse(f[e]);else if(s.required)throw new d("INVALID_REQUEST",`Missing content input ${e}`);for(let e of Object.keys(o.contract.outputs))if(typeof t.actions[e]!="function")throw new d("INVALID_REQUEST",`Missing host handler for ${e}`);let $=N.parse(t.bindings??[]),a=r.ownerDocument,L,k=o.kind==="popup",n=k?a.createElement("dialog"):null,i=a.createElement("div");if(i.dataset.indianaContent=o.kind,i.setAttribute("aria-label",o.name),i.style.cssText="width:100%;min-width:0",n){n.setAttribute("aria-label",o.name);let e=o.screen.presentation;n.style.cssText="box-sizing:border-box;padding:0;border:0;max-width:calc(100vw - 32px);max-height:calc(100dvh - 32px);overflow:auto",e.type==="modal"||e.type==="sheet"?(n.style.width=`${P[e.size]}px`,e.type==="sheet"&&(n.style.margin="auto auto 0")):n.style.width="100vw",n.append(i),r.append(n)}else r.append(i);let c=!1,C=!1,g=!1;w&&!n&&(i.style.width=`${r.getBoundingClientRect().width||a.defaultView?.innerWidth||800}px`);let B=w?_(n??i):()=>{},H,x,A=new Promise((e,s)=>{H=e,x=s});A.catch(()=>{});let l=null,E=structuredClone(o.screen);E.actions=E.actions.map(e=>e.type==="event"?{...e,close:!1}:e);let b;function m(){c||(c=!0,t.signal?.removeEventListener("abort",m),x(new Error("Content preparation cancelled")),b?.destroy(),n?.close(),n?.remove(),i.remove(),V.delete(r),n&&L?.())}let S=()=>{g&&(m(),v(t.onDismiss))};if(n){let e=a.createElement("button");e.type="button",e.textContent="\xD7",e.setAttribute("aria-label",t.locale?.startsWith("zh")?"\u5173\u95ED":"Close"),e.style.cssText="position:absolute;right:8px;top:8px;width:30px;height:30px;z-index:1",e.addEventListener("click",S),n.prepend(e)}n?.addEventListener("cancel",e=>{e.preventDefault(),S()});async function D(){if(c||!g||C||!l)return;let e=l;C=!0,i.setAttribute("aria-busy","true");try{await t.actions[e.actionId](structuredClone(e)),l=null}catch(s){v(t.onError,s)}finally{C=!1,c||i.removeAttribute("aria-busy")}}function R(){if(c)throw new d("CONFLICT","Content host has been destroyed");g||(L=q(a),B(),w&&!n&&(i.style.width="100%"),n?.showModal(),g=!0)}try{w||R(),b=F(i,{mode:"live",title:o.name,viewportHeight:o.kind==="page"?r.clientHeight||a.defaultView?.innerHeight||800:void 0,onRendered:H,onError:u=>{let I=new Error(u);x(I),v(t.onError,I)},onEscape:k?S:void 0,onAction:(u,I,J)=>{if(c||!g||C||l)return;let h=o.screen.actions.find(y=>y.id===u),j=o.contract.outputs[u];if(!(h?.type!=="event"||!j))try{let y=Q(j).parse(Object.hasOwn(h.properties,"value")?h.properties.value:h.properties);l={requestId:crypto.randomUUID(),contentVersionId:T.id,actionId:u,event:h.event,value:y,selection:I??{},answers:J??{},requestedClose:h.close},D()}catch(y){v(t.onError,y)}}});let e={destroy:m,show:R,get pendingAction(){return l?structuredClone(l):null},retryAction:D,updateCapabilities:u=>b?.updateCapabilities(u)};V.set(r,e),t.signal?.addEventListener("abort",m,{once:!0}),t.signal?.aborted&&(m(),t.signal.throwIfAborted());let s=M(E,[t.locale,...a.defaultView?.navigator.languages??[]]);if(b.update(E,s,{capabilities:t.capabilities,serverInput:{bindings:$,answers:{},values:f}}),await Promise.race([b.ready,A]),w&&await A,c)throw new Error("Content preparation cancelled");return e}catch(e){throw m(),e}}export{ne as mountContent,re as prepareContent};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{b as a}from"./chunk-GWHDPLPE.js";import"./chunk-SBPTOOYB.js";import"./chunk-WLFZPIQ4.js";import"./chunk-ACF2FBUB.js";import"./chunk-EARSGITJ.js";import"./chunk-Q2K6SWD2.js";import"./chunk-DSPLGTQI.js";import"./chunk-G56XGVMS.js";import"./chunk-V6XM3KIC.js";import"./chunk-7DXL3LHT.js";import"./chunk-3PZ4Y6UD.js";export{a as showFunnelRun};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a as ke,b as be}from"./chunk-LCLCG2I2.js";import{a as Oe,c as He}from"./chunk-VZVUZWP2.js";import{a as Fe}from"./chunk-JPV7F5XI.js";import{b as De,g as Pe,l as z,m as xe,n as Be,p as Le}from"./chunk-FYGXGMRB.js";import"./chunk-SBPTOOYB.js";import{F as Me,G as Ne,H as Te,Ka as Ee,La as ge,Ma as we,Qa as ve,i as oe,k as q,l as ae,m as ie,n as se,o as ce,p as de,q as ue,qa as ye,r as le,s as me,t as fe,u as pe,w as $,x as he,y as Ie}from"./chunk-Q64VNSPV.js";import{qa as P}from"./chunk-ACF2FBUB.js";import{c as _e}from"./chunk-CLXORAOK.js";import"./chunk-EARSGITJ.js";import{c as Se,d as Re}from"./chunk-XO42OH3M.js";import{a as L}from"./chunk-DSPLGTQI.js";import"./chunk-G56XGVMS.js";import"./chunk-GHJ65ILJ.js";import"./chunk-7DXL3LHT.js";import"./chunk-3PZ4Y6UD.js";import{z as j}from"zod";var r=class extends Error{constructor(u,B=0,I=!1,f="fix_request"){super(u);this.code=u;this.status=B;this.retryable=I;this.recovery=f;this.name="MerchantBridgeError"}code;status;retryable;recovery};async function Ge(n,_){if(!n.headers.get("content-type")?.split(";",1)[0].trim().match(/^application\/(?:json|[a-z0-9.+-]+\+json)$/i))throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE");let u=n.body?.getReader();if(!u)throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE");let B=()=>{u.cancel().catch(()=>{})};_.addEventListener("abort",B,{once:!0});let I=0,f="",a=new TextDecoder("utf-8",{fatal:!0});try{for(_.throwIfAborted();;){let d=await u.read();if(_.throwIfAborted(),d.done)break;if(I+=d.value.byteLength,I>262144)throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE");f+=a.decode(d.value,{stream:!0})}return f+=a.decode(),JSON.parse(f)}catch(d){throw _.throwIfAborted(),d instanceof r?d:new r("MERCHANT_BRIDGE_INVALID_RESPONSE")}finally{_.removeEventListener("abort",B),await u.cancel().catch(()=>{}),u.releaseLock()}}function Ue(n){let _=ce.parse(n.origin),u=de.parse(n.manifest),B={projectId:n.scope.projectId,environmentId:n.scope.environmentId,environment:n.scope.environment};for(let o of["projectId","environmentId","environment"])if(u[o]!==n.scope[o])throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");let I=n.customerId===void 0?null:j.uuid().parse(n.customerId),f=0,a=null,d=null,F=new AbortController,k=n.fetch??globalThis.fetch;async function M(o,t,i,E,N){if(!u.capabilities.includes(o))throw new r("MERCHANT_BRIDGE_CAPABILITY_UNSUPPORTED",501);let v=se[o];if(["merchant_catalog_reader","merchant_refund_authority","indiana_server_key"].includes(v.authority))throw new r("MERCHANT_BRIDGE_FORBIDDEN",403);let T=f,x=AbortSignal.any([F.signal,AbortSignal.timeout(Math.min(3e4,Math.max(1e3,n.timeoutMs??1e4))),...n.signal?[n.signal]:[],...E?[E]:[]]);x.throwIfAborted();let S=new Headers(await n.headers?.());if(x.throwIfAborted(),T!==f)throw new DOMException("Merchant identity changed","AbortError");if(/\bgg_(?:sk|ct|rt)_/.test(S.get("authorization")??"")||S.has("X-Indiana-Bridge-Signature")||S.has("X-Indiana-Bridge-Key-Id"))throw new r("MERCHANT_BRIDGE_FORBIDDEN",403);S.set(ie,String(q)),S.set("Accept","application/json");let g=crypto.randomUUID();S.set("X-Request-Id",g),S.set("Content-Type","application/json");let p=await k(_+ae,{method:"POST",credentials:"include",redirect:"error",cache:"no-store",headers:S,signal:x,body:JSON.stringify({bridgeVersion:q,requestId:g,command:v.command,scope:B,data:N??(i===void 0?{}:{operationId:i})})}),D=await Ge(p,x);if(x.throwIfAborted(),T!==f)throw new DOMException("Merchant identity changed","AbortError");if(!p.ok){let y=Ie.safeParse(D);throw!y.success||y.data.scope.projectId!==n.scope.projectId||y.data.scope.environment!==n.scope.environment||y.data.scope.environmentId!==n.scope.environmentId||y.data.requestId!==g||y.data.command!==v.command?new r("MERCHANT_BRIDGE_INVALID_RESPONSE",p.status):new r(y.data.error.code,p.status,y.data.error.retryable,y.data.error.recovery)}let R=ue(j.unknown()).safeParse(D);if(!R.success||R.data.scope.projectId!==n.scope.projectId||R.data.scope.environment!==n.scope.environment||R.data.scope.environmentId!==n.scope.environmentId||R.data.requestId!==g||R.data.command!==v.command)throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE",p.status);let l=t.safeParse(R.data.data);if(!l.success)throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE",p.status);return l.data}function w(){if(!I)throw new r("MERCHANT_BRIDGE_UNAUTHENTICATED",401);return I}async function H(o,t,i,E){let N=w(),v=pe.parse({operationId:i,customerId:N}),T=await M(o,t,v.operationId,E,v);if(T.customerId!==N||T.operationId!==v.operationId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");return T}return{async readPresentation(o){let t=await M("presentation",me,void 0,o);if(d!==null&&d!==t.nativeUserId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");if(t.presentation&&I!==null&&t.presentation.customerId!==I)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");return d=t.nativeUserId,a=t.presentation,t},async createSession(o){let t=await M("session",le,void 0,o);if(d!==null&&d!==t.nativeUserId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");if(I!==null&&I!==t.session.customerId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");if(a&&a.customerId!==t.session.customerId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");return I=t.session.customerId,d=t.nativeUserId,t.session},async reportPresentation(o,t){let i=w(),E=fe.parse(o);if(!a||a.customerId!==i||a.decisionId!==E.decisionId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");return M("presentation_events",j.strictObject({queued:j.literal(!0)}),void 0,t,E)},async readCustomerCommerce(o){let t=w(),i=await M("customer_commerce",he,void 0,o);if(i.customerId!==t)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");return i},async executePurchase(o,t){let i=await H("purchases",$,o,t);if(i.type!=="purchase")throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE");return i},readOperation:(o,t)=>H("operation_status",oe,o,t),async executeSubscriptionChange(o,t){let i=await H("subscription_changes",$,o,t);if(i.type!=="subscription_change")throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE");return i},reset(){f++,F.abort(),F=new AbortController,I=null,a=null,d=null}}}function je(n){let _=Se(n.baseUrl),u=n.scope,B=ve(n),I=n.fetch??globalThis.fetch.bind(globalThis),f,a=!1,d=!1,F=0,k=0,M="",w=0,H=0,o=0,t,i=!1,E,N=!0,v,T,x,S,g,p,D=e=>{p===e&&(p=void 0)},R=new Set,l=(e,m)=>L(n.onStatus,e,m);function y(){let e=g;g=void 0,e?.host.destroy(),e?.client.reset()}let K=()=>Date.now()<H&&performance.now()<o;function J(e){if(e.target.release!==n.appManifestRelease||!K())return!1;try{return _e(e.target.render,document,location.pathname,()=>{})}catch{return l("incompatible",e.id),!1}}function V(e){return!a&&e.epoch===w&&e.visit===k&&document.visibilityState!=="hidden"}async function X(e){if(p||g){l("in_progress",e.id);return}if(!n.session.peek()){l("identity_required",e.id);return}let m={command:ge.parse({operation:"entry_start",entryId:e.id,funnelId:e.funnelId,versionId:e.versionId,release:n.appManifestRelease,requestId:crypto.randomUUID(),locale:document.documentElement.lang||"en"}),epoch:w,visit:k};p=m,await Z(m)}async function Z(e){if(e.dispatching)return;e.dispatching=!0;let m,b=!1;async function h(s){if(e.epoch!==w)return;let c=s.view;c&&["running","waiting"].includes(c.status)&&await s.submit({operation:"close",requestId:crypto.randomUUID(),nodeId:c.nodeId,visit:c.visit,revision:c.revision})}try{l("starting",e.command.entryId);let s=await B.request("funnels",e.command),c=we.safeParse(s);if(c.success){if(c.data.scope.projectId!==u.projectId||c.data.scope.environment!==u.environment)throw new P("SCOPE_MISMATCH","Entry scope changed");e.epoch===w&&l(c.data.reason,e.command.entryId),D(e);return}let A=ye.parse(s);if(!A.runToken||A.scope.projectId!==u.projectId||A.scope.environment!==u.environment||A.run.source.type!=="funnel"||A.run.source.id!==e.command.funnelId||A.run.source.versionId!==e.command.versionId)throw new P("INVALID_RESPONSE","Invalid entry Run");if(e.epoch!==w){D(e),l("identity_changed",e.command.entryId);return}let C=z({...n,runToken:A.runToken,funnelId:e.command.funnelId});m=C,await C.get();let U=C.view;if(!U||U.id!==A.run.id||U.source.type!=="funnel"||U.source.id!==e.command.funnelId||U.source.versionId!==e.command.versionId)throw new P("INVALID_RESPONSE","Entry Run version changed");if(!V(e)){await h(C),l("left_screen",e.command.entryId),D(e);return}let ne=!1,te=!1,re=await be(n.container,{...n.host,client:C,appManifestRelease:n.appManifestRelease,onDismiss(O){ne=!0,g?.client===C&&(g=void 0,C.reset(),O&&O.status!=="completed"&&l("run_ended",e.command.entryId)),L(n.host?.onDismiss,O)},onComplete(O){te=!0,g?.client===C&&y(),L(n.host?.onComplete,O)},onError(O){L(n.host?.onError,O),g?.client===C&&O instanceof P&&O.code==="FORBIDDEN"&&(y(),l("run_ended",e.command.entryId))}});if(!V(e)||ne||te){re.destroy(),V(e)||await h(C),D(e);return}g={host:re,client:C},b=!0,D(e),l("started",e.command.entryId)}catch(s){!m&&s instanceof P&&s.status!==void 0&&s.status>=400&&s.status<500&&!s.retryable&&D(e),e.epoch===w&&!a&&(l("start_failed",e.command.entryId),L(n.host?.onError,s))}finally{b||m?.reset(),e.dispatching=!1}}function Ce(){if(i=!1,!(a||!d||!K()||document.visibilityState==="hidden")){N=!1;for(let e of f?.entries??[])if(!(e.event!=="screen_enter"||R.has(`${e.funnelId}:${e.id}`)||!J(e))){if(!n.session.peek()){l("identity_required",e.id);continue}R.add(`${e.funnelId}:${e.id}`),X(e);break}}}let G=()=>{!i&&!a&&(i=!0,queueMicrotask(Ce))};function Ae(){let e=location.origin+location.pathname;e!==M&&(M=e,k++,N=!0,R.clear(),G())}function Y(e){if(!e.isTrusted||e.button!==0||e.ctrlKey||e.metaKey||e.shiftKey||e.altKey||document.visibilityState==="hidden")return;let m=e.target;if(!(m instanceof Element)||m.closest("[data-indiana-funnel]"))return;let b=m.closest("button,a,input");if(!(!b||b.matches(":disabled,[aria-disabled='true']")||b.closest("[hidden],[inert],[aria-hidden='true']")))for(let h of f?.entries??[]){let s=h.target.button;if(h.event!=="button_click"||!s||!J(h))continue;let c=document.querySelectorAll(s.selector);if(!(c.length!==1||c[0]!==b)){X(h);break}}}async function Q(){if(a)return;if(E)return E;let e=++F;v=new AbortController;let m=(async()=>{let b=await I(`${_}/api/v1/funnel-entries/${n.environmentId}?iam=${n.appManifestRelease}`,{credentials:"omit",cache:"no-store",signal:v.signal});if(!b.ok)throw new P("SERVICE_UNAVAILABLE","Funnel entries unavailable",b.status);let h=Ee.parse(await b.json()),s=Date.parse(h.validatedAt),c=Date.parse(h.validUntil);if(h.scope.projectId!==u.projectId||h.scope.environment!==u.environment||h.scope.environmentId!==n.environmentId||h.release!==n.appManifestRelease||c<=Date.now()||s>Date.now()+5e3||c-s>6e4||c<=s)throw new P("INVALID_RESPONSE","Entry configuration scope or deadline changed");if(a||e!==F)return;let A=f===void 0;f=h,H=c,o=performance.now()+Math.min(c-Date.now(),6e4),(A||N)&&G()})();E=m;try{await m}finally{E===m&&(E=void 0)}}async function W(){try{await Q()}catch{l("configuration_unavailable")}a||(t=setTimeout(()=>{W()},15e3))}let ee=()=>G();return{async start(){d||a||(d=!0,M=location.origin+location.pathname,T=Re(Ae),x=n.session.subscribeIdentity(()=>{w++,N=!0,p=void 0,R.clear(),y(),G()}),S=new MutationObserver(G),S.observe(document.documentElement,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["hidden","inert","aria-hidden","data-indiana-id","id"]}),document.addEventListener("click",Y,!0),document.addEventListener("visibilitychange",ee),await W())},refresh:Q,async retry(){p&&!a&&p.epoch===w&&await Z(p)},destroy(){a=!0,F++,clearTimeout(t),v?.abort(),S?.disconnect(),T?.(),x?.(),document.removeEventListener("click",Y,!0),document.removeEventListener("visibilitychange",ee),y(),f=void 0}}}async function nn(...n){return(await import("./paywall-527RPFAX.js")).showPaywall(...n)}async function tn(...n){return(await import("./funnel-runtime-host-BJZUVJAI.js")).showFunnelRun(...n)}async function rn(...n){return(await import("./content-QM75T7JO.js")).mountContent(...n)}async function on(...n){return(await import("./content-QM75T7JO.js")).prepareContent(...n)}export{P as IndianaError,r as MerchantBridgeError,Pe as createBrowserFunnelCommandStorage,Be as createConfigurationSync,De as createFeatureClient,He as createFunnelClient,ke as createFunnelPageExperiments,z as createFunnelRunController,je as createIamFunnelEntries,Oe as createIndiana,Le as createLocalFunnelClient,Ue as createMerchantBridge,xe as createPaywallSession,Fe as evaluateFunnelNode,Ne as formatCouponDiscount,Me as formatCouponMoney,Te as formatCouponTerms,rn as mountContent,on as prepareContent,tn as showFunnelRun,nn as showPaywall};
|
|
1
|
+
import{a as ke,b as be}from"./chunk-GWHDPLPE.js";import{a as Oe,c as He}from"./chunk-U77SWE5J.js";import{a as Fe}from"./chunk-E3JBY72K.js";import{b as De,g as Pe,l as z,m as xe,n as Be,p as Le}from"./chunk-KI27THNK.js";import"./chunk-SBPTOOYB.js";import{F as Me,G as Ne,H as Te,Ka as Ee,La as ge,Ma as we,Qa as ve,i as oe,k as q,l as ae,m as ie,n as se,o as ce,p as de,q as ue,qa as ye,r as le,s as me,t as fe,u as pe,w as $,x as he,y as Ie}from"./chunk-WLFZPIQ4.js";import{qa as P}from"./chunk-ACF2FBUB.js";import{c as _e}from"./chunk-CLXORAOK.js";import"./chunk-EARSGITJ.js";import{c as Se,d as Re}from"./chunk-Q2K6SWD2.js";import{a as L}from"./chunk-DSPLGTQI.js";import"./chunk-G56XGVMS.js";import"./chunk-V6XM3KIC.js";import"./chunk-7DXL3LHT.js";import"./chunk-3PZ4Y6UD.js";import{z as j}from"zod";var r=class extends Error{constructor(u,B=0,I=!1,f="fix_request"){super(u);this.code=u;this.status=B;this.retryable=I;this.recovery=f;this.name="MerchantBridgeError"}code;status;retryable;recovery};async function Ge(n,_){if(!n.headers.get("content-type")?.split(";",1)[0].trim().match(/^application\/(?:json|[a-z0-9.+-]+\+json)$/i))throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE");let u=n.body?.getReader();if(!u)throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE");let B=()=>{u.cancel().catch(()=>{})};_.addEventListener("abort",B,{once:!0});let I=0,f="",a=new TextDecoder("utf-8",{fatal:!0});try{for(_.throwIfAborted();;){let d=await u.read();if(_.throwIfAborted(),d.done)break;if(I+=d.value.byteLength,I>262144)throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE");f+=a.decode(d.value,{stream:!0})}return f+=a.decode(),JSON.parse(f)}catch(d){throw _.throwIfAborted(),d instanceof r?d:new r("MERCHANT_BRIDGE_INVALID_RESPONSE")}finally{_.removeEventListener("abort",B),await u.cancel().catch(()=>{}),u.releaseLock()}}function Ue(n){let _=ce.parse(n.origin),u=de.parse(n.manifest),B={projectId:n.scope.projectId,environmentId:n.scope.environmentId,environment:n.scope.environment};for(let o of["projectId","environmentId","environment"])if(u[o]!==n.scope[o])throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");let I=n.customerId===void 0?null:j.uuid().parse(n.customerId),f=0,a=null,d=null,F=new AbortController,k=n.fetch??globalThis.fetch;async function M(o,t,i,E,N){if(!u.capabilities.includes(o))throw new r("MERCHANT_BRIDGE_CAPABILITY_UNSUPPORTED",501);let v=se[o];if(["merchant_catalog_reader","merchant_refund_authority","indiana_server_key"].includes(v.authority))throw new r("MERCHANT_BRIDGE_FORBIDDEN",403);let T=f,x=AbortSignal.any([F.signal,AbortSignal.timeout(Math.min(3e4,Math.max(1e3,n.timeoutMs??1e4))),...n.signal?[n.signal]:[],...E?[E]:[]]);x.throwIfAborted();let S=new Headers(await n.headers?.());if(x.throwIfAborted(),T!==f)throw new DOMException("Merchant identity changed","AbortError");if(/\bgg_(?:sk|ct|rt)_/.test(S.get("authorization")??"")||S.has("X-Indiana-Bridge-Signature")||S.has("X-Indiana-Bridge-Key-Id"))throw new r("MERCHANT_BRIDGE_FORBIDDEN",403);S.set(ie,String(q)),S.set("Accept","application/json");let g=crypto.randomUUID();S.set("X-Request-Id",g),S.set("Content-Type","application/json");let p=await k(_+ae,{method:"POST",credentials:"include",redirect:"error",cache:"no-store",headers:S,signal:x,body:JSON.stringify({bridgeVersion:q,requestId:g,command:v.command,scope:B,data:N??(i===void 0?{}:{operationId:i})})}),D=await Ge(p,x);if(x.throwIfAborted(),T!==f)throw new DOMException("Merchant identity changed","AbortError");if(!p.ok){let y=Ie.safeParse(D);throw!y.success||y.data.scope.projectId!==n.scope.projectId||y.data.scope.environment!==n.scope.environment||y.data.scope.environmentId!==n.scope.environmentId||y.data.requestId!==g||y.data.command!==v.command?new r("MERCHANT_BRIDGE_INVALID_RESPONSE",p.status):new r(y.data.error.code,p.status,y.data.error.retryable,y.data.error.recovery)}let R=ue(j.unknown()).safeParse(D);if(!R.success||R.data.scope.projectId!==n.scope.projectId||R.data.scope.environment!==n.scope.environment||R.data.scope.environmentId!==n.scope.environmentId||R.data.requestId!==g||R.data.command!==v.command)throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE",p.status);let l=t.safeParse(R.data.data);if(!l.success)throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE",p.status);return l.data}function w(){if(!I)throw new r("MERCHANT_BRIDGE_UNAUTHENTICATED",401);return I}async function H(o,t,i,E){let N=w(),v=pe.parse({operationId:i,customerId:N}),T=await M(o,t,v.operationId,E,v);if(T.customerId!==N||T.operationId!==v.operationId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");return T}return{async readPresentation(o){let t=await M("presentation",me,void 0,o);if(d!==null&&d!==t.nativeUserId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");if(t.presentation&&I!==null&&t.presentation.customerId!==I)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");return d=t.nativeUserId,a=t.presentation,t},async createSession(o){let t=await M("session",le,void 0,o);if(d!==null&&d!==t.nativeUserId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");if(I!==null&&I!==t.session.customerId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");if(a&&a.customerId!==t.session.customerId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");return I=t.session.customerId,d=t.nativeUserId,t.session},async reportPresentation(o,t){let i=w(),E=fe.parse(o);if(!a||a.customerId!==i||a.decisionId!==E.decisionId)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");return M("presentation_events",j.strictObject({queued:j.literal(!0)}),void 0,t,E)},async readCustomerCommerce(o){let t=w(),i=await M("customer_commerce",he,void 0,o);if(i.customerId!==t)throw new r("MERCHANT_BRIDGE_SCOPE_MISMATCH");return i},async executePurchase(o,t){let i=await H("purchases",$,o,t);if(i.type!=="purchase")throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE");return i},readOperation:(o,t)=>H("operation_status",oe,o,t),async executeSubscriptionChange(o,t){let i=await H("subscription_changes",$,o,t);if(i.type!=="subscription_change")throw new r("MERCHANT_BRIDGE_INVALID_RESPONSE");return i},reset(){f++,F.abort(),F=new AbortController,I=null,a=null,d=null}}}function je(n){let _=Se(n.baseUrl),u=n.scope,B=ve(n),I=n.fetch??globalThis.fetch.bind(globalThis),f,a=!1,d=!1,F=0,k=0,M="",w=0,H=0,o=0,t,i=!1,E,N=!0,v,T,x,S,g,p,D=e=>{p===e&&(p=void 0)},R=new Set,l=(e,m)=>L(n.onStatus,e,m);function y(){let e=g;g=void 0,e?.host.destroy(),e?.client.reset()}let K=()=>Date.now()<H&&performance.now()<o;function J(e){if(e.target.release!==n.appManifestRelease||!K())return!1;try{return _e(e.target.render,document,location.pathname,()=>{})}catch{return l("incompatible",e.id),!1}}function V(e){return!a&&e.epoch===w&&e.visit===k&&document.visibilityState!=="hidden"}async function X(e){if(p||g){l("in_progress",e.id);return}if(!n.session.peek()){l("identity_required",e.id);return}let m={command:ge.parse({operation:"entry_start",entryId:e.id,funnelId:e.funnelId,versionId:e.versionId,release:n.appManifestRelease,requestId:crypto.randomUUID(),locale:document.documentElement.lang||"en"}),epoch:w,visit:k};p=m,await Z(m)}async function Z(e){if(e.dispatching)return;e.dispatching=!0;let m,b=!1;async function h(s){if(e.epoch!==w)return;let c=s.view;c&&["running","waiting"].includes(c.status)&&await s.submit({operation:"close",requestId:crypto.randomUUID(),nodeId:c.nodeId,visit:c.visit,revision:c.revision})}try{l("starting",e.command.entryId);let s=await B.request("funnels",e.command),c=we.safeParse(s);if(c.success){if(c.data.scope.projectId!==u.projectId||c.data.scope.environment!==u.environment)throw new P("SCOPE_MISMATCH","Entry scope changed");e.epoch===w&&l(c.data.reason,e.command.entryId),D(e);return}let A=ye.parse(s);if(!A.runToken||A.scope.projectId!==u.projectId||A.scope.environment!==u.environment||A.run.source.type!=="funnel"||A.run.source.id!==e.command.funnelId||A.run.source.versionId!==e.command.versionId)throw new P("INVALID_RESPONSE","Invalid entry Run");if(e.epoch!==w){D(e),l("identity_changed",e.command.entryId);return}let C=z({...n,runToken:A.runToken,funnelId:e.command.funnelId});m=C,await C.get();let U=C.view;if(!U||U.id!==A.run.id||U.source.type!=="funnel"||U.source.id!==e.command.funnelId||U.source.versionId!==e.command.versionId)throw new P("INVALID_RESPONSE","Entry Run version changed");if(!V(e)){await h(C),l("left_screen",e.command.entryId),D(e);return}let ne=!1,te=!1,re=await be(n.container,{...n.host,client:C,appManifestRelease:n.appManifestRelease,onDismiss(O){ne=!0,g?.client===C&&(g=void 0,C.reset(),O&&O.status!=="completed"&&l("run_ended",e.command.entryId)),L(n.host?.onDismiss,O)},onComplete(O){te=!0,g?.client===C&&y(),L(n.host?.onComplete,O)},onError(O){L(n.host?.onError,O),g?.client===C&&O instanceof P&&O.code==="FORBIDDEN"&&(y(),l("run_ended",e.command.entryId))}});if(!V(e)||ne||te){re.destroy(),V(e)||await h(C),D(e);return}g={host:re,client:C},b=!0,D(e),l("started",e.command.entryId)}catch(s){!m&&s instanceof P&&s.status!==void 0&&s.status>=400&&s.status<500&&!s.retryable&&D(e),e.epoch===w&&!a&&(l("start_failed",e.command.entryId),L(n.host?.onError,s))}finally{b||m?.reset(),e.dispatching=!1}}function Ce(){if(i=!1,!(a||!d||!K()||document.visibilityState==="hidden")){N=!1;for(let e of f?.entries??[])if(!(e.event!=="screen_enter"||R.has(`${e.funnelId}:${e.id}`)||!J(e))){if(!n.session.peek()){l("identity_required",e.id);continue}R.add(`${e.funnelId}:${e.id}`),X(e);break}}}let G=()=>{!i&&!a&&(i=!0,queueMicrotask(Ce))};function Ae(){let e=location.origin+location.pathname;e!==M&&(M=e,k++,N=!0,R.clear(),G())}function Y(e){if(!e.isTrusted||e.button!==0||e.ctrlKey||e.metaKey||e.shiftKey||e.altKey||document.visibilityState==="hidden")return;let m=e.target;if(!(m instanceof Element)||m.closest("[data-indiana-funnel]"))return;let b=m.closest("button,a,input");if(!(!b||b.matches(":disabled,[aria-disabled='true']")||b.closest("[hidden],[inert],[aria-hidden='true']")))for(let h of f?.entries??[]){let s=h.target.button;if(h.event!=="button_click"||!s||!J(h))continue;let c=document.querySelectorAll(s.selector);if(!(c.length!==1||c[0]!==b)){X(h);break}}}async function Q(){if(a)return;if(E)return E;let e=++F;v=new AbortController;let m=(async()=>{let b=await I(`${_}/api/v1/funnel-entries/${n.environmentId}?iam=${n.appManifestRelease}`,{credentials:"omit",cache:"no-store",signal:v.signal});if(!b.ok)throw new P("SERVICE_UNAVAILABLE","Funnel entries unavailable",b.status);let h=Ee.parse(await b.json()),s=Date.parse(h.validatedAt),c=Date.parse(h.validUntil);if(h.scope.projectId!==u.projectId||h.scope.environment!==u.environment||h.scope.environmentId!==n.environmentId||h.release!==n.appManifestRelease||c<=Date.now()||s>Date.now()+5e3||c-s>6e4||c<=s)throw new P("INVALID_RESPONSE","Entry configuration scope or deadline changed");if(a||e!==F)return;let A=f===void 0;f=h,H=c,o=performance.now()+Math.min(c-Date.now(),6e4),(A||N)&&G()})();E=m;try{await m}finally{E===m&&(E=void 0)}}async function W(){try{await Q()}catch{l("configuration_unavailable")}a||(t=setTimeout(()=>{W()},15e3))}let ee=()=>G();return{async start(){d||a||(d=!0,M=location.origin+location.pathname,T=Re(Ae),x=n.session.subscribeIdentity(()=>{w++,N=!0,p=void 0,R.clear(),y(),G()}),S=new MutationObserver(G),S.observe(document.documentElement,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["hidden","inert","aria-hidden","data-indiana-id","id"]}),document.addEventListener("click",Y,!0),document.addEventListener("visibilitychange",ee),await W())},refresh:Q,async retry(){p&&!a&&p.epoch===w&&await Z(p)},destroy(){a=!0,F++,clearTimeout(t),v?.abort(),S?.disconnect(),T?.(),x?.(),document.removeEventListener("click",Y,!0),document.removeEventListener("visibilitychange",ee),y(),f=void 0}}}async function nn(...n){return(await import("./paywall-SOTWV22V.js")).showPaywall(...n)}async function tn(...n){return(await import("./funnel-runtime-host-I66G3KQD.js")).showFunnelRun(...n)}async function rn(...n){return(await import("./content-Y4ENI5PK.js")).mountContent(...n)}async function on(...n){return(await import("./content-Y4ENI5PK.js")).prepareContent(...n)}export{P as IndianaError,r as MerchantBridgeError,Pe as createBrowserFunnelCommandStorage,Be as createConfigurationSync,De as createFeatureClient,He as createFunnelClient,ke as createFunnelPageExperiments,z as createFunnelRunController,je as createIamFunnelEntries,Oe as createIndiana,Le as createLocalFunnelClient,Ue as createMerchantBridge,xe as createPaywallSession,Fe as evaluateFunnelNode,Ne as formatCouponDiscount,Me as formatCouponMoney,Te as formatCouponTerms,rn as mountContent,on as prepareContent,tn as showFunnelRun,nn as showPaywall};
|