From acf2233bbb0aa5f244ed768430d4a724f2d6b047 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 19 Nov 2019 13:38:14 +0000 Subject: [PATCH 1/2] refactor: convert log API to async/await BREAKING CHANGE: The `log.tail` method now returns an async iterator that yields log messages. Use it like: ```js for await (const message of ipfs.log.tail()) { console.log(message) } ``` Secondly, the response to a call to `log.level` now returns an object that has camel cased keys. i.e. `Message` and `Error` properties have changed to `message` and `error`. License: MIT Signed-off-by: Alan Shaw --- src/log/index.js | 16 ++++++---------- src/log/level.js | 40 ++++++++++++++++++---------------------- src/log/ls.js | 27 ++++++++++++++------------- src/log/tail.js | 32 ++++++++++++++++---------------- test/log.spec.js | 23 +++++++---------------- 5 files changed, 61 insertions(+), 77 deletions(-) diff --git a/src/log/index.js b/src/log/index.js index 94597bdc6..f7d94f910 100644 --- a/src/log/index.js +++ b/src/log/index.js @@ -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)) +}) diff --git a/src/log/level.js b/src/log/level.js index 4304ff90f..dcc20c7bd 100644 --- a/src/log/level.js +++ b/src/log/level.js @@ -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) + } +}) diff --git a/src/log/ls.js b/src/log/ls.js index ab243605b..ce80b8faa 100644 --- a/src/log/ls.js +++ b/src/log/ls.js @@ -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 + } +}) diff --git a/src/log/tail.js b/src/log/tail.js index bad4e4d34..9bf8a2369 100644 --- a/src/log/tail.js +++ b/src/log/tail.js @@ -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)) + } +}) diff --git a/test/log.spec.js b/test/log.spec.js index a9dbc0274..7f8e2c608 100644 --- a/test/log.spec.js +++ b/test/log.spec.js @@ -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 () => { @@ -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') }) }) From e0a4ffd3a916f6decad0fa64bcecd3a8f08fbf42 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 19 Nov 2019 13:52:59 +0000 Subject: [PATCH 2/2] fix: tests --- src/utils/load-commands.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index 3e51acdb2..e4760ee0a 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -113,7 +113,8 @@ function requireCommands (send, config) { config: require('../config')(config), dag: require('../dag')(config), dht: require('../dht')(config), - diag: require('../diag')(config) + diag: require('../diag')(config), + log: require('../log')(config) } Object.assign(cmds.refs, { @@ -144,7 +145,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'),