• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

javascript-obfuscator / javascript-obfuscator / 13621494893

15 Jun 2024 05:29PM CUT coverage: 97.297%. Remained the same
13621494893

push

github

web-flow
Update CHANGELOG.md

2238 of 2373 branches covered (94.31%)

Branch coverage included in aggregate %.

5645 of 5729 relevant lines covered (98.53%)

33055580.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.31
/src/custom-code-helpers/AbstractCustomCodeHelper.ts
1
import { inject, injectable } from 'inversify';
6✔
2
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
6✔
3

4
import { TIdentifierNamesGeneratorFactory } from '../types/container/generators/TIdentifierNamesGeneratorFactory';
5
import { TStatement } from '../types/node/TStatement';
6

7
import { ICustomCodeHelper } from '../interfaces/custom-code-helpers/ICustomCodeHelper';
8
import { ICustomCodeHelperFormatter } from '../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
9
import { ICustomCodeHelperObfuscator } from '../interfaces/custom-code-helpers/ICustomCodeHelperObfuscator';
10
import { IIdentifierNamesGenerator } from '../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
11
import { IOptions } from '../interfaces/options/IOptions';
12
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
13

14
import { GlobalVariableTemplate1 } from './common/templates/GlobalVariableTemplate1';
6✔
15
import { GlobalVariableTemplate2 } from './common/templates/GlobalVariableTemplate2';
6✔
16
import { ObfuscationTarget } from '../enums/ObfuscationTarget';
6✔
17
import { GlobalVariableNoEvalTemplate } from './common/templates/GlobalVariableNoEvalTemplate';
6✔
18
import { GlobalVariableServiceWorkerTemplate } from './common/templates/GlobalVariableServiceWorkerTemplate';
6✔
19

20
@injectable()
21
export abstract class AbstractCustomCodeHelper <
6✔
22
    TInitialData extends unknown[] = unknown[]
23
> implements ICustomCodeHelper <TInitialData> {
24
    /**
25
     * @type {string[]}
26
     */
27
    private static readonly globalVariableTemplateFunctions: string[] = [
6✔
28
        GlobalVariableTemplate1(),
29
        GlobalVariableTemplate2()
30
    ];
31

32
    /**
33
     * @type {TStatement[] | null}
34
     */
35
    protected cachedNode: TStatement[] | null = null;
200,278✔
36

37
    /**
38
     * @type {ICustomCodeHelperFormatter}
39
     */
40
    protected readonly customCodeHelperFormatter: ICustomCodeHelperFormatter;
41

42
    /**
43
     * @type {ICustomCodeHelperObfuscator}
44
     */
45
    protected readonly customCodeHelperObfuscator: ICustomCodeHelperObfuscator;
46

47
    /**
48
     * @type {IIdentifierNamesGenerator}
49
     */
50
    protected readonly identifierNamesGenerator: IIdentifierNamesGenerator;
51

52
    /**
53
     * @type {IOptions}
54
     */
55
    protected readonly options: IOptions;
56

57
    /**
58
     * @type {IRandomGenerator}
59
     */
60
    protected readonly randomGenerator: IRandomGenerator;
61

62
    /**
63
     * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
64
     * @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
65
     * @param {ICustomCodeHelperObfuscator} customCodeHelperObfuscator
66
     * @param {IRandomGenerator} randomGenerator
67
     * @param {IOptions} options
68
     */
69
    public constructor (
70
        @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
71
            identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
72
        @inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
73
        @inject(ServiceIdentifiers.ICustomCodeHelperObfuscator) customCodeHelperObfuscator: ICustomCodeHelperObfuscator,
74
        @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
75
        @inject(ServiceIdentifiers.IOptions) options: IOptions
76
    ) {
77
        this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
200,278✔
78
        this.customCodeHelperFormatter = customCodeHelperFormatter;
200,278✔
79
        this.customCodeHelperObfuscator = customCodeHelperObfuscator;
200,278✔
80
        this.randomGenerator = randomGenerator;
200,278✔
81
        this.options = options;
200,278✔
82
    }
83

84
    /**
85
     * @returns {TStatement[]}
86
     */
87
    public getNode (): TStatement[] {
88
        if (!this.cachedNode) {
199,360!
89
            const codeHelperTemplate: string = this.getCodeHelperTemplate();
199,360✔
90

91
            this.cachedNode = this.customCodeHelperFormatter.formatStructure(
199,360✔
92
                this.getNodeStructure(codeHelperTemplate)
93
            );
94
        }
95

96
        return this.cachedNode;
199,360✔
97
    }
98

99
    /**
100
     * @returns {string}
101
     */
102
    protected getGlobalVariableTemplate (): string {
103
        switch (this.options.target) {
1,596✔
104
            case ObfuscationTarget.BrowserNoEval:
1,596!
105
                return GlobalVariableNoEvalTemplate();
×
106
            case ObfuscationTarget.ServiceWorker:
107
                return GlobalVariableServiceWorkerTemplate();
×
108
            default:
109
                return this.randomGenerator
1,596✔
110
                    .getRandomGenerator()
111
                    .pickone(AbstractCustomCodeHelper.globalVariableTemplateFunctions);
112
        }
113
    }
114

115
    /**
116
     * @returns {string}
117
     */
118
    protected getCodeHelperTemplate (): string {
119
        return '';
×
120
    }
121

122
    /**
123
     * @param {TInitialData} args
124
     */
125
    public abstract initialize (...args: TInitialData): void;
126

127
    /**
128
     * @returns {TStatement[]}
129
     */
130
    protected abstract getNodeStructure (codeHelperTemplate: string): TStatement[];
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc