-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
110 lines (97 loc) · 2.47 KB
/
Copy pathindex.js
File metadata and controls
110 lines (97 loc) · 2.47 KB
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
const Fuse = require('fuse-bindings')
const debug = require('debug')('ipfs-fuse:index')
const mkdirp = require('mkdirp')
const Async = require('async')
const explain = require('explain-error')
const createIpfsFuse = require('./ipfs-fuse')
exports.mount = (mountPath, opts, cb) => {
if (!cb) {
cb = opts
opts = {}
}
opts = opts || {}
cb = cb || (() => {})
Async.auto({
path (cb) {
mkdirp(mountPath, (err) => {
if (err) {
err = explain(err, 'Failed to create mount point')
debug(err)
return cb(err)
}
cb()
})
},
ipfs (cb) {
if (opts.daemon) return cb(null, require('ipfs-api')(opts.ipfs))
const Ipfs = require('ipfs')
const ipfs = new Ipfs(opts.ipfs)
const onReady = () => {
ipfs.removeListener('ready', onReady)
ipfs.removeListener('error', onError)
cb(null, ipfs)
}
const onError = err => {
ipfs.removeListener('ready', onReady)
ipfs.removeListener('error', onError)
cb(err)
}
ipfs.on('ready', onReady).on('error', onError)
},
id: ['ipfs', (res, cb) => {
res.ipfs.id((err, id) => {
if (err) {
err = explain(err, 'Failed to connect to IPFS node')
debug(err)
return cb(err)
}
debug(id)
cb(null, id)
})
}],
mount: ['path', 'ipfs', 'id', (res, cb) => {
Fuse.mount(mountPath, createIpfsFuse(res.ipfs, res.id), opts.fuse, (err) => {
if (err) {
err = explain(err, 'Failed to mount IPFS FUSE volume')
debug(err)
return cb(err)
}
cb()
})
}]
}, (err, res) => {
if (err) {
debug(err)
return cb(err)
}
cb(null, {
unmount (cb) {
cb = cb || (() => {})
Async.parallel([
cb => {
Fuse.unmount(mountPath, err => {
if (err) {
err = explain(err, 'Failed to unmount IPFS FUSE volume')
debug(err)
return cb(err)
}
cb()
})
},
cb => {
// Do not stop a daemon...
if (opts.daemon) return cb()
res.ipfs.stop(err => {
if (err) {
err = explain(err, 'Failed to stop IPFS node')
debug(err)
return cb(err)
}
cb()
})
}
], cb)
}
})
})
}