1 | 'use strict'
|
2 | const merge = require('merge-options').bind({ ignoreUndefined: true })
|
3 | const kyOriginal = require('ky-universal').default
|
4 | const { tmpDir } = require('./utils')
|
5 | const { isNode } = require('ipfs-utils/src/env')
|
6 | const ControllerDaemon = require('./ipfsd-daemon')
|
7 | const ControllerRemote = require('./ipfsd-client')
|
8 | const ControllerProc = require('./ipfsd-in-proc')
|
9 | const testsConfig = require('./config')
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const ky = kyOriginal.extend({ timeout: false })
|
16 | const defaults = {
|
17 | remote: !isNode,
|
18 | endpoint: 'http://localhost:43134',
|
19 | disposable: true,
|
20 | test: false,
|
21 | type: 'go',
|
22 | env: {},
|
23 | args: [],
|
24 | ipfsOptions: {},
|
25 | forceKill: true,
|
26 | forceKillTimeout: 5000
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | class Factory {
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 | constructor (options = {}, overrides = {}) {
|
39 |
|
40 | this.opts = merge(defaults, options)
|
41 |
|
42 |
|
43 | this.overrides = merge({
|
44 | js: merge(this.opts, { type: 'js' }),
|
45 | go: merge(this.opts, { type: 'go' }),
|
46 | proc: merge(this.opts, { type: 'proc' })
|
47 | }, overrides)
|
48 |
|
49 |
|
50 | this.controllers = []
|
51 | }
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | async tmpDir (options) {
|
63 | options = merge(this.opts, options)
|
64 | if (options.remote) {
|
65 | const res = await ky.get(
|
66 | `${options.endpoint}/util/tmp-dir`,
|
67 | { searchParams: { type: options.type } }
|
68 | ).json()
|
69 |
|
70 | return res.tmpDir
|
71 | }
|
72 |
|
73 | return Promise.resolve(tmpDir(options.type))
|
74 | }
|
75 |
|
76 | async _spawnRemote (options) {
|
77 | const opts = {
|
78 | json: {
|
79 | ...options,
|
80 |
|
81 | remote: false
|
82 | }
|
83 | }
|
84 |
|
85 | const res = await ky.post(
|
86 | `${options.endpoint}/spawn`,
|
87 | opts
|
88 | ).json()
|
89 | return new ControllerRemote(
|
90 | options.endpoint,
|
91 | res,
|
92 | options
|
93 | )
|
94 | }
|
95 |
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 | async spawn (options = { }) {
|
102 | const type = options.type || this.opts.type
|
103 | const opts = merge(
|
104 | this.overrides[type],
|
105 | options
|
106 | )
|
107 |
|
108 |
|
109 | const ipfsOptions = merge(
|
110 | {
|
111 | start: false,
|
112 | init: false
|
113 | },
|
114 | opts.test ? {
|
115 | config: testsConfig(opts),
|
116 | preload: { enabled: false }
|
117 | } : {},
|
118 | opts.ipfsOptions
|
119 | )
|
120 |
|
121 | let ctl
|
122 | if (opts.type === 'proc') {
|
123 |
|
124 | ctl = new ControllerProc({ ...opts, ipfsOptions })
|
125 | } else if (opts.remote) {
|
126 |
|
127 | ctl = await this._spawnRemote({ ...opts, ipfsOptions })
|
128 | } else {
|
129 |
|
130 | ctl = new ControllerDaemon({ ...opts, ipfsOptions })
|
131 | }
|
132 |
|
133 |
|
134 | this.controllers.push(ctl)
|
135 |
|
136 |
|
137 | if (opts.disposable && (!options.ipfsOptions || (options.ipfsOptions && options.ipfsOptions.init !== false))) {
|
138 | await ctl.init(ipfsOptions.init)
|
139 | }
|
140 | if (opts.disposable && (!options.ipfsOptions || (options.ipfsOptions && options.ipfsOptions.start !== false))) {
|
141 | await ctl.start()
|
142 | }
|
143 |
|
144 | return ctl
|
145 | }
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 | async clean () {
|
152 | await Promise.all(this.controllers.map(n => n.stop()))
|
153 | this.controllers = []
|
154 | return this
|
155 | }
|
156 | }
|
157 |
|
158 | module.exports = Factory
|