Add pubsub interface definition (#54)

* Add pubsub interface definition

* Functional JsonRpcCoder

* Functional HttpProvider

* Functional WsProvider

* Functional split completed

* Split actual method subscription

* Update flow

* Flatten no user-specified array in sub call
This commit is contained in:
Jaco Greeff
2018-03-15 12:59:49 +01:00
committed by GitHub
parent e84bd1b36b
commit 21c14bd85c
66 changed files with 2500 additions and 1113 deletions
@@ -0,0 +1,29 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { JsonRpcResponse } from '../../types';
import type { RpcCoderState } from './types';
const assert = require('@polkadot/util/assert');
const isNumber = require('@polkadot/util/is/number');
const isUndefined = require('@polkadot/util/is/undefined');
module.exports = function decodeResponse (self: RpcCoderState, response: JsonRpcResponse): mixed {
assert(response, 'Empty response object received');
assert(response.jsonrpc === '2.0', 'Invalid jsonrpc field in decoded object');
assert(isNumber(response.id), 'Invalid id field in decoded object');
if (response.error) {
const { code, message } = response.error;
throw new Error(`[${code}]: ${message}`);
}
assert(!isUndefined(response.result), 'No result found in JsonRpc response');
return response.result;
};
@@ -0,0 +1,49 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const createCoder = require('./index');
describe('decodeResponse', () => {
let coder;
beforeEach(() => {
coder = createCoder();
});
it('expects a non-empty input object', () => {
expect(
() => coder.decodeResponse()
).toThrow(/Empty response/);
});
it('expects a valid jsonrpc field', () => {
expect(
() => coder.decodeResponse({})
).toThrow(/Invalid jsonrpc/);
});
it('expects a valid id field', () => {
expect(
() => coder.decodeResponse({ jsonrpc: '2.0' })
).toThrow(/Invalid id/);
});
it('expects a valid result field', () => {
expect(
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0' })
).toThrow(/No result/);
});
it('throws any error found', () => {
expect(
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0', error: { code: 123, message: 'test error' } })
).toThrow(/\[123\]: test error/);
});
it('returns the result', () => {
expect(
coder.decodeResponse({ id: 1, jsonrpc: '2.0', result: 'some result' })
).toEqual('some result');
});
});
@@ -0,0 +1,14 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { RpcCoderState } from './types';
const encodeObject = require('./encodeObject');
module.exports = function encodeJson (self: RpcCoderState, method: string, params: Array<mixed>): string {
return JSON.stringify(
encodeObject(self, method, params)
);
};
@@ -0,0 +1,19 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const createCoder = require('./index');
describe('encodeJson', () => {
let coder;
beforeEach(() => {
coder = createCoder();
});
it('encodes a valid JsonRPC JSON string', () => {
expect(
coder.encodeJson('method', 'params')
).toEqual('{"id":1,"jsonrpc":"2.0","method":"method","params":"params"}');
});
});
@@ -0,0 +1,16 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { JsonRpcRequest } from '../../types';
import type { RpcCoderState } from './types';
module.exports = function encodeObject (self: RpcCoderState, method: string, params: Array<mixed>): JsonRpcRequest {
return {
id: ++self.id,
jsonrpc: '2.0',
method,
params
};
};
@@ -0,0 +1,29 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
const createCoder = require('./index');
describe('encodeObject', () => {
let coder;
beforeEach(() => {
coder = createCoder();
});
it('starts with id === 0 (nothing sent)', () => {
expect(coder.getId()).toEqual(0);
});
it('encodes a valid JsonRPC object', () => {
expect(
coder.encodeObject('method', 'params')
).toEqual({
id: 1,
jsonrpc: '2.0',
method: 'method',
params: 'params'
});
expect(coder.getId()).toEqual(1);
});
});
@@ -0,0 +1,28 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { JsonRpcRequest, JsonRpcResponse } from '../../types';
import type { RpcCoder, RpcCoderState } from './types';
const decodeResponse = require('./decodeResponse');
const encodeJson = require('./encodeJson');
const encodeObject = require('./encodeObject');
module.exports = function rpcCoder (): RpcCoder {
const self: RpcCoderState = {
id: 0
};
return {
decodeResponse: (response: JsonRpcResponse): mixed =>
decodeResponse(self, response),
encodeJson: (method: string, params: Array<mixed>): string =>
encodeJson(self, method, params),
encodeObject: (method: string, params: Array<mixed>): JsonRpcRequest =>
encodeObject(self, method, params),
getId: (): number =>
self.id
};
};
@@ -0,0 +1,17 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { JsonRpcRequest, JsonRpcResponse } from '../../types';
export type RpcCoder = {
decodeResponse: (response: JsonRpcResponse) => mixed,
encodeObject: (method: string, params: Array<mixed>) => JsonRpcRequest,
encodeJson: (method: string, params: Array<mixed>) => string,
getId: () => number
};
export type RpcCoderState = {
id: number
};