• Stars
    star
    926
  • Rank 49,328 (Top 1.0 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A distributed networking stack for connecting peers.

hyperswarm

See the full API docs at docs.holepunch.to

A high-level API for finding and connecting to peers who are interested in a "topic."

Installation

npm install hyperswarm

Usage

const Hyperswarm = require('hyperswarm')

const swarm1 = new Hyperswarm()
const swarm2 = new Hyperswarm()

swarm1.on('connection', (conn, info) => {
  // swarm1 will receive server connections
  conn.write('this is a server connection')
  conn.end()
})

swarm2.on('connection', (conn, info) => {
  conn.on('data', data => console.log('client got message:', data.toString()))
})

const topic = Buffer.alloc(32).fill('hello world') // A topic must be 32 bytes
const discovery = swarm1.join(topic, { server: true, client: false })
await discovery.flushed() // Waits for the topic to be fully announced on the DHT

swarm2.join(topic, { server: false, client: true })
await swarm2.flush() // Waits for the swarm to connect to pending peers.

// After this point, both client and server should have connections

Hyperswarm API

const swarm = new Hyperswarm(opts = {})

Construct a new Hyperswarm instance.

opts can include:

  • keyPair: A Noise keypair that will be used to listen/connect on the DHT. Defaults to a new key pair.
  • seed: A unique, 32-byte, random seed that can be used to deterministically generate the key pair.
  • maxPeers: The maximum number of peer connections to allow.
  • firewall: A sync function of the form remotePublicKey => (true|false). If true, the connection will be rejected. Defaults to allowing all connections.
  • dht: A DHT instance. Defaults to a new instance.

swarm.connecting

Number that indicates connections in progress.

swarm.connections

A set of all active client/server connections.

swarm.peers

A Map containing all connected peers, of the form: (Noise public key hex string) -> PeerInfo object

See the PeerInfo API for more details.

swarm.dht

A hyperdht instance. Useful if you want lower-level control over Hyperswarm's networking.

swarm.on('connection', (socket, peerInfo) => {})

Emitted whenever the swarm connects to a new peer.

socket is an end-to-end (Noise) encrypted Duplex stream

peerInfo is a PeerInfo instance

swarm.on('update', () => {})

Emitted when internal values are changed, useful for user interfaces.

For example: emitted when swarm.connecting or swarm.connections changes.

const discovery = swarm.join(topic, opts = {})

Start discovering and connecting to peers sharing a common topic. As new peers are connected to, they will be emitted from the swarm as connection events.

topic must be a 32-byte Buffer opts can include:

  • server: Accept server connections for this topic by announcing yourself to the DHT. Defaults to true.
  • client: Actively search for and connect to discovered servers. Defaults to true.

Returns a PeerDiscovery object.

Clients and Servers

In Hyperswarm, there are two ways for peers to join the swarm: client mode and server mode. If you've previously used Hyperswarm v2, these were called "lookup" and "announce", but we now think "client" and "server" are more descriptive.

When you join a topic as a server, the swarm will start accepting incoming connections from clients (peers that have joined the same topic in client mode). Server mode will announce your keypair to the DHT, so that other peers can discover your server. When server connections are emitted, they are not associated with a specific topic -- the server only knows it received an incoming connection.

When you join a topic as a client, the swarm will do a query to discover available servers, and will eagerly connect to them. As with server mode, these connections will be emitted as connection events, but in client mode they will be associated with the topic (info.topics will be set in the connection event).

await swarm.leave(topic)

Stop discovering peers for the given topic.

topic must be a 32-byte Buffer

If a topic was previously joined in server mode, leave will stop announcing the topic on the DHT. If a topic was previously joined in client mode, leave will stop searching for servers announcing the topic.

leave will not close any existing connections.

swarm.joinPeer(noisePublicKey)

Establish a direct connection to a known peer.

noisePublicKey must be a 32-byte Buffer

As with the standard join method, joinPeer will ensure that peer connections are reestablished in the event of failures.

swarm.leavePeer(noisePublicKey)

Stop attempting direct connections to a known peer.

noisePublicKey must be a 32-byte Buffer

If a direct connection is already established, that connection will not be destroyed by leavePeer.

const discovery = swarm.status(topic)

Get the PeerDiscovery object associated with the topic, if it exists.

await swarm.listen()

Explicitly start listening for incoming connections. This will be called internally after the first join, so it rarely needs to be called manually.

await swarm.flush()

Wait for any pending DHT announces, and for the swarm to connect to any pending peers (peers that have been discovered, but are still in the queue awaiting processing).

Once a flush() has completed, the swarm will have connected to every peer it can discover from the current set of topics it's managing.

flush() is not topic-specific, so it will wait for every pending DHT operation and connection to be processed -- it's quite heavyweight, so it could take a while. In most cases, it's not necessary, as connections are emitted by swarm.on('connection') immediately after they're opened.

PeerDiscovery API

swarm.join returns a PeerDiscovery instance which allows you to both control discovery behavior, and respond to lifecycle changes during discovery.

await discovery.flushed()

Wait until the topic has been fully announced to the DHT. This method is only relevant in server mode. When flushed() has completed, the server will be available to the network.

await discovery.refresh({ client, server })

Update the PeerDiscovery configuration, optionally toggling client and server modes. This will also trigger an immediate re-announce of the topic, when the PeerDiscovery is in server mode.

await discovery.destroy()

Stop discovering peers for the given topic.

If a topic was previously joined in server mode, leave will stop announcing the topic on the DHT. If a topic was previously joined in client mode, leave will stop searching for servers announcing the topic.

PeerInfo API

swarm.on('connection', ...) emits a PeerInfo instance whenever a new connection is established.

There is a one-to-one relationship between connections and PeerInfo objects -- if a single peer announces multiple topics, those topics will be multiplexed over a single connection.

peerInfo.publicKey

The peer's Noise public key.

peerInfo.topics

An Array of topics that this Peer is associated with -- topics will only be updated when the Peer is in client mode.

peerInfo.prioritized

If true, the swarm will rapidly attempt to reconnect to this peer.

peerInfo.ban(banStatus = false)

Ban or unban the peer. Banning will prevent any future reconnection attempts, but it will not close any existing connections.

License

MIT

More Repositories

1

hypercore

Hypercore is a secure, distributed append-only log.
JavaScript
2,403
star
2

hyperdrive

Hyperdrive is a secure, real time distributed file system
JavaScript
1,794
star
3

sodium-native

Low level bindings for libsodium
JavaScript
302
star
4

hyperdht

The DHT powering Hyperswarm
JavaScript
251
star
5

hyperbee

An append-only B-tree running on a Hypercore
JavaScript
224
star
6

autobase

Autobase lets you write concise multiwriter data structures with Hypercore
JavaScript
84
star
7

corestore

A simple corestore that wraps a random-access-storage module
JavaScript
60
star
8

libudx

udx is reliable, multiplexed, and congestion-controlled streams over udp
C
40
star
9

hyperdrive-next

Hyperdrive is a secure, real-time distributed file system
JavaScript
37
star
10

hypercore-crypto

The crypto primitives used in hypercore, extracted into a separate module
JavaScript
36
star
11

hyperswarm-dht-relay

Relaying the Hyperswarm DHT over other transport protocols to bring decentralized networking to everyone
JavaScript
36
star
12

b4a

Bridging the gap between buffers and typed arrays
JavaScript
33
star
13

hyperblobs

A blob store for Hypercore
JavaScript
31
star
14

hypershell

Spawn shells anywhere. Fully peer-to-peer, authenticated, and end to end encrypted
JavaScript
31
star
15

hyperswarm-secret-stream

Secret stream backed by Noise and libsodium's secretstream
JavaScript
27
star
16

fs-native-extensions

Native file system extensions for advanced file operations
C
26
star
17

tiny-fs-native

Native fs for Javascript
JavaScript
24
star
18

keypear

πŸ”‘πŸ Keychain that derives deterministic Ed25519 keypairs and attestations
JavaScript
23
star
19

pear-expo-hello-world

C++
17
star
20

hyperswarm-seeders

A seeders only swarm
JavaScript
17
star
21

simple-seeder

Dead simple seeder with zero bugs
JavaScript
15
star
22

examples

Examples of basic flows for modules in the Holepunch ecosystem
JavaScript
15
star
23

noise-handshake

Simple noise handshake, supporting generic handshake patterns
JavaScript
13
star
24

localdrive

Hyperdrive but it is files
JavaScript
13
star
25

mirror-drive

Mirror two drives
JavaScript
12
star
26

tiny-http-native

Tiny HTTP library made purely on libuv and napi
JavaScript
12
star
27

libquickbit

The fastest bit in the West; a library for working with bit fields, accelerated using SIMD on supported hardware
C
11
star
28

drives

CLI to download, seed, and mirror a Hyperdrive or Localdrive
JavaScript
11
star
29

tiny-timers-native

Native timers for Javascript
JavaScript
9
star
30

udx-native

udx is reliable, multiplexed, and congestion-controlled streams over udp
JavaScript
9
star
31

libpearsync

Simple message passing between a libuv thread and something else
C
9
star
32

hyperswarm-doctor

Debugging tool for the swarm
JavaScript
7
star
33

netpaste

Copy and paste over the DHT
JavaScript
7
star
34

quickbit-universal

Universal wrapper for https://github.com/holepunchto/libquickbit with a JavaScript fallback
JavaScript
6
star
35

libcrc

Cross-platform implementation of CRC32 with hardware acceleration
C
6
star
36

hyperswarm-testnet

Small module to help you spin up a local Hyperswarm testnet
JavaScript
6
star
37

wasm-tools

A collection of useful tools for working with WASM/WAT in JavaScript
JavaScript
5
star
38

planb-summer-school

the workshop stuff
JavaScript
5
star
39

tt-native

https://github.com/holepunchto/libtt JavaScript bindings for Node.js
JavaScript
5
star
40

hypercore-id-encoding

Convert Hypercore keys to/from z-base32 or hex
JavaScript
4
star
41

serve-drive

HTTP drive server for entries delivery. Auto detects types like video, images, etc
JavaScript
4
star
42

autobase-test-helpers

Helpers when writing tests for an Autobased application
JavaScript
4
star
43

pear-radio-backend

Pear radio backend
JavaScript
4
star
44

nanodebug

A tiny, zero overhead debugging utility
JavaScript
4
star
45

crc-universal

Universal wrapper for https://github.com/holepunchto/libcrc with a JavaScript fallback
JavaScript
3
star
46

tiny-buffer-map

A very simple Map for Buffers and Uint8Arrays
JavaScript
3
star
47

quickbit-native

https://github.com/holepunchto/libquickbit JavaScript bindings for Node.js
JavaScript
3
star
48

tiny-paths

path for platforms without path
JavaScript
3
star
49

same-object

Determine if two objects are deeply equal
JavaScript
3
star
50

bare-expo

Example of embedding Bare in an Expo application using https://github.com/holepunchto/react-native-bare-kit
TypeScript
3
star
51

simdle-universal

Universal wrapper for https://github.com/holepunchto/libsimdle with a JavaScript fallback
JavaScript
2
star
52

libtt

Virtual console extensions built on libuv
C
2
star
53

cmake-ios

iOS utilities for CMake
CMake
2
star
54

cmake-binary

cmake binaries for windows, linux and macos
JavaScript
2
star
55

prebuild-containers

Containers for prebuilding native Node.js modules
Dockerfile
2
star
56

libsimdle

Simple and portable SIMD instructions for 128 bit vectors, inspired by the WASM SIMD specification
C
2
star
57

bits-to-bytes

Functions for doing bit manipulation of typed arrays
JavaScript
2
star
58

bare-addon

Template repository for creating Bare native addons
C
2
star
59

compact-encoding-bitfield

Compact codec for bitfields
JavaScript
2
star
60

seedbee

Bee for seeds
JavaScript
2
star
61

crc-native

https://github.com/holepunchto/libcrc JavaScript bindings for Node.js
Python
2
star
62

bare-utils

Node.js-compatible utility functions for Bare
JavaScript
2
star
63

bare-distributable-hello-world

Showing how to make a single distributable of a JS app
C
2
star
64

bare-debug-log

Simple debug log for JavaScript
JavaScript
2
star
65

pw-to-ek

Derive a secure encryption key from a password using the sodium's scrypt implementation.
JavaScript
2
star
66

cmake-drive

Drive utilities for CMake
1
star
67

cmake-bare-bundle

Bare bundling utilities for CMake
CMake
1
star
68

simdle-native

https://github.com/holepunchto/libsimdle JavaScript bindings for Node.js
C
1
star
69

native-pipe

Native named pipes
C
1
star
70

hp-rpc-cli

JavaScript
1
star
71

pear-inspect

JavaScript
1
star
72

drive-resolve

Asynchronous require resolution in Hyperdrive
JavaScript
1
star
73

warmup-encoding

Encode/decode sets of random blocks for warmup
JavaScript
1
star
74

framed-stream

Read/write stream messages prefixed 8, 16, 24 or 32 bit length
JavaScript
1
star
75

http-forward-host

Simple stream proxy that sniffs the HTTP host or x-forwarded-for header and allows you to to forward the stream based on that
JavaScript
1
star
76

hypertrace-prometheus

Add support for Prometheus/Grafana to hypertrace
JavaScript
1
star
77

bare-dns

Domain name resolution for JavaScript
C
1
star
78

fifofile

Userland FIFO file
JavaScript
1
star
79

bare-format

String formatting for JavaScript
JavaScript
1
star
80

secure-prompt

Securely prompt stdio using secure buffers
JavaScript
1
star
81

bare-headers

Development headers for Bare
JavaScript
1
star
82

bare-net

TCP and IPC servers and clients for JavaScript
JavaScript
1
star