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

refactor: convert log API to async/await #1165

Closed
wants to merge 4 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
16 changes: 6 additions & 10 deletions src/log/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
'use strict'

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

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

return {
tail: require('./tail')(send),
ls: require('./ls')(send),
level: require('./level')(send)
}
}
module.exports = config => ({
tail: require('./tail')(config),
ls: callbackify.variadic(require('./ls')(config)),
level: callbackify.variadic(require('./level')(config))
})
40 changes: 18 additions & 22 deletions src/log/level.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
'use strict'

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

module.exports = (send) => {
return promisify((subsystem, level, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
if (typeof subsystem !== 'string') {
return callback(new Error('Invalid subsystem type'))
}
module.exports = configure(({ ky }) => {
return async (subsystem, level, options) => {
options = options || {}

if (typeof level !== 'string') {
return callback(new Error('Invalid level type'))
}
const searchParams = new URLSearchParams(options.searchParams)
searchParams.set('arg', subsystem)
searchParams.append('arg', level)

send({
path: 'log/level',
args: [subsystem, level],
qs: opts,
files: undefined,
buffer: true
}, callback)
})
}
const res = await ky.post('log/level', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return toCamel(res)
}
})
27 changes: 14 additions & 13 deletions src/log/ls.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
'use strict'

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

module.exports = (send) => {
return promisify((callback) => {
send({
path: 'log/ls'
}, (err, result) => {
if (err) {
return callback(err)
}
module.exports = configure(({ ky }) => {
return async options => {
options = options || {}

callback(null, result.Strings)
})
})
}
const res = await ky.get('log/ls', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams: options.searchParams
}).json()

return res.Strings
}
})
32 changes: 16 additions & 16 deletions src/log/tail.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
'use strict'

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

module.exports = (send) => {
return promisify((callback) => {
return send({
path: 'log/tail'
}, (err, response) => {
if (err) {
return callback(err)
}
const outputStream = ndjson.parse()
pump(response, outputStream)
callback(null, outputStream)
module.exports = configure(({ ky }) => {
return async function * (options) {
options = options || {}

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

yield * ndjson(toIterable(res.body))
}
})
2 changes: 1 addition & 1 deletion src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ function requireCommands (send, config) {
dht: require('../dht')(config),
diag: require('../diag')(config),
files: require('../files')(config),
log: require('../log')(config),
pin: require('../pin')(config)
}

Expand All @@ -140,7 +141,6 @@ function requireCommands (send, config) {
commands: require('../commands'),
id: require('../id'),
key: require('../key'),
log: require('../log'),
mount: require('../mount'),
repo: require('../repo'),
stop: require('../stop'),
Expand Down
23 changes: 7 additions & 16 deletions test/log.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,11 @@ describe('.log', function () {
}
}, 1000)

const res = await ipfs.log.tail()

return new Promise((resolve, reject) => {
res.on('error', (err) => {
reject(err)
})

res.once('data', (obj) => {
clearInterval(i)
expect(obj).to.be.an('object')
res.end()
resolve()
})
})
for await (const message of ipfs.log.tail()) {
clearInterval(i)
expect(message).to.be.an('object')
break
}
})

it('.log.ls', async () => {
Expand All @@ -65,7 +56,7 @@ describe('.log', function () {

expect(res).to.exist()
expect(res).to.be.an('object')
expect(res).to.not.have.property('Error')
expect(res).to.have.property('Message')
expect(res).to.not.have.property('error')
expect(res).to.have.property('message')
})
})