Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

[WIP] putStream & getStream #60

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions API/block/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
block API
=========

#### `get`

> Get a raw IPFS block.

##### `Go` **WIP**

##### `JavaScript` - ipfs.block.get(multihash, [options, callback])

`multihash` is a [multihash][multihash] which can be passed as:

- Buffer, the raw Buffer of the multihash
- String, the base58 encoded version of the multihash

`callback` must follow `function (err, block) {}` signature, where `err` is an error if the operation was not successful and `block` is a [Block][block] type object, containing both the data and the hash of the block.

```js
ipfs.block.get(multihash, function (err, block) {
if (err) {
throw err
}
console.log(block.key, block.data)
})
```

If no `callback` is passed, a promise is returned.


#### `getStream`

> Get a raw IPFS block as a stream.

##### `Go` **WIP**

##### `JavaScript` - ipfs.block.getStream(multihash, [options])

`multihash` is a [multihash][multihash] which can be passed as:

- Buffer, the raw Buffer of the multihash
- String, the base58 encoded version of the multihash

Returns a readable [pull-stream](pull-stream) of [Block][block]s

```js
pull(
ipfs.block.get(multihash)
pull.log()
)
```

#### `put`

> Stores input as an IPFS block.

##### `Go` **WIP**

##### `JavaScript` - ipfs.block.put(block, [callback])

Where `block` can be:

- `Buffer` - the raw bytes of the Block
- [`Block`][block] instance

`callback` has the signature `function (err, block) {}`, where `err` is an error if the operation was not successful and `block` is a [Block][block] type object, containing both the data and the hash of the block.

If no `callback` is passed, a promise is returned.


#### `putStream`

> Stores input as an IPFS block.

##### `Go` **WIP**

##### `JavaScript` - ipfs.block.putStream()

Returns a duplex [pull-stream](pull-stream).

```js
const b1 = new Block('hello')
const b2 = new Block('world')

pull(
pull.values([b1, bw]),
ipfs.block.putStream()
)
```

#### `stat`

> Print information of a raw IPFS block.

##### `Go` **WIP**

##### `JavaScript` - ipfs.block.stat(multihash, [callback])

`multihash` is a [multihash][multihash] which can be passed as:

- `Buffer`, the raw Buffer of the multihash (or of and encoded version)
- `String`, the toString version of the multihash (or of an encoded version)

`callback` must follow the signature `function (err, stats) {}`, where `err` is an error if the operation was not successful and `stats` is an object with the format:`

```JavaScript
{
Key: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD',
Size: 10
}
```

If no `callback` is passed, a promise is returned.

[block](https://github.com/ipfs/js-ipfs-block)
[multihash](https://github.com/multiformats/multihash)
[pull-stream](https://pull-stream.github.io)
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "interface-ipfs-core",
"version": "0.11.0",
"description": "A test suite and interface you can use to implement a IPFS core interface.",
"main": "lib/index.js",
"main": "src/index.js",
"jsnext:main": "src/index.js",
"scripts": {
"test": "exit(0)",
Expand Down Expand Up @@ -34,6 +34,7 @@
"concat-stream": "^1.5.1",
"detect-node": "^2.0.3",
"ipfs-merkle-dag": "^0.6.2",
"pull-stream": "^3.4.3",
"readable-stream": "1.1.13"
},
"devDependencies": {
Expand All @@ -47,4 +48,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"nginnever <ginneversource@gmail.com>"
]
}
}
128 changes: 128 additions & 0 deletions src/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */

'use strict'

const expect = require('chai').expect
const pull = require('pull-stream')

module.exports = (common) => {
describe('.block', () => {
let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon,
// so we need to increase the timeout for the
// before step
this.timeout(20 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist
factory.spawnNode((err, node) => {
expect(err).to.not.exist
ipfs = node
done()
})
})
})

after((done) => {
common.teardown(done)
})

describe('callback API', () => {
it('.put', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const blob = Buffer('blorb')

ipfs.block.put(blob, (err, res) => {
expect(err).to.not.exist
expect(res).to.have.a.property('Key', expectedHash)
done()
})
})

it('.put error with array of blocks', () => {
const blob = Buffer('blorb')

return ipfs.block.put([blob, blob], (err) => {
expect(err).to.be.an.instanceof(Error)
})
})

it('block.get', (done) => {
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'

ipfs.block.get(hash, (err, res) => {
expect(err).to.not.exist
expect(res.data).to.be.eql(Buffer('blorb'))
done()
})
})

it('block.stat', (done) => {
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'

ipfs.block.stat(hash, (err, res) => {
expect(err).to.not.exist
expect(res).to.have.property('Key')
expect(res).to.have.property('Size')
done()
})
})
})

describe('promise API', () => {
})

describe('stream API', () => {
it('.putStream', (done) => {
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
const blob = Buffer('blorb')

pull(
pull.values([blob]),
ipfs.block.putStream(),
pull.collect((err, res) => {
expect(err).to.not.exist

expect(res).to.have.length(1)
expect(res[0]).to.have.a.property('Key', expectedHash)
done()
})
)
})

it('.putStream multiple blocks', (done) => {
const blobs = [
Buffer('blorb'),
Buffer('borb'),
Buffer('barb')
]

pull(
pull.values(blobs),
ipfs.block.putStream(),
pull.collect((err, res) => {
expect(err).to.not.exist
expect(res).to.have.length(3)
done()
})
)
})

it('.getStream', (done) => {
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'

pull(
ipfs.block.getStream(hash),
pull.collect((err, res) => {
expect(err).to.not.exist
expect(res[0].data).to.be.eql(Buffer('blorb'))
done()
})
)
})
})
})
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ exports.files = require('./files')
exports.config = require('./config')
exports.pin = require('./pin')
exports.generic = require('./generic')
exports.block = require('./block')