-
Notifications
You must be signed in to change notification settings - Fork 66
/
ipfs-protocol.js
69 lines (56 loc) · 1.8 KB
/
ipfs-protocol.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
import fetchToHandler from './fetch-to-handler.js'
import path from 'node:path'
import * as fs from 'node:fs/promises'
export default async function createHandler (ipfsOptions, session) {
return fetchToHandler(async () => {
const { default: makeFetch } = await import('js-ipfs-fetch')
const ipfsHttpModule = await import('ipfs-http-client')
const Ctl = await import('ipfsd-ctl')
const { default: GoIPFS } = await import('go-ipfs')
const ipfsBin = GoIPFS
.path()
.replace(`.asar${path.sep}`, `.asar.unpacked${path.sep}`)
const ipfsdOpts = {
ipfsOptions,
type: 'go',
disposable: false,
test: false,
remote: false,
ipfsHttpModule,
ipfsBin
}
let ipfsd = await Ctl.createController(ipfsdOpts)
await ipfsd.init({ ipfsOptions })
const version = await ipfsd.version()
console.log(`IPFS Version: ${version}\nIPFS bin path: ${ipfsBin}`)
try {
await ipfsd.start()
await ipfsd.api.id()
} catch (e) {
console.log('IPFS Unable to boot daemon', e.message)
const { repo } = ipfsOptions
const lockFile = path.join(repo, 'repo.lock')
const apiFile = path.join(repo, 'api')
try {
await Promise.all([
fs.rm(lockFile),
fs.rm(apiFile)
])
ipfsd = await Ctl.createController(ipfsdOpts)
await ipfsd.start()
await ipfsd.api.id()
} catch (cause) {
const message = `Unable to start daemon due to extra lockfile. Please clear your ipfs folder at ${repo} and try again.`
throw new Error(message, { cause })
}
}
console.log('IPFS ID:', await ipfsd.api.id())
const fetch = await makeFetch({
ipfs: ipfsd.api
})
fetch.close = async () => {
return ipfsd.stop()
}
return fetch
}, session)
}