var needed = 2
var code = -1
var finished = false
var done = function (err) {
if (finished) {
return
}
if (err) {
finished = true
return callback(err)
}
if (--needed < 1) {
finished = true
if (code) {
if (code === 2 && (stderr === '' || /depth lookup: unable to/.test(stderr))) {
return callback(null, code, stdout, stderr)
}
return callback(new Error('Invalid openssl exit code: ' + code + '\n% openssl ' + params.join(' ') + '\n' + stderr), code)
} else {
return callback(null, code, stdout, stderr)
}
}
}
openssl.on('error', done)
openssl.on('exit', function (ret) {
code = ret
done()
})
openssl.on('close', function () {
stdout = (binary ? stdout : Buffer.from(stdout, 'binary').toString('utf-8'))
stderr = Buffer.from(stderr, 'binary').toString('utf-8')
done()
})
})
}
function spawnWrapper (params, tmpfiles, binary, callback) {
if (!callback && typeof binary === 'function') {
callback = binary
binary = false
}
var files = []
var delTempPWFiles = []
if (tmpfiles) {
tmpfiles = [].concat(tmpfiles)
var fpath, i
for (i = 0; i < params.length; i++) {
if (params[i] === '--TMPFILE--') {
fpath = pathlib.join(tempDir, crypto.randomBytes(20).toString('hex'))
files.push({
path: fpath,
contents: tmpfiles.shift()
})
params[i] = fpath
delTempPWFiles.push(fpath)
}
}
}
var file
for (i = 0; i < files.length; i++) {
file = files[i]
fs.writeFileSync(file.path, file.contents)
}
spawn(params, binary, function (err, code, stdout, stderr) {
helper.deleteTempFiles(delTempPWFiles, function (fsErr) {
callback(err || fsErr, code, stdout, stderr)
})
})
}
function testOpenSSLPath (pathBin, callback) {
which(pathBin, function (error) {
if (error) {
return callback(new Error('Could not find openssl on your system on this path: ' + pathBin))
}
callback()
})
}
spawn(['version'], false, function (err, code, stdout, stderr) {
var text = String(stdout) + '\n' + String(stderr) + '\n' + String(err)
var tmp = text.match(/^LibreSSL/i)
set('openSslVersion', (tmp && tmp[0] ? 'LibreSSL' : 'openssl').toUpperCase())
})
module.exports = {
exec: exec,
execBinary: execBinary,
spawn: spawn,
spawnWrapper: spawnWrapper,
set: set,
get: get
}