import to from 'await-to-js' import logger from '@uci-utils/logger' let log = logger({ package: 'base',file:'processing.js'}) // this._processing refers to this module/hash // processing errors that are caught should be sent back to consumer in packets with :error property // but they might also throw local errors/execptions so they should bubble up here and get caught and logged // messaging errors on socket will not be fatal to the entire socket server const processor = async function (packet,socket) { let [err,res] = await to(_process[this.getSocket(socket).type].bind(this)(packet,socket)) if (err) { let error = {error:err, socket:socket, packet:packet, msg:'some possibly unhandled badness happened during packet processing'} log.warn(error) if (process.env.UCI_SHOW_UNHANDLED==='true') console.log(error) } else return res } export { processor, commands, namespaces } const _process = { s: async function (packet,socket) { // console.log('in default socket processor',packet.cmd) if (!packet.cmd) return {error: '[socket] no command in packet', packet: packet } let response = await this._callCmdFunc(packet,socket); if(response!=='failed') return response return {error: 'no socket processing function supplied for command', packet: packet } }, c: async function (packet,socket) { // console.log('in default consumer processor',packet.cmd) if (packet.error) return await this._defaultCmds.c.error(packet) if (packet.cmd) { let response = await this._callCmdFunc(packet,socket); if(response!=='failed') return response packet = {error:'no consumer processing function supplied for command',packet:packet} this._defaultCmds.c.error(packet) } else { packet = {error:'[consumer] no command in packet',packet:packet} return await this._defaultCmds.c.error(packet) } } } const namespaces = { s: ['s','_defaultCmds.s'], c: ['c','_defaultCmds.c'], cn: ['cn'], ct: ['ct'], sn: ['sn'], st: ['st'], cm: ['cm'], sm: ['sm'], sw: ['sw'], } /* * * Default packet command processing functions * */ const commands ={ s:{ echo: async packet => { packet.processed = true packet.info = 'default socket echo' return packet }, ack: async packet => { packet.cmd = 'reply' packet.ack = true return packet } }, c:{ error: function (packet) { // TODO log and make this show only on env debug log.warn({error:packet.error, packet:packet, msg:'==========Packet ERROR [consumer]========='}) }, reply: function(packet) { // TODO log and make this show only on env debug log.debug({packet:packet, msg:'====Packet returned from socket - debug reply==='}) } } }