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

Commit

Permalink
refactor: convert object.* and files.* API methods to async/await (#1160
Browse files Browse the repository at this point in the history
)

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
Alan Shaw authored Nov 20, 2019
1 parent 8b48d57 commit fc73da7
Show file tree
Hide file tree
Showing 35 changed files with 495 additions and 738 deletions.
14 changes: 3 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,11 @@
"cids": "~0.7.1",
"concat-stream": "github:hugomrdias/concat-stream#feat/smaller",
"debug": "^4.1.0",
"delay": "^4.3.0",
"detect-node": "^2.0.4",
"end-of-stream": "^1.4.1",
"err-code": "^2.0.0",
"explain-error": "^1.0.4",
"flatmap": "0.0.3",
"form-data": "^3.0.0",
"fs-extra": "^8.1.0",
"glob": "^7.1.3",
"ipfs-block": "~0.8.1",
"ipfs-utils": "^0.4.0",
Expand All @@ -71,44 +68,39 @@
"is-pull-stream": "0.0.0",
"is-stream": "^2.0.0",
"iso-stream-http": "~0.1.2",
"iso-url": "~0.4.6",
"it-glob": "0.0.6",
"it-tar": "^1.1.0",
"it-to-stream": "^0.1.1",
"iterable-ndjson": "^1.1.0",
"just-kebab-case": "^1.1.0",
"just-map-keys": "^1.1.0",
"kind-of": "^6.0.2",
"ky": "^0.15.0",
"ky-universal": "^0.3.0",
"lru-cache": "^5.1.1",
"merge-options": "^2.0.0",
"multiaddr": "^6.0.6",
"multibase": "~0.6.0",
"multicodec": "~0.5.1",
"multihashes": "~0.4.14",
"ndjson": "github:hugomrdias/ndjson#feat/readable-stream3",
"once": "^1.4.0",
"parse-duration": "^0.1.1",
"peer-id": "~0.12.3",
"peer-info": "~0.15.1",
"promise-nodeify": "^3.0.1",
"promisify-es6": "^1.0.3",
"pull-defer": "~0.2.3",
"pull-stream": "^3.6.9",
"pull-stream-to-async-iterator": "^1.0.2",
"pull-to-stream": "~0.1.1",
"pump": "^3.0.0",
"qs": "^6.5.2",
"readable-stream": "^3.1.1",
"stream-to-pull-stream": "^1.7.2",
"through2": "^3.0.1"
"stream-to-pull-stream": "^1.7.2"
},
"devDependencies": {
"aegir": "^20.4.1",
"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": "^0.120.0",
"ipfsd-ctl": "^0.47.1",
"nock": "^11.4.0",
"stream-equal": "^1.1.1"
Expand Down
37 changes: 21 additions & 16 deletions src/files/cp.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
'use strict'

const promisify = require('promisify-es6')
const findSources = require('../utils/find-sources')
const CID = require('cids')
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', CID.isCID(src) ? `/ipfs/${src}` : 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()
}
})
31 changes: 19 additions & 12 deletions src/files/flush.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
'use strict'

const promisify = require('promisify-es6')
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)

await ky.post('files/flush', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).text()
}
})
34 changes: 18 additions & 16 deletions src/files/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
'use strict'

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

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

return {
cp: require('./cp')(send),
mkdir: require('./mkdir')(send),
flush: require('./flush')(send),
stat: require('./stat')(send),
rm: require('./rm')(send),
ls: require('./ls')(send),
lsReadableStream: require('./ls-readable-stream')(send),
lsPullStream: require('./ls-pull-stream')(send),
read: require('./read')(send),
readReadableStream: require('./read-readable-stream')(send),
readPullStream: require('./read-pull-stream')(send),
write: require('./write')(send),
mv: require('./mv')(send)
cp: callbackify.variadic(require('./cp')(config)),
mkdir: callbackify.variadic(require('./mkdir')(config)),
flush: callbackify.variadic(require('./flush')(config)),
stat: callbackify.variadic(require('./stat')(config)),
rm: callbackify.variadic(require('./rm')(config)),
ls: callbackify.variadic(collectify(ls)),
lsReadableStream: streamify.readable(ls),
lsPullStream: pullify.source(ls),
read: callbackify.variadic(concatify(read)),
readReadableStream: streamify.readable(read),
readPullStream: pullify.source(read),
write: callbackify.variadic(require('./write')(config)),
mv: callbackify.variadic(require('./mv')(config))
}
}
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.

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

const promisify = require('promisify-es6')
const CID = require('cids')
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 || []

callback(null, entries.map((entry) => {
return {
name: entry.Name,
type: entry.Type,
size: entry.Size,
hash: entry.Hash
module.exports = configure(({ ky }) => {
return async function * ls (path, options) {
if (typeof path !== 'string') {
options = path
path = '/'
}
}))
}

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
options = options || {}

if (typeof (args) === 'function') {
callback = args
opts = {}
args = null
}
const searchParams = new URLSearchParams(options.searchParams)
searchParams.set('arg', CID.isCID(path) ? `/ipfs/${path}` : path)
searchParams.set('stream', true)
if (options.cidBase) searchParams.set('cid-base', options.cidBase)
if (options.long != null) searchParams.set('long', options.long)

return send.andTransform({
path: 'files/ls',
args: args,
qs: opts
}, transform, callback)
})
}
const res = await ky.post('files/ls', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
})

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)
}
}
})
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()
}
})
Loading

0 comments on commit fc73da7

Please sign in to comment.