Build 576754stable
Module 307221
11,083 characters · in 7af349a24887a0e7.jsDependencies
Source Raw
function(e,t,i){let n,r;i.d(t,{a:()=>M,i:()=>A,l:()=>g,o:()=>O,r:()=>P,t:()=>R});var o=i(976886),a=i(133237),s=i(257300),l=i(209688),c=i(588233),d=i(171251),u=i(886421);let h=["wasmPath","wasmSimdPath","glueCodePath","glueCodeSimdPath","modelsBasePath"];function p(e){let t=e.trim();if(""===t)return!1;if(function(e){for(let t=0;t<e.length;t+=1){let i=e.charCodeAt(t);if(i<32||127===i)return!0}return!1}(t))return!0;if(/^https?:\/\//i.test(t))try{new URL(t)}catch{return!0}return!1}function f(e,t){"empty"===t?console.warn(`[Incode SDK] wasm.${e} is empty. Falling back to CDN default.`):console.warn(`[Incode SDK] wasm.${e} appears malformed. Falling back to CDN default.`)}function m(e,t,i){if(!t||!(e in t))return i;let n=t[e];return void 0===n||"string"!=typeof n?i:""===n.trim()?(f(e,"empty"),i):p(n)?(f(e,"malformed"),i):n.trim()}function g(e){let t,i,n=(t="https://cdn.incodesmile.com/ml-wasm-kit-release/v2.13.21",{wasmPath:`${t}/webLib.wasm`,wasmSimdPath:`${t}/webLibSimd.wasm`,glueCodePath:`${t}/webLib.js`,glueCodeSimdPath:`${t}/webLibSimd.js`,modelsBasePath:`${t}/models`});if(!e)return n;let r=n.wasmSimdPath??n.wasmPath,o=n.modelsBasePath??(-1===(i=n.wasmPath.lastIndexOf("/"))?"models":`${n.wasmPath.substring(0,i)}/models`),a=m("wasmSimdPath",e,r),s=a.replace(/\.wasm($|\?)/,".js$1"),l={wasmPath:m("wasmPath",e,n.wasmPath),wasmSimdPath:a,glueCodePath:m("glueCodePath",e,n.glueCodePath),glueCodeSimdPath:m("glueCodeSimdPath",e,s),modelsBasePath:m("modelsBasePath",e,o),useSimd:e.useSimd,pipelines:e.pipelines,pipelineModels:e.pipelineModels};for(let e of h){let t=l[e];"string"==typeof t&&p(t)&&console.warn(`[Incode SDK] wasm.${e} appears malformed. Check the URL or path.`)}return l}var v=class{sendBeacon(e,t,i="application/json"){if("u"<typeof navigator||"function"!=typeof navigator.sendBeacon)return!1;let n=new Blob([t],{type:i});return navigator.sendBeacon(e,n)}onPageHide(e){let t=()=>{"hidden"===document.visibilityState&&e()};return document.addEventListener("visibilitychange",t),window.addEventListener("pagehide",e),window.addEventListener("beforeunload",e),()=>{document.removeEventListener("visibilitychange",t),window.removeEventListener("pagehide",e),window.removeEventListener("beforeunload",e)}}},C=class extends Error{constructor(e,t,i,n,r,o){super(`HTTP ${e} ${t}`),this.ok=!1,this.status=e,this.statusText=t,this.url=i,this.method=n,this.headers=r,this.data=o,this.name="FetchHttpError"}};let y=async(e,t)=>{if("response"===t)return e;if("blob"===t)return await e.blob();if("arrayBuffer"===t)return await e.arrayBuffer();if("text"===t)return await e.text();let i=e.headers.get("content-type")??"";if("json"===t||i.includes("application/json"))try{return await e.json()}catch{}return await e.text()};var w=class extends Error{constructor(e,t,i,n,r,o){super(`HTTP ${e} ${t}`),this.ok=!1,this.status=e,this.statusText=t,this.url=i,this.method=n,this.headers=r,this.data=o,this.name="WasmHttpError"}};let b=new Set(["GET","POST","PUT","PATCH","DELETE"]),_=async(e,t,i)=>{if(!t)return await e;if(t.aborted)throw i;return await new Promise((n,r)=>{let o=()=>r(i);t.addEventListener("abort",o,{once:!0}),e.then(n,r).finally(()=>{t.removeEventListener("abort",o)})})},S=async e=>{let t={"Content-Type":"application/json",Accept:"application/json","api-version":"1.0",...e.customHeaders??{}},i=e.isEncryptionEnabled??(()=>!1),n="sha256"===e.mgf1;await (0,o.o)(e.wasm.wasmPath,e.wasm.wasmSimdPath??e.wasm.wasmPath,e.wasm.glueCodePath,e.wasm.glueCodeSimdPath??"",e.wasm.useSimd??!0),await (0,o.c)(e.apiURL,"",e.encryptionEnabled??!1,t,n);let r={baseURL:e.apiURL,headers:t},a={defaults:r,async request(t){var a;let s,{method:l="GET",url:c,headers:d={},query:u,params:h,body:p,signal:f,timeout:m=e.timeout??3e4,onUploadProgress:g,ie:v=!1}=t,C=(e=>{let t=e.toUpperCase();if(!b.has(t))throw Error(`Unsupported HTTP method for WASM client: ${e}`);return t})(l),y=h??u,S=(a=r.baseURL,s=(e=>{if(!e)return"";let t=new URLSearchParams;Object.entries(e).forEach(([e,i])=>{null!=i&&t.append(e,String(i))});let i=t.toString();return i?`?${i}`:""})(y),c.startsWith("http://")||c.startsWith("https://")?`${c}${s}`:`${a.endsWith("/")?a.slice(0,-1):a}${c.startsWith("/")?c:`/${c}`}${s}`),L=v||n&&i(),E={...r.headers,...L?{"X-RSA-Encryption-Scheme":"RSA/NONE/OAEPWITHSHA-256ANDMGF1PADDING"}:{},...d},k=(e=>{if(null!=e)return e instanceof FormData||e instanceof Blob||e instanceof ArrayBuffer||e instanceof Uint8Array||"string"==typeof e||"object"==typeof e?e:String(e)})(p),x=g?e=>{e.total>0&&g(Math.round(e.loaded/e.total*100))}:void 0,I=new w(0,"Request aborted",S,C,{},null);if(f?.aborted)throw I;try{let e=await _((0,o.a)(C,c,k,E,y,m,x,v),f,I);if(e.status<200||e.status>=300)throw new w(e.status,"",S,C,{},e.data);return{ok:!0,status:e.status,statusText:"",url:S,headers:{},data:e.data}}catch(e){if(e instanceof w)throw e;throw new w((e=>{if(e instanceof o.i)return e.statusCode;if("object"==typeof e&&null!==e){if("number"==typeof e.statusCode)return e.statusCode;if("number"==typeof e.status)return e.status}return 0})(e),e instanceof Error?e.message:"WasmWebClient request failed",S,C,{},e instanceof o.i?e.response?.data??null:null)}},get:(e,t)=>a.request({...t,url:e,method:"GET"}),post:(e,t,i)=>a.request({...i,url:e,body:t,method:"POST"}),put:(e,t,i)=>a.request({...i,url:e,body:t,method:"PUT"}),patch:(e,t,i)=>a.request({...i,url:e,body:t,method:"PATCH"}),delete:(e,t)=>a.request({...t,url:e,method:"DELETE"}),head:()=>Promise.reject(Error("HEAD is not supported by the WASM HTTP client")),options:()=>Promise.reject(Error("OPTIONS is not supported by the WASM HTTP client")),setHeader(e,t){r.headers[e]=t}};return a};function L(e){(0,l.o)(e),(0,o.s)(async(t,i,n)=>{let r=await e.request({method:"POST",url:t,body:i,headers:n.headers,params:n.params,timeout:n.timeout,ie:n.ie});return{data:r.data,status:r.status}})}function E(){return n?.encryption.enabled??!1}async function k(e,t,i,r){var o;let a,s,l;L(i?await I(r.enabled,()=>S({apiURL:e,customHeaders:t.customHeaders,timeout:t.timeout,wasm:i,encryptionEnabled:r.enabled,mgf1:r.mgf1,isEncryptionEnabled:E})):(a={"Content-Type":"application/json",Accept:"application/json","api-version":"1.0",...(o={apiURL:e,customHeaders:t.customHeaders,timeout:t.timeout}).customHeaders??{}},l={defaults:s={baseURL:o.apiURL,headers:a},async request(e){var t;let i,{method:n="GET",url:r,headers:a={},query:l,params:c,body:d,signal:u,timeout:h=o.timeout??3e4,parse:p,onUploadProgress:f,keepalive:m}=e,g=(t=s.baseURL,i=(e=>{if(!e)return"";let t=new URLSearchParams;Object.entries(e).forEach(([e,i])=>{null!=i&&t.append(e,String(i))});let i=t.toString();return i?`?${i}`:""})(c??l),r.startsWith("http://")||r.startsWith("https://")?`${r}${i}`:`${t.endsWith("/")?t.slice(0,-1):t}${r.startsWith("/")?r:`/${r}`}${i}`),v={...s.headers,...a},w=null==d?null:d instanceof FormData||d instanceof Blob||d instanceof ArrayBuffer?d:"object"==typeof d?JSON.stringify(d):String(d),b=v;if(null===w&&("POST"===n||"PUT"===n||"PATCH"===n)){let{"Content-Type":e,...t}=v;b=t}if(f){let e;if(null!==w&&"u">typeof ReadableStream&&w instanceof ReadableStream)throw Error("Upload progress tracking is not supported for ReadableStream bodies");return e=b,new Promise((t,i)=>{let r=new XMLHttpRequest;r.open(n,g,!0),Object.entries(e).forEach(([e,t])=>{r.setRequestHeader(e,t)}),r.timeout=h,r.upload.onprogress=e=>{e.lengthComputable&&f(Math.round(e.loaded/e.total*100))},r.onload=()=>{let e,o={};r.getAllResponseHeaders().split("\r\n").forEach(e=>{let t=e.split(": ");2===t.length&&(o[t[0].toLowerCase()]=t[1])});try{e=(r.getResponseHeader("content-type")??"").includes("application/json")?JSON.parse(r.responseText):r.responseText}catch{e=r.responseText}r.status>=200&&r.status<300?t({ok:!0,status:r.status,statusText:r.statusText,url:g,headers:o,data:e}):i(new C(r.status,r.statusText,g,n,o,e))},r.onerror=()=>{i(new C(0,"Network Error",g,n,{},null))},r.ontimeout=()=>{i(new C(0,"Request timeout",g,n,{},null))},u&&u.addEventListener("abort",()=>{r.abort(),i(new C(0,"Request aborted",g,n,{},null))}),r.send(w)})}let _=new AbortController,S=u??_.signal,L=setTimeout(()=>_.abort(),h);try{let e=await fetch(g,{method:n,headers:b,body:w,signal:S,keepalive:m});L&&clearTimeout(L);let t={};e.headers.forEach((e,i)=>{t[i]=e});let i=await y(e,p);if(!e.ok)throw new C(e.status,e.statusText,g,n,t,i);return{ok:!0,status:e.status,statusText:e.statusText,url:g,headers:t,data:i}}catch(e){if(L&&clearTimeout(L),e instanceof C)throw e;if(e instanceof Error&&"AbortError"===e.name)throw new C(0,"Request timeout",g,n,{},null);throw e}},get:(e,t)=>l.request({...t,url:e,method:"GET"}),post:(e,t,i)=>l.request({...i,url:e,body:t,method:"POST"}),put:(e,t,i)=>l.request({...i,url:e,body:t,method:"PUT"}),patch:(e,t,i)=>l.request({...i,url:e,body:t,method:"PATCH"}),delete:(e,t)=>l.request({...t,url:e,method:"DELETE"}),head:(e,t)=>l.request({...t,url:e,method:"HEAD"}),options:(e,t)=>l.request({...t,url:e,method:"OPTIONS"}),setHeader(e,t){s.headers[e]=t}})),n={apiURL:e,customHeaders:t.customHeaders,timeout:t.timeout,isWasm:!!i,encryption:r}}async function x(e){let t=n;t&&!t.isWasm&&(L(await S({apiURL:t.apiURL,customHeaders:t.customHeaders,timeout:t.timeout,wasm:e,encryptionEnabled:t.encryption.enabled,mgf1:t.encryption.mgf1,isEncryptionEnabled:E})),n={...t,isWasm:!0})}async function I(e,t){if(!e)return t();try{return await t()}catch(t){let e=Error("Failed to establish encrypted transport. The /e2ee/key handshake did not complete. Verify the apiURL is reachable and that the environment supports E2EE.");throw e.cause=t,e}}let T="sha1";async function M(e){(0,c.g)()||(0,c.v)((0,c.m)({endpoint:d.t.events,authToken:()=>(0,l.r)(),apiBaseUrl:()=>(0,l.n)().defaults.baseURL,apiClient:l.t,pageLifecycle:new v,timer:s.t.getInstance()}));let t=function(e){if(void 0!==e)return!1===e?{enabled:!1,mgf1:T}:!0===e?{enabled:!0,mgf1:T}:{enabled:!0,mgf1:e.mgf1??T}}(e.encryption),i=n;if(t&&i&&(t.enabled!==i.encryption.enabled||t.enabled&&t.mgf1!==i.encryption.mgf1))throw Error("Encryption is locked at the first setup() call and cannot be changed at runtime. Call reset() to fully reinitialize the SDK if you need a different encryption configuration.");if(t?.enabled&&!1===e.wasm)throw Error("Encryption is incompatible with `wasm: false`. Either remove `wasm: false` or omit `encryption`.");let r=t??i?.encryption??{enabled:!1,mgf1:"sha1"},h=e.wasm||t?.enabled?g(e.wasm||{}):void 0;e.apiURL&&await k(e.apiURL,e,h,r),e.token&&(0,l.i)()&&(0,u.r)({token:e.token,hostingApp:e.hostingApp}).catch(e=>{console.warn("Session initialization failed:",e)}),h&&(await (0,o.n)(h),await (await a.t.getInstance()).initialize({...h,pipelines:h.pipelines?[...h.pipelines]:void 0}))}function A(e){r=e}async function R(e){let t=e??r;if(!t)throw Error("WASM config is required. Provide config parameter or call setWasmConfig() first.");let i=g(t);await (await a.t.getInstance()).initialize({...i,pipelines:i.pipelines?[...i.pipelines]:void 0})}async function O(e){let t=e??r;t&&await x(g(t))}function P(){(0,l.a)(),(0,u.i)(),(0,c._)(),n=void 0,(0,o.s)(null),r=void 0,a.t.resetInstance()}}