Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: wip on migrating core to new API, got caught by circular dep wi…
Browse files Browse the repository at this point in the history
…th mfs
  • Loading branch information
daviddias committed Nov 3, 2018
1 parent 823f165 commit 525b5f1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 48 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}

Expand All @@ -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
})
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -301,7 +323,7 @@ module.exports = function files (self) {
return function () {
const args = Array.from(arguments)

// If we files.add(<pull stream>), then promisify thinks the pull stream
// If we .add(<pull stream>), 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])) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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)
Expand All @@ -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
}
4 changes: 2 additions & 2 deletions src/core/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
2 changes: 1 addition & 1 deletion src/core/components/init-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 22 additions & 14 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,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)
Expand All @@ -154,24 +153,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)
}
}
Expand Down
5 changes: 2 additions & 3 deletions test/core/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ describe('interface-ipfs-core tests', () => {
]
})

tests.filesMFS(defaultCommonFactory)
// TODO needs MFS module to be updated
// tests.filesMFS(defaultCommonFactory)

tests.key(CommonFactory.create({
spawnOptions: {
Expand All @@ -119,8 +120,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()
Expand Down
3 changes: 2 additions & 1 deletion test/http-api/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 525b5f1

Please sign in to comment.