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

Commit 259aa82

Browse files
committed
feat: mfs implementation
License: MIT Signed-off-by: achingbrain <[email protected]>
1 parent 2e40fb8 commit 259aa82

File tree

17 files changed

+129
-72
lines changed

17 files changed

+129
-72
lines changed

README.md

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 29 additions & 28 deletions
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

Lines changed: 1 addition & 1 deletion
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/commands/files/get.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function fileHandler (dir) {
3030
callback(err)
3131
} else {
3232
const fullFilePath = path.join(dir, file.path)
33+
3334
if (file.content) {
3435
file.content
3536
.pipe(fs.createWriteStream(fullFilePath))

src/cli/utils.js

Lines changed: 2 additions & 1 deletion
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: Boolean(process.env.IPFS_MFS)
5556
}
5657
})
5758

src/core/components/index.js

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 10 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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/cli/file.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
const expect = require('chai').expect
55
const runOnAndOff = require('../utils/on-and-off')
66
const file = 'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV'
7-
const dir = 'QmYmW4HiZhotsoSqnv2o1oUusvkRM8b9RweBoH7ao5nki2'
7+
const dir = 'Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z'
88

99
describe('file ls', () => runOnAndOff((thing) => {
1010
let ipfs
1111

1212
before(function () {
1313
this.timeout(50 * 1000)
1414
ipfs = thing.ipfs
15-
return ipfs('files add -r test/fixtures/test-data/recursive-get-dir')
15+
return ipfs('add -r test/fixtures/test-data/recursive-get-dir')
1616
})
1717

1818
it('prints a filename', () => {
@@ -27,9 +27,9 @@ describe('file ls', () => runOnAndOff((thing) => {
2727
return ipfs(`file ls ${dir}`)
2828
.then((out) => expect(out).to.eql(
2929
`This functionality is deprecated, and will be removed in future versions. If possible, please use 'ipfs ls' instead.\n` +
30-
'QmQQHYDwAQms78fPcvx1uFFsfho23YJNoewfLbi9AtdyJ9\n' +
30+
'QmamKEPmEH9RUsqRQsfNf5evZQDQPYL9KXg1ADeT7mkHkT\n' +
3131
'QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN\n' +
32-
'Qma13ZrhKG52MWnwtZ6fMD8jGj8d4Q9sJgn5xtKgeZw5uz\n' +
32+
'QmUqyZtPmsRy1U5Mo8kz2BAMmk1hfJ7yW1KAFTMB2odsFv\n' +
3333
'QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU\n' +
3434
'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV\n'
3535
))

0 commit comments

Comments
 (0)