1module.exports = loadCAFile 2 3var fs = require('fs') 4 5function loadCAFile (cafilePath, cb) { 6 if (!cafilePath) return process.nextTick(cb) 7 8 fs.readFile(cafilePath, 'utf8', afterCARead.bind(this)) 9 10 function afterCARead (er, cadata) { 11 if (er) { 12 // previous cafile no longer exists, so just continue on gracefully 13 if (er.code === 'ENOENT') return cb() 14 return cb(er) 15 } 16 17 var delim = '-----END CERTIFICATE-----' 18 var output 19 20 output = cadata 21 .split(delim) 22 .filter(function (xs) { 23 return !!xs.trim() 24 }) 25 .map(function (xs) { 26 return xs.trimLeft() + delim 27 }) 28 29 this.set('ca', output) 30 cb(null) 31 } 32} 33