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

Commit

Permalink
refactor: object API write methods now return CIDs (#896)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Object API refactor.

Object API methods that write DAG nodes now return a CID instead of a DAG node. Affected methods:

* `ipfs.object.new`
* `ipfs.object.patch.addLink`
* `ipfs.object.patch.appendData`
* `ipfs.object.patch.rmLink`
* `ipfs.object.patch.setData`
* `ipfs.object.put`

Example:

```js
// Before
const dagNode = await ipfs.object.new()
```

```js
// After
const cid = await ipfs.object.new() // now returns a CID
const dagNode = await ipfs.object.get(cid) // fetch the DAG node that was created
```

IMPORTANT: `DAGNode` instances, which are part of the IPLD dag-pb format have been refactored.

These instances no longer have `multihash`, `cid` or `serialized` properties.

This effects the following API methods that return these types of objects:

* `ipfs.object.get`
* `ipfs.dag.get`

See ipld/js-ipld-dag-pb#99 for more information.

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw authored Nov 27, 2018
1 parent 315b7f7 commit 38bed14
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 86 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"lodash": "^4.17.11",
"lru-cache": "^4.1.3",
"multiaddr": "^5.0.2",
"multibase": "~0.5.0",
"multibase": "~0.6.0",
"multihashes": "~0.4.14",
"ndjson": "^1.5.0",
"once": "^1.4.0",
Expand Down Expand Up @@ -85,7 +85,7 @@
"eslint-plugin-react": "^7.11.1",
"go-ipfs-dep": "~0.4.18",
"gulp": "^3.9.1",
"interface-ipfs-core": "~0.87.0",
"interface-ipfs-core": "~0.88.0",
"ipfsd-ctl": "~0.40.0",
"nock": "^10.0.2",
"pull-stream": "^3.6.9",
Expand Down
5 changes: 2 additions & 3 deletions src/object/addLink.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

const promisify = require('promisify-es6')
const CID = require('cids')
const cleanMultihash = require('../utils/clean-multihash')

module.exports = (send) => {
const objectGet = require('./get')(send)

return promisify((multihash, dLink, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
Expand All @@ -32,7 +31,7 @@ module.exports = (send) => {
if (err) {
return callback(err)
}
objectGet(result.Hash, { enc: 'base58' }, callback)
callback(null, new CID(result.Hash))
})
})
}
4 changes: 2 additions & 2 deletions src/object/appendData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const promisify = require('promisify-es6')
const once = require('once')
const CID = require('cids')
const cleanMultihash = require('../utils/clean-multihash')
const SendOneFile = require('../utils/send-one-file')

module.exports = (send) => {
const objectGet = require('./get')(send)
const sendOneFile = SendOneFile(send, 'object/patch/append-data')

return promisify((multihash, data, opts, _callback) => {
Expand All @@ -30,7 +30,7 @@ module.exports = (send) => {
return callback(err)
}

objectGet(result.Hash, { enc: 'base58' }, callback)
callback(null, new CID(result.Hash))
})
})
}
3 changes: 1 addition & 2 deletions src/object/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const promisify = require('promisify-es6')
const dagPB = require('ipld-dag-pb')
const DAGLink = dagPB.DAGLink
const cleanMultihash = require('../utils/clean-multihash')
const bs58 = require('bs58')
const LRU = require('lru-cache')
const lruOptions = {
max: 128
Expand Down Expand Up @@ -46,7 +45,7 @@ module.exports = (send) => {

if (result.Links) {
links = result.Links.map((l) => {
return new DAGLink(l.Name, l.Size, Buffer.from(bs58.decode(l.Hash)))
return new DAGLink(l.Name, l.Size, l.Hash)
})
}
callback(null, links)
Expand Down
23 changes: 2 additions & 21 deletions src/object/new.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict'

const promisify = require('promisify-es6')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const Unixfs = require('ipfs-unixfs')
const CID = require('cids')

module.exports = (send) => {
return promisify((template, callback) => {
Expand All @@ -19,24 +17,7 @@ module.exports = (send) => {
return callback(err)
}

let data

if (template) {
if (template !== 'unixfs-dir') {
return callback(new Error('unkown template: ' + template))
}
data = (new Unixfs('directory')).marshal()
} else {
data = Buffer.alloc(0)
}

DAGNode.create(data, (err, node) => {
if (err) {
return callback(err)
}

callback(null, node)
})
callback(null, new CID(result.Hash))
})
})
}
53 changes: 3 additions & 50 deletions src/object/put.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
'use strict'

const promisify = require('promisify-es6')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const LRU = require('lru-cache')
const lruOptions = {
max: 128
}
const CID = require('cids')
const { DAGNode } = require('ipld-dag-pb')

const cache = LRU(lruOptions)
const SendOneFile = require('../utils/send-one-file')
const once = require('once')

Expand Down Expand Up @@ -72,49 +67,7 @@ module.exports = (send) => {
return callback(err) // early
}

if (Buffer.isBuffer(obj)) {
if (!options.enc) {
obj = { Data: obj, Links: [] }
} else if (options.enc === 'json') {
obj = JSON.parse(obj.toString())
}
}

let node

if (DAGNode.isDAGNode(obj)) {
node = obj
} else if (options.enc === 'protobuf') {
dagPB.util.deserialize(obj, (err, _node) => {
if (err) {
return callback(err)
}
node = _node
next()
})
return
} else {
DAGNode.create(Buffer.from(obj.Data), obj.Links, (err, _node) => {
if (err) {
return callback(err)
}
node = _node
next()
})
return
}
next()

function next () {
dagPB.util.cid(node, (err, cid) => {
if (err) {
return callback(err)
}

cache.set(cid.toBaseEncodedString(), node)
callback(null, node)
})
}
callback(null, new CID(result.Hash))
})
})
}
5 changes: 2 additions & 3 deletions src/object/rmLink.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

const promisify = require('promisify-es6')
const CID = require('cids')
const cleanMultihash = require('../utils/clean-multihash')

module.exports = (send) => {
const objectGet = require('./get')(send)

return promisify((multihash, dLink, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
Expand All @@ -31,7 +30,7 @@ module.exports = (send) => {
if (err) {
return callback(err)
}
objectGet(result.Hash, { enc: 'base58' }, callback)
callback(null, new CID(result.Hash))
})
})
}
4 changes: 2 additions & 2 deletions src/object/setData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const promisify = require('promisify-es6')
const once = require('once')
const CID = require('cids')
const cleanMultihash = require('../utils/clean-multihash')
const SendOneFile = require('../utils/send-one-file')

module.exports = (send) => {
const objectGet = require('./get')(send)
const sendOneFile = SendOneFile(send, 'object/patch/set-data')

return promisify((multihash, data, opts, _callback) => {
Expand All @@ -29,7 +29,7 @@ module.exports = (send) => {
if (err) {
return callback(err)
}
objectGet(result.Hash, { enc: 'base58' }, callback)
callback(null, new CID(result.Hash))
})
})
}
2 changes: 1 addition & 1 deletion test/log.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('.log', function () {

it('.log.tail', (done) => {
let i = setInterval(() => {
ipfs.files.add(Buffer.from('just adding some data to generate logs'))
ipfs.add(Buffer.from('just adding some data to generate logs'))
}, 1000)

const req = ipfs.log.tail((err, res) => {
Expand Down

0 comments on commit 38bed14

Please sign in to comment.