2022-03-30 04:03:44 +13:00
|
|
|
const PouchDB = require("pouchdb")
|
|
|
|
const env = require("../environment")
|
|
|
|
|
2022-04-28 09:36:45 +12:00
|
|
|
function getUrlInfo() {
|
|
|
|
let url = env.COUCH_DB_URL
|
|
|
|
let username, password, host
|
2022-04-23 04:24:50 +12:00
|
|
|
const [protocol, rest] = url.split("://")
|
2022-04-28 09:36:45 +12:00
|
|
|
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
|
2022-04-28 03:56:30 +12:00
|
|
|
username = env.COUCH_DB_USERNAME
|
2022-04-28 09:36:45 +12:00
|
|
|
} else if (urlInfo.auth.username) {
|
|
|
|
// set from url
|
|
|
|
username = urlInfo.auth.username
|
2022-04-28 09:57:03 +12:00
|
|
|
} else if (!env.isTest()) {
|
2022-04-28 09:36:45 +12:00
|
|
|
throw new Error("CouchDB username not set")
|
2022-04-28 03:56:30 +12:00
|
|
|
}
|
2022-04-28 09:36:45 +12:00
|
|
|
if (env.COUCH_DB_PASSWORD) {
|
|
|
|
// set from env
|
2022-04-28 03:56:30 +12:00
|
|
|
password = env.COUCH_DB_PASSWORD
|
2022-04-28 09:36:45 +12:00
|
|
|
} else if (urlInfo.auth.password) {
|
|
|
|
// set from url
|
|
|
|
password = urlInfo.auth.password
|
2022-04-28 09:57:03 +12:00
|
|
|
} else if (!env.isTest()) {
|
2022-04-28 09:36:45 +12:00
|
|
|
throw new Error("CouchDB password not set")
|
2022-04-28 03:56:30 +12:00
|
|
|
}
|
|
|
|
const authCookie = Buffer.from(`${username}:${password}`).toString("base64")
|
2022-04-23 04:24:50 +12:00
|
|
|
return {
|
2022-04-28 09:36:45 +12:00
|
|
|
url: urlInfo.url,
|
2022-04-23 04:24:50 +12:00
|
|
|
auth: {
|
2022-04-28 03:56:30 +12:00
|
|
|
username: username,
|
|
|
|
password: password,
|
2022-04-23 04:24:50 +12:00
|
|
|
},
|
2022-04-28 03:56:30 +12:00
|
|
|
cookie: `Basic ${authCookie}`,
|
2022-04-23 04:24:50 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 04:03:44 +13:00
|
|
|
/**
|
|
|
|
* Return a constructor for PouchDB.
|
|
|
|
* This should be rarely used outside of the main application config.
|
|
|
|
* Exposed for exceptional cases such as in-memory views.
|
|
|
|
*/
|
|
|
|
exports.getPouch = (opts = {}) => {
|
2022-04-28 03:56:30 +12:00
|
|
|
let { url, cookie } = exports.getCouchInfo()
|
2022-03-30 04:03:44 +13:00
|
|
|
let POUCH_DB_DEFAULTS = {
|
2022-04-23 04:24:50 +12:00
|
|
|
prefix: url,
|
|
|
|
fetch: (url, opts) => {
|
|
|
|
// use a specific authorization cookie - be very explicit about how we authenticate
|
2022-04-28 03:56:30 +12:00
|
|
|
opts.headers.set("Authorization", cookie)
|
2022-04-23 04:24:50 +12:00
|
|
|
return PouchDB.fetch(url, opts)
|
|
|
|
},
|
2022-03-30 04:03:44 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.inMemory) {
|
|
|
|
const inMemory = require("pouchdb-adapter-memory")
|
|
|
|
PouchDB.plugin(inMemory)
|
|
|
|
POUCH_DB_DEFAULTS = {
|
|
|
|
prefix: undefined,
|
|
|
|
adapter: "memory",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.replication) {
|
|
|
|
const replicationStream = require("pouchdb-replication-stream")
|
|
|
|
PouchDB.plugin(replicationStream.plugin)
|
|
|
|
PouchDB.adapter("writableStream", replicationStream.adapters.writableStream)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.find) {
|
|
|
|
const find = require("pouchdb-find")
|
|
|
|
PouchDB.plugin(find)
|
|
|
|
}
|
|
|
|
|
2022-05-21 05:29:37 +12:00
|
|
|
return PouchDB.defaults(POUCH_DB_DEFAULTS)
|
2022-03-30 04:03:44 +13:00
|
|
|
}
|