Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add certs support #215

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 56 additions & 16 deletions lib/main.js
Expand Up @@ -10,29 +10,69 @@ var fs = require('fs')
function parse (src) {
var obj = {}

// certs
var storeCerts = []
var currentCert = ''
var currentCertKey = ''
var matchingCert = false

// convert Buffers before splitting into lines and processing
src.toString().split('\n').forEach(function (line) {
// line length
var lineLen = line ? line.length : 0

// matching "KEY' and 'VAL' in 'KEY=VAL'
var keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
// matched?
if (keyValueArr != null) {
var key = keyValueArr[1]

// default undefined or missing values to empty string
var value = keyValueArr[2] ? keyValueArr[2] : ''

// expand newlines in quoted values
var len = value ? value.length : 0
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
value = value.replace(/\\n/gm, '\n')
// check if init cert declaration
if (/-----BEGIN/g.test(line)) {
matchingCert = true
}
//
if (matchingCert) {
// check if end cert declaration
if (/-----END/g.test(line) && lineLen > 0 && line.substring(lineLen - 5, lineLen) === '-----') {
currentCert += line + '\n'
// strore
storeCerts.push({
key: currentCertKey,
value: currentCert
})
currentCert = ''
currentCertKey = ''
matchingCert = false
} else {
// if unmatched?
if (keyValueArr === null) {
currentCert += line + '\n'
} else {
currentCertKey = keyValueArr[1]
let value = keyValueArr[2] ? keyValueArr[2] : ''
currentCert += value + '\n'
}
}
} else {
// matched?
if (keyValueArr != null) {
var key = keyValueArr[1]
// default undefined or missing values to empty string
var value = keyValueArr[2] ? keyValueArr[2] : ''
// expand newlines in quoted values
var len = value ? value.length : 0
if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
value = value.replace(/\\n/gm, '\n')
}
// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()
obj[key] = value
}

// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()

obj[key] = value
}
})
// if stored certs
if (storeCerts.length > 0) {
storeCerts.forEach((item, i, array) => {
obj[item.key] = item.value
})
}

return obj
}
Expand Down