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

Commit f31a236

Browse files
authored
Merge pull request #27 from ipfs/awesome-ipld
awesome IPLD endeavour
2 parents 52bbf1e + 7287621 commit f31a236

File tree

5 files changed

+198
-171
lines changed

5 files changed

+198
-171
lines changed

API.md

Lines changed: 0 additions & 67 deletions
This file was deleted.

README.md

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,17 @@ var repo = new IPFSRepo('example', { stores: Store })
7272
// create a block
7373
const block = new Block('hello world')
7474
console.log(block.data)
75-
console.log(block.key)
75+
console.log(block.key())
7676

7777
// create a service
7878
const bs = new BlockService(repo)
7979

8080
// add the block, then retrieve it
81-
bs.put(block, function (err) {
82-
bs.get(block.key, function (err, b) {
81+
bs.put({
82+
block: block,
83+
cid: cid,
84+
}, function (err) {
85+
bs.get(cid, function (err, b) {
8386
console.log(block.data.toString() === b.data.toString())
8487
})
8588
})
@@ -118,10 +121,70 @@ the global namespace.
118121
<script src="https://unpkg.com/ipfs-block-service/dist/index.js"></script>
119122
```
120123

121-
You can find the [API documentation here](API.md)
124+
# API
122125

123-
[ipfs]: https://ipfs.io
124-
[bitswap]: https://github.com/ipfs/specs/tree/master/bitswap
126+
```js
127+
const BlockService = require('ipfs-block-service')
128+
```
129+
130+
### `new BlockService(repo)`
131+
132+
- `repo: Repo`
133+
134+
Creates a new block service backed by [IPFS Repo][repo] `repo` for storage.
135+
136+
### `goOnline(bitswap)`
137+
138+
- `bitswap: Bitswap`
139+
140+
Add a bitswap instance that communicates with the network to retreive blocks
141+
that are not in the local store.
142+
143+
If the node is online all requests for blocks first check locally and
144+
afterwards ask the network for the blocks.
145+
146+
### `goOffline()`
147+
148+
Remove the bitswap instance and fall back to offline mode.
149+
150+
### `isOnline()`
151+
152+
Returns a `Boolean` indicating if the block service is online or not.
153+
154+
### `put(blockAndCID, callback)`
155+
156+
- `blockAndCID: { block: block, cid: cid }`
157+
- `callback: Function`
158+
159+
Asynchronously adds a block instance to the underlying repo.
160+
161+
### `putStream()`
162+
163+
Returns a through pull-stream, which `blockAndCID`s can be written to, and
164+
that emits the meta data about the written block.
165+
166+
### `get(cid [, extension], callback)`
167+
168+
- `cid: CID`
169+
- `extension: String`, defaults to 'data'
170+
- `callback: Function`
171+
172+
Asynchronously returns the block whose content multihash matches `multihash`.
173+
174+
### `getStream(cid [, extension])`
175+
176+
- `cid: CID`
177+
- `extension: String`, defaults to 'data'
178+
179+
Returns a source pull-stream, which emits the requested block.
180+
181+
### `delete(cids, [, extension], callback)`
182+
183+
- `cids: CID | []CID`
184+
- `extension: String`, defaults to 'data' - `extension: String`, defaults to 'data'
185+
- `callback: Function`
186+
187+
Deletes all blocks referenced by multihashes.
125188

126189
## Contribute
127190

@@ -134,3 +197,9 @@ This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/c
134197
## License
135198

136199
[MIT](LICENSE)
200+
201+
[ipfs]: https://ipfs.io
202+
[bitswap]: https://github.com/ipfs/specs/tree/master/bitswap
203+
[repo]: https://github.com/ipfs/specs/tree/master/repo
204+
205+

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,21 @@
3737
},
3838
"homepage": "https://github.com/ipfs/js-ipfs-block-service#readme",
3939
"devDependencies": {
40-
"aegir": "^8.0.0",
40+
"aegir": "^8.1.2",
4141
"buffer-loader": "0.0.1",
4242
"chai": "^3.5.0",
4343
"fs-pull-blob-store": "^0.3.0",
4444
"idb-pull-blob-store": "^0.5.1",
45-
"ipfs-block": "^0.3.0",
46-
"ipfs-repo": "^0.9.0",
45+
"ipfs-block": "^0.4.0",
46+
"ipfs-repo": "^0.10.0",
4747
"lodash": "^4.15.0",
4848
"ncp": "^2.0.0",
4949
"pre-commit": "^1.1.3",
5050
"rimraf": "^2.5.4",
5151
"run-series": "^1.1.4"
5252
},
5353
"dependencies": {
54+
"cids": "^0.2.0",
5455
"pull-stream": "^3.4.5",
5556
"run-parallel-limit": "^1.0.3"
5657
},
@@ -60,4 +61,4 @@
6061
"Richard Littauer <richard.littauer@gmail.com>",
6162
"Stephen Whitmore <stephen.whitmore@gmail.com>"
6263
]
63-
}
64+
}

src/index.js

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,73 @@ module.exports = class BlockService {
2424
return this._bitswap != null
2525
}
2626

27-
put (block, callback) {
27+
// Note: we have to pass the CID, so that bitswap can then use it for
28+
// the smart selectors. For now, passing the CID is used so that we know
29+
// the right multihash, this means that now we have multiple hashes official
30+
// support \o/
31+
put (blockAndCID, callback) {
2832
callback = callback || (() => {})
29-
if (!block) {
30-
return callback(new Error('Missing block'))
33+
if (!blockAndCID) {
34+
return callback(new Error('Missing block and CID'))
3135
}
3236

3337
pull(
34-
pull.values([block]),
38+
pull.values([
39+
blockAndCID
40+
]),
3541
this.putStream(),
3642
pull.onEnd(callback)
3743
)
3844
}
3945

4046
putStream () {
47+
let ps
4148
if (this.isOnline()) {
42-
return this._bitswap.putStream()
49+
// NOTE: This will have to change in order for bitswap
50+
// to understand CID
51+
ps = this._bitswap.putStream()
52+
} else {
53+
ps = this._repo.blockstore.putStream()
4354
}
4455

45-
return this._repo.blockstore.putStream()
56+
return pull(
57+
pull.map((blockAndCID) => {
58+
return {
59+
data: blockAndCID.block.data,
60+
key: blockAndCID.cid.multihash
61+
}
62+
}),
63+
ps
64+
)
4665
}
4766

48-
get (key, extension, callback) {
49-
if (typeof extension === 'function') {
50-
callback = extension
51-
extension = undefined
52-
}
53-
67+
get (cid, callback) {
5468
pull(
55-
this.getStream(key, extension),
69+
this.getStream(cid),
5670
pull.collect((err, result) => {
57-
if (err) return callback(err)
71+
if (err) {
72+
return callback(err)
73+
}
5874
callback(null, result[0])
5975
})
6076
)
6177
}
6278

63-
getStream (key, extension) {
79+
getStream (cid) {
6480
if (this.isOnline()) {
65-
return this._bitswap.getStream(key)
81+
return this._bitswap.getStream(cid.multihash)
6682
}
6783

68-
return this._repo.blockstore.getStream(key, extension)
84+
return this._repo.blockstore.getStream(cid.multihash)
6985
}
7086

71-
delete (keys, extension, callback) {
72-
if (typeof extension === 'function') {
73-
callback = extension
74-
extension = undefined
75-
}
76-
77-
if (!Array.isArray(keys)) {
78-
keys = [keys]
87+
delete (cids, callback) {
88+
if (!Array.isArray(cids)) {
89+
cids = [cids]
7990
}
8091

81-
parallelLimit(keys.map((key) => (next) => {
82-
this._repo.blockstore.delete(key, extension, next)
92+
parallelLimit(cids.map((cid) => (next) => {
93+
this._repo.blockstore.delete(cid.multihash, next)
8394
}), 100, callback)
8495
}
8596
}

0 commit comments

Comments
 (0)