Skip to content

Commit

Permalink
chore: add typedefs peerstore book template
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Dec 3, 2020
1 parent e519a6c commit bb0cbc1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 75 deletions.
30 changes: 7 additions & 23 deletions src/peer-store/address-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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
})

/**
Expand Down Expand Up @@ -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.
*
Expand Down
23 changes: 12 additions & 11 deletions src/peer-store/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const passthrough = data => data
*/

/**
* @template T
* @template Data, GetData, EventData
*/
class Book {
/**
Expand All @@ -25,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: T) => T[]} [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, T[]|T>}
* @type {Map<string, Data>}
*/
this.data = new Map()
}
Expand All @@ -45,7 +47,7 @@ class Book {
* Set known data of a provided peer.
*
* @param {PeerId} peerId
* @param {T[]|T} data
* @param {unknown} data
*/
set (peerId, data) {
throw errcode(new Error('set must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
Expand All @@ -56,7 +58,7 @@ class Book {
*
* @protected
* @param {PeerId} peerId - peerId of the data to store
* @param {T} 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}
Expand All @@ -76,7 +78,7 @@ class Book {
*
* @protected
* @param {PeerId} peerId
* @param {any} [data]
* @param {Data | undefined} [data]
*/
_emit (peerId, data) {
this._ps.emit(this.eventName, {
Expand All @@ -90,7 +92,7 @@ class Book {
* Returns `undefined` if there is no available data for the given peer.
*
* @param {PeerId} peerId
* @returns {T[]|T|undefined}
* @returns {GetData | undefined}
*/
get (peerId) {
if (!PeerId.isPeerId(peerId)) {
Expand All @@ -99,8 +101,7 @@ class Book {

const rec = this.data.get(peerId.toB58String())

// @ts-ignore
return rec ? [...rec] : undefined
return this.getTransformer(rec)
}

/**
Expand All @@ -118,7 +119,7 @@ class Book {
return false
}

this._emit(peerId, [])
this._emit(peerId, undefined)

return true
}
Expand Down
22 changes: 3 additions & 19 deletions src/peer-store/key-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const {
*/

/**
* @extends {Book}
* @extends {Book<PeerId, PublicKey, PublicKey>}
*/
class KeyBook extends Book {
/**
Expand All @@ -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
})

/**
Expand Down Expand Up @@ -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
35 changes: 15 additions & 20 deletions src/peer-store/metadata-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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()
}
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -167,7 +159,10 @@ class MetadataBook extends Book {
return false
}

this._emit(peerId, key)
this._ps.emit(eventName, {
peerId,
[eventProperty]: key
})

return true
}
Expand Down
18 changes: 16 additions & 2 deletions src/peer-store/proto-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@ const {
* @typedef {import('./')} PeerStore
*/

// @extends {Book<Entry, Address, Multiaddr>}

/**
* @param {Set<string> | undefined} set
* @returns {string[] | undefined}
*/
const transformSetToArray = (set) => {
if (!set) {
return undefined
}
return Array.from(set)
}

/**
* @extends {Book}
* @extends {Book<Set<string>, string[], string[]>}
*
* @fires ProtoBook#change:protocols
*/
Expand All @@ -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)
})

/**
Expand Down

0 comments on commit bb0cbc1

Please sign in to comment.