|
|
|
@ -1,31 +1,53 @@ |
|
|
|
const PouchDB = require("pouchdb") |
|
|
|
const env = require("../environment") |
|
|
|
|
|
|
|
exports.getCouchInfo = () => { |
|
|
|
let url = "http://localhost:4005" |
|
|
|
if (env.COUCH_DB_URL && env.COUCH_DB_URL.includes("@")) { |
|
|
|
url = env.COUCH_DB_URL |
|
|
|
} else if (env.COUCH_DB_URL) { |
|
|
|
const [protocol, ...rest] = env.COUCH_DB_URL.split("://") |
|
|
|
url = `${protocol}://${env.COUCH_DB_USERNAME}:${env.COUCH_DB_PASSWORD}@${rest}` |
|
|
|
if (!env.COUCH_DB_USERNAME || !env.COUCH_DB_PASSWORD) { |
|
|
|
throw new Error( |
|
|
|
"CouchDB configuration invalid. You must provide a fully qualified CouchDB url, or the COUCH_DB_USER and COUCH_DB_PASSWORD environment variables." |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
function getUrlInfo() { |
|
|
|
let url = env.COUCH_DB_URL |
|
|
|
let username, password, host |
|
|
|
const [protocol, rest] = url.split("://") |
|
|
|
const [auth, host] = rest.split("@") |
|
|
|
let [username, password] = auth.split(":") |
|
|
|
if (!username && env.COUCH_DB_USERNAME) { |
|
|
|
if (url.includes("@")) { |
|
|
|
const hostParts = rest.split("@") |
|
|
|
host = hostParts[1] |
|
|
|
const authParts = hostParts[0].split(":") |
|
|
|
username = authParts[0] |
|
|
|
password = authParts[1] |
|
|
|
} else { |
|
|
|
host = rest |
|
|
|
} |
|
|
|
return { |
|
|
|
url: `${protocol}://${host}`, |
|
|
|
auth: { |
|
|
|
username, |
|
|
|
password, |
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
exports.getCouchInfo = () => { |
|
|
|
const urlInfo = getUrlInfo() |
|
|
|
let username |
|
|
|
let password |
|
|
|
if (env.COUCH_DB_USERNAME) { |
|
|
|
// set from env
|
|
|
|
username = env.COUCH_DB_USERNAME |
|
|
|
} else if (urlInfo.auth.username) { |
|
|
|
// set from url
|
|
|
|
username = urlInfo.auth.username |
|
|
|
} else { |
|
|
|
throw new Error("CouchDB username not set") |
|
|
|
} |
|
|
|
if (!password && env.COUCH_DB_PASSWORD) { |
|
|
|
if (env.COUCH_DB_PASSWORD) { |
|
|
|
// set from env
|
|
|
|
password = env.COUCH_DB_PASSWORD |
|
|
|
} else if (urlInfo.auth.password) { |
|
|
|
// set from url
|
|
|
|
password = urlInfo.auth.password |
|
|
|
} else { |
|
|
|
throw new Error("CouchDB password not set") |
|
|
|
} |
|
|
|
const authCookie = Buffer.from(`${username}:${password}`).toString("base64") |
|
|
|
return { |
|
|
|
url: `${protocol}://${host}`, |
|
|
|
url: urlInfo.url, |
|
|
|
auth: { |
|
|
|
username: username, |
|
|
|
password: password, |
|
|
|
|