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

Commit 72018dc

Browse files
committed
interface-core cat command
1 parent cdc7467 commit 72018dc

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ test.all(common)
5555

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

58+
## Files
59+
60+
### `cat`
61+
62+
> Streams the data contained by an IPFS object(s) at the given IPFS multihash..
63+
64+
##### `Go` **WIP**
65+
66+
##### `JavaScript` - ipfs.cat(multihash, [callback])
67+
68+
`multihash` is a [multihash][] which can be passed as
69+
70+
- Buffer, the raw Buffer of the multihash (or of and encoded version)
71+
- String, the toString version of the multihash (or of an encoded version)
72+
73+
`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.
74+
75+
If no `callback` is passed, a promise is returned.
76+
77+
78+
79+
80+
5881
## Object
5982

6083
### `object.new`

test/files.js

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const expect = require('chai').expect
5+
const bl = require('bl')
6+
const bs58 = require('bs58')
7+
const Readable = require('readable-stream')
8+
9+
module.exports = (common) => {
10+
describe('.file', () => {
11+
let ipfs
12+
13+
before((done) => {
14+
common.setup((err, _ipfs) => {
15+
expect(err).to.not.exist
16+
ipfs = _ipfs
17+
done()
18+
})
19+
})
20+
21+
after((done) => {
22+
common.teardown(done)
23+
})
24+
25+
// go-ipfs http and js-ipfs core have different responses
26+
describe('.add', () => {
27+
it('add', (done) => {
28+
const buffered = new Buffer('some data')
29+
const rs = new Readable()
30+
rs.push(buffered)
31+
rs.push(null)
32+
const arr = []
33+
const filePair = {path: 'data.txt', content: rs}
34+
arr.push(filePair)
35+
ipfs.add(arr, (err, res) => {
36+
expect(err).to.not.exist
37+
expect(res[0].path).to.equal('data.txt')
38+
expect(res[0].size).to.equal(17)
39+
expect(bs58.encode(res[0].multihash).toString()).to.equal('QmVv4Wz46JaZJeH5PMV4LGbRiiMKEmszPYY3g6fjGnVXBS')
40+
done()
41+
})
42+
})
43+
})
44+
45+
describe('.cat', () => {
46+
it('returns file stream', (done) => {
47+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
48+
ipfs.cat(hash, (err, file) => {
49+
expect(err).to.not.exist
50+
file.pipe(bl((err, bldata) => {
51+
expect(err).to.not.exist
52+
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
53+
done()
54+
}))
55+
})
56+
})
57+
58+
// This fails on js-ipfs-api
59+
it.skip('takes a buffer input', (done) => {
60+
const mhBuf = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
61+
ipfs.cat(mhBuf, (err, file) => {
62+
expect(err).to.not.exist
63+
file.pipe(bl((err, bldata) => {
64+
expect(err).to.not.exist
65+
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
66+
done()
67+
}))
68+
})
69+
})
70+
71+
// You can add a large file to your ipfs repo and change the hash to the file after installing js-ipfs
72+
it.skip('returns a large file', (done) => {
73+
const hash = ''
74+
ipfs.cat(hash, (err, file) => {
75+
expect(err).to.not.exist
76+
file.pipe(bl((err, bldata) => {
77+
expect(err).to.not.exist
78+
done()
79+
}))
80+
})
81+
})
82+
83+
it('returns error on invalid key', (done) => {
84+
const hash = 'somethingNotMultihash'
85+
ipfs.cat(hash, (err, file) => {
86+
expect(err).to.exist
87+
const errString = err.toString()
88+
if (errString === 'Error: invalid ipfs ref path') {
89+
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
90+
}
91+
if (errString === 'Error: Invalid Key') {
92+
expect(err.toString()).to.contain('Error: Invalid Key')
93+
}
94+
done()
95+
})
96+
})
97+
98+
describe('promise', () => {
99+
it('files.cat', (done) => {
100+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
101+
ipfs.cat(hash)
102+
.then((stream) => {
103+
stream.pipe(bl((err, bldata) => {
104+
expect(err).to.not.exist
105+
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
106+
done()
107+
}))
108+
})
109+
.catch((err) => {
110+
expect(err).to.not.exist
111+
})
112+
})
113+
114+
it('returns error on invalid key', (done) => {
115+
const hash = 'somethingNotMultihash'
116+
ipfs.cat(hash)
117+
.then((stream) => {})
118+
.catch((err) => {
119+
expect(err).to.exist
120+
const errString = err.toString()
121+
if (errString === 'Error: invalid ipfs ref path') {
122+
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
123+
}
124+
if (errString === 'Error: Invalid Key') {
125+
expect(err.toString()).to.contain('Error: Invalid Key')
126+
}
127+
done()
128+
})
129+
})
130+
})
131+
})
132+
})
133+
}

test/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
exports.all = () => {}
44
exports.object = require('./object')
5+
exports.files = require('./files')

test/test-data/15mb.random

14.3 MB
Binary file not shown.

0 commit comments

Comments
 (0)