This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.js
79 lines (67 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict'
const PeerId = require('peer-id')
const IPFSAPI = require('ipfs-api')
const clean = require('../clean')
const HttpApi = require('../../../src/http')
const series = require('async/series')
const eachSeries = require('async/eachSeries')
const defaultConfig = require('./default-config.json')
const os = require('os')
const hat = require('hat')
class Factory {
constructor () {
this.daemonsSpawned = []
}
/* yields a new started node */
spawnNode (repoPath, suppliedConfig, callback) {
if (typeof repoPath === 'function') {
callback = repoPath
repoPath = undefined
}
if (typeof suppliedConfig === 'function') {
callback = suppliedConfig
suppliedConfig = {}
}
repoPath = repoPath || os.tmpdir() + '/ipfs-' + hat()
let daemon
let ctl
let config
series([
(cb) => {
// prepare config for node
config = Object.assign({}, defaultConfig, suppliedConfig)
PeerId.create({ bits: 1024 }, (err, id) => {
if (err) { return cb(err) }
const peerId = id.toJSON()
config.Identity.PeerID = peerId.id
config.Identity.PrivKey = peerId.privKey
cb()
})
},
(cb) => {
daemon = new HttpApi(repoPath, config, {enablePubsubExperiment: true})
daemon.repoPath = repoPath
this.daemonsSpawned.push(daemon)
daemon.start(true, cb)
},
(cb) => {
ctl = IPFSAPI(daemon.apiMultiaddr)
ctl.repoPath = repoPath
ctl.apiMultiaddr = daemon.apiMultiaddr
cb()
}
], (err) => callback(err, ctl))
}
dismantle (callback) {
eachSeries(this.daemonsSpawned, (d, cb) => {
d.stop((err) => {
clean(d.repoPath)
if (err) {
console.error('error stopping', err)
}
cb(err)
})
}, callback)
}
}
module.exports = Factory