cloned incoming socket side packet immediately before processing and copying over header.

tls
David Kebler 2018-02-14 13:26:35 -08:00
parent 450a85d8b9
commit 5ada8cd43a
3 changed files with 12 additions and 11 deletions

View File

@ -47,6 +47,7 @@
},
"dependencies": {
"better-try-catch": "^0.6.2",
"clone": "^2.1.1",
"death": "^1.1.0"
}
}

View File

@ -1,7 +1,6 @@
import { Socket } from 'net'
import btc from 'better-try-catch'
import JsonStream from './json-stream'
import {promisify} from 'util'
import logger from '../../uci-logger/src/logger'
let log = {}

View File

@ -3,6 +3,7 @@ import { unlink as fileDelete } from 'fs'
import btc from 'better-try-catch'
import _ON_DEATH from 'death' //this is intentionally ugly
import JSONStream from './json-stream'
import clone from 'clone'
import logger from '../../uci-logger/src/logger'
let log = {}
@ -84,18 +85,18 @@ export default class Socket extends Server {
// TODO need to start error listener for stream so errors can be processed
stream.on('message', messageProcess.bind(this))
async function messageProcess (packet) {
let res = clone(packet)
// console.log(' incoming packet on socket side',packet)
let processed = Object.assign({},await this._packetProcess(packet)) // must make copy in case processed returned passed packet
if (Object.keys(processed).length === 0) processed = { error: 'socket packet command function likely did not return a promise', packet:packet}
processed._header = packet._header //make sure return packet has header in case it was removed in processing
res = clone((await this._packetProcess(res)),false)
if (Object.keys(res).length === 0) res = { error: 'socket packet command function likely did not return a promise', packet:packet}
res._header = clone(packet._header,false) //make sure return packet has header with id in case it was removed in processing
delete packet._header // remove before adding to response header as request
processed._header.request = packet
processed._header.responder = {name:this.name,instanceID:this.id}
processed._header.socket = this._connectionKey
if (!processed.cmd) processed.cmd = 'reply' // by default return command is 'reply'
// console.log('after processing',processed)
let [err, ser] = await btc(stream.serialize)(processed)
if (err) ser = await stream.serialize({ error: 'was not able to serialze the processed packet', err:err, _header:{id:processed._header.id}})
res._header.request = clone(packet,false)
res._header.responder = {name:this.name,instanceID:this.id}
res._header.socket = this._connectionKey
if (!res.cmd) res.cmd = 'reply' // by default return command is 'reply'
let [err, ser] = await btc(stream.serialize)(res)
if (err) ser = await stream.serialize({ error: 'was not able to serialze the res packet', err:err, _header:{id:res._header.id}})
// console.log('serialized ready for write',ser)
log.info(await write(ser))
}