Encode mock responses as JSON (#66)

* Encode mock responses as JSON

* Update primitives
This commit is contained in:
Jaco Greeff
2018-04-16 18:07:21 +02:00
committed by GitHub
parent 2e4a9e7793
commit d86cd86251
5 changed files with 73 additions and 79 deletions
+1 -1
View File
@@ -33,7 +33,7 @@
"@polkadot/api-jsonrpc": "^0.8.10"
},
"dependencies": {
"@polkadot/primitives-json": "^0.9.5",
"@polkadot/primitives-json": "^0.10.1",
"@polkadot/util": "^0.18.4",
"babel-runtime": "^6.26.0"
}
+3 -2
View File
@@ -8,6 +8,7 @@ import type { InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
const bnDecode = require('@polkadot/primitives-json/bn/decode');
const bytesDecode = require('@polkadot/primitives-json/bytes/decode');
const headerDecode = require('@polkadot/primitives-json/header/decode');
const isNull = require('@polkadot/util/is/null');
const isUndefined = require('@polkadot/util/is/undefined');
const util = require('./util');
@@ -20,8 +21,8 @@ const formatters = {
};
module.exports = function formatOutput (output: InterfaceOutputType, value?: mixed): ?mixed {
if (isUndefined(value)) {
return void 0;
if (isUndefined(value) || isNull(value)) {
return value;
}
return util.format(formatters, output.type, value);
+7 -7
View File
@@ -11,13 +11,13 @@ describe('formatOutput', () => {
expect(
JSON.stringify(
formatOutput({ type: 'Header' }, {
parentHash: '0x1234',
number: '0x1234',
stateRoot: '0x5678',
transactionRoot: '0xabcd',
digest: {
logs: ['0x5678', '0x789a']
}
},
extrinsicsRoot: '0xabcd',
number: '0x1234',
parentHash: '0x1234',
stateRoot: '0x5678'
})
)
).toEqual(
@@ -28,10 +28,10 @@ describe('formatOutput', () => {
new Uint8Array([0x78, 0x9a])
]
},
extrinsicsRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd]),
number: new BN(0x1234),
parentHash: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12, 0x34]),
stateRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x56, 0x78]),
transactionRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd])
stateRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x56, 0x78])
})
);
});
+10 -8
View File
@@ -5,27 +5,29 @@
import type { MockState, MockState$Subscription$Callback } from './types';
const BN = require('bn.js');
const headerEncode = require('@polkadot/primitives-json/header/encode');
const bnToU8a = require('@polkadot/util/bn/toU8a');
const randomAsHex = require('@polkadot/util-crypto/random/asHex');
const randomAsU8a = require('@polkadot/util-crypto/random/asU8a');
function makeBlockHeader (prevNumber: number) {
const blockNumber = prevNumber++;
function makeBlockHeader (prevNumber: BN) {
const blockNumber = prevNumber.addn(1);
return {
digest: {
logs: []
},
extrinsicsRoot: randomAsHex(),
extrinsicsRoot: randomAsU8a(),
number: blockNumber,
parentHash: prevNumber === -1
? '0x0000000000000000000000000000000000000000000000000000000000000000'
parentHash: prevNumber.eqn(-1)
? new Uint8Array(32)
: bnToU8a(prevNumber, 256),
stateRoot: bnToU8a(blockNumber, 256)
};
}
module.exports = function mocks ({ subscriptions }: MockState): void {
let newHead = makeBlockHeader(-1);
let newHead = makeBlockHeader(new BN(-1));
const updateSubs = (method, value) => {
subscriptions[method].lastValue = value;
@@ -45,6 +47,6 @@ module.exports = function mocks ({ subscriptions }: MockState): void {
setInterval(() => {
newHead = makeBlockHeader(newHead.number);
updateSubs('subscribe_newHead', newHead);
updateSubs('subscribe_newHead', headerEncode(newHead));
}, 5000);
};
+52 -61
View File
@@ -117,58 +117,58 @@
stylelint "^9.1.1"
stylelint-config-standard "^18.2.0"
"@polkadot/primitives-builder@^0.9.5":
version "0.9.5"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-builder/-/primitives-builder-0.9.5.tgz#bf4c354029a461d3c27c9d64514543f34f24b418"
"@polkadot/primitives-builder@^0.10.1":
version "0.10.1"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-builder/-/primitives-builder-0.10.1.tgz#1f930bb3629329bbb0a444c15ded864ef8e9db64"
dependencies:
"@polkadot/primitives" "^0.9.5"
"@polkadot/primitives-codec" "^0.9.5"
"@polkadot/util" "^0.17.3"
"@polkadot/util-crypto" "^0.17.3"
"@polkadot/util-triehash" "^0.17.3"
"@polkadot/primitives" "^0.10.1"
"@polkadot/primitives-codec" "^0.10.1"
"@polkadot/util" "^0.18.4"
"@polkadot/util-crypto" "^0.18.4"
"@polkadot/util-triehash" "^0.18.4"
babel-runtime "^6.26.0"
"@polkadot/primitives-codec@^0.9.5":
version "0.9.5"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-codec/-/primitives-codec-0.9.5.tgz#3ef157e8d6a3671945354b2f8b8379cfb4eb7b53"
"@polkadot/primitives-codec@^0.10.1":
version "0.10.1"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-codec/-/primitives-codec-0.10.1.tgz#54f42cbd87bab7af00a63768f845e4f50dc26c2a"
dependencies:
"@polkadot/primitives" "^0.9.5"
"@polkadot/primitives-builder" "^0.9.5"
"@polkadot/primitives-rlp" "^0.9.5"
"@polkadot/util" "^0.17.3"
"@polkadot/util-keyring" "^0.17.3"
"@polkadot/primitives" "^0.10.1"
"@polkadot/primitives-builder" "^0.10.1"
"@polkadot/primitives-rlp" "^0.10.1"
"@polkadot/util" "^0.18.4"
"@polkadot/util-keyring" "^0.18.4"
babel-runtime "^6.26.0"
"@polkadot/primitives-json@^0.9.5":
version "0.9.5"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-json/-/primitives-json-0.9.5.tgz#f5001a2ad820670271ebc3068d515493dd372d89"
"@polkadot/primitives-json@^0.10.1":
version "0.10.1"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-json/-/primitives-json-0.10.1.tgz#39bfec3ed6ddd8aef82bc12b6e3001d3a288af94"
dependencies:
"@polkadot/primitives" "^0.9.5"
"@polkadot/primitives-builder" "^0.9.5"
"@polkadot/util" "^0.17.3"
"@polkadot/primitives" "^0.10.1"
"@polkadot/primitives-builder" "^0.10.1"
"@polkadot/util" "^0.18.4"
babel-runtime "^6.26.0"
"@polkadot/primitives-rlp@^0.9.5":
version "0.9.5"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-rlp/-/primitives-rlp-0.9.5.tgz#c38e28a2ecdb9f7a7fb60b209ed1332f561abee0"
"@polkadot/primitives-rlp@^0.10.1":
version "0.10.1"
resolved "https://registry.yarnpkg.com/@polkadot/primitives-rlp/-/primitives-rlp-0.10.1.tgz#dd2fb7c9a990dc88132c88bc3128f5e749bbc7e3"
dependencies:
"@polkadot/primitives" "^0.9.5"
"@polkadot/primitives-builder" "^0.9.5"
"@polkadot/primitives-json" "^0.9.5"
"@polkadot/util" "^0.17.3"
"@polkadot/util-crypto" "^0.17.3"
"@polkadot/primitives" "^0.10.1"
"@polkadot/primitives-builder" "^0.10.1"
"@polkadot/primitives-json" "^0.10.1"
"@polkadot/util" "^0.18.4"
"@polkadot/util-crypto" "^0.18.4"
babel-runtime "^6.26.0"
"@polkadot/primitives@^0.9.5":
version "0.9.5"
resolved "https://registry.yarnpkg.com/@polkadot/primitives/-/primitives-0.9.5.tgz#3ae9d16b41dd0629294f397b2372e9bc29bda9a1"
"@polkadot/primitives@^0.10.1":
version "0.10.1"
resolved "https://registry.yarnpkg.com/@polkadot/primitives/-/primitives-0.10.1.tgz#c670f132eaed0cc32bd79177b285f178f3d4fa3d"
dependencies:
"@polkadot/util" "^0.17.3"
"@polkadot/util" "^0.18.4"
babel-runtime "^6.26.0"
"@polkadot/util-crypto@^0.17.3":
version "0.17.3"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.17.3.tgz#c72445aa13be10d189bf2e0581f06877e2c141c3"
"@polkadot/util-crypto@^0.18.4":
version "0.18.4"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.18.4.tgz#27e443ad46427cde49067a4b8525a99ffaf71df0"
dependencies:
babel-runtime "^6.26.0"
blakejs "^1.1.0"
@@ -176,39 +176,30 @@
tweetnacl "^1.0.0"
xxhashjs "^0.2.2"
"@polkadot/util-keyring@^0.17.3":
version "0.17.3"
resolved "https://registry.yarnpkg.com/@polkadot/util-keyring/-/util-keyring-0.17.3.tgz#d45677c8bddd4cc6c5589197102a314fb522ba01"
"@polkadot/util-keyring@^0.18.4":
version "0.18.4"
resolved "https://registry.yarnpkg.com/@polkadot/util-keyring/-/util-keyring-0.18.4.tgz#0e2bdbb3700275025632f42171a3fecacde513e3"
dependencies:
"@polkadot/util" "^0.17.3"
"@polkadot/util-crypto" "^0.17.3"
"@polkadot/util" "^0.18.4"
"@polkadot/util-crypto" "^0.18.4"
babel-runtime "^6.26.0"
"@polkadot/util-rlp@^0.17.3":
version "0.17.3"
resolved "https://registry.yarnpkg.com/@polkadot/util-rlp/-/util-rlp-0.17.3.tgz#df1f0b74bf226f625d484685657cd8fb3d9e2fd0"
"@polkadot/util-rlp@^0.18.4":
version "0.18.4"
resolved "https://registry.yarnpkg.com/@polkadot/util-rlp/-/util-rlp-0.18.4.tgz#0b18c2b710eb6bf06aae70245762738e71059ade"
dependencies:
"@polkadot/util" "^0.17.3"
"@polkadot/util" "^0.18.4"
babel-runtime "^6.26.0"
"@polkadot/util-triehash@^0.17.3":
version "0.17.3"
resolved "https://registry.yarnpkg.com/@polkadot/util-triehash/-/util-triehash-0.17.3.tgz#1ad4ac1a0086fcd0fc7c98aa0bef2f356b2a7f0a"
"@polkadot/util-triehash@^0.18.4":
version "0.18.4"
resolved "https://registry.yarnpkg.com/@polkadot/util-triehash/-/util-triehash-0.18.4.tgz#bee1b9a7d59da962e470b52d18270c36fa2d46d0"
dependencies:
"@polkadot/util" "^0.17.3"
"@polkadot/util-crypto" "^0.17.3"
"@polkadot/util-rlp" "^0.17.3"
"@polkadot/util" "^0.18.4"
"@polkadot/util-crypto" "^0.18.4"
"@polkadot/util-rlp" "^0.18.4"
babel-runtime "^6.26.0"
"@polkadot/util@^0.17.3":
version "0.17.3"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.17.3.tgz#9aefcc722d03c4560a9af08a03ee680809bc1b25"
dependencies:
babel-runtime "^6.26.0"
bn.js "^4.11.8"
deasync "^0.1.12"
ip-regex "^2.1.0"
"@polkadot/util@^0.18.4":
version "0.18.4"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.18.4.tgz#b1886a3d4663d302fe1234705f506fae95b371b8"