mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 02:22:14 +00:00
chore: add typedefs peerstore book template
This commit is contained in:
parent
aa98bc2f0e
commit
222bb7bc88
@ -33,11 +33,11 @@ const Envelope = require('../record/envelope')
|
||||
*
|
||||
* @typedef {Object} Entry
|
||||
* @property {Address[]} addresses peer Addresses.
|
||||
* @property {CertifiedRecord} record certified peer record.
|
||||
* @property {CertifiedRecord} [record] certified peer record.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @extends {Book}
|
||||
* @extends {Book<Entry, Address[], Multiaddr[]>}
|
||||
*/
|
||||
class AddressBook extends Book {
|
||||
/**
|
||||
@ -56,12 +56,13 @@ class AddressBook extends Book {
|
||||
peerStore,
|
||||
eventName: 'change:multiaddrs',
|
||||
eventProperty: 'multiaddrs',
|
||||
eventTransformer: (data) => {
|
||||
if (!data.addresses) {
|
||||
eventTransformer: (entry) => {
|
||||
if (!entry || !entry.addresses) {
|
||||
return []
|
||||
}
|
||||
return data.addresses.map((address) => address.multiaddr)
|
||||
}
|
||||
return entry.addresses.map((address) => address.multiaddr)
|
||||
},
|
||||
getTransformer: (entry) => entry && entry.addresses ? [...entry.addresses] : undefined
|
||||
})
|
||||
|
||||
/**
|
||||
@ -263,23 +264,6 @@ class AddressBook extends Book {
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the known data of a provided peer.
|
||||
*
|
||||
* @override
|
||||
* @param {PeerId} peerId
|
||||
* @returns {Address[]|undefined}
|
||||
*/
|
||||
get (peerId) {
|
||||
if (!PeerId.isPeerId(peerId)) {
|
||||
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
||||
}
|
||||
|
||||
const entry = this.data.get(peerId.toB58String())
|
||||
|
||||
return entry && entry.addresses ? [...entry.addresses] : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms received multiaddrs into Address.
|
||||
*
|
||||
|
@ -13,6 +13,9 @@ const passthrough = data => data
|
||||
* @typedef {import('./')} PeerStore
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template Data, GetData, EventData
|
||||
*/
|
||||
class Book {
|
||||
/**
|
||||
* The Book is the skeleton for the PeerStore books.
|
||||
@ -22,18 +25,20 @@ class Book {
|
||||
* @param {PeerStore} properties.peerStore - PeerStore instance.
|
||||
* @param {string} properties.eventName - Name of the event to emit by the PeerStore.
|
||||
* @param {string} properties.eventProperty - Name of the property to emit by the PeerStore.
|
||||
* @param {(data: any) => any[]} [properties.eventTransformer] - Transformer function of the provided data for being emitted.
|
||||
* @param {(data: Data | undefined) => EventData | undefined} [properties.eventTransformer] - Transformer function of the provided data for being emitted.
|
||||
* @param {(data: Data | undefined) => GetData | undefined} [properties.getTransformer] - Transformer function of the provided data for being returned on get.
|
||||
*/
|
||||
constructor ({ peerStore, eventName, eventProperty, eventTransformer = passthrough }) {
|
||||
constructor ({ peerStore, eventName, eventProperty, eventTransformer = passthrough, getTransformer = passthrough }) {
|
||||
this._ps = peerStore
|
||||
this.eventName = eventName
|
||||
this.eventProperty = eventProperty
|
||||
this.eventTransformer = eventTransformer
|
||||
this.getTransformer = getTransformer
|
||||
|
||||
/**
|
||||
* Map known peers to their data.
|
||||
*
|
||||
* @type {Map<string, any[]|any>}
|
||||
* @type {Map<string, Data>}
|
||||
*/
|
||||
this.data = new Map()
|
||||
}
|
||||
@ -42,7 +47,7 @@ class Book {
|
||||
* Set known data of a provided peer.
|
||||
*
|
||||
* @param {PeerId} peerId
|
||||
* @param {any[]|any} data
|
||||
* @param {unknown} data
|
||||
*/
|
||||
set (peerId, data) {
|
||||
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
||||
@ -53,7 +58,7 @@ class Book {
|
||||
*
|
||||
* @protected
|
||||
* @param {PeerId} peerId - peerId of the data to store
|
||||
* @param {any} data - data to store.
|
||||
* @param {Data} data - data to store.
|
||||
* @param {Object} [options] - storing options.
|
||||
* @param {boolean} [options.emit = true] - emit the provided data.
|
||||
* @returns {void}
|
||||
@ -73,7 +78,7 @@ class Book {
|
||||
*
|
||||
* @protected
|
||||
* @param {PeerId} peerId
|
||||
* @param {any} [data]
|
||||
* @param {Data | undefined} [data]
|
||||
*/
|
||||
_emit (peerId, data) {
|
||||
this._ps.emit(this.eventName, {
|
||||
@ -87,7 +92,7 @@ class Book {
|
||||
* Returns `undefined` if there is no available data for the given peer.
|
||||
*
|
||||
* @param {PeerId} peerId
|
||||
* @returns {any[]|any|undefined}
|
||||
* @returns {GetData | undefined}
|
||||
*/
|
||||
get (peerId) {
|
||||
if (!PeerId.isPeerId(peerId)) {
|
||||
@ -96,8 +101,7 @@ class Book {
|
||||
|
||||
const rec = this.data.get(peerId.toB58String())
|
||||
|
||||
// @ts-ignore
|
||||
return rec ? [...rec] : undefined
|
||||
return this.getTransformer(rec)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -115,7 +119,7 @@ class Book {
|
||||
return false
|
||||
}
|
||||
|
||||
this._emit(peerId, [])
|
||||
this._emit(peerId, undefined)
|
||||
|
||||
return true
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ const {
|
||||
*/
|
||||
|
||||
/**
|
||||
* @extends {Book}
|
||||
* @extends {Book<PeerId, PublicKey, PublicKey>}
|
||||
*/
|
||||
class KeyBook extends Book {
|
||||
/**
|
||||
@ -34,7 +34,8 @@ class KeyBook extends Book {
|
||||
peerStore,
|
||||
eventName: 'change:pubkey',
|
||||
eventProperty: 'pubkey',
|
||||
eventTransformer: (data) => data.pubKey
|
||||
eventTransformer: (data) => data && data.pubKey,
|
||||
getTransformer: (data) => data && data.pubKey
|
||||
})
|
||||
|
||||
/**
|
||||
@ -74,23 +75,6 @@ class KeyBook extends Book {
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Public key of the given PeerId, if stored.
|
||||
*
|
||||
* @override
|
||||
* @param {PeerId} peerId
|
||||
* @returns {PublicKey | undefined}
|
||||
*/
|
||||
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.pubKey : undefined
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = KeyBook
|
||||
|
@ -10,17 +10,23 @@ const uint8ArrayEquals = require('uint8arrays/equals')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
const Book = require('./book')
|
||||
|
||||
const {
|
||||
codes: { ERR_INVALID_PARAMETERS }
|
||||
} = require('../errors')
|
||||
|
||||
const eventName = 'change:metadata'
|
||||
const eventProperty = 'metadata'
|
||||
|
||||
/**
|
||||
* @typedef {import('./')} PeerStore
|
||||
*/
|
||||
|
||||
/**
|
||||
* @extends {Book}
|
||||
* @typedef {Map<string, Uint8Array>} Metadata
|
||||
*/
|
||||
|
||||
/**
|
||||
* @extends {Book<Metadata, Metadata, string>}
|
||||
*
|
||||
* @fires MetadataBook#change:metadata
|
||||
*/
|
||||
@ -39,14 +45,14 @@ class MetadataBook extends Book {
|
||||
*/
|
||||
super({
|
||||
peerStore,
|
||||
eventName: 'change:metadata',
|
||||
eventProperty: 'metadata'
|
||||
eventName,
|
||||
eventProperty
|
||||
})
|
||||
|
||||
/**
|
||||
* Map known peers to their known protocols.
|
||||
*
|
||||
* @type {Map<string, Map<string, Uint8Array>>}
|
||||
* @type {Map<string, Metadata>}
|
||||
*/
|
||||
this.data = new Map()
|
||||
}
|
||||
@ -99,20 +105,6 @@ class MetadataBook extends Book {
|
||||
emit && this._emit(peerId, key)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the known data of a provided peer.
|
||||
*
|
||||
* @param {PeerId} peerId
|
||||
* @returns {Map<string, Uint8Array>|undefined}
|
||||
*/
|
||||
get (peerId) {
|
||||
if (!PeerId.isPeerId(peerId)) {
|
||||
throw errcode(new Error('peerId must be an instance of peer-id'), ERR_INVALID_PARAMETERS)
|
||||
}
|
||||
|
||||
return this.data.get(peerId.toB58String())
|
||||
}
|
||||
|
||||
/**
|
||||
* Get specific metadata value, if it exists
|
||||
*
|
||||
@ -167,7 +159,10 @@ class MetadataBook extends Book {
|
||||
return false
|
||||
}
|
||||
|
||||
this._emit(peerId, key)
|
||||
this._ps.emit(eventName, {
|
||||
peerId,
|
||||
[eventProperty]: key
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
|
@ -17,8 +17,21 @@ const {
|
||||
* @typedef {import('./')} PeerStore
|
||||
*/
|
||||
|
||||
// @extends {Book<Entry, Address, Multiaddr>}
|
||||
|
||||
/**
|
||||
* @extends {Book}
|
||||
* @param {Set<string> | undefined} set
|
||||
* @returns {string[] | undefined}
|
||||
*/
|
||||
const transformSetToArray = (set) => {
|
||||
if (!set) {
|
||||
return undefined
|
||||
}
|
||||
return Array.from(set)
|
||||
}
|
||||
|
||||
/**
|
||||
* @extends {Book<Set<string>, string[], string[]>}
|
||||
*
|
||||
* @fires ProtoBook#change:protocols
|
||||
*/
|
||||
@ -39,7 +52,8 @@ class ProtoBook extends Book {
|
||||
peerStore,
|
||||
eventName: 'change:protocols',
|
||||
eventProperty: 'protocols',
|
||||
eventTransformer: (data) => Array.from(data)
|
||||
eventTransformer: (data) => transformSetToArray(data) || [],
|
||||
getTransformer: (data) => transformSetToArray(data)
|
||||
})
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user