import { Request , Response , NextFunction } from 'express' ; import { validator , LiskValidationError } from '@liskhq/lisk-validator' ; import { BaseChannel } from 'lisk-framework' ; import { paginateList } from '../utils' ; const getPeerSchema = { type : 'object' , properties : { limit : { type : 'string' , format : 'uint32' , description : 'Number of peers to be returned' , } , offset : { type : 'string' , format : 'uint32' , description : 'Offset to get peers after a specific point in a peer list' , } , state : { type : 'string' , enum : [ 'connected' , 'disconnected' ] , } , } , } ; enum PeerState { connected = 'connected' , disconnected = 'disconnected' } export interface PeerInfo { readonly ipAddress : string ; readonly port : number ; readonly networkIdentifier : string ; readonly networkVersion : string ; readonly nonce : string ; readonly options : { [ key ] : unknown ; } ; } export const getPeers = ( channel ) => async ( req , res , next ) : Promise < void > => { const errors = validator . validate ( getPeerSchema , req . query ) ; if ( errors . length ) { res . status ( 400 ) . send ( { errors : [ { message : new LiskValidationError ( [ ... errors ] ) . message } ] , } ) ; return ; } const { limit = 100 , offset = 0 , state = PeerState . connected } = req . query ; try { let peers ; if ( state === PeerState . disconnected ) { peers = await channel . invoke < ReadonlyArray < PeerInfo > > ( 'app:getDisconnectedPeers' ) ; } else { peers = await channel . invoke < ReadonlyArray < PeerInfo > > ( 'app:getConnectedPeers' ) ; } res . status ( 200 ) . json ( { meta : { count : peers . length , limit : + limit , offset : + offset } , data : paginateList ( peers , + limit , + offset ) , } ) ; } catch ( err ) { next ( err ) ; } } ;