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

Commit

Permalink
refactor: make blockService an option parameter
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

The IPLD constructor is no longer taking a BlockService as its
only parameter, but an objects object with `blockService` as a
key.

You need to upgrade your code if you initialize IPLD.

Prior to this change:

```js
const ipld = new Ipld(blockService)
```

Now:

```js
const ipld = new Ipld({blockService: blockService})
```
  • Loading branch information
vmx committed Oct 18, 2018
1 parent ed301c9 commit f1c2e12
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 19 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Want to get started? Check our examples folder. You can check the development st
- [Usage](#usage)
- [API](#api)
- IPLD Resolver
- [Constructor](#ipld-constructor)
- [`.put(node, options, callback)`](#putnode-options-callback)
- [`.get(cid [, path] [, options], callback)`](#getcid--path--options-callback)
- [`.getStream(cid [, path] [, options])`](#getstreamcid--path--options)
Expand Down Expand Up @@ -78,7 +79,7 @@ const initIpld = (ipfsRepoPath, callback) => {
return callback(err)
}
const blockService = new IpfsBlockService(repo)
const ipld = new Ipld(blockService)
const ipld = new Ipld({blockService: blockService})
return callback(null, ipld)
})
})
Expand All @@ -91,6 +92,29 @@ initIpld('/tmp/ifpsrepo', (err, ipld) => {

## API

### IPLD constructor

> Creates and returns an instance of IPLD.
```js
const ipld = new Ipld(options)
```

The `options` is an object with any of these properties:

##### `options.blockService`

| Type | Default |
|------|---------|
| [`ipfs.BlockService`](https://github.com/ipfs/js-ipfs-block-service) instance | Required (no default) |

Example:

```js
const blockService = new IpfsBlockService(repo)
const ipld = new Ipld({blockService: blockService})
```

### `.put(node, options, callback)`

> Store the given node of a recognized IPLD Format.
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ const MemoryStore = require('interface-datastore').MemoryDatastore
function noop () {}

class IPLDResolver {
constructor (blockService) {
if (!blockService) {
constructor (options) {
if (!options.blockService) {
throw new Error('Missing blockservice')
}

this.bs = blockService
this.bs = options.blockService

// Support by default dag-pb, dag-cbor, git, and eth-*
this.resolvers = {
Expand Down Expand Up @@ -439,7 +439,7 @@ IPLDResolver.inMemory = function (callback) {
if (err) {
return callback(err)
}
callback(null, new IPLDResolver(blockService))
callback(null, new IPLDResolver({blockService}))
})
}

Expand Down
14 changes: 7 additions & 7 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = (repo) => {
describe('basics', () => {
it('creates an instance', () => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
const r = new IPLDResolver({blockService: bs})
expect(r.bs).to.exist()
})

Expand All @@ -34,7 +34,7 @@ module.exports = (repo) => {
describe('validation', () => {
it('get - errors on unknown resolver', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
const r = new IPLDResolver({blockService: bs})
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(Buffer.from('abcd', 'hex'), 'sha1'))
r.get(cid, '/', {}, (err, result) => {
Expand All @@ -46,7 +46,7 @@ module.exports = (repo) => {

it('_get - errors on unknown resolver', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
const r = new IPLDResolver({blockService: bs})
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(Buffer.from('abcd', 'hex'), 'sha1'))
r.get(cid, (err, result) => {
Expand All @@ -58,7 +58,7 @@ module.exports = (repo) => {

it('put - errors on unknown resolver', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
const r = new IPLDResolver({blockService: bs})
// choosing a format that is not supported
r.put(null, { format: 'base1' }, (err, result) => {
expect(err).to.exist()
Expand All @@ -69,7 +69,7 @@ module.exports = (repo) => {

it('put - errors if no options', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
const r = new IPLDResolver({blockService: bs})
r.put(null, (err, result) => {
expect(err).to.exist()
expect(err.message).to.eql('IPLDResolver.put requires options')
Expand All @@ -79,7 +79,7 @@ module.exports = (repo) => {

it('_put - errors on unknown resolver', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
const r = new IPLDResolver({blockService: bs})
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(Buffer.from('abcd', 'hex'), 'sha1'))
r._put(cid, null, (err, result) => {
Expand All @@ -91,7 +91,7 @@ module.exports = (repo) => {

it('treeStream - errors on unknown resolver', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
const r = new IPLDResolver({blockService: bs})
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(Buffer.from('abcd', 'hex'), 'sha1'))
pull(
Expand Down
2 changes: 1 addition & 1 deletion test/ipld-bitcoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = (repo) => {

before((done) => {
const bs = new BlockService(repo)
resolver = new IPLDResolver(bs)
resolver = new IPLDResolver({blockService: bs})

series([
(cb) => {
Expand Down
2 changes: 1 addition & 1 deletion test/ipld-dag-cbor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = (repo) => {
before((done) => {
const bs = new BlockService(repo)

resolver = new IPLDResolver(bs)
resolver = new IPLDResolver({blockService: bs})

series([
(cb) => {
Expand Down
2 changes: 1 addition & 1 deletion test/ipld-dag-pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = (repo) => {
before((done) => {
const bs = new BlockService(repo)

resolver = new IPLDResolver(bs)
resolver = new IPLDResolver({blockService: bs})

series([
(cb) => {
Expand Down
2 changes: 1 addition & 1 deletion test/ipld-eth-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = (repo) => {

before((done) => {
const bs = new BlockService(repo)
resolver = new IPLDResolver(bs)
resolver = new IPLDResolver({blockService: bs})

series([
(cb) => {
Expand Down
2 changes: 1 addition & 1 deletion test/ipld-eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = (repo) => {
before(function (done) {
this.timeout(10 * 1000)
const bs = new BlockService(repo)
resolver = new IPLDResolver(bs)
resolver = new IPLDResolver({blockService: bs})

async.waterfall([
readFilesFixture,
Expand Down
2 changes: 1 addition & 1 deletion test/ipld-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = (repo) => {
before((done) => {
const bs = new BlockService(repo)

resolver = new IPLDResolver(bs)
resolver = new IPLDResolver({blockService: bs})

series([
(cb) => {
Expand Down
2 changes: 1 addition & 1 deletion test/ipld-zcash.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = (repo) => {

before((done) => {
const bs = new BlockService(repo)
resolver = new IPLDResolver(bs)
resolver = new IPLDResolver({blockService: bs})

series([
(cb) => {
Expand Down

0 comments on commit f1c2e12

Please sign in to comment.