All files / core/blong-gogo/src static.ts

94.64% Statements 53/56
100% Branches 5/5
100% Functions 2/2
94.64% Lines 53/56

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 571x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x     10x   10x 10x 10x 10x 10x 10x 9x 9x 9x 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 {existsSync} from 'node:fs';
import path from 'path';
 
export type IConfig = {
    root: string;
};
 
/**
 * The development serve: a process's own `/s`, from the build beside its entry.
 *
 * A deployment no longer serves its bundle this way — `realm/blong-kustomize` gives `/s/` to
 * `@feasibleone/blong-asset`, a process that carries nothing of the API. This plugin stays for the
 * run a developer makes and for a process reached directly, and the two are one registration apart:
 * `@feasibleone/blong-asset` exports the same routes as a fastify plugin (`assetPlugin`), so a
 * future in-process serve replaces this file's body rather than growing a second set of rules.
 */
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);
    });
    // A root that is not there is a suite deployed without a UI, which is ordinary: a realm-only
    // suite has no browser build, and a released process that refused to start over a missing
    // directory would be an API that broke for the sake of a page nobody asked it to serve. The
    // deploy convention puts the bundle inside the artifact at `browser/dist` (Phase 15 H), and the
    // mount it arrives under is a per-suite setting — so this check is what makes naming the path in
    // a release config safe to do *before* the pipeline that builds the bundle exists.
    const root = config.root ?? path.join(process.cwd(), 'dist');
    if (!existsSync(root)) {
        fastify.log.warn({root}, 'no browser build to serve: /s is not mounted');
        return;
    }
    fastify.register(fastifyStatic, {
        root,
        prefix: '/s',
        redirect: true,
        preCompressed: true,
        cacheControl: true,
        maxAge: '1y',
        immutable: 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'",
                );
            }
        },
    });
});