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

[WIP] feat: async await #21

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ language: node_js

matrix:
include:
- node_js: 6
env: CXX=g++-4.8
- node_js: 8
env: CXX=g++-4.8
- node_js: 10
env: CXX=g++-4.8
# - node_js: stable
# env: CXX=g++-4.8

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ version: "{build}"

environment:
matrix:
- nodejs_version: "6"
- nodejs_version: "8"
- nodejs_version: "10"

matrix:
fast_finish: true
Expand Down
36 changes: 12 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,25 @@ module.exports = {
resolver: {
multicodec: 'raw',
defaultHashAlg: 'sha2-256',
resolve: (binaryBlob, path, callback) => {
callback(null, {
resolve: async (binaryBlob, path) => {
return {
value: binaryBlob,
remainderPath: ''
})
},
tree: (binaryBlob, options, callback) => {
if (typeof options === 'function') {
callback = options
}
callback(null, [])
}
},
tree: async (binaryBlob, options) => []
},
util: {
deserialize: (data, cb) => {
cb(null, data)
},
serialize: (data, cb) => {
cb(null, data)
},
cid: (data, options, cb) => {
if (typeof options === 'function') {
cb = options
options = {}
}
options = options || {}
deserialize: async (data) => data,
serialize: async (data) => data,
cid: async (data, options = {}) => {
const hashAlg = options.hashAlg || 'sha2-256'
const version = typeof options.version === 'undefined' ? 1 : options.version
multihash(data, hashAlg, (err, mh) => {
if (err) return cb(err)
cb(null, new CID(version, 'raw', mh))
return new Promise((resolve, reject) => {
multihash(data, hashAlg, (err, mh) => {
if (err) return reject(err)
resolve(new CID(version, 'raw', mh))
})
})
}
}
Expand Down
91 changes: 35 additions & 56 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ describe('raw codec', () => {
let testData = Buffer.from('test data')
let testBlob

before((done) => {
ipldRaw.util.serialize(testData, (err, result) => {
expect(err).to.not.exist()
testBlob = result
done()
})
before(async () => {
const result = await ipldRaw.util.serialize(testData)
testBlob = result
})

it('multicodec is raw', () => {
Expand All @@ -29,71 +26,53 @@ describe('raw codec', () => {
expect(resolver.defaultHashAlg).to.equal('sha2-256')
})

it('resolver.resolve', () => {
resolver.resolve(testBlob, 'a/b/c/d', (err, result) => {
expect(err).to.not.exist()
expect(result.value.toString('hex')).to.equal(testData.toString('hex'))
expect(result.remainderPath).to.equal('')
})
it('resolver.resolve', async () => {
const result = await resolver.resolve(testBlob, 'a/b/c/d')
expect(result.value.toString('hex')).to.equal(testData.toString('hex'))
expect(result.remainderPath).to.equal('')
})

it('resolver.tree', () => {
resolver.tree(testBlob, {}, (err, paths) => {
expect(err).to.not.exist()
expect(Array.isArray(paths)).to.eql(true)
expect(paths.length).to.eql(0)
})
it('resolver.tree', async () => {
const paths = await resolver.tree(testBlob, {})
expect(Array.isArray(paths)).to.eql(true)
expect(paths.length).to.eql(0)
})

it('resolver.tree option parameter can be ignored', () => {
resolver.tree(testBlob, (err, paths) => {
expect(err).to.not.exist()
expect(Array.isArray(paths)).to.eql(true)
expect(paths.length).to.eql(0)
})
it('resolver.tree option parameter can be ignored', async () => {
const paths = await resolver.tree(testBlob)
expect(Array.isArray(paths)).to.eql(true)
expect(paths.length).to.eql(0)
})
})

describe('raw util', () => {
let rawData = Buffer.from('some raw data')

it('serialize is noop', (done) => {
ipldRaw.util.serialize(rawData, (err, result) => {
expect(err).to.not.exist()
expect(result).to.equal(rawData)
done()
})
it('serialize is noop', async () => {
const result = await ipldRaw.util.serialize(rawData)
expect(result).to.equal(rawData)
})

it('deserialize is noop', (done) => {
ipldRaw.util.deserialize(rawData, (err, result) => {
expect(err).to.not.exist()
expect(result).to.equal(rawData)
done()
})
it('deserialize is noop', async () => {
const result = await ipldRaw.util.deserialize(rawData)
expect(result).to.equal(rawData)
})

it('create cid', (done) => {
ipldRaw.util.cid(rawData, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('raw')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
})
it('create cid', async () => {
const cid = await ipldRaw.util.cid(rawData)
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('raw')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
})

it('create cid with hashAlg', (done) => {
ipldRaw.util.cid(rawData, { hashAlg: 'sha2-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('raw')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
done()
})
it('create cid with hashAlg', async () => {
const cid = await ipldRaw.util.cid(rawData, { hashAlg: 'sha2-512' })
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('raw')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
})
})