From c85ef53db5635cc2ea01e7dfe79281c1b6305520 Mon Sep 17 00:00:00 2001 From: David Dias Date: Fri, 19 Feb 2016 18:25:21 +0000 Subject: [PATCH] swarm, tests and readme badgers --- README.md | 13 +++++++-- package.json | 37 ++++++++++++++++++++++++ src/index.js | 32 +++++++++++++++++++++ tests/test-libp2p-ipfs.js | 60 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 package.json create mode 100644 src/index.js create mode 100644 tests/test-libp2p-ipfs.js diff --git a/README.md b/README.md index 2f1a218..9c7fdef 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ -# js-libp2p-ipfs -libp2p build (module) used in js-ipfs +js-libp2p-ipfs +============== + +[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) +[[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) +![Build Status](https://travis-ci.org/ipfs/js-libp2p-ipfs.svg?style=flat-square)](https://travis-ci.org/ipfs/js-libp2p-ipfs) +![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square) +[![Dependency Status](https://david-dm.org/ipfs/js-libp2p-ipfs.svg?style=flat-square)](https://david-dm.org/ipfs/js-libp2p-ipfs) +[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) + +> libp2p build (module) used in js-ipfs diff --git a/package.json b/package.json new file mode 100644 index 0000000..5c34e18 --- /dev/null +++ b/package.json @@ -0,0 +1,37 @@ +{ + "name": "libp2p-ipfs", + "version": "0.0.0", + "description": "The libp2p build (module) used by js-ipfs on Node.js", + "main": "src/index.js", + "scripts": { + "test": "mocha tests/test-*.js", + "lint": "standard" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ipfs/js-libp2p-ipfs.git" + }, + "keywords": [ + "IPFS" + ], + "author": "David Dias ", + "license": "MIT", + "bugs": { + "url": "https://github.com/ipfs/js-libp2p-ipfs/issues" + }, + "homepage": "https://github.com/ipfs/js-libp2p-ipfs#readme", + "devDependencies": { + "chai": "^3.5.0", + "mocha": "^2.4.5", + "pre-commit": "^1.1.2", + "standard": "^6.0.7" + }, + "dependencies": { + "libp2p-spdy": "^0.1.0", + "libp2p-swarm": "^0.5.5", + "libp2p-tcp": "^0.1.2", + "multiaddr": "^1.1.1", + "peer-id": "^0.5.0", + "peer-info": "^0.4.0" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..38f4f96 --- /dev/null +++ b/src/index.js @@ -0,0 +1,32 @@ +const Swarm = require('libp2p-swarm') +const tcp = require('libp2p-tcp') +// const utp = require('libp2p-utp') +var Spdy = require('libp2p-spdy') + +exports = module.exports = libp2p + +function libp2p (options) { + // options.peer - my Peer Info + // options.multiaddrs = { tcp: , utp: } + // + const swarm = new Swarm(options.peer) + swarm.addStreamMuxer('spdy', Spdy, {}) + + return { + listen: (callback) => { + // add the transports + // spdy and stuff + swarm.addTransport('tcp', + tcp, + { multiaddr: options.multiaddrs.tcp }, + {}, + { port: options.multiaddrs.tcp.toString().split('/')[4] }, + () => { + callback() + }) + }, + records: '', + routing: '', + swarm: swarm + } +} diff --git a/tests/test-libp2p-ipfs.js b/tests/test-libp2p-ipfs.js new file mode 100644 index 0000000..34b34fa --- /dev/null +++ b/tests/test-libp2p-ipfs.js @@ -0,0 +1,60 @@ +/* globals describe, it */ + +const expect = require('chai').expect +const libp2p = require('../src') +const Id = require('peer-id') +const Peer = require('peer-info') +const multiaddr = require('multiaddr') + +describe('libp2p-ipfs', () => { + const idA = Id.create() + const idB = Id.create() + const peerA = new Peer(idA, []) + const peerB = new Peer(idB, []) + + var nodeA + var nodeB + + it('prepare node A', (done) => { + const options = { + peer: peerA, + multiaddrs: { + tcp: multiaddr('/ip4/127.0.0.1/tcp/8010') + } + } + + nodeA = libp2p(options) + nodeA.listen((err) => { + expect(err).to.not.exist + done() + }) + }) + it('prepare node B', (done) => { + const options = { + peer: peerB, + multiaddrs: { + tcp: multiaddr('/ip4/127.0.0.1/tcp/8020') + } + } + + nodeB = libp2p(options) + nodeB.listen((err) => { + expect(err).to.not.exist + done() + }) + }) + + it('B handle /echo/1.0.0', (done) => { + nodeB.swarm.handleProtocol('/echo/1.0.0', (conn) => { + conn.pipe(conn) + }) + done() + }) + + it('A dial B to speak /echo/1.0.0', (done) => { + nodeA.swarm.dial(peerB, {}, '/echo/1.0.0', (err, conn) => { + expect(err).to.not.exist + done() + }) + }) +})