Skip to content

Commit f32bcf2

Browse files
committed
Changed identifier names cache structure to store multiple identifier caches
1 parent 51d4ea0 commit f32bcf2

File tree

21 files changed

+267
-151
lines changed

21 files changed

+267
-151
lines changed

dist/index.browser.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/utils/IdentifierNamesCacheUtils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33

4-
import { TIdentifierNamesCache } from '../../types/storages/TIdentifierNamesCache';
4+
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
55

66
import { IFileData } from '../../interfaces/cli/IFileData';
77

@@ -70,7 +70,10 @@ export class IdentifierNamesCacheUtils {
7070

7171
if (!fileData.content) {
7272
// Initial state of identifier names cache file
73-
return {};
73+
return {
74+
globalIdentifiers: {},
75+
propertyIdentifiers: {}
76+
};
7477
}
7578

7679
try {

src/interfaces/options/IOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TIdentifierNamesCache } from '../../types/storages/TIdentifierNamesCache';
1+
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
22
import { TOptionsPreset } from '../../types/options/TOptionsPreset';
33
import { TStringArrayIndexesType } from '../../types/options/TStringArrayIndexesType';
44
import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';

src/interfaces/source-code/IObfuscationResult.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TIdentifierNamesCache } from '../../types/storages/TIdentifierNamesCache';
1+
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
22
import { IInitializable } from '../IInitializable';
33

44
export interface IObfuscationResult extends IInitializable <[string, string]> {
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import { TIdentifierNamesCache } from '../../../types/storages/TIdentifierNamesCache';
2-
31
import { IMapStorage } from '../IMapStorage';
42

5-
export interface IIdentifierNamesCacheStorage extends Omit<IMapStorage <string, string>, 'getStorage'> {
6-
/**
7-
* @returns {TIdentifierNamesCache}
8-
*/
9-
getCache (): TIdentifierNamesCache;
10-
}
3+
// eslint-disable-next-line
4+
export interface IIdentifierNamesCacheStorage extends IMapStorage <string, string> {}

src/options/Options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
ValidatorOptions
1919
} from 'class-validator';
2020

21-
import { TIdentifierNamesCache } from '../types/storages/TIdentifierNamesCache';
21+
import { TIdentifierNamesCache } from '../types/TIdentifierNamesCache';
2222
import { TInputOptions } from '../types/options/TInputOptions';
2323
import { TOptionsPreset } from '../types/options/TOptionsPreset';
2424
import { TRenamePropertiesMode } from '../types/options/TRenamePropertiesMode';
@@ -46,7 +46,7 @@ import { HIGH_OBFUSCATION_PRESET } from './presets/HighObfuscation';
4646

4747
import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
4848
import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';
49-
import { IsPrimitiveDictionary } from './validators/IsPrimitiveDictionary';
49+
import { IsIdentifierNamesCache } from './validators/IsIdentifierNamesCache';
5050

5151
@injectable()
5252
export class Options implements IOptions {
@@ -147,7 +147,7 @@ export class Options implements IOptions {
147147
/**
148148
* @type {TIdentifierNamesCache}
149149
*/
150-
@IsPrimitiveDictionary('string')
150+
@IsIdentifierNamesCache()
151151
public readonly identifierNamesCache!: TIdentifierNamesCache;
152152

153153
/**

src/options/validators/IsPrimitiveDictionary.ts renamed to src/options/validators/IsIdentifierNamesCache.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,53 @@
11
import equal from 'fast-deep-equal';
22
import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';
33

4+
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
5+
import { TIdentifierNamesCacheDictionary } from '../../types/TIdentifierNamesCacheDictionary';
6+
47
import { IOptions } from '../../interfaces/options/IOptions';
58

69
import { DEFAULT_PRESET } from '../presets/Default';
710

811
/**
9-
* @param {"string" | "number"} valuesType
12+
* @param value
13+
* @returns {boolean}
14+
*/
15+
const validateDictionary = (value: unknown | TIdentifierNamesCacheDictionary): boolean => {
16+
if (typeof value !== 'object') {
17+
return false;
18+
}
19+
20+
if (value === null) {
21+
return false;
22+
}
23+
24+
const objectValues: unknown[] = Object.values(value);
25+
26+
if (!objectValues.length) {
27+
return true;
28+
}
29+
30+
for (const objectValue of objectValues) {
31+
if (typeof objectValue !== 'string') {
32+
return false;
33+
}
34+
}
35+
36+
return true;
37+
};
38+
39+
/**
1040
* @param {ValidationOptions} validationOptions
1141
* @returns {(options: IOptions, propertyName: keyof IOptions) => void}
1242
*/
13-
export function IsPrimitiveDictionary (
14-
valuesType: 'string' | 'number',
43+
export function IsIdentifierNamesCache (
1544
validationOptions?: ValidationOptions
1645
): (options: IOptions, propertyName: keyof IOptions) => void {
1746
return (optionsObject: IOptions, propertyName: keyof IOptions): void => {
1847
registerDecorator({
1948
propertyName,
20-
constraints: [valuesType],
21-
name: 'IsPrimitiveDictionary',
49+
constraints: [],
50+
name: 'IsIdentifierNamesCache',
2251
options: validationOptions,
2352
target: optionsObject.constructor,
2453
validator: {
@@ -27,7 +56,7 @@ export function IsPrimitiveDictionary (
2756
* @param {ValidationArguments} validationArguments
2857
* @returns {boolean}
2958
*/
30-
validate (value: IOptions[keyof IOptions], validationArguments: ValidationArguments): boolean {
59+
validate (value: unknown, validationArguments: ValidationArguments): boolean {
3160
const defaultValue: IOptions[keyof IOptions] | undefined = DEFAULT_PRESET[propertyName];
3261
const isDefaultValue: boolean = equal(value, defaultValue);
3362

@@ -39,26 +68,22 @@ export function IsPrimitiveDictionary (
3968
return false;
4069
}
4170

42-
const objectValues: unknown[] = Object.values<unknown>(value);
43-
44-
if (!objectValues.length) {
45-
return true;
71+
if (value === null) {
72+
return false;
4673
}
4774

48-
for (const objectValue of objectValues) {
49-
if (typeof objectValue !== 'string') {
50-
return false;
51-
}
75+
if (!validateDictionary((<TIdentifierNamesCache>value)?.globalIdentifiers)) {
76+
return false;
5277
}
5378

54-
return true;
79+
return validateDictionary((<TIdentifierNamesCache>value)?.propertyIdentifiers);
5580
},
5681

5782
/**
5883
* @returns {string}
5984
*/
6085
defaultMessage (): string {
61-
return `Passed value must be a dictionary with \`${valuesType}\` values or \`null\` value`;
86+
return 'Passed value must be an identifier names cache object or `null` value';
6287
}
6388
}
6489
});

src/source-code/ObfuscationResult.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { inject, injectable } from 'inversify';
22
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
33

4-
import { TIdentifierNamesCache } from '../types/storages/TIdentifierNamesCache';
4+
import { TIdentifierNamesCache } from '../types/TIdentifierNamesCache';
55

66
import { ICryptUtils } from '../interfaces/utils/ICryptUtils';
77
import { IIdentifierNamesCacheStorage } from '../interfaces/storages/identifier-names-cache/IIdentifierNamesCacheStorage';
@@ -69,7 +69,14 @@ export class ObfuscationResult implements IObfuscationResult {
6969
* @returns {string}
7070
*/
7171
public getIdentifierNamesCache (): TIdentifierNamesCache {
72-
return this.identifierNamesCacheStorage.getCache();
72+
if (!this.options.identifierNamesCache) {
73+
return null;
74+
}
75+
76+
return {
77+
globalIdentifiers: this.identifierNamesCacheStorage.getStorageAsDictionary(),
78+
propertyIdentifiers: {}
79+
};
7380
}
7481

7582
/**

0 commit comments

Comments
 (0)