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

Commit

Permalink
feat: add files.ls streaming methods
Browse files Browse the repository at this point in the history
N.b will not actually do any streaming until ipfs/kubo#5611 is released.
  • Loading branch information
achingbrain committed Dec 4, 2018
1 parent 88ff0df commit 2cad7dd
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 10 deletions.
2 changes: 2 additions & 0 deletions js/src/files-mfs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const tests = {
readReadableStream: require('./read-readable-stream'),
readPullStream: require('./read-pull-stream'),
ls: require('./ls'),
lsReadableStream: require('./ls-readable-stream'),
lsPullStream: require('./ls-pull-stream'),
flush: require('./flush')
}

Expand Down
107 changes: 107 additions & 0 deletions js/src/files-mfs/ls-pull-stream.js
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()
})
)
})
})
})
}
107 changes: 107 additions & 0 deletions js/src/files-mfs/ls-readable-stream.js
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()
})
})
})
})
}
20 changes: 10 additions & 10 deletions js/src/files-mfs/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ module.exports = (createCommon, options) => {

ipfs.files.ls(testDir, (err, info) => {
expect(err).to.not.exist()
expect(info).to.eql([
{ name: 'lv1', type: 0, size: 0, hash: '' },
{ name: 'b', type: 0, size: 0, hash: '' }
expect(info.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()
})
Expand All @@ -73,18 +73,18 @@ module.exports = (createCommon, options) => {

ipfs.files.ls(testDir, { l: true }, (err, info) => {
expect(err).to.not.exist()
expect(info).to.eql([
{
name: 'lv1',
type: 1,
size: 0,
hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
},
expect(info.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()
Expand Down

0 comments on commit 2cad7dd

Please sign in to comment.