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

Commit 8b48d57

Browse files
author
Alan Shaw
authored
refactor: convert pin API to async/await (#1168)
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 12b7a2e commit 8b48d57

File tree

5 files changed

+69
-79
lines changed

5 files changed

+69
-79
lines changed

src/pin/add.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
'use strict'
22

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

5-
module.exports = (send) => {
6-
return promisify((hash, opts, callback) => {
7-
if (typeof opts === 'function') {
8-
callback = opts
9-
opts = null
10-
}
11-
send({
12-
path: 'pin/add',
13-
args: hash,
14-
qs: opts
15-
}, (err, res) => {
16-
if (err) {
17-
return callback(err)
18-
}
19-
callback(null, res.Pins.map((hash) => ({ hash: hash })))
20-
})
21-
})
22-
}
5+
module.exports = configure(({ ky }) => {
6+
return async (path, options) => {
7+
options = options || {}
8+
9+
const searchParams = new URLSearchParams(options.searchParams)
10+
searchParams.set('arg', `${path}`)
11+
if (options.recursive != null) searchParams.set('recursive', options.recursive)
12+
13+
const res = await ky.post('pin/add', {
14+
timeout: options.timeout,
15+
signal: options.signal,
16+
headers: options.headers,
17+
searchParams
18+
}).json()
19+
20+
return (res.Pins || []).map(hash => ({ hash }))
21+
}
22+
})

src/pin/index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
'use strict'
22

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

5-
module.exports = (arg) => {
6-
const send = moduleConfig(arg)
7-
8-
return {
9-
add: require('./add')(send),
10-
rm: require('./rm')(send),
11-
ls: require('./ls')(send)
12-
}
13-
}
5+
module.exports = config => ({
6+
add: callbackify.variadic(require('./add')(config)),
7+
rm: callbackify.variadic(require('./rm')(config)),
8+
ls: callbackify.variadic(require('./ls')(config))
9+
})

src/pin/ls.js

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
'use strict'
22

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

5-
module.exports = (send) => {
6-
return promisify((hash, opts, callback) => {
7-
if (typeof hash === 'function') {
8-
callback = hash
9-
opts = null
10-
hash = null
5+
module.exports = configure(({ ky }) => {
6+
return async (path, options) => {
7+
if (path && path.type) {
8+
options = path
9+
path = null
1110
}
12-
if (typeof opts === 'function') {
13-
callback = opts
14-
opts = null
15-
}
16-
if (hash && hash.type) {
17-
opts = hash
18-
hash = null
19-
}
20-
send({
21-
path: 'pin/ls',
22-
args: hash,
23-
qs: opts
24-
}, (err, res) => {
25-
if (err) {
26-
return callback(err)
27-
}
28-
callback(null, Object.keys(res.Keys).map(hash => (
29-
{ hash, type: res.Keys[hash].Type }
30-
)))
31-
})
32-
})
33-
}
11+
12+
options = options || {}
13+
14+
const searchParams = new URLSearchParams(options.searchParams)
15+
if (path) searchParams.set('arg', `${path}`)
16+
if (options.type) searchParams.set('type', options.type)
17+
18+
const { Keys } = await ky.get('pin/ls', {
19+
timeout: options.timeout,
20+
signal: options.signal,
21+
headers: options.headers,
22+
searchParams
23+
}).json()
24+
25+
return Object.keys(Keys).map(hash => ({ hash, type: Keys[hash].Type }))
26+
}
27+
})

src/pin/rm.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
'use strict'
22

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

5-
module.exports = (send) => {
6-
return promisify((hash, opts, callback) => {
7-
if (typeof opts === 'function') {
8-
callback = opts
9-
opts = null
10-
}
11-
send({
12-
path: 'pin/rm',
13-
args: hash,
14-
qs: opts
15-
}, (err, res) => {
16-
if (err) {
17-
return callback(err)
18-
}
19-
callback(null, res.Pins.map((hash) => ({ hash: hash })))
20-
})
21-
})
22-
}
5+
module.exports = configure(({ ky }) => {
6+
return async (path, options) => {
7+
options = options || {}
8+
9+
const searchParams = new URLSearchParams(options.searchParams)
10+
searchParams.set('arg', `${path}`)
11+
if (options.recursive != null) searchParams.set('recursive', options.recursive)
12+
13+
const res = await ky.post('pin/rm', {
14+
timeout: options.timeout,
15+
signal: options.signal,
16+
headers: options.headers,
17+
searchParams
18+
}).json()
19+
20+
return (res.Pins || []).map(hash => ({ hash }))
21+
}
22+
})

src/utils/load-commands.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ function requireCommands (send, config) {
113113
config: require('../config')(config),
114114
dag: require('../dag')(config),
115115
dht: require('../dht')(config),
116-
diag: require('../diag')(config)
116+
diag: require('../diag')(config),
117+
pin: require('../pin')(config)
117118
}
118119

119120
Object.assign(cmds.refs, {
@@ -129,7 +130,6 @@ function requireCommands (send, config) {
129130

130131
// Graph
131132
object: require('../object'),
132-
pin: require('../pin'),
133133

134134
// Network
135135
name: require('../name'),

0 commit comments

Comments
 (0)