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

Commit 099080c

Browse files
dignifiedquiredaviddias
authored andcommitted
feat: refactor for new ipfs-repo interface
1 parent 1b13724 commit 099080c

File tree

41 files changed

+271
-409
lines changed

Some content is hidden

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

41 files changed

+271
-409
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ node_modules
3535
.node_repl_history
3636

3737
dist
38+
docs

README.md

Lines changed: 29 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ backed by an [IPFS Repo][repo] as its datastore for blocks, and uses [Bitswap][b
2525
┌────────────────────┐
2626
│ BlockService │
2727
└────────────────────┘
28-
2928
┌─────┴─────┐
3029
▼ ▼
3130
┌─────────┐ ┌───────┐
@@ -42,6 +41,7 @@ backed by an [IPFS Repo][repo] as its datastore for blocks, and uses [Bitswap][b
4241
- [Example](#example)
4342
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
4443
- [Browser: `<script>` Tag](#browser-script-tag)
44+
- [API](#api)
4545
- [Contribute](#contribute)
4646
- [License](#license)
4747

@@ -50,7 +50,7 @@ backed by an [IPFS Repo][repo] as its datastore for blocks, and uses [Bitswap][b
5050
### npm
5151

5252
```sh
53-
> npm i ipfs-block-service
53+
> npm install ipfs-block-service
5454
```
5555

5656
## Usage
@@ -68,48 +68,41 @@ const BlockService = require('ipfs-block-service')
6868
const BlockService = require('ipfs-block-service')
6969
const Block = require('ipfs-block')
7070
const IPFSRepo = require('ipfs-repo') // storage repo
71-
const Store = require('interface-pull-blob-store') // in-memory store
7271

7372
// setup a repo
74-
var repo = new IPFSRepo('example', { stores: Store })
73+
const repo = new IPFSRepo('example')
7574

7675
// create a block
77-
const block = new Block('hello world')
78-
console.log(block.data)
79-
console.log(block.key())
80-
81-
// create a service
82-
const bs = new BlockService(repo)
83-
84-
// add the block, then retrieve it
85-
bs.put({
86-
block: block,
87-
cid: cid,
88-
}, function (err) {
89-
bs.get(cid, function (err, b) {
90-
console.log(block.data.toString() === b.data.toString())
76+
const data = new Buffer('hello world')
77+
multihashing(data, 'sha2-256', (err, multihash) => {
78+
if (err) {
79+
throw err
80+
}
81+
82+
const cid = new CID(multihash)
83+
const block = new Block(data, cid)
84+
85+
// create a service
86+
const bs = new BlockService(repo)
87+
88+
// add the block, then retrieve it
89+
bs.put(block, (err) => {
90+
if (err) {
91+
throw err
92+
}
93+
bs.get(cid, (err, b) => {
94+
if (err) {
95+
throw err
96+
}
97+
console.log(block.data.toString() === b.data.toString())
98+
// => true
99+
})
91100
})
92101
})
93102
```
94103

95-
outputs
96-
97-
```
98-
<Buffer 68 65 6c 6c 6f 20 77 61 72 6c 64>
99-
100-
<Buffer 12 20 db 3c 15 23 3f f3 84 8f 42 fe 3b 74 78 90 90 5a 80 7e a6 ef 2b 6d 2f 3c 8b 2c b7 ae be 86 3c 4d>
101-
102-
true
103-
104-
```
105-
106104
### Browser: Browserify, Webpack, other bundlers
107105

108-
The code published to npm that gets loaded on require is in fact a ES5
109-
transpiled version with the right shims added. This means that you can require
110-
it and use with your favourite bundler without having to adjust asset management
111-
process.
112-
113106
```JavaScript
114107
var BlockService = require('ipfs-block-service')
115108
```
@@ -125,70 +118,9 @@ the global namespace.
125118
<script src="https://unpkg.com/ipfs-block-service/dist/index.js"></script>
126119
```
127120

128-
# API
129-
130-
```js
131-
const BlockService = require('ipfs-block-service')
132-
```
133-
134-
### `new BlockService(repo)`
135-
136-
- `repo: Repo`
137-
138-
Creates a new block service backed by [IPFS Repo][repo] `repo` for storage.
139-
140-
### `goOnline(bitswap)`
141-
142-
- `bitswap: Bitswap`
143-
144-
Add a bitswap instance that communicates with the network to retreive blocks
145-
that are not in the local store.
146-
147-
If the node is online all requests for blocks first check locally and
148-
afterwards ask the network for the blocks.
121+
## API
149122

150-
### `goOffline()`
151-
152-
Remove the bitswap instance and fall back to offline mode.
153-
154-
### `isOnline()`
155-
156-
Returns a `Boolean` indicating if the block service is online or not.
157-
158-
### `put(blockAndCID, callback)`
159-
160-
- `blockAndCID: { block: block, cid: cid }`
161-
- `callback: Function`
162-
163-
Asynchronously adds a block instance to the underlying repo.
164-
165-
### `putStream()`
166-
167-
Returns a through pull-stream, which `blockAndCID`s can be written to, and
168-
that emits the meta data about the written block.
169-
170-
### `get(cid [, extension], callback)`
171-
172-
- `cid: CID`
173-
- `extension: String`, defaults to 'data'
174-
- `callback: Function`
175-
176-
Asynchronously returns the block whose content multihash matches `multihash`.
177-
178-
### `getStream(cid [, extension])`
179-
180-
- `cid: CID`
181-
- `extension: String`, defaults to 'data'
182-
183-
Returns a source pull-stream, which emits the requested block.
184-
185-
### `delete(cids, [, extension], callback)`
186-
187-
- `cids: CID | []CID`
188-
- `extension: String`, defaults to 'data' - `extension: String`, defaults to 'data'
189-
- `callback: Function`
190-
191-
Deletes all blocks referenced by multihashes.
123+
See https://ipfs.github.io/js-ipfs-block-service
192124

193125
## Contribute
194126

@@ -205,5 +137,3 @@ This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/c
205137
[ipfs]: https://ipfs.io
206138
[bitswap]: https://github.com/ipfs/specs/tree/master/bitswap
207139
[repo]: https://github.com/ipfs/specs/tree/master/repo
208-
209-

package.json

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
"test": "aegir-test",
1010
"test:node": "aegir-test node",
1111
"test:browser": "aegir-test browser",
12-
"release": "aegir-release",
13-
"release-minor": "aegir-release --type minor",
14-
"release-major": "aegir-release --type major",
12+
"release": "aegir-release --docs",
13+
"release-minor": "aegir-release --type minor --docs",
14+
"release-major": "aegir-release --type major --docs",
1515
"coverage": "aegir-coverage",
16-
"coverage-publish": "aegir-coverage publish"
16+
"coverage-publish": "aegir-coverage publish",
17+
"docs": "aegir-docs"
1718
},
1819
"pre-commit": [
1920
"lint",
@@ -36,25 +37,23 @@
3637
},
3738
"homepage": "https://github.com/ipfs/js-ipfs-block-service#readme",
3839
"devDependencies": {
39-
"aegir": "^10.0.0",
40-
"buffer-loader": "0.0.1",
40+
"aegir": "^11.0.0",
4141
"chai": "^3.5.0",
42-
"fs-pull-blob-store": "~0.4.1",
43-
"idb-pull-blob-store": "~0.5.1",
44-
"ipfs-block": "~0.5.5",
45-
"ipfs-repo": "~0.11.3",
42+
"cids": "^0.4.2",
43+
"dirty-chai": "^1.2.2",
44+
"ipfs-block": "~0.6.0",
45+
"ipfs-repo": "~0.12.0",
4646
"lodash": "^4.17.4",
47+
"multihashing-async": "^0.4.4",
4748
"ncp": "^2.0.0",
4849
"pre-commit": "^1.2.2",
49-
"rimraf": "^2.5.4"
50+
"rimraf": "^2.6.1"
5051
},
5152
"engines": {
52-
"node": ">=4.0.0"
53-
},
54-
"dependencies": {
55-
"async": "^2.1.4",
56-
"cids": "~0.4.1"
53+
"node": ">=4.0.0",
54+
"npm": ">=3.0.0"
5755
},
56+
"dependencies": {},
5857
"contributors": [
5958
"David Dias <[email protected]>",
6059
"Friedel Ziegelmayer <[email protected]>",
@@ -64,4 +63,4 @@
6463
"npmcdn-to-unpkg-bot <[email protected]>",
6564
"wanderer <[email protected]>"
6665
]
67-
}
66+
}

0 commit comments

Comments
 (0)