This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from libp2p/pull
[WIP] Move to pull-streams
- Loading branch information
Showing
8 changed files
with
248 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,61 @@ | ||
js-libp2p-websockets | ||
==================== | ||
# js-libp2p-websockets | ||
|
||
[![](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/) | ||
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) | ||
![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square) | ||
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-websockets.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-websockets) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) | ||
[![Coverage Status](https://coveralls.io/repos/github/libp2p/js-libp2p-websockets/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-libp2p-websockets?branch=master) | ||
[![Travis CI](https://travis-ci.org/libp2p/js-libp2p-websockets.svg?branch=master)](https://travis-ci.org/libp2p/js-libp2p-websockets) | ||
[![Circle CI](https://circleci.com/gh/libp2p/js-libp2p-websockets.svg?style=svg)](https://circleci.com/gh/libp2p/js-libp2p-websockets) | ||
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-websockets.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-websockets) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) | ||
|
||
![](https://raw.githubusercontent.com/libp2p/interface-connection/master/img/badge.png) | ||
![](https://raw.githubusercontent.com/libp2p/interface-transport/master/img/badge.png) | ||
|
||
> JavaScript implementation of the WebSockets module that libp2p uses and that implements the interface-transport interface | ||
## Description | ||
|
||
`libp2p-websockets` is the WebSockets implementation compatible with libp2p. | ||
|
||
**Note:** This module uses [pull-streams](https://pull-stream.github.io) for all stream based interfaces. | ||
|
||
## Example | ||
|
||
``` | ||
TODO | ||
``` | ||
|
||
## Installation | ||
|
||
### npm | ||
|
||
```sh | ||
> npm i libp2p-websockets | ||
``` | ||
|
||
## 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 what took us to make this migration, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362). | ||
|
||
You can learn more about pull-streams at: | ||
|
||
- [The history of Node.js streams, nodebp April 2014](https://www.youtube.com/watch?v=g5ewQEuXjsQ) | ||
- [The history of streams, 2016](http://dominictarr.com/post/145135293917/history-of-streams) | ||
- [pull-streams, the simple streaming primitive](http://dominictarr.com/post/149248845122/pull-streams-pull-streams-are-a-very-simple) | ||
- [pull-streams documentation](https://pull-stream.github.io/) | ||
|
||
### Converting `pull-streams` to Node.js Streams | ||
|
||
If you are a Node.js streams user, you can convert a pull-stream to 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. 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 his utility, visit https://pull-stream.github.io/#pull-stream-to-stream | ||
|
||
## API | ||
|
||
[![](https://raw.githubusercontent.com/diasdavid/interface-transport/master/img/badge.png)](https://github.com/diasdavid/interface-transport) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,38 +22,38 @@ | |
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/diasdavid/js-libp2p-websockets.git" | ||
"url": "git+https://github.com/libp2p/js-libp2p-websockets.git" | ||
}, | ||
"keywords": [ | ||
"IPFS" | ||
], | ||
"author": "David Dias <[email protected]>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/diasdavid/js-libp2p-websockets/issues" | ||
"url": "https://github.com/libp2p/js-libp2p-websockets/issues" | ||
}, | ||
"homepage": "https://github.com/diasdavid/js-libp2p-websockets#readme", | ||
"homepage": "https://github.com/libp2p/js-libp2p-websockets#readme", | ||
"dependencies": { | ||
"detect-node": "^2.0.3", | ||
"interface-connection": "^0.1.8", | ||
"interface-connection": "^0.2.1", | ||
"lodash.contains": "^2.4.3", | ||
"mafmt": "^2.1.0", | ||
"run-parallel": "^1.1.6", | ||
"simple-websocket": "^4.1.0", | ||
"simple-websocket-server": "^0.1.4" | ||
"mafmt": "^2.1.1", | ||
"pull-ws": "^3.2.3" | ||
}, | ||
"devDependencies": { | ||
"aegir": "^6.0.0", | ||
"multiaddr": "^2.0.2", | ||
"aegir": "^6.0.1", | ||
"chai": "^3.5.0", | ||
"gulp": "^3.9.1", | ||
"interface-transport": "^0.2.0", | ||
"pre-commit": "^1.1.2" | ||
"interface-transport": "^0.3.3", | ||
"multiaddr": "^2.0.2", | ||
"pre-commit": "^1.1.3", | ||
"pull-goodbye": "0.0.1", | ||
"pull-stream": "^3.4.3" | ||
}, | ||
"contributors": [ | ||
"David Dias <[email protected]>", | ||
"Francisco Baio Dias <[email protected]>", | ||
"Friedel Ziegelmayer <[email protected]>", | ||
"greenkeeperio-bot <[email protected]>" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const isNode = require('detect-node') | ||
const Connection = require('interface-connection').Connection | ||
const contains = require('lodash.contains') | ||
|
||
// const IPFS_CODE = 421 | ||
|
||
let createServer | ||
|
||
if (isNode) { | ||
createServer = require('pull-ws/server') | ||
} else { | ||
createServer = () => {} | ||
} | ||
|
||
module.exports = (options, handler) => { | ||
const listener = createServer((socket) => { | ||
socket.getObservedAddrs = (cb) => { | ||
// TODO research if we can reuse the address in anyway | ||
return cb(null, []) | ||
} | ||
|
||
handler(new Connection(socket)) | ||
}) | ||
|
||
let listeningMultiaddr | ||
|
||
listener._listen = listener.listen | ||
listener.listen = (ma, cb) => { | ||
cb = cb || (() => {}) | ||
listeningMultiaddr = ma | ||
|
||
if (contains(ma.protoNames(), 'ipfs')) { | ||
ma = ma.decapsulate('ipfs') | ||
} | ||
|
||
listener._listen(ma.toOptions(), cb) | ||
} | ||
|
||
listener.getAddrs = (cb) => { | ||
cb(null, [listeningMultiaddr]) | ||
} | ||
|
||
return listener | ||
} |
Oops, something went wrong.