node-yamux
TypeScript icon, indicating that this package has built-in type declarations

0.0.1-rc3 • Public • Published

node-yamux

stream implementation of https://github.com/ChainSafe/js-libp2p-yamux

JavaScript implementation of yamux.

Install

yarn add node-yamux

Usage

import { Stream, YamuxMuxer } from 'node-yamux'

function createPair (onStream: (stream: Stream) => void): [YamuxMuxer, YamuxMuxer] {
  const server = new YamuxMuxer({
    direction: 'inbound',
    onIncomingStream: onStream
  })

  const client = new YamuxMuxer({
    direction: 'outbound'
  })

  server.pipe(client).pipe(server)

  return [server, client]
}

const [server, client] = createPair((stream) => {
  let received = ''
  console.log('onStream')
  stream.on('data', (data) => {
    received += Buffer.from(data).toString('utf8')
  })
  stream.on('end', () => {
    console.log('SERVER: received: ' + received)
    stream.close()
  })
  stream.on('close', () => {
    console.log('SERVER: CLOSED')
    server.close()
  })
})
void server

const s1 = client.newStream('hello')
console.log('CLIENT: new stream', s1.id)

s1.on('close', () => {
  console.log('CLIENT: CLOSED')
})
s1.write('aaaaaaaaaa', (err) => {
  console.log('CLIENT: WRITE OK')
  s1.end(() => {
    console.log('CLIENT: END OK')
    s1.close()
  })
})

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Package Sidebar

Install

npm i node-yamux

Weekly Downloads

1

Version

0.0.1-rc3

License

Apache-2.0 OR MIT

Unpacked Size

199 kB

Total Files

65

Last publish

Collaborators

  • jc-lab