This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathfiles-mfs.js
98 lines (87 loc) · 2.88 KB
/
files-mfs.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
'use strict'
const mfs = require('ipfs-mfs/core')
const isPullStream = require('is-pull-stream')
const toPullStream = require('async-iterator-to-pull-stream')
const toReadableStream = require('async-iterator-to-stream')
const pullStreamToAsyncIterator = require('pull-stream-to-async-iterator')
const all = require('async-iterator-all')
const callbackify = require('callbackify')
const PassThrough = require('stream').PassThrough
const pull = require('pull-stream/pull')
const map = require('pull-stream/throughs/map')
const mapLsFile = (options = {}) => {
const long = options.long || options.l
return (file) => {
return {
hash: long ? file.cid.toBaseEncodedString(options.cidBase) : '',
name: file.name,
type: long ? file.type : 0,
size: long ? file.size || 0 : 0
}
}
}
module.exports = self => {
const methods = mfs({
ipld: self._ipld,
blocks: self._blockService,
datastore: self._repo.root,
repoOwner: self._options.repoOwner
})
return {
cp: callbackify.variadic(methods.cp),
flush: callbackify.variadic(methods.flush),
ls: callbackify.variadic(async (path, options = {}) => {
const files = await all(methods.ls(path, options))
return files.map(mapLsFile(options))
}),
lsReadableStream: (path, options = {}) => {
const stream = toReadableStream.obj(methods.ls(path, options))
const through = new PassThrough({
objectMode: true
})
stream.on('data', (file) => {
through.write(mapLsFile(options)(file))
})
stream.on('error', (err) => {
through.destroy(err)
})
stream.on('end', (file, enc, cb) => {
if (file) {
file = mapLsFile(options)(file)
}
through.end(file, enc, cb)
})
return through
},
lsPullStream: (path, options = {}) => {
return pull(
toPullStream.source(methods.ls(path, options)),
map(mapLsFile(options))
)
},
mkdir: callbackify.variadic(methods.mkdir),
mv: callbackify.variadic(methods.mv),
read: callbackify(async (path, options = {}) => {
return Buffer.concat(await all(methods.read(path, options)))
}),
readPullStream: (path, options = {}) => {
return toPullStream.source(methods.read(path, options))
},
readReadableStream: (path, options = {}) => {
return toReadableStream(methods.read(path, options))
},
rm: callbackify.variadic(methods.rm),
stat: callbackify(async (path, options = {}) => {
const stats = await methods.stat(path, options)
stats.hash = stats.cid.toBaseEncodedString(options && options.cidBase)
delete stats.cid
return stats
}),
write: callbackify.variadic(async (path, content, options = {}) => {
if (isPullStream.isSource(content)) {
content = pullStreamToAsyncIterator(content)
}
await methods.write(path, content, options)
})
}
}