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

refactor: convert repo.* and stats.* APIs to async/await #1170

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions src/repo/gc.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
'use strict'

const promisify = require('promisify-es6')
const streamToValueWithTransformer = require('../utils/stream-to-value-with-transformer')
const CID = require('cids')
const ndjson = require('iterable-ndjson')
const configure = require('../lib/configure')
const toIterable = require('../lib/stream-to-iterable')

const transform = function (res, callback) {
callback(null, res.map(r => ({
err: r.Err ? new Error(r.Err) : null,
cid: (r.Key || {})['/'] ? new CID(r.Key['/']) : null
})))
}
module.exports = configure(({ ky }) => {
return (peerId, options) => (async function * () {
options = options || {}

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

const request = {
path: 'repo/gc',
qs: opts
}
send(request, (err, result) => {
if (err) {
return callback(err)
}
const searchParams = new URLSearchParams(options.searchParams)
if (options.streamErrors) searchParams.set('stream-errors', options.streamErrors)

streamToValueWithTransformer(result, transform, callback)
const res = await ky.get('repo/gc', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
})
})
}

for await (const gcResult of ndjson(toIterable(res.body))) {
yield {
err: gcResult.Error ? new Error(gcResult.Error) : null,
cid: (gcResult.Key || {})['/'] ? new CID(gcResult.Key['/']) : null
}
}
})()
})
17 changes: 7 additions & 10 deletions src/repo/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict'

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

module.exports = (arg) => {
const send = moduleConfig(arg)

return {
gc: require('./gc')(send),
stat: require('./stat')(send),
version: require('./version')(send)
}
}
module.exports = config => ({
gc: callbackify.variadic(collectify(require('./gc')(config))),
stat: callbackify.variadic(require('./stat')(config)),
version: callbackify.variadic(require('./version')(config))
})
44 changes: 22 additions & 22 deletions src/repo/stat.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
'use strict'

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

const transform = function (res, callback) {
callback(null, {
numObjects: new Big(res.NumObjects),
repoSize: new Big(res.RepoSize),
repoPath: res.RepoPath,
version: res.Version,
storageMax: new Big(res.StorageMax)
})
}
module.exports = configure(({ ky }) => {
return async options => {
options = options || {}

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
const searchParams = new URLSearchParams(options.searchParams)
if (options.sizeOnly) searchParams.set('size-only', options.sizeOnly)

const res = await ky.get('repo/stat', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

send.andTransform({
path: 'repo/stat',
qs: opts
}, transform, callback)
})
}
return {
numObjects: new Big(res.NumObjects),
repoSize: new Big(res.RepoSize),
repoPath: res.RepoPath,
version: res.Version,
storageMax: new Big(res.StorageMax)
}
}
})
32 changes: 16 additions & 16 deletions src/repo/version.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict'

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

const transform = function (res, callback) {
callback(null, res.Version)
}
module.exports = configure(({ ky }) => {
return async options => {
options = options || {}

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
const searchParams = new URLSearchParams(options.searchParams)
if (options.sizeOnly) searchParams.set('size-only', options.sizeOnly)

send.andTransform({
path: 'repo/version',
qs: opts
}, transform, callback)
})
}
const res = await ky.get('repo/version', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return res.Version
}
})
32 changes: 0 additions & 32 deletions src/stats/bitswap.js

This file was deleted.

31 changes: 0 additions & 31 deletions src/stats/bw-pull-stream.js

This file was deleted.

31 changes: 0 additions & 31 deletions src/stats/bw-readable-stream.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/stats/bw-util.js

This file was deleted.

51 changes: 28 additions & 23 deletions src/stats/bw.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
'use strict'

const promisify = require('promisify-es6')
const streamToValue = require('../utils/stream-to-value')
const transformChunk = require('./bw-util')
const ndjson = require('iterable-ndjson')
const Big = require('bignumber.js')
const configure = require('../lib/configure')
const toIterable = require('../lib/stream-to-iterable')

const transform = (res, callback) => {
return streamToValue(res, (err, data) => {
if (err) {
return callback(err)
}
module.exports = configure(({ ky }) => {
return async function * (options) {
options = options || {}

callback(null, transformChunk(data[0]))
})
}
const searchParams = new URLSearchParams(options.searchParams)
if (options.interval) searchParams.set('interval', options.interval)
if (options.peer) searchParams.set('peer', options.peer)
if (options.poll != null) searchParams.set('poll', options.poll)
if (options.proto) searchParams.set('proto', options.proto)

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

send.andTransform({
path: 'stats/bw',
qs: opts
}, transform, callback)
})
}
for await (const stats of ndjson(toIterable(res.body))) {
yield {
totalIn: new Big(stats.TotalIn),
totalOut: new Big(stats.TotalOut),
rateIn: new Big(stats.RateIn),
rateOut: new Big(stats.RateOut)
}
}
}
})
22 changes: 13 additions & 9 deletions src/stats/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
'use strict'

const moduleConfig = require('../utils/module-config')

module.exports = (arg) => {
const send = moduleConfig(arg)
const callbackify = require('callbackify')
const { streamify, pullify } = require('../lib/converters')

module.exports = config => {
const bw = require('./bw')(config)
return {
bitswap: require('./bitswap')(send),
bw: require('./bw')(send),
bwReadableStream: require('./bw-readable-stream')(send),
bwPullStream: require('./bw-pull-stream')(send),
repo: require('./repo')(send)
bitswap: callbackify.variadic(require('../bitswap/stat')(config)),
bw: callbackify.variadic(async options => {
for await (const stats of bw(options)) {
return stats
}
}),
bwReadableStream: streamify.readable(bw),
bwPullStream: pullify.source(bw),
repo: callbackify.variadic(require('../repo/stat')(config))
}
}
28 changes: 0 additions & 28 deletions src/stats/repo.js

This file was deleted.

Loading