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

refactor: convert misc root level APIs to async/await #1169

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions src/commands.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
'use strict'

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

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

return promisify((callback) => {
send({
path: 'commands'
}, callback)
})
}
const configure = require('./lib/configure')

module.exports = configure(({ ky }) => {
return options => {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
if (options.flags != null) searchParams.set('flags', options.flags)

return ky.get('commands', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()
}
})
35 changes: 16 additions & 19 deletions src/dns.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
'use strict'

const promisify = require('promisify-es6')
const moduleConfig = require('./utils/module-config')
const configure = require('./lib/configure')

const transform = function (res, callback) {
callback(null, res.Path)
}
module.exports = configure(({ ky }) => {
return async (domain, options) => {
options = options || {}

module.exports = (arg) => {
const send = moduleConfig(arg)
const searchParams = new URLSearchParams(options.searchParams)
searchParams.set('arg', domain)
if (options.recursive != null) searchParams.set('recursive', options.recursive)

return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
const res = await ky.post('dns', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

send.andTransform({
path: 'dns',
args: args,
qs: opts
}, transform, callback)
})
}
return res.Path
}
})
41 changes: 14 additions & 27 deletions src/id.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,19 @@
'use strict'

const promisify = require('promisify-es6')
const moduleConfig = require('./utils/module-config')
const configure = require('./lib/configure')
const toCamel = require('./lib/object-to-camel')

module.exports = (arg) => {
const send = moduleConfig(arg)
module.exports = configure(({ ky }) => {
return async options => {
options = options || {}

return promisify((opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = undefined
}
const res = await ky.get('id', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams: options.searchParams
}).json()

send({
path: 'id',
args: opts
}, (err, result) => {
if (err) {
return callback(err)
}
const identity = {
id: result.ID,
publicKey: result.PublicKey,
addresses: result.Addresses,
agentVersion: result.AgentVersion,
protocolVersion: result.ProtocolVersion
}
callback(null, identity)
})
})
}
return toCamel(res)
}
})
43 changes: 18 additions & 25 deletions src/mount.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
'use strict'

const promisify = require('promisify-es6')
const moduleConfig = require('./utils/module-config')
const configure = require('./lib/configure')
const toCamel = require('./lib/object-to-camel')

module.exports = (arg) => {
const send = moduleConfig(arg)
module.exports = configure(({ ky }) => {
return async options => {
options = options || {}

return promisify((ipfs, ipns, callback) => {
if (typeof ipfs === 'function') {
callback = ipfs
ipfs = null
} else if (typeof ipns === 'function') {
callback = ipns
ipns = null
}
const opts = {}
if (ipfs) {
opts.f = ipfs
}
if (ipns) {
opts.n = ipns
}
const searchParams = new URLSearchParams(options.searchParams)
if (options.ipfsPath != null) searchParams.set('ipfs-path', options.ipfsPath)
if (options.ipnsPath != null) searchParams.set('ipns-path', options.ipnsPath)

send({
path: 'mount',
qs: opts
}, callback)
})
}
const res = await ky.post('dns', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return toCamel(res)
}
})
34 changes: 0 additions & 34 deletions src/ping-pull-stream.js

This file was deleted.

30 changes: 0 additions & 30 deletions src/ping-readable-stream.js

This file was deleted.

81 changes: 24 additions & 57 deletions src/ping.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,27 @@
'use strict'

const promisify = require('promisify-es6')
const pump = require('pump')
const Writable = require('readable-stream').Writable
const moduleConfig = require('./utils/module-config')
const PingMessageStream = require('./utils/ping-message-stream')

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

return promisify((id, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
}

if (opts.n && opts.count) {
return callback(new Error('Use either n or count, not both'))
}

// Default number of packtes to 1
if (!opts.n && !opts.count) {
opts.n = 1
const ndjson = require('iterable-ndjson')
const configure = require('./lib/configure')
const toIterable = require('./lib/stream-to-iterable')
const toCamel = require('./lib/object-to-camel')

module.exports = configure(({ ky }) => {
return async function * ping (peerId, options) {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
searchParams.set('arg', `${peerId}`)
if (options.count != null) searchParams.set('count', options.count)

const res = await ky.post('ping', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
})

for await (const chunk of ndjson(toIterable(res.body))) {
yield toCamel(chunk)
}

const request = {
path: 'ping',
args: id,
qs: opts
}

// Transform the response stream to a value:
// [{ success: <boolean>, time: <number>, text: <string> }]
const transform = (stream, callback) => {
const messageConverter = new PingMessageStream()
const responses = []

pump(
stream,
messageConverter,
new Writable({
objectMode: true,
write (chunk, enc, cb) {
responses.push(chunk)
cb()
}
}),
(err) => {
if (err) {
return callback(err)
}
callback(null, responses)
}
)
}

send.andTransform(request, transform, callback)
})
}
}
})
75 changes: 23 additions & 52 deletions src/resolve.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,25 @@
'use strict'

const promisify = require('promisify-es6')
const multibase = require('multibase')
const CID = require('cids')

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (typeof (opts) === 'function') {
callback = opts
opts = {}
}

opts = opts || {}

if (opts.cidBase) {
opts['cid-base'] = opts.cidBase
delete opts.cidBase
}

const transform = (res, callback) => {
if (!opts['cid-base']) {
return callback(null, res.Path)
}

// FIXME: remove when go-ipfs supports ?cid-base for /api/v0/resolve
// https://github.com/ipfs/go-ipfs/pull/5777#issuecomment-439838555
const parts = res.Path.split('/') // ['', 'ipfs', 'QmHash', ...]

if (multibase.isEncoded(parts[2]) !== opts['cid-base']) {
try {
let cid = new CID(parts[2])

if (cid.version === 0 && opts['cid-base'] !== 'base58btc') {
cid = cid.toV1()
}

parts[2] = cid.toBaseEncodedString(opts['cid-base'])
res.Path = parts.join('/')
} catch (err) {
return callback(err)
}
}

callback(null, res.Path)
}

send.andTransform({
path: 'resolve',
args: args,
qs: opts
}, transform, callback)
})
}
const configure = require('./lib/configure')

module.exports = configure(({ ky }) => {
return async (path, options) => {
options = options || {}

const searchParams = new URLSearchParams(options.searchParams)
searchParams.set('arg', `${path}`)
if (options.cidBase) searchParams.set('cid-base', options.cidBase)
if (options.dhtRecordCount) searchParams.set('dht-record-count', options.dhtRecordCount)
if (options.dhtTimeout) searchParams.set('dht-timeout', options.dhtTimeout)
if (options.recursive != null) searchParams.set('recursive', options.recursive)

const res = await ky.post('resolve', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams
}).json()

return res.Path
}
})
Loading