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

Commit 3dc7278

Browse files
daviddiasAlan Shaw
authored and
Alan Shaw
committed
feat: move regular files api to top level, add addFromFs and addFromUrl (#378)
1 parent cc60408 commit 3dc7278

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+14461
-278
lines changed

SPEC/FILES.md

Lines changed: 229 additions & 157 deletions
Large diffs are not rendered by default.

js/src/dht/provide.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = (createCommon, options) => {
3535
after((done) => common.teardown(done))
3636

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

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

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

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

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

7676
const cid = new CID(res[0].hash)
File renamed without changes.
File renamed without changes.

js/src/files-mfs/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const { createSuite } = require('../utils/suite')
4+
5+
const tests = {
6+
mkdir: require('./mkdir'),
7+
write: require('./write'),
8+
cp: require('./cp'),
9+
mv: require('./mv'),
10+
rm: require('./rm'),
11+
stat: require('./stat'),
12+
read: require('./read'),
13+
readReadableStream: require('./read-readable-stream'),
14+
readPullStream: require('./read-pull-stream'),
15+
ls: require('./ls'),
16+
flush: require('./flush')
17+
}
18+
19+
module.exports = createSuite(tests)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

js/src/files/stat.js renamed to js/src/files-mfs/stat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const series = require('async/series')
55
const hat = require('hat')
6-
const { fixtures } = require('./utils')
6+
const { fixtures } = require('../files-regular/utils')
77
const { getDescribe, getIt, expect } = require('../utils/mocha')
88

99
module.exports = (createCommon, options) => {
@@ -31,7 +31,7 @@ module.exports = (createCommon, options) => {
3131
})
3232
})
3333

34-
before((done) => ipfs.files.add(fixtures.smallFile.data, done))
34+
before((done) => ipfs.add(fixtures.smallFile.data, done))
3535

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

File renamed without changes.

js/src/files-regular/add-from-fs.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const path = require('path')
5+
const expectTimeout = require('../utils/expect-timeout')
6+
const { getDescribe, getIt, expect } = require('../utils/mocha')
7+
const fs = require('fs')
8+
const os = require('os')
9+
10+
module.exports = (createCommon, options) => {
11+
const describe = getDescribe(options)
12+
const it = getIt(options)
13+
const common = createCommon()
14+
15+
describe('.addFromFs', function () {
16+
this.timeout(40 * 1000)
17+
18+
const fixturesPath = path.join(__dirname, '../../test/fixtures')
19+
let ipfs
20+
21+
before(function (done) {
22+
// CI takes longer to instantiate the daemon, so we need to increase the
23+
// timeout for the before step
24+
this.timeout(60 * 1000)
25+
26+
common.setup((err, factory) => {
27+
expect(err).to.not.exist()
28+
factory.spawnNode((err, node) => {
29+
expect(err).to.not.exist()
30+
ipfs = node
31+
done()
32+
})
33+
})
34+
})
35+
36+
after((done) => common.teardown(done))
37+
38+
it('should add a directory from the file system', (done) => {
39+
const filesPath = path.join(fixturesPath, 'test-folder')
40+
ipfs.addFromFs(filesPath, { recursive: true }, (err, result) => {
41+
expect(err).to.not.exist()
42+
expect(result.length).to.be.above(8)
43+
done()
44+
})
45+
})
46+
47+
it('should add a directory from the file system with an odd name', (done) => {
48+
const filesPath = path.join(fixturesPath, 'weird name folder [v0]')
49+
ipfs.addFromFs(filesPath, { recursive: true }, (err, result) => {
50+
expect(err).to.not.exist()
51+
expect(result.length).to.be.above(8)
52+
done()
53+
})
54+
})
55+
56+
it('should ignore a directory from the file system', (done) => {
57+
const filesPath = path.join(fixturesPath, 'test-folder')
58+
ipfs.addFromFs(filesPath, { recursive: true, ignore: ['files/**'] }, (err, result) => {
59+
expect(err).to.not.exist()
60+
expect(result.length).to.be.below(9)
61+
done()
62+
})
63+
})
64+
65+
it('should add a file from the file system', (done) => {
66+
const filePath = path.join(fixturesPath, 'testfile.txt')
67+
ipfs.addFromFs(filePath, (err, result) => {
68+
expect(err).to.not.exist()
69+
expect(result.length).to.equal(1)
70+
expect(result[0].path).to.equal('testfile.txt')
71+
done()
72+
})
73+
})
74+
75+
it('should add a hidden file in a directory from the file system', (done) => {
76+
const filesPath = path.join(fixturesPath, 'hidden-files-folder')
77+
ipfs.addFromFs(filesPath, { recursive: true, hidden: true }, (err, result) => {
78+
expect(err).to.not.exist()
79+
expect(result.length).to.be.above(10)
80+
expect(result.map(object => object.path)).to.include('hidden-files-folder/.hiddenTest.txt')
81+
expect(result.map(object => object.hash)).to.include('QmdbAjVmLRdpFyi8FFvjPfhTGB2cVXvWLuK7Sbt38HXrtt')
82+
done()
83+
})
84+
})
85+
86+
it('should add a file from the file system with only-hash=true', function () {
87+
this.slow(10 * 1000)
88+
89+
const content = String(Math.random() + Date.now())
90+
const filepath = path.join(os.tmpdir(), `${content}.txt`)
91+
fs.writeFileSync(filepath, content)
92+
93+
return ipfs.addFromFs(filepath, { onlyHash: true })
94+
.then(out => {
95+
fs.unlinkSync(filepath)
96+
return expectTimeout(ipfs.object.get(out[0].hash), 4000)
97+
})
98+
})
99+
})
100+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const loadFixture = require('aegir/fixtures')
5+
const into = require('into-stream')
6+
const { getDescribe, getIt, expect } = require('../utils/mocha')
7+
8+
module.exports = (createCommon, options) => {
9+
const describe = getDescribe(options)
10+
const it = getIt(options)
11+
const common = createCommon()
12+
13+
describe('.addFromStream', function () {
14+
this.timeout(40 * 1000)
15+
16+
let ipfs
17+
18+
before(function (done) {
19+
// CI takes longer to instantiate the daemon, so we need to increase the
20+
// timeout for the before step
21+
this.timeout(60 * 1000)
22+
23+
common.setup((err, factory) => {
24+
expect(err).to.not.exist()
25+
factory.spawnNode((err, node) => {
26+
expect(err).to.not.exist()
27+
ipfs = node
28+
done()
29+
})
30+
})
31+
})
32+
33+
after((done) => common.teardown(done))
34+
35+
it('should add from a stream', (done) => {
36+
const testData = loadFixture('js/test/fixtures/15mb.random', 'interface-ipfs-core')
37+
38+
ipfs.addFromStream(into(testData), (err, result) => {
39+
expect(err).to.not.exist()
40+
expect(result.length).to.equal(1)
41+
done()
42+
})
43+
})
44+
})
45+
}

0 commit comments

Comments
 (0)