From 5ada8cd43a35f60058854426fa8f06c83cc6cc01 Mon Sep 17 00:00:00 2001 From: David Kebler Date: Wed, 14 Feb 2018 13:26:35 -0800 Subject: [PATCH] cloned incoming socket side packet immediately before processing and copying over header. --- package.json | 1 + src/consumer.mjs | 1 - src/socket.mjs | 21 +++++++++++---------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index c49fde2..bec2c5a 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ }, "dependencies": { "better-try-catch": "^0.6.2", + "clone": "^2.1.1", "death": "^1.1.0" } } diff --git a/src/consumer.mjs b/src/consumer.mjs index 5265d53..74b2c70 100644 --- a/src/consumer.mjs +++ b/src/consumer.mjs @@ -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 = {} diff --git a/src/socket.mjs b/src/socket.mjs index 9d8f2e3..c5d37e1 100644 --- a/src/socket.mjs +++ b/src/socket.mjs @@ -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)) }