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

Commit a0fd5b9

Browse files
committed
feat: mfs implementation
License: MIT Signed-off-by: achingbrain <alex@achingbrain.net>
1 parent 2e40fb8 commit a0fd5b9

File tree

11 files changed

+88
-34
lines changed

11 files changed

+88
-34
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ Creates and returns an instance of an IPFS node. Use the `options` argument to s
233233
- `hop` (object)
234234
- `enabled` (boolean): Make this node a relay (other nodes can connect *through* it). (Default: `false`)
235235
- `active` (boolean): Make this an *active* relay node. Active relay nodes will attempt to dial a destination peer even if that peer is not yet connected to the relay. (Default: `false`)
236-
236+
- `mfs` (boolean): Enables Mutable File System commands - see `jsipfs files --help` for more (Default: `false`)
237+
237238
- `config` (object) Modify the default IPFS node config. Find the Node.js defaults at [`src/core/runtime/config-nodejs.js`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/config-nodejs.js) and the browser defaults at [`src/core/runtime/config-browser.js`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/config-browser.js). This object will be *merged* with the default config; it will not replace it.
238239

239240
- `libp2p` (object) add custom modules to the libp2p stack of your node

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"expose-loader": "~0.7.5",
7474
"form-data": "^2.3.2",
7575
"hat": "~0.0.3",
76-
"interface-ipfs-core": "~0.65.7",
76+
"interface-ipfs-core": "~0.66.2",
7777
"ipfsd-ctl": "~0.34.0",
7878
"lodash": "^4.17.10",
7979
"mocha": "^5.1.1",
@@ -109,6 +109,7 @@
109109
"ipfs-bitswap": "~0.20.0",
110110
"ipfs-block": "~0.7.1",
111111
"ipfs-block-service": "~0.14.0",
112+
"ipfs-mfs": "~0.0.1",
112113
"ipfs-multipart": "~0.1.0",
113114
"ipfs-repo": "~0.20.0",
114115
"ipfs-unixfs": "~0.1.14",

src/cli/bin.js

+29-28
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
const yargs = require('yargs')
66
const updateNotifier = require('update-notifier')
77
const readPkgUp = require('read-pkg-up')
8-
const fs = require('fs')
9-
const path = require('path')
108
const utils = require('./utils')
119
const print = utils.print
1210

@@ -18,10 +16,6 @@ updateNotifier({
1816

1917
const args = process.argv.slice(2)
2018

21-
// Determine if the first argument is a sub-system command
22-
const commandNames = fs.readdirSync(path.join(__dirname, 'commands'))
23-
const isCommand = commandNames.includes(`${args[0]}.js`)
24-
2519
const cli = yargs
2620
.option('silent', {
2721
desc: 'Write no output',
@@ -34,14 +28,6 @@ const cli = yargs
3428
type: 'string',
3529
default: ''
3630
})
37-
.commandDir('commands', {
38-
// Only include the commands for the sub-system we're using, or include all
39-
// if no sub-system command has been passed.
40-
include (path, filename) {
41-
if (!isCommand) return true
42-
return `${args[0]}.js` === filename
43-
}
44-
})
4531
.epilog(utils.ipfsPathHelp)
4632
.demandCommand(1)
4733
.fail((msg, err, yargs) => {
@@ -56,27 +42,15 @@ const cli = yargs
5642
yargs.showHelp()
5743
})
5844

59-
// If not a sub-system command then load the top level aliases
60-
if (!isCommand) {
61-
// NOTE: This creates an alias of
62-
// `jsipfs files {add, get, cat}` to `jsipfs {add, get, cat}`.
63-
// This will stay until https://github.com/ipfs/specs/issues/98 is resolved.
64-
const addCmd = require('./commands/files/add')
65-
const catCmd = require('./commands/files/cat')
66-
const getCmd = require('./commands/files/get')
67-
const aliases = [addCmd, catCmd, getCmd]
68-
aliases.forEach((alias) => {
69-
cli.command(alias.command, alias.describe, alias.builder, alias.handler)
70-
})
71-
}
72-
7345
// Need to skip to avoid locking as these commands
7446
// don't require a daemon
7547
if (args[0] === 'daemon' || args[0] === 'init') {
7648
cli
7749
.help()
7850
.strict()
7951
.completion()
52+
.command(require('./commands/daemon'))
53+
.command(require('./commands/init'))
8054
.parse(args)
8155
} else {
8256
// here we have to make a separate yargs instance with
@@ -86,10 +60,37 @@ if (args[0] === 'daemon' || args[0] === 'init') {
8660
if (err) {
8761
throw err
8862
}
63+
8964
utils.getIPFS(argv, (err, ipfs, cleanup) => {
9065
if (err) { throw err }
9166

67+
const enableMfs = ipfs._options && ipfs._options.EXPERIMENTAL && ipfs._options && ipfs._options.EXPERIMENTAL.mfs
68+
69+
if (enableMfs) {
70+
require('ipfs-mfs/cli')(cli)
71+
}
72+
73+
// NOTE: This creates an alias of
74+
// `jsipfs files {add, get, cat}` to `jsipfs {add, get, cat}`.
75+
// This will stay until https://github.com/ipfs/specs/issues/98 is resolved.
76+
const addCmd = require('./commands/files/add')
77+
const catCmd = require('./commands/files/cat')
78+
const getCmd = require('./commands/files/get')
79+
const aliases = [addCmd, catCmd, getCmd]
80+
aliases.forEach((alias) => {
81+
cli.command(alias)
82+
})
83+
9284
cli
85+
.commandDir('commands', {
86+
visit: (commandObject, pathToFile, filename) => {
87+
if (commandObject.command === 'files <command>' && enableMfs) {
88+
return null
89+
}
90+
91+
return commandObject
92+
}
93+
})
9394
.help()
9495
.strict()
9596
.completion()

src/cli/commands/dag/get.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = {
4141
// * reads as 'agree in'
4242
if (node._json) {
4343
delete node._json.multihash
44-
node._json.data = '0x' + node._json.data.toString('hex')
44+
node._json.data = node._json.data.toString('base64')
4545
print(JSON.stringify(node._json))
4646
return
4747
}

src/cli/utils.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ exports.getIPFS = (argv, callback) => {
5151
start: false,
5252
pass: argv.pass,
5353
EXPERIMENTAL: {
54-
pubsub: true
54+
pubsub: true,
55+
mfs: true
5556
}
5657
})
5758

src/core/components/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ exports.dht = require('./dht')
2323
exports.dns = require('./dns')
2424
exports.key = require('./key')
2525
exports.stats = require('./stats')
26+
exports.mfs = require('ipfs-mfs/core')

src/core/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const schema = Joi.object().keys({
1616
EXPERIMENTAL: Joi.object().keys({
1717
pubsub: Joi.boolean(),
1818
sharding: Joi.boolean(),
19-
dht: Joi.boolean()
19+
dht: Joi.boolean(),
20+
mfs: Joi.boolean()
2021
}).allow(null),
2122
config: Joi.object().keys({
2223
Addresses: Joi.object().keys({

src/core/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ class IPFS extends EventEmitter {
120120
if (this._options.EXPERIMENTAL.relay) {
121121
this.log('EXPERIMENTAL Relay is enabled')
122122
}
123+
if (this._options.EXPERIMENTAL.mfs) {
124+
this.log('EXPERIMENTAL mfs is enabled')
125+
const mfs = components.mfs(this)
126+
127+
Object.keys(mfs).forEach(key => {
128+
if (mfs.hasOwnProperty(key)) {
129+
this.files[key] = mfs[key]
130+
}
131+
})
132+
}
123133

124134
this.state = require('./state')(this)
125135

src/http/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ function HttpApi (repo, config, cliArgs) {
7171
EXPERIMENTAL: {
7272
pubsub: cliArgs && cliArgs.enablePubsubExperiment,
7373
dht: cliArgs && cliArgs.enableDhtExperiment,
74-
sharding: cliArgs && cliArgs.enableShardingExperiment
74+
sharding: cliArgs && cliArgs.enableShardingExperiment,
75+
mfs: cliArgs && cliArgs.enableMfsExperiment
7576
},
7677
libp2p: libp2p
7778
})

test/core/interface/files-mfs.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const test = require('interface-ipfs-core')
5+
const parallel = require('async/parallel')
6+
7+
const IPFS = require('../../../src')
8+
9+
const DaemonFactory = require('ipfsd-ctl')
10+
const df = DaemonFactory.create({ type: 'proc', exec: IPFS })
11+
12+
const nodes = []
13+
const common = {
14+
setup: function (callback) {
15+
callback(null, {
16+
spawnNode: (cb) => {
17+
df.spawn({
18+
initOptions: { bits: 1024 },
19+
args: ['--enable-mfs-experiment']
20+
}, (err, _ipfsd) => {
21+
if (err) {
22+
return cb(err)
23+
}
24+
25+
nodes.push(_ipfsd)
26+
cb(null, _ipfsd.api)
27+
})
28+
}
29+
})
30+
},
31+
teardown: function (callback) {
32+
parallel(nodes.map((node) => (cb) => node.stop(cb)), callback)
33+
}
34+
}
35+
36+
test.filesMFS(common)

test/core/interface/interface.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('interface-ipfs-core tests', () => {
99
require('./bootstrap')
1010
require('./config')
1111
require('./files')
12+
require('./files-mfs')
1213
require('./generic')
1314
require('./object')
1415
require('./dag')

0 commit comments

Comments
 (0)