@interopio/gateway
Version:
[](https://www.npmjs.com/package/@interopio/gateway)
3 lines (2 loc) • 9.23 kB
JavaScript
var B=Object.defineProperty;var m=(e,r)=>()=>(e&&(r=e(e=0)),r);var C=(e,r)=>{for(var t in r)B(e,t,{get:r[t],enumerable:!0})};function E(e,r,t){Object.defineProperty(e,r,{value:t,writable:!1,enumerable:!1,configurable:!1})}function H(e){if(e===void 0)return"undefined";if(e===null)return"null";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return String(e);if(typeof e=="bigint")return`${e}n`;if(typeof e=="symbol")return e.toString();if(typeof e=="function")return`[Function${e.name?` ${e.name}`:" (anonymous)"}]`;if(e instanceof Error)try{return String(e)}catch{return"<Unserializable error>"}try{return JSON.stringify(e)}catch{try{return String(e)}catch{return"<Unserializable value>"}}}var O,u,T=m(()=>{O=Symbol("isNonError");u=class e extends Error{constructor(r,{superclass:t=Error}={}){if(e.isNonError(r))return r;if(r instanceof Error)throw new TypeError("Do not pass Error instances to NonError. Throw the error directly instead.");super(`Non-error value: ${H(r)}`),t!==Error&&Object.setPrototypeOf(this,t.prototype),E(this,"name","NonError"),E(this,O,!0),E(this,"isNonError",!0),E(this,"value",r)}static isNonError(r){return r?.[O]===!0}static#e(r,t){try{let o=r(...t);return o&&typeof o.then=="function"?(async()=>{try{return await o}catch(n){throw n instanceof Error?n:new e(n)}})():o}catch(o){throw o instanceof Error?o:new e(o)}}static try(r){return e.#e(r,[])}static wrap(r){return(...t)=>e.#e(r,t)}static[Symbol.hasInstance](r){return e.isNonError(r)}}});function j(e,r){let t,o;if(r){if(typeof r!="function")throw new TypeError("Factory must be a function");try{t=r()}catch(n){throw new Error("Factory is not compatible",{cause:n})}if(!(t instanceof e))throw new TypeError("Factory must return an instance of the constructor");o=t.name}else{try{t=new e}catch(n){throw new Error(`Constructor "${e.name}" is not compatible`,{cause:n})}o=t.name}if(!o||typeof o!="string")throw new TypeError('Error instances must have a non-empty string "name" property');if(b.has(o))throw new Error(`Error constructor "${o}" is already known`);b.set(o,e),r&&P.set(o,r)}var q,b,P,x=m(()=>{q=[Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError,AggregateError,globalThis.DOMException,globalThis.AssertionError,globalThis.SystemError].filter(Boolean).map(e=>[e.name,e]),b=new Map(q),P=new Map});var I={};C(I,{NonError:()=>u,addKnownErrorConstructor:()=>j,deserializeError:()=>Q,isErrorLike:()=>A,serializeError:()=>_});function _(e,r={}){let{maxDepth:t=Number.POSITIVE_INFINITY,useToJSON:o=!0}=r;return typeof e=="object"&&e!==null?k({from:e,seen:new Set,forceEnumerable:!0,maxDepth:t,depth:0,useToJSON:o,serialize:!0}):(typeof e=="function"&&(e="<Function>"),k({from:new u(e),seen:new Set,forceEnumerable:!0,maxDepth:t,depth:0,useToJSON:o,serialize:!0}))}function Q(e,r={}){let{maxDepth:t=Number.POSITIVE_INFINITY}=r;return e instanceof Error?e:Z(e)?k({from:e,seen:new Set,to:W(e.name),maxDepth:t,depth:0,serialize:!1}):new u(e)}function A(e){return!!e&&typeof e=="object"&&typeof e.name=="string"&&typeof e.message=="string"&&typeof e.stack=="string"}function Z(e){return!!e&&typeof e=="object"&&typeof e.message=="string"&&!Array.isArray(e)}var Y,N,X,W,k,$=m(()=>{T();x();x();T();Y=[{property:"name",enumerable:!1},{property:"message",enumerable:!1},{property:"stack",enumerable:!1},{property:"code",enumerable:!0},{property:"cause",enumerable:!1},{property:"errors",enumerable:!1}],N=new WeakSet,X=e=>{N.add(e);let r=e.toJSON();return N.delete(e),r},W=e=>{if(e==="NonError")return new u;let r=P.get(e);if(r)return r();let t=b.get(e)??Error;return t===AggregateError?new t([]):new t},k=({from:e,seen:r,to:t,forceEnumerable:o,maxDepth:n,depth:a,useToJSON:g,serialize:i})=>{if(t||(Array.isArray(e)?t=[]:!i&&A(e)?t=W(e.name):t={}),r.add(e),a>=n)return r.delete(e),t;if(g&&typeof e.toJSON=="function"&&!N.has(e))return r.delete(e),X(e);let h=c=>k({from:c,seen:r,forceEnumerable:o,maxDepth:n,depth:a+1,useToJSON:g,serialize:i});for(let c of Object.keys(e)){let s=e[c];if(s&&s instanceof Uint8Array&&s.constructor.name==="Buffer"){t[c]=i?"[object Buffer]":s;continue}if(s!==null&&typeof s=="object"&&typeof s.pipe=="function"){t[c]=i?"[object Stream]":s;continue}if(typeof s=="function"){i||(t[c]=s);continue}if(i&&typeof s=="bigint"){t[c]=`${s}n`;continue}if(!s||typeof s!="object"){try{t[c]=s}catch{}continue}if(!r.has(s)){t[c]=h(s);continue}t[c]="[Circular]"}if(i||t instanceof Error)for(let{property:c,enumerable:s}of Y){let p=e[c];if(p==null||Object.getOwnPropertyDescriptor(t,c)?.configurable===!1)continue;let S=p;typeof p=="object"&&(S=r.has(p)?"[Circular]":h(p)),Object.defineProperty(t,c,{value:S,enumerable:o||s,configurable:!0,writable:!0})}return r.delete(e),t}});var G={};C(G,{ROOT_LOGGER_NAME:()=>y,configure:()=>te,getLogger:()=>U,logEvent:()=>ee});function ee(e){let{name:r,level:t}=e;R(r),F(t,r)&&L(e)}function M(e,r,t,...o){this.enabledFor(r)&&L({time:new Date,level:r,name:e,message:t,data:o})}function F(e,r){let t=d[e];return l[r]>=t}function re(e){R(e);let r=function(o,n,...a){M.call(r,e,o,n,...a)};function t(o){return isNaN(Number(o))}for(let o of Object.keys(d).filter(t))r[o]=function(n,...a){M.call(r,e,o,n,...a)};return r.enabledFor=function(o){return F(o,e)},r.child=function(o){return U(`${e}.${o}`)},r}function R(e){if(!e.startsWith(y))throw new Error(`Logger name must start with ${y}`);if(l[e]===void 0){let r=Object.entries(l).sort(([o],[n])=>n.localeCompare(o)),[,t]=r.find(([o])=>e.startsWith(o));l[e]=t}}function U(e){let r=v[e];return r===void 0&&(r=re(e),v[e]=r),r}function te(e){function r(o,n){for(let a of Object.keys(l).filter(g=>g.startsWith(o)))l[a]=d[n];l[o]===void 0&&(l[o]=d[n])}let t=e.level;if(typeof t=="string")l[y]=d[t],r(y,t);else if(typeof t=="object"){let o=Object.entries(t).sort(([n],[a])=>n.localeCompare(a));for(let[n,a]of o)r(n,a)}L=e.appender??L}var d,v,l,y,L,J=m(()=>{"use strict";d=(i=>(i[i.off=0]="off",i[i.error=200]="error",i[i.warn=300]="warn",i[i.info=400]="info",i[i.debug=500]="debug",i[i.trace=600]="trace",i[i.all=Number.MAX_SAFE_INTEGER]="all",i))(d||{}),v=Object.create(null),l={},y="gateway";l[y]=600;L=e=>{console[e.level](`${e.time.toISOString()} ${e.level.toUpperCase()} [${e.name}] - ${e.message}`,...e.data)}});var K={};C(K,{default:()=>oe});var f,oe,D=m(()=>{"use strict";f={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(o=>o.startsWith("--")).map(o=>{let n=o.slice(2).split("=");return[n[0],n[1]]}).find(([o,n])=>o===e);t&&([,r]=t)}return r},async streamLogs(e="trace"){let r=o=>{f.send({log:{time:o.time.getTime(),level:o.level,name:o.name,message:o.message,data:o.data}})},t=await Promise.resolve().then(()=>(J(),G));return t.configure({level:e,appender:r}),t},async importModule(e){return await import(e)}};if(typeof self<"u"&&typeof postMessage=="function"&&typeof addEventListener=="function")f.on=(e,r)=>{addEventListener(e,t=>{r(t)})},f.send=e=>{postMessage(e)},typeof close=="function"&&(f.exit=close);else if(typeof process<"u"){globalThis.self=f,f.on=process.on.bind(process);let e=process.send?.bind(process);e&&(f.send=r=>{e(JSON.stringify({type:"message",data:r}))}),f.on("disconnect",()=>{process.exit(1)}),process.on("unhandledRejection",r=>{e&&e(JSON.stringify({type:"error",message:`Unhandled Rejection: ${r instanceof Error?r.message:String(r)}`}))}),process.on("uncaughtException",r=>{e&&e(JSON.stringify({type:"error",message:`Uncaught Error: ${r.message}`}))}),f.exit=process.exit.bind(process)}f.on("message",e=>{f.onmessage&&f.onmessage(e)});f.on("error",e=>{f.onerror&&f.onerror(e)});oe=f});async function V(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 z(e,r,t){e.debug(`loading publisher from ${r}`);let o=await import(r);return typeof o.default=="object"&&(o=o.default),await o.create(t,e.child(`publisher.${o.name??r}`))}var w=class{#e;#r;#t;constructor(r){this.#e=r}async#o(r){let{publishFn:t,cfg:o}=r;if(o?.preload){let g=await V(this.#e,o.preload);if(typeof g=="object")for(let i in g)o[i]=g[i]}let n;typeof t=="function"?n=t:n=await z(this.#e,t,o);let a=await n("start");this.#r=n,this.#t=a}async#n(){let r=await this.#r?.("stop");typeof r=="function"&&await r(this.#t)}async#i(r){if(!this.#r)throw new Error("worker not started");await this.#r(r)}async execute(r,t){switch(r){case"start":{await this.#o(t);break}case"stop":{await this.#n();break}case"update":{await this.#i(t);break}}}};async function ne(e){let r=e.findParameter("logLevel");if(r?.startsWith('"'))try{r=JSON.parse(r)}catch{r=null}return await e.streamLogs(r??"info")}function ie(e,r){let t=new w(r);e.onmessage=async o=>{let{id:n,cmd:a,arg:g}=o.data;try{await t.execute(a,g),e.send({id:n,result:"ok"})}catch(i){let{serializeError:h}=await Promise.resolve().then(()=>($(),I)),c=h(i);e.send({id:n,error:c})}}}Promise.resolve().then(()=>(D(),K)).then(e=>e.default).then(async e=>{let t=(await ne(e)).getLogger("gateway.metrics.worker");ie(e,t),e.send({ready:!0})}).catch(e=>console.error(e));
//# sourceMappingURL=worker.js.map