stellate 2.4.0 → 2.5.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.
@@ -6,8 +6,8 @@ interface Options {
6
6
  fetch: typeof fetch;
7
7
  sendVariablesAsHash?: boolean;
8
8
  }
9
-
10
9
  declare function createBlake3Hash(str: string): number;
10
+
11
11
  declare const createStellateLoggerPlugin: (options: Options) => ApolloServerPlugin;
12
12
 
13
13
  export { createBlake3Hash, createStellateLoggerPlugin };
@@ -1 +1 @@
1
- "use strict";var l=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var w=(t,e)=>{for(var r in e)l(t,r,{get:e[r],enumerable:!0})},P=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of v(e))!A.call(t,s)&&s!==r&&l(t,s,{get:()=>e[s],enumerable:!(n=m(e,s))||n.enumerable});return t};var k=t=>P(l({},"__esModule",{value:!0}),t);var H={};w(H,{createBlake3Hash:()=>i,createStellateLoggerPlugin:()=>O});module.exports=k(H);function i(t){let e=0,r=t.length;if(r===0)return e;for(let n=0;n<r;++n){let s=t.charCodeAt(n);e=(e<<5)-e+s,e&=e}return e>>>0}var O=t=>{let e=t.sendVariablesAsHash!=null?t.sendVariablesAsHash:!1;return{async requestDidStart(r){let n=Date.now(),{request:s}=r,{operationName:h,query:d,variables:c,http:p}=s;if(!p||!d)return;let{headers:o,method:y}=p;if(!o.has("gcdn-request-id"))return{async willSendResponse(u){let{response:a}=u;if(a.body.kind!=="single"){console.warn("Stellate does not currently support logging incremental results.");return}let g=a.body.singleResult,f=o.get("x-forwarded-for"),S=f?f.split(","):[],b={operation:d,operationName:h,variables:e?void 0:c,variableHash:e?i(JSON.stringify(c)):void 0,hasSetCookie:a.http.headers.has("set-cookie"),method:y,elapsed:Date.now()-n,ip:S[0]||o.get("true-client-ip")||o.get("x-real-ip"),referer:o.get("referer"),userAgent:o.get("user-agent"),statusCode:a.http.status||200,errors:u.errors,responseSize:JSON.stringify(g).length,responseHash:i(JSON.stringify(g))};if(typeof t.fetch!="function"){console.warn("Stellate logger plugin requires a fetch function to be provided as an option.");return}await t.fetch(`https://${t.serviceName}.stellate.sh/log`,{method:"POST",body:JSON.stringify(b),headers:{"Content-Type":"application/json","Stellate-Logging-Token":t.token}})}}}}};0&&(module.exports={createBlake3Hash,createStellateLoggerPlugin});
1
+ "use strict";var f=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var N=Object.prototype.hasOwnProperty;var w=(e,r)=>{for(var t in r)f(e,t,{get:r[t],enumerable:!0})},P=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of k(r))!N.call(e,s)&&s!==t&&f(e,s,{get:()=>r[s],enumerable:!(n=v(r,s))||n.enumerable});return e};var H=e=>P(f({},"__esModule",{value:!0}),e);var L={};w(L,{createBlake3Hash:()=>p,createStellateLoggerPlugin:()=>A});module.exports=H(L);function p(e){let r=0,t=e.length;if(t===0)return r;for(let n=0;n<t;++n){let s=e.charCodeAt(n);r=(r<<5)-r+s,r&=r}return r>>>0}function h({headers:e,operation:r,method:t,start:n,operationName:s,errors:g,response:o,variables:i,sendVariablesAsHash:a,hasSetCookie:l}){let c=e.get("x-forwarded-for"),d=c?c.split(","):[];return{operation:r,operationName:s,variables:a?void 0:i,variableHash:a?p(JSON.stringify(i||{})):void 0,method:t,elapsed:Date.now()-n,ip:d[0]||e.get("true-client-ip")||e.get("x-real-ip")||void 0,hasSetCookie:l,referer:e.get("referer")||void 0,userAgent:e.get("user-agent")||void 0,statusCode:200,errors:g,responseSize:JSON.stringify(o).length,responseHash:p(JSON.stringify(o))}}function y(e){if(typeof e!="function"){console.warn("Stellate logger plugin requires a fetch function to be provided as an option.");return}}var x=process.env.STELLATE_ENDPOINT==="local"||process.env.STELLATE_ENDPOINT==="staging"?"stellate.dev":"stellate.sh";async function m({fetch:e,payload:r,token:t,serviceName:n}){return e(`https://${n}.${x}/log`,{method:"POST",body:JSON.stringify(r),headers:{"Content-Type":"application/json","Stellate-Logging-Token":t}})}var A=e=>{let r=e.sendVariablesAsHash!=null?e.sendVariablesAsHash:!1;return{async requestDidStart(t){let n=Date.now(),{request:s}=t,{operationName:g,query:o,variables:i,http:a}=s;if(!a||!o)return;let{headers:l,method:c}=a;if(!l.has("gcdn-request-id"))return{async willSendResponse(d){let{response:u}=d;if(u.body.kind!=="single"){console.warn("Stellate does not currently support logging incremental results.");return}let b=u.body.singleResult,S=h({headers:l,operation:o,method:c,sendVariablesAsHash:r,start:n,operationName:g,errors:d.errors,response:b,variables:i,hasSetCookie:u.http.headers.has("set-cookie")});y(e.fetch),await m({fetch:e.fetch,payload:S,token:e.token,serviceName:e.serviceName})}}}}};0&&(module.exports={createBlake3Hash,createStellateLoggerPlugin});
@@ -1,7 +1,7 @@
1
1
  import { createRequire } from 'module';
2
2
  const require = createRequire(import.meta.url);
3
3
 
4
- // src/apollo-server.ts
4
+ // src/server-plugins-shared.ts
5
5
  function createBlake3Hash(str) {
6
6
  let val = 0;
7
7
  const strlen = str.length;
@@ -15,6 +15,63 @@ function createBlake3Hash(str) {
15
15
  }
16
16
  return val >>> 0;
17
17
  }
18
+ function extractStellatePayload({
19
+ headers,
20
+ operation,
21
+ method,
22
+ start,
23
+ operationName,
24
+ errors,
25
+ response,
26
+ variables,
27
+ sendVariablesAsHash,
28
+ hasSetCookie
29
+ }) {
30
+ const forwardedFor = headers.get("x-forwarded-for");
31
+ const ips = forwardedFor ? forwardedFor.split(",") : [];
32
+ return {
33
+ operation,
34
+ operationName,
35
+ variables: sendVariablesAsHash ? void 0 : variables,
36
+ variableHash: sendVariablesAsHash ? createBlake3Hash(JSON.stringify(variables || {})) : void 0,
37
+ method,
38
+ elapsed: Date.now() - start,
39
+ ip: ips[0] || headers.get("true-client-ip") || headers.get("x-real-ip") || void 0,
40
+ hasSetCookie,
41
+ referer: headers.get("referer") || void 0,
42
+ userAgent: headers.get("user-agent") || void 0,
43
+ statusCode: 200,
44
+ errors,
45
+ responseSize: JSON.stringify(response).length,
46
+ responseHash: createBlake3Hash(JSON.stringify(response))
47
+ };
48
+ }
49
+ function warnFetch(fetch) {
50
+ if (typeof fetch !== "function") {
51
+ console.warn(
52
+ `Stellate logger plugin requires a fetch function to be provided as an option.`
53
+ );
54
+ return;
55
+ }
56
+ }
57
+ var hostname = process.env.STELLATE_ENDPOINT === "local" || process.env.STELLATE_ENDPOINT === "staging" ? "stellate.dev" : "stellate.sh";
58
+ async function logRequest({
59
+ fetch: fetchFn,
60
+ payload,
61
+ token,
62
+ serviceName
63
+ }) {
64
+ return fetchFn(`https://${serviceName}.${hostname}/log`, {
65
+ method: "POST",
66
+ body: JSON.stringify(payload),
67
+ headers: {
68
+ "Content-Type": "application/json",
69
+ "Stellate-Logging-Token": token
70
+ }
71
+ });
72
+ }
73
+
74
+ // src/apollo-server.ts
18
75
  var createStellateLoggerPlugin = (options) => {
19
76
  const sendVariablesAsHash = options.sendVariablesAsHash != null ? options.sendVariablesAsHash : false;
20
77
  return {
@@ -37,41 +94,25 @@ var createStellateLoggerPlugin = (options) => {
37
94
  return;
38
95
  }
39
96
  const respBody = response.body.singleResult;
40
- const forwardedFor = headers.get("x-forwarded-for");
41
- const ips = forwardedFor ? forwardedFor.split(",") : [];
42
- const payload = {
97
+ const stellatePayload = extractStellatePayload({
98
+ headers,
43
99
  operation: query,
44
- operationName,
45
- variables: sendVariablesAsHash ? void 0 : variables,
46
- variableHash: sendVariablesAsHash ? createBlake3Hash(JSON.stringify(variables)) : void 0,
47
- hasSetCookie: response.http.headers.has("set-cookie"),
48
100
  method,
49
- elapsed: Date.now() - start,
50
- ip: ips[0] || headers.get("true-client-ip") || headers.get("x-real-ip"),
51
- referer: headers.get("referer"),
52
- userAgent: headers.get("user-agent"),
53
- statusCode: response.http.status || 200,
101
+ sendVariablesAsHash,
102
+ start,
103
+ operationName,
54
104
  errors: respContext.errors,
55
- responseSize: JSON.stringify(respBody).length,
56
- responseHash: createBlake3Hash(JSON.stringify(respBody))
57
- };
58
- if (typeof options.fetch !== "function") {
59
- console.warn(
60
- `Stellate logger plugin requires a fetch function to be provided as an option.`
61
- );
62
- return;
63
- }
64
- await options.fetch(
65
- `https://${options.serviceName}.stellate.sh/log`,
66
- {
67
- method: "POST",
68
- body: JSON.stringify(payload),
69
- headers: {
70
- "Content-Type": "application/json",
71
- "Stellate-Logging-Token": options.token
72
- }
73
- }
74
- );
105
+ response: respBody,
106
+ variables,
107
+ hasSetCookie: response.http.headers.has("set-cookie")
108
+ });
109
+ warnFetch(options.fetch);
110
+ await logRequest({
111
+ fetch: options.fetch,
112
+ payload: stellatePayload,
113
+ token: options.token,
114
+ serviceName: options.serviceName
115
+ });
75
116
  }
76
117
  };
77
118
  }