Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
interface-core cat command
Browse files Browse the repository at this point in the history
  • Loading branch information
nginnever committed May 23, 2016
1 parent cdc7467 commit 72018dc
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ test.all(common)

A valid (read: that follows this interface) IPFS core implementation, must expose the following API.

## Files

### `cat`

> Streams the data contained by an IPFS object(s) at the given IPFS multihash..
##### `Go` **WIP**

##### `JavaScript` - ipfs.cat(multihash, [callback])

`multihash` is a [multihash][] which can be passed as

- Buffer, the raw Buffer of the multihash (or of and encoded version)
- String, the toString version of the multihash (or of an encoded version)

`callback` must follow `function (err, stream) {}` signature, where `err` is an error if the operation was not successful and `stream` is a readable stream of the file.

If no `callback` is passed, a promise is returned.





## Object

### `object.new`
Expand Down
133 changes: 133 additions & 0 deletions test/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const bl = require('bl')
const bs58 = require('bs58')
const Readable = require('readable-stream')

module.exports = (common) => {
describe('.file', () => {
let ipfs

before((done) => {
common.setup((err, _ipfs) => {
expect(err).to.not.exist
ipfs = _ipfs
done()
})
})

after((done) => {
common.teardown(done)
})

// go-ipfs http and js-ipfs core have different responses
describe('.add', () => {
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', content: rs}
arr.push(filePair)
ipfs.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()
})
})
})

describe('.cat', () => {
it('returns file stream', (done) => {
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
ipfs.cat(hash, (err, file) => {
expect(err).to.not.exist
file.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
done()
}))
})
})

// This fails on js-ipfs-api
it.skip('takes a buffer input', (done) => {
const mhBuf = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
ipfs.cat(mhBuf, (err, file) => {
expect(err).to.not.exist
file.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
done()
}))
})
})

// You can add a large file to your ipfs repo and change the hash to the file after installing js-ipfs
it.skip('returns a large file', (done) => {
const hash = ''
ipfs.cat(hash, (err, file) => {
expect(err).to.not.exist
file.pipe(bl((err, bldata) => {
expect(err).to.not.exist
done()
}))
})
})

it('returns error on invalid key', (done) => {
const hash = 'somethingNotMultihash'
ipfs.cat(hash, (err, file) => {
expect(err).to.exist
const errString = err.toString()
if (errString === 'Error: invalid ipfs ref path') {
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
}
if (errString === 'Error: Invalid Key') {
expect(err.toString()).to.contain('Error: Invalid Key')
}
done()
})
})

describe('promise', () => {
it('files.cat', (done) => {
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
ipfs.cat(hash)
.then((stream) => {
stream.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
done()
}))
})
.catch((err) => {
expect(err).to.not.exist
})
})

it('returns error on invalid key', (done) => {
const hash = 'somethingNotMultihash'
ipfs.cat(hash)
.then((stream) => {})
.catch((err) => {
expect(err).to.exist
const errString = err.toString()
if (errString === 'Error: invalid ipfs ref path') {
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
}
if (errString === 'Error: Invalid Key') {
expect(err.toString()).to.contain('Error: Invalid Key')
}
done()
})
})
})
})
})
}
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

exports.all = () => {}
exports.object = require('./object')
exports.files = require('./files')
Binary file added test/test-data/15mb.random
Binary file not shown.

0 comments on commit 72018dc

Please sign in to comment.