Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 61f77dc

Browse files
committed
feat: support --raw-leaves
License: MIT Signed-off-by: achingbrain <[email protected]>
1 parent 4ef648b commit 61f77dc

24 files changed

+141
-125
lines changed

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"devDependencies": {
4040
"aegir": "^15.0.0",
4141
"chai": "^4.1.2",
42+
"detect-node": "^2.0.3",
4243
"detect-webworker": "^1.0.0",
4344
"dirty-chai": "^2.0.1",
4445
"ipfs": "~0.30.0",
@@ -48,15 +49,13 @@
4849
"dependencies": {
4950
"async": "^2.6.1",
5051
"blob": "~0.0.4",
51-
"bs58": "^4.0.1",
5252
"cids": "~0.5.3",
5353
"debug": "^3.1.0",
54-
"detect-node": "^2.0.3",
5554
"file-api": "~0.10.4",
5655
"filereader-stream": "^2.0.0",
5756
"interface-datastore": "~0.4.2",
5857
"ipfs-unixfs": "~0.1.15",
59-
"ipfs-unixfs-engine": "~0.30.0",
58+
"ipfs-unixfs-engine": "~0.31.1",
6059
"is-pull-stream": "~0.0.0",
6160
"is-stream": "^1.1.0",
6261
"joi": "^13.4.0",
@@ -67,7 +66,7 @@
6766
"pull-cat": "^1.1.11",
6867
"pull-paramap": "^1.2.2",
6968
"pull-pushable": "^2.2.0",
70-
"pull-stream": "^3.6.7",
69+
"pull-stream": "^3.6.8",
7170
"pull-stream-to-stream": "^1.3.4",
7271
"pull-traverse": "^1.0.3",
7372
"stream-to-pull-stream": "^1.7.2"

src/cli/write.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
describe: 'Create any non-existent intermediate directories'
1717
},
1818
create: {
19-
alias: 'c',
19+
alias: 'e',
2020
type: 'boolean',
2121
default: false,
2222
coerce: asBoolean,
@@ -42,7 +42,7 @@ module.exports = {
4242
rawLeaves: {
4343
alias: 'r',
4444
type: 'boolean',
45-
default: true,
45+
default: false,
4646
coerce: asBoolean,
4747
describe: 'Whether to write leaf nodes as raw UnixFS nodes'
4848
},
@@ -65,9 +65,9 @@ module.exports = {
6565
default: 'balanced'
6666
},
6767
cidVersion: {
68-
alias: 'c',
68+
alias: ['cid-ver', 'cid-version'],
6969
type: 'number',
70-
default: 1
70+
default: 0
7171
},
7272
hashAlg: {
7373
alias: 'h',
@@ -105,7 +105,7 @@ module.exports = {
105105
length,
106106
create,
107107
truncate,
108-
rawLeafNodes: rawLeaves,
108+
rawLeaves,
109109
reduceSingleLeafToSelf,
110110
cidVersion,
111111
hashAlg,

src/core/mkdir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const {
1212
const defaultOptions = {
1313
parents: false,
1414
hash: undefined,
15-
cidVersion: undefined
15+
cidVersion: 0
1616
}
1717

1818
module.exports = (ipfs) => {

src/core/utils/load-node.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
const waterfall = require('async/waterfall')
44
const CID = require('cids')
55
const log = require('debug')('ipfs:mfs:utils:load-node')
6-
const bs58 = require('bs58')
76

8-
const loadNode = (ipfs, cid, callback) => {
9-
const multihash = cid && (cid.multihash || cid.hash)
7+
const loadNode = (ipfs, object, callback) => {
8+
const multihash = object && (object.multihash || object.hash)
109

1110
if (!multihash) {
1211
log(`No multihash passed so cannot load DAGNode`)
1312

1413
return callback()
1514
}
1615

17-
log(`Loading DAGNode for child ${bs58.encode(multihash)}`)
16+
const cid = new CID(multihash)
17+
18+
log(`Loading DAGNode for child ${cid.toBaseEncodedString()}`)
1819

1920
waterfall([
20-
(cb) => ipfs.dag.get(new CID(multihash), cb),
21+
(cb) => ipfs.dag.get(cid, cb),
2122
(result, cb) => cb(null, result.value)
2223
], callback)
2324
}

src/core/utils/traverse-to.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const bs58 = require('bs58')
43
const CID = require('cids')
54
const log = require('debug')('ipfs:mfs:utils:traverse-to')
65
const UnixFS = require('ipfs-unixfs')
@@ -77,8 +76,6 @@ const traverseToMfsObject = (ipfs, path, options, callback) => {
7776
node: rootNode,
7877
parent: null
7978
}, (parent, {pathSegment, index}, done) => {
80-
log(`Looking for ${pathSegment} in ${parent.name} ${bs58.encode(parent.node.multihash)}`)
81-
8279
const existingLink = parent.node.links.find(link => link.name === pathSegment)
8380

8481
if (!existingLink) {
@@ -114,6 +111,7 @@ const traverseToMfsObject = (ipfs, path, options, callback) => {
114111
next(error, {
115112
name: pathSegment,
116113
node: emptyDirectory,
114+
cid: new CID(emptyDirectory.multihash),
117115
parent: parent
118116
})
119117
})
@@ -126,43 +124,31 @@ const traverseToMfsObject = (ipfs, path, options, callback) => {
126124
}
127125

128126
let hash = existingLink.hash || existingLink.multihash
127+
const cid = new CID(hash)
129128

130129
// child existed, fetch it
131-
ipfs.dag.get(new CID(hash), (error, result) => {
132-
log(`Loaded ${bs58.encode(result.value.multihash)} from ${bs58.encode(hash)}`)
130+
ipfs.dag.get(cid, (error, result) => {
131+
if (error) {
132+
return done(error)
133+
}
134+
135+
const node = result.value
136+
133137
const child = {
134138
name: pathSegment,
135-
node: result && result.value,
139+
node,
136140
parent: parent
137141
}
138142

139143
trail.push(child)
140144

141-
done(error, child)
145+
done(null, child)
142146
})
143147
}, cb)
144148
}
145149
], done)
146150
}
147-
], (error, trail) => {
148-
if (!error) {
149-
let node = trail
150-
let path = []
151-
152-
while (node) {
153-
path.push(`${node.name} ${bs58.encode(node.node.multihash)}`)
154-
node = node.parent
155-
}
156-
157-
log('Path:')
158-
159-
path
160-
.reverse()
161-
.forEach((segment) => log(segment))
162-
}
163-
164-
callback(error, trail)
165-
})
151+
], callback)
166152
}
167153

168154
module.exports = traverseTo

src/core/utils/update-mfs-root.js

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

3-
const bs58 = require('bs58')
43
const log = require('debug')('ipfs:mfs:utils:update-mfs:root')
54
const waterfall = require('async/waterfall')
5+
const CID = require('cids')
66
const {
77
MFS_ROOT_KEY
88
} = require('./constants')
@@ -15,11 +15,9 @@ const updateMfsRoot = (ipfs, buffer, callback) => {
1515
return callback(new Error('Please run jsipfs init first'))
1616
}
1717

18-
if (typeof buffer === 'string' || buffer instanceof String) {
19-
buffer = bs58.decode(buffer)
20-
}
18+
const cid = new CID(buffer)
2119

22-
log(`New MFS root will be ${bs58.encode(buffer)}`)
20+
log(`New MFS root will be ${cid.toBaseEncodedString()}`)
2321

2422
waterfall([
2523
(cb) => {
@@ -31,8 +29,8 @@ const updateMfsRoot = (ipfs, buffer, callback) => {
3129
log('Datastore was already open')
3230
cb()
3331
},
34-
(cb) => datastore.put(MFS_ROOT_KEY, buffer, (error) => cb(error))
35-
], (error) => callback(error, buffer))
32+
(cb) => datastore.put(MFS_ROOT_KEY, cid.buffer, (error) => cb(error))
33+
], (error) => callback(error, cid))
3634
}
3735

3836
module.exports = updateMfsRoot

src/core/utils/with-mfs-root.js

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

3-
const bs58 = require('bs58')
43
const CID = require('cids')
54
const log = require('debug')('ipfs:mfs:utils:with-mfs-root')
65
const waterfall = require('async/waterfall')
@@ -32,21 +31,21 @@ const withMfsRoot = (ipfs, callback) => {
3231
path: '/'
3332
}, next),
3433
// Turn the hash into a Buffer
35-
([{hash}], next) => next(null, bs58.decode(hash)),
36-
(buffer, next) => repo.closed ? datastore.open((error) => next(error, buffer)) : next(null, buffer),
34+
([{hash}], next) => next(null, new CID(hash)),
35+
(cid, next) => repo.closed ? datastore.open((error) => next(error, cid)) : next(null, cid),
3736
// Store the Buffer in the datastore
38-
(buffer, next) => datastore.put(MFS_ROOT_KEY, buffer, (error) => next(error, buffer))
37+
(cid, next) => datastore.put(MFS_ROOT_KEY, cid.buffer, (error) => next(error, cid))
3938
], cb)
4039
}
4140

42-
cb(error, result)
41+
cb(error, new CID(result))
4342
})
4443
},
4544
// Turn the Buffer into a CID
46-
(hash, cb) => {
47-
log(`Fetched MFS root ${bs58.encode(hash)}`)
45+
(cid, cb) => {
46+
log(`Fetched MFS root ${cid.toBaseEncodedString()}`)
4847

49-
cb(null, new CID(hash))
48+
cb(null, cid)
5049
}
5150
// Invoke the API function with the root CID
5251
], callback)

src/core/write/import-node.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const pull = require('pull-stream/pull')
55
const values = require('pull-stream/sources/values')
66
const collect = require('pull-stream/sinks/collect')
77
const importer = require('ipfs-unixfs-engine').importer
8-
const bs58 = require('bs58')
9-
const log = require('debug')('ipfs:mfs:import-node')
108
const {
119
loadNode
1210
} = require('../utils')
@@ -19,19 +17,16 @@ const importStream = (ipfs, source, options, callback) => {
1917
}]),
2018
importer(ipfs._ipld, {
2119
progress: options.progress,
22-
hashAlg: options.hash,
20+
hashAlg: options.hashAlg,
2321
cidVersion: options.cidVersion,
2422
strategy: options.strategy,
25-
rawLeafNodes: options.rawLeafNodes,
26-
reduceSingleLeafToSelf: options.reduceSingleLeafToSelf
23+
rawLeaves: options.rawLeaves,
24+
reduceSingleLeafToSelf: options.reduceSingleLeafToSelf,
25+
leafType: options.leafType
2726
}),
2827
collect(cb)
2928
),
30-
(results, cb) => {
31-
log(`Imported file ${bs58.encode(results[0].multihash)}`)
32-
33-
return loadNode(ipfs, results[0], cb)
34-
}
29+
(results, cb) => loadNode(ipfs, results[0], cb)
3530
], callback)
3631
}
3732

src/core/write/index.js

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,31 @@ const {
1414
} = require('../utils')
1515
const values = require('pull-stream/sources/values')
1616
const log = require('debug')('ipfs:mfs:write')
17-
const bs58 = require('bs58')
1817
const importNode = require('./import-node')
1918
const updateNode = require('./update-node')
2019
const toPull = require('stream-to-pull-stream')
2120
const isStream = require('is-stream')
22-
const isNode = require('detect-node')
2321
const fileReaderStream = require('filereader-stream')
2422
const isPullStream = require('is-pull-stream')
2523
const cat = require('pull-cat')
2624
const pull = require('pull-stream/pull')
27-
28-
let fs
29-
30-
if (isNode) {
31-
fs = require('fs')
32-
}
25+
const fs = require('fs')
3326

3427
const defaultOptions = {
3528
offset: 0, // the offset in the file to begin writing
3629
length: undefined, // how many bytes from the incoming buffer to write
3730
create: false, // whether to create the file if it does not exist
3831
truncate: false, // whether to truncate the file first
39-
rawLeafNodes: true,
32+
rawLeaves: false,
4033
reduceSingleLeafToSelf: false,
41-
cidVersion: undefined,
34+
cidVersion: 0,
4235
hashAlg: 'sha2-256',
4336
format: 'dag-pb',
4437
parents: false, // whether to create intermediate directories if they do not exist
4538
progress: undefined,
4639
strategy: 'trickle',
47-
flush: true
40+
flush: true,
41+
leafType: 'raw'
4842
}
4943

5044
const toPullSource = (content, options, callback) => {
@@ -160,10 +154,11 @@ const updateOrImport = (ipfs, options, path, source, containingFolder, callback)
160154
}, null)
161155

162156
if (existingChild) {
163-
log('Updating linked DAGNode', bs58.encode(existingChild.multihash))
157+
const cid = new CID(existingChild.multihash)
158+
log(`Updating linked DAGNode ${cid.toBaseEncodedString()}`)
164159

165160
// overwrite the existing file or part of it, possibly truncating what's left
166-
updateNode(ipfs, new CID(existingChild.multihash), source, options, next)
161+
updateNode(ipfs, cid, source, options, next)
167162
} else {
168163
if (!options.create) {
169164
return next(new Error('file does not exist'))
@@ -185,10 +180,7 @@ const updateOrImport = (ipfs, options, path, source, containingFolder, callback)
185180
)
186181

187182
log('Importing file', path.name)
188-
importNode(ipfs, source, options, (error, result) => {
189-
log(`Imported file ${path.name} ${bs58.encode(result.multihash)}`)
190-
next(error, result)
191-
})
183+
importNode(ipfs, source, options, next)
192184
}
193185
},
194186

@@ -205,12 +197,6 @@ const updateOrImport = (ipfs, options, path, source, containingFolder, callback)
205197
// Store new containing folder CID
206198
containingFolder.node = newContaingFolder
207199

208-
log(`New CID for the containing folder is ${bs58.encode(newContaingFolder.multihash)}`)
209-
210-
newContaingFolder.links.forEach(link => {
211-
log(`${link.name} ${bs58.encode(link.multihash)}`)
212-
})
213-
214200
next(error)
215201
}),
216202

src/core/write/truncate-node.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ const truncateNode = (ipfs, dagNode, newLength, options, callback) => {
2929
hashAlg: options.hash,
3030
cidVersion: options.cidVersion,
3131
strategy: options.strategy,
32-
rawLeafNodes: true,
33-
reduceSingleLeafToSelf: false
32+
rawLeaves: options.rawLeaves,
33+
reduceSingleLeafToSelf: options.reduceSingleLeafToSelf,
34+
leafType: options.leafType
3435
}),
3536
collect(cb)
3637
)

0 commit comments

Comments
 (0)