Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
refactor: make code promisify free
Browse files Browse the repository at this point in the history
There's no need for the promisify-es6 module anymore as all the code
is using async/await based libraries now.

This is the same commit as 1a98e16,
the only difference is a commit message that contains the information
about the breaking change.

This reverts commit e84955b.

BREAKING CHANGE: Everyone upgrading to this release also needs to upgrade
ipfs-block-service, ipfs-repo and ipld-in-memory.

This commit uses the new async API of:

 - ipfs-block-service >= 0.16.0
 - ipfs-repo >=0.27.0
 - ipld-in-memory >= 3.0.0

If your library has a dependency on any of those, you likely also need
to upgrade to at least those versions.
  • Loading branch information
vmx committed Aug 1, 2019
1 parent 8f20e0f commit 23de2e4
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 34 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
"dirty-chai": "^2.0.1",
"ethereumjs-block": "^2.2.0",
"fs-extra": "^8.0.1",
"ipfs-block-service": "~0.15.2",
"ipfs-repo": "~0.26.5",
"ipfs-block-service": "^0.16.0",
"ipfs-repo": "^0.27.0",
"ipld-bitcoin": "~0.3.0",
"ipld-ethereum": "^4.0.0",
"ipld-git": "~0.5.0",
"ipld-in-memory": "^2.0.0",
"ipld-in-memory": "^3.0.0",
"ipld-zcash": "~0.3.0",
"merkle-patricia-tree": "^3.0.0",
"multihashes": "~0.4.14",
Expand All @@ -60,7 +60,6 @@
"ipld-raw": "^4.0.0",
"merge-options": "^1.0.1",
"multicodec": "~0.5.1",
"promisify-es6": "^1.0.3",
"typical": "^5.0.0"
},
"contributors": [
Expand Down
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const ipldDagCbor = require('ipld-dag-cbor')
const ipldDagPb = require('ipld-dag-pb')
const ipldRaw = require('ipld-raw')
const multicodec = require('multicodec')
const promisify = require('promisify-es6')
const typical = require('typical')
const { extendIterator } = require('./util')

Expand Down Expand Up @@ -95,7 +94,7 @@ class IPLDResolver {
// get block
// use local resolver
// update path value
const block = await promisify(this.bs.get.bind(this.bs))(cid)
const block = await this.bs.get(cid)
const result = format.resolver.resolve(block.data, path)

// Prepare for the next iteration if there is a `remainderPath`
Expand Down Expand Up @@ -129,7 +128,7 @@ class IPLDResolver {
* @returns {Promise.<Object>} - Returns a Promise with the IPLD Node that correspond to the given `cid`.
*/
async get (cid) {
const block = await promisify(this.bs.get.bind(this.bs))(cid)
const block = await this.bs.get(cid)
const format = await this._getFormat(block.cid.codec)
const node = format.util.deserialize(block.data)

Expand Down Expand Up @@ -194,7 +193,7 @@ class IPLDResolver {

if (!options.onlyHash) {
const block = new Block(serialized, cid)
await promisify(this.bs.put.bind(this.bs))(block)
await this.bs.put(block)
}

return cid
Expand Down Expand Up @@ -255,7 +254,7 @@ class IPLDResolver {
* @return {Promise.<CID>} The CID of the removed IPLD Node.
*/
async remove (cid) { // eslint-disable-line require-await
return promisify(this.bs.delete.bind(this.bs))(cid)
return this.bs.delete(cid)
}

/**
Expand Down Expand Up @@ -336,7 +335,7 @@ class IPLDResolver {
if (treePaths.length === 0 && queue.length > 0) {
({ cid, basePath } = queue.shift())
const format = await this._getFormat(cid.codec)
block = await promisify(this.bs.get.bind(this.bs))(cid)
block = await this.bs.get(cid)

const paths = format.resolver.tree(block.data)
treePaths.push(...paths)
Expand Down
4 changes: 2 additions & 2 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ module.exports = (repo) => {
expect(r.bs).to.exist()
})

it('creates an in memory repo if no blockService is passed', () => {
inMemory(IPLDResolver, (err, r) => {
it('creates an in memory repo if no blockService is passed', async () => {
await inMemory(IPLDResolver, (err, r) => {
expect(err).to.not.exist()
expect(r.bs).to.exist()
})
Expand Down
11 changes: 3 additions & 8 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
'use strict'

const IPFSRepo = require('ipfs-repo')
const promisify = require('promisify-es6')

const basePath = 'ipfs' + Math.random()

Expand All @@ -19,17 +18,13 @@ idb.deleteDatabase(basePath + '/blocks')
describe('Browser', () => {
const repo = new IPFSRepo(basePath)

const repoInit = promisify(repo.init.bind(repo))
const repoOpen = promisify(repo.open.bind(repo))
const repoClose = promisify(repo.close.bind(repo))

before(async () => {
await repoInit({})
await repoOpen()
await repo.init({})
await repo.open()
})

after(async () => {
await repoClose()
await repo.close()
idb.deleteDatabase(basePath)
idb.deleteDatabase(basePath + '/blocks')
})
Expand Down
10 changes: 2 additions & 8 deletions test/ipld-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const dagPB = require('ipld-dag-pb')
const CID = require('cids')
const inMemory = require('ipld-in-memory')
const multicodec = require('multicodec')
const promisify = require('promisify-es6')

const IPLDResolver = require('../src')

Expand All @@ -30,12 +29,7 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => {
let cidPb

before(async () => {
resolver = await new Promise((resolve, reject) => {
inMemory(IPLDResolver, (error, res) => {
if (error) reject(error)
else resolve(res)
})
})
resolver = await inMemory(IPLDResolver)

nodePb = dagPB.DAGNode.create(Buffer.from('I am inside a Protobuf'))
cidPb = await resolver.put(nodePb, multicodec.DAG_PB, { cidVersion: 0 })
Expand Down Expand Up @@ -65,7 +59,7 @@ describe('IPLD Resolver for dag-cbor + dag-pb', () => {
cidVersion: 1,
hashAlg: multicodec.SHA2_256
})
const result = await promisify(resolver.bs._repo.blocks.has)(cid)
const result = await resolver.bs._repo.blocks.has(cid)
expect(result).to.be.false()
})

Expand Down
8 changes: 2 additions & 6 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@

const fs = require('fs-extra')
const IPFSRepo = require('ipfs-repo')
const promisify = require('promisify-es6')
const os = require('os')

describe('Node.js', () => {
const repoExample = process.cwd() + '/test/example-repo'
const repoTests = os.tmpdir() + '/t-r-' + Date.now()
const repo = new IPFSRepo(repoTests)

const repoOpen = promisify(repo.open.bind(repo))
const repoClose = promisify(repo.close.bind(repo))

before(async () => {
await fs.copy(repoExample, repoTests)
await repoOpen()
await repo.open()
})

after(async () => {
await repoClose()
await repo.close()
await fs.remove(repoTests)
})

Expand Down

0 comments on commit 23de2e4

Please sign in to comment.