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

Commit cdf9390

Browse files
author
Alan Shaw
committed
refactor: convert name API to async/await
Depends on: * [ ] ipfs-inactive/interface-js-ipfs-core#561 License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 12b7a2e commit cdf9390

File tree

9 files changed

+102
-107
lines changed

9 files changed

+102
-107
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
"browser-process-platform": "~0.1.1",
109109
"cross-env": "^6.0.0",
110110
"go-ipfs-dep": "^0.4.22",
111-
"interface-ipfs-core": "^0.119.0",
111+
"interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#fix/allow-offline-camel-case",
112112
"ipfsd-ctl": "^0.47.1",
113113
"nock": "^11.4.0",
114114
"stream-equal": "^1.1.1"

src/name/index.js

+6-10
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-
publish: require('./publish')(send),
10-
resolve: require('./resolve')(send),
11-
pubsub: require('./pubsub')(send)
12-
}
13-
}
5+
module.exports = config => ({
6+
publish: callbackify.variadic(require('./publish')(config)),
7+
resolve: callbackify.variadic(require('./resolve')(config)),
8+
pubsub: require('./pubsub')(config)
9+
})

src/name/publish.js

+23-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
'use strict'
22

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

5-
const transform = function (res, callback) {
6-
callback(null, {
7-
name: res.Name,
8-
value: res.Value
9-
})
10-
}
6+
module.exports = configure(({ ky }) => {
7+
return async (path, options) => {
8+
options = options || {}
119

12-
module.exports = (send) => {
13-
return promisify((args, opts, callback) => {
14-
if (typeof (opts) === 'function') {
15-
callback = opts
16-
opts = {}
17-
}
10+
const searchParams = new URLSearchParams(options.searchParams)
11+
searchParams.set('arg', path)
12+
if (options.allowOffline != null) searchParams.set('allow-offline', options.allowOffline)
13+
if (options.key) searchParams.set('key', options.key)
14+
if (options.lifetime) searchParams.set('lifetime', options.lifetime)
15+
if (options.quieter != null) searchParams.set('quieter', options.quieter)
16+
if (options.resolve != null) searchParams.set('resolve', options.resolve)
17+
if (options.ttl) searchParams.set('ttl', options.ttl)
1818

19-
send.andTransform({
20-
path: 'name/publish',
21-
args: args,
22-
qs: opts
23-
}, transform, callback)
24-
})
25-
}
19+
const res = await ky.post('name/publish', {
20+
timeout: options.timeout,
21+
signal: options.signal,
22+
headers: options.headers,
23+
searchParams
24+
}).json()
25+
26+
return toCamel(res)
27+
}
28+
})

src/name/pubsub/cancel.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
'use strict'
22

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

5-
const transform = function (res, callback) {
6-
callback(null, {
7-
canceled: res.Canceled === undefined || res.Canceled === true
8-
})
9-
}
6+
module.exports = configure(({ ky }) => {
7+
return async (name, options) => {
8+
options = options || {}
109

11-
module.exports = (send) => {
12-
return promisify((args, opts, callback) => {
13-
if (typeof (opts) === 'function') {
14-
callback = opts
15-
opts = {}
16-
}
10+
const searchParams = new URLSearchParams(options.searchParams)
11+
searchParams.set('arg', name)
1712

18-
send.andTransform({
19-
path: 'name/pubsub/cancel',
20-
args: args,
21-
qs: opts
22-
}, transform, callback)
23-
})
24-
}
13+
const res = await ky.post('name/pubsub/cancel', {
14+
timeout: options.timeout,
15+
signal: options.signal,
16+
headers: options.headers,
17+
searchParams
18+
}).json()
19+
20+
return toCamel(res)
21+
}
22+
})

src/name/pubsub/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict'
22

3-
module.exports = (send) => ({
4-
cancel: require('./cancel')(send),
5-
state: require('./state')(send),
6-
subs: require('./subs')(send)
3+
const callbackify = require('callbackify')
4+
5+
module.exports = config => ({
6+
cancel: callbackify.variadic(require('./cancel')(config)),
7+
state: callbackify.variadic(require('./state')(config)),
8+
subs: callbackify.variadic(require('./subs')(config))
79
})

src/name/pubsub/state.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
'use strict'
22

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

5-
const transform = function (res, callback) {
6-
callback(null, {
7-
enabled: res.Enabled
8-
})
9-
}
6+
module.exports = configure(({ ky }) => {
7+
return async options => {
8+
options = options || {}
109

11-
module.exports = (send) => {
12-
return promisify((opts, callback) => {
13-
if (typeof (opts) === 'function') {
14-
callback = opts
15-
opts = {}
16-
}
10+
const res = await ky.post('name/pubsub/state', {
11+
timeout: options.timeout,
12+
signal: options.signal,
13+
headers: options.headers,
14+
searchParams: options.searchParams
15+
}).json()
1716

18-
send.andTransform({
19-
path: 'name/pubsub/state',
20-
qs: opts
21-
}, transform, callback)
22-
})
23-
}
17+
return toCamel(res)
18+
}
19+
})

src/name/pubsub/subs.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
'use strict'
22

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

5-
const transform = function (res, callback) {
6-
callback(null, res.Strings || [])
7-
}
5+
module.exports = configure(({ ky }) => {
6+
return async (name, options) => {
7+
options = options || {}
88

9-
module.exports = (send) => {
10-
return promisify((opts, callback) => {
11-
if (typeof (opts) === 'function') {
12-
callback = opts
13-
opts = {}
14-
}
9+
const res = await ky.get('name/pubsub/subs', {
10+
timeout: options.timeout,
11+
signal: options.signal,
12+
headers: options.headers,
13+
searchParams: options.searchParams
14+
}).json()
1515

16-
send.andTransform({
17-
path: 'name/pubsub/subs',
18-
qs: opts
19-
}, transform, callback)
20-
})
21-
}
16+
return res.Strings
17+
}
18+
})

src/name/resolve.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
'use strict'
22

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

5-
const transform = function (res, callback) {
6-
callback(null, res.Path)
7-
}
5+
module.exports = configure(({ ky }) => {
6+
return async (path, options) => {
7+
options = options || {}
88

9-
module.exports = (send) => {
10-
return promisify((args, opts, callback) => {
11-
if (typeof (opts) === 'function') {
12-
callback = opts
13-
opts = {}
14-
}
9+
const searchParams = new URLSearchParams(options.searchParams)
10+
searchParams.set('arg', path)
11+
if (options.dhtRecordCount != null) searchParams.set('dht-record-count', options.dhtRecordCount)
12+
if (options.dhtTimeout != null) searchParams.set('dht-timeout', options.dhtTimeout)
13+
if (options.noCache != null) searchParams.set('nocache', options.noCache)
14+
if (options.recursive != null) searchParams.set('recursive', options.recursive)
1515

16-
send.andTransform({
17-
path: 'name/resolve',
18-
args: args,
19-
qs: opts
20-
}, transform, callback)
21-
})
22-
}
16+
const res = await ky.post('name/resolve', {
17+
timeout: options.timeout,
18+
signal: options.signal,
19+
headers: options.headers,
20+
searchParams
21+
}).json()
22+
23+
return res.Path
24+
}
25+
})

src/utils/load-commands.js

+2-2
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+
name: require('../name')(config)
117118
}
118119

119120
Object.assign(cmds.refs, {
@@ -132,7 +133,6 @@ function requireCommands (send, config) {
132133
pin: require('../pin'),
133134

134135
// Network
135-
name: require('../name'),
136136
ping: require('../ping'),
137137
pingReadableStream: require('../ping-readable-stream'),
138138
pingPullStream: require('../ping-pull-stream'),

0 commit comments

Comments
 (0)