All files / test/ctp/adapter/payshield encode.ts

85% Statements 68/80
54.54% Branches 6/11
100% Functions 1/1
85% Lines 68/80

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 811x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x     1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                 2x 2x 2x  
import {handler, type Errors, type IContext, type ILogger, type IMeta} from '@feasibleone/blong';
import {createRequire} from 'node:module';
import bitsyntax from 'ut-bitsyntax';
 
export default handler(({config, lib, lib: {lmk, upperCaseObject, mask}}) => {
    const errors = lib.errors as unknown as Errors<{
        'payshield.parser.request': unknown;
        'payshield.parser.parserResponse': unknown;
        'payshield.parser.header': unknown;
        'payshield.parser.body': unknown;
        'payshield.notImplemented': unknown;
    }>;
    const commands = lib.commands as unknown as Record<
        string,
        {
            pattern: {name: string; size: number}[];
            code: string;
            warnings?: string[];
            method: string;
            mtid: string;
        }
    >;
    const nonCorrectableFields = Object.assign(
        {},
        createRequire(import.meta.url)('./fields.json'),
        config.nonCorrectableFields,
    );
    const headerPattern = bitsyntax.parse(
        'headerNo:' + config.headerFormat + ', code:2/string, body/binary',
    );
 
    if (headerPattern === false) {
        throw errors['payshield.parser.header']({});
    }
 
    const headerNoSize = headerPattern.filter(value => value.name === 'headerNo').pop()!.size;
    const maxTrace = parseInt('9'.repeat(headerNoSize));
 
    return function encode(
        data: object,
        $meta: IMeta,
        context: IContext & {trace: number},
        log: ILogger,
    ) {
        const commandName = $meta.method!.split('.').pop() + ':' + $meta.mtid;
 
        if (commands[commandName] === undefined)
            throw errors['payshield.notImplemented']({params: {opcode: commandName}});
 
        let headerNo = $meta.mtid === 'request' ? null : $meta.trace;
        if (headerNo === undefined || headerNo === null) {
            headerNo = $meta.trace = ('0'.repeat(headerNoSize) + context.trace).substr(
                -headerNoSize,
            );
            context.trace += 1;
            if (context.trace > maxTrace) {
                context.trace = 0;
            }
        }
        const dataCorrected = upperCaseObject(lmk(data), nonCorrectableFields);
        const bodyBuff = bitsyntax.build(commands[commandName].pattern, dataCorrected);
        if (!bodyBuff) throw errors['payshield.parser.body']({params: {command: commandName}});
 
        const buffer = bitsyntax.build(headerPattern, {
            headerNo,
            code: commands[commandName].code,
            body: bodyBuff,
        });
        log?.trace?.({
            $meta: {mtid: 'frame', method: 'payshield.encode'},
            message: mask(buffer.toString(), dataCorrected, {
                pattern: commands[commandName].pattern,
                maskedKeys: config.maskedKeys,
                maskSymbol: '*',
            }),
            log: context?.session?.log,
        });
        return buffer;
    };
});