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