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

Commit

Permalink
feat: refactor for new ipfs-repo interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and daviddias committed Mar 21, 2017
1 parent 1b13724 commit 7eff97f
Show file tree
Hide file tree
Showing 41 changed files with 272 additions and 409 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ node_modules
.node_repl_history

dist
docs
128 changes: 29 additions & 99 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ backed by an [IPFS Repo][repo] as its datastore for blocks, and uses [Bitswap][b
┌────────────────────┐
│ BlockService │
└────────────────────┘
┌─────┴─────┐
▼ ▼
┌─────────┐ ┌───────┐
Expand All @@ -42,6 +41,7 @@ backed by an [IPFS Repo][repo] as its datastore for blocks, and uses [Bitswap][b
- [Example](#example)
- [Browser: Browserify, Webpack, other bundlers](#browser-browserify-webpack-other-bundlers)
- [Browser: `<script>` Tag](#browser-script-tag)
- [API](#api)
- [Contribute](#contribute)
- [License](#license)

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

```sh
> npm i ipfs-block-service
> npm install ipfs-block-service
```

## Usage
Expand All @@ -68,48 +68,41 @@ const BlockService = require('ipfs-block-service')
const BlockService = require('ipfs-block-service')
const Block = require('ipfs-block')
const IPFSRepo = require('ipfs-repo') // storage repo
const Store = require('interface-pull-blob-store') // in-memory store

// setup a repo
var repo = new IPFSRepo('example', { stores: Store })
const repo = new IPFSRepo('example')

// create a block
const block = new Block('hello world')
console.log(block.data)
console.log(block.key())

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

// add the block, then retrieve it
bs.put({
block: block,
cid: cid,
}, function (err) {
bs.get(cid, function (err, b) {
console.log(block.data.toString() === b.data.toString())
const data = new Buffer('hello world')
multihashing(data, 'sha2-256', (err, multihash) => {
if (err) {
throw err
}

const cid = new CID(multihash)
const block = new Block(data, cid)

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

// add the block, then retrieve it
bs.put(block, (err) => {
if (err) {
throw err
}
bs.get(cid, (err, b) => {
if (err) {
throw err
}
console.log(block.data.toString() === b.data.toString())
// => true
})
})
})
```

outputs

```
<Buffer 68 65 6c 6c 6f 20 77 61 72 6c 64>
<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>
true
```

### Browser: Browserify, Webpack, other bundlers

The code published to npm that gets loaded on require is in fact a ES5
transpiled version with the right shims added. This means that you can require
it and use with your favourite bundler without having to adjust asset management
process.

```JavaScript
var BlockService = require('ipfs-block-service')
```
Expand All @@ -125,70 +118,9 @@ the global namespace.
<script src="https://unpkg.com/ipfs-block-service/dist/index.js"></script>
```

# API

```js
const BlockService = require('ipfs-block-service')
```

### `new BlockService(repo)`

- `repo: Repo`

Creates a new block service backed by [IPFS Repo][repo] `repo` for storage.

### `goOnline(bitswap)`

- `bitswap: Bitswap`

Add a bitswap instance that communicates with the network to retreive blocks
that are not in the local store.

If the node is online all requests for blocks first check locally and
afterwards ask the network for the blocks.
## API

### `goOffline()`

Remove the bitswap instance and fall back to offline mode.

### `isOnline()`

Returns a `Boolean` indicating if the block service is online or not.

### `put(blockAndCID, callback)`

- `blockAndCID: { block: block, cid: cid }`
- `callback: Function`

Asynchronously adds a block instance to the underlying repo.

### `putStream()`

Returns a through pull-stream, which `blockAndCID`s can be written to, and
that emits the meta data about the written block.

### `get(cid [, extension], callback)`

- `cid: CID`
- `extension: String`, defaults to 'data'
- `callback: Function`

Asynchronously returns the block whose content multihash matches `multihash`.

### `getStream(cid [, extension])`

- `cid: CID`
- `extension: String`, defaults to 'data'

Returns a source pull-stream, which emits the requested block.

### `delete(cids, [, extension], callback)`

- `cids: CID | []CID`
- `extension: String`, defaults to 'data' - `extension: String`, defaults to 'data'
- `callback: Function`

Deletes all blocks referenced by multihashes.
See https://ipfs.github.io/js-ipfs-block-service

## Contribute

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


34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"test": "aegir-test",
"test:node": "aegir-test node",
"test:browser": "aegir-test browser",
"release": "aegir-release",
"release-minor": "aegir-release --type minor",
"release-major": "aegir-release --type major",
"release": "aegir-release --docs",
"release-minor": "aegir-release --type minor --docs",
"release-major": "aegir-release --type major --docs",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish"
"coverage-publish": "aegir-coverage publish",
"docs": "aegir-docs"
},
"pre-commit": [
"lint",
Expand All @@ -36,25 +37,24 @@
},
"homepage": "https://github.com/ipfs/js-ipfs-block-service#readme",
"devDependencies": {
"aegir": "^10.0.0",
"buffer-loader": "0.0.1",
"aegir": "^11.0.0",
"async": "^2.1.5",
"chai": "^3.5.0",
"fs-pull-blob-store": "~0.4.1",
"idb-pull-blob-store": "~0.5.1",
"ipfs-block": "~0.5.5",
"ipfs-repo": "~0.11.3",
"cids": "^0.4.2",
"dirty-chai": "^1.2.2",
"ipfs-block": "~0.6.0",
"ipfs-repo": "~0.12.0",
"lodash": "^4.17.4",
"multihashing-async": "^0.4.4",
"ncp": "^2.0.0",
"pre-commit": "^1.2.2",
"rimraf": "^2.5.4"
"rimraf": "^2.6.1"
},
"engines": {
"node": ">=4.0.0"
},
"dependencies": {
"async": "^2.1.4",
"cids": "~0.4.1"
"node": ">=4.0.0",
"npm": ">=3.0.0"
},
"dependencies": {},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
Expand All @@ -64,4 +64,4 @@
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>",
"wanderer <mjbecze@gmail.com>"
]
}
}
Loading

0 comments on commit 7eff97f

Please sign in to comment.