inversify test

This commit is contained in:
sanex3339
2016-11-26 16:35:48 +03:00
parent dc37399c8d
commit 8f92b7eca2
14 changed files with 685 additions and 420 deletions
+540 -376
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -1,5 +1,6 @@
"use strict";
import 'reflect-metadata';
import { JavaScriptObfuscator } from './src/JavaScriptObfuscator';
if (!(<any>global)._babelPolyfill) {
+2
View File
@@ -27,8 +27,10 @@
"escodegen": "1.8.1",
"esprima": "3.1.1",
"estraverse": "4.2.0",
"inversify": "^3.0.0-beta.2",
"is-equal": "^1.5.3",
"mkdirp": "0.5.1",
"reflect-metadata": "^0.1.8",
"source-map-support": "0.4.6",
"string-template": "^1.0.0"
},
+23 -14
View File
@@ -1,19 +1,20 @@
import { InversifyContainerFacade } from "./container/InversifyContainerFacade";
import { ServiceIdentifiers } from './container/ServiceIdentifiers';
import * as esprima from 'esprima';
import * as escodegen from 'escodegen';
import * as ESTree from 'estree';
import { Chance } from 'chance';
import { IGeneratorOutput } from './interfaces/IGeneratorOutput';
import { IObfuscationResult } from './interfaces/IObfuscationResult';
import { IObfuscator } from './interfaces/IObfuscator';
import { IGeneratorOutput } from './interfaces/IGeneratorOutput';
import { IInputOptions } from './interfaces/IInputOptions';
import { IOptions } from './interfaces/IOptions';
import { CustomNodesStorage } from './storages/custom-nodes/CustomNodesStorage';
import { ObfuscationEventEmitter } from './event-emitters/ObfuscationEventEmitter';
import { ObfuscationResult } from './ObfuscationResult';
import { Obfuscator } from './Obfuscator';
import { SourceMapCorrector } from './SourceMapCorrector';
import { StackTraceAnalyzer } from './stack-trace-analyzer/StackTraceAnalyzer';
import { Utils } from './Utils';
export class JavaScriptObfuscatorInternal {
@@ -32,16 +33,29 @@ export class JavaScriptObfuscatorInternal {
loc: true
};
/**
* @types {InversifyContainerFacade}
*/
private readonly inversifyContainerFacade: InversifyContainerFacade;
/**
* @types {IObfuscator}
*/
private readonly obfuscator: IObfuscator;
/**
* @type {IOptions}
*/
private readonly options: IOptions;
/**
* @param options
* @param inputOptions
*/
constructor (options: IOptions) {
this.options = options;
constructor (inputOptions: IInputOptions) {
this.inversifyContainerFacade = new InversifyContainerFacade(inputOptions);
this.obfuscator = this.inversifyContainerFacade.get<IObfuscator>(ServiceIdentifiers.IObfuscator);
this.options = this.inversifyContainerFacade.get<IOptions>(ServiceIdentifiers.IOptions);
}
/**
@@ -98,12 +112,7 @@ export class JavaScriptObfuscatorInternal {
const astTree: ESTree.Program = esprima.parse(sourceCode, JavaScriptObfuscatorInternal.esprimaParams);
// obfuscate AST tree
const obfuscatedAstTree: ESTree.Program = new Obfuscator(
new ObfuscationEventEmitter(),
new StackTraceAnalyzer(),
new CustomNodesStorage(this.options),
this.options
).obfuscateAstTree(astTree);
const obfuscatedAstTree: ESTree.Program = this.obfuscator.obfuscateAstTree(astTree);
// generate code
const generatorOutput: IGeneratorOutput = this.generateCode(sourceCode, obfuscatedAstTree);
+8 -4
View File
@@ -1,3 +1,6 @@
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from './container/ServiceIdentifiers';
import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
@@ -21,6 +24,7 @@ import { NodeObfuscatorsFactory } from './node-transformers/node-obfuscators/fac
import { Node } from './node/Node';
import { NodeUtils } from './node/NodeUtils';
@injectable()
export class Obfuscator implements IObfuscator {
/**
* @type {IStorage<ICustomNode>}
@@ -49,10 +53,10 @@ export class Obfuscator implements IObfuscator {
* @param options
*/
constructor (
obfuscationEventEmitter: IObfuscationEventEmitter,
stackTraceAnalyzer: IStackTraceAnalyzer,
customNodesStorage: IStorage<ICustomNode>,
options: IOptions
@inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
@inject(ServiceIdentifiers.IStackTraceAnalyzer) stackTraceAnalyzer: IStackTraceAnalyzer,
@inject(ServiceIdentifiers.IStorage) customNodesStorage: IStorage<ICustomNode>,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
this.obfuscationEventEmitter = obfuscationEventEmitter;
this.stackTraceAnalyzer = stackTraceAnalyzer;
+66
View File
@@ -0,0 +1,66 @@
import { Container, interfaces } from 'inversify';
import { ServiceIdentifiers } from './ServiceIdentifiers';
import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
import { IInputOptions } from '../interfaces/IInputOptions';
import { IObfuscationEventEmitter } from '../interfaces/IObfuscationEventEmitter';
import { IObfuscator } from '../interfaces/IObfuscator';
import { IOptions } from '../interfaces/IOptions';
import { IStackTraceAnalyzer } from '../interfaces/stack-trace-analyzer/IStackTraceAnalyzer';
import { IStorage } from '../interfaces/IStorage';
import { CustomNodesStorage } from '../storages/custom-nodes/CustomNodesStorage';
import { ObfuscationEventEmitter } from '../event-emitters/ObfuscationEventEmitter';
import { Obfuscator } from '../Obfuscator';
import { Options } from "../options/Options";
import { StackTraceAnalyzer } from '../stack-trace-analyzer/StackTraceAnalyzer';
export class InversifyContainerFacade {
/**
* @type {interfaces.Container}
*/
private container: interfaces.Container;
constructor (options: IInputOptions) {
this.container = new Container();
// IOptions
this.container
.bind<IOptions>(ServiceIdentifiers.IOptions)
.toDynamicValue(() => {
return new Options(options);
})
.inSingletonScope();
// IObfuscator
this.container
.bind<IObfuscator>(ServiceIdentifiers.IObfuscator)
.to(Obfuscator);
// IObfuscationEventEmitter
this.container
.bind<IObfuscationEventEmitter>(ServiceIdentifiers.IObfuscationEventEmitter)
.to(ObfuscationEventEmitter)
.inSingletonScope();
// IStackTraceAnalyzer
this.container
.bind<IStackTraceAnalyzer>(ServiceIdentifiers.IStackTraceAnalyzer)
.to(StackTraceAnalyzer)
.inSingletonScope();
// IStorage<ICustomNode>
this.container
.bind<IStorage<ICustomNode>>(ServiceIdentifiers.IStorage)
.to(CustomNodesStorage)
.inSingletonScope();
}
/**
* @param serviceIdentifier
* @returns {T}
*/
public get <T> (serviceIdentifier: interfaces.ServiceIdentifier<T>) {
return this.container.get<T>(serviceIdentifier);
}
}
+9
View File
@@ -0,0 +1,9 @@
let ServiceIdentifiers = {
IObfuscationEventEmitter: Symbol('IObfuscationEventEmitter'),
IObfuscator: Symbol('IObfuscator'),
IOptions: Symbol('IOptions'),
IStackTraceAnalyzer: Symbol('IStackTraceAnalyzer'),
IStorage: Symbol('IStorage')
};
export { ServiceIdentifiers };
@@ -1,5 +1,10 @@
import { decorate, injectable } from 'inversify';
import { IObfuscationEventEmitter } from '../interfaces/IObfuscationEventEmitter';
import { EventEmitter } from 'events';
export class ObfuscationEventEmitter extends EventEmitter implements IObfuscationEventEmitter {}
decorate(injectable(), EventEmitter);
@injectable()
export class ObfuscationEventEmitter extends EventEmitter implements IObfuscationEventEmitter {}
+1 -9
View File
@@ -1,13 +1,5 @@
import * as ESTree from 'estree';
import { ICustomNode } from './custom-nodes/ICustomNode';
import { IObfuscationEventEmitter } from './IObfuscationEventEmitter';
import { IStorage } from './IStorage';
export interface IObfuscator {
obfuscateAstTree (
astTree: ESTree.Program,
obfuscationEventEmitter: IObfuscationEventEmitter,
customNodesStorage: IStorage<ICustomNode>
): ESTree.Program;
obfuscateAstTree (astTree: ESTree.Program): ESTree.Program;
}
+16 -13
View File
@@ -1,17 +1,19 @@
import { injectable } from 'inversify';
import {
ArrayUnique,
IsBoolean,
IsArray,
IsIn,
IsNumber,
IsString,
IsUrl,
Min,
Max,
ValidateIf,
validateSync,
ValidationError,
ValidatorOptions
ArrayUnique,
IsBoolean,
IsArray,
IsIn,
IsNumber,
IsString,
IsUrl,
Min,
Max,
ValidateIf,
validateSync,
ValidationError,
ValidatorOptions
} from 'class-validator';
import { IInputOptions } from '../interfaces/IInputOptions';
@@ -25,6 +27,7 @@ import { DEFAULT_PRESET } from '../preset-options/DefaultPreset';
import { OptionsNormalizer } from './OptionsNormalizer';
import { ValidationErrorsFormatter } from './ValidationErrorsFormatter';
@injectable()
export class Options implements IOptions {
/**
* @type {ValidatorOptions}
@@ -1,3 +1,5 @@
import { injectable } from 'inversify';
import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
@@ -47,6 +49,7 @@ import { NodeUtils } from '../node/NodeUtils';
* }
* ]
*/
@injectable()
export class StackTraceAnalyzer implements IStackTraceAnalyzer {
/**
* @type {number}
+3
View File
@@ -1,7 +1,10 @@
import { injectable } from 'inversify';
import { IStorage } from '../interfaces/IStorage';
import { Utils } from '../Utils';
@injectable()
export abstract class MapStorage <T> implements IStorage <T> {
/**
* @type {Map <string | number, T>}
@@ -1,3 +1,6 @@
import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import { TCustomNodesFactory } from '../../types/TCustomNodesFactory';
import { ICustomNode } from '../../interfaces/custom-nodes/ICustomNode';
@@ -11,6 +14,7 @@ import { MapStorage } from '../MapStorage';
import { SelfDefendingCustomNodesFactory } from '../../custom-nodes/self-defending-nodes/factory/SelfDefendingCustomNodesFactory';
import { StringArrayCustomNodesFactory } from '../../custom-nodes/string-array-nodes/factory/StringArrayCustomNodesFactory';
@injectable()
export class CustomNodesStorage extends MapStorage <ICustomNode> {
/**
* @type {TCustomNodesFactory[]}
@@ -31,7 +35,7 @@ export class CustomNodesStorage extends MapStorage <ICustomNode> {
/**
* @param options
*/
constructor (options: IOptions) {
constructor (@inject(ServiceIdentifiers.IOptions) options: IOptions) {
super();
this.options = options;
+2 -2
View File
@@ -1767,7 +1767,7 @@
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
//// Types
//// ServiceIdentifiers
var TypeModifier;
(function (TypeModifier) {
TypeModifier[TypeModifier["Const"] = 0] = "Const";
@@ -11471,7 +11471,7 @@
return CompilerConfig;
}());
/**
* Types used for the renderer.
* ServiceIdentifiers used for the renderer.
* Can be replaced to specialize the generated output to a specific renderer
* to help tree shaking.
*/