From 0567881d7d8d434f177b0f918484835d085d59f9 Mon Sep 17 00:00:00 2001 From: David Dias Date: Sun, 28 Oct 2018 15:11:38 +0100 Subject: [PATCH] feat: wip on migrating core to new API, got caught by circular dep with mfs --- src/core/components/{mfs.js => files-mfs.js} | 0 .../components/{files.js => files-regular.js} | 59 ++++++++++--------- src/core/components/index.js | 4 +- src/core/components/init-assets.js | 2 +- src/core/index.js | 36 ++++++----- test/core/interface.spec.js | 5 +- test/http-api/interface.js | 3 +- 7 files changed, 61 insertions(+), 48 deletions(-) rename src/core/components/{mfs.js => files-mfs.js} (100%) rename src/core/components/{files.js => files-regular.js} (94%) diff --git a/src/core/components/mfs.js b/src/core/components/files-mfs.js similarity index 100% rename from src/core/components/mfs.js rename to src/core/components/files-mfs.js diff --git a/src/core/components/files.js b/src/core/components/files-regular.js similarity index 94% rename from src/core/components/files.js rename to src/core/components/files-regular.js index d6f6d889a2..38f40b0f36 100644 --- a/src/core/components/files.js +++ b/src/core/components/files-regular.js @@ -24,6 +24,23 @@ const WRAPPER = 'wrapper/' function noop () {} +function normalizePath (path) { + if (Buffer.isBuffer(path)) { + path = toB58String(path) + } + if (CID.isCID(path)) { + path = path.toBaseEncodedString() + } + if (path.indexOf('/ipfs/') === 0) { + path = path.substring('/ipfs/'.length) + } + if (path.charAt(path.length - 1) === '/') { + path = path.substring(0, path.length - 1) + } + + return path +} + function prepareFile (self, opts, file, callback) { opts = opts || {} @@ -47,7 +64,9 @@ function prepareFile (self, opts, file, callback) { } cb(null, { - path: opts.wrapWithDirectory ? file.path.substring(WRAPPER.length) : (file.path || b58Hash), + path: opts.wrapWithDirectory + ? file.path.substring(WRAPPER.length) + : (file.path || b58Hash), hash: b58Hash, size }) @@ -154,7 +173,8 @@ class AddHelper extends Duplex { } } -module.exports = function files (self) { +module.exports = function (self) { + // Internal add func that gets used by all add funcs function _addPullStream (options = {}) { let chunkerOptions try { @@ -191,6 +211,7 @@ module.exports = function files (self) { ) } + // Internal cat func that gets used by all cat funcs function _catPullStream (ipfsPath, options) { if (typeof ipfsPath === 'function') { throw new Error('You must supply an ipfsPath') @@ -232,7 +253,8 @@ module.exports = function files (self) { return d } - function _lsPullStreamImmutable (ipfsPath, options) { + // Internal ls func that gets used by all ls funcs + function _lsPullStream (ipfsPath, options) { options = options || {} const path = normalizePath(ipfsPath) @@ -301,7 +323,7 @@ module.exports = function files (self) { return function () { const args = Array.from(arguments) - // If we files.add(), then promisify thinks the pull stream + // If we .add(), then promisify thinks the pull stream // is a callback! Add an empty options object in this case so that a // promise is returned. if (args.length === 1 && isSource(args[0])) { @@ -337,7 +359,7 @@ module.exports = function files (self) { } if (typeof callback !== 'function') { - throw new Error('Please supply a callback to ipfs.files.cat') + throw new Error('Please supply a callback to ipfs.cat') } pull( @@ -441,7 +463,7 @@ module.exports = function files (self) { return exporter(ipfsPath, self._ipld, options) }, - lsImmutable: promisify((ipfsPath, options, callback) => { + ls: promisify((ipfsPath, options, callback) => { if (typeof options === 'function') { callback = options options = {} @@ -450,7 +472,7 @@ module.exports = function files (self) { options = options || {} pull( - _lsPullStreamImmutable(ipfsPath, options), + _lsPullStream(ipfsPath, options), pull.collect((err, values) => { if (err) { callback(err) @@ -461,27 +483,10 @@ module.exports = function files (self) { ) }), - lsReadableStreamImmutable: (ipfsPath, options) => { - return toStream.source(_lsPullStreamImmutable(ipfsPath, options)) + lsReadableStream: (ipfsPath, options) => { + return toStream.source(_lsPullStream(ipfsPath, options)) }, - lsPullStreamImmutable: _lsPullStreamImmutable + lsPullStream: _lsPullStream } } - -function normalizePath (path) { - if (Buffer.isBuffer(path)) { - path = toB58String(path) - } - if (CID.isCID(path)) { - path = path.toBaseEncodedString() - } - if (path.indexOf('/ipfs/') === 0) { - path = path.substring('/ipfs/'.length) - } - if (path.charAt(path.length - 1) === '/') { - path = path.substring(0, path.length - 1) - } - - return path -} diff --git a/src/core/components/index.js b/src/core/components/index.js index cf6506e6dd..ac893efbdd 100644 --- a/src/core/components/index.js +++ b/src/core/components/index.js @@ -19,13 +19,13 @@ exports.ping = require('./ping') exports.pingPullStream = require('./ping-pull-stream') exports.pingReadableStream = require('./ping-readable-stream') exports.pin = require('./pin') -exports.files = require('./files') +exports.filesRegular = require('./files-regular') +exports.filesMFS = require('./files-mfs') exports.bitswap = require('./bitswap') exports.pubsub = require('./pubsub') exports.dht = require('./dht') exports.dns = require('./dns') exports.key = require('./key') exports.stats = require('./stats') -exports.mfs = require('./mfs') exports.resolve = require('./resolve') exports.name = require('./name') diff --git a/src/core/components/init-assets.js b/src/core/components/init-assets.js index 9756a83d56..362bad7345 100644 --- a/src/core/components/init-assets.js +++ b/src/core/components/init-assets.js @@ -21,7 +21,7 @@ module.exports = function addDefaultAssets (self, log, callback) { const addPath = element.substring(index + 1) return { path: addPath, content: file(element) } }), - self.files.addPullStream(), + self.addPullStream(), pull.through(file => { if (file.path === 'init-docs') { const cid = new CID(file.hash) diff --git a/src/core/index.js b/src/core/index.js index 0f49423f78..09680a0a02 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -110,7 +110,6 @@ class IPFS extends EventEmitter { this.dag = components.dag(this) this.libp2p = components.libp2p(this) this.swarm = components.swarm(this) - this.files = components.files(this) this.name = components.name(this) this.bitswap = components.bitswap(this) this.pin = components.pin(this) @@ -136,24 +135,33 @@ class IPFS extends EventEmitter { this.state = require('./state')(this) - // ipfs.ls - this.ls = this.files.lsImmutable - this.lsReadableStream = this.files.lsReadableStreamImmutable - this.lsPullStream = this.files.lsPullStreamImmutable + // ipfs regular Files APIs + const filesRegular = components.filesRegular(this) + this.add = filesRegular.add + this.addReadableStream = filesRegular.addReadableStream + this.addPullStream = filesRegular.addPullStream + // TODO create this.addFromFs + // TODO create this.addFromStream + // TODO create this.addFromUrl + this.cat = filesRegular.catImmutable + this.catReadableStream = filesRegular.catReadableStream + this.catPullStream = filesRegular.catPullStream + this.get = filesRegular.getImmutable + this.getReadableStream = filesRegular.getReadableStream + this.getPullStream = filesRegular.getPullStream + this.ls = filesRegular.lsImmutable + this.lsReadableStream = filesRegular.lsReadableStream + this.lsPullStream = filesRegular.lsPullStream + + // ipfs.files API (aka MFS) + this.files = components.filesMFS(this) // ipfs.util this.util = { - crypto: crypto, - isIPFS: isIPFS + crypto, + isIPFS } - // ipfs.files - const mfs = components.mfs(this) - - Object.keys(mfs).forEach(key => { - this.files[key] = mfs[key] - }) - boot(this) } } diff --git a/test/core/interface.spec.js b/test/core/interface.spec.js index 72a2fd6620..c8bba948df 100644 --- a/test/core/interface.spec.js +++ b/test/core/interface.spec.js @@ -73,7 +73,8 @@ describe('interface-ipfs-core tests', () => { ] }) - tests.filesMFS(defaultCommonFactory) + // TODO needs MFS module to be updated + // tests.filesMFS(defaultCommonFactory) tests.key(CommonFactory.create({ spawnOptions: { @@ -82,8 +83,6 @@ describe('interface-ipfs-core tests', () => { } })) - tests.ls(defaultCommonFactory) - tests.miscellaneous(CommonFactory.create({ // No need to stop, because the test suite does a 'stop' test. createTeardown: () => cb => cb() diff --git a/test/http-api/interface.js b/test/http-api/interface.js index ac33dfb370..45d11db419 100644 --- a/test/http-api/interface.js +++ b/test/http-api/interface.js @@ -25,7 +25,8 @@ describe('interface-ipfs-core over ipfs-api tests', () => { skip: { reason: 'TODO: DHT is not implemented in js-ipfs yet!' } }) - tests.files(defaultCommonFactory) + tests.filesRegular(defaultCommonFactory) + tests.filesMFS(defaultCommonFactory) tests.key(CommonFactory.create({ spawnOptions: {