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

Commit

Permalink
fix: use binary blobs directly
Browse files Browse the repository at this point in the history
IPLD shouldn't need to know about IPFS. Hence work directly with
the binary data instead of using an IPFS block.

This is part of ipld/interface-ipld-format#21

BREAKING CHANGE: Everyone calling the functions of `resolve` need to
pass in the binary data instead of an IPFS block.

So if your input is an IPFS block, the code changes from

    resolver.resolve(block, path, (err, result) => {…}

to

    resolver.resolve(block.data, path, (err, result) => {…}
  • Loading branch information
vmx committed Feb 12, 2018
1 parent ae56fd7 commit 6fc00cd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@
"devDependencies": {
"aegir": "^12.4.0",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"ipfs-block": "~0.6.1",
"multihashing-async": "~0.4.7"
"dirty-chai": "^2.0.1"
}
}
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const CID = require('cids')
module.exports = {
resolver: {
multicodec: 'raw',
resolve: (ipfsBlock, path, callback) => {
resolve: (binaryBlob, path, callback) => {
callback(null, {
value: ipfsBlock.data,
value: binaryBlob,
remainderPath: ''
})
},
tree: (ipfsBlock, options, callback) => {
tree: (binaryBlob, options, callback) => {
callback(null, [])
}
},
Expand Down
26 changes: 5 additions & 21 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict'
/* eslint-env mocha */
const IpfsBlock = require('ipfs-block')
const CID = require('cids')
const multihashing = require('multihashing-async')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
Expand All @@ -13,16 +10,13 @@ const resolver = ipldRaw.resolver

describe('raw codec', () => {
let testData = Buffer.from('test data')
let testIpfsBlock
let testBlob

before((done) => {
ipldRaw.util.serialize(testData, (err, result) => {
expect(err).to.not.exist()
toIpfsBlock(resolver.multicodec, result, (err, ipfsBlock) => {
expect(err).to.not.exist()
testIpfsBlock = ipfsBlock
done()
})
testBlob = result
done()
})
})

Expand All @@ -31,28 +25,18 @@ describe('raw codec', () => {
})

it('resolver.resolve', () => {
resolver.resolve(testIpfsBlock, 'a/b/c/d', (err, result) => {
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.tree', () => {
resolver.tree(testIpfsBlock, {}, (err, paths) => {
resolver.tree(testBlob, {}, (err, paths) => {
expect(err).to.not.exist()
expect(Array.isArray(paths)).to.eql(true)
expect(paths.length).to.eql(0)
})
})
})

function toIpfsBlock (multicodec, value, callback) {
multihashing(value, 'keccak-256', (err, hash) => {
if (err) {
return callback(err)
}
const cid = new CID(1, multicodec, hash)
callback(null, new IpfsBlock(value, cid))
})
}

0 comments on commit 6fc00cd

Please sign in to comment.