From a0ee87c412d1b50ab66b927ea37995e6376bd87d Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 19 Nov 2019 21:28:24 +0000 Subject: [PATCH] refactor: convert pin API to async/await License: MIT Signed-off-by: Alan Shaw --- src/pin/add.js | 38 ++++++++++++++-------------- src/pin/index.js | 16 +++++------- src/pin/ls.js | 52 +++++++++++++++++--------------------- src/pin/rm.js | 38 ++++++++++++++-------------- src/utils/load-commands.js | 4 +-- 5 files changed, 69 insertions(+), 79 deletions(-) diff --git a/src/pin/add.js b/src/pin/add.js index 7ef730eec..74eb47a2d 100644 --- a/src/pin/add.js +++ b/src/pin/add.js @@ -1,22 +1,22 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((hash, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = null - } - send({ - path: 'pin/add', - args: hash, - qs: opts - }, (err, res) => { - if (err) { - return callback(err) - } - callback(null, res.Pins.map((hash) => ({ hash: hash }))) - }) - }) -} +module.exports = configure(({ ky }) => { + return async (path, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${path}`) + if (options.recursive != null) searchParams.set('recursive', options.recursive) + + const res = await ky.post('pin/add', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return (res.Pins || []).map(hash => ({ hash })) + } +}) diff --git a/src/pin/index.js b/src/pin/index.js index 859052e2c..c62d3a46a 100644 --- a/src/pin/index.js +++ b/src/pin/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 { - add: require('./add')(send), - rm: require('./rm')(send), - ls: require('./ls')(send) - } -} +module.exports = config => ({ + add: callbackify.variadic(require('./add')(config)), + rm: callbackify.variadic(require('./rm')(config)), + ls: callbackify.variadic(require('./ls')(config)) +}) diff --git a/src/pin/ls.js b/src/pin/ls.js index c8ad26a4b..bf1746d63 100644 --- a/src/pin/ls.js +++ b/src/pin/ls.js @@ -1,33 +1,27 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((hash, opts, callback) => { - if (typeof hash === 'function') { - callback = hash - opts = null - hash = null +module.exports = configure(({ ky }) => { + return async (path, options) => { + if (path && path.type) { + options = path + path = null } - if (typeof opts === 'function') { - callback = opts - opts = null - } - if (hash && hash.type) { - opts = hash - hash = null - } - send({ - path: 'pin/ls', - args: hash, - qs: opts - }, (err, res) => { - if (err) { - return callback(err) - } - callback(null, Object.keys(res.Keys).map(hash => ( - { hash, type: res.Keys[hash].Type } - ))) - }) - }) -} + + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + if (path) searchParams.set('arg', `${path}`) + if (options.type) searchParams.set('type', options.type) + + const { Keys } = await ky.get('pin/ls', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return Object.keys(Keys).map(hash => ({ hash, type: Keys[hash].Type })) + } +}) diff --git a/src/pin/rm.js b/src/pin/rm.js index 05aada109..9f75307ff 100644 --- a/src/pin/rm.js +++ b/src/pin/rm.js @@ -1,22 +1,22 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((hash, opts, callback) => { - if (typeof opts === 'function') { - callback = opts - opts = null - } - send({ - path: 'pin/rm', - args: hash, - qs: opts - }, (err, res) => { - if (err) { - return callback(err) - } - callback(null, res.Pins.map((hash) => ({ hash: hash }))) - }) - }) -} +module.exports = configure(({ ky }) => { + return async (path, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) + searchParams.set('arg', `${path}`) + if (options.recursive != null) searchParams.set('recursive', options.recursive) + + const res = await ky.post('pin/rm', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return (res.Pins || []).map(hash => ({ hash })) + } +}) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index 3e51acdb2..933241a0b 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), + pin: require('../pin')(config) } Object.assign(cmds.refs, { @@ -129,7 +130,6 @@ function requireCommands (send, config) { // Graph object: require('../object'), - pin: require('../pin'), // Network name: require('../name'),