Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore: convert internals to promises
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Sep 26, 2019
1 parent 74ef556 commit 99d7410
Show file tree
Hide file tree
Showing 110 changed files with 2,052 additions and 2,377 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const preloadNode = MockPreloadNode.createNode()
const echoServer = EchoServer.createServer()

module.exports = {
bundlesize: { maxSize: '696kB' },
bundlesize: { maxSize: '683kB' },
webpack: {
resolve: {
mainFields: ['browser', 'main'],
Expand Down
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ env:
# https://travis-ci.community/t/timeout-after-build-finished-and-succeeded/1336
- YARN_GPG=no

addons:
apt:
packages:
# Fixes error while loading shared libraries: libgconf-2.so.4: cannot open shared object file: No such file or directory
# https://github.com/electron/electron/issues/1518
- libgconf-2-4

script: npx nyc -s npx aegir test -t node --timeout 10000 --bail
after_success:
- npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
Expand Down
3 changes: 2 additions & 1 deletion examples/traverse-ipld-graphs/create-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const IPFS = require('../../src/core')
// In your project, replace by the following line and install IPFS as a dep
// const IPFS = require('ipfs')

function createNode (options = {}) {
function createNode (options) {
options = options || {}
options.path = options.path || '/tmp/ipfs' + Math.random()
return IPFS.create({ repo: options.path })
}
Expand Down
3 changes: 1 addition & 2 deletions examples/traverse-ipld-graphs/eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const multihashing = require('multihashing-async')
const Block = require('ipfs-block')
const CID = require('cids')
const fs = require('fs').promises
const { promisify } = require('util')

async function main () {
const ipfs = await createNode()
Expand All @@ -20,7 +19,7 @@ async function main () {

for (const ethBlockPath of ethBlocks) {
const data = await fs.readFile(ethBlockPath)
const multihash = await promisify(multihashing)(data, 'keccak-256')
const multihash = await multihashing(data, 'keccak-256')

const cid = new CID(1, 'eth-block', multihash)
// console.log(cid.toBaseEncodedString())
Expand Down
3 changes: 1 addition & 2 deletions examples/traverse-ipld-graphs/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const multihashing = require('multihashing-async')
const Block = require('ipfs-block')
const CID = require('cids')
const fs = require('fs').promises
const { promisify } = require('util')

async function main () {
const ipfs = await createNode()
Expand All @@ -28,7 +27,7 @@ async function main () {

await Promise.all(gitObjects.map(async gitObjectsPath => {
const data = await fs.readFile(gitObjectsPath)
const multihash = await promisify(multihashing)(data, 'sha1')
const multihash = await multihashing(data, 'sha1')

const cid = new CID(1, 'git-raw', multihash)
console.log(cid.toString())
Expand Down
40 changes: 22 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@hapi/ammo": "^3.1.1",
"@hapi/boom": "^7.4.3",
"@hapi/hapi": "^18.3.2",
"@hapi/joi": "^15.0.1",
"@hapi/joi": "^15.0.0",
"array-shuffle": "^1.0.1",
"async": "^2.6.1",
"async-iterator-all": "^1.0.0",
Expand All @@ -76,11 +76,13 @@
"bs58": "^4.0.1",
"buffer-peek-stream": "^1.0.1",
"byteman": "^1.3.5",
"callbackify": "^1.1.0",
"cid-tool": "~0.3.0",
"cids": "~0.7.1",
"class-is": "^1.1.0",
"dag-cbor-links": "^1.3.0",
"datastore-core": "~0.7.0",
"datastore-pubsub": "~0.1.1",
"datastore-pubsub": "^0.2.1",
"debug": "^4.1.0",
"dlv": "^1.1.3",
"err-code": "^2.0.0",
Expand All @@ -94,14 +96,14 @@
"hashlru": "^2.3.0",
"human-to-milliseconds": "^2.0.0",
"interface-datastore": "~0.7.0",
"ipfs-bitswap": "~0.25.1",
"ipfs-bitswap": "^0.26.0",
"ipfs-block": "~0.8.1",
"ipfs-block-service": "~0.16.0",
"ipfs-http-client": "^37.0.2",
"ipfs-http-client": "^38.0.0",
"ipfs-http-response": "~0.3.1",
"ipfs-mfs": "^0.13.0",
"ipfs-multipart": "^0.2.0",
"ipfs-repo": "~0.27.0",
"ipfs-repo": "^0.28.0",
"ipfs-unixfs": "~0.1.16",
"ipfs-unixfs-exporter": "^0.38.0",
"ipfs-unixfs-importer": "^0.40.0",
Expand All @@ -114,7 +116,7 @@
"ipld-git": "~0.5.0",
"ipld-raw": "^4.0.0",
"ipld-zcash": "~0.3.0",
"ipns": "~0.6.0",
"ipns": "^0.6.1",
"is-domain-name": "^1.0.1",
"is-ipfs": "~0.6.1",
"is-pull-stream": "~0.0.0",
Expand All @@ -124,41 +126,43 @@
"it-to-stream": "^0.1.1",
"just-safe-set": "^2.1.0",
"kind-of": "^6.0.2",
"ky": "~0.14.0",
"ky": "^0.14.0",
"ky-universal": "~0.3.0",
"libp2p": "~0.26.1",
"libp2p-bootstrap": "~0.10.2",
"libp2p-crypto": "~0.17.0",
"libp2p": "^0.26.2",
"libp2p-bootstrap": "~0.9.3",
"libp2p-crypto": "^0.16.2",
"libp2p-delegated-content-routing": "^0.3.1",
"libp2p-delegated-peer-routing": "^0.3.1",
"libp2p-floodsub": "^0.18.0",
"libp2p-gossipsub": "~0.0.5",
"libp2p-kad-dht": "~0.16.0",
"libp2p-keychain": "~0.5.0",
"libp2p-keychain": "^0.5.1",
"libp2p-mdns": "~0.12.0",
"libp2p-record": "~0.7.0",
"libp2p-secio": "~0.11.0",
"libp2p-tcp": "~0.13.1",
"libp2p-tcp": "^0.13.0",
"libp2p-webrtc-star": "~0.16.0",
"libp2p-websocket-star-multi": "~0.4.3",
"libp2p-websockets": "~0.12.3",
"lodash": "^4.17.15",
"lodash.flatten": "^4.4.0",
"mafmt": "^6.0.10",
"merge-options": "^1.0.1",
"mime-types": "^2.1.21",
"mkdirp": "~0.5.1",
"mortice": "^2.0.0",
"multiaddr": "^6.1.0",
"multiaddr": "^6.1.1",
"multiaddr-to-uri": "^5.0.0",
"multibase": "~0.6.0",
"multicodec": "~0.5.5",
"multihashes": "~0.4.14",
"multihashing-async": "~0.7.0",
"multihashing-async": "^0.8.0",
"node-fetch": "^2.3.0",
"p-iteration": "^1.1.8",
"p-queue": "^6.1.0",
"peer-book": "~0.9.0",
"peer-id": "~0.13.2",
"peer-info": "~0.16.0",
"peer-book": "^0.9.1",
"peer-id": "^0.12.2",
"peer-info": "~0.15.1",
"progress": "^2.0.1",
"promise-nodeify": "^3.0.1",
"promisify-es6": "^1.0.3",
Expand Down Expand Up @@ -200,7 +204,7 @@
"execa": "^2.0.4",
"form-data": "^2.5.1",
"hat": "0.0.3",
"interface-ipfs-core": "~0.114.0",
"interface-ipfs-core": "^0.115.0",
"ipfs-interop": "~0.1.0",
"ipfsd-ctl": "^0.47.2",
"libp2p-websocket-star": "~0.10.2",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ exports.getIPFS = (argv, callback) => {

const cleanup = callbackify(async () => {
if (node && node._repo && !node._repo.closed) {
return node._repo.close()
await node._repo.close()
}
})

Expand Down
102 changes: 44 additions & 58 deletions src/core/boot.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,72 @@
'use strict'

const waterfall = require('async/waterfall')
const RepoErrors = require('ipfs-repo').errors

// Boot an IPFS node depending on the options set
module.exports = (self) => {
module.exports = async (self) => {
self.log('booting')
const options = self._options
const doInit = options.init
const doStart = options.start

// Do the actual boot sequence
waterfall([
// Checks if a repo exists, and if so opens it
// Will return callback with a bool indicating the existence
// of the repo
// TODO vmx 2019-08-05: THIS WON'T WORK IN THE BROWSER due to transpiling, this needs a proper fix. This is just a hack to keep things moving
async () => {
// nothing to do
if (!self._repo.closed) {
return true
}
// Checks if a repo exists, and if so opens it
// Will return callback with a bool indicating the existence
// of the repo
async function repoOpened () {
// nothing to do
if (!self._repo.closed) {
return true
}

try {
const res = await self._repo.open()
} catch (err) {
if (isRepoUninitializedError(err)) return false
if (err) throw err
try {
await self._repo.open()
} catch (err) {
if (isRepoUninitializedError(err)) {
return false
}

return true
},
(repoOpened, cb) => {
// Init with existing initialized, opened, repo
if (repoOpened) {
return self.init({ repo: self._repo }, (err) => {
if (err) return cb(Object.assign(err, { emitted: true }))
cb()
})
if (err) {
throw err
}
}

if (doInit) {
const initOptions = Object.assign(
{ bits: 2048, pass: self._options.pass },
typeof options.init === 'object' ? options.init : {}
)
return self.init(initOptions, (err) => {
if (err) return cb(Object.assign(err, { emitted: true }))
cb()
})
}
return true
}

cb()
},
(cb) => {
// No problem, we don't have to start the node
if (!doStart) {
return cb()
// Do the actual boot sequence
try {
// Init with existing initialized, opened, repo
if (await repoOpened()) {
try {
await self.init({ repo: self._repo })
} catch (err) {
throw Object.assign(err, { emitted: true })
}
} else if (doInit) {
const defaultInitOptions = {
bits: 2048,
pass: self._options.pass
}

self.start((err) => {
if (err) return cb(Object.assign(err, { emitted: true }))
cb()
})
const initOptions = Object.assign(defaultInitOptions, typeof options.init === 'object' ? options.init : {})

await self.init(initOptions)
}
], (err) => {
if (err) {
if (!err.emitted) {
self.emit('error', err)
}
return

if (doStart) {
await self.start()
}

self.log('booted')
self.emit('ready')
})
} catch (err) {
if (!err.emitted) {
self.emit('error', err)
}
}
}

function isRepoUninitializedError (err) {
if (!err) {
return false
}

// If the error is that no repo exists,
// which happens when the version file is not found
// we just want to signal that no repo exist, not
Expand Down
Loading

0 comments on commit 99d7410

Please sign in to comment.