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

Commit

Permalink
feat(factory): add ipfs factory, verify it works with object tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Aug 11, 2016
1 parent 45040b2 commit 3db096e
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 6 deletions.
4 changes: 4 additions & 0 deletions test/core/both/test-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
const test = require('interface-ipfs-core')
const IPFS = require('../../../src/core')

// let factory

const common = {
setup: function (cb) {
// TODO change to factory
const ipfs = new IPFS(require('../../utils/repo-path'))
ipfs.load(() => {
cb(null, ipfs)
})
},
teardown: function (cb) {
// factory.teardown
cb()
}
}
Expand Down
11 changes: 5 additions & 6 deletions test/core/both/test-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
'use strict'

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

const IPFS = require('../../../src/core')
let factory

const common = {
setup: function (cb) {
const ipfs = new IPFS(require('../../utils/repo-path'))
ipfs.load(() => {
cb(null, ipfs)
})
factory = new IPFSFactory()
cb(null, factory)
},
teardown: function (cb) {
cb()
factory.dismantle(cb)
}
}

Expand Down
68 changes: 68 additions & 0 deletions test/utils/factory/default-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"Identity": {
"PeerID": "",
"PrivKey": ""
},
"Datastore": {
"Type": "",
"Path": "",
"StorageMax": "",
"StorageGCWatermark": 0,
"GCPeriod": "",
"Params": null,
"NoSync": false
},
"Addresses": {
"Swarm": [
"/ip4/127.0.0.1/tcp/0"
],
"API": "/ip4/127.0.0.1/tcp/0",
"Gateway": "/ip4/127.0.0.1/tcp/0"
},
"Mounts": {
"IPFS": "/ipfs",
"IPNS": "/ipns",
"FuseAllowOther": false
},
"Version": {
"Current": "jsipfs-dev",
"Check": "error",
"CheckDate": "0001-01-01T00:00:00Z",
"CheckPeriod": "172800000000000",
"AutoUpdate": "minor"
},
"Discovery": {
"MDNS": {
"Enabled": true,
"Interval": 10
}
},
"Ipns": {
"RepublishPeriod": "",
"RecordLifetime": "",
"ResolveCacheSize": 128
},
"Bootstrap": [],
"Tour": {
"Last": ""
},
"Gateway": {
"HTTPHeaders": null,
"RootRedirect": "",
"Writable": false
},
"SupernodeRouting": {
"Servers": []
},
"API": {
"HTTPHeaders": null
},
"Swarm": {
"AddrFilters": null
},
"Log": {
"MaxSizeMB": 250,
"MaxBackups": 1,
"MaxAgeDays": 0
}
}
120 changes: 120 additions & 0 deletions test/utils/factory/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
'use strict'

const fs = require('fs')
const path = require('path')
const PeerId = require('peer-id')
const isNode = require('detect-node')
const IPFSRepo = require('ipfs-repo')
const cleanRepo = require('../clean')
const IPFS = require('../../../src/core')
const series = require('run-series')

module.exports = Factory

function Factory () {
if (!(this instanceof Factory)) {
return new Factory()
}

const nodes = []

/* yields a new started node */
this.spawnNode = (repoPath, config, callback) => {
if (typeof repoPath === 'function') {
callback = repoPath
repoPath = undefined
}
if (typeof config === 'function') {
callback = config
config = undefined
}

if (!repoPath) {
repoPath = '/tmp/.ipfs-' + Math.random()
.toString()
.substring(2, 8)
}

if (!config) {
const defaultConfigPath = path.join(__dirname, 'default-config.json')
config = JSON.parse(fs.readFileSync(defaultConfigPath).toString())
const pId = PeerId.create().toJSON()
config.Identity.PeerID = pId.id
config.Identity.PrivKey = pId.privKey
}

// set up the repo
let store
let teardown

if (isNode) {
store = require('fs-blob-store')
teardown = (done) => {
cleanRepo(repoPath)
done()
}
} else {
const idb = window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
window.msIndexedDB
store = require('idb-plus-blob-store')
teardown = (done) => {
idb.deleteDatabase(repoPath)
idb.deleteDatabase(repoPath + '/blocks')
done()
}
}

const repo = new IPFSRepo(repoPath, { stores: store })
repo.teardown = teardown

// create the IPFS node
const ipfs = new IPFS(repo)
ipfs.init({ emptyRepo: true }, (err) => {
if (err) {
return callback(err)
}
repo.config.set(config, launchNode)
})

function launchNode () {
ipfs.load((err) => {
if (err) {
return callback(err)
}

ipfs.goOnline((err) => {
if (err) {
return callback(err)
}

nodes.push({
repo: repo,
ipfs: ipfs
})

callback(null, ipfs)
})
})
}
}

this.dismantle = function (callback) {
series(nodes.map((node) => {
return node.ipfs.goOffline
}), clean)

function clean (err) {
if (err) {
return callback(err)
}
series(
nodes.map((node) => {
return node.repo.teardown
}),
callback
)
}
}
}

0 comments on commit 3db096e

Please sign in to comment.