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

Commit 5ee75a6

Browse files
committed
feat: adds --cid-base argument to stringify cids in different bases
License: MIT Signed-off-by: achingbrain <[email protected]>
1 parent 258d7b2 commit 5ee75a6

File tree

11 files changed

+110
-19
lines changed

11 files changed

+110
-19
lines changed

src/cli/ls.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@ module.exports = {
2020
default: false,
2121
coerce: asBoolean,
2222
describe: 'Use long listing format.'
23+
},
24+
cidBase: {
25+
alias: 'cid-base',
26+
default: 'base58btc',
27+
describe: 'CID base to use.'
2328
}
2429
},
2530

2631
handler (argv) {
2732
let {
2833
path,
2934
ipfs,
30-
long
35+
long,
36+
cidBase
3137
} = argv
3238

3339
argv.resolve(
3440
ipfs.files.ls(path || FILE_SEPARATOR, {
35-
long
41+
long,
42+
cidBase
3643
})
3744
.then(files => {
3845
if (long) {

src/cli/stat.js

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ Type: <type>`,
4141
default: false,
4242
coerce: asBoolean,
4343
describe: 'Compute the amount of the dag that is local, and if possible the total size'
44+
},
45+
cidBase: {
46+
alias: 'cid-base',
47+
default: 'base58btc',
48+
describe: 'CID base to use.'
4449
}
4550
},
4651

src/core/ls.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
const waterfall = require('async/waterfall')
44
const map = require('async/map')
5-
const bs58 = require('bs58')
65
const UnixFs = require('ipfs-unixfs')
76
const {
87
traverseTo,
98
loadNode,
9+
formatCid,
1010
FILE_SEPARATOR,
1111
FILE_TYPES
1212
} = require('./utils')
1313

1414
const defaultOptions = {
15-
long: false
15+
long: false,
16+
cidBase: 'base58btc'
1617
}
1718

1819
module.exports = (ipfs) => {
@@ -47,7 +48,7 @@ module.exports = (ipfs) => {
4748
done(null, {
4849
name: link.name,
4950
type: meta.type,
50-
hash: bs58.encode(node.multihash),
51+
hash: formatCid(node.multihash, options.cidBase),
5152
size: meta.fileSize() || 0
5253
})
5354
}
@@ -57,7 +58,7 @@ module.exports = (ipfs) => {
5758
cb(null, [{
5859
name: result.name,
5960
type: meta.type,
60-
hash: bs58.encode(result.node.multihash),
61+
hash: formatCid(result.node.multihash, options.cidBase),
6162
size: meta.fileSize() || 0
6263
}])
6364
}

src/core/stat.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
'use strict'
22

33
const unmarshal = require('ipfs-unixfs').unmarshal
4-
const bs58 = require('bs58')
54
const {
6-
traverseTo
5+
traverseTo,
6+
formatCid
77
} = require('./utils')
88
const waterfall = require('async/waterfall')
99
const log = require('debug')('ipfs:mfs:stat')
1010

1111
const defaultOptions = {
1212
hash: false,
1313
size: false,
14-
withLocal: false
14+
withLocal: false,
15+
cidBase: 'base58btc'
1516
}
1617

1718
module.exports = (ipfs) => {
@@ -32,7 +33,7 @@ module.exports = (ipfs) => {
3233
({ node }, done) => {
3334
if (options.hash) {
3435
return done(null, {
35-
hash: bs58.encode(node.multihash)
36+
hash: formatCid(node.multihash, options.cidBase)
3637
})
3738
} else if (options.size) {
3839
return done(null, {
@@ -49,7 +50,7 @@ module.exports = (ipfs) => {
4950
}
5051

5152
done(null, {
52-
hash: bs58.encode(node.multihash),
53+
hash: formatCid(node.multihash, options.cidBase),
5354
size: meta.fileSize() || 0,
5455
cumulativeSize: node.size,
5556
blocks: blocks,

src/core/utils/format-cid.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const CID = require('cids')
4+
5+
module.exports = (cid, base) => {
6+
if (Buffer.isBuffer(cid)) {
7+
cid = new CID(cid)
8+
}
9+
10+
if (base === 'base58btc') {
11+
return cid.toBaseEncodedString()
12+
}
13+
14+
return cid.toV1().toBaseEncodedString(base)
15+
}

src/core/utils/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
createLock: require('./create-lock'),
1010
createNode: require('./create-node'),
1111
endPullStream: require('./end-pull-stream'),
12+
formatCid: require('./format-cid'),
1213
limitStreamBytes: require('./limit-stream-bytes'),
1314
loadNode: require('./load-node'),
1415
toSourcesAndDestination: require('./to-sources-and-destination'),

src/http/ls.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ const mfsLs = (api) => {
1313
} = request.server.app
1414
const {
1515
arg,
16-
long
16+
long,
17+
cidBase
1718
} = request.query
1819

1920
return ipfs.files.ls(arg, {
20-
long
21+
long,
22+
cidBase
2123
})
2224
.then(files => {
2325
reply({
@@ -44,7 +46,8 @@ const mfsLs = (api) => {
4446
},
4547
query: Joi.object().keys({
4648
arg: Joi.string().default('/'),
47-
long: Joi.boolean().default(false)
49+
long: Joi.boolean().default(false),
50+
cidBase: Joi.string().default('base58btc')
4851
})
4952
.rename('l', 'long', {
5053
override: true,

src/http/stat.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ const mfsStat = (api) => {
1515
arg,
1616
hash,
1717
size,
18-
withLocal
18+
withLocal,
19+
cidBase
1920
} = request.query
2021

2122
return ipfs.files.stat(arg, {
2223
hash,
2324
size,
24-
withLocal
25+
withLocal,
26+
cidBase
2527
})
2628
.then(stats => {
2729
reply({
@@ -52,7 +54,8 @@ const mfsStat = (api) => {
5254
arg: Joi.string().default('/'),
5355
hash: Joi.boolean().default(false),
5456
size: Joi.boolean().default(false),
55-
withLocal: Joi.boolean().default(false)
57+
withLocal: Joi.boolean().default(false),
58+
cidBase: Joi.string().default('base58btc')
5659
})
5760
}
5861
}

test/helpers/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ module.exports = {
4040
createMfs,
4141
bufferStream: require('./buffer-stream'),
4242
collectLeafCids: require('./collect-leaf-cids'),
43-
EMPTY_DIRECTORY_HASH: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
43+
EMPTY_DIRECTORY_HASH: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
44+
EMPTY_DIRECTORY_HASH_BASE32: 'bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354'
4445
}

test/ls.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ describe('ls', function () {
139139
})
140140
})
141141

142+
it('lists a file with a base32 hash', () => {
143+
const fileName = `small-file-${Math.random()}.txt`
144+
const content = Buffer.from('Hello world')
145+
146+
return mfs.write(`/${fileName}`, content, {
147+
create: true
148+
})
149+
.then(() => mfs.ls(`/${fileName}`, {
150+
long: true,
151+
cidBase: 'base32'
152+
}))
153+
.then(files => {
154+
expect(files.length).to.equal(1)
155+
expect(files[0].name).to.equal(fileName)
156+
expect(files[0].type).to.equal(FILE_TYPES.file)
157+
expect(files[0].size).to.equal(content.length)
158+
expect(files[0].hash.startsWith('b')).to.equal(true)
159+
})
160+
})
161+
142162
it('fails to list non-existent file', () => {
143163
return mfs.ls('/i-do-not-exist')
144164
.then(() => {

test/stat.spec.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const loadFixture = require('aegir/fixtures')
99

1010
const {
1111
createMfs,
12-
EMPTY_DIRECTORY_HASH
12+
EMPTY_DIRECTORY_HASH,
13+
EMPTY_DIRECTORY_HASH_BASE32
1314
} = require('./helpers')
1415

1516
describe('stat', function () {
@@ -86,6 +87,20 @@ describe('stat', function () {
8687
})
8788
})
8889

90+
it('returns only a base32 hash', () => {
91+
const path = `/directory-${Math.random()}`
92+
93+
return mfs.mkdir(path)
94+
.then(() => mfs.stat(path, {
95+
hash: true,
96+
cidBase: 'base32'
97+
}))
98+
.then(stats => {
99+
expect(Object.keys(stats).length).to.equal(1)
100+
expect(stats.hash).to.equal(EMPTY_DIRECTORY_HASH_BASE32)
101+
})
102+
})
103+
89104
it('returns only the size', () => {
90105
const path = `/directory-${Math.random()}`
91106

@@ -134,4 +149,23 @@ describe('stat', function () {
134149
expect(stats.type).to.equal('file')
135150
})
136151
})
152+
153+
it('stats a large file with base32', () => {
154+
const filePath = '/stat/large-file.txt'
155+
156+
return mfs.write(filePath, largeFile, {
157+
create: true,
158+
parents: true
159+
})
160+
.then(() => mfs.stat(filePath, {
161+
cidBase: 'base32'
162+
}))
163+
.then((stats) => {
164+
expect(stats.hash.startsWith('b')).to.equal(true)
165+
expect(stats.size).to.equal(largeFile.length)
166+
expect(stats.cumulativeSize).to.equal(490800)
167+
expect(stats.blocks).to.equal(2)
168+
expect(stats.type).to.equal('file')
169+
})
170+
})
137171
})

0 commit comments

Comments
 (0)