Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unreplicate() method #356

Open
gmaclennan opened this issue Feb 10, 2023 · 2 comments
Open

Add unreplicate() method #356

gmaclennan opened this issue Feb 10, 2023 · 2 comments

Comments

@gmaclennan
Copy link
Contributor

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()?

@gmaclennan
Copy link
Contributor Author

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.

@gmaclennan
Copy link
Contributor Author

Update: the function above was not working for two reasons:

  1. Needed to unpair the protomux
  2. If one peer unreplicated before the other, the other peer would remain "replicated"

Fixed unreplicate function:

/**
 * @param {import('hypercore')<'binary', any>} core Core to unreplicate. Must be ready.
 * @param {import('protomux')} protomux
 */
export function unreplicate(core, protomux) {
  assert(core.discoveryKey, 'Core should have a discovery key')
  protomux.unpair({
    protocol: 'hypercore/alpha',
    id: core.discoveryKey,
  })
  for (const channel of protomux) {
    if (channel.protocol !== 'hypercore/alpha') continue
    if (!channel.id.equals(core.discoveryKey)) continue
    channel.close()
  }
}

For our use-case, we would not need an unreplicate method if #556 was implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant