@@ -10,6 +10,7 @@ const grpc = require('@grpc/grpc-js')
1010const corestore = require ( 'corestore' )
1111const SwarmNetworker = require ( 'corestore-swarm-networking' )
1212
13+
1314const { rpc, loadMetadata } = require ( 'hyperdrive-daemon-client' )
1415const constants = require ( 'hyperdrive-daemon-client/lib/constants' )
1516
@@ -34,12 +35,6 @@ class HyperdriveDaemon extends EventEmitter {
3435 this . storage = opts . storage || constants . storage
3536 this . port = opts . port || constants . port
3637
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-
4338 const corestoreOpts = {
4439 storage : path => raf ( `${ this . storage } /cores/${ path } ` ) ,
4540 sparse : true ,
@@ -60,21 +55,24 @@ class HyperdriveDaemon extends EventEmitter {
6055 }
6156 this . networking = new SwarmNetworker ( this . corestore , networkOpts )
6257
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+
6563 // Set in start.
6664 this . server = null
65+ this . _isMain = ! ! opts . main
6766 this . _cleanup = null
6867
69- this . drives . on ( 'error' , err => this . emit ( 'error' , err ) )
70- this . fuse . on ( 'error' , err => this . emit ( 'error' , err ) )
71-
7268 this . _isClosed = false
73- this . _isReady = false
69+ this . _readyPromise = false
7470
7571 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
7876 }
7977 }
8078
@@ -87,14 +85,24 @@ class HyperdriveDaemon extends EventEmitter {
8785 process . once ( event , this . _cleanup )
8886 }
8987
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 ( [
93101 this . drives . ready ( ) ,
94102 this . fuse ? this . fuse . ready ( ) : Promise . resolve ( )
95- ] ) . then ( ( ) => {
96- this . _ready = true
97- } )
103+ ] )
104+
105+ this . _isReady = true
98106 }
99107
100108 async _loadMetadata ( ) {
@@ -118,10 +126,11 @@ class HyperdriveDaemon extends EventEmitter {
118126 createMainHandlers ( ) {
119127 return {
120128 stop : async ( call ) => {
121- await this . close ( )
129+ await this . stop ( )
122130 setTimeout ( ( ) => {
123131 console . error ( 'Daemon is exiting.' )
124132 this . server . forceShutdown ( )
133+ if ( this . _isMain ) process . exit ( 0 )
125134 } , 250 )
126135 return new rpc . main . messages . StopResponse ( )
127136 } ,
@@ -137,7 +146,6 @@ class HyperdriveDaemon extends EventEmitter {
137146
138147 if ( this . fuse && this . fuse . fuseConfigured ) await this . fuse . unmount ( )
139148 if ( this . networking ) await this . networking . close ( )
140- if ( this . server ) this . server . forceShutdown ( )
141149 await this . db . close ( )
142150
143151 for ( const event of STOP_EVENTS ) {
@@ -171,11 +179,6 @@ class HyperdriveDaemon extends EventEmitter {
171179 return resolve ( )
172180 } )
173181 } )
174-
175-
176- async function close ( ) {
177- await this . close ( )
178- }
179182 }
180183}
181184
@@ -236,7 +239,7 @@ function wrap (metadata, methods, opts) {
236239
237240if ( require . main === module ) {
238241 const opts = extractArguments ( )
239- const daemon = new HyperdriveDaemon ( opts )
242+ const daemon = new HyperdriveDaemon ( { ... opts , main : true } )
240243 daemon . start ( )
241244} else {
242245 module . exports = HyperdriveDaemon
0 commit comments