fix(rpc): RPC now responds to peers via orbitdb

This commit is contained in:
Chris Troutner
2021-04-05 20:42:54 -07:00
parent 75cec6cb78
commit 6e79636d50
6 changed files with 147 additions and 32 deletions
+3 -5
View File
@@ -15,8 +15,8 @@ const config = require('../config') // this first.
const IPFSLib = require('../src/lib/ipfs')
const AdminLib = require('../src/lib/admin')
const adminLib = new AdminLib()
const JSONRPC = require('../src/rpc')
const rpc = new JSONRPC()
// const JSONRPC = require('../src/rpc')
// const rpc = new JSONRPC()
const errorMiddleware = require('../src/middleware')
const wlogger = require('../src/lib/wlogger')
@@ -73,9 +73,7 @@ async function startServer () {
if (success) console.log('System admin user created.')
// Start the IPFS node.
const ipfsLib = new IPFSLib({
rpc
})
const ipfsLib = new IPFSLib()
await ipfsLib.start()
return app
+14 -5
View File
@@ -5,20 +5,25 @@
// Global npm libraries
const IPFS = require('ipfs')
const IpfsCoord = require('ipfs-coord')
// const IpfsCoord = require('ipfs-coord')
const IpfsCoord = require('../../../ipfs-coord')
const BCHJS = require('@psf/bch-js')
// Local libraries
const JSONRPC = require('../rpc')
class IPFSLib {
constructor (localConfig) {
// Encapsulate dependencies
this.IPFS = IPFS
this.IpfsCoord = IpfsCoord
this.bchjs = new BCHJS()
this.rpc = new JSONRPC()
this.rpc = {}
if (localConfig.rpc) {
this.rpc = localConfig.rpc
}
// this.rpc = {}
// if (localConfig.rpc) {
// this.rpc = localConfig.rpc
// }
}
// This is a 'macro' start method. It kicks off several smaller methods that
@@ -26,6 +31,10 @@ class IPFSLib {
async start () {
await this.startIpfs()
await this.startIpfsCoord()
// Update the RPC instance with the instance of ipfs-coord.
this.rpc.ipfsCoord = this.ipfsCoord
console.log('IPFS is ready.')
}
+34 -7
View File
@@ -6,13 +6,15 @@
const jsonrpc = require('jsonrpc-lite')
// Local libraries
const AuthLib = require('../../lib/auth')
// const AuthLib = require('../../lib/auth')
const UserLib = require('../../lib/users')
class AuthRPC {
constructor (localConfig) {
// Encapsulate dependencies
this.authLib = new AuthLib()
// this.authLib = new AuthLib()
this.jsonrpc = jsonrpc
this.userLib = new UserLib()
}
// Top-level router for this library. All other methods in this class are for
@@ -26,13 +28,38 @@ class AuthRPC {
// Route the call based on the value of the method property.
switch (rpcData.payload.method) {
case 'getAll':
return await this.getAll()
case 'getUser':
return await this.getUser(rpcData)
case 'authUser':
return await this.authUser()
}
} catch (err) {
console.error('Error in UsersRPC/rpcRouter()')
console.error('Error in AuthRPC/authRouter()')
throw err
}
}
async authUser (rpcData) {
try {
// const user = await this.passport.authUser(ctx, next)
// if (!user) {
// // ctx.throw(401)
// const retJson =
// }
const user = await this.userLib.authUser()
return user
// const token = user.generateToken()
//
// const response = user.toJSON()
//
// delete response.password
//
// ctx.body = {
// token,
// user: response
// }
} catch (err) {
console.error('Error in authUser()')
throw err
}
}
+22 -7
View File
@@ -8,7 +8,7 @@ const jsonrpc = require('jsonrpc-lite')
// Local support libraries
const wlogger = require('../lib/wlogger')
const UserController = require('./users')
// const AuthController = require('./auth')
const AuthController = require('./auth')
let _this
@@ -17,6 +17,10 @@ class JSONRPC {
// Encapsulate dependencies
this.jsonrpc = jsonrpc
this.userController = new UserController()
this.authController = new AuthController()
// This will be replaced once the ipfs-coord lib finishes initializing.
this.ipfsCoord = {}
_this = this
}
@@ -28,7 +32,7 @@ class JSONRPC {
console.log('router str: ', str)
console.log('router from: ', from)
// Exit if from is not specified.
// Exit quietly if 'from' is not specified.
if (!from || typeof from !== 'string') {
// console.warn(
// 'Warning: Can not send JSON RPC response. Can not determine which peer this message came from.'
@@ -38,16 +42,16 @@ class JSONRPC {
// Attempt to parse the incoming data as a JSON RPC string.
const parsedData = _this.jsonrpc.parse(str)
// console.log('parsedData: ', parsedData)
console.log('parsedData: ', parsedData)
// Exit quietly if the incoming string is an invalid JSON RPC string.
if (parsedData.type === 'invalid') {
return
}
console.log('ping01')
// Default return string
let retStr = this.defaultResponse()
let retStr = _this.defaultResponse()
console.log('ping02')
// Route the command to the appropriate route handler.
switch (parsedData.payload.id) {
case 'users':
@@ -57,8 +61,16 @@ class JSONRPC {
retStr = await _this.authController.authRouter(parsedData)
break
}
console.log('ping03')
console.log('retStr: ', retStr)
return retStr
// TODO: Instead of returning a string, I need to write the return string
// to the OrbitDB of the 'from' peer.
// return retStr
console.log('_this.ipfsCoord.ipfs: ', _this.ipfsCoord.ipfs)
await _this.ipfsCoord.ipfs.orbitdb.sendToDb(from, retStr)
console.log('ping04')
} catch (err) {
wlogger.error('Error in rpc router(): ', err)
// Do not throw error. This is a top-level function.
@@ -68,11 +80,14 @@ class JSONRPC {
// The default JSON RPC response if the incoming command could not be routed.
defaultResponse () {
try {
console.log('Entering defaultResponse()')
const errorObj = this.jsonrpc.error(
'Can not route',
new jsonrpc.JsonRpcError('Input does not match routing rules', 422)
)
const errorStr = JSON.stringify(errorObj)
console.log('Exiting defaultResponse()')
return errorStr
} catch (err) {
console.error('Error in defaultResponse()')
@@ -1,5 +1,5 @@
/*
Unit tests for the rpc/users/index.js file.
Unit tests for the rpc/auth/index.js file.
*/
// Public npm libraries
@@ -8,9 +8,9 @@ const mongoose = require('mongoose')
const config = require('../../../config')
const UserRPC = require('../../../src/rpc/users')
const AuthRPC = require('../../../src/rpc/auth')
describe('#UserRPC', () => {
describe('#AuthRPC', () => {
let uut
before(async () => {
@@ -30,17 +30,18 @@ describe('#UserRPC', () => {
beforeEach(() => {
// sandbox = sinon.createSandbox()
uut = new UserRPC()
uut = new AuthRPC()
})
after(() => {
mongoose.connection.close()
})
describe('#getAll', () => {
it('should do something', async () => {
const result = await uut.getAll()
console.log('result: ', result)
describe('#authUser', () => {
it('should return a JWT token if user successfully authenticates', async () => {
const rpcData = 'placeholder'
await uut.authUser(rpcData)
})
})
})
+65
View File
@@ -0,0 +1,65 @@
/*
Unit tests for the rpc/users/index.js file.
*/
// Public npm libraries
const jsonrpc = require('jsonrpc-lite')
const mongoose = require('mongoose')
const sinon = require('sinon')
const assert = require('chai').assert
const config = require('../../../config')
const UserRPC = require('../../../src/rpc/users')
describe('#UserRPC', () => {
let uut
let sandbox
before(async () => {
// Connect to the Mongo Database.
console.log(`Connecting to database: ${config.database}`)
mongoose.Promise = global.Promise
mongoose.set('useCreateIndex', true) // Stop deprecation warning.
await mongoose.connect(
config.database,
{
useUnifiedTopology: true,
useNewUrlParser: true
}
)
})
beforeEach(() => {
sandbox = sinon.createSandbox()
uut = new UserRPC()
})
afterEach(() => sandbox.restore())
after(() => {
mongoose.connection.close()
})
describe('#userRouter', () => {
it('should route to the getAll method', async () => {
// Mock dependencies
sandbox.stub(uut, 'getAll').resolves(true)
const userCall = jsonrpc.request('users', 'getAll', {})
const jsonStr = JSON.stringify(userCall, null, 2)
const result = await uut.userRouter(jsonStr)
assert.equal(result, true)
})
})
describe('#getAll', () => {
it('should do something', async () => {
const result = await uut.getAll()
console.log('result: ', result)
})
})
})