This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSharedFS.js
282 lines (246 loc) · 8.93 KB
/
SharedFS.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
'use strict'
const AccessControl = require('./AccessControl')
const EventEmitter = require('events').EventEmitter
const Buffer = require('safe-buffer').Buffer
const { default: PQueue } = require('p-queue')
const normaliseInput = require('ipfs-core-utils/src/files/normalise-input')
const treeBuilder = require('./tree-builder')
const util = require('./util')
const { FS } = require('@tabcat/orbit-db-fsstore')
const { content, read, ls, pathName } = FS
const errors = {
...FS.errors,
notStarted: () => new Error('sharedfs was not started')
}
const writeReqs = (self) => {
if (!self.running) throw errors.notStarted()
if (!self.access.hasRead) throw new Error('missing read access')
if (!self.access.hasWrite) throw new Error('missing write access')
}
const storeTypes = { lite: 0, full: 1, archive: 2 }
const defaultOptions = {
autoStart: true,
load: true,
onStop: function () {},
storeType: storeTypes.lite,
Crypter: null
}
class SharedFS {
constructor (db, ipfs, options = {}) {
this._db = db
this._ipfs = ipfs
this.options = { ...defaultOptions, ...options }
this.events = new EventEmitter()
this.address = this._db.address
this.fs = {}
this.fs.root = this._db.root
this.fs.joinPath = this._db.joinPath
this.fs.pathName = this._db.pathName
this.fs.exists = this._db.exists
this.fs.content = this._db.content
this.fs.read = this._db.read
this.fs.tree = this._db.tree
this.fs.ls = this._db.ls
const statsFirst = (p) => [...p.slice(-2), ...p.slice(0, -2)]
this._dbProgress = {
load: (...p) => this.events.emit('db.load.progress', ...statsFirst(p)),
replicate: (...p) => this.events.emit('db.replicate.progress', ...statsFirst(p))
}
this._updateQueue = new PQueue({ concurrency: 1 })
this._onDbUpdate = () => {
this.events.emit('updated')
this._updateQueue.size === 0 &&
this._updateQueue.add(() => this._computeCid())
}
this._emptyDirCid = null
this._emptyFileCid = null
this._CID = null
this.access = null
this.running = null
}
get identity () { return this._db.identity }
get encrypted () { return this.access.crypted }
get Crypter () { return this.access.Crypter }
static async create (fsstore, ipfs, options = {}) {
const sharedfs = new SharedFS(fsstore, ipfs, options)
if (sharedfs.options.autoStart) await sharedfs.start()
return sharedfs
}
async start () {
if (this.running !== null) { return }
this._emptyDirCid = await this._ipfs.object.put({ Data: Buffer.from([8, 1]) })
this._emptyFileCid = await this._ipfs.object.put({ Data: Buffer.from([8, 2]) })
this._CID = this._emptyFileCid.constructor
this.access = await AccessControl.create(this._db, this.options)
this.access.events.on('encrypted', this._onDbUpdate)
this._db.events.on('load.progress', this._dbProgress.load)
this._db.events.on('replicate.progress', this._dbProgress.replicate)
this._db.events.on('replicated', this._onDbUpdate)
this.events.on('upload', this._onDbUpdate)
this.events.on('mkdir', this._onDbUpdate)
this.events.on('mkfile', this._onDbUpdate)
this.events.on('write', this._onDbUpdate)
this.events.on('remove', this._onDbUpdate)
this.events.on('move', this._onDbUpdate)
this.events.on('copy', this._onDbUpdate)
if (this.options.load) await this._db.load()
if (this.access.hasRead) this._onDbUpdate()
this.running = true
this.events.emit('start')
}
async stop ({ drop } = {}) {
if (this.running !== true) { return }
await this.options.onStop()
this.access.events.removeListener('encrypted', this._onDbUpdate)
this._db.events.removeListener('load.progress', this._dbProgress.load)
this._db.events.removeListener('replicate.progress', this._dbProgress.replicate)
this._db.events.removeListener('replicated', this._onDbUpdate)
this.events.removeListener('upload', this._onDbUpdate)
this.events.removeListener('mkdir', this._onDbUpdate)
this.events.removeListener('mkfile', this._onDbUpdate)
this.events.removeListener('write', this._onDbUpdate)
this.events.removeListener('remove', this._onDbUpdate)
this.events.removeListener('move', this._onDbUpdate)
this.events.removeListener('copy', this._onDbUpdate)
await Promise.all([this.access.stop({ drop }), this._updateQueue.onIdle()])
drop ? await this._db.drop() : await this._db.close()
this.running = false
this.events.emit('stop')
}
async upload (path, source, options = {}) {
writeReqs(this)
const prefix = (path) => path.slice(0, Math.max(path.lastIndexOf('/'), 0))
const name = (path) => path.slice(path.lastIndexOf('/') + 1)
const ipfsAddOptions = { ...options, ...util.ipfsAddConfig }
try {
const tree = await treeBuilder(normaliseInput(source))
const batch = this._db.batch()
for (const item of tree.traverse()) {
item.path = util.removeSlash(item.path)
const fsPath = `${path}${item.path && `/${item.path}`}`
if (item.content) {
if (!this.fs.exists(fsPath)) {
batch.mk(prefix(fsPath), name(fsPath))
}
const { mode, mtime, content } = item
const enc = this.encrypted && await util.encryptContent(this.Crypter, content)
const data = this.encrypted ? enc.cipherbytes : content
const { cid } = await this._ipfs.add(data, ipfsAddOptions)
if (util.readCid(this.fs.read(fsPath)) !== cid.toString()) {
const decrypt = this.encrypted ? { key: enc.rawKey, iv: enc.iv } : {}
batch.write(fsPath, { cid: cid.toString(), ...decrypt, ...mtime ? { mtime } : {} })
}
} else {
if (!this.fs.exists(fsPath)) {
batch.mkdir(prefix(fsPath), name(fsPath))
}
}
}
await batch.execute()
this.events.emit('upload')
} catch (e) {
console.error(e)
console.error(new Error('sharedfs.upload failed'))
console.error('path:'); console.error(path)
console.error('source:'); console.error(source)
throw e
}
}
async mkdir (path, name) {
writeReqs(this)
await this._db.mkdir(path, name)
this.events.emit('mkdir')
}
async mkfile (path, name) {
writeReqs(this)
await this._db.mk(path, name)
this.events.emit('mkfile')
}
async write (path, cid, options = {}) {
writeReqs(this)
if (!util.validCid(this._CID, cid)) throw new Error('invalid cid')
await this._db.write(path, { cid: cid.toString(), key: options.key })
this.events.emit('write')
}
async read (path) {
if (!this.fs.exists(path)) throw errors.pathExistNo(path)
return this._computeCid(path)
}
cat (path, options = {}) {
if (!this.fs.exists(path)) throw errors.pathExistNo(path)
if (this.fs.content(path) !== 'file') throw errors.pathFileNo(path)
const file = this.fs.read(path)
const key = file && file.key
const iv = file && file.iv
return {
data: () => util.catCid(
this._ipfs,
util.readCid(file),
{ Crypter: this.Crypter, key, iv, handleUpdate: options.handleUpdate }
)
}
}
async remove (path) {
writeReqs(this)
this.fs.content(path) === 'dir'
? await this._db.rmdir(path)
: await this._db.rm(path)
this.events.emit('remove')
}
async move (path, dest, name) {
writeReqs(this)
this.fs.content(path) === 'dir'
? await this._db.mvdir(path, dest, name)
: await this._db.mv(path, dest, name)
this.events.emit('move')
}
async copy (path, dest, name) {
writeReqs(this)
this.fs.content(path) === 'dir'
? await this._db.cpdir(path, dest, name)
: await this._db.cp(path, dest, name)
this.events.emit('copy')
}
async _computeCid (path = this.fs.root) {
if (!this.access.hasRead) {
console.warn('_computeCid skipped, no read access')
return
}
const fileCid = (cid) => {
try {
return new this._CID(cid)
} catch (e) {
return this._emptyFileCid
}
}
const pathCid = async (fs, path) => {
if (content(fs, path) === 'file') {
return fileCid(util.readCid(read(fs, path)))
}
const dirLinks = await Promise.all(
ls(fs, path)
.map(async (p) => {
const cid = await pathCid(fs, p)
const { size } = await this._ipfs.object.get(cid)
return { name: pathName(p), size, cid }
})
)
if (dirLinks.length === 0) return this._emptyDirCid
// Data buffer says unixFs and directory
return this._ipfs.object.put({ Data: Buffer.from([8, 1]), Links: dirLinks })
}
try {
const fs = this._db.index
const cid = await pathCid(fs, path)
if (path === this.fs.root && this.options.storeType > storeTypes.lite) {
ipfs.get(cid)
}
return cid
} catch (e) {
console.error(e)
console.error(new Error('sharedfs._computeCid failed'))
console.error('path:'); console.error(path)
}
}
}
module.exports = SharedFS