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

dht-universal

v0.4.1

Published

Universal wrapper for @hyperswarm/dht and @hyperswarm/dht-relay working in Node.js and the Browser

Downloads

40

Readme

dht-universal

Universal wrapper for @hyperswarm/dht and @hyperswarm/dht-relay working in Node.js and the Browser

Installation

npm install dht-universal

Usage

In browser, it will use the @hyperswarm/dht-relay with a WebSocket transport, so you need to pass a relay URL to the constructor, and wait for it to be ready to confirm the websocket is open.

Drop-in replacement initialization

import { DHT } from 'dht-universal';
const node = new DHT({ relay: 'wss://dht-relay.example.com/' });
await node.ready();

Async initialization

You can also use the static async function create to try multiple relays until one works.

import { DHT } from 'dht-universal';

const node = await DHT.create({
  relays: ['wss://dht-relay.example.com/'],
  ...opts,
});
// No need to call `await node.ready()`

Relay in Node environment

If you want to use the relay in Node, you can import it explicitly:

import { DHT } from 'dht-universal/relay.js';

const node = await DHT.create({
  relays: ['wss://dht-relay.example.com/'],
  ...opts,
});

API

Should be the same as @hyperswarm/dht.

API test coverage:

The goal of this module is to ensure the usability of the DHT in the browser within the common use cases namely Hyperswarm and replicating Hypercores through Corestore:

import Corestore from 'corestore';
import Hyperswarm from 'hyperswarm';
import ram from 'random-access-memory';
import DHT from 'dht-universal';

(async () => {
  const dht = new DHT({ relay: 'wss://dht-relay.example.com/' });
  const swarm = new Hyperswarm({ dht });
  const store = new Corestore(ram);
  await store.ready();

  swarm.on('connection', (socket) => {
    store.replicate(socket);
  });

  const core = store.get({ key: <0x012...def>});
  await core.ready();

  swarm.join(core.discoveryKey, { server: true, client: true });
  await swarm.flush();

  await core.update();
  const block = await core.get(core.length - 1);
})();

Other unit tests are available but might be removed later.

Setup Relay

This module exposes a helper function to easily run a relay server.

import { setupRelay } from 'dht-universal/setup-relay.js';

const { port, closeRelay } = await setupRelay({ dhtOpts, wsServerOptions });