From ef3064bbdd99a563576d237ea852a666dcde3294 Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Wed, 15 Dec 2021 15:47:10 -0400 Subject: [PATCH] Deserialize buffers in/out of subprocess --- utils/ECC/subprocess.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/utils/ECC/subprocess.js b/utils/ECC/subprocess.js index 18a9d58a..987ee279 100644 --- a/utils/ECC/subprocess.js +++ b/utils/ECC/subprocess.js @@ -5,6 +5,7 @@ const Crypto = require('crypto') const ECCrypto = require('eccrypto') const uuid = require('uuid/v1') const { Buffer } = require('buffer') +const mapValues = require('lodash/mapValues') const logger = require('../../config/log') @@ -27,6 +28,20 @@ process.on('unhandledRejection', e => { * } Method */ +/** + * @param {any} obj + * @returns {any} + */ +const processBufferAfterSerialization = obj => { + if (typeof obj === 'object' && obj !== null) { + if (obj.type === 'Buffer') { + return Buffer.from(obj.data) + } + return mapValues(obj, processBufferAfterSerialization) + } + return obj +} + /** * @typedef {object} Msg * @prop {any[]} args @@ -42,7 +57,8 @@ const handleMsg = async msg => { logger.error('Msg in crypto subprocess not an object') } - const { args, id, method } = msg + const { id, method } = msg + const args = msg.args.map(processBufferAfterSerialization) try { if (method === 'generateRandomString') { @@ -113,7 +129,7 @@ const handleMsg = async msg => { // @ts-expect-error process.send({ id, - payload: ECCrypto.encrypt(processedPublicKey, messageBuffer) + payload: await ECCrypto.encrypt(processedPublicKey, messageBuffer) }) } if (method === 'decrypt') { @@ -150,7 +166,7 @@ const invoke = (method, args, cryptoSubprocess) => if (msg.err) { rej(new Error(msg.err)) } else { - res(msg.payload) + res(processBufferAfterSerialization(msg.payload)) } } }