UNPKG

orbit-db

Version:

Distributed p2p database on IPFS

46 lines (39 loc) 1.5 kB
import Channel from 'ipfs-pubsub-1on1' import Logger from 'logplease' const logger = Logger.create('exchange-heads', { color: Logger.Colors.Yellow }) Logger.setLogLevel('ERROR') const getHeadsForDatabase = async store => { if (!(store && store._cache)) return [] const localHeads = await store._cache.get(store.localHeadsPath) || [] const remoteHeads = await store._cache.get(store.remoteHeadsPath) || [] return [...localHeads, ...remoteHeads] } export default async (ipfs, address, peer, getStore, getDirectConnection, onMessage, onChannelCreated) => { const _handleMessage = message => { const msg = JSON.parse(Buffer.from(message.data).toString()) const { address, heads } = msg onMessage(address, heads) } let channel = getDirectConnection(peer) if (!channel) { try { logger.debug(`Create a channel to ${peer}`) channel = await Channel.open(ipfs, peer) channel.on('message', _handleMessage) logger.debug(`Channel created to ${peer}`) onChannelCreated(channel) } catch (e) { logger.error(e) } } // Wait for the direct channel to be fully connected await channel.connect() logger.debug(`Connected to ${peer}`) // Send the heads if we have any const heads = await getHeadsForDatabase(getStore(address)) logger.debug(`Send latest heads of '${address}':\n`, JSON.stringify(heads.map(e => e.hash), null, 2)) if (heads) { await channel.send(JSON.stringify({ address, heads })) } return channel }