Deserialize buffers in/out of subprocess
This commit is contained in:
parent
1342fb5b3f
commit
ef3064bbdd
1 changed files with 19 additions and 3 deletions
|
|
@ -5,6 +5,7 @@ const Crypto = require('crypto')
|
||||||
const ECCrypto = require('eccrypto')
|
const ECCrypto = require('eccrypto')
|
||||||
const uuid = require('uuid/v1')
|
const uuid = require('uuid/v1')
|
||||||
const { Buffer } = require('buffer')
|
const { Buffer } = require('buffer')
|
||||||
|
const mapValues = require('lodash/mapValues')
|
||||||
|
|
||||||
const logger = require('../../config/log')
|
const logger = require('../../config/log')
|
||||||
|
|
||||||
|
|
@ -27,6 +28,20 @@ process.on('unhandledRejection', e => {
|
||||||
* } Method
|
* } 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
|
* @typedef {object} Msg
|
||||||
* @prop {any[]} args
|
* @prop {any[]} args
|
||||||
|
|
@ -42,7 +57,8 @@ const handleMsg = async msg => {
|
||||||
logger.error('Msg in crypto subprocess not an object')
|
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 {
|
try {
|
||||||
if (method === 'generateRandomString') {
|
if (method === 'generateRandomString') {
|
||||||
|
|
@ -113,7 +129,7 @@ const handleMsg = async msg => {
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
process.send({
|
process.send({
|
||||||
id,
|
id,
|
||||||
payload: ECCrypto.encrypt(processedPublicKey, messageBuffer)
|
payload: await ECCrypto.encrypt(processedPublicKey, messageBuffer)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if (method === 'decrypt') {
|
if (method === 'decrypt') {
|
||||||
|
|
@ -150,7 +166,7 @@ const invoke = (method, args, cryptoSubprocess) =>
|
||||||
if (msg.err) {
|
if (msg.err) {
|
||||||
rej(new Error(msg.err))
|
rej(new Error(msg.err))
|
||||||
} else {
|
} else {
|
||||||
res(msg.payload)
|
res(processBufferAfterSerialization(msg.payload))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue