Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Awesome IPLD Endeavour #5

Merged
merged 2 commits into from
Oct 26, 2016
Merged
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"homepage": "https://github.com/ipfs/js-ipfs-block#readme",
"devDependencies": {
"aegir": "^8.0.0",
"chai": "^3.5.0"
"chai": "^3.5.0",
"multihashes": "^0.2.2"
},
"dependencies": {
"multihashing": "^0.2.0"
Expand All @@ -47,4 +48,4 @@
"David Dias <daviddias.p@gmail.com>",
"dignifiedquire <dignifiedquire@gmail.com>"
]
}
}
26 changes: 9 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'
const util = require('./util')

// Immutable block of data
function Block (data, type) {
const multihashing = require('multihashing')

function Block (data) {
if (!data) {
throw new Error('Block must be constructed with data')
}
Expand All @@ -17,21 +17,13 @@ function Block (data, type) {
this.data = new Buffer(data)
}

this.key = util.hash(this.data)
this.type = type || 'protobuf'
}

Object.defineProperty(Block.prototype, 'extension', {
get () {
switch (this.type) {
case 'protobuf':
return 'data'
case 'ipld':
return 'ipld'
default:
return this.type
this.key = (hashFunc) => {
if (!hashFunc) {
hashFunc = 'sha2-256'
}

return multihashing(this.data, hashFunc)
}
})
}

module.exports = Block
8 changes: 0 additions & 8 deletions src/util.js

This file was deleted.

32 changes: 7 additions & 25 deletions test/block.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@

const expect = require('chai').expect
const Block = require('../src')
const multihash = require('multihashes')

describe('block', () => {
it('create', () => {
const b = new Block('random-data')
expect(b.key).to.exist
expect(multihash.toB58String(b.key())).to.equal('QmeoBGh5g5kHgK3xppJ1YPwB9xgH2GoqhMSuQVpzDdvtJG')
expect(multihash.toB58String(b.key('sha1'))).to.equal('5dsvLgRV9RVj9eSgtxMrXQjbpfFeHY')
expect(b.data).to.exist
expect(b.extension).to.be.eql('data')
})

it('create /wo new', () => {
const b = Block('random-data')
expect(b.key).to.exist
expect(multihash.toB58String(b.key())).to.equal('QmeoBGh5g5kHgK3xppJ1YPwB9xgH2GoqhMSuQVpzDdvtJG')
expect(multihash.toB58String(b.key('sha1'))).to.equal('5dsvLgRV9RVj9eSgtxMrXQjbpfFeHY')
expect(b.data).to.exist
expect(b.extension).to.be.eql('data')
})

it('fail to create an empty block', () => {
Expand All @@ -33,28 +34,9 @@ describe('block', () => {
// it from the original implementation
// It doesn't stricly verify the immutability of the Block object
const block = new Block("Can't change this!")
let key = block.key
let key = block.key()
key = new Buffer('new key')

expect(key.equals(block.key)).to.equal(false)
})

it('has the right extension to type mapping', () => {
const b1 = new Block('hello', 'protobuf')
const b2 = new Block('hello')
const b3 = new Block('hello', 'ipld')
const b4 = new Block('hello', 'woot')

expect(b1.type).to.be.eql('protobuf')
expect(b1.extension).to.be.eql('data')

expect(b2.type).to.be.eql('protobuf')
expect(b2.extension).to.be.eql('data')

expect(b3.type).to.be.eql('ipld')
expect(b3.extension).to.be.eql('ipld')

expect(b4.type).to.be.eql('woot')
expect(b4.extension).to.be.eql('woot')
expect(key.equals(block.key())).to.equal(false)
})
})