diff --git a/.gitignore b/.gitignore index 123ae94..5cd7358 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ build/Release # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git node_modules + +lib +dist diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..00a160d --- /dev/null +++ b/.npmignore @@ -0,0 +1,29 @@ +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git +node_modules + +test diff --git a/README.md b/README.md index e885054..868ee83 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ interface-connection ================== -[![](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) +[![](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) > A test suite and interface you can use to implement a connection. A connection is understood as something that offers mechanism for writing and reading data, back pressure, half and full duplex streams. This module and test suite were heavily inspired by abstract-blob-store and interface-stream-muxer. @@ -11,12 +12,16 @@ Publishing a test suite as a module lets multiple modules all ensure compatibili 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. -> **IMPORTANT** - Tests are still not finished nor the interface - - # Modules that implement the interface -- [node-libp2p-tcp](https://github.com/diasdavid/node-libp2p-tcp) +- [js-libp2p-tcp](https://github.com/diasdavid/node-libp2p-tcp) +- [js-libp2p-webrtc-star](https://github.com/diasdavid/js-libp2p-webrtc-star) +- [js-libp2p-websockets](https://github.com/diasdavid/js-libp2p-websockets) +- [js-libp2p-utp](https://github.com/diasdavid/js-libp2p-utp) +- [webrtc-explorer](https://github.com/diasdavid/webrtc-explorer) +- [js-libp2p-spdy](https://github.com/diasdavid/js-libp2p-spdy) +- [js-libp2p-multiplex](https://github.com/diasdavid/js-libp2p-multiplex) +- [js-libp2p-secio](https://github.com/ipfs/js-libp2p-secio) # Badge @@ -53,9 +58,51 @@ tests(tape, common) A valid (read: that follows this abstraction) connection, must implement the following API. -notes: +**Table of contents:** + +- type: `Connection` + - `conn.getObservedAddrs(callback)` + - `conn.getPeerInfo(callback)` + - `conn.setPeerInfo(peerInfo)` + - `conn.destroy` + - `conn.write` + - `conn.read` + - `conn.pipe` + - `conn.end` + - `conn.pause` + - `conn.resume` + - `conn.destroy` + - `...` + +### Get the Observed Addresses of the peer in the other end + +- `JavaScript` - `conn.getObservedAddrs(callback)` + +This method retrieves the observed addresses we get from the underlying transport, if any. + +`callback` should follow the follow `function (err, multiaddrs) {}`, where `multiaddrs` is an array of [multiaddr](https://github.com/jbenet/multiaddr). + +### Get the PeerInfo + +- `JavaScript` - `conn.getPeerInfo(callback)` + +This method retrieves the a Peer Info object that contains information about the peer that this conn connects to. + +`callback` should follow the `function (err, peerInfo) {}` signature, where peerInfo is a object of type [Peer Info](https://github.com/diasdavid/js-peer-info) + +### Set the PeerInfo + +- `JavaScript` - `conn.setPeerInfo(peerInfo)` +j +This method stores a reference to the peerInfo Object that contains information about the peer that this conn connects to. + +`peerInfo` is a object of type [Peer Info](https://github.com/diasdavid/js-peer-info) + +--- + +notes: + - should follow the remaining Duplex stream operations - should have backpressure into account - should enable half duplex streams (close from one side, but still open for the other) - should support full duplex - tests should be performed by passing two streams - diff --git a/package.json b/package.json index 8ce1c49..e660a21 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,20 @@ "name": "interface-connection", "version": "0.0.3", "description": "A test suite and interface you can use to implement a connection interface.", + "main": "lib/index.js", + "jsnext:main": "src/index.js", + "scripts": { + "lint": "aegir-lint", + "build": "aegir-build", + "test": "exit 0", + "release": "aegir-release", + "release-minor": "aegir-release --type minor", + "release-major": "aegir-release --type major" + }, + "pre-commit": [ + "lint", + "test" + ], "repository": { "type": "git", "url": "https://github.com/diasdavid/interface-connection.git" @@ -16,6 +30,10 @@ }, "homepage": "https://github.com/diasdavid/interface-connection", "dependencies": { + "duplexify": "^3.4.3", "timed-tape": "^0.1.0" + }, + "devDependencies": { + "aegir": "^3.2.0" } } diff --git a/src/connection.js b/src/connection.js new file mode 100644 index 0000000..72d4766 --- /dev/null +++ b/src/connection.js @@ -0,0 +1,53 @@ +'use strict' + +const util = require('util') +const Duplexify = require('duplexify') + +module.exports = Connection + +util.inherits(Connection, Duplexify) + +function Connection (conn) { + if (!(this instanceof Connection)) { + return new Connection(conn) + } + + Duplexify.call(this) + + let peerInfo + + this.getPeerInfo = (callback) => { + if (conn.getPeerInfo) { + return conn.getPeerInfo(callback) + } + + if (!peerInfo) { + return callback(new Error('Peer Info not set yet')) + } + + callback(null, peerInfo) + } + + this.setPeerInfo = (_peerInfo) => { + if (conn.setPeerInfo) { + return conn.setPeerInfo(_peerInfo) + } + peerInfo = _peerInfo + } + + this.getObservedAddrs = (callback) => { + if (conn.getObservedAddrs) { + return conn.getObservedAddrs(callback) + } + callback(null, []) + } + + this.setInnerConn = (conn) => { + this.setReadable(conn) + this.setWritable(conn) + } + + if (conn) { + this.setInnerConn(conn) + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..c15a2c7 --- /dev/null +++ b/src/index.js @@ -0,0 +1,4 @@ +'use strict' + +exports.connection = require('./connection.js') +exports.Connection = require('./connection.js') diff --git a/test/test.spec.js b/test/test.spec.js new file mode 100644 index 0000000..68b07e9 --- /dev/null +++ b/test/test.spec.js @@ -0,0 +1,3 @@ +'use strict' + +// so that aegir does not burp