From 4499bba514a0384be020c4a533ccb7e848e684b9 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 14 Mar 2016 16:57:54 +0000 Subject: [PATCH] add filter feature --- README.md | 9 +++++++-- package.json | 1 + src/index.js | 10 ++++++++++ tests/libp2p-tcp-test.js | 11 +++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 47b7386..1b4d43b 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,16 @@ js-libp2p-tcp =============== -[![](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/diasdavid/js-libp2p-tcp.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-libp2p-tcp) ![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square) [![Dependency Status](https://david-dm.org/diasdavid/js-libp2p-tcp.svg?style=flat-square)](https://david-dm.org/diasdavid/js-libp2p-tcp) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) +[![](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/diasdavid/js-libp2p-tcp.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-libp2p-tcp) +![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square) +[![Dependency Status](https://david-dm.org/diasdavid/js-libp2p-tcp.svg?style=flat-square)](https://david-dm.org/diasdavid/js-libp2p-tcp) +[![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/diasdavid/abstract-connection/master/img/badge.png) ![](https://raw.githubusercontent.com/diasdavid/abstract-transport/master/img/badge.png) -> Node.js implementation of the TCP module that libp2p uses, which implements the [abstract-connection]() interface for dial/listen. +> Node.js implementation of the TCP module that libp2p uses, which implements the [interface-connection]() interface for dial/listen. note: libp2p-tcp in Node.js is a very thin shim that adds the support to dial to a `multiaddr`. This small shim will enable libp2p to use other different transports. diff --git a/package.json b/package.json index 9e622f0..d167f42 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ }, "dependencies": { "ip-address": "^5.8.0", + "mafmt": "^1.0.1", "multiaddr": "^1.1.1" } } diff --git a/src/index.js b/src/index.js index 565c809..3b80273 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const tcp = require('net') const multiaddr = require('multiaddr') const Address6 = require('ip-address').Address6 +const mafmt = require('mafmt') exports = module.exports = TCP @@ -79,6 +80,15 @@ function TCP () { }) }) } + + this.filter = (multiaddrs) => { + if (!Array.isArray(multiaddrs)) { + multiaddrs = [multiaddrs] + } + return multiaddrs.filter((ma) => { + return mafmt.TCP.matches(ma) + }) + } } function getMultiaddr (conn) { diff --git a/tests/libp2p-tcp-test.js b/tests/libp2p-tcp-test.js index 6b6de08..cc30764 100644 --- a/tests/libp2p-tcp-test.js +++ b/tests/libp2p-tcp-test.js @@ -77,5 +77,16 @@ describe('libp2p-tcp', function () { }) }) + it('filter valid addrs for this transport', (done) => { + const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090') + const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090') + const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/http') + + const valid = tcp.filter([mh1, mh2, mh3]) + expect(valid.length).to.equal(1) + expect(valid[0]).to.deep.equal(mh1) + done() + }) + it.skip('listen on IPv6', (done) => {}) })