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

Commit

Permalink
refactor: convert name API to async/await
Browse files Browse the repository at this point in the history
Depends on:

* [ ] ipfs-inactive/interface-js-ipfs-core#561

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
Alan Shaw committed Nov 19, 2019
1 parent 12b7a2e commit cdf9390
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 107 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"browser-process-platform": "~0.1.1",
"cross-env": "^6.0.0",
"go-ipfs-dep": "^0.4.22",
"interface-ipfs-core": "^0.119.0",
"interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#fix/allow-offline-camel-case",
"ipfsd-ctl": "^0.47.1",
"nock": "^11.4.0",
"stream-equal": "^1.1.1"
Expand Down
16 changes: 6 additions & 10 deletions src/name/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
'use strict'

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

module.exports = (arg) => {
const send = moduleConfig(arg)

return {
publish: require('./publish')(send),
resolve: require('./resolve')(send),
pubsub: require('./pubsub')(send)
}
}
module.exports = config => ({
publish: callbackify.variadic(require('./publish')(config)),
resolve: callbackify.variadic(require('./resolve')(config)),
pubsub: require('./pubsub')(config)
})
43 changes: 23 additions & 20 deletions src/name/publish.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
'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, {
name: res.Name,
value: res.Value
})
}
module.exports = configure(({ ky }) => {
return async (path, 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', path)
if (options.allowOffline != null) searchParams.set('allow-offline', options.allowOffline)
if (options.key) searchParams.set('key', options.key)
if (options.lifetime) searchParams.set('lifetime', options.lifetime)
if (options.quieter != null) searchParams.set('quieter', options.quieter)
if (options.resolve != null) searchParams.set('resolve', options.resolve)
if (options.ttl) searchParams.set('ttl', options.ttl)

send.andTransform({
path: 'name/publish',
args: args,
qs: opts
}, transform, callback)
})
}
const res = await ky.post('name/publish', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return toCamel(res)
}
})
36 changes: 17 additions & 19 deletions src/name/pubsub/cancel.js
Original file line number Diff line number Diff line change
@@ -1,24 +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, {
canceled: res.Canceled === undefined || res.Canceled === true
})
}
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)

send.andTransform({
path: 'name/pubsub/cancel',
args: args,
qs: opts
}, transform, callback)
})
}
const res = await ky.post('name/pubsub/cancel', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return toCamel(res)
}
})
10 changes: 6 additions & 4 deletions src/name/pubsub/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict'

module.exports = (send) => ({
cancel: require('./cancel')(send),
state: require('./state')(send),
subs: require('./subs')(send)
const callbackify = require('callbackify')

module.exports = config => ({
cancel: callbackify.variadic(require('./cancel')(config)),
state: callbackify.variadic(require('./state')(config)),
subs: callbackify.variadic(require('./subs')(config))
})
32 changes: 14 additions & 18 deletions src/name/pubsub/state.js
Original file line number Diff line number Diff line change
@@ -1,23 +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, {
enabled: res.Enabled
})
}
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.post('name/pubsub/state', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams: options.searchParams
}).json()

send.andTransform({
path: 'name/pubsub/state',
qs: opts
}, transform, callback)
})
}
return toCamel(res)
}
})
29 changes: 13 additions & 16 deletions src/name/pubsub/subs.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
'use strict'

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

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

module.exports = (send) => {
return promisify((opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
const res = await ky.get('name/pubsub/subs', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams: options.searchParams
}).json()

send.andTransform({
path: 'name/pubsub/subs',
qs: opts
}, transform, callback)
})
}
return res.Strings
}
})
37 changes: 20 additions & 17 deletions src/name/resolve.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
'use strict'

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

const transform = function (res, callback) {
callback(null, res.Path)
}
module.exports = configure(({ ky }) => {
return async (path, 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', path)
if (options.dhtRecordCount != null) searchParams.set('dht-record-count', options.dhtRecordCount)
if (options.dhtTimeout != null) searchParams.set('dht-timeout', options.dhtTimeout)
if (options.noCache != null) searchParams.set('nocache', options.noCache)
if (options.recursive != null) searchParams.set('recursive', options.recursive)

send.andTransform({
path: 'name/resolve',
args: args,
qs: opts
}, transform, callback)
})
}
const res = await ky.post('name/resolve', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return res.Path
}
})
4 changes: 2 additions & 2 deletions src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
name: require('../name')(config)
}

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

// Network
name: require('../name'),
ping: require('../ping'),
pingReadableStream: require('../ping-readable-stream'),
pingPullStream: require('../ping-pull-stream'),
Expand Down

0 comments on commit cdf9390

Please sign in to comment.