Skip to content

Commit

Permalink
Split create functions for RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Dec 17, 2017
1 parent 53de27a commit 3db9f82
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 28 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

const createKoa = require('./koa');
const createError = require('./error');
const createResponse = require('./response');

module.exports = {
createKoa,
createError,
createResponse
};
File renamed without changes.
File renamed without changes.
37 changes: 37 additions & 0 deletions packages/client-rpc/src/create/koa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

import type { RpcType } from '../types';

type CreateKoaOptionType = {
handlers: {
http: Function,
ws: Function
},
path: string,
types: Array<RpcType>
};

const Koa = require('koa');
const koaRoute = require('koa-route');
const koaWebsocket = require('koa-websocket');

module.exports = function createKoa ({ handlers, path, types }: CreateKoaOptionType): Koa {
let app = new Koa();

if (types.includes('http')) {
app.use(
koaRoute.post(path, handlers.http)
);
}

if (types.includes('ws')) {
app = koaWebsocket(app);

(app: any).ws.use(
koaRoute.all(path, handlers.ws)
);
}

return app;
};
44 changes: 44 additions & 0 deletions packages/client-rpc/src/create/koa.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// ISC, Copyright 2017 Jaco Greeff

const createKoa = require('./koa');

describe('createKoa', () => {
it('creates an HTTP interface', () => {
expect(
createKoa({
handlers: {
http: () => true,
ws: () => true
},
path: '/',
types: ['http']
})
).toBeDefined();
});

it('creates an WS interface', () => {
expect(
createKoa({
handlers: {
http: () => true,
ws: () => true
},
path: '/',
types: ['ws']
}).ws
).toBeDefined();
});

it('creates an HTTP + WS interface', () => {
expect(
createKoa({
handlers: {
http: () => true,
ws: () => true
},
path: '/',
types: ['http', 'ws']
})
).toBeDefined();
});
});
File renamed without changes.
36 changes: 12 additions & 24 deletions packages/client-rpc/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,23 @@ type WsContextType = {

const coBody = require('co-body');
const EventEmitter = require('eventemitter3');
const Koa = require('koa');
const koaRoute = require('koa-route');
const koaWebsocket = require('koa-websocket');

const assert = require('@polkadot/util/assert');
const ExtError = require('@polkadot/util/ext/error');
const l = require('@polkadot/util/logger')('rpc');
const isError = require('@polkadot/util/is/error');
const isFunction = require('@polkadot/util/is/function');

const { createKoa, createError, createResponse } = require('./create');
const defaults = require('./defaults');
const { createError, createResponse } = require('./jsonrpc');
const { validateConfig, validateRequest, validateHandlers } = require('./validate');

module.exports = class RPCServer extends EventEmitter implements RpcInterface {
_handlers: HandlersType;
_path: string;
_port: number;
_server: ?net$Server;
_type: Array<RpcType>;
_types: Array<RpcType>;

constructor ({ path = defaults.PATH, port = defaults.PORT, type = defaults.TYPE }: RpcConfigType, handlers: HandlersType, autoStart: boolean = true) {
super();
Expand All @@ -49,7 +46,7 @@ module.exports = class RPCServer extends EventEmitter implements RpcInterface {
this._path = path;
this._port = port;
this._server = null;
this._type = type;
this._types = type;

if (autoStart) {
this.start();
Expand All @@ -59,27 +56,18 @@ module.exports = class RPCServer extends EventEmitter implements RpcInterface {
async start (): Promise<boolean> {
this.stop();

const hasHttp = this._type.includes('http');
const hasWs = this._type.includes('ws');
const app = hasWs
? koaWebsocket(new Koa())
: new Koa();

if (hasHttp) {
app.use(
koaRoute.post(this._path, this._handlePost)
);
}

if (hasWs) {
(app: any).ws.use(
koaRoute.all(this._path, this._handleWs)
);
}
const app = createKoa({
handlers: {
http: this._handlePost,
ws: this._handleWs
},
path: this._path,
types: this._types
});

this._server = app.listen(this._port);

l.log(`Server started on port=${this._port} for type=${this._type.join(',')}`);
l.log(`Server started on port=${this._port} for type=${this._types.join(',')}`);
this.emit('started');

return true;
Expand Down
6 changes: 2 additions & 4 deletions packages/client-rpc/src/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ describe('Server', () => {
});
});

// FIXME: WebSocket server does not shutdown cleanly in the test environment
it.skip('starts and accepts requests, sending responses (WS)', () => {
it('starts and accepts requests, sending responses (WS)', () => {
server = new Server({ port: 9901, type: ['ws'] }, handlers); // eslint-disable-line

return new WsProvider('ws://localhost:9901')
Expand All @@ -267,8 +266,7 @@ describe('Server', () => {
});
});

// FIXME: As above
it.skip('starts and accepts requests, sending responses (HTTP & WS)', () => {
it('starts and accepts requests, sending responses (HTTP & WS)', () => {
server = new Server({ port: 9901, type: ['http', 'ws'] }, handlers); // eslint-disable-line

return new WsProvider('ws://localhost:9901')
Expand Down

0 comments on commit 3db9f82

Please sign in to comment.