Skip to content
This repository was archived by the owner on Oct 19, 2022. It is now read-only.

Commit a2f9aea

Browse files
docs(api): first pass
1 parent dd6f956 commit a2f9aea

File tree

10 files changed

+126
-48
lines changed

10 files changed

+126
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ build/Release
2727
node_modules
2828

2929
dist
30+
docs

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ build/Release
2727
node_modules
2828

2929
test
30+
docs

README.md

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ the global namespace.
102102
### Attach multistream to a connection (socket)
103103

104104
```JavaScript
105-
const Multistream = require('multistream-select')
105+
const multistream = require('multistream-select')
106106

107107
const ms = new multistream.Listener()
108108
// or
@@ -112,39 +112,6 @@ const ms = new multistream.Dialer()
112112
ms.handle(conn, callback)
113113
```
114114

115-
### Handling a protocol
116-
117-
This function is only available in Listener mode
118-
119-
```JavaScript
120-
ms.addHandler(<protocol>, <handlerFunc>, [<matchingFunc>])
121-
```
122-
123-
- `protocol` is a string identifying the protocol.
124-
- `handlerFunc` is a function of type `function (protocol, conn)` that will be called if there is a handshake performed on `protocol`.
125-
- `matchingFunc` is a function that receives a protocol and a callback and should call `callback(err, result)` where `err` is if there was a error on the matching function, and `result` is a boolean that represents if a match happened. The default `matchingFunc` is exact matching. The exact signature should be: `function (protocol, requestedProtocol, function (err, result)`
126-
127-
### Selecting a protocol
128-
129-
This function is only available in Dialer mode
130-
131-
```JavaScript
132-
ms.select(<protocol>, <callback>)
133-
```
134-
135-
- `protocol` is a string of the protocol that we want to handshake.
136-
- `callback` is a function of type `function (err, conn)` where `err` is an error object that gets passed if something wrong happend (e.g: if the protocol selected is not supported by the other end) and conn is the connection handshaked with the other end.
137-
138-
### Listing the available protocols
139-
140-
This function is only available in Dialer mode
141-
142-
```JavaScript
143-
ms.ls(<callback>)
144-
```
145-
146-
`callback` is a function of type `function (err, protocols)` where `err` is an error object that gets passed if something wrong happend and `protocols` is an array of the supported protocols in the other end.
147-
148115
### This module uses `pull-streams`
149116

150117
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 why we did this, see the discussion at this [issue](https://github.com/ipfs/js-ipfs/issues/362).

example.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const multistream = require('multistream-select')
4+
const Connection = require('interface-connection').Connection
5+
6+
const listener = new multistream.Listener()
7+
// or
8+
// const dialer = new multistream.Dialer()
9+
10+
// supply a connection
11+
const conn = new Connection()
12+
13+
// apply the multistream to the conn
14+
listener.handle(conn, () => {
15+
console.log('connection established')
16+
})

package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"lint": "aegir-lint",
88
"test": "aegir-test",
99
"build": "aegir-build",
10-
"release": "aegir-release",
11-
"release-minor": "aegir-release --type minor",
12-
"release-major": "aegir-release --type major",
10+
"docs": "aegir-docs",
11+
"release": "aegir-release --docs",
12+
"release-minor": "aegir-release --type minor --docs",
13+
"release-major": "aegir-release --type major --docs",
1314
"test:node": "aegir-test node",
1415
"test:browser": "aegir-test browser",
1516
"coverage": "aegir-coverage",
@@ -40,21 +41,21 @@
4041
"author": "David Dias <[email protected]>",
4142
"license": "MIT",
4243
"dependencies": {
43-
"async": "^2.1.2",
44-
"debug": "^2.2.0",
44+
"async": "^2.1.4",
45+
"debug": "^2.4.1",
4546
"interface-connection": "^0.3.0",
4647
"lodash.isfunction": "^3.0.8",
4748
"lodash.range": "^3.2.0",
4849
"pull-handshake": "^1.1.4",
4950
"pull-length-prefixed": "^1.2.0",
5051
"pull-stream": "^3.5.0",
5152
"semver": "^5.3.0",
52-
"varint": "^4.0.1"
53+
"varint": "^5.0.0"
5354
},
5455
"devDependencies": {
55-
"aegir": "^9.0.1",
56+
"aegir": "^9.3.0",
5657
"chai": "^3.5.0",
57-
"pre-commit": "^1.1.3",
58+
"pre-commit": "^1.2.2",
5859
"pull-pair": "^1.1.0",
5960
"run-parallel": "^1.1.6",
6061
"run-series": "^1.1.4"
@@ -66,4 +67,4 @@
6667
"Richard Littauer <[email protected]>",
6768
"npm-to-cdn-bot (by Forbes Lindesay) <[email protected]>"
6869
]
69-
}
70+
}

src/dialer/index.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,26 @@ const select = require('../select')
99

1010
const PROTOCOL_ID = require('./../constants').PROTOCOL_ID
1111

12-
module.exports = class Dialer {
12+
/**
13+
*
14+
*/
15+
class Dialer {
16+
/**
17+
* Create a new Dialer.
18+
*/
1319
constructor () {
1420
this.conn = null
1521
this.log = util.log.dialer()
1622
}
1723

18-
// perform the multistream handshake
24+
/**
25+
* Perform the multistream handshake.
26+
*
27+
* @param {Connection} rawConn - The connection on which
28+
* to perform the handshake.
29+
* @param {function(Error)} callback - Called when the handshake completed.
30+
* @returns {undefined}
31+
*/
1932
handle (rawConn, callback) {
2033
this.log('dialer handle conn')
2134
const s = select(PROTOCOL_ID, (err, conn) => {
@@ -36,6 +49,18 @@ module.exports = class Dialer {
3649
)
3750
}
3851

52+
/**
53+
* Select a protocol
54+
*
55+
* @param {string} protocol - A string of the protocol that we want to handshake.
56+
* @param {function(Error, Connection)} callback - `err` is
57+
* an error object that gets passed if something wrong happ
58+
* end (e.g: if the protocol selected is not supported by
59+
* the other end) and conn is the connection handshaked
60+
* with the other end.
61+
*
62+
* @returns {undefined}
63+
*/
3964
select (protocol, callback) {
4065
this.log('dialer select ' + protocol)
4166
if (!this.conn) {
@@ -57,6 +82,16 @@ module.exports = class Dialer {
5782
)
5883
}
5984

85+
/**
86+
* List all available protocols.
87+
*
88+
* @param {function(Error, Array<string>)} callback - If
89+
* something wrong happend `Error` exists, otherwise
90+
* `protocols` is a list of the supported
91+
* protocols on the other end.
92+
*
93+
* @returns {undefined}
94+
*/
6095
ls (callback) {
6196
const lsStream = select('ls', (err, conn) => {
6297
if (err) {
@@ -103,3 +138,5 @@ function collectLs (conn) {
103138
return counter-- > 0
104139
})
105140
}
141+
142+
module.exports = Dialer

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
exports.Listener = exports.listener = require('./listener')
44
exports.Dialer = exports.dialer = require('./dialer')
55
exports.matchSemver = require('./listener/match-semver')
6+
exports.matchExact = require('./listener/match-exact')

src/listener/index.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ const Connection = require('interface-connection').Connection
1313

1414
const PROTOCOL_ID = require('./../constants').PROTOCOL_ID
1515

16-
module.exports = class Listener {
16+
/**
17+
* Listener
18+
*/
19+
class Listener {
20+
/**
21+
* Create a new Listener.
22+
*/
1723
constructor () {
1824
this.handlers = {
1925
ls: {
@@ -25,7 +31,14 @@ module.exports = class Listener {
2531
this.log = util.log.listener()
2632
}
2733

28-
// perform the multistream handshake
34+
/**
35+
* Perform the multistream handshake.
36+
*
37+
* @param {Connection} rawConn - The connection on which
38+
* to perform the handshake.
39+
* @param {function(Error)} callback - Called when the handshake completed.
40+
* @returns {undefined}
41+
*/
2942
handle (rawConn, callback) {
3043
this.log('listener handle conn')
3144

@@ -54,7 +67,14 @@ module.exports = class Listener {
5467
)
5568
}
5669

57-
// be ready for a given `protocol`
70+
/**
71+
* Handle a given `protocol`.
72+
*
73+
* @param {string} protocol - A string identifying the protocol.
74+
* @param {function(string, Connection)} handlerFunc - Will be called if there is a handshake performed on `protocol`.
75+
* @param {matchHandler} [matchFunc=matchExact]
76+
* @returns {undefined}
77+
*/
5878
addHandler (protocol, handlerFunc, matchFunc) {
5979
this.log('adding handler: ' + protocol)
6080
assert(isFunction(handlerFunc), 'handler must be a function')
@@ -72,4 +92,20 @@ module.exports = class Listener {
7292
matchFunc: matchFunc
7393
}
7494
}
95+
96+
/**
97+
* Receives a protocol and a callback and should
98+
* call `callback(err, result)` where `err` is if
99+
* there was a error on the matching function, and
100+
* `result` is a boolean that represents if a
101+
* match happened.
102+
*
103+
* @callback matchHandler
104+
* @param {string} myProtocol
105+
* @param {string} senderProtocol
106+
* @param {function(Error, boolean)} callback
107+
* @returns {undefined}
108+
*/
75109
}
110+
111+
module.exports = Listener

src/listener/match-exact.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
'use strict'
22

3+
/**
4+
* Match protocols exactly.
5+
*
6+
* @param {string} myProtocol
7+
* @param {string} senderProtocol
8+
* @param {function(Error, boolean)} callback
9+
* @returns {undefined}
10+
* @type {matchHandler}
11+
*/
312
function matchExact (myProtocol, senderProtocol, callback) {
413
const result = myProtocol === senderProtocol
514
callback(null, result)

src/listener/match-semver.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
const semver = require('semver')
44

5+
/**
6+
* Match protocols using semver `~` matching.
7+
*
8+
* @param {string} myProtocol
9+
* @param {string} senderProtocol
10+
* @param {function(Error, boolean)} callback
11+
* @returns {undefined}
12+
* @type {matchHandler}
13+
*/
514
function matchSemver (myProtocol, senderProtocol, callback) {
615
const mps = myProtocol.split('/')
716
const sps = senderProtocol.split('/')

0 commit comments

Comments
 (0)