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AI,EAAI,EAAG,EAAI,EAAK,SAAS,OAAQ,IACxC,EAAQ,KAAO,EAAK,SAAS,GAC7B,KAAK,KAAK,eAAgB,EAAK,aAAc,KAAK,UAAU,GAEhE,EAEA,KAAK,YAAc,SAAU,GAEvB,KAAK,eAAe,EAAK,eAAiB,KAAK,eAAe,EAAK,cAAc,QAC9E,KAAK,SAAS,EAAK,WACtB,KAAK,SAAS,EAAK,SAAW,CAC5B,MAAO,IAIX,KAAK,SAAS,EAAK,SAAS,EAAK,eAAgB,EACjD,KAAK,SAAS,EAAK,SAAS,QAC5B,KAAK,eAAe,EAAK,cAAc,SAAS,KAAK,EAAK,SAE9D,EAEA,KAAK,aAAe,SAAU,GAC5B,IAAI,EAEA,KAAK,eAAe,EAAK,eAAiB,KAAK,eAAe,EAAK,cAAc,OAAS,KAAK,SAAS,EAAK,UAAY,KAAK,SAAS,EAAK,SAAS,EAAK,gBACxJ,KAAK,SAAS,EAAK,SAAS,MAAQ,UAC/B,KAAK,SAAS,EAAK,SAAS,EAAK,cACxC,KAAK,SAAS,EAAK,SAAS,gBAGrB,KAAK,SAAS,EAAK,UAKb,KAFf,EAAQ,KAAK,eAAe,EAAK,cAAc,SAAS,QAAQ,EAAK,WAGnE,KAAK,eAAe,EAAK,cAAc,SAAS,OAAO,EAAO,GAGpE,EAEA,KAAK,WAAa,SAAU,GACtB,KAAK,eAAe,IACtB,KAAK,eAAe,GAAU,OAAO,MAAM,kBAAmB,mCAElE,CACF,CAEA,SAAS,KAAK,EAAM,EAAI,GAClB,EAAK,IAAO,EAAK,GAAI,OAAS,EAAK,GAAI,OAAO,aAAe,EAAK,GAAI,OAAO,MAC/E,EAAK,GAAI,OAAO,KAAK,EAEzB,CAEA,OAAO,QAAU","file":"/npm/kuzzle-plugin-websocket@1.0.10/lib/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/kuzzle-plugin-websocket@1.0.10/lib/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","'use strict';\n\nconst\n  WebSocketServer = require('ws').Server;\n\n/**\n * @constructor\n */\nfunction WebsocketProtocol () {\n  this.config = {};\n  this.protocol = 'websocket';\n  this.context = null;\n  this.channels = {};\n  this.connectionPool = {};\n  this.server = {};\n\n  this.init = function (config, context) {\n    this.config = Object.assign({\n      port: 7513\n    }, config || {});\n\n    this.context = context;\n\n    this.server = new WebSocketServer({port: this.config.port}, {perMessageDeflate: false});\n\n    this.server.on('connection', this.onConnection.bind(this));\n    this.server.on('error', this.onServerError.bind(this));\n\n    return this;\n  };\n\n  this.onServerError = function (error) {\n    this.context.log.error('[plugin-websocket] An error has occured:\\n' + error.stack);\n  };\n\n  this.onConnection = function (clientSocket) {\n    let ips = [clientSocket.upgradeReq.connection.remoteAddress];\n    if (clientSocket.upgradeReq.headers['x-forwarded-for']) {\n      ips = clientSocket.upgradeReq.headers['x-forwarded-for']\n        .split(',')\n        .map(s => s.trim())\n        .concat(ips);\n    }\n\n    const connection = new this.context.constructors.ClientConnection(this.protocol, ips, clientSocket.upgradeReq.headers);\n\n    try {\n      this.context.accessors.router.newConnection(connection);\n    }\n    catch (err) {\n      // Using the 4xxx code range, as the current implementation of\n      // the \"ws\" library does not support the 1013 (Try Again Later)\n      // error event\n      clientSocket.close(4000 + err.status, err.message);\n      this.context.log.error('[plugin-websocket] Unable to register connection to the proxy\\n%s', err.stack);\n      return;\n    }\n\n    this.connectionPool[connection.id] = {\n      alive: true,\n      socket: clientSocket,\n      channels: []\n    };\n\n    clientSocket.on('close', () => {\n      this.onClientDisconnection(connection.id);\n    });\n\n    clientSocket.on('error', () => {\n      this.onClientDisconnection(connection.id);\n    });\n\n    clientSocket.on('message', data => {\n      this.onClientMessage(connection.id, data);\n    });\n  };\n\n  this.onClientDisconnection = function (clientId) {\n    if (this.connectionPool[clientId]) {\n      this.connectionPool[clientId].alive = false;\n      this.context.accessors.router.removeConnection(clientId);\n\n      this.connectionPool[clientId].channels.forEach(channel => {\n        if (this.channels[channel] && this.channels[channel].count > 1) {\n          delete this.channels[channel][clientId];\n          this.channels[channel].count--;\n        }\n        else {\n          delete this.channels[channel];\n        }\n      });\n\n      delete this.connectionPool[clientId];\n    }\n  };\n\n  this.onClientMessage = function (clientId, data) {\n    if (data && this.connectionPool[clientId]) {\n      let request;\n\n      try {\n        request = new this.context.constructors.Request(JSON.parse(data), {\n          connectionId: clientId,\n          protocol: this.protocol\n        });\n      }\n      catch (e) {\n        send(this.connectionPool, clientId, JSON.stringify(new this.context.errors.BadRequestError(e.message)));\n        return;\n      }\n\n      this.context.accessors.router.execute(request, response => {\n        if (response.content && typeof response.content === 'object') {\n          response.content.room = response.requestId;\n        }\n        send(this.connectionPool, clientId, JSON.stringify(response.content));\n      });\n    }\n  };\n\n  this.broadcast = function (data) {\n    /*\n     Avoids stringifying the payload multiple times just to update the room:\n      - we start deleting the last character, which is the closing JSON bracket ('}')\n      - we then only have to inject the following string to each channel:\n        ,\"room\":\"<roomID>\"}\n\n      So, instead of stringifying the payload for each channel, we only concat\n      a new substring to the original payload.\n     */\n    const payload = JSON.stringify(data.payload).slice(0, -1) + ',\"room\":\"';\n\n    data.channels.forEach(channel => {\n      if (this.channels[channel]) {\n        let channelPayload = payload + channel + '\"}';\n\n        Object.keys(this.channels[channel]).forEach(connectionId => {\n          send(this.connectionPool, connectionId, channelPayload);\n        });\n      }\n    });\n  };\n\n  this.notify = function (data) {\n    let payload;\n\n    payload = data.payload;\n\n    for (let i = 0; i < data.channels.length; i++) {\n      payload.room = data.channels[i];\n      send(this.connectionPool, data.connectionId, JSON.stringify(payload));\n    }\n  };\n\n  this.joinChannel = function (data) {\n\n    if (this.connectionPool[data.connectionId] && this.connectionPool[data.connectionId].alive) {\n      if (!this.channels[data.channel]) {\n        this.channels[data.channel] = {\n          count: 0\n        };\n      }\n\n      this.channels[data.channel][data.connectionId] = true;\n      this.channels[data.channel].count++;\n      this.connectionPool[data.connectionId].channels.push(data.channel);\n    }\n  };\n\n  this.leaveChannel = function (data) {\n    var index;\n\n    if (this.connectionPool[data.connectionId] && this.connectionPool[data.connectionId].alive && this.channels[data.channel] && this.channels[data.channel][data.connectionId]) {\n      if (this.channels[data.channel].count > 1) {\n        delete this.channels[data.channel][data.connectionId];\n        this.channels[data.channel].count--;\n      }\n      else {\n        delete this.channels[data.channel];\n      }\n\n      index = this.connectionPool[data.connectionId].channels.indexOf(data.channel);\n\n      if (index !== -1) {\n        this.connectionPool[data.connectionId].channels.splice(index, 1);\n      }\n    }\n  };\n\n  this.disconnect = function (clientId) {\n    if (this.connectionPool[clientId]) {\n      this.connectionPool[clientId].socket.close('CLOSEDONREQUEST', 'Connection closed by remote host');\n    }\n  };\n}\n\nfunction send(pool, id, data) {\n  if (pool[id] && pool[id].alive && pool[id].socket.readyState === pool[id].socket.OPEN) {\n    pool[id].socket.send(data);\n  }\n}\n\nmodule.exports = WebsocketProtocol;\n"]}