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
/
interface-common-factory.js
134 lines (114 loc) · 3.29 KB
/
interface-common-factory.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* eslint-env mocha */
'use strict'
const each = require('async/each')
const IPFSFactory = require('ipfsd-ctl')
const ipfsClient = require('ipfs-http-client')
const callbackify = require('callbackify')
const mergeOptions = require('merge-options')
const IPFS = require('../../src')
const DEFAULT_FACTORY_OPTIONS = {
type: 'proc',
exec: IPFS
}
function createFactory (options) {
options = options || {}
options.factoryOptions = options.factoryOptions || { ...DEFAULT_FACTORY_OPTIONS }
options.spawnOptions = mergeOptions({
initOptions: { bits: 512 },
config: {
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: false
},
webRTCStar: {
Enabled: false
}
}
},
preload: { enabled: false }
}, options.spawnOptions)
if (options.factoryOptions.type !== 'proc') {
options.factoryOptions.IpfsClient = options.factoryOptions.IpfsClient || ipfsClient
}
const ipfsFactory = IPFSFactory.create(options.factoryOptions)
const callbackifiedSpawn = callbackify.variadic(ipfsFactory.spawn.bind(ipfsFactory))
return function createCommon () {
const nodes = []
let setup, teardown
if (options.createSetup) {
setup = options.createSetup({ ipfsFactory, nodes }, options)
} else {
setup = (callback) => {
callback(null, {
spawnNode (cb) {
callbackifiedSpawn(options.spawnOptions, (err, _ipfsd) => {
if (err) {
return cb(err)
}
nodes.push(_ipfsd)
cb(null, _ipfsd.api)
})
}
})
}
}
if (options.createTeardown) {
teardown = options.createTeardown({ ipfsFactory, nodes }, options)
} else {
teardown = callback => each(nodes, (node, cb) => callbackify.variadic(node.stop.bind(node))(cb), callback)
}
return { setup, teardown }
}
}
function createAsync (options = {}) {
return () => {
const nodes = []
const setup = async (setupOptions = {}) => {
options.factoryOptions = mergeOptions(
options.factoryOptions ? {} : { ...DEFAULT_FACTORY_OPTIONS },
setupOptions.factoryOptions,
options.factoryOptions
)
// When not an in proc daemon use the http-client js-ipfs depends on, not the one from ipfsd-ctl
if (options.factoryOptions.type !== 'proc') {
options.factoryOptions.IpfsClient = options.factoryOptions.IpfsClient || ipfsClient
}
const ipfsFactory = IPFSFactory.create(options.factoryOptions)
options.spawnOptions = mergeOptions(
{
config: {
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: false
},
webRTCStar: {
Enabled: false
}
}
},
preload: { enabled: false }
},
setupOptions.spawnOptions,
options.spawnOptions
)
const node = await ipfsFactory.spawn(options.spawnOptions)
nodes.push(node)
const id = await node.api.id()
node.api.peerId = id
return node.api
}
const teardown = () => {
return Promise.all(nodes.map(n => n.stop()))
}
return {
setup,
teardown
}
}
}
module.exports = {
createAsync,
create: createFactory
}