Review comments.

This commit is contained in:
mike12345567 2022-04-27 22:36:45 +01:00
parent 6d6e83f7c8
commit 2865d6531c
2 changed files with 42 additions and 20 deletions

View File

@ -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) {
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
}
if (!password && env.COUCH_DB_PASSWORD) {
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 (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,

View File

@ -8,7 +8,7 @@ function isTest() {
module.exports = {
JWT_SECRET: process.env.JWT_SECRET,
COUCH_DB_URL: process.env.COUCH_DB_URL,
COUCH_DB_URL: process.env.COUCH_DB_URL || "http://localhost:4005",
COUCH_DB_USERNAME: process.env.COUCH_DB_USER,
COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD,
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,