@@ -7,39 +7,40 @@ const path = require('path')
7
7
const rimraf = require ( 'rimraf' )
8
8
9
9
const Client = require ( 'libp2p-daemon-client' )
10
- const { getSockPath , isWindows } = require ( './utils' )
10
+ const { getMultiaddr , isWindows } = require ( './utils' )
11
11
12
12
// process path
13
13
const processPath = process . cwd ( )
14
14
15
15
// go-libp2p defaults
16
16
const goDaemon = {
17
- defaultSock : getSockPath ( '/tmp/p2pd-go.sock' ) ,
17
+ defaultAddr : getMultiaddr ( '/tmp/p2pd-go.sock' ) ,
18
18
bin : path . join ( 'go-libp2p-dep' , 'go-libp2p' , isWindows ? 'p2pd.exe' : 'p2pd' )
19
19
}
20
20
21
21
// js-libp2p defaults
22
22
const jsDaemon = {
23
- defaultSock : getSockPath ( '/tmp/p2pd-js.sock' ) ,
23
+ defaultAddr : getMultiaddr ( '/tmp/p2pd-js.sock' ) ,
24
24
bin : path . join ( 'libp2p-daemon' , 'src' , 'cli' , 'bin.js' )
25
25
}
26
26
27
27
class Daemon {
28
28
/**
29
29
* @constructor
30
30
* @param {String } type daemon implementation type ("go" or "js")
31
- * @param {String } sock unix socket path
31
+ * @param {Multiaddr } addr multiaddr for the client to connect to
32
+ * @param {Number } port port for the client to connect to
32
33
*/
33
- constructor ( type , sock ) {
34
+ constructor ( type , addr , port ) {
34
35
assert ( type === 'go' || type === 'js' , 'invalid type received. Should be "go" or "js"' )
35
36
36
37
this . _client = undefined
37
38
this . _type = type
38
39
this . _binPath = this . _getBinPath ( type )
39
- this . _sock = sock && getSockPath ( sock )
40
+ this . _addr = addr && getMultiaddr ( addr , port )
40
41
41
- if ( ! this . _sock ) {
42
- this . _sock = type === 'go' ? goDaemon . defaultSock : jsDaemon . defaultSock
42
+ if ( ! this . _addr ) {
43
+ this . _addr = type === 'go' ? goDaemon . defaultAddr : jsDaemon . defaultAddr
43
44
}
44
45
}
45
46
@@ -80,7 +81,7 @@ class Daemon {
80
81
await this . _startDaemon ( )
81
82
82
83
// start client
83
- this . _client = new Client ( this . _sock )
84
+ this . _client = new Client ( this . _addr )
84
85
85
86
await this . _client . attach ( )
86
87
}
@@ -92,7 +93,7 @@ class Daemon {
92
93
*/
93
94
_startDaemon ( ) {
94
95
return new Promise ( ( resolve , reject ) => {
95
- const options = this . _type === 'go' ? [ '-listen' , `/unix ${ this . _sock } ` ] : [ '--sock ' , this . _sock ]
96
+ const options = this . _type === 'go' ? [ '-listen' , `${ this . _addr } ` ] : [ '--listen ' , this . _addr ]
96
97
const daemon = execa ( this . _binPath , options )
97
98
98
99
daemon . stdout . once ( 'data' , ( ) => {
@@ -121,7 +122,12 @@ class Daemon {
121
122
*/
122
123
_cleanUnixSocket ( ) {
123
124
return new Promise ( ( resolve , reject ) => {
124
- rimraf ( this . _sock , ( err ) => {
125
+ const path = this . _addr . toString ( ) . split ( '/unix' )
126
+ if ( ! path ) {
127
+ return resolve ( )
128
+ }
129
+
130
+ rimraf ( path [ 1 ] , ( err ) => {
125
131
if ( err ) {
126
132
return reject ( err )
127
133
}
0 commit comments