This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
with-mfs-root.js
52 lines (45 loc) · 1.55 KB
/
with-mfs-root.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
'use strict'
const CID = require('cids')
const UnixFs = require('ipfs-unixfs')
const {
DAGNode
} = require('ipld-dag-pb')
const log = require('debug')('ipfs:mfs:utils:with-mfs-root')
const waterfall = require('async/waterfall')
const {
MFS_ROOT_KEY
} = require('./constants')
const withMfsRoot = (context, callback) => {
waterfall([
// Open the repo if it's been closed
(cb) => context.repo.datastore.open((error) => cb(error)),
(cb) => {
// Load the MFS root CID
context.repo.datastore.get(MFS_ROOT_KEY, (error, result) => {
// Once datastore-level releases its error.code addition, we can remove error.notFound logic
if (error && (error.notFound || error.code === 'ERR_NOT_FOUND')) {
log('Creating new MFS root')
return waterfall([
// Store an empty node as the root
(next) => DAGNode.create(new UnixFs('directory').marshal(), next),
(node, next) => context.ipld.put(node, {
version: 0,
hashAlg: 'sha2-256',
format: 'dag-pb'
}, next),
// Store the Buffer in the datastore
(cid, next) => context.repo.datastore.put(MFS_ROOT_KEY, cid.buffer, (error) => next(error, cid))
], cb)
}
cb(error, result ? new CID(result) : null)
})
},
// Turn the Buffer into a CID
(cid, cb) => {
log(`Fetched MFS root ${cid.toBaseEncodedString()}`)
cb(null, cid)
}
// Invoke the API function with the root CID
], callback)
}
module.exports = withMfsRoot