Skip to content

Commit b7ef098

Browse files
committed
Revert "feat: let js-ipfs create a repo for itself, no need to import ipfs-repo"
This reverts commit 42597c1.
1 parent 42597c1 commit b7ef098

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"hapi": "^16.6.2",
8787
"hat": "0.0.3",
8888
"ipfs-api": "^21.0.0",
89+
"ipfs-repo": "~0.20.0",
8990
"joi": "^13.1.2",
9091
"lodash.clone": "^4.5.0",
9192
"lodash.defaults": "^4.2.0",

src/ipfsd-in-proc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multiaddr = require('multiaddr')
44
const defaultsDeep = require('lodash.defaultsdeep')
5+
const createRepo = require('./utils/repo/create-nodejs')
56
const defaults = require('lodash.defaults')
67
const waterfall = require('async/waterfall')
78
const debug = require('debug')
@@ -30,6 +31,7 @@ class Node extends EventEmitter {
3031

3132
this.opts.args = this.opts.args || []
3233
this.path = this.opts.repoPath
34+
this.repo = createRepo(this.path)
3335
this.disposable = this.opts.disposable
3436
this.clean = true
3537
this._apiAddr = null
@@ -62,7 +64,7 @@ class Node extends EventEmitter {
6264
})
6365

6466
this.exec = new IPFS({
65-
repo: this.path,
67+
repo: this.repo,
6668
init: false,
6769
start: false,
6870
pass: this.opts.pass,

src/utils/repo/create-browser.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* global self */
2+
'use strict'
3+
4+
const IPFSRepo = require('ipfs-repo')
5+
const hat = require('hat')
6+
7+
const idb = self.indexedDB ||
8+
self.mozIndexedDB ||
9+
self.webkitIndexedDB ||
10+
self.msIndexedDB
11+
12+
function createTempRepo (repoPath) {
13+
repoPath = repoPath || '/ipfs-' + hat()
14+
15+
const repo = new IPFSRepo(repoPath)
16+
17+
repo.teardown = (done) => {
18+
repo.close(() => {
19+
idb.deleteDatabase(repoPath)
20+
idb.deleteDatabase(repoPath + '/blocks')
21+
done()
22+
})
23+
}
24+
25+
return repo
26+
}
27+
28+
module.exports = createTempRepo

src/utils/repo/create-nodejs.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict'
2+
3+
const IPFSRepo = require('ipfs-repo')
4+
const os = require('os')
5+
const path = require('path')
6+
const hat = require('hat')
7+
const series = require('async/series')
8+
const rimraf = require('rimraf')
9+
const fs = require('fs')
10+
11+
const clean = (dir) => {
12+
try {
13+
fs.accessSync(dir)
14+
} catch (err) {
15+
// Does not exist so all good
16+
return
17+
}
18+
19+
rimraf.sync(dir)
20+
}
21+
22+
function createTempRepo (repoPath) {
23+
repoPath = repoPath || path.join(os.tmpdir(), '/ipfs-test-' + hat())
24+
25+
const repo = new IPFSRepo(repoPath)
26+
27+
repo.teardown = (done) => {
28+
series([
29+
// ignore err, might have been closed already
30+
(cb) => repo.close(() => cb()),
31+
(cb) => {
32+
clean(repoPath)
33+
cb()
34+
}
35+
], done)
36+
}
37+
38+
return repo
39+
}
40+
41+
module.exports = createTempRepo

test/utils.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const path = require('path')
1313
const flatten = require('../src/utils/flatten')
1414
const tempDir = require('../src/utils/tmp-dir')
1515
const findIpfsExecutable = require('../src/utils/find-ipfs-executable')
16+
const createRepo = require('../src/utils/repo/create-nodejs')
1617

1718
const IPFSRepo = require('ipfs-repo')
1819

@@ -62,5 +63,25 @@ describe('utils', () => {
6263
expect(fs.existsSync(execPath)).to.be.ok()
6364
})
6465
})
66+
67+
describe('.createRepo', () => {
68+
let repo = null
69+
let repoPath = tempDir()
70+
71+
it('should create repo', () => {
72+
repo = createRepo(repoPath)
73+
expect(repo).to.exist()
74+
expect(repo).to.be.instanceOf(IPFSRepo)
75+
expect(fs.existsSync(repoPath)).to.be.ok()
76+
})
77+
78+
it('should cleanup repo', (done) => {
79+
repo.teardown((err) => {
80+
expect(err).to.not.exist()
81+
expect(!fs.existsSync(repoPath)).to.be.ok()
82+
done()
83+
})
84+
})
85+
})
6586
}
6687
})

0 commit comments

Comments
 (0)