load-cafile.js 682 B

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