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

88.13% Statements 104/118
88.88% Branches 8/9
44.44% Functions 4/9
88.13% Lines 104/118

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 1191x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 15x 15x 1x 1x 1x 15x 15x 15x 15x 1x 1x 15x 9x 14x   15x 15x 15x 15x 15x 15x 15x 15x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x     8x 8x 8x 8x 8x 8x 8x 8x               8x 8x 8x 8x 8x 8x 8x 8x 8x 8x         8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 15x 15x 15x 15x 15x 1x  
import type {IConfigRuntime, IGateway, ILog, IRegistry} from '@feasibleone/blong/types';
import {Internal} from '@feasibleone/blong/types';
import type {FastifyInstance} from 'fastify';
import fp from 'fastify-plugin';
 
interface IConfig {
    enabled: boolean;
    routePrefix: string;
    auth: false | 'jwt';
}
 
interface IGatewayWithPlugins extends IGateway {
    registerPlugin(plugin: unknown, options?: unknown): void;
}
 
interface IRpcServerWithInfo {
    info(): object;
}
 
// The api object is captured by reference so that configRuntime — which is set
// on it after infra items are constructed (load.ts) — is visible at request time.
interface IApiRef {
    log?: ILog;
    gateway?: IGatewayWithPlugins;
    registry?: IRegistry;
    rpcServer?: IRpcServerWithInfo;
    configRuntime?: IConfigRuntime;
}
 
export default class SystemDebug extends Internal {
    #config: IConfig = {
        enabled: false,
        routePrefix: '/api/sys',
        auth: false,
    };
 
    #apiRef: IApiRef;
 
    public constructor(config: IConfig, apiRef: IApiRef) {
        super({log: apiRef.log});
        this.merge(this.#config, config);
        this.#apiRef = apiRef;
    }
 
    public async init(): Promise<void> {
        if (!this.#config.enabled || !this.#apiRef.gateway) return;
 
        this.log?.warn?.(
            'systemDebug is enabled — introspection endpoints are active; do not enable in production',
        );
 
        const apiRef = this.#apiRef;
        const prefix = this.#config.routePrefix;
        const authConfig = this.#config.auth;
 
        const plugin = fp(
            async (server: FastifyInstance) => {
                // GET /api/sys/config — effective runtime configuration snapshot
                server.route({
                    method: 'GET',
                    url: `${prefix}/config`,
                    config: {auth: authConfig},
                    handler: async () => apiRef.configRuntime?.rawSnapshot ?? {},
                });
 
                // GET /api/sys/ports — registered adapter/orchestrator port definitions
                server.route({
                    method: 'GET',
                    url: `${prefix}/ports`,
                    config: {auth: authConfig},
                    handler: async () => ({
                        ports: Array.from(apiRef.registry?.ports.keys() ?? []),
                    }),
                });
 
                // GET /api/sys/methods — registered handler method groups with handler counts
                server.route({
                    method: 'GET',
                    url: `${prefix}/methods`,
                    config: {auth: authConfig},
                    handler: async () => ({
                        methods: Array.from(apiRef.registry?.methods.entries() ?? []).map(
                            ([name, handlers]) => ({
                                name,
                                handlerCount: handlers.length,
                            }),
                        ),
                    }),
                });
 
                // GET /api/sys/modules — registered realm modules.
                // Symbol keys (used internally for system-tagged infrastructure items)
                // are excluded because they are not JSON-serialisable.
                server.route({
                    method: 'GET',
                    url: `${prefix}/modules`,
                    config: {auth: authConfig},
                    handler: async () => ({
                        modules: Array.from(apiRef.registry?.modules.keys() ?? []).filter(
                            (k): k is string => typeof k === 'string',
                        ),
                    }),
                });
 
                // GET /api/sys/rpc — internal RPC server address info
                server.route({
                    method: 'GET',
                    url: `${prefix}/rpc`,
                    config: {auth: authConfig},
                    handler: async () => apiRef.rpcServer?.info() ?? {},
                });
            },
            {name: 'system-debug'},
        );
 
        apiRef.gateway!.registerPlugin(plugin);
    }
}