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 PouchDB = require("pouchdb")
const env = require("../environment") const env = require("../environment")
exports.getCouchInfo = () => { function getUrlInfo() {
let url = "http://localhost:4005" let url = env.COUCH_DB_URL
if (env.COUCH_DB_URL && env.COUCH_DB_URL.includes("@")) { let username, password, host
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."
)
}
}
const [protocol, rest] = url.split("://") const [protocol, rest] = url.split("://")
const [auth, host] = rest.split("@") if (url.includes("@")) {
let [username, password] = auth.split(":") const hostParts = rest.split("@")
if (!username && env.COUCH_DB_USERNAME) { host = hostParts[1]
username = env.COUCH_DB_USERNAME 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 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") const authCookie = Buffer.from(`${username}:${password}`).toString("base64")
return { return {
url: `${protocol}://${host}`, url: urlInfo.url,
auth: { auth: {
username: username, username: username,
password: password, password: password,

View File

@ -8,7 +8,7 @@ function isTest() {
module.exports = { module.exports = {
JWT_SECRET: process.env.JWT_SECRET, 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_USERNAME: process.env.COUCH_DB_USER,
COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD, COUCH_DB_PASSWORD: process.env.COUCH_DB_PASSWORD,
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID, GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,