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

Commit

Permalink
Merge pull request #786 from ipfs/support-non-array-arguments
Browse files Browse the repository at this point in the history
chore: update method signatures in line with ipfs-inactive/interface-js-ipfs-core#277
  • Loading branch information
achingbrain authored Jun 13, 2018
2 parents 5e7b7c4 + f959aad commit a54f0e3
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"flatmap": "0.0.3",
"glob": "^7.1.2",
"ipfs-block": "~0.7.1",
"ipfs-unixfs": "~0.1.14",
"ipfs-unixfs": "~0.1.15",
"ipld-dag-cbor": "~0.12.0",
"ipld-dag-pb": "~0.14.4",
"is-ipfs": "~0.3.2",
Expand Down
15 changes: 9 additions & 6 deletions src/files/cp.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
'use strict'

const promisify = require('promisify-es6')
const findSources = require('../utils/find-sources')

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
return promisify(function () {
const {
callback,
sources,
opts
} = findSources(Array.prototype.slice.call(arguments))

send({
path: 'files/cp',
args: args,
args: sources,
qs: opts
}, callback)
})
Expand Down
7 changes: 7 additions & 0 deletions src/files/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ module.exports = (send) => {
callback = opts
opts = {}
}

if (typeof (args) === 'function') {
callback = args
opts = {}
args = null
}

return send.andTransform({
path: 'files/ls',
args: args,
Expand Down
16 changes: 9 additions & 7 deletions src/files/mv.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'use strict'

const promisify = require('promisify-es6')
const findSources = require('../utils/find-sources')

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof opts === 'function' &&
callback === undefined) {
callback = opts
opts = {}
}
return promisify(function () {
const {
callback,
sources,
opts
} = findSources(Array.prototype.slice.call(arguments))

send({
path: 'files/mv',
args: args,
args: sources,
qs: opts
}, callback)
})
Expand Down
25 changes: 25 additions & 0 deletions src/utils/find-sources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

module.exports = (args) => {
const callback = args.pop()
let opts = {}
let sources = []

if (!Array.isArray(args[args.length - 1]) && typeof args[args.length - 1] === 'object') {
opts = args.pop()
}

if (args.length === 1 && Array.isArray(args[0])) {
// support ipfs.file.cp([src, dest], opts, cb)
sources = args[0]
} else {
// support ipfs.file.cp(src, dest, opts, cb) and ipfs.file.cp(src1, src2, dest, opts, cb)
sources = args
}

return {
callback,
sources,
opts
}
}
144 changes: 104 additions & 40 deletions test/files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const isNode = require('detect-node')
const loadFixture = require('aegir/fixtures')
const mh = require('multihashes')
const CID = require('cids')
Expand Down Expand Up @@ -330,22 +329,80 @@ describe('.files (the MFS API part)', function () {
ipfs.files.flush('/', done)
})

it('files.cp', (done) => {
ipfs.files.cp([
'/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
'/test-folder/test-file'
], (err) => {
expect(err).to.not.exist()
done()
})
it('files.cp', () => {
const folder = `/test-folder-${Math.random()}`

return ipfs.files.mkdir(folder)
.then(() => ipfs.files.cp([
'/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
`${folder}/test-file-${Math.random()}`
]))
})

it('files.ls', (done) => {
ipfs.files.ls('/test-folder', (err, res) => {
expect(err).to.not.exist()
expect(res.length).to.equal(1)
done()
})
it('files.cp with non-array arguments', () => {
const folder = `/test-folder-${Math.random()}`

return ipfs.files.mkdir(folder)
.then(() => ipfs.files.cp(
'/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
`${folder}/test-file-${Math.random()}`
))
})

it('files.mv', () => {
const folder = `/test-folder-${Math.random()}`
const source = `${folder}/test-file-${Math.random()}`
const dest = `${folder}/test-file-${Math.random()}`

return ipfs.files.mkdir(folder)
.then(() => ipfs.files.cp(
'/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
source
))
.then(() => ipfs.files.mv([
source,
dest
]))
})

it('files.mv with non-array arguments', () => {
const folder = `/test-folder-${Math.random()}`
const source = `${folder}/test-file-${Math.random()}`
const dest = `${folder}/test-file-${Math.random()}`

return ipfs.files.mkdir(folder)
.then(() => ipfs.files.cp(
'/ipfs/Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
source
))
.then(() => ipfs.files.mv(
source,
dest
))
})

it('files.ls', () => {
const folder = `/test-folder-${Math.random()}`
const file = `${folder}/test-file-${Math.random()}`

return ipfs.files.mkdir(folder)
.then(() => ipfs.files.write(file, Buffer.from('Hello, world'), {
create: true
}))
.then(() => ipfs.files.ls(folder))
.then(files => {
expect(files.length).to.equal(1)
})
})

it('files.ls mfs root by default', () => {
const folder = `test-folder-${Math.random()}`

return ipfs.files.mkdir(`/${folder}`)
.then(() => ipfs.files.ls())
.then(files => {
expect(files.find(file => file.name === folder)).to.be.ok()
})
})

it('files.write', (done) => {
Expand Down Expand Up @@ -374,22 +431,27 @@ describe('.files (the MFS API part)', function () {
})
})

it('files.stat', (done) => {
ipfs.files.stat('/test-folder/test-file', (err, res) => {
expect(err).to.not.exist()
expect(res).to.deep.equal({
hash: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP',
size: 12,
cumulativeSize: 20,
blocks: 0,
type: 'file',
withLocality: false,
local: undefined,
sizeLocal: undefined
it('files.stat', () => {
const folder = `/test-folder-${Math.random()}`
const file = `${folder}/test-file-${Math.random()}`

return ipfs.files.mkdir(folder)
.then(() => ipfs.files.write(file, testfile, {
create: true
}))
.then(() => ipfs.files.stat(file))
.then((stats) => {
expect(stats).to.deep.equal({
hash: 'QmQhouoDPAnzhVM148yCa9CbUXK65wSEAZBtgrLGHtmdmP',
size: 12,
cumulativeSize: 70,
blocks: 1,
type: 'file',
withLocality: false,
local: undefined,
sizeLocal: undefined
})
})

done()
})
})

it('files.stat file that does not exist()', (done) => {
Expand All @@ -402,16 +464,18 @@ describe('.files (the MFS API part)', function () {
})
})

it('files.read', (done) => {
if (!isNode) {
return done()
}

ipfs.files.read('/test-folder/test-file', (err, buf) => {
expect(err).to.not.exist()
expect(Buffer.from(buf)).to.deep.equal(testfile)
done()
})
it('files.read', () => {
const folder = `/test-folder-${Math.random()}`
const file = `${folder}/test-file-${Math.random()}`

return ipfs.files.mkdir(folder)
.then(() => ipfs.files.write(file, testfile, {
create: true
}))
.then(() => ipfs.files.read(file))
.then((buf) => {
expect(Buffer.from(buf)).to.deep.equal(testfile)
})
})

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

0 comments on commit a54f0e3

Please sign in to comment.