|
1 | 1 | import IPFSFactory from 'ipfsd-ctl' |
2 | 2 | import logger from './logger' |
| 3 | +import { showConnFailureErrorMessage } from './errors' |
| 4 | +import { join } from 'path' |
| 5 | +import fs from 'fs-extra' |
| 6 | +import { spawnSync } from 'child_process' |
| 7 | +import findExecutable from 'ipfsd-ctl/src/utils/find-ipfs-executable' |
3 | 8 |
|
4 | | -export default async function createDaemon (opts) { |
5 | | - opts.type = opts.type || 'go' |
6 | | - opts.path = opts.path || '' |
7 | | - opts.flags = opts.flags || ['--migrate=true', '--routing=dhtclient'] |
8 | | - opts.keysize = opts.keysize || 4096 |
| 9 | +function repoFsck (path) { |
| 10 | + const exec = findExecutable('go', join(__dirname, '..')) |
| 11 | + spawnSync(exec, ['repo', 'fsck'], { |
| 12 | + env: { |
| 13 | + ...process.env, |
| 14 | + IPFS_PATH: path |
| 15 | + } |
| 16 | + }) |
| 17 | +} |
9 | 18 |
|
10 | | - if (opts.type !== 'go') { |
11 | | - throw new Error(`${opts.type} connection is not supported yet`) |
| 19 | +async function configure (ipfsd) { |
| 20 | + const cfgFile = join(ipfsd.repoPath, 'config') |
| 21 | + const cfg = await fs.readJSON(cfgFile) |
| 22 | + |
| 23 | + let origins = [] |
| 24 | + try { |
| 25 | + origins = cfg.API.HTTPHeaders['Access-Control-Allow-Origin'] |
| 26 | + } catch (e) { |
| 27 | + logger.warn(e) |
12 | 28 | } |
13 | 29 |
|
14 | | - const factory = IPFSFactory.create({ type: opts.type }) |
| 30 | + if (!Array.isArray(origins)) { |
| 31 | + origins = [] |
| 32 | + } |
| 33 | + |
| 34 | + if (!origins.includes('webui://-')) origins.push('webui://-') |
| 35 | + if (!origins.includes('https://webui.ipfs.io')) origins.push('https://webui.ipfs.io') |
| 36 | + |
| 37 | + cfg.API.HTTPHeaders['Access-Control-Allow-Origin'] = origins |
| 38 | + cfg.API.HTTPHeaders['Access-Control-Allow-Methods'] = ['PUT', 'GET', 'POST'] |
| 39 | + |
| 40 | + await fs.writeJSON(cfgFile, cfg) |
| 41 | +} |
| 42 | + |
| 43 | +async function spawn ({ type, path, keysize }) { |
| 44 | + const factory = IPFSFactory.create({ type: type }) |
15 | 45 |
|
16 | | - const ipfsd = await new Promise((resolve, reject) => { |
| 46 | + return new Promise((resolve, reject) => { |
17 | 47 | factory.spawn({ |
18 | 48 | disposable: false, |
19 | 49 | defaultAddrs: true, |
20 | | - repoPath: opts.path |
| 50 | + repoPath: path |
21 | 51 | }, (e, ipfsd) => { |
22 | 52 | if (e) return reject(e) |
23 | 53 | if (ipfsd.initialized) { |
24 | 54 | return resolve(ipfsd) |
25 | 55 | } |
26 | 56 |
|
27 | 57 | ipfsd.init({ |
28 | | - directory: opts.path, |
29 | | - keysize: opts.keysize |
| 58 | + directory: path, |
| 59 | + keysize: keysize |
30 | 60 | }, e => { |
31 | 61 | if (e) return reject(e) |
32 | 62 | resolve(ipfsd) |
33 | 63 | }) |
34 | 64 | }) |
35 | 65 | }) |
| 66 | +} |
36 | 67 |
|
37 | | - if (!ipfsd.started) { |
38 | | - await new Promise((resolve, reject) => { |
39 | | - ipfsd.start(opts.flags, err => { |
40 | | - if (err) { |
41 | | - return reject(err) |
42 | | - } |
| 68 | +async function start (ipfsd, { flags }) { |
| 69 | + await new Promise((resolve, reject) => { |
| 70 | + ipfsd.start(flags, err => { |
| 71 | + if (err) { |
| 72 | + return reject(err) |
| 73 | + } |
43 | 74 |
|
44 | | - resolve() |
45 | | - }) |
| 75 | + resolve() |
46 | 76 | }) |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +export default async function (opts) { |
| 81 | + const ipfsd = await spawn(opts) |
| 82 | + await configure(ipfsd) |
| 83 | + |
| 84 | + if (!ipfsd.started) { |
| 85 | + await start(ipfsd, opts) |
47 | 86 | } |
48 | 87 |
|
49 | | - let origins = [] |
50 | 88 | try { |
51 | | - origins = await ipfsd.api.config.get('API.HTTPHeaders.Access-Control-Allow-Origin') |
| 89 | + await ipfsd.api.id() |
52 | 90 | } catch (e) { |
53 | | - logger.warn(e) |
54 | | - } |
| 91 | + if (!e.message.includes('ECONNREFUSED')) { |
| 92 | + throw e |
| 93 | + } |
55 | 94 |
|
56 | | - if (!origins.includes('webui://-')) origins.push('webui://-') |
57 | | - if (!origins.includes('https://webui.ipfs.io')) origins.push('https://webui.ipfs.io') |
| 95 | + if (!showConnFailureErrorMessage(ipfsd.repoPath, ipfsd.apiAddr)) { |
| 96 | + throw new Error('exit') |
| 97 | + } |
58 | 98 |
|
59 | | - await ipfsd.api.config.set('API.HTTPHeaders.Access-Control-Allow-Origin', origins) |
60 | | - await ipfsd.api.config.set('API.HTTPHeaders.Access-Control-Allow-Methods', ['PUT', 'GET', 'POST']) |
| 99 | + repoFsck(ipfsd.repoPath) |
| 100 | + await start(ipfsd, opts) |
| 101 | + } |
61 | 102 |
|
62 | 103 | return ipfsd |
63 | 104 | } |
0 commit comments