UNPKG

stellate

Version:

The CLI you need to work with Stellate from your CLI. See https://docs.stellate.co/docs/cli for the complete documentation.

3 lines (2 loc) 3.21 kB
"use strict";var S=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var O=(e,t)=>{for(var n in t)S(e,n,{get:t[n],enumerable:!0})},E=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of L(t))!w.call(e,r)&&r!==n&&S(e,r,{get:()=>t[r],enumerable:!(a=C(t,r))||a.enumerable});return e};var A=e=>E(S({},"__esModule",{value:!0}),e);var $={};O($,{createBlake3Hash:()=>p,createStellateLoggerPlugin:()=>V});module.exports=A($);var h=require("graphql"),T=require("graphql-yoga");function p(e){let t=0,n=e.length;if(n===0)return t;for(let a=0;a<n;++a){let r=e.charCodeAt(a);t=(t<<5)-t+r,t&=t}return t>>>0}function k({headers:e,operation:t,method:n,start:a,operationName:r,errors:g,response:i,variables:o,responseHeaders:l,sendVariablesAsHash:s,hasSetCookie:d,graphqlClientName:m,graphqlClientVersion:f}){let u=e.get("x-forwarded-for"),v=u?u.split(","):[],y=l&&l.get("vary"),x;if(y&&y.length){let q=y.split(",").map(c=>c&&c.trim()).sort().map(c=>{let N=c&&e.get(c);return N?`${c}:${N}`:void 0}).filter(Boolean).join(` `);x=p(q)}return{graphqlClientName:m,graphqlClientVersion:f,operation:t,operationName:r,variables:s?void 0:o,variableHash:s?p(JSON.stringify(o||{})):void 0,method:n,elapsed:Date.now()-a,ip:v[0]||e.get("true-client-ip")||e.get("x-real-ip")||void 0,hasSetCookie:d,referer:e.get("referer")||void 0,userAgent:e.get("user-agent")||void 0,statusCode:200,errors:g,responseSize:JSON.stringify(i).length,responseHash:p(JSON.stringify(i)),varyHash:x}}function H(e){if(typeof e!="function"){console.warn("Stellate logger plugin requires a fetch function to be provided as an option.");return}}var b=process.env.STELLATE_ENDPOINT==="local"||process.env.STELLATE_ENDPOINT==="staging"?"stellate.dev":"stellate.sh";async function P({fetch:e,payload:t,token:n,serviceName:a}){return e(`https://${a}.${b}/log`,{method:"POST",body:JSON.stringify(t),headers:{"Content-Type":"application/json","Stellate-Logging-Token":n}})}var V=e=>{var a;let t=(a=e.schemaSyncing)!=null?a:!0,n;return{onSchemaChange(r){if(t){let g=r.schema,i=Math.random()*5e3;n&&clearTimeout(n),n=setTimeout(async()=>{n=null;try{let o=(0,h.introspectionFromSchema)(g);e.fetch(`https://${e.serviceName}.${b}/schema`,{method:"POST",body:JSON.stringify({schema:o}),headers:{"Content-Type":"application/json","Stellate-Schema-Token":e.token}}).then(()=>{}).catch(()=>{})}catch{}},i)}},onExecute(r){var o;let g=(o=e.sendVariablesAsHash)!=null?o:!0,i=Date.now();return{async onExecuteDone({result:l}){if((0,T.isAsyncIterable)(l)){console.warn("Stellate does not currently support logging incremental results.");return}let{headers:s,method:d}=r.args.contextValue.request;if(s.has("gcdn-request-id"))return;let m=s.get("x-graphql-client-name")||void 0,f=s.get("x-graphql-client-version")||void 0,u=k({graphqlClientName:m,graphqlClientVersion:f,headers:s,operation:(0,h.print)(r.args.document),method:d,sendVariablesAsHash:g,start:i,operationName:r.args.operationName,errors:l.errors,response:l});H(e.fetch);try{await P({fetch:e.fetch,payload:u,token:e.token,serviceName:e.serviceName})}catch{}}}}}};0&&(module.exports={createBlake3Hash,createStellateLoggerPlugin});