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

Add extension support #5

Merged
merged 4 commits into from
Apr 11, 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/repo-just-for-test*
test/repo-just-for-test*

# Logs
logs
Expand Down
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/repo-just-for-test*
test

# Logs
logs
Expand Down
51 changes: 30 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
IPFS Blocks JavaScript Implementation
=====================================

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Travis CI](https://travis-ci.org/ipfs/js-ipfs-blocks.svg?branch=master)](https://travis-ci.org/ipfs/js-ipfs-blocks)
Expand Down Expand Up @@ -40,8 +40,8 @@ A Block is a data structure available on this module.

## Use in Node.js

```JavaScript
var ipfsBlocks = require('ipfs-blocks')
```js
const ipfsBlocks = require('ipfs-blocks')
```

## Use in a browser with browserify, webpack or any other bundler
Expand All @@ -65,7 +65,7 @@ Loading this module through a script tag will make the `Unixfs` obj available in
# Usage


```javascript
```js
// then, to access each of the components
ipfsBlocks.BlockService
ipfsBlocks.Block
Expand All @@ -75,45 +75,54 @@ ipfsBlocks.Block

Create a new block

```JavaScript
var block = new blocks.Block('some data')
console.log(block.data)
```js
const block = new blocks.Block('some data')
console.log(block.data)
// It will print 'some data'

console.log(block.key)
// It will print the sha256 multihash of 'some data'
```

A block can also have it's own extension, which by default is `data`.

```js
const block = new blocks.Block('data', 'ipld')
console.log(block.extension)
// => ipld
```

#### BlockService

Create a new block service

```JavaScript
var bs = new ipfsBlocks.BlockService(<IPFS REPO instance> [, <IPFS Exchange>])
```js
const bs = new ipfsBlocks.BlockService(<IPFS REPO instance> [, <IPFS Exchange>])
```

##### addBlock
##### `addBlock`

```JavaScript
```js
bs.addBlock(block, function (err) {
if (!err) {
// block successfuly added
}
})
```

##### addBlocks
##### `addBlocks`

```JavaScript
```js
bs.addBlocks(blockArray, function (err) {
if (!err) {
// blocks successfuly added
}
})
```

##### getBlock
##### `getBlock`

```JavaScript
```js
bs.getBlock(multihash, function (err, block) {
if (!err) {
// block successfuly retrieved
Expand All @@ -122,29 +131,29 @@ bs.getBlock(multihash, function (err, block) {
```


##### getBlocks
##### `getBlocks`

```JavaScript
```js
bs.getBlocks(multihashArray, function (err, block) {
if (!err) {
// block successfuly retrieved
}
})
```

##### deleteBlock
##### `deleteBlock`

```JavaScript
```js
bs.deleteBlock(multihash, function (err) {
if (!err) {
// block successfuly deleted
}
})
```

##### deleteBlocks
##### `deleteBlocks`

```JavaScript
```js
Copy link
Member

Choose a reason for hiding this comment

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

master nicpicker :P

bs.deleteBlocks(multihashArray, function (err) {
if (!err) {
// blocks successfuly deleted
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@
"homepage": "https://github.com/ipfs/js-ipfs-blocks#readme",
"devDependencies": {
"bs58": "^3.0.0",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"dignified.js": "^1.0.0",
"fs-blob-store": "^5.2.1",
"ipfs-repo": "^0.3.0",
"idb-plus-blob-store": "^1.0.0",
"ipfs-repo": "^0.6.1",
"lodash": "^4.8.2",
"ncp": "^2.0.0",
"pre-commit": "^1.1.2",
"rimraf": "^2.5.1"
Expand Down
46 changes: 32 additions & 14 deletions src/block-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const async = require('async')
// It uses an internal `datastore.Datastore` instance to store values.
function BlockService (ipfsRepo, exchange) {
this.addBlock = (block, callback) => {
const ws = ipfsRepo.datastore.createWriteStream(block.key)
const ws = ipfsRepo.datastore.createWriteStream(block.key, block.extension)
ws.write(block.data)
ws.on('finish', callback)
ws.once('finish', callback)
ws.end()
}

Expand All @@ -22,32 +22,40 @@ function BlockService (ipfsRepo, exchange) {

async.each(blocks, (block, next) => {
this.addBlock(block, next)
}, (err) => {
callback(err)
})
}, callback)
}

this.getBlock = (multihash, callback) => {
this.getBlock = (multihash, extension, callback) => {
if (!multihash) {
return callback(new Error('Invalid multihash'))
}

ipfsRepo.datastore.createReadStream(multihash)
if (typeof extension === 'function') {
callback = extension
extension = undefined
}

ipfsRepo.datastore.createReadStream(multihash, extension)
.pipe(bl((err, data) => {
if (err) { return callback(err) }
callback(null, new Block(data))
callback(null, new Block(data, extension))
}))
}

this.getBlocks = (multihashes, callback) => {
this.getBlocks = (multihashes, extension, callback) => {
if (!Array.isArray(multihashes)) {
return callback(new Error('Invalid batch of multihashes'))
}

if (typeof extension === 'function') {
callback = extension
extension = undefined
}

const blocks = []

async.each(multihashes, (multihash, next) => {
this.getBlock(multihash, (err, block) => {
this.getBlock(multihash, extension, (err, block) => {
if (err) { return next(err) }
blocks.push(block)
})
Expand All @@ -56,21 +64,31 @@ function BlockService (ipfsRepo, exchange) {
})
}

this.deleteBlock = (multihash, callback) => {
this.deleteBlock = (multihash, extension, callback) => {
if (!multihash) {
return callback(new Error('Invalid multihash'))
}

ipfsRepo.datastore.remove(multihash, callback)
if (typeof extension === 'function') {
callback = extension
extension = undefined
}

ipfsRepo.datastore.remove(multihash, extension, callback)
}

this.deleteBlocks = (multihashes, callback) => {
this.deleteBlocks = (multihashes, extension, callback) => {
if (!Array.isArray(multihashes)) {
return callback('Invalid batch of multihashes')
}

if (typeof extension === 'function') {
callback = extension
extension = undefined
}

async.each(multihashes, (multihash, next) => {
this.deleteBlock(multihash, next)
this.deleteBlock(multihash, extension, next)
}, (err) => {
callback(err)
})
Expand Down
27 changes: 24 additions & 3 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,36 @@
const util = require('./util')

// Immutable block of data
function Block (data) {
if (!data) { throw new Error('Block must be constructed with data') }
function Block (data, type) {
if (!data) {
throw new Error('Block must be constructed with data')
}

if (!(this instanceof Block)) {
return new Block(data)
}

this.data = new Buffer(data)
if (data instanceof Buffer) {
this.data = data
} else {
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
}
}
})
Copy link
Member

Choose a reason for hiding this comment

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

nice way to do it :)


module.exports = Block
Loading