This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add mfs stream ls methods (#401)
The only mfs ls method at the moment buffers the output into an array before returning it to the user. This PR adds two new methods, lsPullStream and lsReadableStream to allow the user to either buffer the output themseleves (in case they need sorting, etc) or just stream it on to an output of some sort. N.b the http API will not actually do any streaming until ipfs/kubo#5611 is released.
- Loading branch information
1 parent
9959d02
commit 55f303b
Showing
5 changed files
with
298 additions
and
10 deletions.
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const series = require('async/series') | ||
const hat = require('hat') | ||
const { getDescribe, getIt, expect } = require('../utils/mocha') | ||
const pull = require('pull-stream/pull') | ||
const onEnd = require('pull-stream/sinks/on-end') | ||
const collect = require('pull-stream/sinks/collect') | ||
|
||
module.exports = (createCommon, options) => { | ||
const describe = getDescribe(options) | ||
const it = getIt(options) | ||
const common = createCommon() | ||
|
||
describe('.files.lsPullStream', function () { | ||
this.timeout(40 * 1000) | ||
|
||
let ipfs | ||
|
||
before(function (done) { | ||
// CI takes longer to instantiate the daemon, so we need to increase the | ||
// timeout for the before step | ||
this.timeout(60 * 1000) | ||
|
||
common.setup((err, factory) => { | ||
expect(err).to.not.exist() | ||
factory.spawnNode((err, node) => { | ||
expect(err).to.not.exist() | ||
ipfs = node | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
after((done) => common.teardown(done)) | ||
|
||
it('should not ls not found file/dir, expect error', (done) => { | ||
const testDir = `/test-${hat()}` | ||
|
||
pull( | ||
ipfs.files.lsPullStream(`${testDir}/404`), | ||
onEnd((err) => { | ||
expect(err).to.exist() | ||
expect(err.message).to.include('does not exist') | ||
done() | ||
}) | ||
) | ||
}) | ||
|
||
it('should ls directory', (done) => { | ||
const testDir = `/test-${hat()}` | ||
|
||
series([ | ||
(cb) => ipfs.files.mkdir(`${testDir}/lv1`, { p: true }, cb), | ||
(cb) => ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true }, cb) | ||
], (err) => { | ||
expect(err).to.not.exist() | ||
|
||
pull( | ||
ipfs.files.lsPullStream(testDir), | ||
collect((err, entries) => { | ||
expect(err).to.not.exist() | ||
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([ | ||
{ name: 'b', type: 0, size: 0, hash: '' }, | ||
{ name: 'lv1', type: 0, size: 0, hash: '' } | ||
]) | ||
done() | ||
}) | ||
) | ||
}) | ||
}) | ||
|
||
it('should ls -l directory', (done) => { | ||
const testDir = `/test-${hat()}` | ||
|
||
series([ | ||
(cb) => ipfs.files.mkdir(`${testDir}/lv1`, { p: true }, cb), | ||
(cb) => ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true }, cb) | ||
], (err) => { | ||
expect(err).to.not.exist() | ||
|
||
pull( | ||
ipfs.files.lsPullStream(testDir, { l: true }), | ||
collect((err, entries) => { | ||
expect(err).to.not.exist() | ||
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([ | ||
{ | ||
name: 'b', | ||
type: 0, | ||
size: 13, | ||
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T' | ||
}, | ||
{ | ||
name: 'lv1', | ||
type: 1, | ||
size: 0, | ||
hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' | ||
} | ||
]) | ||
done() | ||
}) | ||
) | ||
}) | ||
}) | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const series = require('async/series') | ||
const hat = require('hat') | ||
const { getDescribe, getIt, expect } = require('../utils/mocha') | ||
|
||
module.exports = (createCommon, options) => { | ||
const describe = getDescribe(options) | ||
const it = getIt(options) | ||
const common = createCommon() | ||
|
||
describe('.files.lsReadableStream', function () { | ||
this.timeout(40 * 1000) | ||
|
||
let ipfs | ||
|
||
before(function (done) { | ||
// CI takes longer to instantiate the daemon, so we need to increase the | ||
// timeout for the before step | ||
this.timeout(60 * 1000) | ||
|
||
common.setup((err, factory) => { | ||
expect(err).to.not.exist() | ||
factory.spawnNode((err, node) => { | ||
expect(err).to.not.exist() | ||
ipfs = node | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
after((done) => common.teardown(done)) | ||
|
||
it('should not ls not found file/dir, expect error', (done) => { | ||
const testDir = `/test-${hat()}` | ||
|
||
const stream = ipfs.files.lsReadableStream(`${testDir}/404`) | ||
|
||
stream.once('error', (err) => { | ||
expect(err).to.exist() | ||
expect(err.message).to.include('does not exist') | ||
done() | ||
}) | ||
}) | ||
|
||
it('should ls directory', (done) => { | ||
const testDir = `/test-${hat()}` | ||
|
||
series([ | ||
(cb) => ipfs.files.mkdir(`${testDir}/lv1`, { p: true }, cb), | ||
(cb) => ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true }, cb) | ||
], (err) => { | ||
expect(err).to.not.exist() | ||
|
||
const stream = ipfs.files.lsReadableStream(testDir) | ||
|
||
let entries = [] | ||
|
||
stream.on('data', entry => entries.push(entry)) | ||
|
||
stream.once('end', () => { | ||
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([ | ||
{ name: 'b', type: 0, size: 0, hash: '' }, | ||
{ name: 'lv1', type: 0, size: 0, hash: '' } | ||
]) | ||
done() | ||
}) | ||
}) | ||
}) | ||
|
||
it('should ls -l directory', (done) => { | ||
const testDir = `/test-${hat()}` | ||
|
||
series([ | ||
(cb) => ipfs.files.mkdir(`${testDir}/lv1`, { p: true }, cb), | ||
(cb) => ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true }, cb) | ||
], (err) => { | ||
expect(err).to.not.exist() | ||
|
||
const stream = ipfs.files.lsReadableStream(testDir, { l: true }) | ||
|
||
let entries = [] | ||
|
||
stream.on('data', entry => entries.push(entry)) | ||
|
||
stream.once('end', () => { | ||
expect(entries.sort((a, b) => a.name.localeCompare(b.name))).to.eql([ | ||
{ | ||
name: 'b', | ||
type: 0, | ||
size: 13, | ||
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T' | ||
}, | ||
{ | ||
name: 'lv1', | ||
type: 1, | ||
size: 0, | ||
hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' | ||
} | ||
]) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |
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