From b2dfbc602d4befd915793dcd98318e3bc0c9859f Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 10 Dec 2019 11:07:05 +0000 Subject: [PATCH] fix: convert config API as it used by ipfsd-ctl --- package.json | 1 + src/core/components/add/index.js | 15 ++- src/core/components/config.js | 17 ++- src/core/components/index.js | 2 +- src/core/components/init.js | 1 + src/core/components/start.js | 1 + src/core/components/stop.js | 1 + test/cli/files.js | 2 +- test/core/config.spec.js | 223 ------------------------------- test/core/files.spec.js | 13 +- test/core/interface.spec.js | 2 +- 11 files changed, 30 insertions(+), 248 deletions(-) delete mode 100644 test/core/config.spec.js diff --git a/package.json b/package.json index c7f97a5d71..44f02b7c74 100644 --- a/package.json +++ b/package.json @@ -209,6 +209,7 @@ "interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#refactor/async-iterables", "ipfs-interop": "^0.1.1", "ipfsd-ctl": "github:ipfs/js-ipfsd-ctl#fix/do-not-call-shutdown-twice", + "it-all": "^1.0.1", "libp2p-websocket-star": "~0.10.2", "lodash": "^4.17.15", "ncp": "^2.0.0", diff --git a/src/core/components/add/index.js b/src/core/components/add/index.js index d7e39ce3d4..4eed312d9f 100644 --- a/src/core/components/add/index.js +++ b/src/core/components/add/index.js @@ -59,21 +59,23 @@ module.exports = ({ ipld, dag, gcLock, preload, pin, constructorOptions }) => { function transformFile (dag, opts) { return async function * (source) { for await (const { cid, path, unixfs } of source) { + const hash = cid.toString() + if (opts.onlyHash) { yield { - cid, - path: path || cid.toString(), + hash, + path: path || hash, size: unixfs.fileSize() } continue } - const node = await dag.get(cid, { ...opts, preload: false }) + const { value: node } = await dag.get(cid, { ...opts, preload: false }) yield { - cid, - path: path || cid.toString(), + hash, + path: path || hash, size: Buffer.isBuffer(node) ? node.length : node.size } } @@ -103,9 +105,8 @@ function pinFile (pin, opts) { for await (const file of source) { // Pin a file if it is the root dir of a recursive add or the single file // of a direct add. - const pin = 'pin' in opts ? opts.pin : true const isRootDir = !file.path.includes('/') - const shouldPin = pin && isRootDir && !opts.onlyHash + const shouldPin = (opts.pin == null ? true : opts.pin) && isRootDir && !opts.onlyHash if (shouldPin) { // Note: addAsyncIterator() has already taken a GC lock, so tell diff --git a/src/core/components/config.js b/src/core/components/config.js index 381a36ce61..c747387c73 100644 --- a/src/core/components/config.js +++ b/src/core/components/config.js @@ -1,17 +1,16 @@ 'use strict' -const callbackify = require('callbackify') const getDefaultConfig = require('../runtime/config-nodejs.js') const log = require('debug')('ipfs:core:config') -module.exports = function config (self) { +module.exports = ({ repo }) => { return { - get: callbackify.variadic(self._repo.config.get), - set: callbackify(self._repo.config.set), - replace: callbackify.variadic(self._repo.config.set), + get: repo.config.get, + set: repo.config.set, + replace: repo.config.set, profiles: { - apply: callbackify.variadic(applyProfile), - list: callbackify.variadic(listProfiles) + apply: applyProfile, + list: listProfiles } } @@ -26,12 +25,12 @@ module.exports = function config (self) { } try { - const oldCfg = await self.config.get() + const oldCfg = await repo.config.get() let newCfg = JSON.parse(JSON.stringify(oldCfg)) // clone newCfg = profile.transform(newCfg) if (!dryRun) { - await self.config.replace(newCfg) + await repo.config.set(newCfg) } // Scrub private key from output diff --git a/src/core/components/index.js b/src/core/components/index.js index eb9b0fb384..44d922712a 100644 --- a/src/core/components/index.js +++ b/src/core/components/index.js @@ -1,12 +1,12 @@ 'use strict' exports.add = require('./add') +exports.config = require('./config') exports.init = require('./init') exports.start = require('./start') exports.stop = require('./stop') exports.legacy = { // TODO: these will be removed as the new API is completed - config: require('./config'), dag: require('./dag'), libp2p: require('./libp2p'), object: require('./object'), diff --git a/src/core/components/init.js b/src/core/components/init.js index 7d86d254c9..089d6148dc 100644 --- a/src/core/components/init.js +++ b/src/core/components/init.js @@ -299,6 +299,7 @@ function createApi ({ const api = { add, + config: Commands.config({ repo }), init: () => { throw new AlreadyInitializedError() }, start } diff --git a/src/core/components/start.js b/src/core/components/start.js index 8fcb41ac17..f9f41c7458 100644 --- a/src/core/components/start.js +++ b/src/core/components/start.js @@ -131,6 +131,7 @@ function createApi ({ const api = { add, + config: Commands.config({ repo }), init: () => { throw new AlreadyInitializedError() }, start: () => apiManager.api, stop diff --git a/src/core/components/stop.js b/src/core/components/stop.js index 6785cfd889..4e2a9bb036 100644 --- a/src/core/components/stop.js +++ b/src/core/components/stop.js @@ -98,6 +98,7 @@ function createApi ({ const api = { add, + config: Commands.config({ repo }), init: () => { throw new AlreadyInitializedError() }, start, stop: () => apiManager.api diff --git a/test/cli/files.js b/test/cli/files.js index 4858cc0cb0..ec7e0263a6 100644 --- a/test/cli/files.js +++ b/test/cli/files.js @@ -26,7 +26,7 @@ const HASH_ALGS = [ 'keccak-512' ] -describe('files', () => runOnAndOff((thing) => { +describe.only('files', () => runOnAndOff((thing) => { let ipfs const readme = fs.readFileSync(path.join(process.cwd(), '/src/init-files/init-docs/readme')) .toString('utf-8') diff --git a/test/core/config.spec.js b/test/core/config.spec.js deleted file mode 100644 index ee1fa4a00e..0000000000 --- a/test/core/config.spec.js +++ /dev/null @@ -1,223 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const { expect } = require('interface-ipfs-core/src/utils/mocha') -const config = require('../../src/core/config') - -describe('config', () => { - it('should allow empty config', () => { - const cfg = {} - expect(() => config.validate(cfg)).to.not.throw() - }) - - it('should allow undefined config', () => { - const cfg = undefined - expect(() => config.validate(cfg)).to.not.throw() - }) - - it('should validate valid repo', () => { - const cfgs = [ - { repo: { unknown: 'value' } }, - { repo: '/path/to-repo' }, - { repo: undefined } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.not.throw()) - }) - - it('should validate invalid repo', () => { - const cfgs = [ - { repo: 138 } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw()) - }) - - it('should validate valid init', () => { - const cfgs = [ - { init: { bits: 138 } }, - { init: true }, - { init: false }, - { init: undefined } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.not.throw()) - }) - - it('should validate invalid init', () => { - const cfgs = [ - { init: 138 }, - { init: { bits: 'not an int' } } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw()) - }) - - it('should validate valid start', () => { - const cfgs = [ - { start: true }, - { start: false }, - { start: undefined } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.not.throw()) - }) - - it('should validate invalid start', () => { - const cfgs = [ - { start: 138 }, - { start: 'make it so number 1' }, - { start: null } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw()) - }) - - it('should validate valid pass', () => { - const cfgs = [ - { pass: 'correctbatteryhorsestaple' }, - { pass: '' }, - { pass: undefined } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.not.throw()) - }) - - it('should validate invalid pass', () => { - const cfgs = [ - { pass: 138 }, - { pass: null } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw()) - }) - - it('should validate valid EXPERIMENTAL', () => { - const cfgs = [ - { EXPERIMENTAL: { dht: true, sharding: true } }, - { EXPERIMENTAL: { dht: false, sharding: false } }, - { EXPERIMENTAL: undefined } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.not.throw()) - }) - - it('should validate invalid EXPERIMENTAL', () => { - const cfgs = [ - { EXPERIMENTAL: { dht: 138 } }, - { EXPERIMENTAL: { sharding: 138 } } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw()) - }) - - it('should validate valid config', () => { - const cfgs = [ - { config: { Addresses: { Swarm: ['/ip4/0.0.0.0/tcp/4002'] } } }, - { config: { Addresses: { Swarm: [] } } }, - { config: { Addresses: { Swarm: undefined } } }, - - { config: { Addresses: { API: '/ip4/127.0.0.1/tcp/5002' } } }, - { config: { Addresses: { API: ['/ip4/127.0.0.1/tcp/5002', '/ip4/127.0.0.1/tcp/5003'] } } }, - { config: { Addresses: { API: undefined } } }, - - { config: { Addresses: { Gateway: '/ip4/127.0.0.1/tcp/9090' } } }, - { config: { Addresses: { Gateway: ['/ip4/127.0.0.1/tcp/9090', '/ip4/127.0.0.1/tcp/9091'] } } }, - { config: { Addresses: { Gateway: undefined } } }, - - { config: { Addresses: { Delegates: ['/dns4/node0.preload.ipfs.io/tcp/443/https'] } } }, - { config: { Addresses: { Delegates: [] } } }, - { config: { Addresses: { Delegates: undefined } } }, - - { config: { Addresses: undefined } }, - - { config: { Discovery: { MDNS: { Enabled: true } } } }, - { config: { Discovery: { MDNS: { Enabled: false } } } }, - { config: { Discovery: { MDNS: { Interval: 138 } } } }, - { config: { Discovery: { MDNS: undefined } } }, - - { config: { Discovery: { webRTCStar: { Enabled: true } } } }, - { config: { Discovery: { webRTCStar: { Enabled: false } } } }, - { config: { Discovery: { webRTCStar: undefined } } }, - - { config: { Discovery: undefined } }, - - { config: { Bootstrap: ['/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z'] } }, - { config: { Bootstrap: [] } }, - - { config: { Swarm: { ConnMgr: { LowWater: 200, HighWater: 500 } } } }, - { config: { Swarm: { ConnMgr: { LowWater: undefined, HighWater: undefined } } } }, - { config: { Swarm: { ConnMgr: undefined } } }, - { config: { Swarm: undefined } }, - - { config: { Pubsub: { Enabled: true, Router: 'gossipsub' } } }, - { config: { Pubsub: { Enabled: false } } }, - - { config: undefined } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.not.throw()) - }) - - it('should validate invalid config', () => { - const cfgs = [ - { config: { Addresses: { Swarm: 138 } } }, - { config: { Addresses: { Swarm: null } } }, - - { config: { Addresses: { API: 138 } } }, - { config: { Addresses: { API: null } } }, - - { config: { Addresses: { Gateway: 138 } } }, - { config: { Addresses: { Gateway: null } } }, - - { config: { Discovery: { MDNS: { Enabled: 138 } } } }, - { config: { Discovery: { MDNS: { Interval: true } } } }, - - { config: { Discovery: { webRTCStar: { Enabled: 138 } } } }, - - { config: { Bootstrap: ['/ip4/0.0.0.0/tcp/4002'] } }, - { config: { Bootstrap: 138 } }, - - { config: { Swarm: { ConnMgr: { LowWater: 200, HighWater: {} } } } }, - { config: { Swarm: { ConnMgr: { LowWater: {}, HighWater: 500 } } } }, - { config: { Swarm: { ConnMgr: 138 } } }, - { config: { Swarm: 138 } }, - - { config: { Pubsub: { Enabled: 1 } } }, - - { config: 138 } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw()) - }) - - it('should validate valid libp2p', () => { - const cfgs = [ - { libp2p: { modules: {} } }, - { libp2p: () => {} }, - { libp2p: undefined } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.not.throw()) - }) - - it('should validate invalid libp2p', () => { - const cfgs = [ - { libp2p: 'error' }, - { libp2p: 138 } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw()) - }) - - it('should validate valid profiles', () => { - expect( - () => config.validate({ init: { profiles: ['test'] } }) - ).to.not.throw() - }) - it('should validate invalid profiles', () => { - expect( - () => config.validate({ init: { profiles: 'test' } }) - ).to.throw() - }) -}) diff --git a/test/core/files.spec.js b/test/core/files.spec.js index ea8ca2380a..484c06ba43 100644 --- a/test/core/files.spec.js +++ b/test/core/files.spec.js @@ -6,9 +6,10 @@ const { expect } = require('interface-ipfs-core/src/utils/mocha') const hat = require('hat') const pull = require('pull-stream') const IPFSFactory = require('ipfsd-ctl') +const all = require('it-all') const IPFS = require('../../src/core') -describe('files', function () { +describe.only('files', function () { this.timeout(10 * 1000) let ipfsd, ipfs @@ -74,13 +75,13 @@ describe('files', function () { describe('add', () => { it('should not error when passed null options', async () => { - await ipfs.add(Buffer.from(hat()), null) + await all(ipfs.add(Buffer.from(hat()), null)) }) it('should add a file with a v1 CID', async () => { - const files = await ipfs.add(Buffer.from([0, 1, 2]), { + const files = await all(ipfs.add(Buffer.from([0, 1, 2]), { cidVersion: 1 - }) + })) expect(files.length).to.equal(1) expect(files[0].hash).to.equal('bafkreifojmzibzlof6xyh5auu3r5vpu5l67brf3fitaf73isdlglqw2t7q') @@ -88,10 +89,10 @@ describe('files', function () { }) it('should add a file with a v1 CID and not raw leaves', async () => { - const files = await ipfs.add(Buffer.from([0, 1, 2]), { + const files = await all(ipfs.add(Buffer.from([0, 1, 2]), { cidVersion: 1, rawLeaves: false - }) + })) expect(files.length).to.equal(1) expect(files[0].hash).to.equal('bafybeide2caf5we5a7izifzwzz5ds2gla67vsfgrzvbzpnyyirnfzgwf5e') diff --git a/test/core/interface.spec.js b/test/core/interface.spec.js index 5aab6376e7..d1a1a2e58b 100644 --- a/test/core/interface.spec.js +++ b/test/core/interface.spec.js @@ -41,7 +41,7 @@ describe('interface-ipfs-core tests', function () { } }) - tests.filesMFS(defaultCommonFactory) + tests.files(defaultCommonFactory) tests.key(CommonFactory.create({ spawnOptions: {