diff --git a/package-lock.json b/package-lock.json index a05221c..52c1532 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { + "@open-rpc/client-js": "^1.7.0", "@psf/bch-js": "^4.20.2", "axios": "^0.21.1", "bcryptjs": "^2.4.3", @@ -1670,6 +1671,17 @@ "@octokit/openapi-types": "^6.0.0" } }, + "node_modules/@open-rpc/client-js": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/@open-rpc/client-js/-/client-js-1.7.0.tgz", + "integrity": "sha512-cRGJbXTgdhJNU49vWzJIATRmKBLP2x6tuHJzX9Jg3N8f1VEkge0riUEek2LFIrZiM4TdUp8XV4Ns1W0SZzdfSw==", + "dependencies": { + "isomorphic-fetch": "^3.0.0", + "isomorphic-ws": "^4.0.1", + "strict-event-emitter-types": "^2.0.0", + "ws": "^7.0.0" + } + }, "node_modules/@protobufjs/aspromise": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@protobufjs/aspromise/-/aspromise-1.1.2.tgz", @@ -12810,6 +12822,23 @@ "node": ">=10" } }, + "node_modules/isomorphic-fetch": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-3.0.0.tgz", + "integrity": "sha512-qvUtwJ3j6qwsF3jLxkZ72qCgjMysPzDfeV240JHiGZsANBYd+EEuu35v7dfrJ9Up0Ak07D7GGSkGhCHTqg/5wA==", + "dependencies": { + "node-fetch": "^2.6.1", + "whatwg-fetch": "^3.4.1" + } + }, + "node_modules/isomorphic-ws": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/isomorphic-ws/-/isomorphic-ws-4.0.1.tgz", + "integrity": "sha512-BhBvN2MBpWTaSHdWRb/bwdZJ1WaehQ2L1KngkCkfLUGF0mAWAT1sQUQacEmQ0jXkFw/czDXPNQSL5u2/Krsz1w==", + "peerDependencies": { + "ws": "*" + } + }, "node_modules/isstream": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", @@ -24716,6 +24745,11 @@ "node": ">=10" } }, + "node_modules/strict-event-emitter-types": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/strict-event-emitter-types/-/strict-event-emitter-types-2.0.0.tgz", + "integrity": "sha512-Nk/brWYpD85WlOgzw5h173aci0Teyv8YdIAEtV+N88nDB0dLlazZyJMIsN6eo1/AR61l+p6CJTG1JIyFaoNEEA==" + }, "node_modules/string_decoder": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", @@ -25812,6 +25846,11 @@ "@zxing/text-encoding": "0.9.0" } }, + "node_modules/whatwg-fetch": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.2.tgz", + "integrity": "sha512-bJlen0FcuU/0EMLrdbJ7zOnW6ITZLrZMIarMUVmdKtsGvZna8vxKYaexICWPfZ8qwf9fzNq+UEIZrnSaApt6RA==" + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -27586,6 +27625,17 @@ "@octokit/openapi-types": "^6.0.0" } }, + "@open-rpc/client-js": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/@open-rpc/client-js/-/client-js-1.7.0.tgz", + "integrity": "sha512-cRGJbXTgdhJNU49vWzJIATRmKBLP2x6tuHJzX9Jg3N8f1VEkge0riUEek2LFIrZiM4TdUp8XV4Ns1W0SZzdfSw==", + "requires": { + "isomorphic-fetch": "^3.0.0", + "isomorphic-ws": "^4.0.1", + "strict-event-emitter-types": "^2.0.0", + "ws": "^7.0.0" + } + }, "@protobufjs/aspromise": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@protobufjs/aspromise/-/aspromise-1.1.2.tgz", @@ -37019,6 +37069,21 @@ "resolved": "https://registry.npmjs.org/iso-url/-/iso-url-0.4.7.tgz", "integrity": "sha512-27fFRDnPAMnHGLq36bWTpKET+eiXct3ENlCcdcMdk+mjXrb2kw3mhBUg1B7ewAC0kVzlOPhADzQgz1SE6Tglog==" }, + "isomorphic-fetch": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-3.0.0.tgz", + "integrity": "sha512-qvUtwJ3j6qwsF3jLxkZ72qCgjMysPzDfeV240JHiGZsANBYd+EEuu35v7dfrJ9Up0Ak07D7GGSkGhCHTqg/5wA==", + "requires": { + "node-fetch": "^2.6.1", + "whatwg-fetch": "^3.4.1" + } + }, + "isomorphic-ws": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/isomorphic-ws/-/isomorphic-ws-4.0.1.tgz", + "integrity": "sha512-BhBvN2MBpWTaSHdWRb/bwdZJ1WaehQ2L1KngkCkfLUGF0mAWAT1sQUQacEmQ0jXkFw/czDXPNQSL5u2/Krsz1w==", + "requires": {} + }, "isstream": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", @@ -46632,6 +46697,11 @@ "resolved": "https://registry.npmjs.org/streaming-iterables/-/streaming-iterables-5.0.4.tgz", "integrity": "sha512-nEs6hBGIPsVz6uq6pscGGKfoPDQWrDQW0b0UHurtSDysekfKLmkPg7FQVRE2sj3Rad6yUo9E1sGTxOWyYsHQ/g==" }, + "strict-event-emitter-types": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/strict-event-emitter-types/-/strict-event-emitter-types-2.0.0.tgz", + "integrity": "sha512-Nk/brWYpD85WlOgzw5h173aci0Teyv8YdIAEtV+N88nDB0dLlazZyJMIsN6eo1/AR61l+p6CJTG1JIyFaoNEEA==" + }, "string_decoder": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", @@ -47538,6 +47608,11 @@ "@zxing/text-encoding": "0.9.0" } }, + "whatwg-fetch": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.2.tgz", + "integrity": "sha512-bJlen0FcuU/0EMLrdbJ7zOnW6ITZLrZMIarMUVmdKtsGvZna8vxKYaexICWPfZ8qwf9fzNq+UEIZrnSaApt6RA==" + }, "which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/test/e2e/manual/client/lib/test-utils.js b/test/e2e/manual/client/lib/test-utils.js index 561531d..e87b14b 100644 --- a/test/e2e/manual/client/lib/test-utils.js +++ b/test/e2e/manual/client/lib/test-utils.js @@ -5,10 +5,57 @@ const BCHJS = require('@psf/bch-js') const IpfsCoord = require('ipfs-coord') const IPFS = require('ipfs') +const EventEmitter = require('events') + +let _this class TestUtils { constructor (localConfig = {}) { this.bchjs = new BCHJS() + this.eventEmitter = new EventEmitter() + + _this = this + } + + rpcHandler (inData) { + try { + console.log('inData: ', inData) + + _this.eventEmitter.emit('rpcData', inData) + } catch (err) { + console.error('Error in rpcHandler: ', err) + // Do not throw an error. This is a top-level function. + } + } + + async sendRPC (ipfsId, cmdStr) { + try { + await this.ipfsCoord.ipfs.orbitdb.sendToDb(ipfsId, cmdStr) + + let retData + + this.eventEmitter.on('rpcData', inData => { + retData = inData + }) + + const start = new Date() + let now = start + let timeDiff = 0 + + do { + await this.bchjs.Util.sleep(1000) + + now = new Date() + + timeDiff = now.getTime() - start.getTime() + console.log('timeDiff: ', timeDiff) + } while (!retData || timeDiff > 10000) // eslint-disable-line no-unmodified-loop-condition + + return retData + } catch (err) { + console.error('Error in sendRPC') + throw err + } } async startIpfs () { @@ -23,7 +70,7 @@ class TestUtils { type: 'node.js', // type: 'browser', bchjs: this.bchjs, - privateLog: console.log, // Default to console.log + privateLog: this.rpcHandler, // Default to console.log isCircuitRelay: false, apiInfo: 'none', announceJsonLd: announceJsonLd @@ -70,8 +117,11 @@ class TestUtils { console.log(`Publishing message to ${ipfsId}`) // await this.ipfsCoord.ipfs.pubsub.publishToPubsubChannel(ipfsId, cmdStr) - await this.ipfsCoord.ipfs.orbitdb.sendToDb(ipfsId, cmdStr) + // await this.ipfsCoord.ipfs.orbitdb.sendToDb(ipfsId, cmdStr) + const result = await this.sendRPC(ipfsId, cmdStr) console.log('E2E TEST: Balance command sent.') + + console.log('result: ', result) } catch (err) { console.error('Error in getBalance()') throw err