Skip to content

Commit

Permalink
Release 1.0.0-alpha.27 🚀
Browse files Browse the repository at this point in the history
- Implemented BackEnd WebSocket Adapters
  • Loading branch information
Jonathan committed May 7, 2018
1 parent cc30caa commit debcfef
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 27 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: node_js
node_js:
- "9"
- "9"
before_install:
- npm install uws @types/uws
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onixjs/core",
"version": "1.0.0-alpha.25",
"version": "1.0.0-alpha.27",
"description": "An Enterprise Grade NodeJS Platform that implements Industry Standards and Patterns in order to provide Connectivity, Stability, High-Availability and High-Performance.",
"main": "dist/src/index.js",
"scripts": {
Expand Down Expand Up @@ -52,15 +52,15 @@
"@onixjs/sdk": "^1.0.0-alpha.13.1",
"finalhandler": "^1.1.1",
"reflect-metadata": "^0.1.12",
"router": "^1.3.2",
"uws": "^9.148.0"
"router": "^1.3.2"
},
"devDependencies": {
"@types/mongodb": "^3.0.5",
"@types/mongoose": "^5.0.2",
"@types/node": "^9.4.6",
"@types/send": "^0.14.4",
"@types/uws": "^0.13.2",
"@types/ws": "^5.1.0",
"ava": "^0.25.0",
"coveralls": "^3.0.0",
"dot": "^1.1.2",
Expand All @@ -72,7 +72,9 @@
"tslint": "^5.9.1",
"typedoc": "^0.10.0",
"typeorm": "^0.1.12",
"typescript": "^2.6.2"
"typescript": "^2.6.2",
"uws": "^9.148.0",
"ws": "^5.1.1"
},
"ava": {
"files": [
Expand Down
15 changes: 15 additions & 0 deletions src/adapters/uws.adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as DepWebSocket from 'uws';
import * as http from 'http';
import * as https from 'https';
import {WebSocketAdapter} from '../interfaces';
/**
* @class UWSAdapter
* @author Jonathan Casarrubias
* @license MIT
* @description This adapter will use the uws dependency
*/
export class UWSAdapter implements WebSocketAdapter {
WebSocket(server: http.Server | https.Server): DepWebSocket.Server {
return new DepWebSocket.Server({server});
}
}
15 changes: 15 additions & 0 deletions src/adapters/ws.adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as DepWebSocket from 'ws';
import * as http from 'http';
import * as https from 'https';
import {WebSocketAdapter} from '../interfaces';
/**
* @class WSAdapter
* @author Jonathan Casarrubias
* @license MIT
* @description This adapter will use the ws dependency
*/
export class WSAdapter implements WebSocketAdapter {
WebSocket(server: http.Server | https.Server): DepWebSocket.Server {
return new DepWebSocket.Server({server});
}
}
2 changes: 2 additions & 0 deletions src/core/host.boot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {OnixJS, promiseSeries} from '../index';
import * as path from 'path';
import {OnixConfig, BootConfig} from '../interfaces';
import {WSAdapter} from '../adapters/ws.adapter';
/**
* @class HostBoot
* @author Jonathan Casarrubias
Expand Down Expand Up @@ -40,6 +41,7 @@ export class HostBoot {
private hc: OnixConfig = {
// Default working directory points to ./dist
cwd: path.join(process.cwd(), 'dist'),
adapters: {websocket: WSAdapter},
},
) {
// Make sure we got the right configuration before
Expand Down
11 changes: 7 additions & 4 deletions src/core/host.broker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as http from 'http';
import * as https from 'https';
import * as WebSocket from 'uws';
import {IAppOperation, IAppDirectory} from '../interfaces';
import {IAppOperation, IAppDirectory, WebSocketAdapter} from '../interfaces';
/**
* @class HostBroker
* @author Jonathan Casarrubias
Expand All @@ -20,8 +19,12 @@ export class HostBroker {
* @description The constructor will create a new websocket server from the
* incoming http server created within the OnixJS Host.
*/
constructor(server: http.Server | https.Server, private apps: IAppDirectory) {
new WebSocket.Server({server}).on('connection', (ws: WebSocket) => {
constructor(
private server: http.Server | https.Server,
private websocket: WebSocketAdapter,
private apps: IAppDirectory,
) {
this.websocket.WebSocket(this.server).on('connection', ws => {
ws.on('message', (data: string) => this.handle(ws, JSON.parse(data)));
});
}
Expand Down
19 changes: 14 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as finalhandler from 'finalhandler';
import {Utils} from '@onixjs/sdk/dist/utils';
import {HostBroker} from './core/host.broker';
import {promisify} from 'util';
import {WSAdapter} from './adapters/ws.adapter';
/**
* @class OnixJS
* @author Jonathan Casarrubias <gh: mean-expert-official>
Expand All @@ -40,7 +41,7 @@ export class OnixJS {
* @description Current Onix Version.
*/
get version(): string {
return '1.0.0-alpha.25';
return '1.0.0-alpha.27';
}
/**
* @property router
Expand All @@ -65,7 +66,13 @@ export class OnixJS {
* @description Internally exposes a given configuration.
* It logs to the terminal the current Onix version.
*/
constructor(public config: OnixConfig = {cwd: process.cwd(), port: 3000}) {
constructor(
public config: OnixConfig = {
cwd: process.cwd(),
port: 3000,
adapters: {websocket: WSAdapter},
},
) {
this.config = Object.assign({cwd: process.cwd(), port: 3000}, config);
// Listener for closing process
process.on('exit', async () => {
Expand Down Expand Up @@ -351,7 +358,7 @@ export class OnixJS {
* @description This method will start the systemm level server.
*/
private async startSystemServer() {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
// Setup server
this.server = this.createServer();
// Verify if there is an SSL Activation File
Expand All @@ -371,8 +378,10 @@ export class OnixJS {
new SchemaProvider(this.router, this._apps);
// Listen Server Port
this.server.listen(this.config.port);
// Create schema provider route
new HostBroker(this.server, this._apps);
// Create WebSocket Adapter Instance
const adapter = new this.config.adapters.websocket();
// Create Onix Host Instance
new HostBroker(this.server, adapter, this._apps);
// Indicate the ONIX SERVER is now listening on the given port
console.log(`ONIXJS HOST LOADED: Listening on port ${this.config.port}`);
// Resolve Server
Expand Down
20 changes: 18 additions & 2 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import {ChildProcess} from 'child_process';
import * as http from 'http';
import * as https from 'https';
import {AppNotifier} from '..';
/**
* @interface IAppConfig
* @author Jonathan Casarrubias <gh: mean-expert-official>
* @description This interface will provida configuration for
* a given application.
*/
export interface IAppConfig extends OnixConfig {
export interface IAppConfig {
cwd?: string;
// Port if network enabled
port?: number;
// If network enabled, a HTTP server will be created
network?: {
disabled: boolean;
ssl?: ISSlConfig;
};
// Modules to be loaded for this application
modules: Constructor[];
}
Expand Down Expand Up @@ -330,9 +339,16 @@ export interface OnixConfig extends DomainConfig {
cwd?: string;
// If network enabled, a HTTP server will be created
network?: {
disabled: boolean;
ssl?: ISSlConfig;
};
// WebSocket Adapter
adapters: {
websocket: new () => WebSocketAdapter;
};
}

export interface WebSocketAdapter {
WebSocket(server: http.Server | https.Server);
}

export interface BootConfig {
Expand Down
43 changes: 35 additions & 8 deletions test/onixjs.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@ import {IAppConfig} from '../src/interfaces';
import {Utils} from '@onixjs/sdk/dist/utils';
import {OnixClient, AppReference, ComponentReference} from '@onixjs/sdk';
import {NodeJS} from '@onixjs/sdk/dist/adapters/node.adapters';
import {WSAdapter} from '../src/adapters/ws.adapter';
// Test Onix Version

test('Onix version', t => {
const onix: OnixJS = new OnixJS({cwd, port: 8085});
const onix: OnixJS = new OnixJS({
cwd,
port: 8085,
adapters: {websocket: WSAdapter},
});
t.is(onix.version, pkg.version);
});
//Test Onix App Starter
test('Onix app starter', async t => {
const onix: OnixJS = new OnixJS({cwd, port: 8083});
const onix: OnixJS = new OnixJS({
cwd,
port: 8083,
adapters: {websocket: WSAdapter},
});
await onix.load('TodoApp@todo.app:disabled');
const results: OperationType.APP_START_RESPONSE[] = await onix.start();
t.deepEqual(results, [
Expand All @@ -35,14 +44,18 @@ test('Onix app starter', async t => {

//Test Onix App Pinger
test('Onix app pinger', async t => {
const onix: OnixJS = new OnixJS({cwd, port: 8084});
const onix: OnixJS = new OnixJS({
cwd,
port: 8084,
adapters: {websocket: WSAdapter},
});
await onix.load('TodoApp@todo.app:disabled');
const config: IAppConfig = await onix.ping('TodoApp');
t.true(config.network!.disabled);
});
//Test Onix Apps Say Hello
test('Onix app greeter', async t => {
const onix: OnixJS = new OnixJS({cwd});
const onix: OnixJS = new OnixJS({cwd, adapters: {websocket: WSAdapter}});
await onix.load('BobApp@bob.app');
await onix.load('AliceApp@alice.app');
// Bidimentional array, each app call multiple apps, each
Expand All @@ -56,7 +69,11 @@ test('Onix app greeter', async t => {

//Test Onix RPC component methods
test('Onix rpc component methods from server', async t => {
const onix: OnixJS = new OnixJS({cwd, port: 8085});
const onix: OnixJS = new OnixJS({
cwd,
port: 8085,
adapters: {websocket: WSAdapter},
});
await onix.load('TodoApp@todo.app:disabled');
await onix.start();
const todo: TodoModel = new TodoModel();
Expand All @@ -78,7 +95,11 @@ test('Onix rpc component methods from server', async t => {

//Test Onix RPC component methods
test('Onix rpc component methods from client', async t => {
const onix: OnixJS = new OnixJS({cwd, port: 8086});
const onix: OnixJS = new OnixJS({
cwd,
port: 8086,
adapters: {websocket: WSAdapter},
});
await onix.load('TodoApp@todo.app');
await onix.start();
// Websocket should be available now
Expand Down Expand Up @@ -117,7 +138,11 @@ test('Onix rpc component methods from client', async t => {
test('Onix rpc component stream', async t => {
const text: string = 'Hello SDK World';
// Host Port 8087
const onix: OnixJS = new OnixJS({cwd, port: 8087});
const onix: OnixJS = new OnixJS({
cwd,
port: 8087,
adapters: {websocket: WSAdapter},
});
// SOA Service Port 8078
await onix.load('TodoApp@todo.app:8078');
await onix.start();
Expand Down Expand Up @@ -148,5 +173,7 @@ test('Onix rpc component stream', async t => {
const result = await componentRef.Method('addTodo').call({text});
t.is(result.text, text);
}
await onix.stop();
onix
.stop()
.then(() => console.log('STOPPED'), e => console.log('SOME ERROR', e));
});
17 changes: 14 additions & 3 deletions test/onixjs.core.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {Utils} from '@onixjs/sdk/dist/utils';
import {Mongoose, Schema} from 'mongoose';
import {GroupMatch} from '../src/core/acl.group.match';
import {AllowEveryone} from '../src/core/acl.everyone';
import {WSAdapter} from '../src/adapters/ws.adapter';
const cwd = path.join(process.cwd(), 'dist', 'test');
// Test AppFactory

Expand Down Expand Up @@ -76,6 +77,7 @@ test('Core: OnixJS loads creates duplicated Application.', async t => {
const onix: OnixJS = new OnixJS({
cwd: path.join(process.cwd(), 'dist', 'test'),
port: 8086,
adapters: {websocket: WSAdapter},
});
await onix.load('TodoApp@todo.app:disabled');
const error = await t.throws(onix.load('TodoApp@todo.app:disabled'));
Expand All @@ -87,6 +89,7 @@ test('Core: OnixJS pings missing Application.', async t => {
const onix: OnixJS = new OnixJS({
cwd: path.join(process.cwd(), 'dist', 'test'),
port: 9091,
adapters: {websocket: WSAdapter},
});
const error = await t.throws(onix.ping('MissingApp'));
t.is(
Expand All @@ -100,6 +103,7 @@ test('Core: OnixJS fails on coordinating invalid callee.', async t => {
const onix: OnixJS = new OnixJS({
cwd: path.join(process.cwd(), 'dist', 'test'),
port: 8088,
adapters: {websocket: WSAdapter},
});
const error = await t.throws(
onix.coordinate('Dummy.call', <IRequest>{metadata: {}, payload: {}}),
Expand All @@ -112,6 +116,7 @@ test('Core: OnixJS gets list of apps.', async t => {
const onix: OnixJS = new OnixJS({
cwd: path.join(process.cwd(), 'dist', 'test'),
port: 8089,
adapters: {websocket: WSAdapter},
});
await onix.load('TodoApp@todo.app:disabled');
const apps = onix.apps();
Expand Down Expand Up @@ -539,7 +544,10 @@ test('Core: host boot.', async t => {
{
apps: ['TodoApp@todo.app:8076'],
},
{cwd},
{
cwd,
adapters: {websocket: WSAdapter},
},
);
await t.notThrows(instance.run());
await instance.host.stop();
Expand All @@ -553,8 +561,8 @@ test('Core: host boot ssl activation file.', async t => {
{
cwd,
port: 5000,
adapters: {websocket: WSAdapter},
network: {
disabled: false,
ssl: {
activation: {
endpoint: '/.well-known/activation.txt',
Expand Down Expand Up @@ -583,7 +591,10 @@ test('Core: host boot throws.', async t => {
{
apps: [],
},
{cwd},
{
cwd,
adapters: {websocket: WSAdapter},
},
);
}),
);
Expand Down

0 comments on commit debcfef

Please sign in to comment.