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

refactor: object API write methods now return CIDs #896

Merged
merged 2 commits into from
Nov 27, 2018
Merged
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
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