mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Changed identifier names cache structure to store multiple identifier caches
This commit is contained in:
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -1,7 +1,7 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
import { TIdentifierNamesCache } from '../../types/storages/TIdentifierNamesCache';
|
||||
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
|
||||
|
||||
import { IFileData } from '../../interfaces/cli/IFileData';
|
||||
|
||||
@@ -70,7 +70,10 @@ export class IdentifierNamesCacheUtils {
|
||||
|
||||
if (!fileData.content) {
|
||||
// Initial state of identifier names cache file
|
||||
return {};
|
||||
return {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { TIdentifierNamesCache } from '../../types/storages/TIdentifierNamesCache';
|
||||
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
|
||||
import { TOptionsPreset } from '../../types/options/TOptionsPreset';
|
||||
import { TStringArrayIndexesType } from '../../types/options/TStringArrayIndexesType';
|
||||
import { TStringArrayEncoding } from '../../types/options/TStringArrayEncoding';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { TIdentifierNamesCache } from '../../types/storages/TIdentifierNamesCache';
|
||||
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
|
||||
import { IInitializable } from '../IInitializable';
|
||||
|
||||
export interface IObfuscationResult extends IInitializable <[string, string]> {
|
||||
|
||||
@@ -1,10 +1,4 @@
|
||||
import { TIdentifierNamesCache } from '../../../types/storages/TIdentifierNamesCache';
|
||||
|
||||
import { IMapStorage } from '../IMapStorage';
|
||||
|
||||
export interface IIdentifierNamesCacheStorage extends Omit<IMapStorage <string, string>, 'getStorage'> {
|
||||
/**
|
||||
* @returns {TIdentifierNamesCache}
|
||||
*/
|
||||
getCache (): TIdentifierNamesCache;
|
||||
}
|
||||
// eslint-disable-next-line
|
||||
export interface IIdentifierNamesCacheStorage extends IMapStorage <string, string> {}
|
||||
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
ValidatorOptions
|
||||
} from 'class-validator';
|
||||
|
||||
import { TIdentifierNamesCache } from '../types/storages/TIdentifierNamesCache';
|
||||
import { TIdentifierNamesCache } from '../types/TIdentifierNamesCache';
|
||||
import { TInputOptions } from '../types/options/TInputOptions';
|
||||
import { TOptionsPreset } from '../types/options/TOptionsPreset';
|
||||
import { TRenamePropertiesMode } from '../types/options/TRenamePropertiesMode';
|
||||
@@ -46,7 +46,7 @@ import { HIGH_OBFUSCATION_PRESET } from './presets/HighObfuscation';
|
||||
|
||||
import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
|
||||
import { IsAllowedForObfuscationTargets } from './validators/IsAllowedForObfuscationTargets';
|
||||
import { IsPrimitiveDictionary } from './validators/IsPrimitiveDictionary';
|
||||
import { IsIdentifierNamesCache } from './validators/IsIdentifierNamesCache';
|
||||
|
||||
@injectable()
|
||||
export class Options implements IOptions {
|
||||
@@ -147,7 +147,7 @@ export class Options implements IOptions {
|
||||
/**
|
||||
* @type {TIdentifierNamesCache}
|
||||
*/
|
||||
@IsPrimitiveDictionary('string')
|
||||
@IsIdentifierNamesCache()
|
||||
public readonly identifierNamesCache!: TIdentifierNamesCache;
|
||||
|
||||
/**
|
||||
|
||||
+41
-16
@@ -1,24 +1,53 @@
|
||||
import equal from 'fast-deep-equal';
|
||||
import { registerDecorator, ValidationArguments, ValidationOptions } from 'class-validator';
|
||||
|
||||
import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache';
|
||||
import { TIdentifierNamesCacheDictionary } from '../../types/TIdentifierNamesCacheDictionary';
|
||||
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
|
||||
import { DEFAULT_PRESET } from '../presets/Default';
|
||||
|
||||
/**
|
||||
* @param {"string" | "number"} valuesType
|
||||
* @param value
|
||||
* @returns {boolean}
|
||||
*/
|
||||
const validateDictionary = (value: unknown | TIdentifierNamesCacheDictionary): boolean => {
|
||||
if (typeof value !== 'object') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const objectValues: unknown[] = Object.values(value);
|
||||
|
||||
if (!objectValues.length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (const objectValue of objectValues) {
|
||||
if (typeof objectValue !== 'string') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {ValidationOptions} validationOptions
|
||||
* @returns {(options: IOptions, propertyName: keyof IOptions) => void}
|
||||
*/
|
||||
export function IsPrimitiveDictionary (
|
||||
valuesType: 'string' | 'number',
|
||||
export function IsIdentifierNamesCache (
|
||||
validationOptions?: ValidationOptions
|
||||
): (options: IOptions, propertyName: keyof IOptions) => void {
|
||||
return (optionsObject: IOptions, propertyName: keyof IOptions): void => {
|
||||
registerDecorator({
|
||||
propertyName,
|
||||
constraints: [valuesType],
|
||||
name: 'IsPrimitiveDictionary',
|
||||
constraints: [],
|
||||
name: 'IsIdentifierNamesCache',
|
||||
options: validationOptions,
|
||||
target: optionsObject.constructor,
|
||||
validator: {
|
||||
@@ -27,7 +56,7 @@ export function IsPrimitiveDictionary (
|
||||
* @param {ValidationArguments} validationArguments
|
||||
* @returns {boolean}
|
||||
*/
|
||||
validate (value: IOptions[keyof IOptions], validationArguments: ValidationArguments): boolean {
|
||||
validate (value: unknown, validationArguments: ValidationArguments): boolean {
|
||||
const defaultValue: IOptions[keyof IOptions] | undefined = DEFAULT_PRESET[propertyName];
|
||||
const isDefaultValue: boolean = equal(value, defaultValue);
|
||||
|
||||
@@ -39,26 +68,22 @@ export function IsPrimitiveDictionary (
|
||||
return false;
|
||||
}
|
||||
|
||||
const objectValues: unknown[] = Object.values<unknown>(value);
|
||||
|
||||
if (!objectValues.length) {
|
||||
return true;
|
||||
if (value === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const objectValue of objectValues) {
|
||||
if (typeof objectValue !== 'string') {
|
||||
return false;
|
||||
}
|
||||
if (!validateDictionary((<TIdentifierNamesCache>value)?.globalIdentifiers)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return validateDictionary((<TIdentifierNamesCache>value)?.propertyIdentifiers);
|
||||
},
|
||||
|
||||
/**
|
||||
* @returns {string}
|
||||
*/
|
||||
defaultMessage (): string {
|
||||
return `Passed value must be a dictionary with \`${valuesType}\` values or \`null\` value`;
|
||||
return 'Passed value must be an identifier names cache object or `null` value';
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
import { inject, injectable } from 'inversify';
|
||||
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
|
||||
|
||||
import { TIdentifierNamesCache } from '../types/storages/TIdentifierNamesCache';
|
||||
import { TIdentifierNamesCache } from '../types/TIdentifierNamesCache';
|
||||
|
||||
import { ICryptUtils } from '../interfaces/utils/ICryptUtils';
|
||||
import { IIdentifierNamesCacheStorage } from '../interfaces/storages/identifier-names-cache/IIdentifierNamesCacheStorage';
|
||||
@@ -69,7 +69,14 @@ export class ObfuscationResult implements IObfuscationResult {
|
||||
* @returns {string}
|
||||
*/
|
||||
public getIdentifierNamesCache (): TIdentifierNamesCache {
|
||||
return this.identifierNamesCacheStorage.getCache();
|
||||
if (!this.options.identifierNamesCache) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
globalIdentifiers: this.identifierNamesCacheStorage.getStorageAsDictionary(),
|
||||
propertyIdentifiers: {}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { inject, injectable, postConstruct } from 'inversify';
|
||||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
|
||||
import { TIdentifierNamesCache } from '../../types/storages/TIdentifierNamesCache';
|
||||
|
||||
import { IIdentifierNamesCacheStorage } from '../../interfaces/storages/identifier-names-cache/IIdentifierNamesCacheStorage';
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
||||
@@ -26,17 +24,6 @@ export class IdentifierNamesCacheStorage extends MapStorage <string, string> imp
|
||||
public override initialize (): void {
|
||||
super.initialize();
|
||||
|
||||
this.storage = new Map(Object.entries(this.options.identifierNamesCache ?? {}));
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {TIdentifierNamesCache}
|
||||
*/
|
||||
public getCache (): TIdentifierNamesCache {
|
||||
if (!this.options.identifierNamesCache) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.getStorageAsDictionary();
|
||||
this.storage = new Map(Object.entries(this.options.identifierNamesCache?.globalIdentifiers ?? {}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { TIdentifierNamesCacheDictionary } from './TIdentifierNamesCacheDictionary';
|
||||
|
||||
export type TIdentifierNamesCache = {
|
||||
globalIdentifiers: TIdentifierNamesCacheDictionary;
|
||||
propertyIdentifiers: TIdentifierNamesCacheDictionary;
|
||||
} | null;
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
import { TDictionary } from './TDictionary';
|
||||
|
||||
export type TIdentifierNamesCacheDictionary = TDictionary<string>;
|
||||
@@ -1,3 +0,0 @@
|
||||
import { TDictionary } from '../TDictionary';
|
||||
|
||||
export type TIdentifierNamesCache = TDictionary<string> | null;
|
||||
+4
-1
@@ -15,7 +15,10 @@
|
||||
`,
|
||||
{
|
||||
compact: false,
|
||||
identifierNamesCache: { foo: '_0x5de86d', bar: '_0x2a943b' },
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: { foo: '_0x5de86d', bar: '_0x2a943b' },
|
||||
propertyIdentifiers: {}
|
||||
},
|
||||
renameGlobals: true
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { assert } from 'chai';
|
||||
|
||||
import { TDictionary } from '../../../src/types/TDictionary';
|
||||
import { TIdentifierNamesCache } from '../../../src/types/TIdentifierNamesCache';
|
||||
import { TInputOptions } from '../../../src/types/options/TInputOptions';
|
||||
import { TOptionsPreset } from '../../../src/types/options/TOptionsPreset';
|
||||
import { TTypeFromEnum } from '../../../src/types/utils/TTypeFromEnum';
|
||||
@@ -23,7 +24,6 @@ import { OptionsPreset } from '../../../src/enums/options/presets/OptionsPreset'
|
||||
import { buildLargeCode } from '../../helpers/buildLargeCode';
|
||||
import { getRegExpMatch } from '../../helpers/getRegExpMatch';
|
||||
import { readFileAsString } from '../../helpers/readFileAsString';
|
||||
import { TIdentifierNamesCache } from '../../../src/types/storages/TIdentifierNamesCache';
|
||||
|
||||
describe('JavaScriptObfuscator', () => {
|
||||
describe('obfuscate', () => {
|
||||
@@ -916,9 +916,12 @@ describe('JavaScriptObfuscator', () => {
|
||||
describe('identifier names cache generation', () => {
|
||||
describe('Variant #1: `identifierNamesCache` and `renameGlobal` options are enabled. Existing cache is passed', () => {
|
||||
const expectedIdentifierNamesCache: TIdentifierNamesCache = {
|
||||
foo: 'a',
|
||||
bar: 'b',
|
||||
baz: 'baz_value_from_cache'
|
||||
globalIdentifiers: {
|
||||
foo: 'a',
|
||||
bar: 'b',
|
||||
baz: 'baz_value_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
};
|
||||
|
||||
let identifierNamesCache: TIdentifierNamesCache;
|
||||
@@ -932,7 +935,10 @@ describe('JavaScriptObfuscator', () => {
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {
|
||||
baz: 'baz_value_from_cache'
|
||||
globalIdentifiers: {
|
||||
baz: 'baz_value_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
},
|
||||
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
|
||||
}
|
||||
@@ -946,8 +952,11 @@ describe('JavaScriptObfuscator', () => {
|
||||
|
||||
describe('Variant #2: `identifierNamesCache` and `renameGlobal` options are enabled', () => {
|
||||
const expectedIdentifierNamesCache: TIdentifierNamesCache = {
|
||||
foo: 'a',
|
||||
bar: 'b'
|
||||
globalIdentifiers: {
|
||||
foo: 'a',
|
||||
bar: 'b'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
};
|
||||
|
||||
let identifierNamesCache: TIdentifierNamesCache;
|
||||
@@ -960,7 +969,10 @@ describe('JavaScriptObfuscator', () => {
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {},
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
},
|
||||
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
|
||||
}
|
||||
).getIdentifierNamesCache();
|
||||
@@ -972,7 +984,10 @@ describe('JavaScriptObfuscator', () => {
|
||||
});
|
||||
|
||||
describe('Variant #3: `identifierNamesCache` and `renameGlobal` options are enabled. Source code without global variables', () => {
|
||||
const expectedIdentifierNamesCache: TIdentifierNamesCache = {};
|
||||
const expectedIdentifierNamesCache: TIdentifierNamesCache = {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
};
|
||||
|
||||
let identifierNamesCache: TIdentifierNamesCache;
|
||||
|
||||
@@ -984,7 +999,10 @@ describe('JavaScriptObfuscator', () => {
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {},
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
},
|
||||
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
|
||||
}
|
||||
).getIdentifierNamesCache();
|
||||
|
||||
+12
-3
@@ -22,7 +22,10 @@ describe('ScopeThroughIdentifiersTransformer ClassDeclaration identifiers', () =
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {
|
||||
'Foo': 'Foo_from_cache'
|
||||
globalIdentifiers: {
|
||||
'Foo': 'Foo_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
@@ -52,7 +55,10 @@ describe('ScopeThroughIdentifiersTransformer ClassDeclaration identifiers', () =
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {
|
||||
'Foo': 'Foo_from_cache'
|
||||
globalIdentifiers: {
|
||||
'Foo': 'Foo_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
@@ -76,7 +82,10 @@ describe('ScopeThroughIdentifiersTransformer ClassDeclaration identifiers', () =
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {}
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
+12
-3
@@ -22,7 +22,10 @@ describe('ScopeThroughIdentifiersTransformer FunctionDeclaration identifiers', (
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {
|
||||
'foo': 'foo_from_cache'
|
||||
globalIdentifiers: {
|
||||
'foo': 'foo_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
@@ -52,7 +55,10 @@ describe('ScopeThroughIdentifiersTransformer FunctionDeclaration identifiers', (
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {
|
||||
'foo': 'foo_from_cache'
|
||||
globalIdentifiers: {
|
||||
'foo': 'foo_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
@@ -76,7 +82,10 @@ describe('ScopeThroughIdentifiersTransformer FunctionDeclaration identifiers', (
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {}
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
+28
-7
@@ -22,7 +22,10 @@ describe('ScopeThroughIdentifiersTransformer VariableDeclaration identifiers', (
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {
|
||||
'foo': 'foo_from_cache'
|
||||
globalIdentifiers: {
|
||||
'foo': 'foo_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
@@ -53,7 +56,10 @@ describe('ScopeThroughIdentifiersTransformer VariableDeclaration identifiers', (
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {
|
||||
'foo': 'foo_from_cache'
|
||||
globalIdentifiers: {
|
||||
'foo': 'foo_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
@@ -77,7 +83,10 @@ describe('ScopeThroughIdentifiersTransformer VariableDeclaration identifiers', (
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {}
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
@@ -101,7 +110,10 @@ describe('ScopeThroughIdentifiersTransformer VariableDeclaration identifiers', (
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {
|
||||
'foo': 'foo_from_cache'
|
||||
globalIdentifiers: {
|
||||
'foo': 'foo_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
},
|
||||
reservedNames: ['^foo$']
|
||||
}
|
||||
@@ -129,7 +141,10 @@ describe('ScopeThroughIdentifiersTransformer VariableDeclaration identifiers', (
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {
|
||||
'foo': 'foo_from_cache'
|
||||
globalIdentifiers: {
|
||||
'foo': 'foo_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
@@ -153,7 +168,10 @@ describe('ScopeThroughIdentifiersTransformer VariableDeclaration identifiers', (
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {}
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
@@ -177,7 +195,10 @@ describe('ScopeThroughIdentifiersTransformer VariableDeclaration identifiers', (
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
renameGlobals: true,
|
||||
identifierNamesCache: {
|
||||
'foo': 'foo_from_cache'
|
||||
globalIdentifiers: {
|
||||
'foo': 'foo_from_cache'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
},
|
||||
reservedNames: ['^foo$']
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@ import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade
|
||||
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
|
||||
|
||||
describe('`identifierNamesCache` validation', () => {
|
||||
describe('IsPrimitiveDictionary', () => {
|
||||
describe('IsIdentifierNamesCache', () => {
|
||||
describe('Variant #1: positive validation', () => {
|
||||
describe('Variant #1: object with existing identifier names cache', () => {
|
||||
describe('Variant #1: object with existing identifier names cached', () => {
|
||||
let testFunc: () => string;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -16,7 +16,12 @@ describe('`identifierNamesCache` validation', () => {
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesCache: {
|
||||
foo: '_0x123456'
|
||||
globalIdentifiers: {
|
||||
foo: '_0x123456'
|
||||
},
|
||||
propertyIdentifiers: {
|
||||
bar: '_0x654321'
|
||||
}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
@@ -35,7 +40,12 @@ describe('`identifierNamesCache` validation', () => {
|
||||
'',
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesCache: {}
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {
|
||||
foo: '_0x123456'
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
@@ -45,7 +55,28 @@ describe('`identifierNamesCache` validation', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: `null` value', () => {
|
||||
describe('Variant #3: object with empty identifier names caches', () => {
|
||||
let testFunc: () => string;
|
||||
|
||||
beforeEach(() => {
|
||||
testFunc = () => JavaScriptObfuscator.obfuscate(
|
||||
'',
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should pass validation', () => {
|
||||
assert.doesNotThrow(testFunc);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #4: `null` value', () => {
|
||||
let testFunc: () => string;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -65,7 +96,7 @@ describe('`identifierNamesCache` validation', () => {
|
||||
});
|
||||
|
||||
describe('Variant #2: negative validation', () => {
|
||||
const expectedError: string = 'Passed value must be a dictionary with `string` values or `null` value';
|
||||
const expectedError: string = 'Passed value must be an identifier names cache object or `null` value';
|
||||
|
||||
describe('Variant #1: string value', () => {
|
||||
let testFunc: () => string;
|
||||
@@ -85,7 +116,7 @@ describe('`identifierNamesCache` validation', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #2: object with number values', () => {
|
||||
describe('Variant #2: cache with number values inside single cache', () => {
|
||||
let testFunc: () => string;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -94,8 +125,11 @@ describe('`identifierNamesCache` validation', () => {
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesCache: {
|
||||
foo: <any>1,
|
||||
bar: <any>2,
|
||||
globalIdentifiers: {
|
||||
foo: <any>1,
|
||||
bar: <any>2,
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
@@ -106,7 +140,7 @@ describe('`identifierNamesCache` validation', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: object with mixed values', () => {
|
||||
describe('Variant #3: cache with number values inside both caches', () => {
|
||||
let testFunc: () => string;
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -115,8 +149,61 @@ describe('`identifierNamesCache` validation', () => {
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesCache: {
|
||||
foo: <any>1,
|
||||
bar: '_0x1234567',
|
||||
globalIdentifiers: {
|
||||
foo: <any>1,
|
||||
bar: <any>2,
|
||||
},
|
||||
propertyIdentifiers: {
|
||||
baz: <any>3,
|
||||
bark: <any>4,
|
||||
}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should not pass validation', () => {
|
||||
assert.throws(testFunc, expectedError);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #4: cache with mixed values', () => {
|
||||
let testFunc: () => string;
|
||||
|
||||
beforeEach(() => {
|
||||
testFunc = () => JavaScriptObfuscator.obfuscate(
|
||||
'',
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: {
|
||||
foo: <any>1,
|
||||
bar: '_0x1234567',
|
||||
},
|
||||
propertyIdentifiers: {
|
||||
foo: '_0x123456'
|
||||
}
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
});
|
||||
|
||||
it('should not pass validation', () => {
|
||||
assert.throws(testFunc, expectedError);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #4: cache with nullable dictionary fields', () => {
|
||||
let testFunc: () => string;
|
||||
|
||||
beforeEach(() => {
|
||||
testFunc = () => JavaScriptObfuscator.obfuscate(
|
||||
'',
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
identifierNamesCache: {
|
||||
globalIdentifiers: <any>null,
|
||||
propertyIdentifiers: <any>null
|
||||
}
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
|
||||
+15
-69
@@ -5,7 +5,6 @@ import { assert } from 'chai';
|
||||
import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';
|
||||
|
||||
import { TDictionary } from '../../../../src/types/TDictionary';
|
||||
import { TIdentifierNamesCache } from '../../../../src/types/storages/TIdentifierNamesCache';
|
||||
|
||||
import { IIdentifierNamesCacheStorage } from '../../../../src/interfaces/storages/identifier-names-cache/IIdentifierNamesCacheStorage';
|
||||
import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
|
||||
@@ -54,7 +53,10 @@ describe('IdentifierNamesCacheStorage', () => {
|
||||
before(() => {
|
||||
storage = getStorageInstance({
|
||||
identifierNamesCache: {
|
||||
[storageKey]: storageValue
|
||||
globalIdentifiers: {
|
||||
[storageKey]: storageValue
|
||||
},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -71,93 +73,37 @@ describe('IdentifierNamesCacheStorage', () => {
|
||||
|
||||
let dictionary: TDictionary<string>;
|
||||
|
||||
before(() => {
|
||||
storage = getStorageInstance({
|
||||
identifierNamesCache: {}
|
||||
});
|
||||
|
||||
dictionary = storage.getStorageAsDictionary();
|
||||
});
|
||||
|
||||
it('should initialize storage with `identifierNamesStorage` option object', () => {
|
||||
assert.deepEqual(dictionary, expectedDictionary);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: `identifierNamesCache` option values is `null`', () => {
|
||||
const expectedDictionary: TDictionary<string> = {};
|
||||
|
||||
let dictionary: TDictionary<string>;
|
||||
|
||||
before(() => {
|
||||
storage = getStorageInstance({
|
||||
identifierNamesCache: null
|
||||
});
|
||||
|
||||
dictionary = storage.getStorageAsDictionary();
|
||||
});
|
||||
|
||||
it('should initialize storage with `identifierNamesStorage` option object', () => {
|
||||
assert.deepEqual(dictionary, expectedDictionary);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCache', () => {
|
||||
describe('Variant #1: `identifierNamesCache` option values is object', () => {
|
||||
const expectedIdentifierNamesCache: TIdentifierNamesCache = {
|
||||
[storageKey]: storageValue
|
||||
};
|
||||
|
||||
let identifierNamesCache: TIdentifierNamesCache;
|
||||
|
||||
before(() => {
|
||||
storage = getStorageInstance({
|
||||
identifierNamesCache: {
|
||||
[storageKey]: storageValue
|
||||
globalIdentifiers: {},
|
||||
propertyIdentifiers: {}
|
||||
}
|
||||
});
|
||||
|
||||
identifierNamesCache = storage.getCache();
|
||||
dictionary = storage.getStorageAsDictionary();
|
||||
});
|
||||
|
||||
it('should return cache object', () => {
|
||||
assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #2: `identifierNamesCache` option values is empty object', () => {
|
||||
const expectedIdentifierNamesCache: TIdentifierNamesCache = {};
|
||||
|
||||
let identifierNamesCache: TIdentifierNamesCache;
|
||||
|
||||
before(() => {
|
||||
storage = getStorageInstance({
|
||||
identifierNamesCache: {}
|
||||
});
|
||||
|
||||
identifierNamesCache = storage.getCache();
|
||||
});
|
||||
|
||||
it('should return empty cache object', () => {
|
||||
assert.deepEqual(identifierNamesCache, expectedIdentifierNamesCache);
|
||||
it('should initialize storage with `identifierNamesStorage` option object', () => {
|
||||
assert.deepEqual(dictionary, expectedDictionary);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: `identifierNamesCache` option values is `null`', () => {
|
||||
let identifierNamesCache: TIdentifierNamesCache;
|
||||
const expectedDictionary: TDictionary<string> = {};
|
||||
|
||||
let dictionary: TDictionary<string>;
|
||||
|
||||
before(() => {
|
||||
storage = getStorageInstance({
|
||||
identifierNamesCache: null
|
||||
});
|
||||
storage.set(storageKey, storageValue);
|
||||
|
||||
identifierNamesCache = storage.getCache();
|
||||
dictionary = storage.getStorageAsDictionary();
|
||||
});
|
||||
|
||||
it('should return `null`', () => {
|
||||
assert.isNull(identifierNamesCache);
|
||||
it('should initialize storage with `identifierNamesStorage` option object', () => {
|
||||
assert.deepEqual(dictionary, expectedDictionary);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user