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

Commit

Permalink
feat(ipfs.get): Add the ipfs.files.get call
Browse files Browse the repository at this point in the history
  • Loading branch information
nginnever authored and daviddias committed Aug 10, 2016
1 parent a0ec626 commit 6f97418
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/api/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

module.exports = (send) => {
return function get (path, opts, cb) {
if (typeof opts === 'function' && !cb) {
cb = opts
opts = {}
}
return send('get', path, opts, null, cb)
}
}
1 change: 1 addition & 0 deletions src/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function requireCommands () {
dht: require('./api/dht'),
diag: require('./api/diag'),
id: require('./api/id'),
get: require('./api/get'),
log: require('./api/log'),
ls: require('./api/ls'),
mount: require('./api/mount'),
Expand Down
101 changes: 101 additions & 0 deletions test/api/get.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-env mocha */
/* globals apiClients */
'use strict'

const expect = require('chai').expect
const isNode = require('detect-node')
const fs = require('fs')
const bl = require('bl')

const path = require('path')
const streamEqual = require('stream-equal')

let testfile
let testfileBig

if (isNode) {
testfile = fs.readFileSync(path.join(__dirname, '/../testfile.txt'))
testfileBig = fs.createReadStream(path.join(__dirname, '/../15mb.random'), { bufferSize: 128 })
} else {
testfile = require('raw!../testfile.txt')
}

describe('.get', () => {
it('get with no compression args', (done) => {
apiClients.a
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', (err, res) => {
expect(err).to.not.exist
res.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.contain(testfile.toString())
done()
}))
})
})

it('get with archive true', (done) => {
apiClients.a
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {archive: true}, (err, res) => {
expect(err).to.not.exist
res.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.contain(testfile.toString())
done()
}))
})
})

it('get err with out of range compression level', (done) => {
apiClients.a
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {compress: true, 'compression-level': 10}, (err, res) => {
expect(err).to.exist
expect(err.toString()).to.equal('Error: Compression level must be between 1 and 9')
done()
})
})

it('get with compression level', (done) => {
apiClients.a
.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP', {compress: true, 'compression-level': 1}, (err, res) => {
expect(err).to.not.exist
done()
})
})

it.skip('get BIG file', (done) => {
if (!isNode) {
return done()
}

apiClients.a.get('Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq', (err, res) => {
expect(err).to.not.exist

// Do not blow out the memory of nodejs :)
streamEqual(res, testfileBig, (err, equal) => {
expect(err).to.not.exist
expect(equal).to.be.true
done()
})
})
})

describe('promise', () => {
it.skip('get', (done) => {
return apiClients.a.get('Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP')
.then((res) => {
let buf = ''
res
.on('error', (err) => {
throw err
})
.on('data', (data) => {
buf += data
})
.on('end', () => {
expect(buf).to.contain(testfile.toString())
done()
})
})
})
})
})

0 comments on commit 6f97418

Please sign in to comment.