All files / blong-gogo/src static.ts

70.96% Statements 22/31
100% Branches 1/1
50% Functions 1/2
70.96% Lines 22/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x               1x 1x  
import helmet from '@fastify/helmet';
import fastifyStatic from '@fastify/static';
import type {FastifyInstance} from 'fastify';
import fp from 'fastify-plugin';
import path from 'path';
 
export type IConfig = {
    root: string;
};
 
export default fp<IConfig>(async function staticPlugin(fastify: FastifyInstance, config: IConfig) {
    await fastify.register(helmet, {
        contentSecurityPolicy: false, // Prevents helmet from wasting bytes on images/scripts/JSON
    });
    fastify.get('/favicon.ico', async (_request, reply) => {
        return reply.sendFile('favicon.ico', import.meta.dirname);
    });
    fastify.register(fastifyStatic, {
        root: config.root ?? path.join(process.cwd(), 'dist'),
        prefix: '/s',
        redirect: true,
        setHeaders: (res, filePath) => {
            if (filePath.endsWith('.html')) {
                res.header(
                    'Content-Security-Policy',
                    "default-src 'self';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline'",
                );
            }
        },
    });
});