@@ -7,10 +7,8 @@ var stream = require('readable-stream')
7
7
var events = require ( 'events' )
8
8
var sodium = require ( 'sodium-prebuilt' ) . api // TODO: make me work in the browser
9
9
var increment = require ( 'increment-buffer' )
10
- var bitfield = require ( 'bitfield' )
11
10
var messages = require ( './messages' )
12
11
13
- var MAX_BITFIELD = 4 * 1024 * 1024
14
12
var MAX_MESSAGE = 5 * 1024 * 1024
15
13
var MAX_EXTENSIONS = 64 // theoretically we can support any amount though
16
14
var MAX_SINGLE_BYTE_VARINT = 127
@@ -52,17 +50,10 @@ function use (extensions) {
52
50
this . id = id
53
51
this . key = null
54
52
this . context = null // someone else can set this
55
-
56
53
this . remoteOpened = false
57
54
this . opened = false
58
-
59
55
this . closed = false
60
56
61
- this . remotePausing = true
62
- this . pausing = true
63
-
64
- this . remoteBitfield = bitfield ( 1 , { grow : MAX_BITFIELD } )
65
-
66
57
this . _secure = protocol . _secure !== false
67
58
this . _nonce = null
68
59
this . _remoteNonce = null
@@ -106,9 +97,9 @@ function use (extensions) {
106
97
this . emit ( 'update' )
107
98
}
108
99
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 )
112
103
}
113
104
114
105
Channel . prototype . resume = function ( ) {
@@ -119,16 +110,18 @@ function use (extensions) {
119
110
this . _send ( 4 , null )
120
111
}
121
112
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 )
124
116
}
125
117
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 )
128
120
}
129
121
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 )
132
125
}
133
126
134
127
Channel . prototype . remoteSupports = function ( id ) {
@@ -274,48 +267,27 @@ function use (extensions) {
274
267
}
275
268
276
269
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' )
293
271
}
294
272
295
273
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' )
300
275
}
301
276
302
277
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' )
307
279
}
308
280
309
281
Channel . prototype . _onrequest = function ( message ) {
310
- if ( ! this . closed ) this . emit ( 'request' , message . block )
282
+ if ( ! this . closed ) this . emit ( 'request' , message )
311
283
}
312
284
313
285
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 )
315
287
}
316
288
317
289
Channel . prototype . _oncancel = function ( message ) {
318
- if ( ! this . closed ) this . emit ( 'cancel' , message . block )
290
+ if ( ! this . closed ) this . emit ( 'cancel' , message )
319
291
}
320
292
321
293
Channel . prototype . _decrypt = function ( cipher ) {
@@ -557,12 +529,6 @@ function publicId (key) {
557
529
return crypto . createHmac ( 'sha256' , key ) . update ( 'hypercore' ) . digest ( )
558
530
}
559
531
560
- function toBuffer ( bitfield ) {
561
- if ( ! bitfield ) return null
562
- if ( Buffer . isBuffer ( bitfield ) ) return bitfield
563
- return bitfield . buffer
564
- }
565
-
566
532
function toString ( val ) {
567
533
return val . toString ( )
568
534
}
0 commit comments