var g=Object.defineProperty;var b=(n,e)=>{for(var r in e)g(n,r,{get:e[r],enumerable:!0})};var y=class{serviceId;constructor(e){this.serviceId=e}createPrototype(e){return new a(this.serviceId,e.id,e.message)}},a=class{serviceId;errorId;errorMessage;constructor(e,r,s){this.serviceId=e,this.errorId=r,this.errorMessage=s}create(e){return new c(this,e)}formatMessage(e){let r=this.errorMessage;if(!r)return JSON.stringify(e);for(let s of Object.keys(e))r=r.replace(`{${s}}`,e[s].toString());return r}equals(e){return e instanceof a?e.serviceId===this.serviceId&&e.errorId===this.errorId:!1}},c=class extends Error{prototype;payload;constructor(e,r){super(e.formatMessage(r)),this.prototype=e,this.payload=r}is(e){if(e instanceof a)return e.equals(this.prototype);if(e instanceof c){if(!e.prototype.equals(this.prototype)||Object.keys(e.payload).length!==Object.keys(this.payload).length)return!1;for(let r of Object.keys(this.payload))if(e.payload[r]!==this.payload[r])return!1;return!0}return!1}};var m={};b(m,{LocalServiceEventEmitter:()=>p,LocalServiceEventSubscriber:()=>u});var p=class{_subscriber;static create(){return new this}subscriber(){return this._subscriber}constructor(){this._subscriber=new u}async emit(e,r){this._subscriber.dispatch(e,r)}},u=class{listeners={};dispatch(e,r){this.listeners[e]?.forEach(s=>{s(r).catch(()=>{this.dispatch(e,r)})})}on(e,r){this.listeners[e]??=[],this.listeners[e].push(r)}};var h={};b(h,{PubSubPushMessageListener:()=>d,PubSubServiceEventEmitter:()=>v});var v=class{topic;static create(e){return new this(e)}constructor(e){this.topic=e.topic}async emit(e,r){return new Promise((s,o)=>{this.topic.publishMessage({json:{name:e,data:r}},i=>{i?o(i):s()})})}};import f,{Router as S}from"express";var d=class{subscribers;constructor(){this.subscribers={}}async createSubscriber(e){if(this.subscribers[e])throw`subscriber for topic ${e} already created`;let r=new l;return this.subscribers[e]=r,r}listen(e,r){let s=f();s.use(f.json()),s.post(r??"/",this.handlePushMessage.bind(this)),s.listen(e)}router(e){let r=S();return r.post(e,this.handlePushMessage),r}async handlePushMessage(e,r){if(!e.body){let t="no Pub/Sub message received";return console.error(`error: ${t}`),r.status(400).send(`Bad Request: ${t}`)}if(!e.body.message){let t="invalid Pub/Sub message format";return console.error(`error: ${t}`),r.status(400).send(`Bad Request: ${t}`)}let s=e.body.message;if(!s.data){let t="invalid service message format";return console.error(`error: ${t}`),r.status(400).send(`Bad Request: ${t}`)}let o=s.attributes.topic;if(!o){let t="invalid service message format (no topic in attributes)";return console.error(`error: ${t}`),r.status(400).send(`Bad Request: ${t}`)}let i=this.subscribers[o];if(!i){let t="no topic subscriber";return console.warn(`warning: ${t}`),r.status(200).send(`Skip: ${t}`)}try{await i.handleMessage(s),r.status(200)}catch{r.status(400)}return r.send()}},l=class{listeners;anyListener;constructor(){this.listeners={}}async handleMessage(e){let r=JSON.parse(Buffer.from(e,"base64").toString().trim()),{name:s,data:o}=r,i=this.listeners[s]??this.anyListener;!i||await i(o)}on(e,r){if(e==="*"){this.anyListener=r;return}this.listeners[e]=r}};export{m as Local,h as PubSub,y as ServerErrorFactory,c as ServiceError,a as ServiceErrorPrototype};
//# sourceMappingURL=index.esm.js.map
