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

Commit ccecb1b

Browse files
committed
feat: basic mfs.write command
License: MIT Signed-off-by: achingbrain <[email protected]>
1 parent 723bbe9 commit ccecb1b

16 files changed

+494
-150
lines changed

package.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,30 @@
3838
"aegir": "^13.0.6",
3939
"chai": "^4.1.2",
4040
"dirty-chai": "^2.0.1",
41-
"ipfs": "^0.28.2",
41+
"ipfs": "~0.28.2",
4242
"pre-commit": "^1.2.2",
4343
"safe-buffer": "^5.1.1",
44-
"tmp": "0.0.33"
44+
"sign-commit": "~0.1.0",
45+
"tmp": "~0.0.33"
4546
},
4647
"dependencies": {
4748
"async": "^2.6.0",
48-
"blob": "0.0.4",
49+
"blob": "~0.0.4",
4950
"bs58": "^4.0.1",
5051
"cids": "~0.5.3",
5152
"debug": "^3.1.0",
5253
"detect-node": "^2.0.3",
53-
"file-api": "^0.10.4",
54+
"file-api": "~0.10.4",
5455
"filereader-stream": "^1.0.0",
55-
"interface-datastore": "^0.4.2",
56-
"ipfs-unixfs": "^0.1.14",
57-
"ipfs-unixfs-engine": "^0.29.0",
58-
"is-pull-stream": "0.0.0",
56+
"interface-datastore": "~0.4.2",
57+
"ipfs-unixfs": "~0.1.14",
58+
"ipfs-unixfs-engine": "~0.29.0",
59+
"is-pull-stream": "~0.0.0",
5960
"is-stream": "^1.1.0",
6061
"promisify-es6": "^1.0.3",
6162
"pull-cat": "^1.1.11",
6263
"pull-paramap": "^1.2.2",
64+
"pull-pushable": "^2.2.0",
6365
"pull-stream": "^3.6.7",
6466
"pull-traverse": "^1.0.3",
6567
"stream-to-pull-stream": "^1.7.2"

src/core/stat.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,9 @@ module.exports = function mfsStat (ipfs) {
4545

4646
const meta = unmarshal(node.data)
4747

48-
let size = 0
49-
50-
if (meta.data && meta.data.length) {
51-
size = meta.data.length
52-
}
53-
54-
if (meta.blockSizes && meta.blockSizes.length) {
55-
size = meta.blockSizes.reduce((acc, curr) => acc + curr, 0)
56-
}
57-
5848
done(null, {
5949
hash: node.multihash,
60-
size: size,
50+
size: meta.fileSize(),
6151
cumulativeSize: node.size,
6252
childBlocks: meta.blockSizes.length,
6353
type: meta.type

src/core/utils/add-link.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const addLink = (ipfs, options, callback) => {
1212
options = Object.assign({}, {
1313
parent: undefined,
1414
child: undefined,
15-
name: undefined,
15+
name: '',
1616
flush: true
1717
}, options)
1818

@@ -24,14 +24,14 @@ const addLink = (ipfs, options, callback) => {
2424
return callback(new Error('No child passed to addLink'))
2525
}
2626

27-
if (!options.name) {
28-
return callback(new Error('No name passed to addLink'))
29-
}
30-
3127
waterfall([
3228
(done) => {
33-
// Remove the old link if necessary
34-
DAGNode.rmLink(options.parent, options.name, done)
29+
if (options.name) {
30+
// Remove the old link if necessary
31+
return DAGNode.rmLink(options.parent, options.name, done)
32+
}
33+
34+
done(null, options.parent)
3535
},
3636
(parent, done) => {
3737
// Add the new link to the parent

src/core/utils/constants.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ const Key = require('interface-datastore').Key
44

55
module.exports = {
66
FILE_SEPARATOR: '/',
7-
MFS_ROOT_KEY: new Key('/local/filesroot')
7+
MFS_ROOT_KEY: new Key('/local/filesroot'),
8+
MAX_CHUNK_SIZE: 262144,
9+
MAX_LINKS: 174
810
}

src/core/utils/create-node.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const waterfall = require('async/waterfall')
4+
const {
5+
DAGNode
6+
} = require('ipld-dag-pb')
7+
8+
const createNode = (ipfs, data, links, options, callback) => {
9+
waterfall([
10+
// Create a DAGNode with the new data
11+
(cb) => DAGNode.create(data, links, cb),
12+
(newNode, cb) => {
13+
// Persist it
14+
ipfs.dag.put(newNode, {
15+
format: options.format,
16+
hashAlg: options.hashAlg
17+
}, (error) => cb(error, newNode))
18+
}
19+
], callback)
20+
}
21+
22+
module.exports = createNode

src/core/utils/end-pull-stream.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
const endPullStream = (callback) => {
4+
// Ugh. https://github.com/standard/standard/issues/623
5+
const foo = true
6+
return callback(foo)
7+
}
8+
9+
module.exports = endPullStream

src/core/utils/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
const constants = require('./constants')
44

55
module.exports = {
6+
endPullStream: require('./end-pull-stream'),
67
validatePath: require('./validate-path'),
78
withMfsRoot: require('./with-mfs-root'),
89
updateMfsRoot: require('./update-mfs-root'),
910
traverseTo: require('./traverse-to'),
1011
addLink: require('./add-link'),
1112
updateTree: require('./update-tree'),
13+
createNode: require('./create-node'),
1214
limitStreamBytes: require('./limit-stream-bytes'),
13-
FILE_SEPARATOR: constants.FILE_SEPARATOR
15+
loadNode: require('./load-node'),
16+
zeros: require('./zeros'),
17+
FILE_SEPARATOR: constants.FILE_SEPARATOR,
18+
MAX_CHUNK_SIZE: constants.MAX_CHUNK_SIZE,
19+
MAX_LINKS: constants.MAX_LINKS
1420
}

src/core/utils/limit-stream-bytes.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict'
22

33
const asyncMap = require('pull-stream/throughs/async-map')
4+
const endPullStream = require('./end-pull-stream')
45

56
const limitStreamBytes = (limit) => {
67
let bytesRead = 0
78

89
return asyncMap((buffer, cb) => {
910
if (bytesRead > limit) {
10-
// Ugh. https://github.com/standard/standard/issues/623
11-
const foo = true
12-
return cb(foo)
11+
endPullStream(cb)
1312
}
1413

1514
// If we only need to return part of this buffer, slice it to make it smaller

src/core/utils/load-node.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
const waterfall = require('async/waterfall')
4+
const CID = require('cids')
5+
const log = require('debug')('mfs:utils:load-node')
6+
const bs58 = require('bs58')
7+
8+
const loadNode = (ipfs, cid, callback) => {
9+
const multihash = cid && (cid.multihash || cid.hash)
10+
11+
if (!multihash) {
12+
log(`No multihash passed so cannot load DAGNode`)
13+
14+
return callback()
15+
}
16+
17+
log(`Loading DAGNode for child ${bs58.encode(multihash)}`)
18+
19+
waterfall([
20+
(cb) => ipfs.dag.get(new CID(multihash), cb),
21+
(result, cb) => cb(null, result.value)
22+
], callback)
23+
}
24+
25+
module.exports = loadNode

src/core/utils/traverse-to.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const traverseTo = (ipfs, path, options, callback) => {
4848
node: rootNode,
4949
parent: null
5050
}, (parent, {pathSegment, index}, done) => {
51-
log(`Looking for ${pathSegment} in ${parent.name} ${bs58.encode(parent.node.multihash)}s`)
51+
log(`Looking for ${pathSegment} in ${parent.name} ${bs58.encode(parent.node.multihash)}`)
5252

5353
parent.node.links.forEach(link => {
5454
log(`${bs58.encode(link.multihash)} ${link.name}`)

src/core/utils/zeros.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
// A pull stream source that will emit buffers full of zeros up to the specified length
4+
const zeros = (max = Infinity, increment = 4096) => {
5+
let i = 0
6+
7+
return (end, cb) => {
8+
if (end) {
9+
return cb && cb(end)
10+
}
11+
12+
if (i >= max) {
13+
// Ugh. https://github.com/standard/standard/issues/623
14+
const foo = true
15+
return cb(foo)
16+
}
17+
18+
let nextLength = increment
19+
20+
if ((i + nextLength) > max) {
21+
// final chunk doesn't divide neatly into increment
22+
nextLength = max - i
23+
}
24+
25+
i += nextLength
26+
27+
cb(null, Buffer.alloc(nextLength, 0))
28+
}
29+
}
30+
31+
module.exports = zeros

src/core/write/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,6 @@ module.exports = function mfsWrite (ipfs) {
199199
(newRoot, next) => updateMfsRoot(ipfs, newRoot.node.multihash, next)
200200
], done)
201201
}
202-
], (error, result) => callback(error, result))
202+
], (error) => callback(error))
203203
})
204204
}

0 commit comments

Comments
 (0)