Skip to content

Commit

Permalink
Merge pull request #15 from Ruben-Arushanyan/14-v4.2.0
Browse files Browse the repository at this point in the history
14 v4.2.0
  • Loading branch information
Ruben-Arushanyan authored Aug 18, 2022
2 parents f400fb2 + e32dc75 commit 1889670
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 299 deletions.
71 changes: 39 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Secure EventEmitter
# Secure Event Emitter

**secure-event-emitter** is a tiny javascript package that uses restrict rules and mechanisms to build safer and protected event-driven architecture. It's similar to nodejs [EventEmitter](https://nodejs.org/api/events.html), but dictates stricter rules to prevent misuse.

Expand All @@ -10,64 +10,71 @@

## Usage

### basic usage
### Class: `SecureEventEmitter`

> ### `new SecureEventEmitter(types, emitterKey, [validator])`
- `types` **string[]** : All event types․
- `emitterKey` **string | symbol** : Emitter Key: Without which we cannot perform **.emit()**
- `validator` **function**: Function for validating emitted values

```js
import {SecureEventEmitter} from 'Libs/secure-event-emitter'
import {SecureEventEmitter} from 'secure-event-emitter'

// create emitterKey
const emitterKey = Symbol('My Emitter Key')
const emitterKey = Symbol()

// create myEmitter instance
const myEmitter = new SecureEventEmitter(
['abc', 'xyz'], // all event types
emitterKey // emitter key is an any Symbol type value
['event-1', 'event-2'], // all event types
emitterKey // emitter key is an any Symbol or String type value
)

// add listeners
myEmitter.on('abc', (a, b) => {
// ...
myEmitter.on('event-1', (a, b) => {
console.log(a, b)
})
myEmitter.on('xyz', (x) => {
// ...
myEmitter.on('event-2', (x) => {
console.log(x)
})

myEmitter.unlock(emitterKey).emit('abc', 2021, 2022)
myEmitter.unlock(emitterKey).emit('xyz', 11)
myEmitter.unlock(emitterKey).emit('event-1', 2021, 2022)
myEmitter.unlock(emitterKey).emit('event-2', 123)

```

### validatorFunction usage
### Validator Function

```js
import {SecureEventEmitter} from 'Libs/secure-event-emitter'
import {SecureEventEmitter} from 'secure-event-emitter'

// create emitterKey
const emitterKey = Symbol('My Emitter Key')
const validatorFunction = (payload) => {
if (typeof payload === 'number') {
return
const emitterKey = Symbol()

// create validator function
const validator = (x) => {
if (typeof x !== 'number') {
return 'Can emit only numbers!' // error message
}
return 'My Emitter payload can be an only number type' // error message
}

// create myEmitter instance
const myEmitter = new SecureEventEmitter(
['abc', 'xyz'], // all event types
emitterKey, // emitter key is an any Symbol type value
validatorFunction,
['event-1', 'event-2'],
emitterKey,
validator,
)

// add listeners
myEmitter.on('abc', (x) => {
// ...
myEmitter.on('event-1', (x) => {
console.log(x)
})
myEmitter.on('xyz', (x) => {
// ...
myEmitter.on('event-2', (x) => {
console.log(x)
})

myEmitter.unlock(emitterKey).emit('abc', 2021)
myEmitter.unlock(emitterKey).emit('xyz', '2021') // TypeError: My Emitter payload can be an only number type
myEmitter.unlock(emitterKey).emit('event-1', 2021)
myEmitter.unlock(emitterKey).emit('event-2', '2021') // TypeError: Can emit only numbers!

```

Expand All @@ -78,7 +85,7 @@ myEmitter.unlock(emitterKey).emit('xyz', '2021') // TypeError: My Emitter payloa
### basic usage

```js
import {SingularEventEmitter} from 'Libs/secure-event-emitter'
import {SingularEventEmitter} from 'secure-event-emitter'

// create emitterKey
const emitterKey = Symbol('My Singular Emitter Key')
Expand Down Expand Up @@ -109,7 +116,7 @@ onFoo.unlock(emitterKey).emit(2022)
### basic usage

```js
import {Payload} from 'Libs/secure-event-emitter'
import {Payload} from 'secure-event-emitter'

// first argument is an origin, can be only symbol type and required
const payload_1 = new Payload(Symbol('My Origin 1'), 1, 2, 3)
Expand Down Expand Up @@ -141,7 +148,7 @@ const payload_2 = new Payload(Symbol('My Origin 2'), 'a', 'b')
### basic usage

```js
import {Handler, Payload} from 'Libs/secure-event-emitter'
import {Handler, Payload} from 'secure-event-emitter'

const myHandler = Handler((payload) => {
// ...
Expand All @@ -160,7 +167,7 @@ myHandler(2021) // TypeError('handler argument type must be a [[Payload]] class
### basic usage

```js
import {useListener} from 'Libs/secure-event-emitter/react'
import {useListener} from 'secure-event-emitter/react'

// ...
// ...
Expand Down
File renamed without changes.
14 changes: 2 additions & 12 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ const {
isArray,
isUndefined,
isFunction,
isDescribedSymbol,
isString,
isValidEventType,
} = require('./utils');

const error_if_invalid_emitterKey = (emitterKey) => {
if (!isSymbol(emitterKey)) {
throw new TypeError('SecureEventEmitter emitterKey must be a Symbol type');
}

if (!isDescribedSymbol(emitterKey)) {
throw new TypeError('SecureEventEmitter emitterKey symbol description is required. Try Symbol(description)');
if (!isSymbol(emitterKey) && !isString(emitterKey)) {
throw new TypeError('SecureEventEmitter emitterKey must be a Symbol or String type');
}
};

Expand All @@ -40,10 +34,6 @@ const error_if_invalid_eventType = (eventType) => {
if (!isString(eventType)) {
throw new TypeError(`eventType must be a string. \n\n Invalid eventType:\n ${eventType}\n\n`);
}

if (!isValidEventType(eventType)) {
throw new TypeError(`eventType must be a valid formated string(lowercase Letters and Hyphen): ${eventType}\n`);
}
};

const error_if_eventType_not_exist = (eventType, eventTypes) => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

const {Handler} = require('./handler')
const {SecureEventEmitter} = require('./secure-event-emitter')
const {SecureEventEmitter} = require('./SecureEventEmitter')
const {SingularEventEmitter} = require('./singular-event-emitter')
const {Payload} = require('./payload')

Expand Down
2 changes: 1 addition & 1 deletion src/singular-event-emitter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {SecureEventEmitter} = require('./secure-event-emitter');
const {SecureEventEmitter} = require('./SecureEventEmitter');

const EVENT_TYPE = 'singular-event';

Expand Down
2 changes: 0 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const isSymbol = (x) => typeof x === 'symbol';
const isDescribedSymbol = (x) => (isSymbol(x) && x.description);
const isObject = (x) => (typeof x === 'object') && (x !== null);
const isSubset = (setArr, subsetArr) => subsetArr.every(val => setArr.includes(val));
const isValidEventType = (x) => isString(x) && /^[a-z]+(-[a-z]+)*$/.test(x);
const isObservable = (x) => isObject(x) && isFunction(x.on) && isFunction(x.off);

module.exports = {
Expand All @@ -20,6 +19,5 @@ module.exports = {
isObject,
isSubset,
isDescribedSymbol,
isValidEventType,
isObservable,
};
8 changes: 2 additions & 6 deletions test/basic.test.js → test/SecureEventEmitter/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@


////////////////////////// TEST ////////////////////////////
const {
SecureEventEmitter,
} = require('../.packed')
const {SecureEventEmitter} = require('../../.packed')



test('Exceptions for [on] method', () => {
test('SecureEventEmitter Basic Test', () => {
const emitterKey = Symbol('my emitter key')
let emitter = new SecureEventEmitter(
[
Expand Down
43 changes: 43 additions & 0 deletions test/SecureEventEmitter/exceptions/constructor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const {SecureEventEmitter} = require('../../../.packed')

test('SecureEventEmitter - exceptions [constructor]', () => {
expect(() => new SecureEventEmitter()).toThrow(/SecureEventEmitter eventTypes must be an array/)
expect(() => new SecureEventEmitter([])).toThrow(/SecureEventEmitter eventTypes must be a not empty array/)

expect(() => new SecureEventEmitter([4])).toThrow(/eventType must be a string/)
expect(() => new SecureEventEmitter([true])).toThrow(/eventType must be a string/)
expect(() => new SecureEventEmitter([{a: "hello"}])).toThrow(/eventType must be a string/)


expect(() => new SecureEventEmitter(
["ab-ab", "ab"],
{},
)).toThrow(/SecureEventEmitter emitterKey must be a Symbol or String type/)
expect(() => new SecureEventEmitter(
["ab-ab", "ab"],
45
)).toThrow(/SecureEventEmitter emitterKey must be a Symbol or String type/)

expect(() => new SecureEventEmitter(
["ab-ab", "ab"],
Symbol("fdfd fdfd fdf"),
45
)).toThrow(/SecureEventEmitter validatorFunction must be a function/)

expect(new SecureEventEmitter(
["ab-ab", "ab"],
Symbol("fdfd fdfd fdf"),
))

expect(() => new SecureEventEmitter(
["ab-ab", "ab"],
Symbol("fdfd fdfd fdf"),
{}
)).toThrow(/SecureEventEmitter validatorFunction must be a function/)

expect(new SecureEventEmitter(
["ab-ab", "ab"],
Symbol("fdfd fdfd fdf"),
() => {}
))
})
25 changes: 25 additions & 0 deletions test/SecureEventEmitter/exceptions/emit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const {SecureEventEmitter} = require('../../../.packed')

test('SecureEventEmitter - exceptions - [emit]', () => {
const emitterKey = Symbol('my emitter key')
const emitter = new SecureEventEmitter(
[
'xxx',
'yyy',
'zzz'
],
emitterKey
)

const x = emitter.unlock(emitterKey)

expect(() => x.emit()).toThrow(/eventType must be a string/)
expect(() => x.emit(45)).toThrow(/eventType must be a string/)
expect(() => x.emit({})).toThrow(/eventType must be a string/)


expect(() => x.emit('yy')).toThrow(/not exist/)
expect(x.emit('yyy'))
expect(x.emit('yyy', 45))
expect(x.emit('yyy', {}, {}))
})
24 changes: 24 additions & 0 deletions test/SecureEventEmitter/exceptions/off.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const {SecureEventEmitter} = require('../../../.packed')

test('SecureEventEmitter - exceptions - [off]', () => {
const emitter = new SecureEventEmitter(
[
'xxx',
'yyy',
'zzz'
],
'my emitter key'
)


expect(() => emitter.off()).toThrow(/eventType must be a string/)
expect(() => emitter.off(45)).toThrow(/eventType must be a string/)
expect(() => emitter.off({})).toThrow(/eventType must be a string/)

expect(() => emitter.off("aa")).toThrow(/not exist/)

expect(() => emitter.off("zzz")).toThrow(/listener must be a function/)
expect(() => emitter.off("zzz", {})).toThrow(/listener must be a function/)
expect(() => emitter.off("zzz", true)).toThrow(/listener must be a function/)
expect(emitter.off("zzz", () => {}))
})
23 changes: 23 additions & 0 deletions test/SecureEventEmitter/exceptions/on.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const {SecureEventEmitter} = require('../../../.packed')

test('SecureEventEmitter - exceptions - [on]', () => {
const emitter = new SecureEventEmitter(
[
'xxx',
'yyy',
'zzz'
],
'my emitter key'
)

expect(() => emitter.on()).toThrow(/eventType must be a string/)
expect(() => emitter.on(45)).toThrow(/eventType must be a string/)
expect(() => emitter.on({})).toThrow(/eventType must be a string/)

expect(() => emitter.on("aa")).toThrow(/not exist/)

expect(() => emitter.on("zzz")).toThrow(/listener must be a function/)
expect(() => emitter.on("zzz", {})).toThrow(/listener must be a function/)
expect(() => emitter.on("zzz", true)).toThrow(/listener must be a function/)
expect(emitter.on("zzz", () => {}))
})
21 changes: 21 additions & 0 deletions test/SecureEventEmitter/exceptions/unlock.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const {SecureEventEmitter} = require('../../../.packed')

test('SecureEventEmitter - exceptions - [unlock]', () => {
const emitterKey = Symbol('my emitter key')
const emitter = new SecureEventEmitter(
[
'xxx',
'yyy',
'zzz'
],
emitterKey
)


expect(() => emitter.unlock()).toThrow(/Unlock Failed/)
expect(() => emitter.unlock(Symbol())).toThrow(/Unlock Failed/)
expect(() => emitter.unlock(Symbol('my emitter key'))).toThrow(/Unlock Failed/)
expect(() => emitter.unlock(44)).toThrow(/Unlock Failed/)

expect(emitter.unlock(emitterKey))
})
Loading

0 comments on commit 1889670

Please sign in to comment.