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

Commit 536a01e

Browse files
committed
chore: update method signatures in line with ipfs-inactive/interface-js-ipfs-core#277
1 parent 5e7b7c4 commit 536a01e

File tree

5 files changed

+148
-50
lines changed

5 files changed

+148
-50
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"flatmap": "0.0.3",
3636
"glob": "^7.1.2",
3737
"ipfs-block": "~0.7.1",
38-
"ipfs-unixfs": "~0.1.14",
38+
"ipfs-unixfs": "~0.1.15",
3939
"ipld-dag-cbor": "~0.12.0",
4040
"ipld-dag-pb": "~0.14.4",
4141
"is-ipfs": "~0.3.2",

src/files/cp.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,28 @@
33
const promisify = require('promisify-es6')
44

55
module.exports = (send) => {
6-
return promisify((args, opts, callback) => {
7-
if (typeof (opts) === 'function') {
8-
callback = opts
9-
opts = {}
6+
return promisify(function () {
7+
const args = Array.prototype.slice.call(arguments)
8+
9+
const callback = args.pop()
10+
let opts = {}
11+
let sources = []
12+
13+
if (!Array.isArray(args[args.length - 1]) && typeof args[args.length - 1] === 'object') {
14+
opts = args.pop()
1015
}
16+
17+
if (args.length === 1 && Array.isArray(args[0])) {
18+
// support ipfs.file.cp([src, dest], opts, cb)
19+
sources = args[0]
20+
} else {
21+
// support ipfs.file.cp(src, dest, opts, cb) and ipfs.file.cp(src1, src2, dest, opts, cb)
22+
sources = args
23+
}
24+
1125
send({
1226
path: 'files/cp',
13-
args: args,
27+
args: sources,
1428
qs: opts
1529
}, callback)
1630
})

src/files/ls.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ module.exports = (send) => {
2121
callback = opts
2222
opts = {}
2323
}
24+
25+
if (typeof (args) === 'function') {
26+
callback = args
27+
opts = {}
28+
args = null
29+
}
30+
2431
return send.andTransform({
2532
path: 'files/ls',
2633
args: args,

src/files/mv.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33
const promisify = require('promisify-es6')
44

55
module.exports = (send) => {
6-
return promisify((args, opts, callback) => {
7-
if (typeof opts === 'function' &&
8-
callback === undefined) {
9-
callback = opts
10-
opts = {}
6+
return promisify(function () {
7+
const args = Array.prototype.slice.call(arguments)
8+
const callback = args.pop()
9+
let opts = {}
10+
let sources = []
11+
12+
if (!Array.isArray(args[args.length - 1]) && typeof args[args.length - 1] === 'object') {
13+
opts = args.pop()
14+
}
15+
16+
if (args.length === 1 && Array.isArray(args[0])) {
17+
// support ipfs.file.mv([src, dest], opts, cb)
18+
sources = args[0]
19+
} else {
20+
// support ipfs.file.mv(src, dest, opts, cb) and ipfs.file.cp(src1, src2, dest, opts, cb)
21+
sources = args
1122
}
23+
1224
send({
1325
path: 'files/mv',
1426
args: args,

test/files.spec.js

Lines changed: 104 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -330,22 +330,80 @@ describe('.files (the MFS API part)', function () {
330330
ipfs.files.flush('/', done)
331331
})
332332

333-
it('files.cp', (done) => {
334-
ipfs.files.cp([
335-
'/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
336-
'/test-folder/test-file'
337-
], (err) => {
338-
expect(err).to.not.exist()
339-
done()
340-
})
333+
it('files.cp', () => {
334+
const folder = `/test-folder-${Math.random()}`
335+
336+
return ipfs.files.mkdir(folder)
337+
.then(() => ipfs.files.cp([
338+
'/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
339+
`${folder}/test-file-${Math.random()}`
340+
]))
341341
})
342342

343-
it('files.ls', (done) => {
344-
ipfs.files.ls('/test-folder', (err, res) => {
345-
expect(err).to.not.exist()
346-
expect(res.length).to.equal(1)
347-
done()
348-
})
343+
it('files.cp with non-array arguments', () => {
344+
const folder = `/test-folder-${Math.random()}`
345+
346+
return ipfs.files.mkdir(folder)
347+
.then(() => ipfs.files.cp(
348+
'/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
349+
`${folder}/test-file-${Math.random()}`
350+
))
351+
})
352+
353+
it('files.mv', () => {
354+
const folder = `/test-folder-${Math.random()}`
355+
const source = `${folder}/test-file-${Math.random()}`
356+
const dest = `${folder}/test-file-${Math.random()}`
357+
358+
return ipfs.files.mkdir(folder)
359+
.then(() => ipfs.files.cp(
360+
'/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
361+
source
362+
))
363+
.then(() => ipfs.files.mv([
364+
source,
365+
dest
366+
]))
367+
})
368+
369+
it('files.mv with non-array arguments', () => {
370+
const folder = `/test-folder-${Math.random()}`
371+
const source = `${folder}/test-file-${Math.random()}`
372+
const dest = `${folder}/test-file-${Math.random()}`
373+
374+
return ipfs.files.mkdir(folder)
375+
.then(() => ipfs.files.cp(
376+
'/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
377+
source
378+
))
379+
.then(() => ipfs.files.mv(
380+
source,
381+
dest
382+
))
383+
})
384+
385+
it('files.ls', () => {
386+
const folder = `/test-folder-${Math.random()}`
387+
const file = `${folder}/test-file-${Math.random()}`
388+
389+
return ipfs.files.mkdir(folder)
390+
.then(() => ipfs.files.write(file, Buffer.from('Hello, world'), {
391+
create: true
392+
}))
393+
.then(() => ipfs.files.ls(folder))
394+
.then(files => {
395+
expect(files.length).to.equal(1)
396+
})
397+
})
398+
399+
it('files.ls mfs root by default', () => {
400+
const folder = `/test-folder-${Math.random()}`
401+
402+
return ipfs.files.mkdir(folder)
403+
.then(() => ipfs.files.ls())
404+
.then(files => {
405+
expect(files.length).to.equal(1)
406+
})
349407
})
350408

351409
it('files.write', (done) => {
@@ -374,22 +432,27 @@ describe('.files (the MFS API part)', function () {
374432
})
375433
})
376434

377-
it('files.stat', (done) => {
378-
ipfs.files.stat('/test-folder/test-file', (err, res) => {
379-
expect(err).to.not.exist()
380-
expect(res).to.deep.equal({
381-
hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
382-
size: 12,
383-
cumulativeSize: 20,
384-
blocks: 0,
385-
type: 'file',
386-
withLocality: false,
387-
local: undefined,
388-
sizeLocal: undefined
435+
it('files.stat', () => {
436+
const folder = `/test-folder-${Math.random()}`
437+
const file = `${folder}/test-file-${Math.random()}`
438+
439+
return ipfs.files.mkdir(folder)
440+
.then(() => ipfs.files.write(file, testfile, {
441+
create: true
442+
}))
443+
.then(() => ipfs.files.stat(file))
444+
.then((stats) => {
445+
expect(stats).to.deep.equal({
446+
hash: 'QmQhouoDPAnzhVM148yCa9CbUXK65wSEAZBtgrLGHtmdmP',
447+
size: 12,
448+
cumulativeSize: 70,
449+
blocks: 1,
450+
type: 'file',
451+
withLocality: false,
452+
local: undefined,
453+
sizeLocal: undefined
454+
})
389455
})
390-
391-
done()
392-
})
393456
})
394457

395458
it('files.stat file that does not exist()', (done) => {
@@ -402,16 +465,18 @@ describe('.files (the MFS API part)', function () {
402465
})
403466
})
404467

405-
it('files.read', (done) => {
406-
if (!isNode) {
407-
return done()
408-
}
409-
410-
ipfs.files.read('/test-folder/test-file', (err, buf) => {
411-
expect(err).to.not.exist()
412-
expect(Buffer.from(buf)).to.deep.equal(testfile)
413-
done()
414-
})
468+
it('files.read', () => {
469+
const folder = `/test-folder-${Math.random()}`
470+
const file = `${folder}/test-file-${Math.random()}`
471+
472+
return ipfs.files.mkdir(folder)
473+
.then(() => ipfs.files.write(file, testfile, {
474+
create: true
475+
}))
476+
.then(() => ipfs.files.read(file))
477+
.then((buf) => {
478+
expect(Buffer.from(buf)).to.deep.equal(testfile)
479+
})
415480
})
416481

417482
it('files.rm without options', (done) => {

0 commit comments

Comments
 (0)