forked from hyperspace-org/hyperdrive-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (114 loc) · 3.44 KB
/
index.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
const os = require('os')
const p = require('path')
const { NanoresourcePromise: Nanoresource } = require('nanoresource-promise/emitter')
const DHubClient = require('@dhub/client')
const ddrive = require('ddrive')
const applyHeuristics = require('@ddrive/network-heuristics')
const maybe = require('call-me-maybe')
const pino = require('pino')
const { loadConfig, saveConfig } = require('./lib/config')
const NetworkHandlers = require('./lib/network')
const DEFAULT_MNT = p.join(os.homedir(), 'DDrive')
module.exports = class DDriveService extends Nanoresource {
constructor (opts = {}) {
super()
this.mnt = opts.mnt || DEFAULT_MNT
this.key = opts.key
this.log = opts.log || pino({
name: 'dhub-fuse',
level: opts.logLevel || 'info'
}, pino.destination(2))
this.remember = (opts.remember === undefined) ? true : !!opts.remember
this.disableFuse = !!opts.disableFuse
this._client = opts.client || new DHubClient(opts)
this._store = null
this._rootDrive = null
}
async _open () {
await this._client.ready()
this._store = this._client.basestore()
if (this.remember) {
const config = await loadConfig()
if (!this.key && config.rootDriveKey) this.key = Buffer.from(config.rootDriveKey, 'hex')
if (!this.mnt && config.mnt) this.mnt = config.mnt
}
if (this._fuseEnabled()) await this._mount()
}
async _close () {
await this._unmount()
await this._client.close()
}
_fuseEnabled () {
if (this.disableFuse) return false
var dwebfuse = null
try {
dwebfuse = require('@ddrive/fuse')
} catch (err) {
notAvailable()
return false
}
return new Promise(resolve => {
dwebfuse.isConfigured((err, configured) => {
if (err) {
notAvailable()
return resolve(false)
}
if (!configured) {
notConfigured()
return resolve(false)
}
return resolve(true)
})
})
function notConfigured () {
console.warn('FUSE is not configured. Run `ddrive fuse-setup`.')
}
function notAvailable () {
console.warn('FUSE is not available on your platform.')
}
}
async _createDrive (opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = null
}
var drive = ddrive(this._store, opts && opts.key, {
...opts,
extension: false
}).promises
return maybe(cb, (async () => {
await drive.ready()
await applyHeuristics(drive, this._client.network)
return drive
})())
}
async _mount () {
await this._unmount()
const { DDriveFuse } = require('@ddrive/fuse')
const drive = await this._createDrive({ key: this.key })
const fuseLogger = this.log.child({ component: 'fuse' })
const fuse = new DDriveFuse(drive.drive, this.mnt, {
force: true,
displayFolder: true,
log: fuseLogger.trace.bind(this.log)
})
const handlers = fuse.getBaseHandlers()
const networkHandlers = new NetworkHandlers(this._createDrive.bind(this), handlers, fuseLogger)
await fuse.mount(networkHandlers.generateHandlers())
this._rootDrive = drive
this._rootFuse = fuse
if (this.remember) {
const config = {
rootDriveKey: this.key.toString('hex'),
mnt: this.mnt
}
await saveConfig(config)
}
}
async _unmount () {
if (!this._rootFuse) return
await this._rootFuse.unmount()
this._rootDrive = null
this._rootFuse = null
}
}