From e7c3728a74f3468bd04ab9359a54a549191f219a Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 14 Dec 2018 17:23:17 +0000 Subject: [PATCH 01/16] fix: remove defaults-deep --- package.json | 2 +- src/core/components/init.js | 4 ++-- src/core/components/libp2p.js | 4 ++-- src/core/components/pre-start.js | 4 ++-- src/core/index.js | 4 ++-- src/core/runtime/libp2p-browser.js | 4 ++-- src/core/runtime/libp2p-nodejs.js | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 37ce2001f7..17eec55d70 100644 --- a/package.json +++ b/package.json @@ -78,7 +78,6 @@ "stream-to-promise": "^2.2.0" }, "dependencies": { - "@nodeutils/defaults-deep": "^1.1.0", "async": "^2.6.1", "bignumber.js": "^8.0.2", "binary-querystring": "~0.1.2", @@ -141,6 +140,7 @@ "libp2p-websockets": "~0.12.2", "lodash": "^4.17.11", "mafmt": "^6.0.2", + "merge-options": "^1.0.1", "mime-types": "^2.1.21", "mkdirp": "~0.5.1", "multiaddr": "^6.0.0", diff --git a/src/core/components/init.js b/src/core/components/init.js index 29cac89402..96b8572baf 100644 --- a/src/core/components/init.js +++ b/src/core/components/init.js @@ -1,10 +1,10 @@ 'use strict' const peerId = require('peer-id') +const mergeOptions = require('merge-options') const waterfall = require('async/waterfall') const parallel = require('async/parallel') const promisify = require('promisify-es6') -const defaultsDeep = require('@nodeutils/defaults-deep') const defaultConfig = require('../runtime/config-nodejs.js') const Keychain = require('libp2p-keychain') const { @@ -59,7 +59,7 @@ module.exports = function init (self) { opts.bits = Number(opts.bits) || 2048 opts.log = opts.log || function () {} - const config = defaultsDeep(self._options.config, defaultConfig()) + const config = mergeOptions(defaultConfig(), self._options.config) let privateKey waterfall([ diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index 7fc21bc922..5939fa729a 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -1,8 +1,8 @@ 'use strict' const get = require('lodash/get') -const defaultsDeep = require('@nodeutils/defaults-deep') const ipnsUtils = require('../ipns/routing/utils') +const mergeOptions = require('merge-options') module.exports = function libp2p (self, config) { const options = self._options || {} @@ -102,7 +102,7 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) { }) } - const libp2pOptions = defaultsDeep(get(options, 'libp2p', {}), libp2pDefaults) + const libp2pOptions = mergeOptions(libp2pDefaults, get(options, 'libp2p', {})) // Required inline to reduce startup time // Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified diff --git a/src/core/components/pre-start.js b/src/core/components/pre-start.js index 13d914acc4..02cf59cb4b 100644 --- a/src/core/components/pre-start.js +++ b/src/core/components/pre-start.js @@ -3,9 +3,9 @@ const peerId = require('peer-id') const PeerInfo = require('peer-info') const multiaddr = require('multiaddr') +const mergeOptions = require('merge-options') const waterfall = require('async/waterfall') const Keychain = require('libp2p-keychain') -const defaultsDeep = require('@nodeutils/defaults-deep') const NoKeychain = require('./no-keychain') /* * Load stuff from Repo into memory @@ -22,7 +22,7 @@ module.exports = function preStart (self) { return cb(null, config) } - config = defaultsDeep(self._options.config, config) + config = mergeOptions(config, self._options.config) self.config.replace(config, (err) => { if (err) { diff --git a/src/core/index.js b/src/core/index.js index 8248a21765..2076d7cee7 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -13,7 +13,7 @@ const multibase = require('multibase') const multicodec = require('multicodec') const CID = require('cids') const debug = require('debug') -const defaultsDeep = require('@nodeutils/defaults-deep') +const mergeOptions = require('merge-options') const EventEmitter = require('events') const config = require('./config') @@ -78,7 +78,7 @@ class IPFS extends EventEmitter { options = config.validate(options || {}) - this._options = defaultsDeep(options, defaults) + this._options = mergeOptions(defaults, options) if (options.init === false) { this._options.init = false diff --git a/src/core/runtime/libp2p-browser.js b/src/core/runtime/libp2p-browser.js index 8fae1ae176..19fe27398b 100644 --- a/src/core/runtime/libp2p-browser.js +++ b/src/core/runtime/libp2p-browser.js @@ -8,7 +8,7 @@ const SECIO = require('libp2p-secio') const Bootstrap = require('libp2p-bootstrap') const KadDHT = require('libp2p-kad-dht') const libp2p = require('libp2p') -const defaultsDeep = require('@nodeutils/defaults-deep') +const mergeOptions = require('merge-options') const multiaddr = require('multiaddr') class Node extends libp2p { @@ -62,7 +62,7 @@ class Node extends libp2p { } } - super(defaultsDeep(_options, defaults)) + super(mergeOptions(defaults, _options)) } } diff --git a/src/core/runtime/libp2p-nodejs.js b/src/core/runtime/libp2p-nodejs.js index 01e7046e19..4f11036a86 100644 --- a/src/core/runtime/libp2p-nodejs.js +++ b/src/core/runtime/libp2p-nodejs.js @@ -9,7 +9,7 @@ const KadDHT = require('libp2p-kad-dht') const Multiplex = require('libp2p-mplex') const SECIO = require('libp2p-secio') const libp2p = require('libp2p') -const defaultsDeep = require('@nodeutils/defaults-deep') +const mergeOptions = require('merge-options') const multiaddr = require('multiaddr') class Node extends libp2p { @@ -65,7 +65,7 @@ class Node extends libp2p { } } - super(defaultsDeep(_options, defaults)) + super(mergeOptions(defaults, _options)) } } From ccf662915c43b558e3d6c08c93b8fd89f4582ce5 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 14 Dec 2018 22:52:01 +0000 Subject: [PATCH 02/16] fix: make ipld configurable and remove formats browser --- README.md | 8 ++++++++ package.json | 1 + src/core/index.js | 44 ++------------------------------------------ 3 files changed, 11 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 21a2584a81..f21dc9a18a 100644 --- a/README.md +++ b/README.md @@ -324,6 +324,14 @@ Enable and configure experimental features. Modify the default IPFS node config. This object will be *merged* with the default config; it will not replace it. +##### `options.ipld` + +| Type | Default | +|------|---------| +| object | [`ipld-nodejs.js`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/ipld-nodejs.js) in Node.js, [`ipld-browser.js`](https://github.com/ipfs/js-ipfs/tree/master/src/core/runtime/ipld-browser.js) in browsers | + +Modify the default IPLD config. This object will be *merged* with the default config; it will not replace it. Check IPLD [docs](https://github.com/ipld/js-ipld#ipld-constructor) for more information on the available options. + ##### `options.libp2p` | Type | Default | diff --git a/package.json b/package.json index 17eec55d70..69590297d5 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js", "./src/core/runtime/preload-nodejs.js": "./src/core/runtime/preload-browser.js", "./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js", + "./src/core/runtime/ipld-nodejs.js": "./src/core/runtime/ipld-browser.js", "./test/utils/create-repo-nodejs.js": "./test/utils/create-repo-browser.js", "stream": "readable-stream", "joi": "joi-browser" diff --git a/src/core/index.js b/src/core/index.js index 2076d7cee7..80bf396060 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -24,40 +24,7 @@ const components = require('./components') const defaultRepo = require('./runtime/repo-nodejs') const preload = require('./preload') const mfsPreload = require('./mfs-preload') - -// All known (non-default) IPLD formats -const IpldFormats = { - get 'bitcoin-block' () { - return require('ipld-bitcoin') - }, - get 'eth-account-snapshot' () { - return require('ipld-ethereum').ethAccountSnapshot - }, - get 'eth-block' () { - return require('ipld-ethereum').ethBlock - }, - get 'eth-block-list' () { - return require('ipld-ethereum').ethBlockList - }, - get 'eth-state-trie' () { - return require('ipld-ethereum').ethStateTrie - }, - get 'eth-storage-trie' () { - return require('ipld-ethereum').ethStorageTrie - }, - get 'eth-tx' () { - return require('ipld-ethereum').ethTx - }, - get 'eth-tx-trie' () { - return require('ipld-ethereum').ethTxTrie - }, - get 'git-raw' () { - return require('ipld-git') - }, - get 'zcash-block' () { - return require('ipld-zcash') - } -} +const ipldOptions = require('./runtime/ipld-nodejs') class IPFS extends EventEmitter { constructor (options) { @@ -105,14 +72,7 @@ class IPFS extends EventEmitter { this._peerInfo = undefined this._bitswap = undefined this._blockService = new BlockService(this._repo) - this._ipld = new Ipld({ - blockService: this._blockService, - loadFormat: (codec, callback) => { - this.log('Loading IPLD format', codec) - if (IpldFormats[codec]) return callback(null, IpldFormats[codec]) - callback(new Error(`Missing IPLD format "${codec}"`)) - } - }) + this._ipld = new Ipld(ipldOptions(this._blockService, this._options.ipld)) this._preload = preload(this) this._mfsPreload = mfsPreload(this) this._ipns = undefined From 5e971333eedd7223a1a9c95e92265556130568ee Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 24 Dec 2018 11:46:38 +0000 Subject: [PATCH 03/16] fix: reduce bundle size --- package.json | 10 +- src/cli/commands/add.js | 18 ++- src/core/components/dag.js | 2 +- .../components/files-regular/add-from-url.js | 2 +- src/core/components/files-regular/add.js | 3 +- src/core/components/libp2p.js | 3 +- src/core/components/start.js | 2 +- src/core/components/swarm.js | 5 +- src/core/config.js | 132 ++++++++++-------- src/http/api/resources/config.js | 8 +- src/http/api/resources/pin.js | 15 +- test/core/bitswap.spec.js | 6 +- test/utils/ipfs-exec.js | 6 +- 13 files changed, 114 insertions(+), 98 deletions(-) diff --git a/package.json b/package.json index 69590297d5..ab1c960486 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "datastore-core": "~0.6.0", "datastore-pubsub": "~0.1.1", "debug": "^4.1.0", + "dlv": "^1.1.2", "err-code": "^1.1.2", "file-type": "^10.2.0", "fnv1a": "^1.0.1", @@ -106,7 +107,7 @@ "ipfs-bitswap": "~0.22.0", "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", - "ipfs-http-client": "^29.0.0", + "ipfs-http-client": "ipfs/js-ipfs-http-client#fix/bundle-size", "ipfs-http-response": "~0.2.1", "ipfs-mfs": "~0.9.1", "ipfs-multipart": "~0.1.0", @@ -123,6 +124,7 @@ "is-ipfs": "~0.4.8", "is-pull-stream": "~0.0.0", "is-stream": "^1.1.0", + "iso-url": "^0.4.4", "joi": "^14.3.0", "joi-browser": "^13.4.0", "joi-multiaddr": "^4.0.0", @@ -131,15 +133,17 @@ "libp2p-crypto": "~0.16.0", "libp2p-kad-dht": "~0.14.7", "libp2p-keychain": "~0.3.3", + "just-flatten-it": "^2.1.0", + "just-range": "^2.1.0", + "just-safe-set": "^2.1.0", "libp2p-mdns": "~0.12.0", - "libp2p-mplex": "~0.8.4", + "libp2p-mplex": "libp2p/js-libp2p-mplex#fix/bundle-size", "libp2p-record": "~0.6.1", "libp2p-secio": "~0.11.0", "libp2p-tcp": "~0.13.0", "libp2p-webrtc-star": "~0.15.5", "libp2p-websocket-star-multi": "~0.4.0", "libp2p-websockets": "~0.12.2", - "lodash": "^4.17.11", "mafmt": "^6.0.2", "merge-options": "^1.0.1", "mime-types": "^2.1.21", diff --git a/src/cli/commands/add.js b/src/cli/commands/add.js index 11fc84c1ee..40bd9b0e74 100644 --- a/src/cli/commands/add.js +++ b/src/cli/commands/add.js @@ -1,6 +1,8 @@ 'use strict' -const sortBy = require('lodash/sortBy') +const fs = require('fs') +const path = require('path') +const glob = require('glob') const pull = require('pull-stream') const promisify = require('promisify-es6') const getFolderSize = promisify(require('get-folder-size')) @@ -38,15 +40,11 @@ function addPipeline (source, addStream, options) { return resolve() } - sortBy(added, 'path') - .reverse() - .map((file) => { - const log = options.quiet ? [] : ['added'] - log.push(cidToString(file.hash, { base: options.cidBase })) - if (!options.quiet && file.path.length > 0) log.push(file.path) - return log.join(' ') - }) - .forEach((msg) => print(msg)) + added + .sort((a, b) => a.path.localCompare(b.path)) + .reverse() + .map((file) => { + const log = [ 'added', file.hash ] resolve() }) diff --git a/src/core/components/dag.js b/src/core/components/dag.js index dc9ef2bdf5..ffc6ccfd0a 100644 --- a/src/core/components/dag.js +++ b/src/core/components/dag.js @@ -5,7 +5,7 @@ const CID = require('cids') const pull = require('pull-stream') const mapAsync = require('async/map') const setImmediate = require('async/setImmediate') -const flattenDeep = require('lodash/flattenDeep') +const flattenDeep = require('just-flatten-it') const errCode = require('err-code') module.exports = function dag (self) { diff --git a/src/core/components/files-regular/add-from-url.js b/src/core/components/files-regular/add-from-url.js index b486c569d3..73697aa2cf 100644 --- a/src/core/components/files-regular/add-from-url.js +++ b/src/core/components/files-regular/add-from-url.js @@ -1,6 +1,6 @@ 'use strict' -const { URL } = require('url') +const { URL } = require('iso-url') const fetch = require('../../runtime/fetch-nodejs') module.exports = (self) => { diff --git a/src/core/components/files-regular/add.js b/src/core/components/files-regular/add.js index 1c36b7d20a..174cf8d51a 100644 --- a/src/core/components/files-regular/add.js +++ b/src/core/components/files-regular/add.js @@ -5,7 +5,6 @@ const pull = require('pull-stream') const sort = require('pull-sort') const isStream = require('is-stream') const isSource = require('is-pull-stream').isSource -const isString = require('lodash/isString') module.exports = function (self) { const add = promisify((data, options, callback) => { @@ -24,7 +23,7 @@ module.exports = function (self) { // path is optional if content is present if (obj.content) return isBufferOrStream(obj.content) // path must be a non-empty string if no content - return Boolean(obj.path) && isString(obj.path) + return Boolean(obj.path) && typeof obj.path === 'string' } // An input atom: a buffer, stream or content object const isInput = obj => isBufferOrStream(obj) || isContentObject(obj) diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index 5939fa729a..7c856fc494 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -1,6 +1,7 @@ 'use strict' -const get = require('lodash/get') +const promisify = require('promisify-es6') +const get = require('dlv') const ipnsUtils = require('../ipns/routing/utils') const mergeOptions = require('merge-options') diff --git a/src/core/components/start.js b/src/core/components/start.js index dda2faf267..64ee8c73e0 100644 --- a/src/core/components/start.js +++ b/src/core/components/start.js @@ -2,7 +2,7 @@ const series = require('async/series') const Bitswap = require('ipfs-bitswap') -const get = require('lodash/get') +const get = require('dlv') const setImmediate = require('async/setImmediate') const promisify = require('promisify-es6') const { TieredDatastore } = require('datastore-core') diff --git a/src/core/components/swarm.js b/src/core/components/swarm.js index 62da0ac76e..4869cf2032 100644 --- a/src/core/components/swarm.js +++ b/src/core/components/swarm.js @@ -1,7 +1,6 @@ 'use strict' const promisify = require('promisify-es6') -const values = require('lodash/values') const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR @@ -25,7 +24,7 @@ module.exports = function swarm (self) { const peers = [] - values(self._peerInfoBook.getAll()).forEach((peer) => { + Object.values(self._peerInfoBook.getAll()).forEach((peer) => { const connectedAddr = peer.isConnected() if (!connectedAddr) { return } @@ -50,7 +49,7 @@ module.exports = function swarm (self) { return callback(new Error(OFFLINE_ERROR)) } - const peers = values(self._peerInfoBook.getAll()) + const peers = Object.values(self._peerInfoBook.getAll()) callback(null, peers) }), diff --git a/src/core/config.js b/src/core/config.js index c3b9e892a4..7714b0702b 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -1,62 +1,78 @@ 'use strict' -const Joi = require('joi').extend(require('joi-multiaddr')) +const Multiaddr = require('multiaddr') +const mafmt = require('mafmt') +const { struct, superstruct } = require('superstruct') +const { optional, list, union } = struct +const s = superstruct({ + multiaddr: v => { + if (v === null) { + return `multiaddr invalid, value must be a string, Buffer, or another Multiaddr got ${v}` + } -const schema = Joi.object().keys({ - repo: Joi.alternatives().try( - Joi.object(), // TODO: schema for IPFS repo - Joi.string() - ).allow(null), - repoOwner: Joi.boolean().default(true), - preload: Joi.object().keys({ - enabled: Joi.boolean().default(true), - addresses: Joi.array().items(Joi.multiaddr().options({ convert: false })), - interval: Joi.number().integer().default(30 * 1000) - }).allow(null), - init: Joi.alternatives().try( - Joi.boolean(), - Joi.object().keys({ bits: Joi.number().integer() }) - ).allow(null), - start: Joi.boolean(), - offline: Joi.boolean(), - pass: Joi.string().allow(''), - relay: Joi.object().keys({ - enabled: Joi.boolean(), - hop: Joi.object().keys({ - enabled: Joi.boolean(), - active: Joi.boolean() - }).allow(null) - }).allow(null), - EXPERIMENTAL: Joi.object().keys({ - pubsub: Joi.boolean(), - ipnsPubsub: Joi.boolean(), - sharding: Joi.boolean(), - dht: Joi.boolean() - }).allow(null), - connectionManager: Joi.object().allow(null), - config: Joi.object().keys({ - Addresses: Joi.object().keys({ - Swarm: Joi.array().items(Joi.multiaddr().options({ convert: false })), - API: Joi.multiaddr().options({ convert: false }), - Gateway: Joi.multiaddr().options({ convert: false }) - }).allow(null), - Discovery: Joi.object().keys({ - MDNS: Joi.object().keys({ - Enabled: Joi.boolean(), - Interval: Joi.number().integer() - }).allow(null), - webRTCStar: Joi.object().keys({ - Enabled: Joi.boolean() - }).allow(null) - }).allow(null), - Bootstrap: Joi.array().items(Joi.multiaddr().IPFS().options({ convert: false })) - }).allow(null), - libp2p: Joi.alternatives().try( - Joi.func(), - Joi.object().keys({ - modules: Joi.object().allow(null) // TODO: schemas for libp2p modules? - }) - ).allow(null) -}).options({ allowUnknown: true }) + try { + Multiaddr(v) + } catch (err) { + return `multiaddr invalid, ${err.message}` + } -module.exports.validate = (config) => Joi.attempt(config, schema) + return true + }, + 'multiaddr-ipfs': v => { + return mafmt.IPFS.matches(v) + ? true + : `multiaddr IPFS invalid` + } +}) + +const configSchema = s({ + repo: optional(s('object|string')), + repoOwner: 'boolean?', + preload: s({ + enabled: 'boolean?', + addresses: optional(list(['multiaddr'])), + interval: 'number?' + }, { enabled: true, interval: 30 * 1000 }), + init: optional(union(['boolean', s({ bits: 'number?' })])), + start: 'boolean?', + local: 'boolean?', + pass: 'string?', + relay: 'object?', // relay validates in libp2p + EXPERIMENTAL: optional(s({ + pubsub: 'boolean?', + ipnsPubsub: 'boolean?', + sharding: 'boolean?', + dht: 'boolean?' + })), + connectionManager: 'object?', + config: optional(s({ + Addresses: s({ + Swarm: optional(list(['multiaddr'])), + API: 'multiaddr?', + Gateway: 'multiaddr' + }), + Discovery: optional(s({ + MDSN: optional(s({ + Enabled: 'boolean?', + Interval: 'number?' + })), + webRTCStar: optional(s({ + Enabled: 'boolean?' + })), + Bootstrap: optional(list(['multiaddr-ipfs'])) + })) + })), + libp2p: optional(union(['function', 'object'])) // libp2p validates this +}, { + repoOwner: true +}) +module.exports.validate = (opts) => { + const [error, options] = configSchema.validate(opts) + + // Improve errors throwed, reduce stack by throwing here and add reason to the message + if (error) { + throw new Error(`${error.message}${error.reason ? ' - ' + error.reason : ''}`) + } + + return options +} diff --git a/src/http/api/resources/config.js b/src/http/api/resources/config.js index 944d6058ba..dc195ce032 100644 --- a/src/http/api/resources/config.js +++ b/src/http/api/resources/config.js @@ -1,10 +1,10 @@ 'use strict' const debug = require('debug') -const get = require('lodash/get') -const set = require('lodash/set') -const log = debug('ipfs:http-api:config') -log.error = debug('ipfs:http-api:config:error') +const get = require('dlv') +const set = require('just-safe-set') +const log = debug('jsipfs:http-api:config') +log.error = debug('jsipfs:http-api:config:error') const multipart = require('ipfs-multipart') const Boom = require('boom') diff --git a/src/http/api/resources/pin.js b/src/http/api/resources/pin.js index b4232c6916..bba80c26cf 100644 --- a/src/http/api/resources/pin.js +++ b/src/http/api/resources/pin.js @@ -1,12 +1,10 @@ 'use strict' -const mapValues = require('lodash/mapValues') -const keyBy = require('lodash/keyBy') const multibase = require('multibase') -const Joi = require('joi') const Boom = require('boom') const isIpfs = require('is-ipfs') const { cidToString } = require('../../../utils/cid') +const Joi = require('joi') function parseArgs (request, h) { let { arg } = request.query @@ -63,10 +61,13 @@ exports.ls = { } return h.response({ - Keys: mapValues( - keyBy(result, obj => cidToString(obj.hash, { base: request.query['cid-base'] })), - obj => ({ Type: obj.type }) - ) + Keys: result.reduce((acc, v) => { + const prop = cidToString(obj.hash, { base: request.query['cid-base'] }) + acc[v[prop]] = { Type: v.type } + return acc + }, {}) + }) + }) } } diff --git a/test/core/bitswap.spec.js b/test/core/bitswap.spec.js index 9540aba53e..9f3e23c53d 100644 --- a/test/core/bitswap.spec.js +++ b/test/core/bitswap.spec.js @@ -6,7 +6,7 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const _ = require('lodash') +const range = require('just-range') const series = require('async/series') const waterfall = require('async/waterfall') const parallel = require('async/parallel') @@ -175,7 +175,7 @@ describe('bitswap', function () { const remoteNodes = [] series([ - (cb) => parallel(_.range(6).map((i) => makeBlock), (err, _blocks) => { + (cb) => parallel(range(6).map((i) => makeBlock), (err, _blocks) => { expect(err).to.not.exist() blocks = _blocks cb() @@ -196,7 +196,7 @@ describe('bitswap', function () { (cb) => inProcNode.block.put(blocks[4], cb), (cb) => inProcNode.block.put(blocks[5], cb), // 3. Fetch blocks on all nodes - (cb) => parallel(_.range(6).map((i) => (cbI) => { + (cb) => parallel(range(6).map((i) => (cbI) => { const check = (n, cid, callback) => { n.block.get(cid, (err, b) => { expect(err).to.not.exist() diff --git a/test/utils/ipfs-exec.js b/test/utils/ipfs-exec.js index 3f9572f2a3..acfe54752d 100644 --- a/test/utils/ipfs-exec.js +++ b/test/utils/ipfs-exec.js @@ -2,12 +2,11 @@ const execa = require('execa') const chai = require('chai') +const merge = require('merge-options') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) -const _ = require('lodash') - // This is our new test utility to easily check and execute ipfs cli commands. // // The top level export is a function that can be passed a `repoPath` @@ -19,8 +18,7 @@ const _ = require('lodash') // The `.fail` variation asserts that the command exited with `Code > 0` // and returns a promise that resolves to `stderr`. module.exports = (repoPath, opts) => { - const env = _.clone(process.env) - env.IPFS_PATH = repoPath + const env = merge(process.env, { IPFS_PATH: repoPath }) const config = Object.assign({}, { stripEof: false, From d26fbac831bff22607421a2d64494a88588fd21c Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 24 Dec 2018 11:46:55 +0000 Subject: [PATCH 04/16] fix: reduce bundle size 2 --- src/core/runtime/ipld-browser.js | 8 ++++++ src/core/runtime/ipld-nodejs.js | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/core/runtime/ipld-browser.js create mode 100644 src/core/runtime/ipld-nodejs.js diff --git a/src/core/runtime/ipld-browser.js b/src/core/runtime/ipld-browser.js new file mode 100644 index 0000000000..2420a8dae3 --- /dev/null +++ b/src/core/runtime/ipld-browser.js @@ -0,0 +1,8 @@ +'use strict' +const mergeOptions = require('merge-options') + +module.exports = (blockService, options = {}) => { + return mergeOptions({ + blockService: blockService + }, options) +} diff --git a/src/core/runtime/ipld-nodejs.js b/src/core/runtime/ipld-nodejs.js new file mode 100644 index 0000000000..868453d689 --- /dev/null +++ b/src/core/runtime/ipld-nodejs.js @@ -0,0 +1,47 @@ +'use strict' +const mergeOptions = require('merge-options') + +// All known (non-default) IPLD formats +const IpldFormats = { + get 'bitcoin-block' () { + return require('ipld-bitcoin') + }, + get 'eth-account-snapshot' () { + return require('ipld-ethereum').ethAccountSnapshot + }, + get 'eth-block' () { + return require('ipld-ethereum').ethBlock + }, + get 'eth-block-list' () { + return require('ipld-ethereum').ethBlockList + }, + get 'eth-state-trie' () { + return require('ipld-ethereum').ethStateTrie + }, + get 'eth-storage-trie' () { + return require('ipld-ethereum').ethStorageTrie + }, + get 'eth-tx' () { + return require('ipld-ethereum').ethTx + }, + get 'eth-tx-trie' () { + return require('ipld-ethereum').ethTxTrie + }, + get 'git-raw' () { + return require('ipld-git') + }, + get 'zcash-block' () { + return require('ipld-zcash') + } +} + +module.exports = (blockService, options = {}) => { + return mergeOptions({ + blockService: blockService, + loadFormat: (codec, callback) => { + this.log('Loading IPLD format', codec) + if (IpldFormats[codec]) return callback(null, IpldFormats[codec]) + callback(new Error(`Missing IPLD format "${codec}"`)) + } + }, options) +} From 951c95ad562817fd275894025c9a7f75b6a4e8d1 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Wed, 6 Mar 2019 15:30:13 +0000 Subject: [PATCH 05/16] fix: remove joi-browser --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index ab1c960486..461da330de 100644 --- a/package.json +++ b/package.json @@ -126,7 +126,6 @@ "is-stream": "^1.1.0", "iso-url": "^0.4.4", "joi": "^14.3.0", - "joi-browser": "^13.4.0", "joi-multiaddr": "^4.0.0", "libp2p": "~0.25.0-rc.3", "libp2p-bootstrap": "~0.9.3", From 756861229ba754ee3b68ad3bd5e099fd921c74b8 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 18 Mar 2019 15:40:28 +0000 Subject: [PATCH 06/16] chore: update deps --- package.json | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 461da330de..785c451bc8 100644 --- a/package.json +++ b/package.json @@ -69,8 +69,8 @@ "execa": "^1.0.0", "form-data": "^2.3.3", "hat": "0.0.3", - "interface-ipfs-core": "~0.98.1", - "ipfsd-ctl": "~0.41.0", + "interface-ipfs-core": "~0.99.0", + "ipfsd-ctl": "~0.42.0", "libp2p-websocket-star": "~0.10.2", "ncp": "^2.0.0", "qs": "^6.5.2", @@ -82,7 +82,7 @@ "async": "^2.6.1", "bignumber.js": "^8.0.2", "binary-querystring": "~0.1.2", - "bl": "^2.1.2", + "bl": "^3.0.0", "boom": "^7.2.0", "bs58": "^4.0.1", "byteman": "^1.3.5", @@ -104,14 +104,14 @@ "hoek": "^6.1.2", "human-to-milliseconds": "^1.0.0", "interface-datastore": "~0.6.0", - "ipfs-bitswap": "~0.22.0", + "ipfs-bitswap": "~0.23.0", "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", - "ipfs-http-client": "ipfs/js-ipfs-http-client#fix/bundle-size", + "ipfs-http-client": "^30.1.0", "ipfs-http-response": "~0.2.1", "ipfs-mfs": "~0.9.1", "ipfs-multipart": "~0.1.0", - "ipfs-repo": "~0.26.1", + "ipfs-repo": "ipfs/js-ipfs-repo#master", "ipfs-unixfs": "~0.1.16", "ipfs-unixfs-engine": "~0.35.3", "ipld": "~0.21.1", @@ -121,27 +121,27 @@ "ipld-git": "~0.2.2", "ipld-zcash": "~0.1.6", "ipns": "~0.5.0", - "is-ipfs": "~0.4.8", + "is-ipfs": "~0.6.0", "is-pull-stream": "~0.0.0", "is-stream": "^1.1.0", "iso-url": "^0.4.4", "joi": "^14.3.0", "joi-multiaddr": "^4.0.0", + "just-flatten-it": "^2.1.0", + "just-range": "^2.1.0", + "just-safe-set": "^2.1.0", "libp2p": "~0.25.0-rc.3", "libp2p-bootstrap": "~0.9.3", "libp2p-crypto": "~0.16.0", "libp2p-kad-dht": "~0.14.7", - "libp2p-keychain": "~0.3.3", - "just-flatten-it": "^2.1.0", - "just-range": "^2.1.0", - "just-safe-set": "^2.1.0", + "libp2p-keychain": "~0.4.1", "libp2p-mdns": "~0.12.0", - "libp2p-mplex": "libp2p/js-libp2p-mplex#fix/bundle-size", + "libp2p-mplex": "~0.8.4", "libp2p-record": "~0.6.1", "libp2p-secio": "~0.11.0", "libp2p-tcp": "~0.13.0", "libp2p-webrtc-star": "~0.15.5", - "libp2p-websocket-star-multi": "~0.4.0", + "libp2p-websocket-star-multi": "hugomrdias/js-libp2p-websocket-star-multi#fix/bundle-size", "libp2p-websockets": "~0.12.2", "mafmt": "^6.0.2", "merge-options": "^1.0.1", @@ -179,7 +179,7 @@ "update-notifier": "^2.5.0", "uri-to-multiaddr": "^3.0.1", "varint": "^5.0.0", - "yargs": "^12.0.5", + "yargs": "^13.2.2", "yargs-promise": "^1.1.0" }, "optionalDependencies": { @@ -299,4 +299,4 @@ "Łukasz Magiera ", "Максим Ильин " ] -} \ No newline at end of file +} From 5dfdb553f9838e0cef87e08f1693a390b120fb4e Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 18 Mar 2019 15:42:04 +0000 Subject: [PATCH 07/16] chore: update deps 2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 785c451bc8..fc272a59cb 100644 --- a/package.json +++ b/package.json @@ -111,7 +111,7 @@ "ipfs-http-response": "~0.2.1", "ipfs-mfs": "~0.9.1", "ipfs-multipart": "~0.1.0", - "ipfs-repo": "ipfs/js-ipfs-repo#master", + "ipfs-repo": "~0.26.4", "ipfs-unixfs": "~0.1.16", "ipfs-unixfs-engine": "~0.35.3", "ipld": "~0.21.1", From 926c9ce80619750ef10c9f1023c54ae33fd345f5 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 18 Mar 2019 16:00:36 +0000 Subject: [PATCH 08/16] fix: fix rebase --- package.json | 2 +- src/cli/commands/add.js | 19 ++++++++++--------- src/core/components/libp2p.js | 1 - src/http/api/resources/pin.js | 4 +--- test/core/bitswap.spec.js | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index fc272a59cb..5227d26ac3 100644 --- a/package.json +++ b/package.json @@ -124,7 +124,7 @@ "is-ipfs": "~0.6.0", "is-pull-stream": "~0.0.0", "is-stream": "^1.1.0", - "iso-url": "^0.4.4", + "iso-url": "~0.4.4", "joi": "^14.3.0", "joi-multiaddr": "^4.0.0", "just-flatten-it": "^2.1.0", diff --git a/src/cli/commands/add.js b/src/cli/commands/add.js index 40bd9b0e74..9bb1572ed6 100644 --- a/src/cli/commands/add.js +++ b/src/cli/commands/add.js @@ -1,8 +1,5 @@ 'use strict' -const fs = require('fs') -const path = require('path') -const glob = require('glob') const pull = require('pull-stream') const promisify = require('promisify-es6') const getFolderSize = promisify(require('get-folder-size')) @@ -40,12 +37,16 @@ function addPipeline (source, addStream, options) { return resolve() } - added - .sort((a, b) => a.path.localCompare(b.path)) - .reverse() - .map((file) => { - const log = [ 'added', file.hash ] - + added + .sort((a, b) => a.path.localCompare(b.path)) + .reverse() + .map((file) => { + const log = options.quiet ? [] : ['added'] + log.push(cidToString(file.hash, { base: options.cidBase })) + if (!options.quiet && file.path.length > 0) log.push(file.path) + return log.join(' ') + }) + .forEach((msg) => print(msg)) resolve() }) ) diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index 7c856fc494..0390c3a72b 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -1,6 +1,5 @@ 'use strict' -const promisify = require('promisify-es6') const get = require('dlv') const ipnsUtils = require('../ipns/routing/utils') const mergeOptions = require('merge-options') diff --git a/src/http/api/resources/pin.js b/src/http/api/resources/pin.js index bba80c26cf..09468eebef 100644 --- a/src/http/api/resources/pin.js +++ b/src/http/api/resources/pin.js @@ -62,13 +62,11 @@ exports.ls = { return h.response({ Keys: result.reduce((acc, v) => { - const prop = cidToString(obj.hash, { base: request.query['cid-base'] }) + const prop = cidToString(v.hash, { base: request.query['cid-base'] }) acc[v[prop]] = { Type: v.type } return acc }, {}) }) - - }) } } diff --git a/test/core/bitswap.spec.js b/test/core/bitswap.spec.js index 9f3e23c53d..33bd3302cb 100644 --- a/test/core/bitswap.spec.js +++ b/test/core/bitswap.spec.js @@ -36,7 +36,7 @@ function wire (targetNode, dialerNode, callback) { expect(err).to.not.exist() const addr = identity.addresses .map((addr) => multiaddr(addr.toString().split('ipfs')[0])) - .filter((addr) => _.includes(addr.protoNames(), 'ws'))[0] + .filter((addr) => addr.protoNames().includes('ws'))[0] if (!addr) { // Note: the browser doesn't have a websockets listening addr From 132d81416a5a97fdabcd4cf7c05febb3ccf5d86c Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 18 Mar 2019 16:32:00 +0000 Subject: [PATCH 09/16] chore: add missing deps --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index 5227d26ac3..52908d3bc6 100644 --- a/package.json +++ b/package.json @@ -143,6 +143,7 @@ "libp2p-webrtc-star": "~0.15.5", "libp2p-websocket-star-multi": "hugomrdias/js-libp2p-websocket-star-multi#fix/bundle-size", "libp2p-websockets": "~0.12.2", + "lodash": "^4.17.11", "mafmt": "^6.0.2", "merge-options": "^1.0.1", "mime-types": "^2.1.21", @@ -174,6 +175,7 @@ "readable-stream": "^3.1.1", "receptacle": "^1.3.2", "stream-to-pull-stream": "^1.7.2", + "superstruct": "^0.6.0", "tar-stream": "^2.0.0", "temp": "~0.9.0", "update-notifier": "^2.5.0", From 19414617021be3d3546427eb0138e9cffd47a129 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 18 Mar 2019 16:37:28 +0000 Subject: [PATCH 10/16] chore: fix semver lint --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 52908d3bc6..bab76cf467 100644 --- a/package.json +++ b/package.json @@ -175,7 +175,7 @@ "readable-stream": "^3.1.1", "receptacle": "^1.3.2", "stream-to-pull-stream": "^1.7.2", - "superstruct": "^0.6.0", + "superstruct": "~0.6.0", "tar-stream": "^2.0.0", "temp": "~0.9.0", "update-notifier": "^2.5.0", From df1132718f0a36a0a7b877e7c7fc89357fafdffc Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 19 Mar 2019 10:23:39 +0000 Subject: [PATCH 11/16] fix: debug string Co-Authored-By: hugomrdias --- src/http/api/resources/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/http/api/resources/config.js b/src/http/api/resources/config.js index dc195ce032..f37a9e4cd3 100644 --- a/src/http/api/resources/config.js +++ b/src/http/api/resources/config.js @@ -3,7 +3,7 @@ const debug = require('debug') const get = require('dlv') const set = require('just-safe-set') -const log = debug('jsipfs:http-api:config') +const log = debug('ipfs:http-api:config') log.error = debug('jsipfs:http-api:config:error') const multipart = require('ipfs-multipart') const Boom = require('boom') From be10447702facdffe0d90b2ed45643ea63e14632 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 19 Mar 2019 10:24:01 +0000 Subject: [PATCH 12/16] fix: fix debug string Co-Authored-By: hugomrdias --- src/http/api/resources/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/http/api/resources/config.js b/src/http/api/resources/config.js index f37a9e4cd3..1c5c08e369 100644 --- a/src/http/api/resources/config.js +++ b/src/http/api/resources/config.js @@ -4,7 +4,7 @@ const debug = require('debug') const get = require('dlv') const set = require('just-safe-set') const log = debug('ipfs:http-api:config') -log.error = debug('jsipfs:http-api:config:error') +log.error = debug('ipfs:http-api:config:error') const multipart = require('ipfs-multipart') const Boom = require('boom') From 8a85fd7a1ad2e49851552341732738e49f949762 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Wed, 20 Mar 2019 11:02:14 +0000 Subject: [PATCH 13/16] fix: fix stuff --- package.json | 2 +- src/cli/commands/add.js | 11 ++++++-- src/core/config.js | 47 ++++++++++++++++---------------- src/core/index.js | 2 +- src/core/runtime/ipld-nodejs.js | 4 +-- src/http/api/resources/config.js | 9 ++++-- src/http/api/resources/pin.js | 2 +- test/cli/dag.js | 18 ++++++------ test/cli/files.js | 5 ++-- test/utils/ipfs-exec.js | 5 +--- 10 files changed, 57 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index bab76cf467..471609e8eb 100644 --- a/package.json +++ b/package.json @@ -141,7 +141,7 @@ "libp2p-secio": "~0.11.0", "libp2p-tcp": "~0.13.0", "libp2p-webrtc-star": "~0.15.5", - "libp2p-websocket-star-multi": "hugomrdias/js-libp2p-websocket-star-multi#fix/bundle-size", + "libp2p-websocket-star-multi": "hugomrdias/js-libp2p-websocket-star-multi#master", "libp2p-websockets": "~0.12.2", "lodash": "^4.17.11", "mafmt": "^6.0.2", diff --git a/src/cli/commands/add.js b/src/cli/commands/add.js index 9bb1572ed6..4ad22b7f6b 100644 --- a/src/cli/commands/add.js +++ b/src/cli/commands/add.js @@ -36,9 +36,16 @@ function addPipeline (source, addStream, options) { print(added.pop().hash) return resolve() } - added - .sort((a, b) => a.path.localCompare(b.path)) + .sort((a, b) => { + if (a.path > b.path) { + return 1 + } + if (a.path < b.path) { + return -1 + } + return 0 + }) .reverse() .map((file) => { const log = options.quiet ? [] : ['added'] diff --git a/src/core/config.js b/src/core/config.js index 7714b0702b..384340ad3b 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -3,25 +3,23 @@ const Multiaddr = require('multiaddr') const mafmt = require('mafmt') const { struct, superstruct } = require('superstruct') -const { optional, list, union } = struct +const { optional, union } = struct const s = superstruct({ - multiaddr: v => { - if (v === null) { - return `multiaddr invalid, value must be a string, Buffer, or another Multiaddr got ${v}` - } + types: { + multiaddr: v => { + if (v === null) { + return `multiaddr invalid, value must be a string, Buffer, or another Multiaddr got ${v}` + } - try { - Multiaddr(v) - } catch (err) { - return `multiaddr invalid, ${err.message}` - } + try { + Multiaddr(v) + } catch (err) { + return `multiaddr invalid, ${err.message}` + } - return true - }, - 'multiaddr-ipfs': v => { - return mafmt.IPFS.matches(v) - ? true - : `multiaddr IPFS invalid` + return true + }, + 'multiaddr-ipfs': v => mafmt.IPFS.matches(v) ? true : `multiaddr IPFS invalid` } }) @@ -30,13 +28,14 @@ const configSchema = s({ repoOwner: 'boolean?', preload: s({ enabled: 'boolean?', - addresses: optional(list(['multiaddr'])), + addresses: optional(s(['multiaddr'])), interval: 'number?' }, { enabled: true, interval: 30 * 1000 }), init: optional(union(['boolean', s({ bits: 'number?' })])), start: 'boolean?', - local: 'boolean?', + offline: 'boolean?', pass: 'string?', + silent: 'boolean?', relay: 'object?', // relay validates in libp2p EXPERIMENTAL: optional(s({ pubsub: 'boolean?', @@ -46,11 +45,11 @@ const configSchema = s({ })), connectionManager: 'object?', config: optional(s({ - Addresses: s({ - Swarm: optional(list(['multiaddr'])), + Addresses: optional(s({ + Swarm: optional(s(['multiaddr'])), API: 'multiaddr?', Gateway: 'multiaddr' - }), + })), Discovery: optional(s({ MDSN: optional(s({ Enabled: 'boolean?', @@ -58,9 +57,9 @@ const configSchema = s({ })), webRTCStar: optional(s({ Enabled: 'boolean?' - })), - Bootstrap: optional(list(['multiaddr-ipfs'])) - })) + })) + })), + Bootstrap: optional(s(['multiaddr-ipfs'])) })), libp2p: optional(union(['function', 'object'])) // libp2p validates this }, { diff --git a/src/core/index.js b/src/core/index.js index 80bf396060..637040febe 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -72,7 +72,7 @@ class IPFS extends EventEmitter { this._peerInfo = undefined this._bitswap = undefined this._blockService = new BlockService(this._repo) - this._ipld = new Ipld(ipldOptions(this._blockService, this._options.ipld)) + this._ipld = new Ipld(ipldOptions(this._blockService, this._options.ipld, this.log)) this._preload = preload(this) this._mfsPreload = mfsPreload(this) this._ipns = undefined diff --git a/src/core/runtime/ipld-nodejs.js b/src/core/runtime/ipld-nodejs.js index 868453d689..e2d143d0e0 100644 --- a/src/core/runtime/ipld-nodejs.js +++ b/src/core/runtime/ipld-nodejs.js @@ -35,11 +35,11 @@ const IpldFormats = { } } -module.exports = (blockService, options = {}) => { +module.exports = (blockService, options = {}, log) => { return mergeOptions({ blockService: blockService, loadFormat: (codec, callback) => { - this.log('Loading IPLD format', codec) + log('Loading IPLD format', codec) if (IpldFormats[codec]) return callback(null, IpldFormats[codec]) callback(new Error(`Missing IPLD format "${codec}"`)) } diff --git a/src/http/api/resources/config.js b/src/http/api/resources/config.js index 1c5c08e369..d0ee5011b8 100644 --- a/src/http/api/resources/config.js +++ b/src/http/api/resources/config.js @@ -74,11 +74,14 @@ exports.getOrSet = { } } else { // Set the new value of a given key - const updatedConfig = set(originalConfig, key, value) + const result = set(originalConfig, key, value) + if (!result) { + throw Boom.badRequest('Failed to set config value') + } try { - await ipfs.config.replace(updatedConfig) + await ipfs.config.replace(originalConfig) } catch (err) { - throw Boom.boomify(err, { message: 'Failed to get config value' }) + throw Boom.boomify(err, { message: 'Failed to replace config value' }) } } diff --git a/src/http/api/resources/pin.js b/src/http/api/resources/pin.js index 09468eebef..29e80dcd9d 100644 --- a/src/http/api/resources/pin.js +++ b/src/http/api/resources/pin.js @@ -63,7 +63,7 @@ exports.ls = { return h.response({ Keys: result.reduce((acc, v) => { const prop = cidToString(v.hash, { base: request.query['cid-base'] }) - acc[v[prop]] = { Type: v.type } + acc[prop] = { Type: v.type } return acc }, {}) }) diff --git a/test/cli/dag.js b/test/cli/dag.js index 9cd6a3a586..ba0c9f65b3 100644 --- a/test/cli/dag.js +++ b/test/cli/dag.js @@ -15,13 +15,15 @@ describe('dag', () => runOnAndOff.off((thing) => { this.timeout(20 * 1000) // put test eth-block - return ipfs('block put --format eth-block --mhtype keccak-256 test/fixtures/test-data/eth-block').then((out) => { - expect(out).to.eql('z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS\n') - // lookup path on eth-block - return ipfs('dag get z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS/parentHash') - }).then((out) => { - let expectHash = Buffer.from('c8c0a17305adea9bbb4b98a52d44f0c1478f5c48fc4b64739ee805242501b256', 'hex') - expect(out).to.be.eql('0x' + expectHash.toString('hex') + '\n') - }) + return ipfs('block put --format eth-block --mhtype keccak-256 test/fixtures/test-data/eth-block') + .then((out) => { + expect(out).to.eql('z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS\n') + // lookup path on eth-block + return ipfs('dag get z43AaGF23fmvRnDP56Ub9WcJCfzSfqtmzNCCvmz5eudT8dtdCDS/parentHash') + }) + .then((out) => { + let expectHash = Buffer.from('c8c0a17305adea9bbb4b98a52d44f0c1478f5c48fc4b64739ee805242501b256', 'hex') + expect(out).to.be.eql('0x' + expectHash.toString('hex') + '\n') + }) }) })) diff --git a/test/cli/files.js b/test/cli/files.js index e9a6c69422..14b83c2201 100644 --- a/test/cli/files.js +++ b/test/cli/files.js @@ -311,7 +311,7 @@ describe('files', () => runOnAndOff((thing) => { it('add --silent', function () { this.timeout(30 * 1000) - return ipfs('add --silent src/init-files/init-docs/readme') + return ipfs('add src/init-files/init-docs/readme --silent') .then((out) => { expect(out) .to.eql('') @@ -347,7 +347,7 @@ describe('files', () => runOnAndOff((thing) => { }) }) - it('add pins by default', function () { + it.only('add pins by default', function () { this.timeout(10 * 1000) const filePath = path.join(os.tmpdir(), hat()) const content = String(Math.random()) @@ -356,6 +356,7 @@ describe('files', () => runOnAndOff((thing) => { return ipfs(`add -Q ${filePath}`) .then(out => { const hash = out.trim() + console.log('TCL: hash', hash) return ipfs(`pin ls ${hash}`) .then(ls => expect(ls).to.include(hash)) }) diff --git a/test/utils/ipfs-exec.js b/test/utils/ipfs-exec.js index acfe54752d..7198dd4938 100644 --- a/test/utils/ipfs-exec.js +++ b/test/utils/ipfs-exec.js @@ -2,7 +2,6 @@ const execa = require('execa') const chai = require('chai') -const merge = require('merge-options') const dirtyChai = require('dirty-chai') const expect = chai.expect chai.use(dirtyChai) @@ -18,11 +17,9 @@ chai.use(dirtyChai) // The `.fail` variation asserts that the command exited with `Code > 0` // and returns a promise that resolves to `stderr`. module.exports = (repoPath, opts) => { - const env = merge(process.env, { IPFS_PATH: repoPath }) - const config = Object.assign({}, { stripEof: false, - env: env, + env: Object.assign({ IPFS_PATH: repoPath }, process.env), timeout: 60 * 1000 }, opts) From f777a222b118f57489f3f147340ceb45b7646345 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Wed, 20 Mar 2019 15:44:11 +0000 Subject: [PATCH 14/16] fix: more fixes --- package.json | 4 ++-- src/core/config.js | 3 ++- test/cli/files.js | 3 +-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 471609e8eb..e545af7fc5 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "form-data": "^2.3.3", "hat": "0.0.3", "interface-ipfs-core": "~0.99.0", - "ipfsd-ctl": "~0.42.0", + "ipfsd-ctl": "ipfs/js-ipfsd-ctl#fix/config", "libp2p-websocket-star": "~0.10.2", "ncp": "^2.0.0", "qs": "^6.5.2", @@ -141,7 +141,7 @@ "libp2p-secio": "~0.11.0", "libp2p-tcp": "~0.13.0", "libp2p-webrtc-star": "~0.15.5", - "libp2p-websocket-star-multi": "hugomrdias/js-libp2p-websocket-star-multi#master", + "libp2p-websocket-star-multi": "~0.4.4", "libp2p-websockets": "~0.12.2", "lodash": "^4.17.11", "mafmt": "^6.0.2", diff --git a/src/core/config.js b/src/core/config.js index 384340ad3b..6e64ebe99e 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -45,13 +45,14 @@ const configSchema = s({ })), connectionManager: 'object?', config: optional(s({ + API: 'object?', Addresses: optional(s({ Swarm: optional(s(['multiaddr'])), API: 'multiaddr?', Gateway: 'multiaddr' })), Discovery: optional(s({ - MDSN: optional(s({ + MDNS: optional(s({ Enabled: 'boolean?', Interval: 'number?' })), diff --git a/test/cli/files.js b/test/cli/files.js index 14b83c2201..e5237ff0cc 100644 --- a/test/cli/files.js +++ b/test/cli/files.js @@ -347,7 +347,7 @@ describe('files', () => runOnAndOff((thing) => { }) }) - it.only('add pins by default', function () { + it('add pins by default', function () { this.timeout(10 * 1000) const filePath = path.join(os.tmpdir(), hat()) const content = String(Math.random()) @@ -356,7 +356,6 @@ describe('files', () => runOnAndOff((thing) => { return ipfs(`add -Q ${filePath}`) .then(out => { const hash = out.trim() - console.log('TCL: hash', hash) return ipfs(`pin ls ${hash}`) .then(ls => expect(ls).to.include(hash)) }) From 25e538a687857808e4abea85db7b98c685b6de57 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 21 Mar 2019 15:09:27 +0000 Subject: [PATCH 15/16] chore: update ctl --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e545af7fc5..f1dc8de4ce 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "form-data": "^2.3.3", "hat": "0.0.3", "interface-ipfs-core": "~0.99.0", - "ipfsd-ctl": "ipfs/js-ipfsd-ctl#fix/config", + "ipfsd-ctl": "~0.42.1", "libp2p-websocket-star": "~0.10.2", "ncp": "^2.0.0", "qs": "^6.5.2", From 13eab63a11d948d82932db785529366f45c24aac Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 22 Mar 2019 10:30:07 +0000 Subject: [PATCH 16/16] fix: fix init config validation --- src/core/config.js | 7 +++++- test/core/bitswap.spec.js | 6 ++--- test/core/config.spec.js | 51 +-------------------------------------- 3 files changed, 9 insertions(+), 55 deletions(-) diff --git a/src/core/config.js b/src/core/config.js index 6e64ebe99e..327f445d8e 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -31,7 +31,12 @@ const configSchema = s({ addresses: optional(s(['multiaddr'])), interval: 'number?' }, { enabled: true, interval: 30 * 1000 }), - init: optional(union(['boolean', s({ bits: 'number?' })])), + init: optional(union(['boolean', s({ + bits: 'number?', + emptyRepo: 'boolean?', + privateKey: optional(s('object|string')), // object should be a custom type for PeerId using 'kind-of' + pass: 'string?' + })])), start: 'boolean?', offline: 'boolean?', pass: 'string?', diff --git a/test/core/bitswap.spec.js b/test/core/bitswap.spec.js index 33bd3302cb..aba37ccfa5 100644 --- a/test/core/bitswap.spec.js +++ b/test/core/bitswap.spec.js @@ -113,10 +113,8 @@ describe('bitswap', function () { if (isNode) { config = Object.assign({}, config, { - config: { - Addresses: { - Swarm: ['/ip4/127.0.0.1/tcp/0'] - } + Addresses: { + Swarm: ['/ip4/127.0.0.1/tcp/0'] } }) } diff --git a/test/core/config.spec.js b/test/core/config.spec.js index 3250c5b9d4..4ce3ffab05 100644 --- a/test/core/config.spec.js +++ b/test/core/config.spec.js @@ -19,16 +19,10 @@ describe('config', () => { expect(() => config.validate(cfg)).to.not.throw() }) - it('should allow unknown key at root', () => { - const cfg = { [`${Date.now()}`]: 'test' } - expect(() => config.validate(cfg)).to.not.throw() - }) - it('should validate valid repo', () => { const cfgs = [ { repo: { unknown: 'value' } }, { repo: '/path/to-repo' }, - { repo: null }, { repo: undefined } ] @@ -46,10 +40,8 @@ describe('config', () => { it('should validate valid init', () => { const cfgs = [ { init: { bits: 138 } }, - { init: { bits: 138, unknown: 'value' } }, { init: true }, { init: false }, - { init: null }, { init: undefined } ] @@ -104,36 +96,10 @@ describe('config', () => { cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw()) }) - it('should validate valid relay', () => { - const cfgs = [ - { relay: { enabled: true, hop: { enabled: true } } }, - { relay: { enabled: false, hop: { enabled: false } } }, - { relay: { enabled: false, hop: null } }, - { relay: { enabled: false } }, - { relay: null }, - { relay: undefined } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.not.throw()) - }) - - it('should validate invalid relay', () => { - const cfgs = [ - { relay: 138 }, - { relay: { enabled: 138 } }, - { relay: { enabled: true, hop: 138 } }, - { relay: { enabled: true, hop: { enabled: 138 } } } - ] - - cfgs.forEach(cfg => expect(() => config.validate(cfg)).to.throw()) - }) - it('should validate valid EXPERIMENTAL', () => { const cfgs = [ { EXPERIMENTAL: { pubsub: true, dht: true, sharding: true } }, { EXPERIMENTAL: { pubsub: false, dht: false, sharding: false } }, - { EXPERIMENTAL: { unknown: 'value' } }, - { EXPERIMENTAL: null }, { EXPERIMENTAL: undefined } ] @@ -162,32 +128,22 @@ describe('config', () => { { config: { Addresses: { Gateway: '/ip4/127.0.0.1/tcp/9090' } } }, { config: { Addresses: { Gateway: undefined } } }, - { config: { Addresses: { unknown: 'value' } } }, - { config: { Addresses: null } }, { config: { Addresses: undefined } }, { config: { Discovery: { MDNS: { Enabled: true } } } }, { config: { Discovery: { MDNS: { Enabled: false } } } }, { config: { Discovery: { MDNS: { Interval: 138 } } } }, - { config: { Discovery: { MDNS: { unknown: 'value' } } } }, - { config: { Discovery: { MDNS: null } } }, { config: { Discovery: { MDNS: undefined } } }, { config: { Discovery: { webRTCStar: { Enabled: true } } } }, { config: { Discovery: { webRTCStar: { Enabled: false } } } }, - { config: { Discovery: { webRTCStar: { unknown: 'value' } } } }, - { config: { Discovery: { webRTCStar: null } } }, { config: { Discovery: { webRTCStar: undefined } } }, - { config: { Discovery: { unknown: 'value' } } }, - { config: { Discovery: null } }, { config: { Discovery: undefined } }, { config: { Bootstrap: ['/ip4/104.236.176.52/tcp/4001/ipfs/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z'] } }, { config: { Bootstrap: [] } }, - { config: { unknown: 'value' } }, - { config: null }, { config: undefined } ] @@ -222,12 +178,7 @@ describe('config', () => { it('should validate valid libp2p', () => { const cfgs = [ { libp2p: { modules: {} } }, - { libp2p: { modules: { unknown: 'value' } } }, - { libp2p: { modules: null } }, - { libp2p: { modules: undefined } }, - { libp2p: { unknown: 'value' } }, { libp2p: () => {} }, - { libp2p: null }, { libp2p: undefined } ] @@ -236,7 +187,7 @@ describe('config', () => { it('should validate invalid libp2p', () => { const cfgs = [ - { libp2p: { modules: 138 } }, + { libp2p: 'error' }, { libp2p: 138 } ]