diff --git a/.aegir.js b/.aegir.js index 0ca4c8882..efeced9a7 100644 --- a/.aegir.js +++ b/.aegir.js @@ -1,20 +1,12 @@ 'use strict' -const path = require('path') - module.exports = { - webpack: { - resolve: { - alias: { - 'node-forge': path.resolve( - path.dirname(require.resolve('libp2p-crypto')), - '../vendor/forge.bundle.js' - ) - } - }, - externals: { - fs: '{}', - mkdirp: '{}' - } + karma: { + files: [{ + pattern: 'node_modules/interface-ipfs-core/test/fixtures/**/*', + watched: false, + served: true, + included: false + }] } -} +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 0234febe2..7cdb316de 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ node_modules *.log test/setup/tmp-disposable-nodes-addrs.json dist -lib coverage diff --git a/.travis.yml b/.travis.yml index c2e95ab4f..ec2e62243 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,16 @@ sudo: false language: node_js -node_js: - - 4 - - 5 - - "stable" + +matrix: + include: + - node_js: 4 + env: CXX=g++-4.8 + - node_js: 6 + env: + - SAUCE=true + - CXX=g++-4.8 + - node_js: stable + env: CXX=g++-4.8 # Make sure we have new NPM. before_install: @@ -11,15 +18,20 @@ before_install: script: - npm run lint - - npm run test:node + - npm run test - npm run coverage -addons: - firefox: 'latest' - before_script: - export DISPLAY=:99.0 - sh -e /etc/init.d/xvfb start after_success: - npm run coverage-publish + +addons: + firefox: 'latest' + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-4.8 diff --git a/README.md b/README.md index 2d8d70f70..9cc2eee22 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,12 @@ ipfs-api [![Dependency Status](https://david-dm.org/ipfs/js-ipfs-api.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-api) [![Travis CI](https://travis-ci.org/ipfs/js-ipfs-api.svg?branch=master)](https://travis-ci.org/ipfs/js-ipfs-api) [![Circle CI](https://circleci.com/gh/ipfs/js-ipfs-api.svg?style=svg)](https://circleci.com/gh/ipfs/js-ipfs-api) +![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square) +![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square) + +[![Sauce Test Status](https://saucelabs.com/browser-matrix/js-ipfs-api.svg)](https://saucelabs.com/u/ipfs-js-api) + +> **Note: If you see CI red, that is due a failing test when adding nested directories in the browser, all the other features work as expect, if this is something you also need, please consider helping us identifying the solution for it, join the discussion at: https://github.com/ipfs/js-ipfs-api/issues/339** > A client library for the IPFS HTTP API, implemented in JavaScript. This client library implements the [interface-ipfs-core](https://github.com/ipfs/interface-ipfs-core) enabling applications to change between a embebed js-ipfs node and any remote IPFS node without having to change the code. In addition, this client library implements a set of utility functions. @@ -99,6 +105,8 @@ If are using this module in a browser with something like browserify, then you w ```bash $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin "[\"http://example.com\"]" +$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\"true\"]" +$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"POST\", \"GET\"]" ``` ## Usage diff --git a/package.json b/package.json index 047be97eb..5bb974025 100644 --- a/package.json +++ b/package.json @@ -2,62 +2,66 @@ "name": "ipfs-api", "version": "11.0.1", "description": "A client library for the IPFS HTTP API. Follows interface-ipfs-core spec", - "main": "lib/index.js", - "jsnext:main": "src/index.js", + "main": "src/index.js", + "browser": { + "glob": false, + "fs": false, + "stream": "readable-stream" + }, "scripts": { - "test": "PHANTOM=off node --max_old_space_size=8192 node_modules/.bin/gulp test:node", - "test:node": "PHANTOM=off gulp test:node", - "test:browser": "PHANTOM=off node --max_old_space_size=8192 node_modules/.bin/gulp test:browser", + "test": "gulp test", + "test:node": "gulp test:node", + "test:browser": "gulp test:browser", "lint": "aegir-lint", "build": "gulp build", - "release": "PHANTOM=off node --max_old_space_size=8192 node_modules/.bin/gulp release", - "release-minor": "PHANTOM=off node --max_old_space_size=8192 node_modules/.bin/gulp release --type minor", - "release-major": "PHANTOM=off node --max_old_space_size=8192 node_modules/.bin/gulp release --type major", + "release": "gulp release", + "release-minor": "gulp release --type minor", + "release-major": "gulp release --type major", "coverage": "gulp coverage", "coverage-publish": "aegir-coverage publish" }, "dependencies": { "async": "^2.1.2", - "babel-runtime": "^6.18.0", "bl": "^1.1.2", "bs58": "^3.0.0", "detect-node": "^2.0.3", "flatmap": "0.0.3", "glob": "^7.1.1", - "ipfs-block": "^0.4.0", - "ipld-dag-pb": "^0.1.3", - "is-ipfs": "^0.2.0", + "ipfs-block": "^0.5.0", + "ipld-dag-pb": "^0.8.0", + "is-ipfs": "^0.2.1", "isstream": "^0.1.2", - "multiaddr": "^2.0.2", + "multiaddr": "^2.0.3", "multipart-stream": "^2.0.1", "ndjson": "^1.4.3", - "peer-id": "^0.7.0", - "peer-info": "^0.7.1", - "promisify-es6": "^1.0.1", + "once": "^1.4.0", + "peer-id": "^0.8.0", + "peer-info": "^0.8.0", + "promisify-es6": "^1.0.2", "qs": "^6.3.0", + "readable-stream": "^1.1.14", "streamifier": "^0.1.1", "tar-stream": "^1.5.2", "wreck": "^10.0.0" }, "engines": { - "node": ">=4.2.2" + "node": ">=4.0.0" }, "repository": { "type": "git", "url": "https://github.com/ipfs/js-ipfs-api" }, "devDependencies": { - "aegir": "^8.1.2", + "aegir": "^9.1.0", "chai": "^3.5.0", "gulp": "^3.9.1", "hapi": "^15.2.0", - "interface-ipfs-core": "^0.16.1", + "interface-ipfs-core": "^0.18.0", "ipfsd-ctl": "^0.17.0", "pre-commit": "^1.1.3", "socket.io": "^1.5.1", "socket.io-client": "^1.5.1", - "stream-equal": "^0.1.8", - "stream-http": "^2.4.0" + "stream-equal": "^0.1.9" }, "pre-commit": [ "lint", @@ -103,4 +107,4 @@ "url": "https://github.com/ipfs/js-ipfs-api/issues" }, "homepage": "https://github.com/ipfs/js-ipfs-api" -} \ No newline at end of file +} diff --git a/src/add-to-dagnode-transform.js b/src/add-to-dagnode-transform.js index 5f2e65785..5566de542 100644 --- a/src/add-to-dagnode-transform.js +++ b/src/add-to-dagnode-transform.js @@ -1,6 +1,8 @@ 'use strict' const map = require('async/map') +const waterfall = require('async/waterfall') + const getDagNode = require('./get-dagnode') // transform { Hash: '...' } objects into { path: 'string', node: DAGNode } @@ -9,24 +11,18 @@ module.exports = (err, res, send, done) => { return done(err) } - map(res, (entry, next) => { - getDagNode(send, entry.Hash, (err, node) => { + map(res, (entry, next) => waterfall([ + (cb) => getDagNode(send, entry.Hash, cb), + (node, cb) => node.size((err, size) => { if (err) { - return next(err) + return cb(err) } - node.size((err, size) => { - if (err) { - return next(err) - } - const obj = { - path: entry.Name, - hash: entry.Hash, - size: size - } - next(null, obj) + + cb(null, { + path: entry.Name, + hash: entry.Hash, + size: size }) }) - }, (err, res) => { - done(err, res) - }) + ], next), done) } diff --git a/test/factory/daemon-spawner.js b/test/factory/daemon-spawner.js index f4f9f9498..86a2d808b 100644 --- a/test/factory/daemon-spawner.js +++ b/test/factory/daemon-spawner.js @@ -2,7 +2,9 @@ // const defaultConfig = require('./default-config.json') const ipfsd = require('ipfsd-ctl') -const series = require('run-series') +const series = require('async/series') +const eachSeries = require('async/eachSeries') +const once = require('once') module.exports = Factory @@ -11,7 +13,7 @@ function Factory () { return new Factory() } - const nodes = [] + let nodes = [] this.spawnNode = (repoPath, config, callback) => { if (typeof repoPath === 'function') { @@ -44,11 +46,18 @@ function Factory () { }) } - this.dismantle = function (callback) { - series( - nodes.map((node) => { - return node.stopDaemon - }), callback) + this.dismantle = (callback) => { + eachSeries(nodes, (node, cb) => { + cb = once(cb) + node.stopDaemon(cb) + }, (err) => { + if (err) { + return callback(err) + } + nodes = [] + + callback() + }) } } @@ -62,22 +71,19 @@ function spawnEphemeralNode (callback) { // doesn't work as expected series([ (cb) => { - node.setConfig('Bootstrap', null, cb) - }, - (cb) => { - node.setConfig('Discovery', '{}', cb) - }, - (cb) => { - const headers = { - HTTPHeaders: { - 'Access-Control-Allow-Origin': ['*'] - } + const configValues = { + Bootstrap: [], + Discovery: {}, + 'HTTPHeaders.Access-Control-Allow-Origin': ['*'], + 'HTTPHeaders.Access-Control-Allow-Credentials': 'true', + 'HTTPHeaders.Access-Control-Allow-Methods': ['PUT', 'POST', 'GET'] } - node.setConfig('API', JSON.stringify(headers), cb) + + eachSeries(Object.keys(configValues), (configKey, cb) => { + node.setConfig(`API.${configKey}`, JSON.stringify(configValues[configKey]), cb) + }, cb) }, - (cb) => { - node.startDaemon(cb) - } + (cb) => node.startDaemon(cb) ], (err) => { if (err) { return callback(err) diff --git a/test/data/15mb.random b/test/fixtures/15mb.random similarity index 100% rename from test/data/15mb.random rename to test/fixtures/15mb.random diff --git a/test/data/r-config.json b/test/fixtures/r-config.json similarity index 100% rename from test/data/r-config.json rename to test/fixtures/r-config.json diff --git a/test/data/test-folder/add.js b/test/fixtures/test-folder/add similarity index 100% rename from test/data/test-folder/add.js rename to test/fixtures/test-folder/add diff --git a/test/data/test-folder/cat.js b/test/fixtures/test-folder/cat similarity index 100% rename from test/data/test-folder/cat.js rename to test/fixtures/test-folder/cat diff --git a/test/data/test-folder/files/hello.txt b/test/fixtures/test-folder/files/hello.txt similarity index 100% rename from test/data/test-folder/files/hello.txt rename to test/fixtures/test-folder/files/hello.txt diff --git a/test/data/test-folder/files/ipfs.txt b/test/fixtures/test-folder/files/ipfs.txt similarity index 100% rename from test/data/test-folder/files/ipfs.txt rename to test/fixtures/test-folder/files/ipfs.txt diff --git a/test/data/test-folder/hello-link b/test/fixtures/test-folder/hello-link similarity index 100% rename from test/data/test-folder/hello-link rename to test/fixtures/test-folder/hello-link diff --git a/test/data/test-folder/ipfs-add.js b/test/fixtures/test-folder/ipfs-add similarity index 100% rename from test/data/test-folder/ipfs-add.js rename to test/fixtures/test-folder/ipfs-add diff --git a/test/data/test-folder/ls.js b/test/fixtures/test-folder/ls similarity index 100% rename from test/data/test-folder/ls.js rename to test/fixtures/test-folder/ls diff --git a/test/data/test-folder/version.js b/test/fixtures/test-folder/version similarity index 100% rename from test/data/test-folder/version.js rename to test/fixtures/test-folder/version diff --git a/test/data/testconfig.json b/test/fixtures/testconfig.json similarity index 100% rename from test/data/testconfig.json rename to test/fixtures/testconfig.json diff --git a/test/data/testfile.txt b/test/fixtures/testfile.txt similarity index 100% rename from test/data/testfile.txt rename to test/fixtures/testfile.txt diff --git a/test/interface-ipfs-core/files.spec.js b/test/interface-ipfs-core/files.spec.js index bed5c1506..43e181ce7 100644 --- a/test/interface-ipfs-core/files.spec.js +++ b/test/interface-ipfs-core/files.spec.js @@ -4,11 +4,17 @@ const expect = require('chai').expect const isNode = require('detect-node') -const path = require('path') const test = require('interface-ipfs-core') -const fs = require('fs') +const loadFixture = require('aegir/fixtures') + const FactoryClient = require('../factory/factory-client') -const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt')) + +let testfile +if (isNode) { + testfile = loadFixture(__dirname, '../fixtures/testfile.txt') +} else { + testfile = loadFixture(__dirname, 'fixtures/testfile.txt') +} // add, cat, get and ls tests from interface-ipfs-core let fc diff --git a/test/interface-ipfs-core/get.spec.js b/test/interface-ipfs-core/get.spec.js index b989811c7..fe6d22505 100644 --- a/test/interface-ipfs-core/get.spec.js +++ b/test/interface-ipfs-core/get.spec.js @@ -10,15 +10,18 @@ const concat = require('concat-stream') const through = require('through2') const streamEqual = require('stream-equal') const path = require('path') +const loadFixture = require('aegir/fixtures') const FactoryClient = require('../factory/factory-client') -const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt')) - +let testfile let testfileBig let tfbPath if (isNode) { - tfbPath = path.join(__dirname, '/../data/15mb.random') + tfbPath = path.join(__dirname, '../fixtures/15mb.random') testfileBig = fs.createReadStream(tfbPath, { bufferSize: 128 }) + testfile = loadFixture(__dirname, '../fixtures/testfile.txt') +} else { + testfile = loadFixture(__dirname, 'fixtures/testfile.txt') } describe('.get', () => { diff --git a/test/interface-ipfs-core/ls.spec.js b/test/interface-ipfs-core/ls.spec.js index 44d60fc5b..fdb5be93e 100644 --- a/test/interface-ipfs-core/ls.spec.js +++ b/test/interface-ipfs-core/ls.spec.js @@ -24,12 +24,12 @@ describe('ls', function () { (cb) => fc.spawnNode(cb), (node, cb) => { ipfs = node - const filesPath = path.join(__dirname, '../data/test-folder') + const filesPath = path.join(__dirname, '../fixtures/test-folder') ipfs.util.addFromFs(filesPath, { recursive: true }, cb) }, (hashes, cb) => { folder = hashes[hashes.length - 1].hash - expect(folder).to.be.eql('QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6') + expect(folder).to.be.eql('QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh') cb() } ], done) @@ -45,7 +45,7 @@ describe('ls', function () { expect(res).to.have.a.property('Objects') expect(res.Objects[0]).to.have.a.property('Links') - expect(res.Objects[0]).to.have.property('Hash', 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6') + expect(res.Objects[0]).to.have.property('Hash', 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh') done() }) }) @@ -72,7 +72,7 @@ describe('ls', function () { .then((res) => { expect(res).to.have.a.property('Objects') expect(res.Objects[0]).to.have.a.property('Links') - expect(res.Objects[0]).to.have.property('Hash', 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6') + expect(res.Objects[0]).to.have.property('Hash', 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh') }) }) diff --git a/test/interface-ipfs-core/name.spec.js b/test/interface-ipfs-core/name.spec.js index 4e3e9194f..20de8f012 100644 --- a/test/interface-ipfs-core/name.spec.js +++ b/test/interface-ipfs-core/name.spec.js @@ -3,10 +3,15 @@ 'use strict' const expect = require('chai').expect -const fs = require('fs') -const path = require('path') +const isNode = require('detect-node') +const loadFixture = require('aegir/fixtures') -const testfile = fs.readFileSync(path.join(__dirname, '/../data/testfile.txt')) +let testfile +if (isNode) { + testfile = loadFixture(__dirname, '../fixtures/testfile.txt') +} else { + testfile = loadFixture(__dirname, 'fixtures/testfile.txt') +} describe('.name', () => { let name diff --git a/test/interface-ipfs-core/refs.spec.js b/test/interface-ipfs-core/refs.spec.js index ea658e736..5b33662b0 100644 --- a/test/interface-ipfs-core/refs.spec.js +++ b/test/interface-ipfs-core/refs.spec.js @@ -23,12 +23,12 @@ describe('.refs', () => { (cb) => fc.spawnNode(cb), (node, cb) => { ipfs = node - const filesPath = path.join(__dirname, '../data/test-folder') + const filesPath = path.join(__dirname, '../fixtures/test-folder') ipfs.util.addFromFs(filesPath, { recursive: true }, cb) }, (hashes, cb) => { folder = hashes[hashes.length - 1].hash - expect(folder).to.be.eql('QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6') + expect(folder).to.be.eql('QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh') cb() } ], done) @@ -39,25 +39,25 @@ describe('.refs', () => { }) const result = [{ - Ref: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6 QmcUYKmQxmTcFom4R4UZP7FWeQzgJkwcFn51XrvsMy7PE9 add.js', + Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmcUYKmQxmTcFom4R4UZP7FWeQzgJkwcFn51XrvsMy7PE9 add', Err: '' }, { - Ref: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6 QmNeHxDfQfjVFyYj2iruvysLH9zpp78v3cu1s3BZq1j5hY cat.js', + Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmNeHxDfQfjVFyYj2iruvysLH9zpp78v3cu1s3BZq1j5hY cat', Err: '' }, { - Ref: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6 QmTYFLz5vsdMpq4XXw1a1pSxujJc9Z5V3Aw1Qg64d849Zy files', + Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmTYFLz5vsdMpq4XXw1a1pSxujJc9Z5V3Aw1Qg64d849Zy files', Err: '' }, { - Ref: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6 QmY9cxiHqTFoWamkQVkpmmqzBrY3hCBEL2XNu3NtX74Fuu hello-link', + Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmY9cxiHqTFoWamkQVkpmmqzBrY3hCBEL2XNu3NtX74Fuu hello-link', Err: '' }, { - Ref: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6 QmU7wetVaAqc3Meurif9hcYBHGvQmL5QdpPJYBoZizyTNL ipfs-add.js', + Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmU7wetVaAqc3Meurif9hcYBHGvQmL5QdpPJYBoZizyTNL ipfs-add', Err: '' }, { - Ref: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6 QmctZfSuegbi2TMFY2y3VQjxsH5JbRBu7XmiLfHNvshhio ls.js', + Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmctZfSuegbi2TMFY2y3VQjxsH5JbRBu7XmiLfHNvshhio ls', Err: '' }, { - Ref: 'QmRNjDeKStKGTQXnJ2NFqeQ9oW23WcpbmvCVrpDHgDg3T6 QmbkMNB6rwfYAxRvnG9CWJ6cKKHEdq2ZKTozyF5FQ7H8Rs version.js', + Ref: 'QmQao3KNcpCsdXaLGpjieFGMfXzsSXgsf6Rnc5dJJA3QMh QmbkMNB6rwfYAxRvnG9CWJ6cKKHEdq2ZKTozyF5FQ7H8Rs version', Err: '' }] diff --git a/test/ipfs-api/request-api.spec.js b/test/ipfs-api/request-api.spec.js index 79219b7d7..e362a255f 100644 --- a/test/ipfs-api/request-api.spec.js +++ b/test/ipfs-api/request-api.spec.js @@ -57,7 +57,7 @@ describe('ipfsAPI request tests', () => { res.end() }).listen(6001, () => { ipfsAPI('/ip4/127.0.0.1/tcp/6001') - .config.replace('test/data/r-config.json', (err) => { + .config.replace('test/fixtures/r-config.json', (err) => { expect(err).to.not.exist server.close(done) }) diff --git a/test/ipfs-api/util.spec.js b/test/ipfs-api/util.spec.js index b4453a5c8..e0990a9d1 100644 --- a/test/ipfs-api/util.spec.js +++ b/test/ipfs-api/util.spec.js @@ -31,7 +31,7 @@ describe('.util', () => { }) it('.streamAdd', (done) => { - const tfpath = path.join(__dirname, '/../data/testfile.txt') + const tfpath = path.join(__dirname, '/../fixtures/testfile.txt') const rs = fs.createReadStream(tfpath) rs.path = '' // clean the path for testing purposes @@ -43,7 +43,7 @@ describe('.util', () => { }) it('.fsAdd a directory', (done) => { - const filesPath = path.join(__dirname, '../data/test-folder') + const filesPath = path.join(__dirname, '../fixtures/test-folder') ipfs.util.addFromFs(filesPath, { recursive: true }, (err, result) => { expect(err).to.not.exist expect(result.length).to.be.above(8) @@ -52,7 +52,7 @@ describe('.util', () => { }) it('.fsAdd a file', (done) => { - const filePath = path.join(__dirname, '../data/testfile.txt') + const filePath = path.join(__dirname, '../fixtures/testfile.txt') ipfs.util.addFromFs(filePath, (err, result) => { expect(err).to.not.exist expect(result.length).to.be.above(5) diff --git a/test/setup/spawn-daemons.js b/test/setup/spawn-daemons.js index 986073732..5a2d3b659 100644 --- a/test/setup/spawn-daemons.js +++ b/test/setup/spawn-daemons.js @@ -6,6 +6,8 @@ const gulp = require('gulp') const fs = require('fs') const path = require('path') const ipfsd = require('ipfsd-ctl') +const eachSeries = require('async/eachSeries') +const parallel = require('async/parallel') let daemons @@ -13,82 +15,60 @@ function startDisposableDaemons (callback) { // a, b, c const ipfsNodes = {} - // a, b, c - const apiAddrs = {} - - let counter = 0 - - function finish () { - counter++ - if (counter === 3) { - const targetPath = path.join(__dirname, '/tmp-disposable-nodes-addrs.json') - fs.writeFileSync(targetPath, JSON.stringify(apiAddrs)) - callback(ipfsNodes) - } - } - - function startIndependentNode (nodes, addrs, key, cb) { + function startIndependentNode (nodes, key, cb) { ipfsd.disposable((err, node) => { if (err) { - throw err + return cb(err) } nodes[key] = node console.log(' ipfs init done - (bootstrap and mdns off) - ' + key) - nodes[key].setConfig('Bootstrap', null, (err) => { + const configValues = { + Bootstrap: [], + Discovery: {}, + 'HTTPHeaders.Access-Control-Allow-Origin': ['*'], + 'HTTPHeaders.Access-Control-Allow-Credentials': 'true', + 'HTTPHeaders.Access-Control-Allow-Methods': ['PUT', 'POST', 'GET'] + } + + eachSeries(Object.keys(configValues), (configKey, cb) => { + nodes[key].setConfig(`API.${configKey}`, JSON.stringify(configValues[configKey]), cb) + }, (err) => { if (err) { - throw err + return cb(err) } - nodes[key].setConfig('Discovery', '{}', (err) => { - if (err) { - throw err - } - - const headers = { - HTTPHeaders: { - 'Access-Control-Allow-Origin': ['*'] - } - } - nodes[key].setConfig('API', JSON.stringify(headers), (err) => { - if (err) { - throw err - } - nodes[key].startDaemon((err, ignore) => { - if (err) { - throw err - } - - addrs[key] = nodes[key].apiAddr - cb() - }) - }) - }) + nodes[key].startDaemon(cb) }) }) } - startIndependentNode(ipfsNodes, apiAddrs, 'a', finish) - startIndependentNode(ipfsNodes, apiAddrs, 'b', finish) - startIndependentNode(ipfsNodes, apiAddrs, 'c', finish) + parallel([ + (cb) => startIndependentNode(ipfsNodes, 'a', cb), + (cb) => startIndependentNode(ipfsNodes, 'b', cb), + (cb) => startIndependentNode(ipfsNodes, 'c', cb) + ], (err) => { + if (err) { + return callback(err) + } + const targetPath = path.join(__dirname, '/tmp-disposable-nodes-addrs.json') + fs.writeFileSync(targetPath, JSON.stringify({ + a: ipfsNodes.a.apiAddr, + b: ipfsNodes.b.apiAddr, + c: ipfsNodes.c.apiAddr + })) + callback(null, ipfsNodes) + }) } function stopDisposableDaemons (ds, callback) { - let counter = 0 - function finish () { - counter++ - if (counter === 3) { - callback() - } - } - - function stopIPFSNode (list, key, cb) { + function stopIPFSNode (node, cb) { let nodeStopped - list[key].stopDaemon((err) => { + node.stopDaemon((err) => { if (err) { - throw err + return cb(err) } if (!nodeStopped) { nodeStopped = true @@ -97,20 +77,28 @@ function stopDisposableDaemons (ds, callback) { }) } - stopIPFSNode(ds, 'a', finish) - stopIPFSNode(ds, 'b', finish) - stopIPFSNode(ds, 'c', finish) + parallel([ + (cb) => stopIPFSNode(ds.a, cb), + (cb) => stopIPFSNode(ds.b, cb), + (cb) => stopIPFSNode(ds.c, cb) + ], callback) } gulp.task('daemons:start', (done) => { - startDisposableDaemons((d) => { + startDisposableDaemons((err, d) => { + if (err) { + return done(err) + } daemons = d done() }) }) gulp.task('daemons:stop', (done) => { - stopDisposableDaemons(daemons, () => { + stopDisposableDaemons(daemons, (err) => { + if (err) { + return done(err) + } daemons = null done() }) diff --git a/test/tmp-disposable-nodes-addrs.json b/test/tmp-disposable-nodes-addrs.json deleted file mode 100644 index 851a00021..000000000 --- a/test/tmp-disposable-nodes-addrs.json +++ /dev/null @@ -1 +0,0 @@ -{"c":"/ip4/127.0.0.1/tcp/56459","a":"/ip4/127.0.0.1/tcp/56466","b":"/ip4/127.0.0.1/tcp/56479"} \ No newline at end of file