forked from hyperspace-org/hyperdrive-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
181 lines (161 loc) · 5.24 KB
/
client.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const p = require('path')
const isOptions = require('is-options')
const ddrive = require('ddrive')
const DHubClient = require('@dhub/client')
const { loadConfig } = require('./lib/config')
const importDirectory = require('./lib/cli/import')
const exportDrive = require('./lib/cli/export')
module.exports = class DDriveServiceClient {
constructor (opts = {}) {
this.key = opts.key
this.mnt = opts.mnt
this.dhubClient = opts.client || new DHubClient(opts)
this._store = null
this._rootDrive = null
}
async ready () {
if (this._rootDrive) return
if (!this.key || !this.mnt) {
const config = await loadConfig()
if (config.rootDriveKey) this.key = Buffer.from(config.rootDriveKey, 'hex')
if (config.mnt) this.mnt = config.mnt
}
if (!this.key) throw new Error('DDriveServiceClient was not given a root drive key.')
if (!this.mnt) throw new Error('DDriveServiceClient was not given a root mountpoint.')
this._store = this.dhubClient.basestore()
this._rootDrive = await this._createDrive({ key: this.key })
}
_resolvePath (path) {
const fullPath = p.resolve(path)
if (!fullPath.startsWith(this.mnt)) throw new Error('Can only create a mount within the root drive.')
return fullPath.slice(this.mnt.length)
}
async _createDrive (opts = {}) {
var drive = ddrive(this._store, opts && opts.key, {
...opts,
extension: false
}).promises
await drive.ready()
return drive
}
async mount (path, opts = {}) {
await this.ready()
const drive = await this._createDrive({ key: opts.key })
await this._rootDrive.mount(this._resolvePath(path), drive.key, { ...opts, key: null })
return drive
}
async unmount (path) {
await this.ready()
return this._rootDrive.unmount(this._resolvePath(path))
}
async info (path) {
await this.ready()
return new Promise((resolve, reject) => {
const noopFilePath = '__does_not_exist'
const noopPath = p.join(this._resolvePath(path), noopFilePath)
this._rootDrive.drive.stat(noopPath, { trie: true }, (err, stat, trie, _, __, mountPath) => {
if (err && err.errno !== 2) return reject(err)
if (err && !trie) return resolve(null)
this.dhubClient.network.status(trie.feed.discoveryKey, (err, networkConfig) => {
if (err) return reject(err)
return resolve({
key: trie.key,
discoveryKey: trie.feed.discoveryKey,
writable: trie.feed.writable,
mountPath: mountPath.slice(0, mountPath.length - noopFilePath.length),
announce: !!(networkConfig && networkConfig.announce),
lookup: !!(networkConfig && networkConfig.lookup)
})
})
})
})
}
async _driveFromPath (path, opts = {}) {
var driveKey = null
if (path) {
const info = await this.info(path)
if (!info) return null
driveKey = info.key
} else {
driveKey = opts.key
}
return this._createDrive({ key: driveKey })
}
async stats (path, opts = {}) {
if (isOptions(path)) {
opts = path
path = null
}
const drive = await this._driveFromPath(path, opts)
const allMounts = await drive.getAllMounts({ memory: true })
const network = {}
for (const [mountpoint, { metadata, content }] of allMounts) {
network[mountpoint] = {
metadata: baseStats(metadata),
content: baseStats(content)
}
}
const storageObj = {}
if (opts.storage) {
const stats = await drive.stats('/', opts)
for (const [k, v] of stats) {
storageObj[k] = v
}
}
await drive.close()
return {
storage: storageObj,
network
}
function baseStats (base) {
return {
key: base.key.toString('hex'),
discoveryKey: base.discoveryKey.toString('hex'),
writable: base.writable,
length: base.length,
byteLength: base.byteLength,
peers: base.peers.map(mapPeer)
}
}
function mapPeer (p) {
return {
...p,
remotePublicKey: p.remotePublicKey.toString('hex')
}
}
}
async seed (path, opts = {}) {
if (isOptions(path)) {
opts = path
path = null
}
var discoveryKey = opts.discoveryKey
if (discoveryKey && !Buffer.isBuffer(discoveryKey)) discoveryKey = Buffer.from(discoveryKey, 'hex')
if (!discoveryKey) {
const drive = await this._driveFromPath(path, opts)
discoveryKey = drive.discoveryKey
await drive.close()
}
return this.dhubClient.network.configure(discoveryKey, { ...opts, announce: true, lookup: true })
}
async unseed (path, opts = {}) {
if (isOptions(path)) {
opts = path
path = null
}
var discoveryKey = opts.discoveryKey
if (discoveryKey && !Buffer.isBuffer(discoveryKey)) discoveryKey = Buffer.from(discoveryKey, 'hex')
if (!discoveryKey) {
const drive = await this._driveFromPath(path, opts)
discoveryKey = drive.discoveryKey
await drive.close()
}
return this.dhubClient.network.configure(discoveryKey, { ...opts, announce: false, lookup: false })
}
async import (key, dir, opts) {
return importDirectory(this, key, dir, opts)
}
async export (key, dir, opts) {
return exportDrive(this, key, dir, opts)
}
}