@interopio/gateway
Version:
[](https://www.npmjs.com/package/@interopio/gateway)
3 lines (2 loc) • 3.08 kB
JavaScript
;var l=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var y=(t,e)=>{for(var n in e)l(t,n,{get:e[n],enumerable:!0})},m=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of b(e))!w.call(t,i)&&i!==n&&l(t,i,{get:()=>e[i],enumerable:!(r=p(e,i))||r.enumerable});return t};var g=t=>m(l({},"__esModule",{value:!0}),t);var P={};y(P,{create:()=>G,name:()=>T});module.exports=g(P);async function c(t){t.bodyUsed||await t.blob()}var S=new Set([200,201,202]);function C({user:t,password:e}){let n=`${t}:${e}`;return typeof process<"u"?Buffer.from(n).toString("base64"):globalThis.btoa(n)}var T="rest";async function O(t,{logger:e,fetchFn:n,headers:r,timeout:i}){e.enabledFor("debug")&&e.debug(`sending initial GET to do auth dance. url: ${t}`);try{let a=await n(t,f(i,r));if(a.ok){if(e.info("initial GET request completed successfully."),e.enabledFor("debug")){let o=a.headers.getSetCookie();o.length>0&&e.enabledFor("debug")&&e.debug(`will set following cookies: ${o}`)}}else e.warn(`initial GET request did not completed successfully. Status: ${a.status}`)}catch(a){throw e.warn("error executing initial GET request",a),a}}async function R(t,e){if(!h(t)&&t!==!1){let n=new URL(e.endpoint);t?.path&&(n.pathname=t.path),await O(n,e)}e.logger.info("rest publisher started")}function U({logger:t}){t.info("rest publisher stopped")}function f(t,e,n){return n?{method:"POST",redirect:"manual",credentials:"include",signal:AbortSignal.timeout(t),headers:e,body:JSON.stringify(n)}:{method:"GET",redirect:"follow",credentials:"include",signal:AbortSignal.timeout(t),headers:e}}async function $(t,e,{endpoint:n,headers:r,timeout:i,fetchFn:a,logger:o}){let u=f(i,r,t);try{let s=await a(n,u);if(s.redirected){o.info("Redirect detected will do GET to allow auth dance and re-POST"),await c(s);let d=await a(n,f(i,r));await c(d),d.ok?s=await a(n,u):o.error(`GET failed with status: ${d.status} headers: ${JSON.stringify(d.headers)}`)}await c(s),e.prev=e.now,e.now=S.has(s.status),e.prev&&!e.now?o.error(`Unable to publish metrics. status ${s.status} headers: ${JSON.stringify(s.headers)} msg: ${JSON.stringify(t)}`):e.now&&!e.prev&&o.info("Publishing metrics.")}catch(s){let d=e.now;e.prev=e.now,e.now=!1,d&&o.error(`Unable to publish metrics. msg ${JSON.stringify(t)}`,s)}}function h(t){return t!==void 0&&t&&t.user!==void 0}function G(t,e){let n=t.endpoint,r=t.timeout??1e3,i=new Headers(t.headers);i.has("Content-Type")||i.set("Content-Type","application/json"),i.has("User-Agent")||i.set("User-Agent",t["user-agent"]??"metrics-rest/0.2.x"),h(t.authentication)&&!i.has("Authorization")&&i.set("Authorization",`Basic ${C(t.authentication)}`);let a=t.fetch??globalThis.fetch,o={prev:!1,now:!0},u={endpoint:n,headers:i,timeout:r,fetchFn:a,logger:e};return async s=>{switch(e.enabledFor("debug")&&e.debug(`handling publish request: ${JSON.stringify(s)}`),s){case"start":{await R(t.authentication,u);break}case"stop":{U(u);break}default:{await $(s,o,u);break}}}}
//# sourceMappingURL=rest.cjs.map