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

Commit 0e1eaca

Browse files
refactor: Migrate to pull-streams
1 parent d3df0a0 commit 0e1eaca

File tree

43 files changed

+159
-213
lines changed

Some content is hidden

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

43 files changed

+159
-213
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ const BlockService = require('ipfs-block-service')
6464
const BlockService = require('ipfs-block-service')
6565
const Block = require('ipfs-block')
6666
const IPFSRepo = require('ipfs-repo') // storage repo
67-
const memstore = require('abstract-blob-store') // in-memory store
67+
const Store = require(interface-pull-blob-store') // in-memory store
6868
6969
// setup a repo
70-
var repo = new IPFSRepo('example', { stores: memstore })
70+
var repo = new IPFSRepo('example', { stores: Store })
7171
7272
// create a block
7373
const block = new Block('hello warld')

package.json

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,21 @@
3737
},
3838
"homepage": "https://github.com/ipfs/js-ipfs-block-service#readme",
3939
"devDependencies": {
40-
"aegir": "^3.0.0",
41-
"bs58": "^3.0.0",
40+
"aegir": "^6.0.1",
4241
"buffer-loader": "0.0.1",
4342
"chai": "^3.5.0",
44-
"fs-blob-store": "^5.2.1",
45-
"idb-plus-blob-store": "^1.1.2",
43+
"fs-pull-blob-store": "^0.3.0",
44+
"idb-pull-blob-store": "^0.4.0",
4645
"ipfs-block": "^0.3.0",
47-
"ipfs-repo": "^0.7.1",
48-
"lodash": "^4.8.2",
46+
"ipfs-repo": "^0.8.0",
47+
"lodash": "^4.15.0",
4948
"ncp": "^2.0.0",
50-
"pre-commit": "^1.1.2",
51-
"rimraf": "^2.5.1",
49+
"pre-commit": "^1.1.3",
50+
"rimraf": "^2.5.4",
5251
"run-series": "^1.1.4"
5352
},
5453
"dependencies": {
55-
"multihashes": "^0.2.2",
54+
"pull-stream": "^3.4.3",
5655
"run-parallel-limit": "^1.0.3"
5756
},
5857
"contributors": [

src/index.js

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const parallelLimit = require('run-parallel-limit')
4-
const mh = require('multihashes')
4+
const pull = require('pull-stream')
55

66
// BlockService is a hybrid block datastore. It stores data in a local
77
// datastore and may retrieve data from a remote Exchange.
@@ -24,72 +24,54 @@ module.exports = class BlockService {
2424
return this._bitswap != null
2525
}
2626

27-
addBlock (block, extension, callback) {
28-
if (this.isOnline()) {
29-
if (typeof extension === 'function') {
30-
callback = extension
31-
extension = undefined
32-
}
33-
34-
this._bitswap.hasBlock(block, callback)
35-
} else {
36-
this._repo.datastore.put(block, extension, callback)
37-
}
38-
}
39-
40-
addBlocks (blocks, callback) {
41-
if (!Array.isArray(blocks)) {
42-
return callback(new Error('expects an array of Blocks'))
27+
put (block, callback) {
28+
callback = callback || (() => {})
29+
if (!block) {
30+
return callback(new Error('Missing block'))
4331
}
4432

45-
parallelLimit(blocks.map((block) => (next) => {
46-
this.addBlock(block, next)
47-
}), 100, callback)
33+
pull(
34+
pull.values([block]),
35+
this.putStream(),
36+
pull.onEnd(callback)
37+
)
4838
}
4939

50-
getBlock (key, extension, callback) {
40+
putStream () {
5141
if (this.isOnline()) {
52-
if (typeof extension === 'function') {
53-
callback = extension
54-
extension = undefined
55-
}
56-
57-
this._bitswap.getBlock(key, callback)
58-
} else {
59-
this._repo.datastore.get(key, extension, callback)
42+
return this._bitswap.putStream()
6043
}
44+
45+
return this._repo.blockstore.putStream()
6146
}
6247

63-
getBlocks (multihashes, extension, callback) {
48+
get (key, extension, callback) {
6449
if (typeof extension === 'function') {
6550
callback = extension
6651
extension = undefined
6752
}
6853

69-
if (!Array.isArray(multihashes)) {
70-
return callback(new Error('Invalid batch of multihashes'))
71-
}
54+
callback = callback || (() => {})
7255

73-
if (this.isOnline()) {
74-
this._bitswap.getBlocks(multihashes, (results) => {
75-
callback(null, results)
56+
pull(
57+
this.getStream(key, extension),
58+
pull.collect((err, result) => {
59+
if (err) return callback(err)
60+
callback(null, result[0])
7661
})
77-
return
62+
)
63+
}
64+
65+
getStream (key, extension) {
66+
if (this.isOnline()) {
67+
return this._bitswap.getStream(key)
7868
}
7969

80-
const results = {}
81-
parallelLimit(multihashes.map((key) => (next) => {
82-
this._repo.datastore.get(key, extension, (error, block) => {
83-
results[mh.toB58String(key)] = {error, block}
84-
next()
85-
})
86-
}), 100, (err) => {
87-
callback(err, results)
88-
})
70+
return this._repo.blockstore.getStream(key, extension)
8971
}
9072

9173
deleteBlock (key, extension, callback) {
92-
this._repo.datastore.delete(key, extension, callback)
74+
this._repo.blockstore.delete(key, extension, callback)
9375
}
9476

9577
deleteBlocks (multihashes, extension, callback) {

0 commit comments

Comments
 (0)