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

Commit 09740c2

Browse files
authored
Merge pull request #407 from ipfs/feat/ipfs-factory
feat(factory): add ipfs factory, verify it works with object tests
2 parents 45040b2 + 50c9f7c commit 09740c2

File tree

8 files changed

+206
-18
lines changed

8 files changed

+206
-18
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"form-data": "^1.0.0-rc4",
4848
"gulp": "^3.9.1",
4949
"idb-plus-blob-store": "^1.1.2",
50-
"interface-ipfs-core": "^0.6.0",
50+
"interface-ipfs-core": "^0.7.2",
5151
"left-pad": "^1.1.1",
5252
"lodash": "^4.14.1",
5353
"ncp": "^2.0.0",
@@ -121,4 +121,4 @@
121121
"kumavis <[email protected]>",
122122
"nginnever <[email protected]>"
123123
]
124-
}
124+
}

test/cli/test-version.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const nexpect = require('nexpect')
66
const HttpAPI = require('../../src/http-api')
77
const repoPath = require('./index').repoPath
88
const _ = require('lodash')
9+
const pkgversion = require('../../package.json').version
910

1011
describe('version', () => {
1112
const env = _.clone(process.env)
@@ -16,7 +17,7 @@ describe('version', () => {
1617
nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version'], {env})
1718
.run((err, stdout, exitcode) => {
1819
expect(err).to.not.exist
19-
expect(stdout[0]).to.equal('js-ipfs version: 0.14.1')
20+
expect(stdout[0]).to.equal('js-ipfs version: ' + pkgversion)
2021
expect(exitcode).to.equal(0)
2122
done()
2223
})
@@ -46,7 +47,7 @@ describe('version', () => {
4647
.run((err, stdout, exitcode) => {
4748
expect(err).to.not.exist
4849
expect(exitcode).to.equal(0)
49-
expect(stdout[0]).to.equal('js-ipfs version: 0.14.1')
50+
expect(stdout[0]).to.equal('js-ipfs version: ' + pkgversion)
5051
done()
5152
})
5253
})

test/core/both/test-files.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
'use strict'
33

44
const test = require('interface-ipfs-core')
5-
const IPFS = require('../../../src/core')
5+
const IPFSFactory = require('../../utils/factory')
6+
7+
let factory
68

79
const common = {
810
setup: function (cb) {
9-
const ipfs = new IPFS(require('../../utils/repo-path'))
10-
ipfs.load(() => {
11-
cb(null, ipfs)
12-
})
11+
factory = new IPFSFactory()
12+
cb(null, factory)
1313
},
1414
teardown: function (cb) {
15-
cb()
15+
factory.dismantle(cb)
1616
}
1717
}
1818

test/core/both/test-object.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
'use strict'
44

55
const test = require('interface-ipfs-core')
6+
const IPFSFactory = require('../../utils/factory')
67

7-
const IPFS = require('../../../src/core')
8+
let factory
89

910
const common = {
1011
setup: function (cb) {
11-
const ipfs = new IPFS(require('../../utils/repo-path'))
12-
ipfs.load(() => {
13-
cb(null, ipfs)
14-
})
12+
factory = new IPFSFactory()
13+
cb(null, factory)
1514
},
1615
teardown: function (cb) {
17-
cb()
16+
factory.dismantle(cb)
1817
}
1918
}
2019

test/core/both/test-version.js

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

44
const expect = require('chai').expect
5+
const pkgversion = require('../../../package.json').version
56

67
const IPFS = require('../../../src/core')
78

@@ -16,7 +17,7 @@ describe('version', () => {
1617
it('get version', (done) => {
1718
ipfs.version((err, version) => {
1819
expect(err).to.not.exist
19-
expect(version).to.equal('0.14.1')
20+
expect(version).to.equal(pkgversion)
2021
done()
2122
})
2223
})

test/http-api/test-version.js

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

44
const expect = require('chai').expect
55
const APIctl = require('ipfs-api')
6+
const pkgversion = require('../../package.json').version
67

78
module.exports = (httpAPI) => {
89
describe('version', () => {
@@ -18,7 +19,7 @@ module.exports = (httpAPI) => {
1819
method: 'GET',
1920
url: '/api/v0/version'
2021
}, (res) => {
21-
expect(res.result.Version).to.equal('0.14.1')
22+
expect(res.result.Version).to.equal(pkgversion)
2223
expect(res.result).to.have.a.property('Commit')
2324
expect(res.result).to.have.a.property('Repo')
2425
done()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"Identity": {
3+
"PeerID": "",
4+
"PrivKey": ""
5+
},
6+
"Datastore": {
7+
"Type": "",
8+
"Path": "",
9+
"StorageMax": "",
10+
"StorageGCWatermark": 0,
11+
"GCPeriod": "",
12+
"Params": null,
13+
"NoSync": false
14+
},
15+
"Addresses": {
16+
"Swarm": [
17+
"/ip4/127.0.0.1/tcp/0"
18+
],
19+
"API": "/ip4/127.0.0.1/tcp/0",
20+
"Gateway": "/ip4/127.0.0.1/tcp/0"
21+
},
22+
"Mounts": {
23+
"IPFS": "/ipfs",
24+
"IPNS": "/ipns",
25+
"FuseAllowOther": false
26+
},
27+
"Version": {
28+
"Current": "jsipfs-dev",
29+
"Check": "error",
30+
"CheckDate": "0001-01-01T00:00:00Z",
31+
"CheckPeriod": "172800000000000",
32+
"AutoUpdate": "minor"
33+
},
34+
"Discovery": {
35+
"MDNS": {
36+
"Enabled": true,
37+
"Interval": 10
38+
}
39+
},
40+
"Ipns": {
41+
"RepublishPeriod": "",
42+
"RecordLifetime": "",
43+
"ResolveCacheSize": 128
44+
},
45+
"Bootstrap": [],
46+
"Tour": {
47+
"Last": ""
48+
},
49+
"Gateway": {
50+
"HTTPHeaders": null,
51+
"RootRedirect": "",
52+
"Writable": false
53+
},
54+
"SupernodeRouting": {
55+
"Servers": []
56+
},
57+
"API": {
58+
"HTTPHeaders": null
59+
},
60+
"Swarm": {
61+
"AddrFilters": null
62+
},
63+
"Log": {
64+
"MaxSizeMB": 250,
65+
"MaxBackups": 1,
66+
"MaxAgeDays": 0
67+
}
68+
}

test/utils/factory/index.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
'use strict'
2+
3+
const PeerId = require('peer-id')
4+
const isNode = require('detect-node')
5+
const IPFSRepo = require('ipfs-repo')
6+
const cleanRepo = require('../clean')
7+
const IPFS = require('../../../src/core')
8+
const series = require('run-series')
9+
const defaultConfig = require('./default-config.json')
10+
11+
module.exports = Factory
12+
13+
function Factory () {
14+
if (!(this instanceof Factory)) {
15+
return new Factory()
16+
}
17+
18+
const nodes = []
19+
20+
/* yields a new started node */
21+
this.spawnNode = (repoPath, config, callback) => {
22+
if (typeof repoPath === 'function') {
23+
callback = repoPath
24+
repoPath = undefined
25+
}
26+
if (typeof config === 'function') {
27+
callback = config
28+
config = undefined
29+
}
30+
31+
if (!repoPath) {
32+
repoPath = '/tmp/.ipfs-' + Math.random()
33+
.toString()
34+
.substring(2, 8)
35+
}
36+
37+
if (!config) {
38+
config = JSON.parse(JSON.stringify(defaultConfig))
39+
const pId = PeerId.create({ bits: 32 }).toJSON()
40+
config.Identity.PeerID = pId.id
41+
config.Identity.PrivKey = pId.privKey
42+
}
43+
44+
// set up the repo
45+
let store
46+
let teardown
47+
48+
if (isNode) {
49+
store = require('fs-blob-store')
50+
teardown = (done) => {
51+
cleanRepo(repoPath)
52+
done()
53+
}
54+
} else {
55+
const idb = window.indexedDB ||
56+
window.mozIndexedDB ||
57+
window.webkitIndexedDB ||
58+
window.msIndexedDB
59+
store = require('idb-plus-blob-store')
60+
teardown = (done) => {
61+
idb.deleteDatabase(repoPath)
62+
idb.deleteDatabase(repoPath + '/blocks')
63+
done()
64+
}
65+
}
66+
67+
const repo = new IPFSRepo(repoPath, { stores: store })
68+
repo.teardown = teardown
69+
70+
// create the IPFS node
71+
const ipfs = new IPFS(repo)
72+
ipfs.init({ emptyRepo: true }, (err) => {
73+
if (err) {
74+
return callback(err)
75+
}
76+
repo.config.set(config, launchNode)
77+
})
78+
79+
function launchNode () {
80+
ipfs.load((err) => {
81+
if (err) {
82+
return callback(err)
83+
}
84+
85+
ipfs.goOnline((err) => {
86+
if (err) {
87+
return callback(err)
88+
}
89+
90+
nodes.push({
91+
repo: repo,
92+
ipfs: ipfs
93+
})
94+
95+
callback(null, ipfs)
96+
})
97+
})
98+
}
99+
}
100+
101+
this.dismantle = function (callback) {
102+
series(nodes.map((node) => {
103+
return node.ipfs.goOffline
104+
}), clean)
105+
106+
function clean (err) {
107+
if (err) {
108+
return callback(err)
109+
}
110+
series(
111+
nodes.map((node) => {
112+
return node.repo.teardown
113+
}),
114+
callback
115+
)
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)