Fixed vercel serve-handler so that it handles directory junctions and directory symlinks without error

This commit is contained in:
David Ball 2024-04-15 02:32:31 -04:00
parent cc2b9e686e
commit e352bedfd9
2 changed files with 36 additions and 6 deletions

View File

@ -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

View File

@ -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;