98 lines
4.2 KiB
Markdown
Raw Normal View History

2015-12-11 20:48:29 -08:00
interface-stream-muxer
2015-07-10 14:41:19 -07:00
=====================
2016-03-06 22:42:53 +00:00
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
2016-03-06 22:42:53 +00:00
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Travis CI](https://travis-ci.org/ipfs/interface-stream-muxer.svg?branch=master)](https://travis-ci.org/ipfs/interface-stream-muxer)
[![Dependency Status](https://david-dm.org/ipfs/interface-stream-muxer.svg?style=flat-square)](https://david-dm.org/ipfs/interface-stream-muxer) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
2015-07-10 14:41:19 -07:00
2015-07-13 14:42:27 -07:00
> A test suite and interface you can use to implement a stream muxer. "A one stop shop for all your muxing needs"
2015-07-10 14:41:19 -07:00
The primary goal of this module is to enable developers to pick and swap their stream muxing module as they see fit for their application, without having to go through shims or compatibility issues. This module and test suite was heavily inspired by [abstract-blob-store](https://github.com/maxogden/abstract-blob-store).
2015-07-10 14:50:43 -07:00
Publishing a test suite as a module lets multiple modules all ensure compatibility since they use the same test suite.
The API is presented with both Node.js and Go primitives, however, there is not actual limitations for it to be extended for any other language, pushing forward the cross compatibility and interop through diferent stacks.
2018-11-09 16:08:48 +01:00
## Lead Maintainer
[Jacob Heun](https://github.com/jacobheun/)
## Modules that implement the interface
2015-07-10 14:41:19 -07:00
- [JavaScript libp2p-spdy](https://github.com/libp2p/js-libp2p-spdy)
2018-07-13 17:22:57 +02:00
- [JavaScript libp2p-mplex](https://github.com/libp2p/js-libp2p-mplex)
- [Go spdy, muxado, yamux and multiplex](https://github.com/jbenet/go-stream-muxer)
2015-07-10 14:41:19 -07:00
Send a PR to add a new one if you happen to find or write one.
## Badge
2015-07-10 14:41:19 -07:00
2015-12-11 20:48:29 -08:00
Include this badge in your readme if you make a new module that uses interface-stream-muxer API.
2015-07-10 15:33:56 -07:00
2015-07-10 14:41:19 -07:00
![](/img/badge.png)
## Usage
### Node.js
Install `interface-stream-muxer` as one of the dependencies of your project and as a test file. Then, using `mocha` (for JavaScript) or a test runner with compatible API, do:
```js
const test = require('interface-stream-muxer')
const common = {
setup (cb) {
cb(null, yourMuxer)
},
teardown (cb) {
cb()
}
}
// use all of the test suits
test(common)
```
### Go
2015-07-10 14:41:19 -07:00
> WIP
2015-07-10 14:41:19 -07:00
## API
2015-07-10 14:41:19 -07:00
A valid (read: that follows this abstraction) stream muxer, must implement the following API.
### Attach muxer to a Connection
2015-07-10 14:50:43 -07:00
- `JavaScript` muxedConn = muxer(conn, isListener)
- `Go` muxedConn, err := muxer.Attach(conn, isListener)
2015-07-10 15:33:56 -07:00
This method attaches our stream muxer to an instance of [Connection](https://github.com/libp2p/interface-connection/blob/master/src/connection.js) defined by [interface-connection](https://github.com/libp2p/interface-connection).
2015-07-10 14:50:43 -07:00
2015-07-10 15:33:56 -07:00
If `err` is passed, no operation should be made in `conn`.
2015-07-10 18:47:49 -07:00
`isListener` is a bool that tells the side of the socket we are, `isListener = true` for listener/server and `isListener = false` for dialer/client side.
2016-03-06 22:42:53 +00:00
`muxedConn` interfaces our established Connection with the other endpoint, it must offer an interface to open a stream inside this connection and to receive incomming stream requests.
2015-07-10 14:50:43 -07:00
### Dial(open/create) a new stream
2015-07-10 14:41:19 -07:00
- `JavaScript` stream = muxedConn.newStream([function (err, stream)])
2016-03-06 22:42:53 +00:00
- `Go` stream, err := muxedConn.newStream()
2015-07-10 15:33:56 -07:00
This method negotiates and opens a new stream with the other endpoint.
2015-07-10 14:41:19 -07:00
2015-07-10 15:33:56 -07:00
If `err` is passed, no operation should be made in `stream`.
2015-07-10 14:41:19 -07:00
`stream` interface our established Stream with the other endpoint, it must implement the [Duplex pull-stream interface](https://pull-stream.github.io) in JavaScript or the [ReadWriteCloser](http://golang.org/pkg/io/#ReadWriteCloser) in Go.
2015-07-10 15:33:56 -07:00
### Listen(wait/accept) a new incoming stream
2015-07-10 14:41:19 -07:00
- `JavaScript` muxedConn.on('stream', function (stream) {})
2016-03-06 22:42:53 +00:00
- `Go` stream := muxedConn.Accept()
2015-07-10 15:33:56 -07:00
Each time a dialing peer initiates the new stream handshake, a new stream is created on the listening side.
In JavaScript, the Event Emitter pattern is expected to be used in order to receive new incoming streams, while in Go, it expects to wait when Accept is called.