node/lib/internal/tls.js
Michaël Zasso 141a6e34ee
lib: enforce use of Array from primordials
PR-URL: https://github.com/nodejs/node/pull/30635
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2019-11-27 19:29:01 +01:00

34 lines
730 B
JavaScript

'use strict';
const {
ArrayIsArray,
ObjectCreate,
} = primordials;
// Example:
// C=US\nST=CA\nL=SF\nO=Joyent\nOU=Node.js\nCN=ca1\nemailAddress=ry@clouds.org
function parseCertString(s) {
const out = ObjectCreate(null);
const parts = s.split('\n');
for (let i = 0, len = parts.length; i < len; i++) {
const sepIndex = parts[i].indexOf('=');
if (sepIndex > 0) {
const key = parts[i].slice(0, sepIndex);
const value = parts[i].slice(sepIndex + 1);
if (key in out) {
if (!ArrayIsArray(out[key])) {
out[key] = [out[key]];
}
out[key].push(value);
} else {
out[key] = value;
}
}
}
return out;
}
module.exports = {
parseCertString
};