Permalink
Browse files

support opaque user data (#13)

  • Loading branch information...
scriptjs authored and mafintosh committed Apr 9, 2017
1 parent 04ab65d commit 61eec47adbcfd730414c21f45aa88b941e4c39ed
Showing with 18 additions and 4 deletions.
  1. +2 −1 README.md
  2. +4 −1 index.js
  3. +1 −0 schema.proto
  4. +11 −2 test.js
View
@@ -37,6 +37,7 @@ Options include:
{
id: optionalPeerId, // you can use this to detect if you connect to yourself
live: keepStreamOpen, // signal to the other peer that you want to keep this stream open forever
userData: opaqueUserData // include user data that you can retrieve on handshake
encrypt: true, // set to false to disable encryption if you are already piping through a encrypted stream
timeout: 5000 // stream timeout. set to 0 or false to disable.
}
@@ -54,7 +55,7 @@ should be the same one. The key of the first feed is also used to encrypt the st
#### `stream.on('handshake')`
Emitted when a protocol handshake has been received. Afterwards you can check `.remoteId` to get the remote peer id and `.remoteLive` to get their live status.
Emitted when a protocol handshake has been received. Afterwards you can check `.remoteId` to get the remote peer id, `.remoteLive` to get its live status, or `.remoteUserData` to get its user data.
#### `stream.on('feed', discoveryKey)`
View
@@ -15,8 +15,10 @@ function Protocol (opts) {
this.id = opts.id || randomBytes(32)
this.live = !!opts.live
this.userData = opts.userData || null
this.remoteId = null
this.remoteLive = false
this.remoteUserData = null
this.destroyed = false
this.encrypted = opts.encrypt !== false
@@ -129,7 +131,7 @@ Protocol.prototype.feed = function (key, opts) {
if (this.destroyed) return null
if (first) {
ch.handshake({id: this.id, live: this.live})
ch.handshake({id: this.id, live: this.live, userData: this.userData})
}
if (ch._buffer.length) ch._resume()
@@ -235,6 +237,7 @@ Protocol.prototype._onhandshake = function (handshake) {
if (this.remoteId) return
this.remoteId = handshake.id || randomBytes(32)
this.remoteLive = handshake.live
this.remoteUserData = handshake.userData
this.emit('handshake')
}
View
@@ -11,6 +11,7 @@ message Feed {
message Handshake {
optional bytes id = 1;
optional bool live = 2; // keep the connection open forever? both ends have to agree
optional bytes userData = 3;
}
// type=2, message indicating state changes etc.
View
13 test.js
@@ -26,9 +26,14 @@ tape('basic', function (t) {
})
tape('basic with handshake options', function (t) {
t.plan(8)
t.plan(12)
var a = protocol({id: new Buffer('a'), live: true})
var data = [
'eeaa62fbb11ba521cce58cf3fae42deb15d94a0436fc7fa0cbba8f130e7c0499',
'8c797667bf307d82c51a8308fe477b781a13708e0ec1f2cc7f497392574e2464'
]
var a = protocol({id: new Buffer('a'), live: true, userData: new Buffer(data)})
var b = protocol({id: new Buffer('b'), live: false})
a.feed(KEY)
@@ -37,15 +42,19 @@ tape('basic with handshake options', function (t) {
a.once('handshake', function () {
t.same(a.id, new Buffer('a'))
t.same(a.live, true)
t.same(a.userData, new Buffer(data))
t.same(a.remoteId, new Buffer('b'))
t.same(a.remoteLive, false)
t.same(a.remoteUserData, null)
})
b.once('handshake', function () {
t.same(b.id, new Buffer('b'))
t.same(b.live, false)
t.same(b.userData, null)
t.same(b.remoteId, new Buffer('a'))
t.same(b.remoteLive, true)
t.same(b.remoteUserData, new Buffer(data))
})
a.pipe(b).pipe(a)

0 comments on commit 61eec47

Please sign in to comment.