Dealing with what should be a simple problem, but I've been stuck on it for awhile. Might have to do with string encodings, or pipes, or something else entirely - not sure.
Here's some code that shows the problem - it's a child spawn calling a python script:
const spawn = require('child_process').spawn;
let py = spawn('python', ['../py-docker-encryption/handle_encrypt.py']);
let encMsg = {action: 'enc', data: []}
encMsg.data.push(allArr[0].items.S)
let dataString;
py.stdout.on('data', (data) => {
dataString += data.toString();
})
py.stdout.on('end', () => {
console.log(dataString)
})
py.stdin.write(JSON.stringify(encMsg));
py.stdin.end()
// py.on('close', (code) => {
// cb(null, dataString)
// })
The python code is complex, but I've tested it thoroughly: it's an encryption script, which returns the ciphertext to stdout. But that doesn't matter. Bottom line, the stdout in the .on('data') pipe returns normally, but by the time the script closes, or .on('end') is called, a single "undefined" is tacked to the beginning of the dataString string. For example, this is my console (all that matters is the undefined at the start - the rest is normal operations):
undefined[{"data": "AYADeJldNsBlMPApbYJydOfQ5msAXwABABVhd3MtY3J5cHRvLXB1YmxpYy1r
ZXkAREF3aUNDRWRHOTlUUHNYMFlwWVZLVnBPaHFxMDhiQ0NXOUkyWUVocEdTMWV4TkpjV0VxRnlFZ0xa
dkpIOVVmZEM1QT09AAEAB2F3cy1rbXMATmFybjphd3M6a21zOmV1LWNlbnRyYWwtMTo5MDUwNDk5MjMx
NjI6a2V5L2Q4NTNhNzdhLTJmMmMtNDRkNy04ZmNjLTE3MzNmZmVjYmM5NwC4AQIBAHg4n+ZTthRASUgK
QrDeQL96fA+8KdXwWlK3rIBH8nfwGQEln5SRtpBSM1tkyjxWDfoLAAAAfjB8BgkqhkiG9w0BBwagbzBt
AgEAMGgGCSqGSIb3DQEHATAeBglghkgBZQMEAS4wEQQMv+tUKJb0bdkvMe8FAgEQgDuy7Vx1nDBCUUGS
+GmG5gl7VcFP1e7t0BcSZ3KYeOgZDdZsH3iMXajtPktejPYzmBxFbxigN0ZQLXti2wIAAAAADAAAEAAA
AAAAAAAAAAAAAADhoAGEpZmeu/1Y+eOqL8OX/////wAAAAEAAAAAAAAAAAAAAAEAAAAEZ0I1VTfw2cHO
wx7ejXvVx9+vZjsAZzBlAjAw7KDk/iwWADqUfKmjyjKGrEab/bTUXu59A5xA0Db/L5JgCnhqlw3n8MTW
haVlqmwCMQDKSmZeKXJn0tvDYIYlVY20VwD+HRBTji/P62cREOE89iPbjLOykxeQJyqB3K7eGlA=\n"}
]
Any ideas on this? My use case is more complicated than this, and I can't see myself just parsing the undefined's out - seems really hacky.
Thanks much!