Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit bdc6f4d

Browse files
author
Alan Shaw
committed
refactor: object APIs write methods now return CIDs
For the back story on this change, please see: ipfs-inactive/interface-js-ipfs-core#388 (review) BREAKING CHANGE: Object API refactor. Object API methods that write DAG nodes now return a [CID](https://www.npmjs.com/package/cids) 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]>
1 parent 5237dd9 commit bdc6f4d

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/core/components/object.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module.exports = function object (self) {
9898
self._preload(cid)
9999
}
100100

101-
cb(null, node)
101+
cb(null, cid)
102102
})
103103
})
104104
}
@@ -150,7 +150,7 @@ module.exports = function object (self) {
150150
self._preload(cid)
151151
}
152152

153-
callback(null, node)
153+
callback(null, cid)
154154
})
155155
})
156156
}),
@@ -209,9 +209,11 @@ module.exports = function object (self) {
209209
return callback(err)
210210
}
211211

212-
self.object.get(cid, {
213-
preload: options.preload
214-
}, callback)
212+
if (options.preload !== false) {
213+
self._preload(cid)
214+
}
215+
216+
callback(null, cid)
215217
})
216218
}
217219
}),

src/http/api/resources/object.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ exports.new = (request, reply) => {
3737

3838
waterfall([
3939
(cb) => ipfs.object.new(template, cb),
40-
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
40+
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid }))
4141
], (err, results) => {
4242
if (err) {
4343
log.error(err)
@@ -200,8 +200,7 @@ exports.put = {
200200

201201
waterfall([
202202
(cb) => DAGNode.create(Buffer.from(node.Data), node.Links, cb),
203-
(node, cb) => ipfs.object.put(node, cb),
204-
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { cid, node }))
203+
(node, cb) => ipfs.object.put(node, (err, cid) => cb(err, { cid, node }))
205204
], (err, results) => {
206205
if (err) {
207206
log.error(err)
@@ -366,7 +365,7 @@ exports.patchAppendData = {
366365

367366
waterfall([
368367
(cb) => ipfs.object.patch.appendData(key, data, cb),
369-
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
368+
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid }))
370369
], (err, results) => {
371370
if (err) {
372371
log.error(err)
@@ -409,7 +408,7 @@ exports.patchSetData = {
409408

410409
waterfall([
411410
(cb) => ipfs.object.patch.setData(key, data, cb),
412-
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
411+
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid }))
413412
], (err, results) => {
414413
if (err) {
415414
log.error(err)
@@ -477,7 +476,7 @@ exports.patchAddLink = {
477476
waterfall([
478477
(cb) => ipfs.object.get(ref, cb),
479478
(node, cb) => ipfs.object.patch.addLink(root, new DAGLink(name, node.size, ref), cb),
480-
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
479+
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid }))
481480
], (err, results) => {
482481
if (err) {
483482
log.error(err)
@@ -544,7 +543,7 @@ exports.patchRmLink = {
544543

545544
waterfall([
546545
(cb) => ipfs.object.patch.rmLink(root, { name: link }, cb),
547-
(node, cb) => dagPB.util.cid(node, (err, cid) => cb(err, { node, cid }))
546+
(cid, cb) => ipfs.object.get(cid, (err, node) => cb(err, { node, cid }))
548547
], (err, results) => {
549548
if (err) {
550549
log.error(err)

0 commit comments

Comments
 (0)