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

Commit

Permalink
Socket 2.0.0 (#43)
Browse files Browse the repository at this point in the history
* feat(socket): Refactoring for 2.0.0

* chore(socket): Fixed empty middleware issue && adjusted example accordingly

* chore(socket): Small polishing, plus added WrapperClass option for IO Decorator
  • Loading branch information
serhiisol authored Apr 24, 2017
1 parent 89a0223 commit 3715ce3
Show file tree
Hide file tree
Showing 23 changed files with 758 additions and 544 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
"description": "node decorators",
"main": "index.js",
"scripts": {
"compile": "tsc"
"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"
},
"dependencies": {
"co": "4.6.0",
"express": "4.14.0",
"socket.io": "1.7.2"
"express": "4.14.0"
},
"devDependencies": {
"@types/express": "4.0.34",
"@types/socket.io": "1.4.27",
"typescript": "2.1.1"
},
"keywords": [
Expand Down
55 changes: 16 additions & 39 deletions playground/socket/index.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,32 @@
import { listen } from 'socket.io';
import {
Middleware,
GlobalMiddleware,
ServerMiddleware,
Event,
attachControllers,
Controller,
Socket,
Args,
bootstrapSocketIO,
Namespace,
Socket
IO,
Ack
} from '@decorators/socket';

const server = listen(3000);

class SocketWrapper {
constructor(private socket: SocketIO.Socket) {}

log() {
console.log('Log');
class Wrapper {
constructor(data: any) {
console.log('Wrapper');
}
}

@ServerMiddleware((io, socket, next) => {
console.log('Global Server Middleware');
next();
})
@GlobalMiddleware((io, socket, packet, next) => {
console.log('Global socket middleware');
next();
})
@Middleware((io, socket, packet, next) => {
console.log('Controller based middleware');
next();
})
@Namespace('/messaging')
class FirstController {

constructor(...args) {
console.log(args);
}
@Controller('/')
class MessagingController {

@Event('message', (io, socket, packet, next) => {
console.log('Message middleware');
next();
})
onMessage(@Args() message, @Socket(SocketWrapper) socket: SocketWrapper) {
socket.log();
console.log(`Message: ${message}`);
@Event('message')
onMessage(@Args() message: string) {
console.log(
`Message: ${message}`
);
}

}

bootstrapSocketIO(server, [
{ provide: FirstController, deps: [1, 2, 3]}
]);
attachControllers(server, [ MessagingController ]);
5 changes: 3 additions & 2 deletions playground/socket/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@decorators/playground-socket",
"version": "1.3.3",
"version": "2.0.0",
"description": "node decorators",
"main": "index.js",
"dependencies": {
"@decorators/socket": "../../socket"
"@decorators/socket": "../../socket",
"@types/socket.io": "1.4.29"
},
"keywords": [
"nodejs",
Expand Down
14 changes: 13 additions & 1 deletion socket/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# Socket#2.0.0
* Features
* Added wrap option for **@IO(WrapperClass?)** decorator, see **@Socket()** decorator for details
* Renamed
* **@Callback()** to **@Ack()**
* **@GlobalMiddleware()** for socket global middleware to **@Middleware()**
* Renamed **@Namespace()** to **@Controller(namespace: string, middleware?: Function | Function[])**
* Removed
* **@Middleware(middleware: Function | Function[])** - use Controller based middleware
* deprecated **bootstrapSocketIO(io: SocketIO.Server, Controllers)** - Attaches controllers to IO server - use **attachControllers()** instead
* deprecated **attachControllerToSocket(io: SocketIO.Server, socket: SocketIO.Socket, Controllers)** - Attaches controllers to Socket - use **attachControllersToSocket()** instead

# Socket#1.4.0
* Controller DI
```typescript
Expand Down Expand Up @@ -71,7 +83,7 @@ class SocketWrapper {
* **bootstrapSocketIO(rootController: Controller)**
##### Class
* **@Connect(serverOrPort: number | string | HttpServer, opts?: any)**
* **@Middleware(fn: Function)**
* **@Middleware(fn: Function)**
##### Method
* **@OnIO(event: string)**
* **@OnConnect()**
Expand Down
74 changes: 38 additions & 36 deletions socket/README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
![Node Decorators](https://github.com/serhiisol/node-decorators/blob/master/decorators.png?raw=true)

Project implements decorators for modern tools for NodeJS like:
- [Socket.IO]
Project implements decorators for [Socket.IO]

### Installation
```
npm install @decorators/socket --save
```
### API
#### Functions
* **attachControllers(io: SocketIO.Server, Controller[] || Injectable[])** - Attaches controllers to IO server
* **attachControllersToSocket(io: SocketIO.Server, socket: SocketIO.Socket, Controller[] || Injectable[])** - Attaches controllers to Socket
where Injectable:
```typescript
{ provide: UserController, deps: [UserService] }
```

* **bootstrapSocketIO(io: SocketIO.Server, Controllers)** - Attaches controllers to IO server - **_[deprecated, use attachControllers - will be removed in v2.0.0 of this library]_**
* **attachControllerToSocket(io: SocketIO.Server, socket: SocketIO.Socket, Controllers)** - Attaches controllers to Socket - **_[deprecated, use attachControllersToSocket - will be removed in v2.0.0 of this library]_**

#### Decorators
##### Class
* **@Namespace(namespace: string)** - registers controller for namespace
* **@Controller(namespace?: string, middleware?: Function | Function[])** - registers controller for controller

* **@ServerMiddleware(middleware: Function | Function[])** - registers global server (io) middleware
```typescript
Expand All @@ -32,9 +21,7 @@ function middleware(
) {}
```

* **@GlobalMiddleware(middleware: Function | Function[]) => {})** - registers socket global middleware
* **@Middleware(middleware: Function | Function[])** - registers controller-based middleware,
will handle only socket events registered in controller
* **@Middleware(middleware: Function | Function[]) => {})** - registers socket global middleware
```typescript
function middleware(
io: SocketIO.Server | SocketIO.Namespace,
Expand All @@ -45,11 +32,11 @@ function middleware(
```

##### Method
* **@GlobalEvent(event: string)** - register global event (**io.on**)
* **@Connection()** - register **connection** listener (**io.on('connection', fn)**)
* **@Disconnect()** - register disconnect socket event (**socket.on('disconnect', fn)**)
* **@GlobalEvent(event: string)** - register global event (**io.on**)

* **@Event(event: string, middleware || \[middleware\])** - register socket event (**socket.on**),
* **@Event(event: string, middleware?: Function | Function[])** - register socket event (**socket.on**),
where middleware is a function which accepts four parameters:
```typescript
function middleware(
Expand All @@ -61,45 +48,60 @@ function middleware(
```

##### Parameter
* **@IO()** - returns server itself
* **@Socket(WrapperClass?: Class)** - returns socket, if **WrapperClass** provided, returns instance
of **WrapperClass**, passes **socket** as dependency into **WrapperClass**
* **@IO(WrapperClass?: Class)** - returns server itself
* **@Socket(WrapperClass?: Class)** - returns socket

If **WrapperClass** provided, returns instance
of **WrapperClass**, passes **socket** or **server** as dependency into **WrapperClass**

```typescript
class SocketWrapper {
constructor(private socket: SocketIO.Socket) {}
constructor(private ioSock: SocketIO.Server|SocketIO.Namespace|SocketIO.Socket) {}
}
```
* **@Args()** - returns event arguments (excluding callback)(if it exists)
* **@Callback()** - returns callback function (if it exists)

* **@Args()** - returns event arguments (excluding callback, if it exists)
* **@Ack()** - returns ack callback function (if it exists)

#### Helper Functions
* **attachControllers(io: SocketIO.Server, Controller[] || Injectable[])** - Attaches controllers to IO server
* **attachControllersToSocket(io: SocketIO.Server, socket: SocketIO.Socket, Controller[] || Injectable[])** - Attaches controllers to Socket

where Injectable:
```typescript
{ provide: UserController, deps: [UserService] }
```

### Details
#### Middleware
The middleware order :
* Global Server Middleware
* Global Socket middleware
* Controller based middleware
* Event based middleware
Additionally to this order depends on the order how you've registered appropriate types of middleware
* Global Server Middleware (**io.use(...)**)
* Global Socket middleware (**socket.use(...)**)
* Controller based middleware (**@Controller(...)**)
* Event based middleware (**@Event(...)**)

Additionally to this order depends on the order how you've registered appropriate types of middleware

### Quick Example:
```typescript
import { listen } from 'socket.io';
import { Event, Args, bootstrapSocketIO, Namespace } from '@decorators/socket';
import { Event, Args, attachControllers, Controller } from '@decorators/socket';

const server = listen(3000);

@Namespace('/messaging')
@Controller('/')
class MessageController {

@Event('message')
onMessage(@Args() message) {
console.log(`Message: ${message}`);
console.log(
`Message: ${message}`
);
}

}

bootstrapSocketIO(server, [ MessageController ]);
attachControllers(server, [ MessageController ]);
```

### License
MIT

[Socket.IO]:http://socket.io/
19 changes: 0 additions & 19 deletions socket/index.ts

This file was deleted.

12 changes: 8 additions & 4 deletions socket/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"name": "@decorators/socket",
"version": "1.4.0",
"description": "node decorators",
"main": "index.js",
"version": "2.0.0",
"description": "node decorators - decorators for socket.io library",
"main": "src/index.js",
"dependencies": {
"socket.io": "1.7.2"
"socket.io": ">=1.7.3"
},
"devDependencies": {
"@types/socket.io": "1.4.29",
"typescript": "2.2.2"
},
"keywords": [
"nodejs",
Expand Down
76 changes: 0 additions & 76 deletions socket/src/decorators/class.ts

This file was deleted.

Loading

0 comments on commit 3715ce3

Please sign in to comment.