Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat: Updated dependencies, fixed types issues, added ServerMiddlewar…
Browse files Browse the repository at this point in the history
…e interface, improved library structure (#99)
  • Loading branch information
serhiisol authored Sep 5, 2018
1 parent d358de4 commit 264be93
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 73 deletions.
5 changes: 3 additions & 2 deletions playground/socket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Controller,
Socket,
Args,
ServerMiddleware,
Middleware,
Event,
IO_MIDDLEWARE
Expand All @@ -14,7 +15,7 @@ import { Container, Injectable, Inject, InjectionToken } from '@decorators/di';
const server = listen(3000);
const MESSAGE = new InjectionToken('MESSAGE');

class ServerMiddleware implements Middleware {
class GlobalMiddleware implements ServerMiddleware {
public use(io, socket, next) {
console.log('ServerMiddleware');
next();
Expand Down Expand Up @@ -49,7 +50,7 @@ class ConnectionController {

Container.provide([
{ provide: MESSAGE, useValue: 'Socket welcomes user' },
{ provide: IO_MIDDLEWARE, useClass: ServerMiddleware }
{ provide: IO_MIDDLEWARE, useClass: GlobalMiddleware }
]);

attachControllers(server, [ ConnectionController ]);
13 changes: 8 additions & 5 deletions playground/socket/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"name": "@decorators/playground-socket",
"version": "3.0.0",
"version": "3.1.0",
"description": "node decorators",
"main": "index.js",
"dependencies": {
"@decorators/di": ">=1.0.2",
"@decorators/socket": "../../socket",
"@types/socket.io": "1.4.29",
"typescript": "2.6.1"
"socket.io": ">=2.1.1"
},
"devDependencies": {
"@types/socket.io": ">=1.4.36",
"typescript": ">=2.9.2"
},
"keywords": [
"nodejs",
Expand All @@ -20,7 +23,7 @@
"url": "https://github.com/serhiiso/node-decorators/issues"
},
"engines": {
"node": ">=6.3.0"
"node": ">=7.1.0"
},
"repository": {
"type": "git",
Expand Down
6 changes: 6 additions & 0 deletions socket/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Socket#3.1.0
* Changed library structure
* Fixed interfaces
* Added new interface to implement for server middleware `ServerMiddleware`, controller `Middleware` interface remains the same
* Updated versions of dependencies, like `socket.io` and `@decorators/di`

# Socket#3.0.2
* Fix sorting parameters when applying events

Expand Down
19 changes: 16 additions & 3 deletions socket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,32 @@ class ControllerMiddleware implements Middleware {
}
}
```
To register global middleware handler, use **IO_MIDDLEWARE** injection token with **Container** from `@decorators/di` package, like so:
To register global middleware handler create a class, that implements interface **ServerMiddleware**, like so:
```typescript
class GlobalMiddleware implements ServerMiddleware {
public use(
io: SocketIO.Server | SocketIO.Namespace,
socket: SocketIO.Socket,
next: Function
) {
console.log('GlobalMiddleware');
next();
}
}
```

Then register it by using **IO_MIDDLEWARE** injection token with **Container** from `@decorators/di` package, like so:

```typescript
Container.provide([
{ provide: IO_MIDDLEWARE, useClass: ServerMiddleware }
{ provide: IO_MIDDLEWARE, useClass: GlobalMiddleware }
]);
```

### Details
#### Middleware
The middleware order :
* Global Server Middleware (**io.use(...)**)
* Global Socket middleware (**socket.use(...)**)
* Controller based middleware (**@Controller(...)**)
* Event based middleware (**@Event(...)**)

Expand Down
1 change: 1 addition & 0 deletions socket/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src';
18 changes: 9 additions & 9 deletions socket/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "@decorators/socket",
"version": "3.0.2",
"version": "3.1.0",
"description": "node decorators - decorators for socket.io library",
"main": "src/index.js",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"dependencies": {
"socket.io": ">=2.0.4",
"@decorators/di": ">=1.0.0"
"socket.io": ">=2.1.1",
"@decorators/di": ">=1.0.2"
},
"devDependencies": {
"@types/socket.io": ">=1.4.31",
"typescript": ">=2.6.1"
"@types/socket.io": ">=1.4.36",
"typescript": ">=2.9.2"
},
"keywords": [
"nodejs",
Expand All @@ -23,14 +24,13 @@
"url": "https://github.com/serhiiso/node-decorators/issues"
},
"engines": {
"node": ">=6.3.0"
"node": ">=7.1.0"
},
"repository": {
"type": "git",
"url": "https://github.com/serhiiso/node-decorators.git"
},
"scripts": {
"compile": "tsc",
"clean-tsc": "find . -name \"*.js\" -type f -not -path \"./node_modules/*\" -delete && find . -name \"*.js.map\" -type f -not -path \"./node_modules/*\" -delete && find . -name \"*.d.ts\" -type f -not -path \"./node_modules/*\" -not -path \"./types/*\" -delete"
"compile": "tsc"
}
}
6 changes: 3 additions & 3 deletions socket/src/decorators/controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { getMeta, SocketMeta } from '../meta';
import { Middleware } from '../middleware';
import { Type } from '../middleware';

/**
* Defines namespace for the controller and controller-based middleware
*
* @param {string} namespace
* @param {Middleware[]} [middleware]
* @param {Type[]} [middleware]
*/
export function Controller(namespace: string, middleware?: Middleware[]) {
export function Controller(namespace: string, middleware?: Type[]) {
return (target): void => {
const meta: SocketMeta = getMeta(target.prototype);

Expand Down
22 changes: 11 additions & 11 deletions socket/src/decorators/events.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getMeta, SocketClass, SocketMeta, EventType } from '../meta';
import { Middleware } from '../middleware';
import { Type } from '../middleware';

/**
* Listener decorator factory, creates listener decorator
*
* @param {EventType} type Listener type: io or socket
* @param {string} event Event name
* @param {Middleware[]} middleware Event middleware
* @param {Type[]} middleware Event middleware
*/
function makeDecorator(type: EventType, event: string, middleware?: Middleware[]) {
function makeDecorator(type: EventType, event: string, middleware?: Type[]) {
return (target: SocketClass, methodName: string, descriptor?: any) => {
const meta: SocketMeta = getMeta(target);

Expand All @@ -23,9 +23,9 @@ function makeDecorator(type: EventType, event: string, middleware?: Middleware[]
*
* @see **io.on('connection', fn)**
*
* @param {Middleware[]} middleware Event middleware
* @param {Type[]} middleware Event middleware
*/
export function Connection(middleware?: Middleware[]) {
export function Connection(middleware?: Type[]) {
return makeDecorator(EventType.IO, 'connection', middleware);
}
/**
Expand All @@ -39,9 +39,9 @@ export const Connect = Connection;
* @see **io.on**
*
* @param {string} event
* @param {Middleware[]} middleware Event middleware
* @param {Type[]} middleware Event middleware
*/
export function GlobalEvent(event: string, middleware?: Middleware[]) {
export function GlobalEvent(event: string, middleware?: Type[]) {
return makeDecorator(EventType.IO, event, middleware);
}

Expand All @@ -50,9 +50,9 @@ export function GlobalEvent(event: string, middleware?: Middleware[]) {
*
* @see **socket.on('disconnect', fn)**
*
* @param {Middleware[]} middleware Event middleware
* @param {Type[]} middleware Event middleware
*/
export function Disconnect(middleware?: Middleware[]) {
export function Disconnect(middleware?: Type[]) {
return makeDecorator(EventType.Socket, 'disconnect', middleware);
}

Expand All @@ -62,8 +62,8 @@ export function Disconnect(middleware?: Middleware[]) {
* @see **socket.on**
*
* @param {string} event
* @param {Middleware[]} middleware Event middleware
* @param {Type[]} middleware Event middleware
*/
export function Event(event: string, middleware?: Middleware[]) {
export function Event(event: string, middleware?: Type[]) {
return makeDecorator(EventType.Socket, event, middleware);
};
17 changes: 3 additions & 14 deletions socket/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
export {
Controller,
Connection,
Connect,
Disconnect,
GlobalEvent,
Event,
IO,
Socket,
Args,
Ack
} from './decorators';
export { IO_MIDDLEWARE, Middleware } from './middleware';
export { attachControllers } from './socket';
export * from './decorators';
export * from './middleware';
export * from './socket';
10 changes: 4 additions & 6 deletions socket/src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Middleware } from './middleware';
import { Type } from './middleware';

/**
* Parameter types enum
Expand Down Expand Up @@ -38,7 +38,7 @@ export interface Listener {
methodName: string;
event: string;
type: EventType;
middleware: Middleware[];
middleware: Type[];
}

/**
Expand All @@ -56,7 +56,7 @@ export class SocketMeta {
/**
* Middleware
*/
middleware: Middleware[] = [];
middleware: Type[] = [];

/**
* Event listeners
Expand All @@ -77,9 +77,7 @@ export class SocketMeta {
* @extends {Object}
*/
export interface SocketClass extends Object {
__socket_meta__: SocketMeta;

new (...deps: any[]);
__socket_meta__?: SocketMeta;
}

/**
Expand Down
40 changes: 30 additions & 10 deletions socket/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,57 @@ import { Container, InjectionToken } from '@decorators/di';
*/
export const IO_MIDDLEWARE = new InjectionToken('IO_MIDDLEWARE');

export interface Type extends Function {
new (...args: any[]);
}

export type NextFunction = (err?: Error) => void;

/**
* Server Middleware class interface
*
* @export
* @interface ServerMiddleware
*/
export interface ServerMiddleware {
use(
io: SocketIO.Server | SocketIO.Namespace,
socket: SocketIO.Socket,
next: NextFunction
): void;
}

/**
* Middleware class interface
*
* @export
* @interface Middleware
*/
export interface Middleware {
new (...args: any[]);

use(...args: any[]): void;
use(
io: SocketIO.Server | SocketIO.Namespace,
socket: SocketIO.Socket,
args: any,
next: NextFunction
): void;
}

/**
* Create request middleware handler that uses class or function provided as middleware
*
* @export
* @param {Middleware} middleware
* @param {Type | InjectionToken} middleware
*
* @returns {RequestHandler}
*/
export function middlewareHandler(middleware: Middleware | InjectionToken) {
export function middlewareHandler(middleware: Type | InjectionToken) {
return function(...args: any[]): any {
let instance: Middleware;
let instance: Middleware | ServerMiddleware;

try {
instance = Container.get(middleware);
} catch (e) {
instance = new (middleware as Middleware)();
instance = new (middleware as Type)();
}

return instance.use.apply(instance, args);
Expand All @@ -44,13 +65,12 @@ export function middlewareHandler(middleware: Middleware | InjectionToken) {
/**
* Loops through all registered middlewares
*
* @description middleware approach
* @param {MiddlewareFunction[]} fns Array of middleware functions
* @param {Type[]} middleware Array of middleware
* @param {any[]} [args = []] Arguments to pass in
*
* @returns {Promise<*>}
*/
export function executeMiddleware(middleware: Middleware[], args: any[] = []): Promise<any> {
export function executeMiddleware(middleware: Type[], args: any[] = []): Promise<any> {
function iteratee(done: (err: Error) => void, i = 0) {
try {
middlewareHandler(middleware[i])(...args, (err) => {
Expand Down
Loading

0 comments on commit 264be93

Please sign in to comment.