npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@canvas-js/libp2p-rendezvous

v0.2.0

Published

[![standard-readme compliant](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg)](https://github.com/RichardLitt/standard-readme) [![license](https://img.shields.io/github/license/canvasxyz/libp2p-rendezvous)](https://opensource.org/lic

Downloads

1,820

Readme

libp2p-rendezvous

standard-readme compliant license NPM version TypeScript types

JavaScript implementation of the libp2p rendezvous protocol.

Table of Contents

Install

npm i @canvas-js/libp2p-rendezvous

Usage

Rendezvous servers maintain a SQLite database of "registrations", consisting of a string namespace and a signed peer record. Rendezvous clients can register and unregister themselves for arbitrary namespaces, and discover other peer registrations by namespace.

Server

Add the rendezvous server service to a public libp2p peer:

import { rendezvousServer } from "@canvas-js/libp2p-rendezvous/server"

const libp2p = await createLibp2p({
  // ...
  services: {
    // ...
    rendezvous: rendezvousServer({ path: "rendezvous-registrations.sqlite" }),
  },
})

The path is optional; an in-memory SQLite database will be used if it is null or not provided.

Client

Then add the rendezvous client service to the peers that need to find each other:

import { bootstrap } from "@libp2p/bootstrap"
import { rendezvousClient } from "@canvas-js/libp2-rendezvous/client"

const libp2p = await createLibp2p({
  // ...
  peerDiscovery: [bootstrap(["/dns4/my-rendezvous-server/..."])]
  services: {
    // ...
    rendezvous: rendezvousClient({
      autoRegister: ["topic-a", "topic-b"],
      autoDiscover: true,
    })
  },
})

Providing namespaces to autoRegister and enabling autoDiscovery will cause the client to automatically register those namespaces with every rendezvous server it connects to (ie every peer supporting the rendezvous protocol), and automatically renew its regsitrations when they expire, re-connecting to the server if necessary.

Alternatively, you can choose to manually connect to a server and make register, unregister, and discover calls yourself:

const serverPeerId = peerIdFromString("...")
await libp2p.services.rendezvous.connect(serverPeerId, async (point) => {
  await point.register("topic-a")
  await point.unregister("topic-b")
  const peers = await point.discover("topic-c")
  // peers discovered manually are not automatically added to the peerStore
})

API

// @canvas-js/libp2p-rendezvous/client

import type { TypedEventTarget, Libp2pEvents, PeerId, PeerStore, Peer, Connection } from "@libp2p/interface"
import type { Registrar, AddressManager, ConnectionManager } from "@libp2p/interface-internal"
import type { Multiaddr } from "@multiformats/multiaddr"

export interface RendezvousPoint {
  discover(namespace: string, options?: { limit?: number }): Promise<Peer[]>
  register(namespace: string, options?: { ttl?: number }): Promise<{ ttl: number }>
  unregister(namespace: string): Promise<void>
}

export type RendezvousClientComponents = {
  events: TypedEventTarget<Libp2pEvents>
  peerId: PeerId
  peerStore: PeerStore
  registrar: Registrar
  addressManager: AddressManager
  connectionManager: ConnectionManager
}

export interface RendezvousClientInit {
  /**
   * namespace or array of namespaces to register automatically
   * with all peers that support the rendezvous server protocol
   */
  autoRegister?: string[] | null
  autoDiscover?: boolean
  connectionFilter?: (connection: Connection) => boolean
}

export declare class RendezvousClient {
  public static protocol = "/canvas/rendezvous/1.0.0"

  public constructor(components: RendezvousClientComponents, init: RendezvousClientInit)

  public connect<T>(
    server: PeerId | Multiaddr | Multiaddr[],
    callback: (point: RendezvousPoint) => T | Promise<T>,
  ): Promise<T>
}

export declare const rendezvousClient: (
  init?: RendezvousClientInit,
) => (components: RendezvousClientComponents) => RendezvousClient
// @canvas-js/libp2p-rendezvous/server

import { TypedEventTarget, Libp2pEvents, PeerId, PeerStore } from "@libp2p/interface"
import { Registrar, AddressManager, ConnectionManager } from "@libp2p/interface-internal"

export type RendezvousServerComponents = {
  events: TypedEventTarget<Libp2pEvents>
  peerId: PeerId
  peerStore: PeerStore
  registrar: Registrar
  addressManager: AddressManager
  connectionManager: ConnectionManager
}

export interface RendezvousServerInit {
  path?: string | null
}

export declare class RendezvousServer implements Startable {
  public static protocol = "/canvas/rendezvous/1.0.0"

  public constructor(components: RendezvousServerComponents, init: RendezvousServerInit)
}

export declare const rendezvousServer: (
  init?: RendezvousServerInit,
) => (components: RendezvousServerComponents) => RendezvousServer

Contributing

Open an issue if you have questions, find bugs, or have interface suggestions. Only minor PRs will be considered without prior discussion.

License

MIT © Canvas Technologies, Inc.