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