stellate 2.8.2 → 2.10.0

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.
@@ -5,6 +5,7 @@ interface Options {
5
5
  token: string;
6
6
  fetch: typeof fetch;
7
7
  sendVariablesAsHash?: boolean;
8
+ schemaSyncing?: boolean;
8
9
  }
9
10
  declare function createBlake3Hash(str: string): number;
10
11
 
@@ -1 +1,2 @@
1
- "use strict";var f=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var H=(e,r)=>{for(var t in r)f(e,t,{get:r[t],enumerable:!0})},A=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of P(r))!x.call(e,o)&&o!==t&&f(e,o,{get:()=>r[o],enumerable:!(n=w(r,o))||n.enumerable});return e};var L=e=>A(f({},"__esModule",{value:!0}),e);var T={};H(T,{createBlake3Hash:()=>c,createStellateLoggerPlugin:()=>q});module.exports=L(T);function c(e){let r=0,t=e.length;if(t===0)return r;for(let n=0;n<t;++n){let o=e.charCodeAt(n);r=(r<<5)-r+o,r&=r}return r>>>0}function m({headers:e,operation:r,method:t,start:n,operationName:o,errors:p,response:i,variables:l,sendVariablesAsHash:s,hasSetCookie:u}){let a=e.get("x-forwarded-for"),d=a?a.split(","):[];return{operation:r,operationName:o,variables:s?void 0:l,variableHash:s?c(JSON.stringify(l||{})):void 0,method:t,elapsed:Date.now()-n,ip:d[0]||e.get("true-client-ip")||e.get("x-real-ip")||void 0,hasSetCookie:u,referer:e.get("referer")||void 0,userAgent:e.get("user-agent")||void 0,statusCode:200,errors:p,responseSize:JSON.stringify(i).length,responseHash:c(JSON.stringify(i))}}function b(e){if(typeof e!="function"){console.warn("Stellate logger plugin requires a fetch function to be provided as an option.");return}}var O=process.env.STELLATE_ENDPOINT==="local"||process.env.STELLATE_ENDPOINT==="staging"?"stellate.dev":"stellate.sh";async function S({fetch:e,payload:r,token:t,serviceName:n}){return e(`https://${n}.${O}/log`,{method:"POST",body:JSON.stringify(r),headers:{"Content-Type":"application/json","Stellate-Logging-Token":t}})}var q=e=>{var t;let r=(t=e.sendVariablesAsHash)!=null?t:!0;return{async requestDidStart(n){let o=Date.now(),{request:p}=n,{operationName:i,variables:l,http:s,query:u}=p;if(!s)return;let{headers:a,method:d}=s;if(!a.has("gcdn-request-id"))return{async willSendResponse(h){let{response:g,source:v}=h;if(g.body.kind!=="single"){console.warn("Stellate does not currently support logging incremental results.");return}let k=g.body.singleResult,y=u||v;if(!y)return;let N=m({headers:a,operation:y,method:d,sendVariablesAsHash:r,start:o,operationName:i,errors:h.errors,response:k,variables:l,hasSetCookie:g.http.headers.has("set-cookie")});b(e.fetch);try{await S({fetch:e.fetch,payload:N,token:e.token,serviceName:e.serviceName})}catch{}}}}}};0&&(module.exports={createBlake3Hash,createStellateLoggerPlugin});
1
+ "use strict";var b=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var E=Object.prototype.hasOwnProperty;var C=(e,t)=>{for(var n in t)b(e,n,{get:t[n],enumerable:!0})},D=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of q(t))!E.call(e,r)&&r!==n&&b(e,r,{get:()=>t[r],enumerable:!(o=A(t,r))||o.enumerable});return e};var R=e=>D(b({},"__esModule",{value:!0}),e);var J={};C(J,{createBlake3Hash:()=>g,createStellateLoggerPlugin:()=>$});module.exports=R(J);function g(e){let t=0,n=e.length;if(n===0)return t;for(let o=0;o<n;++o){let r=e.charCodeAt(o);t=(t<<5)-t+r,t&=t}return t>>>0}function T({headers:e,operation:t,method:n,start:o,operationName:r,errors:m,response:d,variables:u,responseHeaders:s,sendVariablesAsHash:l,hasSetCookie:p}){let h=e.get("x-forwarded-for"),y=h?h.split(","):[],c=s&&s.get("vary"),f;if(c&&c.length){let v=c.split(",").map(a=>a&&a.trim()).sort().map(a=>{let i=a&&e.get(a);return i?`${a}:${i}`:void 0}).filter(Boolean).join(`
2
+ `);f=g(v)}return{operation:t,operationName:r,variables:l?void 0:u,variableHash:l?g(JSON.stringify(u||{})):void 0,method:n,elapsed:Date.now()-o,ip:y[0]||e.get("true-client-ip")||e.get("x-real-ip")||void 0,hasSetCookie:p,referer:e.get("referer")||void 0,userAgent:e.get("user-agent")||void 0,statusCode:200,errors:m,responseSize:JSON.stringify(d).length,responseHash:g(JSON.stringify(d)),varyHash:f}}function P(e){if(typeof e!="function"){console.warn("Stellate logger plugin requires a fetch function to be provided as an option.");return}}var k=process.env.STELLATE_ENDPOINT==="local"||process.env.STELLATE_ENDPOINT==="staging"?"stellate.dev":"stellate.sh";async function L({fetch:e,payload:t,token:n,serviceName:o}){return e(`https://${o}.${k}/log`,{method:"POST",body:JSON.stringify(t),headers:{"Content-Type":"application/json","Stellate-Logging-Token":n}})}var N=require("graphql");var $=e=>{var d,u;let t=(d=e.sendVariablesAsHash)!=null?d:!0,n=(u=e.schemaSyncing)!=null?u:!0,o=!1,r,m=async s=>{if(n){let l=(0,N.introspectionFromSchema)(s),p=Math.random()*5e3;r&&clearTimeout(r),r=setTimeout(async()=>{if(r=null,!o)try{e.fetch(`https://${e.serviceName}.${k}/schema`,{method:"POST",body:JSON.stringify({schema:l}),headers:{"Content-Type":"application/json","Stellate-Schema-Token":e.token}}).then(()=>{})}catch{}},p)}};return{async serverWillStart(){return{schemaDidLoadOrUpdate({apiSchema:s}){m(s)},async serverWillStop(){o=!0,r&&clearTimeout(r)}}},async requestDidStart(s){let l=Date.now(),{request:p}=s,{operationName:h,variables:y,http:c,query:f}=p;if(!c)return;let{headers:S,method:v}=c;if(!S.has("gcdn-request-id"))return{async willSendResponse(a){let{response:i,source:w}=a;if(i.body.kind!=="single"){console.warn("Stellate does not currently support logging incremental results.");return}let O=i.body.singleResult,H=f||w;if(!H)return;let x=T({headers:S,responseHeaders:i.http.headers,operation:H,method:v,sendVariablesAsHash:t,start:l,operationName:h,errors:a.errors,response:O,variables:y,hasSetCookie:i.http.headers.has("set-cookie")});P(e.fetch);try{await L({fetch:e.fetch,payload:x,token:e.token,serviceName:e.serviceName})}catch{}}}}}};0&&(module.exports={createBlake3Hash,createStellateLoggerPlugin});
@@ -21,11 +21,22 @@ function extractStellatePayload({
21
21
  errors,
22
22
  response,
23
23
  variables,
24
+ responseHeaders,
24
25
  sendVariablesAsHash,
25
26
  hasSetCookie
26
27
  }) {
27
28
  const forwardedFor = headers.get("x-forwarded-for");
28
29
  const ips = forwardedFor ? forwardedFor.split(",") : [];
30
+ const vary = responseHeaders && responseHeaders.get("vary");
31
+ let varyHash = void 0;
32
+ if (vary && vary.length) {
33
+ const varyHeaders = vary.split(",").map((headerName) => headerName && headerName.trim()).sort();
34
+ const variedValues = varyHeaders.map((headerName) => {
35
+ const headerValue = headerName && headers.get(headerName);
36
+ return headerValue ? `${headerName}:${headerValue}` : void 0;
37
+ }).filter(Boolean).join("\n");
38
+ varyHash = createBlake3Hash(variedValues);
39
+ }
29
40
  return {
30
41
  operation,
31
42
  operationName,
@@ -40,7 +51,8 @@ function extractStellatePayload({
40
51
  statusCode: 200,
41
52
  errors,
42
53
  responseSize: JSON.stringify(response).length,
43
- responseHash: createBlake3Hash(JSON.stringify(response))
54
+ responseHash: createBlake3Hash(JSON.stringify(response)),
55
+ varyHash
44
56
  };
45
57
  }
46
58
  function warnFetch(fetch) {
@@ -69,10 +81,53 @@ async function logRequest({
69
81
  }
70
82
 
71
83
  // src/apollo-server.ts
84
+ import { introspectionFromSchema } from "graphql";
72
85
  var createStellateLoggerPlugin = (options) => {
73
- var _a;
86
+ var _a, _b;
74
87
  const sendVariablesAsHash = (_a = options.sendVariablesAsHash) != null ? _a : true;
88
+ const shouldSyncSchema = (_b = options.schemaSyncing) != null ? _b : true;
89
+ let stopped = false, timeout;
90
+ const sendSchema = async (apiSchema) => {
91
+ if (shouldSyncSchema) {
92
+ const introspection = introspectionFromSchema(apiSchema);
93
+ const randomTimeout = Math.random() * 5e3;
94
+ if (timeout)
95
+ clearTimeout(timeout);
96
+ timeout = setTimeout(async () => {
97
+ timeout = null;
98
+ if (!stopped) {
99
+ try {
100
+ options.fetch(`https://${options.serviceName}.${hostname}/schema`, {
101
+ method: "POST",
102
+ body: JSON.stringify({
103
+ schema: introspection
104
+ }),
105
+ headers: {
106
+ "Content-Type": "application/json",
107
+ "Stellate-Schema-Token": options.token
108
+ }
109
+ }).then(() => {
110
+ });
111
+ } catch (e) {
112
+ }
113
+ }
114
+ }, randomTimeout);
115
+ }
116
+ };
75
117
  return {
118
+ async serverWillStart() {
119
+ return {
120
+ schemaDidLoadOrUpdate({ apiSchema }) {
121
+ sendSchema(apiSchema);
122
+ },
123
+ async serverWillStop() {
124
+ stopped = true;
125
+ if (timeout) {
126
+ clearTimeout(timeout);
127
+ }
128
+ }
129
+ };
130
+ },
76
131
  async requestDidStart(requestCtx) {
77
132
  const start = Date.now();
78
133
  const { request } = requestCtx;
@@ -97,6 +152,7 @@ var createStellateLoggerPlugin = (options) => {
97
152
  return;
98
153
  const stellatePayload = extractStellatePayload({
99
154
  headers,
155
+ responseHeaders: response.http.headers,
100
156
  operation: queryString,
101
157
  method,
102
158
  sendVariablesAsHash,