mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-31 11:11:23 +00:00
BREAKING CHANGE: all API methods with peer-info parameters or return values were changed. You can check the API.md document, in order to check the new values to use
89 lines
1.9 KiB
JavaScript
89 lines
1.9 KiB
JavaScript
'use strict'
|
|
|
|
const errcode = require('err-code')
|
|
const PeerId = require('peer-id')
|
|
|
|
const {
|
|
ERR_INVALID_PARAMETERS
|
|
} = require('../errors')
|
|
|
|
/**
|
|
* The Book is the skeleton for the PeerStore books.
|
|
*/
|
|
class Book {
|
|
constructor (peerStore, eventName, eventProperty) {
|
|
this._ps = peerStore
|
|
this.eventName = eventName
|
|
this.eventProperty = eventProperty
|
|
|
|
/**
|
|
* Map known peers to their data.
|
|
* @type {Map<string, Array<Data>}
|
|
*/
|
|
this.data = new Map()
|
|
}
|
|
|
|
/**
|
|
* Set known data of a provided peer.
|
|
* @param {PeerId} peerId
|
|
* @param {Array<Data>|Data} data
|
|
*/
|
|
set (peerId, data) {
|
|
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
|
}
|
|
|
|
/**
|
|
* Add known data of a provided peer.
|
|
* @param {PeerId} peerId
|
|
* @param {Array<Data>|Data} data
|
|
*/
|
|
add (peerId, data) {
|
|
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
|
}
|
|
|
|
/**
|
|
* Get the known data of a provided peer.
|
|
* @param {PeerId} peerId
|
|
* @returns {Array<Data>}
|
|
*/
|
|
get (peerId) {
|
|
if (!PeerId.isPeerId(peerId)) {
|
|
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
|
}
|
|
|
|
const rec = this.data.get(peerId.toB58String())
|
|
|
|
return rec ? [...rec] : undefined
|
|
}
|
|
|
|
/**
|
|
* Deletes the provided peer from the book.
|
|
* @param {PeerId} peerId
|
|
* @returns {boolean}
|
|
*/
|
|
delete (peerId) {
|
|
if (!PeerId.isPeerId(peerId)) {
|
|
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
|
}
|
|
|
|
if (!this.data.delete(peerId.toB58String())) {
|
|
return false
|
|
}
|
|
|
|
this._ps.emit(this.eventName, {
|
|
peerId,
|
|
[this.eventProperty]: []
|
|
})
|
|
|
|
return true
|
|
}
|
|
|
|
_setPeerId (peerId) {
|
|
if (!this._ps.peerIds.get(peerId)) {
|
|
this._ps.peerIds.set(peerId.toB58String(), peerId)
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = Book
|