New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

message-bridge-js

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

message-bridge-js - npm Package Compare versions

Comparing version 0.9.0 to 0.9.1

2

dist/index.js

@@ -1,2 +0,2 @@

var e,s,t=require("@microsoft/signalr"),r=require("uuid");function o(e,s=exports.MessageDirection.ToClient){return e.isError=e.type===exports.MessageType.Error,e.direction??=s,e}function n(e){return{name:e.name,payload:e.payload,type:e.type,direction:e.direction??exports.MessageDirection.ToClient,trackId:e.trackId??r.v4(),module:e.module,schema:e.schema,created:(new Date).toISOString(),isError:e.type===exports.MessageType.Error}}function i(e){return n({...e,type:exports.MessageType.Command})}function a(e){return n({...e,type:exports.MessageType.Query})}function c(e){return n({...e,type:exports.MessageType.Event,payload:void 0})}exports.MessageType=void 0,(e=exports.MessageType||(exports.MessageType={})).Command="Command",e.CommandResponse="CommandResponse",e.Query="Query",e.QueryResponse="QueryResponse",e.Event="Event",e.Error="Error",exports.MessageDirection=void 0,(s=exports.MessageDirection||(exports.MessageDirection={})).ToClient="ToClient",s.ToServer="ToServer";class d{constructor(e){this.wsUri=void 0,this.connected=!1,this.subscribedTrackIdMap={},this.subscribedEventListMap={},this.history=[],this.bridgeErrors=[],this.options={timeout:void 0,keepHistoryForReceivedMessages:!1,keepHistoryForSendingMessages:!1,logger:()=>console?.log??(()=>{}),logParseIncomingMessageError:!0,timeoutFromBridgeOptionsMessage:e=>`Timeout after ${e}ms (BridgeOptions.timeout)`,timeoutFromRequestOptionsMessage:e=>`Timeout after ${e}ms (RequestOptions.timeout)`,logParseIncomingMessageErrorFormat:e=>["Bridge-Error (parse messageReceived):",e],logMessageReceived:!1,logMessageReceivedFormat:e=>["Bridge (messageReceived):",e],logSendingMessage:!1,logSendingMessageFormat:e=>["Bridge (sendingMessage):",e]},this.wsUri=e}setOptions(e){this.options={...this.options,...e}}getTrackedRequestMessage(e){return this.subscribedTrackIdMap[e]?.requestMessage}onConnect(){this.connected=!0,this.options.onConnect?.()}onError(e,s){void 0!==e&&this.bridgeErrors.push(e),this.options.onError?.(e,s)}onClose(e,s){void 0!==e&&this.bridgeErrors.push(e),this.connected=!1,this.options.onClose?.(e,s)}setOptionalRequestTimeout({requestMessage:e,timeout:s,onTimeout:t}){let r,o;if(void 0!==this.options.timeout&&(r=this.options.timeoutFromBridgeOptionsMessage?.(this.options.timeout)??`timeout after ${this.options.timeout}`,o=this.options.timeout),void 0!==s&&(r=this.options.timeoutFromRequestOptionsMessage?.(s)??`timeout after ${s}`,o=s),void 0!==o)return setTimeout(()=>{t({reason:r,responseMessage:void 0,request:e.payload,requestMessage:e})},o)}sendMessageTracked(e){return new Promise((s,t)=>{this.sendMessagePromiseHandler({...e,handleResponseReject:(e,r)=>{e&&this.options.throwOnTrackedError?t(r):s(r)}})}).finally(()=>{delete this.subscribedTrackIdMap[e.requestMessage.trackId]})}sendMessage(e){return new Promise((s,t)=>{this.sendMessagePromiseHandler({...e,handleResponseReject:(e,r,o)=>{e?this.options.avoidThrowOnNonTrackedError?s(o?.payload):t(r):s(r.response)}})})}sendMessagePromiseHandler({handleResponseReject:e,requestMessage:s,onSuccess:t,onError:r,timeout:o}){s.direction=exports.MessageDirection.ToServer,this.options.interceptSendMessage&&(s=this.options.interceptSendMessage(s));const n=t=>{i&&clearTimeout(i);const o={response:void 0,responseMessage:void 0,request:s.payload,requestMessage:s,isError:!0,error:t.reason,errorMessage:t.responseMessage};this.onError(o),r?.(o),e(!0,o,t)},i=this.setOptionalRequestTimeout({requestMessage:s,timeout:o,onTimeout:e=>{n(e)}});this.subscribedTrackIdMap[s.trackId]={successTrack:r=>{const o={response:r.payload,responseMessage:r,request:s.payload,requestMessage:s,isError:!1};i&&clearTimeout(i),t?.(o),e(!1,o,void 0)},errorTrack:e=>{n({reason:e?.payload,responseMessage:e,request:s.payload,requestMessage:s})},requestMessage:s},this.internalSendMessage(s)}subscribeEvent({name:e,onEvent:s}){if(Array.isArray(e)){const t=e.map(e=>this.subscribeEvent({name:e,onEvent:s}));return()=>t.forEach(e=>e())}return this.subscribedEventListMap[e]||(this.subscribedEventListMap[e]=[]),this.subscribedEventListMap[e].push(s),()=>{const t=this.subscribedEventListMap[e].findIndex(e=>e===s);this.subscribedEventListMap[e].splice(t,1)}}sendCommand(e){const s=i(e);return this.sendMessage({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendCommandTracked(e){const s=i(e);return this.sendMessageTracked({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendQuery(e){const s=a(e);return this.sendMessage({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendQueryTracked(e){const s=a(e);return this.sendMessageTracked({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendEvent(e){let s=c(e);return s.direction=exports.MessageDirection.ToServer,this.options.interceptSendMessage&&(s=this.options.interceptSendMessage(s)),this.internalSendMessage(s),s}onMessage(e){let s;try{s="string"==typeof e?JSON.parse(e):e}catch(e){return void this.onError(e)}try{let e=o(s);this.options.interceptReceivedMessage&&(e=this.options.interceptReceivedMessage(e)),this.handleIncomingMessage(e)}catch(e){if(this.onError(e),this.options?.logger&&this.options?.logParseIncomingMessageError){const t=this.options?.logParseIncomingMessageErrorFormat?.(s)??[e];this.options.logger(t)}}}internalSendMessage(e){if(this.options.keepHistoryForSendingMessages&&this.history.push(e),this.options?.logger&&this.options?.logSendingMessage){let s=!0;if(this.options?.logSendingMessageFilter&&(s=!!e.name.match(this.options?.logSendingMessageFilter)),s){const s=this.options?.logSendingMessageFormat?.(e)??[e];this.options.logger(...s)}}this.options.onSend?.(e),this.sendNetworkMessage(e)}handleIncomingMessage(e){if(this.options.keepHistoryForReceivedMessages&&this.history.push(e),this.options?.logger&&this.options?.logMessageReceived){let s=!0;if(this.options?.logMessageReceivedFilter&&(s=!!e.name.match(this.options?.logMessageReceivedFilter)),s){const s=this.options?.logMessageReceivedFormat?.(e)??[e];this.options.logger(...s)}}this.options.onMessage?.(e);let s=e.type!==exports.MessageType.Error;if(e.type===exports.MessageType.Event)return void this.receiveEventMessage(e);const t=this.subscribedTrackIdMap[e.trackId];t&&(e.type===exports.MessageType.Error?(t.errorTrack?.(e),s=!0):t.successTrack?.(e),delete this.subscribedTrackIdMap[e.trackId]),s||this.onError?.(e)}receiveEventMessage(e){this.subscribedEventListMap[e.name]&&this.subscribedEventListMap[e.name].forEach(s=>s(e.payload,e))}}exports.ClientSideMessageBridgeService=class extends d{constructor(...e){super(...e),this.server=void 0}setServer(e){this.server=e}connect(){if(!this.server)throw new Error("No server set");return this.server?.connect(e=>{this.onMessage(e),this.onConnect()}),Promise.resolve()}close(){this.onClose()}sendNetworkMessage(e){setTimeout(()=>{this.server?.onMessage(e)},10)}},exports.InMemoryClientSideServer=class{constructor(){this.store={},this.commands={},this.queries={},this.eventListeners={},this.sendMessage=void 0}saveToLocalStorage(e){localStorage.setItem(e,JSON.stringify(this.store))}loadFromLocalStorage(e){const s=localStorage.getItem(e);s&&(this.store=JSON.parse(s))}connect(e){this.sendMessage=e}sendError(e,s){const t=n({trackId:s,type:exports.MessageType.Error,name:"Error",payload:e,direction:exports.MessageDirection.ToClient});this.sendMessage?.(t)}sendResponse(e,s,t,r){const o=n({trackId:r,type:e,name:s,payload:t,direction:exports.MessageDirection.ToClient});this.sendMessage?.(o)}sendEvent(e,s){const t=n({type:exports.MessageType.Event,name:e,payload:s,direction:exports.MessageDirection.ToClient});this.sendMessage?.(t)}onMessage(e){if("string"==typeof e)try{e=JSON.parse(e)}catch(s){return this.sendError({message:`Error parsing message: ${s}`,request:e,error:s,stack:s?.stack}),void console.error("Error parsing message",s)}if("Command"===e.type){if(!this.commands[e.name])return void this.sendError({message:`Command ${e.name} not found (Register it with addCommand)`,request:e},e.trackId);this.serverHandleCommand(e)}if("Query"===e.type){if(!this.queries[e.name])return void this.sendError({message:`Query ${e.name} not found (Register it with addQuery)`,request:e},e.trackId);this.serverHandleQuery(e)}if("Event"===e.type){if(!this.eventListeners[e.name])return void this.sendError({message:`Event ${e.name} not found (Register it with addEvent)`,request:e},e.trackId);this.serverHandleEvent(e)}}serverHandleCommand(e){const s=this.commands[e.name],t=s=>{this.sendResponse(exports.MessageType.CommandResponse,e.name,s,e.trackId)},r=s=>{this.sendError({message:s,request:e},e.trackId)},o=(e,s)=>{this.sendEvent(e,s)};try{s({requestMessage:e,request:e.payload,store:this.store,error:r,event:o,response:t})}catch(s){r({message:`Error in command handler for '${e.name}'`,requestMessage:e,error:s,stack:s?.stack})}}serverHandleQuery(e){(0,this.queries[e.name])({requestMessage:e,request:e.payload,store:this.store,error:s=>{this.sendError({message:s,request:e},e.trackId)},event:(e,s)=>{this.sendEvent(e,s)},response:s=>{this.sendResponse(exports.MessageType.QueryResponse,e.name,s,e.trackId)}})}serverHandleEvent(e){(0,this.eventListeners[e.name])({requestMessage:e,request:e.payload,store:this.store,error:s=>{this.sendError({message:s,request:e},e.trackId)},event:(e,s)=>{this.sendEvent(e,s)}})}addCommand(e,s){this.commands[e]=s}addQuery(e,s){this.queries[e]=s}addEventListener(e,s){this.eventListeners[e]=s}},exports.MessageBridgeServiceBase=d,exports.SignalRMessageBridgeService=class extends d{constructor(...e){super(...e),this.connection=void 0}connect(e={}){if(this.connection=(new t.HubConnectionBuilder).withUrl(this.wsUri,e).withAutomaticReconnect().build(),!this.connection)throw new Error("Failed to create SignalR connection");return this.connection.on("ReceiveMessage",e=>{this.onMessage(e)}),this.connection.onclose(e=>{this.onClose(e)}),this.connection.start().then(()=>{this.onConnect()}).catch(e=>{this.onError(e)})}close(){this.connection?.stop(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.connection?.invoke("SendMessage",s).catch(e=>(this.onError(e),console.error(e.toString())))}},exports.WebsocketMessageBridgeService=class extends d{constructor(...e){super(...e),this.socket=void 0,this.connectedCallback=void 0}connect(){return this.socket=new WebSocket(this.wsUri),this.socket.addEventListener("message",e=>{this.onMessage(e.data)}),this.socket.addEventListener("close",e=>{this.onClose(e.reason,e)}),this.socket.addEventListener("error",e=>{this.onError(e,e)}),new Promise((e,s)=>{this.socket?.addEventListener("open",s=>{this.onConnect(),e()})})}close(){this.socket?.close(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.socket?.send(s)}},exports.createCommandMessage=i,exports.createEventMessage=c,exports.createMessage=n,exports.createMessageFromDto=o,exports.createQueryMessage=a;
var e,s,t=require("@microsoft/signalr"),r=require("uuid");function n(e,s=exports.MessageDirection.ToClient){return e.isError=e.type===exports.MessageType.Error,e.direction??=s,e}function o(e){return{name:e.name,payload:e.payload,type:e.type,direction:e.direction??exports.MessageDirection.ToClient,trackId:e.trackId??r.v4(),module:e.module,schema:e.schema,created:(new Date).toISOString(),isError:e.type===exports.MessageType.Error}}function i(e){return o({...e,type:exports.MessageType.Command})}function a(e){return o({...e,type:exports.MessageType.Query})}function c(e){return o({...e,type:exports.MessageType.Event})}exports.MessageType=void 0,(e=exports.MessageType||(exports.MessageType={})).Command="Command",e.CommandResponse="CommandResponse",e.Query="Query",e.QueryResponse="QueryResponse",e.Event="Event",e.Error="Error",exports.MessageDirection=void 0,(s=exports.MessageDirection||(exports.MessageDirection={})).ToClient="ToClient",s.ToServer="ToServer";class d{constructor(e){this.wsUri=void 0,this.connected=!1,this.trackedRequestMap={},this.subscribedEventListMap={},this.history=[],this.bridgeErrors=[],this.options={timeout:void 0,allowResponseValueWhenCancelled:!1,keepHistoryForReceivedMessages:!1,keepHistoryForSendingMessages:!1,logger:()=>console?.log??(()=>{}),logParseIncomingMessageError:!0,timeoutFromBridgeOptionsMessage:e=>`Timeout after ${e}ms (BridgeOptions.timeout)`,timeoutFromRequestOptionsMessage:e=>`Timeout after ${e}ms (RequestOptions.timeout)`,logParseIncomingMessageErrorFormat:e=>["Bridge-Error (parse messageReceived):",e],logMessageReceived:!1,logMessageReceivedFormat:e=>["Bridge (messageReceived):",e],logSendingMessage:!1,logSendingMessageFormat:e=>["Bridge (sendingMessage):",e]},this.wsUri=e}setOptions(e){this.options={...this.options,...e}}getTrackedRequestMessage(e){return this.trackedRequestMap[e]?.requestMessage}cancelRequest(e){this.trackedRequestMap[e]&&(this.trackedRequestMap[e].requestMessage.cancelled=!0)}onConnect(){this.connected=!0,this.options.onConnect?.()}onError(e,s){void 0!==e&&this.bridgeErrors.push(e),this.options.onError?.(e,s)}onClose(e,s){void 0!==e&&this.bridgeErrors.push(e),this.connected=!1,this.options.onClose?.(e,s)}setOptionalRequestTimeout({requestMessage:e,timeout:s,onTimeout:t}){let r,n;if(void 0!==s?(r=this.options.timeoutFromRequestOptionsMessage?.(s)??`timeout after ${s}`,n=s):void 0!==this.options.timeout&&(r=this.options.timeoutFromBridgeOptionsMessage?.(this.options.timeout)??`timeout after ${this.options.timeout}`,n=this.options.timeout),void 0!==n)return setTimeout(()=>{e.timedOut=!0,t({reason:r,responseMessage:void 0,request:e.payload,requestMessage:e})},n)}sendMessageTracked(e){if(e.requestMessage.cancelled){let s=!0;if(void 0!==e.requestOptions.sendCancelled?s=!e.requestOptions.sendCancelled:this.options.sendCancelledRequest&&(s=!1),s)return Promise.resolve({request:e.requestMessage.payload,requestMessage:e.requestMessage,cancelled:!0})}return new Promise((s,t)=>{this.sendMessagePromiseHandler({...e,handleSuccess:(e,t)=>{s(t)},handleError:(e,r)=>{this.options.throwOnTrackedError?t(r):s(r)}})}).finally(()=>{delete this.trackedRequestMap[e.requestMessage.trackId]})}sendMessage(e){const s=void 0;if(e.requestMessage.cancelled){let t=!0;if(void 0!==e.requestOptions.sendCancelled?t=!e.requestOptions.sendCancelled:this.options.sendCancelledRequest&&(t=!1),t)return Promise.resolve(s)}return new Promise((t,r)=>{this.sendMessagePromiseHandler({...e,handleSuccess:(e,r)=>{t(e?s:r.response)},handleError:(e,n,o)=>{e&&t(s),this.options.avoidThrowOnNonTrackedError?t(o?.payload):r(n)}})})}sendMessagePromiseHandler({handleError:e,handleSuccess:s,requestMessage:t,requestOptions:r}){t.direction=exports.MessageDirection.ToServer,this.options.interceptSendMessage&&(t=this.options.interceptSendMessage(t));const n=(s,n)=>{o&&clearTimeout(o);const i={response:void 0,responseMessage:void 0,request:t.payload,requestMessage:t,isError:!0,error:n.reason,errorMessage:n.responseMessage,cancelled:t.cancelled||n.requestMessage?.cancelled||n.responseMessage?.cancelled,timedOut:t.timedOut||n.requestMessage?.timedOut||n.responseMessage?.timedOut};s&&this.options.callOnErrorWhenRequestIsCancelled&&r.callOnErrorWhenRequestIsCancelled||(this.onError(i),r.onError?.(i)),e(s,i,n)},o=this.setOptionalRequestTimeout({requestMessage:t,timeout:r.timeout,onTimeout:e=>{const s=this.handleCancelOptions(r,t);n(s,e)}});this.trackedRequestMap[t.trackId]={successTrack:e=>{const{response:n,cancelled:i}=this.handleCancelResponse(r,t,e),a={response:n,responseMessage:e,requestOptions:r,request:t.payload,requestMessage:t,isError:!1,cancelled:t.cancelled||e.cancelled,timedOut:t.timedOut||e.timedOut};o&&clearTimeout(o),(!i||this.options.callOnSuccessWhenRequestIsCancelled||r.callOnSuccessWhenRequestIsCancelled)&&(this.options.onSuccess?.(a),r.onSuccess?.(a)),s(i,a)},errorTrack:e=>{const{response:s,cancelled:o}=this.handleCancelResponse(r,t,e);n(o,{reason:s,responseMessage:e,request:t.payload,requestMessage:t})},requestMessage:t,requestOptions:r},this.internalSendMessage(t)}handleCancelOptions(e,s,t){let r=!1;void 0!==e.resolveCancelledForNonTracked?r=e.resolveCancelledForNonTracked:this.options.resolveCancelledNonTrackedRequest&&(r=!0);let n=!1;return(t?.cancelled||s.cancelled)&&(n=!0),r&&n&&(n=!1),n}handleCancelResponse(e,s,t){const r=this.handleCancelOptions(e,s,t);let n=t?.payload;return r&&(void 0!==e.allowResponseValueWhenCancelled?!0!==e.allowResponseValueWhenCancelled&&(n=void 0):!0!==this.options.allowResponseValueWhenCancelled&&(n=void 0)),{response:n,cancelled:r}}subscribeEvent({name:e,onEvent:s}){if(Array.isArray(e)){const t=e.map(e=>this.subscribeEvent({name:e,onEvent:s}));return()=>t.forEach(e=>e())}return this.subscribedEventListMap[e]||(this.subscribedEventListMap[e]=[]),this.subscribedEventListMap[e].push(s),()=>{const t=this.subscribedEventListMap[e].findIndex(e=>e===s);this.subscribedEventListMap[e].splice(t,1)}}createTrackedMessage(e){const s=e.requestMessage.trackId;let t={trackId:s,requestMessage:e.requestMessage,requestOptions:e.requestOptions,send:()=>this.sendMessage(e),sendTracked:()=>this.sendMessageTracked(e),cancel:()=>{e?.requestMessage&&(e.requestMessage.cancelled=!0),this.cancelRequest(s)}};return this.options.interceptCreatedMessageOptions&&(t=this.options.interceptCreatedMessageOptions(t)),t}createCommand(e){const s=i(e);return this.createTrackedMessage({requestMessage:s,requestOptions:e})}createQuery(e){const s=a(e);return this.createTrackedMessage({requestMessage:s,requestOptions:e})}sendCommand(e){return this.createCommand(e).send()}sendCommandTracked(e){return this.createCommand(e).sendTracked()}sendQuery(e){return this.createQuery(e).send()}sendQueryTracked(e){return this.createQuery(e).sendTracked()}createEvent(e){let s=c(e);s.direction=exports.MessageDirection.ToServer;let t={trackId:s.trackId,requestMessage:s,requestOptions:e,cancel:()=>{s&&(s.cancelled=!0)},send:()=>{if(s.cancelled)if(void 0!==e.sendCancelled){if(!e.sendCancelled)return}else if(!this.options.sendCancelledRequest)return;this.options.interceptSendMessage&&(s=this.options.interceptSendMessage(s)),this.internalSendMessage(s)}};return this.options.interceptCreatedEventMessageOptions&&(t=this.options.interceptCreatedEventMessageOptions(t)),t}sendEvent(e){return this.createEvent(e).send()}onMessage(e){let s;try{s="string"==typeof e?JSON.parse(e):e}catch(e){return void this.onError(e)}try{let e=n(s);this.options.interceptReceivedMessage&&(e=this.options.interceptReceivedMessage(e)),this.handleIncomingMessage(e)}catch(e){if(this.onError(e),this.options.logger&&this.options.logParseIncomingMessageError){const t=this.options.logParseIncomingMessageErrorFormat?.(s)??[e];this.options.logger(t)}}}internalSendMessage(e){if(this.options.keepHistoryForSendingMessages&&this.history.push(e),this.options.logger&&this.options.logSendingMessage){let s=!0;if(this.options.logSendingMessageFilter&&(s=!!e.name.match(this.options.logSendingMessageFilter)),s){const s=this.options.logSendingMessageFormat?.(e)??[e];this.options.logger(...s)}}this.options.onSend?.(e),this.sendNetworkMessage(e)}handleIncomingMessage(e){if(this.options.keepHistoryForReceivedMessages&&this.history.push(e),this.options.logger&&this.options.logMessageReceived){let s=!0;if(this.options.logMessageReceivedFilter&&(s=!!e.name.match(this.options.logMessageReceivedFilter)),s){const s=this.options.logMessageReceivedFormat?.(e)??[e];this.options.logger(...s)}}this.options.onMessage?.(e);let s=e.type!==exports.MessageType.Error;if(e.type===exports.MessageType.Event)return void this.receiveEventMessage(e);const t=this.trackedRequestMap[e.trackId];t&&(e.type===exports.MessageType.Error?(t.errorTrack(e),s=!0):t.successTrack(e),delete this.trackedRequestMap[e.trackId]),s||this.onError?.(e)}receiveEventMessage(e){this.subscribedEventListMap[e.name]&&this.subscribedEventListMap[e.name].forEach(s=>s(e.payload,e))}}exports.ClientSideMessageBridgeService=class extends d{constructor(...e){super(...e),this.server=void 0}setServer(e){this.server=e}connect(){if(!this.server)throw new Error("No server set");return this.server?.connect(e=>{const s=JSON.parse(JSON.stringify(e));this.onMessage(s),this.onConnect()}),Promise.resolve()}close(){this.onClose()}sendNetworkMessage(e){const s=JSON.parse(JSON.stringify(e));setTimeout(()=>{this.server?.onMessage(s)},10)}},exports.InMemoryClientSideServer=class{constructor(){this.store={},this.commands={},this.queries={},this.eventListeners={},this.sendMessage=void 0}saveToLocalStorage(e){localStorage.setItem(e,JSON.stringify(this.store))}loadFromLocalStorage(e){const s=localStorage.getItem(e);s&&(this.store=JSON.parse(s))}connect(e){this.sendMessage=e}sendError(e,s,t,r){const n=o({trackId:s,type:exports.MessageType.Error,name:"Error",payload:e,direction:exports.MessageDirection.ToClient,cancelled:t,timedOut:r});this.sendMessage?.(n)}createMessage(e){return o({...e,direction:exports.MessageDirection.ToClient})}sendResponse(e){const s=this.createMessage(e);this.sendMessage?.(s)}sendEvent(e,s){const t=o({type:exports.MessageType.Event,name:e,payload:s,direction:exports.MessageDirection.ToClient});this.sendMessage?.(t)}onMessage(e){if("string"==typeof e)try{e=JSON.parse(e)}catch(s){return this.sendError({message:`Error parsing message: ${s}`,request:e,error:s,stack:s?.stack}),void console.error("Error parsing message",s)}if("Command"===e.type){if(!this.commands[e.name])return void this.sendError({message:`Command ${e.name} not found (Register it with addCommand)`,request:e},e.trackId);this.serverHandleCommand(e)}if("Query"===e.type){if(!this.queries[e.name])return void this.sendError({message:`Query ${e.name} not found (Register it with addQuery)`,request:e},e.trackId);this.serverHandleQuery(e)}if("Event"===e.type){if(!this.eventListeners[e.name])return void this.sendError({message:`Event ${e.name} not found (Register it with addEvent)`,request:e},e.trackId);this.serverHandleEvent(e)}}serverHandleCommand(e){const s=this.commands[e.name],t=e=>{this.sendMessage?.(e)},r=s=>this.createMessage({name:e.name,type:exports.MessageType.CommandResponse,payload:s,trackId:e.trackId}),n=e=>{const s=r(e);t(s)},o=(s,t,r)=>{this.sendError({message:s,request:e},e.trackId,t,r)},i=(e,s)=>{this.sendEvent(e,s)};try{s({requestMessage:e,request:e.payload,store:this.store,error:o,event:i,response:n,createResponseMessage:r,sendResponseMessage:t})}catch(s){o({message:`Error in command handler for '${e.name}'`,requestMessage:e,error:s,stack:s?.stack})}}serverHandleQuery(e){const s=e=>{this.sendMessage?.(e)},t=s=>this.createMessage({name:e.name,type:exports.MessageType.QueryResponse,payload:s,trackId:e.trackId});(0,this.queries[e.name])({requestMessage:e,request:e.payload,store:this.store,error:(s,t,r)=>{this.sendError({message:s,request:e},e.trackId,t,r)},event:(e,s)=>{this.sendEvent(e,s)},response:e=>{const r=t(e);s(r)},createResponseMessage:t,sendResponseMessage:s})}serverHandleEvent(e){(0,this.eventListeners[e.name])({requestMessage:e,request:e.payload,store:this.store,error:s=>{this.sendError({message:s,request:e},e.trackId)},event:(e,s)=>{this.sendEvent(e,s)}})}addCommand(e,s){this.commands[e]=s}addQuery(e,s){this.queries[e]=s}addEventListener(e,s){this.eventListeners[e]=s}},exports.MessageBridgeServiceBase=d,exports.SignalRMessageBridgeService=class extends d{constructor(...e){super(...e),this.connection=void 0}connect(e={}){if(this.connection=(new t.HubConnectionBuilder).withUrl(this.wsUri,e).withAutomaticReconnect().build(),!this.connection)throw new Error("Failed to create SignalR connection");return this.connection.on("ReceiveMessage",e=>{this.onMessage(e)}),this.connection.onclose(e=>{this.onClose(e)}),this.connection.start().then(()=>{this.onConnect()}).catch(e=>{this.onError(e)})}close(){this.connection?.stop(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.connection?.invoke("SendMessage",s).catch(e=>(this.onError(e),console.error(e.toString())))}},exports.WebsocketMessageBridgeService=class extends d{constructor(...e){super(...e),this.socket=void 0,this.connectedCallback=void 0}connect(){return this.socket=new WebSocket(this.wsUri),this.socket.addEventListener("message",e=>{this.onMessage(e.data)}),this.socket.addEventListener("close",e=>{this.onClose(e.reason,e)}),this.socket.addEventListener("error",e=>{this.onError(e,e)}),new Promise((e,s)=>{this.socket?.addEventListener("open",s=>{this.onConnect(),e()})})}close(){this.socket?.close(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.socket?.send(s)}},exports.createCommandMessage=i,exports.createEventMessage=c,exports.createMessage=o,exports.createMessageFromDto=n,exports.createQueryMessage=a;
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

import{HubConnectionBuilder as e}from"@microsoft/signalr";import{v4 as s}from"uuid";var t,r;function o(e,s=r.ToClient){return e.isError=e.type===t.Error,e.direction??=s,e}function n(e){return{name:e.name,payload:e.payload,type:e.type,direction:e.direction??r.ToClient,trackId:e.trackId??s(),module:e.module,schema:e.schema,created:(new Date).toISOString(),isError:e.type===t.Error}}function i(e){return n({...e,type:t.Command})}function a(e){return n({...e,type:t.Query})}function c(e){return n({...e,type:t.Event,payload:void 0})}!function(e){e.Command="Command",e.CommandResponse="CommandResponse",e.Query="Query",e.QueryResponse="QueryResponse",e.Event="Event",e.Error="Error"}(t||(t={})),function(e){e.ToClient="ToClient",e.ToServer="ToServer"}(r||(r={}));class d{constructor(e){this.wsUri=void 0,this.connected=!1,this.subscribedTrackIdMap={},this.subscribedEventListMap={},this.history=[],this.bridgeErrors=[],this.options={timeout:void 0,keepHistoryForReceivedMessages:!1,keepHistoryForSendingMessages:!1,logger:()=>console?.log??(()=>{}),logParseIncomingMessageError:!0,timeoutFromBridgeOptionsMessage:e=>`Timeout after ${e}ms (BridgeOptions.timeout)`,timeoutFromRequestOptionsMessage:e=>`Timeout after ${e}ms (RequestOptions.timeout)`,logParseIncomingMessageErrorFormat:e=>["Bridge-Error (parse messageReceived):",e],logMessageReceived:!1,logMessageReceivedFormat:e=>["Bridge (messageReceived):",e],logSendingMessage:!1,logSendingMessageFormat:e=>["Bridge (sendingMessage):",e]},this.wsUri=e}setOptions(e){this.options={...this.options,...e}}getTrackedRequestMessage(e){return this.subscribedTrackIdMap[e]?.requestMessage}onConnect(){this.connected=!0,this.options.onConnect?.()}onError(e,s){void 0!==e&&this.bridgeErrors.push(e),this.options.onError?.(e,s)}onClose(e,s){void 0!==e&&this.bridgeErrors.push(e),this.connected=!1,this.options.onClose?.(e,s)}setOptionalRequestTimeout({requestMessage:e,timeout:s,onTimeout:t}){let r,o;if(void 0!==this.options.timeout&&(r=this.options.timeoutFromBridgeOptionsMessage?.(this.options.timeout)??`timeout after ${this.options.timeout}`,o=this.options.timeout),void 0!==s&&(r=this.options.timeoutFromRequestOptionsMessage?.(s)??`timeout after ${s}`,o=s),void 0!==o)return setTimeout(()=>{t({reason:r,responseMessage:void 0,request:e.payload,requestMessage:e})},o)}sendMessageTracked(e){return new Promise((s,t)=>{this.sendMessagePromiseHandler({...e,handleResponseReject:(e,r)=>{e&&this.options.throwOnTrackedError?t(r):s(r)}})}).finally(()=>{delete this.subscribedTrackIdMap[e.requestMessage.trackId]})}sendMessage(e){return new Promise((s,t)=>{this.sendMessagePromiseHandler({...e,handleResponseReject:(e,r,o)=>{e?this.options.avoidThrowOnNonTrackedError?s(o?.payload):t(r):s(r.response)}})})}sendMessagePromiseHandler({handleResponseReject:e,requestMessage:s,onSuccess:t,onError:o,timeout:n}){s.direction=r.ToServer,this.options.interceptSendMessage&&(s=this.options.interceptSendMessage(s));const i=t=>{a&&clearTimeout(a);const r={response:void 0,responseMessage:void 0,request:s.payload,requestMessage:s,isError:!0,error:t.reason,errorMessage:t.responseMessage};this.onError(r),o?.(r),e(!0,r,t)},a=this.setOptionalRequestTimeout({requestMessage:s,timeout:n,onTimeout:e=>{i(e)}});this.subscribedTrackIdMap[s.trackId]={successTrack:r=>{const o={response:r.payload,responseMessage:r,request:s.payload,requestMessage:s,isError:!1};a&&clearTimeout(a),t?.(o),e(!1,o,void 0)},errorTrack:e=>{i({reason:e?.payload,responseMessage:e,request:s.payload,requestMessage:s})},requestMessage:s},this.internalSendMessage(s)}subscribeEvent({name:e,onEvent:s}){if(Array.isArray(e)){const t=e.map(e=>this.subscribeEvent({name:e,onEvent:s}));return()=>t.forEach(e=>e())}return this.subscribedEventListMap[e]||(this.subscribedEventListMap[e]=[]),this.subscribedEventListMap[e].push(s),()=>{const t=this.subscribedEventListMap[e].findIndex(e=>e===s);this.subscribedEventListMap[e].splice(t,1)}}sendCommand(e){const s=i(e);return this.sendMessage({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendCommandTracked(e){const s=i(e);return this.sendMessageTracked({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendQuery(e){const s=a(e);return this.sendMessage({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendQueryTracked(e){const s=a(e);return this.sendMessageTracked({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendEvent(e){let s=c(e);return s.direction=r.ToServer,this.options.interceptSendMessage&&(s=this.options.interceptSendMessage(s)),this.internalSendMessage(s),s}onMessage(e){let s;try{s="string"==typeof e?JSON.parse(e):e}catch(e){return void this.onError(e)}try{let e=o(s);this.options.interceptReceivedMessage&&(e=this.options.interceptReceivedMessage(e)),this.handleIncomingMessage(e)}catch(e){if(this.onError(e),this.options?.logger&&this.options?.logParseIncomingMessageError){const t=this.options?.logParseIncomingMessageErrorFormat?.(s)??[e];this.options.logger(t)}}}internalSendMessage(e){if(this.options.keepHistoryForSendingMessages&&this.history.push(e),this.options?.logger&&this.options?.logSendingMessage){let s=!0;if(this.options?.logSendingMessageFilter&&(s=!!e.name.match(this.options?.logSendingMessageFilter)),s){const s=this.options?.logSendingMessageFormat?.(e)??[e];this.options.logger(...s)}}this.options.onSend?.(e),this.sendNetworkMessage(e)}handleIncomingMessage(e){if(this.options.keepHistoryForReceivedMessages&&this.history.push(e),this.options?.logger&&this.options?.logMessageReceived){let s=!0;if(this.options?.logMessageReceivedFilter&&(s=!!e.name.match(this.options?.logMessageReceivedFilter)),s){const s=this.options?.logMessageReceivedFormat?.(e)??[e];this.options.logger(...s)}}this.options.onMessage?.(e);let s=e.type!==t.Error;if(e.type===t.Event)return void this.receiveEventMessage(e);const r=this.subscribedTrackIdMap[e.trackId];r&&(e.type===t.Error?(r.errorTrack?.(e),s=!0):r.successTrack?.(e),delete this.subscribedTrackIdMap[e.trackId]),s||this.onError?.(e)}receiveEventMessage(e){this.subscribedEventListMap[e.name]&&this.subscribedEventListMap[e.name].forEach(s=>s(e.payload,e))}}class h extends d{constructor(...e){super(...e),this.connection=void 0}connect(s={}){if(this.connection=(new e).withUrl(this.wsUri,s).withAutomaticReconnect().build(),!this.connection)throw new Error("Failed to create SignalR connection");return this.connection.on("ReceiveMessage",e=>{this.onMessage(e)}),this.connection.onclose(e=>{this.onClose(e)}),this.connection.start().then(()=>{this.onConnect()}).catch(e=>{this.onError(e)})}close(){this.connection?.stop(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.connection?.invoke("SendMessage",s).catch(e=>(this.onError(e),console.error(e.toString())))}}class g extends d{constructor(...e){super(...e),this.socket=void 0,this.connectedCallback=void 0}connect(){return this.socket=new WebSocket(this.wsUri),this.socket.addEventListener("message",e=>{this.onMessage(e.data)}),this.socket.addEventListener("close",e=>{this.onClose(e.reason,e)}),this.socket.addEventListener("error",e=>{this.onError(e,e)}),new Promise((e,s)=>{this.socket?.addEventListener("open",s=>{this.onConnect(),e()})})}close(){this.socket?.close(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.socket?.send(s)}}class u extends d{constructor(...e){super(...e),this.server=void 0}setServer(e){this.server=e}connect(){if(!this.server)throw new Error("No server set");return this.server?.connect(e=>{this.onMessage(e),this.onConnect()}),Promise.resolve()}close(){this.onClose()}sendNetworkMessage(e){setTimeout(()=>{this.server?.onMessage(e)},10)}}class p{constructor(){this.store={},this.commands={},this.queries={},this.eventListeners={},this.sendMessage=void 0}saveToLocalStorage(e){localStorage.setItem(e,JSON.stringify(this.store))}loadFromLocalStorage(e){const s=localStorage.getItem(e);s&&(this.store=JSON.parse(s))}connect(e){this.sendMessage=e}sendError(e,s){const o=n({trackId:s,type:t.Error,name:"Error",payload:e,direction:r.ToClient});this.sendMessage?.(o)}sendResponse(e,s,t,o){const i=n({trackId:o,type:e,name:s,payload:t,direction:r.ToClient});this.sendMessage?.(i)}sendEvent(e,s){const o=n({type:t.Event,name:e,payload:s,direction:r.ToClient});this.sendMessage?.(o)}onMessage(e){if("string"==typeof e)try{e=JSON.parse(e)}catch(s){return this.sendError({message:`Error parsing message: ${s}`,request:e,error:s,stack:s?.stack}),void console.error("Error parsing message",s)}if("Command"===e.type){if(!this.commands[e.name])return void this.sendError({message:`Command ${e.name} not found (Register it with addCommand)`,request:e},e.trackId);this.serverHandleCommand(e)}if("Query"===e.type){if(!this.queries[e.name])return void this.sendError({message:`Query ${e.name} not found (Register it with addQuery)`,request:e},e.trackId);this.serverHandleQuery(e)}if("Event"===e.type){if(!this.eventListeners[e.name])return void this.sendError({message:`Event ${e.name} not found (Register it with addEvent)`,request:e},e.trackId);this.serverHandleEvent(e)}}serverHandleCommand(e){const s=this.commands[e.name],r=s=>{this.sendResponse(t.CommandResponse,e.name,s,e.trackId)},o=s=>{this.sendError({message:s,request:e},e.trackId)},n=(e,s)=>{this.sendEvent(e,s)};try{s({requestMessage:e,request:e.payload,store:this.store,error:o,event:n,response:r})}catch(s){o({message:`Error in command handler for '${e.name}'`,requestMessage:e,error:s,stack:s?.stack})}}serverHandleQuery(e){(0,this.queries[e.name])({requestMessage:e,request:e.payload,store:this.store,error:s=>{this.sendError({message:s,request:e},e.trackId)},event:(e,s)=>{this.sendEvent(e,s)},response:s=>{this.sendResponse(t.QueryResponse,e.name,s,e.trackId)}})}serverHandleEvent(e){(0,this.eventListeners[e.name])({requestMessage:e,request:e.payload,store:this.store,error:s=>{this.sendError({message:s,request:e},e.trackId)},event:(e,s)=>{this.sendEvent(e,s)}})}addCommand(e,s){this.commands[e]=s}addQuery(e,s){this.queries[e]=s}addEventListener(e,s){this.eventListeners[e]=s}}export{u as ClientSideMessageBridgeService,p as InMemoryClientSideServer,d as MessageBridgeServiceBase,r as MessageDirection,t as MessageType,h as SignalRMessageBridgeService,g as WebsocketMessageBridgeService,i as createCommandMessage,c as createEventMessage,n as createMessage,o as createMessageFromDto,a as createQueryMessage};
import{HubConnectionBuilder as e}from"@microsoft/signalr";import{v4 as s}from"uuid";var t,r;function n(e,s=r.ToClient){return e.isError=e.type===t.Error,e.direction??=s,e}function o(e){return{name:e.name,payload:e.payload,type:e.type,direction:e.direction??r.ToClient,trackId:e.trackId??s(),module:e.module,schema:e.schema,created:(new Date).toISOString(),isError:e.type===t.Error}}function i(e){return o({...e,type:t.Command})}function a(e){return o({...e,type:t.Query})}function c(e){return o({...e,type:t.Event})}!function(e){e.Command="Command",e.CommandResponse="CommandResponse",e.Query="Query",e.QueryResponse="QueryResponse",e.Event="Event",e.Error="Error"}(t||(t={})),function(e){e.ToClient="ToClient",e.ToServer="ToServer"}(r||(r={}));class d{constructor(e){this.wsUri=void 0,this.connected=!1,this.trackedRequestMap={},this.subscribedEventListMap={},this.history=[],this.bridgeErrors=[],this.options={timeout:void 0,allowResponseValueWhenCancelled:!1,keepHistoryForReceivedMessages:!1,keepHistoryForSendingMessages:!1,logger:()=>console?.log??(()=>{}),logParseIncomingMessageError:!0,timeoutFromBridgeOptionsMessage:e=>`Timeout after ${e}ms (BridgeOptions.timeout)`,timeoutFromRequestOptionsMessage:e=>`Timeout after ${e}ms (RequestOptions.timeout)`,logParseIncomingMessageErrorFormat:e=>["Bridge-Error (parse messageReceived):",e],logMessageReceived:!1,logMessageReceivedFormat:e=>["Bridge (messageReceived):",e],logSendingMessage:!1,logSendingMessageFormat:e=>["Bridge (sendingMessage):",e]},this.wsUri=e}setOptions(e){this.options={...this.options,...e}}getTrackedRequestMessage(e){return this.trackedRequestMap[e]?.requestMessage}cancelRequest(e){this.trackedRequestMap[e]&&(this.trackedRequestMap[e].requestMessage.cancelled=!0)}onConnect(){this.connected=!0,this.options.onConnect?.()}onError(e,s){void 0!==e&&this.bridgeErrors.push(e),this.options.onError?.(e,s)}onClose(e,s){void 0!==e&&this.bridgeErrors.push(e),this.connected=!1,this.options.onClose?.(e,s)}setOptionalRequestTimeout({requestMessage:e,timeout:s,onTimeout:t}){let r,n;if(void 0!==s?(r=this.options.timeoutFromRequestOptionsMessage?.(s)??`timeout after ${s}`,n=s):void 0!==this.options.timeout&&(r=this.options.timeoutFromBridgeOptionsMessage?.(this.options.timeout)??`timeout after ${this.options.timeout}`,n=this.options.timeout),void 0!==n)return setTimeout(()=>{e.timedOut=!0,t({reason:r,responseMessage:void 0,request:e.payload,requestMessage:e})},n)}sendMessageTracked(e){if(e.requestMessage.cancelled){let s=!0;if(void 0!==e.requestOptions.sendCancelled?s=!e.requestOptions.sendCancelled:this.options.sendCancelledRequest&&(s=!1),s)return Promise.resolve({request:e.requestMessage.payload,requestMessage:e.requestMessage,cancelled:!0})}return new Promise((s,t)=>{this.sendMessagePromiseHandler({...e,handleSuccess:(e,t)=>{s(t)},handleError:(e,r)=>{this.options.throwOnTrackedError?t(r):s(r)}})}).finally(()=>{delete this.trackedRequestMap[e.requestMessage.trackId]})}sendMessage(e){const s=void 0;if(e.requestMessage.cancelled){let t=!0;if(void 0!==e.requestOptions.sendCancelled?t=!e.requestOptions.sendCancelled:this.options.sendCancelledRequest&&(t=!1),t)return Promise.resolve(s)}return new Promise((t,r)=>{this.sendMessagePromiseHandler({...e,handleSuccess:(e,r)=>{t(e?s:r.response)},handleError:(e,n,o)=>{e&&t(s),this.options.avoidThrowOnNonTrackedError?t(o?.payload):r(n)}})})}sendMessagePromiseHandler({handleError:e,handleSuccess:s,requestMessage:t,requestOptions:n}){t.direction=r.ToServer,this.options.interceptSendMessage&&(t=this.options.interceptSendMessage(t));const o=(s,r)=>{i&&clearTimeout(i);const o={response:void 0,responseMessage:void 0,request:t.payload,requestMessage:t,isError:!0,error:r.reason,errorMessage:r.responseMessage,cancelled:t.cancelled||r.requestMessage?.cancelled||r.responseMessage?.cancelled,timedOut:t.timedOut||r.requestMessage?.timedOut||r.responseMessage?.timedOut};s&&this.options.callOnErrorWhenRequestIsCancelled&&n.callOnErrorWhenRequestIsCancelled||(this.onError(o),n.onError?.(o)),e(s,o,r)},i=this.setOptionalRequestTimeout({requestMessage:t,timeout:n.timeout,onTimeout:e=>{const s=this.handleCancelOptions(n,t);o(s,e)}});this.trackedRequestMap[t.trackId]={successTrack:e=>{const{response:r,cancelled:o}=this.handleCancelResponse(n,t,e),a={response:r,responseMessage:e,requestOptions:n,request:t.payload,requestMessage:t,isError:!1,cancelled:t.cancelled||e.cancelled,timedOut:t.timedOut||e.timedOut};i&&clearTimeout(i),(!o||this.options.callOnSuccessWhenRequestIsCancelled||n.callOnSuccessWhenRequestIsCancelled)&&(this.options.onSuccess?.(a),n.onSuccess?.(a)),s(o,a)},errorTrack:e=>{const{response:s,cancelled:r}=this.handleCancelResponse(n,t,e);o(r,{reason:s,responseMessage:e,request:t.payload,requestMessage:t})},requestMessage:t,requestOptions:n},this.internalSendMessage(t)}handleCancelOptions(e,s,t){let r=!1;void 0!==e.resolveCancelledForNonTracked?r=e.resolveCancelledForNonTracked:this.options.resolveCancelledNonTrackedRequest&&(r=!0);let n=!1;return(t?.cancelled||s.cancelled)&&(n=!0),r&&n&&(n=!1),n}handleCancelResponse(e,s,t){const r=this.handleCancelOptions(e,s,t);let n=t?.payload;return r&&(void 0!==e.allowResponseValueWhenCancelled?!0!==e.allowResponseValueWhenCancelled&&(n=void 0):!0!==this.options.allowResponseValueWhenCancelled&&(n=void 0)),{response:n,cancelled:r}}subscribeEvent({name:e,onEvent:s}){if(Array.isArray(e)){const t=e.map(e=>this.subscribeEvent({name:e,onEvent:s}));return()=>t.forEach(e=>e())}return this.subscribedEventListMap[e]||(this.subscribedEventListMap[e]=[]),this.subscribedEventListMap[e].push(s),()=>{const t=this.subscribedEventListMap[e].findIndex(e=>e===s);this.subscribedEventListMap[e].splice(t,1)}}createTrackedMessage(e){const s=e.requestMessage.trackId;let t={trackId:s,requestMessage:e.requestMessage,requestOptions:e.requestOptions,send:()=>this.sendMessage(e),sendTracked:()=>this.sendMessageTracked(e),cancel:()=>{e?.requestMessage&&(e.requestMessage.cancelled=!0),this.cancelRequest(s)}};return this.options.interceptCreatedMessageOptions&&(t=this.options.interceptCreatedMessageOptions(t)),t}createCommand(e){const s=i(e);return this.createTrackedMessage({requestMessage:s,requestOptions:e})}createQuery(e){const s=a(e);return this.createTrackedMessage({requestMessage:s,requestOptions:e})}sendCommand(e){return this.createCommand(e).send()}sendCommandTracked(e){return this.createCommand(e).sendTracked()}sendQuery(e){return this.createQuery(e).send()}sendQueryTracked(e){return this.createQuery(e).sendTracked()}createEvent(e){let s=c(e);s.direction=r.ToServer;let t={trackId:s.trackId,requestMessage:s,requestOptions:e,cancel:()=>{s&&(s.cancelled=!0)},send:()=>{if(s.cancelled)if(void 0!==e.sendCancelled){if(!e.sendCancelled)return}else if(!this.options.sendCancelledRequest)return;this.options.interceptSendMessage&&(s=this.options.interceptSendMessage(s)),this.internalSendMessage(s)}};return this.options.interceptCreatedEventMessageOptions&&(t=this.options.interceptCreatedEventMessageOptions(t)),t}sendEvent(e){return this.createEvent(e).send()}onMessage(e){let s;try{s="string"==typeof e?JSON.parse(e):e}catch(e){return void this.onError(e)}try{let e=n(s);this.options.interceptReceivedMessage&&(e=this.options.interceptReceivedMessage(e)),this.handleIncomingMessage(e)}catch(e){if(this.onError(e),this.options.logger&&this.options.logParseIncomingMessageError){const t=this.options.logParseIncomingMessageErrorFormat?.(s)??[e];this.options.logger(t)}}}internalSendMessage(e){if(this.options.keepHistoryForSendingMessages&&this.history.push(e),this.options.logger&&this.options.logSendingMessage){let s=!0;if(this.options.logSendingMessageFilter&&(s=!!e.name.match(this.options.logSendingMessageFilter)),s){const s=this.options.logSendingMessageFormat?.(e)??[e];this.options.logger(...s)}}this.options.onSend?.(e),this.sendNetworkMessage(e)}handleIncomingMessage(e){if(this.options.keepHistoryForReceivedMessages&&this.history.push(e),this.options.logger&&this.options.logMessageReceived){let s=!0;if(this.options.logMessageReceivedFilter&&(s=!!e.name.match(this.options.logMessageReceivedFilter)),s){const s=this.options.logMessageReceivedFormat?.(e)??[e];this.options.logger(...s)}}this.options.onMessage?.(e);let s=e.type!==t.Error;if(e.type===t.Event)return void this.receiveEventMessage(e);const r=this.trackedRequestMap[e.trackId];r&&(e.type===t.Error?(r.errorTrack(e),s=!0):r.successTrack(e),delete this.trackedRequestMap[e.trackId]),s||this.onError?.(e)}receiveEventMessage(e){this.subscribedEventListMap[e.name]&&this.subscribedEventListMap[e.name].forEach(s=>s(e.payload,e))}}class l extends d{constructor(...e){super(...e),this.connection=void 0}connect(s={}){if(this.connection=(new e).withUrl(this.wsUri,s).withAutomaticReconnect().build(),!this.connection)throw new Error("Failed to create SignalR connection");return this.connection.on("ReceiveMessage",e=>{this.onMessage(e)}),this.connection.onclose(e=>{this.onClose(e)}),this.connection.start().then(()=>{this.onConnect()}).catch(e=>{this.onError(e)})}close(){this.connection?.stop(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.connection?.invoke("SendMessage",s).catch(e=>(this.onError(e),console.error(e.toString())))}}class h extends d{constructor(...e){super(...e),this.socket=void 0,this.connectedCallback=void 0}connect(){return this.socket=new WebSocket(this.wsUri),this.socket.addEventListener("message",e=>{this.onMessage(e.data)}),this.socket.addEventListener("close",e=>{this.onClose(e.reason,e)}),this.socket.addEventListener("error",e=>{this.onError(e,e)}),new Promise((e,s)=>{this.socket?.addEventListener("open",s=>{this.onConnect(),e()})})}close(){this.socket?.close(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.socket?.send(s)}}class u extends d{constructor(...e){super(...e),this.server=void 0}setServer(e){this.server=e}connect(){if(!this.server)throw new Error("No server set");return this.server?.connect(e=>{const s=JSON.parse(JSON.stringify(e));this.onMessage(s),this.onConnect()}),Promise.resolve()}close(){this.onClose()}sendNetworkMessage(e){const s=JSON.parse(JSON.stringify(e));setTimeout(()=>{this.server?.onMessage(s)},10)}}class g{constructor(){this.store={},this.commands={},this.queries={},this.eventListeners={},this.sendMessage=void 0}saveToLocalStorage(e){localStorage.setItem(e,JSON.stringify(this.store))}loadFromLocalStorage(e){const s=localStorage.getItem(e);s&&(this.store=JSON.parse(s))}connect(e){this.sendMessage=e}sendError(e,s,n,i){const a=o({trackId:s,type:t.Error,name:"Error",payload:e,direction:r.ToClient,cancelled:n,timedOut:i});this.sendMessage?.(a)}createMessage(e){return o({...e,direction:r.ToClient})}sendResponse(e){const s=this.createMessage(e);this.sendMessage?.(s)}sendEvent(e,s){const n=o({type:t.Event,name:e,payload:s,direction:r.ToClient});this.sendMessage?.(n)}onMessage(e){if("string"==typeof e)try{e=JSON.parse(e)}catch(s){return this.sendError({message:`Error parsing message: ${s}`,request:e,error:s,stack:s?.stack}),void console.error("Error parsing message",s)}if("Command"===e.type){if(!this.commands[e.name])return void this.sendError({message:`Command ${e.name} not found (Register it with addCommand)`,request:e},e.trackId);this.serverHandleCommand(e)}if("Query"===e.type){if(!this.queries[e.name])return void this.sendError({message:`Query ${e.name} not found (Register it with addQuery)`,request:e},e.trackId);this.serverHandleQuery(e)}if("Event"===e.type){if(!this.eventListeners[e.name])return void this.sendError({message:`Event ${e.name} not found (Register it with addEvent)`,request:e},e.trackId);this.serverHandleEvent(e)}}serverHandleCommand(e){const s=this.commands[e.name],r=e=>{this.sendMessage?.(e)},n=s=>this.createMessage({name:e.name,type:t.CommandResponse,payload:s,trackId:e.trackId}),o=e=>{const s=n(e);r(s)},i=(s,t,r)=>{this.sendError({message:s,request:e},e.trackId,t,r)},a=(e,s)=>{this.sendEvent(e,s)};try{s({requestMessage:e,request:e.payload,store:this.store,error:i,event:a,response:o,createResponseMessage:n,sendResponseMessage:r})}catch(s){i({message:`Error in command handler for '${e.name}'`,requestMessage:e,error:s,stack:s?.stack})}}serverHandleQuery(e){const s=e=>{this.sendMessage?.(e)},r=s=>this.createMessage({name:e.name,type:t.QueryResponse,payload:s,trackId:e.trackId});(0,this.queries[e.name])({requestMessage:e,request:e.payload,store:this.store,error:(s,t,r)=>{this.sendError({message:s,request:e},e.trackId,t,r)},event:(e,s)=>{this.sendEvent(e,s)},response:e=>{const t=r(e);s(t)},createResponseMessage:r,sendResponseMessage:s})}serverHandleEvent(e){(0,this.eventListeners[e.name])({requestMessage:e,request:e.payload,store:this.store,error:s=>{this.sendError({message:s,request:e},e.trackId)},event:(e,s)=>{this.sendEvent(e,s)}})}addCommand(e,s){this.commands[e]=s}addQuery(e,s){this.queries[e]=s}addEventListener(e,s){this.eventListeners[e]=s}}export{u as ClientSideMessageBridgeService,g as InMemoryClientSideServer,d as MessageBridgeServiceBase,r as MessageDirection,t as MessageType,l as SignalRMessageBridgeService,h as WebsocketMessageBridgeService,i as createCommandMessage,c as createEventMessage,o as createMessage,n as createMessageFromDto,a as createQueryMessage};
//# sourceMappingURL=index.module.js.map

@@ -1,2 +0,2 @@

!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports,require("@microsoft/signalr"),require("uuid")):"function"==typeof define&&define.amd?define(["exports","@microsoft/signalr","uuid"],s):s((e||self).messageBridgeJs={},e.signalr,e.uuid)}(this,function(e,s,t){var r,o;function n(s,t=e.MessageDirection.ToClient){return s.isError=s.type===e.MessageType.Error,s.direction??=t,s}function i(s){return{name:s.name,payload:s.payload,type:s.type,direction:s.direction??e.MessageDirection.ToClient,trackId:s.trackId??t.v4(),module:s.module,schema:s.schema,created:(new Date).toISOString(),isError:s.type===e.MessageType.Error}}function a(s){return i({...s,type:e.MessageType.Command})}function c(s){return i({...s,type:e.MessageType.Query})}function d(s){return i({...s,type:e.MessageType.Event,payload:void 0})}e.MessageType=void 0,(r=e.MessageType||(e.MessageType={})).Command="Command",r.CommandResponse="CommandResponse",r.Query="Query",r.QueryResponse="QueryResponse",r.Event="Event",r.Error="Error",e.MessageDirection=void 0,(o=e.MessageDirection||(e.MessageDirection={})).ToClient="ToClient",o.ToServer="ToServer";class g{constructor(e){this.wsUri=void 0,this.connected=!1,this.subscribedTrackIdMap={},this.subscribedEventListMap={},this.history=[],this.bridgeErrors=[],this.options={timeout:void 0,keepHistoryForReceivedMessages:!1,keepHistoryForSendingMessages:!1,logger:()=>console?.log??(()=>{}),logParseIncomingMessageError:!0,timeoutFromBridgeOptionsMessage:e=>`Timeout after ${e}ms (BridgeOptions.timeout)`,timeoutFromRequestOptionsMessage:e=>`Timeout after ${e}ms (RequestOptions.timeout)`,logParseIncomingMessageErrorFormat:e=>["Bridge-Error (parse messageReceived):",e],logMessageReceived:!1,logMessageReceivedFormat:e=>["Bridge (messageReceived):",e],logSendingMessage:!1,logSendingMessageFormat:e=>["Bridge (sendingMessage):",e]},this.wsUri=e}setOptions(e){this.options={...this.options,...e}}getTrackedRequestMessage(e){return this.subscribedTrackIdMap[e]?.requestMessage}onConnect(){this.connected=!0,this.options.onConnect?.()}onError(e,s){void 0!==e&&this.bridgeErrors.push(e),this.options.onError?.(e,s)}onClose(e,s){void 0!==e&&this.bridgeErrors.push(e),this.connected=!1,this.options.onClose?.(e,s)}setOptionalRequestTimeout({requestMessage:e,timeout:s,onTimeout:t}){let r,o;if(void 0!==this.options.timeout&&(r=this.options.timeoutFromBridgeOptionsMessage?.(this.options.timeout)??`timeout after ${this.options.timeout}`,o=this.options.timeout),void 0!==s&&(r=this.options.timeoutFromRequestOptionsMessage?.(s)??`timeout after ${s}`,o=s),void 0!==o)return setTimeout(()=>{t({reason:r,responseMessage:void 0,request:e.payload,requestMessage:e})},o)}sendMessageTracked(e){return new Promise((s,t)=>{this.sendMessagePromiseHandler({...e,handleResponseReject:(e,r)=>{e&&this.options.throwOnTrackedError?t(r):s(r)}})}).finally(()=>{delete this.subscribedTrackIdMap[e.requestMessage.trackId]})}sendMessage(e){return new Promise((s,t)=>{this.sendMessagePromiseHandler({...e,handleResponseReject:(e,r,o)=>{e?this.options.avoidThrowOnNonTrackedError?s(o?.payload):t(r):s(r.response)}})})}sendMessagePromiseHandler({handleResponseReject:s,requestMessage:t,onSuccess:r,onError:o,timeout:n}){t.direction=e.MessageDirection.ToServer,this.options.interceptSendMessage&&(t=this.options.interceptSendMessage(t));const i=e=>{a&&clearTimeout(a);const r={response:void 0,responseMessage:void 0,request:t.payload,requestMessage:t,isError:!0,error:e.reason,errorMessage:e.responseMessage};this.onError(r),o?.(r),s(!0,r,e)},a=this.setOptionalRequestTimeout({requestMessage:t,timeout:n,onTimeout:e=>{i(e)}});this.subscribedTrackIdMap[t.trackId]={successTrack:e=>{const o={response:e.payload,responseMessage:e,request:t.payload,requestMessage:t,isError:!1};a&&clearTimeout(a),r?.(o),s(!1,o,void 0)},errorTrack:e=>{i({reason:e?.payload,responseMessage:e,request:t.payload,requestMessage:t})},requestMessage:t},this.internalSendMessage(t)}subscribeEvent({name:e,onEvent:s}){if(Array.isArray(e)){const t=e.map(e=>this.subscribeEvent({name:e,onEvent:s}));return()=>t.forEach(e=>e())}return this.subscribedEventListMap[e]||(this.subscribedEventListMap[e]=[]),this.subscribedEventListMap[e].push(s),()=>{const t=this.subscribedEventListMap[e].findIndex(e=>e===s);this.subscribedEventListMap[e].splice(t,1)}}sendCommand(e){const s=a(e);return this.sendMessage({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendCommandTracked(e){const s=a(e);return this.sendMessageTracked({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendQuery(e){const s=c(e);return this.sendMessage({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendQueryTracked(e){const s=c(e);return this.sendMessageTracked({requestMessage:s,onSuccess:e.onSuccess,onError:e.onError,timeout:e.timeout})}sendEvent(s){let t=d(s);return t.direction=e.MessageDirection.ToServer,this.options.interceptSendMessage&&(t=this.options.interceptSendMessage(t)),this.internalSendMessage(t),t}onMessage(e){let s;try{s="string"==typeof e?JSON.parse(e):e}catch(e){return void this.onError(e)}try{let e=n(s);this.options.interceptReceivedMessage&&(e=this.options.interceptReceivedMessage(e)),this.handleIncomingMessage(e)}catch(e){if(this.onError(e),this.options?.logger&&this.options?.logParseIncomingMessageError){const t=this.options?.logParseIncomingMessageErrorFormat?.(s)??[e];this.options.logger(t)}}}internalSendMessage(e){if(this.options.keepHistoryForSendingMessages&&this.history.push(e),this.options?.logger&&this.options?.logSendingMessage){let s=!0;if(this.options?.logSendingMessageFilter&&(s=!!e.name.match(this.options?.logSendingMessageFilter)),s){const s=this.options?.logSendingMessageFormat?.(e)??[e];this.options.logger(...s)}}this.options.onSend?.(e),this.sendNetworkMessage(e)}handleIncomingMessage(s){if(this.options.keepHistoryForReceivedMessages&&this.history.push(s),this.options?.logger&&this.options?.logMessageReceived){let e=!0;if(this.options?.logMessageReceivedFilter&&(e=!!s.name.match(this.options?.logMessageReceivedFilter)),e){const e=this.options?.logMessageReceivedFormat?.(s)??[s];this.options.logger(...e)}}this.options.onMessage?.(s);let t=s.type!==e.MessageType.Error;if(s.type===e.MessageType.Event)return void this.receiveEventMessage(s);const r=this.subscribedTrackIdMap[s.trackId];r&&(s.type===e.MessageType.Error?(r.errorTrack?.(s),t=!0):r.successTrack?.(s),delete this.subscribedTrackIdMap[s.trackId]),t||this.onError?.(s)}receiveEventMessage(e){this.subscribedEventListMap[e.name]&&this.subscribedEventListMap[e.name].forEach(s=>s(e.payload,e))}}e.ClientSideMessageBridgeService=class extends g{constructor(...e){super(...e),this.server=void 0}setServer(e){this.server=e}connect(){if(!this.server)throw new Error("No server set");return this.server?.connect(e=>{this.onMessage(e),this.onConnect()}),Promise.resolve()}close(){this.onClose()}sendNetworkMessage(e){setTimeout(()=>{this.server?.onMessage(e)},10)}},e.InMemoryClientSideServer=class{constructor(){this.store={},this.commands={},this.queries={},this.eventListeners={},this.sendMessage=void 0}saveToLocalStorage(e){localStorage.setItem(e,JSON.stringify(this.store))}loadFromLocalStorage(e){const s=localStorage.getItem(e);s&&(this.store=JSON.parse(s))}connect(e){this.sendMessage=e}sendError(s,t){const r=i({trackId:t,type:e.MessageType.Error,name:"Error",payload:s,direction:e.MessageDirection.ToClient});this.sendMessage?.(r)}sendResponse(s,t,r,o){const n=i({trackId:o,type:s,name:t,payload:r,direction:e.MessageDirection.ToClient});this.sendMessage?.(n)}sendEvent(s,t){const r=i({type:e.MessageType.Event,name:s,payload:t,direction:e.MessageDirection.ToClient});this.sendMessage?.(r)}onMessage(e){if("string"==typeof e)try{e=JSON.parse(e)}catch(s){return this.sendError({message:`Error parsing message: ${s}`,request:e,error:s,stack:s?.stack}),void console.error("Error parsing message",s)}if("Command"===e.type){if(!this.commands[e.name])return void this.sendError({message:`Command ${e.name} not found (Register it with addCommand)`,request:e},e.trackId);this.serverHandleCommand(e)}if("Query"===e.type){if(!this.queries[e.name])return void this.sendError({message:`Query ${e.name} not found (Register it with addQuery)`,request:e},e.trackId);this.serverHandleQuery(e)}if("Event"===e.type){if(!this.eventListeners[e.name])return void this.sendError({message:`Event ${e.name} not found (Register it with addEvent)`,request:e},e.trackId);this.serverHandleEvent(e)}}serverHandleCommand(s){const t=this.commands[s.name],r=t=>{this.sendResponse(e.MessageType.CommandResponse,s.name,t,s.trackId)},o=e=>{this.sendError({message:e,request:s},s.trackId)},n=(e,s)=>{this.sendEvent(e,s)};try{t({requestMessage:s,request:s.payload,store:this.store,error:o,event:n,response:r})}catch(e){o({message:`Error in command handler for '${s.name}'`,requestMessage:s,error:e,stack:e?.stack})}}serverHandleQuery(s){(0,this.queries[s.name])({requestMessage:s,request:s.payload,store:this.store,error:e=>{this.sendError({message:e,request:s},s.trackId)},event:(e,s)=>{this.sendEvent(e,s)},response:t=>{this.sendResponse(e.MessageType.QueryResponse,s.name,t,s.trackId)}})}serverHandleEvent(e){(0,this.eventListeners[e.name])({requestMessage:e,request:e.payload,store:this.store,error:s=>{this.sendError({message:s,request:e},e.trackId)},event:(e,s)=>{this.sendEvent(e,s)}})}addCommand(e,s){this.commands[e]=s}addQuery(e,s){this.queries[e]=s}addEventListener(e,s){this.eventListeners[e]=s}},e.MessageBridgeServiceBase=g,e.SignalRMessageBridgeService=class extends g{constructor(...e){super(...e),this.connection=void 0}connect(e={}){if(this.connection=(new s.HubConnectionBuilder).withUrl(this.wsUri,e).withAutomaticReconnect().build(),!this.connection)throw new Error("Failed to create SignalR connection");return this.connection.on("ReceiveMessage",e=>{this.onMessage(e)}),this.connection.onclose(e=>{this.onClose(e)}),this.connection.start().then(()=>{this.onConnect()}).catch(e=>{this.onError(e)})}close(){this.connection?.stop(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.connection?.invoke("SendMessage",s).catch(e=>(this.onError(e),console.error(e.toString())))}},e.WebsocketMessageBridgeService=class extends g{constructor(...e){super(...e),this.socket=void 0,this.connectedCallback=void 0}connect(){return this.socket=new WebSocket(this.wsUri),this.socket.addEventListener("message",e=>{this.onMessage(e.data)}),this.socket.addEventListener("close",e=>{this.onClose(e.reason,e)}),this.socket.addEventListener("error",e=>{this.onError(e,e)}),new Promise((e,s)=>{this.socket?.addEventListener("open",s=>{this.onConnect(),e()})})}close(){this.socket?.close(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.socket?.send(s)}},e.createCommandMessage=a,e.createEventMessage=d,e.createMessage=i,e.createMessageFromDto=n,e.createQueryMessage=c});
!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports,require("@microsoft/signalr"),require("uuid")):"function"==typeof define&&define.amd?define(["exports","@microsoft/signalr","uuid"],s):s((e||self).messageBridgeJs={},e.signalr,e.uuid)}(this,function(e,s,t){var r,n;function o(s,t=e.MessageDirection.ToClient){return s.isError=s.type===e.MessageType.Error,s.direction??=t,s}function i(s){return{name:s.name,payload:s.payload,type:s.type,direction:s.direction??e.MessageDirection.ToClient,trackId:s.trackId??t.v4(),module:s.module,schema:s.schema,created:(new Date).toISOString(),isError:s.type===e.MessageType.Error}}function a(s){return i({...s,type:e.MessageType.Command})}function c(s){return i({...s,type:e.MessageType.Query})}function d(s){return i({...s,type:e.MessageType.Event})}e.MessageType=void 0,(r=e.MessageType||(e.MessageType={})).Command="Command",r.CommandResponse="CommandResponse",r.Query="Query",r.QueryResponse="QueryResponse",r.Event="Event",r.Error="Error",e.MessageDirection=void 0,(n=e.MessageDirection||(e.MessageDirection={})).ToClient="ToClient",n.ToServer="ToServer";class l{constructor(e){this.wsUri=void 0,this.connected=!1,this.trackedRequestMap={},this.subscribedEventListMap={},this.history=[],this.bridgeErrors=[],this.options={timeout:void 0,allowResponseValueWhenCancelled:!1,keepHistoryForReceivedMessages:!1,keepHistoryForSendingMessages:!1,logger:()=>console?.log??(()=>{}),logParseIncomingMessageError:!0,timeoutFromBridgeOptionsMessage:e=>`Timeout after ${e}ms (BridgeOptions.timeout)`,timeoutFromRequestOptionsMessage:e=>`Timeout after ${e}ms (RequestOptions.timeout)`,logParseIncomingMessageErrorFormat:e=>["Bridge-Error (parse messageReceived):",e],logMessageReceived:!1,logMessageReceivedFormat:e=>["Bridge (messageReceived):",e],logSendingMessage:!1,logSendingMessageFormat:e=>["Bridge (sendingMessage):",e]},this.wsUri=e}setOptions(e){this.options={...this.options,...e}}getTrackedRequestMessage(e){return this.trackedRequestMap[e]?.requestMessage}cancelRequest(e){this.trackedRequestMap[e]&&(this.trackedRequestMap[e].requestMessage.cancelled=!0)}onConnect(){this.connected=!0,this.options.onConnect?.()}onError(e,s){void 0!==e&&this.bridgeErrors.push(e),this.options.onError?.(e,s)}onClose(e,s){void 0!==e&&this.bridgeErrors.push(e),this.connected=!1,this.options.onClose?.(e,s)}setOptionalRequestTimeout({requestMessage:e,timeout:s,onTimeout:t}){let r,n;if(void 0!==s?(r=this.options.timeoutFromRequestOptionsMessage?.(s)??`timeout after ${s}`,n=s):void 0!==this.options.timeout&&(r=this.options.timeoutFromBridgeOptionsMessage?.(this.options.timeout)??`timeout after ${this.options.timeout}`,n=this.options.timeout),void 0!==n)return setTimeout(()=>{e.timedOut=!0,t({reason:r,responseMessage:void 0,request:e.payload,requestMessage:e})},n)}sendMessageTracked(e){if(e.requestMessage.cancelled){let s=!0;if(void 0!==e.requestOptions.sendCancelled?s=!e.requestOptions.sendCancelled:this.options.sendCancelledRequest&&(s=!1),s)return Promise.resolve({request:e.requestMessage.payload,requestMessage:e.requestMessage,cancelled:!0})}return new Promise((s,t)=>{this.sendMessagePromiseHandler({...e,handleSuccess:(e,t)=>{s(t)},handleError:(e,r)=>{this.options.throwOnTrackedError?t(r):s(r)}})}).finally(()=>{delete this.trackedRequestMap[e.requestMessage.trackId]})}sendMessage(e){const s=void 0;if(e.requestMessage.cancelled){let t=!0;if(void 0!==e.requestOptions.sendCancelled?t=!e.requestOptions.sendCancelled:this.options.sendCancelledRequest&&(t=!1),t)return Promise.resolve(s)}return new Promise((t,r)=>{this.sendMessagePromiseHandler({...e,handleSuccess:(e,r)=>{t(e?s:r.response)},handleError:(e,n,o)=>{e&&t(s),this.options.avoidThrowOnNonTrackedError?t(o?.payload):r(n)}})})}sendMessagePromiseHandler({handleError:s,handleSuccess:t,requestMessage:r,requestOptions:n}){r.direction=e.MessageDirection.ToServer,this.options.interceptSendMessage&&(r=this.options.interceptSendMessage(r));const o=(e,t)=>{i&&clearTimeout(i);const o={response:void 0,responseMessage:void 0,request:r.payload,requestMessage:r,isError:!0,error:t.reason,errorMessage:t.responseMessage,cancelled:r.cancelled||t.requestMessage?.cancelled||t.responseMessage?.cancelled,timedOut:r.timedOut||t.requestMessage?.timedOut||t.responseMessage?.timedOut};e&&this.options.callOnErrorWhenRequestIsCancelled&&n.callOnErrorWhenRequestIsCancelled||(this.onError(o),n.onError?.(o)),s(e,o,t)},i=this.setOptionalRequestTimeout({requestMessage:r,timeout:n.timeout,onTimeout:e=>{const s=this.handleCancelOptions(n,r);o(s,e)}});this.trackedRequestMap[r.trackId]={successTrack:e=>{const{response:s,cancelled:o}=this.handleCancelResponse(n,r,e),a={response:s,responseMessage:e,requestOptions:n,request:r.payload,requestMessage:r,isError:!1,cancelled:r.cancelled||e.cancelled,timedOut:r.timedOut||e.timedOut};i&&clearTimeout(i),(!o||this.options.callOnSuccessWhenRequestIsCancelled||n.callOnSuccessWhenRequestIsCancelled)&&(this.options.onSuccess?.(a),n.onSuccess?.(a)),t(o,a)},errorTrack:e=>{const{response:s,cancelled:t}=this.handleCancelResponse(n,r,e);o(t,{reason:s,responseMessage:e,request:r.payload,requestMessage:r})},requestMessage:r,requestOptions:n},this.internalSendMessage(r)}handleCancelOptions(e,s,t){let r=!1;void 0!==e.resolveCancelledForNonTracked?r=e.resolveCancelledForNonTracked:this.options.resolveCancelledNonTrackedRequest&&(r=!0);let n=!1;return(t?.cancelled||s.cancelled)&&(n=!0),r&&n&&(n=!1),n}handleCancelResponse(e,s,t){const r=this.handleCancelOptions(e,s,t);let n=t?.payload;return r&&(void 0!==e.allowResponseValueWhenCancelled?!0!==e.allowResponseValueWhenCancelled&&(n=void 0):!0!==this.options.allowResponseValueWhenCancelled&&(n=void 0)),{response:n,cancelled:r}}subscribeEvent({name:e,onEvent:s}){if(Array.isArray(e)){const t=e.map(e=>this.subscribeEvent({name:e,onEvent:s}));return()=>t.forEach(e=>e())}return this.subscribedEventListMap[e]||(this.subscribedEventListMap[e]=[]),this.subscribedEventListMap[e].push(s),()=>{const t=this.subscribedEventListMap[e].findIndex(e=>e===s);this.subscribedEventListMap[e].splice(t,1)}}createTrackedMessage(e){const s=e.requestMessage.trackId;let t={trackId:s,requestMessage:e.requestMessage,requestOptions:e.requestOptions,send:()=>this.sendMessage(e),sendTracked:()=>this.sendMessageTracked(e),cancel:()=>{e?.requestMessage&&(e.requestMessage.cancelled=!0),this.cancelRequest(s)}};return this.options.interceptCreatedMessageOptions&&(t=this.options.interceptCreatedMessageOptions(t)),t}createCommand(e){const s=a(e);return this.createTrackedMessage({requestMessage:s,requestOptions:e})}createQuery(e){const s=c(e);return this.createTrackedMessage({requestMessage:s,requestOptions:e})}sendCommand(e){return this.createCommand(e).send()}sendCommandTracked(e){return this.createCommand(e).sendTracked()}sendQuery(e){return this.createQuery(e).send()}sendQueryTracked(e){return this.createQuery(e).sendTracked()}createEvent(s){let t=d(s);t.direction=e.MessageDirection.ToServer;let r={trackId:t.trackId,requestMessage:t,requestOptions:s,cancel:()=>{t&&(t.cancelled=!0)},send:()=>{if(t.cancelled)if(void 0!==s.sendCancelled){if(!s.sendCancelled)return}else if(!this.options.sendCancelledRequest)return;this.options.interceptSendMessage&&(t=this.options.interceptSendMessage(t)),this.internalSendMessage(t)}};return this.options.interceptCreatedEventMessageOptions&&(r=this.options.interceptCreatedEventMessageOptions(r)),r}sendEvent(e){return this.createEvent(e).send()}onMessage(e){let s;try{s="string"==typeof e?JSON.parse(e):e}catch(e){return void this.onError(e)}try{let e=o(s);this.options.interceptReceivedMessage&&(e=this.options.interceptReceivedMessage(e)),this.handleIncomingMessage(e)}catch(e){if(this.onError(e),this.options.logger&&this.options.logParseIncomingMessageError){const t=this.options.logParseIncomingMessageErrorFormat?.(s)??[e];this.options.logger(t)}}}internalSendMessage(e){if(this.options.keepHistoryForSendingMessages&&this.history.push(e),this.options.logger&&this.options.logSendingMessage){let s=!0;if(this.options.logSendingMessageFilter&&(s=!!e.name.match(this.options.logSendingMessageFilter)),s){const s=this.options.logSendingMessageFormat?.(e)??[e];this.options.logger(...s)}}this.options.onSend?.(e),this.sendNetworkMessage(e)}handleIncomingMessage(s){if(this.options.keepHistoryForReceivedMessages&&this.history.push(s),this.options.logger&&this.options.logMessageReceived){let e=!0;if(this.options.logMessageReceivedFilter&&(e=!!s.name.match(this.options.logMessageReceivedFilter)),e){const e=this.options.logMessageReceivedFormat?.(s)??[s];this.options.logger(...e)}}this.options.onMessage?.(s);let t=s.type!==e.MessageType.Error;if(s.type===e.MessageType.Event)return void this.receiveEventMessage(s);const r=this.trackedRequestMap[s.trackId];r&&(s.type===e.MessageType.Error?(r.errorTrack(s),t=!0):r.successTrack(s),delete this.trackedRequestMap[s.trackId]),t||this.onError?.(s)}receiveEventMessage(e){this.subscribedEventListMap[e.name]&&this.subscribedEventListMap[e.name].forEach(s=>s(e.payload,e))}}e.ClientSideMessageBridgeService=class extends l{constructor(...e){super(...e),this.server=void 0}setServer(e){this.server=e}connect(){if(!this.server)throw new Error("No server set");return this.server?.connect(e=>{const s=JSON.parse(JSON.stringify(e));this.onMessage(s),this.onConnect()}),Promise.resolve()}close(){this.onClose()}sendNetworkMessage(e){const s=JSON.parse(JSON.stringify(e));setTimeout(()=>{this.server?.onMessage(s)},10)}},e.InMemoryClientSideServer=class{constructor(){this.store={},this.commands={},this.queries={},this.eventListeners={},this.sendMessage=void 0}saveToLocalStorage(e){localStorage.setItem(e,JSON.stringify(this.store))}loadFromLocalStorage(e){const s=localStorage.getItem(e);s&&(this.store=JSON.parse(s))}connect(e){this.sendMessage=e}sendError(s,t,r,n){const o=i({trackId:t,type:e.MessageType.Error,name:"Error",payload:s,direction:e.MessageDirection.ToClient,cancelled:r,timedOut:n});this.sendMessage?.(o)}createMessage(s){return i({...s,direction:e.MessageDirection.ToClient})}sendResponse(e){const s=this.createMessage(e);this.sendMessage?.(s)}sendEvent(s,t){const r=i({type:e.MessageType.Event,name:s,payload:t,direction:e.MessageDirection.ToClient});this.sendMessage?.(r)}onMessage(e){if("string"==typeof e)try{e=JSON.parse(e)}catch(s){return this.sendError({message:`Error parsing message: ${s}`,request:e,error:s,stack:s?.stack}),void console.error("Error parsing message",s)}if("Command"===e.type){if(!this.commands[e.name])return void this.sendError({message:`Command ${e.name} not found (Register it with addCommand)`,request:e},e.trackId);this.serverHandleCommand(e)}if("Query"===e.type){if(!this.queries[e.name])return void this.sendError({message:`Query ${e.name} not found (Register it with addQuery)`,request:e},e.trackId);this.serverHandleQuery(e)}if("Event"===e.type){if(!this.eventListeners[e.name])return void this.sendError({message:`Event ${e.name} not found (Register it with addEvent)`,request:e},e.trackId);this.serverHandleEvent(e)}}serverHandleCommand(s){const t=this.commands[s.name],r=e=>{this.sendMessage?.(e)},n=t=>this.createMessage({name:s.name,type:e.MessageType.CommandResponse,payload:t,trackId:s.trackId}),o=e=>{const s=n(e);r(s)},i=(e,t,r)=>{this.sendError({message:e,request:s},s.trackId,t,r)},a=(e,s)=>{this.sendEvent(e,s)};try{t({requestMessage:s,request:s.payload,store:this.store,error:i,event:a,response:o,createResponseMessage:n,sendResponseMessage:r})}catch(e){i({message:`Error in command handler for '${s.name}'`,requestMessage:s,error:e,stack:e?.stack})}}serverHandleQuery(s){const t=e=>{this.sendMessage?.(e)},r=t=>this.createMessage({name:s.name,type:e.MessageType.QueryResponse,payload:t,trackId:s.trackId});(0,this.queries[s.name])({requestMessage:s,request:s.payload,store:this.store,error:(e,t,r)=>{this.sendError({message:e,request:s},s.trackId,t,r)},event:(e,s)=>{this.sendEvent(e,s)},response:e=>{const s=r(e);t(s)},createResponseMessage:r,sendResponseMessage:t})}serverHandleEvent(e){(0,this.eventListeners[e.name])({requestMessage:e,request:e.payload,store:this.store,error:s=>{this.sendError({message:s,request:e},e.trackId)},event:(e,s)=>{this.sendEvent(e,s)}})}addCommand(e,s){this.commands[e]=s}addQuery(e,s){this.queries[e]=s}addEventListener(e,s){this.eventListeners[e]=s}},e.MessageBridgeServiceBase=l,e.SignalRMessageBridgeService=class extends l{constructor(...e){super(...e),this.connection=void 0}connect(e={}){if(this.connection=(new s.HubConnectionBuilder).withUrl(this.wsUri,e).withAutomaticReconnect().build(),!this.connection)throw new Error("Failed to create SignalR connection");return this.connection.on("ReceiveMessage",e=>{this.onMessage(e)}),this.connection.onclose(e=>{this.onClose(e)}),this.connection.start().then(()=>{this.onConnect()}).catch(e=>{this.onError(e)})}close(){this.connection?.stop(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.connection?.invoke("SendMessage",s).catch(e=>(this.onError(e),console.error(e.toString())))}},e.WebsocketMessageBridgeService=class extends l{constructor(...e){super(...e),this.socket=void 0,this.connectedCallback=void 0}connect(){return this.socket=new WebSocket(this.wsUri),this.socket.addEventListener("message",e=>{this.onMessage(e.data)}),this.socket.addEventListener("close",e=>{this.onClose(e.reason,e)}),this.socket.addEventListener("error",e=>{this.onError(e,e)}),new Promise((e,s)=>{this.socket?.addEventListener("open",s=>{this.onConnect(),e()})})}close(){this.socket?.close(),this.onClose()}sendNetworkMessage(e){const s=JSON.stringify(e);this.socket?.send(s)}},e.createCommandMessage=a,e.createEventMessage=d,e.createMessage=i,e.createMessageFromDto=o,e.createQueryMessage=c});
//# sourceMappingURL=index.umd.js.map
import { Message, MessageDirection, OmitAndOptional } from "./MessageBridgeTypes";
export declare function createMessageFromDto<TPayload = any, TSchema = any>(msg: Message<TPayload, TSchema>, direction?: MessageDirection): Message<TPayload, TSchema>;
export declare function createMessage<TPayload = any, TSchema = any>(opt: OmitAndOptional<Message<TPayload, TSchema>, "created" | "isError", "trackId" | "direction">): Message<TPayload, TSchema>;
export declare function createMessage<TPayload = any | undefined, TSchema = any>(opt: OmitAndOptional<Message<TPayload, TSchema>, "created" | "isError", "trackId" | "direction">): Message<TPayload, TSchema>;
export declare function createCommandMessage<TRequest = any, TSchema = any>(opt: OmitAndOptional<Message<TRequest, TSchema>, "created" | "isError" | "type", "trackId" | "direction">): Message<TRequest, TSchema>;
export declare function createQueryMessage<TRequest = any, TSchema = any>(opt: OmitAndOptional<Message<TRequest, TSchema>, "created" | "isError" | "type", "trackId" | "direction">): Message<TRequest, TSchema>;
export declare function createEventMessage<TRequest = any>(opt: OmitAndOptional<Message<TRequest>, "created" | "isError" | "type", "trackId" | "direction" | "payload">): Message<undefined, any>;
export declare function createEventMessage<TPayload = any>(opt: OmitAndOptional<Message<TPayload>, "created" | "isError" | "type", "trackId" | "direction">): Message<TPayload, any>;
//# sourceMappingURL=MessageBridgeHelper.d.ts.map
/// <reference types="node" />
import { Message, RequestResponse, SubscribeEvent, InternalTrackedSubscribeResponseWithCatch, OmitAndOptional, SubscribeError, RequestOptionsTracked, BridgeOptions, RequestMaybeNoError, SendMessageOptions } from "./MessageBridgeTypes";
import { Message, RequestResponse, SubscribeEvent, InternalTrackedRequest, OmitAndOptional, OnTimeoutHandler, RequestOptions, BridgeOptions, RequestMaybeNoError, SendMessageOptions, CreatedMessage, CreatedEvent, EventOptions } from "./MessageBridgeTypes";
export declare abstract class MessageBridgeServiceBase {
wsUri: string;
connected: boolean;
subscribedTrackIdMap: {
[trackId: string]: InternalTrackedSubscribeResponseWithCatch<any, any>;
trackedRequestMap: {
[trackId: string]: InternalTrackedRequest<any, any>;
};

@@ -21,2 +21,3 @@ subscribedEventListMap: {

getTrackedRequestMessage(trackId: string): Message | undefined;
cancelRequest(trackId: string): void;
protected onConnect(): void;

@@ -28,3 +29,3 @@ protected onError(err?: unknown, eventOrData?: unknown): void;

timeout: number | undefined;
onTimeout: SubscribeError<string, TRequest>;
onTimeout: OnTimeoutHandler<string, TRequest>;
}): NodeJS.Timeout | undefined;

@@ -37,5 +38,8 @@ sendMessageTracked<TRequest = any, TResponse = any, TError = any, TSchema = any>(opt: SendMessageOptions<TRequest, TResponse, TError, TSchema>): Promise<RequestResponse<TRequest, TResponse, TError>>;

sendMessage<TRequest = any, TResponse = any, TError = any, TSchema = any>(opt: SendMessageOptions<TRequest, TResponse, TError, TSchema>): Promise<TResponse>;
protected sendMessagePromiseHandler<TRequest = any, TResponse = any, TError = any, TSchema = any>({ handleResponseReject, requestMessage, onSuccess, onError, timeout, }: SendMessageOptions<TRequest, TResponse, TError, TSchema> & {
handleResponseReject: (isError: boolean, response: RequestResponse<TRequest, TResponse, TError>, error?: RequestMaybeNoError<any, TRequest>) => void;
protected sendMessagePromiseHandler<TRequest = any, TResponse = any, TError = any, TSchema = any>({ handleError, handleSuccess, requestMessage, requestOptions, }: SendMessageOptions<TRequest, TResponse, TError, TSchema> & {
handleError: (cancelled: boolean, response: RequestResponse<TRequest, TResponse, TError>, error?: RequestMaybeNoError<any, TRequest>) => void;
handleSuccess: (cancelled: boolean, response: RequestResponse<TRequest, TResponse, TError>) => void;
}): void;
private handleCancelOptions;
private handleCancelResponse;
subscribeEvent<TResponse = any>({ name, onEvent, }: {

@@ -45,7 +49,11 @@ name: string | string[];

}): () => void;
sendCommand<TRequest = any, TResponse = any, TSchema = any>(opt: RequestOptionsTracked<TRequest, TResponse>): Promise<TResponse>;
sendCommandTracked<TRequest = any, TResponse = any, TSchema = any>(opt: RequestOptionsTracked<TRequest, TResponse>): Promise<RequestResponse<TRequest, TResponse, TSchema>>;
sendQuery<TRequest = any, TResponse = any, TError = any, TSchema = any>(opt: RequestOptionsTracked<TRequest, TResponse>): Promise<TResponse>;
sendQueryTracked<TRequest = any, TResponse = any, TError = any, TSchema = any>(opt: RequestOptionsTracked<TRequest, TResponse, TError>): Promise<RequestResponse<TRequest, TResponse, TError>>;
sendEvent<TPayload = any>(top: OmitAndOptional<Message<TPayload>, "trackId" | "created" | "isError" | "type", "direction">): Message<undefined, any>;
private createTrackedMessage;
createCommand<TRequest = any, TResponse = any, TError = any, TSchema = any>(requestOptions: RequestOptions<TRequest, TResponse>): CreatedMessage<TRequest, TResponse, TError, TSchema>;
createQuery<TRequest = any, TResponse = any, TError = any, TSchema = any>(requestOptions: RequestOptions<TRequest, TResponse>): CreatedMessage<TRequest, TResponse, TError, TSchema>;
sendCommand<TRequest = any, TResponse = any, TError = any, TSchema = any>(requestOptions: RequestOptions<TRequest, TResponse>): Promise<TResponse>;
sendCommandTracked<TRequest = any, TResponse = any, TError = any, TSchema = any>(requestOptions: RequestOptions<TRequest, TResponse>): Promise<RequestResponse<TRequest, TResponse, TError>>;
sendQuery<TRequest = any, TResponse = any, TError = any, TSchema = any>(requestOptions: RequestOptions<TRequest, TResponse>): Promise<TResponse>;
sendQueryTracked<TRequest = any, TResponse = any, TError = any, TSchema = any>(requestOptions: RequestOptions<TRequest, TResponse, TError>): Promise<RequestResponse<TRequest, TResponse, TError>>;
createEvent<TPayload = any>(eventOptions: EventOptions<TPayload>): CreatedEvent<TPayload>;
sendEvent<TPayload = any>(eventOptions: OmitAndOptional<Message<TPayload>, "trackId" | "created" | "isError" | "type", "direction">): void;
protected onMessage(messageString: string | Message): void;

@@ -52,0 +60,0 @@ protected internalSendMessage(msg: Message): void;

@@ -7,9 +7,18 @@ export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;

onError?: (err?: unknown, eventOrData?: unknown) => void;
onSuccess?: (msg: RequestResponse) => void;
onClose?: (err?: unknown, eventOrData?: unknown) => void;
onConnect?: () => void;
onCancel?: (msg: Message) => void;
interceptSendMessage?: (msg: Message) => Message;
interceptReceivedMessage?: (msg: Message) => Message;
interceptCreatedMessageOptions?: (msg: CreatedMessage) => CreatedMessage;
interceptCreatedEventMessageOptions?: (msg: CreatedEvent) => CreatedEvent;
avoidThrowOnNonTrackedError?: boolean;
throwOnTrackedError?: boolean;
timeout?: number;
resolveCancelledNonTrackedRequest?: boolean;
sendCancelledRequest?: boolean;
callOnErrorWhenRequestIsCancelled?: boolean;
callOnSuccessWhenRequestIsCancelled?: boolean;
allowResponseValueWhenCancelled?: boolean;
timeoutFromBridgeOptionsMessage?: (ms: number) => string;

@@ -46,2 +55,4 @@ timeoutFromRequestOptionsMessage?: (ms: number) => string;

isError: boolean;
cancelled?: boolean;
timedOut?: boolean;
trackId: string;

@@ -53,3 +64,3 @@ created: string;

};
export type RequestResponse<TRequest, TResponse, TError = any> = {
export type RequestResponse<TRequest = any, TResponse = any, TError = any> = {
response: TResponse;

@@ -59,34 +70,50 @@ request: TRequest;

requestMessage: Message<TRequest>;
requestOptions: RequestOptions<TRequest, TResponse, TError>;
isError?: boolean;
error?: TError;
errorMessage?: Message<TError>;
cancelled?: boolean;
timedOut?: boolean;
};
export type RequestOptionsTracked<TRequest, TResponse, TError = any> = {
export type RequestOptions<TRequest, TResponse, TError = any> = {
name: string;
payload: TRequest;
onSuccess?: SubscribeResponseTracked<TRequest, TResponse>;
onError?: SubscribeErrorResponseTracked<TRequest, TResponse, TError>;
onSuccess?: TrackedOnSuccess<TRequest, TResponse>;
onError?: TrackedOnError<TRequest, TResponse, TError>;
module?: string;
timeout?: number;
resolveCancelledForNonTracked?: boolean;
sendCancelled?: boolean;
callOnErrorWhenRequestIsCancelled?: boolean;
callOnSuccessWhenRequestIsCancelled?: boolean;
allowResponseValueWhenCancelled?: boolean;
};
export type SendMessageOptions<TRequest = any, TResponse = any, TError = any, TSchema = any> = {
requestMessage: Message<TRequest, TSchema>;
onSuccess?: SubscribeResponseTracked<TRequest, TResponse>;
onError?: SubscribeErrorResponseTracked<TRequest, TResponse, TError>;
timeout?: number;
requestOptions: RequestOptions<TRequest, TResponse, TError>;
};
export type SubscribeResponseAsync<TRequest, TResponse> = (opt: RequestResponse<TRequest, TResponse>) => Promise<RequestResponse<TRequest, TResponse>>;
export type SubscribeResponseTracked<TRequest, TResponse> = (opt: RequestResponse<TRequest, TResponse>) => void;
export type SubscribeResponseWithCallbacks<TRequest, TResponse, TError = any> = {
onSuccess?: SubscribeResponseAsync<TRequest, TResponse>;
onError?: SubscribeErrorAsync<TError, TRequest>;
requestMessage: Message<TRequest>;
export type CreatedMessage<TRequest = any, TResponse = any, TError = any, TSchema = any> = {
trackId: string;
requestMessage: Message<TRequest, TSchema>;
requestOptions: RequestOptions<TRequest, TResponse, TError>;
send: () => Promise<TResponse>;
sendTracked: () => Promise<RequestResponse<TRequest, TResponse, TError>>;
cancel: () => void;
};
export type CreatedEvent<TPayload = any> = {
trackId: string;
requestMessage: Message<TPayload>;
requestOptions: EventOptions<TPayload>;
send: () => void;
cancel: () => void;
};
export type EventOptions<TPayload = any> = {
name: string;
payload: TPayload;
module?: string;
sendCancelled?: boolean;
};
export type TrackedOnSuccess<TRequest, TResponse> = (opt: RequestResponse<TRequest, TResponse>) => void;
export type TrackedOnError<TRequest = any, TResponse = any, TError = any> = (opt: RequestResponse<TRequest, TResponse, TError>) => void;
export type SubscribeEvent<TResponse> = (payload: TResponse, eventMessage: Message<TResponse>) => void;
export type RequestError<TError = any, TRequest = any> = {
reason?: TError;
request: TRequest;
responseMessage?: Message<TError>;
requestMessage: Message<TRequest>;
};
export type RequestMaybeNoError<TError = any, TRequest = any> = {

@@ -98,12 +125,11 @@ reason?: TError;

};
export type SubscribeErrorAsync<TError = any, TRequest = any> = (opt: RequestMaybeNoError<TError, TRequest>) => Promise<RequestResponse<TError, TRequest>>;
export type SubscribeError<TError = any, TRequest = any> = (opt: RequestMaybeNoError<TError, TRequest>) => void;
export type SubscribeErrorResponseTracked<TRequest = any, TResponse = any, TError = any> = (opt: RequestResponse<TRequest, TResponse, TError>) => void;
export type InternalTrackedSubscribeResponse<TResponse> = (responseMessage: Message<TResponse>) => void;
export type InternalTrackedSubscribeError<TError> = (responseMessage: Message<TError> | undefined) => void;
export type InternalTrackedSubscribeResponseWithCatch<TRequest, TResponse, TError = any> = {
successTrack?: InternalTrackedSubscribeResponse<TResponse>;
errorTrack?: InternalTrackedSubscribeError<TError>;
export type OnTimeoutHandler<TError = any, TRequest = any> = (opt: RequestMaybeNoError<TError, TRequest>) => void;
export type InternalTrackedOnSuccess<TResponse> = (responseMessage: Message<TResponse>) => void;
export type InternalTrackedOnError<TError> = (responseMessage: Message<TError> | undefined) => void;
export type InternalTrackedRequest<TRequest, TResponse, TError = any> = {
successTrack: InternalTrackedOnSuccess<TResponse>;
errorTrack: InternalTrackedOnError<TError>;
requestMessage: Message<TRequest>;
requestOptions: RequestOptions<TRequest, TResponse, TError>;
};
//# sourceMappingURL=MessageBridgeTypes.d.ts.map

@@ -14,4 +14,6 @@ import { Message, MessageType } from "../MessageBridgeTypes";

event: (name: string, payload: any) => void;
error: (reason: any) => void;
error: (reason: any, cancelled?: boolean, timedOut?: boolean) => void;
response: (response: TResponse) => void;
sendResponseMessage: (responseMessage: Message<TRequest, TResponse>) => void;
createResponseMessage(response: TResponse): Message<TRequest, TResponse>;
}) => void;

@@ -40,4 +42,15 @@ export type RequestEventHandler<TRequest = any, TResponse = any, TStore = any> = (opt: {

connect(sendMessage: (msg: Message) => void): void;
sendError(payload: RequestErrorResponse, trackId?: string): void;
sendResponse(type: MessageType, name: string, payload: any, trackId: string): void;
sendError(payload: RequestErrorResponse, trackId?: string, cancelled?: boolean, timedOut?: boolean): void;
createMessage(opt: {
type: MessageType;
name: string;
payload: any;
trackId: string;
}): Message<any, any>;
sendResponse(opt: {
type: MessageType;
name: string;
payload: any;
trackId: string;
}): void;
sendEvent(name: string, payload: any): void;

@@ -44,0 +57,0 @@ onMessage(requestMessage: Message | string): void;

{
"name": "message-bridge-js",
"version": "0.9.0",
"version": "0.9.1",
"homepage": "https://github.com/alfnielsen/MessageBridgeJS",

@@ -29,2 +29,3 @@ "repository": {

"test:intercept": "yarn jest -i tests/intercept.test.ts --runInBand --detectOpenHandles",
"test:cancel": "yarn jest -i tests/cancel.test.ts --runInBand --detectOpenHandles",
"build": "microbundle --format umd,cjs,es",

@@ -31,0 +32,0 @@ "publish": "npm publish"

@@ -18,2 +18,4 @@ # Message Bridge (JS)

[Demo (CodeSandbox):](https://codesandbox.io/s/message-brigde-js-u7bzxi?file=/src/App.tsx)
```ts

@@ -63,2 +65,28 @@ // See "/tests" in repository for more examples

Advanced tracked requests:
```ts
const query = bridge.createQuery({
name: "GetTotoItem",
payload: { id: 25 },
})
const { response, request, requestMessage } = await query.sendTracked()
// Or
const response = await query.send()
// Advanced tracked:
const {
trackId, // string (bridge message's track id)
requestMessage, // Message<TRequest, TSchema>
requestOptions, // RequestOptions<TRequest, TResponse, TError>
send, // () => Promise<TResponse>
sendTracked, // () => Promise<RequestResponse<TRequest, TResponse, TError>>
cancel, // () => void
} = bridge.createQuery({
name: "GetTotoItem",
payload: { id: 25 },
})
```
Multiple parallel requests:

@@ -176,3 +204,3 @@

The backend handles the Command/Query/Event and create a response message.
The sent the response message back to the frontend including correct **type** and the **trackId**
Then sent the response message back to the frontend including correct **type** and the **trackId**

@@ -197,2 +225,8 @@ The server can also send events to the frontend. _(without any prior request)_

For tracking (error handling and cancellation) use 'create' and then 'send' or 'sendTracked' later.
- createCommand
- createQuery
- createEvent
Tracked versions of requests (They resolve the promise with a full RequestResponse<TRequest,TResponse>)

@@ -204,9 +238,2 @@ It includes the request and response messages (So you can track which request data what used to get the response)

Underlying commands _(can sometime be used to fetch trackId ect...)_
- sendMessage
- sendMessageTracked
- onError _(override to handle errors)_
- onClose _(override to handle close)_
Helper commands _(advanced use)_

@@ -242,3 +269,3 @@

Test Suites: <span style="color: green; ">10 passed</span>, 10 total
Tests: <span style="color: green; ">38 passed</span>, 38 total
Tests: <span style="color: green; ">53 passed</span>, 53 total

@@ -307,2 +334,20 @@ ## Async vs Callback

## Cancellation
You can cancel a tracked request by using the cancel method. (The server can also send _cancelled: true_ in teh response messsage)
```ts
const cmd = await bridge.createCommand({
name: "CreateTodo",
payload: command,
})
cmd.send().then((response) => {
// handle response
})
// or
const { cancelled } = await cmd.sendTracked()
```
## Bridge options

@@ -321,3 +366,2 @@

})
// BridgeOptions defined in MessageBridgeTypes.ts
export type BridgeOptions = {

@@ -328,4 +372,7 @@ // Add listeners:

onError?: (err?: unknown /*Error*/, eventOrData?: unknown) => void
onSuccess?: (msg: RequestResponse) => void
onClose?: (err?: unknown /*Error*/, eventOrData?: unknown) => void
onConnect?: () => void
// Can be used to send a cancel request to the server
onCancel?: (msg: Message) => void
// Interception:

@@ -338,2 +385,5 @@ // - can be used to generalize behavior (Happens as early as possible in the process)

interceptReceivedMessage?: (msg: Message) => Message // (default: undefined)
// Happens after the options for createMessage is applied)
interceptCreatedMessageOptions?: (msg: CreatedMessage) => CreatedMessage // (default: undefined)
interceptCreatedEventMessageOptions?: (msg: CreatedEvent) => CreatedEvent // (default: undefined)
// Handle errors and timeouts:

@@ -343,2 +393,10 @@ avoidThrowOnNonTrackedError?: boolean // (default: undefined)

timeout?: number // (default: undefined)
// Cancel
// resolve on cancel (Let the process that did the request handle the cancel)
resolveCancelledNonTrackedRequest?: boolean // (default: undefined)
sendCancelledRequest?: boolean // (default: undefined)
callOnErrorOnCancelledRequest?: boolean // (default: undefined)
callOnSuccessOnCancelledRequest?: boolean // (default: undefined)
// if true, the response can still have a value, else it will be undefined
allowResponseOnCancelledTrackedRequest?: boolean // (default: undefined)
// Debugging options:

@@ -368,3 +426,12 @@ timeoutFromBridgeOptionsMessage?: (ms: number) => string // (has default implementation)

- timeout // set timeout for this request (overrides bridge timeout\*)
- module // info that the server can use
// advanced options:
- resolveCancelledForNonTracked?: boolean
- sendCancelled?: boolean
- callOnErrorOnCancelledRequest?: boolean
- callOnSuccessOnCancelledRequest?: boolean
- allowResponseOnCancelled?: boolean
\*_The bridge options has NO timeout as default_

@@ -491,8 +558,10 @@

const [todo, setTodo] = useState<TodoItem | undefined>()
useEffect(async () => {
const todos = await bridge.sendQuery<GetTodoItemQuery, GetTodoItemQueryResponse>({
name: RequestType.GetTodoItemQuery,
payload: { id },
})
setTodo(todos?.[0])
useEffect(() => {
const fetchTodoes = async () => {
const todos = await bridge.sendQuery<GetTodoItemQuery, GetTodoItemQueryResponse>({
name: RequestType.GetTodoItemQuery,
payload: { id },
})
setTodo(todos?.[0])
}
const unsub = bridge.subscribeEvent<TodoItemUpdatedEvent>({

@@ -499,0 +568,0 @@ name: RequestType.TodoItemUpdated,

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc