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

Commit

Permalink
fix: allow CID inputs to Object API
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw committed Dec 16, 2018
1 parent 95a163d commit cb1f27d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 30 deletions.
9 changes: 4 additions & 5 deletions src/object/addLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

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

module.exports = (send) => {
return promisify((multihash, dLink, opts, callback) => {
return promisify((cid, dLink, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
Expand All @@ -15,17 +14,17 @@ module.exports = (send) => {
}

try {
multihash = cleanMultihash(multihash, opts)
cid = new CID(cid)
} catch (err) {
return callback(err)
}

send({
path: 'object/patch/add-link',
args: [
multihash,
cid.toString(),
dLink.name,
cleanMultihash(dLink.cid.buffer)
dLink.cid.toString()
]
}, (err, result) => {
if (err) {
Expand Down
7 changes: 3 additions & 4 deletions src/object/appendData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
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 sendOneFile = SendOneFile(send, 'object/patch/append-data')

return promisify((multihash, data, opts, _callback) => {
return promisify((cid, data, opts, _callback) => {
if (typeof opts === 'function') {
_callback = opts
opts = {}
Expand All @@ -20,12 +19,12 @@ module.exports = (send) => {
}

try {
multihash = cleanMultihash(multihash, opts)
cid = new CID(cid)
} catch (err) {
return callback(err)
}

sendOneFile(data, { args: [multihash] }, (err, result) => {
sendOneFile(data, { args: [cid.toString()] }, (err, result) => {
if (err) {
return callback(err)
}
Expand Down
1 change: 0 additions & 1 deletion src/object/get.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 DAGNode = dagPB.DAGNode
const DAGLink = dagPB.DAGLink
const bs58 = require('bs58')
const CID = require('cids')
const LRU = require('lru-cache')
const lruOptions = {
Expand Down
14 changes: 6 additions & 8 deletions src/object/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const promisify = require('promisify-es6')
const dagPB = require('ipld-dag-pb')
const DAGLink = dagPB.DAGLink
const cleanMultihash = require('../utils/clean-multihash')
const CID = require('cids')
const LRU = require('lru-cache')
const lruOptions = {
max: 128
Expand All @@ -12,7 +12,7 @@ const lruOptions = {
const cache = new LRU(lruOptions)

module.exports = (send) => {
return promisify((multihash, options, callback) => {
return promisify((cid, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
Expand All @@ -22,20 +22,20 @@ module.exports = (send) => {
}

try {
multihash = cleanMultihash(multihash, options)
cid = new CID(cid)
} catch (err) {
return callback(err)
}

const node = cache.get(multihash)
const node = cache.get(cid.toString())

if (node) {
return callback(null, node.links)
}

send({
path: 'object/links',
args: multihash
args: cid.toString()
}, (err, result) => {
if (err) {
return callback(err)
Expand All @@ -44,9 +44,7 @@ module.exports = (send) => {
let links = []

if (result.Links) {
links = result.Links.map((l) => {
return new DAGLink(l.Name, l.Size, l.Hash)
})
links = result.Links.map((l) => new DAGLink(l.Name, l.Size, l.Hash))
}
callback(null, links)
})
Expand Down
7 changes: 3 additions & 4 deletions src/object/rmLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

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

module.exports = (send) => {
return promisify((multihash, dLink, opts, callback) => {
return promisify((cid, dLink, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
Expand All @@ -15,15 +14,15 @@ module.exports = (send) => {
}

try {
multihash = cleanMultihash(multihash, opts)
cid = new CID(cid)
} catch (err) {
return callback(err)
}

send({
path: 'object/patch/rm-link',
args: [
multihash,
cid.toString(),
dLink.name
]
}, (err, result) => {
Expand Down
7 changes: 3 additions & 4 deletions src/object/setData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
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 sendOneFile = SendOneFile(send, 'object/patch/set-data')

return promisify((multihash, data, opts, _callback) => {
return promisify((cid, data, opts, _callback) => {
if (typeof opts === 'function') {
_callback = opts
opts = {}
Expand All @@ -20,12 +19,12 @@ module.exports = (send) => {
}

try {
multihash = cleanMultihash(multihash, opts)
cid = new CID(cid)
} catch (err) {
return callback(err)
}

sendOneFile(data, { args: [multihash] }, (err, result) => {
sendOneFile(data, { args: [cid.toString()] }, (err, result) => {
if (err) {
return callback(err)
}
Expand Down
8 changes: 4 additions & 4 deletions src/object/stat.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

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

module.exports = (send) => {
return promisify((multihash, opts, callback) => {
return promisify((cid, opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
Expand All @@ -14,14 +14,14 @@ module.exports = (send) => {
}

try {
multihash = cleanMultihash(multihash, opts)
cid = new CID(cid)
} catch (err) {
return callback(err)
}

send({
path: 'object/stat',
args: multihash
args: cid.toString()
}, callback)
})
}

0 comments on commit cb1f27d

Please sign in to comment.