All files / blong-gogo/src handlerProxy.ts

56.09% Statements 92/164
66.66% Branches 10/15
75% Functions 6/8
56.09% Lines 92/164

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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 1651x 1x 1x 1x 1x 1x 1x 1x 68x 68x 68x 68x 68x 68x 68x 68x 1x 1x 1x 1x 1x 1x 1x 1x 1x 82x 82x 82x 82x 82x 82x 82x 82x 82x 290x 290x 290x 290x 290x 290x 290x 290x 5x 5x 5x 5x 4x 4x 4x 4x 4x 5x 5x 290x 68x 68x 68x 5x 5x 5x   5x 5x 5x 5x   5x 5x 5x 5x 5x 68x 68x 222x 222x 290x 290x                                                   290x 290x 290x                                                                     290x 290x 290x 290x 290x 290x 290x                       290x 135x 135x 290x 290x 82x 82x  
import type {Adapter, ILib, IMeta} from '@feasibleone/blong/types';
import merge from 'ut-function.merge';
 
import {camelToSentence, parseAnnotatedKey} from './lib.ts';
 
/**
 * Rename a function's `.name` property.
 */
function rename<T>(value: string, fn: T): T {
    Object.defineProperty(fn, 'name', {
        value,
        configurable: true,
        enumerable: false,
    });
    return fn;
}
 
/**
 * Create the handler proxy — the IoC mechanism that resolves handler calls
 * at runtime through the registry (local or remote).
 *
 * This is the most important abstraction in the framework: handlers never
 * import each other directly; they access dependencies through this proxy.
 */
export default function createHandlerProxy(
    local: object,
    port: Adapter,
    remote: (methodName: string) => () => unknown,
    attachCheckpoint: ((meta: IMeta) => void) | undefined,
    lib: ILib,
    mergedConfig: Record<string, unknown>,
): object {
    return new Proxy(local, {
        get(target: unknown, handlerName: string) {
            if (typeof handlerName !== 'string') return undefined;
 
            function resolveHandler(
                resolvedName: string,
            ): (...params: unknown[]) => unknown {
                let fn: (() => unknown) | undefined;
                const sentence = camelToSentence(resolvedName);
                function nameSteps(result: unknown): unknown {
                    if (
                        Array.isArray(result) &&
                        !(result as {name?: string}).name
                    ) {
                        Object.defineProperty(result, 'name', {
                            value: sentence,
                            configurable: true,
                        });
                    }
                    return result;
                }
                if (port.handles?.(resolvedName)) {
                    return rename(
                        resolvedName,
                        function (...params: unknown[]) {
                            fn ||= port.findHandler?.(resolvedName) as (() => unknown) | undefined;
                            if (!fn)
                                throw new Error(
                                    `Handler '${resolvedName}' not found`,
                                );
                            const $meta =
                                params.length > 1
                                    ? (params[1] as IMeta)
                                    : undefined;
                            if ($meta && typeof $meta === 'object') {
                                attachCheckpoint?.($meta);
                            }
                            return nameSteps(fn.apply(port, params as []));
                        },
                    );
                }
                return remote(resolvedName);
            }
 
            function wrapWithMeta(
                baseFn: (...params: unknown[]) => unknown,
                metaOverrides: Record<string, unknown>,
                aliasName?: string,
            ): (...params: unknown[]) => unknown {
                return rename(
                    aliasName || baseFn.name,
                    function (...params: unknown[]) {
                        const $meta =
                            params.length > 1
                                ? (params[1] as IMeta)
                                : undefined;
                        if ($meta && typeof $meta === 'object') {
                            merge($meta, metaOverrides);
                        }
                        const result = baseFn(...params);
                        if (Array.isArray(result) && metaOverrides.name) {
                            Object.defineProperty(result, 'name', {
                                value: metaOverrides.name,
                                configurable: true,
                            });
                        }
                        return result;
                    },
                );
            }
 
            // Approach 2: Annotation syntax
            if (handlerName.startsWith('@')) {
                const parsed = parseAnnotatedKey(handlerName);
                const baseFn = resolveHandler(parsed.handlerName);
                const metaOverrides: Record<string, unknown> = {};
                for (const ann of parsed.annotations) {
                    const hasKeyValue = ann.params.some(p => p.includes('='));
                    if (ann.params.length > 0 && !hasKeyValue) {
                        // Mode A: $meta injection
                        metaOverrides[ann.name] = ann.params.join(' ');
                    } else {
                        // Mode B: config-object reference with deep merge
                        const handlerConfig = mergedConfig?.handler as
                            | Record<string, unknown>
                            | undefined;
                        const configObj = handlerConfig?.[ann.name];
                        if (configObj && typeof configObj === 'object') {
                            merge(
                                metaOverrides,
                                configObj as Record<string, unknown>,
                            );
                        }
                        for (const p of ann.params) {
                            const eqIdx = p.indexOf('=');
                            if (eqIdx > 0) {
                                lib.setProperty(
                                    metaOverrides,
                                    p.slice(0, eqIdx),
                                    p.slice(eqIdx + 1),
                                );
                            }
                        }
                    }
                }
                return wrapWithMeta(baseFn, metaOverrides);
            }
 
            // Resolve handler (local or remote)
            const resolved = resolveHandler(handlerName);
 
            // Approach 1: Wrap with naming proxy for sub-property destructuring
            return new Proxy(resolved, {
                get(proxyTarget, prop, receiver) {
                    if (typeof prop !== 'string' || prop in proxyTarget) {
                        return Reflect.get(proxyTarget, prop, receiver);
                    }
                    return wrapWithMeta(
                        proxyTarget as (...params: unknown[]) => unknown,
                        {
                            name: camelToSentence(prop),
                        },
                        prop,
                    );
                },
                apply(proxyTarget, thisArg, args) {
                    return Reflect.apply(proxyTarget, thisArg, args);
                },
            });
        },
    });
}