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

refactor: async iterables #1174

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f281b20
refactor: convert key API to async/await
Nov 19, 2019
acf2233
refactor: convert log API to async/await
Nov 19, 2019
0ad3897
fix: tests
Nov 19, 2019
e0a4ffd
fix: tests
Nov 19, 2019
cdf9390
refactor: convert name API to async/await
Nov 19, 2019
2895384
fix: tests
Nov 19, 2019
96b86f5
refactor: convert misc root level APIs to async/await
Nov 19, 2019
8638f9c
Merge branch 'master' into refactor/key-async-await
Nov 19, 2019
aa33b9a
Merge branch 'master' into refactor/log-async-await
Nov 19, 2019
2c110a5
Merge branch 'master' into refactor/name-async-await
Nov 19, 2019
e673f91
Merge branch 'master' into refactor/key-async-await
Nov 20, 2019
373d1e0
Merge branch 'master' into refactor/log-async-await
Nov 20, 2019
f4476cc
Merge branch 'master' into refactor/name-async-await
Nov 20, 2019
29cce3a
refactor: convert repo API to async await
Nov 20, 2019
a891032
refactor: convert stats API to async/await
Nov 21, 2019
a88767f
refactor: convert swarm API to async/await
Nov 21, 2019
1363199
fix: tests
Nov 21, 2019
c3cd680
refactor: convert mount API to async/await
Nov 21, 2019
e011c8e
fix: load mount
Nov 21, 2019
4186327
fix: post all the things
Nov 21, 2019
2281599
refactor: convert version API to async/await
Nov 21, 2019
d8da835
refactor: convert ping API to async/await
Nov 21, 2019
aa83f91
refactor: convert update API to async/await
Nov 21, 2019
f0d2aca
Merge branch 'master' into refactor/misc-async-await
Nov 21, 2019
09afe67
fix: remove tests for go-ipfs 0.4.4 peer response
Nov 21, 2019
3840f40
fix: ping sub modules test
Nov 21, 2019
1980530
Merge branch 'refactor/log-async-await' into refactor/async-await-rou…
Nov 21, 2019
81e72b3
Merge branch 'refactor/name-async-await' into refactor/async-await-ro…
Nov 21, 2019
12ff0d8
Merge branch 'refactor/misc-async-await' into refactor/async-await-ro…
Nov 21, 2019
6ecf79b
Merge branch 'refactor/repo-async-await' into refactor/async-await-ro…
Nov 21, 2019
d2beb18
Merge branch 'refactor/swarm-async-await' into refactor/async-await-r…
Nov 21, 2019
78b430a
refactor: move loadCommands into index and remove unused code
Nov 21, 2019
d7adafc
fix: browser and Node.js configuration
Nov 21, 2019
ec0e66c
fix: dev dependencies
Nov 21, 2019
b9d6d1b
fix: post all the things so not more breaking changes
Nov 21, 2019
31881e5
fix: inconsistent error message in browsers
Nov 21, 2019
b7717bc
refactor: async iterators wip
Nov 21, 2019
c9d530b
feat: use ipfs-utils without node/pull streams
Nov 21, 2019
be6641e
feat: remove peer-info and peer-id
Nov 22, 2019
c86dcae
Merge branch 'master' into refactor/async-iterables
Nov 22, 2019
4fb1b8c
Merge branch 'master' into refactor/async-iterables
Nov 27, 2019
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
Prev Previous commit
Next Next commit
refactor: convert misc root level APIs to async/await
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw committed Nov 19, 2019
commit 96b86f556db8cf0e32da8f547e9c611c5bf351ff
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)
}
})
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
}
})
20 changes: 12 additions & 8 deletions src/stop.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
'use strict'

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

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

return promisify((callback) => {
send({ path: 'shutdown' }, callback)
})
}
return ky.post('shutdown', {
timeout: options.timeout,
signal: options.signal,
headers: options.headers,
searchParams: options.searchParams
}).text()
}
})
14 changes: 7 additions & 7 deletions src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ function requireCommands (send, config) {
catReadableStream: streamify.readable(cat),
catPullStream: pullify.source(cat),
_catAsyncIterator: cat,
commands: callbackify.variadic(require('../commands')(config)),
dns: callbackify.variadic(require('../dns')(config)),
get: callbackify.variadic(async (path, options) => {
const output = []

Expand Down Expand Up @@ -98,6 +100,7 @@ function requireCommands (send, config) {
)
},
_getAsyncIterator: get,
id: callbackify.variadic(require('../id')(config)),
ls: callbackify.variadic((path, options) => collectify(ls)(path, options)),
lsReadableStream: streamify.readable(ls),
lsPullStream: pullify.source(ls),
Expand All @@ -106,6 +109,9 @@ function requireCommands (send, config) {
refsReadableStream: streamify.readable(refs),
refsPullStream: pullify.source(refs),
_refsAsyncIterator: refs,
resolve: callbackify.variadic(require('../resolve')(config)),
stop: callbackify.variadic(require('../stop')(config)),
shutdown: callbackify.variadic(require('../stop')(config)),
getEndpointConfig: require('../get-endpoint-config')(config),
bitswap: require('../bitswap')(config),
block: require('../block')(config),
Expand Down Expand Up @@ -138,21 +144,15 @@ function requireCommands (send, config) {
pingPullStream: require('../ping-pull-stream'),
swarm: require('../swarm'),
pubsub: require('../pubsub'),
dns: require('../dns'),

// Miscellaneous
commands: require('../commands'),
id: require('../id'),
key: require('../key'),
log: require('../log'),
mount: require('../mount'),
repo: require('../repo'),
stop: require('../stop'),
shutdown: require('../stop'),
stats: require('../stats'),
update: require('../update'),
version: require('../version'),
resolve: require('../resolve')
version: require('../version')
}

Object.keys(subCmds).forEach((file) => {
Expand Down
21 changes: 3 additions & 18 deletions test/custom-headers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,13 @@
const isNode = require('detect-node')
const { expect } = require('interface-ipfs-core/src/utils/mocha')
const ipfsClient = require('../src')
const f = require('./utils/factory')

describe('custom headers', function () {
// do not test in browser
if (!isNode) { return }
this.timeout(50 * 1000) // slow CI
let ipfs
let ipfsd
// initialize ipfs with custom headers
before(async () => {
ipfsd = await f.spawn({
initOptions: {
bits: 1024,
profile: 'test'
}
})

before(() => {
ipfs = ipfsClient({
host: 'localhost',
port: 6001,
Expand All @@ -37,6 +27,7 @@ describe('custom headers', function () {
req.on('data', () => {})
req.on('end', () => {
res.writeHead(200)
res.write(JSON.stringify({}))
res.end()
// ensure custom headers are present
expect(req.headers.authorization).to.equal('Bearer ' + 'YOLO')
Expand All @@ -48,16 +39,10 @@ describe('custom headers', function () {
server.listen(6001, () => {
ipfs.id((err, res) => {
if (err) {
throw new Error('Unexpected error.')
throw err
}
// this call is used to test that headers are being sent.
})
})
})

after(async () => {
if (ipfsd) {
await ipfsd.stop()
}
})
})