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
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 <[email protected]>
  • Loading branch information
Alan Shaw committed Nov 27, 2018
1 parent 315b7f7 commit 251762e
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 81 deletions.
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))
})
})
}
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))
})
})
}

0 comments on commit 251762e

Please sign in to comment.