@@ -10,6 +10,7 @@ const grpc = require('@grpc/grpc-js')
10
10
const corestore = require ( 'corestore' )
11
11
const SwarmNetworker = require ( 'corestore-swarm-networking' )
12
12
13
+
13
14
const { rpc, loadMetadata } = require ( 'hyperdrive-daemon-client' )
14
15
const constants = require ( 'hyperdrive-daemon-client/lib/constants' )
15
16
@@ -34,12 +35,6 @@ class HyperdriveDaemon extends EventEmitter {
34
35
this . storage = opts . storage || constants . storage
35
36
this . port = opts . port || constants . port
36
37
37
- this . db = level ( `${ this . storage } /db` , { valueEncoding : 'json' } )
38
- const dbs = {
39
- fuse : sub ( this . db , 'fuse' , { valueEncoding : 'json' } ) ,
40
- drives : sub ( this . db , 'drives' , { valueEncoding : 'json' } )
41
- }
42
-
43
38
const corestoreOpts = {
44
39
storage : path => raf ( `${ this . storage } /cores/${ path } ` ) ,
45
40
sparse : true ,
@@ -60,21 +55,24 @@ class HyperdriveDaemon extends EventEmitter {
60
55
}
61
56
this . networking = new SwarmNetworker ( this . corestore , networkOpts )
62
57
63
- this . drives = new DriveManager ( this . corestore , this . networking , dbs . drives , this . opts )
64
- this . fuse = hyperfuse ? new FuseManager ( this . megastore , this . drives , dbs . fuse , this . opts ) : null
58
+ // Set in ready.
59
+ this . db = null
60
+ this . drives = null
61
+ this . fuse = null
62
+
65
63
// Set in start.
66
64
this . server = null
65
+ this . _isMain = ! ! opts . main
67
66
this . _cleanup = null
68
67
69
- this . drives . on ( 'error' , err => this . emit ( 'error' , err ) )
70
- this . fuse . on ( 'error' , err => this . emit ( 'error' , err ) )
71
-
72
68
this . _isClosed = false
73
- this . _isReady = false
69
+ this . _readyPromise = false
74
70
75
71
this . ready = ( ) => {
76
- if ( this . _isReady ) return Promise . resolve ( )
77
- return this . _ready ( )
72
+ if ( this . _isClosed ) return Promise . resolve ( )
73
+ if ( this . _readyPromise ) return this . _readyPromise
74
+ this . _readyPromise = this . _ready ( )
75
+ return this . _readyPromise
78
76
}
79
77
}
80
78
@@ -87,14 +85,24 @@ class HyperdriveDaemon extends EventEmitter {
87
85
process . once ( event , this . _cleanup )
88
86
}
89
87
90
- return Promise . all ( [
91
- this . db . open ( ) ,
92
- this . networking . listen ( ) ,
88
+ this . networking . listen ( )
89
+
90
+ this . db = level ( `${ this . storage } /db` , { valueEncoding : 'json' } )
91
+ const dbs = {
92
+ fuse : sub ( this . db , 'fuse' , { valueEncoding : 'json' } ) ,
93
+ drives : sub ( this . db , 'drives' , { valueEncoding : 'json' } )
94
+ }
95
+ this . drives = new DriveManager ( this . corestore , this . networking , dbs . drives , this . opts )
96
+ this . fuse = hyperfuse ? new FuseManager ( this . drives , dbs . fuse , this . opts ) : null
97
+ this . drives . on ( 'error' , err => this . emit ( 'error' , err ) )
98
+ this . fuse . on ( 'error' , err => this . emit ( 'error' , err ) )
99
+
100
+ await Promise . all ( [
93
101
this . drives . ready ( ) ,
94
102
this . fuse ? this . fuse . ready ( ) : Promise . resolve ( )
95
- ] ) . then ( ( ) => {
96
- this . _ready = true
97
- } )
103
+ ] )
104
+
105
+ this . _isReady = true
98
106
}
99
107
100
108
async _loadMetadata ( ) {
@@ -118,10 +126,11 @@ class HyperdriveDaemon extends EventEmitter {
118
126
createMainHandlers ( ) {
119
127
return {
120
128
stop : async ( call ) => {
121
- await this . close ( )
129
+ await this . stop ( )
122
130
setTimeout ( ( ) => {
123
131
console . error ( 'Daemon is exiting.' )
124
132
this . server . forceShutdown ( )
133
+ if ( this . _isMain ) process . exit ( 0 )
125
134
} , 250 )
126
135
return new rpc . main . messages . StopResponse ( )
127
136
} ,
@@ -137,7 +146,6 @@ class HyperdriveDaemon extends EventEmitter {
137
146
138
147
if ( this . fuse && this . fuse . fuseConfigured ) await this . fuse . unmount ( )
139
148
if ( this . networking ) await this . networking . close ( )
140
- if ( this . server ) this . server . forceShutdown ( )
141
149
await this . db . close ( )
142
150
143
151
for ( const event of STOP_EVENTS ) {
@@ -171,11 +179,6 @@ class HyperdriveDaemon extends EventEmitter {
171
179
return resolve ( )
172
180
} )
173
181
} )
174
-
175
-
176
- async function close ( ) {
177
- await this . close ( )
178
- }
179
182
}
180
183
}
181
184
@@ -236,7 +239,7 @@ function wrap (metadata, methods, opts) {
236
239
237
240
if ( require . main === module ) {
238
241
const opts = extractArguments ( )
239
- const daemon = new HyperdriveDaemon ( opts )
242
+ const daemon = new HyperdriveDaemon ( { ... opts , main : true } )
240
243
daemon . start ( )
241
244
} else {
242
245
module . exports = HyperdriveDaemon
0 commit comments