This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
updated cat core to return just file stream #253
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,19 +3,20 @@ | |
const Importer = require('ipfs-unixfs-engine').importer | ||
const Exporter = require('ipfs-unixfs-engine').exporter | ||
const UnixFS = require('ipfs-unixfs') | ||
const promisify = require('promisify-es6') | ||
|
||
module.exports = function files (self) { | ||
return { | ||
add: (arr, callback) => { | ||
add: promisify((arr, cb) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. our pattern is to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's just @diasdavid s pattern 😛 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. haha to callback or cb, i'll switch back to callback here |
||
if (typeof arr === 'function') { | ||
callback = arr | ||
cb = arr | ||
arr = undefined | ||
} | ||
if (callback === undefined) { | ||
callback = function noop () {} | ||
if (cb === undefined) { | ||
cb = function noop () {} | ||
} | ||
if (arr === undefined) { | ||
return new Importer(self._dagS) | ||
cb(null, new Importer(self._dagS)) | ||
} | ||
|
||
const i = new Importer(self._dagS) | ||
|
@@ -26,32 +27,36 @@ module.exports = function files (self) { | |
}) | ||
|
||
i.once('end', () => { | ||
callback(null, res) | ||
cb(null, res) | ||
}) | ||
|
||
arr.forEach((tuple) => { | ||
i.write(tuple) | ||
}) | ||
|
||
i.end() | ||
}, | ||
cat: (hash, callback) => { | ||
}), | ||
|
||
cat: promisify((hash, cb) => { | ||
self._dagS.get(hash, (err, fetchedNode) => { | ||
if (err) { | ||
return callback(err, null) | ||
return cb(err, null) | ||
} | ||
const data = UnixFS.unmarshal(fetchedNode.data) | ||
if (data.type === 'directory') { | ||
callback('This dag node is a directory', null) | ||
cb('This dag node is a directory', null) | ||
} else { | ||
const exportStream = Exporter(hash, self._dagS) | ||
callback(null, exportStream) | ||
exportStream.once('data', (object) => { | ||
cb(null, object.stream) | ||
}) | ||
} | ||
}) | ||
}, | ||
get: (hash, callback) => { | ||
}), | ||
|
||
get: promisify((hash, cb) => { | ||
var exportFile = Exporter(hash, self._dagS) | ||
callback(null, exportFile) | ||
} | ||
cb(null, exportFile) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,20 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const bl = require('bl') | ||
const expect = require('chai').expect | ||
const Readable = require('stream').Readable | ||
const bs58 = require('bs58') | ||
const test = require('interface-ipfs-core') | ||
|
||
const IPFS = require('../../src/core') | ||
|
||
describe('files', () => { | ||
let ipfs | ||
|
||
before((done) => { | ||
ipfs = new IPFS(require('./repo-path')) | ||
ipfs.load(done) | ||
}) | ||
|
||
it('add', (done) => { | ||
const buffered = new Buffer('some data') | ||
const rs = new Readable() | ||
rs.push(buffered) | ||
rs.push(null) | ||
const arr = [] | ||
const filePair = {path: 'data.txt', stream: rs} | ||
arr.push(filePair) | ||
ipfs.files.add(arr, (err, res) => { | ||
expect(err).to.not.exist | ||
expect(res[0].path).to.equal('data.txt') | ||
expect(res[0].size).to.equal(17) | ||
expect(bs58.encode(res[0].multihash).toString()).to.equal('QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS') | ||
done() | ||
const common = { | ||
setup: function (cb) { | ||
const ipfs = new IPFS(require('./repo-path')) | ||
ipfs.load(() => { | ||
cb(null, ipfs) | ||
}) | ||
}) | ||
}, | ||
teardown: function (cb) { | ||
cb() | ||
} | ||
} | ||
|
||
it('cat', (done) => { | ||
const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' | ||
ipfs.files.cat(hash, (err, res) => { | ||
expect(err).to.not.exist | ||
res.on('data', (data) => { | ||
data.stream.pipe(bl((err, bldata) => { | ||
expect(err).to.not.exist | ||
expect(bldata.toString()).to.equal('hello world\n') | ||
done() | ||
})) | ||
}) | ||
}) | ||
}) | ||
|
||
it('get', (done) => { | ||
// TODO create non-trival get test | ||
const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o' | ||
ipfs.files.get(hash, (err, res) => { | ||
expect(err).to.not.exist | ||
res.on('data', (data) => { | ||
data.stream.pipe(bl((err, bldata) => { | ||
expect(err).to.not.exist | ||
expect(bldata.toString()).to.equal('hello world\n') | ||
done() | ||
})) | ||
}) | ||
}) | ||
}) | ||
}) | ||
test.files(common) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why console.log and file.pipe(process.stdout?
The test for cat is not checking the output, and that is why this got missed, please update the test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whooops, this is using the interface-core tests now :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is on the CLI, which is not covered by the interface-ipfs-core tests. If you check the cli-test, you are not checking the output of the CLI.