From bfa1c9966b5e85338664e9d2c9b5fb84c552be24 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 20 Jan 2020 09:59:17 +0000 Subject: [PATCH] refactor: renames and deletes prior to async/await --- src/cli/commands/file.js | 15 - src/cli/commands/file/ls.js | 27 -- src/core/boot.js | 90 ----- .../add-async-iterator.js => add/index.js} | 0 .../{files-regular => add}/utils.js | 0 .../{bitswap.js => bitswap/stat.js} | 0 .../components/{block.js => block/put.js} | 0 .../{bootstrap.js => bootstrap/add.js} | 0 .../cat-async-iterator.js => cat.js} | 0 src/core/components/{dag.js => dag/put.js} | 0 .../components/files-regular/add-from-fs.js | 3 - .../files-regular/add-from-stream.js | 3 - .../components/files-regular/add-from-url.js | 23 -- .../files-regular/add-pull-stream.js | 11 - .../files-regular/add-readable-stream.js | 13 - src/core/components/files-regular/add.js | 22 -- .../files-regular/cat-pull-stream.js | 9 - .../files-regular/cat-readable-stream.js | 11 - src/core/components/files-regular/cat.js | 10 - .../files-regular/get-pull-stream.js | 20 -- .../files-regular/get-readable-stream.js | 19 -- src/core/components/files-regular/get.js | 18 - src/core/components/files-regular/index.js | 34 -- .../files-regular/ls-pull-stream.js | 9 - .../files-regular/ls-readable-stream.js | 11 - src/core/components/files-regular/ls.js | 10 - .../files-regular/refs-local-pull-stream.js | 9 - .../refs-local-readable-stream.js | 11 - .../components/files-regular/refs-local.js | 10 - .../files-regular/refs-pull-stream.js | 9 - .../files-regular/refs-readable-stream.js | 11 - src/core/components/files-regular/refs.js | 16 - .../get-async-iterator.js => get.js} | 0 src/core/components/{key.js => key/rename.js} | 0 .../ls-async-iterator.js => ls.js} | 0 .../{name-pubsub.js => name/pubsub/utils.js} | 0 .../path.js => components/name/utils.js} | 0 .../components/{object.js => object/put.js} | 0 src/core/components/pin/gc-lock.js | 83 ----- src/core/components/{pin.js => pin/ls.js} | 0 src/core/components/ping-pull-stream.js | 100 ------ src/core/components/ping-readable-stream.js | 7 - src/core/components/pre-start.js | 75 ----- .../refs-async-iterator.js => refs/index.js} | 0 .../local.js} | 0 src/core/components/{pin => repo}/gc.js | 0 .../components/{repo.js => repo/version.js} | 0 src/core/components/{stats.js => stats/bw.js} | 0 .../components/{swarm.js => swarm/peers.js} | 0 src/core/runtime/add-from-fs-browser.js | 9 - src/core/runtime/add-from-fs-nodejs.js | 13 - .../init-assets-nodejs.js} | 0 src/core/state.js | 65 ---- src/http/api/resources/file.js | 103 ------ src/http/api/routes/file.js | 15 - src/utils/mutex.js | 52 --- test/cli/file.js | 38 --- test/core/config.spec.js | 223 ------------ test/core/gc-lock.spec.js | 316 ------------------ test/utils/expect-timeout.js | 16 - 60 files changed, 1539 deletions(-) delete mode 100644 src/cli/commands/file.js delete mode 100644 src/cli/commands/file/ls.js delete mode 100644 src/core/boot.js rename src/core/components/{files-regular/add-async-iterator.js => add/index.js} (100%) rename src/core/components/{files-regular => add}/utils.js (100%) rename src/core/components/{bitswap.js => bitswap/stat.js} (100%) rename src/core/components/{block.js => block/put.js} (100%) rename src/core/components/{bootstrap.js => bootstrap/add.js} (100%) rename src/core/components/{files-regular/cat-async-iterator.js => cat.js} (100%) rename src/core/components/{dag.js => dag/put.js} (100%) delete mode 100644 src/core/components/files-regular/add-from-fs.js delete mode 100644 src/core/components/files-regular/add-from-stream.js delete mode 100644 src/core/components/files-regular/add-from-url.js delete mode 100644 src/core/components/files-regular/add-pull-stream.js delete mode 100644 src/core/components/files-regular/add-readable-stream.js delete mode 100644 src/core/components/files-regular/add.js delete mode 100644 src/core/components/files-regular/cat-pull-stream.js delete mode 100644 src/core/components/files-regular/cat-readable-stream.js delete mode 100644 src/core/components/files-regular/cat.js delete mode 100644 src/core/components/files-regular/get-pull-stream.js delete mode 100644 src/core/components/files-regular/get-readable-stream.js delete mode 100644 src/core/components/files-regular/get.js delete mode 100644 src/core/components/files-regular/index.js delete mode 100644 src/core/components/files-regular/ls-pull-stream.js delete mode 100644 src/core/components/files-regular/ls-readable-stream.js delete mode 100644 src/core/components/files-regular/ls.js delete mode 100644 src/core/components/files-regular/refs-local-pull-stream.js delete mode 100644 src/core/components/files-regular/refs-local-readable-stream.js delete mode 100644 src/core/components/files-regular/refs-local.js delete mode 100644 src/core/components/files-regular/refs-pull-stream.js delete mode 100644 src/core/components/files-regular/refs-readable-stream.js delete mode 100644 src/core/components/files-regular/refs.js rename src/core/components/{files-regular/get-async-iterator.js => get.js} (100%) rename src/core/components/{key.js => key/rename.js} (100%) rename src/core/components/{files-regular/ls-async-iterator.js => ls.js} (100%) rename src/core/components/{name-pubsub.js => name/pubsub/utils.js} (100%) rename src/core/{ipns/path.js => components/name/utils.js} (100%) rename src/core/components/{object.js => object/put.js} (100%) delete mode 100644 src/core/components/pin/gc-lock.js rename src/core/components/{pin.js => pin/ls.js} (100%) delete mode 100644 src/core/components/ping-pull-stream.js delete mode 100644 src/core/components/ping-readable-stream.js delete mode 100644 src/core/components/pre-start.js rename src/core/components/{files-regular/refs-async-iterator.js => refs/index.js} (100%) rename src/core/components/{files-regular/refs-local-async-iterator.js => refs/local.js} (100%) rename src/core/components/{pin => repo}/gc.js (100%) rename src/core/components/{repo.js => repo/version.js} (100%) rename src/core/components/{stats.js => stats/bw.js} (100%) rename src/core/components/{swarm.js => swarm/peers.js} (100%) delete mode 100644 src/core/runtime/add-from-fs-browser.js delete mode 100644 src/core/runtime/add-from-fs-nodejs.js rename src/core/{components/init-assets.js => runtime/init-assets-nodejs.js} (100%) delete mode 100644 src/core/state.js delete mode 100644 src/http/api/resources/file.js delete mode 100644 src/http/api/routes/file.js delete mode 100644 src/utils/mutex.js delete mode 100644 test/cli/file.js delete mode 100644 test/core/config.spec.js delete mode 100644 test/core/gc-lock.spec.js delete mode 100644 test/utils/expect-timeout.js diff --git a/src/cli/commands/file.js b/src/cli/commands/file.js deleted file mode 100644 index 0c178f0cd2..0000000000 --- a/src/cli/commands/file.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict' - -module.exports = { - command: 'file ', - - description: 'Interact with IPFS objects representing Unix filesystems.', - - builder (yargs) { - return yargs - .commandDir('file') - }, - - handler (argv) { - } -} diff --git a/src/cli/commands/file/ls.js b/src/cli/commands/file/ls.js deleted file mode 100644 index 20be00c5cc..0000000000 --- a/src/cli/commands/file/ls.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict' - -module.exports = { - command: 'ls ', - - describe: 'List directory contents for Unix filesystem objects.', - - builder: {}, - - handler (argv) { - argv.resolve((async () => { - const path = argv.key - // `ipfs file ls` is deprecated. See https://ipfs.io/docs/commands/#ipfs-file-ls - argv.print('This functionality is deprecated, and will be removed in future versions. If possible, please use \'ipfs ls\' instead.') - - const ipfs = await argv.getIpfs() - let links = await ipfs.ls(path) - - // Single file? Then print its hash - if (links.length === 0) { - links = [{ hash: path }] - } - - links.forEach((file) => argv.print(file.hash)) - })()) - } -} diff --git a/src/core/boot.js b/src/core/boot.js deleted file mode 100644 index aa47be16af..0000000000 --- a/src/core/boot.js +++ /dev/null @@ -1,90 +0,0 @@ -'use strict' - -const RepoErrors = require('ipfs-repo').errors - -// Boot an IPFS node depending on the options set -module.exports = async (self) => { - self.log('booting') - const options = self._options - const doInit = options.init - const doStart = options.start - - // Checks if a repo exists, and if so opens it - // Will return callback with a bool indicating the existence - // of the repo - async function repoOpened () { - // nothing to do - if (!self._repo.closed) { - return true - } - - try { - await self._repo.open() - } catch (err) { - if (isRepoUninitializedError(err)) { - return false - } - - if (err) { - throw err - } - } - - return true - } - - // Do the actual boot sequence - try { - // Init with existing initialized, opened, repo - if (await repoOpened()) { - try { - await self.init({ repo: self._repo }) - } catch (err) { - throw Object.assign(err, { emitted: true }) - } - } else if (doInit) { - const defaultInitOptions = { - bits: 2048, - pass: self._options.pass - } - - const initOptions = Object.assign(defaultInitOptions, typeof options.init === 'object' ? options.init : {}) - - await self.init(initOptions) - } - - if (doStart) { - await self.start() - } - - self.log('booted') - self.emit('ready') - } catch (err) { - if (!err.emitted) { - self.emit('error', err) - } - } -} - -function isRepoUninitializedError (err) { - // If the error is that no repo exists, - // which happens when the version file is not found - // we just want to signal that no repo exist, not - // fail the whole process. - - // Use standardized errors as much as possible - if (err.code === RepoErrors.ERR_REPO_NOT_INITIALIZED) { - return true - } - - // TODO: As error codes continue to be standardized, this logic can be phase out; - // it is here to maintain compatibility - if (err.message.match(/not found/) || // indexeddb - err.message.match(/ENOENT/) || // fs - err.message.match(/No value/) // memory - ) { - return true - } - - return false -} diff --git a/src/core/components/files-regular/add-async-iterator.js b/src/core/components/add/index.js similarity index 100% rename from src/core/components/files-regular/add-async-iterator.js rename to src/core/components/add/index.js diff --git a/src/core/components/files-regular/utils.js b/src/core/components/add/utils.js similarity index 100% rename from src/core/components/files-regular/utils.js rename to src/core/components/add/utils.js diff --git a/src/core/components/bitswap.js b/src/core/components/bitswap/stat.js similarity index 100% rename from src/core/components/bitswap.js rename to src/core/components/bitswap/stat.js diff --git a/src/core/components/block.js b/src/core/components/block/put.js similarity index 100% rename from src/core/components/block.js rename to src/core/components/block/put.js diff --git a/src/core/components/bootstrap.js b/src/core/components/bootstrap/add.js similarity index 100% rename from src/core/components/bootstrap.js rename to src/core/components/bootstrap/add.js diff --git a/src/core/components/files-regular/cat-async-iterator.js b/src/core/components/cat.js similarity index 100% rename from src/core/components/files-regular/cat-async-iterator.js rename to src/core/components/cat.js diff --git a/src/core/components/dag.js b/src/core/components/dag/put.js similarity index 100% rename from src/core/components/dag.js rename to src/core/components/dag/put.js diff --git a/src/core/components/files-regular/add-from-fs.js b/src/core/components/files-regular/add-from-fs.js deleted file mode 100644 index 409c4e8d7e..0000000000 --- a/src/core/components/files-regular/add-from-fs.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict' - -module.exports = (self) => require('../../runtime/add-from-fs-nodejs')(self) diff --git a/src/core/components/files-regular/add-from-stream.js b/src/core/components/files-regular/add-from-stream.js deleted file mode 100644 index 6925dce2ba..0000000000 --- a/src/core/components/files-regular/add-from-stream.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict' - -module.exports = self => require('./add')(self) diff --git a/src/core/components/files-regular/add-from-url.js b/src/core/components/files-regular/add-from-url.js deleted file mode 100644 index bc12850cbe..0000000000 --- a/src/core/components/files-regular/add-from-url.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict' - -const { URL } = require('iso-url') -const nodeify = require('promise-nodeify') -const { default: ky } = require('ky-universal') - -module.exports = (ipfs) => { - const addFromURL = async (url, opts) => { - opts = opts || {} - const res = await ky.get(url) - const path = decodeURIComponent(new URL(res.url).pathname.split('/').pop()) - const content = Buffer.from(await res.arrayBuffer()) - return ipfs.add({ content, path }, opts) - } - - return (name, opts, cb) => { - if (typeof opts === 'function') { - cb = opts - opts = {} - } - return nodeify(addFromURL(name, opts), cb) - } -} diff --git a/src/core/components/files-regular/add-pull-stream.js b/src/core/components/files-regular/add-pull-stream.js deleted file mode 100644 index e3c1519531..0000000000 --- a/src/core/components/files-regular/add-pull-stream.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict' - -const toPullStream = require('async-iterator-to-pull-stream') - -module.exports = function (self) { - return function addPullStream (options) { - return toPullStream.transform((source) => { - return self._addAsyncIterator(source, options) - }) - } -} diff --git a/src/core/components/files-regular/add-readable-stream.js b/src/core/components/files-regular/add-readable-stream.js deleted file mode 100644 index 914a1fd9fb..0000000000 --- a/src/core/components/files-regular/add-readable-stream.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict' - -const toStream = require('it-to-stream') - -module.exports = function (self) { - return function addReadableStream (options) { - return toStream.transform(source => { - return self._addAsyncIterator(source, options) - }, { - objectMode: true - }) - } -} diff --git a/src/core/components/files-regular/add.js b/src/core/components/files-regular/add.js deleted file mode 100644 index d79bcfc39f..0000000000 --- a/src/core/components/files-regular/add.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict' - -const all = require('it-all') - -module.exports = function (self) { - // can't use callbackify because if `data` is a pull stream - // it thinks we are passing a callback. This is why we can't have nice things. - return function add (data, options, callback) { - if (!callback && typeof options === 'function') { - callback = options - options = {} - } - - const result = all(self._addAsyncIterator(data, options)) - - if (!callback) { - return result - } - - result.then((result) => callback(null, result), callback) - } -} diff --git a/src/core/components/files-regular/cat-pull-stream.js b/src/core/components/files-regular/cat-pull-stream.js deleted file mode 100644 index 5ad3f79411..0000000000 --- a/src/core/components/files-regular/cat-pull-stream.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const toPullStream = require('async-iterator-to-pull-stream') - -module.exports = function (self) { - return function catPullStream (ipfsPath, options) { - return toPullStream.source(self._catAsyncIterator(ipfsPath, options)) - } -} diff --git a/src/core/components/files-regular/cat-readable-stream.js b/src/core/components/files-regular/cat-readable-stream.js deleted file mode 100644 index 70df087f16..0000000000 --- a/src/core/components/files-regular/cat-readable-stream.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict' - -const toStream = require('it-to-stream') - -module.exports = function (self) { - return function catReadableStream (ipfsPath, options) { - return toStream.readable(self._catAsyncIterator(ipfsPath, options), { - objectMode: true - }) - } -} diff --git a/src/core/components/files-regular/cat.js b/src/core/components/files-regular/cat.js deleted file mode 100644 index 020e2848c3..0000000000 --- a/src/core/components/files-regular/cat.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' - -const callbackify = require('callbackify') -const all = require('it-all') - -module.exports = function (self) { - return callbackify.variadic(async function cat (ipfsPath, options) { - return Buffer.concat(await all(self._catAsyncIterator(ipfsPath, options))) - }) -} diff --git a/src/core/components/files-regular/get-pull-stream.js b/src/core/components/files-regular/get-pull-stream.js deleted file mode 100644 index fa769aa89c..0000000000 --- a/src/core/components/files-regular/get-pull-stream.js +++ /dev/null @@ -1,20 +0,0 @@ -'use strict' - -const toPullStream = require('async-iterator-to-pull-stream') -const pull = require('pull-stream/pull') -const map = require('pull-stream/throughs/map') - -module.exports = function (self) { - return function getPullStream (ipfsPath, options) { - return pull( - toPullStream.source(self._getAsyncIterator(ipfsPath, options)), - map(file => { - if (file.content) { - file.content = toPullStream.source(file.content()) - } - - return file - }) - ) - } -} diff --git a/src/core/components/files-regular/get-readable-stream.js b/src/core/components/files-regular/get-readable-stream.js deleted file mode 100644 index 3b6b78b3dd..0000000000 --- a/src/core/components/files-regular/get-readable-stream.js +++ /dev/null @@ -1,19 +0,0 @@ -'use strict' - -const toStream = require('it-to-stream') - -module.exports = function (self) { - return function getReadableStream (ipfsPath, options) { - return toStream.readable((async function * mapStreamFileContents () { - for await (const file of self._getAsyncIterator(ipfsPath, options)) { - if (file.content) { - file.content = toStream.readable(file.content()) - } - - yield file - } - })(), { - objectMode: true - }) - } -} diff --git a/src/core/components/files-regular/get.js b/src/core/components/files-regular/get.js deleted file mode 100644 index dd0d1243c1..0000000000 --- a/src/core/components/files-regular/get.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict' - -const callbackify = require('callbackify') -const all = require('it-all') - -module.exports = function (self) { - return callbackify.variadic(async function get (ipfsPath, options) { // eslint-disable-line require-await - return all(async function * () { - for await (const file of self._getAsyncIterator(ipfsPath, options)) { - if (file.content) { - file.content = Buffer.concat(await all(file.content())) - } - - yield file - } - }()) - }) -} diff --git a/src/core/components/files-regular/index.js b/src/core/components/files-regular/index.js deleted file mode 100644 index 8bff57d331..0000000000 --- a/src/core/components/files-regular/index.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict' - -module.exports = (self) => { - const filesRegular = { - add: require('./add')(self), - addFromFs: require('./add-from-fs')(self), - addFromStream: require('./add-from-stream')(self), - addFromURL: require('./add-from-url')(self), - addPullStream: require('./add-pull-stream')(self), - addReadableStream: require('./add-readable-stream')(self), - _addAsyncIterator: require('./add-async-iterator')(self), - cat: require('./cat')(self), - catPullStream: require('./cat-pull-stream')(self), - catReadableStream: require('./cat-readable-stream')(self), - _catAsyncIterator: require('./cat-async-iterator')(self), - get: require('./get')(self), - getPullStream: require('./get-pull-stream')(self), - getReadableStream: require('./get-readable-stream')(self), - _getAsyncIterator: require('./get-async-iterator')(self), - ls: require('./ls')(self), - lsPullStream: require('./ls-pull-stream')(self), - lsReadableStream: require('./ls-readable-stream')(self), - _lsAsyncIterator: require('./ls-async-iterator')(self), - refs: require('./refs')(self), - refsReadableStream: require('./refs-readable-stream')(self), - refsPullStream: require('./refs-pull-stream')(self), - _refsAsyncIterator: require('./refs-async-iterator')(self) - } - filesRegular.refs.local = require('./refs-local')(self) - filesRegular.refs.localReadableStream = require('./refs-local-readable-stream')(self) - filesRegular.refs.localPullStream = require('./refs-local-pull-stream')(self) - filesRegular.refs._localAsyncIterator = require('./refs-local-async-iterator')(self) - return filesRegular -} diff --git a/src/core/components/files-regular/ls-pull-stream.js b/src/core/components/files-regular/ls-pull-stream.js deleted file mode 100644 index 3cf5a6cd74..0000000000 --- a/src/core/components/files-regular/ls-pull-stream.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const toPullStream = require('async-iterator-to-pull-stream') - -module.exports = function (self) { - return function lsPullStream (ipfsPath, options) { - return toPullStream.source(self._lsAsyncIterator(ipfsPath, options)) - } -} diff --git a/src/core/components/files-regular/ls-readable-stream.js b/src/core/components/files-regular/ls-readable-stream.js deleted file mode 100644 index 794095f752..0000000000 --- a/src/core/components/files-regular/ls-readable-stream.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict' - -const toStream = require('it-to-stream') - -module.exports = function (self) { - return function lsReadableStream (ipfsPath, options) { - return toStream.readable(self._lsAsyncIterator(ipfsPath, options), { - objectMode: true - }) - } -} diff --git a/src/core/components/files-regular/ls.js b/src/core/components/files-regular/ls.js deleted file mode 100644 index db35300791..0000000000 --- a/src/core/components/files-regular/ls.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' - -const callbackify = require('callbackify') -const all = require('it-all') - -module.exports = function (self) { - return callbackify.variadic(async function ls (ipfsPath, options) { // eslint-disable-line require-await - return all(self._lsAsyncIterator(ipfsPath, options)) - }) -} diff --git a/src/core/components/files-regular/refs-local-pull-stream.js b/src/core/components/files-regular/refs-local-pull-stream.js deleted file mode 100644 index 1bb02cec51..0000000000 --- a/src/core/components/files-regular/refs-local-pull-stream.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const toPullStream = require('async-iterator-to-pull-stream') - -module.exports = function (self) { - return function refsLocalPullStream () { - return toPullStream.source(self.refs._localAsyncIterator()) - } -} diff --git a/src/core/components/files-regular/refs-local-readable-stream.js b/src/core/components/files-regular/refs-local-readable-stream.js deleted file mode 100644 index f66920ef51..0000000000 --- a/src/core/components/files-regular/refs-local-readable-stream.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict' - -const toStream = require('it-to-stream') - -module.exports = function (self) { - return function refsLocalReadableStream () { - return toStream.readable(self.refs._localAsyncIterator(), { - objectMode: true - }) - } -} diff --git a/src/core/components/files-regular/refs-local.js b/src/core/components/files-regular/refs-local.js deleted file mode 100644 index 8812d97c8e..0000000000 --- a/src/core/components/files-regular/refs-local.js +++ /dev/null @@ -1,10 +0,0 @@ -'use strict' - -const callbackify = require('callbackify') -const all = require('it-all') - -module.exports = function (self) { - return callbackify.variadic(async function refsLocal (ipfsPath, options) { // eslint-disable-line require-await - return all(self.refs._localAsyncIterator(ipfsPath, options)) - }) -} diff --git a/src/core/components/files-regular/refs-pull-stream.js b/src/core/components/files-regular/refs-pull-stream.js deleted file mode 100644 index 4d10812c37..0000000000 --- a/src/core/components/files-regular/refs-pull-stream.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const toPullStream = require('async-iterator-to-pull-stream') - -module.exports = function (self) { - return function refsPullStream (ipfsPath, options) { - return toPullStream.source(self._refsAsyncIterator(ipfsPath, options)) - } -} diff --git a/src/core/components/files-regular/refs-readable-stream.js b/src/core/components/files-regular/refs-readable-stream.js deleted file mode 100644 index 509e65a508..0000000000 --- a/src/core/components/files-regular/refs-readable-stream.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict' - -const toStream = require('it-to-stream') - -module.exports = function (self) { - return function refsReadableStream (ipfsPath, options) { - return toStream.readable(self._refsAsyncIterator(ipfsPath, options), { - objectMode: true - }) - } -} diff --git a/src/core/components/files-regular/refs.js b/src/core/components/files-regular/refs.js deleted file mode 100644 index 3e3f490197..0000000000 --- a/src/core/components/files-regular/refs.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict' - -const callbackify = require('callbackify') -const all = require('it-all') - -module.exports = function (self) { - return callbackify.variadic(async function refs (ipfsPath, options) { // eslint-disable-line require-await - return all(self._refsAsyncIterator(ipfsPath, options)) - }) -} - -// Preset format strings -module.exports.Format = { - default: '', - edges: ' -> ' -} diff --git a/src/core/components/files-regular/get-async-iterator.js b/src/core/components/get.js similarity index 100% rename from src/core/components/files-regular/get-async-iterator.js rename to src/core/components/get.js diff --git a/src/core/components/key.js b/src/core/components/key/rename.js similarity index 100% rename from src/core/components/key.js rename to src/core/components/key/rename.js diff --git a/src/core/components/files-regular/ls-async-iterator.js b/src/core/components/ls.js similarity index 100% rename from src/core/components/files-regular/ls-async-iterator.js rename to src/core/components/ls.js diff --git a/src/core/components/name-pubsub.js b/src/core/components/name/pubsub/utils.js similarity index 100% rename from src/core/components/name-pubsub.js rename to src/core/components/name/pubsub/utils.js diff --git a/src/core/ipns/path.js b/src/core/components/name/utils.js similarity index 100% rename from src/core/ipns/path.js rename to src/core/components/name/utils.js diff --git a/src/core/components/object.js b/src/core/components/object/put.js similarity index 100% rename from src/core/components/object.js rename to src/core/components/object/put.js diff --git a/src/core/components/pin/gc-lock.js b/src/core/components/pin/gc-lock.js deleted file mode 100644 index faceea12cf..0000000000 --- a/src/core/components/pin/gc-lock.js +++ /dev/null @@ -1,83 +0,0 @@ -'use strict' - -const pull = require('pull-stream/pull') -const pullThrough = require('pull-stream/throughs/through') -const pullAsyncMap = require('pull-stream/throughs/async-map') -const Mutex = require('../../../utils/mutex') -const log = require('debug')('ipfs:gc:lock') - -class GCLock { - constructor (repoOwner, options) { - options = options || {} - - this.mutex = new Mutex(repoOwner, { ...options, log }) - } - - readLock () { - return this.mutex.readLock() - } - - writeLock () { - return this.mutex.writeLock() - } - - pullReadLock (lockedPullFn) { - return this.pullLock('readLock', lockedPullFn) - } - - pullWriteLock (lockedPullFn) { - return this.pullLock('writeLock', lockedPullFn) - } - - pullLock (type, lockedPullFn) { - const pullLocker = new PullLocker(this.mutex, type) - - return pull( - pullLocker.take(), - lockedPullFn(), - pullLocker.release() - ) - } -} - -class PullLocker { - constructor (mutex, type) { - this.mutex = mutex - this.type = type - - // The function to call to release the lock. It is set when the lock is taken - this.releaseLock = null - } - - take () { - return pullAsyncMap((i, cb) => { - // Check if the lock has already been acquired. - // Note: new items will only come through the pull stream once the first - // item has acquired a lock. - if (this.releaseLock) { - // The lock has been acquired so return immediately - return cb(null, i) - } - - // Request the lock - this.mutex[this.type]() - .then(release => { - // Save the release function to be called when the stream completes - this.releaseLock = release - - // The lock has been granted, so run the locked piece of code - cb(null, i) - }, cb) - }) - } - - // Releases the lock - release () { - return pullThrough(null, (err) => { - // When the stream completes, release the lock - this.releaseLock(err) - }) - } -} - -module.exports = GCLock diff --git a/src/core/components/pin.js b/src/core/components/pin/ls.js similarity index 100% rename from src/core/components/pin.js rename to src/core/components/pin/ls.js diff --git a/src/core/components/ping-pull-stream.js b/src/core/components/ping-pull-stream.js deleted file mode 100644 index 838378bace..0000000000 --- a/src/core/components/ping-pull-stream.js +++ /dev/null @@ -1,100 +0,0 @@ -'use strict' - -const debug = require('debug') -const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR -const PeerId = require('peer-id') -const pull = require('pull-stream') -const Pushable = require('pull-pushable') - -const log = debug('ipfs:pingPullStream') -log.error = debug('ipfs:pingPullStream:error') - -module.exports = function pingPullStream (self) { - return (peerId, opts) => { - if (!self.isOnline()) { - return pull.error(new Error(OFFLINE_ERROR)) - } - - opts = Object.assign({ count: 10 }, opts) - - const source = Pushable() - - getPeer(self.libp2p, source, peerId, (err, peer) => { - if (err) { - log.error(err) - source.end(err) - return - } - - runPing(self.libp2p, source, opts.count, peer, (err) => { - if (err) { - log.error(err) - source.push(getPacket({ success: false, text: err.toString() })) - source.end() - } - }) - }) - - return source - } -} - -function getPacket (msg) { - // Default msg - const basePacket = { success: true, time: 0, text: '' } - return Object.assign(basePacket, msg) -} - -function getPeer (libp2pNode, statusStream, peerIdStr, cb) { - let peerId - - try { - peerId = PeerId.createFromB58String(peerIdStr) - } catch (err) { - return cb(err) - } - - let peerInfo - - try { - peerInfo = libp2pNode.peerBook.get(peerId) - } catch (err) { - log('Peer not found in peer book, trying peer routing') - - // Share lookup status just as in the go implemmentation - statusStream.push(getPacket({ text: `Looking up peer ${peerIdStr}` })) - return libp2pNode.peerRouting.findPeer(peerId, cb) - } - - cb(null, peerInfo) -} - -function runPing (libp2pNode, statusStream, count, peer, cb) { - libp2pNode.ping(peer, (err, p) => { - if (err) { return cb(err) } - - let packetCount = 0 - let totalTime = 0 - statusStream.push(getPacket({ text: `PING ${peer.id.toB58String()}` })) - - p.on('ping', (time) => { - statusStream.push(getPacket({ time })) - totalTime += time - packetCount++ - if (packetCount >= count) { - const average = totalTime / count - p.stop() - statusStream.push(getPacket({ text: `Average latency: ${average}ms` })) - statusStream.end() - } - }) - - p.on('error', (err) => { - log.error(err) - p.stop() - cb(err) - }) - - p.start() - }) -} diff --git a/src/core/components/ping-readable-stream.js b/src/core/components/ping-readable-stream.js deleted file mode 100644 index b6809ffb48..0000000000 --- a/src/core/components/ping-readable-stream.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict' - -const toStream = require('pull-stream-to-stream') - -module.exports = function pingReadableStream (self) { - return (peerId, opts) => toStream.source(self.pingPullStream(peerId, opts)) -} diff --git a/src/core/components/pre-start.js b/src/core/components/pre-start.js deleted file mode 100644 index 639b94a61f..0000000000 --- a/src/core/components/pre-start.js +++ /dev/null @@ -1,75 +0,0 @@ -'use strict' - -const peerId = require('peer-id') -const PeerInfo = require('peer-info') -const multiaddr = require('multiaddr') -const Keychain = require('libp2p-keychain') -const mergeOptions = require('merge-options') -const NoKeychain = require('./no-keychain') -const callbackify = require('callbackify') -const promisify = require('promisify-es6') - -/* - * Load stuff from Repo into memory - */ -module.exports = function preStart (self) { - return callbackify(async () => { - self.log('pre-start') - - const pass = self._options.pass - let config = await self._repo.config.get() - - if (self._options.config) { - config = mergeOptions(config, self._options.config) - await self.config.replace(config) - } - - // Create keychain configuration, if needed. - if (!config.Keychain) { - config.Keychain = Keychain.generateOptions() - await self.config.set('Keychain', config.Keychain) - self.log('using default keychain options') - } - - // Construct the keychain - if (self._keychain) { - // most likely an init or upgrade has happened - } else if (pass) { - const keychainOptions = Object.assign({ passPhrase: pass }, config.Keychain) - self._keychain = new Keychain(self._repo.keys, keychainOptions) - self.log('keychain constructed') - } else { - self._keychain = new NoKeychain() - self.log('no keychain, use --pass') - } - - const privKey = config.Identity.PrivKey - const id = await promisify(peerId.createFromPrivKey)(privKey) - - // Import the private key as 'self', if needed. - if (pass) { - try { - await self._keychain.findKeyByName('self') - } catch (err) { - self.log('Creating "self" key') - await self._keychain.importPeer('self', id) - } - } - - self.log('peer created') - self._peerInfo = new PeerInfo(id) - if (config.Addresses && config.Addresses.Swarm) { - config.Addresses.Swarm.forEach((addr) => { - let ma = multiaddr(addr) - - if (ma.getPeerId()) { - ma = ma.encapsulate('/ipfs/' + self._peerInfo.id.toB58String()) - } - - self._peerInfo.multiaddrs.add(ma) - }) - } - - await self.pin.pinManager.load() - }) -} diff --git a/src/core/components/files-regular/refs-async-iterator.js b/src/core/components/refs/index.js similarity index 100% rename from src/core/components/files-regular/refs-async-iterator.js rename to src/core/components/refs/index.js diff --git a/src/core/components/files-regular/refs-local-async-iterator.js b/src/core/components/refs/local.js similarity index 100% rename from src/core/components/files-regular/refs-local-async-iterator.js rename to src/core/components/refs/local.js diff --git a/src/core/components/pin/gc.js b/src/core/components/repo/gc.js similarity index 100% rename from src/core/components/pin/gc.js rename to src/core/components/repo/gc.js diff --git a/src/core/components/repo.js b/src/core/components/repo/version.js similarity index 100% rename from src/core/components/repo.js rename to src/core/components/repo/version.js diff --git a/src/core/components/stats.js b/src/core/components/stats/bw.js similarity index 100% rename from src/core/components/stats.js rename to src/core/components/stats/bw.js diff --git a/src/core/components/swarm.js b/src/core/components/swarm/peers.js similarity index 100% rename from src/core/components/swarm.js rename to src/core/components/swarm/peers.js diff --git a/src/core/runtime/add-from-fs-browser.js b/src/core/runtime/add-from-fs-browser.js deleted file mode 100644 index aaf9691c7c..0000000000 --- a/src/core/runtime/add-from-fs-browser.js +++ /dev/null @@ -1,9 +0,0 @@ -'use strict' - -const callbackify = require('callbackify') - -module.exports = () => { - return callbackify(async () => { // eslint-disable-line require-await - throw new Error('not available in the browser') - }) -} diff --git a/src/core/runtime/add-from-fs-nodejs.js b/src/core/runtime/add-from-fs-nodejs.js deleted file mode 100644 index 8a30815135..0000000000 --- a/src/core/runtime/add-from-fs-nodejs.js +++ /dev/null @@ -1,13 +0,0 @@ -'use strict' - -const callbackify = require('callbackify') -const globSource = require('ipfs-utils/src/files/glob-source') -const all = require('it-all') - -module.exports = self => { - return callbackify.variadic(async (...args) => { // eslint-disable-line require-await - const options = typeof args[args.length - 1] === 'string' ? {} : args.pop() - - return all(self._addAsyncIterator(globSource(...args, options), options)) - }) -} diff --git a/src/core/components/init-assets.js b/src/core/runtime/init-assets-nodejs.js similarity index 100% rename from src/core/components/init-assets.js rename to src/core/runtime/init-assets-nodejs.js diff --git a/src/core/state.js b/src/core/state.js deleted file mode 100644 index 802e2fd334..0000000000 --- a/src/core/state.js +++ /dev/null @@ -1,65 +0,0 @@ -'use strict' - -const debug = require('debug') -const log = debug('ipfs:state') -log.error = debug('ipfs:state:error') - -const fsm = require('fsm-event') - -module.exports = (self) => { - const s = fsm('uninitialized', { - uninitialized: { - init: 'initializing', - initialized: 'stopped' - }, - initializing: { - initialized: 'stopped' - }, - stopped: { - start: 'starting' - }, - starting: { - started: 'running' - }, - running: { - stop: 'stopping' - }, - stopping: { - stopped: 'stopped' - } - }) - - // log events - s.on('error', (err) => log.error(err)) - s.on('done', () => log('-> ' + s._state)) - - // -- Actions - - s.init = () => { - s('init') - } - - s.initialized = () => { - s('initialized') - } - - s.stop = () => { - s('stop') - } - - s.stopped = () => { - s('stopped') - } - - s.start = () => { - s('start') - } - - s.started = () => { - s('started') - } - - s.state = () => s._state - - return s -} diff --git a/src/http/api/resources/file.js b/src/http/api/resources/file.js deleted file mode 100644 index e28fb36cc7..0000000000 --- a/src/http/api/resources/file.js +++ /dev/null @@ -1,103 +0,0 @@ -'use strict' - -const isIpfs = require('is-ipfs') -const exporter = require('ipfs-unixfs-exporter') -const pull = require('pull-stream') -const toB58String = require('multihashes').toB58String -const Boom = require('@hapi/boom') - -const fileTypeMap = { - file: 'File', - dir: 'Directory' -} - -function toFileObject (file) { - const fo = { - Hash: toB58String(file.hash), - Size: file.size, - Type: fileTypeMap[file.type] || file.type, - Mode: file.mode, - Mtime: file.mtime - } - if (fo.Hash !== file.name) { - fo.Name = file.name - } - if (file.mtime) { - fo.Mtime = Math.round(file.mtime.getTime() / 1000) - } - if (file.mode) { - fo.Mode = file.mode - } - return fo -} - -// common pre request handler that parses the args and returns `key` which is assigned to `request.pre.args` -exports.parseKey = (request, h) => { - if (!request.query.arg) { - throw Boom.badRequest("Argument 'key' is required") - } - - let key = request.query.arg - if (key.indexOf('/ipfs/') === 0) { - key = key.substring(6) - } - - let hash = key - const slashIndex = hash.indexOf('/') - if (slashIndex > 0) { - hash = hash.substring(0, slashIndex) - } - - if (!isIpfs.ipfsPath(request.query.arg) && !isIpfs.cid(request.query.arg)) { - throw Boom.badRequest('invalid ipfs ref path') - } - - const subpaths = key.split('/') - subpaths.shift() - return { - path: request.query.arg, - subpaths: subpaths, - key: key, - hash: hash - } -} - -exports.ls = { - // uses common parseKey method that returns a `key` - parseArgs: exports.parseKey, - - // main route handler which is called after the above `parseArgs`, but only if the args were valid - async handler (request, h) { - const { ipfs } = request.server.app - const { path, subpaths } = request.pre.args.path - const rootDepth = subpaths.length - - const files = await new Promise((resolve, reject) => { - pull( - exporter(path, ipfs._ipld, { maxDepth: rootDepth + 1 }), - pull.collect((err, files) => { - if (err) return reject(err) - resolve(files) - }) - ) - }) - - const res = { - Arguments: {}, - Objects: {} - } - const links = [] - files.forEach((file) => { - if (file.depth === rootDepth) { - const id = file.cid.toString() - res.Arguments[path] = id - res.Objects[id] = toFileObject(file) - res.Objects[id].Links = file.type === 'file' ? null : links - } else { - links.push(toFileObject(file)) - } - }) - - return h.response(res) - } -} diff --git a/src/http/api/routes/file.js b/src/http/api/routes/file.js deleted file mode 100644 index 5fd57355bd..0000000000 --- a/src/http/api/routes/file.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict' - -const resources = require('../resources') - -module.exports = { - // TODO fix method - method: '*', - path: '/api/v0/file/ls', - config: { - pre: [ - { method: resources.file.ls.parseArgs, assign: 'args' } - ] - }, - handler: resources.file.ls.handler -} diff --git a/src/utils/mutex.js b/src/utils/mutex.js deleted file mode 100644 index 8cb3df36cc..0000000000 --- a/src/utils/mutex.js +++ /dev/null @@ -1,52 +0,0 @@ -'use strict' - -const assert = require('assert') -const mortice = require('mortice') -const noop = () => {} - -// Wrap mortice to present a callback interface -class Mutex { - constructor (repoOwner, options) { - options = options || {} - - this.mutex = mortice(options.morticeId, { - singleProcess: repoOwner - }) - - this.log = options.log || noop - this.lockId = 0 - } - - readLock () { - return this._lock('readLock') - } - - writeLock () { - return this._lock('writeLock') - } - - /** - * Request a read or write lock - * - * @param {String} type The type of lock: readLock / writeLock - * @returns {Promise} - */ - async _lock (type) { - assert(typeof type === 'string', `first argument to Mutex.${type}() must be a string, got ${typeof type}`) - - const lockId = this.lockId++ - this.log(`[${lockId}] ${type} requested`) - - // Get a Promise for the lock, wrap it for logging - const release = await this.mutex[type]() - - this.log(`[${lockId}] ${type} started`) - - return () => { - this.log(`[${lockId}] ${type} released`) - release() - } - } -} - -module.exports = Mutex diff --git a/test/cli/file.js b/test/cli/file.js deleted file mode 100644 index 86a15d499d..0000000000 --- a/test/cli/file.js +++ /dev/null @@ -1,38 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const { expect } = require('interface-ipfs-core/src/utils/mocha') -const runOnAndOff = require('../utils/on-and-off') -const file = 'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV' -const dir = 'Qmaj2NmcyAXT8dFmZRRytE12wpcaHADzbChKToMEjBsj5Z' - -describe('file ls', () => runOnAndOff((thing) => { - let ipfs - - before(async function () { - this.timeout(50 * 1000) - ipfs = thing.ipfs - await ipfs('add -r test/fixtures/test-data/recursive-get-dir') - }) - - it('prints a filename', async () => { - const out = await ipfs(`file ls ${file}`) - - expect(out).to.eql( - 'This functionality is deprecated, and will be removed in future versions. If possible, please use \'ipfs ls\' instead.\n' + - `${file}\n` - ) - }) - - it('prints the filenames in a directory', async () => { - const out = await ipfs(`file ls ${dir}`) - expect(out).to.eql( - 'This functionality is deprecated, and will be removed in future versions. If possible, please use \'ipfs ls\' instead.\n' + - 'QmamKEPmEH9RUsqRQsfNf5evZQDQPYL9KXg1ADeT7mkHkT\n' + - 'QmPkWYfSLCEBLZu7BZt4kigGDMe3cpogMbeVf97gN2xJDN\n' + - 'QmUqyZtPmsRy1U5Mo8kz2BAMmk1hfJ7yW1KAFTMB2odsFv\n' + - 'QmUhUuiTKkkK8J6JZ9zmj8iNHPuNfGYcszgRumzhHBxEEU\n' + - 'QmR56UJmAaZLXLdTT1ALrE9vVqV8soUEekm9BMd4FnuYqV\n' - ) - }) -})) 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/gc-lock.spec.js b/test/core/gc-lock.spec.js deleted file mode 100644 index 30410204fb..0000000000 --- a/test/core/gc-lock.spec.js +++ /dev/null @@ -1,316 +0,0 @@ -/* eslint-env mocha */ -'use strict' - -const { expect } = require('interface-ipfs-core/src/utils/mocha') -const delay = require('delay') -const parallel = require('async/parallel') -const pull = require('pull-stream') -const pullThrough = require('pull-stream/throughs/through') -const pullAsyncMap = require('pull-stream/throughs/async-map') -const pullCollect = require('pull-stream/sinks/collect') -const pullValues = require('pull-stream/sources/values') -const GCLock = require('../../src/core/components/pin/gc-lock') - -const promiseTakeLock = (type, lock, out, id, duration) => { - return (cb) => { - lock[type + 'Lock']() - .then(async (release) => { - try { - out.push(`${type} ${id} start`) - - await delay(duration) - - out.push(`${type} ${id} end`) - } finally { - release() - } - }) - .then(() => cb()) - } -} -const promiseReadLock = (lock, out, id, duration) => { - return promiseTakeLock('read', lock, out, id, duration) -} -const promiseWriteLock = (lock, out, id, duration) => { - return promiseTakeLock('write', lock, out, id, duration) -} -const promiseTakeLockError = (type, lock, out, errs, id, duration) => { - return (cb) => { - lock[type + 'Lock']() - .then(async (release) => { - try { - out.push(`${type} ${id} start`) - - await delay(duration) - - out.push(`${type} ${id} error`) - - const err = new Error('err') - errs.push(err) - - throw err - } finally { - release() - } - }) - .catch(() => cb()) - } -} -const promiseReadLockError = (lock, out, errs, id, duration) => { - return promiseTakeLockError('read', lock, out, errs, id, duration) -} -const promiseWriteLockError = (lock, out, errs, id, duration) => { - return promiseTakeLockError('write', lock, out, errs, id, duration) -} - -const pullTakeLock = (type, lock, out, id, duration) => { - const lockFn = type === 'read' ? 'pullReadLock' : 'pullWriteLock' - const vals = ['a', 'b', 'c'] - return (cb) => { - pull( - pullValues(vals), - lock[lockFn](() => { - let started = false - return pull( - pullThrough((i) => { - if (!started) { - out.push(`${type} ${id} start`) - started = true - } - }), - pullAsyncMap((i, cb) => { - setTimeout(() => cb(null, i), duration / vals.length) - }) - ) - }), - pullCollect(() => { - out.push(`${type} ${id} end`) - cb() - }) - ) - } -} -const pullReadLock = (lock, out, id, duration) => { - return pullTakeLock('read', lock, out, id, duration) -} -const pullWriteLock = (lock, out, id, duration) => { - return pullTakeLock('write', lock, out, id, duration) -} -const pullTakeLockError = (type, lock, out, errs, id, duration) => { - const lockFn = type === 'read' ? 'pullReadLock' : 'pullWriteLock' - const vals = ['a', 'b', 'c'] - return (cb) => { - pull( - pullValues(vals), - lock[lockFn](() => { - let started = false - return pull( - pullThrough((i) => { - if (!started) { - out.push(`${type} ${id} start`) - started = true - } - }), - pullAsyncMap((i, cb) => { - setTimeout(() => cb(new Error('err')), duration) - }) - ) - }), - pullCollect((err) => { - out.push(`${type} ${id} error`) - errs.push(err) - cb() - }) - ) - } -} -const pullReadLockError = (lock, out, errs, id, duration) => { - return pullTakeLockError('read', lock, out, errs, id, duration) -} -const pullWriteLockError = (lock, out, errs, id, duration) => { - return pullTakeLockError('write', lock, out, errs, id, duration) -} - -const expectResult = (out, exp, errs, expErrCount, done) => { - if (typeof errs === 'function') { - done = errs - } - return () => { - try { - expect(out).to.eql(exp) - if (typeof expErrCount === 'number') { - expect(errs.length).to.eql(expErrCount) - for (const e of errs) { - expect(e.message).to.eql('err') - } - } - } catch (err) { - return done(err) - } - done() - } -} - -const runTests = (suiteName, { readLock, writeLock, readLockError, writeLockError }) => { - describe(suiteName, () => { - it('multiple simultaneous reads', (done) => { - const lock = new GCLock() - const out = [] - parallel([ - readLock(lock, out, 1, 100), - readLock(lock, out, 2, 200), - readLock(lock, out, 3, 300) - ], expectResult(out, [ - 'read 1 start', - 'read 2 start', - 'read 3 start', - 'read 1 end', - 'read 2 end', - 'read 3 end' - ], done)) - }) - - it('multiple simultaneous writes', (done) => { - const lock = new GCLock() - const out = [] - parallel([ - writeLock(lock, out, 1, 100), - writeLock(lock, out, 2, 200), - writeLock(lock, out, 3, 300) - ], expectResult(out, [ - 'write 1 start', - 'write 1 end', - 'write 2 start', - 'write 2 end', - 'write 3 start', - 'write 3 end' - ], done)) - }) - - it('read then write then read', (done) => { - const lock = new GCLock() - const out = [] - parallel([ - readLock(lock, out, 1, 100), - writeLock(lock, out, 1, 100), - readLock(lock, out, 2, 100) - ], expectResult(out, [ - 'read 1 start', - 'read 1 end', - 'write 1 start', - 'write 1 end', - 'read 2 start', - 'read 2 end' - ], done)) - }) - - it('write then read then write', (done) => { - const lock = new GCLock() - const out = [] - parallel([ - writeLock(lock, out, 1, 100), - readLock(lock, out, 1, 100), - writeLock(lock, out, 2, 100) - ], expectResult(out, [ - 'write 1 start', - 'write 1 end', - 'read 1 start', - 'read 1 end', - 'write 2 start', - 'write 2 end' - ], done)) - }) - - it('two simultaneous reads then write then read', (done) => { - const lock = new GCLock() - const out = [] - parallel([ - readLock(lock, out, 1, 100), - readLock(lock, out, 2, 200), - writeLock(lock, out, 1, 100), - readLock(lock, out, 3, 100) - ], expectResult(out, [ - 'read 1 start', - 'read 2 start', - 'read 1 end', - 'read 2 end', - 'write 1 start', - 'write 1 end', - 'read 3 start', - 'read 3 end' - ], done)) - }) - - it('two simultaneous writes then read then write', (done) => { - const lock = new GCLock() - const out = [] - parallel([ - writeLock(lock, out, 1, 100), - writeLock(lock, out, 2, 100), - readLock(lock, out, 1, 100), - writeLock(lock, out, 3, 100) - ], expectResult(out, [ - 'write 1 start', - 'write 1 end', - 'write 2 start', - 'write 2 end', - 'read 1 start', - 'read 1 end', - 'write 3 start', - 'write 3 end' - ], done)) - }) - - it('simultaneous reads with error then write', (done) => { - const lock = new GCLock() - const out = [] - const errs = [] - parallel([ - readLockError(lock, out, errs, 1, 100), - readLock(lock, out, 2, 200), - writeLock(lock, out, 1, 100) - ], expectResult(out, [ - 'read 1 start', - 'read 2 start', - 'read 1 error', - 'read 2 end', - 'write 1 start', - 'write 1 end' - ], errs, 1, done)) - }) - - it('simultaneous writes with error then read', (done) => { - const lock = new GCLock() - const out = [] - const errs = [] - parallel([ - writeLockError(lock, out, errs, 1, 100), - writeLock(lock, out, 2, 100), - readLock(lock, out, 1, 100) - ], expectResult(out, [ - 'write 1 start', - 'write 1 error', - 'write 2 start', - 'write 2 end', - 'read 1 start', - 'read 1 end' - ], errs, 1, done)) - }) - }) -} - -describe('gc-lock', function () { - runTests('promise style lock', { - readLock: promiseReadLock, - writeLock: promiseWriteLock, - readLockError: promiseReadLockError, - writeLockError: promiseWriteLockError - }) - - runTests('pull stream style lock', { - readLock: pullReadLock, - writeLock: pullWriteLock, - readLockError: pullReadLockError, - writeLockError: pullWriteLockError - }) -}) diff --git a/test/utils/expect-timeout.js b/test/utils/expect-timeout.js deleted file mode 100644 index 51c7330755..0000000000 --- a/test/utils/expect-timeout.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict' - -/** - * Resolve if @param promise hangs for at least @param ms, throw otherwise - * @param {Promise} promise promise that you expect to hang - * @param {Number} ms millis to wait - * @return {Promise} - */ -module.exports = (promise, ms) => { - return Promise.race([ - promise.then((out) => { - throw new Error('Expected Promise to timeout but it was successful.') - }), - new Promise((resolve, reject) => setTimeout(resolve, ms)) - ]) -}