# hyperswarm-proxy

> Proxy p2p connections using a duplex stream and Hyperswarm

Latest version **1.5.2** (published 2022-03-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install hyperswarm-proxy
pnpm add hyperswarm-proxy
yarn add hyperswarm-proxy
bun add hyperswarm-proxy
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.2 |
| Published | 2022-03-01 |
| First published | 2019-08-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 28 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 32 |
| Author | rangermauve |
| Maintainers | rangermauve, falafeljan |
| Keywords | dat, hyperswarm, swarm, p2p, proxy, stream |

## Links

- npm: https://www.npmjs.com/package/hyperswarm-proxy
- Repository: https://github.com/RangerMauve/hyperswarm-proxy
- Homepage: https://github.com/RangerMauve/hyperswarm-proxy#readme
- Issues: https://github.com/RangerMauve/hyperswarm-proxy/issues
- npm.io page: https://npm.io/package/hyperswarm-proxy

## Dependencies (3)

- [pump](https://npm.io/package/pump.md) ^3.0.0
- [@hyperswarm/network](https://npm.io/package/@hyperswarm/network.md) ^2.1.0
- [length-prefixed-stream](https://npm.io/package/length-prefixed-stream.md) ^2.0.0

## Alternatives

- [@regle/core](https://npm.io/package/@regle/core.md) — 47.0K weekly downloads
- [typeof-arguments](https://npm.io/package/typeof-arguments.md) — 12.5K weekly downloads
- [@lokalise/projects-engine-contracts](https://npm.io/package/@lokalise/projects-engine-contracts.md) — 978 weekly downloads
- [@osjwnpm/nam-laboriosam-quibusdam](https://npm.io/package/@osjwnpm/nam-laboriosam-quibusdam.md) — 70 weekly downloads
- [@oridune/validator](https://npm.io/package/@oridune/validator.md) — 16 weekly downloads

## Recent versions

- 1.5.2 (latest) — 2022-03-01
- 1.5.1 — 2022-02-23
- 1.5.0 — 2021-04-01
- 1.4.0 — 2020-09-03
- 1.3.2 — 2020-04-11
- 1.3.1 — 2020-04-10
- 1.3.0 — 2020-01-02
- 1.2.1 — 2019-11-10
- 1.2.0 — 2019-11-05
- 1.1.0 — 2019-11-02
- 1.0.0 — 2019-08-24

## README

# hyperswarm-proxy
Proxy p2p connections using a duplex stream and Hyperswarm

## How it works

- A server initializes an instance of [hyperswarm](https://github.com/hyperswarm/hyperswarm)
- A client opens a connection to the server (use any duplex stream)
- Client instance behaves the same as hyperswarm
- The client asks to join "topics"
- The server starts searching for peers and sends peer information to the client
- The client chooses which peers to connect to (all of them by default)
- The server establishes a connection to the peer and proxies it over to the client
- The client can relay whatever data they want to the peer.

## Server

```js
const HyperswarmProxyServer = require('hyperswarm-proxy/server')

const server = new HyperswarmProxyServer({
  // The bootstrap nodes to pass to hyperswarm (optional)
  bootstrap,

  // Whether your server will be sort lived (ephemeral: true)
  ephemeral,

  // Pass in an existing hyperswarm instance instead of creating a new one (optional)
  network,

  // Function that takes incoming connections to decide what to do with them
  // Can be used to notify clients about peers
  // Disconnect incoming by default
  handleIncoming: (socket) => socket.end(),
})

const somestream = getStreamForClient()

// Get the server to handle streams from clients
server.handleStream(somestream)

// Tell all clients about a peer for a particular topic
// This is useful in conjunction with `handleIncoming`
// Use handleIncoming to figure out which topic the connection is from
// Then tell clients to connect to them using `socket.address()`
server.connectClientsTo(SOME_TOPIC, port, hostname)

// Close connections to clients and any connected peers
server.destroy(() => {
  // Done!
})
```

## Client

```js
const HyperswarmProxyClient = require('hyperswarm-proxy/client')

const somestream = getStreamForServer()

const swarm = new HyperswarmProxyClient({
  // Pass in the stream which connects to the server
  // If this isn't passed in, invoke `.reconnect(somestream)` to start connecting
  connection: somestream,

  // Whether you should autoconnect to peers
  autoconnect: true,

  // The max number of peers to connect to before stopping to autoconnect
  maxPeers: 24,
})

// When we disconnect, reconnect to the server
swarm.on('disconnected', () => {
  swarm.reconnect(getStreamForServer())
})

// Same as hyperswarm
swarm.on('connection', (connection, info) => {

  const stream = getSomeStream(info.topic)
  // Pipe the data from the connection into something to process it
  // E.G. `hyperdrive.replicate()`
  connection.pipe(stream).pipe(connection)
})

swarm.join(topic)

swarm.leave(topic)

// Listen for peers manually
swarm.on('peer', (peer) => {
  // If autoconnect is `false` you can connect manually here
  swarm.connect(peer, (err, connection) => {
    // Wow
  })
})

// Destroy the swarm
swarm.destroy()
```

---
_Source: https://npm.io/package/hyperswarm-proxy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
