11'use strict'
22
3- var level = require ( 'level' )
4- var has = require ( 'has' )
5- var pump = require ( 'pump' )
6- var fs = require ( 'fs' )
7- var net = require ( 'net' )
8- var path = require ( 'path' )
9- var multileveldown = require ( 'multileveldown' )
10-
11- module . exports = function ( dir , opts ) {
12- if ( ! opts ) opts = { }
13- if ( ! has ( opts , 'retry' ) ) opts . retry = true
14-
15- var sockPath = process . platform === 'win32'
3+ const level = require ( 'level' )
4+ const { pipeline : pump } = require ( 'readable-stream' )
5+ const fs = require ( 'fs' )
6+ const net = require ( 'net' )
7+ const path = require ( 'path' )
8+ const multileveldown = require ( 'multileveldown' )
9+
10+ module . exports = function ( dir , opts = { } ) {
11+ const sockPath = process . platform === 'win32'
1612 ? '\\\\.\\pipe\\level-party\\' + path . resolve ( dir )
1713 : path . join ( dir , 'level-party.sock' )
1814
19- var client = multileveldown . client ( opts )
15+ opts = { retry : true , ...opts }
16+
17+ const client = multileveldown . client ( opts )
2018
2119 client . open ( tryConnect )
2220
2321 function tryConnect ( ) {
24- if ( ! client . isOpen ( ) ) return
22+ if ( ! client . isOpen ( ) ) {
23+ return
24+ }
2525
26- var socket = net . connect ( sockPath )
27- var connected = false
26+ const socket = net . connect ( sockPath )
27+ let connected = false
2828
2929 socket . on ( 'connect' , function ( ) {
3030 connected = true
3131 } )
3232
33- // we pass socket as the ref option so we dont hang the event loop
33+ // Pass socket as the ref option so we dont hang the event loop.
3434 pump ( socket , client . createRpcStream ( { ref : socket } ) , socket , function ( ) {
35- if ( ! client . isOpen ( ) ) return
35+ // TODO: err?
36+
37+ if ( ! client . isOpen ( ) ) {
38+ return
39+ }
3640
37- var db = level ( dir , opts , onopen )
41+ const db = level ( dir , opts , onopen )
3842
3943 function onopen ( err ) {
4044 if ( err ) {
41- if ( connected ) return tryConnect ( )
42- return setTimeout ( tryConnect , 100 )
45+ // TODO: This can cause an invisible retry loop that never completes
46+ // and leads to memory leaks.
47+ // TODO: What errors should be retried?
48+ if ( connected ) {
49+ tryConnect ( )
50+ } else {
51+ setTimeout ( tryConnect , 100 )
52+ }
53+ return
4354 }
4455
4556 fs . unlink ( sockPath , function ( err ) {
46- if ( err && err . code !== 'ENOENT' ) return db . emit ( 'error' , err )
47- if ( ! client . isOpen ( ) ) return
57+ if ( err && err . code !== 'ENOENT' ) {
58+ // TODO: Is this how to forward errors?
59+ db . emit ( 'error' , err )
60+ return
61+ }
62+
63+ if ( ! client . isOpen ( ) ) {
64+ return
65+ }
66+
67+ const sockets = new Set ( )
68+ const server = net . createServer ( function ( sock ) {
69+ if ( sock . unref ) {
70+ sock . unref ( )
71+ }
4872
49- var sockets = [ ]
50- var server = net . createServer ( function ( sock ) {
51- if ( sock . unref ) sock . unref ( )
52- sockets . push ( sock )
73+ sockets . add ( sock )
5374 pump ( sock , multileveldown . server ( db ) , sock , function ( ) {
54- sockets . splice ( sockets . indexOf ( sock ) , 1 )
75+ // TODO: err?
76+ sockets . delete ( sock )
5577 } )
5678 } )
5779
@@ -60,30 +82,41 @@ module.exports = function (dir, opts) {
6082 client . forward ( db )
6183
6284 server . listen ( sockPath , onlistening )
85+ . on ( 'error' , function ( ) {
86+ // TODO: Is this how to forward errors?
87+ // TODO: tryConnect()?
88+ db . emit ( 'error' , err )
89+ } )
6390
6491 function shutdown ( cb ) {
65- sockets . forEach ( function ( sock ) {
92+ for ( const sock of sockets ) {
6693 sock . destroy ( )
67- } )
68- server . close ( function ( ) {
94+ }
95+ server . close ( ( ) => {
6996 db . close ( cb )
7097 } )
7198 }
7299
73100 function onlistening ( ) {
74- if ( server . unref ) server . unref ( )
75- if ( client . isFlushed ( ) ) return
76-
77- var sock = net . connect ( sockPath )
78- pump ( sock , client . createRpcStream ( ) , sock )
101+ if ( server . unref ) {
102+ server . unref ( )
103+ }
104+ if ( client . isFlushed ( ) ) {
105+ return
106+ }
107+
108+ const sock = net . connect ( sockPath )
109+ pump ( sock , client . createRpcStream ( ) , sock , function ( ) {
110+ // TODO: err?
111+ } )
79112 client . once ( 'flush' , function ( ) {
80113 sock . destroy ( )
81114 } )
82115 }
83116 } )
84117 }
85118 } )
86- } ;
119+ }
87120
88121 return client
89122}
0 commit comments