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

Commit f29115d

Browse files
author
Alan Shaw
committed
refactor: back out of core accepting cidBase
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent e804933 commit f29115d

File tree

18 files changed

+82
-130
lines changed

18 files changed

+82
-130
lines changed

src/cli/commands/bitswap/stat.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multibase = require('multibase')
44
const { print } = require('../../utils')
5+
const { cidToString } = require('../../../utils/cid')
56

67
module.exports = {
78
command: 'stat',
@@ -17,12 +18,12 @@ module.exports = {
1718
},
1819

1920
handler ({ ipfs, cidBase }) {
20-
ipfs.bitswap.stat({ cidBase }, (err, stats) => {
21+
ipfs.bitswap.stat((err, stats) => {
2122
if (err) {
2223
throw err
2324
}
2425

25-
stats.wantlist = stats.wantlist || []
26+
stats.wantlist = stats.wantlist.Keys.map(cid => cidToString(cid, cidBase))
2627
stats.peers = stats.peers || []
2728

2829
print(`bitswap status

src/cli/commands/bitswap/wantlist.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multibase = require('multibase')
44
const { print } = require('../../utils')
5+
const { cidToString } = require('../../../utils/cid')
56

67
module.exports = {
78
command: 'wantlist [peer]',
@@ -22,11 +23,11 @@ module.exports = {
2223
},
2324

2425
handler ({ ipfs, peer, cidBase }) {
25-
ipfs.bitswap.wantlist(peer, { cidBase }, (err, cids) => {
26+
ipfs.bitswap.wantlist(peer, (err, cids) => {
2627
if (err) {
2728
throw err
2829
}
29-
cids.forEach((cid) => print(cid))
30+
cids.forEach((cid) => print(cidToString(cid, cidBase)))
3031
})
3132
}
3233
}

src/cli/commands/block/stat.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multibase = require('multibase')
44
const { print } = require('../../utils')
5+
const { cidToString } = require('../../../utils/cid')
56

67
module.exports = {
78
command: 'stat <key>',
@@ -17,12 +18,12 @@ module.exports = {
1718
},
1819

1920
handler ({ ipfs, key, cidBase }) {
20-
ipfs.block.stat(key, { cidBase }, (err, stats) => {
21+
ipfs.block.stat(key, (err, stats) => {
2122
if (err) {
2223
throw err
2324
}
2425

25-
print('Key: ' + stats.key)
26+
print('Key: ' + cidToString(stats.key, cidBase))
2627
print('Size: ' + stats.size)
2728
})
2829
}

src/cli/commands/files/add.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const waterfall = require('async/waterfall')
1313
const mh = require('multihashes')
1414
const multibase = require('multibase')
1515
const { print, isDaemonOn, createProgressBar } = require('../../utils')
16+
const { cidToString } = require('../../../utils/cid')
1617

1718
function checkPath (inPath, recursive) {
1819
// This function is to check for the following possible inputs
@@ -89,7 +90,7 @@ function addPipeline (index, addStream, list, argv) {
8990
sortBy(added, 'path')
9091
.reverse()
9192
.map((file) => {
92-
const log = [ 'added', file.hash ]
93+
const log = [ 'added', cidToString(file.hash, argv.cidBase) ]
9394

9495
if (!quiet && file.path.length > 0) log.push(file.path)
9596

@@ -198,7 +199,6 @@ module.exports = {
198199
? argv.shardSplitThreshold
199200
: Infinity,
200201
cidVersion: argv.cidVersion,
201-
cidBase: argv.cidBase,
202202
rawLeaves: argv.rawLeaves,
203203
onlyHash: argv.onlyHash,
204204
hashAlg: argv.hash,

src/cli/commands/object/stat.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multibase = require('multibase')
44
const print = require('../../utils').print
5+
const { cidToString } = require('../../../utils/cid')
56

67
module.exports = {
78
command: 'stat <key>',
@@ -17,15 +18,15 @@ module.exports = {
1718
},
1819

1920
handler ({ ipfs, key, cidBase }) {
20-
ipfs.object.stat(key, { enc: 'base58', cidBase }, (err, stats) => {
21+
ipfs.object.stat(key, { enc: 'base58' }, (err, stats) => {
2122
if (err) {
2223
throw err
2324
}
2425

2526
delete stats.Hash // only for js-ipfs-api output
2627

2728
Object.keys(stats).forEach((key) => {
28-
print(`${key}: ${stats[key]}`)
29+
print(`${key}: ${cidToString(stats[key], cidBase)}`)
2930
})
3031
})
3132
}

src/cli/commands/pin/add.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multibase = require('multibase')
44
const { print } = require('../../utils')
5+
const { cidToString } = require('../../../utils/cid')
56

67
module.exports = {
78
command: 'add <ipfsPath...>',
@@ -25,10 +26,10 @@ module.exports = {
2526
handler ({ ipfs, ipfsPath, recursive, cidBase }) {
2627
const type = recursive ? 'recursive' : 'direct'
2728

28-
ipfs.pin.add(ipfsPath, { recursive, cidBase }, (err, results) => {
29+
ipfs.pin.add(ipfsPath, { recursive }, (err, results) => {
2930
if (err) { throw err }
3031
results.forEach((res) => {
31-
print(`pinned ${res.hash} ${type}ly`)
32+
print(`pinned ${cidToString(res.hash, cidBase)} ${type}ly`)
3233
})
3334
})
3435
}

src/cli/commands/pin/ls.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multibase = require('multibase')
44
const { print } = require('../../utils')
5+
const { cidToString } = require('../../../utils/cid')
56

67
module.exports = {
78
// bracket syntax with '...' tells yargs to optionally accept a list
@@ -33,10 +34,10 @@ module.exports = {
3334
handler: ({ ipfs, ipfsPath, type, quiet, cidBase }) => {
3435
const paths = ipfsPath
3536

36-
ipfs.pin.ls(paths, { type, cidBase }, (err, results) => {
37+
ipfs.pin.ls(paths, { type }, (err, results) => {
3738
if (err) { throw err }
3839
results.forEach((res) => {
39-
let line = res.hash
40+
let line = cidToString(res.hash, cidBase)
4041
if (!quiet) {
4142
line += ` ${res.type}`
4243
}

src/cli/commands/pin/rm.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const multibase = require('multibase')
44
const { print } = require('../../utils')
5+
const { cidToString } = require('../../../utils/cid')
56

67
module.exports = {
78
command: 'rm <ipfsPath...>',
@@ -23,10 +24,10 @@ module.exports = {
2324
},
2425

2526
handler: ({ ipfs, ipfsPath, recursive, cidBase }) => {
26-
ipfs.pin.rm(ipfsPath, { recursive, cidBase }, (err, results) => {
27+
ipfs.pin.rm(ipfsPath, { recursive }, (err, results) => {
2728
if (err) { throw err }
2829
results.forEach((res) => {
29-
print(`unpinned ${res.hash}`)
30+
print(`unpinned ${cidToString(res.hash, cidBase)}`)
3031
})
3132
})
3233
}

src/core/components/bitswap.js

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,23 @@ const Big = require('big.js')
77
const CID = require('cids')
88
const PeerId = require('peer-id')
99
const errCode = require('err-code')
10-
const multibase = require('multibase')
11-
const { cidToString } = require('../../utils/cid')
1210

1311
function formatWantlist (list, cidBase) {
14-
return Array.from(list).map((e) => cidToString(e[1].cid, cidBase))
12+
return Array.from(list).map((e) => ({ '/': e[1].cid.toBaseEncodedString() }))
1513
}
1614

1715
module.exports = function bitswap (self) {
1816
return {
19-
wantlist: promisify((peerId, options, callback) => {
17+
wantlist: promisify((peerId, callback) => {
2018
if (typeof peerId === 'function') {
2119
callback = peerId
22-
options = {}
2320
peerId = null
24-
} else if (typeof options === 'function') {
25-
callback = options
26-
options = {}
2721
}
2822

29-
options = options || {}
30-
3123
if (!self.isOnline()) {
3224
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
3325
}
3426

35-
if (options.cidBase && !multibase.names.includes(options.cidBase)) {
36-
return setImmediate(() => {
37-
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
38-
})
39-
}
40-
4127
let list
4228
if (peerId) {
4329
try {
@@ -53,34 +39,21 @@ module.exports = function bitswap (self) {
5339
list = self._bitswap.getWantlist()
5440
}
5541

56-
setImmediate(() => callback(null, formatWantlist(list, options.cidBase)))
42+
setImmediate(() => callback(null, formatWantlist(list)))
5743
}),
5844

59-
stat: promisify((options, callback) => {
60-
if (typeof options === 'function') {
61-
callback = options
62-
options = {}
63-
}
64-
65-
options = options || {}
66-
45+
stat: promisify((callback) => {
6746
if (!self.isOnline()) {
6847
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
6948
}
7049

71-
if (options.cidBase && !multibase.names.includes(options.cidBase)) {
72-
return setImmediate(() => {
73-
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
74-
})
75-
}
76-
7750
const snapshot = self._bitswap.stat().snapshot
7851

7952
setImmediate(() => {
8053
callback(null, {
8154
provideBufLen: parseInt(snapshot.providesBufferLength.toString()),
8255
blocksReceived: new Big(snapshot.blocksReceived),
83-
wantlist: formatWantlist(self._bitswap.getWantlist(), options.cidBase),
56+
wantlist: formatWantlist(self._bitswap.getWantlist()),
8457
peers: self._bitswap.peers().map((id) => id.toB58String()),
8558
dupBlksReceived: new Big(snapshot.dupBlksReceived),
8659
dupDataReceived: new Big(snapshot.dupDataReceived),

src/core/components/block.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ const waterfall = require('async/waterfall')
77
const setImmediate = require('async/setImmediate')
88
const promisify = require('promisify-es6')
99
const errCode = require('err-code')
10-
const multibase = require('multibase')
11-
const { cidToString } = require('../../utils/cid')
1210

1311
module.exports = function block (self) {
1412
return {
@@ -118,12 +116,6 @@ module.exports = function block (self) {
118116
return setImmediate(() => callback(errCode(err, 'ERR_INVALID_CID')))
119117
}
120118

121-
if (options.cidBase && !multibase.names.includes(options.cidBase)) {
122-
return setImmediate(() => {
123-
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
124-
})
125-
}
126-
127119
if (options.preload !== false) {
128120
self._preload(cid)
129121
}
@@ -133,7 +125,7 @@ module.exports = function block (self) {
133125
return callback(err)
134126
}
135127
callback(null, {
136-
key: cidToString(cid, options.cidBase),
128+
key: cid.toString(),
137129
size: block.data.length
138130
})
139131
})

src/core/components/files.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ const pushable = require('pull-pushable')
1010
const toStream = require('pull-stream-to-stream')
1111
const toPull = require('stream-to-pull-stream')
1212
const deferred = require('pull-defer')
13+
const waterfall = require('async/waterfall')
1314
const isStream = require('is-stream')
1415
const isSource = require('is-pull-stream').isSource
1516
const Duplex = require('readable-stream').Duplex
1617
const OtherBuffer = require('buffer').Buffer
1718
const CID = require('cids')
1819
const toB58String = require('multihashes').toB58String
1920
const errCode = require('err-code')
20-
const multibase = require('multibase')
2121
const parseChunkerString = require('../utils').parseChunkerString
22-
const { cidToString } = require('../../utils/cid')
2322

2423
const WRAPPER = 'wrapper/'
2524

2625
function noop () {}
2726

28-
function prepareFile (file, opts) {
27+
function prepareFile (file, self, opts, callback) {
2928
opts = opts || {}
3029

3130
let cid = new CID(file.multihash)
@@ -34,15 +33,28 @@ function prepareFile (file, opts) {
3433
cid = cid.toV1()
3534
}
3635

37-
const cidStr = cidToString(cid, opts.cidBase)
36+
waterfall([
37+
(cb) => opts.onlyHash
38+
? cb(null, file)
39+
: self.object.get(file.multihash, Object.assign({}, opts, { preload: false }), cb),
40+
(node, cb) => {
41+
const b58Hash = cid.toBaseEncodedString()
3842

39-
return {
40-
path: opts.wrapWithDirectory
41-
? file.path.substring(WRAPPER.length)
42-
: (file.path || cidStr),
43-
hash: cidStr,
44-
size: file.size
45-
}
43+
let size = node.size
44+
45+
if (Buffer.isBuffer(node)) {
46+
size = node.length
47+
}
48+
49+
cb(null, {
50+
path: opts.wrapWithDirectory
51+
? file.path.substring(WRAPPER.length)
52+
: (file.path || b58Hash),
53+
hash: b58Hash,
54+
size
55+
})
56+
}
57+
], callback)
4658
}
4759

4860
function normalizeContent (content, opts) {
@@ -161,12 +173,6 @@ module.exports = function files (self) {
161173
: Infinity
162174
}, options, chunkerOptions)
163175

164-
if (opts.cidBase && !multibase.names.includes(opts.cidBase)) {
165-
return pull.map(() => {
166-
throw errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE')
167-
})
168-
}
169-
170176
if (opts.hashAlg && opts.cidVersion !== 1) {
171177
opts.cidVersion = 1
172178
}
@@ -184,7 +190,7 @@ module.exports = function files (self) {
184190
pull.map(content => normalizeContent(content, opts)),
185191
pull.flatten(),
186192
importer(self._ipld, opts),
187-
pull.map(file => prepareFile(file, opts)),
193+
pull.asyncMap((file, cb) => prepareFile(file, self, opts, cb)),
188194
pull.map(file => preloadFile(file, self, opts)),
189195
pull.asyncMap((file, cb) => pinFile(file, self, opts, cb))
190196
)
@@ -241,10 +247,6 @@ module.exports = function files (self) {
241247
const maxDepth = recursive ? global.Infinity : pathDepth
242248
options.maxDepth = options.maxDepth || maxDepth
243249

244-
if (options.cidBase && !multibase.names.includes(options.cidBase)) {
245-
return pull.error(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
246-
}
247-
248250
if (options.preload !== false) {
249251
self._preload(pathComponents[0])
250252
}
@@ -255,7 +257,8 @@ module.exports = function files (self) {
255257
recursive ? node.depth >= pathDepth : node.depth === pathDepth
256258
),
257259
pull.map(node => {
258-
node = Object.assign({}, node, { hash: cidToString(node.hash, options.cidBase) })
260+
const cid = new CID(node.hash)
261+
node = Object.assign({}, node, { hash: cid.toBaseEncodedString() })
259262
delete node.content
260263
return node
261264
})

0 commit comments

Comments
 (0)