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) 3.33 kB
"use strict";var c=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var m=(e,t)=>{for(var i in t)c(e,i,{get:t[i],enumerable:!0})},g=(e,t,i,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of p(t))!y.call(e,n)&&n!==i&&c(e,n,{get:()=>t[n],enumerable:!(a=w(t,n))||a.enumerable});return e};var S=e=>g(c({},"__esModule",{value:!0}),e);var E={};m(E,{create:()=>P,fetchWithTimeout:()=>b,name:()=>T});module.exports=S(E);async function l(e){e.bodyUsed||await e.blob()}var C=new Set([200,201,202]);function R({user:e,password:t}){let i=`${e}:${t}`;return typeof process<"u"?Buffer.from(i).toString("base64"):globalThis.btoa(i)}var T="rest";async function O(e,{logger:t,fetchFn:i,headers:a,timeout:n}){t.enabledFor("debug")&&t.debug(`sending initial GET to do auth dance. url: ${e}`);try{let s=await i(e,f(a),n);if(s.ok){if(t.info("initial GET request completed successfully."),t.enabledFor("debug")){let r=s.headers.getSetCookie();r.length>0&&t.enabledFor("debug")&&t.debug(`will set following cookies: ${r}`)}}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 $(e,t){if(!h(e)&&e!==!1){let i=new URL(t.endpoint);e?.path&&(i.pathname=e.path),await O(i,t)}t.logger.info("rest publisher started")}function U({logger:e}){e.info("rest publisher stopped")}function f(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 G(e,t,{endpoint:i,headers:a,timeout:n,fetchFn:s,logger:r}){let u=f(a,e);try{let o=await s(i,u,n);if(o.redirected){r.info("Redirect detected will do GET to allow auth dance and re-POST"),await l(o);let d=await s(i,f(a),n);await l(d),d.ok?o=await s(i,u,n):r.error(`GET failed with status: ${d.status} headers: ${JSON.stringify(d.headers)}`)}await l(o),t.prev=t.now,t.now=C.has(o.status),t.prev&&!t.now?r.error(`Unable to publish metrics. status ${o.status} headers: ${JSON.stringify(o.headers)} msg: ${JSON.stringify(e)}`):t.now&&!t.prev&&r.info("Publishing metrics.")}catch(o){let d=t.now;t.prev=t.now,t.now=!1,d&&r.error(`Unable to publish metrics. msg ${JSON.stringify(e)}`,o)}}function h(e){return e!==void 0&&e&&e.user!==void 0}var b=()=>async(e,t,i)=>{let a=new AbortController,n=!1,s=setTimeout(()=>{n=!0,a.abort(`Request to ${e} timed out after ${i} ms`)},i);try{return await fetch(e,{...t,signal:a.signal}).catch(r=>{throw n?new Error(`Request ${JSON.stringify(e)} timed out`):r})}finally{s!==void 0&&clearTimeout(s)}};function P(e,t){let i=e.endpoint,a=e.timeout??1e3,n=new Headers(e.headers);n.has("Content-Type")||n.set("Content-Type","application/json"),n.has("User-Agent")||n.set("User-Agent",e["user-agent"]??"metrics-rest/0.2.x"),h(e.authentication)&&!n.has("Authorization")&&n.set("Authorization",`Basic ${R(e.authentication)}`);let s=e.fetch??b(),r={prev:!1,now:!0},u={endpoint:i,headers:n,timeout:a,fetchFn:s,logger:t};return async o=>{switch(t.enabledFor("debug")&&t.debug(`handling publish request: ${JSON.stringify(o)}`),o){case"start":{await $(e.authentication,u);break}case"stop":{U(u);break}default:{await G(o,r,u);break}}}} //# sourceMappingURL=rest.cjs.map