holepunchto / hypercore

Hypercore is a secure, distributed append-only log.

Home Page:https://docs.holepunch.to

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add unreplicate() method

gmaclennan opened this issue · comments

This existed a while ago in #49

It would be useful to be able to "unreplicate" a core from a hypercore protocol stream, without destroying the underlying stream. I guess closing the protomux channel would do this, and it would remove the peer. However I'm not sure if there's a way to access the protomux channel from the stream returned from core.replicate()?

This is how we are currently doing "unreplicate":

export function unreplicate(core, protomux) {
  const peerToUnreplicate = core.peers.find(
    (peer) => peer.protomux === protomux
  )
  if (!peerToUnreplicate) return
  peerToUnreplicate.channel.close()
  return
}

The protomux instance can be access from the raw stream returned by const protocolStream = core.replicate(), via const protomux = protocolStream.noiseStream.userData.

This seems to work ok.