From e352bedfd957c104df3f9e0cbaf4140422569bd5 Mon Sep 17 00:00:00 2001 From: David Ball Date: Mon, 15 Apr 2024 02:32:31 -0400 Subject: [PATCH] Fixed vercel serve-handler so that it handles directory junctions and directory symlinks without error --- app/server.js | 16 +++++++++++++++- app/vercel-serve.js | 26 +++++++++++++++++++++----- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/app/server.js b/app/server.js index 18ca6029..039b72c0 100644 --- a/app/server.js +++ b/app/server.js @@ -37,7 +37,21 @@ app.use(express.json()); app.get('/ads.txt', (req, res) => { res.setHeader("Content-Type", "text/plain"); - res.send('google.com, pub-8937572456576531, DIRECT, f08c47fec0942fa0'); + res.setHeader("Cache-Control", "no-cache"); + res.send(`google.com, pub-8937572456576531, DIRECT, f08c47fec0942fa0`); +}); + +app.get('/robots.txt', (req, res) => { + res.setHeader("Content-Type", "text/plain"); + res.setHeader("Cache-Control", "no-cache"); + // TODO: Implement Site Map feature and provide sitemap url in robots.txt + res.send( +`User-agent: * +Allow: / + +# TODO: Implement Site Map feature and provide sitemap url in robots.txt +#sitemap: https://no-moss-3-carbo-landfill-library.online/sitemap.xml` + );//end of res.send() for robots.txt }); // Search endpoints diff --git a/app/vercel-serve.js b/app/vercel-serve.js index 66e176d3..b92ebb6c 100644 --- a/app/vercel-serve.js +++ b/app/vercel-serve.js @@ -4,7 +4,7 @@ const {promisify} = require('util'); const path = require('path'); const {createHash} = require('crypto'); -const {realpath, lstat, createReadStream, readdir} = require('fs'); +const {realpath, readlink, lstat, createReadStream, readdir} = require('fs'); // Packages const url = require('fast-url-parser'); @@ -36,7 +36,7 @@ const directoryTemplate = (vals) => { }; const errorTemplate = (vals) => { return new Promise((resolve, reject) => { - ejs.renderFile("views/error.ejs", { h: helpers, ...vals }, (err, str) => { + ejs.renderFile(path.join(__dirname, '..', 'views', 'error.ejs'), { h: helpers, ...vals }, (err, str) => { if (err) { reject(err); } else { @@ -46,6 +46,19 @@ const errorTemplate = (vals) => { }); }; +const isDirectoryOrDirectorySymbolicLink = (path, max_recursion_depth = 10, cb) => { + lstat(path, {}, (err, sym_stats) => { + if (sym_stats.isSymbolicLink() && max_recursion_depth > 0) { + readlink(path, {}, (err, path) => { + isDirectoryOrDirectorySymbolicLink(path, max_recursion_depth-1, cb); + }); + } + else { + cb(err, sym_stats.isDirectory()); + } + }); +}; + const etags = new Map(); const calculateSha = (handlers, absolutePath) => @@ -387,7 +400,8 @@ const renderDirectory = async (current, acceptsJSON, handlers, methods, config, details.relative = path.join(relativePath, details.base); - if (stats.isDirectory()) { + //if (stats.isDirectory()) { + if (await handlers.isDirectoryOrDirectorySymbolicLink(filePath, 10)) { details.base += slashSuffix; details.relative += slashSuffix; details.type = 'folder'; @@ -569,7 +583,8 @@ const getHandlers = methods => Object.assign({ realpath: promisify(realpath), createReadStream, readdir: promisify(readdir), - sendError + sendError, + isDirectoryOrDirectorySymbolicLink: promisify(isDirectoryOrDirectorySymbolicLink) }, methods); module.exports = async (request, response, config = {}, methods = {}) => { @@ -668,7 +683,8 @@ module.exports = async (request, response, config = {}, methods = {}) => { } } - if (stats && stats.isDirectory()) { + //if (stats && stats.isDirectory()) { + if (stats && await handlers.isDirectoryOrDirectorySymbolicLink(absolutePath, 10)) { let directory = null; let singleFile = null;