All files / src/lib message.ts

0% Statements 0/52
0% Branches 0/16
0% Functions 0/30
0% Lines 0/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import EventEmitter from 'events'
import GleeQuoreConnection from './connection.js'
import { OperationInterface } from '@asyncapi/parser'
 
type MessageHeaders = { [key: string]: any }
type QueryParam = { [key: string]: string } | { [key: string]: string[] }
 
export interface IGleeQuoreMessageConstructor {
  payload?: any
  headers?: MessageHeaders
  channel?: string
  operation?: OperationInterface
  request?: GleeQuoreMessage
  serverName?: string
  connection?: GleeQuoreConnection
  broadcast?: boolean
  cluster?: boolean
  query?: QueryParam
}
 
export default class GleeQuoreMessage extends EventEmitter {
  private _payload: any
  private _headers: { [key: string]: string }
  private _channel: string
  private _serverName: string
  private _connection: GleeQuoreConnection
  private _broadcast: boolean
  private _inbound: boolean
  private _request: GleeQuoreMessage
  private _operation: OperationInterface
  private _outbound: boolean
  private _cluster: boolean
  private _params: { [key: string]: string }
  private _query: QueryParam
 
  /**
   * Instantiates a new GleeMessage.
   *
   * @param {Object} options
   * @param {Any} [options.payload] Message payload.
   * @param {Object} [options.headers] Message headers.
   * @param {String} [options.channel] Message channel.
   * @param {String} [options.serverName] The name of the associated AsyncAPI server.
   * @param {OperationInterface} [options.operation] The operation that this message belongs to.
   * @param {GleeQuoreMessage} [options.request] If this message is a reply, the parent message that this message is created for as a reply.
   * @param {GleeQuoreConnection} [options.connection] The connection through which the message will be sent or has been received.
   * @param {Boolean} [options.broadcast=false] Whether the message should be broadcasted or not.
   * @param {Boolean} [options.cluster=false] Whether the message is from a cluster adapter or not.
   * @param {Object} [options.query] The query parameters to send or receive query when using the HTTP protocol.
   */
  constructor({
    payload,
    headers,
    channel,
    serverName,
    operation,
    connection,
    request,
    broadcast = false,
    cluster = false,
    query,
  }: IGleeQuoreMessageConstructor) {
    super()
 
    Iif (payload) this._payload = payload
    Iif (headers) this._headers = headers
    Iif (channel) this._channel = channel
    Iif (serverName) this._serverName = serverName
    Iif (connection) this._connection = connection
    Iif (broadcast) this._broadcast = !!broadcast
    Iif (cluster) this._cluster = cluster
    Iif (query) this._query = query
    Iif (request) this._request = request
    Iif (operation) this._operation = operation
  }
 
  get payload(): any {
    return this._payload
  }
 
  set payload(value: any) {
    this._payload = value
  }
 
  hasRequest(): boolean {
    return !!this._request
  }
 
  set request(value: GleeQuoreMessage) {
    this._request = value
  }
 
  get request() {
    return this._request
  }
 
  get operation(): OperationInterface {
    return this._operation
  }
 
  set operation(value: OperationInterface) {
    this._operation = value
  }
 
 
  get headers(): { [key: string]: string } {
    return this._headers
  }
 
  set headers(value: { [key: string]: string }) {
    this._headers = value
  }
 
  get channel(): string {
    return this._channel
  }
 
  set channel(value: string) {
    this._channel = value
  }
 
  get serverName(): string {
    return this._serverName
  }
 
  set serverName(value: string) {
    this._serverName = value
  }
 
  get connection(): GleeQuoreConnection {
    return this._connection
  }
 
  set connection(value: GleeQuoreConnection) {
    this._connection = value
  }
 
  get broadcast(): boolean {
    return this._broadcast
  }
 
  get params(): { [key: string]: string } {
    return this._params
  }
 
  set params(value: { [key: string]: string }) {
    this._params = value
  }
 
  get cluster(): boolean {
    return this._cluster
  }
 
  set cluster(value: boolean) {
    this._cluster = value
  }
 
  get query(): QueryParam {
    return this._query
  }
 
  set query(value: QueryParam) {
    this._query = value
  }
 
  /**
   * Makes the message suitable only for the inbound pipeline.
   */
  setInbound() {
    this._inbound = true
    this._outbound = false
  }
 
  /**
   * Makes the message suitable only for the outbound pipeline.
   */
  setOutbound() {
    this._inbound = false
    this._outbound = true
  }
 
  /**
   * Checks if it's an inbound message.
   */
  isInbound() {
    return this._inbound && !this._outbound
  }
 
  /**
   * Checks if it's an outbound message.
   */
  isOutbound() {
    return this._outbound && !this._inbound
  }
 
  /**
   * Tells Glee to send the message.
   */
  send() {
    this.emit('send', this)
  }
 
  /**
   * Indicates successfully processed the message
   */
  notifySuccessfulProcessing() {
    this.emit('processing:successful')
  }
 
  /**
   * Indicates failure in processing the message
   */
  notifyFailedProcessing() {
    this.emit('processing:failed')
  }
}