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

Commit 1d12ffb

Browse files
AuHauAlan Shaw
authored andcommitted
feat: integrate ipfs-repo-migrations tool (#2527)
Resolves: #1115 Supersedes: #2044 Depends on: * [x] ipfs/js-ipfs-repo#202
1 parent b852460 commit 1d12ffb

40 files changed

+548
-28
lines changed

.aegir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const preloadNode = MockPreloadNode.createNode()
1111
const echoServer = EchoServer.createServer()
1212

1313
module.exports = {
14-
bundlesize: { maxSize: '685kB' },
14+
bundlesize: { maxSize: '650kB' },
1515
webpack: {
1616
resolve: {
1717
mainFields: ['browser', 'main'],

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ Example:
284284
const node = await IPFS.create({ repo: '/var/ipfs/data' })
285285
```
286286

287+
##### `options.repoAutoMigrate`
288+
289+
| Type | Default |
290+
|------|---------|
291+
| boolean | `true` |
292+
293+
`js-ipfs` comes bundled with a tool that automatically migrates your IPFS repository when a new version is available.
294+
295+
**For apps that build on top of `js-ipfs` and run in the browser environment, be aware that disabling automatic
296+
migrations leaves the user with no way to run the migrations because there is no CLI in the browser. In such
297+
a case, you should provide a way to trigger migrations manually.**
298+
287299
##### `options.init`
288300

289301
| Type | Default |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"ipfs-http-response": "~0.4.0",
105105
"ipfs-mfs": "^0.13.0",
106106
"ipfs-multipart": "^0.2.0",
107-
"ipfs-repo": "^0.28.1",
107+
"ipfs-repo": "^0.29.0",
108108
"ipfs-unixfs": "~0.1.16",
109109
"ipfs-unixfs-exporter": "^0.38.0",
110110
"ipfs-unixfs-importer": "^0.40.0",

src/cli/bin.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ if (!semver.satisfies(process.versions.node, pkg.engines.node)) {
2626
const YargsPromise = require('yargs-promise')
2727
const updateNotifier = require('update-notifier')
2828
const debug = require('debug')('ipfs:cli')
29+
const { errors: { InvalidRepoVersionError } } = require('ipfs-repo')
2930
const parser = require('./parser')
3031
const commandAlias = require('./command-alias')
3132
const { print } = require('./utils')
@@ -50,13 +51,19 @@ cli
5051
})
5152
.catch(({ error, argv }) => {
5253
getIpfs = argv && argv.getIpfs
54+
55+
if (error.code === InvalidRepoVersionError.code) {
56+
error.message = 'Incompatible repo version. Migration needed. Pass --migrate for automatic migration'
57+
}
58+
5359
if (error.message) {
5460
print(error.message)
5561
debug(error)
5662
} else {
5763
print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output')
5864
debug(error)
5965
}
66+
6067
process.exit(1)
6168
})
6269
.finally(() => {

src/cli/commands/daemon.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ module.exports = {
7373
config,
7474
silent: argv.silent,
7575
repo: process.env.IPFS_PATH,
76+
repoAutoMigrate: argv.migrate,
7677
offline: argv.offline,
7778
pass: argv.pass,
7879
preload: { enabled: argv.enablePreload },
@@ -96,10 +97,8 @@ module.exports = {
9697
print(`Web UI available at ${toUri(apiServer.info.ma)}/webui`)
9798
})
9899
} catch (err) {
99-
if (err.code === 'ENOENT' && err.message.match(/uninitialized/i)) {
100-
print('Error: no initialized ipfs repo found in ' + repoPath)
101-
print('please run: jsipfs init')
102-
process.exit(1)
100+
if (err.code === 'ERR_REPO_NOT_INITIALIZED' || err.message.match(/uninitialized/i)) {
101+
err.message = 'no initialized ipfs repo found in ' + repoPath + '\nplease run: jsipfs init'
103102
}
104103
throw err
105104
}

src/cli/daemon.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,8 @@ class Daemon {
5454
}
5555

5656
// start the daemon
57-
const ipfsOpts = Object.assign({ }, { init: true, start: true, libp2p }, this._options)
58-
const ipfs = new IPFS(ipfsOpts)
59-
60-
await new Promise((resolve, reject) => {
61-
ipfs.once('error', err => {
62-
this._log('error starting core', err)
63-
err.code = 'ENOENT'
64-
reject(err)
65-
})
66-
ipfs.once('start', resolve)
67-
})
57+
const ipfsOpts = Object.assign({}, { init: true, start: true, libp2p }, this._options)
58+
const ipfs = await IPFS.create(ipfsOpts)
6859

6960
this._ipfs = ipfs
7061

src/cli/parser.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const parser = yargs
1919
type: 'string',
2020
default: ''
2121
})
22+
.option('migrate', {
23+
desc: 'Enable/disable automatic repo migrations',
24+
type: 'boolean',
25+
default: false
26+
})
2227
.epilog(utils.ipfsPathHelp)
2328
.demandCommand(1)
2429
.fail((msg, err, yargs) => {

src/cli/utils.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ const fs = require('fs')
44
const os = require('os')
55
const multiaddr = require('multiaddr')
66
const path = require('path')
7-
const debug = require('debug')
8-
const log = debug('cli')
9-
log.error = debug('cli:error')
7+
const log = require('debug')('ipfs:cli:utils')
108
const Progress = require('progress')
119
const byteman = require('byteman')
1210
const promisify = require('promisify-es6')
@@ -47,6 +45,7 @@ exports.getIPFS = (argv, callback) => {
4745
const IPFS = require('../core')
4846
const node = new IPFS({
4947
silent: argv.silent,
48+
repoAutoMigrate: argv.migrate,
5049
repo: exports.getRepoPath(),
5150
init: false,
5251
start: false,
@@ -60,7 +59,7 @@ exports.getIPFS = (argv, callback) => {
6059
})
6160

6261
node.on('error', (err) => {
63-
throw err
62+
callback(err)
6463
})
6564

6665
node.once('ready', () => {

src/core/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const s = superstruct({
2828
const configSchema = s({
2929
repo: optional(s('object|string')),
3030
repoOwner: 'boolean?',
31+
repoAutoMigrate: 'boolean?',
3132
preload: s({
3233
enabled: 'boolean?',
3334
addresses: optional(s(['multiaddr'])),

src/core/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class IPFS extends EventEmitter {
7070

7171
if (typeof options.repo === 'string' ||
7272
options.repo === undefined) {
73-
this._repo = defaultRepo(options.repo)
73+
this._repo = defaultRepo(options)
7474
} else {
7575
this._repo = options.repo
7676
}

0 commit comments

Comments
 (0)