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

hyper-webrtc

v1.0.0

Published

WebRTC tools for the hypercore-protocol stack

Downloads

6

Readme

hyper-webrtc

WebRTC tools for the hypercore-protocol stack

npm i hyper-webrtc

Warning: This is experimental.

Usage

Minimal example, Hypercore replication via WebRTC:

swarm.on('connection', function (relay) {
  // Convert the relayed socket into a RTCDataChannel stream
  const stream = HyperWebRTC.from(relay)
  core.replicate(stream)
})

// Split RTC traffic from UDX traffic
const discoveryWeb = crypto.discoveryKey(core.discoveryKey)
swarm.join(discoveryWeb)

Another minimal example, DHT server and client:

server.on('connection', function (relay) {
  // Convert the relayed socket into a RTCDataChannel stream
  const rtc = HyperWebRTC.from(relay)
})

const relay = dht.connect(server.publicKey)
const rtc = HyperWebRTC.from(relay) // Convert it here also

DHT example

import HyperWebRTC from 'hyper-webrtc'

import DHT from '@hyperswarm/dht-relay'
import Stream from '@hyperswarm/dht-relay/ws'

const node = createDHTRelay()
const server = node.createServer()

server.on('connection', function (relay) {
  const rtc = HyperWebRTC.from(relay)

  rtc.on('data', function (data) {
    console.log('Server-side received', data.toString())
  })
})

await server.listen()

const anotherNode = createDHTRelay()
const relay = anotherNode.connect(server.publicKey)
const rtc = HyperWebRTC.from(relay)

rtc.on('open', function () {
  console.log('Client-side opened')
})

rtc.write('Hello World!')

function createDHTRelay () {
  const socket = new WebSocket('wss://dht1-relay.leet.ar:49443')
  const dht = new DHT(new Stream(true, socket))
  return dht
}

Hypercore example

import HyperWebRTC from 'hyper-webrtc'

import DHT from '@hyperswarm/dht-relay'
import Stream from '@hyperswarm/dht-relay/ws'
import Hyperswarm from 'hyperswarm'

import Hypercore from 'hypercore'
import RAM from 'random-access-memory'
import crypto from 'hypercore-crypto'

await writer().then(reader)

async function writer () {
  const swarm = new Hyperswarm({ dht: createDHTRelay() })

  const core = new Hypercore(RAM)
  await core.append(['a', 'b', 'c'])

  swarm.on('connection', function (relay) {
    const stream = HyperWebRTC.from(relay)
    core.replicate(stream)
  })

  const discoveryWeb = crypto.discoveryKey(core.discoveryKey)
  const discovery = swarm.join(discoveryWeb)
  await discovery.flushed() // Just for testing, otherwise don't wait for this

  return core.id
}

async function reader (key) {
  const swarm = new Hyperswarm({ dht: createDHTRelay() })

  const core = new Hypercore(RAM, key)
  await core.ready()

  const done = core.findingPeers()

  swarm.on('connection', function (relay) {
    const stream = HyperWebRTC.from(relay)
    core.replicate(stream)
  })

  const discoveryWeb = crypto.discoveryKey(core.discoveryKey)
  swarm.join(discoveryWeb)
  swarm.flush().then(done, done)

  console.log(await core.get(0))
  console.log(await core.get(1))
  console.log(await core.get(2))
}

function createDHTRelay () {
  const socket = new WebSocket('wss://dht1-relay.leet.ar:49443')
  const dht = new DHT(new Stream(true, socket))
  return dht
}

License

MIT