From f281b20e599cb0b60bcb0c68ddd79e715baac146 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 19 Nov 2019 13:09:07 +0000 Subject: [PATCH 1/2] refactor: convert key API to async/await License: MIT Signed-off-by: Alan Shaw --- src/key/export.js | 35 ++++++++++++++++++++++------------- src/key/gen.js | 39 +++++++++++++++++++-------------------- src/key/import.js | 44 +++++++++++++++++++++++++------------------- src/key/index.js | 22 +++++++++------------- src/key/list.js | 35 ++++++++++++++--------------------- src/key/rename.js | 37 ++++++++++++++++++++----------------- src/key/rm.js | 33 ++++++++++++++++++--------------- 7 files changed, 127 insertions(+), 118 deletions(-) diff --git a/src/key/export.js b/src/key/export.js index f19db4022..810555e9a 100644 --- a/src/key/export.js +++ b/src/key/export.js @@ -1,16 +1,25 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((name, password, callback) => { - send({ - path: 'key/export', - args: name, - qs: { password: password } - }, (err, pem) => { - if (err) return callback(err) - callback(null, pem.toString()) - }) - }) -} +module.exports = configure(({ ky }) => { + return (name, password, options) => { + if (typeof password !== 'string') { + options = password + password = null + } + + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', name) + if (password) searchParams.set('password', password) + + return ky.get('key/export', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).text() + } +}) diff --git a/src/key/gen.js b/src/key/gen.js index 38612d9af..5fd6acb86 100644 --- a/src/key/gen.js +++ b/src/key/gen.js @@ -1,25 +1,24 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - callback(null, { - id: res.Id, - name: res.Name - }) -} +module.exports = configure(({ ky }) => { + return async (name, options) => { + options = options || {} -module.exports = (send) => { - return promisify((args, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', name) + if (options.type) searchParams.set('type', options.type) + if (options.size != null) searchParams.set('size', options.size) - send.andTransform({ - path: 'key/gen', - args: args, - qs: opts - }, transform, callback) - }) -} + const res = await ky.post('key/gen', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return toCamel(res) + } +}) diff --git a/src/key/import.js b/src/key/import.js index 9cea80e27..7cc8f5ddc 100644 --- a/src/key/import.js +++ b/src/key/import.js @@ -1,23 +1,29 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - callback(null, { - id: res.Id, - name: res.Name - }) -} +module.exports = configure(({ ky }) => { + return async (name, pem, password, options) => { + if (typeof password !== 'string') { + options = password + password = null + } -module.exports = (send) => { - return promisify((name, pem, password, callback) => { - send.andTransform({ - path: 'key/import', - args: name, - qs: { - pem: pem, - password: password - } - }, transform, callback) - }) -} + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', name) + searchParams.set('pem', pem) + if (password) searchParams.set('password', password) + + const res = await ky.post('key/import', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return toCamel(res) + } +}) diff --git a/src/key/index.js b/src/key/index.js index 717d719c3..7293236f1 100644 --- a/src/key/index.js +++ b/src/key/index.js @@ -1,16 +1,12 @@ 'use strict' -const moduleConfig = require('../utils/module-config') +const callbackify = require('callbackify') -module.exports = (arg) => { - const send = moduleConfig(arg) - - return { - gen: require('./gen')(send), - list: require('./list')(send), - rename: require('./rename')(send), - rm: require('./rm')(send), - export: require('./export')(send), - import: require('./import')(send) - } -} +module.exports = config => ({ + gen: callbackify.variadic(require('./gen')(config)), + list: callbackify.variadic(require('./list')(config)), + rename: callbackify.variadic(require('./rename')(config)), + rm: callbackify.variadic(require('./rm')(config)), + export: callbackify.variadic(require('./export')(config)), + import: callbackify.variadic(require('./import')(config)) +}) diff --git a/src/key/list.js b/src/key/list.js index bcd9bc8e2..d6f0dd578 100644 --- a/src/key/list.js +++ b/src/key/list.js @@ -1,26 +1,19 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - callback(null, res.Keys.map(key => { - return { - id: key.Id, - name: key.Name - } - })) -} +module.exports = configure(({ ky }) => { + return async options => { + options = options || {} -module.exports = (send) => { - return promisify((opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } + const res = await ky.get('key/list', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).json() - send.andTransform({ - path: 'key/list', - qs: opts - }, transform, callback) - }) -} + return (res.Keys || []).map(k => toCamel(k)) + } +}) diff --git a/src/key/rename.js b/src/key/rename.js index 68b45f010..18e069768 100644 --- a/src/key/rename.js +++ b/src/key/rename.js @@ -1,21 +1,24 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - callback(null, { - id: res.Id, - was: res.Was, - now: res.Now, - overwrite: res.Overwrite - }) -} +module.exports = configure(({ ky }) => { + return async (oldName, newName, options) => { + options = options || {} -module.exports = (send) => { - return promisify((oldName, newName, callback) => { - send.andTransform({ - path: 'key/rename', - args: [oldName, newName] - }, transform, callback) - }) -} + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', oldName) + searchParams.append('arg', newName) + if (options.force != null) searchParams.set('force', options.force) + + const res = await ky.post('key/rename', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return toCamel(res) + } +}) diff --git a/src/key/rm.js b/src/key/rm.js index 7e4e98500..edab61060 100644 --- a/src/key/rm.js +++ b/src/key/rm.js @@ -1,19 +1,22 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') +const toCamel = require('../lib/object-to-camel') -const transform = function (res, callback) { - callback(null, { - id: res.Keys[0].Id, - name: res.Keys[0].Name - }) -} +module.exports = configure(({ ky }) => { + return async (name, options) => { + options = options || {} -module.exports = (send) => { - return promisify((args, callback) => { - send.andTransform({ - path: 'key/rm', - args: args - }, transform, callback) - }) -} + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', name) + + const res = await ky.post('key/rm', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return toCamel(res.Keys[0]) + } +}) From 0ad38970bf889a2eec7039b2e74b746c1b171ff8 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 19 Nov 2019 13:51:04 +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..7d0c97cd8 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), + key: require('../key')(config) } Object.assign(cmds.refs, { @@ -143,7 +144,6 @@ function requireCommands (send, config) { // Miscellaneous commands: require('../commands'), id: require('../id'), - key: require('../key'), log: require('../log'), mount: require('../mount'), repo: require('../repo'),