Skip to content

Commit 3f87879

Browse files
committed
chore: update daemon and daemon-client
1 parent e3364bf commit 3f87879

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
},
2828
"homepage": "https://github.com/libp2p/interop#readme",
2929
"devDependencies": {
30-
"aegir": "^18.1.0",
30+
"aegir": "^18.2.1",
3131
"chai": "^4.2.0",
3232
"chai-checkmark": "^1.0.1",
3333
"cross-env": "^5.2.0",
3434
"dirty-chai": "^2.0.1",
3535
"go-libp2p-dep": "^6.0.30",
36-
"libp2p-daemon": "~0.1.2",
37-
"libp2p-daemon-client": "~0.0.3",
36+
"libp2p-daemon": "~0.2.0",
37+
"libp2p-daemon-client": "~0.1.0",
38+
"multiaddr": "^6.0.6",
3839
"rimraf": "^2.6.3"
3940
},
4041
"contributors": [],

src/daemon.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,40 @@ const path = require('path')
77
const rimraf = require('rimraf')
88

99
const Client = require('libp2p-daemon-client')
10-
const { getSockPath, isWindows } = require('./utils')
10+
const { getMultiaddr, isWindows } = require('./utils')
1111

1212
// process path
1313
const processPath = process.cwd()
1414

1515
// go-libp2p defaults
1616
const goDaemon = {
17-
defaultSock: getSockPath('/tmp/p2pd-go.sock'),
17+
defaultAddr: getMultiaddr('/tmp/p2pd-go.sock'),
1818
bin: path.join('go-libp2p-dep', 'go-libp2p', isWindows ? 'p2pd.exe' : 'p2pd')
1919
}
2020

2121
// js-libp2p defaults
2222
const jsDaemon = {
23-
defaultSock: getSockPath('/tmp/p2pd-js.sock'),
23+
defaultAddr: getMultiaddr('/tmp/p2pd-js.sock'),
2424
bin: path.join('libp2p-daemon', 'src', 'cli', 'bin.js')
2525
}
2626

2727
class Daemon {
2828
/**
2929
* @constructor
3030
* @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
3233
*/
33-
constructor (type, sock) {
34+
constructor (type, addr, port) {
3435
assert(type === 'go' || type === 'js', 'invalid type received. Should be "go" or "js"')
3536

3637
this._client = undefined
3738
this._type = type
3839
this._binPath = this._getBinPath(type)
39-
this._sock = sock && getSockPath(sock)
40+
this._addr = addr && getMultiaddr(addr, port)
4041

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
4344
}
4445
}
4546

@@ -80,7 +81,7 @@ class Daemon {
8081
await this._startDaemon()
8182

8283
// start client
83-
this._client = new Client(this._sock)
84+
this._client = new Client(this._addr)
8485

8586
await this._client.attach()
8687
}
@@ -92,7 +93,7 @@ class Daemon {
9293
*/
9394
_startDaemon () {
9495
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]
9697
const daemon = execa(this._binPath, options)
9798

9899
daemon.stdout.once('data', () => {
@@ -121,7 +122,12 @@ class Daemon {
121122
*/
122123
_cleanUnixSocket () {
123124
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) => {
125131
if (err) {
126132
return reject(err)
127133
}

src/utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
const os = require('os')
44
const path = require('path')
5+
const ma = require('multiaddr')
56
const isWindows = os.platform() === 'win32'
67

78
exports.isWindows = isWindows
89

910
exports.getSockPath = (sockPath) => isWindows
1011
? path.join('\\\\?\\pipe', sockPath)
1112
: path.resolve(os.tmpdir(), sockPath)
13+
14+
exports.getMultiaddr = (sockPath, port) => isWindows
15+
? ma(`/ip4/0.0.0.0/tcp/${port || 8080}`)
16+
: ma(`/unix${path.resolve(os.tmpdir(), sockPath)}`)

test/connect/go2go.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('connect', () => {
1717
this.timeout(20 * 1000)
1818

1919
goDaemon1 = new Daemon('go')
20-
goDaemon2 = new Daemon('go', '/tmp/p2pd-go2.sock')
20+
goDaemon2 = new Daemon('go', '/tmp/p2pd-go2.sock', 9090)
2121

2222
await Promise.all([
2323
goDaemon1.start(),

test/connect/js2js.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('connect', () => {
1717
this.timeout(20 * 1000)
1818

1919
jsDaemon1 = new Daemon('js')
20-
jsDaemon2 = new Daemon('js', '/tmp/p2pd-js2.sock')
20+
jsDaemon2 = new Daemon('js', '/tmp/p2pd-js2.sock', 9090)
2121

2222
await Promise.all([
2323
jsDaemon1.start(),

0 commit comments

Comments
 (0)