use centralised document types and separators

This commit is contained in:
Martin McKeaveney 2020-10-09 10:50:44 +01:00
commit f54062575b
26 changed files with 77 additions and 86 deletions

View File

@ -1,5 +1,5 @@
const fs = require("fs")
const { join } = require("../../utilities/sanitisedPath")
const { join } = require("../../utilities/centralPath")
const readline = require("readline")
const { budibaseAppsDir } = require("../../utilities/budibaseDir")
const ENV_FILE_PATH = "/.env"

View File

@ -8,7 +8,7 @@ const { budibaseAppsDir } = require("../../utilities/budibaseDir")
const sqrl = require("squirrelly")
const setBuilderToken = require("../../utilities/builder/setBuilderToken")
const fs = require("fs-extra")
const { join, resolve } = require("../../utilities/sanitisedPath")
const { join, resolve } = require("../../utilities/centralPath")
const { promisify } = require("util")
const chmodr = require("chmodr")
const { generateAppID, getAppParams } = require("../../db/utils")

View File

@ -1,6 +1,6 @@
const CouchDB = require("../../db")
const ClientDb = require("../../db/clientDb")
const { resolve, join } = require("../../utilities/sanitisedPath")
const { resolve, join } = require("../../utilities/centralPath")
const {
budibaseTempDir,
budibaseAppsDir,

View File

@ -1,5 +1,5 @@
const fs = require("fs")
const { join } = require("../../../utilities/sanitisedPath")
const { join } = require("../../../utilities/centralPath")
const AWS = require("aws-sdk")
const fetch = require("node-fetch")
const { budibaseAppsDir } = require("../../../utilities/budibaseDir")

View File

@ -5,7 +5,7 @@ const {
verifyDeployment,
updateDeploymentQuota,
} = require("./aws")
const { DocumentTypes } = require("../../../db/utils")
const { DocumentTypes, SEPARATOR, UNICODE_MAX } = require("../../../db/utils")
function replicate(local, remote) {
return new Promise((resolve, reject) => {
@ -38,20 +38,25 @@ async function replicateCouch({ instanceId, clientId, credentials }) {
async function getCurrentInstanceQuota(instanceId) {
const db = new PouchDB(instanceId)
const records = await db.allDocs({
startkey: "re:",
endkey: `re:\ufff0`,
startkey: DocumentTypes.RECORD + SEPARATOR,
endkey: DocumentTypes.RECORD + SEPARATOR + UNICODE_MAX,
})
const users = await db.allDocs({
startkey: DocumentTypes.USER + ,
endkey: `us:\ufff0`,
startkey: DocumentTypes.USER + SEPARATOR,
endkey: DocumentTypes.USER + SEPARATOR + UNICODE_MAX,
})
const existingRecords = records.rows.length
const existingUsers = users.rows.length
const designDoc = await db.get("_design/database")
return {
records: existingRecords,
users: existingUsers,
views: Object.keys(designDoc.views).length,
}
}

View File

@ -3,7 +3,7 @@ const CouchDB = require("../../db")
const client = require("../../db/clientDb")
const newid = require("../../db/newid")
const { createLinkView } = require("../../db/linkedRecords")
const { join } = require("../../utilities/sanitisedPath")
const { join } = require("../../utilities/centralPath")
const { downloadTemplate } = require("../../utilities/templates")
exports.create = async function(ctx) {

View File

@ -1,10 +1,15 @@
const CouchDB = require("../../db")
const validateJs = require("validate.js")
const linkRecords = require("../../db/linkedRecords")
const { getRecordParams, generateRecordID } = require("../../db/utils")
const {
getRecordParams,
generateRecordID,
DocumentTypes,
SEPARATOR,
} = require("../../db/utils")
const { cloneDeep } = require("lodash")
const MODEL_VIEW_BEGINS_WITH = "all_model:"
const MODEL_VIEW_BEGINS_WITH = `all${SEPARATOR}${DocumentTypes.MODEL}${SEPARATOR}`
validateJs.extend(validateJs.validators.datetime, {
parse: function(value) {

View File

@ -1,5 +1,5 @@
const send = require("koa-send")
const { resolve, join } = require("../../utilities/sanitisedPath")
const { resolve, join } = require("../../utilities/centralPath")
const jwt = require("jsonwebtoken")
const fetch = require("node-fetch")
const fs = require("fs-extra")

View File

@ -1,7 +1,7 @@
const CouchDB = require("../../../db")
const viewTemplate = require("./viewBuilder")
const fs = require("fs")
const { join } = require("../../../utilities/sanitisedPath")
const { join } = require("../../../utilities/centralPath")
const os = require("os")
const exporters = require("./exporters")
const { fetchView } = require("../record")

View File

@ -6,7 +6,7 @@ const createUser = require("./steps/createUser")
const environment = require("../environment")
const download = require("download")
const fetch = require("node-fetch")
const { join } = require("../utilities/sanitisedPath")
const { join } = require("../utilities/centralPath")
const os = require("os")
const fs = require("fs")
const Sentry = require("@sentry/node")

View File

@ -2,7 +2,6 @@ const PouchDB = require("pouchdb")
const replicationStream = require("pouchdb-replication-stream")
const allDbs = require("pouchdb-all-dbs")
const { budibaseAppsDir } = require("../utilities/budibaseDir")
const { sanitise } = require("../utilities/sanitisedPath")
const env = require("../environment")
const COUCH_DB_URL = env.COUCH_DB_URL || `leveldb://${budibaseAppsDir()}/.data/`
@ -27,10 +26,4 @@ const Pouch = PouchDB.defaults(POUCH_DB_DEFAULTS)
allDbs(Pouch)
function PouchWrapper(instance) {
Pouch.apply(this, [sanitise(instance)])
}
PouchWrapper.prototype = Object.create(Pouch.prototype)
module.exports = PouchWrapper
module.exports = Pouch

View File

@ -1,5 +1,8 @@
const newid = require("./newid")
const UNICODE_MAX = "\ufff0"
const SEPARATOR = "_"
const DocumentTypes = {
MODEL: "mo",
RECORD: "re",
@ -11,9 +14,7 @@ const DocumentTypes = {
}
exports.DocumentTypes = DocumentTypes
const UNICODE_MAX = "\ufff0"
exports.SEPARATOR = SEPARATOR
exports.UNICODE_MAX = UNICODE_MAX
/**
@ -34,8 +35,8 @@ function getDocParams(docType, docId = null, otherProps = {}) {
}
return {
...otherProps,
startkey: `${docType}_${docId}`,
endkey: `${docType}_${docId}${UNICODE_MAX}`,
startkey: `${docType}${SEPARATOR}${docId}`,
endkey: `${docType}${SEPARATOR}${docId}${UNICODE_MAX}`,
}
}
@ -51,7 +52,7 @@ exports.getModelParams = (modelId = null, otherProps = {}) => {
* @returns {string} The new model ID which the model doc can be stored under.
*/
exports.generateModelID = () => {
return `${DocumentTypes.MODEL}_${newid()}`
return `${DocumentTypes.MODEL}${SEPARATOR}${newid()}`
}
/**
@ -66,7 +67,10 @@ exports.getRecordParams = (modelId, recordId = null, otherProps = {}) => {
if (modelId == null) {
throw "Cannot build params for records without a model ID"
}
const endOfKey = recordId == null ? `${modelId}_` : `${modelId}_${recordId}`
const endOfKey =
recordId == null
? `${modelId}${SEPARATOR}`
: `${modelId}${SEPARATOR}${recordId}`
return getDocParams(DocumentTypes.RECORD, endOfKey, otherProps)
}
@ -76,7 +80,7 @@ exports.getRecordParams = (modelId, recordId = null, otherProps = {}) => {
* @returns {string} The new ID which a record doc can be stored under.
*/
exports.generateRecordID = modelId => {
return `${DocumentTypes.RECORD}_${modelId}_${newid()}`
return `${DocumentTypes.RECORD}${SEPARATOR}${modelId}${SEPARATOR}${newid()}`
}
/**
@ -92,7 +96,7 @@ exports.getUserParams = (username = null, otherProps = {}) => {
* @returns {string} The new user ID which the user doc can be stored under.
*/
exports.generateUserID = username => {
return `${DocumentTypes.USER}_${username}`
return `${DocumentTypes.USER}${SEPARATOR}${username}`
}
/**
@ -107,7 +111,7 @@ exports.getAutomationParams = (automationId = null, otherProps = {}) => {
* @returns {string} The new automation ID which the automation doc can be stored under.
*/
exports.generateAutomationID = () => {
return `${DocumentTypes.AUTOMATION}_${newid()}`
return `${DocumentTypes.AUTOMATION}${SEPARATOR}${newid()}`
}
/**
@ -120,7 +124,7 @@ exports.generateAutomationID = () => {
* @returns {string} The new link doc ID which the automation doc can be stored under.
*/
exports.generateLinkID = (modelId1, modelId2, recordId1, recordId2) => {
return `${DocumentTypes.AUTOMATION}_${modelId1}_${modelId2}_${recordId1}_${recordId2}`
return `${DocumentTypes.AUTOMATION}${SEPARATOR}${modelId1}${SEPARATOR}${modelId2}${SEPARATOR}${recordId1}${SEPARATOR}${recordId2}`
}
/**
@ -128,7 +132,7 @@ exports.generateLinkID = (modelId1, modelId2, recordId1, recordId2) => {
* @returns {string} The new app ID which the app doc can be stored under.
*/
exports.generateAppID = () => {
return `${DocumentTypes.APP}_${newid()}`
return `${DocumentTypes.APP}${SEPARATOR}${newid()}`
}
/**
@ -143,7 +147,7 @@ exports.getAppParams = (appId = null, otherProps = {}) => {
* @returns {string} The new access level ID which the access level doc can be stored under.
*/
exports.generateAccessLevelID = () => {
return `${DocumentTypes.ACCESS_LEVEL}_${newid()}`
return `${DocumentTypes.ACCESS_LEVEL}${SEPARATOR}${newid()}`
}
/**

View File

@ -1,5 +1,5 @@
const { app, BrowserWindow, shell, dialog } = require("electron")
const { join } = require("./utilities/sanitisedPath")
const { join } = require("./utilities/centralPath")
const isDev = require("electron-is-dev")
const { autoUpdater } = require("electron-updater")
const unhandled = require("electron-unhandled")

View File

@ -1,4 +1,4 @@
const { resolve, join } = require("./utilities/sanitisedPath")
const { resolve, join } = require("./utilities/centralPath")
const { homedir } = require("os")
const { app } = require("electron")
const fixPath = require("fix-path")

View File

@ -1,4 +1,4 @@
const { join } = require("./sanitisedPath")
const { join } = require("./centralPath")
const { homedir, tmpdir } = require("os")
const env = require("../environment")

View File

@ -6,7 +6,7 @@ const {
readFile,
writeJSON,
} = require("fs-extra")
const { join, resolve } = require("../sanitisedPath")
const { join, resolve } = require("../centralPath")
const sqrl = require("squirrelly")
const { convertCssToFiles } = require("./convertCssToFiles")
const publicPath = require("./publicPath")

View File

@ -1,6 +1,6 @@
const crypto = require("crypto")
const { ensureDir, emptyDir, writeFile } = require("fs-extra")
const { join } = require("../sanitisedPath")
const { join } = require("../centralPath")
module.exports.convertCssToFiles = async (publicPagePath, pkg) => {
const cssDir = join(publicPagePath, "css")

View File

@ -1,5 +1,5 @@
const { readJSON, readdir } = require("fs-extra")
const { join } = require("../sanitisedPath")
const { join } = require("../centralPath")
module.exports = async appPath => {
const pages = {}

View File

@ -8,7 +8,7 @@ const {
unlink,
rmdir,
} = require("fs-extra")
const { join, resolve } = require("../sanitisedPath")
const { join, resolve } = require("../centralPath")
const { dirname } = require("path")
const env = require("../../environment")

View File

@ -1,6 +1,6 @@
const { appPackageFolder } = require("../createAppPackage")
const { readJSON, readdir, stat } = require("fs-extra")
const { join } = require("../sanitisedPath")
const { join } = require("../centralPath")
const { keyBy } = require("lodash/fp")
module.exports = async (config, appname, pagename) => {

View File

@ -1,3 +1,3 @@
const { join } = require("../sanitisedPath")
const { join } = require("../centralPath")
module.exports = (appPath, pageName) => join(appPath, "public", pageName)

View File

@ -0,0 +1,22 @@
const path = require("path")
// this simply runs all of our path join and resolve functions through
// a central location incase we need to add some protection to file paths
/**
* Exactly the same as path.join
* @param args Any number of string arguments to add to a path
* @returns {string} The final path ready to use
*/
exports.join = function(...args) {
return path.join(...args)
}
/**
* Exactly the same as path.resolve
* @param args Any number of string arguments to add to a path
* @returns {string} The final path ready to use
*/
exports.resolve = function(...args) {
return path.resolve(...args)
}

View File

@ -1,4 +1,4 @@
const { resolve } = require("./sanitisedPath")
const { resolve } = require("./centralPath")
const { cwd } = require("process")
const stream = require("stream")
const fetch = require("node-fetch")

View File

@ -1,5 +1,5 @@
const { exists, readFile, writeFile, ensureDir } = require("fs-extra")
const { join, resolve } = require("./sanitisedPath")
const { join, resolve } = require("./centralPath")
const Sqrl = require("squirrelly")
const uuid = require("uuid")

View File

@ -1,38 +0,0 @@
const path = require("path")
const regex = new RegExp(/:(?![\\/])/g)
function sanitiseArgs(args) {
let sanitised = []
for (let arg of args) {
sanitised.push(arg.replace(regex, ""))
}
return sanitised
}
/**
* Exactly the same as path.join but creates a sanitised path.
* @param args Any number of string arguments to add to a path
* @returns {string} The final path ready to use
*/
exports.join = function(...args) {
return path.join(...sanitiseArgs(args))
}
/**
* Exactly the same as path.resolve but creates a sanitised path.
* @param args Any number of string arguments to add to a path
* @returns {string} The final path ready to use
*/
exports.resolve = function(...args) {
return path.resolve(...sanitiseArgs(args))
}
/**
* Sanitise a single string
* @param string input string to sanitise
* @returns {string} the final sanitised string
*/
exports.sanitise = function(string) {
return sanitiseArgs([string])[0]
}

View File

@ -1,5 +1,5 @@
const fs = require("fs-extra")
const { join } = require("./sanitisedPath")
const { join } = require("./centralPath")
const os = require("os")
const fetch = require("node-fetch")
const stream = require("stream")