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

Commit

Permalink
feat: add cidBase option to resolve (#893)
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 authored Dec 16, 2018
1 parent d46fcc9 commit ec6285d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"eslint-plugin-react": "^7.11.1",
"go-ipfs-dep": "~0.4.18",
"gulp": "^3.9.1",
"interface-ipfs-core": "~0.93.0",
"interface-ipfs-core": "~0.94.0",
"ipfsd-ctl": "~0.40.0",
"nock": "^10.0.2",
"pull-stream": "^3.6.9",
Expand Down
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
3 changes: 1 addition & 2 deletions 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 Expand Up @@ -53,7 +52,7 @@ module.exports = (send) => {
result.Data = Buffer.from(result.Data, 'base64')

const 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)
})

DAGNode.create(result.Data, links, (err, node) => {
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)
})
}
40 changes: 36 additions & 4 deletions src/resolve.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'

const promisify = require('promisify-es6')

const transform = function (res, callback) {
callback(null, res.Path)
}
const multibase = require('multibase')
const CID = require('cids')

module.exports = (send) => {
return promisify((args, opts, callback) => {
Expand All @@ -13,6 +11,40 @@ module.exports = (send) => {
opts = {}
}

opts = opts || {}

if (opts.cidBase) {
opts['cid-base'] = opts.cidBase
delete opts.cidBase
}

const transform = (res, callback) => {
if (!opts['cid-base']) {
return callback(null, res.Path)
}

// FIXME: remove when go-ipfs supports ?cid-base for /api/v0/resolve
// https://github.com/ipfs/go-ipfs/pull/5777#issuecomment-439838555
const parts = res.Path.split('/') // ['', 'ipfs', 'QmHash', ...]

if (multibase.isEncoded(parts[2]) !== opts['cid-base']) {
try {
let cid = new CID(parts[2])

if (cid.version === 0 && opts['cid-base'] !== 'base58btc') {
cid = cid.toV1()
}

parts[2] = cid.toBaseEncodedString(opts['cid-base'])
res.Path = parts.join('/')
} catch (err) {
return callback(err)
}
}

callback(null, res.Path)
}

send.andTransform({
path: 'resolve',
args: args,
Expand Down

0 comments on commit ec6285d

Please sign in to comment.