From 46ed8fa397021beba6d1abd35dbe02ddf996e749 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 9 May 2018 01:07:48 -0600 Subject: [PATCH 01/12] feat: dont call spawn cb more than once on error --- package.json | 2 +- src/factory-in-proc.js | 8 ++++++-- test/spawn-options.spec.js | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 197204c1..e8c2ca9c 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,6 @@ "lodash.defaults": "^4.2.0", "lodash.defaultsdeep": "^4.6.0", "multiaddr": "^4.0.0", - "once": "^1.4.0", "readable-stream": "^2.3.6", "rimraf": "^2.6.2", "safe-json-parse": "^4.0.0", @@ -113,6 +112,7 @@ "ipfs": "~0.28.2", "is-running": "1.0.5", "mkdirp": "^0.5.1", + "once": "^1.4.0", "proxyquire": "^2.0.1", "sinon": "^4.5.0", "superagent-mocker": "^0.5.2", diff --git a/src/factory-in-proc.js b/src/factory-in-proc.js index 25e6d70c..4f2dc1f7 100644 --- a/src/factory-in-proc.js +++ b/src/factory-in-proc.js @@ -130,7 +130,8 @@ class FactoryInProc { } const node = new Node(options) - node.once('error', err => callback(err, node)) + const errHandler = (err) => callback(err, node) + node.once('error', errHandler) series([ (cb) => node.once('ready', cb), @@ -146,7 +147,10 @@ class FactoryInProc { (cb) => options.start ? node.start(options.args, cb) : cb() - ], (err) => callback(err, node)) + ], (err) => { + node.removeListener('error', errHandler) + callback(err, node) + }) } } diff --git a/test/spawn-options.spec.js b/test/spawn-options.spec.js index 8228d96d..c546b62e 100644 --- a/test/spawn-options.spec.js +++ b/test/spawn-options.spec.js @@ -13,10 +13,11 @@ const isNode = require('detect-node') const hat = require('hat') const IPFSFactory = require('../src') const JSIPFS = require('ipfs') +const once = require('once') const tests = [ - { type: 'go', bits: 1024 }, - { type: 'js', bits: 512 }, + // { type: 'go', bits: 1024 }, + // { type: 'js', bits: 512 }, { type: 'proc', exec: JSIPFS, bits: 512 } ] @@ -160,7 +161,6 @@ describe('Spawn options', function () { }) }) - // TODO ?? What is this test trying to prove? describe('spawn a node and attach api', () => { let ipfsd @@ -440,5 +440,35 @@ describe('Spawn options', function () { }) }) }) + + describe(`don't callback twice on error`, () => { + if (!isNode && fOpts.type !== 'proc') { return } + it('spawn with error', (done) => { + this.timeout(20 * 1000) + // `once.strict` should throw if its called more than once + const callback = once.strict((err, ipfsd) => { + if (err) { return done(err) } + + ipfsd.once('error', () => {}) // avoid EventEmitter throws + + // Do an operation, just to make sure we're working + ipfsd.api.id((err) => { + if (err) { + return done(err) + } + + // Do something to make stopping fail + ipfsd.exec._repo.close((err) => { + if (err) { return done(err) } + ipfsd.stop((err) => { + expect(err).to.exist() + done() + }) + }) + }) + }) + f.spawn(callback) + }) + }) })) }) From 1f0b7361a2219b8e4f375eb907560485c2a17000 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 9 May 2018 09:58:12 -0600 Subject: [PATCH 02/12] fix: add flag to signal callback was already called --- src/factory-in-proc.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/factory-in-proc.js b/src/factory-in-proc.js index 4f2dc1f7..62abb767 100644 --- a/src/factory-in-proc.js +++ b/src/factory-in-proc.js @@ -130,7 +130,11 @@ class FactoryInProc { } const node = new Node(options) - const errHandler = (err) => callback(err, node) + let called = false + const errHandler = (err) => { + called = true + callback(err, node) + } node.once('error', errHandler) series([ @@ -149,7 +153,7 @@ class FactoryInProc { : cb() ], (err) => { node.removeListener('error', errHandler) - callback(err, node) + if (!called) { callback(err, node) } }) } } From e0fc12465672939bd3ef6ff97c6dc1a7ea33d3fe Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Wed, 9 May 2018 10:03:22 -0600 Subject: [PATCH 03/12] docs: adding TODO --- src/ipfsd-in-proc.js | 2 ++ test/spawn-options.spec.js | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ipfsd-in-proc.js b/src/ipfsd-in-proc.js index 981b1afd..43d271e1 100644 --- a/src/ipfsd-in-proc.js +++ b/src/ipfsd-in-proc.js @@ -72,6 +72,8 @@ class Node extends EventEmitter { libp2p: this.opts.libp2p }) + // TODO: should this be wrapped in a process.nextTick(), for context: + // https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#why-use-process-nexttick this.exec.once('error', err => this.emit('error', err)) this.exec.once('ready', () => this.emit('ready')) } diff --git a/test/spawn-options.spec.js b/test/spawn-options.spec.js index c546b62e..35753106 100644 --- a/test/spawn-options.spec.js +++ b/test/spawn-options.spec.js @@ -16,8 +16,8 @@ const JSIPFS = require('ipfs') const once = require('once') const tests = [ - // { type: 'go', bits: 1024 }, - // { type: 'js', bits: 512 }, + { type: 'go', bits: 1024 }, + { type: 'js', bits: 512 }, { type: 'proc', exec: JSIPFS, bits: 512 } ] From fd13f5acca60de276dd2604735fe13437b7e236c Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 14 May 2018 20:33:22 +0100 Subject: [PATCH 04/12] chore: fix deps semvers --- package.json | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index e8c2ca9c..d8cf5974 100644 --- a/package.json +++ b/package.json @@ -86,17 +86,17 @@ "hapi": "^16.6.2", "hat": "0.0.3", "ipfs-api": "^21.0.0", - "ipfs-repo": "^0.19.0", + "ipfs-repo": "~0.20.0", "joi": "^13.1.2", "lodash.clone": "^4.5.0", "lodash.defaults": "^4.2.0", "lodash.defaultsdeep": "^4.6.0", - "multiaddr": "^4.0.0", + "multiaddr": "^5.0.0", "readable-stream": "^2.3.6", "rimraf": "^2.6.2", "safe-json-parse": "^4.0.0", "safe-json-stringify": "^1.1.0", - "shutdown": "^0.3.0", + "shutdown": "~0.3.0", "stream-http": "^2.8.1", "subcomandante": "^1.0.5", "superagent": "^3.8.2", @@ -115,7 +115,7 @@ "once": "^1.4.0", "proxyquire": "^2.0.1", "sinon": "^4.5.0", - "superagent-mocker": "^0.5.2", + "superagent-mocker": "~0.5.2", "supertest": "^3.0.0" }, "repository": { @@ -125,9 +125,5 @@ "bugs": { "url": "https://github.com/ipfs/js-ipfsd-ctl/issues" }, - "homepage": "https://github.com/ipfs/js-ipfsd-ctl", - "directories": { - "example": "examples", - "test": "test" - } + "homepage": "https://github.com/ipfs/js-ipfsd-ctl" } From 825f74aa6a2217eceda71803553368b8c19c4476 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 14 May 2018 20:56:14 +0100 Subject: [PATCH 05/12] test: skip inProc for add-retrieve until js-ipfs 0-29 --- test/add-retrieve.spec.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/add-retrieve.spec.js b/test/add-retrieve.spec.js index c40057de..2efb332b 100644 --- a/test/add-retrieve.spec.js +++ b/test/add-retrieve.spec.js @@ -12,8 +12,10 @@ const JSIPFS = require('ipfs') const tests = [ { type: 'go', bits: 1024 }, - { type: 'js', bits: 512 }, - { type: 'proc', exec: JSIPFS, bits: 512 } + { type: 'js', bits: 512 } + // TODO: Enable again once js-ipfs 0.29 is released + // A weird dependency wrangling is breaking a isBlock call + // { type: 'proc', exec: JSIPFS, bits: 512 } ] describe('data can be put and fetched', () => { From 42597c17d3fa9d0ade378ab019d7e18216904691 Mon Sep 17 00:00:00 2001 From: David Dias Date: Mon, 14 May 2018 21:06:16 +0100 Subject: [PATCH 06/12] feat: let js-ipfs create a repo for itself, no need to import ipfs-repo --- package.json | 1 - src/ipfsd-in-proc.js | 4 +--- src/utils/repo/create-browser.js | 28 ---------------------- src/utils/repo/create-nodejs.js | 41 -------------------------------- test/utils.spec.js | 21 ---------------- 5 files changed, 1 insertion(+), 94 deletions(-) delete mode 100644 src/utils/repo/create-browser.js delete mode 100644 src/utils/repo/create-nodejs.js diff --git a/package.json b/package.json index d8cf5974..72f9e81c 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,6 @@ "hapi": "^16.6.2", "hat": "0.0.3", "ipfs-api": "^21.0.0", - "ipfs-repo": "~0.20.0", "joi": "^13.1.2", "lodash.clone": "^4.5.0", "lodash.defaults": "^4.2.0", diff --git a/src/ipfsd-in-proc.js b/src/ipfsd-in-proc.js index 43d271e1..cec9ab68 100644 --- a/src/ipfsd-in-proc.js +++ b/src/ipfsd-in-proc.js @@ -2,7 +2,6 @@ const multiaddr = require('multiaddr') const defaultsDeep = require('lodash.defaultsdeep') -const createRepo = require('./utils/repo/create-nodejs') const defaults = require('lodash.defaults') const waterfall = require('async/waterfall') const debug = require('debug') @@ -31,7 +30,6 @@ class Node extends EventEmitter { this.opts.args = this.opts.args || [] this.path = this.opts.repoPath - this.repo = createRepo(this.path) this.disposable = this.opts.disposable this.clean = true this._apiAddr = null @@ -64,7 +62,7 @@ class Node extends EventEmitter { }) this.exec = new IPFS({ - repo: this.repo, + repo: this.path, init: false, start: false, pass: this.opts.pass, diff --git a/src/utils/repo/create-browser.js b/src/utils/repo/create-browser.js deleted file mode 100644 index 18b220b7..00000000 --- a/src/utils/repo/create-browser.js +++ /dev/null @@ -1,28 +0,0 @@ -/* global self */ -'use strict' - -const IPFSRepo = require('ipfs-repo') -const hat = require('hat') - -const idb = self.indexedDB || - self.mozIndexedDB || - self.webkitIndexedDB || - self.msIndexedDB - -function createTempRepo (repoPath) { - repoPath = repoPath || '/ipfs-' + hat() - - const repo = new IPFSRepo(repoPath) - - repo.teardown = (done) => { - repo.close(() => { - idb.deleteDatabase(repoPath) - idb.deleteDatabase(repoPath + '/blocks') - done() - }) - } - - return repo -} - -module.exports = createTempRepo diff --git a/src/utils/repo/create-nodejs.js b/src/utils/repo/create-nodejs.js deleted file mode 100644 index 79d91fe3..00000000 --- a/src/utils/repo/create-nodejs.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict' - -const IPFSRepo = require('ipfs-repo') -const os = require('os') -const path = require('path') -const hat = require('hat') -const series = require('async/series') -const rimraf = require('rimraf') -const fs = require('fs') - -const clean = (dir) => { - try { - fs.accessSync(dir) - } catch (err) { - // Does not exist so all good - return - } - - rimraf.sync(dir) -} - -function createTempRepo (repoPath) { - repoPath = repoPath || path.join(os.tmpdir(), '/ipfs-test-' + hat()) - - const repo = new IPFSRepo(repoPath) - - repo.teardown = (done) => { - series([ - // ignore err, might have been closed already - (cb) => repo.close(() => cb()), - (cb) => { - clean(repoPath) - cb() - } - ], done) - } - - return repo -} - -module.exports = createTempRepo diff --git a/test/utils.spec.js b/test/utils.spec.js index 091191ee..5c29ca4c 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -13,7 +13,6 @@ const path = require('path') const flatten = require('../src/utils/flatten') const tempDir = require('../src/utils/tmp-dir') const findIpfsExecutable = require('../src/utils/find-ipfs-executable') -const createRepo = require('../src/utils/repo/create-nodejs') const IPFSRepo = require('ipfs-repo') @@ -63,25 +62,5 @@ describe('utils', () => { expect(fs.existsSync(execPath)).to.be.ok() }) }) - - describe('.createRepo', () => { - let repo = null - let repoPath = tempDir() - - it('should create repo', () => { - repo = createRepo(repoPath) - expect(repo).to.exist() - expect(repo).to.be.instanceOf(IPFSRepo) - expect(fs.existsSync(repoPath)).to.be.ok() - }) - - it('should cleanup repo', (done) => { - repo.teardown((err) => { - expect(err).to.not.exist() - expect(!fs.existsSync(repoPath)).to.be.ok() - done() - }) - }) - }) } }) From b7ef098eab862de26cd723acd0c9ce598ff725c4 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 14 May 2018 15:39:39 -0600 Subject: [PATCH 07/12] Revert "feat: let js-ipfs create a repo for itself, no need to import ipfs-repo" This reverts commit 42597c17d3fa9d0ade378ab019d7e18216904691. --- package.json | 1 + src/ipfsd-in-proc.js | 4 +++- src/utils/repo/create-browser.js | 28 ++++++++++++++++++++++ src/utils/repo/create-nodejs.js | 41 ++++++++++++++++++++++++++++++++ test/utils.spec.js | 21 ++++++++++++++++ 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/utils/repo/create-browser.js create mode 100644 src/utils/repo/create-nodejs.js diff --git a/package.json b/package.json index 72f9e81c..d8cf5974 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "hapi": "^16.6.2", "hat": "0.0.3", "ipfs-api": "^21.0.0", + "ipfs-repo": "~0.20.0", "joi": "^13.1.2", "lodash.clone": "^4.5.0", "lodash.defaults": "^4.2.0", diff --git a/src/ipfsd-in-proc.js b/src/ipfsd-in-proc.js index cec9ab68..43d271e1 100644 --- a/src/ipfsd-in-proc.js +++ b/src/ipfsd-in-proc.js @@ -2,6 +2,7 @@ const multiaddr = require('multiaddr') const defaultsDeep = require('lodash.defaultsdeep') +const createRepo = require('./utils/repo/create-nodejs') const defaults = require('lodash.defaults') const waterfall = require('async/waterfall') const debug = require('debug') @@ -30,6 +31,7 @@ class Node extends EventEmitter { this.opts.args = this.opts.args || [] this.path = this.opts.repoPath + this.repo = createRepo(this.path) this.disposable = this.opts.disposable this.clean = true this._apiAddr = null @@ -62,7 +64,7 @@ class Node extends EventEmitter { }) this.exec = new IPFS({ - repo: this.path, + repo: this.repo, init: false, start: false, pass: this.opts.pass, diff --git a/src/utils/repo/create-browser.js b/src/utils/repo/create-browser.js new file mode 100644 index 00000000..18b220b7 --- /dev/null +++ b/src/utils/repo/create-browser.js @@ -0,0 +1,28 @@ +/* global self */ +'use strict' + +const IPFSRepo = require('ipfs-repo') +const hat = require('hat') + +const idb = self.indexedDB || + self.mozIndexedDB || + self.webkitIndexedDB || + self.msIndexedDB + +function createTempRepo (repoPath) { + repoPath = repoPath || '/ipfs-' + hat() + + const repo = new IPFSRepo(repoPath) + + repo.teardown = (done) => { + repo.close(() => { + idb.deleteDatabase(repoPath) + idb.deleteDatabase(repoPath + '/blocks') + done() + }) + } + + return repo +} + +module.exports = createTempRepo diff --git a/src/utils/repo/create-nodejs.js b/src/utils/repo/create-nodejs.js new file mode 100644 index 00000000..79d91fe3 --- /dev/null +++ b/src/utils/repo/create-nodejs.js @@ -0,0 +1,41 @@ +'use strict' + +const IPFSRepo = require('ipfs-repo') +const os = require('os') +const path = require('path') +const hat = require('hat') +const series = require('async/series') +const rimraf = require('rimraf') +const fs = require('fs') + +const clean = (dir) => { + try { + fs.accessSync(dir) + } catch (err) { + // Does not exist so all good + return + } + + rimraf.sync(dir) +} + +function createTempRepo (repoPath) { + repoPath = repoPath || path.join(os.tmpdir(), '/ipfs-test-' + hat()) + + const repo = new IPFSRepo(repoPath) + + repo.teardown = (done) => { + series([ + // ignore err, might have been closed already + (cb) => repo.close(() => cb()), + (cb) => { + clean(repoPath) + cb() + } + ], done) + } + + return repo +} + +module.exports = createTempRepo diff --git a/test/utils.spec.js b/test/utils.spec.js index 5c29ca4c..091191ee 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -13,6 +13,7 @@ const path = require('path') const flatten = require('../src/utils/flatten') const tempDir = require('../src/utils/tmp-dir') const findIpfsExecutable = require('../src/utils/find-ipfs-executable') +const createRepo = require('../src/utils/repo/create-nodejs') const IPFSRepo = require('ipfs-repo') @@ -62,5 +63,25 @@ describe('utils', () => { expect(fs.existsSync(execPath)).to.be.ok() }) }) + + describe('.createRepo', () => { + let repo = null + let repoPath = tempDir() + + it('should create repo', () => { + repo = createRepo(repoPath) + expect(repo).to.exist() + expect(repo).to.be.instanceOf(IPFSRepo) + expect(fs.existsSync(repoPath)).to.be.ok() + }) + + it('should cleanup repo', (done) => { + repo.teardown((err) => { + expect(err).to.not.exist() + expect(!fs.existsSync(repoPath)).to.be.ok() + done() + }) + }) + }) } }) From 21b9850ae35e389736b6e43be5470fac0b8cc063 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 14 May 2018 15:52:39 -0600 Subject: [PATCH 08/12] fix: use once to call cb once --- package.json | 4 ++-- src/factory-in-proc.js | 19 +++++++++---------- test/spawn-options.spec.js | 3 ++- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index d8cf5974..f0580a1b 100644 --- a/package.json +++ b/package.json @@ -100,7 +100,8 @@ "stream-http": "^2.8.1", "subcomandante": "^1.0.5", "superagent": "^3.8.2", - "truthy": "0.0.1" + "truthy": "0.0.1", + "once": "^1.4.0" }, "devDependencies": { "aegir": "^13.0.6", @@ -112,7 +113,6 @@ "ipfs": "~0.28.2", "is-running": "1.0.5", "mkdirp": "^0.5.1", - "once": "^1.4.0", "proxyquire": "^2.0.1", "sinon": "^4.5.0", "superagent-mocker": "~0.5.2", diff --git a/src/factory-in-proc.js b/src/factory-in-proc.js index 62abb767..cdfcef04 100644 --- a/src/factory-in-proc.js +++ b/src/factory-in-proc.js @@ -5,6 +5,7 @@ const clone = require('lodash.clone') const series = require('async/series') const path = require('path') const tmpDir = require('./utils/tmp-dir') +const once = require('once') const Node = require('./ipfsd-in-proc') const defaultConfig = require('./defaults/config') @@ -130,12 +131,13 @@ class FactoryInProc { } const node = new Node(options) - let called = false - const errHandler = (err) => { - called = true - callback(err, node) - } - node.once('error', errHandler) + const callbackOnce = once((err) => { + if (err) { + return callback(err) + } + callback(null, node) + }) + node.once('error', callbackOnce) series([ (cb) => node.once('ready', cb), @@ -151,10 +153,7 @@ class FactoryInProc { (cb) => options.start ? node.start(options.args, cb) : cb() - ], (err) => { - node.removeListener('error', errHandler) - if (!called) { callback(err, node) } - }) + ], callbackOnce) } } diff --git a/test/spawn-options.spec.js b/test/spawn-options.spec.js index 35753106..60d993f9 100644 --- a/test/spawn-options.spec.js +++ b/test/spawn-options.spec.js @@ -8,6 +8,7 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) + const fs = require('fs') const isNode = require('detect-node') const hat = require('hat') @@ -442,7 +443,7 @@ describe('Spawn options', function () { }) describe(`don't callback twice on error`, () => { - if (!isNode && fOpts.type !== 'proc') { return } + if (fOpts.type !== 'proc') { return } it('spawn with error', (done) => { this.timeout(20 * 1000) // `once.strict` should throw if its called more than once From 0ed2e96e363f6412f078c49925e3fdb7353a6934 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 14 May 2018 16:06:05 -0600 Subject: [PATCH 09/12] test: skip in proc tests untill js-ipfs 0.29 release is out --- test/add-retrieve.spec.js | 2 +- test/spawn-options.spec.js | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/add-retrieve.spec.js b/test/add-retrieve.spec.js index 2efb332b..dd2470c9 100644 --- a/test/add-retrieve.spec.js +++ b/test/add-retrieve.spec.js @@ -8,7 +8,7 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) const IPFSFactory = require('../src') -const JSIPFS = require('ipfs') +// const JSIPFS = require('ipfs') const tests = [ { type: 'go', bits: 1024 }, diff --git a/test/spawn-options.spec.js b/test/spawn-options.spec.js index 60d993f9..b9c326f6 100644 --- a/test/spawn-options.spec.js +++ b/test/spawn-options.spec.js @@ -13,13 +13,15 @@ const fs = require('fs') const isNode = require('detect-node') const hat = require('hat') const IPFSFactory = require('../src') -const JSIPFS = require('ipfs') +// const JSIPFS = require('ipfs') const once = require('once') const tests = [ { type: 'go', bits: 1024 }, - { type: 'js', bits: 512 }, - { type: 'proc', exec: JSIPFS, bits: 512 } + { type: 'js', bits: 512 } + // TODO: Enable again once js-ipfs 0.29 is released + // A weird dependency wrangling is breaking a isBlock call + // { type: 'proc', exec: JSIPFS, bits: 512 } ] const jsVersion = require('ipfs/package.json').version From ffa36a2618074b820580d28f91c9b039414ba360 Mon Sep 17 00:00:00 2001 From: David Dias Date: Tue, 15 May 2018 10:55:54 +0100 Subject: [PATCH 10/12] chore: use 8.11.1 for circle --- circle.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circle.yml b/circle.yml index 58355193..895c5dfc 100644 --- a/circle.yml +++ b/circle.yml @@ -1,7 +1,7 @@ # Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories. machine: node: - version: stable + version: 8.11.1 test: post: From cf6f8653ce9645f172e8ba484b73cbde620aa386 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 15 May 2018 21:30:21 -0600 Subject: [PATCH 11/12] feat: detect inited repo, remove ipfs-repo --- package.json | 8 +++---- src/factory-in-proc.js | 8 +++---- src/ipfsd-in-proc.js | 11 +++++---- src/utils/repo/browser.js | 24 +++++++++++++++++++ src/utils/repo/create-browser.js | 28 ---------------------- src/utils/repo/create-nodejs.js | 41 -------------------------------- src/utils/repo/nodejs.js | 29 ++++++++++++++++++++++ test/add-retrieve.spec.js | 8 +++---- test/spawn-options.spec.js | 8 +++---- test/utils.spec.js | 23 ------------------ 10 files changed, 75 insertions(+), 113 deletions(-) create mode 100644 src/utils/repo/browser.js delete mode 100644 src/utils/repo/create-browser.js delete mode 100644 src/utils/repo/create-nodejs.js create mode 100644 src/utils/repo/nodejs.js diff --git a/package.json b/package.json index f0580a1b..afb9c0b1 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "joi": false, "stream": "readable-stream", "http": "stream-http", - "./src/utils/repo/create-nodejs.js": "./src/utils/repo/create-browser.js", + "./src/utils/repo/nodejs.js": "./src/utils/repo/browser.js", "./src/utils/exec.js": false, "./src/utils/find-ipfs-executable.js": false, "./src/utils/tmp-dir.js": "./src/utils/tmp-dir-browser.js", @@ -83,15 +83,16 @@ "boom": "^7.2.0", "debug": "^3.1.0", "detect-node": "^2.0.3", + "dexie": "^1.5.1", "hapi": "^16.6.2", "hat": "0.0.3", "ipfs-api": "^21.0.0", - "ipfs-repo": "~0.20.0", "joi": "^13.1.2", "lodash.clone": "^4.5.0", "lodash.defaults": "^4.2.0", "lodash.defaultsdeep": "^4.6.0", "multiaddr": "^5.0.0", + "once": "^1.4.0", "readable-stream": "^2.3.6", "rimraf": "^2.6.2", "safe-json-parse": "^4.0.0", @@ -100,8 +101,7 @@ "stream-http": "^2.8.1", "subcomandante": "^1.0.5", "superagent": "^3.8.2", - "truthy": "0.0.1", - "once": "^1.4.0" + "truthy": "0.0.1" }, "devDependencies": { "aegir": "^13.0.6", diff --git a/src/factory-in-proc.js b/src/factory-in-proc.js index cdfcef04..1ea73488 100644 --- a/src/factory-in-proc.js +++ b/src/factory-in-proc.js @@ -6,6 +6,7 @@ const series = require('async/series') const path = require('path') const tmpDir = require('./utils/tmp-dir') const once = require('once') +const repoUtils = require('./utils/repo/nodejs') const Node = require('./ipfsd-in-proc') const defaultConfig = require('./defaults/config') @@ -141,10 +142,9 @@ class FactoryInProc { series([ (cb) => node.once('ready', cb), - (cb) => node.repo._isInitialized(err => { - // if err exists, repo failed to find config or the ipfs-repo package - // version is different than that of the existing repo. - node.initialized = !err + (cb) => repoUtils.repoExists(node.path, (err, initialized) => { + if (err) { return cb(err) } + node.initialized = initialized cb() }), (cb) => options.init diff --git a/src/ipfsd-in-proc.js b/src/ipfsd-in-proc.js index 43d271e1..58f25c60 100644 --- a/src/ipfsd-in-proc.js +++ b/src/ipfsd-in-proc.js @@ -2,7 +2,7 @@ const multiaddr = require('multiaddr') const defaultsDeep = require('lodash.defaultsdeep') -const createRepo = require('./utils/repo/create-nodejs') +const repoUtils = require('./utils/repo/nodejs') const defaults = require('lodash.defaults') const waterfall = require('async/waterfall') const debug = require('debug') @@ -30,14 +30,14 @@ class Node extends EventEmitter { IPFS = this.opts.exec this.opts.args = this.opts.args || [] - this.path = this.opts.repoPath - this.repo = createRepo(this.path) + this.path = this.opts.repoPath || repoUtils.createTempRepoPath() this.disposable = this.opts.disposable this.clean = true this._apiAddr = null this._gatewayAddr = null this._started = false this.api = null + this.initialized = false this.bits = this.opts.initOptions ? this.opts.initOptions.bits : null this.opts.EXPERIMENTAL = defaultsDeep({}, opts.EXPERIMENTAL, { @@ -64,7 +64,7 @@ class Node extends EventEmitter { }) this.exec = new IPFS({ - repo: this.repo, + repo: this.path, init: false, start: false, pass: this.opts.pass, @@ -178,7 +178,8 @@ class Node extends EventEmitter { return callback() } - this.repo.teardown(callback) + repoUtils.removeRepo(this.path) + callback() } /** diff --git a/src/utils/repo/browser.js b/src/utils/repo/browser.js new file mode 100644 index 00000000..e4c04bfa --- /dev/null +++ b/src/utils/repo/browser.js @@ -0,0 +1,24 @@ +/* global self */ +'use strict' + +const hat = require('hat') +const Dexie = require('dexie') + +exports.createTempRepoPath = function createTempPathRepo () { + return '/ipfs-' + hat() +} + +exports.removeRepo = function removeRepo (repoPath) { + Dexie.delete(repoPath) +} + +exports.repoExists = function repoExists (repoPath, cb) { + const db = new Dexie(repoPath) + db.open(repoPath) + .then((store) => { + const table = store.table(repoPath) + return table + .count((cnt) => cb(null, cnt > 0)) + .catch(cb) + }).catch(cb) +} diff --git a/src/utils/repo/create-browser.js b/src/utils/repo/create-browser.js deleted file mode 100644 index 18b220b7..00000000 --- a/src/utils/repo/create-browser.js +++ /dev/null @@ -1,28 +0,0 @@ -/* global self */ -'use strict' - -const IPFSRepo = require('ipfs-repo') -const hat = require('hat') - -const idb = self.indexedDB || - self.mozIndexedDB || - self.webkitIndexedDB || - self.msIndexedDB - -function createTempRepo (repoPath) { - repoPath = repoPath || '/ipfs-' + hat() - - const repo = new IPFSRepo(repoPath) - - repo.teardown = (done) => { - repo.close(() => { - idb.deleteDatabase(repoPath) - idb.deleteDatabase(repoPath + '/blocks') - done() - }) - } - - return repo -} - -module.exports = createTempRepo diff --git a/src/utils/repo/create-nodejs.js b/src/utils/repo/create-nodejs.js deleted file mode 100644 index 79d91fe3..00000000 --- a/src/utils/repo/create-nodejs.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict' - -const IPFSRepo = require('ipfs-repo') -const os = require('os') -const path = require('path') -const hat = require('hat') -const series = require('async/series') -const rimraf = require('rimraf') -const fs = require('fs') - -const clean = (dir) => { - try { - fs.accessSync(dir) - } catch (err) { - // Does not exist so all good - return - } - - rimraf.sync(dir) -} - -function createTempRepo (repoPath) { - repoPath = repoPath || path.join(os.tmpdir(), '/ipfs-test-' + hat()) - - const repo = new IPFSRepo(repoPath) - - repo.teardown = (done) => { - series([ - // ignore err, might have been closed already - (cb) => repo.close(() => cb()), - (cb) => { - clean(repoPath) - cb() - } - ], done) - } - - return repo -} - -module.exports = createTempRepo diff --git a/src/utils/repo/nodejs.js b/src/utils/repo/nodejs.js new file mode 100644 index 00000000..69f8b296 --- /dev/null +++ b/src/utils/repo/nodejs.js @@ -0,0 +1,29 @@ +'use strict' + +const os = require('os') +const path = require('path') +const hat = require('hat') +const rimraf = require('rimraf') +const fs = require('fs') + +exports.removeRepo = function removeRepo (dir) { + try { + fs.accessSync(dir) + } catch (err) { + // Does not exist so all good + return + } + + return rimraf.sync(dir) +} + +exports.createTempRepoPath = function createTempRepo () { + return path.join(os.tmpdir(), '/ipfs-test-' + hat()) +} + +exports.repoExists = function (repoPath, cb) { + fs.access(`${repoPath}/config`, (err) => { + if (err) { return cb(null, false) } + cb(null, true) + }) +} diff --git a/test/add-retrieve.spec.js b/test/add-retrieve.spec.js index dd2470c9..9472fec8 100644 --- a/test/add-retrieve.spec.js +++ b/test/add-retrieve.spec.js @@ -8,14 +8,14 @@ const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) const IPFSFactory = require('../src') -// const JSIPFS = require('ipfs') +const JSIPFS = require('ipfs') const tests = [ - { type: 'go', bits: 1024 }, - { type: 'js', bits: 512 } + // { type: 'go', bits: 1024 }, + // { type: 'js', bits: 512 }, // TODO: Enable again once js-ipfs 0.29 is released // A weird dependency wrangling is breaking a isBlock call - // { type: 'proc', exec: JSIPFS, bits: 512 } + { type: 'proc', exec: JSIPFS, bits: 512 } ] describe('data can be put and fetched', () => { diff --git a/test/spawn-options.spec.js b/test/spawn-options.spec.js index b9c326f6..ed162add 100644 --- a/test/spawn-options.spec.js +++ b/test/spawn-options.spec.js @@ -13,15 +13,15 @@ const fs = require('fs') const isNode = require('detect-node') const hat = require('hat') const IPFSFactory = require('../src') -// const JSIPFS = require('ipfs') +const JSIPFS = require('ipfs') const once = require('once') const tests = [ - { type: 'go', bits: 1024 }, - { type: 'js', bits: 512 } + // { type: 'go', bits: 1024 }, + // { type: 'js', bits: 512 }, // TODO: Enable again once js-ipfs 0.29 is released // A weird dependency wrangling is breaking a isBlock call - // { type: 'proc', exec: JSIPFS, bits: 512 } + { type: 'proc', exec: JSIPFS, bits: 512 } ] const jsVersion = require('ipfs/package.json').version diff --git a/test/utils.spec.js b/test/utils.spec.js index 091191ee..52bff0df 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -13,9 +13,6 @@ const path = require('path') const flatten = require('../src/utils/flatten') const tempDir = require('../src/utils/tmp-dir') const findIpfsExecutable = require('../src/utils/find-ipfs-executable') -const createRepo = require('../src/utils/repo/create-nodejs') - -const IPFSRepo = require('ipfs-repo') describe('utils', () => { describe('.flatten', () => { @@ -63,25 +60,5 @@ describe('utils', () => { expect(fs.existsSync(execPath)).to.be.ok() }) }) - - describe('.createRepo', () => { - let repo = null - let repoPath = tempDir() - - it('should create repo', () => { - repo = createRepo(repoPath) - expect(repo).to.exist() - expect(repo).to.be.instanceOf(IPFSRepo) - expect(fs.existsSync(repoPath)).to.be.ok() - }) - - it('should cleanup repo', (done) => { - repo.teardown((err) => { - expect(err).to.not.exist() - expect(!fs.existsSync(repoPath)).to.be.ok() - done() - }) - }) - }) } }) From 408b96f4e973f246e46c3557e6efbc08b5b23b61 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 15 May 2018 21:43:12 -0600 Subject: [PATCH 12/12] fix: remove TODOs --- test/add-retrieve.spec.js | 6 ++---- test/spawn-options.spec.js | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/test/add-retrieve.spec.js b/test/add-retrieve.spec.js index 9472fec8..c40057de 100644 --- a/test/add-retrieve.spec.js +++ b/test/add-retrieve.spec.js @@ -11,10 +11,8 @@ const IPFSFactory = require('../src') const JSIPFS = require('ipfs') const tests = [ - // { type: 'go', bits: 1024 }, - // { type: 'js', bits: 512 }, - // TODO: Enable again once js-ipfs 0.29 is released - // A weird dependency wrangling is breaking a isBlock call + { type: 'go', bits: 1024 }, + { type: 'js', bits: 512 }, { type: 'proc', exec: JSIPFS, bits: 512 } ] diff --git a/test/spawn-options.spec.js b/test/spawn-options.spec.js index ed162add..60d993f9 100644 --- a/test/spawn-options.spec.js +++ b/test/spawn-options.spec.js @@ -17,10 +17,8 @@ const JSIPFS = require('ipfs') const once = require('once') const tests = [ - // { type: 'go', bits: 1024 }, - // { type: 'js', bits: 512 }, - // TODO: Enable again once js-ipfs 0.29 is released - // A weird dependency wrangling is breaking a isBlock call + { type: 'go', bits: 1024 }, + { type: 'js', bits: 512 }, { type: 'proc', exec: JSIPFS, bits: 512 } ]