All files / core/blong-kukum/orchestrator/kukum handlers.ts

100% Statements 70/70
100% Branches 7/7
100% Functions 3/3
100% Lines 70/70

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 711x 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 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 6x 6x 7x 1x 1x 1x 16x 80x 1x 1x 1x 1x 1x 1x 1x 80x 16x 1x 1x 1x  
import {handler} from '@feasibleone/blong';
import {PREDICATES} from '../../engine.ts';
import {methodName} from '../../operation.ts';
import {PRIMITIVES} from '../../primitives/index.ts';
 
/**
 * The kukum API surface, generated from the primitive catalogue.
 *
 * Every method is a one-line delegate to the `library()` function that
 * implements it — one per endpoint, beside this file. The file name is the
 * endpoint path with the `kukum` namespace dropped, which is also the name the
 * lib proxy registers: `kukum.source.get` is `sourceGet.ts`, and
 * `kukum.<primitive>.add` — one file serving every primitive — is `add.ts`.
 *
 * `layerProxy` normalises every object key with `methodId` (lower-cased, dots
 * removed), which is exactly what the gateway sends for `/rpc/kukum/handler/add`
 * (`kukum.handler.add` → the same key). Adding a primitive to the catalogue
 * therefore adds five routes with no new files — the same record-driven pattern
 * as `srv`'s subject orchestrator and `blong-mock`.
 */
 
/**
 * A registered method name → the injected library function implementing it.
 *
 * `kukum` is the namespace, not part of the endpoint's path, so the library
 * drops it: `kukum.source.get` is implemented by `lib.sourceGet`.
 */
const libName = (method: string): string => {
    const rest = method.slice('kukum'.length);
    return rest.charAt(0).toLowerCase() + rest.slice(1);
};
 
/** The registered methods that are not per-primitive. */
const FIXED_METHODS = [
    'kukumPrimitiveFind',
    'kukumActivationFind',
    'kukumMethodFind',
    'kukumTreeFind',
    'kukumSourceGet',
    'kukumSourceCheck',
    'kukumInstructionFind',
] as const;
 
export default handler<object, Record<string, unknown>>(({lib}) => {
    const methods_ = {} as Record<string, unknown>;
    const api = lib as unknown as Record<string, (...params: unknown[]) => unknown>;
 
    // ── meta / cross-cutting ────────────────────────────────────────────────
    for (const name of FIXED_METHODS) {
        methods_[name] = function (params: unknown) {
            return api[libName(name)](params);
        };
    }
 
    // ── per-primitive management endpoints ──────────────────────────────────
    for (const primitive of PRIMITIVES) {
        for (const predicate of PREDICATES) {
            methods_[methodName(primitive.id, predicate)] = function (params: unknown) {
                // Called ON `api` on purpose: the binding reads the platform and
                // registry off `this`, so extracting it into a local and calling
                // it bare would leave `this` undefined. The library name is the
                // predicate alone — one library serves every primitive, so the
                // segment that differs (the primitive id) is skipped.
                return api[predicate](primitive.id, params);
            };
        }
    }
 
    return methods_;
});