Skip to content

Commit 61eec47

Browse files
scriptjsmafintosh
authored andcommitted
support opaque user data (hypercore-protocol#13)
1 parent 04ab65d commit 61eec47

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Options include:
3737
{
3838
id: optionalPeerId, // you can use this to detect if you connect to yourself
3939
live: keepStreamOpen, // signal to the other peer that you want to keep this stream open forever
40+
userData: opaqueUserData // include user data that you can retrieve on handshake
4041
encrypt: true, // set to false to disable encryption if you are already piping through a encrypted stream
4142
timeout: 5000 // stream timeout. set to 0 or false to disable.
4243
}
@@ -54,7 +55,7 @@ should be the same one. The key of the first feed is also used to encrypt the st
5455

5556
#### `stream.on('handshake')`
5657

57-
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.
58+
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.
5859

5960
#### `stream.on('feed', discoveryKey)`
6061

index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ function Protocol (opts) {
1515

1616
this.id = opts.id || randomBytes(32)
1717
this.live = !!opts.live
18+
this.userData = opts.userData || null
1819
this.remoteId = null
1920
this.remoteLive = false
21+
this.remoteUserData = null
2022

2123
this.destroyed = false
2224
this.encrypted = opts.encrypt !== false
@@ -129,7 +131,7 @@ Protocol.prototype.feed = function (key, opts) {
129131
if (this.destroyed) return null
130132

131133
if (first) {
132-
ch.handshake({id: this.id, live: this.live})
134+
ch.handshake({id: this.id, live: this.live, userData: this.userData})
133135
}
134136

135137
if (ch._buffer.length) ch._resume()
@@ -235,6 +237,7 @@ Protocol.prototype._onhandshake = function (handshake) {
235237
if (this.remoteId) return
236238
this.remoteId = handshake.id || randomBytes(32)
237239
this.remoteLive = handshake.live
240+
this.remoteUserData = handshake.userData
238241
this.emit('handshake')
239242
}
240243

schema.proto

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ message Feed {
1111
message Handshake {
1212
optional bytes id = 1;
1313
optional bool live = 2; // keep the connection open forever? both ends have to agree
14+
optional bytes userData = 3;
1415
}
1516

1617
// type=2, message indicating state changes etc.

test.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@ tape('basic', function (t) {
2626
})
2727

2828
tape('basic with handshake options', function (t) {
29-
t.plan(8)
29+
t.plan(12)
3030

31-
var a = protocol({id: new Buffer('a'), live: true})
31+
var data = [
32+
'eeaa62fbb11ba521cce58cf3fae42deb15d94a0436fc7fa0cbba8f130e7c0499',
33+
'8c797667bf307d82c51a8308fe477b781a13708e0ec1f2cc7f497392574e2464'
34+
]
35+
36+
var a = protocol({id: new Buffer('a'), live: true, userData: new Buffer(data)})
3237
var b = protocol({id: new Buffer('b'), live: false})
3338

3439
a.feed(KEY)
@@ -37,15 +42,19 @@ tape('basic with handshake options', function (t) {
3742
a.once('handshake', function () {
3843
t.same(a.id, new Buffer('a'))
3944
t.same(a.live, true)
45+
t.same(a.userData, new Buffer(data))
4046
t.same(a.remoteId, new Buffer('b'))
4147
t.same(a.remoteLive, false)
48+
t.same(a.remoteUserData, null)
4249
})
4350

4451
b.once('handshake', function () {
4552
t.same(b.id, new Buffer('b'))
4653
t.same(b.live, false)
54+
t.same(b.userData, null)
4755
t.same(b.remoteId, new Buffer('a'))
4856
t.same(b.remoteLive, true)
57+
t.same(b.remoteUserData, new Buffer(data))
4958
})
5059

5160
a.pipe(b).pipe(a)

0 commit comments

Comments
 (0)