Skip to content

Commit 061144e

Browse files
authored
Merge pull request #72 from hapinessjs/next
release(version): v1.3.0
2 parents 934f6a8 + 045220e commit 061144e

16 files changed

+468
-117
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ $ yarn add @hapiness/core rxjs
8787

8888
```javascript
8989
"dependencies": {
90-
"@hapiness/core": "^1.2.2",
90+
"@hapiness/core": "^1.3.0",
9191
//...
9292
}
9393
//...
9494
```
9595

9696
### Use Hapiness API
9797

98-
Actually, we're in : **v1.2.1**
98+
Actually, we're in : **v1.3.0**
9999

100100
See [API](https://github.com/hapinessjs/hapiness/blob/master/API.md) Reference to know what's already implemented.
101101

@@ -115,6 +115,10 @@ To set up your development environment:
115115
[Back to top](#table-of-contents)
116116

117117
## Change History
118+
* v1.3.0 (2017-11-30)
119+
* Option that allow WS Server Ext to share Http Server Ext port
120+
* Handle HapiJS connections system to listen multiple ports
121+
* Documentation
118122
* v1.2.2 (2017-11-20)
119123
* Export `createDecorator()`, `extractMetadata()`, `extractMetadataByDecorator`, `errorHandler()` methods and `DependencyInjection` class
120124
* Documentation

package-lock.json

+20-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hapiness/core",
3-
"version": "1.2.2",
3+
"version": "1.3.0",
44
"description": "Project to have a HapiJS (https://hapijs.com/) based framework to create easier NodeJS back-end with some awesome features",
55
"main": "commonjs/index.js",
66
"types": "index.d.ts",
@@ -75,12 +75,12 @@
7575
"dependencies": {
7676
"@types/hapi": "^16.1.11",
7777
"@types/hoek": "^4.1.3",
78-
"@types/joi": "^13.0.0",
78+
"@types/joi": "^13.0.1",
7979
"@types/node": "^8.0.53",
80-
"@types/websocket": "^0.0.34",
80+
"@types/websocket": "^0.0.35",
8181
"debug": "^3.1.0",
8282
"hapi": "^16.6.2",
83-
"injection-js": "^2.2.0",
83+
"injection-js": "^2.2.1",
8484
"reflect-metadata": "^0.1.10",
8585
"websocket": "^1.0.25"
8686
},
@@ -95,7 +95,7 @@
9595
"rxjs": "^5.5.2",
9696
"ts-node": "^3.3.0",
9797
"tslint": "^5.8.0",
98-
"typescript": "^2.6.1",
98+
"typescript": "^2.6.2",
9999
"unit.js": "^2.0.0"
100100
},
101101
"engines": {

src/extensions/http-server/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ The extension is based on HapiJS
1212
);
1313
```
1414

15+
```
16+
{
17+
host: string
18+
port: number
19+
options?: ServerOptions
20+
}
21+
//or
22+
{
23+
connections: ConnectionOptions[]
24+
options?: ServerOptions
25+
}
26+
```
27+
1528
Configuration - [HapiJS Connection options](https://hapijs.com/api#serverconnectionoptions)
1629

1730
## Extension provider
@@ -37,6 +50,7 @@ Declare HTTP routes
3750
- `method` - can be an array, values: (get, post, put, delete, patch, options)
3851
- `config` - partially implemented, see [HapiJS Route config](https://hapijs.com/api#route-configuration)
3952
- `providers` - Providers to add in the request DI, it means at each request a new instance of the provider will be created
53+
- `labels`- `string | string[]` Used to attach a route to a connection for multiple connections
4054

4155
- interfaces
4256
- see request and reply on [HapiJS Docs](https://hapijs.com/api#requests)
@@ -82,7 +96,7 @@ Request lifecycle component
8296

8397
- `event` - request lifecycle event, see [HapiJS Request lifecycle](https://hapijs.com/api#request-lifecycle)
8498
events: (onPreAuth, onPostAuth, onPreHandler, onPostHandler, onPreResponse)
85-
99+
86100
- interfaces
87101

88102
- see request and reply on [HapiJS Docs](https://hapijs.com/api#requests)

src/extensions/http-server/decorators.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ export interface Route {
66
method: string | string[];
77
config?: RouteConfig;
88
providers?: Array<Type<any>|any>;
9+
labels?: string | string[];
910
}
1011
export const Route: CoreDecorator<Route> = createDecorator<Route>('Route', {
1112
path: undefined,
1213
method: undefined,
1314
config: undefined,
14-
providers: undefined
15+
providers: undefined,
16+
labels: undefined
1517
});
1618

1719
export interface Lifecycle {

src/extensions/http-server/extension.ts

+80-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Type } from '../../core/decorators';
99
import { enumByMethod, LifecycleComponentEnum } from './enums';
1010
import { LifecycleManager } from './lifecycle';
1111
import { RouteBuilder } from './route';
12-
import { CoreRoute, HapiConfig, HapinessHTTPHandlerResponse } from './interfaces';
12+
import { ConnectionOptions, CoreRoute, HapiConfig, HTTPHandlerResponse } from './interfaces';
1313
import { Observable } from 'rxjs';
1414
import { RouteConfiguration, Server, Request, ReplyNoContinue, ReplyWithContinue } from 'hapi';
1515

@@ -32,7 +32,12 @@ export class HttpServerExt implements OnExtensionLoad, OnModuleInstantiated {
3232
onExtensionLoad(module: CoreModule, config: HapiConfig): Observable<Extension> {
3333
return Observable
3434
.of(new Server(config.options))
35-
.do(_ => _.connection(Object.assign({}, config, { options: undefined })))
35+
.flatMap(server => Observable
36+
.of(Object.assign({}, config, { options: undefined }))
37+
.map(_ => this.formatConnections(_))
38+
.do(_ => _.forEach(connection => server.connection(connection)))
39+
.map(_ => server)
40+
)
3641
.flatMap(server =>
3742
Observable
3843
.of({
@@ -63,6 +68,23 @@ export class HttpServerExt implements OnExtensionLoad, OnModuleInstantiated {
6368
.flatMap(_ => server.start());
6469
}
6570

71+
/**
72+
* Format the config provided
73+
* to a list of ConnectionOptions
74+
*
75+
* @param {HapiConfig} config
76+
* @returns ConnectionOptions
77+
*/
78+
private formatConnections(config: HapiConfig): ConnectionOptions[] {
79+
return []
80+
.concat(!!(<any>config).connections ?
81+
(<any>config).connections :
82+
config
83+
)
84+
.filter(_ => !!_)
85+
.map(_ => <ConnectionOptions>_);
86+
}
87+
6688
/**
6789
* Register a HapiJS Plugin
6890
*
@@ -71,36 +93,50 @@ export class HttpServerExt implements OnExtensionLoad, OnModuleInstantiated {
7193
* @returns Observable
7294
*/
7395
private registerPlugin(module: CoreModule, server: Server): Observable<CoreRoute[]> {
74-
const register: any = (s, o, n) => n();
75-
register.attributes = {
76-
name: module.name,
77-
version: module.version
78-
};
79-
return Observable
80-
.fromPromise(server.register(register))
81-
.flatMap(_ => this.addRoutes(module, server));
96+
return this
97+
.buildRoutes(module)
98+
.flatMap(routes => Observable
99+
.of(<any>this.registerHandler(routes))
100+
.do(_ => _.attributes = { name: module.name, version: module.version })
101+
.flatMap(_ => Observable
102+
.fromPromise(server.register(_))
103+
.map(__ => routes)
104+
)
105+
);
82106
}
83107

84108
/**
85109
* Add routes from CoreModule
86110
*
87-
* @param {CoreModule} module
88-
* @param {Server} server
111+
* @param {CoreRoute[]} module
89112
* @returns Observable
90113
*/
91-
private addRoutes(module: CoreModule, server: Server): Observable<CoreRoute[]> {
92-
return Observable
93-
.from(RouteBuilder.buildRoutes(module))
94-
.do(_ =>
95-
server
96-
.route(<RouteConfiguration>{
114+
private registerHandler(routes: CoreRoute[] = []): (s, o, n) => void {
115+
return (server: Server, options, next) => {
116+
routes
117+
.forEach(_ => {
118+
const _server = !!_.labels ? server.select(_.labels) : server;
119+
_server.route(<RouteConfiguration>{
97120
method: _.method,
98121
path: _.path,
99122
config: Object.assign({
100123
handler: (request, reply) => this.httpHandler(request, reply, _)
101124
}, _.config)
102-
})
103-
)
125+
});
126+
});
127+
next();
128+
}
129+
}
130+
131+
/**
132+
* Build CoreRoute based on a module
133+
*
134+
* @param {CoreModule} module
135+
* @returns Observable
136+
*/
137+
private buildRoutes(module: CoreModule): Observable<CoreRoute[]> {
138+
return Observable
139+
.from(RouteBuilder.buildRoutes(module))
104140
.toArray();
105141
}
106142

@@ -135,14 +171,26 @@ export class HttpServerExt implements OnExtensionLoad, OnModuleInstantiated {
135171
);
136172
}
137173

138-
private formatResponse(data: any): HapinessHTTPHandlerResponse {
174+
/**
175+
* Format response to HTTPHandlerResponse object
176+
*
177+
* @param {any} data
178+
* @returns HTTPHandlerResponse
179+
*/
180+
private formatResponse(data: any): HTTPHandlerResponse {
139181
return {
140182
statusCode: !!data ? data.statusCode || 200 : 200,
141183
headers: !!data ? data.headers || {} : {},
142184
response: !!data ? data.response || data : data
143185
};
144186
}
145187

188+
/**
189+
* Check of response is not empty
190+
*
191+
* @param {any} response
192+
* @returns boolean
193+
*/
146194
private isValid(response: any): boolean {
147195
return typeof(response) !== 'undefined' && response !== null;
148196
}
@@ -172,6 +220,17 @@ export class HttpServerExt implements OnExtensionLoad, OnModuleInstantiated {
172220
.map(_ => module);
173221
}
174222

223+
/**
224+
* Lifecycle Event Handler
225+
* Instantiate the Lifecycle component
226+
* And trigger the hook
227+
*
228+
* @param {Type<any>} lifecycle
229+
* @param {CoreModule} module
230+
* @param {Request} request
231+
* @param {ReplyWithContinue} reply
232+
* @returns Observable
233+
*/
175234
private eventHandler(lifecycle: Type<any>, module: CoreModule, request: Request, reply: ReplyWithContinue): Observable<any> {
176235
return Observable
177236
.of(lifecycle)

0 commit comments

Comments
 (0)