@@ -3,22 +3,24 @@ const { EventEmitter } = require('events')
3
3
4
4
const hyperdrive = require ( 'hyperdrive' )
5
5
const sub = require ( 'subleveldown' )
6
- const collect = require ( 'stream-collector' )
6
+ const collectStream = require ( 'stream-collector' )
7
7
const datEncoding = require ( 'dat-encoding' )
8
8
9
9
const { fromHyperdriveOptions, toHyperdriveOptions } = require ( 'hyperdrive-daemon-client/lib/common' )
10
10
const log = require ( '../log' ) . child ( { component : 'drive-manager' } )
11
11
12
12
class DriveManager extends EventEmitter {
13
- constructor ( megastore , db , opts ) {
13
+ constructor ( megastore , networking , db , opts ) {
14
14
super ( )
15
15
16
16
this . megastore = megastore
17
+ this . networking = networking
17
18
this . db = db
18
19
this . opts = opts || { }
19
20
20
21
this . _driveIndex = sub ( this . db , 'drives' , { valueEncoding : 'json' } )
21
- this . _nameIndex = sub ( this . db , 'names' , { valueEncoding : 'utf-8' } )
22
+ this . _seedIndex = sub ( this . db , 'seeding' , { valueEncoding : 'utf8' } )
23
+ this . _nameIndex = sub ( this . db , 'names' , { valueEncoding : 'utf8' } )
22
24
23
25
if ( this . opts . stats ) {
24
26
this . _statsIndex = sub ( this . db , 'stats' , { valueEncoding : 'json' } )
@@ -27,8 +29,18 @@ class DriveManager extends EventEmitter {
27
29
28
30
// TODO: Replace with an LRU cache.
29
31
this . _drives = new Map ( )
30
- // TODO: Any ready behavior here?
31
- this . ready = ( ) => Promise . resolve ( )
32
+ this . _ready = new Promise ( async resolve => {
33
+ await this . _reseed ( )
34
+ return resolve ( )
35
+ } )
36
+ this . ready = ( ) => this . _ready
37
+ }
38
+
39
+ async _reseed ( ) {
40
+ const driveList = await collect ( this . _driveIndex )
41
+ for ( const { key : discoveryKey } of driveList ) {
42
+ this . networking . seed ( discoveryKey )
43
+ }
32
44
}
33
45
34
46
_configureDrive ( drive , opts ) {
@@ -147,11 +159,15 @@ class DriveManager extends EventEmitter {
147
159
}
148
160
149
161
async publish ( drive ) {
150
- return this . megastore . seed ( drive . discoveryKey )
162
+ const encodedKey = datEncoding . encode ( drive . discoveryKey )
163
+ this . networking . seed ( drive . discoveryKey )
164
+ await this . _seedIndex . put ( encodedKey , '' )
151
165
}
152
166
153
167
async unpublish ( drive ) {
154
- return this . megastore . unseed ( drive . discoveryKey )
168
+ const encodedKey = datEncoding . encode ( drive . discoveryKey )
169
+ this . networking . unseed ( drive . discoveryKey )
170
+ await this . _seedIndex . del ( encodedKey )
155
171
}
156
172
157
173
// TODO: Retrieving stats from managed hyperdrives is trickier with corestores/mounts.
@@ -206,6 +222,15 @@ function createDriveHandlers (driveManager) {
206
222
}
207
223
}
208
224
225
+ function collect ( index , opts ) {
226
+ return new Promise ( ( resolve , reject ) => {
227
+ collectStream ( index . createReadStream ( opts ) , ( err , list ) => {
228
+ if ( err ) return reject ( err )
229
+ return resolve ( list )
230
+ } )
231
+ } )
232
+ }
233
+
209
234
module . exports = {
210
235
DriveManager,
211
236
createDriveHandlers
0 commit comments