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

Commit

Permalink
Merge fa9a47f into 9c036f3
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Oct 5, 2016
2 parents 9c036f3 + fa9a47f commit 95877e6
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 86 deletions.
14 changes: 11 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ script:
- npm test
- npm run coverage

addons:
firefox: 'latest'

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

after_success:
- npm run coverage-publish

env:
- CXX=g++-4.8

addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
27 changes: 12 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
- [Browser: `<script>` Tag](#browser-script-tag)
- [API](#api)
- [Block](#block)
- [`new Block(data, [type])`](#new-blockdata-type)
- [`new Block(data)`](#new-blockdata)
- [`block.data`](#blockdata)
- [`block.key`](#blockkey)
- [`block.extension`](#blockextension)
- [`block.key([hashFn,] callback)`](#blockkeyhashfn-callback)
- [Contribute](#contribute)
- [License](#license)

Expand Down Expand Up @@ -56,7 +55,7 @@ const Block = require('ipfs-block')
// create a block
const block = new Block('hello world')
console.log(block.data)
console.log(block.key)
block.key((err, key) => console.log(err, key))
```

### Browser: Browserify, Webpack, other bundlers
Expand All @@ -67,7 +66,7 @@ it and use with your favourite bundler without having to adjust asset management
process.

```js
var Block = require('ipfs-block')
const Block = require('ipfs-block')
```

### Browser: `<script>` Tag
Expand All @@ -89,26 +88,24 @@ const Block = require('ipfs-block')

### Block

#### `new Block(data, [type])`
#### `new Block(data)`

- `data: Buffer|String`

Creates a new block with raw data `data`. `type` can be either `'protobuf'` or `'ipld'`
Creates a new block with raw data `data`.

#### `block.data`

The raw data of the block. Its format matches whatever was provided in its
constructor.

#### `block.key`

The [multihash][multihash] of the block's data, as a buffer.

#### `block.key([hashFn,] callback)`

### `block.extension`
- `hashFn: String`, optional. Default `sha2-256`.
- `callback: Function`

The extension on how to store the blog, depends on the type:
The callback will be called with the [multihash][multihash] of the block's data, as a buffer.

- `'protobuf'`: `'data'`
- `'ipld'`: `'ipld'`

[ipfs]: https://ipfs.io
[multihash]: https://github.com/jbenet/js-multihash
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"scripts": {
"lint": "aegir-lint",
"build": "aegir-build",
"test": "aegir-test",
"test": "PHANTOM=off aegir-test",
"test:node": "aegir-test node",
"test:browser": "aegir-test browser",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"test:browser": "PHANTOM=off aegir-test browser",
"release": "PHANTOM=off aegir-release",
"release-minor": "PHANTOM=off aegir-release --type minor",
"release-major": "PHANTOM=off aegir-release --type major",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
},
Expand All @@ -38,13 +38,14 @@
"homepage": "https://github.com/ipfs/js-ipfs-block#readme",
"devDependencies": {
"aegir": "^8.0.0",
"async": "^2.0.1",
"chai": "^3.5.0"
},
"dependencies": {
"multihashing": "^0.2.0"
"multihashing-async": "^0.1.0"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
"dignifiedquire <dignifiedquire@gmail.com>"
]
}
}
50 changes: 26 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
'use strict'
const util = require('./util')

// Immutable block of data
function Block (data, type) {
if (!data) {
throw new Error('Block must be constructed with data')
}
const multihashing = require('multihashing-async')

module.exports = Block

// Immutable block of data
function Block (data) {
if (!(this instanceof Block)) {
return new Block(data)
}

if (data instanceof Buffer) {
this.data = data
} else {
this.data = new Buffer(data)
if (!data) {
throw new Error('Block must be constructed with data')
}

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

this.key = (hashFunc, callback) => {
if (typeof hashFunc === 'function') {
callback = hashFunc
hashFunc = null
}

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

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

module.exports = Block
function ensureBuffer (data) {
if (Buffer.isBuffer(data)) {
return data
}

return new Buffer(data)
}
8 changes: 0 additions & 8 deletions src/util.js

This file was deleted.

71 changes: 42 additions & 29 deletions test/block.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,72 @@
'use strict'

const expect = require('chai').expect
const parallel = require('async/parallel')
const Block = require('../src')
const mh = require('multihashes')

function expectKey (block, hashFn, expectedKey, callback) {
const cb = (err, key) => {
if (err) {
return callback(err)
}
expect(mh.toB58String(key)).to.be.eql(expectedKey)
callback()
}

if (typeof expectedKey === 'function') {
callback = expectedKey
expectedKey = hashFn
block.key(cb)
} else {
block.key(hashFn, cb)
}
}

describe('block', () => {
it('create', () => {
it('create', (done) => {
const b = new Block('random-data')
expect(b.key).to.exist
expect(b.data).to.exist
expect(b.extension).to.be.eql('data')
parallel([
(cb) => expectKey(b, 'QmeoBGh5g5kHgK3xppJ1YPwB9xgH2GoqhMSuQVpzDdvtJG', cb),
(cb) => expectKey(b, 'sha1', '5dsvLgRV9RVj9eSgtxMrXQjbpfFeHY', cb)
], done)
})

it('create /wo new', () => {
it('create /wo new', (done) => {
const b = Block('random-data')
expect(b.key).to.exist
expect(b.data).to.exist
expect(b.extension).to.be.eql('data')
parallel([
(cb) => expectKey(b, 'QmeoBGh5g5kHgK3xppJ1YPwB9xgH2GoqhMSuQVpzDdvtJG', cb),
(cb) => expectKey(b, 'sha1', '5dsvLgRV9RVj9eSgtxMrXQjbpfFeHY', cb)
], done)
})

it('fail to create an empty block', () => {
expect(() => new Block()).to.throw()
})

it('2 different blocks have different hashes', () => {
it('2 different blocks have different hashes', (done) => {
const b1 = new Block('random-data')
const b2 = new Block('more-random-data')
expect(b1).to.not.deep.equal(b2)

parallel([
(cb) => b1.key(cb),
(cb) => b2.key(cb)
], (err, keys) => {
expect(err).to.not.exist
expect(keys[0]).to.not.deep.equal(keys[1])
done()
})
})

it.skip('block stays immutable', () => {
// 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).to.not.eql(block.key())
})
})

0 comments on commit 95877e6

Please sign in to comment.