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

Commit 6245e66

Browse files
committed
Merge pull request #5 from ipfs/ipld
Add extension support
2 parents c483206 + addf54b commit 6245e66

File tree

51 files changed

+288
-171
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+288
-171
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/repo-just-for-test*
1+
test/repo-just-for-test*
22

33
# Logs
44
logs

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/repo-just-for-test*
1+
test
22

33
# Logs
44
logs

README.md

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
IPFS Blocks JavaScript Implementation
22
=====================================
33

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

4141
## Use in Node.js
4242

43-
```JavaScript
44-
var ipfsBlocks = require('ipfs-blocks')
43+
```js
44+
const ipfsBlocks = require('ipfs-blocks')
4545
```
4646

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

6767

68-
```javascript
68+
```js
6969
// then, to access each of the components
7070
ipfsBlocks.BlockService
7171
ipfsBlocks.Block
@@ -75,45 +75,54 @@ ipfsBlocks.Block
7575

7676
Create a new block
7777

78-
```JavaScript
79-
var block = new blocks.Block('some data')
80-
console.log(block.data)
78+
```js
79+
const block = new blocks.Block('some data')
80+
console.log(block.data)
8181
// It will print 'some data'
82+
8283
console.log(block.key)
8384
// It will print the sha256 multihash of 'some data'
8485
```
8586

87+
A block can also have it's own extension, which by default is `data`.
88+
89+
```js
90+
const block = new blocks.Block('data', 'ipld')
91+
console.log(block.extension)
92+
// => ipld
93+
```
94+
8695
#### BlockService
8796

8897
Create a new block service
8998

90-
```JavaScript
91-
var bs = new ipfsBlocks.BlockService(<IPFS REPO instance> [, <IPFS Exchange>])
99+
```js
100+
const bs = new ipfsBlocks.BlockService(<IPFS REPO instance> [, <IPFS Exchange>])
92101
```
93102

94-
##### addBlock
103+
##### `addBlock`
95104

96-
```JavaScript
105+
```js
97106
bs.addBlock(block, function (err) {
98107
if (!err) {
99108
// block successfuly added
100109
}
101110
})
102111
```
103112

104-
##### addBlocks
113+
##### `addBlocks`
105114

106-
```JavaScript
115+
```js
107116
bs.addBlocks(blockArray, function (err) {
108117
if (!err) {
109118
// blocks successfuly added
110119
}
111120
})
112121
```
113122

114-
##### getBlock
123+
##### `getBlock`
115124

116-
```JavaScript
125+
```js
117126
bs.getBlock(multihash, function (err, block) {
118127
if (!err) {
119128
// block successfuly retrieved
@@ -122,29 +131,29 @@ bs.getBlock(multihash, function (err, block) {
122131
```
123132

124133

125-
##### getBlocks
134+
##### `getBlocks`
126135

127-
```JavaScript
136+
```js
128137
bs.getBlocks(multihashArray, function (err, block) {
129138
if (!err) {
130139
// block successfuly retrieved
131140
}
132141
})
133142
```
134143

135-
##### deleteBlock
144+
##### `deleteBlock`
136145

137-
```JavaScript
146+
```js
138147
bs.deleteBlock(multihash, function (err) {
139148
if (!err) {
140149
// block successfuly deleted
141150
}
142151
})
143152
```
144153

145-
##### deleteBlocks
154+
##### `deleteBlocks`
146155

147-
```JavaScript
156+
```js
148157
bs.deleteBlocks(multihashArray, function (err) {
149158
if (!err) {
150159
// blocks successfuly deleted

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@
3434
"homepage": "https://github.com/ipfs/js-ipfs-blocks#readme",
3535
"devDependencies": {
3636
"bs58": "^3.0.0",
37+
"buffer-loader": "0.0.1",
3738
"chai": "^3.5.0",
3839
"dignified.js": "^1.0.0",
3940
"fs-blob-store": "^5.2.1",
40-
"ipfs-repo": "^0.3.0",
41+
"idb-plus-blob-store": "^1.0.0",
42+
"ipfs-repo": "^0.6.1",
43+
"lodash": "^4.8.2",
4144
"ncp": "^2.0.0",
4245
"pre-commit": "^1.1.2",
4346
"rimraf": "^2.5.1"

src/block-service.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const async = require('async')
99
// It uses an internal `datastore.Datastore` instance to store values.
1010
function BlockService (ipfsRepo, exchange) {
1111
this.addBlock = (block, callback) => {
12-
const ws = ipfsRepo.datastore.createWriteStream(block.key)
12+
const ws = ipfsRepo.datastore.createWriteStream(block.key, block.extension)
1313
ws.write(block.data)
14-
ws.on('finish', callback)
14+
ws.once('finish', callback)
1515
ws.end()
1616
}
1717

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

2323
async.each(blocks, (block, next) => {
2424
this.addBlock(block, next)
25-
}, (err) => {
26-
callback(err)
27-
})
25+
}, callback)
2826
}
2927

30-
this.getBlock = (multihash, callback) => {
28+
this.getBlock = (multihash, extension, callback) => {
3129
if (!multihash) {
3230
return callback(new Error('Invalid multihash'))
3331
}
3432

35-
ipfsRepo.datastore.createReadStream(multihash)
33+
if (typeof extension === 'function') {
34+
callback = extension
35+
extension = undefined
36+
}
37+
38+
ipfsRepo.datastore.createReadStream(multihash, extension)
3639
.pipe(bl((err, data) => {
3740
if (err) { return callback(err) }
38-
callback(null, new Block(data))
41+
callback(null, new Block(data, extension))
3942
}))
4043
}
4144

42-
this.getBlocks = (multihashes, callback) => {
45+
this.getBlocks = (multihashes, extension, callback) => {
4346
if (!Array.isArray(multihashes)) {
4447
return callback(new Error('Invalid batch of multihashes'))
4548
}
4649

50+
if (typeof extension === 'function') {
51+
callback = extension
52+
extension = undefined
53+
}
54+
4755
const blocks = []
4856

4957
async.each(multihashes, (multihash, next) => {
50-
this.getBlock(multihash, (err, block) => {
58+
this.getBlock(multihash, extension, (err, block) => {
5159
if (err) { return next(err) }
5260
blocks.push(block)
5361
})
@@ -56,21 +64,31 @@ function BlockService (ipfsRepo, exchange) {
5664
})
5765
}
5866

59-
this.deleteBlock = (multihash, callback) => {
67+
this.deleteBlock = (multihash, extension, callback) => {
6068
if (!multihash) {
6169
return callback(new Error('Invalid multihash'))
6270
}
6371

64-
ipfsRepo.datastore.remove(multihash, callback)
72+
if (typeof extension === 'function') {
73+
callback = extension
74+
extension = undefined
75+
}
76+
77+
ipfsRepo.datastore.remove(multihash, extension, callback)
6578
}
6679

67-
this.deleteBlocks = (multihashes, callback) => {
80+
this.deleteBlocks = (multihashes, extension, callback) => {
6881
if (!Array.isArray(multihashes)) {
6982
return callback('Invalid batch of multihashes')
7083
}
7184

85+
if (typeof extension === 'function') {
86+
callback = extension
87+
extension = undefined
88+
}
89+
7290
async.each(multihashes, (multihash, next) => {
73-
this.deleteBlock(multihash, next)
91+
this.deleteBlock(multihash, extension, next)
7492
}, (err) => {
7593
callback(err)
7694
})

src/block.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,36 @@
22
const util = require('./util')
33

44
// Immutable block of data
5-
function Block (data) {
6-
if (!data) { throw new Error('Block must be constructed with data') }
5+
function Block (data, type) {
6+
if (!data) {
7+
throw new Error('Block must be constructed with data')
8+
}
79

810
if (!(this instanceof Block)) {
911
return new Block(data)
1012
}
1113

12-
this.data = new Buffer(data)
14+
if (data instanceof Buffer) {
15+
this.data = data
16+
} else {
17+
this.data = new Buffer(data)
18+
}
19+
1320
this.key = util.hash(this.data)
21+
this.type = type || 'protobuf'
1422
}
1523

24+
Object.defineProperty(Block.prototype, 'extension', {
25+
get () {
26+
switch (this.type) {
27+
case 'protobuf':
28+
return 'data'
29+
case 'ipld':
30+
return 'ipld'
31+
default:
32+
return this.type
33+
}
34+
}
35+
})
36+
1637
module.exports = Block

0 commit comments

Comments
 (0)