From e39bb4ecc7bf552f6b7762058cea82bbc221cad0 Mon Sep 17 00:00:00 2001 From: Glen Adams Date: Fri, 26 Jan 2024 12:49:55 -0500 Subject: [PATCH] fix paths --- packages/server/lib/services/cfg.js | 9 +++-- packages/server/lib/services/db.js | 6 ++- packages/server/lib/services/filetree.js | 4 +- packages/server/lib/services/resources.js | 46 +++++++++++------------ packages/server/lib/services/server.js | 30 +++++++-------- packages/server/lib/services/svg.js | 4 +- packages/server/lib/services/utils.js | 12 +++--- 7 files changed, 58 insertions(+), 53 deletions(-) diff --git a/packages/server/lib/services/cfg.js b/packages/server/lib/services/cfg.js index c9753a3b..a5bc0d89 100644 --- a/packages/server/lib/services/cfg.js +++ b/packages/server/lib/services/cfg.js @@ -8,7 +8,7 @@ const stat = promisify(fs.stat); const readFile = promisify(fs.readFile); const mkdir = promisify(fs.mkdir); -const configFile = require("./paths.js").get().cfgFile; +const paths = require("./paths.js"); const defaults = { listeners: [{ @@ -38,6 +38,7 @@ const hiddenOpts = ["dev"]; const cfg = module.exports = {}; cfg.init = (config) => new Promise(async (resolve, reject) => { + const configFile = paths.get().cfgFile; if (typeof config === "object" && config !== null) { config = Object.assign({}, defaults, config); return resolve(config); @@ -49,7 +50,7 @@ cfg.init = (config) => new Promise(async (resolve, reject) => { config = defaults; await mkdir(dirname(configFile), {recursive: true}); - await write(config); + await write(configFile, config); resolve(config); } else { return reject(err); @@ -79,7 +80,7 @@ cfg.init = (config) => new Promise(async (resolve, reject) => { delete config[key]; } }); - await write(config); + await write(configFile, config); return resolve(config); } catch (err) { // TODO: can we print helpful information here? @@ -88,7 +89,7 @@ cfg.init = (config) => new Promise(async (resolve, reject) => { } }); -function write(config) { +function write(configFile, config) { return new Promise((resolve, reject) => { fs.writeFile(configFile, JSON.stringify(config, null, 2), (err) => { if (err) { diff --git a/packages/server/lib/services/db.js b/packages/server/lib/services/db.js index 45cfb5e3..70bfa1dd 100644 --- a/packages/server/lib/services/db.js +++ b/packages/server/lib/services/db.js @@ -7,12 +7,13 @@ const crypto = require("crypto"); const path = require("path"); const log = require("./log.js"); -const dbFile = require("./paths.js").get().db; +const paths = require("./paths.js"); const defaults = {users: {}, sessions: {}, links: {}}; let database, watching; db.load = function(callback) { + const dbFile = paths.get().db; fs.stat(dbFile, err => { if (err) { if (err.code === "ENOENT") { @@ -77,6 +78,7 @@ db.load = function(callback) { }; db.parse = function(cb) { + const dbFile = paths.get().db; fs.readFile(dbFile, "utf8", (err, data) => { if (err) return cb(err); @@ -147,6 +149,7 @@ db.authUser = function(user, pass) { }; db.watch = function(config) { + const dbFile = paths.get().db; chokidar.watch(dbFile, { ignoreInitial: true, usePolling: Boolean(config.pollingInterval), @@ -165,6 +168,7 @@ db.watch = function(config) { // TODO: async function write() { + const dbFile = paths.get().db; watching = false; fs.writeFileSync(dbFile, JSON.stringify(database, null, 2)); diff --git a/packages/server/lib/services/filetree.js b/packages/server/lib/services/filetree.js index 75223f74..5019526b 100644 --- a/packages/server/lib/services/filetree.js +++ b/packages/server/lib/services/filetree.js @@ -12,7 +12,7 @@ const rfdc = require("rfdc"); const util = require("util"); const log = require("./log.js"); -const paths = require("./paths.js").get(); +const paths = require("./paths.js"); const utils = require("./utils.js"); const clone = rfdc(); @@ -32,7 +32,7 @@ filetree.init = function(config) { }; filetree.watch = function() { - chokidar.watch(paths.files, { + chokidar.watch(paths.get().files, { alwaysStat: true, ignoreInitial: true, usePolling: Boolean(cfg.pollingInterval), diff --git a/packages/server/lib/services/resources.js b/packages/server/lib/services/resources.js index 871ee2be..b946037b 100644 --- a/packages/server/lib/services/resources.js +++ b/packages/server/lib/services/resources.js @@ -11,12 +11,12 @@ const {stat, mkdir, readdir, readFile, writeFile} = require("fs").promises; const {promisify} = require("util"); const log = require("./log.js"); -const paths = require("./paths.js").get(); +const paths = require("./paths.js"); const utils = require("./utils.js"); -const themesPath = path.join(paths.client, "/node_modules/codemirror/theme"); -const modesPath = path.join(paths.client, "/node_modules/codemirror/mode"); -const cachePath = process.env.DROPPY_CACHE_PATH ?? path.join(paths.homedir, "/.droppy/cache/cache.json"); +const themesPath = path.join(paths.get().client, "/node_modules/codemirror/theme"); +const modesPath = path.join(paths.get().client, "/node_modules/codemirror/mode"); +const cachePath = process.env.DROPPY_CACHE_PATH ?? path.join(paths.get().homedir, "/.droppy/cache/cache.json"); const pkg = require("../../package.json"); @@ -107,10 +107,10 @@ try { resources.files = { css: [ - `${paths.client}/lib/style.css`, - `${paths.client}/lib/sprites.css`, - `${paths.client}/lib/tooltips.css`, - `${paths.client}/lib/clienttheme.css`, + `${paths.get().client}/lib/style.css`, + `${paths.get().client}/lib/sprites.css`, + `${paths.get().client}/lib/tooltips.css`, + `${paths.get().client}/lib/clienttheme.css`, ], js: [ "node_modules/handlebars/dist/handlebars.runtime.min.js", @@ -229,8 +229,8 @@ async function isCacheFresh(cb) { const files = []; for (const type of Object.keys(resources.files)) { resources.files[type].forEach(file => { - if (fs.existsSync(path.join(paths.client, file))) { - files.push(path.join(paths.client, file)); + if (fs.existsSync(path.join(paths.get().client, file))) { + files.push(path.join(paths.get().client, file)); } else { files.push(file); } @@ -239,15 +239,15 @@ async function isCacheFresh(cb) { for (const file of Object.keys(libs)) { if (typeof libs[file] === "string") { - if (fs.existsSync(path.join(paths.client, libs[file]))) { - files.push(path.join(paths.client, libs[file])); + if (fs.existsSync(path.join(paths.get().client, libs[file]))) { + files.push(path.join(paths.get().client, libs[file])); } else { files.push(libs[file]); } } else { libs[file].forEach(file => { - if (fs.existsSync(path.join(paths.client, file))) { - files.push(path.join(paths.client, file)); + if (fs.existsSync(path.join(paths.get().client, file))) { + files.push(path.join(paths.get().client, file)); } else { files.push(file); } @@ -324,7 +324,7 @@ async function readThemes() { themes[name.replace(/\.css$/, "")] = Buffer.from(await minifyCSS(String(data))); } - const droppyTheme = await readFile(path.join(paths.client, "/lib/cmtheme.css")); + const droppyTheme = await readFile(path.join(paths.get().client, "/lib/cmtheme.css")); themes.droppy = Buffer.from(await minifyCSS(String(droppyTheme))); return themes; @@ -334,7 +334,7 @@ async function readModes() { const modes = {}; // parse meta.js from CM for supported modes - const js = await readFile(path.join(paths.client, "/node_modules/codemirror/mode/meta.js")); + const js = await readFile(path.join(paths.get().client, "/node_modules/codemirror/mode/meta.js")); // Extract modes from CodeMirror const sandbox = {CodeMirror: {}}; @@ -357,7 +357,7 @@ async function readLibs() { for (const [dest, files] of Object.entries(libs)) { lib[dest] = Buffer.concat(await Promise.all(files.map(file => { - return readFile(path.join(paths.client, file)); + return readFile(path.join(paths.get().client, file)); }))); } @@ -397,8 +397,8 @@ function templates() { "templates=Handlebars.templates=Handlebars.templates||{};"; const suffix = "Handlebars.partials=Handlebars.templates})();"; - return prefix + fs.readdirSync(paths.templates).map(file => { - const p = path.join(paths.templates, file); + return prefix + fs.readdirSync(paths.get().templates).map(file => { + const p = path.join(paths.get().templates, file); const name = file.replace(/\..+$/, ""); let html = htmlMinifier.minify(fs.readFileSync(p, "utf8"), opts.htmlMinifier); @@ -421,8 +421,8 @@ function templates() { resources.compileJS = async function() { let js = ""; resources.files.js.forEach(file => { - if (fs.existsSync(path.join(paths.client, file))) { - js += `${fs.readFileSync(path.join(paths.client, file), "utf8")};`; + if (fs.existsSync(path.join(paths.get().client, file))) { + js += `${fs.readFileSync(path.join(paths.get().client, file), "utf8")};`; } else { js += `${fs.readFileSync(file, "utf8")};`; } @@ -458,7 +458,7 @@ resources.compileCSS = async function() { }; resources.compileHTML = async function(res) { - let html = fs.readFileSync(path.join(paths.client, "lib", "index.html"), "utf8"); + let html = fs.readFileSync(path.join(paths.get().client, "lib", "index.html"), "utf8"); html = html.replace("", svg()); let auth = html.replace("{{type}}", "a"); @@ -485,7 +485,7 @@ async function compileAll() { // Read misc files for (const file of resources.files.other) { const name = path.basename(file); - const fullPath = path.join(paths.client, file); + const fullPath = path.join(paths.get().client, file); const data = fs.readFileSync(fullPath); res[name] = {data, etag: etag(data), mime: utils.contentType(name)}; } diff --git a/packages/server/lib/services/server.js b/packages/server/lib/services/server.js index 7bd6afdb..82da6725 100644 --- a/packages/server/lib/services/server.js +++ b/packages/server/lib/services/server.js @@ -23,7 +23,7 @@ const db = require("./db.js"); const filetree = require("./filetree.js"); const log = require("./log.js"); const manifest = require("./manifest.js"); -const paths = require("./paths.js").get(); +const paths = require("./paths.js"); const pkg = require("../../package.json"); const resources = require("./resources.js"); const utils = require("./utils.js"); @@ -54,11 +54,11 @@ module.exports = async function droppy(opts, isStandalone, dev, callback) { ].join(" "), [ blue("config"), "at", - green(paths.config) + green(paths.get().config) ].join(" "), [ blue("files"), "at", - green(paths.files) + green(paths.get().files) ].join(" ") ); } @@ -66,12 +66,12 @@ module.exports = async function droppy(opts, isStandalone, dev, callback) { try { await promisify((cb) => { - utils.mkdir([paths.files, paths.config], cb); + utils.mkdir([paths.get().files, paths.get().config], cb); })(); await promisify((cb) => { if (isStandalone) { - fs.writeFile(paths.pid, String(process.pid), cb); + fs.writeFile(paths.get().pid, String(process.pid), cb); } else { cb(); } @@ -967,7 +967,7 @@ function handleUploadRequest(req, res) { const tmpPath = utils.addUploadTempExt(filename); rootNames.add(utils.rootname(tmpPath)); - const dst = path.join(paths.files, dstDir, tmpPath); + const dst = path.join(paths.get().files, dstDir, tmpPath); utils.mkdir(path.dirname(dst), () => { fs.stat(dst, err => { if (err && err.code === "ENOENT") { @@ -999,8 +999,8 @@ function handleUploadRequest(req, res) { // move temp files into place await Promise.all([...rootNames].map(async p => { - const srcPath = path.join(paths.files, dstDir, p); - const dstPath = path.join(paths.files, dstDir, utils.removeUploadTempExt(p)); + const srcPath = path.join(paths.get().files, dstDir, p); + const dstPath = path.join(paths.get().files, dstDir, utils.removeUploadTempExt(p)); await promisify(utils.move)(srcPath, dstPath); })); @@ -1014,7 +1014,7 @@ function handleUploadRequest(req, res) { // remove all uploaded temp files on cancel await Promise.all([...rootNames].map(async p => { - await promisify(utils.rm)(path.join(paths.files, dstDir, p)); + await promisify(utils.rm)(path.join(paths.get().files, dstDir, p)); })); filetree.updateDir(dstDir); @@ -1085,7 +1085,7 @@ function removeClientPerDir(sid, vId) { } function debug() { - require("chokidar").watch(paths.client, { + require("chokidar").watch(paths.get().client, { alwaysStat: true, ignoreInitial: true }).on("change", file => { @@ -1129,7 +1129,7 @@ function cleanupLinks(callback) { return; } // check for links where the target does not exist anymore - fs.stat(path.join(paths.files, location), (error, stats) => { + fs.stat(path.join(paths.get().files, location), (error, stats) => { if (!stats || error) { log.debug(`deleting nonexistant link: ${shareLink}`); delete links[shareLink]; @@ -1222,7 +1222,7 @@ function streamFile(req, res, filepath, download, stats, shareLink) { // send expects a url-encoded argument sendFile(req, encodeURIComponent(utils.removeFilesPath(filepath).substring(1)), { - root: paths.files, + root: paths.get().files, dotfiles: "allow", index: false, etag: false, @@ -1267,8 +1267,8 @@ async function tlsSetup(opts, cb) { return cb(new Error("Missing TLS option 'cert'")); } - const cert = await readFile(path.resolve(paths.config, ut(opts.cert)), "utf8"); - const key = await readFile(path.resolve(paths.config, ut(opts.key)), "utf8"); + const cert = await readFile(path.resolve(paths.get().config, ut(opts.cert)), "utf8"); + const key = await readFile(path.resolve(paths.get().config, ut(opts.key)), "utf8"); cb(null, {cert, key}); } @@ -1316,6 +1316,6 @@ function endProcess(signal) { } }); if (count > 0) log.info(`Closed ${count} WebSocket${count > 1 ? "s" : ""}`); - try { fs.unlinkSync(paths.pid); } catch {} + try { fs.unlinkSync(paths.get().pid); } catch {} process.exit(0); } diff --git a/packages/server/lib/services/svg.js b/packages/server/lib/services/svg.js index 0a22e576..be348455 100644 --- a/packages/server/lib/services/svg.js +++ b/packages/server/lib/services/svg.js @@ -3,7 +3,7 @@ const svgstore = require("@droppyjs/svgstore"); const fs = require("fs"); const path = require("path"); -const paths = require("./paths.js").get(); +const paths = require("./paths.js"); module.exports = function svg() { const sprites = svgstore({ @@ -12,7 +12,7 @@ module.exports = function svg() { }, }); - fs.readdirSync(paths.svg).forEach(file => { + fs.readdirSync(paths.get().svg).forEach(file => { sprites.add(`i-${file.replace(/\.svg/, "")}`, fs.readFileSync(path.join(paths.svg, file))); }); diff --git a/packages/server/lib/services/utils.js b/packages/server/lib/services/utils.js index c1834dba..7580529f 100644 --- a/packages/server/lib/services/utils.js +++ b/packages/server/lib/services/utils.js @@ -13,7 +13,7 @@ const util = require("util"); const validate = require("valid-filename"); const {mkdir, stat, lstat, copyFile, readdir, access} = require("fs").promises; -const paths = require("./paths.js").get(); +const paths = require("./paths.js"); const forceBinaryTypes = [ "pdf", @@ -134,19 +134,19 @@ utils.normalizePath = function(p) { }; utils.addFilesPath = function(p) { - return p === "/" ? paths.files : path.join(`${paths.files}/${p}`); + return p === "/" ? paths.get().files : path.join(`${paths.get().files}/${p}`); }; utils.removeFilesPath = function(p) { - if (p.length > paths.files.length) { - return utils.normalizePath(p.substring(paths.files.length)); - } else if (p === paths.files) { + if (p.length > paths.get().files.length) { + return utils.normalizePath(p.substring(paths.get().files.length)); + } else if (p === paths.get().files) { return "/"; } }; utils.sanitizePathsInString = function(str) { - return (str || "").replace(new RegExp(escapeStringRegexp(paths.files), "g"), ""); + return (str || "").replace(new RegExp(escapeStringRegexp(paths.get().files), "g"), ""); }; utils.isPathSane = function(p, isURL) {