Skip to content

Commit 31b2bbb

Browse files
committed
be stateless
1 parent a066c20 commit 31b2bbb

File tree

3 files changed

+24
-59
lines changed

3 files changed

+24
-59
lines changed

index.js

+17-51
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ var stream = require('readable-stream')
77
var events = require('events')
88
var sodium = require('sodium-prebuilt').api // TODO: make me work in the browser
99
var increment = require('increment-buffer')
10-
var bitfield = require('bitfield')
1110
var messages = require('./messages')
1211

13-
var MAX_BITFIELD = 4 * 1024 * 1024
1412
var MAX_MESSAGE = 5 * 1024 * 1024
1513
var MAX_EXTENSIONS = 64 // theoretically we can support any amount though
1614
var MAX_SINGLE_BYTE_VARINT = 127
@@ -52,17 +50,10 @@ function use (extensions) {
5250
this.id = id
5351
this.key = null
5452
this.context = null // someone else can set this
55-
5653
this.remoteOpened = false
5754
this.opened = false
58-
5955
this.closed = false
6056

61-
this.remotePausing = true
62-
this.pausing = true
63-
64-
this.remoteBitfield = bitfield(1, {grow: MAX_BITFIELD})
65-
6657
this._secure = protocol._secure !== false
6758
this._nonce = null
6859
this._remoteNonce = null
@@ -106,9 +97,9 @@ function use (extensions) {
10697
this.emit('update')
10798
}
10899

109-
Channel.prototype.have = function (blocks, bitfield) {
110-
if (typeof blocks === 'number') blocks = [blocks]
111-
this._send(2, {blocks: blocks, bitfield: toBuffer(bitfield)})
100+
Channel.prototype.have = function (have) {
101+
if (typeof have === 'number') this._send(2, {blocks: [have]})
102+
else this._send(2, have)
112103
}
113104

114105
Channel.prototype.resume = function () {
@@ -119,16 +110,18 @@ function use (extensions) {
119110
this._send(4, null)
120111
}
121112

122-
Channel.prototype.request = function (block) {
123-
this._send(5, {block: block})
113+
Channel.prototype.request = function (request) {
114+
if (typeof request === 'number') this._send(5, {block: request})
115+
else this._send(5, request)
124116
}
125117

126-
Channel.prototype.response = function (block, data, proof) {
127-
this._send(6, {block: block, data: data, proof: proof})
118+
Channel.prototype.response = function (response) {
119+
this._send(6, response)
128120
}
129121

130-
Channel.prototype.cancel = function (block) {
131-
this._send(7, {block: block})
122+
Channel.prototype.cancel = function (cancel) {
123+
if (typeof cancel === 'number') this._send(7, {block: cancel})
124+
else this._send(7, cancel)
132125
}
133126

134127
Channel.prototype.remoteSupports = function (id) {
@@ -274,48 +267,27 @@ function use (extensions) {
274267
}
275268

276269
Channel.prototype._onhave = function (message) {
277-
if (this.closed) return
278-
279-
if (message.bitfield) {
280-
// TODO: this should be a proof bitfield instead
281-
this.remoteBitfield = bitfield(message.bitfield, {grow: MAX_BITFIELD})
282-
}
283-
284-
var i = 0
285-
286-
for (i = 0; i < message.blocks.length; i++) {
287-
var block = message.blocks[i]
288-
this.remoteBitfield.set(block)
289-
}
290-
291-
this.emit('have')
292-
this.emit('update')
270+
if (!this.closed) this.emit('have')
293271
}
294272

295273
Channel.prototype._onresume = function () {
296-
if (this.closed) return
297-
this.remotePausing = false
298-
this.emit('resume')
299-
this.emit('update')
274+
if (this.closed) this.emit('resume')
300275
}
301276

302277
Channel.prototype._onpause = function () {
303-
if (this.closed) return
304-
this.remotePausing = true
305-
this.emit('pause')
306-
this.emit('update')
278+
if (!this.closed) this.emit('pause')
307279
}
308280

309281
Channel.prototype._onrequest = function (message) {
310-
if (!this.closed) this.emit('request', message.block)
282+
if (!this.closed) this.emit('request', message)
311283
}
312284

313285
Channel.prototype._onresponse = function (message) {
314-
if (!this.closed) this.emit('response', message.block, message.data, message.proof)
286+
if (!this.closed) this.emit('response', message)
315287
}
316288

317289
Channel.prototype._oncancel = function (message) {
318-
if (!this.closed) this.emit('cancel', message.block)
290+
if (!this.closed) this.emit('cancel', message)
319291
}
320292

321293
Channel.prototype._decrypt = function (cipher) {
@@ -557,12 +529,6 @@ function publicId (key) {
557529
return crypto.createHmac('sha256', key).update('hypercore').digest()
558530
}
559531

560-
function toBuffer (bitfield) {
561-
if (!bitfield) return null
562-
if (Buffer.isBuffer(bitfield)) return bitfield
563-
return bitfield.buffer
564-
}
565-
566532
function toString (val) {
567533
return val.toString()
568534
}

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"description": "Stream that implements the hypercore protocol",
55
"main": "index.js",
66
"dependencies": {
7-
"bitfield": "^1.1.2",
87
"brfs": "^1.4.3",
98
"duplexify": "^3.4.3",
109
"increment-buffer": "^1.0.0",

test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ tape('encrypts messages', function (t) {
6262
})
6363

6464
var ch = p.join(key)
65-
ch.response(0, Buffer('hello i should be encrypted.'))
65+
ch.response({block: 0, data: Buffer('hello i should be encrypted.')})
6666
p.end()
6767
})
6868

@@ -78,18 +78,18 @@ tape('remote joins', function (t) {
7878
remoteJoined--
7979
})
8080

81-
ch1.on('request', function (block) {
82-
t.same(block, 42, 'received request')
83-
ch1.response(42, Buffer('some data'))
81+
ch1.on('request', function (request) {
82+
t.same(request.block, 42, 'received request')
83+
ch1.response({block: 42, data: Buffer('some data')})
8484
})
8585

8686
var ch2 = p2.join(key)
8787

8888
ch2.request(42)
8989

90-
ch2.on('response', function (block, data, proof) {
91-
t.same(block, 42, 'received response')
92-
t.same(data, Buffer('some data'), 'expected data')
90+
ch2.on('response', function (response) {
91+
t.same(response.block, 42, 'received response')
92+
t.same(response.data, Buffer('some data'), 'expected data')
9393
t.same(remoteJoined, 0, 'both emitted open')
9494
t.end()
9595
})

0 commit comments

Comments
 (0)