From b0c7e3a8ae1ec8d605444efd2a30d306e3307e9e Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sat, 18 Nov 2017 08:38:18 +0100 Subject: [PATCH] Initial add --- .babelrc | 12 +++ .coveralls.yml | 1 + .editorconfig | 10 +++ .eslintrc.json | 35 +++++++++ .flowconfig | 10 +++ .gitignore | 4 + .istanbul.yml | 4 + .npmignore | 13 ++++ .travis.yml | 4 + LICENSE | 6 ++ README.md | 40 ++++++++++ package.json | 75 ++++++++++++++++++ scripts/travis.sh | 56 ++++++++++++++ src/api.js | 71 ++++++++++++++++++ src/api.spec.js | 118 +++++++++++++++++++++++++++++ src/format/index.js | 10 +++ src/format/input/address.js | 10 +++ src/format/input/address.spec.js | 13 ++++ src/format/input/hex.js | 33 ++++++++ src/format/input/hex.spec.js | 53 +++++++++++++ src/format/input/index.js | 24 ++++++ src/format/input/index.spec.js | 22 ++++++ src/format/noop.js | 6 ++ src/format/noop.spec.js | 13 ++++ src/format/output/index.js | 18 +++++ src/format/output/index.spec.js | 13 ++++ src/format/types.js | 4 + src/format/util.js | 36 +++++++++ src/format/util.spec.js | 59 +++++++++++++++ src/index.js | 8 ++ src/index.spec.js | 11 +++ src/polyfill.js | 10 +++ src/provider/http.js | 37 +++++++++ src/provider/http.spec.js | 97 ++++++++++++++++++++++++ src/provider/index.js | 8 ++ src/provider/jsonRpcCoder.js | 55 ++++++++++++++ src/provider/jsonRpcCoder.spec.js | 77 +++++++++++++++++++ src/provider/types.js | 26 +++++++ src/provider/ws.js | 103 +++++++++++++++++++++++++ src/provider/ws.spec.js | 121 ++++++++++++++++++++++++++++++ src/types.js | 7 ++ src/util/hex.js | 28 +++++++ src/util/hex.spec.js | 67 +++++++++++++++++ src/util/is.js | 20 +++++ src/util/is.spec.js | 49 ++++++++++++ test/mocha.config.js | 10 +++ test/mocha.opts | 1 + test/mockHttp.js | 31 ++++++++ test/mockWs.js | 55 ++++++++++++++ 49 files changed, 1594 insertions(+) create mode 100644 .babelrc create mode 100644 .coveralls.yml create mode 100644 .editorconfig create mode 100644 .eslintrc.json create mode 100644 .flowconfig create mode 100644 .gitignore create mode 100644 .istanbul.yml create mode 100644 .npmignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 package.json create mode 100755 scripts/travis.sh create mode 100644 src/api.js create mode 100644 src/api.spec.js create mode 100644 src/format/index.js create mode 100644 src/format/input/address.js create mode 100644 src/format/input/address.spec.js create mode 100644 src/format/input/hex.js create mode 100644 src/format/input/hex.spec.js create mode 100644 src/format/input/index.js create mode 100644 src/format/input/index.spec.js create mode 100644 src/format/noop.js create mode 100644 src/format/noop.spec.js create mode 100644 src/format/output/index.js create mode 100644 src/format/output/index.spec.js create mode 100644 src/format/types.js create mode 100644 src/format/util.js create mode 100644 src/format/util.spec.js create mode 100644 src/index.js create mode 100644 src/index.spec.js create mode 100644 src/polyfill.js create mode 100644 src/provider/http.js create mode 100644 src/provider/http.spec.js create mode 100644 src/provider/index.js create mode 100644 src/provider/jsonRpcCoder.js create mode 100644 src/provider/jsonRpcCoder.spec.js create mode 100644 src/provider/types.js create mode 100644 src/provider/ws.js create mode 100644 src/provider/ws.spec.js create mode 100644 src/types.js create mode 100644 src/util/hex.js create mode 100644 src/util/hex.spec.js create mode 100644 src/util/is.js create mode 100644 src/util/is.spec.js create mode 100644 test/mocha.config.js create mode 100644 test/mocha.opts create mode 100644 test/mockHttp.js create mode 100644 test/mockWs.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 000000000000..30c9a9580c83 --- /dev/null +++ b/.babelrc @@ -0,0 +1,12 @@ +{ + "presets": [ + "env", + "stage-0" + ], + "plugins": [ + "transform-flow-strip-types", + "transform-class-properties", + "transform-object-rest-spread", + "transform-runtime" + ] +} diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 000000000000..cf27a3702483 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1 @@ +service_name: travis-pro diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000000..3c229b5dc188 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true +[*] +indent_style=space +indent_size=2 +tab_width=2 +end_of_line=lf +charset=utf-8 +trim_trailing_whitespace=true +max_line_length=120 +insert_final_newline=true diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 000000000000..4bee27805ffa --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,35 @@ +{ + "env": { + "browser": true, + "mocha": true, + "node": true + }, + "extends": [ + "semistandard", + "plugin:flowtype/recommended" + ], + "globals": { + "expect": true + }, + "parser": "babel-eslint", + "plugins": [ + "flowtype" + ], + "rules": { + "curly": ["error", "all"], + "newline-after-var": ["error", "always"], + "no-alert": "error", + "no-debugger": "error", + "no-duplicate-imports": ["error", { + "includeExports": true + }], + "object-curly-spacing": ["error", "always"], + "object-property-newline": 0, + "one-var-declaration-per-line": ["error", "always"], + "padded-blocks": ["error", { + "blocks": "never", + "classes": "never", + "switches": "never" + }] + } +} diff --git a/.flowconfig b/.flowconfig new file mode 100644 index 000000000000..ec8c008e3f00 --- /dev/null +++ b/.flowconfig @@ -0,0 +1,10 @@ +[ignore] + +[include] + +[libs] + +[options] +esproposal.decorators=ignore +module.ignore_non_literal_requires=true +unsafe.enable_getters_and_setters=true diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000000..49a8b5ab2283 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +coverage/ +lib/ +node_modules/ +tmp/ diff --git a/.istanbul.yml b/.istanbul.yml new file mode 100644 index 000000000000..5fdd3d7dc28b --- /dev/null +++ b/.istanbul.yml @@ -0,0 +1,4 @@ +instrumentation: + root: src + extensions: + - .js diff --git a/.npmignore b/.npmignore new file mode 100644 index 000000000000..30e898f5b1a0 --- /dev/null +++ b/.npmignore @@ -0,0 +1,13 @@ +coverage +scripts +src +test +tmp +.babelrc +.coveralls.yml +.editorconfig +.eslintrc.json +.flowconfig +.gitignore +.istanbul.yml +.travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000000..02e934b034ec --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - "8" +script: ./scripts/travis.sh diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000000..c35dc7b52e03 --- /dev/null +++ b/LICENSE @@ -0,0 +1,6 @@ +ISC License (ISC) +Copyright 2017 Jaco Greeff + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 000000000000..b4dfae886426 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# Polkadot JavaScript API + +[![Build Status](https://travis-ci.org/polkadot-js/api.svg?branch=master)](https://travis-ci.org/polkadot-js/api) +[![Coverage Status](https://coveralls.io/repos/github/polkadot-js/api/badge.svg?branch=master)](https://coveralls.io/github/polkadot-js/api?branch=master) +[![Dependency Status](https://david-dm.org/polkadot-js/api.svg)](https://david-dm.org/polkadot-js/api) +[![devDependency Status](https://david-dm.org/polkadot-js/api/dev-status.svg)](https://david-dm.org/polkadot-js/api#info=devDependencies) + +## Introduction + +Warning - currently this does not actually do all that much, it is an attempt to put into code some thoughts about how to maintain the endpoints. + +## Usage + +Installation - + +``` +npm install --save @polkadot/api +``` + +Initialisation - + +``` +import Api from '@polkadot/api'; + +const provider = new Api.HttpProvider('http://127.0.0.1:9933'); +const api = new Api(provider); +``` + +Making calls - + +``` +api.chain + .getHeader('0x1234567890') + .then((header) => console.log(header)) + .catch((error) => console.error(error)); +``` + +## Available methods + +For a list of currently exposed methods, see the [@polkadot/jsonrpc](https://github.com/polkadot-js/jsonrpc) repository. diff --git a/package.json b/package.json new file mode 100644 index 000000000000..bfdef6c75377 --- /dev/null +++ b/package.json @@ -0,0 +1,75 @@ +{ + "name": "@polkadot/api", + "version": "0.2.0", + "description": "A JavaScript wrapper for the Polkadot JsonRPC interface", + "main": "lib/index.js", + "keywords": [ + "Polkadot", + "JsonRPC" + ], + "author": "Jaco Greeff ", + "license": "ISC", + "engines": { + "node": ">=6.4" + }, + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/polkadot-js/api.git" + }, + "bugs": { + "url": "https://github.com/polkadot-js/api/issues" + }, + "homepage": "https://github.com/polkadot-js/api#readme", + "scripts": { + "build": "npm run build:js", + "build:js": "rimraf lib && babel src --out-dir lib --ignore *.spec.js,types.js", + "check": "npm run check:flow && npm run check:lint", + "check:flow": "flow check", + "check:lint": "eslint src", + "ci:makeshift": "makeshift", + "ci:coveralls": "coveralls < coverage/lcov.info", + "test": "cross-env NODE_ENV=test babel-istanbul cover _mocha 'src/*.spec.js' 'src/**/*.spec.js'" + }, + "devDependencies": { + "babel-cli": "^6.26.0", + "babel-core": "^6.26.0", + "babel-eslint": "^8.0.2", + "babel-istanbul": "^0.12.2", + "babel-plugin-transform-class-properties": "^6.24.1", + "babel-plugin-transform-flow-strip-types": "^6.22.0", + "babel-plugin-transform-object-rest-spread": "^6.26.0", + "babel-plugin-transform-runtime": "^6.23.0", + "babel-preset-env": "^1.6.1", + "babel-preset-flow": "^6.23.0", + "babel-preset-stage-0": "^6.24.1", + "chai": "^4.1.2", + "coveralls": "^3.0.0", + "cross-env": "^5.1.1", + "eslint": "^4.11.0", + "eslint-config-semistandard": "^11.0.0", + "eslint-config-standard": "^10.2.1", + "eslint-plugin-flowtype": "^2.39.1", + "eslint-plugin-import": "^2.8.0", + "eslint-plugin-node": "^5.2.1", + "eslint-plugin-promise": "^3.6.0", + "eslint-plugin-standard": "^3.0.1", + "flow-bin": "^0.59.0", + "makeshift": "^1.1.0", + "mocha": "^4.0.1", + "mock-socket": "^7.1.0", + "nock": "^9.1.0", + "rimraf": "^2.6.2", + "sinon": "^4.1.2", + "sinon-chai": "^2.14.0" + }, + "dependencies": { + "@polkadot/jsonrpc": "^0.2.0", + "babel-runtime": "^6.26.0", + "isomorphic-fetch": "^2.2.1", + "ws": "^3.3.1" + } +} diff --git a/scripts/travis.sh b/scripts/travis.sh new file mode 100755 index 000000000000..0921c8557541 --- /dev/null +++ b/scripts/travis.sh @@ -0,0 +1,56 @@ +#!/bin/bash +# ISC, Copyright 2017 Jaco Greeff + +set -e + +function setupGit () { + REPO=$1 + + echo "Setting up GitHub config for $REPO" + + git config push.default simple + git config merge.ours.driver true + git config user.name "Travis CI" + git config user.email "$COMMIT_AUTHOR_EMAIL" + git remote set-url origin https://${GH_TOKEN}@github.com/${REPO}.git > /dev/null 2>&1 +} + +echo "Running code checks & build" + +npm run check +npm run build +npm run test +npm run ci:coveralls + +# Pull requests and commits to other branches shouldn't try to deploy, just build to verify +if [ "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_BRANCH" != "master" ]; then + echo "Branch check completed" + + exit 0 +fi + +echo "Setting up GitHub config" + +setupGit ${TRAVIS_REPO_SLUG} + +if [ -n "$(git status --untracked-files=no --porcelain)" ]; then + echo "Adding build artifacts" + + git add . + git commit -m "[CI Skip] Build artifacts" +fi + +echo "Publishing to npm" + +npm run ci:makeshift +npm --no-git-tag-version version +npm version patch -m "[CI Skip] Version bump" +npm publish + +echo "Final push to GitHub" + +git push --quiet origin HEAD:refs/heads/$TRAVIS_BRANCH > /dev/null 2>&1 + +echo "Release completed" + +exit 0 diff --git a/src/api.js b/src/api.js new file mode 100644 index 000000000000..d1e7b8498d86 --- /dev/null +++ b/src/api.js @@ -0,0 +1,71 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +import type { InterfaceDefinition } from '@polkadot/jsonrpc/src/types'; +import type { ApiInterface } from './types'; +import type { ProviderInterface } from './provider/types'; + +const interfaces = require('@polkadot/jsonrpc'); +const { callSignature } = require('@polkadot/jsonrpc/lib/util'); +const { formatInputs, formatOutput } = require('./format'); +const { HttpProvider } = require('./provider'); +const { isFunction } = require('./util/is'); + +module.exports = class Api implements ApiInterface { + _provider: ProviderInterface; + _chainInterface: any = null; + _stateInterface: any = null; + + constructor (provider: ProviderInterface) { + if (!provider) { + throw new Error('Instantiate the Api with `new Api(new Provider(...))`'); + } + + if (!isFunction(provider.send)) { + throw new Error('Supplied provider does not expose send method'); + } + + this._provider = provider; + + this._chainInterface = this._createInterface(interfaces, 'chain'); + this._stateInterface = this._createInterface(interfaces, 'state'); + } + + get chain (): any { + return this._chainInterface; + } + + get state (): any { + return this._stateInterface; + } + + _createInterface (definitions: { [string]: InterfaceDefinition }, section: string): any { + const definition = definitions[section]; + + return Object + .keys(definition.methods) + .reduce((container, method: string) => { + const { inputs, output } = definition.methods[method]; + const rpcName = `${section}_${method}`; + + container[method] = async (..._params: Array): any => { + try { + if (inputs.length !== _params.length) { + throw new Error(`${inputs.length} params expected, found ${_params.length} instead`); + } + + const params = formatInputs(inputs, _params); + const result = await this._provider.send(rpcName, params); + + return formatOutput(output, result); + } catch (error) { + throw new Error(`${callSignature(rpcName, inputs, output)}:: ${error.message}`); + } + }; + + return container; + }, {}); + } + + static HttpProvider = HttpProvider; +}; diff --git a/src/api.spec.js b/src/api.spec.js new file mode 100644 index 000000000000..624146972c8f --- /dev/null +++ b/src/api.spec.js @@ -0,0 +1,118 @@ +// ISC, Copyright 2017 Jaco Greeff + +/* eslint-disable no-unused-expressions */ + +const sinon = require('sinon'); + +const { isFunction } = require('./util/is'); + +const Api = require('./api'); + +describe('Api', () => { + let api; + let provider; + + beforeEach(() => { + provider = { + send: (method, params) => { + return Promise.resolve(params[0]); + } + }; + sinon.spy(provider, 'send'); + api = new Api(provider); + }); + + afterEach(() => { + provider.send.restore(); + }); + + describe('constructor', () => { + it('requires a provider', () => { + expect( + () => new Api() + ).to.throw(/Instantiate the Api/); + }); + + it('requires a provider with a send method', () => { + expect( + () => new Api({}) + ).to.throw(/does not expose send/); + }); + + it('sets up the chain interface', () => { + expect(api.chain).to.be.ok; + }); + + it('sets up the state interface', () => { + expect(api.state).to.be.ok; + }); + }); + + describe('_createInterface', () => { + let container; + + beforeEach(() => { + container = api._createInterface({ + test: { + methods: { + blah: { + inputs: [ + { name: 'foo', type: 'Address' } + ], + output: { type: 'Address' } + }, + bleh: { + inputs: [], + output: { type: 'Address' } + } + } + } + }, 'test'); + }); + + describe('method expansion', () => { + it('adds the specified methods to the interface', () => { + expect(Object.keys(container)).to.deep.equal(['blah', 'bleh']); + }); + + it('had function calls for the attached methods', () => { + expect(isFunction(container.blah)).to.be.true; + expect(isFunction(container.bleh)).to.be.true; + }); + }); + + describe('calling', () => { + it('wraps errors with the call signature', () => { + return container + .blah() + .catch((error) => { + expect(error).to.match(/test_blah\(foo: Address\) => Address/); + }); + }); + + it('checks for mismatched parameters', () => { + return container + .bleh(1) + .catch((error) => { + expect(error).to.match(/0 params expected, found 1 instead/); + }); + }); + + it('calls the provider with the correct parameters', () => { + return container + .blah('0x123') + .then(() => { + expect(provider.send).to.have.been.calledWith('test_blah', [ + '0x0000000000000000000000000000000000000123' + ]); + }); + }); + }); + }); + + describe('static', () => { + it('exports Api.HttpProvider', () => { + expect(Api.HttpProvider).to.be.ok; + }); + }); +}); diff --git a/src/format/index.js b/src/format/index.js new file mode 100644 index 000000000000..18a3031f6954 --- /dev/null +++ b/src/format/index.js @@ -0,0 +1,10 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const formatInputs = require('./input'); +const formatOutput = require('./output'); + +module.exports = { + formatInputs, + formatOutput +}; diff --git a/src/format/input/address.js b/src/format/input/address.js new file mode 100644 index 000000000000..9809ffc21266 --- /dev/null +++ b/src/format/input/address.js @@ -0,0 +1,10 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const { formatH160 } = require('./hex'); + +// TODO: Currently the format assumes 160-bit values (like Ethereum) +// this will probably change along the way as things get firmed up +module.exports = function format (value: ?string): string { + return formatH160(value); +}; diff --git a/src/format/input/address.spec.js b/src/format/input/address.spec.js new file mode 100644 index 000000000000..954ce0b3408d --- /dev/null +++ b/src/format/input/address.spec.js @@ -0,0 +1,13 @@ +// ISC, Copyright 2017 Jaco Greeff + +const format = require('./address'); + +describe('format/input/address', () => { + describe('format', () => { + it('pads to H160 value', () => { + expect( + format('0x1234567890') + ).to.equal('0x0000000000000000000000000000001234567890'); + }); + }); +}); diff --git a/src/format/input/hex.js b/src/format/input/hex.js new file mode 100644 index 000000000000..169a04acba0a --- /dev/null +++ b/src/format/input/hex.js @@ -0,0 +1,33 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const { addHexPrefix, hasHexPrefix, stripHexPrefix } = require('../../util/hex'); + +const H64_ZERO: string = '00000000000000000000000000000000'; +const H128_ZERO: string = `${H64_ZERO}${H64_ZERO}`; +const H256_ZERO: string = `${H128_ZERO}${H128_ZERO}`; +// const H512_ZERO: string = `${H256_ZERO}${H256_ZERO}`; + +function leftHexPad (value: ?string, bitLength: number): string { + const length = 2 * bitLength / 8; + + if (hasHexPrefix(value)) { + value = stripHexPrefix(value); + } + + return addHexPrefix(`${H256_ZERO}${value || ''}`.slice(-length)); +} + +function formatH160 (value: ?string): string { + return leftHexPad(value, 160); +} + +function formatH256 (value: ?string): string { + return leftHexPad(value, 256); +} + +module.exports = { + leftHexPad, + formatH160, + formatH256 +}; diff --git a/src/format/input/hex.spec.js b/src/format/input/hex.spec.js new file mode 100644 index 000000000000..5f777cefa05d --- /dev/null +++ b/src/format/input/hex.spec.js @@ -0,0 +1,53 @@ +// ISC, Copyright 2017 Jaco Greeff + +const { leftHexPad, formatH160, formatH256 } = require('./hex'); + +describe('format/input/hex', () => { + describe('leftHexPad', () => { + it('padds to the required length', () => { + expect( + leftHexPad('0x123', 16) + ).to.equal('0x0123'); + }); + + it('padds to the required length (no prefix)', () => { + expect( + leftHexPad('123', 16) + ).to.equal('0x0123'); + }); + + it('pads null values correctly', () => { + expect( + leftHexPad(null, 16) + ).to.equal('0x0000'); + }); + }); + + describe('formatH160', () => { + it('pads to 40 bytes', () => { + expect( + formatH160('0x1234567890') + ).to.equal('0x0000000000000000000000000000001234567890'); + }); + + it('pads null values correctly', () => { + expect( + formatH160(null) + ).to.match(/^0x(00){20,}$/); + }); + }); + + describe('formatH256', () => { + it('pads to 64 bytes', () => { + expect( + formatH256('0x1234567890') + ).to.equal('0x0000000000000000000000000000000000000000000000000000001234567890'); + }); + + it('pads null values correctly', () => { + expect( + formatH256(null) + ).to.match(/^0x(00){32,}$/); + }); + }); +}); diff --git a/src/format/input/index.js b/src/format/input/index.js new file mode 100644 index 000000000000..aa96545e8f65 --- /dev/null +++ b/src/format/input/index.js @@ -0,0 +1,24 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +import type { FormatInputType, InterfaceInputType } from '@polkadot/jsonrpc/src/types'; +import type { FormatterFunction } from '../types'; + +const formatAddress = require('./address'); +const { formatH256 } = require('./hex'); +const formatNoop = require('../noop'); +const util = require('../util'); + +const formatters: { [FormatInputType]: FormatterFunction } = { + 'Address': formatAddress, + 'CallData': formatNoop, + 'H256': formatH256, + 'HeaderHash': formatH256, + 'String': formatNoop +}; + +module.exports = function format (inputs: Array, values: Array): Array { + const types = inputs.map(({ type }) => type); + + return util.formatArray(formatters, types, values); +}; diff --git a/src/format/input/index.spec.js b/src/format/input/index.spec.js new file mode 100644 index 000000000000..2c6c3abf3fbb --- /dev/null +++ b/src/format/input/index.spec.js @@ -0,0 +1,22 @@ +// ISC, Copyright 2017 Jaco Greeff + +const format = require('./index'); + +describe('format/input', () => { + describe('format', () => { + it('formats each value in an array', () => { + expect( + format( + [ + { name: 'foo', type: 'Address' }, + { name: 'bar', type: 'H256' } + ], + ['0x1234', '0xabcd'] + ) + ).to.deep.equal([ + '0x0000000000000000000000000000000000001234', + '0x000000000000000000000000000000000000000000000000000000000000abcd' + ]); + }); + }); +}); diff --git a/src/format/noop.js b/src/format/noop.js new file mode 100644 index 000000000000..6fc35ab9e808 --- /dev/null +++ b/src/format/noop.js @@ -0,0 +1,6 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +module.exports = function format (value: any): any { + return value; +}; diff --git a/src/format/noop.spec.js b/src/format/noop.spec.js new file mode 100644 index 000000000000..87502f00a42a --- /dev/null +++ b/src/format/noop.spec.js @@ -0,0 +1,13 @@ +// ISC, Copyright 2017 Jaco Greeff + +const format = require('./noop'); + +describe('format/noop', () => { + it('returns input value as output value', () => { + const input = { 'some': 'object' }; + + expect( + format(input) + ).to.equal(input); + }); +}); diff --git a/src/format/output/index.js b/src/format/output/index.js new file mode 100644 index 000000000000..d1d74151fc40 --- /dev/null +++ b/src/format/output/index.js @@ -0,0 +1,18 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +import type { FormatOutputType, InterfaceOutputType } from '@polkadot/jsonrpc/src/types'; +import type { FormatterFunction } from '../types'; + +const formatNoop = require('../noop'); +const util = require('../util'); + +const formatters: { [FormatOutputType]: FormatterFunction } = { + 'Header': formatNoop, + 'OutData': formatNoop, + 'StorageData': formatNoop +}; + +module.exports = function format (output: InterfaceOutputType, value: any): any { + return util.format(formatters, output.type, value); +}; diff --git a/src/format/output/index.spec.js b/src/format/output/index.spec.js new file mode 100644 index 000000000000..73ed7ff1477b --- /dev/null +++ b/src/format/output/index.spec.js @@ -0,0 +1,13 @@ +// ISC, Copyright 2017 Jaco Greeff + +const format = require('./index'); + +describe('format/output', () => { + describe('format', () => { + it('formats the value', () => { + expect( + format('Header', 'test') + ).to.deep.equal('test'); + }); + }); +}); diff --git a/src/format/types.js b/src/format/types.js new file mode 100644 index 000000000000..7099809f172a --- /dev/null +++ b/src/format/types.js @@ -0,0 +1,4 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +export type FormatterFunction = (value: any) => any; diff --git a/src/format/util.js b/src/format/util.js new file mode 100644 index 000000000000..7bb86e180801 --- /dev/null +++ b/src/format/util.js @@ -0,0 +1,36 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +import type { FormatterFunction } from './types'; + +const formatNoop = require('./noop'); +const { isUndefined } = require('../util/is'); + +function format (formatters: { [any]: FormatterFunction }, type: string, value: any): any { + const formatter = formatters[type]; + + if (isUndefined(formatter)) { + console.warn(`Unable to find default formatter for '${type}', falling back to noop`); + + return formatNoop(value); + } + + try { + return formatter(value); + } catch (error) { + throw new Error(`Error formatting '${value}' as '${type}'': ${error.message}`); + } +} + +function formatArray (formatters: { [any]: FormatterFunction }, types: Array, values: Array): Array { + return types.map((type, index) => { + const value = values[index]; + + return format(formatters, type, value); + }); +} + +module.exports = { + format, + formatArray +}; diff --git a/src/format/util.spec.js b/src/format/util.spec.js new file mode 100644 index 000000000000..ee6ecf29ddb4 --- /dev/null +++ b/src/format/util.spec.js @@ -0,0 +1,59 @@ +// ISC, Copyright 2017 Jaco Greeff + +const sinon = require('sinon'); + +const { format, formatArray } = require('./util'); + +describe('format/util', () => { + let formatters; + let addressStub; + let warnSpy; + + beforeEach(() => { + addressStub = sinon.stub(); + formatters = { + 'Address': addressStub, + 'Exception': () => { + throw new Error('something went wrong'); + } + }; + warnSpy = sinon.spy(console, 'warn'); + }); + + afterEach(() => { + console.warn.restore(); + }); + + describe('format', () => { + it('formats unknown types with a fallback', () => { + expect( + format(formatters, 'Unknown', 'test') + ).to.equal('test'); + }); + + it('logs a warning with unknown types', () => { + format(formatters, 'Unknown', 'test'); + expect(warnSpy).to.have.been.calledWith("Unable to find default formatter for 'Unknown', falling back to noop"); + }); + + it('formats known types using the supplied formatter', () => { + format(formatters, 'Address', '0xaddress'); + + expect(addressStub).to.have.been.calledWith('0xaddress'); + }); + + it('wraps exceptions with the type', () => { + expect( + () => format(formatters, 'Exception', 'test') + ).to.throw(/Error formatting 'test' as 'Exception'/); + }); + }); + + describe('formatArray', () => { + it('formats each value in an array', () => { + expect( + formatArray(formatters, ['Unknown', 'Unknown'], ['test', 'blah']) + ).to.deep.equal(['test', 'blah']); + }); + }); +}); diff --git a/src/index.js b/src/index.js new file mode 100644 index 000000000000..35b6ff5e3e47 --- /dev/null +++ b/src/index.js @@ -0,0 +1,8 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +require('./polyfill'); + +const Api = require('./api'); + +module.exports = Api; diff --git a/src/index.spec.js b/src/index.spec.js new file mode 100644 index 000000000000..49ddcb96b3d0 --- /dev/null +++ b/src/index.spec.js @@ -0,0 +1,11 @@ +// ISC, Copyright 2017 Jaco Greeff + +/* eslint-disable no-unused-expressions */ + +const Api = require('./index'); + +describe('index', () => { + it('exports default Api', () => { + expect(Api).to.be.ok; + }); +}); diff --git a/src/polyfill.js b/src/polyfill.js new file mode 100644 index 000000000000..d08aeb7d4d31 --- /dev/null +++ b/src/polyfill.js @@ -0,0 +1,10 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +if (typeof fetch === 'undefined') { + require('isomorphic-fetch'); +} + +if (typeof WebSocket === 'undefined') { + global.WebSocket = require('ws'); +} diff --git a/src/provider/http.js b/src/provider/http.js new file mode 100644 index 000000000000..b7de67273bc9 --- /dev/null +++ b/src/provider/http.js @@ -0,0 +1,37 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +import type { ProviderInterface } from './types'; + +const JsonRpcCoder = require('./jsonRpcCoder'); + +module.exports = class HttpProvider extends JsonRpcCoder implements ProviderInterface { + _endpoint: string; + + constructor (endpoint: string) { + super(); + + this._endpoint = endpoint; + } + + async send (method: string, params: Array): Promise { + const body = this.encodeJson(method, params); + const response = await fetch(this._endpoint, { + body, + headers: { + 'Accept': 'application/json', + 'Content-Length': `${body.length}`, + 'Content-Type': 'application/json' + }, + method: 'POST' + }); + + if (!response.ok) { + throw new Error(`[${response.status}]: ${response.statusText}`); + } + + const result = await response.json(); + + return this.decodeResponse(result); + } +}; diff --git a/src/provider/http.spec.js b/src/provider/http.spec.js new file mode 100644 index 000000000000..b7660e0cccab --- /dev/null +++ b/src/provider/http.spec.js @@ -0,0 +1,97 @@ +// ISC, Copyright 2017 Jaco Greeff + +/* eslint-disable no-unused-expressions */ + +const { mockHttp, TEST_HTTP_URL } = require('../../test/mockHttp'); + +const sinon = require('sinon'); + +const Http = require('./http'); + +describe('provider/Http', () => { + let http; + let mock; + + beforeEach(() => { + http = new Http(TEST_HTTP_URL); + + sinon.spy(http, 'encodeJson'); + sinon.spy(http, 'decodeResponse'); + }); + + afterEach(() => { + http.encodeJson.restore(); + http.decodeResponse.restore(); + + mock.done(); + }); + + describe('send', () => { + it('encodes requests', () => { + mock = mockHttp([{ + method: 'test_encoding', + reply: { + result: 'ok' + } + }]); + + return http + .send('test_encoding', ['param']) + .then((result) => { + expect(http.encodeJson).to.have.been.calledWith('test_encoding', ['param']); + }); + }); + + it('decodes responses', () => { + mock = mockHttp([{ + method: 'test_encoding', + reply: { + result: 'ok' + } + }]); + + return http + .send('test_encoding', ['param']) + .then((result) => { + expect(http.decodeResponse).to.have.been.calledWith({ + id: 1, + jsonrpc: '2.0', + result: 'ok' + }); + }); + }); + + it('passes the body through correctly', () => { + mock = mockHttp([{ + method: 'test_body', + reply: { + result: 'ok' + } + }]); + + return http + .send('test_body', ['param']) + .then((result) => { + expect(mock.body['test_body']).to.deep.equal({ + id: 1, + jsonrpc: '2.0', + method: 'test_body', + params: ['param'] + }); + }); + }); + + it('throws error when !response.ok', () => { + mock = mockHttp([{ + code: 500, + method: 'test_error' + }]); + + return http + .send('test_error', []) + .catch((error) => { + expect(error).to.match(/\[500\]: Internal Server/); + }); + }); + }); +}); diff --git a/src/provider/index.js b/src/provider/index.js new file mode 100644 index 000000000000..729345f56b57 --- /dev/null +++ b/src/provider/index.js @@ -0,0 +1,8 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const HttpProvider = require('./http'); + +module.exports = { + HttpProvider +}; diff --git a/src/provider/jsonRpcCoder.js b/src/provider/jsonRpcCoder.js new file mode 100644 index 000000000000..39ebe3191941 --- /dev/null +++ b/src/provider/jsonRpcCoder.js @@ -0,0 +1,55 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +import type { JsonRpcRequest, JsonRpcResponse } from './types'; + +const { isNumber, isUndefined } = require('../util/is'); + +module.exports = class JsonRpcCoder { + _id: number = 0; + + decodeResponse (response: JsonRpcResponse): any { + if (!response) { + throw new Error('Empty response object received'); + } + + if (response.jsonrpc !== '2.0') { + throw new Error('Invalid jsonrpc field in decoded object'); + } + + if (!isNumber(response.id)) { + throw new Error('Invalid id field in decoded object'); + } + + if (response.error) { + const { code, message } = response.error; + + throw new Error(`[${code}]: ${message}`); + } + + if (isUndefined(response.result)) { + throw new Error('No result found in JsonRpc response'); + } + + return response.result; + } + + encodeObject (method: string, params: Array): JsonRpcRequest { + return { + id: ++this._id, + jsonrpc: '2.0', + method, + params + }; + } + + encodeJson (method: string, params: Array): string { + return JSON.stringify( + this.encodeObject(method, params) + ); + } + + get id (): number { + return this._id; + } +}; diff --git a/src/provider/jsonRpcCoder.spec.js b/src/provider/jsonRpcCoder.spec.js new file mode 100644 index 000000000000..a9ad50cfea7d --- /dev/null +++ b/src/provider/jsonRpcCoder.spec.js @@ -0,0 +1,77 @@ +// ISC, Copyright 2017 Jaco Greeff + +const JsonRpcCoder = require('./jsonRpcCoder'); + +describe('provider/JsonRpcCoder', () => { + let coder; + + beforeEach(() => { + coder = new JsonRpcCoder(); + }); + + describe('id', () => { + it('starts with id === 0 (nothing sent)', () => { + expect(coder.id).to.equal(0); + }); + }); + + describe('decodeResponse', () => { + it('expects a non-empty input object', () => { + expect( + () => coder.decodeResponse() + ).to.throw(/Empty response/); + }); + + it('expects a valid jsonrpc field', () => { + expect( + () => coder.decodeResponse({}) + ).to.throw(/Invalid jsonrpc/); + }); + + it('expects a valid id field', () => { + expect( + () => coder.decodeResponse({ jsonrpc: '2.0' }) + ).to.throw(/Invalid id/); + }); + + it('expects a valid result field', () => { + expect( + () => coder.decodeResponse({ id: 1, jsonrpc: '2.0' }) + ).to.throw(/No result/); + }); + + it('throws any error found', () => { + expect( + () => coder.decodeResponse({ id: 1, jsonrpc: '2.0', error: { code: 123, message: 'test error' } }) + ).to.throw(/\[123\]: test error/); + }); + + it('returns the result', () => { + expect( + coder.decodeResponse({ id: 1, jsonrpc: '2.0', result: 'some result' }) + ).to.equal('some result'); + }); + }); + + describe('encodeObject', () => { + it('encodes a valid JsonRPC object', () => { + expect( + coder.encodeObject('method', 'params') + ).to.deep.equal({ + id: 1, + jsonrpc: '2.0', + method: 'method', + params: 'params' + }); + expect(coder.id).to.equal(1); + }); + }); + + describe('encodeJson', () => { + it('encodes a valid JsonRPC JSON string', () => { + expect( + coder.encodeJson('method', 'params') + ).to.equal('{"id":1,"jsonrpc":"2.0","method":"method","params":"params"}'); + }); + }); +}); diff --git a/src/provider/types.js b/src/provider/types.js new file mode 100644 index 000000000000..ab60b4d7cbd0 --- /dev/null +++ b/src/provider/types.js @@ -0,0 +1,26 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +export type JsonRpcObject = { + id: number; + jsonrpc: '2.0'; +}; + +export type JsonRpcRequest = JsonRpcObject & { + method: string; + params: Array; +}; + +export type JsonRpcResponseBase = { + error?: { + code: number, + message: string + }; + result?: any; +} + +export type JsonRpcResponse = JsonRpcObject & JsonRpcResponseBase; + +export interface ProviderInterface { + send (method: string, params: Array): Promise; +} diff --git a/src/provider/ws.js b/src/provider/ws.js new file mode 100644 index 000000000000..78ffad99b3bf --- /dev/null +++ b/src/provider/ws.js @@ -0,0 +1,103 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +import type { JsonRpcResponse, ProviderInterface } from './types'; + +type AwaitingType = { + callback: (error: ?Error, result: any) => void +}; + +type WsMessageType = { + data: any +}; + +const JsonRpcCoder = require('./jsonRpcCoder'); + +module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterface { + _autoConnect: boolean = true; + _endpoint: string; + _handlers: { [number]: AwaitingType } = {}; + _websocket: WebSocket; + + constructor (endpoint: string, autoConnect: boolean = true) { + super(); + + this._endpoint = endpoint; + this._autoConnect = autoConnect; + + if (autoConnect) { + this.connect(); + } + } + + connect = () => { + this._handlers = {}; + + this._websocket = new WebSocket(this._endpoint); + + this._websocket.onclose = this._onClose; + this._websocket.onerror = this._onError; + this._websocket.onmessage = this._onMessage; + this._websocket.onopen = this._onOpen; + } + + _onClose = () => { + console.log('disconnected from', this._endpoint); + + if (this._autoConnect) { + setTimeout(this.connect, 1000); + } + } + + _onError = (error: Event) => { + console.error(error); + } + + _onOpen = () => { + console.log('connected to', this._endpoint); + } + + _onMessage = (message: WsMessageType) => { + // {"jsonrpc":"2.0","id":2,"result":"0x9ce59a13059e417087c02d3236a0b1cc"} + // "jsonrpc": "2.0", "method": "eth_subscription", "params": { "result": {...} }, "subscription": "0x9ce59a13059e417087c02d3236a0b1cc" + const response: JsonRpcResponse = JSON.parse(message.data); + const handler = this._handlers[response.id]; + + if (!handler) { + console.error(`Unable to find handler for ${response.id}`); + return; + } + + try { + const result = this.decodeResponse(response); + + handler.callback(null, result); + } catch (error) { + handler.callback(error); + } + + delete this._handlers[response.id]; + } + + send (method: string, params: Array): Promise { + return new Promise((resolve, reject) => { + try { + this._websocket.send( + this.encodeObject(method, params) + ); + + this._handlers[this.id] = { + callback: (error: ?Error, result: any) => { + if (error) { + reject(error); + } else { + resolve(result); + } + } + }; + } catch (error) { + reject(error); + } + }); + } +}; diff --git a/src/provider/ws.spec.js b/src/provider/ws.spec.js new file mode 100644 index 000000000000..387ca40a024e --- /dev/null +++ b/src/provider/ws.spec.js @@ -0,0 +1,121 @@ +// ISC, Copyright 2017 Jaco Greeff + +/* eslint-disable no-unused-expressions */ + +const { mockWs, TEST_WS_URL } = require('../../test/mockWs'); + +const sinon = require('sinon'); + +const Ws = require('./ws'); + +let ws; +let mock; + +function createWs (requests, autoConnect = true) { + mock = autoConnect + ? mockWs(requests) + : null; + + ws = new Ws(TEST_WS_URL, autoConnect); + + sinon.spy(ws, 'encodeObject'); + sinon.spy(ws, 'decodeResponse'); + + return ws; +} + +describe('provider/Ws', () => { + afterEach(() => { + ws.encodeObject.restore(); + ws.decodeResponse.restore(); + + if (mock) { + mock.done(); + } + }); + + describe('send', () => { + it('handles internal errors', () => { + ws = createWs([], false); + + return ws + .send('test_encoding', ['param']) + .catch((error) => { + expect(error).to.be.ok; + }); + }); + + it('encodes requests', () => { + ws = createWs([{ + id: 1, + method: 'test_encoding', + reply: { + result: 'ok' + } + }]); + + return ws + .send('test_encoding', ['param']) + .then((result) => { + expect(ws.encodeObject).to.have.been.calledWith('test_encoding', ['param']); + }); + }); + + it('decodes responses', () => { + ws = createWs([{ + id: 1, + method: 'test_encoding', + reply: { + result: 'ok' + } + }]); + + return ws + .send('test_encoding', ['param']) + .then((result) => { + expect(ws.decodeResponse).to.have.been.calledWith({ + id: 1, + jsonrpc: '2.0', + result: 'ok' + }); + }); + }); + + it('passes the body through correctly', () => { + ws = createWs([{ + id: 1, + method: 'test_body', + reply: { + result: 'ok' + } + }]); + + return ws + .send('test_body', ['param']) + .then((result) => { + expect(mock.body['test_body']).to.deep.equal({ + id: 1, + jsonrpc: '2.0', + method: 'test_body', + params: ['param'] + }); + }); + }); + + it('throws error when !response.ok', () => { + ws = createWs([{ + id: 1, + error: { + code: 666, + message: 'error' + } + }]); + + return ws + .send('test_error', []) + .catch((error) => { + expect(error).to.match(/\[666\]: error/); + }); + }); + }); +}); diff --git a/src/types.js b/src/types.js new file mode 100644 index 000000000000..9a12855c3787 --- /dev/null +++ b/src/types.js @@ -0,0 +1,7 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +export interface ApiInterface { + +chain: any; + +state: any; +} diff --git a/src/util/hex.js b/src/util/hex.js new file mode 100644 index 000000000000..dacb263a659b --- /dev/null +++ b/src/util/hex.js @@ -0,0 +1,28 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +function addHexPrefix (value: ?string): string { + if (value && hasHexPrefix(value)) { + return value; + } + + return `0x${value || ''}`; +} + +function hasHexPrefix (value: ?string): boolean { + return !!(value && value.substr(0, 2) === '0x'); +} + +function stripHexPrefix (value: ?string): string { + if (value && hasHexPrefix(value)) { + return value.substr(2); + } + + return value || ''; +} + +module.exports = { + addHexPrefix, + hasHexPrefix, + stripHexPrefix +}; diff --git a/src/util/hex.spec.js b/src/util/hex.spec.js new file mode 100644 index 000000000000..be4bdccfb409 --- /dev/null +++ b/src/util/hex.spec.js @@ -0,0 +1,67 @@ +// ISC, Copyright 2017 Jaco Greeff + +/* eslint-disable no-unused-expressions */ + +const { addHexPrefix, hasHexPrefix, stripHexPrefix } = require('./hex'); + +describe('util/hex', () => { + describe('addHexPrefix', () => { + it('does not add when prefix is available', () => { + expect( + addHexPrefix('0x123') + ).to.equal('0x123'); + }); + + it('adds the prefix when it is not available', () => { + expect( + addHexPrefix('123') + ).to.equal('0x123'); + }); + + it('returns null as 0x', () => { + expect( + addHexPrefix(null) + ).to.equal('0x'); + }); + }); + + describe('hasHexPrefix', () => { + it('returns true when hex prefix is found', () => { + expect( + hasHexPrefix('0x123') + ).to.be.true; + }); + + it('returns false when no prefix attached', () => { + expect( + hasHexPrefix('123') + ).to.be.false; + }); + + it('returns false when null value supplied', () => { + expect( + hasHexPrefix(null) + ).to.be.false; + }); + }); + + describe('stripHexPrefix', () => { + it('returns the value as-is when no prefix', () => { + expect( + stripHexPrefix('01ab') + ).to.equal('01ab'); + }); + + it('returns an empty string when null value supplied', () => { + expect( + stripHexPrefix(null) + ).to.equal(''); + }); + + it('strips the prefix from other strings', () => { + expect( + stripHexPrefix('0x1223') + ).to.equal('1223'); + }); + }); +}); diff --git a/src/util/is.js b/src/util/is.js new file mode 100644 index 000000000000..be2d6b635d42 --- /dev/null +++ b/src/util/is.js @@ -0,0 +1,20 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +function isFunction (value: any): boolean { + return typeof value === 'function'; +} + +function isNumber (value: any): boolean { + return typeof value === 'number'; +} + +function isUndefined (value: any): boolean { + return typeof value === 'undefined'; +} + +module.exports = { + isFunction, + isNumber, + isUndefined +}; diff --git a/src/util/is.spec.js b/src/util/is.spec.js new file mode 100644 index 000000000000..dc2246420326 --- /dev/null +++ b/src/util/is.spec.js @@ -0,0 +1,49 @@ +// ISC, Copyright 2017 Jaco Greeff + +/* eslint-disable no-unused-expressions */ + +const { isFunction, isNumber, isUndefined } = require('./is'); + +describe('util/is', () => { + describe('isFunction', () => { + it('returns true on valid functions', () => { + expect( + isFunction(isFunction) + ).to.be.true; + }); + + it('returns false of invalid functions', () => { + expect( + isFunction('notAFunction') + ).to.be.false; + }); + }); + + describe('isNumber', () => { + it('returns true on valid numbers', () => { + expect( + isNumber(2) + ).to.be.true; + }); + + it('returns fals on invalid numbers', () => { + expect( + isNumber('2') + ).to.be.false; + }); + }); + + describe('isUndefined', () => { + it('returns true on undefined values', () => { + expect( + isUndefined() + ).to.be.true; + }); + + it('returns false on defined values', () => { + expect( + isUndefined(null) + ).to.be.false; + }); + }); +}); diff --git a/test/mocha.config.js b/test/mocha.config.js new file mode 100644 index 000000000000..ea8fc2b2a499 --- /dev/null +++ b/test/mocha.config.js @@ -0,0 +1,10 @@ +// ISC, Copyright 2017 Jaco Greeff + +require('../src/polyfill'); + +const chai = require('chai'); +const sinonChai = require('sinon-chai'); + +chai.use(sinonChai); + +global.expect = chai.expect; diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 000000000000..0ed8269b4ff3 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1 @@ +-r ./test/mocha.config diff --git a/test/mockHttp.js b/test/mockHttp.js new file mode 100644 index 000000000000..f33a37bf9989 --- /dev/null +++ b/test/mockHttp.js @@ -0,0 +1,31 @@ +// ISC, Copyright 2017 Jaco Greeff + +const nock = require('nock'); + +const TEST_HTTP_URL = 'http://localhost:9944'; + +function mockHttp (requests) { + nock.cleanAll(); + + return requests.reduce((scope, request, index) => { + return scope + .post('/') + .reply(request.code || 200, (uri, body) => { + if (body.method !== request.method) { + return { + error: `Invalid method ${body.method}, expected ${request.method}` + }; + } + + scope.body = scope.body || {}; + scope.body[request.method] = body; + + return Object.assign({ id: body.id, jsonrpc: '2.0' }, request.reply || {}); + }); + }, nock(TEST_HTTP_URL)); +} + +module.exports = { + TEST_HTTP_URL, + mockHttp +}; diff --git a/test/mockWs.js b/test/mockWs.js new file mode 100644 index 000000000000..6ca9b7618ac0 --- /dev/null +++ b/test/mockWs.js @@ -0,0 +1,55 @@ +// ISC, Copyright 2017 Jaco Greeff + +const { Server } = require('mock-socket'); + +const TEST_WS_URL = 'ws://localhost:9955'; + +let server; + +function mockWs (requests) { + server = new Server(TEST_WS_URL); + let requestCount = 0; + + server.on('message', (body) => { + try { + const request = requests[requestCount]; + const result = request.reply; + const response = request.error + ? { + id: request.id, + jsonrpc: '2.0', + error: { + code: request.error.code, + message: request.error.message + } + } + : { + id: request.id, + jsonrpc: '2.0', + result: result.result + }; + + scope.body[request.method] = body; + requestCount++; + + server.send(JSON.stringify(response)); + } catch (error) { + console.error('mock:onmessage', body, error); + } + }); + + const scope = { + body: {}, + requests: 0, + server, + done: () => server.stop(), + isDone: () => requestCount === requests.length + }; + + return scope; +} + +module.exports = { + TEST_WS_URL, + mockWs +};