Skip to content

Commit

Permalink
Release 1.0.0-alpha.8 🚀
Browse files Browse the repository at this point in the history
- Increased test coverage
- Renamed ICall to OnixMessage
- Passed OnixMessage in LifeCycles instead of Metadata
  • Loading branch information
Jonathan committed Mar 26, 2018
1 parent 3fe7f49 commit 2c612e7
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onixjs/core",
"version": "1.0.0-alpha.7.3",
"version": "1.0.0-alpha.8",
"description": "The High-Performance SOA Real-Time Framework for Node.JS",
"main": "dist/src/index.js",
"scripts": {
Expand Down
10 changes: 6 additions & 4 deletions src/core/app.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IAppOperation,
IAppConfig,
AppConstructor,
ICall,
OnixMessage,
} from '../index';
import {AppFactory} from './app.factory';
import {CallResponser} from './call.responser';
Expand Down Expand Up @@ -117,7 +117,9 @@ export class AppServer {
// These events are done through internal processes.
// External remote calls will be executed inside the OnixConnection
case OperationType.ONIX_REMOTE_CALL_PROCEDURE:
const result = await this.responser.process(<ICall>operation.message);
const result = await this.responser.process(
<OnixMessage>operation.message,
);
// Send result back to broker
if (process.send)
process.send({
Expand All @@ -126,7 +128,7 @@ export class AppServer {
});
break;
// System level event to coordinate every application in the
// cluster, in order to automatically call between each others
// cluster, in order to automatOnixMessagey call between each others
case OperationType.APP_GREET:
let apps: string[] = <string[]>operation.message;
apps = apps.filter((name: string) => this.AppClass.name !== name);
Expand Down Expand Up @@ -161,7 +163,7 @@ export class AppServer {
apps.map(
(name: string) =>
new Promise<boolean>(async (resolve, reject) => {
const result: boolean = await this.responser.process(<ICall>{
const result: boolean = await this.responser.process(<OnixMessage>{
uuid: '1',
rpc: `${name}.isAlive`,
request: {metadata: {}, payload: {}},
Expand Down
28 changes: 14 additions & 14 deletions src/core/call.responser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ICall, AppConstructor, ReflectionKeys} from '../interfaces';
import {OnixMessage, AppConstructor, ReflectionKeys} from '../interfaces';
import {AppFactory, LifeCycle} from '../core';
/**
* @class CallResponse
Expand Down Expand Up @@ -26,7 +26,7 @@ export class CallResponser {
* @description This method will process an incoming call in order
* to send back an answer.
*/
async process(message: ICall): Promise<any> {
async process(message: OnixMessage): Promise<any> {
return new Promise<any>(async (resolve, reject) => {
console.log(
`Onix callee app ${this.AppClass.name} got remote call procedure`,
Expand Down Expand Up @@ -92,25 +92,25 @@ export class CallResponser {
// Execute main hook, might be app/system or module level.
const result = await mainHook(
this.factory.app,
message.request.metadata,
message,
async (): Promise<any> => {
// If there is a custom component level hook for this call
// then execute it first.
if (slaveHook) {
// Do whatever the developer defined in component config
return slaveHook(
this.factory.app,
message.request.metadata,
async (): Promise<any> => {
// Ok cool, let me finis. lol (freaking genius)
return method
? method.call(scope, message.request.payload)
: null;
},
);
return await slaveHook(this.factory.app, message, async (): Promise<
any
> => {
// Ok cool, let me finish. lol (freaking genius)
return method
? await method.call(scope, message.request.payload)
: null;
});
} else {
// Else just call the requested method now.
return method ? method.call(scope, message.request.payload) : null;
return method
? await method.call(scope, message.request.payload)
: null;
}
},
);
Expand Down
4 changes: 2 additions & 2 deletions src/core/call.streamer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AppFactory} from './app.factory';
import {AppConstructor, ICall} from '../interfaces';
import {AppConstructor, OnixMessage} from '../interfaces';

export class CallStreamer {
/**
Expand All @@ -14,7 +14,7 @@ export class CallStreamer {
* @description This method will register an incoming call in order
* to send back an answer.
*/
register(message: ICall, handler) {
register(message: OnixMessage, handler) {
console.log(
`Onix callee app ${this.AppClass.name} got remote stream request`,
);
Expand Down
4 changes: 2 additions & 2 deletions src/core/connection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as WebSocket from 'uws';
import {CallResponser} from './call.responser';
import {ICall, OperationType} from '../index';
import {OnixMessage, OperationType} from '../index';
import {CallStreamer} from './call.streamer';
/**
* @class ClientConnection
Expand All @@ -27,7 +27,7 @@ export class ClientConnection {
* @param data
* @description This method will handle
*/
async handle(data: ICall) {
async handle(data: OnixMessage) {
// Remote Procedure Stream
if (data.request.metadata.stream) {
this.stream.register(data, chunk =>
Expand Down
9 changes: 5 additions & 4 deletions src/core/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {IApp, IMetaData} from '../index';
import {IApp} from '../index';
import {OnixMessage} from '../interfaces';
/**
* @class LifeCycle
* @author Jonathan Casarrubias
Expand All @@ -19,7 +20,7 @@ export class LifeCycle {
*/
async onAppMethodCall(
app: IApp,
metadata: IMetaData,
message: OnixMessage,
method: () => Promise<any>,
): Promise<any> {
console.log('Before App Method Call');
Expand All @@ -40,7 +41,7 @@ export class LifeCycle {
*/
async onModuleMethodCall(
app: IApp,
metadata: IMetaData,
message: OnixMessage,
method: () => Promise<any>,
): Promise<any> {
console.log('Before Module Method Call');
Expand All @@ -61,7 +62,7 @@ export class LifeCycle {
*/
async onComponentMethodCall(
app: IApp,
metadata: IMetaData,
message: OnixMessage,
method: () => Promise<any>,
): Promise<any> {
console.log('Before Component Method Call');
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IAppConfig,
OperationType,
IAppOperation,
ICall,
OnixMessage,
IRequest,
OnixConfig,
} from './interfaces';
Expand Down Expand Up @@ -33,7 +33,7 @@ export class OnixJS {
* @description Current Onix Version.
*/
get version(): string {
return '1.0.0-alpha.7.3';
return '1.0.0-alpha.8';
}
/**
* @property server
Expand Down Expand Up @@ -179,7 +179,7 @@ export class OnixJS {
const operation: IAppOperation = {
uuid: Utils.uuid(),
type: OperationType.ONIX_REMOTE_CALL_PROCEDURE,
message: <ICall>{
message: <OnixMessage>{
rpc,
request,
},
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ export interface IRequest {
payload: any;
}
/**
* @interface ICall
* @interface OnixMessage
* @author Jonathan Casarrubias
* @description ICall inteface for internal (OS Event communication)
* @description OnixMessage inteface for internal (OS Event communication)
*/
export interface ICall {
export interface OnixMessage {
uuid: string;
rpc: string;
request: IRequest;
Expand Down
4 changes: 2 additions & 2 deletions test/onix.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {TodoModel} from './todo.shared/todo.model';
const pkg = require('../../package.json');
const cwd = path.join(process.cwd(), 'dist', 'test');
import * as WebSocket from 'uws';
import {IAppConfig, ICall} from '../src/interfaces';
import {IAppConfig, OnixMessage} from '../src/interfaces';
import {OnixClient, AppReference, ComponentReference} from '@onixjs/sdk';
import {Utils} from '@onixjs/sdk/dist/utils';
import {NodeJS} from '@onixjs/sdk/dist/core/node.adapters';
Expand Down Expand Up @@ -109,7 +109,7 @@ test('Onix rpc component methods from client', async t => {
client.on('open', () => {
// Send Todo
client.send(
JSON.stringify(<ICall>{
JSON.stringify(<OnixMessage>{
rpc: 'TodoApp.TodoModule.TodoComponent.addTodo',
request: <IRequest>{
metadata: {stream: false, caller: 'tester', token: 'dummytoken'},
Expand Down
82 changes: 79 additions & 3 deletions test/onixjs.core.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ import {
IAppOperation,
Component,
ClientConnection,
ICall,
OnixMessage,
HostBoot,
Injector,
Service,
Inject,
DataSource,
IDataSource,
IModel,
Model,
Property,
} from '../src';
import * as path from 'path';
import {CallResponser} from '../src/core/call.responser';
import * as WebSocket from 'uws';
import {CallStreamer} from '../src/core/call.streamer';
import {Utils} from '@onixjs/sdk/dist/utils';
import {Mongoose, Schema, Model as MongooseModel} from 'mongoose';
const cwd = path.join(process.cwd(), 'dist', 'test');
// Test AppFactory
test('Core: AppFactory creates an Application.', async t => {
Expand Down Expand Up @@ -415,15 +421,15 @@ test('Core: Connection.', async t => {
// Create a new Client Connection
const connection = new ClientConnection(ws, responser, streamer);
// Handle RPC Call
await connection.handle(<ICall>{
await connection.handle(<OnixMessage>{
rpc: 'MyApp.MyModule.MyComponent.testRPC',
request: <IRequest>{
metadata: {stream: false, caller: 'tester', token: 'dummytoken'},
payload,
},
});
// Handle Stream
await connection.handle(<ICall>{
await connection.handle(<OnixMessage>{
rpc: 'MyApp.MyModule.MyComponent.testStream',
request: <IRequest>{
metadata: {stream: true, caller: 'tester', token: 'dummytoken'},
Expand Down Expand Up @@ -504,3 +510,73 @@ test('Core: Injector.', async t => {
t.is(instance.test(), text);
t.is(instance.test2(), text);
});
// Test Injector has, get and set
test('Core: injector has, get and set.', t => {
const injector: Injector = new Injector();
const hello = 'world';
if (!injector.has(hello)) {
injector.set('hello', hello);
}
t.is(injector.get('hello'), hello);
});
// Test Inject Model and Services
test('Core: Inject Model and Services.', async t => {
// Test Reference
const criteria: string = 'Hello World';
// DataSource
@DataSource()
class MongooseDatasource implements IDataSource {
/**
* @property mongoose
* @description Mongoose instance reference
*/
private mongoose: Mongoose = new Mongoose();
async connect(): Promise<Mongoose> {
return this.mongoose.connect(
'mongodb://lb-sdk-test:lb-sdk-test@ds153400.mlab.com:53400/heroku_pmkjxjwz',
);
}
async disconnect(): Promise<void> {
return this.mongoose.disconnect();
}
register(name: string, model: IModel, schema: Schema): any {
return this.mongoose.model(name, schema);
}
}
// Model
@Model({
datasource: MongooseDatasource,
})
class TodoModel implements IModel {
_id?: string;
@Property(String) text: String;
}
// Service
@Service()
class TodoService {
test(): string {
return criteria;
}
}
// Component
class TodoComponent {
@Inject.Model(TodoModel) public model: MongooseModel<any>;
@Inject.Model(TodoModel) public model2: MongooseModel<any>;
@Inject.Service(TodoService) public service: TodoService;
@Inject.Service(TodoService) public service2: TodoService;
}
// Inject Model and Service
const injector: Injector = new Injector();
const instance: TodoComponent = new TodoComponent();
injector.inject(TodoComponent, instance, {
components: [],
models: [TodoModel],
services: [TodoService],
});
// Test Service
t.is(instance.service.test(), criteria);
// Test Model
const result = await instance.model.create({text: criteria});
t.truthy(result._id);
t.is(result.text, criteria);
});
1 change: 1 addition & 0 deletions test/todo.shared/todo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class TodoComponent implements IComponent {
*/
@RPC()
async addTodo(todo: TodoModel): Promise<TodoModel> {
console.log('REQUEST: ', todo);
const result = await this.service.create(todo);
this.emmiter.emit('onCreate', result);
return result;
Expand Down
12 changes: 10 additions & 2 deletions test/todo.shared/todo.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Module} from '../../src/index';
import {TodoComponent} from './todo.component';
import {TodoModel} from './todo.model';
import {TodoService} from './todo.service';
import {TodoApp} from '../todo.app';
import {OnixMessage} from '../../src/interfaces';
/**
* @class TodoModule
* @author Jonathan Casarrubias
Expand All @@ -13,8 +15,14 @@ import {TodoService} from './todo.service';
models: [TodoModel],
services: [TodoService],
components: [TodoComponent],
lifecycle: async (app, metadata, method): Promise<any> => {
console.log('I could be doing some authentication here');
lifecycle: async (
app: TodoApp,
message: OnixMessage,
method,
): Promise<any> => {
// Add Created At
const date = new Date();
message.request.payload.createdAt = date.toISOString();
// before call
const result = await method();
// after call
Expand Down

0 comments on commit 2c612e7

Please sign in to comment.