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

Commit 89b097b

Browse files
daviddiasAlan Shaw
authored and
Alan Shaw
committed
feat: wip on migrating core to new API, got caught by circular dep with mfs
1 parent f3af847 commit 89b097b

File tree

7 files changed

+61
-52
lines changed

7 files changed

+61
-52
lines changed
File renamed without changes.

src/core/components/files.js renamed to src/core/components/files-regular.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ const WRAPPER = 'wrapper/'
2424

2525
function noop () {}
2626

27+
function normalizePath (path) {
28+
if (Buffer.isBuffer(path)) {
29+
path = toB58String(path)
30+
}
31+
if (CID.isCID(path)) {
32+
path = path.toBaseEncodedString()
33+
}
34+
if (path.indexOf('/ipfs/') === 0) {
35+
path = path.substring('/ipfs/'.length)
36+
}
37+
if (path.charAt(path.length - 1) === '/') {
38+
path = path.substring(0, path.length - 1)
39+
}
40+
41+
return path
42+
}
43+
2744
function prepareFile (self, opts, file, callback) {
2845
opts = opts || {}
2946

@@ -47,7 +64,9 @@ function prepareFile (self, opts, file, callback) {
4764
}
4865

4966
cb(null, {
50-
path: opts.wrapWithDirectory ? file.path.substring(WRAPPER.length) : (file.path || b58Hash),
67+
path: opts.wrapWithDirectory
68+
? file.path.substring(WRAPPER.length)
69+
: (file.path || b58Hash),
5170
hash: b58Hash,
5271
size
5372
})
@@ -154,7 +173,8 @@ class AddHelper extends Duplex {
154173
}
155174
}
156175

157-
module.exports = function files (self) {
176+
module.exports = function (self) {
177+
// Internal add func that gets used by all add funcs
158178
function _addPullStream (options = {}) {
159179
let chunkerOptions
160180
try {
@@ -191,6 +211,7 @@ module.exports = function files (self) {
191211
)
192212
}
193213

214+
// Internal cat func that gets used by all cat funcs
194215
function _catPullStream (ipfsPath, options) {
195216
if (typeof ipfsPath === 'function') {
196217
throw new Error('You must supply an ipfsPath')
@@ -232,7 +253,8 @@ module.exports = function files (self) {
232253
return d
233254
}
234255

235-
function _lsPullStreamImmutable (ipfsPath, options) {
256+
// Internal ls func that gets used by all ls funcs
257+
function _lsPullStream (ipfsPath, options) {
236258
options = options || {}
237259

238260
const path = normalizePath(ipfsPath)
@@ -301,7 +323,7 @@ module.exports = function files (self) {
301323
return function () {
302324
const args = Array.from(arguments)
303325

304-
// If we files.add(<pull stream>), then promisify thinks the pull stream
326+
// If we .add(<pull stream>), then promisify thinks the pull stream
305327
// is a callback! Add an empty options object in this case so that a
306328
// promise is returned.
307329
if (args.length === 1 && isSource(args[0])) {
@@ -337,7 +359,7 @@ module.exports = function files (self) {
337359
}
338360

339361
if (typeof callback !== 'function') {
340-
throw new Error('Please supply a callback to ipfs.files.cat')
362+
throw new Error('Please supply a callback to ipfs.cat')
341363
}
342364

343365
pull(
@@ -441,7 +463,7 @@ module.exports = function files (self) {
441463
return exporter(ipfsPath, self._ipld, options)
442464
},
443465

444-
lsImmutable: promisify((ipfsPath, options, callback) => {
466+
ls: promisify((ipfsPath, options, callback) => {
445467
if (typeof options === 'function') {
446468
callback = options
447469
options = {}
@@ -450,7 +472,7 @@ module.exports = function files (self) {
450472
options = options || {}
451473

452474
pull(
453-
_lsPullStreamImmutable(ipfsPath, options),
475+
_lsPullStream(ipfsPath, options),
454476
pull.collect((err, values) => {
455477
if (err) {
456478
callback(err)
@@ -461,27 +483,10 @@ module.exports = function files (self) {
461483
)
462484
}),
463485

464-
lsReadableStreamImmutable: (ipfsPath, options) => {
465-
return toStream.source(_lsPullStreamImmutable(ipfsPath, options))
486+
lsReadableStream: (ipfsPath, options) => {
487+
return toStream.source(_lsPullStream(ipfsPath, options))
466488
},
467489

468-
lsPullStreamImmutable: _lsPullStreamImmutable
490+
lsPullStream: _lsPullStream
469491
}
470492
}
471-
472-
function normalizePath (path) {
473-
if (Buffer.isBuffer(path)) {
474-
path = toB58String(path)
475-
}
476-
if (CID.isCID(path)) {
477-
path = path.toBaseEncodedString()
478-
}
479-
if (path.indexOf('/ipfs/') === 0) {
480-
path = path.substring('/ipfs/'.length)
481-
}
482-
if (path.charAt(path.length - 1) === '/') {
483-
path = path.substring(0, path.length - 1)
484-
}
485-
486-
return path
487-
}

src/core/components/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ exports.ping = require('./ping')
1919
exports.pingPullStream = require('./ping-pull-stream')
2020
exports.pingReadableStream = require('./ping-readable-stream')
2121
exports.pin = require('./pin')
22-
exports.files = require('./files')
22+
exports.filesRegular = require('./files-regular')
23+
exports.filesMFS = require('./files-mfs')
2324
exports.bitswap = require('./bitswap')
2425
exports.pubsub = require('./pubsub')
2526
exports.dht = require('./dht')
2627
exports.dns = require('./dns')
2728
exports.key = require('./key')
2829
exports.stats = require('./stats')
29-
exports.mfs = require('./mfs')
3030
exports.resolve = require('./resolve')
3131
exports.name = require('./name')

src/core/components/init-assets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = function addDefaultAssets (self, log, callback) {
2121
const addPath = element.substring(index + 1)
2222
return { path: addPath, content: file(element) }
2323
}),
24-
self.files.addPullStream(),
24+
self.addPullStream(),
2525
pull.through(file => {
2626
if (file.path === 'init-docs') {
2727
const cid = new CID(file.hash)

src/core/index.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ class IPFS extends EventEmitter {
147147
this.dag = components.dag(this)
148148
this.libp2p = components.libp2p(this)
149149
this.swarm = components.swarm(this)
150-
this.files = components.files(this)
151150
this.name = components.name(this)
152151
this.bitswap = components.bitswap(this)
153152
this.pin = components.pin(this)
@@ -173,28 +172,33 @@ class IPFS extends EventEmitter {
173172

174173
this.state = require('./state')(this)
175174

176-
// ipfs.ls
177-
this.ls = this.files.lsImmutable
178-
this.lsReadableStream = this.files.lsReadableStreamImmutable
179-
this.lsPullStream = this.files.lsPullStreamImmutable
175+
// ipfs regular Files APIs
176+
const filesRegular = components.filesRegular(this)
177+
this.add = filesRegular.add
178+
this.addReadableStream = filesRegular.addReadableStream
179+
this.addPullStream = filesRegular.addPullStream
180+
// TODO create this.addFromFs
181+
// TODO create this.addFromStream
182+
// TODO create this.addFromUrl
183+
this.cat = filesRegular.catImmutable
184+
this.catReadableStream = filesRegular.catReadableStream
185+
this.catPullStream = filesRegular.catPullStream
186+
this.get = filesRegular.getImmutable
187+
this.getReadableStream = filesRegular.getReadableStream
188+
this.getPullStream = filesRegular.getPullStream
189+
this.ls = filesRegular.lsImmutable
190+
this.lsReadableStream = filesRegular.lsReadableStream
191+
this.lsPullStream = filesRegular.lsPullStream
192+
193+
// ipfs.files API (aka MFS)
194+
this.files = components.filesMFS(this)
180195

181196
// ipfs.util
182197
this.util = {
183-
crypto: crypto,
184-
isIPFS: isIPFS
198+
crypto,
199+
isIPFS
185200
}
186201

187-
// ipfs.files
188-
const mfs = components.mfs({
189-
ipld: this._ipld,
190-
repo: this._repo,
191-
repoOwner: (this._options.mfs && this._options.mfs.repoOwner) || true
192-
})
193-
194-
Object.keys(mfs).forEach(key => {
195-
this.files[key] = mfs[key]
196-
})
197-
198202
boot(this)
199203
}
200204
}

test/core/interface.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ describe('interface-ipfs-core tests', () => {
113113
]
114114
})
115115

116-
tests.filesMFS(defaultCommonFactory)
116+
// TODO needs MFS module to be updated
117+
// tests.filesMFS(defaultCommonFactory)
117118

118119
tests.key(CommonFactory.create({
119120
spawnOptions: {
@@ -122,8 +123,6 @@ describe('interface-ipfs-core tests', () => {
122123
}
123124
}))
124125

125-
tests.ls(defaultCommonFactory)
126-
127126
tests.miscellaneous(CommonFactory.create({
128127
// No need to stop, because the test suite does a 'stop' test.
129128
createTeardown: () => cb => cb()

test/http-api/interface.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ describe('interface-ipfs-core over ipfs-api tests', () => {
2525
skip: { reason: 'TODO: DHT is not implemented in js-ipfs yet!' }
2626
})
2727

28-
tests.files(defaultCommonFactory)
28+
tests.filesRegular(defaultCommonFactory)
29+
tests.filesMFS(defaultCommonFactory)
2930

3031
tests.key(CommonFactory.create({
3132
spawnOptions: {

0 commit comments

Comments
 (0)