mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-27 04:41:03 +00:00
* fix: use new libp2p-crypto
* fix: remove node globals and reduce size
- adds buffer require
- adds globalThis where needed
- streaming-iterables was remove and new utils created, this will be consolidated in `ipfs-utils` and backported here to normalize all these iterator helper functions
- latency-monitor was copied inside the repo
- iso-random-stream is now used instead of node crypto
- the test `should ignore self on discovery` was moved to node only
Size: 172.97KB
47.03KB below the 220KB limit.
`aegir build --node false` and `aegir test -t browser --node false` now work 🎉
* fix: fix require path
* fix: feedback
* fix: update deps and bundle size
* chore: bump interfaces
* chore: update size
106 lines
2.7 KiB
JavaScript
106 lines
2.7 KiB
JavaScript
'use strict'
|
|
|
|
const { Buffer } = require('buffer')
|
|
const errCode = require('err-code')
|
|
const { messages, codes } = require('./errors')
|
|
|
|
module.exports = (node, Pubsub, config) => {
|
|
const pubsub = new Pubsub(node.peerInfo, node.registrar, config)
|
|
|
|
return {
|
|
/**
|
|
* Subscribe the given handler to a pubsub topic
|
|
* @param {string} topic
|
|
* @param {function} handler The handler to subscribe
|
|
* @returns {void}
|
|
*/
|
|
subscribe: (topic, handler) => {
|
|
if (!node.isStarted() && !pubsub.started) {
|
|
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
|
}
|
|
|
|
if (pubsub.listenerCount(topic) === 0) {
|
|
pubsub.subscribe(topic)
|
|
}
|
|
|
|
pubsub.on(topic, handler)
|
|
},
|
|
|
|
/**
|
|
* Unsubscribes from a pubsub topic
|
|
* @param {string} topic
|
|
* @param {function} [handler] The handler to unsubscribe from
|
|
*/
|
|
unsubscribe: (topic, handler) => {
|
|
if (!node.isStarted() && !pubsub.started) {
|
|
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
|
}
|
|
|
|
if (!handler) {
|
|
pubsub.removeAllListeners(topic)
|
|
} else {
|
|
pubsub.removeListener(topic, handler)
|
|
}
|
|
|
|
if (pubsub.listenerCount(topic) === 0) {
|
|
pubsub.unsubscribe(topic)
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Publish messages to the given topics.
|
|
* @param {Array<string>|string} topic
|
|
* @param {Buffer} data
|
|
* @returns {Promise<void>}
|
|
*/
|
|
publish: (topic, data) => {
|
|
if (!node.isStarted() && !pubsub.started) {
|
|
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
|
}
|
|
|
|
try {
|
|
data = Buffer.from(data)
|
|
} catch (err) {
|
|
throw errCode(new Error('data must be convertible to a Buffer'), 'ERR_DATA_IS_NOT_VALID')
|
|
}
|
|
|
|
return pubsub.publish(topic, data)
|
|
},
|
|
|
|
/**
|
|
* Get a list of topics the node is subscribed to.
|
|
* @returns {Array<String>} topics
|
|
*/
|
|
getTopics: () => {
|
|
if (!node.isStarted() && !pubsub.started) {
|
|
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
|
}
|
|
|
|
return pubsub.getTopics()
|
|
},
|
|
|
|
/**
|
|
* Get a list of the peer-ids that are subscribed to one topic.
|
|
* @param {string} topic
|
|
* @returns {Array<string>}
|
|
*/
|
|
getSubscribers: (topic) => {
|
|
if (!node.isStarted() && !pubsub.started) {
|
|
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
|
}
|
|
|
|
return pubsub.getSubscribers(topic)
|
|
},
|
|
|
|
setMaxListeners (n) {
|
|
return pubsub.setMaxListeners(n)
|
|
},
|
|
|
|
_pubsub: pubsub,
|
|
|
|
start: () => pubsub.start(),
|
|
|
|
stop: () => pubsub.stop()
|
|
}
|
|
}
|