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

Commit

Permalink
refactor: convert files cp, flush, ls, mkdir and mv to async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Nov 18, 2019
1 parent 3a7de05 commit b7b4a91
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 170 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"browser-process-platform": "~0.1.1",
"cross-env": "^6.0.0",
"go-ipfs-dep": "^0.4.22",
"interface-ipfs-core": "^0.119.0",
"interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#fix/files-tests-tweaks",
"ipfsd-ctl": "^0.47.1",
"nock": "^11.4.0",
"stream-equal": "^1.1.1"
Expand Down
36 changes: 20 additions & 16 deletions src/files/cp.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
'use strict'

const promisify = require('promisify-es6')
const findSources = require('../utils/find-sources')
const configure = require('../lib/configure')
const { findSources } = require('./utils')

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

send({
path: 'files/cp',
args: sources,
qs: opts
}, (error) => callback(error))
})
}
const searchParams = new URLSearchParams(options.searchParams)
sources.forEach(src => searchParams.append('arg', src))
if (options.format) searchParams.set('format', options.format)
if (options.flush != null) searchParams.set('flush', options.flush)
if (options.hashAlg) searchParams.set('hash', options.hashAlg)
if (options.parents != null) searchParams.set('parents', options.parents)

return ky.post('files/cp', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).text()
}
})
34 changes: 22 additions & 12 deletions src/files/flush.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
'use strict'

const promisify = require('promisify-es6')
const CID = require('cids')
const configure = require('../lib/configure')

module.exports = (send) => {
return promisify((args, callback) => {
if (typeof args === 'function') {
callback = args
args = '/'
module.exports = configure(({ ky }) => {
return async (path, options) => {
if (typeof path !== 'string') {
options = path
path = '/'
}

return send({
path: 'files/flush',
args: args
}, (error) => callback(error))
})
}
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
searchParams.set('arg', path)

const { Cid } = await ky.post('files/flush', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return new CID(Cid)
}
})
14 changes: 8 additions & 6 deletions src/files/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
'use strict'

const callbackify = require('callbackify')
const { collectify, streamify, pullify } = require('../lib/converters')
const moduleConfig = require('../utils/module-config')

module.exports = (arg, config) => {
const send = moduleConfig(arg)
const ls = require('./ls')(config)

return {
cp: require('./cp')(send),
mkdir: require('./mkdir')(send),
flush: require('./flush')(send),
cp: callbackify.variadic(require('./cp')(config)),
mkdir: callbackify.variadic(require('./mkdir')(config)),
flush: callbackify.variadic(require('./flush')(config)),
stat: require('./stat')(send),
rm: require('./rm')(send),
ls: require('./ls')(send),
lsReadableStream: require('./ls-readable-stream')(send),
lsPullStream: require('./ls-pull-stream')(send),
ls: callbackify.variadic(collectify(ls)),
lsReadableStream: streamify.readable(ls),
lsPullStream: pullify.source(ls),
read: require('./read')(send),
readReadableStream: require('./read-readable-stream')(send),
readPullStream: require('./read-pull-stream')(send),
Expand Down
12 changes: 0 additions & 12 deletions src/files/ls-pull-stream.js

This file was deleted.

62 changes: 0 additions & 62 deletions src/files/ls-readable-stream.js

This file was deleted.

59 changes: 29 additions & 30 deletions src/files/ls.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
'use strict'

const promisify = require('promisify-es6')
const ndjson = require('iterable-ndjson')
const toIterable = require('../lib/stream-to-iterable')
const configure = require('../lib/configure')
const toCamel = require('../lib/object-to-camel')

const transform = function (res, callback) {
const entries = res.Entries || []
module.exports = configure(({ ky }) => {
return async function * ls (path, options) {
options = options || {}

callback(null, entries.map((entry) => {
return {
name: entry.Name,
type: entry.Type,
size: entry.Size,
hash: entry.Hash
}
}))
}
const searchParams = new URLSearchParams(options.searchParams)
searchParams.set('arg', `${path}`)
searchParams.set('stream', true)
if (options.cidBase) searchParams.set('cid-base', options.cidBase)
if (options.long != null) searchParams.set('long', options.long)

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
const res = await ky.post('files/ls', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
})

if (typeof (args) === 'function') {
callback = args
opts = {}
args = null
for await (const result of ndjson(toIterable(res.body))) {
// go-ipfs does not yet support the "stream" option
if ('Entries' in result) {
for (const entry of result.Entries || []) {
yield toCamel(entry)
}
return
}
yield toCamel(result)
}

return send.andTransform({
path: 'files/ls',
args: args,
qs: opts
}, transform, callback)
})
}
}
})
36 changes: 21 additions & 15 deletions src/files/mkdir.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@

'use strict'

const promisify = require('promisify-es6')
const configure = require('../lib/configure')

module.exports = configure(({ ky }) => {
return (path, options) => {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
searchParams.append('arg', path)
if (options.cidVersion != null) searchParams.set('cid-version', options.cidVersion)
if (options.format) searchParams.set('format', options.format)
if (options.flush != null) searchParams.set('flush', options.flush)
if (options.hashAlg) searchParams.set('hash', options.hashAlg)
if (options.parents != null) searchParams.set('parents', options.parents)

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
send({
path: 'files/mkdir',
args: args,
qs: opts
}, (error) => callback(error))
})
}
return ky.post('files/mkdir', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).text()
}
})
36 changes: 20 additions & 16 deletions src/files/mv.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
'use strict'

const promisify = require('promisify-es6')
const findSources = require('../utils/find-sources')
const configure = require('../lib/configure')
const { findSources } = require('./utils')

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

send({
path: 'files/mv',
args: sources,
qs: opts
}, (error) => callback(error))
})
}
const searchParams = new URLSearchParams(options.searchParams)
sources.forEach(src => searchParams.append('arg', src))
if (options.format) searchParams.set('format', options.format)
if (options.flush != null) searchParams.set('flush', options.flush)
if (options.hashAlg) searchParams.set('hash', options.hashAlg)
if (options.parents != null) searchParams.set('parents', options.parents)

return ky.post('files/mv', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).text()
}
})
23 changes: 23 additions & 0 deletions src/files/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

exports.findSources = (args) => {
let options = {}
let sources = []

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

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

return {
sources,
options
}
}

0 comments on commit b7b4a91

Please sign in to comment.