Skip to content

Commit 60e0da7

Browse files
dignifiedquiredaviddias
authored andcommitted
fix: add backwards compatibility and more tests (#138)
* fix: backwards compatibility
1 parent 9220fda commit 60e0da7

8 files changed

+131
-77
lines changed

package.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"description": "IPFS Repo implementation",
55
"main": "src/index.js",
66
"browser": {
7+
"rimraf": false,
8+
"datastore-fs": "datastore-level",
9+
"leveldown": "level-js",
710
"./src/lock.js": "./src/lock-memory.js",
811
"./src/default-options.js": "./src/default-options-browser.js"
912
},
@@ -40,8 +43,8 @@
4043
},
4144
"devDependencies": {
4245
"aegir": "^11.0.2",
43-
"chai": "^4.0.1",
44-
"dirty-chai": "^1.2.2",
46+
"chai": "^4.0.2",
47+
"dirty-chai": "^2.0.0",
4548
"lodash": "^4.17.4",
4649
"memdown": "^1.2.4",
4750
"multihashes": "~0.4.5",
@@ -50,7 +53,7 @@
5053
"rimraf": "^2.6.1"
5154
},
5255
"dependencies": {
53-
"async": "^2.4.1",
56+
"async": "^2.5.0",
5457
"base32.js": "^0.1.0",
5558
"cids": "^0.5.0",
5659
"datastore-core": "^0.2.0",
@@ -60,10 +63,10 @@
6063
"interface-datastore": "^0.2.2",
6164
"ipfs-block": "~0.6.0",
6265
"level-js": "timkuijsten/level.js#idbunwrapper",
63-
"leveldown": "^1.7.1",
66+
"leveldown": "^1.7.2",
6467
"lock-me": "^1.0.2",
6568
"multiaddr": "^2.3.0",
66-
"safe-buffer": "^5.1.0"
69+
"safe-buffer": "^5.1.1"
6770
},
6871
"license": "MIT",
6972
"contributors": [
@@ -73,7 +76,6 @@
7376
"Francisco Baio Dias <[email protected]>",
7477
"Friedel Ziegelmayer <[email protected]>",
7578
"Greenkeeper <[email protected]>",
76-
"Justin Chase <[email protected]>",
7779
"Lars-Magnus Skog <[email protected]>",
7880
"Pau Ramon Revilla <[email protected]>",
7981
"Richard Littauer <[email protected]>",
@@ -82,4 +84,4 @@
8284
"nginnever <[email protected]>",
8385
"npmcdn-to-unpkg-bot <[email protected]>"
8486
]
85-
}
87+
}

src/default-options-browser.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
// Default configuration for a repo in the browser
44
module.exports = {
5-
blockStore: require('datastore-level'),
6-
blockStoreOptions: { db: require('level-js') },
7-
dataStore: require('datastore-level'),
8-
dataStoreOptions: { db: require('level-js') },
9-
sharding: false
5+
fs: require('datastore-level'),
6+
sharding: false,
7+
lock: 'memory',
8+
fsOptions: {
9+
db: require('level-js')
10+
},
11+
level: require('level-js')
1012
}

src/default-options.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
// Default configuration for a repo in node.js
44
module.exports = {
5-
blockStore: require('datastore-fs'),
6-
dataStore: require('datastore-level'),
7-
dataStoreOptions: { db: require('leveldown') }
5+
sharding: true,
6+
lock: 'fs',
7+
fs: require('datastore-fs'),
8+
level: require('leveldown')
89
}

src/index.js

+34-36
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const MountStore = core.MountDatastore
55
const ShardingStore = core.ShardingDatastore
66

77
const Key = require('interface-datastore').Key
8+
const LevelStore = require('datastore-level')
89
const waterfall = require('async/waterfall')
910
const series = require('async/series')
1011
const parallel = require('async/parallel')
@@ -17,12 +18,13 @@ const debug = require('debug')
1718
const version = require('./version')
1819
const config = require('./config')
1920
const blockstore = require('./blockstore')
21+
const defaultOptions = require('./default-options')
2022

2123
const log = debug('repo')
2224

2325
const apiFile = new Key('api')
24-
const blockStoreDirectory = 'blocks'
25-
const dataStoreDirectory = 'datastore'
26+
const flatfsDirectory = 'blocks'
27+
const levelDirectory = 'datastore'
2628
const repoVersion = 5
2729

2830
/**
@@ -33,27 +35,27 @@ class IpfsRepo {
3335
/**
3436
* @param {string} repoPath - path where the repo is stored
3537
* @param {object} options - Configuration
36-
* @param {datastore} options.blockStore
37-
* @param {datastore} options.dataStore
38-
* @param {object} [options.blockStoreOptions={}]
39-
* @param {object} [options.dataStoreOptions={}]
38+
* @param {Datastore} options.fs
39+
* @param {Leveldown} options.level
40+
* @param {object} [options.fsOptions={}]
4041
* @param {bool} [options.sharding=true] - Enable sharding (flatfs on disk), not needed in the browser.
4142
* @param {string} [options.lock='fs'] - Either `fs` or `memory`.
4243
*/
4344
constructor (repoPath, options) {
4445
assert.equal(typeof repoPath, 'string', 'missing repoPath')
4546

47+
this.options = Object.assign({}, defaultOptions, options)
48+
4649
this.closed = true
4750
this.path = repoPath
48-
this.options = Object.assign({ lock: 'memory', sharding: true },
49-
options, require('./default-options'))
50-
51-
const BlockStore = this.options.blockStore
52-
this._blockStore = new BlockStore(this.path,
53-
Object.assign(this.options.blockStoreOptions || {}, { extension: '' }))
51+
const FsStore = this.options.fs
52+
const fsOptions = Object.assign({}, this.options.fsOptions, {
53+
extension: ''
54+
})
55+
this._fsStore = new FsStore(this.path, fsOptions)
5456

55-
this.version = version(this._blockStore)
56-
this.config = config(this._blockStore)
57+
this.version = version(this._fsStore)
58+
this.config = config(this._fsStore)
5759

5860
if (this.options.lock === 'memory') {
5961
this._locker = require('./lock-memory')
@@ -75,7 +77,7 @@ class IpfsRepo {
7577
log('initializing at: %s', this.path)
7678

7779
series([
78-
(cb) => this._blockStore.open((err) => {
80+
(cb) => this._fsStore.open((err) => {
7981
if (err && err.message === 'Already open') {
8082
return cb()
8183
}
@@ -101,7 +103,7 @@ class IpfsRepo {
101103

102104
// check if the repo is already initialized
103105
waterfall([
104-
(cb) => this._blockStore.open((err) => {
106+
(cb) => this._fsStore.open((err) => {
105107
if (err && err.message === 'Already open') {
106108
return cb()
107109
}
@@ -114,8 +116,8 @@ class IpfsRepo {
114116
this.lockfile = lck
115117

116118
log('creating flatfs')
117-
const BlockStore = this.options.blockStore
118-
const s = new BlockStore(path.join(this.path, blockStoreDirectory), this.options.blockStoreOptions)
119+
const FsStore = this.options.fs
120+
const s = new FsStore(path.join(this.path, flatfsDirectory), Object.assign({}, this.options.fsOptions))
119121

120122
if (this.options.sharding) {
121123
const shard = new core.shard.NextToLast(2)
@@ -124,21 +126,17 @@ class IpfsRepo {
124126
cb(null, s)
125127
}
126128
},
127-
(blockStore, cb) => {
129+
(flatfs, cb) => {
128130
log('Flatfs store opened')
129-
const DataStore = this.options.dataStore
130-
const dataStore = new DataStore(path.join(this.path, dataStoreDirectory), this.options.dataStoreOptions)
131-
log(dataStore)
132-
this.store = new MountStore([
133-
{
134-
prefix: new Key(blockStoreDirectory),
135-
datastore: blockStore
136-
},
137-
{
138-
prefix: new Key('/'),
139-
datastore: dataStore
140-
}
141-
])
131+
this.store = new MountStore([{
132+
prefix: new Key(flatfsDirectory),
133+
datastore: flatfs
134+
}, {
135+
prefix: new Key('/'),
136+
datastore: new LevelStore(path.join(this.path, levelDirectory), {
137+
db: this.options.level
138+
})
139+
}])
142140

143141
this.blockstore = blockstore(this)
144142
this.closed = false
@@ -194,14 +192,14 @@ class IpfsRepo {
194192

195193
log('closing at: %s', this.path)
196194
series([
197-
(cb) => this._blockStore.delete(apiFile, (err) => {
195+
(cb) => this._fsStore.delete(apiFile, (err) => {
198196
if (err && err.message.startsWith('ENOENT')) {
199197
return cb()
200198
}
201199
cb(err)
202200
}),
203201
(cb) => this.store.close(cb),
204-
(cb) => this._blockStore.close(cb),
202+
(cb) => this._fsStore.close(cb),
205203
(cb) => {
206204
log('unlocking')
207205
this.closed = true
@@ -232,7 +230,7 @@ class IpfsRepo {
232230
* @returns {void}
233231
*/
234232
setApiAddress (addr, callback) {
235-
this._blockStore.put(apiFile, Buffer.from(addr.toString()), callback)
233+
this._fsStore.put(apiFile, Buffer.from(addr.toString()), callback)
236234
}
237235

238236
/**
@@ -242,7 +240,7 @@ class IpfsRepo {
242240
* @returns {void}
243241
*/
244242
apiAddress (callback) {
245-
this._blockStore.get(apiFile, (err, rawAddr) => {
243+
this._fsStore.get(apiFile, (err, rawAddr) => {
246244
if (err) {
247245
return callback(err)
248246
}

test/browser.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const series = require('async/series')
77
const IPFSRepo = require('../src')
88

99
describe('IPFS Repo Tests on the Browser', () => {
10+
require('./options-test')
1011
const repo = new IPFSRepo('myrepo')
1112

1213
before((done) => {

test/node.js

+24-15
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,37 @@ const expect = chai.expect
1313
const IPFSRepo = require('../src')
1414

1515
describe('IPFS Repo Tests on on Node.js', () => {
16-
const repos = [
17-
{
18-
name: 'default',
19-
opts: undefined
16+
require('./options-test')
17+
18+
const repos = [{
19+
name: 'default',
20+
opts: undefined,
21+
init: false
22+
}, {
23+
name: 'memory',
24+
opts: {
25+
fs: require('interface-datastore').MemoryDatastore,
26+
level: require('memdown'),
27+
lock: 'memory'
2028
},
21-
{
22-
name: 'memory',
23-
opts: {
24-
blockStore: require('interface-datastore').MemoryDatastore,
25-
dataStore: require('interface-datastore').MemoryDatastore
26-
}
27-
}
28-
]
29+
init: true
30+
}]
2931
repos.forEach((r) => describe(r.name, () => {
3032
const testRepoPath = path.join(__dirname, 'test-repo')
3133
const date = Date.now().toString()
3234
const repoPath = testRepoPath + '-for-' + date
35+
3336
const repo = new IPFSRepo(repoPath, r.opts)
37+
3438
before((done) => {
3539
series([
36-
(cb) => ncp(testRepoPath, repoPath, cb),
37-
(cb) => repo.init({}, cb),
40+
(cb) => {
41+
if (r.init) {
42+
repo.init({}, cb)
43+
} else {
44+
ncp(testRepoPath, repoPath, cb)
45+
}
46+
},
3847
(cb) => repo.open(cb)
3948
], done)
4049
})
@@ -66,7 +75,7 @@ describe('IPFS Repo Tests on on Node.js', () => {
6675
require('./repo-test')(repo)
6776
require('./blockstore-test')(repo)
6877
require('./datastore-test')(repo)
69-
if (r.name === 'default') {
78+
if (!r.init) {
7079
require('./interop-test')(repo)
7180
}
7281
}))

test/options-test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
chai.use(require('dirty-chai'))
6+
const expect = chai.expect
7+
const path = require('path')
8+
const rimraf = require('rimraf')
9+
if (!rimraf.sync) {
10+
// browser
11+
rimraf.sync = noop
12+
}
13+
const Repo = require('../')
14+
15+
describe('IPFS Repo options Tests', () => {
16+
const repoPath = path.join(__dirname, 'slash', 'path')
17+
after(() => {
18+
rimraf.sync(repoPath)
19+
})
20+
21+
it('missing repoPath', () => {
22+
expect(
23+
() => new Repo()
24+
).to.throw('missing repoPath')
25+
})
26+
27+
it('default options', () => {
28+
const repo = new Repo(repoPath)
29+
expect(repo.options).to.deep.equal(expectedRepoOptions())
30+
})
31+
})
32+
33+
function noop () {}
34+
35+
function expectedRepoOptions () {
36+
const options = {
37+
// packages are exchanged to browser-compatible
38+
// equivalents via package.browser.
39+
fs: require('datastore-fs'),
40+
level: require('leveldown'),
41+
lock: process.browser ? 'memory' : 'fs',
42+
sharding: !process.browser
43+
}
44+
45+
if (process.browser) {
46+
options.fsOptions = {
47+
db: require('leveldown')
48+
}
49+
}
50+
51+
return options
52+
}

test/repo-test.js

-11
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,8 @@ const expect = chai.expect
77
const series = require('async/series')
88
const waterfall = require('async/waterfall')
99

10-
const Repo = require('../src')
11-
1210
module.exports = (repo) => {
1311
describe('IPFS Repo Tests', () => {
14-
describe('new', () => {
15-
it('missing arguments', () => {
16-
expect(
17-
() => new Repo()
18-
).to.throw(Error)
19-
})
20-
})
21-
2212
it('check if Repo exists', (done) => {
2313
repo.exists((err, exists) => {
2414
expect(err).to.not.exist()
@@ -83,7 +73,6 @@ module.exports = (repo) => {
8373
(cb) => repo.open(cb),
8474
(cb) => repo.version.get(cb),
8575
(version, cb) => {
86-
console.log(version)
8776
expect(version).to.exist()
8877
cb()
8978
}

0 commit comments

Comments
 (0)