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

Async Crypto Endeavour #4

Merged
merged 8 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ node_modules
# Optional REPL history
.node_repl_history

lib
dist
17 changes: 13 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ sudo: false
language: node_js
node_js:
- 4
- 5
- 6
- stable

# Make sure we have new NPM.
before_install:
Expand All @@ -13,12 +14,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
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
[![Coverage Status](https://coveralls.io/repos/github/ipfs/js-ipfs-block/badge.svg?branch=master)](https://coveralls.io/github/ipfs/js-ipfs-block?branch=master)
[![Dependency Status](https://david-dm.org/ipfs/js-ipfs-block.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-block)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)

[![Sauce Test Status](https://saucelabs.com/browser-matrix/js-ipfs-block.svg)](https://saucelabs.com/u/js-ipf
s-block)

> [IPFS][ipfs] implementation of the Block data structure in JavaScript.

Expand All @@ -25,10 +30,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/hashFn/hashAlg

- [Contribute](#contribute)
- [License](#license)

Expand Down Expand Up @@ -56,7 +60,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 +71,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 +93,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)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/hashFn/hashAlg


### `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
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "ipfs-block",
"version": "0.4.0",
"description": "JavaScript Implementation of IPFS Block",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"main": "src/index.js",
"scripts": {
"lint": "aegir-lint",
"build": "aegir-build",
Expand Down Expand Up @@ -37,12 +36,16 @@
},
"homepage": "https://github.com/ipfs/js-ipfs-block#readme",
"devDependencies": {
"aegir": "^8.0.0",
"aegir": "^9.0.0",
"async": "^2.0.1",
"chai": "^3.5.0",
"multihashes": "^0.2.2"
},
"dependencies": {
"multihashing": "^0.2.0"
"multihashing-async": "^0.1.0"
},
"engines": {
"node": ">=4.0.0"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand All @@ -51,4 +54,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>"
]
}
}
34 changes: 22 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
'use strict'

const multihashing = require('multihashing')
const multihashing = require('multihashing-async')

function Block (data) {
if (!data) {
throw new Error('Block must be constructed with data')
}
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 = (hashFunc) => {
this.data = ensureBuffer(data)

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

if (!hashFunc) {
hashFunc = 'sha2-256'
}

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

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

return new Buffer(data)
}
50 changes: 41 additions & 9 deletions test/block.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,64 @@
'use strict'

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

const Block = require('../src')
const multihash = 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(multihash.toB58String(b.key())).to.equal('QmeoBGh5g5kHgK3xppJ1YPwB9xgH2GoqhMSuQVpzDdvtJG')
expect(multihash.toB58String(b.key('sha1'))).to.equal('5dsvLgRV9RVj9eSgtxMrXQjbpfFeHY')
expect(b.data).to.exist
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(multihash.toB58String(b.key())).to.equal('QmeoBGh5g5kHgK3xppJ1YPwB9xgH2GoqhMSuQVpzDdvtJG')
expect(multihash.toB58String(b.key('sha1'))).to.equal('5dsvLgRV9RVj9eSgtxMrXQjbpfFeHY')
expect(b.data).to.exist
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', () => {
Expand Down