Fix #3453. Thanks to #3499, #3500 and #3521, most of the obstacles to read-only installation have been resolved. This PR addresses the final piece, ensuring that SillyTavern no longer changes directories to `serverDirectory` and outputs files there. Instead, it outputs or copies necessary files to the directory where it is being run. Now, `serverDirectory` is read-only for SillyTavern (i.e., SillyTavern will not attempt to modify `serverDirectory`). Additionally, this PR sets the permissions for copied `default-user` files to be writable, so even if SillyTavern is installed as read-only, the copied `default-user` folder can still be modified.
91 lines
2.8 KiB
JavaScript
91 lines
2.8 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import mime from 'mime-types';
|
|
import { serverDirectory } from './server-directory.js';
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
|
|
const ALLOWED_EXTENSIONS = [
|
|
'.wasm',
|
|
];
|
|
|
|
/**
|
|
* Checks if a child path is under a parent path.
|
|
* @param {string} parentPath Parent path
|
|
* @param {string} childPath Child path
|
|
* @returns {boolean} Returns true if the child path is under the parent path, false otherwise
|
|
*/
|
|
function isPathUnderParent(parentPath, childPath) {
|
|
const normalizedParent = path.normalize(parentPath);
|
|
const normalizedChild = path.normalize(childPath);
|
|
|
|
const relativePath = path.relative(normalizedParent, normalizedChild);
|
|
|
|
return !relativePath.startsWith('..') && !path.isAbsolute(relativePath);
|
|
}
|
|
|
|
/**
|
|
* Checks if the given request is a file URL.
|
|
* @param {string | URL | Request} request The request to check
|
|
* @return {boolean} Returns true if the request is a file URL, false otherwise
|
|
*/
|
|
function isFileURL(request) {
|
|
if (typeof request === 'string') {
|
|
return request.startsWith('file://');
|
|
}
|
|
if (request instanceof URL) {
|
|
return request.protocol === 'file:';
|
|
}
|
|
if (request instanceof Request) {
|
|
return request.url.startsWith('file://');
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Gets the URL from the request.
|
|
* @param {string | URL | Request} request The request to get the URL from
|
|
* @return {string} The URL of the request
|
|
*/
|
|
function getRequestURL(request) {
|
|
if (typeof request === 'string') {
|
|
return request;
|
|
}
|
|
if (request instanceof URL) {
|
|
return request.href;
|
|
}
|
|
if (request instanceof Request) {
|
|
return request.url;
|
|
}
|
|
throw new TypeError('Invalid request type');
|
|
}
|
|
|
|
// Patched fetch function that handles file URLs
|
|
globalThis.fetch = async (/** @type {string | URL | Request} */ request, /** @type {RequestInit | undefined} */ options) => {
|
|
if (!isFileURL(request)) {
|
|
return originalFetch(request, options);
|
|
}
|
|
const url = getRequestURL(request);
|
|
const filePath = path.resolve(fileURLToPath(url));
|
|
const isUnderServerDirectory = isPathUnderParent(serverDirectory, filePath);
|
|
if (!isUnderServerDirectory) {
|
|
throw new Error('Requested file path is outside of the server directory.');
|
|
}
|
|
const parsedPath = path.parse(filePath);
|
|
if (!ALLOWED_EXTENSIONS.includes(parsedPath.ext)) {
|
|
throw new Error('Unsupported file extension.');
|
|
}
|
|
const fileName = parsedPath.base;
|
|
const buffer = await fs.promises.readFile(filePath);
|
|
const response = new Response(buffer, {
|
|
status: 200,
|
|
statusText: 'OK',
|
|
headers: {
|
|
'Content-Type': mime.lookup(fileName) || 'application/octet-stream',
|
|
'Content-Length': buffer.length.toString(),
|
|
},
|
|
});
|
|
return response;
|
|
};
|