Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 607623d

Browse files
achingbrainAlan Shaw
authored and
Alan Shaw
committed
test: most tests passing
1 parent 812ec2e commit 607623d

File tree

7 files changed

+209
-63
lines changed

7 files changed

+209
-63
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@
7272
"execa": "^1.0.0",
7373
"form-data": "^2.3.3",
7474
"hat": "0.0.3",
75+
"interface-ipfs-core": "~0.102.0",
7576
"ipfsd-ctl": "~0.42.0",
7677
"libp2p-websocket-star": "~0.10.2",
7778
"ncp": "^2.0.0",
7879
"qs": "^6.5.2",
7980
"rimraf": "^2.6.2",
80-
"interface-ipfs-core": "~0.102.0",
8181
"sinon": "^7.3.1",
8282
"stream-to-promise": "^2.2.0"
8383
},
@@ -86,7 +86,9 @@
8686
"@hapi/hapi": "^18.3.1",
8787
"@hapi/joi": "^15.0.1",
8888
"async": "^2.6.1",
89+
"async-iterator-all": "0.0.2",
8990
"async-iterator-to-pull-stream": "^1.1.0",
91+
"async-iterator-to-stream": "^1.1.0",
9092
"base32.js": "~0.1.0",
9193
"bignumber.js": "^8.0.2",
9294
"binary-querystring": "~0.1.2",

src/core/components/files-mfs.js

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,75 @@
11
'use strict'
22

33
const mfs = require('ipfs-mfs/core')
4+
const toPullStream = require('async-iterator-to-pull-stream')
5+
const toReadableStream = require('async-iterator-to-stream')
6+
const all = require('async-iterator-all')
7+
const callbackify = require('util').callbackify
8+
const PassThrough = require('stream').PassThrough
9+
const pull = require('pull-stream/pull')
10+
const map = require('pull-stream/throughs/map')
411

5-
module.exports = self => mfs({
6-
ipld: self._ipld,
7-
repo: self._repo,
8-
repoOwner: self._options.repoOwner
9-
})
12+
const mapLsFile = (options = {}) => {
13+
const long = options.long || options.l
14+
15+
return (file) => {
16+
console.info(file)
17+
18+
return {
19+
hash: long ? file.cid.toBaseEncodedString(options.cidBase) : '',
20+
name: file.name,
21+
type: long ? file.type : 0,
22+
size: long ? file.size || 0 : 0
23+
}
24+
}
25+
}
26+
27+
module.exports = self => {
28+
const methods = mfs({
29+
ipld: self._ipld,
30+
blocks: self._blockService,
31+
datastore: self._repo.root,
32+
repoOwner: self._options.repoOwner
33+
})
34+
35+
return {
36+
cp: callbackify(methods.cp),
37+
flush: callbackify(methods.flush),
38+
ls: callbackify(async (path, options) => {
39+
const files = await all(methods.ls(path, options))
40+
41+
return files.map(mapLsFile(options))
42+
}),
43+
lsReadableStream: (path, options) => {
44+
const stream = toReadableStream.obj(methods.ls(path, options))
45+
const through = new PassThrough({
46+
objectMode: true
47+
})
48+
stream.on('data', (file) => through.emit('data', mapLsFile(options)(file)))
49+
stream.on('error', through.emit.bind(through, 'error'))
50+
stream.on('end', through.emit.bind(through, 'end'))
51+
52+
return through
53+
},
54+
lsPullStream: (path, options) => {
55+
return pull(
56+
toPullStream.source(methods.ls(path, options)),
57+
map(mapLsFile(options))
58+
)
59+
},
60+
mkdir: callbackify(methods.mkdir),
61+
mv: callbackify(methods.mv),
62+
read: callbackify(async (path, options) => {
63+
return Buffer.concat(await all(methods.read(path, options)))
64+
}),
65+
readPullStream: (path, options) => {
66+
return toReadableStream(methods.read(path, options))
67+
},
68+
readReadableStream: (path, options) => {
69+
return toPullStream.source(methods.read(path, options))
70+
},
71+
rm: callbackify(methods.rm),
72+
stat: callbackify(methods.stat),
73+
write: callbackify(methods.write)
74+
}
75+
}

src/core/components/files-regular/add-pull-stream.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const importer = require('ipfs-unixfs-importer')
4+
<<<<<<< HEAD
45
const kindOf = require('kind-of')
56
const CID = require('cids')
67
const pullValues = require('pull-stream/sources/values')
@@ -12,6 +13,15 @@ const toPull = require('stream-to-pull-stream')
1213
const waterfall = require('async/waterfall')
1314
const isStream = require('is-stream')
1415
const { isSource } = require('is-pull-stream')
16+
=======
17+
const toAsyncIterator = require('pull-stream-to-async-iterator')
18+
const toPullStream = require('async-iterator-to-pull-stream')
19+
const pull = require('pull-stream')
20+
const toPull = require('stream-to-pull-stream')
21+
const waterfall = require('async/waterfall')
22+
const isStream = require('is-stream')
23+
const isSource = require('is-pull-stream').isSource
24+
>>>>>>> test: most tests passing
1525
const { parseChunkerString } = require('./utils')
1626
const streamFromFileReader = require('ipfs-utils/src/streams/stream-from-filereader')
1727
const { supportsFileReader } = require('ipfs-utils/src/supports')
@@ -23,30 +33,28 @@ function noop () {}
2333
function prepareFile (file, self, opts, callback) {
2434
opts = opts || {}
2535

26-
let cid = new CID(file.multihash)
27-
28-
if (opts.cidVersion === 1) {
29-
cid = cid.toV1()
30-
}
36+
let cid = file.cid
3137

3238
waterfall([
3339
(cb) => opts.onlyHash
3440
? cb(null, file)
35-
: self.object.get(file.multihash, Object.assign({}, opts, { preload: false }), cb),
41+
: self.object.get(file.cid, Object.assign({}, opts, { preload: false }), cb),
3642
(node, cb) => {
37-
const b58Hash = cid.toBaseEncodedString()
43+
if (opts.cidVersion === 1) {
44+
cid = cid.toV1()
45+
}
3846

47+
const b58Hash = cid.toBaseEncodedString()
3948
let size = node.size
4049

4150
if (Buffer.isBuffer(node)) {
4251
size = node.length
4352
}
4453

4554
cb(null, {
46-
path: opts.wrapWithDirectory
47-
? file.path.substring(WRAPPER.length)
48-
: (file.path || b58Hash),
55+
path: file.path === undefined ? b58Hash : (file.path || ''),
4956
hash: b58Hash,
57+
// multihash: b58Hash,
5058
size
5159
})
5260
}
@@ -90,16 +98,16 @@ function normalizeContent (content, opts) {
9098
throw new Error('Must provide a path when wrapping with a directory')
9199
}
92100

93-
if (opts.wrapWithDirectory) {
94-
data.path = WRAPPER + data.path
95-
}
101+
//if (opts.wrapWithDirectory) {
102+
// data.path = WRAPPER + data.path
103+
//}
96104

97105
return data
98106
})
99107
}
100108

101109
function preloadFile (file, self, opts) {
102-
const isRootFile = opts.wrapWithDirectory
110+
const isRootFile = !file.path || opts.wrapWithDirectory
103111
? file.path === ''
104112
: !file.path.includes('/')
105113

@@ -140,7 +148,10 @@ module.exports = function (self) {
140148
shardSplitThreshold: self._options.EXPERIMENTAL.sharding
141149
? 1000
142150
: Infinity
143-
}, options, chunkerOptions)
151+
}, options, {
152+
...chunkerOptions.chunkerOptions,
153+
chunker: chunkerOptions.chunker
154+
})
144155

145156
// CID v0 is for multihashes encoded with sha2-256
146157
if (opts.hashAlg && opts.cidVersion !== 1) {
@@ -157,12 +168,25 @@ module.exports = function (self) {
157168

158169
opts.progress = progress
159170
return pull(
171+
<<<<<<< HEAD
160172
pullMap(content => normalizeContent(content, opts)),
161173
pullFlatten(),
162174
importer(self._ipld, opts),
163175
pullAsyncMap((file, cb) => prepareFile(file, self, opts, cb)),
164176
pullMap(file => preloadFile(file, self, opts)),
165177
pullAsyncMap((file, cb) => pinFile(file, self, opts, cb))
178+
=======
179+
pull.map(content => normalizeContent(content, opts)),
180+
pull.flatten(),
181+
pull.map(file => ({
182+
path: file.path ? file.path : undefined,
183+
content: file.content ? toAsyncIterator(file.content) : undefined
184+
})),
185+
toPullStream.transform(source => importer(source, self._ipld, opts)),
186+
pull.asyncMap((file, cb) => prepareFile(file, self, opts, cb)),
187+
pull.map(file => preloadFile(file, self, opts)),
188+
pull.asyncMap((file, cb) => pinFile(file, self, opts, cb))
189+
>>>>>>> test: most tests passing
166190
)
167191
}
168192
}

src/core/components/files-regular/cat-pull-stream.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

33
const exporter = require('ipfs-unixfs-exporter')
4-
const pull = require('pull-stream')
54
const deferred = require('pull-defer')
5+
const toPullStream = require('async-iterator-to-pull-stream')
66
const { normalizePath } = require('./utils')
77

88
module.exports = function (self) {
@@ -15,40 +15,31 @@ module.exports = function (self) {
1515

1616
ipfsPath = normalizePath(ipfsPath)
1717
const pathComponents = ipfsPath.split('/')
18-
const fileNameOrHash = pathComponents[pathComponents.length - 1]
1918

2019
if (options.preload !== false) {
2120
self._preload(pathComponents[0])
2221
}
2322

2423
const d = deferred.source()
2524

26-
pull(
27-
exporter(ipfsPath, self._ipld, options),
28-
pull.filter(file => file.path === fileNameOrHash),
29-
pull.take(1),
30-
pull.collect((err, files) => {
31-
if (err) {
32-
return d.abort(err)
25+
exporter(ipfsPath, self._ipld, options)
26+
.then(file => {
27+
if (!file.unixfs) {
28+
return d.abort(new Error('this dag node is not a UnixFS node'))
3329
}
3430

35-
if (!files.length) {
36-
return d.abort(new Error('No such file'))
37-
}
38-
39-
const file = files[0]
40-
41-
if (!file.content && file.type === 'dir') {
31+
if (file.unixfs.type.includes('dir')) {
4232
return d.abort(new Error('this dag node is a directory'))
4333
}
4434

4535
if (!file.content) {
4636
return d.abort(new Error('this dag node has no content'))
4737
}
4838

49-
d.resolve(file.content)
39+
d.resolve(toPullStream.source(file.content(options)))
40+
}, err => {
41+
d.abort(err)
5042
})
51-
)
5243

5344
return d
5445
}

src/core/components/files-regular/get-pull-stream.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict'
22

33
const exporter = require('ipfs-unixfs-exporter')
4-
const pull = require('pull-stream')
4+
const toPullStream = require('async-iterator-to-pull-stream')
55
const errCode = require('err-code')
6-
const { normalizePath } = require('./utils')
6+
const pull = require('pull-stream/pull')
7+
const map = require('pull-stream/throughs/map')
8+
const { normalizePath, mapFile } = require('./utils')
79

810
module.exports = function (self) {
911
return (ipfsPath, options) => {
@@ -22,12 +24,11 @@ module.exports = function (self) {
2224
}
2325

2426
return pull(
25-
exporter(ipfsPath, self._ipld, options),
26-
pull.map(file => {
27-
file.hash = file.cid.toString()
28-
delete file.cid
29-
return file
30-
})
27+
toPullStream.source(exporter.recursive(ipfsPath, self._ipld, options)),
28+
map(mapFile({
29+
...options,
30+
includeContent: true
31+
}))
3132
)
3233
}
3334
}
Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
'use strict'
22

33
const exporter = require('ipfs-unixfs-exporter')
4-
const pull = require('pull-stream')
5-
const { normalizePath } = require('./utils')
4+
const deferred = require('pull-defer')
5+
const pull = require('pull-stream/pull')
6+
const once = require('pull-stream/sources/once')
7+
const map = require('pull-stream/throughs/map')
8+
const filter = require('pull-stream/throughs/filter')
9+
const errCode = require('err-code')
10+
const toPullStream = require('async-iterator-to-pull-stream')
11+
const { normalizePath, mapFile } = require('./utils')
612

713
module.exports = function (self) {
814
return function (ipfsPath, options) {
@@ -11,25 +17,48 @@ module.exports = function (self) {
1117
const path = normalizePath(ipfsPath)
1218
const recursive = options.recursive
1319
const pathComponents = path.split('/')
14-
const pathDepth = pathComponents.length
15-
const maxDepth = recursive ? global.Infinity : pathDepth
16-
options.maxDepth = options.maxDepth || maxDepth
1720

1821
if (options.preload !== false) {
1922
self._preload(pathComponents[0])
2023
}
2124

22-
return pull(
23-
exporter(ipfsPath, self._ipld, options),
24-
pull.filter(node =>
25-
recursive ? node.depth >= pathDepth : node.depth === pathDepth
26-
),
27-
pull.map(node => {
28-
node.hash = node.cid.toString()
29-
delete node.cid
30-
delete node.content
31-
return node
25+
const d = deferred.source()
26+
27+
exporter(ipfsPath, self._ipld, options)
28+
.then(file => {
29+
if (!file.unixfs) {
30+
return d.abort(errCode(new Error('dag node was not a UnixFS node'), 'ENOTUNIXFS'))
31+
}
32+
33+
if (file.unixfs.type === 'file') {
34+
return d.resolve(once(mapFile(options)(file)))
35+
}
36+
37+
if (file.unixfs.type.includes('dir')) {
38+
if (recursive) {
39+
return d.resolve(pull(
40+
toPullStream.source(exporter.recursive(file.cid, self._ipld, options)),
41+
filter(child => file.cid.toBaseEncodedString() !== child.cid.toBaseEncodedString()),
42+
map(mapFile(options))
43+
))
44+
}
45+
46+
return d.resolve(pull(
47+
toPullStream.source(file.content()),
48+
map(mapFile(options)),
49+
map((file) => {
50+
file.depth--
51+
52+
return file
53+
})
54+
))
55+
}
56+
57+
d.abort(errCode(new Error(`Unknown UnixFS type ${file.unixfs.type}`), 'EUNKNOWNUNIXFSTYPE'))
58+
}, err => {
59+
d.abort(err)
3260
})
33-
)
61+
62+
return d
3463
}
3564
}

0 commit comments

Comments
 (0)