fix: once and for all fix #907

This commit is contained in:
diced
2026-03-10 21:37:26 -07:00
parent 13e3a58035
commit dee86aaa86
2 changed files with 34 additions and 26 deletions

View File

@@ -86,19 +86,6 @@ async function main() {
trustProxy: config.core.trustProxy, trustProxy: config.core.trustProxy,
}).withTypeProvider<ZodTypeProvider>(); }).withTypeProvider<ZodTypeProvider>();
if (process.env.DEBUG_EVENT_EMITTER) {
server.addHook('onSend', async (req, res) => {
const counts = {
listeners: res.raw.eventNames(),
close: res.raw.listenerCount('close'),
data: res.raw.listenerCount('data'),
end: res.raw.listenerCount('end'),
error: res.raw.listenerCount('error'),
};
logger.debug('event emitter counts', { path: req.url, ...counts });
});
}
server.setValidatorCompiler(validatorCompiler); server.setValidatorCompiler(validatorCompiler);
server.setSerializerCompiler(serializerCompiler); server.setSerializerCompiler(serializerCompiler);

View File

@@ -1,25 +1,46 @@
import { config } from '@/lib/config'; import { config } from '@/lib/config';
import { existsSync, readFileSync } from 'fs';
import { join } from 'path'; import { join } from 'path';
import typedPlugin from '../typedPlugin'; import typedPlugin from '../typedPlugin';
import { sanitizeFilename } from '@/lib/fs';
export const FAVICON_SIZES = [16, 32, 64, 128, 512]; export const FAVICON_SIZES = [16, 32, 64, 128, 512];
export const PUBLIC_DIR = join(process.cwd(), 'public');
export const PATH = '/favicon.ico'; function loadFavicon(file: string): Buffer | null {
const path = join(PUBLIC_DIR, file);
if (!existsSync(path)) return null;
return readFileSync(path);
}
const FAVICONS: Record<string, Buffer | null> = {
'favicon.ico': loadFavicon('favicon.ico'),
...Object.fromEntries(
FAVICON_SIZES.map((size) => {
const name = `favicon-${size}x${size}.png`;
return [name, loadFavicon(name)];
}),
),
};
export const PATH = '/favicon*';
export default typedPlugin( export default typedPlugin(
async (server) => { async (server) => {
server.get(PATH, (_, res) => { server.get(PATH, (req, res) => {
return res.sendFile('favicon.ico', join(process.cwd(), 'public')); const filename = sanitizeFilename(req.url.replace('/', ''));
}); if (!filename) return res.callNotFound();
// different sizes of favicon for PWA, if they exist then serve them const buffer = FAVICONS[filename];
for (const size of FAVICON_SIZES) {
const str = `${size}x${size}`;
server.get(`/favicon-${str}.png`, async (_, res) => {
if (!config.pwa.enabled) return res.callNotFound();
return res.sendFile(`favicon-${str}.png`, join(process.cwd(), 'public')); if (!buffer) return res.callNotFound();
if (filename.startsWith('favicon-') && !config.pwa.enabled) return res.callNotFound();
return res
.type(filename.endsWith('.ico') ? 'image/x-icon' : 'image/png')
.header('Cache-Control', 'public, max-age=86400')
.send(buffer);
}); });
}
}, },
{ name: PATH }, { name: PATH },
); );