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
Tests aren't working yet
  • Loading branch information
achingbrain committed Sep 23, 2019
1 parent 838acf3 commit 642deaf
Show file tree
Hide file tree
Showing 106 changed files with 1,643 additions and 2,120 deletions.
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
38 changes: 20 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 Down Expand Up @@ -94,18 +94,18 @@
"hashlru": "^2.3.0",
"human-to-milliseconds": "^2.0.0",
"interface-datastore": "~0.7.0",
"ipfs-bitswap": "~0.25.1",
"ipfs-bitswap": "ipfs/js-ipfs-bitswap#feat/async-await",
"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 +114,7 @@
"ipld-git": "~0.5.0",
"ipld-raw": "^4.0.0",
"ipld-zcash": "~0.3.0",
"ipns": "~0.6.0",
"ipns": "ipfs/js-ipns#downgrade-peer-id",
"is-domain-name": "^1.0.1",
"is-ipfs": "~0.6.1",
"is-pull-stream": "~0.0.0",
Expand All @@ -124,41 +124,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-bootstrap": "~0.9.3",
"libp2p-crypto": "~0.16.1",
"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": "libp2p/js-libp2p-keychain#downgrade-peer-id",
"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 +202,9 @@
"execa": "^2.0.4",
"form-data": "^2.5.1",
"hat": "0.0.3",
"interface-ipfs-core": "^0.113.0",
"interface-ipfs-core": "^0.114.0",
"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/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = {
print('Daemon is ready')

const cleanup = async () => {
print(`Received interrupt signal, shutting down...`)
print('Received interrupt signal, shutting down...')
await daemon.stop()
process.exit(0)
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/file/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
argv.resolve((async () => {
const path = argv.key
// `ipfs file ls` is deprecated. See https://ipfs.io/docs/commands/#ipfs-file-ls
argv.print(`This functionality is deprecated, and will be removed in future versions. If possible, please use 'ipfs ls' instead.`)
argv.print('This functionality is deprecated, and will be removed in future versions. If possible, please use \'ipfs ls\' instead.')

const ipfs = await argv.getIpfs()
let links = await ipfs.ls(path)
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = {
})
} catch (err) {
if (err.code === 'EACCES') {
err.message = `EACCES: permission denied, stat $IPFS_PATH/version`
err.message = 'EACCES: permission denied, stat $IPFS_PATH/version'
}
throw err
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ module.exports = {
commit: {
type: 'boolean',
default: false,
describe: `Include the version's commit hash`
describe: 'Include the version\'s commit hash'
},
repo: {
type: 'boolean',
default: false,
describe: `Print only the repo's version number`
describe: 'Print only the repo\'s version number'
},
all: {
type: 'boolean',
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: 45 additions & 57 deletions src/core/boot.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,74 @@
'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
}
}

return true
}

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()
})
// 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 })
}
}

cb()
},
(cb) => {
// No problem, we don't have to start the node
if (!doStart) {
return cb()
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 642deaf

Please sign in to comment.