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

feat: move regular files api to top level, add addFromFs and addFromUrl #378

Merged
merged 10 commits into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
386 changes: 229 additions & 157 deletions SPEC/FILES.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions js/src/dht/provide.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = (createCommon, options) => {
after((done) => common.teardown(done))

it('should provide local CID', (done) => {
ipfs.files.add(Buffer.from('test'), (err, res) => {
ipfs.add(Buffer.from('test'), (err, res) => {
if (err) return done(err)

ipfs.dht.provide(new CID(res[0].hash), (err) => {
Expand All @@ -56,7 +56,7 @@ module.exports = (createCommon, options) => {
})

it('should allow multiple CIDs to be passed', (done) => {
ipfs.files.add([Buffer.from('t0'), Buffer.from('t1')], (err, res) => {
ipfs.add([Buffer.from('t0'), Buffer.from('t1')], (err, res) => {
if (err) return done(err)

ipfs.dht.provide([
Expand All @@ -70,7 +70,7 @@ module.exports = (createCommon, options) => {
})

it('should provide a CIDv1', (done) => {
ipfs.files.add(Buffer.from('test'), { 'cid-version': 1 }, (err, res) => {
ipfs.add(Buffer.from('test'), { 'cid-version': 1 }, (err, res) => {
if (err) return done(err)

const cid = new CID(res[0].hash)
Expand Down
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions js/src/files-mfs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

const { createSuite } = require('../utils/suite')

const tests = {
mkdir: require('./mkdir'),
write: require('./write'),
cp: require('./cp'),
mv: require('./mv'),
rm: require('./rm'),
stat: require('./stat'),
read: require('./read'),
readReadableStream: require('./read-readable-stream'),
readPullStream: require('./read-pull-stream'),
ls: require('./ls'),
flush: require('./flush')
}

module.exports = createSuite(tests)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions js/src/files/stat.js → js/src/files-mfs/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const series = require('async/series')
const hat = require('hat')
const { fixtures } = require('./utils')
const { fixtures } = require('../files-regular/utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')

module.exports = (createCommon, options) => {
Expand Down Expand Up @@ -31,7 +31,7 @@ module.exports = (createCommon, options) => {
})
})

before((done) => ipfs.files.add(fixtures.smallFile.data, done))
before((done) => ipfs.add(fixtures.smallFile.data, done))

after((done) => common.teardown(done))

Expand Down
File renamed without changes.
100 changes: 100 additions & 0 deletions js/src/files-regular/add-from-fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* eslint-env mocha */
'use strict'

const path = require('path')
const expectTimeout = require('../utils/expect-timeout')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const fs = require('fs')
const os = require('os')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.addFromFs', function () {
this.timeout(40 * 1000)

const fixturesPath = path.join(__dirname, '../../test/fixtures')
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('a directory', (done) => {
const filesPath = path.join(fixturesPath, 'test-folder')
ipfs.addFromFs(filesPath, { recursive: true }, (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.be.above(8)
done()
})
})

it('a directory with an odd name', (done) => {
const filesPath = path.join(fixturesPath, 'weird name folder [v0]')
ipfs.addFromFs(filesPath, { recursive: true }, (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.be.above(8)
done()
})
})

it('add and ignore a directory', (done) => {
const filesPath = path.join(fixturesPath, 'test-folder')
ipfs.addFromFs(filesPath, { recursive: true, ignore: ['files/**'] }, (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.be.below(9)
done()
})
})

it('a file', (done) => {
const filePath = path.join(fixturesPath, 'testfile.txt')
ipfs.addFromFs(filePath, (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.equal(1)
expect(result[0].path).to.equal('testfile.txt')
done()
})
})

it('a hidden file in a directory', (done) => {
const filesPath = path.join(fixturesPath, 'hidden-files-folder')
ipfs.addFromFs(filesPath, { recursive: true, hidden: true }, (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.be.above(10)
expect(result.map(object => object.path)).to.include('hidden-files-folder/.hiddenTest.txt')
expect(result.map(object => object.hash)).to.include('QmdbAjVmLRdpFyi8FFvjPfhTGB2cVXvWLuK7Sbt38HXrtt')
done()
})
})

it('with only-hash=true', function () {
this.slow(10 * 1000)

const content = String(Math.random() + Date.now())
const filepath = path.join(os.tmpdir(), `${content}.txt`)
fs.writeFileSync(filepath, content)

return ipfs.addFromFs(filepath, { onlyHash: true })
.then(out => {
fs.unlinkSync(filepath)
return expectTimeout(ipfs.object.get(out[0].hash), 4000)
})
})
})
}
45 changes: 45 additions & 0 deletions js/src/files-regular/add-from-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-env mocha */
'use strict'

const loadFixture = require('aegir/fixtures')
const into = require('into-stream')
const { getDescribe, getIt, expect } = require('../utils/mocha')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.addFromStream', 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('same as .add', (done) => {
const testData = loadFixture('js/test/fixtures/15mb.random', 'interface-ipfs-core')

ipfs.addFromStream(into(testData), (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.equal(1)
done()
})
})
})
}
Loading