mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32: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
|
* @typedef {Object} Entry
|
||||||
* @property {Address[]} addresses peer Addresses.
|
* @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 {
|
class AddressBook extends Book {
|
||||||
/**
|
/**
|
||||||
@ -56,12 +56,13 @@ class AddressBook extends Book {
|
|||||||
peerStore,
|
peerStore,
|
||||||
eventName: 'change:multiaddrs',
|
eventName: 'change:multiaddrs',
|
||||||
eventProperty: 'multiaddrs',
|
eventProperty: 'multiaddrs',
|
||||||
eventTransformer: (data) => {
|
eventTransformer: (entry) => {
|
||||||
if (!data.addresses) {
|
if (!entry || !entry.addresses) {
|
||||||
return []
|
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
|
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.
|
* Transforms received multiaddrs into Address.
|
||||||
*
|
*
|
||||||
|
@ -13,6 +13,9 @@ const passthrough = data => data
|
|||||||
* @typedef {import('./')} PeerStore
|
* @typedef {import('./')} PeerStore
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @template Data, GetData, EventData
|
||||||
|
*/
|
||||||
class Book {
|
class Book {
|
||||||
/**
|
/**
|
||||||
* The Book is the skeleton for the PeerStore books.
|
* The Book is the skeleton for the PeerStore books.
|
||||||
@ -22,18 +25,20 @@ class Book {
|
|||||||
* @param {PeerStore} properties.peerStore - PeerStore instance.
|
* @param {PeerStore} properties.peerStore - PeerStore instance.
|
||||||
* @param {string} properties.eventName - Name of the event to emit by the PeerStore.
|
* @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 {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._ps = peerStore
|
||||||
this.eventName = eventName
|
this.eventName = eventName
|
||||||
this.eventProperty = eventProperty
|
this.eventProperty = eventProperty
|
||||||
this.eventTransformer = eventTransformer
|
this.eventTransformer = eventTransformer
|
||||||
|
this.getTransformer = getTransformer
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map known peers to their data.
|
* Map known peers to their data.
|
||||||
*
|
*
|
||||||
* @type {Map<string, any[]|any>}
|
* @type {Map<string, Data>}
|
||||||
*/
|
*/
|
||||||
this.data = new Map()
|
this.data = new Map()
|
||||||
}
|
}
|
||||||
@ -42,7 +47,7 @@ class Book {
|
|||||||
* Set known data of a provided peer.
|
* Set known data of a provided peer.
|
||||||
*
|
*
|
||||||
* @param {PeerId} peerId
|
* @param {PeerId} peerId
|
||||||
* @param {any[]|any} data
|
* @param {unknown} data
|
||||||
*/
|
*/
|
||||||
set (peerId, data) {
|
set (peerId, data) {
|
||||||
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
||||||
@ -53,7 +58,7 @@ class Book {
|
|||||||
*
|
*
|
||||||
* @protected
|
* @protected
|
||||||
* @param {PeerId} peerId - peerId of the data to store
|
* @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 {Object} [options] - storing options.
|
||||||
* @param {boolean} [options.emit = true] - emit the provided data.
|
* @param {boolean} [options.emit = true] - emit the provided data.
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
@ -73,7 +78,7 @@ class Book {
|
|||||||
*
|
*
|
||||||
* @protected
|
* @protected
|
||||||
* @param {PeerId} peerId
|
* @param {PeerId} peerId
|
||||||
* @param {any} [data]
|
* @param {Data | undefined} [data]
|
||||||
*/
|
*/
|
||||||
_emit (peerId, data) {
|
_emit (peerId, data) {
|
||||||
this._ps.emit(this.eventName, {
|
this._ps.emit(this.eventName, {
|
||||||
@ -87,7 +92,7 @@ class Book {
|
|||||||
* Returns `undefined` if there is no available data for the given peer.
|
* Returns `undefined` if there is no available data for the given peer.
|
||||||
*
|
*
|
||||||
* @param {PeerId} peerId
|
* @param {PeerId} peerId
|
||||||
* @returns {any[]|any|undefined}
|
* @returns {GetData | undefined}
|
||||||
*/
|
*/
|
||||||
get (peerId) {
|
get (peerId) {
|
||||||
if (!PeerId.isPeerId(peerId)) {
|
if (!PeerId.isPeerId(peerId)) {
|
||||||
@ -96,8 +101,7 @@ class Book {
|
|||||||
|
|
||||||
const rec = this.data.get(peerId.toB58String())
|
const rec = this.data.get(peerId.toB58String())
|
||||||
|
|
||||||
// @ts-ignore
|
return this.getTransformer(rec)
|
||||||
return rec ? [...rec] : undefined
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -115,7 +119,7 @@ class Book {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
this._emit(peerId, [])
|
this._emit(peerId, undefined)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ const {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @extends {Book}
|
* @extends {Book<PeerId, PublicKey, PublicKey>}
|
||||||
*/
|
*/
|
||||||
class KeyBook extends Book {
|
class KeyBook extends Book {
|
||||||
/**
|
/**
|
||||||
@ -34,7 +34,8 @@ class KeyBook extends Book {
|
|||||||
peerStore,
|
peerStore,
|
||||||
eventName: 'change:pubkey',
|
eventName: 'change:pubkey',
|
||||||
eventProperty: '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
|
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
|
module.exports = KeyBook
|
||||||
|
@ -10,17 +10,23 @@ const uint8ArrayEquals = require('uint8arrays/equals')
|
|||||||
const PeerId = require('peer-id')
|
const PeerId = require('peer-id')
|
||||||
|
|
||||||
const Book = require('./book')
|
const Book = require('./book')
|
||||||
|
|
||||||
const {
|
const {
|
||||||
codes: { ERR_INVALID_PARAMETERS }
|
codes: { ERR_INVALID_PARAMETERS }
|
||||||
} = require('../errors')
|
} = require('../errors')
|
||||||
|
|
||||||
|
const eventName = 'change:metadata'
|
||||||
|
const eventProperty = 'metadata'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('./')} PeerStore
|
* @typedef {import('./')} PeerStore
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @extends {Book}
|
* @typedef {Map<string, Uint8Array>} Metadata
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends {Book<Metadata, Metadata, string>}
|
||||||
*
|
*
|
||||||
* @fires MetadataBook#change:metadata
|
* @fires MetadataBook#change:metadata
|
||||||
*/
|
*/
|
||||||
@ -39,14 +45,14 @@ class MetadataBook extends Book {
|
|||||||
*/
|
*/
|
||||||
super({
|
super({
|
||||||
peerStore,
|
peerStore,
|
||||||
eventName: 'change:metadata',
|
eventName,
|
||||||
eventProperty: 'metadata'
|
eventProperty
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map known peers to their known protocols.
|
* Map known peers to their known protocols.
|
||||||
*
|
*
|
||||||
* @type {Map<string, Map<string, Uint8Array>>}
|
* @type {Map<string, Metadata>}
|
||||||
*/
|
*/
|
||||||
this.data = new Map()
|
this.data = new Map()
|
||||||
}
|
}
|
||||||
@ -99,20 +105,6 @@ class MetadataBook extends Book {
|
|||||||
emit && this._emit(peerId, key)
|
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
|
* Get specific metadata value, if it exists
|
||||||
*
|
*
|
||||||
@ -167,7 +159,10 @@ class MetadataBook extends Book {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
this._emit(peerId, key)
|
this._ps.emit(eventName, {
|
||||||
|
peerId,
|
||||||
|
[eventProperty]: key
|
||||||
|
})
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,21 @@ const {
|
|||||||
* @typedef {import('./')} PeerStore
|
* @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
|
* @fires ProtoBook#change:protocols
|
||||||
*/
|
*/
|
||||||
@ -39,7 +52,8 @@ class ProtoBook extends Book {
|
|||||||
peerStore,
|
peerStore,
|
||||||
eventName: 'change:protocols',
|
eventName: 'change:protocols',
|
||||||
eventProperty: '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