"use strict";var E=Object.create;var p=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var T=Object.getPrototypeOf,w=Object.prototype.hasOwnProperty;var d=(t,e)=>{for(var r in e)p(t,r,{get:e[r],enumerable:!0})},S=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of x(e))!w.call(t,n)&&n!==r&&p(t,n,{get:()=>e[n],enumerable:!(s=P(e,n))||s.enumerable});return t};var A=(t,e,r)=>(r=t!=null?E(T(t)):{},S(e||!t||!t.__esModule?p(r,"default",{value:t,enumerable:!0}):r,t)),k=t=>S(p({},"__esModule",{value:!0}),t);var $={};d($,{Local:()=>y,PubSub:()=>g,ServerErrorFactory:()=>l,ServiceError:()=>c,ServiceErrorPrototype:()=>a});module.exports=k($);var l=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 y={};d(y,{LocalServiceEventEmitter:()=>b,LocalServiceEventSubscriber:()=>v});var b=class{_subscriber;static create(){return new this}subscriber(){return this._subscriber}constructor(){this._subscriber=new v}async emit(e,r){this._subscriber.dispatch(e,r)}},v=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 g={};d(g,{PubSubPushMessageListener:()=>f,PubSubServiceEventEmitter:()=>m});var m=class{topic;static create(e){return new this(e)}constructor(e){this.topic=e.topic}async emit(e,r){return new Promise((s,n)=>{this.topic.publishMessage({json:{name:e,data:r}},o=>{o?n(o):s()})})}};var u=A(require("express")),f=class{subscribers;constructor(){this.subscribers={}}async createSubscriber(e){if(this.subscribers[e])throw`subscriber for topic ${e} already created`;let r=new h;return this.subscribers[e]=r,r}listen(e,r){let s=(0,u.default)();s.use(u.default.json()),s.post(r??"/",this.handlePushMessage.bind(this)),s.listen(e)}router(e){let r=(0,u.Router)();return r.post(e,this.handlePushMessage),r}async handlePushMessage(e,r){if(!e.body){let i="no Pub/Sub message received";return console.error(`error: ${i}`),r.status(400).send(`Bad Request: ${i}`)}if(!e.body.message){let i="invalid Pub/Sub message format";return console.error(`error: ${i}`),r.status(400).send(`Bad Request: ${i}`)}let s=e.body.message;if(!s.data){let i="invalid service message format";return console.error(`error: ${i}`),r.status(400).send(`Bad Request: ${i}`)}let n=s.attributes.topic;if(!n){let i="invalid service message format (no topic in attributes)";return console.error(`error: ${i}`),r.status(400).send(`Bad Request: ${i}`)}let o=this.subscribers[n];if(!o){let i="no topic subscriber";return console.warn(`warning: ${i}`),r.status(200).send(`Skip: ${i}`)}try{await o.handleMessage(s),r.status(200)}catch{r.status(400)}return r.send()}},h=class{listeners;anyListener;constructor(){this.listeners={}}async handleMessage(e){let r=JSON.parse(Buffer.from(e,"base64").toString().trim()),{name:s,data:n}=r,o=this.listeners[s]??this.anyListener;!o||await o(n)}on(e,r){if(e==="*"){this.anyListener=r;return}this.listeners[e]=r}};
//# sourceMappingURL=index.js.map
