All files / src/container/modules/node-transformers NodeObfuscatorsModule.ts

100% Statements 21/21
100% Branches 2/2
100% Functions 3/3
100% Lines 21/21
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 531x 1x       1x   1x 1x 1x 1x   1x 2628x   2628x       2628x       2628x       2628x       2628x   11446x   11446x 80181x 65146x     15035x           15035x   15035x        
import { ContainerModule, interfaces } from 'inversify';
import { ServiceIdentifiers } from '../../ServiceIdentifiers';
 
import { IReplacer } from '../../../interfaces/node-transformers/IReplacer';
 
import { NodeObfuscatorsReplacers } from '../../../enums/container/NodeObfuscatorsReplacers';
 
import { BooleanLiteralReplacer } from '../../../node-transformers/node-obfuscators/replacers/BooleanLiteralReplacer';
import { IdentifierReplacer } from '../../../node-transformers/node-obfuscators/replacers/IdentifierReplacer';
import { NumberLiteralReplacer } from '../../../node-transformers/node-obfuscators/replacers/NumberLiteralReplacer';
import { StringLiteralReplacer } from '../../../node-transformers/node-obfuscators/replacers/StringLiteralReplacer';
 
export const nodeObfuscatorsModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
    const nodeObfuscatorsReplacersTag: string = 'nodeObfuscatorsReplacers';
 
    bind<IReplacer>(ServiceIdentifiers.IReplacer)
        .to(BooleanLiteralReplacer)
        .whenTargetTagged(nodeObfuscatorsReplacersTag, NodeObfuscatorsReplacers.BooleanReplacer);
 
    bind<IReplacer>(ServiceIdentifiers.IReplacer)
        .to(IdentifierReplacer)
        .whenTargetTagged(nodeObfuscatorsReplacersTag, NodeObfuscatorsReplacers.IdentifierReplacer);
 
    bind<IReplacer>(ServiceIdentifiers.IReplacer)
        .to(NumberLiteralReplacer)
        .whenTargetTagged(nodeObfuscatorsReplacersTag, NodeObfuscatorsReplacers.NumberLiteralReplacer);
 
    bind<IReplacer>(ServiceIdentifiers.IReplacer)
        .to(StringLiteralReplacer)
        .whenTargetTagged(nodeObfuscatorsReplacersTag, NodeObfuscatorsReplacers.StringLiteralReplacer);
 
    bind<IReplacer>(ServiceIdentifiers['Factory<IReplacer>'])
        .toFactory<IReplacer>((context: interfaces.Context) => {
            const cache: Map <NodeObfuscatorsReplacers, IReplacer> = new Map <NodeObfuscatorsReplacers, IReplacer> ();
 
            return (replacer: NodeObfuscatorsReplacers) => {
                if (cache.has(replacer)) {
                    return <IReplacer>cache.get(replacer);
                }
 
                const obfuscationReplacer: IReplacer = context.container.getTagged<IReplacer>(
                    ServiceIdentifiers.IReplacer,
                    nodeObfuscatorsReplacersTag,
                    replacer
                );
 
                cache.set(replacer, obfuscationReplacer);
 
                return obfuscationReplacer;
            };
        });
});