🚀 Launch Week Day 4:Introducing the Alert Details Page: A Better Way to Explore Alerts.Learn More →
Socket
Book a DemoInstallSign in
Socket

protoplex

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

protoplex

Multiplex multiple streams over a stream. Bidirectional clients / servers.

latest
npmnpm
Version
2.2.0
Version published
Maintainers
1
Created
Source

Protoplex

Multiplexed streams over a protomux instance. Enables bi-directional clients and servers.

npm install protoplex

Usage

import SecretStream from '@hyperswarm/secret-stream'
import Protomux from 'protomux'
import Protoplex from 'protoplex'
import { pipeline } from 'streamx'

const server = new Protoplex(new Protomux(new SecretStream(false)))
const client = new Protoplex(new Protomux(new SecretStream(true)))

pipeline(
  client.mux.stream.rawStream,
  server.mux.stream.rawStream,
  client.mux.stream.rawStream
)

const message = Buffer.from('Hello, World!')

server.on('connection', async (stream) => {
  let str = ''
  for await (const buf of stream) str += buf.toString()
  console.log(str) // prints 'Hello, World!'
})

server.listen(Buffer.from('1'))

let stream = client.connect(Buffer.from('1'))
stream.write(message)
stream.end()

// protoplex makes no distinction between clients and servers

client.on('connection', async (stream) => {
  let str = ''
  for await (const buf of stream) str += buf.toString()
  console.log(str) // prints 'Hello, World!'
})

client.listen(Buffer.from('2'))
stream = server.connect(Buffer.from('2'))
stream.write(message)
stream.end()

API

const plex = new Protoplex(mux, [options])

Options include:

{
  id: b4a.from([]), // default id
  handshake: b4a.from([]), // default handshake value
  handshakeEncoding: c.raw, // default handshake encoding
  onhandshake: (handshake) => true, // default function to accept or reject connection
  encoding: c.raw, // default encoding for values in a stream
  unique: false, // whether the underlying protomux channels should allow multi opens for a given  protcol, id pair
  ...streamOptions // the rest of the options are default options for the underlying Duplex streams
}

const plex = Protoplex.from(muxOrStream, [options])

Options passed through to new Protoplex(mux, [options]).

const stream = plex.connect(id, [options])

Options are the same as for Protoplex.from but override those defaults for.

stream.on('connect')

Emitted when the stream is opened and the handshake was accepted.

plex.on('connection', (stream) => {})

Emitted when a remote connection is opened.

for (const connection of plex)

Iterate over all open connections.

for await (const stream of plex)

Iterate over all connections as they arrive.

FAQs

Package last updated on 20 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts