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

Commit a51e761

Browse files
author
Alan Shaw
committed
feat: add cidBase option to resolve
License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent 008e353 commit a51e761

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/resolve.js

+36-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
'use strict'
22

33
const promisify = require('promisify-es6')
4-
5-
const transform = function (res, callback) {
6-
callback(null, res.Path)
7-
}
4+
const multibase = require('multibase')
5+
const CID = require('cids')
86

97
module.exports = (send) => {
108
return promisify((args, opts, callback) => {
@@ -13,6 +11,40 @@ module.exports = (send) => {
1311
opts = {}
1412
}
1513

14+
opts = opts || {}
15+
16+
if (opts.cidBase) {
17+
opts['cid-base'] = opts.cidBase
18+
delete opts.cidBase
19+
}
20+
21+
const transform = (res, callback) => {
22+
if (!opts['cid-base']) {
23+
return callback(null, res.Path)
24+
}
25+
26+
// FIXME: remove when go-ipfs supports ?cid-base for /api/v0/resolve
27+
// https://github.com/ipfs/go-ipfs/pull/5777#issuecomment-439838555
28+
const parts = res.Path.split('/') // ['', 'ipfs', 'QmHash', ...]
29+
30+
if (multibase.isEncoded(parts[2]) !== opts['cid-base']) {
31+
try {
32+
let cid = new CID(parts[2])
33+
34+
if (cid.version === 0 && opts['cid-base'] !== 'base58btc') {
35+
cid = cid.toV1()
36+
}
37+
38+
parts[2] = cid.toBaseEncodedString(opts['cid-base'])
39+
res.Path = parts.join('/')
40+
} catch (err) {
41+
return callback(err)
42+
}
43+
}
44+
45+
callback(null, res.Path)
46+
}
47+
1648
send.andTransform({
1749
path: 'resolve',
1850
args: args,

0 commit comments

Comments
 (0)