Files
SillyTavern/src/fetch-patch.js
T
Cohee 3ec9b1a099 Implement data clean-up dialog (#4072)
* [wip] Add user data cleanup service

* Add clean-up report viewer

* Fix review comments

* Add function comments

* Implement item actions

* Fix UI styles

* Add placeholder for empty results, update category description view

* Add displayEmptyPlaceholder method to show message when results list is empty

* Adjust menu buttons row

* Delete char-scoped data bank attachments on character deletion

* Data Bank: Handle character attachments on rename

* Remove line breaks in description strings

* Drop the category when the last item is deleted

* Skip invalid hashes instead of bailing
2025-06-01 13:56:34 +03:00

41 lines
1.4 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';
import { getRequestURL, isFileURL, isPathUnderParent } from './util.js';
const originalFetch = globalThis.fetch;
const ALLOWED_EXTENSIONS = [
'.wasm',
];
// 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;
};