Skip to content

Commit

Permalink
fix: event-stream vulnerability
Browse files Browse the repository at this point in the history
  • Loading branch information
luchsamapparat committed Nov 27, 2018
1 parent a2e33b4 commit da98370
Show file tree
Hide file tree
Showing 8 changed files with 617 additions and 74 deletions.
89 changes: 16 additions & 73 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"babel-plugin-transform-es2015-modules-commonjs": "6.26.2",
"faker": "4.1.0",
"jest": "23.4.1",
"npm-run-all": "4.1.3",
"npm-run-all": "4.1.5",
"rimraf": "2.6.2",
"standard-version": "4.4.0",
"ts-jest": "23.0.0",
Expand Down
122 changes: 122 additions & 0 deletions src/temp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { commerce, random } from 'faker';
import { flow, isFunction, isUndefined, mapValues } from 'lodash';

// tslint:disable:no-invalid-this

type Transformation<T> = (value: T) => T;
type AddTransformation<T = any> = (transformation: Transformation<T>) => void;

const resourceBuilder = (addTransformation: AddTransformation) => {
return {
toResource(id: string) {
addTransformation(
(currentValue: any) => ({
...currentValue,
id
})
);
return this;
}
};
};

function createBlueprintBuilder<T>(blueprintFn: Blueprint<T> | BlueprintFactory<T>): (addTransformation: AddTransformation) => BlueprintBuilderMethods<T> {
const blueprint = isFunction(blueprintFn) ? blueprintFn() : blueprintFn;
return (addTransformation: AddTransformation) => generateBlueprintBuilderMethods(blueprint, addTransformation);
}

interface Product {
name: string;
price: number;
}

const productBlueprint: Blueprint<Product> = {
name: () => commerce.productName(),
price: () => random.number({ min: 0.01, max: 99.99, precision: 0.01 })
};

const productBuilder = createBlueprintBuilder(productBlueprint);

type BuilderFactory<BT> = (addTransformation: AddTransformation) => Builder<BT>;

type Builder<T> = { build(): any } & {
[P in keyof T]: T[P] extends (...args: infer R) => any ? (...args: R) => Builder<T> : () => Builder<T>
};


function combineBuilders<BT1, B1 extends BuilderFactory<BT1>, BT2, B2 extends BuilderFactory<BT2>>(builder1: B1, builder2: B2): Builder<ReturnType<B1> & ReturnType<B2>> {
let transformations: Transformation<any>[] = [];

const addTransformation = (transformationFn: Transformation<any>) => {
transformations = [
...transformations,
transformationFn
];
};

return {
...<any> builder1(addTransformation),
...<any> builder2(addTransformation),
build() {
return flow(transformations)({});
}
};
}

const builder = combineBuilders(
productBuilder,
resourceBuilder
);

console.log(builder
.fake()
.name('Cars')
.toResource('id')
.build());



type Blueprint<T> = {
[P in keyof T]: () => T[P]
};

type BlueprintFactory<T> = () => Blueprint<T>;

type BlueprintBuilderMethod<V, T> = (value: V) => BlueprintBuilderMethods<T>;

type BlueprintBuilderMethods<T> = { fake(): void } & {
[P in keyof T]: BlueprintBuilderMethod<T[P], T>;
};

function fromBlueprint<T>(blueprint: Blueprint<T>, values?: Partial<T>): T {
return {
...<any> mapValues<Blueprint<T>, any>(
blueprint,
fn => fn()
),
...<any> (isUndefined(values) ? {} : values)
};
}

function generateBlueprintBuilderMethods<T>(blueprint: Blueprint<T>, addTransformation: AddTransformation): BlueprintBuilderMethods<T> {
return {
fake() {
addTransformation(() => fromBlueprint(blueprint));
return this;
},
...<any> mapValues(
blueprint,
(blueprintFn, prop) => generateBlueprintBuilderMethod(prop, addTransformation)
)
};
}

function generateBlueprintBuilderMethod<T>(prop: keyof T, addTransformation: AddTransformation): BlueprintBuilderMethod<T[keyof T], T> {
return function (this: BlueprintBuilderMethods<T>, value: T[keyof T]) {
addTransformation((currentValue: T) => ({
...<any> currentValue,
[prop]: value
}));
return this;
};
}
83 changes: 83 additions & 0 deletions src/temp2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { flow } from 'lodash';

// tslint:disable:no-invalid-this

type Transformation<CVT, RVT> = (value: CVT) => RVT;

type AddTransformation<CVT, RVT = CVT> = (transformation: Transformation<CVT, RVT>) => void;

type BuilderPluginMethods<BP> = {
[M in keyof BP]: BP[M] extends (...args: infer R) => any ? (...args: R) => BuilderPluginMethods<BP> : (...args: any[]) => BuilderPluginMethods<BP>
};

type BuilderPlugin<CVT = any, RVT = any> = (addTransformation: AddTransformation<CVT, RVT>) => BuilderPluginMethods<ReturnType<BuilderPlugin<CVT, RVT>>>;

interface Builder<T> {
addPlugin<CVT, RVT, BP extends BuilderPlugin<CVT, RVT>>(plugin: BP & BuilderPlugin<CVT, RVT>): ReturnType<BP> & Builder<T & RVT> & this;
build(): T;
}

type BuilderFactory<T = {}> = (initialValue?: T) => Builder<T>;

/* ---------- */

export interface Resource {
_id: string;
}

interface Product {
name: string;
price: number;
}

/* ---------- */

const resourceBuilderPlugin = (addTransformation: AddTransformation<any, Resource>) => ({
toResource(id: string) {
addTransformation(currentValue => ({
...currentValue,
_id: id
}));
return this;
}
});

const productBuilderPlugin = (addTransformation: AddTransformation<any, Product>) => ({
toProduct() {
addTransformation(currentValue => ({
...currentValue,
name: 'name',
price: 1.23
}));
return this;
}
});

const builder: BuilderFactory = (initialValue = {}) => {
let transformations: Transformation<any, any>[] = [];

const addTransformation = (transformationFn: Transformation<any, any>) => {
transformations = [
...transformations,
transformationFn
];
};

return {
addPlugin<CVT, RVT>(plugin: BuilderPlugin<CVT, RVT>) {
return <any> {
...this,
...plugin(addTransformation)
};
},
build() {
return flow(transformations)(initialValue);
}
};
};

const productBuilder = builder()
.addPlugin(resourceBuilderPlugin)
.addPlugin(productBuilderPlugin);

const product = productBuilder.build();
Loading

0 comments on commit da98370

Please sign in to comment.