Awesome
⛔️ DEPRECATED libp2p-spdy is now deprecated in libp2p and will no longer be maintained. Please visit https://github.com/libp2p/js-libp2p#packages for a list of supported stream multiplexers available for js-libp2p.
js-libp2p-spdy
SPDY 3.1 implementation wrapper that is compatible with libp2p Stream Muxer expected interface
Lead Maintainer
Installation
npm
> npm i libp2p-spdy
Use in Node.js
const spdy = require('libp2p-spdy')
Use in a browser with browserify, webpack or any other bundler
The code published to npm that gets loaded on require is in fact a ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust asset management process.
var spdy = require('libp2p-spdy')
Use in a browser Using a script tag
Loading this module through a script tag will make the Lip2pSpdy
obj available in the global namespace.
<script src="https://unpkg.com/libp2p-spdy/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/libp2p-spdy/dist/index.js"></script>
Usage
API
Attaching it to a socket (duplex stream)
As a listener
const listener = spdy(conn, true)
As a dialer
const dialer = spdy(conn, false)
Opening a multiplex duplex stream
const conn = dialer.newStream((err, conn) => {})
conn.on('error', (err) => {})
note: Works the same on the listener side
Receiving incoming stream
dialer.on('stream', (conn) => {})
note: Works the same on the listener side
Close
dialer.close()
note: Works the same on the listener side
Other events
dialer.on('close', () => {})
dialer.on('error', () => {})
note: Works the same on the listener side
This module uses pull-streams
We expose a streaming interface based on pull-streams
, rather then on the Node.js core streams implementation (aka Node.js streams). pull-streams
offers us a better mechanism for error handling and flow control guarantees. If you would like to know more about why we did this, see the discussion at this issue.
You can learn more about pull-streams at:
- The history of Node.js streams, nodebp April 2014
- The history of streams, 2016
- pull-streams, the simple streaming primitive
- pull-streams documentation
Converting pull-streams
to Node.js Streams
If you are a Node.js streams user, you can convert a pull-stream to a Node.js stream using the module pull-stream-to-stream
, giving you an instance of a Node.js stream that is linked to the pull-stream. For example:
const pullToStream = require('pull-stream-to-stream')
const nodeStreamInstance = pullToStream(pullStreamInstance)
// nodeStreamInstance is an instance of a Node.js Stream
To learn more about this utility, visit https://pull-stream.github.io/#pull-stream-to-stream.