UNPKG

altair-static

Version:

Static package for altair graphql client

7 lines (6 loc) 7.99 kB
var h=Object.defineProperty;var g=(e,t,r)=>t in e?h(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r;var m=(e,t,r)=>g(e,typeof t!="symbol"?t+"":t,r);(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const o of document.querySelectorAll('link[rel="modulepreload"]'))n(o);new MutationObserver(o=>{for(const s of o)if(s.type==="childList")for(const i of s.addedNodes)i.tagName==="LINK"&&i.rel==="modulepreload"&&n(i)}).observe(document,{childList:!0,subtree:!0});function r(o){const s={};return o.integrity&&(s.integrity=o.integrity),o.referrerPolicy&&(s.referrerPolicy=o.referrerPolicy),o.crossOrigin==="use-credentials"?s.credentials="include":o.crossOrigin==="anonymous"?s.credentials="omit":s.credentials="same-origin",s}function n(o){if(o.ep)return;o.ep=!0;const s=r(o);fetch(o.href,s)}})();let getRandomValues;const rnds8=new Uint8Array(16);function rng(){if(!getRandomValues&&(getRandomValues=typeof crypto<"u"&&crypto.getRandomValues&&crypto.getRandomValues.bind(crypto),!getRandomValues))throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");return getRandomValues(rnds8)}const byteToHex=[];for(let e=0;e<256;++e)byteToHex.push((e+256).toString(16).slice(1));function unsafeStringify(e,t=0){return byteToHex[e[t+0]]+byteToHex[e[t+1]]+byteToHex[e[t+2]]+byteToHex[e[t+3]]+"-"+byteToHex[e[t+4]]+byteToHex[e[t+5]]+"-"+byteToHex[e[t+6]]+byteToHex[e[t+7]]+"-"+byteToHex[e[t+8]]+byteToHex[e[t+9]]+"-"+byteToHex[e[t+10]]+byteToHex[e[t+11]]+byteToHex[e[t+12]]+byteToHex[e[t+13]]+byteToHex[e[t+14]]+byteToHex[e[t+15]]}const randomUUID=typeof crypto<"u"&&crypto.randomUUID&&crypto.randomUUID.bind(crypto),native={randomUUID};function v4(e,t,r){if(native.randomUUID&&!t&&!e)return native.randomUUID();e=e||{};const n=e.random||(e.rng||rng)();if(n[6]=n[6]&15|64,n[8]=n[8]&63|128,t){r=r||0;for(let o=0;o<16;++o)t[r+o]=n[o];return t}return unsafeStringify(n)}const SCRIPT_INIT_EXECUTE="init_execute",scriptRel="modulepreload",assetsURL=function(e){return"/"+e},seen={},__vitePreload=function(t,r,n){let o=Promise.resolve();if(r&&r.length>0){const s=document.getElementsByTagName("link"),i=document.querySelector("meta[property=csp-nonce]"),p=(i==null?void 0:i.nonce)||(i==null?void 0:i.getAttribute("nonce"));o=Promise.all(r.map(a=>{if(a=assetsURL(a),a in seen)return;seen[a]=!0;const l=a.endsWith(".css"),y=l?'[rel="stylesheet"]':"";if(!!n)for(let u=s.length-1;u>=0;u--){const d=s[u];if(d.href===a&&(!l||d.rel==="stylesheet"))return}else if(document.querySelector(`link[href="${a}"]${y}`))return;const c=document.createElement("link");if(c.rel=l?"stylesheet":scriptRel,l||(c.as="script",c.crossOrigin=""),c.href=a,p&&c.setAttribute("nonce",p),document.head.appendChild(c),l)return new Promise((u,d)=>{c.addEventListener("load",u),c.addEventListener("error",()=>d(new Error(`Unable to preload CSS for ${a}`)))})}))}return o.then(()=>t()).catch(s=>{const i=new Event("vite:preloadError",{cancelable:!0});if(i.payload=s,window.dispatchEvent(i),!i.defaultPrevented)throw s})};var RequestType;(function(e){e.INTROSPECTION="introspection",e.QUERY="query",e.SUBSCRIPTION="subscription"})(RequestType||(RequestType={}));class ScriptEvaluatorWorker{}const ModuleImports={atob:{async exec(){return(await __vitePreload(async()=>{const{atob:e}=await import("./index-CzXqqJF2.js").then(t=>t.i);return{atob:e}},[])).atob}},btoa:{async exec(){return(await __vitePreload(async()=>{const{btoa:e}=await import("./index-CzXqqJF2.js").then(t=>t.i);return{btoa:e}},[])).btoa}},"crypto-js":{async exec(){return(await __vitePreload(async()=>{const{default:e}=await import("./index-CYpZL7_S.js").then(t=>t.i);return{default:e}},[])).default}}},buildContextResponse=e=>{if(e.response)return{body:e.response.body,requestType:e.requestType??RequestType.QUERY,responseTime:e.response.responseTime,statusCode:e.response.status,headers:e.response.headers}},importModuleHelper=async e=>{const t=ModuleImports[e];if(!t)throw new Error(`No request script module found matching "${e}"`);return t.exec()},getGlobalContext=(e,t)=>({data:e,helpers:{getEnvironment:r=>e.environment[r],setEnvironment:(r,n,o=!1)=>{e.environment[r]=n,o&&(e.__toSetActiveEnvironment=e.__toSetActiveEnvironment??{},e.__toSetActiveEnvironment[r]=n)},getCookie:r=>{var n;return((n=e.__cookieJar)==null?void 0:n[r])??""},setCookie:(r,n,o)=>{t.setCookie(r,n,o)},request:async(r,n,o)=>t.request(r,n,o)},storage:{get:r=>t.getStorageItem(r),set:(r,n)=>t.setStorageItem(r,n)},importModule:r=>importModuleHelper(r),response:buildContextResponse(e),log:r=>{e.requestScriptLogs=e.requestScriptLogs??[],e.requestScriptLogs.push({time:Date.now(),text:JSON.stringify(r,null,2),source:"Request script"})}}),workerHandlerNames=["setCookie","request","getStorageItem","setStorageItem"];class ScriptEvaluatorWorkerEngine{constructor(e){this.worker=e}start(){this.worker.onMessage(async e=>{switch(e.type){case SCRIPT_INIT_EXECUTE:try{await this.initExecute(e.payload)}catch(t){await this.makeCall("scriptError",t)}break}})}async initExecute(payload){const[script,data]=payload,res=await new Promise((resolve,reject)=>{this.worker.onError(e=>{reject(e)});const clonedMutableData=JSON.parse(JSON.stringify(data)),handlers=workerHandlerNames.reduce((e,t)=>(e[t]=(...r)=>this.makeCall(t,...r),e),{}),context={altair:getGlobalContext(clonedMutableData,handlers),alert:e=>this.makeCall("alert",e)},contextEntries=Object.entries(context);try{const res=(function(){return eval(` (async(${contextEntries.map(e=>e[0]).join(",")}) => { ${script}; return altair.data; })(...this.__ctxE.map(e => e[1])); `)}).call({__ctxE:contextEntries});return resolve(res)}catch(e){return reject(e)}});res.__toSetActiveEnvironment&&this.makeCall("updateActiveEnvironment",res.__toSetActiveEnvironment),this.makeCall("executeComplete",res)}makeCall(e,...t){return new Promise((r,n)=>{const o=v4();this.worker.onMessage(s=>{switch(s.type){case`${e}_response`:return s.payload.id!==o?void 0:r(s.payload.response);case`${e}_error`:return s.payload.id!==o?void 0:n(s.payload.error)}}),this.worker.send(e,{id:o,args:t})})}}class EvaluatorFrameWorker extends ScriptEvaluatorWorker{constructor(){super();m(this,"origin");const n=new URLSearchParams(window.location.search).get("sc");if(!n)throw new Error("Invalid source provided!");this.origin=n}onMessage(r){window.addEventListener("message",n=>{n.origin===this.origin&&r(n.data)})}send(r,n){window.parent.postMessage({type:r,payload:n},this.origin)}onError(r){window.addEventListener("error",r),window.addEventListener("unhandledrejection",n=>{n.preventDefault(),r(n.reason)})}}const injectScript=e=>new Promise((t,r)=>{const n=document.getElementsByTagName("head")[0];if(!n)return r(new Error("No head found!"));const o=document.createElement("script");o.type="text/javascript",o.src=e,o.onload=()=>t(null),o.onerror=s=>r(s),n.appendChild(o)}),injectStylesheet=e=>new Promise((t,r)=>{const n=document.getElementsByTagName("head")[0];if(!n)return r(new Error("No head found!"));const o=document.createElement("link");o.type="text/css",o.rel="stylesheet",o.href=e,o.onload=()=>t(null),o.onerror=s=>r(s),n.appendChild(o)}),handlePluginSandbox=async()=>{const t=new URLSearchParams(window.location.search).get("plugin_sandbox_opts"),r=t?JSON.parse(t):void 0;if(!r)throw new Error("Invalid plugin options provided!");if(r.type!=="scripts")throw new Error("Invalid plugin option type provided!");if(document.querySelectorAll('style,link[rel="stylesheet"]').forEach(n=>n.remove()),r.styleUrls)for(const n of r.styleUrls)await injectStylesheet(n);if(r.scriptUrls)for(const n of r.scriptUrls)await injectScript(n)},searchParams=new URLSearchParams(window.location.search),sandboxType=searchParams.get("sandbox_type");switch(sandboxType){case"plugin":handlePluginSandbox();break;case"script":default:new ScriptEvaluatorWorkerEngine(new EvaluatorFrameWorker).start()}