@interopio/gateway
Version:
[](https://www.npmjs.com/package/@interopio/gateway)
3 lines (2 loc) • 6.46 kB
JavaScript
var $=Object.defineProperty;var y=(e,r)=>()=>(e&&(r=e(e=0)),r);var h=(e,r)=>{for(var t in r)$(e,t,{get:r[t],enumerable:!0})};var G={};h(G,{configure:()=>K,getLogger:()=>p,logEvent:()=>A});function A(e){let{name:r,level:t}=e;I(r),C(t,r)&&w(e)}function x(e,r,t,...n){this.enabledFor(r)&&w({time:new Date,level:r,name:e,message:t,data:n})}function C(e,r){let t=u[e];return f[r]<=t}function J(e){I(e);let r=function(n,s,...o){x.call(r,e,n,s,...o)};function t(n){return isNaN(Number(n))}for(let n of Object.keys(u).filter(t))r[n]=function(s,...o){x.call(r,e,n,s,...o)};return r.enabledFor=function(n){return C(n,e)},r.child=function(n){return p(`${e}.${n}`)},r}function I(e){if(!e.startsWith(d))throw new Error(`Logger name must start with ${d}`);if(!f[e]){let r=Object.entries(f).sort(([n],[s])=>s.localeCompare(n)),[,t]=r.find(([n])=>e.startsWith(n));f[e]=t}}function p(e){let r=T[e];return r===void 0&&(r=J(e),T[e]=r),r}function K(e){function r(n,s){for(let o of Object.keys(f).filter(g=>g.startsWith(n)))f[o]=u[s]}let t=e.level;if(typeof t=="string")f[d]=u[t],r(d,t);else if(typeof t=="object"){let n=Object.entries(t).sort(([s],[o])=>s.localeCompare(o));for(let[s,o]of n)r(s,o)}w=e.appender??w}var u,T,f,d,w,k=y(()=>{"use strict";u=(o=>(o[o.trace=0]="trace",o[o.debug=1]="debug",o[o.info=2]="info",o[o.warn=3]="warn",o[o.error=4]="error",o))(u||{}),T=Object.create(null),f={},d="gateway";f[d]=0;w=e=>{console[e.level](`${e.time.toISOString()} ${e.level.toUpperCase()} [${e.name}] - ${e.message}`,...e.data)}});var R,U,L,E=y(()=>{R=[EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError,globalThis.DOMException,globalThis.AssertionError,globalThis.SystemError].filter(Boolean).map(e=>[e.name,e]),U=new Map(R),L=U});var W={};h(W,{NonError:()=>b,deserializeError:()=>_,errorConstructors:()=>L,isErrorLike:()=>O,serializeError:()=>F});function F(e,r={}){let{maxDepth:t=Number.POSITIVE_INFINITY,useToJSON:n=!0}=r;return typeof e=="object"&&e!==null?P({from:e,seen:[],forceEnumerable:!0,maxDepth:t,depth:0,useToJSON:n,serialize:!0}):typeof e=="function"?`[Function: ${e.name||"anonymous"}]`:e}function _(e,r={}){let{maxDepth:t=Number.POSITIVE_INFINITY}=r;if(e instanceof Error)return e;if(D(e)){let n=S(e.name);return P({from:e,seen:[],to:new n,maxDepth:t,depth:0,serialize:!1})}return new b(e)}function O(e){return!!e&&typeof e=="object"&&"name"in e&&"message"in e&&"stack"in e}function D(e){return!!e&&typeof e=="object"&&"message"in e&&!Array.isArray(e)}var b,z,v,B,S,P,j=y(()=>{E();E();b=class e extends Error{name="NonError";constructor(r){super(e._prepareSuperMessage(r))}static _prepareSuperMessage(r){try{return JSON.stringify(r)}catch{return String(r)}}},z=[{property:"name",enumerable:!1},{property:"message",enumerable:!1},{property:"stack",enumerable:!1},{property:"code",enumerable:!0},{property:"cause",enumerable:!1}],v=new WeakSet,B=e=>{v.add(e);let r=e.toJSON();return v.delete(e),r},S=e=>L.get(e)??Error,P=({from:e,seen:r,to:t,forceEnumerable:n,maxDepth:s,depth:o,useToJSON:g,serialize:l})=>{if(!t)if(Array.isArray(e))t=[];else if(!l&&O(e)){let i=S(e.name);t=new i}else t={};if(r.push(e),o>=s)return t;if(g&&typeof e.toJSON=="function"&&!v.has(e))return B(e);let m=i=>P({from:i,seen:[...r],forceEnumerable:n,maxDepth:s,depth:o,useToJSON:g,serialize:l});for(let[i,c]of Object.entries(e)){if(c&&c instanceof Uint8Array&&c.constructor.name==="Buffer"){t[i]="[object Buffer]";continue}if(c!==null&&typeof c=="object"&&typeof c.pipe=="function"){t[i]="[object Stream]";continue}if(typeof c!="function"){if(!c||typeof c!="object"){try{t[i]=c}catch{}continue}if(!r.includes(e[i])){o++,t[i]=m(e[i]);continue}t[i]="[Circular]"}}for(let{property:i,enumerable:c}of z)typeof e[i]<"u"&&e[i]!==null&&Object.defineProperty(t,i,{value:O(e[i])?m(e[i]):e[i],enumerable:n?!0:c,configurable:!0,writable:!0});return t}});var N={};h(N,{default:()=>V});var a,V,M=y(()=>{"use strict";a={send:()=>{},on:()=>{},onmessage:null,onerror:null,exit:()=>{},findParameter(e){let r=new URLSearchParams(globalThis.location?.search).get(e);if(!r){let t=globalThis.process?.argv.filter(n=>n.startsWith("--")).map(n=>{let s=n.slice(2).split("=");return[s[0],s[1]]}).find(([n,s])=>n===e);t&&([,r]=t)}return r},async streamLogs(e="trace"){let r=t=>{a.send({log:{time:t.time.getTime(),level:t.level,name:t.name,message:t.message,data:t.data}})};(await Promise.resolve().then(()=>(k(),G))).configure({level:e,appender:r})},async importModule(e){return await import(e)}};if(typeof self<"u"&&typeof postMessage=="function"&&typeof addEventListener=="function")a.on=(e,r)=>{addEventListener(e,t=>{r(t)})},a.send=e=>{postMessage(e)},typeof close=="function"&&(a.exit=close);else if(typeof process<"u"){globalThis.self=a,a.on=process.on.bind(process);let e=process.send?.bind(process);e&&(a.send=r=>{e(JSON.stringify({type:"message",data:r}))}),a.on("disconnect",()=>{process.exit(1)}),process.on("uncaughtException",r=>{e&&e(JSON.stringify({type:"error",message:`Uncaught Error: ${r.message}`}))}),a.exit=process.exit.bind(process)}a.on("message",e=>{a.onmessage&&a.onmessage(e)});a.on("error",e=>{a.onerror&&a.onerror(e)});V=a});k();async function H(e,r){try{let t=await import(r);if(typeof t=="function")return await t();if(typeof t.default=="function")return await t.default()}catch(t){e.error("failed to load preload",t)}}async function q(e,r,t){e.debug(`loading publisher from ${r}`);let n=await import(r);return typeof n.default=="object"&&(n=n.default),await n.create(t,p(`gateway.metrics.publisher.${n.name??r}`))}async function Y(e){let r=e.findParameter("logLevel");if(r?.startsWith('"'))try{r=JSON.parse(r)}catch{r=null}await e.streamLogs(r??"info")}async function Q(e,r){let{publishFn:t,cfg:n}=r;if(n?.preload){let g=await H(e,n.preload);if(typeof g=="object")for(let l in g)n[l]=g[l]}let s;typeof t=="function"?s=t:s=await q(e,t,n);let o=await s("start");return[s,o]}async function X(e,r,t){let n=await r("stop");typeof n=="function"&&await n(t)}function Z(e){let r,t;e.onmessage=async n=>{let s=p("gateway.metrics.worker"),{id:o,cmd:g,arg:l}=n.data;try{switch(g){case"start":{[r,t]=await Q(s,l);break}case"stop":{await X(s,r,t);break}case"update":{await r(l);break}}e.send({id:o,result:"ok"})}catch(m){let{serializeError:i}=await Promise.resolve().then(()=>(j(),W)),c=i(m);e.send({id:o,error:c})}}}Promise.resolve().then(()=>(M(),N)).then(e=>e.default).then(async e=>{await Y(e),Z(e),e.send({ready:!0})}).catch(e=>console.error(e));
//# sourceMappingURL=worker.js.map