This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(factory): add ipfs factory, verify it works with object tests
- Loading branch information
Showing
4 changed files
with
197 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} | ||
} |