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

Commit

Permalink
feat: mfs implementation
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: achingbrain <[email protected]>
  • Loading branch information
achingbrain committed May 23, 2018
1 parent 2e40fb8 commit a0fd5b9
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 34 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ Creates and returns an instance of an IPFS node. Use the `options` argument to s
- `hop` (object)
- `enabled` (boolean): Make this node a relay (other nodes can connect *through* it). (Default: `false`)
- `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`)

- `mfs` (boolean): Enables Mutable File System commands - see `jsipfs files --help` for more (Default: `false`)

- `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.

- `libp2p` (object) add custom modules to the libp2p stack of your node
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"expose-loader": "~0.7.5",
"form-data": "^2.3.2",
"hat": "~0.0.3",
"interface-ipfs-core": "~0.65.7",
"interface-ipfs-core": "~0.66.2",
"ipfsd-ctl": "~0.34.0",
"lodash": "^4.17.10",
"mocha": "^5.1.1",
Expand Down Expand Up @@ -109,6 +109,7 @@
"ipfs-bitswap": "~0.20.0",
"ipfs-block": "~0.7.1",
"ipfs-block-service": "~0.14.0",
"ipfs-mfs": "~0.0.1",
"ipfs-multipart": "~0.1.0",
"ipfs-repo": "~0.20.0",
"ipfs-unixfs": "~0.1.14",
Expand Down
57 changes: 29 additions & 28 deletions src/cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
const yargs = require('yargs')
const updateNotifier = require('update-notifier')
const readPkgUp = require('read-pkg-up')
const fs = require('fs')
const path = require('path')
const utils = require('./utils')
const print = utils.print

Expand All @@ -18,10 +16,6 @@ updateNotifier({

const args = process.argv.slice(2)

// Determine if the first argument is a sub-system command
const commandNames = fs.readdirSync(path.join(__dirname, 'commands'))
const isCommand = commandNames.includes(`${args[0]}.js`)

const cli = yargs
.option('silent', {
desc: 'Write no output',
Expand All @@ -34,14 +28,6 @@ const cli = yargs
type: 'string',
default: ''
})
.commandDir('commands', {
// Only include the commands for the sub-system we're using, or include all
// if no sub-system command has been passed.
include (path, filename) {
if (!isCommand) return true
return `${args[0]}.js` === filename
}
})
.epilog(utils.ipfsPathHelp)
.demandCommand(1)
.fail((msg, err, yargs) => {
Expand All @@ -56,27 +42,15 @@ const cli = yargs
yargs.showHelp()
})

// If not a sub-system command then load the top level aliases
if (!isCommand) {
// NOTE: This creates an alias of
// `jsipfs files {add, get, cat}` to `jsipfs {add, get, cat}`.
// This will stay until https://github.com/ipfs/specs/issues/98 is resolved.
const addCmd = require('./commands/files/add')
const catCmd = require('./commands/files/cat')
const getCmd = require('./commands/files/get')
const aliases = [addCmd, catCmd, getCmd]
aliases.forEach((alias) => {
cli.command(alias.command, alias.describe, alias.builder, alias.handler)
})
}

// Need to skip to avoid locking as these commands
// don't require a daemon
if (args[0] === 'daemon' || args[0] === 'init') {
cli
.help()
.strict()
.completion()
.command(require('./commands/daemon'))
.command(require('./commands/init'))
.parse(args)
} else {
// here we have to make a separate yargs instance with
Expand All @@ -86,10 +60,37 @@ if (args[0] === 'daemon' || args[0] === 'init') {
if (err) {
throw err
}

utils.getIPFS(argv, (err, ipfs, cleanup) => {
if (err) { throw err }

const enableMfs = ipfs._options && ipfs._options.EXPERIMENTAL && ipfs._options && ipfs._options.EXPERIMENTAL.mfs

if (enableMfs) {
require('ipfs-mfs/cli')(cli)
}

// NOTE: This creates an alias of
// `jsipfs files {add, get, cat}` to `jsipfs {add, get, cat}`.
// This will stay until https://github.com/ipfs/specs/issues/98 is resolved.
const addCmd = require('./commands/files/add')
const catCmd = require('./commands/files/cat')
const getCmd = require('./commands/files/get')
const aliases = [addCmd, catCmd, getCmd]
aliases.forEach((alias) => {
cli.command(alias)
})

cli
.commandDir('commands', {
visit: (commandObject, pathToFile, filename) => {
if (commandObject.command === 'files <command>' && enableMfs) {
return null
}

return commandObject
}
})
.help()
.strict()
.completion()
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
// * reads as 'agree in'
if (node._json) {
delete node._json.multihash
node._json.data = '0x' + node._json.data.toString('hex')
node._json.data = node._json.data.toString('base64')
print(JSON.stringify(node._json))
return
}
Expand Down
3 changes: 2 additions & 1 deletion src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ exports.getIPFS = (argv, callback) => {
start: false,
pass: argv.pass,
EXPERIMENTAL: {
pubsub: true
pubsub: true,
mfs: true
}
})

Expand Down
1 change: 1 addition & 0 deletions src/core/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ exports.dht = require('./dht')
exports.dns = require('./dns')
exports.key = require('./key')
exports.stats = require('./stats')
exports.mfs = require('ipfs-mfs/core')
3 changes: 2 additions & 1 deletion src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const schema = Joi.object().keys({
EXPERIMENTAL: Joi.object().keys({
pubsub: Joi.boolean(),
sharding: Joi.boolean(),
dht: Joi.boolean()
dht: Joi.boolean(),
mfs: Joi.boolean()
}).allow(null),
config: Joi.object().keys({
Addresses: Joi.object().keys({
Expand Down
10 changes: 10 additions & 0 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ class IPFS extends EventEmitter {
if (this._options.EXPERIMENTAL.relay) {
this.log('EXPERIMENTAL Relay is enabled')
}
if (this._options.EXPERIMENTAL.mfs) {
this.log('EXPERIMENTAL mfs is enabled')
const mfs = components.mfs(this)

Object.keys(mfs).forEach(key => {
if (mfs.hasOwnProperty(key)) {
this.files[key] = mfs[key]
}
})
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ function HttpApi (repo, config, cliArgs) {
EXPERIMENTAL: {
pubsub: cliArgs && cliArgs.enablePubsubExperiment,
dht: cliArgs && cliArgs.enableDhtExperiment,
sharding: cliArgs && cliArgs.enableShardingExperiment
sharding: cliArgs && cliArgs.enableShardingExperiment,
mfs: cliArgs && cliArgs.enableMfsExperiment
},
libp2p: libp2p
})
Expand Down
36 changes: 36 additions & 0 deletions test/core/interface/files-mfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-env mocha */
'use strict'

const test = require('interface-ipfs-core')
const parallel = require('async/parallel')

const IPFS = require('../../../src')

const DaemonFactory = require('ipfsd-ctl')
const df = DaemonFactory.create({ type: 'proc', exec: IPFS })

const nodes = []
const common = {
setup: function (callback) {
callback(null, {
spawnNode: (cb) => {
df.spawn({
initOptions: { bits: 1024 },
args: ['--enable-mfs-experiment']
}, (err, _ipfsd) => {
if (err) {
return cb(err)
}

nodes.push(_ipfsd)
cb(null, _ipfsd.api)
})
}
})
},
teardown: function (callback) {
parallel(nodes.map((node) => (cb) => node.stop(cb)), callback)
}
}

test.filesMFS(common)
1 change: 1 addition & 0 deletions test/core/interface/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('interface-ipfs-core tests', () => {
require('./bootstrap')
require('./config')
require('./files')
require('./files-mfs')
require('./generic')
require('./object')
require('./dag')
Expand Down

0 comments on commit a0fd5b9

Please sign in to comment.