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 25, 2019
1 parent 7899008 commit cc9d222
Show file tree
Hide file tree
Showing 107 changed files with 2,036 additions and 2,368 deletions.
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
43 changes: 23 additions & 20 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 @@ -79,8 +79,9 @@
"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": "ipfs/js-datastore-pubsub#key-should-be-a-buffer",
"debug": "^4.1.0",
"dlv": "^1.1.3",
"err-code": "^2.0.0",
Expand All @@ -94,18 +95,18 @@
"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": "^35.1.0",
"ipfs-http-client": "ipfs/js-ipfs-http-client#update-ipld-dag-pb",
"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",
"ipfs-utils": "~0.2.0",
"ipfs-utils": "^0.3.0",
"ipld": "~0.25.0",
"ipld-bitcoin": "~0.3.0",
"ipld-dag-cbor": "~0.15.0",
Expand All @@ -114,7 +115,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 +125,43 @@
"it-to-stream": "^0.1.1",
"just-safe-set": "^2.1.0",
"kind-of": "^6.0.2",
"ky": "~0.13.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,9 +203,9 @@
"execa": "^2.0.4",
"form-data": "^2.5.1",
"hat": "0.0.3",
"interface-ipfs-core": "^0.113.0",
"interface-ipfs-core": "ipfs/interface-js-ipfs-core#update-dag-pb",
"ipfs-interop": "~0.1.0",
"ipfsd-ctl": "~0.46.0",
"ipfsd-ctl": "^0.47.1",
"libp2p-websocket-star": "~0.10.2",
"ncp": "^2.0.0",
"p-event": "^4.1.0",
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 cc9d222

Please sign in to comment.