UNPKG

@interopio/gateway

Version:

[![npm version](https://img.shields.io/npm/v/@interopio/gateway.svg)](https://www.npmjs.com/package/@interopio/gateway)

3 lines (2 loc) 2.87 kB
async function c(e){e.bodyUsed||await e.blob()}var h=new Set([200,201,202]);function b({user:e,password:t}){let n=`${e}:${t}`;return typeof process<"u"?Buffer.from(n).toString("base64"):globalThis.btoa(n)}var S="rest";async function w(e,{logger:t,fetchFn:n,headers:r,timeout:i}){t.enabledFor("debug")&&t.debug(`sending initial GET to do auth dance. url: ${e}`);try{let s=await n(e,l(r),i);if(s.ok){if(t.info("initial GET request completed successfully."),t.enabledFor("debug")){let a=s.headers.getSetCookie();a.length>0&&t.enabledFor("debug")&&t.debug(`will set following cookies: ${a}`)}}else t.warn(`initial GET request did not completed successfully. Status: ${s.status}`)}catch(s){throw t.warn("error executing initial GET request",s),s}}async function p(e,t){if(!f(e)&&e!==!1){let n=new URL(t.endpoint);e?.path&&(n.pathname=e.path),await w(n,t)}t.logger.info("rest publisher started")}function y({logger:e}){e.info("rest publisher stopped")}function l(e,t){return t?{method:"POST",redirect:"manual",credentials:"include",headers:e,body:JSON.stringify(t)}:{method:"GET",redirect:"follow",credentials:"include",headers:e}}async function m(e,t,{endpoint:n,headers:r,timeout:i,fetchFn:s,logger:a}){let u=l(r,e);try{let o=await s(n,u,i);if(o.redirected){a.info("Redirect detected will do GET to allow auth dance and re-POST"),await c(o);let d=await s(n,l(r),i);await c(d),d.ok?o=await s(n,u,i):a.error(`GET failed with status: ${d.status} headers: ${JSON.stringify(d.headers)}`)}await c(o),t.prev=t.now,t.now=h.has(o.status),t.prev&&!t.now?a.error(`Unable to publish metrics. status ${o.status} headers: ${JSON.stringify(o.headers)} msg: ${JSON.stringify(e)}`):t.now&&!t.prev&&a.info("Publishing metrics.")}catch(o){let d=t.now;t.prev=t.now,t.now=!1,d&&a.error(`Unable to publish metrics. msg ${JSON.stringify(e)}`,o)}}function f(e){return e!==void 0&&e&&e.user!==void 0}var g=()=>async(e,t,n)=>{let r=new AbortController,i=!1,s=setTimeout(()=>{i=!0,r.abort(`Request to ${e} timed out after ${n} ms`)},n);try{return await fetch(e,{...t,signal:r.signal}).catch(a=>{throw i?new Error(`Request ${JSON.stringify(e)} timed out`):a})}finally{s!==void 0&&clearTimeout(s)}};function C(e,t){let n=e.endpoint,r=e.timeout??1e3,i=new Headers(e.headers);i.has("Content-Type")||i.set("Content-Type","application/json"),i.has("User-Agent")||i.set("User-Agent",e["user-agent"]??"metrics-rest/0.2.x"),f(e.authentication)&&!i.has("Authorization")&&i.set("Authorization",`Basic ${b(e.authentication)}`);let s=e.fetch??g(),a={prev:!1,now:!0},u={endpoint:n,headers:i,timeout:r,fetchFn:s,logger:t};return async o=>{switch(t.enabledFor("debug")&&t.debug(`handling publish request: ${JSON.stringify(o)}`),o){case"start":{await p(e.authentication,u);break}case"stop":{y(u);break}default:{await m(o,a,u);break}}}}export{C as create,g as fetchWithTimeout,S as name}; //# sourceMappingURL=rest.js.map