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.16 kB
"use strict";var c=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var m=(e,t)=>{for(var n in t)c(e,n,{get:t[n],enumerable:!0})},g=(e,t,n,d)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of w(t))!y.call(e,s)&&s!==n&&c(e,s,{get:()=>t[s],enumerable:!(d=b(t,s))||d.enumerable});return e};var S=e=>g(c({},"__esModule",{value:!0}),e);var E={};m(E,{create:()=>P,name:()=>U});module.exports=S(E);async function f(e){e.bodyUsed||await e.blob()}var C=new Set([200,201,202]);function R({user:e,password:t}){let n=`${e}:${t}`;return typeof process<"u"?Buffer.from(n).toString("base64"):globalThis.btoa(n)}var U="rest";async function T(e,{logger:t,fetch:n,headers:d,timeout:s}){t.enabledFor("debug")&&t.debug(`sending initial GET to do auth dance. url: ${e}`);try{let i=await n(e,h(s,d));if(i.ok){if(t.info("initial GET request completed successfully."),t.enabledFor("debug")){let a=i.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: ${i.status}`)}catch(i){throw t.warn("error executing initial GET request",i),i}}async function L(e,t){if(!p(e)&&e!==!1){let n=new t.URL(t.endpoint);e?.path&&(n.pathname=e.path),await T(n,t)}t.logger.info("rest publisher started")}function H({logger:e}){e.info("rest publisher stopped")}function h(e,t,n){return n?{method:"POST",redirect:"manual",credentials:"include",signal:AbortSignal.timeout(e),headers:t,body:JSON.stringify(n)}:{method:"GET",redirect:"follow",credentials:"include",signal:AbortSignal.timeout(e),headers:t}}async function $(e,t,{endpoint:n,headers:d,timeout:s,fetch:i,logger:a}){let u=h(s,d,e);try{let o=await i(n,u);if(o.redirected){a.info("Redirect detected will do GET to allow auth dance and re-POST"),await f(o);let r=await i(n,h(s,d));await f(r),r.ok?o=await i(n,u):a.error(`GET failed with status: ${r.status} headers: ${JSON.stringify(r.headers)}`)}await f(o),t.prev=t.now,t.now=C.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 r=t.now;t.prev=t.now,t.now=!1,r&&a.error(`Unable to publish metrics. msg ${JSON.stringify(e)}`,o)}}function p(e){return e!==void 0&&e&&e.user!==void 0}async function P(e,t){let n=e.endpoint,d=e.timeout??1e3,s=e.Headers??globalThis.Headers??Headers,i=new s(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"),p(e.authentication)&&!i.has("Authorization")&&i.set("Authorization",`Basic ${R(e.authentication)}`);let a=e.fetch??globalThis.fetch??fetch,u=e.URL??globalThis.URL??URL,o={prev:!1,now:!0},r={endpoint:n,headers:i,timeout:d,fetch:a,URL:u,logger:t};return async l=>{switch(t.enabledFor("debug")&&t.debug(`handling publish request: ${JSON.stringify(l)}`),l){case"start":{await L(e.authentication,r);break}case"stop":{H(r);break}default:{await $(l,o,r);break}}}} //# sourceMappingURL=rest.cjs.map