diff --git a/README.md b/README.md index 900b5dc76..35e55dba2 100644 --- a/README.md +++ b/README.md @@ -172,15 +172,15 @@ $ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"P - [`ipfs.files.getPullStream(ipfsPath, [options])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#getpullstream) - `ipfs.ls` - MFS (mutable file system) specific: - - `ipfs.files.cp` - - `ipfs.files.ls` - - `ipfs.files.mkdir` - - `ipfs.files.stat` - - `ipfs.files.rm` - - `ipfs.files.read` - - `ipfs.files.write` - - `ipfs.files.mv` - - `ipfs.files.flush(path, [callback])` + - [`ipfs.files.cp([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#cp) + - [`ipfs.files.mkdir(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#mkdir) + - [`ipfs.files.stat(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#stat) + - [`ipfs.files.rm(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#rm) + - [`ipfs.files.read(path, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#read) + - [`ipfs.files.write(path, content, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#write) + - [`ipfs.files.mv([from, to], [callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#mv) + - [`ipfs.files.ls([path, options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#ls) + - [`ipfs.files.flush([path, callback])`(https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/FILES.md#flush) - [block](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md) - [`ipfs.block.get(cid, [options, callback])`](https://github.com/ipfs/interface-ipfs-core/tree/master/SPEC/BLOCK.md#get) diff --git a/src/files/flush.js b/src/files/flush.js index 9f70f2adb..ea4de8dc6 100644 --- a/src/files/flush.js +++ b/src/files/flush.js @@ -4,6 +4,11 @@ const promisify = require('promisify-es6') module.exports = (send) => { return promisify((args, callback) => { + if (typeof args === 'function') { + callback = args + args = '/' + } + return send({ path: 'files/flush', args: args diff --git a/src/files/read.js b/src/files/read.js index bf339c24b..378d53dfa 100644 --- a/src/files/read.js +++ b/src/files/read.js @@ -1,6 +1,7 @@ 'use strict' const promisify = require('promisify-es6') +const streamToValue = require('../utils/stream-to-value') module.exports = (send) => { return promisify((args, opts, callback) => { @@ -8,10 +9,11 @@ module.exports = (send) => { callback = opts opts = {} } - send({ + + send.andTransform({ path: 'files/read', args: args, qs: opts - }, callback) + }, streamToValue, callback) }) } diff --git a/test/files.spec.js b/test/files.spec.js index 3a9425d56..3c88e26b5 100644 --- a/test/files.spec.js +++ b/test/files.spec.js @@ -244,19 +244,10 @@ describe('.files (the MFS API part)', function () { .write('/test-folder/test-file-2.txt', Buffer.from('hello world'), {create: true}, (err) => { expect(err).to.not.exist() - ipfs.files.read('/test-folder/test-file-2.txt', (err, stream) => { + ipfs.files.read('/test-folder/test-file-2.txt', (err, buf) => { expect(err).to.not.exist() - - let buf = '' - stream - .on('error', (err) => expect(err).to.not.exist()) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.be.equal('hello world') - done() - }) + expect(buf.toString()).to.be.equal('hello world') + done() }) }) }) @@ -266,21 +257,10 @@ describe('.files (the MFS API part)', function () { .write('/test-folder/test-file-2.txt', Buffer.from('hello world'), (err) => { expect(err).to.not.exist() - ipfs.files.read('/test-folder/test-file-2.txt', (err, stream) => { + ipfs.files.read('/test-folder/test-file-2.txt', (err, buf) => { expect(err).to.not.exist() - - let buf = '' - stream - .on('error', (err) => { - expect(err).to.not.exist() - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.be.equal('hello world') - done() - }) + expect(buf.toString()).to.be.equal('hello world') + done() }) }) }) @@ -315,20 +295,10 @@ describe('.files (the MFS API part)', function () { return done() } - ipfs.files.read('/test-folder/test-file', (err, stream) => { + ipfs.files.read('/test-folder/test-file', (err, buf) => { expect(err).to.not.exist() - let buf = '' - stream - .on('error', (err) => { - expect(err).to.not.exist() - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(Buffer.from(buf)).to.deep.equal(testfile) - done() - }) + expect(Buffer.from(buf)).to.deep.equal(testfile) + done() }) }) @@ -417,19 +387,9 @@ describe('.files (the MFS API part)', function () { .then(() => { return ipfs.files.read('/test-folder/test-file-2.txt') }) - .then((stream) => { - let buf = '' - stream - .on('error', (err) => { - expect(err).to.not.exist() - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.be.equal('hello world') - done() - }) + .then((buf) => { + expect(buf.toString()).to.be.equal('hello world') + done() }) .catch(done) }) @@ -440,19 +400,9 @@ describe('.files (the MFS API part)', function () { .then(() => { return ipfs.files.read('/test-folder/test-file-2.txt') }) - .then((stream) => { - let buf = '' - stream - .on('error', (err) => { - expect(err).to.not.exist() - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.be.equal('hello world') - done() - }) + .then((buf) => { + expect(buf.toString()).to.be.equal('hello world') + done() }) .catch(done) }) @@ -482,19 +432,9 @@ describe('.files (the MFS API part)', function () { if (!isNode) { return done() } ipfs.files.read('/test-folder/test-file') - .then((stream) => { - let buf = '' - stream - .on('error', (err) => { - expect(err).to.not.exist() - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(Buffer.from(buf)).to.eql(testfile) - done() - }) + .then((buf) => { + expect(Buffer.from(buf)).to.eql(testfile) + done() }) })